org.el 586 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773
  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. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2318. Use customize to modify this, or restart Emacs after changing it."
  2319. :group 'org-font-lock
  2320. :set 'org-set-emph-re
  2321. :type '(repeat
  2322. (list
  2323. (string :tag "Marker character")
  2324. (choice
  2325. (face :tag "Font-lock-face")
  2326. (plist :tag "Face property list"))
  2327. (string :tag "HTML start tag")
  2328. (string :tag "HTML end tag")
  2329. (option (const verbatim)))))
  2330. ;;; Miscellaneous options
  2331. (defgroup org-completion nil
  2332. "Completion in Org-mode."
  2333. :tag "Org Completion"
  2334. :group 'org)
  2335. (defcustom org-completion-use-ido nil
  2336. "Non-nil means, use ido completion wherever possible.
  2337. Note that `ido-mode' must be active for this variable to be relevant.
  2338. If you decide to turn this variable on, you might well want to turn off
  2339. `org-outline-path-complete-in-steps'."
  2340. :group 'org-completion
  2341. :type 'boolean)
  2342. (defcustom org-completion-fallback-command 'hippie-expand
  2343. "The expansion command called by \\[org-complete] in normal context.
  2344. Normal means, no org-mode-specific context."
  2345. :group 'org-completion
  2346. :type 'function)
  2347. ;;; Functions and variables from ther packages
  2348. ;; Declared here to avoid compiler warnings
  2349. ;; XEmacs only
  2350. (defvar outline-mode-menu-heading)
  2351. (defvar outline-mode-menu-show)
  2352. (defvar outline-mode-menu-hide)
  2353. (defvar zmacs-regions) ; XEmacs regions
  2354. ;; Emacs only
  2355. (defvar mark-active)
  2356. ;; Various packages
  2357. (declare-function find-library-name "find-func" (library))
  2358. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2359. (declare-function calendar-forward-day "cal-move" (arg))
  2360. (declare-function calendar-goto-date "cal-move" (date))
  2361. (declare-function calendar-goto-today "cal-move" ())
  2362. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2363. (defvar calc-embedded-close-formula)
  2364. (defvar calc-embedded-open-formula)
  2365. (declare-function cdlatex-tab "ext:cdlatex" ())
  2366. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2367. (defvar font-lock-unfontify-region-function)
  2368. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2369. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2370. (defvar iswitchb-temp-buflist)
  2371. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2372. (defvar org-agenda-tags-todo-honor-ignore-options)
  2373. (declare-function org-agenda-skip "org-agenda" ())
  2374. (declare-function org-format-agenda-item "org-agenda"
  2375. (extra txt &optional category tags dotime noprefix remove-re))
  2376. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2377. (declare-function org-agenda-change-all-lines "org-agenda"
  2378. (newhead hdmarker &optional fixface just-this))
  2379. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2380. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2381. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2382. (beg end))
  2383. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2384. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2385. "org-agenda" (&optional end))
  2386. (declare-function parse-time-string "parse-time" (string))
  2387. (declare-function remember "remember" (&optional initial))
  2388. (declare-function remember-buffer-desc "remember" ())
  2389. (declare-function remember-finalize "remember" ())
  2390. (defvar remember-save-after-remembering)
  2391. (defvar remember-data-file)
  2392. (defvar remember-register)
  2393. (defvar remember-buffer)
  2394. (defvar remember-handler-functions)
  2395. (defvar remember-annotation-functions)
  2396. (defvar texmathp-why)
  2397. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2398. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2399. (defvar w3m-current-url)
  2400. (defvar w3m-current-title)
  2401. (defvar org-latex-regexps)
  2402. ;;; Autoload and prepare some org modules
  2403. ;; Some table stuff that needs to be defined here, because it is used
  2404. ;; by the functions setting up org-mode or checking for table context.
  2405. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2406. "Detects an org-type or table-type table.")
  2407. (defconst org-table-line-regexp "^[ \t]*|"
  2408. "Detects an org-type table line.")
  2409. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2410. "Detects an org-type table line.")
  2411. (defconst org-table-hline-regexp "^[ \t]*|-"
  2412. "Detects an org-type table hline.")
  2413. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2414. "Detects a table-type table hline.")
  2415. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2416. "Searching from within a table (any type) this finds the first line
  2417. outside the table.")
  2418. ;; Autoload the functions in org-table.el that are needed by functions here.
  2419. (eval-and-compile
  2420. (org-autoload "org-table"
  2421. '(org-table-align org-table-begin org-table-blank-field
  2422. org-table-convert org-table-convert-region org-table-copy-down
  2423. org-table-copy-region org-table-create
  2424. org-table-create-or-convert-from-region
  2425. org-table-create-with-table.el org-table-current-dline
  2426. org-table-cut-region org-table-delete-column org-table-edit-field
  2427. org-table-edit-formulas org-table-end org-table-eval-formula
  2428. org-table-export org-table-field-info
  2429. org-table-get-stored-formulas org-table-goto-column
  2430. org-table-hline-and-move org-table-import org-table-insert-column
  2431. org-table-insert-hline org-table-insert-row org-table-iterate
  2432. org-table-justify-field-maybe org-table-kill-row
  2433. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2434. org-table-move-column org-table-move-column-left
  2435. org-table-move-column-right org-table-move-row
  2436. org-table-move-row-down org-table-move-row-up
  2437. org-table-next-field org-table-next-row org-table-paste-rectangle
  2438. org-table-previous-field org-table-recalculate
  2439. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2440. org-table-toggle-coordinate-overlays
  2441. org-table-toggle-formula-debugger org-table-wrap-region
  2442. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2443. (defun org-at-table-p (&optional table-type)
  2444. "Return t if the cursor is inside an org-type table.
  2445. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2446. (if org-enable-table-editor
  2447. (save-excursion
  2448. (beginning-of-line 1)
  2449. (looking-at (if table-type org-table-any-line-regexp
  2450. org-table-line-regexp)))
  2451. nil))
  2452. (defsubst org-table-p () (org-at-table-p))
  2453. (defun org-at-table.el-p ()
  2454. "Return t if and only if we are at a table.el table."
  2455. (and (org-at-table-p 'any)
  2456. (save-excursion
  2457. (goto-char (org-table-begin 'any))
  2458. (looking-at org-table1-hline-regexp))))
  2459. (defun org-table-recognize-table.el ()
  2460. "If there is a table.el table nearby, recognize it and move into it."
  2461. (if org-table-tab-recognizes-table.el
  2462. (if (org-at-table.el-p)
  2463. (progn
  2464. (beginning-of-line 1)
  2465. (if (looking-at org-table-dataline-regexp)
  2466. nil
  2467. (if (looking-at org-table1-hline-regexp)
  2468. (progn
  2469. (beginning-of-line 2)
  2470. (if (looking-at org-table-any-border-regexp)
  2471. (beginning-of-line -1)))))
  2472. (if (re-search-forward "|" (org-table-end t) t)
  2473. (progn
  2474. (require 'table)
  2475. (if (table--at-cell-p (point))
  2476. t
  2477. (message "recognizing table.el table...")
  2478. (table-recognize-table)
  2479. (message "recognizing table.el table...done")))
  2480. (error "This should not happen..."))
  2481. t)
  2482. nil)
  2483. nil))
  2484. (defun org-at-table-hline-p ()
  2485. "Return t if the cursor is inside a hline in a table."
  2486. (if org-enable-table-editor
  2487. (save-excursion
  2488. (beginning-of-line 1)
  2489. (looking-at org-table-hline-regexp))
  2490. nil))
  2491. (defvar org-table-clean-did-remove-column nil)
  2492. (defun org-table-map-tables (function)
  2493. "Apply FUNCTION to the start of all tables in the buffer."
  2494. (save-excursion
  2495. (save-restriction
  2496. (widen)
  2497. (goto-char (point-min))
  2498. (while (re-search-forward org-table-any-line-regexp nil t)
  2499. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2500. (beginning-of-line 1)
  2501. (if (looking-at org-table-line-regexp)
  2502. (save-excursion (funcall function)))
  2503. (re-search-forward org-table-any-border-regexp nil 1))))
  2504. (message "Mapping tables: done"))
  2505. ;; Declare and autoload functions from org-exp.el
  2506. (declare-function org-default-export-plist "org-exp")
  2507. (declare-function org-infile-export-plist "org-exp")
  2508. (declare-function org-get-current-options "org-exp")
  2509. (eval-and-compile
  2510. (org-autoload "org-exp"
  2511. '(org-export org-export-as-ascii org-export-visible
  2512. org-insert-export-options-template org-export-as-html-and-open
  2513. org-export-as-html-batch org-export-as-html-to-buffer
  2514. org-replace-region-by-html org-export-region-as-html
  2515. org-export-as-html org-export-icalendar-this-file
  2516. org-export-icalendar-all-agenda-files
  2517. org-table-clean-before-export
  2518. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2519. ;; Declare and autoload functions from org-agenda.el
  2520. (eval-and-compile
  2521. (org-autoload "org-agenda"
  2522. '(org-agenda org-agenda-list org-search-view
  2523. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2524. org-diary org-agenda-to-appt
  2525. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2526. ;; Autoload org-remember
  2527. (eval-and-compile
  2528. (org-autoload "org-remember"
  2529. '(org-remember-insinuate org-remember-annotation
  2530. org-remember-apply-template org-remember org-remember-handler)))
  2531. ;; Autoload org-clock.el
  2532. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2533. (beg end))
  2534. (declare-function org-clock-update-mode-line "org-clock" ())
  2535. (defvar org-clock-start-time)
  2536. (defvar org-clock-marker (make-marker)
  2537. "Marker recording the last clock-in.")
  2538. (eval-and-compile
  2539. (org-autoload
  2540. "org-clock"
  2541. '(org-clock-in org-clock-out org-clock-cancel
  2542. org-clock-goto org-clock-sum org-clock-display
  2543. org-clock-remove-overlays org-clock-report
  2544. org-clocktable-shift org-dblock-write:clocktable
  2545. org-get-clocktable)))
  2546. (defun org-clock-update-time-maybe ()
  2547. "If this is a CLOCK line, update it and return t.
  2548. Otherwise, return nil."
  2549. (interactive)
  2550. (save-excursion
  2551. (beginning-of-line 1)
  2552. (skip-chars-forward " \t")
  2553. (when (looking-at org-clock-string)
  2554. (let ((re (concat "[ \t]*" org-clock-string
  2555. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2556. "\\([ \t]*=>.*\\)?\\)?"))
  2557. ts te h m s neg)
  2558. (cond
  2559. ((not (looking-at re))
  2560. nil)
  2561. ((not (match-end 2))
  2562. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2563. (> org-clock-marker (point))
  2564. (<= org-clock-marker (point-at-eol)))
  2565. ;; The clock is running here
  2566. (setq org-clock-start-time
  2567. (apply 'encode-time
  2568. (org-parse-time-string (match-string 1))))
  2569. (org-clock-update-mode-line)))
  2570. (t
  2571. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2572. (end-of-line 1)
  2573. (setq ts (match-string 1)
  2574. te (match-string 3))
  2575. (setq s (- (time-to-seconds
  2576. (apply 'encode-time (org-parse-time-string te)))
  2577. (time-to-seconds
  2578. (apply 'encode-time (org-parse-time-string ts))))
  2579. neg (< s 0)
  2580. s (abs s)
  2581. h (floor (/ s 3600))
  2582. s (- s (* 3600 h))
  2583. m (floor (/ s 60))
  2584. s (- s (* 60 s)))
  2585. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2586. t))))))
  2587. (defun org-check-running-clock ()
  2588. "Check if the current buffer contains the running clock.
  2589. If yes, offer to stop it and to save the buffer with the changes."
  2590. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2591. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2592. (buffer-name))))
  2593. (org-clock-out)
  2594. (when (y-or-n-p "Save changed buffer?")
  2595. (save-buffer))))
  2596. (defun org-clocktable-try-shift (dir n)
  2597. "Check if this line starts a clock table, if yes, shift the time block."
  2598. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2599. (org-clocktable-shift dir n)))
  2600. ;; Autoload org-timer.el
  2601. ;(declare-function org-timer "org-timer")
  2602. (eval-and-compile
  2603. (org-autoload
  2604. "org-timer"
  2605. '(org-timer-start org-timer org-timer-item
  2606. org-timer-change-times-in-region)))
  2607. ;; Autoload archiving code
  2608. ;; The stuff that is needed for cycling and tags has to be defined here.
  2609. (defgroup org-archive nil
  2610. "Options concerning archiving in Org-mode."
  2611. :tag "Org Archive"
  2612. :group 'org-structure)
  2613. (defcustom org-archive-location "%s_archive::"
  2614. "The location where subtrees should be archived.
  2615. The value of this variable is a string, consisting of two parts,
  2616. separated by a double-colon. The first part is a filename and
  2617. the second part is a headline.
  2618. When the filename is omitted, archiving happens in the same file.
  2619. %s in the filename will be replaced by the current file
  2620. name (without the directory part). Archiving to a different file
  2621. is useful to keep archived entries from contributing to the
  2622. Org-mode Agenda.
  2623. The archived entries will be filed as subtrees of the specified
  2624. headline. When the headline is omitted, the subtrees are simply
  2625. filed away at the end of the file, as top-level entries. Also in
  2626. the heading you can use %s to represent the file name, this can be
  2627. useful when using the same archive for a number of different files.
  2628. Here are a few examples:
  2629. \"%s_archive::\"
  2630. If the current file is Projects.org, archive in file
  2631. Projects.org_archive, as top-level trees. This is the default.
  2632. \"::* Archived Tasks\"
  2633. Archive in the current file, under the top-level headline
  2634. \"* Archived Tasks\".
  2635. \"~/org/archive.org::\"
  2636. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2637. \"~/org/archive.org::From %s\"
  2638. Archive in file ~/org/archive.org (absolute path), und headlines
  2639. \"From FILENAME\" where file name is the current file name.
  2640. \"basement::** Finished Tasks\"
  2641. Archive in file ./basement (relative path), as level 3 trees
  2642. below the level 2 heading \"** Finished Tasks\".
  2643. You may set this option on a per-file basis by adding to the buffer a
  2644. line like
  2645. #+ARCHIVE: basement::** Finished Tasks
  2646. You may also define it locally for a subtree by setting an ARCHIVE property
  2647. in the entry. If such a property is found in an entry, or anywhere up
  2648. the hierarchy, it will be used."
  2649. :group 'org-archive
  2650. :type 'string)
  2651. (defcustom org-archive-tag "ARCHIVE"
  2652. "The tag that marks a subtree as archived.
  2653. An archived subtree does not open during visibility cycling, and does
  2654. not contribute to the agenda listings.
  2655. After changing this, font-lock must be restarted in the relevant buffers to
  2656. get the proper fontification."
  2657. :group 'org-archive
  2658. :group 'org-keywords
  2659. :type 'string)
  2660. (defcustom org-agenda-skip-archived-trees t
  2661. "Non-nil means, the agenda will skip any items located in archived trees.
  2662. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2663. variable is no longer recommended, you should leave it at the value t.
  2664. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2665. :group 'org-archive
  2666. :group 'org-agenda-skip
  2667. :type 'boolean)
  2668. (defcustom org-cycle-open-archived-trees nil
  2669. "Non-nil means, `org-cycle' will open archived trees.
  2670. An archived tree is a tree marked with the tag ARCHIVE.
  2671. When nil, archived trees will stay folded. You can still open them with
  2672. normal outline commands like `show-all', but not with the cycling commands."
  2673. :group 'org-archive
  2674. :group 'org-cycle
  2675. :type 'boolean)
  2676. (defcustom org-sparse-tree-open-archived-trees nil
  2677. "Non-nil means sparse tree construction shows matches in archived trees.
  2678. When nil, matches in these trees are highlighted, but the trees are kept in
  2679. collapsed state."
  2680. :group 'org-archive
  2681. :group 'org-sparse-trees
  2682. :type 'boolean)
  2683. (defun org-cycle-hide-archived-subtrees (state)
  2684. "Re-hide all archived subtrees after a visibility state change."
  2685. (when (and (not org-cycle-open-archived-trees)
  2686. (not (memq state '(overview folded))))
  2687. (save-excursion
  2688. (let* ((globalp (memq state '(contents all)))
  2689. (beg (if globalp (point-min) (point)))
  2690. (end (if globalp (point-max) (org-end-of-subtree t))))
  2691. (org-hide-archived-subtrees beg end)
  2692. (goto-char beg)
  2693. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2694. (message "%s" (substitute-command-keys
  2695. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2696. (defun org-force-cycle-archived ()
  2697. "Cycle subtree even if it is archived."
  2698. (interactive)
  2699. (setq this-command 'org-cycle)
  2700. (let ((org-cycle-open-archived-trees t))
  2701. (call-interactively 'org-cycle)))
  2702. (defun org-hide-archived-subtrees (beg end)
  2703. "Re-hide all archived subtrees after a visibility state change."
  2704. (save-excursion
  2705. (let* ((re (concat ":" org-archive-tag ":")))
  2706. (goto-char beg)
  2707. (while (re-search-forward re end t)
  2708. (and (org-on-heading-p) (hide-subtree))
  2709. (org-end-of-subtree t)))))
  2710. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2711. (eval-and-compile
  2712. (org-autoload "org-archive"
  2713. '(org-add-archive-files org-archive-subtree
  2714. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2715. ;; Autoload Column View Code
  2716. (declare-function org-columns-number-to-string "org-colview")
  2717. (declare-function org-columns-get-format-and-top-level "org-colview")
  2718. (declare-function org-columns-compute "org-colview")
  2719. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2720. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2721. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2722. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2723. ;; Autoload ID code
  2724. (declare-function org-id-store-link "org-id")
  2725. (declare-function org-id-locations-load "org-id")
  2726. (declare-function org-id-locations-save "org-id")
  2727. (defvar org-id-track-globally)
  2728. (org-autoload "org-id"
  2729. '(org-id-get-create org-id-new org-id-copy org-id-get
  2730. org-id-get-with-outline-path-completion
  2731. org-id-get-with-outline-drilling
  2732. org-id-goto org-id-find org-id-store-link))
  2733. ;; Autoload Plotting Code
  2734. (org-autoload "org-plot"
  2735. '(org-plot/gnuplot))
  2736. ;;; Variables for pre-computed regular expressions, all buffer local
  2737. (defvar org-drawer-regexp nil
  2738. "Matches first line of a hidden block.")
  2739. (make-variable-buffer-local 'org-drawer-regexp)
  2740. (defvar org-todo-regexp nil
  2741. "Matches any of the TODO state keywords.")
  2742. (make-variable-buffer-local 'org-todo-regexp)
  2743. (defvar org-not-done-regexp nil
  2744. "Matches any of the TODO state keywords except the last one.")
  2745. (make-variable-buffer-local 'org-not-done-regexp)
  2746. (defvar org-todo-line-regexp nil
  2747. "Matches a headline and puts TODO state into group 2 if present.")
  2748. (make-variable-buffer-local 'org-todo-line-regexp)
  2749. (defvar org-complex-heading-regexp nil
  2750. "Matches a headline and puts everything into groups:
  2751. group 1: the stars
  2752. group 2: The todo keyword, maybe
  2753. group 3: Priority cookie
  2754. group 4: True headline
  2755. group 5: Tags")
  2756. (make-variable-buffer-local 'org-complex-heading-regexp)
  2757. (defvar org-todo-line-tags-regexp nil
  2758. "Matches a headline and puts TODO state into group 2 if present.
  2759. Also put tags into group 4 if tags are present.")
  2760. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2761. (defvar org-nl-done-regexp nil
  2762. "Matches newline followed by a headline with the DONE keyword.")
  2763. (make-variable-buffer-local 'org-nl-done-regexp)
  2764. (defvar org-looking-at-done-regexp nil
  2765. "Matches the DONE keyword a point.")
  2766. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2767. (defvar org-ds-keyword-length 12
  2768. "Maximum length of the Deadline and SCHEDULED keywords.")
  2769. (make-variable-buffer-local 'org-ds-keyword-length)
  2770. (defvar org-deadline-regexp nil
  2771. "Matches the DEADLINE keyword.")
  2772. (make-variable-buffer-local 'org-deadline-regexp)
  2773. (defvar org-deadline-time-regexp nil
  2774. "Matches the DEADLINE keyword together with a time stamp.")
  2775. (make-variable-buffer-local 'org-deadline-time-regexp)
  2776. (defvar org-deadline-line-regexp nil
  2777. "Matches the DEADLINE keyword and the rest of the line.")
  2778. (make-variable-buffer-local 'org-deadline-line-regexp)
  2779. (defvar org-scheduled-regexp nil
  2780. "Matches the SCHEDULED keyword.")
  2781. (make-variable-buffer-local 'org-scheduled-regexp)
  2782. (defvar org-scheduled-time-regexp nil
  2783. "Matches the SCHEDULED keyword together with a time stamp.")
  2784. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2785. (defvar org-closed-time-regexp nil
  2786. "Matches the CLOSED keyword together with a time stamp.")
  2787. (make-variable-buffer-local 'org-closed-time-regexp)
  2788. (defvar org-keyword-time-regexp nil
  2789. "Matches any of the 4 keywords, together with the time stamp.")
  2790. (make-variable-buffer-local 'org-keyword-time-regexp)
  2791. (defvar org-keyword-time-not-clock-regexp nil
  2792. "Matches any of the 3 keywords, together with the time stamp.")
  2793. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2794. (defvar org-maybe-keyword-time-regexp nil
  2795. "Matches a timestamp, possibly preceeded by a keyword.")
  2796. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2797. (defvar org-planning-or-clock-line-re nil
  2798. "Matches a line with planning or clock info.")
  2799. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2800. (defconst org-plain-time-of-day-regexp
  2801. (concat
  2802. "\\(\\<[012]?[0-9]"
  2803. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2804. "\\(--?"
  2805. "\\(\\<[012]?[0-9]"
  2806. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2807. "\\)?")
  2808. "Regular expression to match a plain time or time range.
  2809. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2810. groups carry important information:
  2811. 0 the full match
  2812. 1 the first time, range or not
  2813. 8 the second time, if it is a range.")
  2814. (defconst org-plain-time-extension-regexp
  2815. (concat
  2816. "\\(\\<[012]?[0-9]"
  2817. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2818. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2819. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2820. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2821. groups carry important information:
  2822. 0 the full match
  2823. 7 hours of duration
  2824. 9 minutes of duration")
  2825. (defconst org-stamp-time-of-day-regexp
  2826. (concat
  2827. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2828. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2829. "\\(--?"
  2830. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2831. "Regular expression to match a timestamp time or time range.
  2832. After a match, the following groups carry important information:
  2833. 0 the full match
  2834. 1 date plus weekday, for backreferencing to make sure both times on same day
  2835. 2 the first time, range or not
  2836. 4 the second time, if it is a range.")
  2837. (defconst org-startup-options
  2838. '(("fold" org-startup-folded t)
  2839. ("overview" org-startup-folded t)
  2840. ("nofold" org-startup-folded nil)
  2841. ("showall" org-startup-folded nil)
  2842. ("content" org-startup-folded content)
  2843. ("hidestars" org-hide-leading-stars t)
  2844. ("showstars" org-hide-leading-stars nil)
  2845. ("odd" org-odd-levels-only t)
  2846. ("oddeven" org-odd-levels-only nil)
  2847. ("align" org-startup-align-all-tables t)
  2848. ("noalign" org-startup-align-all-tables nil)
  2849. ("customtime" org-display-custom-times t)
  2850. ("logdone" org-log-done time)
  2851. ("lognotedone" org-log-done note)
  2852. ("nologdone" org-log-done nil)
  2853. ("lognoteclock-out" org-log-note-clock-out t)
  2854. ("nolognoteclock-out" org-log-note-clock-out nil)
  2855. ("logrepeat" org-log-repeat state)
  2856. ("lognoterepeat" org-log-repeat note)
  2857. ("nologrepeat" org-log-repeat nil)
  2858. ("fninline" org-footnote-define-inline t)
  2859. ("nofninline" org-footnote-define-inline nil)
  2860. ("fnlocal" org-footnote-section nil)
  2861. ("fnauto" org-footnote-auto-label t)
  2862. ("fnprompt" org-footnote-auto-label nil)
  2863. ("fnconfirm" org-footnote-auto-label confirm)
  2864. ("fnplain" org-footnote-auto-label plain)
  2865. ("constcgs" constants-unit-system cgs)
  2866. ("constSI" constants-unit-system SI)
  2867. ("noptag" org-tag-persistent-alist nil))
  2868. "Variable associated with STARTUP options for org-mode.
  2869. Each element is a list of three items: The startup options as written
  2870. in the #+STARTUP line, the corresponding variable, and the value to
  2871. set this variable to if the option is found. An optional forth element PUSH
  2872. means to push this value onto the list in the variable.")
  2873. (defun org-set-regexps-and-options ()
  2874. "Precompute regular expressions for current buffer."
  2875. (when (org-mode-p)
  2876. (org-set-local 'org-todo-kwd-alist nil)
  2877. (org-set-local 'org-todo-key-alist nil)
  2878. (org-set-local 'org-todo-key-trigger nil)
  2879. (org-set-local 'org-todo-keywords-1 nil)
  2880. (org-set-local 'org-done-keywords nil)
  2881. (org-set-local 'org-todo-heads nil)
  2882. (org-set-local 'org-todo-sets nil)
  2883. (org-set-local 'org-todo-log-states nil)
  2884. (org-set-local 'org-file-properties nil)
  2885. (org-set-local 'org-file-tags nil)
  2886. (let ((re (org-make-options-regexp
  2887. '("CATEGORY" "TODO" "COLUMNS"
  2888. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2889. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2890. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2891. (splitre "[ \t]+")
  2892. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2893. tail sep kws1 prio props ftags drawers
  2894. ext-setup-or-nil setup-contents (start 0))
  2895. (save-excursion
  2896. (save-restriction
  2897. (widen)
  2898. (goto-char (point-min))
  2899. (while (or (and ext-setup-or-nil
  2900. (string-match re ext-setup-or-nil start)
  2901. (setq start (match-end 0)))
  2902. (and (setq ext-setup-or-nil nil start 0)
  2903. (re-search-forward re nil t)))
  2904. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2905. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2906. (cond
  2907. ((equal key "CATEGORY")
  2908. (if (string-match "[ \t]+$" value)
  2909. (setq value (replace-match "" t t value)))
  2910. (setq cat value))
  2911. ((member key '("SEQ_TODO" "TODO"))
  2912. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2913. ((equal key "TYP_TODO")
  2914. (push (cons 'type (org-split-string value splitre)) kwds))
  2915. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2916. ;; general TODO-like setup
  2917. (push (cons (intern (downcase (match-string 1 key)))
  2918. (org-split-string value splitre)) kwds))
  2919. ((equal key "TAGS")
  2920. (setq tags (append tags (if tags '("\\n") nil)
  2921. (org-split-string value splitre))))
  2922. ((equal key "COLUMNS")
  2923. (org-set-local 'org-columns-default-format value))
  2924. ((equal key "LINK")
  2925. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2926. (push (cons (match-string 1 value)
  2927. (org-trim (match-string 2 value)))
  2928. links)))
  2929. ((equal key "PRIORITIES")
  2930. (setq prio (org-split-string value " +")))
  2931. ((equal key "PROPERTY")
  2932. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2933. (push (cons (match-string 1 value) (match-string 2 value))
  2934. props)))
  2935. ((equal key "FILETAGS")
  2936. (when (string-match "\\S-" value)
  2937. (setq ftags
  2938. (append
  2939. ftags
  2940. (apply 'append
  2941. (mapcar (lambda (x) (org-split-string x ":"))
  2942. (org-split-string value)))))))
  2943. ((equal key "DRAWERS")
  2944. (setq drawers (org-split-string value splitre)))
  2945. ((equal key "CONSTANTS")
  2946. (setq const (append const (org-split-string value splitre))))
  2947. ((equal key "STARTUP")
  2948. (let ((opts (org-split-string value splitre))
  2949. l var val)
  2950. (while (setq l (pop opts))
  2951. (when (setq l (assoc l org-startup-options))
  2952. (setq var (nth 1 l) val (nth 2 l))
  2953. (if (not (nth 3 l))
  2954. (set (make-local-variable var) val)
  2955. (if (not (listp (symbol-value var)))
  2956. (set (make-local-variable var) nil))
  2957. (set (make-local-variable var) (symbol-value var))
  2958. (add-to-list var val))))))
  2959. ((equal key "ARCHIVE")
  2960. (string-match " *$" value)
  2961. (setq arch (replace-match "" t t value))
  2962. (remove-text-properties 0 (length arch)
  2963. '(face t fontified t) arch))
  2964. ((equal key "SETUPFILE")
  2965. (setq setup-contents (org-file-contents
  2966. (expand-file-name
  2967. (org-remove-double-quotes value))
  2968. 'noerror))
  2969. (if (not ext-setup-or-nil)
  2970. (setq ext-setup-or-nil setup-contents start 0)
  2971. (setq ext-setup-or-nil
  2972. (concat (substring ext-setup-or-nil 0 start)
  2973. "\n" setup-contents "\n"
  2974. (substring ext-setup-or-nil start)))))
  2975. ))))
  2976. (when cat
  2977. (org-set-local 'org-category (intern cat))
  2978. (push (cons "CATEGORY" cat) props))
  2979. (when prio
  2980. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2981. (setq prio (mapcar 'string-to-char prio))
  2982. (org-set-local 'org-highest-priority (nth 0 prio))
  2983. (org-set-local 'org-lowest-priority (nth 1 prio))
  2984. (org-set-local 'org-default-priority (nth 2 prio)))
  2985. (and props (org-set-local 'org-file-properties (nreverse props)))
  2986. (and ftags (org-set-local 'org-file-tags ftags))
  2987. (and drawers (org-set-local 'org-drawers drawers))
  2988. (and arch (org-set-local 'org-archive-location arch))
  2989. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2990. ;; Process the TODO keywords
  2991. (unless kwds
  2992. ;; Use the global values as if they had been given locally.
  2993. (setq kwds (default-value 'org-todo-keywords))
  2994. (if (stringp (car kwds))
  2995. (setq kwds (list (cons org-todo-interpretation
  2996. (default-value 'org-todo-keywords)))))
  2997. (setq kwds (reverse kwds)))
  2998. (setq kwds (nreverse kwds))
  2999. (let (inter kws kw)
  3000. (while (setq kws (pop kwds))
  3001. (let ((kws (or
  3002. (run-hook-with-args-until-success
  3003. 'org-todo-setup-filter-hook kws)
  3004. kws)))
  3005. (setq inter (pop kws) sep (member "|" kws)
  3006. kws0 (delete "|" (copy-sequence kws))
  3007. kwsa nil
  3008. kws1 (mapcar
  3009. (lambda (x)
  3010. ;; 1 2
  3011. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3012. (progn
  3013. (setq kw (match-string 1 x)
  3014. key (and (match-end 2) (match-string 2 x))
  3015. log (org-extract-log-state-settings x))
  3016. (push (cons kw (and key (string-to-char key))) kwsa)
  3017. (and log (push log org-todo-log-states))
  3018. kw)
  3019. (error "Invalid TODO keyword %s" x)))
  3020. kws0)
  3021. kwsa (if kwsa (append '((:startgroup))
  3022. (nreverse kwsa)
  3023. '((:endgroup))))
  3024. hw (car kws1)
  3025. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3026. tail (list inter hw (car dws) (org-last dws))))
  3027. (add-to-list 'org-todo-heads hw 'append)
  3028. (push kws1 org-todo-sets)
  3029. (setq org-done-keywords (append org-done-keywords dws nil))
  3030. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3031. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3032. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3033. (setq org-todo-sets (nreverse org-todo-sets)
  3034. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3035. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3036. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3037. ;; Process the constants
  3038. (when const
  3039. (let (e cst)
  3040. (while (setq e (pop const))
  3041. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3042. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3043. (setq org-table-formula-constants-local cst)))
  3044. ;; Process the tags.
  3045. (when tags
  3046. (let (e tgs)
  3047. (while (setq e (pop tags))
  3048. (cond
  3049. ((equal e "{") (push '(:startgroup) tgs))
  3050. ((equal e "}") (push '(:endgroup) tgs))
  3051. ((equal e "\\n") (push '(:newline) tgs))
  3052. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3053. (push (cons (match-string 1 e)
  3054. (string-to-char (match-string 2 e)))
  3055. tgs))
  3056. (t (push (list e) tgs))))
  3057. (org-set-local 'org-tag-alist nil)
  3058. (while (setq e (pop tgs))
  3059. (or (and (stringp (car e))
  3060. (assoc (car e) org-tag-alist))
  3061. (push e org-tag-alist)))))
  3062. ;; Compute the regular expressions and other local variables
  3063. (if (not org-done-keywords)
  3064. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3065. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3066. (length org-scheduled-string)
  3067. (length org-clock-string)
  3068. (length org-closed-string)))
  3069. org-drawer-regexp
  3070. (concat "^[ \t]*:\\("
  3071. (mapconcat 'regexp-quote org-drawers "\\|")
  3072. "\\):[ \t]*$")
  3073. org-not-done-keywords
  3074. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3075. org-todo-regexp
  3076. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3077. "\\|") "\\)\\>")
  3078. org-not-done-regexp
  3079. (concat "\\<\\("
  3080. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3081. "\\)\\>")
  3082. org-todo-line-regexp
  3083. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3084. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3085. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3086. org-complex-heading-regexp
  3087. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3088. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3089. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3090. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3091. org-nl-done-regexp
  3092. (concat "\n\\*+[ \t]+"
  3093. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3094. "\\)" "\\>")
  3095. org-todo-line-tags-regexp
  3096. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3097. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3098. (org-re
  3099. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3100. org-looking-at-done-regexp
  3101. (concat "^" "\\(?:"
  3102. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3103. "\\>")
  3104. org-deadline-regexp (concat "\\<" org-deadline-string)
  3105. org-deadline-time-regexp
  3106. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3107. org-deadline-line-regexp
  3108. (concat "\\<\\(" org-deadline-string "\\).*")
  3109. org-scheduled-regexp
  3110. (concat "\\<" org-scheduled-string)
  3111. org-scheduled-time-regexp
  3112. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3113. org-closed-time-regexp
  3114. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3115. org-keyword-time-regexp
  3116. (concat "\\<\\(" org-scheduled-string
  3117. "\\|" org-deadline-string
  3118. "\\|" org-closed-string
  3119. "\\|" org-clock-string "\\)"
  3120. " *[[<]\\([^]>]+\\)[]>]")
  3121. org-keyword-time-not-clock-regexp
  3122. (concat "\\<\\(" org-scheduled-string
  3123. "\\|" org-deadline-string
  3124. "\\|" org-closed-string
  3125. "\\)"
  3126. " *[[<]\\([^]>]+\\)[]>]")
  3127. org-maybe-keyword-time-regexp
  3128. (concat "\\(\\<\\(" org-scheduled-string
  3129. "\\|" org-deadline-string
  3130. "\\|" org-closed-string
  3131. "\\|" org-clock-string "\\)\\)?"
  3132. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3133. org-planning-or-clock-line-re
  3134. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3135. "\\|" org-deadline-string
  3136. "\\|" org-closed-string "\\|" org-clock-string
  3137. "\\)\\>\\)")
  3138. )
  3139. (org-compute-latex-and-specials-regexp)
  3140. (org-set-font-lock-defaults))))
  3141. (defun org-file-contents (file &optional noerror)
  3142. "Return the contents of FILE, as a string."
  3143. (if (or (not file)
  3144. (not (file-readable-p file)))
  3145. (if noerror
  3146. (progn
  3147. (message "Cannot read file %s" file)
  3148. (ding) (sit-for 2)
  3149. "")
  3150. (error "Cannot read file %s" file))
  3151. (with-temp-buffer
  3152. (insert-file-contents file)
  3153. (buffer-string))))
  3154. (defun org-extract-log-state-settings (x)
  3155. "Extract the log state setting from a TODO keyword string.
  3156. This will extract info from a string like \"WAIT(w@/!)\"."
  3157. (let (kw key log1 log2)
  3158. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3159. (setq kw (match-string 1 x)
  3160. key (and (match-end 2) (match-string 2 x))
  3161. log1 (and (match-end 3) (match-string 3 x))
  3162. log2 (and (match-end 4) (match-string 4 x)))
  3163. (and (or log1 log2)
  3164. (list kw
  3165. (and log1 (if (equal log1 "!") 'time 'note))
  3166. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3167. (defun org-remove-keyword-keys (list)
  3168. "Remove a pair of parenthesis at the end of each string in LIST."
  3169. (mapcar (lambda (x)
  3170. (if (string-match "(.*)$" x)
  3171. (substring x 0 (match-beginning 0))
  3172. x))
  3173. list))
  3174. ;; FIXME: this could be done much better, using second characters etc.
  3175. (defun org-assign-fast-keys (alist)
  3176. "Assign fast keys to a keyword-key alist.
  3177. Respect keys that are already there."
  3178. (let (new e k c c1 c2 (char ?a))
  3179. (while (setq e (pop alist))
  3180. (cond
  3181. ((equal e '(:startgroup)) (push e new))
  3182. ((equal e '(:endgroup)) (push e new))
  3183. ((equal e '(:newline)) (push e new))
  3184. (t
  3185. (setq k (car e) c2 nil)
  3186. (if (cdr e)
  3187. (setq c (cdr e))
  3188. ;; automatically assign a character.
  3189. (setq c1 (string-to-char
  3190. (downcase (substring
  3191. k (if (= (string-to-char k) ?@) 1 0)))))
  3192. (if (or (rassoc c1 new) (rassoc c1 alist))
  3193. (while (or (rassoc char new) (rassoc char alist))
  3194. (setq char (1+ char)))
  3195. (setq c2 c1))
  3196. (setq c (or c2 char)))
  3197. (push (cons k c) new))))
  3198. (nreverse new)))
  3199. ;;; Some variables used in various places
  3200. (defvar org-window-configuration nil
  3201. "Used in various places to store a window configuration.")
  3202. (defvar org-finish-function nil
  3203. "Function to be called when `C-c C-c' is used.
  3204. This is for getting out of special buffers like remember.")
  3205. ;; FIXME: Occasionally check by commenting these, to make sure
  3206. ;; no other functions uses these, forgetting to let-bind them.
  3207. (defvar entry)
  3208. (defvar state)
  3209. (defvar last-state)
  3210. (defvar date)
  3211. (defvar description)
  3212. ;; Defined somewhere in this file, but used before definition.
  3213. (defvar org-html-entities)
  3214. (defvar org-struct-menu)
  3215. (defvar org-org-menu)
  3216. (defvar org-tbl-menu)
  3217. (defvar org-agenda-keymap)
  3218. ;;;; Define the Org-mode
  3219. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3220. (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."))
  3221. ;; We use a before-change function to check if a table might need
  3222. ;; an update.
  3223. (defvar org-table-may-need-update t
  3224. "Indicates that a table might need an update.
  3225. This variable is set by `org-before-change-function'.
  3226. `org-table-align' sets it back to nil.")
  3227. (defun org-before-change-function (beg end)
  3228. "Every change indicates that a table might need an update."
  3229. (setq org-table-may-need-update t))
  3230. (defvar org-mode-map)
  3231. (defvar org-mode-hook nil
  3232. "Mode hook for Org-mode, run after the mode was turned on.")
  3233. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3234. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3235. (defvar org-table-buffer-is-an nil)
  3236. (defconst org-outline-regexp "\\*+ ")
  3237. ;;;###autoload
  3238. (define-derived-mode org-mode outline-mode "Org"
  3239. "Outline-based notes management and organizer, alias
  3240. \"Carsten's outline-mode for keeping track of everything.\"
  3241. Org-mode develops organizational tasks around a NOTES file which
  3242. contains information about projects as plain text. Org-mode is
  3243. implemented on top of outline-mode, which is ideal to keep the content
  3244. of large files well structured. It supports ToDo items, deadlines and
  3245. time stamps, which magically appear in the diary listing of the Emacs
  3246. calendar. Tables are easily created with a built-in table editor.
  3247. Plain text URL-like links connect to websites, emails (VM), Usenet
  3248. messages (Gnus), BBDB entries, and any files related to the project.
  3249. For printing and sharing of notes, an Org-mode file (or a part of it)
  3250. can be exported as a structured ASCII or HTML file.
  3251. The following commands are available:
  3252. \\{org-mode-map}"
  3253. ;; Get rid of Outline menus, they are not needed
  3254. ;; Need to do this here because define-derived-mode sets up
  3255. ;; the keymap so late. Still, it is a waste to call this each time
  3256. ;; we switch another buffer into org-mode.
  3257. (if (featurep 'xemacs)
  3258. (when (boundp 'outline-mode-menu-heading)
  3259. ;; Assume this is Greg's port, it used easymenu
  3260. (easy-menu-remove outline-mode-menu-heading)
  3261. (easy-menu-remove outline-mode-menu-show)
  3262. (easy-menu-remove outline-mode-menu-hide))
  3263. (define-key org-mode-map [menu-bar headings] 'undefined)
  3264. (define-key org-mode-map [menu-bar hide] 'undefined)
  3265. (define-key org-mode-map [menu-bar show] 'undefined))
  3266. (org-load-modules-maybe)
  3267. (easy-menu-add org-org-menu)
  3268. (easy-menu-add org-tbl-menu)
  3269. (org-install-agenda-files-menu)
  3270. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3271. (org-add-to-invisibility-spec '(org-cwidth))
  3272. (when (featurep 'xemacs)
  3273. (org-set-local 'line-move-ignore-invisible t))
  3274. (org-set-local 'outline-regexp org-outline-regexp)
  3275. (org-set-local 'outline-level 'org-outline-level)
  3276. (when (and org-ellipsis
  3277. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3278. (fboundp 'make-glyph-code))
  3279. (unless org-display-table
  3280. (setq org-display-table (make-display-table)))
  3281. (set-display-table-slot
  3282. org-display-table 4
  3283. (vconcat (mapcar
  3284. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3285. org-ellipsis)))
  3286. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3287. (setq buffer-display-table org-display-table))
  3288. (org-set-regexps-and-options)
  3289. (when (and org-tag-faces (not org-tags-special-faces-re))
  3290. ;; tag faces set outside customize.... force initialization.
  3291. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3292. ;; Calc embedded
  3293. (org-set-local 'calc-embedded-open-mode "# ")
  3294. (modify-syntax-entry ?# "<")
  3295. (modify-syntax-entry ?@ "w")
  3296. (if org-startup-truncated (setq truncate-lines t))
  3297. (org-set-local 'font-lock-unfontify-region-function
  3298. 'org-unfontify-region)
  3299. ;; Activate before-change-function
  3300. (org-set-local 'org-table-may-need-update t)
  3301. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3302. 'local)
  3303. ;; Check for running clock before killing a buffer
  3304. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3305. ;; Paragraphs and auto-filling
  3306. (org-set-autofill-regexps)
  3307. (setq indent-line-function 'org-indent-line-function)
  3308. (org-update-radio-target-regexp)
  3309. ;; Make sure dependence stuff works reliably, even for users who set it
  3310. ;; too late :-(
  3311. (if org-enforce-todo-dependencies
  3312. (add-hook 'org-blocker-hook
  3313. 'org-block-todo-from-children-or-siblings)
  3314. (remove-hook 'org-blocker-hook
  3315. 'org-block-todo-from-children-or-siblings))
  3316. (if org-enforce-todo-checkbox-dependencies
  3317. (add-hook 'org-blocker-hook
  3318. 'org-block-todo-from-checkboxes)
  3319. (remove-hook 'org-blocker-hook
  3320. 'org-block-todo-from-checkboxes))
  3321. ;; Comment characters
  3322. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3323. (org-set-local 'comment-padding " ")
  3324. ;; Align options lines
  3325. (org-set-local
  3326. 'align-mode-rules-list
  3327. '((org-in-buffer-settings
  3328. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3329. (modes . '(org-mode)))))
  3330. ;; Imenu
  3331. (org-set-local 'imenu-create-index-function
  3332. 'org-imenu-get-tree)
  3333. ;; Make isearch reveal context
  3334. (if (or (featurep 'xemacs)
  3335. (not (boundp 'outline-isearch-open-invisible-function)))
  3336. ;; Emacs 21 and XEmacs make use of the hook
  3337. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3338. ;; Emacs 22 deals with this through a special variable
  3339. (org-set-local 'outline-isearch-open-invisible-function
  3340. (lambda (&rest ignore) (org-show-context 'isearch))))
  3341. ;; If empty file that did not turn on org-mode automatically, make it to.
  3342. (if (and org-insert-mode-line-in-empty-file
  3343. (interactive-p)
  3344. (= (point-min) (point-max)))
  3345. (insert "# -*- mode: org -*-\n\n"))
  3346. (unless org-inhibit-startup
  3347. (when org-startup-align-all-tables
  3348. (let ((bmp (buffer-modified-p)))
  3349. (org-table-map-tables 'org-table-align)
  3350. (set-buffer-modified-p bmp)))
  3351. (org-set-startup-visibility)))
  3352. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3353. (defun org-current-time ()
  3354. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3355. (if (> (car org-time-stamp-rounding-minutes) 1)
  3356. (let ((r (car org-time-stamp-rounding-minutes))
  3357. (time (decode-time)))
  3358. (apply 'encode-time
  3359. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3360. (nthcdr 2 time))))
  3361. (current-time)))
  3362. ;;;; Font-Lock stuff, including the activators
  3363. (defvar org-mouse-map (make-sparse-keymap))
  3364. (org-defkey org-mouse-map
  3365. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3366. (org-defkey org-mouse-map
  3367. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3368. (when org-mouse-1-follows-link
  3369. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3370. (when org-tab-follows-link
  3371. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3372. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3373. (require 'font-lock)
  3374. (defconst org-non-link-chars "]\t\n\r<>")
  3375. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3376. "shell" "elisp"))
  3377. (defvar org-link-types-re nil
  3378. "Matches a link that has a url-like prefix like \"http:\"")
  3379. (defvar org-link-re-with-space nil
  3380. "Matches a link with spaces, optional angular brackets around it.")
  3381. (defvar org-link-re-with-space2 nil
  3382. "Matches a link with spaces, optional angular brackets around it.")
  3383. (defvar org-link-re-with-space3 nil
  3384. "Matches a link with spaces, only for internal part in bracket links.")
  3385. (defvar org-angle-link-re nil
  3386. "Matches link with angular brackets, spaces are allowed.")
  3387. (defvar org-plain-link-re nil
  3388. "Matches plain link, without spaces.")
  3389. (defvar org-bracket-link-regexp nil
  3390. "Matches a link in double brackets.")
  3391. (defvar org-bracket-link-analytic-regexp nil
  3392. "Regular expression used to analyze links.
  3393. Here is what the match groups contain after a match:
  3394. 1: http:
  3395. 2: http
  3396. 3: path
  3397. 4: [desc]
  3398. 5: desc")
  3399. (defvar org-bracket-link-analytic-regexp++ nil
  3400. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3401. (defvar org-any-link-re nil
  3402. "Regular expression matching any link.")
  3403. (defun org-make-link-regexps ()
  3404. "Update the link regular expressions.
  3405. This should be called after the variable `org-link-types' has changed."
  3406. (setq org-link-types-re
  3407. (concat
  3408. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3409. org-link-re-with-space
  3410. (concat
  3411. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3412. "\\([^" org-non-link-chars " ]"
  3413. "[^" org-non-link-chars "]*"
  3414. "[^" org-non-link-chars " ]\\)>?")
  3415. org-link-re-with-space2
  3416. (concat
  3417. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3418. "\\([^" org-non-link-chars " ]"
  3419. "[^\t\n\r]*"
  3420. "[^" org-non-link-chars " ]\\)>?")
  3421. org-link-re-with-space3
  3422. (concat
  3423. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3424. "\\([^" org-non-link-chars " ]"
  3425. "[^\t\n\r]*\\)")
  3426. org-angle-link-re
  3427. (concat
  3428. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3429. "\\([^" org-non-link-chars " ]"
  3430. "[^" org-non-link-chars "]*"
  3431. "\\)>")
  3432. org-plain-link-re
  3433. (concat
  3434. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3435. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3436. org-bracket-link-regexp
  3437. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3438. org-bracket-link-analytic-regexp
  3439. (concat
  3440. "\\[\\["
  3441. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3442. "\\([^]]+\\)"
  3443. "\\]"
  3444. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3445. "\\]")
  3446. org-bracket-link-analytic-regexp++
  3447. (concat
  3448. "\\[\\["
  3449. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3450. "\\([^]]+\\)"
  3451. "\\]"
  3452. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3453. "\\]")
  3454. org-any-link-re
  3455. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3456. org-angle-link-re "\\)\\|\\("
  3457. org-plain-link-re "\\)")))
  3458. (org-make-link-regexps)
  3459. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3460. "Regular expression for fast time stamp matching.")
  3461. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3462. "Regular expression for fast time stamp matching.")
  3463. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3464. "Regular expression matching time strings for analysis.
  3465. This one does not require the space after the date, so it can be used
  3466. on a string that terminates immediately after the date.")
  3467. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3468. "Regular expression matching time strings for analysis.")
  3469. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3470. "Regular expression matching time stamps, with groups.")
  3471. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3472. "Regular expression matching time stamps (also [..]), with groups.")
  3473. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3474. "Regular expression matching a time stamp range.")
  3475. (defconst org-tr-regexp-both
  3476. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3477. "Regular expression matching a time stamp range.")
  3478. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3479. org-ts-regexp "\\)?")
  3480. "Regular expression matching a time stamp or time stamp range.")
  3481. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3482. org-ts-regexp-both "\\)?")
  3483. "Regular expression matching a time stamp or time stamp range.
  3484. The time stamps may be either active or inactive.")
  3485. (defvar org-emph-face nil)
  3486. (defun org-do-emphasis-faces (limit)
  3487. "Run through the buffer and add overlays to links."
  3488. (let (rtn)
  3489. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3490. (if (not (= (char-after (match-beginning 3))
  3491. (char-after (match-beginning 4))))
  3492. (progn
  3493. (setq rtn t)
  3494. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3495. 'face
  3496. (nth 1 (assoc (match-string 3)
  3497. org-emphasis-alist)))
  3498. (add-text-properties (match-beginning 2) (match-end 2)
  3499. '(font-lock-multiline t))
  3500. (when org-hide-emphasis-markers
  3501. (add-text-properties (match-end 4) (match-beginning 5)
  3502. '(invisible org-link))
  3503. (add-text-properties (match-beginning 3) (match-end 3)
  3504. '(invisible org-link)))))
  3505. (backward-char 1))
  3506. rtn))
  3507. (defun org-emphasize (&optional char)
  3508. "Insert or change an emphasis, i.e. a font like bold or italic.
  3509. If there is an active region, change that region to a new emphasis.
  3510. If there is no region, just insert the marker characters and position
  3511. the cursor between them.
  3512. CHAR should be either the marker character, or the first character of the
  3513. HTML tag associated with that emphasis. If CHAR is a space, the means
  3514. to remove the emphasis of the selected region.
  3515. If char is not given (for example in an interactive call) it
  3516. will be prompted for."
  3517. (interactive)
  3518. (let ((eal org-emphasis-alist) e det
  3519. (erc org-emphasis-regexp-components)
  3520. (prompt "")
  3521. (string "") beg end move tag c s)
  3522. (if (org-region-active-p)
  3523. (setq beg (region-beginning) end (region-end)
  3524. string (buffer-substring beg end))
  3525. (setq move t))
  3526. (while (setq e (pop eal))
  3527. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3528. c (aref tag 0))
  3529. (push (cons c (string-to-char (car e))) det)
  3530. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3531. (substring tag 1)))))
  3532. (setq det (nreverse det))
  3533. (unless char
  3534. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3535. (setq char (read-char-exclusive)))
  3536. (setq char (or (cdr (assoc char det)) char))
  3537. (if (equal char ?\ )
  3538. (setq s "" move nil)
  3539. (unless (assoc (char-to-string char) org-emphasis-alist)
  3540. (error "No such emphasis marker: \"%c\"" char))
  3541. (setq s (char-to-string char)))
  3542. (while (and (> (length string) 1)
  3543. (equal (substring string 0 1) (substring string -1))
  3544. (assoc (substring string 0 1) org-emphasis-alist))
  3545. (setq string (substring string 1 -1)))
  3546. (setq string (concat s string s))
  3547. (if beg (delete-region beg end))
  3548. (unless (or (bolp)
  3549. (string-match (concat "[" (nth 0 erc) "\n]")
  3550. (char-to-string (char-before (point)))))
  3551. (insert " "))
  3552. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3553. (char-to-string (char-after (point))))
  3554. (insert " ") (backward-char 1))
  3555. (insert string)
  3556. (and move (backward-char 1))))
  3557. (defconst org-nonsticky-props
  3558. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3559. (defun org-activate-plain-links (limit)
  3560. "Run through the buffer and add overlays to links."
  3561. (catch 'exit
  3562. (let (f)
  3563. (while (re-search-forward org-plain-link-re limit t)
  3564. (setq f (get-text-property (match-beginning 0) 'face))
  3565. (if (or (eq f 'org-tag)
  3566. (and (listp f) (memq 'org-tag f)))
  3567. nil
  3568. (add-text-properties (match-beginning 0) (match-end 0)
  3569. (list 'mouse-face 'highlight
  3570. 'rear-nonsticky org-nonsticky-props
  3571. 'keymap org-mouse-map
  3572. ))
  3573. (throw 'exit t))))))
  3574. (defun org-activate-code (limit)
  3575. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3576. (progn
  3577. (remove-text-properties (match-beginning 0) (match-end 0)
  3578. '(display t invisible t intangible t))
  3579. t)))
  3580. (defun org-activate-angle-links (limit)
  3581. "Run through the buffer and add overlays to links."
  3582. (if (re-search-forward org-angle-link-re limit t)
  3583. (progn
  3584. (add-text-properties (match-beginning 0) (match-end 0)
  3585. (list 'mouse-face 'highlight
  3586. 'rear-nonsticky org-nonsticky-props
  3587. 'keymap org-mouse-map
  3588. ))
  3589. t)))
  3590. (defun org-activate-footnote-links (limit)
  3591. "Run through the buffer and add overlays to links."
  3592. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3593. limit t)
  3594. (progn
  3595. (add-text-properties (match-beginning 2) (match-end 2)
  3596. (list 'mouse-face 'highlight
  3597. 'rear-nonsticky org-nonsticky-props
  3598. 'keymap org-mouse-map
  3599. 'help-echo
  3600. (if (= (point-at-bol) (match-beginning 2))
  3601. "Footnote definition"
  3602. "Footnote reference")
  3603. ))
  3604. t)))
  3605. (defun org-activate-bracket-links (limit)
  3606. "Run through the buffer and add overlays to bracketed links."
  3607. (if (re-search-forward org-bracket-link-regexp limit t)
  3608. (let* ((help (concat "LINK: "
  3609. (org-match-string-no-properties 1)))
  3610. ;; FIXME: above we should remove the escapes.
  3611. ;; but that requires another match, protecting match data,
  3612. ;; a lot of overhead for font-lock.
  3613. (ip (org-maybe-intangible
  3614. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3615. 'keymap org-mouse-map 'mouse-face 'highlight
  3616. 'font-lock-multiline t 'help-echo help)))
  3617. (vp (list 'rear-nonsticky org-nonsticky-props
  3618. 'keymap org-mouse-map 'mouse-face 'highlight
  3619. ' font-lock-multiline t 'help-echo help)))
  3620. ;; We need to remove the invisible property here. Table narrowing
  3621. ;; may have made some of this invisible.
  3622. (remove-text-properties (match-beginning 0) (match-end 0)
  3623. '(invisible nil))
  3624. (if (match-end 3)
  3625. (progn
  3626. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3627. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3628. (add-text-properties (match-end 3) (match-end 0) ip))
  3629. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3630. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3631. (add-text-properties (match-end 1) (match-end 0) ip))
  3632. t)))
  3633. (defun org-activate-dates (limit)
  3634. "Run through the buffer and add overlays to dates."
  3635. (if (re-search-forward org-tsr-regexp-both limit t)
  3636. (progn
  3637. (add-text-properties (match-beginning 0) (match-end 0)
  3638. (list 'mouse-face 'highlight
  3639. 'rear-nonsticky org-nonsticky-props
  3640. 'keymap org-mouse-map))
  3641. (when org-display-custom-times
  3642. (if (match-end 3)
  3643. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3644. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3645. t)))
  3646. (defvar org-target-link-regexp nil
  3647. "Regular expression matching radio targets in plain text.")
  3648. (make-variable-buffer-local 'org-target-link-regexp)
  3649. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3650. "Regular expression matching a link target.")
  3651. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3652. "Regular expression matching a radio target.")
  3653. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3654. "Regular expression matching any target.")
  3655. (defun org-activate-target-links (limit)
  3656. "Run through the buffer and add overlays to target matches."
  3657. (when org-target-link-regexp
  3658. (let ((case-fold-search t))
  3659. (if (re-search-forward org-target-link-regexp limit t)
  3660. (progn
  3661. (add-text-properties (match-beginning 0) (match-end 0)
  3662. (list 'mouse-face 'highlight
  3663. 'rear-nonsticky org-nonsticky-props
  3664. 'keymap org-mouse-map
  3665. 'help-echo "Radio target link"
  3666. 'org-linked-text t))
  3667. t)))))
  3668. (defun org-update-radio-target-regexp ()
  3669. "Find all radio targets in this file and update the regular expression."
  3670. (interactive)
  3671. (when (memq 'radio org-activate-links)
  3672. (setq org-target-link-regexp
  3673. (org-make-target-link-regexp (org-all-targets 'radio)))
  3674. (org-restart-font-lock)))
  3675. (defun org-hide-wide-columns (limit)
  3676. (let (s e)
  3677. (setq s (text-property-any (point) (or limit (point-max))
  3678. 'org-cwidth t))
  3679. (when s
  3680. (setq e (next-single-property-change s 'org-cwidth))
  3681. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3682. (goto-char e)
  3683. t)))
  3684. (defvar org-latex-and-specials-regexp nil
  3685. "Regular expression for highlighting export special stuff.")
  3686. (defvar org-match-substring-regexp)
  3687. (defvar org-match-substring-with-braces-regexp)
  3688. (defvar org-export-html-special-string-regexps)
  3689. (defun org-compute-latex-and-specials-regexp ()
  3690. "Compute regular expression for stuff treated specially by exporters."
  3691. (if (not org-highlight-latex-fragments-and-specials)
  3692. (org-set-local 'org-latex-and-specials-regexp nil)
  3693. (require 'org-exp)
  3694. (let*
  3695. ((matchers (plist-get org-format-latex-options :matchers))
  3696. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3697. org-latex-regexps)))
  3698. (options (org-combine-plists (org-default-export-plist)
  3699. (org-infile-export-plist)))
  3700. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3701. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3702. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3703. (org-export-html-expand (plist-get options :expand-quoted-html))
  3704. (org-export-with-special-strings (plist-get options :special-strings))
  3705. (re-sub
  3706. (cond
  3707. ((equal org-export-with-sub-superscripts '{})
  3708. (list org-match-substring-with-braces-regexp))
  3709. (org-export-with-sub-superscripts
  3710. (list org-match-substring-regexp))
  3711. (t nil)))
  3712. (re-latex
  3713. (if org-export-with-LaTeX-fragments
  3714. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3715. (re-macros
  3716. (if org-export-with-TeX-macros
  3717. (list (concat "\\\\"
  3718. (regexp-opt
  3719. (append (mapcar 'car org-html-entities)
  3720. (if (boundp 'org-latex-entities)
  3721. org-latex-entities nil))
  3722. 'words))) ; FIXME
  3723. ))
  3724. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3725. (re-special (if org-export-with-special-strings
  3726. (mapcar (lambda (x) (car x))
  3727. org-export-html-special-string-regexps)))
  3728. (re-rest
  3729. (delq nil
  3730. (list
  3731. (if org-export-html-expand "@<[^>\n]+>")
  3732. ))))
  3733. (org-set-local
  3734. 'org-latex-and-specials-regexp
  3735. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3736. re-rest) "\\|")))))
  3737. (defun org-do-latex-and-special-faces (limit)
  3738. "Run through the buffer and add overlays to links."
  3739. (when org-latex-and-specials-regexp
  3740. (let (rtn d)
  3741. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3742. limit t))
  3743. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3744. 'face))
  3745. '(org-code org-verbatim underline)))
  3746. (progn
  3747. (setq rtn t
  3748. d (cond ((member (char-after (1+ (match-beginning 0)))
  3749. '(?_ ?^)) 1)
  3750. (t 0)))
  3751. (font-lock-prepend-text-property
  3752. (+ d (match-beginning 0)) (match-end 0)
  3753. 'face 'org-latex-and-export-specials)
  3754. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3755. '(font-lock-multiline t)))))
  3756. rtn)))
  3757. (defun org-restart-font-lock ()
  3758. "Restart font-lock-mode, to force refontification."
  3759. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3760. (font-lock-mode -1)
  3761. (font-lock-mode 1)))
  3762. (defun org-all-targets (&optional radio)
  3763. "Return a list of all targets in this file.
  3764. With optional argument RADIO, only find radio targets."
  3765. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3766. rtn)
  3767. (save-excursion
  3768. (goto-char (point-min))
  3769. (while (re-search-forward re nil t)
  3770. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3771. rtn)))
  3772. (defun org-make-target-link-regexp (targets)
  3773. "Make regular expression matching all strings in TARGETS.
  3774. The regular expression finds the targets also if there is a line break
  3775. between words."
  3776. (and targets
  3777. (concat
  3778. "\\<\\("
  3779. (mapconcat
  3780. (lambda (x)
  3781. (while (string-match " +" x)
  3782. (setq x (replace-match "\\s-+" t t x)))
  3783. x)
  3784. targets
  3785. "\\|")
  3786. "\\)\\>")))
  3787. (defun org-activate-tags (limit)
  3788. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3789. (progn
  3790. (add-text-properties (match-beginning 1) (match-end 1)
  3791. (list 'mouse-face 'highlight
  3792. 'rear-nonsticky org-nonsticky-props
  3793. 'keymap org-mouse-map))
  3794. t)))
  3795. (defun org-outline-level ()
  3796. (save-excursion
  3797. (looking-at outline-regexp)
  3798. (if (match-beginning 1)
  3799. (+ (org-get-string-indentation (match-string 1)) 1000)
  3800. (1- (- (match-end 0) (match-beginning 0))))))
  3801. (defvar org-font-lock-keywords nil)
  3802. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3803. "Regular expression matching a property line.")
  3804. (defvar org-font-lock-hook nil
  3805. "Functions to be called for special font lock stuff.")
  3806. (defun org-font-lock-hook (limit)
  3807. (run-hook-with-args 'org-font-lock-hook limit))
  3808. (defun org-set-font-lock-defaults ()
  3809. (let* ((em org-fontify-emphasized-text)
  3810. (lk org-activate-links)
  3811. (org-font-lock-extra-keywords
  3812. (list
  3813. ;; Call the hook
  3814. '(org-font-lock-hook)
  3815. ;; Headlines
  3816. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3817. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3818. ;; Table lines
  3819. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3820. (1 'org-table t))
  3821. ;; Table internals
  3822. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3823. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3824. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3825. ;; Drawers
  3826. (list org-drawer-regexp '(0 'org-special-keyword t))
  3827. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3828. ;; Properties
  3829. (list org-property-re
  3830. '(1 'org-special-keyword t)
  3831. '(3 'org-property-value t))
  3832. (if org-format-transports-properties-p
  3833. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3834. ;; Links
  3835. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3836. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3837. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3838. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3839. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3840. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3841. (if (memq 'footnote lk) '(org-activate-footnote-links
  3842. (2 'org-footnote t)))
  3843. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3844. '(org-hide-wide-columns (0 nil append))
  3845. ;; TODO lines
  3846. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3847. '(1 (org-get-todo-face 1) t))
  3848. ;; DONE
  3849. (if org-fontify-done-headline
  3850. (list (concat "^[*]+ +\\<\\("
  3851. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3852. "\\)\\(.*\\)")
  3853. '(2 'org-headline-done t))
  3854. nil)
  3855. ;; Priorities
  3856. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3857. ;; Tags
  3858. '(org-font-lock-add-tag-faces)
  3859. ;; Special keywords
  3860. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3861. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3862. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3863. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3864. ;; Emphasis
  3865. (if em
  3866. (if (featurep 'xemacs)
  3867. '(org-do-emphasis-faces (0 nil append))
  3868. '(org-do-emphasis-faces)))
  3869. ;; Checkboxes
  3870. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3871. 2 'bold prepend)
  3872. (if org-provide-checkbox-statistics
  3873. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3874. (0 (org-get-checkbox-statistics-face) t)))
  3875. ;; Description list items
  3876. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3877. 2 'bold prepend)
  3878. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3879. '(1 'org-archived prepend))
  3880. ;; Specials
  3881. '(org-do-latex-and-special-faces)
  3882. ;; Code
  3883. '(org-activate-code (1 'org-code t))
  3884. ;; COMMENT
  3885. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3886. "\\|" org-quote-string "\\)\\>")
  3887. '(1 'org-special-keyword t))
  3888. '("^#.*" (0 'font-lock-comment-face t))
  3889. )))
  3890. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3891. ;; Now set the full font-lock-keywords
  3892. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3893. (org-set-local 'font-lock-defaults
  3894. '(org-font-lock-keywords t nil nil backward-paragraph))
  3895. (kill-local-variable 'font-lock-keywords) nil))
  3896. (defvar org-m nil)
  3897. (defvar org-l nil)
  3898. (defvar org-f nil)
  3899. (defun org-get-level-face (n)
  3900. "Get the right face for match N in font-lock matching of headlines."
  3901. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3902. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3903. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3904. (cond
  3905. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3906. ((eq n 2) org-f)
  3907. (t (if org-level-color-stars-only nil org-f))))
  3908. (defun org-get-todo-face (kwd)
  3909. "Get the right face for a TODO keyword KWD.
  3910. If KWD is a number, get the corresponding match group."
  3911. (if (numberp kwd) (setq kwd (match-string kwd)))
  3912. (or (cdr (assoc kwd org-todo-keyword-faces))
  3913. (and (member kwd org-done-keywords) 'org-done)
  3914. 'org-todo))
  3915. (defun org-font-lock-add-tag-faces (limit)
  3916. "Add the special tag faces."
  3917. (when (and org-tag-faces org-tags-special-faces-re)
  3918. (while (re-search-forward org-tags-special-faces-re limit t)
  3919. (add-text-properties (match-beginning 1) (match-end 1)
  3920. (list 'face (org-get-tag-face 1)
  3921. 'font-lock-fontified t))
  3922. (backward-char 1))))
  3923. (defun org-get-tag-face (kwd)
  3924. "Get the right face for a TODO keyword KWD.
  3925. If KWD is a number, get the corresponding match group."
  3926. (if (numberp kwd) (setq kwd (match-string kwd)))
  3927. (or (cdr (assoc kwd org-tag-faces))
  3928. 'org-tag))
  3929. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3930. "Remove fontification and activation overlays from links."
  3931. (font-lock-default-unfontify-region beg end)
  3932. (let* ((buffer-undo-list t)
  3933. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3934. (inhibit-modification-hooks t)
  3935. deactivate-mark buffer-file-name buffer-file-truename)
  3936. (remove-text-properties beg end
  3937. '(mouse-face t keymap t org-linked-text t
  3938. invisible t intangible t))))
  3939. ;;;; Visibility cycling, including org-goto and indirect buffer
  3940. ;;; Cycling
  3941. (defvar org-cycle-global-status nil)
  3942. (make-variable-buffer-local 'org-cycle-global-status)
  3943. (defvar org-cycle-subtree-status nil)
  3944. (make-variable-buffer-local 'org-cycle-subtree-status)
  3945. ;;;###autoload
  3946. (defun org-cycle (&optional arg)
  3947. "Visibility cycling for Org-mode.
  3948. - When this function is called with a prefix argument, rotate the entire
  3949. buffer through 3 states (global cycling)
  3950. 1. OVERVIEW: Show only top-level headlines.
  3951. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3952. 3. SHOW ALL: Show everything.
  3953. When called with two C-u C-u prefixes, switch to the startup visibility,
  3954. determined by the variable `org-startup-folded', and by any VISIBILITY
  3955. properties in the buffer.
  3956. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3957. including drawers.
  3958. - When point is at the beginning of a headline, rotate the subtree started
  3959. by this line through 3 different states (local cycling)
  3960. 1. FOLDED: Only the main headline is shown.
  3961. 2. CHILDREN: The main headline and the direct children are shown.
  3962. From this state, you can move to one of the children
  3963. and zoom in further.
  3964. 3. SUBTREE: Show the entire subtree, including body text.
  3965. - When there is a numeric prefix, go up to a heading with level ARG, do
  3966. a `show-subtree' and return to the previous cursor position. If ARG
  3967. is negative, go up that many levels.
  3968. - When point is not at the beginning of a headline, execute the global
  3969. binding for TAB, which is re-indenting the line. See the option
  3970. `org-cycle-emulate-tab' for details.
  3971. - Special case: if point is at the beginning of the buffer and there is
  3972. no headline in line 1, this function will act as if called with prefix arg.
  3973. But only if also the variable `org-cycle-global-at-bob' is t."
  3974. (interactive "P")
  3975. (org-load-modules-maybe)
  3976. (let* ((outline-regexp
  3977. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3978. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3979. outline-regexp))
  3980. (bob-special (and org-cycle-global-at-bob (bobp)
  3981. (not (looking-at outline-regexp))))
  3982. (org-cycle-hook
  3983. (if bob-special
  3984. (delq 'org-optimize-window-after-visibility-change
  3985. (copy-sequence org-cycle-hook))
  3986. org-cycle-hook))
  3987. (pos (point)))
  3988. (if (or bob-special (equal arg '(4)))
  3989. ;; special case: use global cycling
  3990. (setq arg t))
  3991. (cond
  3992. ((equal arg '(16))
  3993. (org-set-startup-visibility)
  3994. (message "Startup visibility, plus VISIBILITY properties"))
  3995. ((equal arg '(64))
  3996. (show-all)
  3997. (message "Entire buffer visible, including drawers"))
  3998. ((org-at-table-p 'any)
  3999. ;; Enter the table or move to the next field in the table
  4000. (or (org-table-recognize-table.el)
  4001. (progn
  4002. (if arg (org-table-edit-field t)
  4003. (org-table-justify-field-maybe)
  4004. (call-interactively 'org-table-next-field)))))
  4005. ((eq arg t) ;; Global cycling
  4006. (cond
  4007. ((and (eq last-command this-command)
  4008. (eq org-cycle-global-status 'overview))
  4009. ;; We just created the overview - now do table of contents
  4010. ;; This can be slow in very large buffers, so indicate action
  4011. (message "CONTENTS...")
  4012. (org-content)
  4013. (message "CONTENTS...done")
  4014. (setq org-cycle-global-status 'contents)
  4015. (run-hook-with-args 'org-cycle-hook 'contents))
  4016. ((and (eq last-command this-command)
  4017. (eq org-cycle-global-status 'contents))
  4018. ;; We just showed the table of contents - now show everything
  4019. (show-all)
  4020. (message "SHOW ALL")
  4021. (setq org-cycle-global-status 'all)
  4022. (run-hook-with-args 'org-cycle-hook 'all))
  4023. (t
  4024. ;; Default action: go to overview
  4025. (org-overview)
  4026. (message "OVERVIEW")
  4027. (setq org-cycle-global-status 'overview)
  4028. (run-hook-with-args 'org-cycle-hook 'overview))))
  4029. ((and org-drawers org-drawer-regexp
  4030. (save-excursion
  4031. (beginning-of-line 1)
  4032. (looking-at org-drawer-regexp)))
  4033. ;; Toggle block visibility
  4034. (org-flag-drawer
  4035. (not (get-char-property (match-end 0) 'invisible))))
  4036. ((integerp arg)
  4037. ;; Show-subtree, ARG levels up from here.
  4038. (save-excursion
  4039. (org-back-to-heading)
  4040. (outline-up-heading (if (< arg 0) (- arg)
  4041. (- (funcall outline-level) arg)))
  4042. (org-show-subtree)))
  4043. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4044. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4045. ;; At a heading: rotate between three different views
  4046. (org-back-to-heading)
  4047. (let ((goal-column 0) eoh eol eos)
  4048. ;; First, some boundaries
  4049. (save-excursion
  4050. (org-back-to-heading)
  4051. (save-excursion
  4052. (beginning-of-line 2)
  4053. (while (and (not (eobp)) ;; this is like `next-line'
  4054. (get-char-property (1- (point)) 'invisible))
  4055. (beginning-of-line 2)) (setq eol (point)))
  4056. (outline-end-of-heading) (setq eoh (point))
  4057. (org-end-of-subtree t)
  4058. (unless (eobp)
  4059. (skip-chars-forward " \t\n")
  4060. (beginning-of-line 1) ; in case this is an item
  4061. )
  4062. (setq eos (1- (point))))
  4063. ;; Find out what to do next and set `this-command'
  4064. (cond
  4065. ((= eos eoh)
  4066. ;; Nothing is hidden behind this heading
  4067. (message "EMPTY ENTRY")
  4068. (setq org-cycle-subtree-status nil)
  4069. (save-excursion
  4070. (goto-char eos)
  4071. (outline-next-heading)
  4072. (if (org-invisible-p) (org-flag-heading nil))))
  4073. ((or (>= eol eos)
  4074. (not (string-match "\\S-" (buffer-substring eol eos))))
  4075. ;; Entire subtree is hidden in one line: open it
  4076. (org-show-entry)
  4077. (show-children)
  4078. (message "CHILDREN")
  4079. (save-excursion
  4080. (goto-char eos)
  4081. (outline-next-heading)
  4082. (if (org-invisible-p) (org-flag-heading nil)))
  4083. (setq org-cycle-subtree-status 'children)
  4084. (run-hook-with-args 'org-cycle-hook 'children))
  4085. ((and (eq last-command this-command)
  4086. (eq org-cycle-subtree-status 'children))
  4087. ;; We just showed the children, now show everything.
  4088. (org-show-subtree)
  4089. (message "SUBTREE")
  4090. (setq org-cycle-subtree-status 'subtree)
  4091. (run-hook-with-args 'org-cycle-hook 'subtree))
  4092. (t
  4093. ;; Default action: hide the subtree.
  4094. (hide-subtree)
  4095. (message "FOLDED")
  4096. (setq org-cycle-subtree-status 'folded)
  4097. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4098. ;; TAB emulation and template completion
  4099. (buffer-read-only (org-back-to-heading))
  4100. ((org-try-structure-completion))
  4101. ((org-try-cdlatex-tab))
  4102. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4103. (or (not (bolp))
  4104. (not (looking-at outline-regexp))))
  4105. (call-interactively (global-key-binding "\t")))
  4106. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4107. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4108. (or (and (eq org-cycle-emulate-tab 'white)
  4109. (= (match-end 0) (point-at-eol)))
  4110. (and (eq org-cycle-emulate-tab 'whitestart)
  4111. (>= (match-end 0) pos))))
  4112. t
  4113. (eq org-cycle-emulate-tab t))
  4114. (call-interactively (global-key-binding "\t")))
  4115. (t (save-excursion
  4116. (org-back-to-heading)
  4117. (org-cycle))))))
  4118. ;;;###autoload
  4119. (defun org-global-cycle (&optional arg)
  4120. "Cycle the global visibility. For details see `org-cycle'.
  4121. With C-u prefix arg, switch to startup visibility.
  4122. With a numeric prefix, show all headlines up to that level."
  4123. (interactive "P")
  4124. (let ((org-cycle-include-plain-lists
  4125. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4126. (cond
  4127. ((integerp arg)
  4128. (show-all)
  4129. (hide-sublevels arg)
  4130. (setq org-cycle-global-status 'contents))
  4131. ((equal arg '(4))
  4132. (org-set-startup-visibility)
  4133. (message "Startup visibility, plus VISIBILITY properties."))
  4134. (t
  4135. (org-cycle '(4))))))
  4136. (defun org-set-startup-visibility ()
  4137. "Set the visibility required by startup options and properties."
  4138. (cond
  4139. ((eq org-startup-folded t)
  4140. (org-cycle '(4)))
  4141. ((eq org-startup-folded 'content)
  4142. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4143. (org-cycle '(4)) (org-cycle '(4)))))
  4144. (org-set-visibility-according-to-property 'no-cleanup)
  4145. (org-cycle-hide-archived-subtrees 'all)
  4146. (org-cycle-hide-drawers 'all)
  4147. (org-cycle-show-empty-lines 'all))
  4148. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4149. "Switch subtree visibilities according to :VISIBILITY: property."
  4150. (interactive)
  4151. (let (org-show-entry-below state)
  4152. (save-excursion
  4153. (goto-char (point-min))
  4154. (while (re-search-forward
  4155. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4156. nil t)
  4157. (setq state (match-string 1))
  4158. (save-excursion
  4159. (org-back-to-heading t)
  4160. (hide-subtree)
  4161. (org-reveal)
  4162. (cond
  4163. ((equal state '("fold" "folded"))
  4164. (hide-subtree))
  4165. ((equal state "children")
  4166. (org-show-hidden-entry)
  4167. (show-children))
  4168. ((equal state "content")
  4169. (save-excursion
  4170. (save-restriction
  4171. (org-narrow-to-subtree)
  4172. (org-content))))
  4173. ((member state '("all" "showall"))
  4174. (show-subtree)))))
  4175. (unless no-cleanup
  4176. (org-cycle-hide-archived-subtrees 'all)
  4177. (org-cycle-hide-drawers 'all)
  4178. (org-cycle-show-empty-lines 'all)))))
  4179. (defun org-overview ()
  4180. "Switch to overview mode, showing only top-level headlines.
  4181. Really, this shows all headlines with level equal or greater than the level
  4182. of the first headline in the buffer. This is important, because if the
  4183. first headline is not level one, then (hide-sublevels 1) gives confusing
  4184. results."
  4185. (interactive)
  4186. (let ((level (save-excursion
  4187. (goto-char (point-min))
  4188. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4189. (progn
  4190. (goto-char (match-beginning 0))
  4191. (funcall outline-level))))))
  4192. (and level (hide-sublevels level))))
  4193. (defun org-content (&optional arg)
  4194. "Show all headlines in the buffer, like a table of contents.
  4195. With numerical argument N, show content up to level N."
  4196. (interactive "P")
  4197. (save-excursion
  4198. ;; Visit all headings and show their offspring
  4199. (and (integerp arg) (org-overview))
  4200. (goto-char (point-max))
  4201. (catch 'exit
  4202. (while (and (progn (condition-case nil
  4203. (outline-previous-visible-heading 1)
  4204. (error (goto-char (point-min))))
  4205. t)
  4206. (looking-at outline-regexp))
  4207. (if (integerp arg)
  4208. (show-children (1- arg))
  4209. (show-branches))
  4210. (if (bobp) (throw 'exit nil))))))
  4211. (defun org-optimize-window-after-visibility-change (state)
  4212. "Adjust the window after a change in outline visibility.
  4213. This function is the default value of the hook `org-cycle-hook'."
  4214. (when (get-buffer-window (current-buffer))
  4215. (cond
  4216. ((eq state 'content) nil)
  4217. ((eq state 'all) nil)
  4218. ((eq state 'folded) nil)
  4219. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4220. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4221. (defun org-compact-display-after-subtree-move ()
  4222. "Show a compacter version of the tree of the entry's parent."
  4223. (save-excursion
  4224. (if (org-up-heading-safe)
  4225. (progn
  4226. (hide-subtree)
  4227. (show-entry)
  4228. (show-children)
  4229. (org-cycle-show-empty-lines 'children)
  4230. (org-cycle-hide-drawers 'children))
  4231. (org-overview))))
  4232. (defun org-cycle-show-empty-lines (state)
  4233. "Show empty lines above all visible headlines.
  4234. The region to be covered depends on STATE when called through
  4235. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4236. entire buffer covered. Note that an empty line is only shown if there
  4237. are at least `org-cycle-separator-lines' empty lines before the headline."
  4238. (when (> org-cycle-separator-lines 0)
  4239. (save-excursion
  4240. (let* ((n org-cycle-separator-lines)
  4241. (re (cond
  4242. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4243. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4244. (t (let ((ns (number-to-string (- n 2))))
  4245. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4246. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4247. beg end)
  4248. (cond
  4249. ((memq state '(overview contents t))
  4250. (setq beg (point-min) end (point-max)))
  4251. ((memq state '(children folded))
  4252. (setq beg (point) end (progn (org-end-of-subtree t t)
  4253. (beginning-of-line 2)
  4254. (point)))))
  4255. (when beg
  4256. (goto-char beg)
  4257. (while (re-search-forward re end t)
  4258. (if (not (get-char-property (match-end 1) 'invisible))
  4259. (outline-flag-region
  4260. (match-beginning 1) (match-end 1) nil)))))))
  4261. ;; Never hide empty lines at the end of the file.
  4262. (save-excursion
  4263. (goto-char (point-max))
  4264. (outline-previous-heading)
  4265. (outline-end-of-heading)
  4266. (if (and (looking-at "[ \t\n]+")
  4267. (= (match-end 0) (point-max)))
  4268. (outline-flag-region (point) (match-end 0) nil))))
  4269. (defun org-show-empty-lines-in-parent ()
  4270. "Move to the parent and re-show empty lines before visible headlines."
  4271. (save-excursion
  4272. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4273. (org-cycle-show-empty-lines context))))
  4274. (defun org-cycle-hide-drawers (state)
  4275. "Re-hide all drawers after a visibility state change."
  4276. (when (and (org-mode-p)
  4277. (not (memq state '(overview folded))))
  4278. (save-excursion
  4279. (let* ((globalp (memq state '(contents all)))
  4280. (beg (if globalp (point-min) (point)))
  4281. (end (if globalp (point-max) (org-end-of-subtree t))))
  4282. (goto-char beg)
  4283. (while (re-search-forward org-drawer-regexp end t)
  4284. (org-flag-drawer t))))))
  4285. (defun org-flag-drawer (flag)
  4286. (save-excursion
  4287. (beginning-of-line 1)
  4288. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4289. (let ((b (match-end 0))
  4290. (outline-regexp org-outline-regexp))
  4291. (if (re-search-forward
  4292. "^[ \t]*:END:"
  4293. (save-excursion (outline-next-heading) (point)) t)
  4294. (outline-flag-region b (point-at-eol) flag)
  4295. (error ":END: line missing"))))))
  4296. (defun org-subtree-end-visible-p ()
  4297. "Is the end of the current subtree visible?"
  4298. (pos-visible-in-window-p
  4299. (save-excursion (org-end-of-subtree t) (point))))
  4300. (defun org-first-headline-recenter (&optional N)
  4301. "Move cursor to the first headline and recenter the headline.
  4302. Optional argument N means, put the headline into the Nth line of the window."
  4303. (goto-char (point-min))
  4304. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4305. (beginning-of-line)
  4306. (recenter (prefix-numeric-value N))))
  4307. ;;; Org-goto
  4308. (defvar org-goto-window-configuration nil)
  4309. (defvar org-goto-marker nil)
  4310. (defvar org-goto-map
  4311. (let ((map (make-sparse-keymap)))
  4312. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4313. (while (setq cmd (pop cmds))
  4314. (substitute-key-definition cmd cmd map global-map)))
  4315. (suppress-keymap map)
  4316. (org-defkey map "\C-m" 'org-goto-ret)
  4317. (org-defkey map [(return)] 'org-goto-ret)
  4318. (org-defkey map [(left)] 'org-goto-left)
  4319. (org-defkey map [(right)] 'org-goto-right)
  4320. (org-defkey map [(control ?g)] 'org-goto-quit)
  4321. (org-defkey map "\C-i" 'org-cycle)
  4322. (org-defkey map [(tab)] 'org-cycle)
  4323. (org-defkey map [(down)] 'outline-next-visible-heading)
  4324. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4325. (if org-goto-auto-isearch
  4326. (if (fboundp 'define-key-after)
  4327. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4328. nil)
  4329. (org-defkey map "q" 'org-goto-quit)
  4330. (org-defkey map "n" 'outline-next-visible-heading)
  4331. (org-defkey map "p" 'outline-previous-visible-heading)
  4332. (org-defkey map "f" 'outline-forward-same-level)
  4333. (org-defkey map "b" 'outline-backward-same-level)
  4334. (org-defkey map "u" 'outline-up-heading))
  4335. (org-defkey map "/" 'org-occur)
  4336. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4337. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4338. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4339. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4340. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4341. map))
  4342. (defconst org-goto-help
  4343. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4344. RET=jump to location [Q]uit and return to previous location
  4345. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4346. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4347. ;; FIXME: Docstring doe not mention both interfaces
  4348. (defun org-goto (&optional alternative-interface)
  4349. "Look up a different location in the current file, keeping current visibility.
  4350. When you want look-up or go to a different location in a document, the
  4351. fastest way is often to fold the entire buffer and then dive into the tree.
  4352. This method has the disadvantage, that the previous location will be folded,
  4353. which may not be what you want.
  4354. This command works around this by showing a copy of the current buffer
  4355. in an indirect buffer, in overview mode. You can dive into the tree in
  4356. that copy, use org-occur and incremental search to find a location.
  4357. When pressing RET or `Q', the command returns to the original buffer in
  4358. which the visibility is still unchanged. After RET is will also jump to
  4359. the location selected in the indirect buffer and expose the
  4360. the headline hierarchy above."
  4361. (interactive "P")
  4362. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4363. (org-refile-use-outline-path t)
  4364. (interface
  4365. (if (not alternative-interface)
  4366. org-goto-interface
  4367. (if (eq org-goto-interface 'outline)
  4368. 'outline-path-completion
  4369. 'outline)))
  4370. (org-goto-start-pos (point))
  4371. (selected-point
  4372. (if (eq interface 'outline)
  4373. (car (org-get-location (current-buffer) org-goto-help))
  4374. (nth 3 (org-refile-get-location "Goto: ")))))
  4375. (if selected-point
  4376. (progn
  4377. (org-mark-ring-push org-goto-start-pos)
  4378. (goto-char selected-point)
  4379. (if (or (org-invisible-p) (org-invisible-p2))
  4380. (org-show-context 'org-goto)))
  4381. (message "Quit"))))
  4382. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4383. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4384. (defvar org-goto-local-auto-isearch-map) ; defined below
  4385. (defun org-get-location (buf help)
  4386. "Let the user select a location in the Org-mode buffer BUF.
  4387. This function uses a recursive edit. It returns the selected position
  4388. or nil."
  4389. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4390. (isearch-hide-immediately nil)
  4391. (isearch-search-fun-function
  4392. (lambda () 'org-goto-local-search-headings))
  4393. (org-goto-selected-point org-goto-exit-command))
  4394. (save-excursion
  4395. (save-window-excursion
  4396. (delete-other-windows)
  4397. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4398. (switch-to-buffer
  4399. (condition-case nil
  4400. (make-indirect-buffer (current-buffer) "*org-goto*")
  4401. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4402. (with-output-to-temp-buffer "*Help*"
  4403. (princ help))
  4404. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4405. (setq buffer-read-only nil)
  4406. (let ((org-startup-truncated t)
  4407. (org-startup-folded nil)
  4408. (org-startup-align-all-tables nil))
  4409. (org-mode)
  4410. (org-overview))
  4411. (setq buffer-read-only t)
  4412. (if (and (boundp 'org-goto-start-pos)
  4413. (integer-or-marker-p org-goto-start-pos))
  4414. (let ((org-show-hierarchy-above t)
  4415. (org-show-siblings t)
  4416. (org-show-following-heading t))
  4417. (goto-char org-goto-start-pos)
  4418. (and (org-invisible-p) (org-show-context)))
  4419. (goto-char (point-min)))
  4420. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4421. (message "Select location and press RET")
  4422. (use-local-map org-goto-map)
  4423. (recursive-edit)
  4424. ))
  4425. (kill-buffer "*org-goto*")
  4426. (cons org-goto-selected-point org-goto-exit-command)))
  4427. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4428. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4429. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4430. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4431. (defun org-goto-local-search-headings (string bound noerror)
  4432. "Search and make sure that any matches are in headlines."
  4433. (catch 'return
  4434. (while (if isearch-forward
  4435. (search-forward string bound noerror)
  4436. (search-backward string bound noerror))
  4437. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4438. (and (member :headline context)
  4439. (not (member :tags context))))
  4440. (throw 'return (point))))))
  4441. (defun org-goto-local-auto-isearch ()
  4442. "Start isearch."
  4443. (interactive)
  4444. (goto-char (point-min))
  4445. (let ((keys (this-command-keys)))
  4446. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4447. (isearch-mode t)
  4448. (isearch-process-search-char (string-to-char keys)))))
  4449. (defun org-goto-ret (&optional arg)
  4450. "Finish `org-goto' by going to the new location."
  4451. (interactive "P")
  4452. (setq org-goto-selected-point (point)
  4453. org-goto-exit-command 'return)
  4454. (throw 'exit nil))
  4455. (defun org-goto-left ()
  4456. "Finish `org-goto' by going to the new location."
  4457. (interactive)
  4458. (if (org-on-heading-p)
  4459. (progn
  4460. (beginning-of-line 1)
  4461. (setq org-goto-selected-point (point)
  4462. org-goto-exit-command 'left)
  4463. (throw 'exit nil))
  4464. (error "Not on a heading")))
  4465. (defun org-goto-right ()
  4466. "Finish `org-goto' by going to the new location."
  4467. (interactive)
  4468. (if (org-on-heading-p)
  4469. (progn
  4470. (setq org-goto-selected-point (point)
  4471. org-goto-exit-command 'right)
  4472. (throw 'exit nil))
  4473. (error "Not on a heading")))
  4474. (defun org-goto-quit ()
  4475. "Finish `org-goto' without cursor motion."
  4476. (interactive)
  4477. (setq org-goto-selected-point nil)
  4478. (setq org-goto-exit-command 'quit)
  4479. (throw 'exit nil))
  4480. ;;; Indirect buffer display of subtrees
  4481. (defvar org-indirect-dedicated-frame nil
  4482. "This is the frame being used for indirect tree display.")
  4483. (defvar org-last-indirect-buffer nil)
  4484. (defun org-tree-to-indirect-buffer (&optional arg)
  4485. "Create indirect buffer and narrow it to current subtree.
  4486. With numerical prefix ARG, go up to this level and then take that tree.
  4487. If ARG is negative, go up that many levels.
  4488. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4489. indirect buffer previously made with this command, to avoid proliferation of
  4490. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4491. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4492. is kept so that you can work with several indirect buffers at the same time.
  4493. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4494. requests that a new frame be made for the new buffer, so that the dedicated
  4495. frame is not changed."
  4496. (interactive "P")
  4497. (let ((cbuf (current-buffer))
  4498. (cwin (selected-window))
  4499. (pos (point))
  4500. beg end level heading ibuf)
  4501. (save-excursion
  4502. (org-back-to-heading t)
  4503. (when (numberp arg)
  4504. (setq level (org-outline-level))
  4505. (if (< arg 0) (setq arg (+ level arg)))
  4506. (while (> (setq level (org-outline-level)) arg)
  4507. (outline-up-heading 1 t)))
  4508. (setq beg (point)
  4509. heading (org-get-heading))
  4510. (org-end-of-subtree t) (setq end (point)))
  4511. (if (and (buffer-live-p org-last-indirect-buffer)
  4512. (not (eq org-indirect-buffer-display 'new-frame))
  4513. (not arg))
  4514. (kill-buffer org-last-indirect-buffer))
  4515. (setq ibuf (org-get-indirect-buffer cbuf)
  4516. org-last-indirect-buffer ibuf)
  4517. (cond
  4518. ((or (eq org-indirect-buffer-display 'new-frame)
  4519. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4520. (select-frame (make-frame))
  4521. (delete-other-windows)
  4522. (switch-to-buffer ibuf)
  4523. (org-set-frame-title heading))
  4524. ((eq org-indirect-buffer-display 'dedicated-frame)
  4525. (raise-frame
  4526. (select-frame (or (and org-indirect-dedicated-frame
  4527. (frame-live-p org-indirect-dedicated-frame)
  4528. org-indirect-dedicated-frame)
  4529. (setq org-indirect-dedicated-frame (make-frame)))))
  4530. (delete-other-windows)
  4531. (switch-to-buffer ibuf)
  4532. (org-set-frame-title (concat "Indirect: " heading)))
  4533. ((eq org-indirect-buffer-display 'current-window)
  4534. (switch-to-buffer ibuf))
  4535. ((eq org-indirect-buffer-display 'other-window)
  4536. (pop-to-buffer ibuf))
  4537. (t (error "Invalid value.")))
  4538. (if (featurep 'xemacs)
  4539. (save-excursion (org-mode) (turn-on-font-lock)))
  4540. (narrow-to-region beg end)
  4541. (show-all)
  4542. (goto-char pos)
  4543. (and (window-live-p cwin) (select-window cwin))))
  4544. (defun org-get-indirect-buffer (&optional buffer)
  4545. (setq buffer (or buffer (current-buffer)))
  4546. (let ((n 1) (base (buffer-name buffer)) bname)
  4547. (while (buffer-live-p
  4548. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4549. (setq n (1+ n)))
  4550. (condition-case nil
  4551. (make-indirect-buffer buffer bname 'clone)
  4552. (error (make-indirect-buffer buffer bname)))))
  4553. (defun org-set-frame-title (title)
  4554. "Set the title of the current frame to the string TITLE."
  4555. ;; FIXME: how to name a single frame in XEmacs???
  4556. (unless (featurep 'xemacs)
  4557. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4558. ;;;; Structure editing
  4559. ;;; Inserting headlines
  4560. (defun org-previous-line-empty-p ()
  4561. (save-excursion
  4562. (and (not (bobp))
  4563. (or (beginning-of-line 0) t)
  4564. (save-match-data
  4565. (looking-at "[ \t]*$")))))
  4566. (defun org-insert-heading (&optional force-heading)
  4567. "Insert a new heading or item with same depth at point.
  4568. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4569. If point is at the beginning of a headline, insert a sibling before the
  4570. current headline. If point is not at the beginning, do not split the line,
  4571. but create the new headline after the current line."
  4572. (interactive "P")
  4573. (if (= (buffer-size) 0)
  4574. (insert "\n* ")
  4575. (when (or force-heading (not (org-insert-item)))
  4576. (let* ((empty-line-p nil)
  4577. (head (save-excursion
  4578. (condition-case nil
  4579. (progn
  4580. (org-back-to-heading)
  4581. (setq empty-line-p (org-previous-line-empty-p))
  4582. (match-string 0))
  4583. (error "*"))))
  4584. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4585. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4586. pos hide-previous previous-pos)
  4587. (cond
  4588. ((and (org-on-heading-p) (bolp)
  4589. (or (bobp)
  4590. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4591. ;; insert before the current line
  4592. (open-line (if blank 2 1)))
  4593. ((and (bolp)
  4594. (or (bobp)
  4595. (save-excursion
  4596. (backward-char 1) (not (org-invisible-p)))))
  4597. ;; insert right here
  4598. nil)
  4599. (t
  4600. ;; somewhere in the line
  4601. (save-excursion
  4602. (setq previous-pos (point-at-bol))
  4603. (end-of-line)
  4604. (setq hide-previous (org-invisible-p)))
  4605. (and org-insert-heading-respect-content (org-show-subtree))
  4606. (let ((split
  4607. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4608. (save-excursion
  4609. (let ((p (point)))
  4610. (goto-char (point-at-bol))
  4611. (and (looking-at org-complex-heading-regexp)
  4612. (> p (match-beginning 4)))))))
  4613. tags pos)
  4614. (cond
  4615. (org-insert-heading-respect-content
  4616. (org-end-of-subtree nil t)
  4617. (or (bolp) (newline))
  4618. (or (org-previous-line-empty-p)
  4619. (and blank (newline)))
  4620. (open-line 1))
  4621. ((org-on-heading-p)
  4622. (when hide-previous
  4623. (show-children)
  4624. (org-show-entry))
  4625. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4626. (setq tags (and (match-end 2) (match-string 2)))
  4627. (and (match-end 1)
  4628. (delete-region (match-beginning 1) (match-end 1)))
  4629. (setq pos (point-at-bol))
  4630. (or split (end-of-line 1))
  4631. (delete-horizontal-space)
  4632. (newline (if blank 2 1))
  4633. (when tags
  4634. (save-excursion
  4635. (goto-char pos)
  4636. (end-of-line 1)
  4637. (insert " " tags)
  4638. (org-set-tags nil 'align))))
  4639. (t
  4640. (or split (end-of-line 1))
  4641. (newline (if blank 2 1)))))))
  4642. (insert head) (just-one-space)
  4643. (setq pos (point))
  4644. (end-of-line 1)
  4645. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4646. (when (and org-insert-heading-respect-content hide-previous)
  4647. (save-excursion
  4648. (goto-char previous-pos)
  4649. (hide-subtree)))
  4650. (run-hooks 'org-insert-heading-hook)))))
  4651. (defun org-get-heading (&optional no-tags)
  4652. "Return the heading of the current entry, without the stars."
  4653. (save-excursion
  4654. (org-back-to-heading t)
  4655. (if (looking-at
  4656. (if no-tags
  4657. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4658. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4659. (match-string 1) "")))
  4660. (defun org-heading-components ()
  4661. "Return the components of the current heading.
  4662. This is a list with the following elements:
  4663. - the level as an integer
  4664. - the reduced level, different if `org-odd-levels-only' is set.
  4665. - the TODO keyword, or nil
  4666. - the priority character, like ?A, or nil if no priority is given
  4667. - the headline text itself, or the tags string if no headline text
  4668. - the tags string, or nil."
  4669. (save-excursion
  4670. (org-back-to-heading t)
  4671. (if (looking-at org-complex-heading-regexp)
  4672. (list (length (match-string 1))
  4673. (org-reduced-level (length (match-string 1)))
  4674. (org-match-string-no-properties 2)
  4675. (and (match-end 3) (aref (match-string 3) 2))
  4676. (org-match-string-no-properties 4)
  4677. (org-match-string-no-properties 5)))))
  4678. (defun org-insert-heading-after-current ()
  4679. "Insert a new heading with same level as current, after current subtree."
  4680. (interactive)
  4681. (org-back-to-heading)
  4682. (org-insert-heading)
  4683. (org-move-subtree-down)
  4684. (end-of-line 1))
  4685. (defun org-insert-heading-respect-content ()
  4686. (interactive)
  4687. (let ((org-insert-heading-respect-content t))
  4688. (org-insert-heading t)))
  4689. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4690. (interactive "P")
  4691. (let ((org-insert-heading-respect-content t))
  4692. (org-insert-todo-heading force-state t)))
  4693. (defun org-insert-todo-heading (arg &optional force-heading)
  4694. "Insert a new heading with the same level and TODO state as current heading.
  4695. If the heading has no TODO state, or if the state is DONE, use the first
  4696. state (TODO by default). Also with prefix arg, force first state."
  4697. (interactive "P")
  4698. (when (or force-heading (not (org-insert-item 'checkbox)))
  4699. (org-insert-heading force-heading)
  4700. (save-excursion
  4701. (org-back-to-heading)
  4702. (outline-previous-heading)
  4703. (looking-at org-todo-line-regexp))
  4704. (let*
  4705. ((new-mark-x
  4706. (if (or arg
  4707. (not (match-beginning 2))
  4708. (member (match-string 2) org-done-keywords))
  4709. (car org-todo-keywords-1)
  4710. (match-string 2)))
  4711. (new-mark
  4712. (or
  4713. (run-hook-with-args-until-success
  4714. 'org-todo-get-default-hook new-mark-x nil)
  4715. new-mark-x)))
  4716. (insert new-mark " "))
  4717. (when org-provide-todo-statistics
  4718. (org-update-parent-todo-statistics))))
  4719. (defun org-insert-subheading (arg)
  4720. "Insert a new subheading and demote it.
  4721. Works for outline headings and for plain lists alike."
  4722. (interactive "P")
  4723. (org-insert-heading arg)
  4724. (cond
  4725. ((org-on-heading-p) (org-do-demote))
  4726. ((org-at-item-p) (org-indent-item 1))))
  4727. (defun org-insert-todo-subheading (arg)
  4728. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4729. Works for outline headings and for plain lists alike."
  4730. (interactive "P")
  4731. (org-insert-todo-heading arg)
  4732. (cond
  4733. ((org-on-heading-p) (org-do-demote))
  4734. ((org-at-item-p) (org-indent-item 1))))
  4735. ;;; Promotion and Demotion
  4736. (defun org-promote-subtree ()
  4737. "Promote the entire subtree.
  4738. See also `org-promote'."
  4739. (interactive)
  4740. (save-excursion
  4741. (org-map-tree 'org-promote))
  4742. (org-fix-position-after-promote))
  4743. (defun org-demote-subtree ()
  4744. "Demote the entire subtree. See `org-demote'.
  4745. See also `org-promote'."
  4746. (interactive)
  4747. (save-excursion
  4748. (org-map-tree 'org-demote))
  4749. (org-fix-position-after-promote))
  4750. (defun org-do-promote ()
  4751. "Promote the current heading higher up the tree.
  4752. If the region is active in `transient-mark-mode', promote all headings
  4753. in the region."
  4754. (interactive)
  4755. (save-excursion
  4756. (if (org-region-active-p)
  4757. (org-map-region 'org-promote (region-beginning) (region-end))
  4758. (org-promote)))
  4759. (org-fix-position-after-promote))
  4760. (defun org-do-demote ()
  4761. "Demote the current heading lower down the tree.
  4762. If the region is active in `transient-mark-mode', demote all headings
  4763. in the region."
  4764. (interactive)
  4765. (save-excursion
  4766. (if (org-region-active-p)
  4767. (org-map-region 'org-demote (region-beginning) (region-end))
  4768. (org-demote)))
  4769. (org-fix-position-after-promote))
  4770. (defun org-fix-position-after-promote ()
  4771. "Make sure that after pro/demotion cursor position is right."
  4772. (let ((pos (point)))
  4773. (when (save-excursion
  4774. (beginning-of-line 1)
  4775. (looking-at org-todo-line-regexp)
  4776. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4777. (cond ((eobp) (insert " "))
  4778. ((eolp) (insert " "))
  4779. ((equal (char-after) ?\ ) (forward-char 1))))))
  4780. (defun org-reduced-level (l)
  4781. "Compute the effective level of a heading.
  4782. This takes into account the setting of `org-odd-levels-only'."
  4783. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4784. (defun org-get-valid-level (level &optional change)
  4785. "Rectify a level change under the influence of `org-odd-levels-only'
  4786. LEVEL is a current level, CHANGE is by how much the level should be
  4787. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4788. even level numbers will become the next higher odd number."
  4789. (if org-odd-levels-only
  4790. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4791. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4792. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4793. (max 1 (+ level (or change 0)))))
  4794. (if (boundp 'define-obsolete-function-alias)
  4795. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4796. (define-obsolete-function-alias 'org-get-legal-level
  4797. 'org-get-valid-level)
  4798. (define-obsolete-function-alias 'org-get-legal-level
  4799. 'org-get-valid-level "23.1")))
  4800. (defun org-promote ()
  4801. "Promote the current heading higher up the tree.
  4802. If the region is active in `transient-mark-mode', promote all headings
  4803. in the region."
  4804. (org-back-to-heading t)
  4805. (let* ((level (save-match-data (funcall outline-level)))
  4806. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4807. (diff (abs (- level (length up-head) -1))))
  4808. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4809. (replace-match up-head nil t)
  4810. ;; Fixup tag positioning
  4811. (and org-auto-align-tags (org-set-tags nil t))
  4812. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4813. (defun org-demote ()
  4814. "Demote the current heading lower down the tree.
  4815. If the region is active in `transient-mark-mode', demote all headings
  4816. in the region."
  4817. (org-back-to-heading t)
  4818. (let* ((level (save-match-data (funcall outline-level)))
  4819. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4820. (diff (abs (- level (length down-head) -1))))
  4821. (replace-match down-head nil t)
  4822. ;; Fixup tag positioning
  4823. (and org-auto-align-tags (org-set-tags nil t))
  4824. (if org-adapt-indentation (org-fixup-indentation diff))))
  4825. (defun org-map-tree (fun)
  4826. "Call FUN for every heading underneath the current one."
  4827. (org-back-to-heading)
  4828. (let ((level (funcall outline-level)))
  4829. (save-excursion
  4830. (funcall fun)
  4831. (while (and (progn
  4832. (outline-next-heading)
  4833. (> (funcall outline-level) level))
  4834. (not (eobp)))
  4835. (funcall fun)))))
  4836. (defun org-map-region (fun beg end)
  4837. "Call FUN for every heading between BEG and END."
  4838. (let ((org-ignore-region t))
  4839. (save-excursion
  4840. (setq end (copy-marker end))
  4841. (goto-char beg)
  4842. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4843. (< (point) end))
  4844. (funcall fun))
  4845. (while (and (progn
  4846. (outline-next-heading)
  4847. (< (point) end))
  4848. (not (eobp)))
  4849. (funcall fun)))))
  4850. (defun org-fixup-indentation (diff)
  4851. "Change the indentation in the current entry by DIFF
  4852. However, if any line in the current entry has no indentation, or if it
  4853. would end up with no indentation after the change, nothing at all is done."
  4854. (save-excursion
  4855. (let ((end (save-excursion (outline-next-heading)
  4856. (point-marker)))
  4857. (prohibit (if (> diff 0)
  4858. "^\\S-"
  4859. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4860. col)
  4861. (unless (save-excursion (end-of-line 1)
  4862. (re-search-forward prohibit end t))
  4863. (while (and (< (point) end)
  4864. (re-search-forward "^[ \t]+" end t))
  4865. (goto-char (match-end 0))
  4866. (setq col (current-column))
  4867. (if (< diff 0) (replace-match ""))
  4868. (org-indent-to-column (+ diff col))))
  4869. (move-marker end nil))))
  4870. (defun org-convert-to-odd-levels ()
  4871. "Convert an org-mode file with all levels allowed to one with odd levels.
  4872. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4873. level 5 etc."
  4874. (interactive)
  4875. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4876. (let ((org-odd-levels-only nil) n)
  4877. (save-excursion
  4878. (goto-char (point-min))
  4879. (while (re-search-forward "^\\*\\*+ " nil t)
  4880. (setq n (- (length (match-string 0)) 2))
  4881. (while (>= (setq n (1- n)) 0)
  4882. (org-demote))
  4883. (end-of-line 1))))))
  4884. (defun org-convert-to-oddeven-levels ()
  4885. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4886. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4887. section with an even level, conversion would destroy the structure of the file. An error
  4888. is signaled in this case."
  4889. (interactive)
  4890. (goto-char (point-min))
  4891. ;; First check if there are no even levels
  4892. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4893. (org-show-context t)
  4894. (error "Not all levels are odd in this file. Conversion not possible."))
  4895. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4896. (let ((org-odd-levels-only nil) n)
  4897. (save-excursion
  4898. (goto-char (point-min))
  4899. (while (re-search-forward "^\\*\\*+ " nil t)
  4900. (setq n (/ (1- (length (match-string 0))) 2))
  4901. (while (>= (setq n (1- n)) 0)
  4902. (org-promote))
  4903. (end-of-line 1))))))
  4904. (defun org-tr-level (n)
  4905. "Make N odd if required."
  4906. (if org-odd-levels-only (1+ (/ n 2)) n))
  4907. ;;; Vertical tree motion, cutting and pasting of subtrees
  4908. (defun org-move-subtree-up (&optional arg)
  4909. "Move the current subtree up past ARG headlines of the same level."
  4910. (interactive "p")
  4911. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4912. (defun org-move-subtree-down (&optional arg)
  4913. "Move the current subtree down past ARG headlines of the same level."
  4914. (interactive "p")
  4915. (setq arg (prefix-numeric-value arg))
  4916. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4917. 'outline-get-last-sibling))
  4918. (ins-point (make-marker))
  4919. (cnt (abs arg))
  4920. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4921. ;; Select the tree
  4922. (org-back-to-heading)
  4923. (setq beg0 (point))
  4924. (save-excursion
  4925. (setq ne-beg (org-back-over-empty-lines))
  4926. (setq beg (point)))
  4927. (save-match-data
  4928. (save-excursion (outline-end-of-heading)
  4929. (setq folded (org-invisible-p)))
  4930. (outline-end-of-subtree))
  4931. (outline-next-heading)
  4932. (setq ne-end (org-back-over-empty-lines))
  4933. (setq end (point))
  4934. (goto-char beg0)
  4935. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4936. ;; include less whitespace
  4937. (save-excursion
  4938. (goto-char beg)
  4939. (forward-line (- ne-beg ne-end))
  4940. (setq beg (point))))
  4941. ;; Find insertion point, with error handling
  4942. (while (> cnt 0)
  4943. (or (and (funcall movfunc) (looking-at outline-regexp))
  4944. (progn (goto-char beg0)
  4945. (error "Cannot move past superior level or buffer limit")))
  4946. (setq cnt (1- cnt)))
  4947. (if (> arg 0)
  4948. ;; Moving forward - still need to move over subtree
  4949. (progn (org-end-of-subtree t t)
  4950. (save-excursion
  4951. (org-back-over-empty-lines)
  4952. (or (bolp) (newline)))))
  4953. (setq ne-ins (org-back-over-empty-lines))
  4954. (move-marker ins-point (point))
  4955. (setq txt (buffer-substring beg end))
  4956. (org-save-markers-in-region beg end)
  4957. (delete-region beg end)
  4958. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4959. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4960. (let ((bbb (point)))
  4961. (insert-before-markers txt)
  4962. (org-reinstall-markers-in-region bbb)
  4963. (move-marker ins-point bbb))
  4964. (or (bolp) (insert "\n"))
  4965. (setq ins-end (point))
  4966. (goto-char ins-point)
  4967. (org-skip-whitespace)
  4968. (when (and (< arg 0)
  4969. (org-first-sibling-p)
  4970. (> ne-ins ne-beg))
  4971. ;; Move whitespace back to beginning
  4972. (save-excursion
  4973. (goto-char ins-end)
  4974. (let ((kill-whole-line t))
  4975. (kill-line (- ne-ins ne-beg)) (point)))
  4976. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4977. (move-marker ins-point nil)
  4978. (org-compact-display-after-subtree-move)
  4979. (org-show-empty-lines-in-parent)
  4980. (unless folded
  4981. (org-show-entry)
  4982. (show-children)
  4983. (org-cycle-hide-drawers 'children))))
  4984. (defvar org-subtree-clip ""
  4985. "Clipboard for cut and paste of subtrees.
  4986. This is actually only a copy of the kill, because we use the normal kill
  4987. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4988. (defvar org-subtree-clip-folded nil
  4989. "Was the last copied subtree folded?
  4990. This is used to fold the tree back after pasting.")
  4991. (defun org-cut-subtree (&optional n)
  4992. "Cut the current subtree into the clipboard.
  4993. With prefix arg N, cut this many sequential subtrees.
  4994. This is a short-hand for marking the subtree and then cutting it."
  4995. (interactive "p")
  4996. (org-copy-subtree n 'cut))
  4997. (defun org-copy-subtree (&optional n cut force-store-markers)
  4998. "Cut the current subtree into the clipboard.
  4999. With prefix arg N, cut this many sequential subtrees.
  5000. This is a short-hand for marking the subtree and then copying it.
  5001. If CUT is non-nil, actually cut the subtree.
  5002. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5003. of some markers in the region, even if CUT is non-nil. This is
  5004. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5005. (interactive "p")
  5006. (let (beg end folded (beg0 (point)))
  5007. (if (interactive-p)
  5008. (org-back-to-heading nil) ; take what looks like a subtree
  5009. (org-back-to-heading t)) ; take what is really there
  5010. (org-back-over-empty-lines)
  5011. (setq beg (point))
  5012. (skip-chars-forward " \t\r\n")
  5013. (save-match-data
  5014. (save-excursion (outline-end-of-heading)
  5015. (setq folded (org-invisible-p)))
  5016. (condition-case nil
  5017. (outline-forward-same-level (1- n))
  5018. (error nil))
  5019. (org-end-of-subtree t t))
  5020. (org-back-over-empty-lines)
  5021. (setq end (point))
  5022. (goto-char beg0)
  5023. (when (> end beg)
  5024. (setq org-subtree-clip-folded folded)
  5025. (when (or cut force-store-markers)
  5026. (org-save-markers-in-region beg end))
  5027. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5028. (setq org-subtree-clip (current-kill 0))
  5029. (message "%s: Subtree(s) with %d characters"
  5030. (if cut "Cut" "Copied")
  5031. (length org-subtree-clip)))))
  5032. (defun org-paste-subtree (&optional level tree for-yank)
  5033. "Paste the clipboard as a subtree, with modification of headline level.
  5034. The entire subtree is promoted or demoted in order to match a new headline
  5035. level.
  5036. If the cursor is at the beginning of a headline, the same level as
  5037. that headline is used to paste the tree
  5038. If not, the new level is derived from the *visible* headings
  5039. before and after the insertion point, and taken to be the inferior headline
  5040. level of the two. So if the previous visible heading is level 3 and the
  5041. next is level 4 (or vice versa), level 4 will be used for insertion.
  5042. This makes sure that the subtree remains an independent subtree and does
  5043. not swallow low level entries.
  5044. You can also force a different level, either by using a numeric prefix
  5045. argument, or by inserting the heading marker by hand. For example, if the
  5046. cursor is after \"*****\", then the tree will be shifted to level 5.
  5047. If optional TREE is given, use this text instead of the kill ring.
  5048. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5049. move back over whitespace before inserting, and move point to the end of
  5050. the inserted text when done."
  5051. (interactive "P")
  5052. (unless (org-kill-is-subtree-p tree)
  5053. (error "%s"
  5054. (substitute-command-keys
  5055. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5056. (let* ((visp (not (org-invisible-p)))
  5057. (txt (or tree (and kill-ring (current-kill 0))))
  5058. (^re (concat "^\\(" outline-regexp "\\)"))
  5059. (re (concat "\\(" outline-regexp "\\)"))
  5060. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5061. (old-level (if (string-match ^re txt)
  5062. (- (match-end 0) (match-beginning 0) 1)
  5063. -1))
  5064. (force-level (cond (level (prefix-numeric-value level))
  5065. ((and (looking-at "[ \t]*$")
  5066. (string-match
  5067. ^re_ (buffer-substring
  5068. (point-at-bol) (point))))
  5069. (- (match-end 1) (match-beginning 1)))
  5070. ((and (bolp)
  5071. (looking-at org-outline-regexp))
  5072. (- (match-end 0) (point) 1))
  5073. (t nil)))
  5074. (previous-level (save-excursion
  5075. (condition-case nil
  5076. (progn
  5077. (outline-previous-visible-heading 1)
  5078. (if (looking-at re)
  5079. (- (match-end 0) (match-beginning 0) 1)
  5080. 1))
  5081. (error 1))))
  5082. (next-level (save-excursion
  5083. (condition-case nil
  5084. (progn
  5085. (or (looking-at outline-regexp)
  5086. (outline-next-visible-heading 1))
  5087. (if (looking-at re)
  5088. (- (match-end 0) (match-beginning 0) 1)
  5089. 1))
  5090. (error 1))))
  5091. (new-level (or force-level (max previous-level next-level)))
  5092. (shift (if (or (= old-level -1)
  5093. (= new-level -1)
  5094. (= old-level new-level))
  5095. 0
  5096. (- new-level old-level)))
  5097. (delta (if (> shift 0) -1 1))
  5098. (func (if (> shift 0) 'org-demote 'org-promote))
  5099. (org-odd-levels-only nil)
  5100. beg end newend)
  5101. ;; Remove the forced level indicator
  5102. (if force-level
  5103. (delete-region (point-at-bol) (point)))
  5104. ;; Paste
  5105. (beginning-of-line 1)
  5106. (unless for-yank (org-back-over-empty-lines))
  5107. (setq beg (point))
  5108. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5109. (insert-before-markers txt)
  5110. (unless (string-match "\n\\'" txt) (insert "\n"))
  5111. (setq newend (point))
  5112. (org-reinstall-markers-in-region beg)
  5113. (setq end (point))
  5114. (goto-char beg)
  5115. (skip-chars-forward " \t\n\r")
  5116. (setq beg (point))
  5117. (if (and (org-invisible-p) visp)
  5118. (save-excursion (outline-show-heading)))
  5119. ;; Shift if necessary
  5120. (unless (= shift 0)
  5121. (save-restriction
  5122. (narrow-to-region beg end)
  5123. (while (not (= shift 0))
  5124. (org-map-region func (point-min) (point-max))
  5125. (setq shift (+ delta shift)))
  5126. (goto-char (point-min))
  5127. (setq newend (point-max))))
  5128. (when (or (interactive-p) for-yank)
  5129. (message "Clipboard pasted as level %d subtree" new-level))
  5130. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5131. kill-ring
  5132. (eq org-subtree-clip (current-kill 0))
  5133. org-subtree-clip-folded)
  5134. ;; The tree was folded before it was killed/copied
  5135. (hide-subtree))
  5136. (and for-yank (goto-char newend))))
  5137. (defun org-kill-is-subtree-p (&optional txt)
  5138. "Check if the current kill is an outline subtree, or a set of trees.
  5139. Returns nil if kill does not start with a headline, or if the first
  5140. headline level is not the largest headline level in the tree.
  5141. So this will actually accept several entries of equal levels as well,
  5142. which is OK for `org-paste-subtree'.
  5143. If optional TXT is given, check this string instead of the current kill."
  5144. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5145. (start-level (and kill
  5146. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5147. org-outline-regexp "\\)")
  5148. kill)
  5149. (- (match-end 2) (match-beginning 2) 1)))
  5150. (re (concat "^" org-outline-regexp))
  5151. (start (1+ (or (match-beginning 2) -1))))
  5152. (if (not start-level)
  5153. (progn
  5154. nil) ;; does not even start with a heading
  5155. (catch 'exit
  5156. (while (setq start (string-match re kill (1+ start)))
  5157. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5158. (throw 'exit nil)))
  5159. t))))
  5160. (defvar org-markers-to-move nil
  5161. "Markers that should be moved with a cut-and-paste operation.
  5162. Those markers are stored together with their positions relative to
  5163. the start of the region.")
  5164. (defun org-save-markers-in-region (beg end)
  5165. "Check markers in region.
  5166. If these markers are between BEG and END, record their position relative
  5167. to BEG, so that after moving the block of text, we can put the markers back
  5168. into place.
  5169. This function gets called just before an entry or tree gets cut from the
  5170. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5171. called immediately, to move the markers with the entries."
  5172. (setq org-markers-to-move nil)
  5173. (when (featurep 'org-clock)
  5174. (org-clock-save-markers-for-cut-and-paste beg end))
  5175. (when (featurep 'org-agenda)
  5176. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5177. (defun org-check-and-save-marker (marker beg end)
  5178. "Check if MARKER is between BEG and END.
  5179. If yes, remember the marker and the distance to BEG."
  5180. (when (and (marker-buffer marker)
  5181. (equal (marker-buffer marker) (current-buffer)))
  5182. (if (and (>= marker beg) (< marker end))
  5183. (push (cons marker (- marker beg)) org-markers-to-move))))
  5184. (defun org-reinstall-markers-in-region (beg)
  5185. "Move all remembered markers to their position relative to BEG."
  5186. (mapc (lambda (x)
  5187. (move-marker (car x) (+ beg (cdr x))))
  5188. org-markers-to-move)
  5189. (setq org-markers-to-move nil))
  5190. (defun org-narrow-to-subtree ()
  5191. "Narrow buffer to the current subtree."
  5192. (interactive)
  5193. (save-excursion
  5194. (save-match-data
  5195. (narrow-to-region
  5196. (progn (org-back-to-heading) (point))
  5197. (progn (org-end-of-subtree t) (point))))))
  5198. ;;; Outline Sorting
  5199. (defun org-sort (with-case)
  5200. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5201. Optional argument WITH-CASE means sort case-sensitively.
  5202. With a double prefix argument, also remove duplicate entries."
  5203. (interactive "P")
  5204. (if (org-at-table-p)
  5205. (org-call-with-arg 'org-table-sort-lines with-case)
  5206. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5207. (defun org-sort-remove-invisible (s)
  5208. (remove-text-properties 0 (length s) org-rm-props s)
  5209. (while (string-match org-bracket-link-regexp s)
  5210. (setq s (replace-match (if (match-end 2)
  5211. (match-string 3 s)
  5212. (match-string 1 s)) t t s)))
  5213. s)
  5214. (defvar org-priority-regexp) ; defined later in the file
  5215. (defun org-sort-entries-or-items
  5216. (&optional with-case sorting-type getkey-func compare-func property)
  5217. "Sort entries on a certain level of an outline tree.
  5218. If there is an active region, the entries in the region are sorted.
  5219. Else, if the cursor is before the first entry, sort the top-level items.
  5220. Else, the children of the entry at point are sorted.
  5221. Sorting can be alphabetically, numerically, and by date/time as given by
  5222. the first time stamp in the entry. The command prompts for the sorting
  5223. type unless it has been given to the function through the SORTING-TYPE
  5224. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5225. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5226. called with point at the beginning of the record. It must return either
  5227. a string or a number that should serve as the sorting key for that record.
  5228. Comparing entries ignores case by default. However, with an optional argument
  5229. WITH-CASE, the sorting considers case as well."
  5230. (interactive "P")
  5231. (let ((case-func (if with-case 'identity 'downcase))
  5232. start beg end stars re re2
  5233. txt what tmp plain-list-p)
  5234. ;; Find beginning and end of region to sort
  5235. (cond
  5236. ((org-region-active-p)
  5237. ;; we will sort the region
  5238. (setq end (region-end)
  5239. what "region")
  5240. (goto-char (region-beginning))
  5241. (if (not (org-on-heading-p)) (outline-next-heading))
  5242. (setq start (point)))
  5243. ((org-at-item-p)
  5244. ;; we will sort this plain list
  5245. (org-beginning-of-item-list) (setq start (point))
  5246. (org-end-of-item-list) (setq end (point))
  5247. (goto-char start)
  5248. (setq plain-list-p t
  5249. what "plain list"))
  5250. ((or (org-on-heading-p)
  5251. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5252. ;; we will sort the children of the current headline
  5253. (org-back-to-heading)
  5254. (setq start (point)
  5255. end (progn (org-end-of-subtree t t)
  5256. (org-back-over-empty-lines)
  5257. (point))
  5258. what "children")
  5259. (goto-char start)
  5260. (show-subtree)
  5261. (outline-next-heading))
  5262. (t
  5263. ;; we will sort the top-level entries in this file
  5264. (goto-char (point-min))
  5265. (or (org-on-heading-p) (outline-next-heading))
  5266. (setq start (point) end (point-max) what "top-level")
  5267. (goto-char start)
  5268. (show-all)))
  5269. (setq beg (point))
  5270. (if (>= beg end) (error "Nothing to sort"))
  5271. (unless plain-list-p
  5272. (looking-at "\\(\\*+\\)")
  5273. (setq stars (match-string 1)
  5274. re (concat "^" (regexp-quote stars) " +")
  5275. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5276. txt (buffer-substring beg end))
  5277. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5278. (if (and (not (equal stars "*")) (string-match re2 txt))
  5279. (error "Region to sort contains a level above the first entry")))
  5280. (unless sorting-type
  5281. (message
  5282. (if plain-list-p
  5283. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5284. "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:")
  5285. what)
  5286. (setq sorting-type (read-char-exclusive))
  5287. (and (= (downcase sorting-type) ?f)
  5288. (setq getkey-func
  5289. (org-ido-completing-read "Sort using function: "
  5290. obarray 'fboundp t nil nil))
  5291. (setq getkey-func (intern getkey-func)))
  5292. (and (= (downcase sorting-type) ?r)
  5293. (setq property
  5294. (org-ido-completing-read "Property: "
  5295. (mapcar 'list (org-buffer-property-keys t))
  5296. nil t))))
  5297. (message "Sorting entries...")
  5298. (save-restriction
  5299. (narrow-to-region start end)
  5300. (let ((dcst (downcase sorting-type))
  5301. (now (current-time)))
  5302. (sort-subr
  5303. (/= dcst sorting-type)
  5304. ;; This function moves to the beginning character of the "record" to
  5305. ;; be sorted.
  5306. (if plain-list-p
  5307. (lambda nil
  5308. (if (org-at-item-p) t (goto-char (point-max))))
  5309. (lambda nil
  5310. (if (re-search-forward re nil t)
  5311. (goto-char (match-beginning 0))
  5312. (goto-char (point-max)))))
  5313. ;; This function moves to the last character of the "record" being
  5314. ;; sorted.
  5315. (if plain-list-p
  5316. 'org-end-of-item
  5317. (lambda nil
  5318. (save-match-data
  5319. (condition-case nil
  5320. (outline-forward-same-level 1)
  5321. (error
  5322. (goto-char (point-max)))))))
  5323. ;; This function returns the value that gets sorted against.
  5324. (if plain-list-p
  5325. (lambda nil
  5326. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5327. (cond
  5328. ((= dcst ?n)
  5329. (string-to-number (buffer-substring (match-end 0)
  5330. (point-at-eol))))
  5331. ((= dcst ?a)
  5332. (buffer-substring (match-end 0) (point-at-eol)))
  5333. ((= dcst ?t)
  5334. (if (re-search-forward org-ts-regexp
  5335. (point-at-eol) t)
  5336. (org-time-string-to-time (match-string 0))
  5337. now))
  5338. ((= dcst ?f)
  5339. (if getkey-func
  5340. (progn
  5341. (setq tmp (funcall getkey-func))
  5342. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5343. tmp)
  5344. (error "Invalid key function `%s'" getkey-func)))
  5345. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5346. (lambda nil
  5347. (cond
  5348. ((= dcst ?n)
  5349. (if (looking-at org-complex-heading-regexp)
  5350. (string-to-number (match-string 4))
  5351. nil))
  5352. ((= dcst ?a)
  5353. (if (looking-at org-complex-heading-regexp)
  5354. (funcall case-func (match-string 4))
  5355. nil))
  5356. ((= dcst ?t)
  5357. (if (re-search-forward org-ts-regexp
  5358. (save-excursion
  5359. (forward-line 2)
  5360. (point)) t)
  5361. (org-time-string-to-time (match-string 0))
  5362. now))
  5363. ((= dcst ?p)
  5364. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5365. (string-to-char (match-string 2))
  5366. org-default-priority))
  5367. ((= dcst ?r)
  5368. (or (org-entry-get nil property) ""))
  5369. ((= dcst ?o)
  5370. (if (looking-at org-complex-heading-regexp)
  5371. (- 9999 (length (member (match-string 2)
  5372. org-todo-keywords-1)))))
  5373. ((= dcst ?f)
  5374. (if getkey-func
  5375. (progn
  5376. (setq tmp (funcall getkey-func))
  5377. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5378. tmp)
  5379. (error "Invalid key function `%s'" getkey-func)))
  5380. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5381. nil
  5382. (cond
  5383. ((= dcst ?a) 'string<)
  5384. ((= dcst ?t) 'time-less-p)
  5385. ((= dcst ?f) compare-func)
  5386. (t nil)))))
  5387. (message "Sorting entries...done")))
  5388. (defun org-do-sort (table what &optional with-case sorting-type)
  5389. "Sort TABLE of WHAT according to SORTING-TYPE.
  5390. The user will be prompted for the SORTING-TYPE if the call to this
  5391. function does not specify it. WHAT is only for the prompt, to indicate
  5392. what is being sorted. The sorting key will be extracted from
  5393. the car of the elements of the table.
  5394. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5395. (unless sorting-type
  5396. (message
  5397. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5398. what)
  5399. (setq sorting-type (read-char-exclusive)))
  5400. (let ((dcst (downcase sorting-type))
  5401. extractfun comparefun)
  5402. ;; Define the appropriate functions
  5403. (cond
  5404. ((= dcst ?n)
  5405. (setq extractfun 'string-to-number
  5406. comparefun (if (= dcst sorting-type) '< '>)))
  5407. ((= dcst ?a)
  5408. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5409. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5410. comparefun (if (= dcst sorting-type)
  5411. 'string<
  5412. (lambda (a b) (and (not (string< a b))
  5413. (not (string= a b)))))))
  5414. ((= dcst ?t)
  5415. (setq extractfun
  5416. (lambda (x)
  5417. (if (or (string-match org-ts-regexp x)
  5418. (string-match org-ts-regexp-both x))
  5419. (time-to-seconds
  5420. (org-time-string-to-time (match-string 0 x)))
  5421. 0))
  5422. comparefun (if (= dcst sorting-type) '< '>)))
  5423. (t (error "Invalid sorting type `%c'" sorting-type)))
  5424. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5425. table)
  5426. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5427. ;;; Editing source examples
  5428. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5429. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5430. (defvar org-edit-src-force-single-line nil)
  5431. (defvar org-edit-src-from-org-mode nil)
  5432. (defvar org-edit-src-picture nil)
  5433. (define-minor-mode org-exit-edit-mode
  5434. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5435. (defun org-edit-src-code ()
  5436. "Edit the source code example at point.
  5437. An indirect buffer is created, and that buffer is then narrowed to the
  5438. example at point and switched to the correct language mode. When done,
  5439. exit by killing the buffer with \\[org-edit-src-exit]."
  5440. (interactive)
  5441. (let ((line (org-current-line))
  5442. (case-fold-search t)
  5443. (msg (substitute-command-keys
  5444. "Edit, then exit with C-c ' (C-c and single quote)"))
  5445. (info (org-edit-src-find-region-and-lang))
  5446. (org-mode-p (eq major-mode 'org-mode))
  5447. beg end lang lang-f single lfmt)
  5448. (if (not info)
  5449. nil
  5450. (setq beg (nth 0 info)
  5451. end (nth 1 info)
  5452. lang (nth 2 info)
  5453. single (nth 3 info)
  5454. lfmt (nth 4 info)
  5455. lang-f (intern (concat lang "-mode")))
  5456. (unless (functionp lang-f)
  5457. (error "No such language mode: %s" lang-f))
  5458. (goto-line line)
  5459. (if (get-buffer "*Org Edit Src Example*")
  5460. (kill-buffer "*Org Edit Src Example*"))
  5461. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5462. "*Org Edit Src Example*"))
  5463. (narrow-to-region beg end)
  5464. (remove-text-properties beg end '(display nil invisible nil
  5465. intangible nil))
  5466. (let ((org-inhibit-startup t))
  5467. (funcall lang-f))
  5468. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5469. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5470. (when lfmt
  5471. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5472. (when org-mode-p
  5473. (goto-char (point-min))
  5474. (while (re-search-forward "^," nil t)
  5475. (replace-match "")))
  5476. (goto-line line)
  5477. (org-exit-edit-mode)
  5478. (org-set-local 'header-line-format msg)
  5479. (message "%s" msg)
  5480. t)))
  5481. (defun org-edit-fixed-width-region ()
  5482. "Edit the fixed-width ascii drawing at point.
  5483. This must be a region where each line starts with a colon followed by
  5484. a space character.
  5485. An indirect buffer is created, and that buffer is then narrowed to the
  5486. example at point and switched to artist-mode. When done,
  5487. exit by killing the buffer with \\[org-edit-src-exit]."
  5488. (interactive)
  5489. (let ((line (org-current-line))
  5490. (case-fold-search t)
  5491. (msg (substitute-command-keys
  5492. "Edit, then exit with C-c ' (C-c and single quote)"))
  5493. (org-mode-p (eq major-mode 'org-mode))
  5494. beg end)
  5495. (beginning-of-line 1)
  5496. (if (looking-at "[ \t]*[^:\n \t]")
  5497. nil
  5498. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5499. (setq beg (point) end beg)
  5500. (save-excursion
  5501. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5502. (setq beg (point-at-bol 2))
  5503. (setq beg (point))))
  5504. (save-excursion
  5505. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5506. (setq end (1- (match-beginning 0)))
  5507. (setq end (point))))
  5508. (goto-line line))
  5509. (if (get-buffer "*Org Edit Picture*")
  5510. (kill-buffer "*Org Edit Picture*"))
  5511. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5512. "*Org Edit Picture*"))
  5513. (narrow-to-region beg end)
  5514. (remove-text-properties beg end '(display nil invisible nil
  5515. intangible nil))
  5516. (when (fboundp 'font-lock-unfontify-region)
  5517. (font-lock-unfontify-region (point-min) (point-max)))
  5518. (cond
  5519. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5520. (fundamental-mode)
  5521. (artist-mode 1))
  5522. (t (funcall org-edit-fixed-width-region-mode)))
  5523. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5524. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5525. (set (make-local-variable 'org-edit-src-picture) t)
  5526. (goto-char (point-min))
  5527. (while (re-search-forward "^[ \t]*: ?" nil t)
  5528. (replace-match ""))
  5529. (goto-line line)
  5530. (org-exit-edit-mode)
  5531. (org-set-local 'header-line-format msg)
  5532. (message "%s" msg)
  5533. t)))
  5534. (defun org-edit-src-find-region-and-lang ()
  5535. "Find the region and language for a local edit.
  5536. Return a list with beginning and end of the region, a string representing
  5537. the language, a switch telling of the content should be in a single line."
  5538. (let ((re-list
  5539. (append
  5540. org-edit-src-region-extra
  5541. '(
  5542. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5543. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5544. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5545. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5546. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5547. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5548. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5549. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5550. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5551. ("^#\\+html:" "\n" "html" single-line)
  5552. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5553. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5554. ("^#\\+latex:" "\n" "latex" single-line)
  5555. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5556. ("^#\\+ascii:" "\n" "ascii" single-line)
  5557. )))
  5558. (pos (point))
  5559. re1 re2 single beg end lang lfmt match-re1)
  5560. (catch 'exit
  5561. (while (setq entry (pop re-list))
  5562. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5563. single (nth 3 entry))
  5564. (save-excursion
  5565. (if (or (looking-at re1)
  5566. (re-search-backward re1 nil t))
  5567. (progn
  5568. (setq match-re1 (match-string 0))
  5569. (setq beg (match-end 0)
  5570. lang (org-edit-src-get-lang lang)
  5571. lfmt (org-edit-src-get-label-format match-re1))
  5572. (if (and (re-search-forward re2 nil t)
  5573. (>= (match-end 0) pos))
  5574. (throw 'exit (list beg (match-beginning 0)
  5575. lang single lfmt))))
  5576. (if (or (looking-at re2)
  5577. (re-search-forward re2 nil t))
  5578. (progn
  5579. (setq end (match-beginning 0))
  5580. (if (and (re-search-backward re1 nil t)
  5581. (<= (match-beginning 0) pos))
  5582. (progn
  5583. (setq lfmt (org-edit-src-get-label-format
  5584. (match-string 0)))
  5585. (throw 'exit
  5586. (list (match-end 0) end
  5587. (org-edit-src-get-lang lang)
  5588. single lfmt))))))))))))
  5589. (defun org-edit-src-get-lang (lang)
  5590. "Extract the src language."
  5591. (let ((m (match-string 0)))
  5592. (cond
  5593. ((stringp lang) lang)
  5594. ((integerp lang) (match-string lang))
  5595. ((and (eq lang 'lang)
  5596. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5597. (match-string 1 m))
  5598. ((and (eq lang 'style)
  5599. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5600. (match-string 1 m))
  5601. (t "fundamental"))))
  5602. (defun org-edit-src-get-label-format (s)
  5603. "Extract the label format."
  5604. (save-match-data
  5605. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5606. (match-string 1 s))))
  5607. (defun org-edit-src-exit ()
  5608. "Exit special edit and protect problematic lines."
  5609. (interactive)
  5610. (unless (buffer-base-buffer (current-buffer))
  5611. (error "This is not an indirect buffer, something is wrong..."))
  5612. (unless (> (point-min) 1)
  5613. (error "This buffer is not narrowed, something is wrong..."))
  5614. (goto-char (point-min))
  5615. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5616. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5617. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5618. (goto-char (point-min))
  5619. (while (re-search-forward "\n" nil t)
  5620. (replace-match " "))
  5621. (goto-char (point-min))
  5622. (if (looking-at "\\s-*") (replace-match " "))
  5623. (if (re-search-forward "\\s-+\\'" nil t)
  5624. (replace-match "")))
  5625. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5626. (goto-char (point-min))
  5627. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5628. (replace-match ",\\1"))
  5629. (when font-lock-mode
  5630. (font-lock-unfontify-region (point-min) (point-max)))
  5631. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5632. (when (org-bound-and-true-p org-edit-src-picture)
  5633. (untabify (point-min) (point-max))
  5634. (goto-char (point-min))
  5635. (while (re-search-forward "^" nil t)
  5636. (replace-match ": "))
  5637. (when font-lock-mode
  5638. (font-lock-unfontify-region (point-min) (point-max)))
  5639. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5640. (kill-buffer (current-buffer))
  5641. (and (org-mode-p) (org-restart-font-lock)))
  5642. ;;; The orgstruct minor mode
  5643. ;; Define a minor mode which can be used in other modes in order to
  5644. ;; integrate the org-mode structure editing commands.
  5645. ;; This is really a hack, because the org-mode structure commands use
  5646. ;; keys which normally belong to the major mode. Here is how it
  5647. ;; works: The minor mode defines all the keys necessary to operate the
  5648. ;; structure commands, but wraps the commands into a function which
  5649. ;; tests if the cursor is currently at a headline or a plain list
  5650. ;; item. If that is the case, the structure command is used,
  5651. ;; temporarily setting many Org-mode variables like regular
  5652. ;; expressions for filling etc. However, when any of those keys is
  5653. ;; used at a different location, function uses `key-binding' to look
  5654. ;; up if the key has an associated command in another currently active
  5655. ;; keymap (minor modes, major mode, global), and executes that
  5656. ;; command. There might be problems if any of the keys is otherwise
  5657. ;; used as a prefix key.
  5658. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5659. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5660. ;; addresses this by checking explicitly for both bindings.
  5661. (defvar orgstruct-mode-map (make-sparse-keymap)
  5662. "Keymap for the minor `orgstruct-mode'.")
  5663. (defvar org-local-vars nil
  5664. "List of local variables, for use by `orgstruct-mode'")
  5665. ;;;###autoload
  5666. (define-minor-mode orgstruct-mode
  5667. "Toggle the minor more `orgstruct-mode'.
  5668. This mode is for using Org-mode structure commands in other modes.
  5669. The following key behave as if Org-mode was active, if the cursor
  5670. is on a headline, or on a plain list item (both in the definition
  5671. of Org-mode).
  5672. M-up Move entry/item up
  5673. M-down Move entry/item down
  5674. M-left Promote
  5675. M-right Demote
  5676. M-S-up Move entry/item up
  5677. M-S-down Move entry/item down
  5678. M-S-left Promote subtree
  5679. M-S-right Demote subtree
  5680. M-q Fill paragraph and items like in Org-mode
  5681. C-c ^ Sort entries
  5682. C-c - Cycle list bullet
  5683. TAB Cycle item visibility
  5684. M-RET Insert new heading/item
  5685. S-M-RET Insert new TODO heading / Checkbox item
  5686. C-c C-c Set tags / toggle checkbox"
  5687. nil " OrgStruct" nil
  5688. (org-load-modules-maybe)
  5689. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5690. ;;;###autoload
  5691. (defun turn-on-orgstruct ()
  5692. "Unconditionally turn on `orgstruct-mode'."
  5693. (orgstruct-mode 1))
  5694. (defun orgstruct++-mode (&optional arg)
  5695. "Toggle `orgstruct-mode', the enhanced version of it.
  5696. In addition to setting orgstruct-mode, this also exports all indentation and
  5697. autofilling variables from org-mode into the buffer. It will also
  5698. recognize item context in multiline items.
  5699. Note that turning off orgstruct-mode will *not* remove the
  5700. indentation/paragraph settings. This can only be done by refreshing the
  5701. major mode, for example with \[normal-mode]."
  5702. (interactive "P")
  5703. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5704. (if (< arg 1)
  5705. (orgstruct-mode -1)
  5706. (orgstruct-mode 1)
  5707. (let (var val)
  5708. (mapc
  5709. (lambda (x)
  5710. (when (string-match
  5711. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5712. (symbol-name (car x)))
  5713. (setq var (car x) val (nth 1 x))
  5714. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5715. org-local-vars)
  5716. (org-set-local 'orgstruct-is-++ t))))
  5717. (defvar orgstruct-is-++ nil
  5718. "Is orgstruct-mode in ++ version in the current-buffer?")
  5719. (make-variable-buffer-local 'orgstruct-is-++)
  5720. ;;;###autoload
  5721. (defun turn-on-orgstruct++ ()
  5722. "Unconditionally turn on `orgstruct++-mode'."
  5723. (orgstruct++-mode 1))
  5724. (defun orgstruct-error ()
  5725. "Error when there is no default binding for a structure key."
  5726. (interactive)
  5727. (error "This key has no function outside structure elements"))
  5728. (defun orgstruct-setup ()
  5729. "Setup orgstruct keymaps."
  5730. (let ((nfunc 0)
  5731. (bindings
  5732. (list
  5733. '([(meta up)] org-metaup)
  5734. '([(meta down)] org-metadown)
  5735. '([(meta left)] org-metaleft)
  5736. '([(meta right)] org-metaright)
  5737. '([(meta shift up)] org-shiftmetaup)
  5738. '([(meta shift down)] org-shiftmetadown)
  5739. '([(meta shift left)] org-shiftmetaleft)
  5740. '([(meta shift right)] org-shiftmetaright)
  5741. '([(shift up)] org-shiftup)
  5742. '([(shift down)] org-shiftdown)
  5743. '([(shift left)] org-shiftleft)
  5744. '([(shift right)] org-shiftright)
  5745. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5746. '("\M-q" fill-paragraph)
  5747. '("\C-c^" org-sort)
  5748. '("\C-c-" org-cycle-list-bullet)))
  5749. elt key fun cmd)
  5750. (while (setq elt (pop bindings))
  5751. (setq nfunc (1+ nfunc))
  5752. (setq key (org-key (car elt))
  5753. fun (nth 1 elt)
  5754. cmd (orgstruct-make-binding fun nfunc key))
  5755. (org-defkey orgstruct-mode-map key cmd))
  5756. ;; Special treatment needed for TAB and RET
  5757. (org-defkey orgstruct-mode-map [(tab)]
  5758. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5759. (org-defkey orgstruct-mode-map "\C-i"
  5760. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5761. (org-defkey orgstruct-mode-map "\M-\C-m"
  5762. (orgstruct-make-binding 'org-insert-heading 105
  5763. "\M-\C-m" [(meta return)]))
  5764. (org-defkey orgstruct-mode-map [(meta return)]
  5765. (orgstruct-make-binding 'org-insert-heading 106
  5766. [(meta return)] "\M-\C-m"))
  5767. (org-defkey orgstruct-mode-map [(shift meta return)]
  5768. (orgstruct-make-binding 'org-insert-todo-heading 107
  5769. [(meta return)] "\M-\C-m"))
  5770. (unless org-local-vars
  5771. (setq org-local-vars (org-get-local-variables)))
  5772. t))
  5773. (defun orgstruct-make-binding (fun n &rest keys)
  5774. "Create a function for binding in the structure minor mode.
  5775. FUN is the command to call inside a table. N is used to create a unique
  5776. command name. KEYS are keys that should be checked in for a command
  5777. to execute outside of tables."
  5778. (eval
  5779. (list 'defun
  5780. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5781. '(arg)
  5782. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5783. "Outside of structure, run the binding of `"
  5784. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5785. "'.")
  5786. '(interactive "p")
  5787. (list 'if
  5788. `(org-context-p 'headline 'item
  5789. (and orgstruct-is-++
  5790. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  5791. 'item-body))
  5792. (list 'org-run-like-in-org-mode (list 'quote fun))
  5793. (list 'let '(orgstruct-mode)
  5794. (list 'call-interactively
  5795. (append '(or)
  5796. (mapcar (lambda (k)
  5797. (list 'key-binding k))
  5798. keys)
  5799. '('orgstruct-error))))))))
  5800. (defun org-context-p (&rest contexts)
  5801. "Check if local context is any of CONTEXTS.
  5802. Possible values in the list of contexts are `table', `headline', and `item'."
  5803. (let ((pos (point)))
  5804. (goto-char (point-at-bol))
  5805. (prog1 (or (and (memq 'table contexts)
  5806. (looking-at "[ \t]*|"))
  5807. (and (memq 'headline contexts)
  5808. ;;????????? (looking-at "\\*+"))
  5809. (looking-at outline-regexp))
  5810. (and (memq 'item contexts)
  5811. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  5812. (and (memq 'item-body contexts)
  5813. (org-in-item-p)))
  5814. (goto-char pos))))
  5815. (defun org-get-local-variables ()
  5816. "Return a list of all local variables in an org-mode buffer."
  5817. (let (varlist)
  5818. (with-current-buffer (get-buffer-create "*Org tmp*")
  5819. (erase-buffer)
  5820. (org-mode)
  5821. (setq varlist (buffer-local-variables)))
  5822. (kill-buffer "*Org tmp*")
  5823. (delq nil
  5824. (mapcar
  5825. (lambda (x)
  5826. (setq x
  5827. (if (symbolp x)
  5828. (list x)
  5829. (list (car x) (list 'quote (cdr x)))))
  5830. (if (string-match
  5831. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5832. (symbol-name (car x)))
  5833. x nil))
  5834. varlist))))
  5835. ;;;###autoload
  5836. (defun org-run-like-in-org-mode (cmd)
  5837. "Run a command, pretending that the current buffer is in Org-mode.
  5838. This will temporarily bind local variables that are typically bound in
  5839. Org-mode to the values they have in Org-mode, and then interactively
  5840. call CMD."
  5841. (org-load-modules-maybe)
  5842. (unless org-local-vars
  5843. (setq org-local-vars (org-get-local-variables)))
  5844. (eval (list 'let org-local-vars
  5845. (list 'call-interactively (list 'quote cmd)))))
  5846. ;;;; Archiving
  5847. (defun org-get-category (&optional pos)
  5848. "Get the category applying to position POS."
  5849. (get-text-property (or pos (point)) 'org-category))
  5850. (defun org-refresh-category-properties ()
  5851. "Refresh category text properties in the buffer."
  5852. (let ((def-cat (cond
  5853. ((null org-category)
  5854. (if buffer-file-name
  5855. (file-name-sans-extension
  5856. (file-name-nondirectory buffer-file-name))
  5857. "???"))
  5858. ((symbolp org-category) (symbol-name org-category))
  5859. (t org-category)))
  5860. beg end cat pos optionp)
  5861. (org-unmodified
  5862. (save-excursion
  5863. (save-restriction
  5864. (widen)
  5865. (goto-char (point-min))
  5866. (put-text-property (point) (point-max) 'org-category def-cat)
  5867. (while (re-search-forward
  5868. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5869. (setq pos (match-end 0)
  5870. optionp (equal (char-after (match-beginning 0)) ?#)
  5871. cat (org-trim (match-string 2)))
  5872. (if optionp
  5873. (setq beg (point-at-bol) end (point-max))
  5874. (org-back-to-heading t)
  5875. (setq beg (point) end (org-end-of-subtree t t)))
  5876. (put-text-property beg end 'org-category cat)
  5877. (goto-char pos)))))))
  5878. ;;;; Link Stuff
  5879. ;;; Link abbreviations
  5880. (defun org-link-expand-abbrev (link)
  5881. "Apply replacements as defined in `org-link-abbrev-alist."
  5882. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5883. (let* ((key (match-string 1 link))
  5884. (as (or (assoc key org-link-abbrev-alist-local)
  5885. (assoc key org-link-abbrev-alist)))
  5886. (tag (and (match-end 2) (match-string 3 link)))
  5887. rpl)
  5888. (if (not as)
  5889. link
  5890. (setq rpl (cdr as))
  5891. (cond
  5892. ((symbolp rpl) (funcall rpl tag))
  5893. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5894. ((string-match "%h" rpl)
  5895. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5896. (t (concat rpl tag)))))
  5897. link))
  5898. ;;; Storing and inserting links
  5899. (defvar org-insert-link-history nil
  5900. "Minibuffer history for links inserted with `org-insert-link'.")
  5901. (defvar org-stored-links nil
  5902. "Contains the links stored with `org-store-link'.")
  5903. (defvar org-store-link-plist nil
  5904. "Plist with info about the most recently link created with `org-store-link'.")
  5905. (defvar org-link-protocols nil
  5906. "Link protocols added to Org-mode using `org-add-link-type'.")
  5907. (defvar org-store-link-functions nil
  5908. "List of functions that are called to create and store a link.
  5909. Each function will be called in turn until one returns a non-nil
  5910. value. Each function should check if it is responsible for creating
  5911. this link (for example by looking at the major mode).
  5912. If not, it must exit and return nil.
  5913. If yes, it should return a non-nil value after a calling
  5914. `org-store-link-props' with a list of properties and values.
  5915. Special properties are:
  5916. :type The link prefix. like \"http\". This must be given.
  5917. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5918. This is obligatory as well.
  5919. :description Optional default description for the second pair
  5920. of brackets in an Org-mode link. The user can still change
  5921. this when inserting this link into an Org-mode buffer.
  5922. In addition to these, any additional properties can be specified
  5923. and then used in remember templates.")
  5924. (defun org-add-link-type (type &optional follow export)
  5925. "Add TYPE to the list of `org-link-types'.
  5926. Re-compute all regular expressions depending on `org-link-types'
  5927. FOLLOW and EXPORT are two functions.
  5928. FOLLOW should take the link path as the single argument and do whatever
  5929. is necessary to follow the link, for example find a file or display
  5930. a mail message.
  5931. EXPORT should format the link path for export to one of the export formats.
  5932. It should be a function accepting three arguments:
  5933. path the path of the link, the text after the prefix (like \"http:\")
  5934. desc the description of the link, if any, nil if there was no description
  5935. format the export format, a symbol like `html' or `latex'.
  5936. The function may use the FORMAT information to return different values
  5937. depending on the format. The return value will be put literally into
  5938. the exported file.
  5939. Org-mode has a built-in default for exporting links. If you are happy with
  5940. this default, there is no need to define an export function for the link
  5941. type. For a simple example of an export function, see `org-bbdb.el'."
  5942. (add-to-list 'org-link-types type t)
  5943. (org-make-link-regexps)
  5944. (if (assoc type org-link-protocols)
  5945. (setcdr (assoc type org-link-protocols) (list follow export))
  5946. (push (list type follow export) org-link-protocols)))
  5947. ;;;###autoload
  5948. (defun org-store-link (arg)
  5949. "\\<org-mode-map>Store an org-link to the current location.
  5950. This link is added to `org-stored-links' and can later be inserted
  5951. into an org-buffer with \\[org-insert-link].
  5952. For some link types, a prefix arg is interpreted:
  5953. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5954. For file links, arg negates `org-context-in-file-links'."
  5955. (interactive "P")
  5956. (org-load-modules-maybe)
  5957. (setq org-store-link-plist nil) ; reset
  5958. (let (link cpltxt desc description search txt)
  5959. (cond
  5960. ((run-hook-with-args-until-success 'org-store-link-functions)
  5961. (setq link (plist-get org-store-link-plist :link)
  5962. desc (or (plist-get org-store-link-plist :description) link)))
  5963. ((equal (buffer-name) "*Org Edit Src Example*")
  5964. (let (label gc)
  5965. (while (or (not label)
  5966. (save-excursion
  5967. (save-restriction
  5968. (widen)
  5969. (goto-char (point-min))
  5970. (re-search-forward
  5971. (regexp-quote (format org-coderef-label-format label))
  5972. nil t))))
  5973. (when label (message "Label exists already") (sit-for 2))
  5974. (setq label (read-string "Code line label: " label)))
  5975. (end-of-line 1)
  5976. (setq link (format org-coderef-label-format label))
  5977. (setq gc (- 79 (length link)))
  5978. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5979. (insert link)
  5980. (setq link (concat "(" label ")") desc nil)))
  5981. ((eq major-mode 'calendar-mode)
  5982. (let ((cd (calendar-cursor-to-date)))
  5983. (setq link
  5984. (format-time-string
  5985. (car org-time-stamp-formats)
  5986. (apply 'encode-time
  5987. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5988. nil nil nil))))
  5989. (org-store-link-props :type "calendar" :date cd)))
  5990. ((eq major-mode 'w3-mode)
  5991. (setq cpltxt (url-view-url t)
  5992. link (org-make-link cpltxt))
  5993. (org-store-link-props :type "w3" :url (url-view-url t)))
  5994. ((eq major-mode 'w3m-mode)
  5995. (setq cpltxt (or w3m-current-title w3m-current-url)
  5996. link (org-make-link w3m-current-url))
  5997. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5998. ((setq search (run-hook-with-args-until-success
  5999. 'org-create-file-search-functions))
  6000. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6001. "::" search))
  6002. (setq cpltxt (or description link)))
  6003. ((eq major-mode 'image-mode)
  6004. (setq cpltxt (concat "file:"
  6005. (abbreviate-file-name buffer-file-name))
  6006. link (org-make-link cpltxt))
  6007. (org-store-link-props :type "image" :file buffer-file-name))
  6008. ((eq major-mode 'dired-mode)
  6009. ;; link to the file in the current line
  6010. (setq cpltxt (concat "file:"
  6011. (abbreviate-file-name
  6012. (expand-file-name
  6013. (dired-get-filename nil t))))
  6014. link (org-make-link cpltxt)))
  6015. ((and buffer-file-name (org-mode-p))
  6016. (cond
  6017. ((org-in-regexp "<<\\(.*?\\)>>")
  6018. (setq cpltxt
  6019. (concat "file:"
  6020. (abbreviate-file-name buffer-file-name)
  6021. "::" (match-string 1))
  6022. link (org-make-link cpltxt)))
  6023. ((and (featurep 'org-id)
  6024. (or (eq org-link-to-org-use-id t)
  6025. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6026. (interactive-p))
  6027. (and org-link-to-org-use-id
  6028. (condition-case nil
  6029. (org-entry-get nil "ID")
  6030. (error nil)))))
  6031. ;; We can make a link using the ID.
  6032. (setq link (condition-case nil
  6033. (prog1 (org-id-store-link)
  6034. (setq desc (plist-get org-store-link-plist
  6035. :description)))
  6036. (error
  6037. ;; probably before first headline, link to file only
  6038. (concat "file:"
  6039. (abbreviate-file-name buffer-file-name))))))
  6040. (t
  6041. ;; Just link to current headline
  6042. (setq cpltxt (concat "file:"
  6043. (abbreviate-file-name buffer-file-name)))
  6044. ;; Add a context search string
  6045. (when (org-xor org-context-in-file-links arg)
  6046. (setq txt (cond
  6047. ((org-on-heading-p) nil)
  6048. ((org-region-active-p)
  6049. (buffer-substring (region-beginning) (region-end)))
  6050. (t nil)))
  6051. (when (or (null txt) (string-match "\\S-" txt))
  6052. (setq cpltxt
  6053. (concat cpltxt "::"
  6054. (condition-case nil
  6055. (org-make-org-heading-search-string txt)
  6056. (error "")))
  6057. desc "NONE")))
  6058. (if (string-match "::\\'" cpltxt)
  6059. (setq cpltxt (substring cpltxt 0 -2)))
  6060. (setq link (org-make-link cpltxt)))))
  6061. ((buffer-file-name (buffer-base-buffer))
  6062. ;; Just link to this file here.
  6063. (setq cpltxt (concat "file:"
  6064. (abbreviate-file-name
  6065. (buffer-file-name (buffer-base-buffer)))))
  6066. ;; Add a context string
  6067. (when (org-xor org-context-in-file-links arg)
  6068. (setq txt (if (org-region-active-p)
  6069. (buffer-substring (region-beginning) (region-end))
  6070. (buffer-substring (point-at-bol) (point-at-eol))))
  6071. ;; Only use search option if there is some text.
  6072. (when (string-match "\\S-" txt)
  6073. (setq cpltxt
  6074. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6075. desc "NONE")))
  6076. (setq link (org-make-link cpltxt)))
  6077. ((interactive-p)
  6078. (error "Cannot link to a buffer which is not visiting a file"))
  6079. (t (setq link nil)))
  6080. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6081. (setq link (or link cpltxt)
  6082. desc (or desc cpltxt))
  6083. (if (equal desc "NONE") (setq desc nil))
  6084. (if (and (interactive-p) link)
  6085. (progn
  6086. (setq org-stored-links
  6087. (cons (list link desc) org-stored-links))
  6088. (message "Stored: %s" (or desc link)))
  6089. (and link (org-make-link-string link desc)))))
  6090. (defun org-store-link-props (&rest plist)
  6091. "Store link properties, extract names and addresses."
  6092. (let (x adr)
  6093. (when (setq x (plist-get plist :from))
  6094. (setq adr (mail-extract-address-components x))
  6095. (setq plist (plist-put plist :fromname (car adr)))
  6096. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6097. (when (setq x (plist-get plist :to))
  6098. (setq adr (mail-extract-address-components x))
  6099. (setq plist (plist-put plist :toname (car adr)))
  6100. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6101. (let ((from (plist-get plist :from))
  6102. (to (plist-get plist :to)))
  6103. (when (and from to org-from-is-user-regexp)
  6104. (setq plist
  6105. (plist-put plist :fromto
  6106. (if (string-match org-from-is-user-regexp from)
  6107. (concat "to %t")
  6108. (concat "from %f"))))))
  6109. (setq org-store-link-plist plist))
  6110. (defun org-add-link-props (&rest plist)
  6111. "Add these properties to the link property list."
  6112. (let (key value)
  6113. (while plist
  6114. (setq key (pop plist) value (pop plist))
  6115. (setq org-store-link-plist
  6116. (plist-put org-store-link-plist key value)))))
  6117. (defun org-email-link-description (&optional fmt)
  6118. "Return the description part of an email link.
  6119. This takes information from `org-store-link-plist' and formats it
  6120. according to FMT (default from `org-email-link-description-format')."
  6121. (setq fmt (or fmt org-email-link-description-format))
  6122. (let* ((p org-store-link-plist)
  6123. (to (plist-get p :toaddress))
  6124. (from (plist-get p :fromaddress))
  6125. (table
  6126. (list
  6127. (cons "%c" (plist-get p :fromto))
  6128. (cons "%F" (plist-get p :from))
  6129. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6130. (cons "%T" (plist-get p :to))
  6131. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6132. (cons "%s" (plist-get p :subject))
  6133. (cons "%m" (plist-get p :message-id)))))
  6134. (when (string-match "%c" fmt)
  6135. ;; Check if the user wrote this message
  6136. (if (and org-from-is-user-regexp from to
  6137. (save-match-data (string-match org-from-is-user-regexp from)))
  6138. (setq fmt (replace-match "to %t" t t fmt))
  6139. (setq fmt (replace-match "from %f" t t fmt))))
  6140. (org-replace-escapes fmt table)))
  6141. (defun org-make-org-heading-search-string (&optional string heading)
  6142. "Make search string for STRING or current headline."
  6143. (interactive)
  6144. (let ((s (or string (org-get-heading))))
  6145. (unless (and string (not heading))
  6146. ;; We are using a headline, clean up garbage in there.
  6147. (if (string-match org-todo-regexp s)
  6148. (setq s (replace-match "" t t s)))
  6149. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6150. (setq s (replace-match "" t t s)))
  6151. (setq s (org-trim s))
  6152. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6153. org-comment-string "\\)") s)
  6154. (setq s (replace-match "" t t s)))
  6155. (while (string-match org-ts-regexp s)
  6156. (setq s (replace-match "" t t s))))
  6157. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6158. (setq s (replace-match " " t t s)))
  6159. (or string (setq s (concat "*" s))) ; Add * for headlines
  6160. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6161. (defun org-make-link (&rest strings)
  6162. "Concatenate STRINGS."
  6163. (apply 'concat strings))
  6164. (defun org-make-link-string (link &optional description)
  6165. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6166. (unless (string-match "\\S-" link)
  6167. (error "Empty link"))
  6168. (when (stringp description)
  6169. ;; Remove brackets from the description, they are fatal.
  6170. (while (string-match "\\[" description)
  6171. (setq description (replace-match "{" t t description)))
  6172. (while (string-match "\\]" description)
  6173. (setq description (replace-match "}" t t description))))
  6174. (when (equal (org-link-escape link) description)
  6175. ;; No description needed, it is identical
  6176. (setq description nil))
  6177. (when (and (not description)
  6178. (not (equal link (org-link-escape link))))
  6179. (setq description (org-extract-attributes link)))
  6180. (concat "[[" (org-link-escape link) "]"
  6181. (if description (concat "[" description "]") "")
  6182. "]"))
  6183. (defconst org-link-escape-chars
  6184. '((?\ . "%20")
  6185. (?\[ . "%5B")
  6186. (?\] . "%5D")
  6187. (?\340 . "%E0") ; `a
  6188. (?\342 . "%E2") ; ^a
  6189. (?\347 . "%E7") ; ,c
  6190. (?\350 . "%E8") ; `e
  6191. (?\351 . "%E9") ; 'e
  6192. (?\352 . "%EA") ; ^e
  6193. (?\356 . "%EE") ; ^i
  6194. (?\364 . "%F4") ; ^o
  6195. (?\371 . "%F9") ; `u
  6196. (?\373 . "%FB") ; ^u
  6197. (?\; . "%3B")
  6198. (?? . "%3F")
  6199. (?= . "%3D")
  6200. (?+ . "%2B")
  6201. )
  6202. "Association list of escapes for some characters problematic in links.
  6203. This is the list that is used for internal purposes.")
  6204. (defconst org-link-escape-chars-browser
  6205. '((?\ . "%20")) ; 32 for the SPC char
  6206. "Association list of escapes for some characters problematic in links.
  6207. This is the list that is used before handing over to the browser.")
  6208. (defun org-link-escape (text &optional table)
  6209. "Escape characters in TEXT that are problematic for links."
  6210. (setq table (or table org-link-escape-chars))
  6211. (when text
  6212. (let ((re (mapconcat (lambda (x) (regexp-quote
  6213. (char-to-string (car x))))
  6214. table "\\|")))
  6215. (while (string-match re text)
  6216. (setq text
  6217. (replace-match
  6218. (cdr (assoc (string-to-char (match-string 0 text))
  6219. table))
  6220. t t text)))
  6221. text)))
  6222. (defun org-link-unescape (text &optional table)
  6223. "Reverse the action of `org-link-escape'."
  6224. (setq table (or table org-link-escape-chars))
  6225. (when text
  6226. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6227. table "\\|")))
  6228. (while (string-match re text)
  6229. (setq text
  6230. (replace-match
  6231. (char-to-string (car (rassoc (match-string 0 text) table)))
  6232. t t text)))
  6233. text)))
  6234. (defun org-xor (a b)
  6235. "Exclusive or."
  6236. (if a (not b) b))
  6237. (defun org-fixup-message-id-for-http (s)
  6238. "Replace special characters in a message id, so it can be used in an http query."
  6239. (while (string-match "<" s)
  6240. (setq s (replace-match "%3C" t t s)))
  6241. (while (string-match ">" s)
  6242. (setq s (replace-match "%3E" t t s)))
  6243. (while (string-match "@" s)
  6244. (setq s (replace-match "%40" t t s)))
  6245. s)
  6246. ;;;###autoload
  6247. (defun org-insert-link-global ()
  6248. "Insert a link like Org-mode does.
  6249. This command can be called in any mode to insert a link in Org-mode syntax."
  6250. (interactive)
  6251. (org-load-modules-maybe)
  6252. (org-run-like-in-org-mode 'org-insert-link))
  6253. (defun org-insert-link (&optional complete-file link-location)
  6254. "Insert a link. At the prompt, enter the link.
  6255. Completion can be used to insert any of the link protocol prefixes like
  6256. http or ftp in use.
  6257. The history can be used to select a link previously stored with
  6258. `org-store-link'. When the empty string is entered (i.e. if you just
  6259. press RET at the prompt), the link defaults to the most recently
  6260. stored link. As SPC triggers completion in the minibuffer, you need to
  6261. use M-SPC or C-q SPC to force the insertion of a space character.
  6262. You will also be prompted for a description, and if one is given, it will
  6263. be displayed in the buffer instead of the link.
  6264. If there is already a link at point, this command will allow you to edit link
  6265. and description parts.
  6266. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6267. be selected using completion. The path to the file will be relative to the
  6268. current directory if the file is in the current directory or a subdirectory.
  6269. Otherwise, the link will be the absolute path as completed in the minibuffer
  6270. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6271. option `org-link-file-path-type'.
  6272. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6273. the current directory or below.
  6274. With three \\[universal-argument] prefixes, negate the meaning of
  6275. `org-keep-stored-link-after-insertion'.
  6276. If `org-make-link-description-function' is non-nil, this function will be
  6277. called with the link target, and the result will be the default
  6278. link description.
  6279. If the LINK-LOCATION parameter is non-nil, this value will be
  6280. used as the link location instead of reading one interactively."
  6281. (interactive "P")
  6282. (let* ((wcf (current-window-configuration))
  6283. (region (if (org-region-active-p)
  6284. (buffer-substring (region-beginning) (region-end))))
  6285. (remove (and region (list (region-beginning) (region-end))))
  6286. (desc region)
  6287. tmphist ; byte-compile incorrectly complains about this
  6288. (link link-location)
  6289. entry file)
  6290. (cond
  6291. (link-location) ; specified by arg, just use it.
  6292. ((org-in-regexp org-bracket-link-regexp 1)
  6293. ;; We do have a link at point, and we are going to edit it.
  6294. (setq remove (list (match-beginning 0) (match-end 0)))
  6295. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6296. (setq link (read-string "Link: "
  6297. (org-link-unescape
  6298. (org-match-string-no-properties 1)))))
  6299. ((or (org-in-regexp org-angle-link-re)
  6300. (org-in-regexp org-plain-link-re))
  6301. ;; Convert to bracket link
  6302. (setq remove (list (match-beginning 0) (match-end 0))
  6303. link (read-string "Link: "
  6304. (org-remove-angle-brackets (match-string 0)))))
  6305. ((member complete-file '((4) (16)))
  6306. ;; Completing read for file names.
  6307. (setq file (read-file-name "File: "))
  6308. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6309. (pwd1 (file-name-as-directory (abbreviate-file-name
  6310. (expand-file-name ".")))))
  6311. (cond
  6312. ((equal complete-file '(16))
  6313. (setq link (org-make-link
  6314. "file:"
  6315. (abbreviate-file-name (expand-file-name file)))))
  6316. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6317. (setq link (org-make-link "file:" (match-string 1 file))))
  6318. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6319. (expand-file-name file))
  6320. (setq link (org-make-link
  6321. "file:" (match-string 1 (expand-file-name file)))))
  6322. (t (setq link (org-make-link "file:" file))))))
  6323. (t
  6324. ;; Read link, with completion for stored links.
  6325. (with-output-to-temp-buffer "*Org Links*"
  6326. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6327. (when org-stored-links
  6328. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6329. (princ (mapconcat
  6330. (lambda (x)
  6331. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6332. (reverse org-stored-links) "\n"))))
  6333. (let ((cw (selected-window)))
  6334. (select-window (get-buffer-window "*Org Links*"))
  6335. (setq truncate-lines t)
  6336. (org-fit-window-to-buffer)
  6337. (select-window cw))
  6338. ;; Fake a link history, containing the stored links.
  6339. (setq tmphist (append (mapcar 'car org-stored-links)
  6340. org-insert-link-history))
  6341. (unwind-protect
  6342. (setq link
  6343. (let ((org-completion-use-ido nil))
  6344. (org-completing-read
  6345. "Link: "
  6346. (append
  6347. (mapcar (lambda (x) (list (concat (car x) ":")))
  6348. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6349. (mapcar (lambda (x) (list (concat x ":")))
  6350. org-link-types))
  6351. nil nil nil
  6352. 'tmphist
  6353. (or (car (car org-stored-links))))))
  6354. (set-window-configuration wcf)
  6355. (kill-buffer "*Org Links*"))
  6356. (setq entry (assoc link org-stored-links))
  6357. (or entry (push link org-insert-link-history))
  6358. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6359. (not org-keep-stored-link-after-insertion))
  6360. (setq org-stored-links (delq (assoc link org-stored-links)
  6361. org-stored-links)))
  6362. (setq desc (or desc (nth 1 entry)))))
  6363. (if (string-match org-plain-link-re link)
  6364. ;; URL-like link, normalize the use of angular brackets.
  6365. (setq link (org-make-link (org-remove-angle-brackets link))))
  6366. ;; Check if we are linking to the current file with a search option
  6367. ;; If yes, simplify the link by using only the search option.
  6368. (when (and buffer-file-name
  6369. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6370. (let* ((path (match-string 1 link))
  6371. (case-fold-search nil)
  6372. (search (match-string 2 link)))
  6373. (save-match-data
  6374. (if (equal (file-truename buffer-file-name) (file-truename path))
  6375. ;; We are linking to this same file, with a search option
  6376. (setq link search)))))
  6377. ;; Check if we can/should use a relative path. If yes, simplify the link
  6378. (when (string-match "^file:\\(.*\\)" link)
  6379. (let* ((path (match-string 1 link))
  6380. (origpath path)
  6381. (case-fold-search nil))
  6382. (cond
  6383. ((or (eq org-link-file-path-type 'absolute)
  6384. (equal complete-file '(16)))
  6385. (setq path (abbreviate-file-name (expand-file-name path))))
  6386. ((eq org-link-file-path-type 'noabbrev)
  6387. (setq path (expand-file-name path)))
  6388. ((eq org-link-file-path-type 'relative)
  6389. (setq path (file-relative-name path)))
  6390. (t
  6391. (save-match-data
  6392. (if (string-match (concat "^" (regexp-quote
  6393. (file-name-as-directory
  6394. (expand-file-name "."))))
  6395. (expand-file-name path))
  6396. ;; We are linking a file with relative path name.
  6397. (setq path (substring (expand-file-name path)
  6398. (match-end 0)))
  6399. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6400. (setq link (concat "file:" path))
  6401. (if (equal desc origpath)
  6402. (setq desc path))))
  6403. (if org-make-link-description-function
  6404. (setq desc (funcall org-make-link-description-function link desc)))
  6405. (setq desc (read-string "Description: " desc))
  6406. (unless (string-match "\\S-" desc) (setq desc nil))
  6407. (if remove (apply 'delete-region remove))
  6408. (insert (org-make-link-string link desc))))
  6409. (defun org-completing-read (&rest args)
  6410. "Completing-read with SPACE being a normal character."
  6411. (let ((minibuffer-local-completion-map
  6412. (copy-keymap minibuffer-local-completion-map)))
  6413. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6414. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6415. (apply 'org-ido-completing-read args)))
  6416. (defun org-completing-read-no-ido (&rest args)
  6417. (let (org-completion-use-ido)
  6418. (apply 'org-completing-read args)))
  6419. (defun org-ido-completing-read (&rest args)
  6420. "Completing-read using `ido-mode' speedups if available"
  6421. (if (and org-completion-use-ido
  6422. (fboundp 'ido-completing-read)
  6423. (boundp 'ido-mode) ido-mode
  6424. (listp (second args)))
  6425. (let ((ido-enter-matching-directory nil))
  6426. (apply 'ido-completing-read (concat (car args))
  6427. (mapcar (lambda (x) (car x)) (nth 1 args))
  6428. (cddr args)))
  6429. (apply 'completing-read args)))
  6430. (defun org-extract-attributes (s)
  6431. "Extract the attributes cookie from a string and set as text property."
  6432. (let (a attr (start 0) key value)
  6433. (save-match-data
  6434. (when (string-match "{{\\([^}]+\\)}}$" s)
  6435. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6436. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6437. (setq key (match-string 1 a) value (match-string 2 a)
  6438. start (match-end 0)
  6439. attr (plist-put attr (intern key) value))))
  6440. (org-add-props s nil 'org-attr attr))
  6441. s))
  6442. (defun org-attributes-to-string (plist)
  6443. "Format a property list into an HTML attribute list."
  6444. (let ((s "") key value)
  6445. (while plist
  6446. (setq key (pop plist) value (pop plist))
  6447. (and value
  6448. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6449. s))
  6450. ;;; Opening/following a link
  6451. (defvar org-link-search-failed nil)
  6452. (defun org-next-link ()
  6453. "Move forward to the next link.
  6454. If the link is in hidden text, expose it."
  6455. (interactive)
  6456. (when (and org-link-search-failed (eq this-command last-command))
  6457. (goto-char (point-min))
  6458. (message "Link search wrapped back to beginning of buffer"))
  6459. (setq org-link-search-failed nil)
  6460. (let* ((pos (point))
  6461. (ct (org-context))
  6462. (a (assoc :link ct)))
  6463. (if a (goto-char (nth 2 a)))
  6464. (if (re-search-forward org-any-link-re nil t)
  6465. (progn
  6466. (goto-char (match-beginning 0))
  6467. (if (org-invisible-p) (org-show-context)))
  6468. (goto-char pos)
  6469. (setq org-link-search-failed t)
  6470. (error "No further link found"))))
  6471. (defun org-previous-link ()
  6472. "Move backward to the previous link.
  6473. If the link is in hidden text, expose it."
  6474. (interactive)
  6475. (when (and org-link-search-failed (eq this-command last-command))
  6476. (goto-char (point-max))
  6477. (message "Link search wrapped back to end of buffer"))
  6478. (setq org-link-search-failed nil)
  6479. (let* ((pos (point))
  6480. (ct (org-context))
  6481. (a (assoc :link ct)))
  6482. (if a (goto-char (nth 1 a)))
  6483. (if (re-search-backward org-any-link-re nil t)
  6484. (progn
  6485. (goto-char (match-beginning 0))
  6486. (if (org-invisible-p) (org-show-context)))
  6487. (goto-char pos)
  6488. (setq org-link-search-failed t)
  6489. (error "No further link found"))))
  6490. (defun org-translate-link (s)
  6491. "Translate a link string if a translation function has been defined."
  6492. (if (and org-link-translation-function
  6493. (fboundp org-link-translation-function)
  6494. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6495. (progn
  6496. (setq s (funcall org-link-translation-function
  6497. (match-string 1) (match-string 2)))
  6498. (concat (car s) ":" (cdr s)))
  6499. s))
  6500. (defun org-translate-link-from-planner (type path)
  6501. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6502. This is still an experimental function, your mileage may vary."
  6503. (cond
  6504. ((member type '("http" "https" "news" "ftp"))
  6505. ;; standard Internet links are the same.
  6506. nil)
  6507. ((and (equal type "irc") (string-match "^//" path))
  6508. ;; Planner has two / at the beginning of an irc link, we have 1.
  6509. ;; We should have zero, actually....
  6510. (setq path (substring path 1)))
  6511. ((and (equal type "lisp") (string-match "^/" path))
  6512. ;; Planner has a slash, we do not.
  6513. (setq type "elisp" path (substring path 1)))
  6514. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6515. ;; A typical message link. Planner has the id after the fina slash,
  6516. ;; we separate it with a hash mark
  6517. (setq path (concat (match-string 1 path) "#"
  6518. (org-remove-angle-brackets (match-string 2 path)))))
  6519. )
  6520. (cons type path))
  6521. (defun org-find-file-at-mouse (ev)
  6522. "Open file link or URL at mouse."
  6523. (interactive "e")
  6524. (mouse-set-point ev)
  6525. (org-open-at-point 'in-emacs))
  6526. (defun org-open-at-mouse (ev)
  6527. "Open file link or URL at mouse."
  6528. (interactive "e")
  6529. (mouse-set-point ev)
  6530. (if (eq major-mode 'org-agenda-mode)
  6531. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6532. (org-open-at-point))
  6533. (defvar org-window-config-before-follow-link nil
  6534. "The window configuration before following a link.
  6535. This is saved in case the need arises to restore it.")
  6536. (defvar org-open-link-marker (make-marker)
  6537. "Marker pointing to the location where `org-open-at-point; was called.")
  6538. ;;;###autoload
  6539. (defun org-open-at-point-global ()
  6540. "Follow a link like Org-mode does.
  6541. This command can be called in any mode to follow a link that has
  6542. Org-mode syntax."
  6543. (interactive)
  6544. (org-run-like-in-org-mode 'org-open-at-point))
  6545. ;;;###autoload
  6546. (defun org-open-link-from-string (s &optional arg)
  6547. "Open a link in the string S, as if it was in Org-mode."
  6548. (interactive "sLink: \nP")
  6549. (with-temp-buffer
  6550. (let ((org-inhibit-startup t))
  6551. (org-mode)
  6552. (insert s)
  6553. (goto-char (point-min))
  6554. (org-open-at-point arg))))
  6555. (defun org-open-at-point (&optional in-emacs)
  6556. "Open link at or after point.
  6557. If there is no link at point, this function will search forward up to
  6558. the end of the current subtree.
  6559. Normally, files will be opened by an appropriate application. If the
  6560. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6561. With a double prefix argument, try to open outside of Emacs, in the
  6562. application the system uses for this file type."
  6563. (interactive "P")
  6564. (org-load-modules-maybe)
  6565. (move-marker org-open-link-marker (point))
  6566. (setq org-window-config-before-follow-link (current-window-configuration))
  6567. (org-remove-occur-highlights nil nil t)
  6568. (cond
  6569. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6570. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6571. (org-footnote-action))
  6572. (t
  6573. (let (type path link line search (pos (point)))
  6574. (catch 'match
  6575. (save-excursion
  6576. (skip-chars-forward "^]\n\r")
  6577. (when (org-in-regexp org-bracket-link-regexp)
  6578. (setq link (org-extract-attributes
  6579. (org-link-unescape (org-match-string-no-properties 1))))
  6580. (while (string-match " *\n *" link)
  6581. (setq link (replace-match " " t t link)))
  6582. (setq link (org-link-expand-abbrev link))
  6583. (cond
  6584. ((or (file-name-absolute-p link)
  6585. (string-match "^\\.\\.?/" link))
  6586. (setq type "file" path link))
  6587. ((string-match org-link-re-with-space3 link)
  6588. (setq type (match-string 1 link) path (match-string 2 link)))
  6589. (t (setq type "thisfile" path link)))
  6590. (throw 'match t)))
  6591. (when (get-text-property (point) 'org-linked-text)
  6592. (setq type "thisfile"
  6593. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6594. (1+ (point)) (point))
  6595. path (buffer-substring
  6596. (previous-single-property-change pos 'org-linked-text)
  6597. (next-single-property-change pos 'org-linked-text)))
  6598. (throw 'match t))
  6599. (save-excursion
  6600. (when (or (org-in-regexp org-angle-link-re)
  6601. (org-in-regexp org-plain-link-re))
  6602. (setq type (match-string 1) path (match-string 2))
  6603. (throw 'match t)))
  6604. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6605. (setq type "tree-match"
  6606. path (match-string 1))
  6607. (throw 'match t))
  6608. (save-excursion
  6609. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6610. (setq type "tags"
  6611. path (match-string 1))
  6612. (while (string-match ":" path)
  6613. (setq path (replace-match "+" t t path)))
  6614. (throw 'match t))))
  6615. (unless path
  6616. (error "No link found"))
  6617. ;; Remove any trailing spaces in path
  6618. (if (string-match " +\\'" path)
  6619. (setq path (replace-match "" t t path)))
  6620. (if (and org-link-translation-function
  6621. (fboundp org-link-translation-function))
  6622. ;; Check if we need to translate the link
  6623. (let ((tmp (funcall org-link-translation-function type path)))
  6624. (setq type (car tmp) path (cdr tmp))))
  6625. (cond
  6626. ((assoc type org-link-protocols)
  6627. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6628. ((equal type "mailto")
  6629. (let ((cmd (car org-link-mailto-program))
  6630. (args (cdr org-link-mailto-program)) args1
  6631. (address path) (subject "") a)
  6632. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6633. (setq address (match-string 1 path)
  6634. subject (org-link-escape (match-string 2 path))))
  6635. (while args
  6636. (cond
  6637. ((not (stringp (car args))) (push (pop args) args1))
  6638. (t (setq a (pop args))
  6639. (if (string-match "%a" a)
  6640. (setq a (replace-match address t t a)))
  6641. (if (string-match "%s" a)
  6642. (setq a (replace-match subject t t a)))
  6643. (push a args1))))
  6644. (apply cmd (nreverse args1))))
  6645. ((member type '("http" "https" "ftp" "news"))
  6646. (browse-url (concat type ":" (org-link-escape
  6647. path org-link-escape-chars-browser))))
  6648. ((member type '("message"))
  6649. (browse-url (concat type ":" path)))
  6650. ((string= type "tags")
  6651. (org-tags-view in-emacs path))
  6652. ((string= type "thisfile")
  6653. (if in-emacs
  6654. (switch-to-buffer-other-window
  6655. (org-get-buffer-for-internal-link (current-buffer)))
  6656. (org-mark-ring-push))
  6657. (let ((cmd `(org-link-search
  6658. ,path
  6659. ,(cond ((equal in-emacs '(4)) 'occur)
  6660. ((equal in-emacs '(16)) 'org-occur)
  6661. (t nil))
  6662. ,pos)))
  6663. (condition-case nil (eval cmd)
  6664. (error (progn (widen) (eval cmd))))))
  6665. ((string= type "tree-match")
  6666. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6667. ((string= type "file")
  6668. (if (string-match "::\\([0-9]+\\)\\'" path)
  6669. (setq line (string-to-number (match-string 1 path))
  6670. path (substring path 0 (match-beginning 0)))
  6671. (if (string-match "::\\(.+\\)\\'" path)
  6672. (setq search (match-string 1 path)
  6673. path (substring path 0 (match-beginning 0)))))
  6674. (if (string-match "[*?{]" (file-name-nondirectory path))
  6675. (dired path)
  6676. (org-open-file path in-emacs line search)))
  6677. ((string= type "news")
  6678. (require 'org-gnus)
  6679. (org-gnus-follow-link path))
  6680. ((string= type "shell")
  6681. (let ((cmd path))
  6682. (if (or (not org-confirm-shell-link-function)
  6683. (funcall org-confirm-shell-link-function
  6684. (format "Execute \"%s\" in shell? "
  6685. (org-add-props cmd nil
  6686. 'face 'org-warning))))
  6687. (progn
  6688. (message "Executing %s" cmd)
  6689. (shell-command cmd))
  6690. (error "Abort"))))
  6691. ((string= type "elisp")
  6692. (let ((cmd path))
  6693. (if (or (not org-confirm-elisp-link-function)
  6694. (funcall org-confirm-elisp-link-function
  6695. (format "Execute \"%s\" as elisp? "
  6696. (org-add-props cmd nil
  6697. 'face 'org-warning))))
  6698. (message "%s => %s" cmd
  6699. (if (equal (string-to-char cmd) ?\()
  6700. (eval (read cmd))
  6701. (call-interactively (read cmd))))
  6702. (error "Abort"))))
  6703. (t
  6704. (browse-url-at-point))))))
  6705. (move-marker org-open-link-marker nil)
  6706. (run-hook-with-args 'org-follow-link-hook))
  6707. ;;;; Time estimates
  6708. (defun org-get-effort (&optional pom)
  6709. "Get the effort estimate for the current entry."
  6710. (org-entry-get pom org-effort-property))
  6711. ;;; File search
  6712. (defvar org-create-file-search-functions nil
  6713. "List of functions to construct the right search string for a file link.
  6714. These functions are called in turn with point at the location to
  6715. which the link should point.
  6716. A function in the hook should first test if it would like to
  6717. handle this file type, for example by checking the major-mode or
  6718. the file extension. If it decides not to handle this file, it
  6719. should just return nil to give other functions a chance. If it
  6720. does handle the file, it must return the search string to be used
  6721. when following the link. The search string will be part of the
  6722. file link, given after a double colon, and `org-open-at-point'
  6723. will automatically search for it. If special measures must be
  6724. taken to make the search successful, another function should be
  6725. added to the companion hook `org-execute-file-search-functions',
  6726. which see.
  6727. A function in this hook may also use `setq' to set the variable
  6728. `description' to provide a suggestion for the descriptive text to
  6729. be used for this link when it gets inserted into an Org-mode
  6730. buffer with \\[org-insert-link].")
  6731. (defvar org-execute-file-search-functions nil
  6732. "List of functions to execute a file search triggered by a link.
  6733. Functions added to this hook must accept a single argument, the
  6734. search string that was part of the file link, the part after the
  6735. double colon. The function must first check if it would like to
  6736. handle this search, for example by checking the major-mode or the
  6737. file extension. If it decides not to handle this search, it
  6738. should just return nil to give other functions a chance. If it
  6739. does handle the search, it must return a non-nil value to keep
  6740. other functions from trying.
  6741. Each function can access the current prefix argument through the
  6742. variable `current-prefix-argument'. Note that a single prefix is
  6743. used to force opening a link in Emacs, so it may be good to only
  6744. use a numeric or double prefix to guide the search function.
  6745. In case this is needed, a function in this hook can also restore
  6746. the window configuration before `org-open-at-point' was called using:
  6747. (set-window-configuration org-window-config-before-follow-link)")
  6748. (defun org-link-search (s &optional type avoid-pos)
  6749. "Search for a link search option.
  6750. If S is surrounded by forward slashes, it is interpreted as a
  6751. regular expression. In org-mode files, this will create an `org-occur'
  6752. sparse tree. In ordinary files, `occur' will be used to list matches.
  6753. If the current buffer is in `dired-mode', grep will be used to search
  6754. in all files. If AVOID-POS is given, ignore matches near that position."
  6755. (let ((case-fold-search t)
  6756. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6757. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6758. (append '(("") (" ") ("\t") ("\n"))
  6759. org-emphasis-alist)
  6760. "\\|") "\\)"))
  6761. (pos (point))
  6762. (pre nil) (post nil)
  6763. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6764. (cond
  6765. ;; First check if there are any special
  6766. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6767. ;; Now try the builtin stuff
  6768. ((save-excursion
  6769. (goto-char (point-min))
  6770. (and
  6771. (re-search-forward
  6772. (concat "<<" (regexp-quote s0) ">>") nil t)
  6773. (setq type 'dedicated
  6774. pos (match-beginning 0))))
  6775. ;; There is an exact target for this
  6776. (goto-char pos))
  6777. ((and (string-match "^(\\(.*\\))$" s0)
  6778. (save-excursion
  6779. (goto-char (point-min))
  6780. (and
  6781. (re-search-forward
  6782. (concat "[^[]" (regexp-quote
  6783. (format org-coderef-label-format
  6784. (match-string 1 s0))))
  6785. nil t)
  6786. (setq type 'dedicated
  6787. pos (1+ (match-beginning 0))))))
  6788. ;; There is a coderef target for this
  6789. (goto-char pos))
  6790. ((string-match "^/\\(.*\\)/$" s)
  6791. ;; A regular expression
  6792. (cond
  6793. ((org-mode-p)
  6794. (org-occur (match-string 1 s)))
  6795. ;;((eq major-mode 'dired-mode)
  6796. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6797. (t (org-do-occur (match-string 1 s)))))
  6798. (t
  6799. ;; A normal search strings
  6800. (when (equal (string-to-char s) ?*)
  6801. ;; Anchor on headlines, post may include tags.
  6802. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6803. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6804. s (substring s 1)))
  6805. (remove-text-properties
  6806. 0 (length s)
  6807. '(face nil mouse-face nil keymap nil fontified nil) s)
  6808. ;; Make a series of regular expressions to find a match
  6809. (setq words (org-split-string s "[ \n\r\t]+")
  6810. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6811. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6812. "\\)" markers)
  6813. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6814. re2a (concat "[ \t\r\n]" re2a_)
  6815. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6816. re4 (concat "[^a-zA-Z_]" re4_)
  6817. re1 (concat pre re2 post)
  6818. re3 (concat pre (if pre re4_ re4) post)
  6819. re5 (concat pre ".*" re4)
  6820. re2 (concat pre re2)
  6821. re2a (concat pre (if pre re2a_ re2a))
  6822. re4 (concat pre (if pre re4_ re4))
  6823. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6824. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6825. re5 "\\)"
  6826. ))
  6827. (cond
  6828. ((eq type 'org-occur) (org-occur reall))
  6829. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6830. (t (goto-char (point-min))
  6831. (setq type 'fuzzy)
  6832. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6833. (org-search-not-self 1 re1 nil t)
  6834. (org-search-not-self 1 re2 nil t)
  6835. (org-search-not-self 1 re2a nil t)
  6836. (org-search-not-self 1 re3 nil t)
  6837. (org-search-not-self 1 re4 nil t)
  6838. (org-search-not-self 1 re5 nil t)
  6839. )
  6840. (goto-char (match-beginning 1))
  6841. (goto-char pos)
  6842. (error "No match")))))
  6843. (t
  6844. ;; Normal string-search
  6845. (goto-char (point-min))
  6846. (if (search-forward s nil t)
  6847. (goto-char (match-beginning 0))
  6848. (error "No match"))))
  6849. (and (org-mode-p) (org-show-context 'link-search))
  6850. type))
  6851. (defun org-search-not-self (group &rest args)
  6852. "Execute `re-search-forward', but only accept matches that do not
  6853. enclose the position of `org-open-link-marker'."
  6854. (let ((m org-open-link-marker))
  6855. (catch 'exit
  6856. (while (apply 're-search-forward args)
  6857. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6858. (goto-char (match-end group))
  6859. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6860. (> (match-beginning 0) (marker-position m))
  6861. (< (match-end 0) (marker-position m)))
  6862. (save-match-data
  6863. (or (not (org-in-regexp
  6864. org-bracket-link-analytic-regexp 1))
  6865. (not (match-end 4)) ; no description
  6866. (and (<= (match-beginning 4) (point))
  6867. (>= (match-end 4) (point))))))
  6868. (throw 'exit (point))))))))
  6869. (defun org-get-buffer-for-internal-link (buffer)
  6870. "Return a buffer to be used for displaying the link target of internal links."
  6871. (cond
  6872. ((not org-display-internal-link-with-indirect-buffer)
  6873. buffer)
  6874. ((string-match "(Clone)$" (buffer-name buffer))
  6875. (message "Buffer is already a clone, not making another one")
  6876. ;; we also do not modify visibility in this case
  6877. buffer)
  6878. (t ; make a new indirect buffer for displaying the link
  6879. (let* ((bn (buffer-name buffer))
  6880. (ibn (concat bn "(Clone)"))
  6881. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6882. (with-current-buffer ib (org-overview))
  6883. ib))))
  6884. (defun org-do-occur (regexp &optional cleanup)
  6885. "Call the Emacs command `occur'.
  6886. If CLEANUP is non-nil, remove the printout of the regular expression
  6887. in the *Occur* buffer. This is useful if the regex is long and not useful
  6888. to read."
  6889. (occur regexp)
  6890. (when cleanup
  6891. (let ((cwin (selected-window)) win beg end)
  6892. (when (setq win (get-buffer-window "*Occur*"))
  6893. (select-window win))
  6894. (goto-char (point-min))
  6895. (when (re-search-forward "match[a-z]+" nil t)
  6896. (setq beg (match-end 0))
  6897. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6898. (setq end (1- (match-beginning 0)))))
  6899. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6900. (goto-char (point-min))
  6901. (select-window cwin))))
  6902. ;;; The mark ring for links jumps
  6903. (defvar org-mark-ring nil
  6904. "Mark ring for positions before jumps in Org-mode.")
  6905. (defvar org-mark-ring-last-goto nil
  6906. "Last position in the mark ring used to go back.")
  6907. ;; Fill and close the ring
  6908. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6909. (loop for i from 1 to org-mark-ring-length do
  6910. (push (make-marker) org-mark-ring))
  6911. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6912. org-mark-ring)
  6913. (defun org-mark-ring-push (&optional pos buffer)
  6914. "Put the current position or POS into the mark ring and rotate it."
  6915. (interactive)
  6916. (setq pos (or pos (point)))
  6917. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6918. (move-marker (car org-mark-ring)
  6919. (or pos (point))
  6920. (or buffer (current-buffer)))
  6921. (message "%s"
  6922. (substitute-command-keys
  6923. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6924. (defun org-mark-ring-goto (&optional n)
  6925. "Jump to the previous position in the mark ring.
  6926. With prefix arg N, jump back that many stored positions. When
  6927. called several times in succession, walk through the entire ring.
  6928. Org-mode commands jumping to a different position in the current file,
  6929. or to another Org-mode file, automatically push the old position
  6930. onto the ring."
  6931. (interactive "p")
  6932. (let (p m)
  6933. (if (eq last-command this-command)
  6934. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6935. (setq p org-mark-ring))
  6936. (setq org-mark-ring-last-goto p)
  6937. (setq m (car p))
  6938. (switch-to-buffer (marker-buffer m))
  6939. (goto-char m)
  6940. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6941. (defun org-remove-angle-brackets (s)
  6942. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6943. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6944. s)
  6945. (defun org-add-angle-brackets (s)
  6946. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6947. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6948. s)
  6949. (defun org-remove-double-quotes (s)
  6950. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6951. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6952. s)
  6953. ;;; Following specific links
  6954. (defun org-follow-timestamp-link ()
  6955. (cond
  6956. ((org-at-date-range-p t)
  6957. (let ((org-agenda-start-on-weekday)
  6958. (t1 (match-string 1))
  6959. (t2 (match-string 2)))
  6960. (setq t1 (time-to-days (org-time-string-to-time t1))
  6961. t2 (time-to-days (org-time-string-to-time t2)))
  6962. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6963. ((org-at-timestamp-p t)
  6964. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6965. (substring (match-string 1) 0 10)))
  6966. 1))
  6967. (t (error "This should not happen"))))
  6968. ;;; Following file links
  6969. (defvar org-wait nil)
  6970. (defun org-open-file (path &optional in-emacs line search)
  6971. "Open the file at PATH.
  6972. First, this expands any special file name abbreviations. Then the
  6973. configuration variable `org-file-apps' is checked if it contains an
  6974. entry for this file type, and if yes, the corresponding command is launched.
  6975. If no application is found, Emacs simply visits the file.
  6976. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6977. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6978. and o use an external application to visit the file.
  6979. Optional LINE specifies a line to go to, optional SEARCH a string to
  6980. search for. If LINE or SEARCH is given, the file will always be
  6981. opened in Emacs.
  6982. If the file does not exist, an error is thrown."
  6983. (setq in-emacs (or in-emacs line search))
  6984. (let* ((file (if (equal path "")
  6985. buffer-file-name
  6986. (substitute-in-file-name (expand-file-name path))))
  6987. (apps (append org-file-apps (org-default-apps)))
  6988. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6989. (dirp (if remp nil (file-directory-p file)))
  6990. (file (if (and dirp org-open-directory-means-index-dot-org)
  6991. (concat (file-name-as-directory file) "index.org")
  6992. file))
  6993. (a-m-a-p (assq 'auto-mode apps))
  6994. (dfile (downcase file))
  6995. (old-buffer (current-buffer))
  6996. (old-pos (point))
  6997. (old-mode major-mode)
  6998. ext cmd)
  6999. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7000. (setq ext (match-string 1 dfile))
  7001. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7002. (setq ext (match-string 1 dfile))))
  7003. (cond
  7004. ((equal in-emacs '(16))
  7005. (setq cmd (cdr (assoc 'system apps))))
  7006. (in-emacs (setq cmd 'emacs))
  7007. (t
  7008. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7009. (and dirp (cdr (assoc 'directory apps)))
  7010. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7011. 'string-match)
  7012. (cdr (assoc ext apps))
  7013. (cdr (assoc t apps))))))
  7014. (when (eq cmd 'system)
  7015. (setq cmd (cdr (assoc 'system apps))))
  7016. (when (eq cmd 'default)
  7017. (setq cmd (cdr (assoc t apps))))
  7018. (when (eq cmd 'mailcap)
  7019. (require 'mailcap)
  7020. (mailcap-parse-mailcaps)
  7021. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7022. (command (mailcap-mime-info mime-type)))
  7023. (if (stringp command)
  7024. (setq cmd command)
  7025. (setq cmd 'emacs))))
  7026. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7027. (not (file-exists-p file))
  7028. (not org-open-non-existing-files))
  7029. (error "No such file: %s" file))
  7030. (cond
  7031. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7032. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7033. (while (string-match "['\"]%s['\"]" cmd)
  7034. (setq cmd (replace-match "%s" t t cmd)))
  7035. (while (string-match "%s" cmd)
  7036. (setq cmd (replace-match
  7037. (save-match-data
  7038. (shell-quote-argument
  7039. (convert-standard-filename file)))
  7040. t t cmd)))
  7041. (save-window-excursion
  7042. (start-process-shell-command cmd nil cmd)
  7043. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7044. ))
  7045. ((or (stringp cmd)
  7046. (eq cmd 'emacs))
  7047. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7048. (widen)
  7049. (if line (goto-line line)
  7050. (if search (org-link-search search))))
  7051. ((consp cmd)
  7052. (let ((file (convert-standard-filename file)))
  7053. (eval cmd)))
  7054. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7055. (and (org-mode-p) (eq old-mode 'org-mode)
  7056. (or (not (equal old-buffer (current-buffer)))
  7057. (not (equal old-pos (point))))
  7058. (org-mark-ring-push old-pos old-buffer))))
  7059. (defun org-default-apps ()
  7060. "Return the default applications for this operating system."
  7061. (cond
  7062. ((eq system-type 'darwin)
  7063. org-file-apps-defaults-macosx)
  7064. ((eq system-type 'windows-nt)
  7065. org-file-apps-defaults-windowsnt)
  7066. (t org-file-apps-defaults-gnu)))
  7067. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7068. "Convert extensions to regular expressions in the cars of LIST.
  7069. Also, weed out any non-string entries, because the return value is used
  7070. only for regexp matching.
  7071. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7072. point to the symbol `emacs', indicating that the file should
  7073. be opened in Emacs."
  7074. (append
  7075. (delq nil
  7076. (mapcar (lambda (x)
  7077. (if (not (stringp (car x)))
  7078. nil
  7079. (if (string-match "\\W" (car x))
  7080. x
  7081. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7082. list))
  7083. (if add-auto-mode
  7084. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7085. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7086. (defun org-file-remote-p (file)
  7087. "Test whether FILE specifies a location on a remote system.
  7088. Return non-nil if the location is indeed remote.
  7089. For example, the filename \"/user@host:/foo\" specifies a location
  7090. on the system \"/user@host:\"."
  7091. (cond ((fboundp 'file-remote-p)
  7092. (file-remote-p file))
  7093. ((fboundp 'tramp-handle-file-remote-p)
  7094. (tramp-handle-file-remote-p file))
  7095. ((and (boundp 'ange-ftp-name-format)
  7096. (string-match (car ange-ftp-name-format) file))
  7097. t)
  7098. (t nil)))
  7099. ;;;; Refiling
  7100. (defun org-get-org-file ()
  7101. "Read a filename, with default directory `org-directory'."
  7102. (let ((default (or org-default-notes-file remember-data-file)))
  7103. (read-file-name (format "File name [%s]: " default)
  7104. (file-name-as-directory org-directory)
  7105. default)))
  7106. (defun org-notes-order-reversed-p ()
  7107. "Check if the current file should receive notes in reversed order."
  7108. (cond
  7109. ((not org-reverse-note-order) nil)
  7110. ((eq t org-reverse-note-order) t)
  7111. ((not (listp org-reverse-note-order)) nil)
  7112. (t (catch 'exit
  7113. (let ((all org-reverse-note-order)
  7114. entry)
  7115. (while (setq entry (pop all))
  7116. (if (string-match (car entry) buffer-file-name)
  7117. (throw 'exit (cdr entry))))
  7118. nil)))))
  7119. (defvar org-refile-target-table nil
  7120. "The list of refile targets, created by `org-refile'.")
  7121. (defvar org-agenda-new-buffers nil
  7122. "Buffers created to visit agenda files.")
  7123. (defun org-get-refile-targets (&optional default-buffer)
  7124. "Produce a table with refile targets."
  7125. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7126. targets txt re files f desc descre fast-path-p level)
  7127. (message "Getting targets...")
  7128. (with-current-buffer (or default-buffer (current-buffer))
  7129. (while (setq entry (pop entries))
  7130. (setq files (car entry) desc (cdr entry))
  7131. (setq fast-path-p nil)
  7132. (cond
  7133. ((null files) (setq files (list (current-buffer))))
  7134. ((eq files 'org-agenda-files)
  7135. (setq files (org-agenda-files 'unrestricted)))
  7136. ((and (symbolp files) (fboundp files))
  7137. (setq files (funcall files)))
  7138. ((and (symbolp files) (boundp files))
  7139. (setq files (symbol-value files))))
  7140. (if (stringp files) (setq files (list files)))
  7141. (cond
  7142. ((eq (car desc) :tag)
  7143. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7144. ((eq (car desc) :todo)
  7145. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7146. ((eq (car desc) :regexp)
  7147. (setq descre (cdr desc)))
  7148. ((eq (car desc) :level)
  7149. (setq descre (concat "^\\*\\{" (number-to-string
  7150. (if org-odd-levels-only
  7151. (1- (* 2 (cdr desc)))
  7152. (cdr desc)))
  7153. "\\}[ \t]")))
  7154. ((eq (car desc) :maxlevel)
  7155. (setq fast-path-p t)
  7156. (setq descre (concat "^\\*\\{1," (number-to-string
  7157. (if org-odd-levels-only
  7158. (1- (* 2 (cdr desc)))
  7159. (cdr desc)))
  7160. "\\}[ \t]")))
  7161. (t (error "Bad refiling target description %s" desc)))
  7162. (while (setq f (pop files))
  7163. (save-excursion
  7164. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7165. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7166. (setq f (expand-file-name f))
  7167. (save-excursion
  7168. (save-restriction
  7169. (widen)
  7170. (goto-char (point-min))
  7171. (while (re-search-forward descre nil t)
  7172. (goto-char (point-at-bol))
  7173. (when (looking-at org-complex-heading-regexp)
  7174. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7175. txt (org-link-display-format (match-string 4))
  7176. re (concat "^" (regexp-quote
  7177. (buffer-substring (match-beginning 1)
  7178. (match-end 4)))))
  7179. (if (match-end 5) (setq re (concat re "[ \t]+"
  7180. (regexp-quote
  7181. (match-string 5)))))
  7182. (setq re (concat re "[ \t]*$"))
  7183. (when org-refile-use-outline-path
  7184. (setq txt (mapconcat 'org-protect-slash
  7185. (append
  7186. (if (eq org-refile-use-outline-path 'file)
  7187. (list (file-name-nondirectory
  7188. (buffer-file-name (buffer-base-buffer))))
  7189. (if (eq org-refile-use-outline-path 'full-file-path)
  7190. (list (buffer-file-name (buffer-base-buffer)))))
  7191. (org-get-outline-path fast-path-p level txt)
  7192. (list txt))
  7193. "/")))
  7194. (push (list txt f re (point)) targets))
  7195. (goto-char (point-at-eol))))))))
  7196. (message "Getting targets...done")
  7197. (nreverse targets))))
  7198. (defun org-protect-slash (s)
  7199. (while (string-match "/" s)
  7200. (setq s (replace-match "\\" t t s)))
  7201. s)
  7202. (defvar org-olpa (make-vector 20 nil))
  7203. (defun org-get-outline-path (&optional fastp level heading)
  7204. "Return the outline path to the current entry, as a list."
  7205. (if fastp
  7206. (progn
  7207. (if (> level 19)
  7208. (error "Outline path failure, more than 19 levels."))
  7209. (loop for i from level upto 19 do
  7210. (aset org-olpa i nil))
  7211. (prog1
  7212. (delq nil (append org-olpa nil))
  7213. (aset org-olpa level heading)))
  7214. (let (rtn)
  7215. (save-excursion
  7216. (while (org-up-heading-safe)
  7217. (when (looking-at org-complex-heading-regexp)
  7218. (push (org-match-string-no-properties 4) rtn)))
  7219. rtn))))
  7220. (defvar org-refile-history nil
  7221. "History for refiling operations.")
  7222. (defun org-refile (&optional goto default-buffer)
  7223. "Move the entry at point to another heading.
  7224. The list of target headings is compiled using the information in
  7225. `org-refile-targets', which see. This list is created before each use
  7226. and will therefore always be up-to-date.
  7227. At the target location, the entry is filed as a subitem of the target heading.
  7228. Depending on `org-reverse-note-order', the new subitem will either be the
  7229. first or the last subitem.
  7230. If there is an active region, all entries in that region will be moved.
  7231. However, the region must fulfil the requirement that the first heading
  7232. is the first one sets the top-level of the moved text - at most siblings
  7233. below it are allowed.
  7234. With prefix arg GOTO, the command will only visit the target location,
  7235. not actually move anything.
  7236. With a double prefix `C-u C-u', go to the location where the last refiling
  7237. operation has put the subtree."
  7238. (interactive "P")
  7239. (let* ((cbuf (current-buffer))
  7240. (regionp (org-region-active-p))
  7241. (region-start (and regionp (region-beginning)))
  7242. (region-end (and regionp (region-end)))
  7243. (region-length (and regionp (- region-end region-start)))
  7244. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7245. pos it nbuf file re level reversed)
  7246. (when regionp (goto-char region-start)
  7247. (unless (org-kill-is-subtree-p
  7248. (buffer-substring region-start region-end))
  7249. (error "The region is not a (sequence of) subtree(s)")))
  7250. (if (equal goto '(16))
  7251. (org-refile-goto-last-stored)
  7252. (when (setq it (org-refile-get-location
  7253. (if goto "Goto: " "Refile to: ") default-buffer))
  7254. (setq file (nth 1 it)
  7255. re (nth 2 it)
  7256. pos (nth 3 it))
  7257. (if (and (equal (buffer-file-name) file)
  7258. (if regionp
  7259. (and (>= pos region-start)
  7260. (<= pos region-end))
  7261. (and (>= pos (point))
  7262. (< pos (save-excursion
  7263. (org-end-of-subtree t t))))))
  7264. (error "Cannot refile to position inside the tree or region"))
  7265. (setq nbuf (or (find-buffer-visiting file)
  7266. (find-file-noselect file)))
  7267. (if goto
  7268. (progn
  7269. (switch-to-buffer nbuf)
  7270. (goto-char pos)
  7271. (org-show-context 'org-goto))
  7272. (if regionp
  7273. (progn
  7274. (kill-new (buffer-substring region-start region-end))
  7275. (org-save-markers-in-region region-start region-end))
  7276. (org-copy-subtree 1 nil t))
  7277. (save-excursion
  7278. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7279. (find-file-noselect file))))
  7280. (setq reversed (org-notes-order-reversed-p))
  7281. (save-excursion
  7282. (save-restriction
  7283. (widen)
  7284. (goto-char pos)
  7285. (looking-at outline-regexp)
  7286. (setq level (org-get-valid-level (funcall outline-level) 1))
  7287. (goto-char
  7288. (if reversed
  7289. (or (outline-next-heading) (point-max))
  7290. (or (save-excursion (outline-get-next-sibling))
  7291. (org-end-of-subtree t t)
  7292. (point-max))))
  7293. (if (not (bolp)) (newline))
  7294. (bookmark-set "org-refile-last-stored")
  7295. (org-paste-subtree level))))
  7296. (if regionp
  7297. (delete-region (point) (+ (point) region-length))
  7298. (org-cut-subtree))
  7299. (setq org-markers-to-move nil)
  7300. (message "Refiled to \"%s\"" (car it)))))))
  7301. (defun org-refile-goto-last-stored ()
  7302. "Go to the location where the last refile was stored."
  7303. (interactive)
  7304. (bookmark-jump "org-refile-last-stored")
  7305. (message "This is the location of the last refile"))
  7306. (defun org-refile-get-location (&optional prompt default-buffer)
  7307. "Prompt the user for a refile location, using PROMPT."
  7308. (let ((org-refile-targets org-refile-targets)
  7309. (org-refile-use-outline-path org-refile-use-outline-path))
  7310. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7311. (unless org-refile-target-table
  7312. (error "No refile targets"))
  7313. (let* ((cbuf (current-buffer))
  7314. (partial-completion-mode nil)
  7315. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7316. (cfunc (if (and org-refile-use-outline-path
  7317. org-outline-path-complete-in-steps)
  7318. 'org-olpath-completing-read
  7319. 'org-ido-completing-read))
  7320. (extra (if org-refile-use-outline-path "/" ""))
  7321. (filename (and cfn (expand-file-name cfn)))
  7322. (tbl (mapcar
  7323. (lambda (x)
  7324. (if (not (equal filename (nth 1 x)))
  7325. (cons (concat (car x) extra " ("
  7326. (file-name-nondirectory (nth 1 x)) ")")
  7327. (cdr x))
  7328. (cons (concat (car x) extra) (cdr x))))
  7329. org-refile-target-table))
  7330. (completion-ignore-case t))
  7331. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7332. tbl)))
  7333. (defun org-olpath-completing-read (prompt collection &rest args)
  7334. "Read an outline path like a file name."
  7335. (let ((thetable collection)
  7336. (org-completion-use-ido nil)) ; does not work with ido.
  7337. (apply
  7338. 'org-ido-completing-read prompt
  7339. (lambda (string predicate &optional flag)
  7340. (let (rtn r f (l (length string)))
  7341. (cond
  7342. ((eq flag nil)
  7343. ;; try completion
  7344. (try-completion string thetable))
  7345. ((eq flag t)
  7346. ;; all-completions
  7347. (setq rtn (all-completions string thetable predicate))
  7348. (mapcar
  7349. (lambda (x)
  7350. (setq r (substring x l))
  7351. (if (string-match " ([^)]*)$" x)
  7352. (setq f (match-string 0 x))
  7353. (setq f ""))
  7354. (if (string-match "/" r)
  7355. (concat string (substring r 0 (match-end 0)) f)
  7356. x))
  7357. rtn))
  7358. ((eq flag 'lambda)
  7359. ;; exact match?
  7360. (assoc string thetable)))
  7361. ))
  7362. args)))
  7363. ;;;; Dynamic blocks
  7364. (defun org-find-dblock (name)
  7365. "Find the first dynamic block with name NAME in the buffer.
  7366. If not found, stay at current position and return nil."
  7367. (let (pos)
  7368. (save-excursion
  7369. (goto-char (point-min))
  7370. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7371. nil t)
  7372. (match-beginning 0))))
  7373. (if pos (goto-char pos))
  7374. pos))
  7375. (defconst org-dblock-start-re
  7376. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7377. "Matches the startline of a dynamic block, with parameters.")
  7378. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7379. "Matches the end of a dynamic block.")
  7380. (defun org-create-dblock (plist)
  7381. "Create a dynamic block section, with parameters taken from PLIST.
  7382. PLIST must contain a :name entry which is used as name of the block."
  7383. (unless (bolp) (newline))
  7384. (let ((name (plist-get plist :name)))
  7385. (insert "#+BEGIN: " name)
  7386. (while plist
  7387. (if (eq (car plist) :name)
  7388. (setq plist (cddr plist))
  7389. (insert " " (prin1-to-string (pop plist)))))
  7390. (insert "\n\n#+END:\n")
  7391. (beginning-of-line -2)))
  7392. (defun org-prepare-dblock ()
  7393. "Prepare dynamic block for refresh.
  7394. This empties the block, puts the cursor at the insert position and returns
  7395. the property list including an extra property :name with the block name."
  7396. (unless (looking-at org-dblock-start-re)
  7397. (error "Not at a dynamic block"))
  7398. (let* ((begdel (1+ (match-end 0)))
  7399. (name (org-no-properties (match-string 1)))
  7400. (params (append (list :name name)
  7401. (read (concat "(" (match-string 3) ")")))))
  7402. (unless (re-search-forward org-dblock-end-re nil t)
  7403. (error "Dynamic block not terminated"))
  7404. (setq params
  7405. (append params
  7406. (list :content (buffer-substring
  7407. begdel (match-beginning 0)))))
  7408. (delete-region begdel (match-beginning 0))
  7409. (goto-char begdel)
  7410. (open-line 1)
  7411. params))
  7412. (defun org-map-dblocks (&optional command)
  7413. "Apply COMMAND to all dynamic blocks in the current buffer.
  7414. If COMMAND is not given, use `org-update-dblock'."
  7415. (let ((cmd (or command 'org-update-dblock))
  7416. pos)
  7417. (save-excursion
  7418. (goto-char (point-min))
  7419. (while (re-search-forward org-dblock-start-re nil t)
  7420. (goto-char (setq pos (match-beginning 0)))
  7421. (condition-case nil
  7422. (funcall cmd)
  7423. (error (message "Error during update of dynamic block")))
  7424. (goto-char pos)
  7425. (unless (re-search-forward org-dblock-end-re nil t)
  7426. (error "Dynamic block not terminated"))))))
  7427. (defun org-dblock-update (&optional arg)
  7428. "User command for updating dynamic blocks.
  7429. Update the dynamic block at point. With prefix ARG, update all dynamic
  7430. blocks in the buffer."
  7431. (interactive "P")
  7432. (if arg
  7433. (org-update-all-dblocks)
  7434. (or (looking-at org-dblock-start-re)
  7435. (org-beginning-of-dblock))
  7436. (org-update-dblock)))
  7437. (defun org-update-dblock ()
  7438. "Update the dynamic block at point
  7439. This means to empty the block, parse for parameters and then call
  7440. the correct writing function."
  7441. (save-window-excursion
  7442. (let* ((pos (point))
  7443. (line (org-current-line))
  7444. (params (org-prepare-dblock))
  7445. (name (plist-get params :name))
  7446. (cmd (intern (concat "org-dblock-write:" name))))
  7447. (message "Updating dynamic block `%s' at line %d..." name line)
  7448. (funcall cmd params)
  7449. (message "Updating dynamic block `%s' at line %d...done" name line)
  7450. (goto-char pos))))
  7451. (defun org-beginning-of-dblock ()
  7452. "Find the beginning of the dynamic block at point.
  7453. Error if there is no such block at point."
  7454. (let ((pos (point))
  7455. beg)
  7456. (end-of-line 1)
  7457. (if (and (re-search-backward org-dblock-start-re nil t)
  7458. (setq beg (match-beginning 0))
  7459. (re-search-forward org-dblock-end-re nil t)
  7460. (> (match-end 0) pos))
  7461. (goto-char beg)
  7462. (goto-char pos)
  7463. (error "Not in a dynamic block"))))
  7464. (defun org-update-all-dblocks ()
  7465. "Update all dynamic blocks in the buffer.
  7466. This function can be used in a hook."
  7467. (when (org-mode-p)
  7468. (org-map-dblocks 'org-update-dblock)))
  7469. ;;;; Completion
  7470. (defconst org-additional-option-like-keywords
  7471. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7472. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7473. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7474. "BEGIN_QUOTE" "END_QUOTE"
  7475. "BEGIN_VERSE" "END_VERSE"
  7476. "BEGIN_SRC" "END_SRC"
  7477. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7478. (defcustom org-structure-template-alist
  7479. '(
  7480. ("s" "#+begin_src ?\n\n#+end_src"
  7481. "<src lang=\"?\">\n\n</src>")
  7482. ("e" "#+begin_example\n?\n#+end_example"
  7483. "<example>\n?\n</example>")
  7484. ("q" "#+begin_quote\n?\n#+end_quote"
  7485. "<quote>\n?\n</quote>")
  7486. ("v" "#+begin_verse\n?\n#+end_verse"
  7487. "<verse>\n?\n/verse>")
  7488. ("l" "#+begin_latex\n?\n#+end_latex"
  7489. "<literal style=\"latex\">\n?\n</literal>")
  7490. ("L" "#+latex: "
  7491. "<literal style=\"latex\">?</literal>")
  7492. ("h" "#+begin_html\n?\n#+end_html"
  7493. "<literal style=\"html\">\n?\n</literal>")
  7494. ("H" "#+html: "
  7495. "<literal style=\"html\">?</literal>")
  7496. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7497. ("A" "#+ascii: ")
  7498. ("i" "#+include %file ?"
  7499. "<include file=%file markup=\"?\">")
  7500. )
  7501. "Structure completion elements.
  7502. This is a list of abbreviation keys and values. The value gets inserted
  7503. it you type @samp{.} followed by the key and then the completion key,
  7504. usually `M-TAB'. %file will be replaced by a file name after prompting
  7505. for the file using completion.
  7506. There are two templates for each key, the first uses the original Org syntax,
  7507. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7508. the default when the /org-mtags.el/ module has been loaded. See also the
  7509. variable `org-mtags-prefer-muse-templates'.
  7510. This is an experimental feature, it is undecided if it is going to stay in."
  7511. :group 'org-completion
  7512. :type '(repeat
  7513. (string :tag "Key")
  7514. (string :tag "Template")
  7515. (string :tag "Muse Template")))
  7516. (defun org-try-structure-completion ()
  7517. "Try to complete a structure template before point.
  7518. This looks for strings like \"<e\" on an otherwise empty line and
  7519. expands them."
  7520. (let ((l (buffer-substring (point-at-bol) (point)))
  7521. a)
  7522. (when (and (looking-at "[ \t]*$")
  7523. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7524. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7525. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7526. (match-beginning 1)) a)
  7527. t)))
  7528. (defun org-complete-expand-structure-template (start cell)
  7529. "Expand a structure template."
  7530. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7531. (rpl (nth (if musep 2 1) cell)))
  7532. (delete-region start (point))
  7533. (when (string-match "\\`#\\+" rpl)
  7534. (cond
  7535. ((bolp))
  7536. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7537. (delete-region (point-at-bol) (point)))
  7538. (t (newline))))
  7539. (setq start (point))
  7540. (if (string-match "%file" rpl)
  7541. (setq rpl (replace-match
  7542. (concat
  7543. "\""
  7544. (save-match-data
  7545. (abbreviate-file-name (read-file-name "Include file: ")))
  7546. "\"")
  7547. t t rpl)))
  7548. (insert rpl)
  7549. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7550. (defun org-complete (&optional arg)
  7551. "Perform completion on word at point.
  7552. At the beginning of a headline, this completes TODO keywords as given in
  7553. `org-todo-keywords'.
  7554. If the current word is preceded by a backslash, completes the TeX symbols
  7555. that are supported for HTML support.
  7556. If the current word is preceded by \"#+\", completes special words for
  7557. setting file options.
  7558. In the line after \"#+STARTUP:, complete valid keywords.\"
  7559. At all other locations, this simply calls the value of
  7560. `org-completion-fallback-command'."
  7561. (interactive "P")
  7562. (org-without-partial-completion
  7563. (catch 'exit
  7564. (let* ((a nil)
  7565. (end (point))
  7566. (beg1 (save-excursion
  7567. (skip-chars-backward (org-re "[:alnum:]_@"))
  7568. (point)))
  7569. (beg (save-excursion
  7570. (skip-chars-backward "a-zA-Z0-9_:$")
  7571. (point)))
  7572. (confirm (lambda (x) (stringp (car x))))
  7573. (searchhead (equal (char-before beg) ?*))
  7574. (struct
  7575. (when (and (member (char-before beg1) '(?. ?<))
  7576. (setq a (assoc (buffer-substring beg1 (point))
  7577. org-structure-template-alist)))
  7578. (org-complete-expand-structure-template (1- beg1) a)
  7579. (throw 'exit t)))
  7580. (tag (and (equal (char-before beg1) ?:)
  7581. (equal (char-after (point-at-bol)) ?*)))
  7582. (prop (and (equal (char-before beg1) ?:)
  7583. (not (equal (char-after (point-at-bol)) ?*))))
  7584. (texp (equal (char-before beg) ?\\))
  7585. (link (equal (char-before beg) ?\[))
  7586. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7587. beg)
  7588. "#+"))
  7589. (startup (string-match "^#\\+STARTUP:.*"
  7590. (buffer-substring (point-at-bol) (point))))
  7591. (completion-ignore-case opt)
  7592. (type nil)
  7593. (tbl nil)
  7594. (table (cond
  7595. (opt
  7596. (setq type :opt)
  7597. (require 'org-exp)
  7598. (append
  7599. (mapcar
  7600. (lambda (x)
  7601. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7602. (cons (match-string 2 x) (match-string 1 x)))
  7603. (org-split-string (org-get-current-options) "\n"))
  7604. (mapcar 'list org-additional-option-like-keywords)))
  7605. (startup
  7606. (setq type :startup)
  7607. org-startup-options)
  7608. (link (append org-link-abbrev-alist-local
  7609. org-link-abbrev-alist))
  7610. (texp
  7611. (setq type :tex)
  7612. org-html-entities)
  7613. ((string-match "\\`\\*+[ \t]+\\'"
  7614. (buffer-substring (point-at-bol) beg))
  7615. (setq type :todo)
  7616. (mapcar 'list org-todo-keywords-1))
  7617. (searchhead
  7618. (setq type :searchhead)
  7619. (save-excursion
  7620. (goto-char (point-min))
  7621. (while (re-search-forward org-todo-line-regexp nil t)
  7622. (push (list
  7623. (org-make-org-heading-search-string
  7624. (match-string 3) t))
  7625. tbl)))
  7626. tbl)
  7627. (tag (setq type :tag beg beg1)
  7628. (or org-tag-alist (org-get-buffer-tags)))
  7629. (prop (setq type :prop beg beg1)
  7630. (mapcar 'list (org-buffer-property-keys nil t t)))
  7631. (t (progn
  7632. (call-interactively org-completion-fallback-command)
  7633. (throw 'exit nil)))))
  7634. (pattern (buffer-substring-no-properties beg end))
  7635. (completion (try-completion pattern table confirm)))
  7636. (cond ((eq completion t)
  7637. (if (not (assoc (upcase pattern) table))
  7638. (message "Already complete")
  7639. (if (and (equal type :opt)
  7640. (not (member (car (assoc (upcase pattern) table))
  7641. org-additional-option-like-keywords)))
  7642. (insert (substring (cdr (assoc (upcase pattern) table))
  7643. (length pattern)))
  7644. (if (memq type '(:tag :prop)) (insert ":")))))
  7645. ((null completion)
  7646. (message "Can't find completion for \"%s\"" pattern)
  7647. (ding))
  7648. ((not (string= pattern completion))
  7649. (delete-region beg end)
  7650. (if (string-match " +$" completion)
  7651. (setq completion (replace-match "" t t completion)))
  7652. (insert completion)
  7653. (if (get-buffer-window "*Completions*")
  7654. (delete-window (get-buffer-window "*Completions*")))
  7655. (if (assoc completion table)
  7656. (if (eq type :todo) (insert " ")
  7657. (if (memq type '(:tag :prop)) (insert ":"))))
  7658. (if (and (equal type :opt) (assoc completion table))
  7659. (message "%s" (substitute-command-keys
  7660. "Press \\[org-complete] again to insert example settings"))))
  7661. (t
  7662. (message "Making completion list...")
  7663. (let ((list (sort (all-completions pattern table confirm)
  7664. 'string<)))
  7665. (with-output-to-temp-buffer "*Completions*"
  7666. (condition-case nil
  7667. ;; Protection needed for XEmacs and emacs 21
  7668. (display-completion-list list pattern)
  7669. (error (display-completion-list list)))))
  7670. (message "Making completion list...%s" "done")))))))
  7671. ;;;; TODO, DEADLINE, Comments
  7672. (defun org-toggle-comment ()
  7673. "Change the COMMENT state of an entry."
  7674. (interactive)
  7675. (save-excursion
  7676. (org-back-to-heading)
  7677. (let (case-fold-search)
  7678. (if (looking-at (concat outline-regexp
  7679. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7680. (replace-match "" t t nil 1)
  7681. (if (looking-at outline-regexp)
  7682. (progn
  7683. (goto-char (match-end 0))
  7684. (insert org-comment-string " ")))))))
  7685. (defvar org-last-todo-state-is-todo nil
  7686. "This is non-nil when the last TODO state change led to a TODO state.
  7687. If the last change removed the TODO tag or switched to DONE, then
  7688. this is nil.")
  7689. (defvar org-setting-tags nil) ; dynamically skipped
  7690. (defun org-parse-local-options (string var)
  7691. "Parse STRING for startup setting relevant for variable VAR."
  7692. (let ((rtn (symbol-value var))
  7693. e opts)
  7694. (save-match-data
  7695. (if (or (not string) (not (string-match "\\S-" string)))
  7696. rtn
  7697. (setq opts (delq nil (mapcar (lambda (x)
  7698. (setq e (assoc x org-startup-options))
  7699. (if (eq (nth 1 e) var) e nil))
  7700. (org-split-string string "[ \t]+"))))
  7701. (if (not opts)
  7702. rtn
  7703. (setq rtn nil)
  7704. (while (setq e (pop opts))
  7705. (if (not (nth 3 e))
  7706. (setq rtn (nth 2 e))
  7707. (if (not (listp rtn)) (setq rtn nil))
  7708. (push (nth 2 e) rtn)))
  7709. rtn)))))
  7710. (defvar org-todo-setup-filter-hook nil
  7711. "Hook for functions that pre-filter todo specs.
  7712. Each function takes a todo spec and returns either `nil' or the spec
  7713. transformed into canonical form." )
  7714. (defvar org-todo-get-default-hook nil
  7715. "Hook for functions that get a default item for todo.
  7716. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7717. `nil' or a string to be used for the todo mark." )
  7718. (defvar org-agenda-headline-snapshot-before-repeat)
  7719. (defun org-todo (&optional arg)
  7720. "Change the TODO state of an item.
  7721. The state of an item is given by a keyword at the start of the heading,
  7722. like
  7723. *** TODO Write paper
  7724. *** DONE Call mom
  7725. The different keywords are specified in the variable `org-todo-keywords'.
  7726. By default the available states are \"TODO\" and \"DONE\".
  7727. So for this example: when the item starts with TODO, it is changed to DONE.
  7728. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7729. DONE are present, add TODO at the beginning of the heading.
  7730. With C-u prefix arg, use completion to determine the new state.
  7731. With numeric prefix arg, switch to that state.
  7732. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7733. With a tripple C-u prefix, circumvent any state blocking.
  7734. For calling through lisp, arg is also interpreted in the following way:
  7735. 'none -> empty state
  7736. \"\"(empty string) -> switch to empty state
  7737. 'done -> switch to DONE
  7738. 'nextset -> switch to the next set of keywords
  7739. 'previousset -> switch to the previous set of keywords
  7740. \"WAITING\" -> switch to the specified keyword, but only if it
  7741. really is a member of `org-todo-keywords'."
  7742. (interactive "P")
  7743. (if (equal arg '(16)) (setq arg 'nextset))
  7744. (let ((org-blocker-hook org-blocker-hook))
  7745. (when (equal arg '(64))
  7746. (setq arg nil org-blocker-hook nil))
  7747. (save-excursion
  7748. (catch 'exit
  7749. (org-back-to-heading)
  7750. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7751. (or (looking-at (concat " +" org-todo-regexp " *"))
  7752. (looking-at " *"))
  7753. (let* ((match-data (match-data))
  7754. (startpos (point-at-bol))
  7755. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7756. (org-log-done org-log-done)
  7757. (org-log-repeat org-log-repeat)
  7758. (org-todo-log-states org-todo-log-states)
  7759. (this (match-string 1))
  7760. (hl-pos (match-beginning 0))
  7761. (head (org-get-todo-sequence-head this))
  7762. (ass (assoc head org-todo-kwd-alist))
  7763. (interpret (nth 1 ass))
  7764. (done-word (nth 3 ass))
  7765. (final-done-word (nth 4 ass))
  7766. (last-state (or this ""))
  7767. (completion-ignore-case t)
  7768. (member (member this org-todo-keywords-1))
  7769. (tail (cdr member))
  7770. (state (cond
  7771. ((and org-todo-key-trigger
  7772. (or (and (equal arg '(4))
  7773. (eq org-use-fast-todo-selection 'prefix))
  7774. (and (not arg) org-use-fast-todo-selection
  7775. (not (eq org-use-fast-todo-selection
  7776. 'prefix)))))
  7777. ;; Use fast selection
  7778. (org-fast-todo-selection))
  7779. ((and (equal arg '(4))
  7780. (or (not org-use-fast-todo-selection)
  7781. (not org-todo-key-trigger)))
  7782. ;; Read a state with completion
  7783. (org-ido-completing-read
  7784. "State: " (mapcar (lambda(x) (list x))
  7785. org-todo-keywords-1)
  7786. nil t))
  7787. ((eq arg 'right)
  7788. (if this
  7789. (if tail (car tail) nil)
  7790. (car org-todo-keywords-1)))
  7791. ((eq arg 'left)
  7792. (if (equal member org-todo-keywords-1)
  7793. nil
  7794. (if this
  7795. (nth (- (length org-todo-keywords-1)
  7796. (length tail) 2)
  7797. org-todo-keywords-1)
  7798. (org-last org-todo-keywords-1))))
  7799. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7800. (setq arg nil))) ; hack to fall back to cycling
  7801. (arg
  7802. ;; user or caller requests a specific state
  7803. (cond
  7804. ((equal arg "") nil)
  7805. ((eq arg 'none) nil)
  7806. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7807. ((eq arg 'nextset)
  7808. (or (car (cdr (member head org-todo-heads)))
  7809. (car org-todo-heads)))
  7810. ((eq arg 'previousset)
  7811. (let ((org-todo-heads (reverse org-todo-heads)))
  7812. (or (car (cdr (member head org-todo-heads)))
  7813. (car org-todo-heads))))
  7814. ((car (member arg org-todo-keywords-1)))
  7815. ((nth (1- (prefix-numeric-value arg))
  7816. org-todo-keywords-1))))
  7817. ((null member) (or head (car org-todo-keywords-1)))
  7818. ((equal this final-done-word) nil) ;; -> make empty
  7819. ((null tail) nil) ;; -> first entry
  7820. ((memq interpret '(type priority))
  7821. (if (eq this-command last-command)
  7822. (car tail)
  7823. (if (> (length tail) 0)
  7824. (or done-word (car org-done-keywords))
  7825. nil)))
  7826. (t
  7827. (car tail))))
  7828. (state (or
  7829. (run-hook-with-args-until-success
  7830. 'org-todo-get-default-hook state last-state)
  7831. state))
  7832. (next (if state (concat " " state " ") " "))
  7833. (change-plist (list :type 'todo-state-change :from this :to state
  7834. :position startpos))
  7835. dolog now-done-p)
  7836. (when org-blocker-hook
  7837. (setq org-last-todo-state-is-todo
  7838. (not (member this org-done-keywords)))
  7839. (unless (save-excursion
  7840. (save-match-data
  7841. (run-hook-with-args-until-failure
  7842. 'org-blocker-hook change-plist)))
  7843. (if (interactive-p)
  7844. (error "TODO state change from %s to %s blocked" this state)
  7845. ;; fail silently
  7846. (message "TODO state change from %s to %s blocked" this state)
  7847. (throw 'exit nil))))
  7848. (store-match-data match-data)
  7849. (replace-match next t t)
  7850. (unless (pos-visible-in-window-p hl-pos)
  7851. (message "TODO state changed to %s" (org-trim next)))
  7852. (unless head
  7853. (setq head (org-get-todo-sequence-head state)
  7854. ass (assoc head org-todo-kwd-alist)
  7855. interpret (nth 1 ass)
  7856. done-word (nth 3 ass)
  7857. final-done-word (nth 4 ass)))
  7858. (when (memq arg '(nextset previousset))
  7859. (message "Keyword-Set %d/%d: %s"
  7860. (- (length org-todo-sets) -1
  7861. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7862. (length org-todo-sets)
  7863. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7864. (setq org-last-todo-state-is-todo
  7865. (not (member state org-done-keywords)))
  7866. (setq now-done-p (and (member state org-done-keywords)
  7867. (not (member this org-done-keywords))))
  7868. (and logging (org-local-logging logging))
  7869. (when (and (or org-todo-log-states org-log-done)
  7870. (not (memq arg '(nextset previousset))))
  7871. ;; we need to look at recording a time and note
  7872. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7873. (nth 2 (assoc this org-todo-log-states))))
  7874. (when (and state
  7875. (member state org-not-done-keywords)
  7876. (not (member this org-not-done-keywords)))
  7877. ;; This is now a todo state and was not one before
  7878. ;; If there was a CLOSED time stamp, get rid of it.
  7879. (org-add-planning-info nil nil 'closed))
  7880. (when (and now-done-p org-log-done)
  7881. ;; It is now done, and it was not done before
  7882. (org-add-planning-info 'closed (org-current-time))
  7883. (if (and (not dolog) (eq 'note org-log-done))
  7884. (org-add-log-setup 'done state this 'findpos 'note)))
  7885. (when (and state dolog)
  7886. ;; This is a non-nil state, and we need to log it
  7887. (org-add-log-setup 'state state this 'findpos dolog)))
  7888. ;; Fixup tag positioning
  7889. (org-todo-trigger-tag-changes state)
  7890. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7891. (when org-provide-todo-statistics
  7892. (org-update-parent-todo-statistics))
  7893. (run-hooks 'org-after-todo-state-change-hook)
  7894. (if (and arg (not (member state org-done-keywords)))
  7895. (setq head (org-get-todo-sequence-head state)))
  7896. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7897. ;; Do we need to trigger a repeat?
  7898. (when now-done-p
  7899. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7900. ;; This is for the agenda, take a snapshot of the headline.
  7901. (save-match-data
  7902. (setq org-agenda-headline-snapshot-before-repeat
  7903. (org-get-heading))))
  7904. (org-auto-repeat-maybe state))
  7905. ;; Fixup cursor location if close to the keyword
  7906. (if (and (outline-on-heading-p)
  7907. (not (bolp))
  7908. (save-excursion (beginning-of-line 1)
  7909. (looking-at org-todo-line-regexp))
  7910. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7911. (progn
  7912. (goto-char (or (match-end 2) (match-end 1)))
  7913. (just-one-space)))
  7914. (when org-trigger-hook
  7915. (save-excursion
  7916. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  7917. (defun org-block-todo-from-children-or-siblings (change-plist)
  7918. "Block turning an entry into a TODO, using the hierarchy.
  7919. This checks whether the current task should be blocked from state
  7920. changes. Such blocking occurs when:
  7921. 1. The task has children which are not all in a completed state.
  7922. 2. A task has a parent with the property :ORDERED:, and there
  7923. are siblings prior to the current task with incomplete
  7924. status."
  7925. (catch 'dont-block
  7926. ;; If this is not a todo state change, or if this entry is already DONE,
  7927. ;; do not block
  7928. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7929. (member (plist-get change-plist :from)
  7930. (cons 'done org-done-keywords))
  7931. (member (plist-get change-plist :to)
  7932. (cons 'todo org-not-done-keywords)))
  7933. (throw 'dont-block t))
  7934. ;; If this task has children, and any are undone, it's blocked
  7935. (save-excursion
  7936. (org-back-to-heading t)
  7937. (let ((this-level (funcall outline-level)))
  7938. (outline-next-heading)
  7939. (let ((child-level (funcall outline-level)))
  7940. (while (and (not (eobp))
  7941. (> child-level this-level))
  7942. ;; this todo has children, check whether they are all
  7943. ;; completed
  7944. (if (and (not (org-entry-is-done-p))
  7945. (org-entry-is-todo-p))
  7946. (throw 'dont-block nil))
  7947. (outline-next-heading)
  7948. (setq child-level (funcall outline-level))))))
  7949. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7950. ;; any previous siblings are undone, it's blocked
  7951. (save-excursion
  7952. (org-back-to-heading t)
  7953. (when (save-excursion
  7954. (ignore-errors
  7955. (org-up-heading-all 1)
  7956. (org-entry-get (point) "ORDERED")))
  7957. (let* ((this-level (funcall outline-level))
  7958. (current-level this-level))
  7959. (while (and (not (bobp))
  7960. (= current-level this-level))
  7961. (outline-previous-heading)
  7962. (setq current-level (funcall outline-level))
  7963. (if (= current-level this-level)
  7964. ;; this todo has children, check whether they are all
  7965. ;; completed
  7966. (if (and (not (org-entry-is-done-p))
  7967. (org-entry-is-todo-p))
  7968. (throw 'dont-block nil)))))))
  7969. t)) ; don't block
  7970. (defcustom org-track-ordered-property-with-tag nil
  7971. "Should the ORDERED property also be shown as a tag?
  7972. The ORDERED property decides if an entry should require subtasks to be
  7973. completed in sequence. Since a property is not very visible, setting
  7974. this option means that toggling the ORDERED property with the command
  7975. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  7976. not relevant for the behavior, but it makes things more visible.
  7977. Note that toggling the tag with tags commands will not change the property
  7978. and therefore not influence behavior!
  7979. This can be t, meaning the tag ORDERED should be used, It can also be a
  7980. string to select a different tag for this task."
  7981. :group 'org-todo
  7982. :type '(choice
  7983. (const :tag "No tracking" nil)
  7984. (const :tag "Track with ORDERED tag" t)
  7985. (string :tag "Use other tag")))
  7986. (defun org-toggle-ordered-property ()
  7987. "Toggle the ORDERED property of the current entry.
  7988. For better visibility, you can track the value of this property with a tag.
  7989. See variable `org-track-ordered-property-with-tag'."
  7990. (interactive)
  7991. (let* ((t1 org-track-ordered-property-with-tag)
  7992. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  7993. (save-excursion
  7994. (org-back-to-heading)
  7995. (if (org-entry-get nil "ORDERED")
  7996. (progn
  7997. (org-delete-property "ORDERED")
  7998. (and tag (org-toggle-tag tag 'off))
  7999. (message "Subtasks can be completed in arbitrary order"))
  8000. (org-entry-put nil "ORDERED" "t")
  8001. (and tag (org-toggle-tag tag 'on))
  8002. (message "Subtasks must be completed in sequence")))))
  8003. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8004. (defun org-block-todo-from-checkboxes (change-plist)
  8005. "Block turning an entry into a TODO, using checkboxes.
  8006. This checks whether the current task should be blocked from state
  8007. changes because there are uncheckd boxes in this entry."
  8008. (catch 'dont-block
  8009. ;; If this is not a todo state change, or if this entry is already DONE,
  8010. ;; do not block
  8011. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8012. (member (plist-get change-plist :from)
  8013. (cons 'done org-done-keywords))
  8014. (member (plist-get change-plist :to)
  8015. (cons 'todo org-not-done-keywords)))
  8016. (throw 'dont-block t))
  8017. ;; If this task has checkboxes that are not checked, it's blocked
  8018. (save-excursion
  8019. (org-back-to-heading t)
  8020. (let ((beg (point)) end)
  8021. (outline-next-heading)
  8022. (setq end (point))
  8023. (goto-char beg)
  8024. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8025. end t)
  8026. (progn
  8027. (if (boundp 'org-blocked-by-checkboxes)
  8028. (setq org-blocked-by-checkboxes t))
  8029. (throw 'dont-block nil)))))
  8030. t)) ; do not block
  8031. (defun org-update-parent-todo-statistics ()
  8032. "Update any statistics cookie in the parent of the current headline."
  8033. (interactive)
  8034. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8035. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8036. (catch 'exit
  8037. (save-excursion
  8038. (setq level (org-up-heading-safe))
  8039. (unless level
  8040. (throw 'exit nil))
  8041. (while (re-search-forward box-re (point-at-eol) t)
  8042. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8043. (setq is-percent (match-end 2))
  8044. (save-match-data
  8045. (unless (outline-next-heading) (throw 'exit nil))
  8046. (while (looking-at org-todo-line-regexp)
  8047. (setq kwd (match-string 2))
  8048. (and kwd (setq cnt-all (1+ cnt-all)))
  8049. (and (member kwd org-done-keywords)
  8050. (setq cnt-done (1+ cnt-done)))
  8051. (condition-case nil
  8052. (org-forward-same-level 1)
  8053. (error (end-of-line 1)))))
  8054. (replace-match
  8055. (if is-percent
  8056. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8057. (format "[%d/%d]" cnt-done cnt-all))))
  8058. (when cookie-present
  8059. (run-hook-with-args 'org-after-todo-statistics-hook
  8060. cnt-done (- cnt-all cnt-done)))))))
  8061. (defvar org-after-todo-statistics-hook nil
  8062. "Hook that is called after a TODO statistics cookie has been updated.
  8063. Each function is called with two arguments: the number of not-done entries
  8064. and the number of done entries.
  8065. For example, the following function, when added to this hook, will switch
  8066. an entry to DONE when all children are done, and back to TODO when new
  8067. entries are set to a TODO status. Note that this hook is only called
  8068. when there is a statistics cookie in the headline!
  8069. (defun org-summary-todo (n-done n-not-done)
  8070. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8071. (let (org-log-done org-log-states) ; turn off logging
  8072. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8073. ")
  8074. (defun org-todo-trigger-tag-changes (state)
  8075. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8076. (let ((l org-todo-state-tags-triggers)
  8077. changes)
  8078. (when (or (not state) (equal state ""))
  8079. (setq changes (append changes (cdr (assoc "" l)))))
  8080. (when (and (stringp state) (> (length state) 0))
  8081. (setq changes (append changes (cdr (assoc state l)))))
  8082. (when (member state org-not-done-keywords)
  8083. (setq changes (append changes (cdr (assoc 'todo l)))))
  8084. (when (member state org-done-keywords)
  8085. (setq changes (append changes (cdr (assoc 'done l)))))
  8086. (dolist (c changes)
  8087. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8088. (defun org-local-logging (value)
  8089. "Get logging settings from a property VALUE."
  8090. (let* (words w a)
  8091. ;; directly set the variables, they are already local.
  8092. (setq org-log-done nil
  8093. org-log-repeat nil
  8094. org-todo-log-states nil)
  8095. (setq words (org-split-string value))
  8096. (while (setq w (pop words))
  8097. (cond
  8098. ((setq a (assoc w org-startup-options))
  8099. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8100. (set (nth 1 a) (nth 2 a))))
  8101. ((setq a (org-extract-log-state-settings w))
  8102. (and (member (car a) org-todo-keywords-1)
  8103. (push a org-todo-log-states)))))))
  8104. (defun org-get-todo-sequence-head (kwd)
  8105. "Return the head of the TODO sequence to which KWD belongs.
  8106. If KWD is not set, check if there is a text property remembering the
  8107. right sequence."
  8108. (let (p)
  8109. (cond
  8110. ((not kwd)
  8111. (or (get-text-property (point-at-bol) 'org-todo-head)
  8112. (progn
  8113. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8114. nil (point-at-eol)))
  8115. (get-text-property p 'org-todo-head))))
  8116. ((not (member kwd org-todo-keywords-1))
  8117. (car org-todo-keywords-1))
  8118. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8119. (defun org-fast-todo-selection ()
  8120. "Fast TODO keyword selection with single keys.
  8121. Returns the new TODO keyword, or nil if no state change should occur."
  8122. (let* ((fulltable org-todo-key-alist)
  8123. (done-keywords org-done-keywords) ;; needed for the faces.
  8124. (maxlen (apply 'max (mapcar
  8125. (lambda (x)
  8126. (if (stringp (car x)) (string-width (car x)) 0))
  8127. fulltable)))
  8128. (expert nil)
  8129. (fwidth (+ maxlen 3 1 3))
  8130. (ncol (/ (- (window-width) 4) fwidth))
  8131. tg cnt e c tbl
  8132. groups ingroup)
  8133. (save-excursion
  8134. (save-window-excursion
  8135. (if expert
  8136. (set-buffer (get-buffer-create " *Org todo*"))
  8137. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8138. (erase-buffer)
  8139. (org-set-local 'org-done-keywords done-keywords)
  8140. (setq tbl fulltable cnt 0)
  8141. (while (setq e (pop tbl))
  8142. (cond
  8143. ((equal e '(:startgroup))
  8144. (push '() groups) (setq ingroup t)
  8145. (when (not (= cnt 0))
  8146. (setq cnt 0)
  8147. (insert "\n"))
  8148. (insert "{ "))
  8149. ((equal e '(:endgroup))
  8150. (setq ingroup nil cnt 0)
  8151. (insert "}\n"))
  8152. ((equal e '(:newline))
  8153. (when (not (= cnt 0))
  8154. (setq cnt 0)
  8155. (insert "\n")
  8156. (setq e (car tbl))
  8157. (while (equal (car tbl) '(:newline))
  8158. (insert "\n")
  8159. (setq tbl (cdr tbl)))))
  8160. (t
  8161. (setq tg (car e) c (cdr e))
  8162. (if ingroup (push tg (car groups)))
  8163. (setq tg (org-add-props tg nil 'face
  8164. (org-get-todo-face tg)))
  8165. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8166. (insert "[" c "] " tg (make-string
  8167. (- fwidth 4 (length tg)) ?\ ))
  8168. (when (= (setq cnt (1+ cnt)) ncol)
  8169. (insert "\n")
  8170. (if ingroup (insert " "))
  8171. (setq cnt 0)))))
  8172. (insert "\n")
  8173. (goto-char (point-min))
  8174. (if (not expert) (org-fit-window-to-buffer))
  8175. (message "[a-z..]:Set [SPC]:clear")
  8176. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8177. (cond
  8178. ((or (= c ?\C-g)
  8179. (and (= c ?q) (not (rassoc c fulltable))))
  8180. (setq quit-flag t))
  8181. ((= c ?\ ) nil)
  8182. ((setq e (rassoc c fulltable) tg (car e))
  8183. tg)
  8184. (t (setq quit-flag t)))))))
  8185. (defun org-entry-is-todo-p ()
  8186. (member (org-get-todo-state) org-not-done-keywords))
  8187. (defun org-entry-is-done-p ()
  8188. (member (org-get-todo-state) org-done-keywords))
  8189. (defun org-get-todo-state ()
  8190. (save-excursion
  8191. (org-back-to-heading t)
  8192. (and (looking-at org-todo-line-regexp)
  8193. (match-end 2)
  8194. (match-string 2))))
  8195. (defun org-at-date-range-p (&optional inactive-ok)
  8196. "Is the cursor inside a date range?"
  8197. (interactive)
  8198. (save-excursion
  8199. (catch 'exit
  8200. (let ((pos (point)))
  8201. (skip-chars-backward "^[<\r\n")
  8202. (skip-chars-backward "<[")
  8203. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8204. (>= (match-end 0) pos)
  8205. (throw 'exit t))
  8206. (skip-chars-backward "^<[\r\n")
  8207. (skip-chars-backward "<[")
  8208. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8209. (>= (match-end 0) pos)
  8210. (throw 'exit t)))
  8211. nil)))
  8212. (defun org-get-repeat ()
  8213. "Check if there is a deadline/schedule with repeater in this entry."
  8214. (save-match-data
  8215. (save-excursion
  8216. (org-back-to-heading t)
  8217. (if (re-search-forward
  8218. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8219. (match-string 1)))))
  8220. (defvar org-last-changed-timestamp)
  8221. (defvar org-last-inserted-timestamp)
  8222. (defvar org-log-post-message)
  8223. (defvar org-log-note-purpose)
  8224. (defvar org-log-note-how)
  8225. (defvar org-log-note-extra)
  8226. (defun org-auto-repeat-maybe (done-word)
  8227. "Check if the current headline contains a repeated deadline/schedule.
  8228. If yes, set TODO state back to what it was and change the base date
  8229. of repeating deadline/scheduled time stamps to new date.
  8230. This function is run automatically after each state change to a DONE state."
  8231. ;; last-state is dynamically scoped into this function
  8232. (let* ((repeat (org-get-repeat))
  8233. (aa (assoc last-state org-todo-kwd-alist))
  8234. (interpret (nth 1 aa))
  8235. (head (nth 2 aa))
  8236. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8237. (msg "Entry repeats: ")
  8238. (org-log-done nil)
  8239. (org-todo-log-states nil)
  8240. (nshiftmax 10) (nshift 0)
  8241. re type n what ts time)
  8242. (when repeat
  8243. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8244. (org-todo (if (eq interpret 'type) last-state head))
  8245. (when org-log-repeat
  8246. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8247. (memq 'org-add-log-note post-command-hook))
  8248. ;; OK, we are already setup for some record
  8249. (if (eq org-log-repeat 'note)
  8250. ;; make sure we take a note, not only a time stamp
  8251. (setq org-log-note-how 'note))
  8252. ;; Set up for taking a record
  8253. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8254. last-state
  8255. 'findpos org-log-repeat)))
  8256. (org-back-to-heading t)
  8257. (org-add-planning-info nil nil 'closed)
  8258. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8259. org-deadline-time-regexp "\\)\\|\\("
  8260. org-ts-regexp "\\)"))
  8261. (while (re-search-forward
  8262. re (save-excursion (outline-next-heading) (point)) t)
  8263. (setq type (if (match-end 1) org-scheduled-string
  8264. (if (match-end 3) org-deadline-string "Plain:"))
  8265. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8266. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8267. (setq n (string-to-number (match-string 2 ts))
  8268. what (match-string 3 ts))
  8269. (if (equal what "w") (setq n (* n 7) what "d"))
  8270. ;; Preparation, see if we need to modify the start date for the change
  8271. (when (match-end 1)
  8272. (setq time (save-match-data (org-time-string-to-time ts)))
  8273. (cond
  8274. ((equal (match-string 1 ts) ".")
  8275. ;; Shift starting date to today
  8276. (org-timestamp-change
  8277. (- (time-to-days (current-time)) (time-to-days time))
  8278. 'day))
  8279. ((equal (match-string 1 ts) "+")
  8280. (while (or (= nshift 0)
  8281. (<= (time-to-days time) (time-to-days (current-time))))
  8282. (when (= (incf nshift) nshiftmax)
  8283. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8284. (error "Abort")))
  8285. (org-timestamp-change n (cdr (assoc what whata)))
  8286. (org-at-timestamp-p t)
  8287. (setq ts (match-string 1))
  8288. (setq time (save-match-data (org-time-string-to-time ts))))
  8289. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8290. ;; rematch, so that we have everything in place for the real shift
  8291. (org-at-timestamp-p t)
  8292. (setq ts (match-string 1))
  8293. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8294. (org-timestamp-change n (cdr (assoc what whata)))
  8295. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8296. (setq org-log-post-message msg)
  8297. (message "%s" msg))))
  8298. (defun org-show-todo-tree (arg)
  8299. "Make a compact tree which shows all headlines marked with TODO.
  8300. The tree will show the lines where the regexp matches, and all higher
  8301. headlines above the match.
  8302. With a \\[universal-argument] prefix, also show the DONE entries.
  8303. With a numeric prefix N, construct a sparse tree for the Nth element
  8304. of `org-todo-keywords-1'."
  8305. (interactive "P")
  8306. (let ((case-fold-search nil)
  8307. (kwd-re
  8308. (cond ((null arg) org-not-done-regexp)
  8309. ((equal arg '(4))
  8310. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8311. (mapcar 'list org-todo-keywords-1))))
  8312. (concat "\\("
  8313. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8314. "\\)\\>")))
  8315. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8316. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8317. org-todo-keywords-1)))
  8318. (t (error "Invalid prefix argument: %s" arg)))))
  8319. (message "%d TODO entries found"
  8320. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8321. (defun org-deadline (&optional remove time)
  8322. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8323. With argument REMOVE, remove any deadline from the item.
  8324. When TIME is set, it should be an internal time specification, and the
  8325. scheduling will use the corresponding date."
  8326. (interactive "P")
  8327. (if remove
  8328. (progn
  8329. (org-remove-timestamp-with-keyword org-deadline-string)
  8330. (message "Item no longer has a deadline."))
  8331. (if (org-get-repeat)
  8332. (error "Cannot change deadline on task with repeater, please do that by hand")
  8333. (org-add-planning-info 'deadline time 'closed)
  8334. (message "Deadline on %s" org-last-inserted-timestamp))))
  8335. (defun org-schedule (&optional remove time)
  8336. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8337. With argument REMOVE, remove any scheduling date from the item.
  8338. When TIME is set, it should be an internal time specification, and the
  8339. scheduling will use the corresponding date."
  8340. (interactive "P")
  8341. (if remove
  8342. (progn
  8343. (org-remove-timestamp-with-keyword org-scheduled-string)
  8344. (message "Item is no longer scheduled."))
  8345. (if (org-get-repeat)
  8346. (error "Cannot reschedule task with repeater, please do that by hand")
  8347. (org-add-planning-info 'scheduled time 'closed)
  8348. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8349. (defun org-get-scheduled-time (pom &optional inherit)
  8350. "Get the scheduled time as a time tuple, of a format suitable
  8351. for calling org-schedule with, or if there is no scheduling,
  8352. returns nil."
  8353. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8354. (when time
  8355. (apply 'encode-time (org-parse-time-string time)))))
  8356. (defun org-get-deadline-time (pom &optional inherit)
  8357. "Get the deadine as a time tuple, of a format suitable for
  8358. calling org-deadlin with, or if there is no scheduling, returns
  8359. nil."
  8360. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8361. (when time
  8362. (apply 'encode-time (org-parse-time-string time)))))
  8363. (defun org-remove-timestamp-with-keyword (keyword)
  8364. "Remove all time stamps with KEYWORD in the current entry."
  8365. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8366. beg)
  8367. (save-excursion
  8368. (org-back-to-heading t)
  8369. (setq beg (point))
  8370. (org-end-of-subtree t t)
  8371. (while (re-search-backward re beg t)
  8372. (replace-match "")
  8373. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8374. (equal (char-before) ?\ ))
  8375. (backward-delete-char 1)
  8376. (if (string-match "^[ \t]*$" (buffer-substring
  8377. (point-at-bol) (point-at-eol)))
  8378. (delete-region (point-at-bol)
  8379. (min (point-max) (1+ (point-at-eol))))))))))
  8380. (defun org-add-planning-info (what &optional time &rest remove)
  8381. "Insert new timestamp with keyword in the line directly after the headline.
  8382. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8383. If non is given, the user is prompted for a date.
  8384. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8385. be removed."
  8386. (interactive)
  8387. (let (org-time-was-given org-end-time-was-given ts
  8388. end default-time default-input)
  8389. (catch 'exit
  8390. (when (and (not time) (memq what '(scheduled deadline)))
  8391. ;; Try to get a default date/time from existing timestamp
  8392. (save-excursion
  8393. (org-back-to-heading t)
  8394. (setq end (save-excursion (outline-next-heading) (point)))
  8395. (when (re-search-forward (if (eq what 'scheduled)
  8396. org-scheduled-time-regexp
  8397. org-deadline-time-regexp)
  8398. end t)
  8399. (setq ts (match-string 1)
  8400. default-time
  8401. (apply 'encode-time (org-parse-time-string ts))
  8402. default-input (and ts (org-get-compact-tod ts))))))
  8403. (when what
  8404. ;; If necessary, get the time from the user
  8405. (setq time (or time (org-read-date nil 'to-time nil nil
  8406. default-time default-input))))
  8407. (when (and org-insert-labeled-timestamps-at-point
  8408. (member what '(scheduled deadline)))
  8409. (insert
  8410. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8411. (org-insert-time-stamp time org-time-was-given
  8412. nil nil nil (list org-end-time-was-given))
  8413. (setq what nil))
  8414. (save-excursion
  8415. (save-restriction
  8416. (let (col list elt ts buffer-invisibility-spec)
  8417. (org-back-to-heading t)
  8418. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8419. (goto-char (match-end 1))
  8420. (setq col (current-column))
  8421. (goto-char (match-end 0))
  8422. (if (eobp) (insert "\n") (forward-char 1))
  8423. (when (and (not what)
  8424. (not (looking-at org-keyword-time-not-clock-regexp)))
  8425. ;; Nothing to add, nothing to remove...... :-)
  8426. (throw 'exit nil))
  8427. (if (and (not (looking-at outline-regexp))
  8428. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8429. "[^\r\n]*"))
  8430. (not (equal (match-string 1) org-clock-string)))
  8431. (narrow-to-region (match-beginning 0) (match-end 0))
  8432. (insert-before-markers "\n")
  8433. (backward-char 1)
  8434. (narrow-to-region (point) (point))
  8435. (and org-adapt-indentation (org-indent-to-column col)))
  8436. ;; Check if we have to remove something.
  8437. (setq list (cons what remove))
  8438. (while list
  8439. (setq elt (pop list))
  8440. (goto-char (point-min))
  8441. (when (or (and (eq elt 'scheduled)
  8442. (re-search-forward org-scheduled-time-regexp nil t))
  8443. (and (eq elt 'deadline)
  8444. (re-search-forward org-deadline-time-regexp nil t))
  8445. (and (eq elt 'closed)
  8446. (re-search-forward org-closed-time-regexp nil t)))
  8447. (replace-match "")
  8448. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8449. (if (looking-at " +") (replace-match ""))))
  8450. (goto-char (point-max))
  8451. (when what
  8452. (insert
  8453. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8454. (cond ((eq what 'scheduled) org-scheduled-string)
  8455. ((eq what 'deadline) org-deadline-string)
  8456. ((eq what 'closed) org-closed-string))
  8457. " ")
  8458. (setq ts (org-insert-time-stamp
  8459. time
  8460. (or org-time-was-given
  8461. (and (eq what 'closed) org-log-done-with-time))
  8462. (eq what 'closed)
  8463. nil nil (list org-end-time-was-given)))
  8464. (end-of-line 1))
  8465. (goto-char (point-min))
  8466. (widen)
  8467. (if (and (looking-at "[ \t]+\n")
  8468. (equal (char-before) ?\n))
  8469. (delete-region (1- (point)) (point-at-eol)))
  8470. ts))))))
  8471. (defvar org-log-note-marker (make-marker))
  8472. (defvar org-log-note-purpose nil)
  8473. (defvar org-log-note-state nil)
  8474. (defvar org-log-note-previous-state nil)
  8475. (defvar org-log-note-how nil)
  8476. (defvar org-log-note-extra nil)
  8477. (defvar org-log-note-window-configuration nil)
  8478. (defvar org-log-note-return-to (make-marker))
  8479. (defvar org-log-post-message nil
  8480. "Message to be displayed after a log note has been stored.
  8481. The auto-repeater uses this.")
  8482. (defun org-add-note ()
  8483. "Add a note to the current entry.
  8484. This is done in the same way as adding a state change note."
  8485. (interactive)
  8486. (org-add-log-setup 'note nil nil 'findpos nil))
  8487. (defvar org-property-end-re)
  8488. (defun org-add-log-setup (&optional purpose state prev-state
  8489. findpos how &optional extra)
  8490. "Set up the post command hook to take a note.
  8491. If this is about to TODO state change, the new state is expected in STATE.
  8492. When FINDPOS is non-nil, find the correct position for the note in
  8493. the current entry. If not, assume that it can be inserted at point.
  8494. HOW is an indicator what kind of note should be created.
  8495. EXTRA is additional text that will be inserted into the notes buffer."
  8496. (let ((drawer (cond ((stringp org-log-into-drawer)
  8497. org-log-into-drawer)
  8498. (org-log-into-drawer "LOGBOOK")
  8499. (t nil))))
  8500. (save-restriction
  8501. (save-excursion
  8502. (when findpos
  8503. (org-back-to-heading t)
  8504. (narrow-to-region (point) (save-excursion
  8505. (outline-next-heading) (point)))
  8506. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8507. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8508. "[^\r\n]*\\)?"))
  8509. (goto-char (match-end 0))
  8510. (cond
  8511. (drawer
  8512. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8513. nil t)
  8514. (progn
  8515. (goto-char (match-end 0))
  8516. (or org-log-states-order-reversed
  8517. (and (re-search-forward org-property-end-re nil t)
  8518. (goto-char (1- (match-beginning 0))))))
  8519. (insert "\n:" drawer ":\n:END:")
  8520. (beginning-of-line 0)
  8521. (org-indent-line-function)
  8522. (beginning-of-line 2)
  8523. (org-indent-line-function)
  8524. (end-of-line 0)))
  8525. ((and org-log-state-notes-insert-after-drawers
  8526. (save-excursion
  8527. (forward-line) (looking-at org-drawer-regexp)))
  8528. (forward-line)
  8529. (while (looking-at org-drawer-regexp)
  8530. (goto-char (match-end 0))
  8531. (re-search-forward org-property-end-re (point-max) t)
  8532. (forward-line))
  8533. (forward-line -1)))
  8534. (unless org-log-states-order-reversed
  8535. (and (= (char-after) ?\n) (forward-char 1))
  8536. (org-skip-over-state-notes)
  8537. (skip-chars-backward " \t\n\r")))
  8538. (move-marker org-log-note-marker (point))
  8539. (setq org-log-note-purpose purpose
  8540. org-log-note-state state
  8541. org-log-note-previous-state prev-state
  8542. org-log-note-how how
  8543. org-log-note-extra extra)
  8544. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8545. (defun org-skip-over-state-notes ()
  8546. "Skip past the list of State notes in an entry."
  8547. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8548. (while (looking-at "[ \t]*- State")
  8549. (condition-case nil
  8550. (org-next-item)
  8551. (error (org-end-of-item)))))
  8552. (defun org-add-log-note (&optional purpose)
  8553. "Pop up a window for taking a note, and add this note later at point."
  8554. (remove-hook 'post-command-hook 'org-add-log-note)
  8555. (setq org-log-note-window-configuration (current-window-configuration))
  8556. (delete-other-windows)
  8557. (move-marker org-log-note-return-to (point))
  8558. (switch-to-buffer (marker-buffer org-log-note-marker))
  8559. (goto-char org-log-note-marker)
  8560. (org-switch-to-buffer-other-window "*Org Note*")
  8561. (erase-buffer)
  8562. (if (memq org-log-note-how '(time state))
  8563. (let (current-prefix-arg) (org-store-log-note))
  8564. (let ((org-inhibit-startup t)) (org-mode))
  8565. (insert (format "# Insert note for %s.
  8566. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8567. (cond
  8568. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8569. ((eq org-log-note-purpose 'done) "closed todo item")
  8570. ((eq org-log-note-purpose 'state)
  8571. (format "state change from \"%s\" to \"%s\""
  8572. (or org-log-note-previous-state "")
  8573. (or org-log-note-state "")))
  8574. ((eq org-log-note-purpose 'note)
  8575. "this entry")
  8576. (t (error "This should not happen")))))
  8577. (if org-log-note-extra (insert org-log-note-extra))
  8578. (org-set-local 'org-finish-function 'org-store-log-note)))
  8579. (defvar org-note-abort nil) ; dynamically scoped
  8580. (defun org-store-log-note ()
  8581. "Finish taking a log note, and insert it to where it belongs."
  8582. (let ((txt (buffer-string))
  8583. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8584. lines ind)
  8585. (kill-buffer (current-buffer))
  8586. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8587. (setq txt (replace-match "" t t txt)))
  8588. (if (string-match "\\s-+\\'" txt)
  8589. (setq txt (replace-match "" t t txt)))
  8590. (setq lines (org-split-string txt "\n"))
  8591. (when (and note (string-match "\\S-" note))
  8592. (setq note
  8593. (org-replace-escapes
  8594. note
  8595. (list (cons "%u" (user-login-name))
  8596. (cons "%U" user-full-name)
  8597. (cons "%t" (format-time-string
  8598. (org-time-stamp-format 'long 'inactive)
  8599. (current-time)))
  8600. (cons "%s" (if org-log-note-state
  8601. (concat "\"" org-log-note-state "\"")
  8602. ""))
  8603. (cons "%S" (if org-log-note-previous-state
  8604. (concat "\"" org-log-note-previous-state "\"")
  8605. "\"\"")))))
  8606. (if lines (setq note (concat note " \\\\")))
  8607. (push note lines))
  8608. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8609. (when lines
  8610. (save-excursion
  8611. (set-buffer (marker-buffer org-log-note-marker))
  8612. (save-excursion
  8613. (goto-char org-log-note-marker)
  8614. (move-marker org-log-note-marker nil)
  8615. (end-of-line 1)
  8616. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8617. (insert "- " (pop lines))
  8618. (org-indent-line-function)
  8619. (beginning-of-line 1)
  8620. (looking-at "[ \t]*")
  8621. (setq ind (concat (match-string 0) " "))
  8622. (end-of-line 1)
  8623. (while lines (insert "\n" ind (pop lines)))
  8624. (message "Note stored")
  8625. (org-back-to-heading t)
  8626. (org-cycle-hide-drawers 'children)))))
  8627. (set-window-configuration org-log-note-window-configuration)
  8628. (with-current-buffer (marker-buffer org-log-note-return-to)
  8629. (goto-char org-log-note-return-to))
  8630. (move-marker org-log-note-return-to nil)
  8631. (and org-log-post-message (message "%s" org-log-post-message)))
  8632. (defun org-sparse-tree (&optional arg)
  8633. "Create a sparse tree, prompt for the details.
  8634. This command can create sparse trees. You first need to select the type
  8635. of match used to create the tree:
  8636. t Show entries with a specific TODO keyword.
  8637. m Show entries selected by a tags/property match.
  8638. p Enter a property name and its value (both with completion on existing
  8639. names/values) and show entries with that property.
  8640. r Show entries matching a regular expression
  8641. d Show deadlines due within `org-deadline-warning-days'."
  8642. (interactive "P")
  8643. (let (ans kwd value)
  8644. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date")
  8645. (setq ans (read-char-exclusive))
  8646. (cond
  8647. ((equal ans ?d)
  8648. (call-interactively 'org-check-deadlines))
  8649. ((equal ans ?b)
  8650. (call-interactively 'org-check-before-date))
  8651. ((equal ans ?t)
  8652. (org-show-todo-tree '(4)))
  8653. ((member ans '(?T ?m))
  8654. (call-interactively 'org-match-sparse-tree))
  8655. ((member ans '(?p ?P))
  8656. (setq kwd (org-ido-completing-read "Property: "
  8657. (mapcar 'list (org-buffer-property-keys))))
  8658. (setq value (org-ido-completing-read "Value: "
  8659. (mapcar 'list (org-property-values kwd))))
  8660. (unless (string-match "\\`{.*}\\'" value)
  8661. (setq value (concat "\"" value "\"")))
  8662. (org-match-sparse-tree arg (concat kwd "=" value)))
  8663. ((member ans '(?r ?R ?/))
  8664. (call-interactively 'org-occur))
  8665. (t (error "No such sparse tree command \"%c\"" ans)))))
  8666. (defvar org-occur-highlights nil
  8667. "List of overlays used for occur matches.")
  8668. (make-variable-buffer-local 'org-occur-highlights)
  8669. (defvar org-occur-parameters nil
  8670. "Parameters of the active org-occur calls.
  8671. This is a list, each call to org-occur pushes as cons cell,
  8672. containing the regular expression and the callback, onto the list.
  8673. The list can contain several entries if `org-occur' has been called
  8674. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8675. will only contain one set of parameters. When the highlights are
  8676. removed (for example with `C-c C-c', or with the next edit (depending
  8677. on `org-remove-highlights-with-change'), this variable is emptied
  8678. as well.")
  8679. (make-variable-buffer-local 'org-occur-parameters)
  8680. (defun org-occur (regexp &optional keep-previous callback)
  8681. "Make a compact tree which shows all matches of REGEXP.
  8682. The tree will show the lines where the regexp matches, and all higher
  8683. headlines above the match. It will also show the heading after the match,
  8684. to make sure editing the matching entry is easy.
  8685. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8686. call to `org-occur' will be kept, to allow stacking of calls to this
  8687. command.
  8688. If CALLBACK is non-nil, it is a function which is called to confirm
  8689. that the match should indeed be shown."
  8690. (interactive "sRegexp: \nP")
  8691. (unless keep-previous
  8692. (org-remove-occur-highlights nil nil t))
  8693. (push (cons regexp callback) org-occur-parameters)
  8694. (let ((cnt 0))
  8695. (save-excursion
  8696. (goto-char (point-min))
  8697. (if (or (not keep-previous) ; do not want to keep
  8698. (not org-occur-highlights)) ; no previous matches
  8699. ;; hide everything
  8700. (org-overview))
  8701. (while (re-search-forward regexp nil t)
  8702. (when (or (not callback)
  8703. (save-match-data (funcall callback)))
  8704. (setq cnt (1+ cnt))
  8705. (when org-highlight-sparse-tree-matches
  8706. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8707. (org-show-context 'occur-tree))))
  8708. (when org-remove-highlights-with-change
  8709. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8710. nil 'local))
  8711. (unless org-sparse-tree-open-archived-trees
  8712. (org-hide-archived-subtrees (point-min) (point-max)))
  8713. (run-hooks 'org-occur-hook)
  8714. (if (interactive-p)
  8715. (message "%d match(es) for regexp %s" cnt regexp))
  8716. cnt))
  8717. (defun org-show-context (&optional key)
  8718. "Make sure point and context and visible.
  8719. How much context is shown depends upon the variables
  8720. `org-show-hierarchy-above', `org-show-following-heading'. and
  8721. `org-show-siblings'."
  8722. (let ((heading-p (org-on-heading-p t))
  8723. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8724. (following-p (org-get-alist-option org-show-following-heading key))
  8725. (entry-p (org-get-alist-option org-show-entry-below key))
  8726. (siblings-p (org-get-alist-option org-show-siblings key)))
  8727. (catch 'exit
  8728. ;; Show heading or entry text
  8729. (if (and heading-p (not entry-p))
  8730. (org-flag-heading nil) ; only show the heading
  8731. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8732. (org-show-hidden-entry))) ; show entire entry
  8733. (when following-p
  8734. ;; Show next sibling, or heading below text
  8735. (save-excursion
  8736. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8737. (org-flag-heading nil))))
  8738. (when siblings-p (org-show-siblings))
  8739. (when hierarchy-p
  8740. ;; show all higher headings, possibly with siblings
  8741. (save-excursion
  8742. (while (and (condition-case nil
  8743. (progn (org-up-heading-all 1) t)
  8744. (error nil))
  8745. (not (bobp)))
  8746. (org-flag-heading nil)
  8747. (when siblings-p (org-show-siblings))))))))
  8748. (defun org-reveal (&optional siblings)
  8749. "Show current entry, hierarchy above it, and the following headline.
  8750. This can be used to show a consistent set of context around locations
  8751. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8752. not t for the search context.
  8753. With optional argument SIBLINGS, on each level of the hierarchy all
  8754. siblings are shown. This repairs the tree structure to what it would
  8755. look like when opened with hierarchical calls to `org-cycle'."
  8756. (interactive "P")
  8757. (let ((org-show-hierarchy-above t)
  8758. (org-show-following-heading t)
  8759. (org-show-siblings (if siblings t org-show-siblings)))
  8760. (org-show-context nil)))
  8761. (defun org-highlight-new-match (beg end)
  8762. "Highlight from BEG to END and mark the highlight is an occur headline."
  8763. (let ((ov (org-make-overlay beg end)))
  8764. (org-overlay-put ov 'face 'secondary-selection)
  8765. (push ov org-occur-highlights)))
  8766. (defun org-remove-occur-highlights (&optional beg end noremove)
  8767. "Remove the occur highlights from the buffer.
  8768. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8769. from the `before-change-functions' in the current buffer."
  8770. (interactive)
  8771. (unless org-inhibit-highlight-removal
  8772. (mapc 'org-delete-overlay org-occur-highlights)
  8773. (setq org-occur-highlights nil)
  8774. (setq org-occur-parameters nil)
  8775. (unless noremove
  8776. (remove-hook 'before-change-functions
  8777. 'org-remove-occur-highlights 'local))))
  8778. ;;;; Priorities
  8779. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8780. "Regular expression matching the priority indicator.")
  8781. (defvar org-remove-priority-next-time nil)
  8782. (defun org-priority-up ()
  8783. "Increase the priority of the current item."
  8784. (interactive)
  8785. (org-priority 'up))
  8786. (defun org-priority-down ()
  8787. "Decrease the priority of the current item."
  8788. (interactive)
  8789. (org-priority 'down))
  8790. (defun org-priority (&optional action)
  8791. "Change the priority of an item by ARG.
  8792. ACTION can be `set', `up', `down', or a character."
  8793. (interactive)
  8794. (setq action (or action 'set))
  8795. (let (current new news have remove)
  8796. (save-excursion
  8797. (org-back-to-heading t)
  8798. (if (looking-at org-priority-regexp)
  8799. (setq current (string-to-char (match-string 2))
  8800. have t)
  8801. (setq current org-default-priority))
  8802. (cond
  8803. ((or (eq action 'set)
  8804. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8805. (if (not (eq action 'set))
  8806. (setq new action)
  8807. (message "Priority %c-%c, SPC to remove: "
  8808. org-highest-priority org-lowest-priority)
  8809. (setq new (read-char-exclusive)))
  8810. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8811. (= (upcase org-lowest-priority) org-lowest-priority))
  8812. (setq new (upcase new)))
  8813. (cond ((equal new ?\ ) (setq remove t))
  8814. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8815. (error "Priority must be between `%c' and `%c'"
  8816. org-highest-priority org-lowest-priority))))
  8817. ((eq action 'up)
  8818. (if (and (not have) (eq last-command this-command))
  8819. (setq new org-lowest-priority)
  8820. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8821. org-default-priority (1- current)))))
  8822. ((eq action 'down)
  8823. (if (and (not have) (eq last-command this-command))
  8824. (setq new org-highest-priority)
  8825. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8826. org-default-priority (1+ current)))))
  8827. (t (error "Invalid action")))
  8828. (if (or (< (upcase new) org-highest-priority)
  8829. (> (upcase new) org-lowest-priority))
  8830. (setq remove t))
  8831. (setq news (format "%c" new))
  8832. (if have
  8833. (if remove
  8834. (replace-match "" t t nil 1)
  8835. (replace-match news t t nil 2))
  8836. (if remove
  8837. (error "No priority cookie found in line")
  8838. (looking-at org-todo-line-regexp)
  8839. (if (match-end 2)
  8840. (progn
  8841. (goto-char (match-end 2))
  8842. (insert " [#" news "]"))
  8843. (goto-char (match-beginning 3))
  8844. (insert "[#" news "] ")))))
  8845. (org-preserve-lc (org-set-tags nil 'align))
  8846. (if remove
  8847. (message "Priority removed")
  8848. (message "Priority of current item set to %s" news))))
  8849. (defun org-get-priority (s)
  8850. "Find priority cookie and return priority."
  8851. (save-match-data
  8852. (if (not (string-match org-priority-regexp s))
  8853. (* 1000 (- org-lowest-priority org-default-priority))
  8854. (* 1000 (- org-lowest-priority
  8855. (string-to-char (match-string 2 s)))))))
  8856. ;;;; Tags
  8857. (defvar org-agenda-archives-mode)
  8858. (defun org-scan-tags (action matcher &optional todo-only)
  8859. "Scan headline tags with inheritance and produce output ACTION.
  8860. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8861. or `agenda' to produce an entry list for an agenda view. It can also be
  8862. a Lisp form or a function that should be called at each matched headline, in
  8863. this case the return value is a list of all return values from these calls.
  8864. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8865. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8866. only lines with a TODO keyword are included in the output."
  8867. (require 'org-agenda)
  8868. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8869. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8870. (org-re
  8871. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8872. (props (list 'face 'default
  8873. 'done-face 'org-done
  8874. 'undone-face 'default
  8875. 'mouse-face 'highlight
  8876. 'org-not-done-regexp org-not-done-regexp
  8877. 'org-todo-regexp org-todo-regexp
  8878. 'keymap org-agenda-keymap
  8879. 'help-echo
  8880. (format "mouse-2 or RET jump to org file %s"
  8881. (abbreviate-file-name
  8882. (or (buffer-file-name (buffer-base-buffer))
  8883. (buffer-name (buffer-base-buffer)))))))
  8884. (case-fold-search nil)
  8885. lspos tags tags-list
  8886. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8887. (llast 0) rtn rtn1 level category i txt
  8888. todo marker entry priority)
  8889. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8890. (setq action (list 'lambda nil action)))
  8891. (save-excursion
  8892. (goto-char (point-min))
  8893. (when (eq action 'sparse-tree)
  8894. (org-overview)
  8895. (org-remove-occur-highlights))
  8896. (while (re-search-forward re nil t)
  8897. (catch :skip
  8898. (setq todo (if (match-end 1) (match-string 2))
  8899. tags (if (match-end 4) (match-string 4)))
  8900. (goto-char (setq lspos (1+ (match-beginning 0))))
  8901. (setq level (org-reduced-level (funcall outline-level))
  8902. category (org-get-category))
  8903. (setq i llast llast level)
  8904. ;; remove tag lists from same and sublevels
  8905. (while (>= i level)
  8906. (when (setq entry (assoc i tags-alist))
  8907. (setq tags-alist (delete entry tags-alist)))
  8908. (setq i (1- i)))
  8909. ;; add the next tags
  8910. (when tags
  8911. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8912. tags-alist
  8913. (cons (cons level tags) tags-alist)))
  8914. ;; compile tags for current headline
  8915. (setq tags-list
  8916. (if org-use-tag-inheritance
  8917. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8918. tags))
  8919. (when org-use-tag-inheritance
  8920. (setcdr (car tags-alist)
  8921. (mapcar (lambda (x)
  8922. (setq x (copy-sequence x))
  8923. (org-add-prop-inherited x))
  8924. (cdar tags-alist))))
  8925. (when (and tags org-use-tag-inheritance
  8926. (not (eq t org-use-tag-inheritance)))
  8927. ;; selective inheritance, remove uninherited ones
  8928. (setcdr (car tags-alist)
  8929. (org-remove-uniherited-tags (cdar tags-alist))))
  8930. (when (and (or (not todo-only)
  8931. (and (member todo org-not-done-keywords)
  8932. (or (not org-agenda-tags-todo-honor-ignore-options)
  8933. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8934. (let ((case-fold-search t)) (eval matcher))
  8935. (or
  8936. (not (member org-archive-tag tags-list))
  8937. ;; we have an archive tag, should we use this anyway?
  8938. (or (not org-agenda-skip-archived-trees)
  8939. (and (eq action 'agenda) org-agenda-archives-mode))))
  8940. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8941. ;; select this headline
  8942. (cond
  8943. ((eq action 'sparse-tree)
  8944. (and org-highlight-sparse-tree-matches
  8945. (org-get-heading) (match-end 0)
  8946. (org-highlight-new-match
  8947. (match-beginning 0) (match-beginning 1)))
  8948. (org-show-context 'tags-tree))
  8949. ((eq action 'agenda)
  8950. (setq txt (org-format-agenda-item
  8951. ""
  8952. (concat
  8953. (if org-tags-match-list-sublevels
  8954. (make-string (1- level) ?.) "")
  8955. (org-get-heading))
  8956. category (org-get-tags-at))
  8957. priority (org-get-priority txt))
  8958. (goto-char lspos)
  8959. (setq marker (org-agenda-new-marker))
  8960. (org-add-props txt props
  8961. 'org-marker marker 'org-hd-marker marker 'org-category category
  8962. 'todo-state todo
  8963. 'priority priority 'type "tagsmatch")
  8964. (push txt rtn))
  8965. ((functionp action)
  8966. (save-excursion
  8967. (setq rtn1 (funcall action))
  8968. (push rtn1 rtn))
  8969. (goto-char (point-at-eol)))
  8970. (t (error "Invalid action")))
  8971. ;; if we are to skip sublevels, jump to end of subtree
  8972. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8973. (when (and (eq action 'sparse-tree)
  8974. (not org-sparse-tree-open-archived-trees))
  8975. (org-hide-archived-subtrees (point-min) (point-max)))
  8976. (nreverse rtn)))
  8977. (defun org-remove-uniherited-tags (tags)
  8978. "Remove all tags that are not inherited from the list TAGS."
  8979. (cond
  8980. ((eq org-use-tag-inheritance t)
  8981. (if org-tags-exclude-from-inheritance
  8982. (org-delete-all org-tags-exclude-from-inheritance tags)
  8983. tags))
  8984. ((not org-use-tag-inheritance) nil)
  8985. ((stringp org-use-tag-inheritance)
  8986. (delq nil (mapcar
  8987. (lambda (x)
  8988. (if (and (string-match org-use-tag-inheritance x)
  8989. (not (member x org-tags-exclude-from-inheritance)))
  8990. x nil))
  8991. tags)))
  8992. ((listp org-use-tag-inheritance)
  8993. (delq nil (mapcar
  8994. (lambda (x)
  8995. (if (member x org-use-tag-inheritance) x nil))
  8996. tags)))))
  8997. (defvar todo-only) ;; dynamically scoped
  8998. (defun org-match-sparse-tree (&optional todo-only match)
  8999. "Create a sparse tree according to tags string MATCH.
  9000. MATCH can contain positive and negative selection of tags, like
  9001. \"+WORK+URGENT-WITHBOSS\".
  9002. If optional argument TODO-ONLY is non-nil, only select lines that are
  9003. also TODO lines."
  9004. (interactive "P")
  9005. (org-prepare-agenda-buffers (list (current-buffer)))
  9006. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9007. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9008. (defvar org-cached-props nil)
  9009. (defun org-cached-entry-get (pom property)
  9010. (if (or (eq t org-use-property-inheritance)
  9011. (and (stringp org-use-property-inheritance)
  9012. (string-match org-use-property-inheritance property))
  9013. (and (listp org-use-property-inheritance)
  9014. (member property org-use-property-inheritance)))
  9015. ;; Caching is not possible, check it directly
  9016. (org-entry-get pom property 'inherit)
  9017. ;; Get all properties, so that we can do complicated checks easily
  9018. (cdr (assoc property (or org-cached-props
  9019. (setq org-cached-props
  9020. (org-entry-properties pom)))))))
  9021. (defun org-global-tags-completion-table (&optional files)
  9022. "Return the list of all tags in all agenda buffer/files."
  9023. (save-excursion
  9024. (org-uniquify
  9025. (delq nil
  9026. (apply 'append
  9027. (mapcar
  9028. (lambda (file)
  9029. (set-buffer (find-file-noselect file))
  9030. (append (org-get-buffer-tags)
  9031. (mapcar (lambda (x) (if (stringp (car-safe x))
  9032. (list (car-safe x)) nil))
  9033. org-tag-alist)))
  9034. (if (and files (car files))
  9035. files
  9036. (org-agenda-files))))))))
  9037. (defun org-make-tags-matcher (match)
  9038. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9039. ;; todo-only is scoped dynamically into this function, and the function
  9040. ;; may change it if the matcher asks for it.
  9041. (unless match
  9042. ;; Get a new match request, with completion
  9043. (let ((org-last-tags-completion-table
  9044. (org-global-tags-completion-table)))
  9045. (setq match (org-completing-read-no-ido
  9046. "Match: " 'org-tags-completion-function nil nil nil
  9047. 'org-tags-history))))
  9048. ;; Parse the string and create a lisp form
  9049. (let ((match0 match)
  9050. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9051. minus tag mm
  9052. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9053. orterms term orlist re-p str-p level-p level-op time-p
  9054. prop-p pn pv po cat-p gv rest)
  9055. (if (string-match "/+" match)
  9056. ;; match contains also a todo-matching request
  9057. (progn
  9058. (setq tagsmatch (substring match 0 (match-beginning 0))
  9059. todomatch (substring match (match-end 0)))
  9060. (if (string-match "^!" todomatch)
  9061. (setq todo-only t todomatch (substring todomatch 1)))
  9062. (if (string-match "^\\s-*$" todomatch)
  9063. (setq todomatch nil)))
  9064. ;; only matching tags
  9065. (setq tagsmatch match todomatch nil))
  9066. ;; Make the tags matcher
  9067. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9068. (setq tagsmatcher t)
  9069. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9070. (while (setq term (pop orterms))
  9071. (while (and (equal (substring term -1) "\\") orterms)
  9072. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9073. (while (string-match re term)
  9074. (setq rest (substring term (match-end 0))
  9075. minus (and (match-end 1)
  9076. (equal (match-string 1 term) "-"))
  9077. tag (match-string 2 term)
  9078. re-p (equal (string-to-char tag) ?{)
  9079. level-p (match-end 4)
  9080. prop-p (match-end 5)
  9081. mm (cond
  9082. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9083. (level-p
  9084. (setq level-op (org-op-to-function (match-string 3 term)))
  9085. `(,level-op level ,(string-to-number
  9086. (match-string 4 term))))
  9087. (prop-p
  9088. (setq pn (match-string 5 term)
  9089. po (match-string 6 term)
  9090. pv (match-string 7 term)
  9091. cat-p (equal pn "CATEGORY")
  9092. re-p (equal (string-to-char pv) ?{)
  9093. str-p (equal (string-to-char pv) ?\")
  9094. time-p (save-match-data
  9095. (string-match "^\"[[<].*[]>]\"$" pv))
  9096. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9097. (if time-p (setq pv (org-matcher-time pv)))
  9098. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9099. (cond
  9100. ((equal pn "CATEGORY")
  9101. (setq gv '(get-text-property (point) 'org-category)))
  9102. ((equal pn "TODO")
  9103. (setq gv 'todo))
  9104. (t
  9105. (setq gv `(org-cached-entry-get nil ,pn))))
  9106. (if re-p
  9107. (if (eq po 'org<>)
  9108. `(not (string-match ,pv (or ,gv "")))
  9109. `(string-match ,pv (or ,gv "")))
  9110. (if str-p
  9111. `(,po (or ,gv "") ,pv)
  9112. `(,po (string-to-number (or ,gv ""))
  9113. ,(string-to-number pv) ))))
  9114. (t `(member ,(downcase tag) tags-list)))
  9115. mm (if minus (list 'not mm) mm)
  9116. term rest)
  9117. (push mm tagsmatcher))
  9118. (push (if (> (length tagsmatcher) 1)
  9119. (cons 'and tagsmatcher)
  9120. (car tagsmatcher))
  9121. orlist)
  9122. (setq tagsmatcher nil))
  9123. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9124. (setq tagsmatcher
  9125. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9126. ;; Make the todo matcher
  9127. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9128. (setq todomatcher t)
  9129. (setq orterms (org-split-string todomatch "|") orlist nil)
  9130. (while (setq term (pop orterms))
  9131. (while (string-match re term)
  9132. (setq minus (and (match-end 1)
  9133. (equal (match-string 1 term) "-"))
  9134. kwd (match-string 2 term)
  9135. re-p (equal (string-to-char kwd) ?{)
  9136. term (substring term (match-end 0))
  9137. mm (if re-p
  9138. `(string-match ,(substring kwd 1 -1) todo)
  9139. (list 'equal 'todo kwd))
  9140. mm (if minus (list 'not mm) mm))
  9141. (push mm todomatcher))
  9142. (push (if (> (length todomatcher) 1)
  9143. (cons 'and todomatcher)
  9144. (car todomatcher))
  9145. orlist)
  9146. (setq todomatcher nil))
  9147. (setq todomatcher (if (> (length orlist) 1)
  9148. (cons 'or orlist) (car orlist))))
  9149. ;; Return the string and lisp forms of the matcher
  9150. (setq matcher (if todomatcher
  9151. (list 'and tagsmatcher todomatcher)
  9152. tagsmatcher))
  9153. (cons match0 matcher)))
  9154. (defun org-op-to-function (op &optional stringp)
  9155. "Turn an operator into the appropriate function."
  9156. (setq op
  9157. (cond
  9158. ((equal op "<" ) '(< string< org-time<))
  9159. ((equal op ">" ) '(> org-string> org-time>))
  9160. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9161. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9162. ((member op '("=" "==")) '(= string= org-time=))
  9163. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9164. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9165. (defun org<> (a b) (not (= a b)))
  9166. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9167. (defun org-string>= (a b) (not (string< a b)))
  9168. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9169. (defun org-string<> (a b) (not (string= 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) (> a b)))
  9174. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9175. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9176. (defun org-2ft (s)
  9177. "Convert S to a floating point time.
  9178. If S is already a number, just return it. If it is a string, parse
  9179. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9180. (cond
  9181. ((numberp s) s)
  9182. ((stringp s)
  9183. (condition-case nil
  9184. (float-time (apply 'encode-time (org-parse-time-string s)))
  9185. (error 0.)))
  9186. (t 0.)))
  9187. (defun org-time-today ()
  9188. "Time in seconds today at 0:00.
  9189. Returns the float number of seconds since the beginning of the
  9190. epoch to the beginning of today (00:00)."
  9191. (float-time (apply 'encode-time
  9192. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9193. (defun org-matcher-time (s)
  9194. "Interpret a time comparison value."
  9195. (save-match-data
  9196. (cond
  9197. ((string= s "<now>") (float-time))
  9198. ((string= s "<today>") (org-time-today))
  9199. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9200. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9201. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9202. (+ (org-time-today)
  9203. (* (string-to-number (match-string 1 s))
  9204. (cdr (assoc (match-string 2 s)
  9205. '(("d" . 86400.0) ("w" . 604800.0)
  9206. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9207. (t (org-2ft s)))))
  9208. (defun org-match-any-p (re list)
  9209. "Does re match any element of list?"
  9210. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9211. (delq nil list))
  9212. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9213. (defvar org-tags-overlay (org-make-overlay 1 1))
  9214. (org-detach-overlay org-tags-overlay)
  9215. (defun org-get-local-tags-at (&optional pos)
  9216. "Get a list of tags defined in the current headline."
  9217. (org-get-tags-at pos 'local))
  9218. (defun org-get-local-tags ()
  9219. "Get a list of tags defined in the current headline."
  9220. (org-get-tags-at nil 'local))
  9221. (defun org-get-tags-at (&optional pos local)
  9222. "Get a list of all headline tags applicable at POS.
  9223. POS defaults to point. If tags are inherited, the list contains
  9224. the targets in the same sequence as the headlines appear, i.e.
  9225. the tags of the current headline come last.
  9226. When LOCAL is non-nil, only return tags from the current headline,
  9227. ignore inherited ones."
  9228. (interactive)
  9229. (let (tags ltags lastpos parent)
  9230. (save-excursion
  9231. (save-restriction
  9232. (widen)
  9233. (goto-char (or pos (point)))
  9234. (save-match-data
  9235. (catch 'done
  9236. (condition-case nil
  9237. (progn
  9238. (org-back-to-heading t)
  9239. (while (not (equal lastpos (point)))
  9240. (setq lastpos (point))
  9241. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9242. (setq ltags (org-split-string
  9243. (org-match-string-no-properties 1) ":"))
  9244. (when parent
  9245. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9246. (setq tags (append
  9247. (if parent
  9248. (org-remove-uniherited-tags ltags)
  9249. ltags)
  9250. tags)))
  9251. (or org-use-tag-inheritance (throw 'done t))
  9252. (if local (throw 'done t))
  9253. (org-up-heading-all 1)
  9254. (setq parent t)))
  9255. (error nil)))))
  9256. (append (org-remove-uniherited-tags org-file-tags) tags))))
  9257. (defun org-add-prop-inherited (s)
  9258. (add-text-properties 0 (length s) '(inherited t) s)
  9259. s)
  9260. (defun org-toggle-tag (tag &optional onoff)
  9261. "Toggle the tag TAG for the current line.
  9262. If ONOFF is `on' or `off', don't toggle but set to this state."
  9263. (let (res current)
  9264. (save-excursion
  9265. (org-back-to-heading t)
  9266. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9267. (point-at-eol) t)
  9268. (progn
  9269. (setq current (match-string 1))
  9270. (replace-match ""))
  9271. (setq current ""))
  9272. (setq current (nreverse (org-split-string current ":")))
  9273. (cond
  9274. ((eq onoff 'on)
  9275. (setq res t)
  9276. (or (member tag current) (push tag current)))
  9277. ((eq onoff 'off)
  9278. (or (not (member tag current)) (setq current (delete tag current))))
  9279. (t (if (member tag current)
  9280. (setq current (delete tag current))
  9281. (setq res t)
  9282. (push tag current))))
  9283. (end-of-line 1)
  9284. (if current
  9285. (progn
  9286. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9287. (org-set-tags nil t))
  9288. (delete-horizontal-space))
  9289. (run-hooks 'org-after-tags-change-hook))
  9290. res))
  9291. (defun org-align-tags-here (to-col)
  9292. ;; Assumes that this is a headline
  9293. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9294. (beginning-of-line 1)
  9295. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9296. (< pos (match-beginning 2)))
  9297. (progn
  9298. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9299. (goto-char (match-beginning 1))
  9300. (insert " ")
  9301. (delete-region (point) (1+ (match-beginning 2)))
  9302. (setq ncol (max (1+ (current-column))
  9303. (1+ col)
  9304. (if (> to-col 0)
  9305. to-col
  9306. (- (abs to-col) tags-l))))
  9307. (setq p (point))
  9308. (insert (make-string (- ncol (current-column)) ?\ ))
  9309. (setq ncol (current-column))
  9310. (when indent-tabs-mode (tabify p (point-at-eol)))
  9311. (org-move-to-column (min ncol col) t))
  9312. (goto-char pos))))
  9313. (defun org-set-tags-command (&optional arg just-align)
  9314. "Call the set-tags command for the current entry."
  9315. (interactive "P")
  9316. (if (org-on-heading-p)
  9317. (org-set-tags arg just-align)
  9318. (save-excursion
  9319. (org-back-to-heading t)
  9320. (org-set-tags arg just-align))))
  9321. (defun org-set-tags (&optional arg just-align)
  9322. "Set the tags for the current headline.
  9323. With prefix ARG, realign all tags in headings in the current buffer."
  9324. (interactive "P")
  9325. (let* ((re (concat "^" outline-regexp))
  9326. (current (org-get-tags-string))
  9327. (col (current-column))
  9328. (org-setting-tags t)
  9329. table current-tags inherited-tags ; computed below when needed
  9330. tags p0 c0 c1 rpl)
  9331. (if arg
  9332. (save-excursion
  9333. (goto-char (point-min))
  9334. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9335. (while (re-search-forward re nil t)
  9336. (org-set-tags nil t)
  9337. (end-of-line 1)))
  9338. (message "All tags realigned to column %d" org-tags-column))
  9339. (if just-align
  9340. (setq tags current)
  9341. ;; Get a new set of tags from the user
  9342. (save-excursion
  9343. (setq table (append org-tag-persistent-alist
  9344. (or org-tag-alist (org-get-buffer-tags)))
  9345. org-last-tags-completion-table table
  9346. current-tags (org-split-string current ":")
  9347. inherited-tags (nreverse
  9348. (nthcdr (length current-tags)
  9349. (nreverse (org-get-tags-at))))
  9350. tags
  9351. (if (or (eq t org-use-fast-tag-selection)
  9352. (and org-use-fast-tag-selection
  9353. (delq nil (mapcar 'cdr table))))
  9354. (org-fast-tag-selection
  9355. current-tags inherited-tags table
  9356. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9357. (let ((org-add-colon-after-tag-completion t))
  9358. (org-trim
  9359. (org-without-partial-completion
  9360. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9361. nil nil current 'org-tags-history)))))))
  9362. (while (string-match "[-+&]+" tags)
  9363. ;; No boolean logic, just a list
  9364. (setq tags (replace-match ":" t t tags))))
  9365. (if (string-match "\\`[\t ]*\\'" tags)
  9366. (setq tags "")
  9367. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9368. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9369. ;; Insert new tags at the correct column
  9370. (beginning-of-line 1)
  9371. (cond
  9372. ((and (equal current "") (equal tags "")))
  9373. ((re-search-forward
  9374. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9375. (point-at-eol) t)
  9376. (if (equal tags "")
  9377. (setq rpl "")
  9378. (goto-char (match-beginning 0))
  9379. (setq c0 (current-column) p0 (point)
  9380. c1 (max (1+ c0) (if (> org-tags-column 0)
  9381. org-tags-column
  9382. (- (- org-tags-column) (length tags))))
  9383. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9384. (replace-match rpl t t)
  9385. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9386. tags)
  9387. (t (error "Tags alignment failed")))
  9388. (org-move-to-column col)
  9389. (unless just-align
  9390. (run-hooks 'org-after-tags-change-hook)))))
  9391. (defun org-change-tag-in-region (beg end tag off)
  9392. "Add or remove TAG for each entry in the region.
  9393. This works in the agenda, and also in an org-mode buffer."
  9394. (interactive
  9395. (list (region-beginning) (region-end)
  9396. (let ((org-last-tags-completion-table
  9397. (if (org-mode-p)
  9398. (org-get-buffer-tags)
  9399. (org-global-tags-completion-table))))
  9400. (org-ido-completing-read
  9401. "Tag: " 'org-tags-completion-function nil nil nil
  9402. 'org-tags-history))
  9403. (progn
  9404. (message "[s]et or [r]emove? ")
  9405. (equal (read-char-exclusive) ?r))))
  9406. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9407. (let ((agendap (equal major-mode 'org-agenda-mode))
  9408. l1 l2 m buf pos newhead (cnt 0))
  9409. (goto-char end)
  9410. (setq l2 (1- (org-current-line)))
  9411. (goto-char beg)
  9412. (setq l1 (org-current-line))
  9413. (loop for l from l1 to l2 do
  9414. (goto-line l)
  9415. (setq m (get-text-property (point) 'org-hd-marker))
  9416. (when (or (and (org-mode-p) (org-on-heading-p))
  9417. (and agendap m))
  9418. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9419. pos (if agendap m (point)))
  9420. (with-current-buffer buf
  9421. (save-excursion
  9422. (save-restriction
  9423. (goto-char pos)
  9424. (setq cnt (1+ cnt))
  9425. (org-toggle-tag tag (if off 'off 'on))
  9426. (setq newhead (org-get-heading)))))
  9427. (and agendap (org-agenda-change-all-lines newhead m))))
  9428. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9429. (defun org-tags-completion-function (string predicate &optional flag)
  9430. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9431. (confirm (lambda (x) (stringp (car x)))))
  9432. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9433. (setq s1 (match-string 1 string)
  9434. s2 (match-string 2 string))
  9435. (setq s1 "" s2 string))
  9436. (cond
  9437. ((eq flag nil)
  9438. ;; try completion
  9439. (setq rtn (try-completion s2 ctable confirm))
  9440. (if (stringp rtn)
  9441. (setq rtn
  9442. (concat s1 s2 (substring rtn (length s2))
  9443. (if (and org-add-colon-after-tag-completion
  9444. (assoc rtn ctable))
  9445. ":" ""))))
  9446. rtn)
  9447. ((eq flag t)
  9448. ;; all-completions
  9449. (all-completions s2 ctable confirm)
  9450. )
  9451. ((eq flag 'lambda)
  9452. ;; exact match?
  9453. (assoc s2 ctable)))
  9454. ))
  9455. (defun org-fast-tag-insert (kwd tags face &optional end)
  9456. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9457. (insert (format "%-12s" (concat kwd ":"))
  9458. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9459. (or end "")))
  9460. (defun org-fast-tag-show-exit (flag)
  9461. (save-excursion
  9462. (goto-line 3)
  9463. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9464. (replace-match ""))
  9465. (when flag
  9466. (end-of-line 1)
  9467. (org-move-to-column (- (window-width) 19) t)
  9468. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9469. (defun org-set-current-tags-overlay (current prefix)
  9470. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9471. (if (featurep 'xemacs)
  9472. (org-overlay-display org-tags-overlay (concat prefix s)
  9473. 'secondary-selection)
  9474. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9475. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9476. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9477. "Fast tag selection with single keys.
  9478. CURRENT is the current list of tags in the headline, INHERITED is the
  9479. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9480. possibly with grouping information. TODO-TABLE is a similar table with
  9481. TODO keywords, should these have keys assigned to them.
  9482. If the keys are nil, a-z are automatically assigned.
  9483. Returns the new tags string, or nil to not change the current settings."
  9484. (let* ((fulltable (append table todo-table))
  9485. (maxlen (apply 'max (mapcar
  9486. (lambda (x)
  9487. (if (stringp (car x)) (string-width (car x)) 0))
  9488. fulltable)))
  9489. (buf (current-buffer))
  9490. (expert (eq org-fast-tag-selection-single-key 'expert))
  9491. (buffer-tags nil)
  9492. (fwidth (+ maxlen 3 1 3))
  9493. (ncol (/ (- (window-width) 4) fwidth))
  9494. (i-face 'org-done)
  9495. (c-face 'org-todo)
  9496. tg cnt e c char c1 c2 ntable tbl rtn
  9497. ov-start ov-end ov-prefix
  9498. (exit-after-next org-fast-tag-selection-single-key)
  9499. (done-keywords org-done-keywords)
  9500. groups ingroup)
  9501. (save-excursion
  9502. (beginning-of-line 1)
  9503. (if (looking-at
  9504. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9505. (setq ov-start (match-beginning 1)
  9506. ov-end (match-end 1)
  9507. ov-prefix "")
  9508. (setq ov-start (1- (point-at-eol))
  9509. ov-end (1+ ov-start))
  9510. (skip-chars-forward "^\n\r")
  9511. (setq ov-prefix
  9512. (concat
  9513. (buffer-substring (1- (point)) (point))
  9514. (if (> (current-column) org-tags-column)
  9515. " "
  9516. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9517. (org-move-overlay org-tags-overlay ov-start ov-end)
  9518. (save-window-excursion
  9519. (if expert
  9520. (set-buffer (get-buffer-create " *Org tags*"))
  9521. (delete-other-windows)
  9522. (split-window-vertically)
  9523. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9524. (erase-buffer)
  9525. (org-set-local 'org-done-keywords done-keywords)
  9526. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9527. (org-fast-tag-insert "Current" current c-face "\n\n")
  9528. (org-fast-tag-show-exit exit-after-next)
  9529. (org-set-current-tags-overlay current ov-prefix)
  9530. (setq tbl fulltable char ?a cnt 0)
  9531. (while (setq e (pop tbl))
  9532. (cond
  9533. ((equal e '(:startgroup))
  9534. (push '() groups) (setq ingroup t)
  9535. (when (not (= cnt 0))
  9536. (setq cnt 0)
  9537. (insert "\n"))
  9538. (insert "{ "))
  9539. ((equal e '(:endgroup))
  9540. (setq ingroup nil cnt 0)
  9541. (insert "}\n"))
  9542. ((equal e '(:newline))
  9543. (when (not (= cnt 0))
  9544. (setq cnt 0)
  9545. (insert "\n")
  9546. (setq e (car tbl))
  9547. (while (equal (car tbl) '(:newline))
  9548. (insert "\n")
  9549. (setq tbl (cdr tbl)))))
  9550. (t
  9551. (setq tg (car e) c2 nil)
  9552. (if (cdr e)
  9553. (setq c (cdr e))
  9554. ;; automatically assign a character.
  9555. (setq c1 (string-to-char
  9556. (downcase (substring
  9557. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9558. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9559. (while (or (rassoc char ntable) (rassoc char table))
  9560. (setq char (1+ char)))
  9561. (setq c2 c1))
  9562. (setq c (or c2 char)))
  9563. (if ingroup (push tg (car groups)))
  9564. (setq tg (org-add-props tg nil 'face
  9565. (cond
  9566. ((not (assoc tg table))
  9567. (org-get-todo-face tg))
  9568. ((member tg current) c-face)
  9569. ((member tg inherited) i-face)
  9570. (t nil))))
  9571. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9572. (insert "[" c "] " tg (make-string
  9573. (- fwidth 4 (length tg)) ?\ ))
  9574. (push (cons tg c) ntable)
  9575. (when (= (setq cnt (1+ cnt)) ncol)
  9576. (insert "\n")
  9577. (if ingroup (insert " "))
  9578. (setq cnt 0)))))
  9579. (setq ntable (nreverse ntable))
  9580. (insert "\n")
  9581. (goto-char (point-min))
  9582. (if (not expert) (org-fit-window-to-buffer))
  9583. (setq rtn
  9584. (catch 'exit
  9585. (while t
  9586. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9587. (if groups " [!] no groups" " [!]groups")
  9588. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9589. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9590. (cond
  9591. ((= c ?\r) (throw 'exit t))
  9592. ((= c ?!)
  9593. (setq groups (not groups))
  9594. (goto-char (point-min))
  9595. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9596. ((= c ?\C-c)
  9597. (if (not expert)
  9598. (org-fast-tag-show-exit
  9599. (setq exit-after-next (not exit-after-next)))
  9600. (setq expert nil)
  9601. (delete-other-windows)
  9602. (split-window-vertically)
  9603. (org-switch-to-buffer-other-window " *Org tags*")
  9604. (org-fit-window-to-buffer)))
  9605. ((or (= c ?\C-g)
  9606. (and (= c ?q) (not (rassoc c ntable))))
  9607. (org-detach-overlay org-tags-overlay)
  9608. (setq quit-flag t))
  9609. ((= c ?\ )
  9610. (setq current nil)
  9611. (if exit-after-next (setq exit-after-next 'now)))
  9612. ((= c ?\t)
  9613. (condition-case nil
  9614. (setq tg (org-ido-completing-read
  9615. "Tag: "
  9616. (or buffer-tags
  9617. (with-current-buffer buf
  9618. (org-get-buffer-tags)))))
  9619. (quit (setq tg "")))
  9620. (when (string-match "\\S-" tg)
  9621. (add-to-list 'buffer-tags (list tg))
  9622. (if (member tg current)
  9623. (setq current (delete tg current))
  9624. (push tg current)))
  9625. (if exit-after-next (setq exit-after-next 'now)))
  9626. ((setq e (rassoc c todo-table) tg (car e))
  9627. (with-current-buffer buf
  9628. (save-excursion (org-todo tg)))
  9629. (if exit-after-next (setq exit-after-next 'now)))
  9630. ((setq e (rassoc c ntable) tg (car e))
  9631. (if (member tg current)
  9632. (setq current (delete tg current))
  9633. (loop for g in groups do
  9634. (if (member tg g)
  9635. (mapc (lambda (x)
  9636. (setq current (delete x current)))
  9637. g)))
  9638. (push tg current))
  9639. (if exit-after-next (setq exit-after-next 'now))))
  9640. ;; Create a sorted list
  9641. (setq current
  9642. (sort current
  9643. (lambda (a b)
  9644. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9645. (if (eq exit-after-next 'now) (throw 'exit t))
  9646. (goto-char (point-min))
  9647. (beginning-of-line 2)
  9648. (delete-region (point) (point-at-eol))
  9649. (org-fast-tag-insert "Current" current c-face)
  9650. (org-set-current-tags-overlay current ov-prefix)
  9651. (while (re-search-forward
  9652. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9653. (setq tg (match-string 1))
  9654. (add-text-properties
  9655. (match-beginning 1) (match-end 1)
  9656. (list 'face
  9657. (cond
  9658. ((member tg current) c-face)
  9659. ((member tg inherited) i-face)
  9660. (t (get-text-property (match-beginning 1) 'face))))))
  9661. (goto-char (point-min)))))
  9662. (org-detach-overlay org-tags-overlay)
  9663. (if rtn
  9664. (mapconcat 'identity current ":")
  9665. nil))))
  9666. (defun org-get-tags-string ()
  9667. "Get the TAGS string in the current headline."
  9668. (unless (org-on-heading-p t)
  9669. (error "Not on a heading"))
  9670. (save-excursion
  9671. (beginning-of-line 1)
  9672. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9673. (org-match-string-no-properties 1)
  9674. "")))
  9675. (defun org-get-tags ()
  9676. "Get the list of tags specified in the current headline."
  9677. (org-split-string (org-get-tags-string) ":"))
  9678. (defun org-get-buffer-tags ()
  9679. "Get a table of all tags used in the buffer, for completion."
  9680. (let (tags)
  9681. (save-excursion
  9682. (goto-char (point-min))
  9683. (while (re-search-forward
  9684. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9685. (when (equal (char-after (point-at-bol 0)) ?*)
  9686. (mapc (lambda (x) (add-to-list 'tags x))
  9687. (org-split-string (org-match-string-no-properties 1) ":")))))
  9688. (mapcar 'list tags)))
  9689. ;;;; The mapping API
  9690. ;;;###autoload
  9691. (defun org-map-entries (func &optional match scope &rest skip)
  9692. "Call FUNC at each headline selected by MATCH in SCOPE.
  9693. FUNC is a function or a lisp form. The function will be called without
  9694. arguments, with the cursor positioned at the beginning of the headline.
  9695. The return values of all calls to the function will be collected and
  9696. returned as a list.
  9697. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9698. Only headlines that are matched by this query will be considered during
  9699. the iteration. When MATCH is nil or t, all headlines will be
  9700. visited by the iteration.
  9701. SCOPE determines the scope of this command. It can be any of:
  9702. nil The current buffer, respecting the restriction if any
  9703. tree The subtree started with the entry at point
  9704. file The current buffer, without restriction
  9705. file-with-archives
  9706. The current buffer, and any archives associated with it
  9707. agenda All agenda files
  9708. agenda-with-archives
  9709. All agenda files with any archive files associated with them
  9710. \(file1 file2 ...)
  9711. If this is a list, all files in the list will be scanned
  9712. The remaining args are treated as settings for the skipping facilities of
  9713. the scanner. The following items can be given here:
  9714. archive skip trees with the archive tag.
  9715. comment skip trees with the COMMENT keyword
  9716. function or Emacs Lisp form:
  9717. will be used as value for `org-agenda-skip-function', so whenever
  9718. the the function returns t, FUNC will not be called for that
  9719. entry and search will continue from the point where the
  9720. function leaves it."
  9721. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9722. (org-agenda-skip-archived-trees (memq 'archive skip))
  9723. (org-agenda-skip-comment-trees (memq 'comment skip))
  9724. (org-agenda-skip-function
  9725. (car (org-delete-all '(comment archive) skip)))
  9726. (org-tags-match-list-sublevels t)
  9727. matcher file res
  9728. org-todo-keywords-for-agenda
  9729. org-done-keywords-for-agenda
  9730. org-todo-keyword-alist-for-agenda
  9731. org-tag-alist-for-agenda)
  9732. (cond
  9733. ((eq match t) (setq matcher t))
  9734. ((eq match nil) (setq matcher t))
  9735. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9736. (save-excursion
  9737. (save-restriction
  9738. (when (eq scope 'tree)
  9739. (org-back-to-heading t)
  9740. (org-narrow-to-subtree)
  9741. (setq scope nil))
  9742. (if (not scope)
  9743. (progn
  9744. (org-prepare-agenda-buffers
  9745. (list (buffer-file-name (current-buffer))))
  9746. (setq res (org-scan-tags func matcher)))
  9747. ;; Get the right scope
  9748. (cond
  9749. ((and scope (listp scope) (symbolp (car scope)))
  9750. (setq scope (eval scope)))
  9751. ((eq scope 'agenda)
  9752. (setq scope (org-agenda-files t)))
  9753. ((eq scope 'agenda-with-archives)
  9754. (setq scope (org-agenda-files t))
  9755. (setq scope (org-add-archive-files scope)))
  9756. ((eq scope 'file)
  9757. (setq scope (list (buffer-file-name))))
  9758. ((eq scope 'file-with-archives)
  9759. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9760. (org-prepare-agenda-buffers scope)
  9761. (while (setq file (pop scope))
  9762. (with-current-buffer (org-find-base-buffer-visiting file)
  9763. (save-excursion
  9764. (save-restriction
  9765. (widen)
  9766. (goto-char (point-min))
  9767. (setq res (append res (org-scan-tags func matcher))))))))))
  9768. res))
  9769. ;;;; Properties
  9770. ;;; Setting and retrieving properties
  9771. (defconst org-special-properties
  9772. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9773. "TIMESTAMP" "TIMESTAMP_IA")
  9774. "The special properties valid in Org-mode.
  9775. These are properties that are not defined in the property drawer,
  9776. but in some other way.")
  9777. (defconst org-default-properties
  9778. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9779. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9780. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9781. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9782. "Some properties that are used by Org-mode for various purposes.
  9783. Being in this list makes sure that they are offered for completion.")
  9784. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9785. "Regular expression matching the first line of a property drawer.")
  9786. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9787. "Regular expression matching the first line of a property drawer.")
  9788. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9789. "Regular expression matching the first line of a property drawer.")
  9790. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9791. "Regular expression matching the first line of a property drawer.")
  9792. (defconst org-property-drawer-re
  9793. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9794. org-property-end-re "\\)\n?")
  9795. "Matches an entire property drawer.")
  9796. (defconst org-clock-drawer-re
  9797. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9798. org-property-end-re "\\)\n?")
  9799. "Matches an entire clock drawer.")
  9800. (defun org-property-action ()
  9801. "Do an action on properties."
  9802. (interactive)
  9803. (let (c)
  9804. (org-at-property-p)
  9805. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9806. (setq c (read-char-exclusive))
  9807. (cond
  9808. ((equal c ?s)
  9809. (call-interactively 'org-set-property))
  9810. ((equal c ?d)
  9811. (call-interactively 'org-delete-property))
  9812. ((equal c ?D)
  9813. (call-interactively 'org-delete-property-globally))
  9814. ((equal c ?c)
  9815. (call-interactively 'org-compute-property-at-point))
  9816. (t (error "No such property action %c" c)))))
  9817. (defun org-at-property-p ()
  9818. "Is the cursor in a property line?"
  9819. ;; FIXME: Does not check if we are actually in the drawer.
  9820. ;; FIXME: also returns true on any drawers.....
  9821. ;; This is used by C-c C-c for property action.
  9822. (save-excursion
  9823. (beginning-of-line 1)
  9824. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9825. (defun org-get-property-block (&optional beg end force)
  9826. "Return the (beg . end) range of the body of the property drawer.
  9827. BEG and END can be beginning and end of subtree, if not given
  9828. they will be found.
  9829. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9830. (catch 'exit
  9831. (save-excursion
  9832. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9833. (end (or end (progn (outline-next-heading) (point)))))
  9834. (goto-char beg)
  9835. (if (re-search-forward org-property-start-re end t)
  9836. (setq beg (1+ (match-end 0)))
  9837. (if force
  9838. (save-excursion
  9839. (org-insert-property-drawer)
  9840. (setq end (progn (outline-next-heading) (point))))
  9841. (throw 'exit nil))
  9842. (goto-char beg)
  9843. (if (re-search-forward org-property-start-re end t)
  9844. (setq beg (1+ (match-end 0)))))
  9845. (if (re-search-forward org-property-end-re end t)
  9846. (setq end (match-beginning 0))
  9847. (or force (throw 'exit nil))
  9848. (goto-char beg)
  9849. (setq end beg)
  9850. (org-indent-line-function)
  9851. (insert ":END:\n"))
  9852. (cons beg end)))))
  9853. (defun org-entry-properties (&optional pom which)
  9854. "Get all properties of the entry at point-or-marker POM.
  9855. This includes the TODO keyword, the tags, time strings for deadline,
  9856. scheduled, and clocking, and any additional properties defined in the
  9857. entry. The return value is an alist, keys may occur multiple times
  9858. if the property key was used several times.
  9859. POM may also be nil, in which case the current entry is used.
  9860. If WHICH is nil or `all', get all properties. If WHICH is
  9861. `special' or `standard', only get that subclass."
  9862. (setq which (or which 'all))
  9863. (org-with-point-at pom
  9864. (let ((clockstr (substring org-clock-string 0 -1))
  9865. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9866. beg end range props sum-props key value string clocksum)
  9867. (save-excursion
  9868. (when (condition-case nil
  9869. (and (org-mode-p) (org-back-to-heading t))
  9870. (error nil))
  9871. (setq beg (point))
  9872. (setq sum-props (get-text-property (point) 'org-summaries))
  9873. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9874. (outline-next-heading)
  9875. (setq end (point))
  9876. (when (memq which '(all special))
  9877. ;; Get the special properties, like TODO and tags
  9878. (goto-char beg)
  9879. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9880. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9881. (when (looking-at org-priority-regexp)
  9882. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9883. (when (and (setq value (org-get-tags-string))
  9884. (string-match "\\S-" value))
  9885. (push (cons "TAGS" value) props))
  9886. (when (setq value (org-get-tags-at))
  9887. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9888. props))
  9889. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9890. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9891. string (if (equal key clockstr)
  9892. (org-no-properties
  9893. (org-trim
  9894. (buffer-substring
  9895. (match-beginning 3) (goto-char (point-at-eol)))))
  9896. (substring (org-match-string-no-properties 3) 1 -1)))
  9897. (unless key
  9898. (if (= (char-after (match-beginning 3)) ?\[)
  9899. (setq key "TIMESTAMP_IA")
  9900. (setq key "TIMESTAMP")))
  9901. (when (or (equal key clockstr) (not (assoc key props)))
  9902. (push (cons key string) props)))
  9903. )
  9904. (when (memq which '(all standard))
  9905. ;; Get the standard properties, like :PORP: ...
  9906. (setq range (org-get-property-block beg end))
  9907. (when range
  9908. (goto-char (car range))
  9909. (while (re-search-forward
  9910. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9911. (cdr range) t)
  9912. (setq key (org-match-string-no-properties 1)
  9913. value (org-trim (or (org-match-string-no-properties 2) "")))
  9914. (unless (member key excluded)
  9915. (push (cons key (or value "")) props)))))
  9916. (if clocksum
  9917. (push (cons "CLOCKSUM"
  9918. (org-columns-number-to-string (/ (float clocksum) 60.)
  9919. 'add_times))
  9920. props))
  9921. (unless (assoc "CATEGORY" props)
  9922. (setq value (or (org-get-category)
  9923. (progn (org-refresh-category-properties)
  9924. (org-get-category))))
  9925. (push (cons "CATEGORY" value) props))
  9926. (append sum-props (nreverse props)))))))
  9927. (defun org-entry-get (pom property &optional inherit)
  9928. "Get value of PROPERTY for entry at point-or-marker POM.
  9929. If INHERIT is non-nil and the entry does not have the property,
  9930. then also check higher levels of the hierarchy.
  9931. If INHERIT is the symbol `selective', use inheritance only if the setting
  9932. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9933. If the property is present but empty, the return value is the empty string.
  9934. If the property is not present at all, nil is returned."
  9935. (org-with-point-at pom
  9936. (if (and inherit (if (eq inherit 'selective)
  9937. (org-property-inherit-p property)
  9938. t))
  9939. (org-entry-get-with-inheritance property)
  9940. (if (member property org-special-properties)
  9941. ;; We need a special property. Use brute force, get all properties.
  9942. (cdr (assoc property (org-entry-properties nil 'special)))
  9943. (let ((range (org-get-property-block)))
  9944. (if (and range
  9945. (goto-char (car range))
  9946. (re-search-forward
  9947. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9948. (cdr range) t))
  9949. ;; Found the property, return it.
  9950. (if (match-end 1)
  9951. (org-match-string-no-properties 1)
  9952. "")))))))
  9953. (defun org-property-or-variable-value (var &optional inherit)
  9954. "Check if there is a property fixing the value of VAR.
  9955. If yes, return this value. If not, return the current value of the variable."
  9956. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9957. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9958. (read prop)
  9959. (symbol-value var))))
  9960. (defun org-entry-delete (pom property)
  9961. "Delete the property PROPERTY from entry at point-or-marker POM."
  9962. (org-with-point-at pom
  9963. (if (member property org-special-properties)
  9964. nil ; cannot delete these properties.
  9965. (let ((range (org-get-property-block)))
  9966. (if (and range
  9967. (goto-char (car range))
  9968. (re-search-forward
  9969. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9970. (cdr range) t))
  9971. (progn
  9972. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9973. t)
  9974. nil)))))
  9975. ;; Multi-values properties are properties that contain multiple values
  9976. ;; These values are assumed to be single words, separated by whitespace.
  9977. (defun org-entry-add-to-multivalued-property (pom property value)
  9978. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9979. (let* ((old (org-entry-get pom property))
  9980. (values (and old (org-split-string old "[ \t]"))))
  9981. (setq value (org-entry-protect-space value))
  9982. (unless (member value values)
  9983. (setq values (cons value values))
  9984. (org-entry-put pom property
  9985. (mapconcat 'identity values " ")))))
  9986. (defun org-entry-remove-from-multivalued-property (pom property value)
  9987. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9988. (let* ((old (org-entry-get pom property))
  9989. (values (and old (org-split-string old "[ \t]"))))
  9990. (setq value (org-entry-protect-space value))
  9991. (when (member value values)
  9992. (setq values (delete value values))
  9993. (org-entry-put pom property
  9994. (mapconcat 'identity values " ")))))
  9995. (defun org-entry-member-in-multivalued-property (pom property value)
  9996. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9997. (let* ((old (org-entry-get pom property))
  9998. (values (and old (org-split-string old "[ \t]"))))
  9999. (setq value (org-entry-protect-space value))
  10000. (member value values)))
  10001. (defun org-entry-get-multivalued-property (pom property)
  10002. "Return a list of values in a multivalued property."
  10003. (let* ((value (org-entry-get pom property))
  10004. (values (and value (org-split-string value "[ \t]"))))
  10005. (mapcar 'org-entry-restore-space values)))
  10006. (defun org-entry-put-multivalued-property (pom property &rest values)
  10007. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10008. VALUES should be a list of strings. Spaces will be protected."
  10009. (org-entry-put pom property
  10010. (mapconcat 'org-entry-protect-space values " "))
  10011. (let* ((value (org-entry-get pom property))
  10012. (values (and value (org-split-string value "[ \t]"))))
  10013. (mapcar 'org-entry-restore-space values)))
  10014. (defun org-entry-protect-space (s)
  10015. "Protect spaces and newline in string S."
  10016. (while (string-match " " s)
  10017. (setq s (replace-match "%20" t t s)))
  10018. (while (string-match "\n" s)
  10019. (setq s (replace-match "%0A" t t s)))
  10020. s)
  10021. (defun org-entry-restore-space (s)
  10022. "Restore spaces and newline in string S."
  10023. (while (string-match "%20" s)
  10024. (setq s (replace-match " " t t s)))
  10025. (while (string-match "%0A" s)
  10026. (setq s (replace-match "\n" t t s)))
  10027. s)
  10028. (defvar org-entry-property-inherited-from (make-marker)
  10029. "Marker pointing to the entry from where a property was inherited.
  10030. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10031. location of the entry where the inheritance search matched. If there was
  10032. no match, the marker will point nowhere.
  10033. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10034. is set.")
  10035. (defun org-entry-get-with-inheritance (property)
  10036. "Get entry property, and search higher levels if not present."
  10037. (move-marker org-entry-property-inherited-from nil)
  10038. (let (tmp)
  10039. (save-excursion
  10040. (save-restriction
  10041. (widen)
  10042. (catch 'ex
  10043. (while t
  10044. (when (setq tmp (org-entry-get nil property))
  10045. (org-back-to-heading t)
  10046. (move-marker org-entry-property-inherited-from (point))
  10047. (throw 'ex tmp))
  10048. (or (org-up-heading-safe) (throw 'ex nil)))))
  10049. (or tmp
  10050. (cdr (assoc property org-file-properties))
  10051. (cdr (assoc property org-global-properties))
  10052. (cdr (assoc property org-global-properties-fixed))))))
  10053. (defun org-entry-put (pom property value)
  10054. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10055. (org-with-point-at pom
  10056. (org-back-to-heading t)
  10057. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10058. range)
  10059. (cond
  10060. ((equal property "TODO")
  10061. (when (and (stringp value) (string-match "\\S-" value)
  10062. (not (member value org-todo-keywords-1)))
  10063. (error "\"%s\" is not a valid TODO state" value))
  10064. (if (or (not value)
  10065. (not (string-match "\\S-" value)))
  10066. (setq value 'none))
  10067. (org-todo value)
  10068. (org-set-tags nil 'align))
  10069. ((equal property "PRIORITY")
  10070. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10071. (string-to-char value) ?\ ))
  10072. (org-set-tags nil 'align))
  10073. ((equal property "SCHEDULED")
  10074. (if (re-search-forward org-scheduled-time-regexp end t)
  10075. (cond
  10076. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10077. ((eq value 'later) (org-timestamp-change 1 'day))
  10078. (t (call-interactively 'org-schedule)))
  10079. (call-interactively 'org-schedule)))
  10080. ((equal property "DEADLINE")
  10081. (if (re-search-forward org-deadline-time-regexp end t)
  10082. (cond
  10083. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10084. ((eq value 'later) (org-timestamp-change 1 'day))
  10085. (t (call-interactively 'org-deadline)))
  10086. (call-interactively 'org-deadline)))
  10087. ((member property org-special-properties)
  10088. (error "The %s property can not yet be set with `org-entry-put'"
  10089. property))
  10090. (t ; a non-special property
  10091. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10092. (setq range (org-get-property-block beg end 'force))
  10093. (goto-char (car range))
  10094. (if (re-search-forward
  10095. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10096. (progn
  10097. (delete-region (match-beginning 1) (match-end 1))
  10098. (goto-char (match-beginning 1)))
  10099. (goto-char (cdr range))
  10100. (insert "\n")
  10101. (backward-char 1)
  10102. (org-indent-line-function)
  10103. (insert ":" property ":"))
  10104. (and value (insert " " value))
  10105. (org-indent-line-function)))))))
  10106. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10107. "Get all property keys in the current buffer.
  10108. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10109. like tags and TODO state.
  10110. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10111. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10112. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10113. formats in the current buffer."
  10114. (let (rtn range cfmt s p)
  10115. (save-excursion
  10116. (save-restriction
  10117. (widen)
  10118. (goto-char (point-min))
  10119. (while (re-search-forward org-property-start-re nil t)
  10120. (setq range (org-get-property-block))
  10121. (goto-char (car range))
  10122. (while (re-search-forward
  10123. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10124. (cdr range) t)
  10125. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10126. (outline-next-heading))))
  10127. (when include-specials
  10128. (setq rtn (append org-special-properties rtn)))
  10129. (when include-defaults
  10130. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10131. (when include-columns
  10132. (save-excursion
  10133. (save-restriction
  10134. (widen)
  10135. (goto-char (point-min))
  10136. (while (re-search-forward
  10137. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10138. nil t)
  10139. (setq cfmt (match-string 2) s 0)
  10140. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10141. cfmt s)
  10142. (setq s (match-end 0)
  10143. p (match-string 1 cfmt))
  10144. (unless (or (equal p "ITEM")
  10145. (member p org-special-properties))
  10146. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10147. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10148. (defun org-property-values (key)
  10149. "Return a list of all values of property KEY."
  10150. (save-excursion
  10151. (save-restriction
  10152. (widen)
  10153. (goto-char (point-min))
  10154. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10155. values)
  10156. (while (re-search-forward re nil t)
  10157. (add-to-list 'values (org-trim (match-string 1))))
  10158. (delete "" values)))))
  10159. (defun org-insert-property-drawer ()
  10160. "Insert a property drawer into the current entry."
  10161. (interactive)
  10162. (org-back-to-heading t)
  10163. (looking-at outline-regexp)
  10164. (let ((indent (- (match-end 0)(match-beginning 0)))
  10165. (beg (point))
  10166. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10167. end hiddenp)
  10168. (outline-next-heading)
  10169. (setq end (point))
  10170. (goto-char beg)
  10171. (while (re-search-forward re end t))
  10172. (setq hiddenp (org-invisible-p))
  10173. (end-of-line 1)
  10174. (and (equal (char-after) ?\n) (forward-char 1))
  10175. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10176. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10177. ;; just skip this line
  10178. (beginning-of-line 2)
  10179. ;; Drawer start, find the end
  10180. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10181. (beginning-of-line 1)))
  10182. (org-skip-over-state-notes)
  10183. (skip-chars-backward " \t\n\r")
  10184. (if (eq (char-before) ?*) (forward-char 1))
  10185. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10186. (beginning-of-line 0)
  10187. (org-indent-to-column indent)
  10188. (beginning-of-line 2)
  10189. (org-indent-to-column indent)
  10190. (beginning-of-line 0)
  10191. (if hiddenp
  10192. (save-excursion
  10193. (org-back-to-heading t)
  10194. (hide-entry))
  10195. (org-flag-drawer t))))
  10196. (defun org-set-property (property value)
  10197. "In the current entry, set PROPERTY to VALUE.
  10198. When called interactively, this will prompt for a property name, offering
  10199. completion on existing and default properties. And then it will prompt
  10200. for a value, offering completion either on allowed values (via an inherited
  10201. xxx_ALL property) or on existing values in other instances of this property
  10202. in the current file."
  10203. (interactive
  10204. (let* ((completion-ignore-case t)
  10205. (keys (org-buffer-property-keys nil t t))
  10206. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10207. (prop (if (member prop0 keys)
  10208. prop0
  10209. (or (cdr (assoc (downcase prop0)
  10210. (mapcar (lambda (x) (cons (downcase x) x))
  10211. keys)))
  10212. prop0)))
  10213. (cur (org-entry-get nil prop))
  10214. (allowed (org-property-get-allowed-values nil prop 'table))
  10215. (existing (mapcar 'list (org-property-values prop)))
  10216. (val (if allowed
  10217. (org-completing-read "Value: " allowed nil 'req-match)
  10218. (let (org-completion-use-ido)
  10219. (org-completing-read
  10220. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10221. (concat "[" cur "]") "")
  10222. ": ")
  10223. existing nil nil "" nil cur)))))
  10224. (list prop (if (equal val "") cur val))))
  10225. (unless (equal (org-entry-get nil property) value)
  10226. (org-entry-put nil property value)))
  10227. (defun org-delete-property (property)
  10228. "In the current entry, delete PROPERTY."
  10229. (interactive
  10230. (let* ((completion-ignore-case t)
  10231. (prop (org-ido-completing-read
  10232. "Property: " (org-entry-properties nil 'standard))))
  10233. (list prop)))
  10234. (message "Property %s %s" property
  10235. (if (org-entry-delete nil property)
  10236. "deleted"
  10237. "was not present in the entry")))
  10238. (defun org-delete-property-globally (property)
  10239. "Remove PROPERTY globally, from all entries."
  10240. (interactive
  10241. (let* ((completion-ignore-case t)
  10242. (prop (org-ido-completing-read
  10243. "Globally remove property: "
  10244. (mapcar 'list (org-buffer-property-keys)))))
  10245. (list prop)))
  10246. (save-excursion
  10247. (save-restriction
  10248. (widen)
  10249. (goto-char (point-min))
  10250. (let ((cnt 0))
  10251. (while (re-search-forward
  10252. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10253. nil t)
  10254. (setq cnt (1+ cnt))
  10255. (replace-match ""))
  10256. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10257. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10258. (defun org-compute-property-at-point ()
  10259. "Compute the property at point.
  10260. This looks for an enclosing column format, extracts the operator and
  10261. then applies it to the property in the column format's scope."
  10262. (interactive)
  10263. (unless (org-at-property-p)
  10264. (error "Not at a property"))
  10265. (let ((prop (org-match-string-no-properties 2)))
  10266. (org-columns-get-format-and-top-level)
  10267. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10268. (error "No operator defined for property %s" prop))
  10269. (org-columns-compute prop)))
  10270. (defun org-property-get-allowed-values (pom property &optional table)
  10271. "Get allowed values for the property PROPERTY.
  10272. When TABLE is non-nil, return an alist that can directly be used for
  10273. completion."
  10274. (let (vals)
  10275. (cond
  10276. ((equal property "TODO")
  10277. (setq vals (org-with-point-at pom
  10278. (append org-todo-keywords-1 '("")))))
  10279. ((equal property "PRIORITY")
  10280. (let ((n org-lowest-priority))
  10281. (while (>= n org-highest-priority)
  10282. (push (char-to-string n) vals)
  10283. (setq n (1- n)))))
  10284. ((member property org-special-properties))
  10285. (t
  10286. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10287. (when (and vals (string-match "\\S-" vals))
  10288. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10289. (setq vals (mapcar (lambda (x)
  10290. (cond ((stringp x) x)
  10291. ((numberp x) (number-to-string x))
  10292. ((symbolp x) (symbol-name x))
  10293. (t "???")))
  10294. vals)))))
  10295. (if table (mapcar 'list vals) vals)))
  10296. (defun org-property-previous-allowed-value (&optional previous)
  10297. "Switch to the next allowed value for this property."
  10298. (interactive)
  10299. (org-property-next-allowed-value t))
  10300. (defun org-property-next-allowed-value (&optional previous)
  10301. "Switch to the next allowed value for this property."
  10302. (interactive)
  10303. (unless (org-at-property-p)
  10304. (error "Not at a property"))
  10305. (let* ((key (match-string 2))
  10306. (value (match-string 3))
  10307. (allowed (or (org-property-get-allowed-values (point) key)
  10308. (and (member value '("[ ]" "[-]" "[X]"))
  10309. '("[ ]" "[X]"))))
  10310. nval)
  10311. (unless allowed
  10312. (error "Allowed values for this property have not been defined"))
  10313. (if previous (setq allowed (reverse allowed)))
  10314. (if (member value allowed)
  10315. (setq nval (car (cdr (member value allowed)))))
  10316. (setq nval (or nval (car allowed)))
  10317. (if (equal nval value)
  10318. (error "Only one allowed value for this property"))
  10319. (org-at-property-p)
  10320. (replace-match (concat " :" key ": " nval) t t)
  10321. (org-indent-line-function)
  10322. (beginning-of-line 1)
  10323. (skip-chars-forward " \t")))
  10324. (defun org-find-entry-with-id (ident)
  10325. "Locate the entry that contains the ID property with exact value IDENT.
  10326. IDENT can be a string, a symbol or a number, this function will search for
  10327. the string representation of it.
  10328. Return the position where this entry starts, or nil if there is no such entry."
  10329. (interactive "sID: ")
  10330. (let ((id (cond
  10331. ((stringp ident) ident)
  10332. ((symbol-name ident) (symbol-name ident))
  10333. ((numberp ident) (number-to-string ident))
  10334. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10335. (case-fold-search nil))
  10336. (save-excursion
  10337. (save-restriction
  10338. (widen)
  10339. (goto-char (point-min))
  10340. (when (re-search-forward
  10341. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10342. nil t)
  10343. (org-back-to-heading)
  10344. (point))))))
  10345. ;;;; Timestamps
  10346. (defvar org-last-changed-timestamp nil)
  10347. (defvar org-last-inserted-timestamp nil
  10348. "The last time stamp inserted with `org-insert-time-stamp'.")
  10349. (defvar org-time-was-given) ; dynamically scoped parameter
  10350. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10351. (defvar org-ts-what) ; dynamically scoped parameter
  10352. (defun org-time-stamp (arg &optional inactive)
  10353. "Prompt for a date/time and insert a time stamp.
  10354. If the user specifies a time like HH:MM, or if this command is called
  10355. with a prefix argument, the time stamp will contain date and time.
  10356. Otherwise, only the date will be included. All parts of a date not
  10357. specified by the user will be filled in from the current date/time.
  10358. So if you press just return without typing anything, the time stamp
  10359. will represent the current date/time. If there is already a timestamp
  10360. at the cursor, it will be modified."
  10361. (interactive "P")
  10362. (let* ((ts nil)
  10363. (default-time
  10364. ;; Default time is either today, or, when entering a range,
  10365. ;; the range start.
  10366. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10367. (save-excursion
  10368. (re-search-backward
  10369. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10370. (- (point) 20) t)))
  10371. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10372. (current-time)))
  10373. (default-input (and ts (org-get-compact-tod ts)))
  10374. org-time-was-given org-end-time-was-given time)
  10375. (cond
  10376. ((and (org-at-timestamp-p t)
  10377. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10378. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10379. (insert "--")
  10380. (setq time (let ((this-command this-command))
  10381. (org-read-date arg 'totime nil nil
  10382. default-time default-input)))
  10383. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10384. ((org-at-timestamp-p t)
  10385. (setq time (let ((this-command this-command))
  10386. (org-read-date arg 'totime nil nil default-time default-input)))
  10387. (when (org-at-timestamp-p t) ; just to get the match data
  10388. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10389. (replace-match "")
  10390. (setq org-last-changed-timestamp
  10391. (org-insert-time-stamp
  10392. time (or org-time-was-given arg)
  10393. inactive nil nil (list org-end-time-was-given))))
  10394. (message "Timestamp updated"))
  10395. (t
  10396. (setq time (let ((this-command this-command))
  10397. (org-read-date arg 'totime nil nil default-time default-input)))
  10398. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10399. nil nil (list org-end-time-was-given))))))
  10400. ;; FIXME: can we use this for something else, like computing time differences?
  10401. (defun org-get-compact-tod (s)
  10402. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10403. (let* ((t1 (match-string 1 s))
  10404. (h1 (string-to-number (match-string 2 s)))
  10405. (m1 (string-to-number (match-string 3 s)))
  10406. (t2 (and (match-end 4) (match-string 5 s)))
  10407. (h2 (and t2 (string-to-number (match-string 6 s))))
  10408. (m2 (and t2 (string-to-number (match-string 7 s))))
  10409. dh dm)
  10410. (if (not t2)
  10411. t1
  10412. (setq dh (- h2 h1) dm (- m2 m1))
  10413. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10414. (concat t1 "+" (number-to-string dh)
  10415. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10416. (defun org-time-stamp-inactive (&optional arg)
  10417. "Insert an inactive time stamp.
  10418. An inactive time stamp is enclosed in square brackets instead of angle
  10419. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10420. does not link to the calendar and cannot be changed with the S-cursor keys.
  10421. So these are more for recording a certain time/date."
  10422. (interactive "P")
  10423. (org-time-stamp arg 'inactive))
  10424. (defvar org-date-ovl (org-make-overlay 1 1))
  10425. (org-overlay-put org-date-ovl 'face 'org-warning)
  10426. (org-detach-overlay org-date-ovl)
  10427. (defvar org-ans1) ; dynamically scoped parameter
  10428. (defvar org-ans2) ; dynamically scoped parameter
  10429. (defvar org-plain-time-of-day-regexp) ; defined below
  10430. (defvar org-overriding-default-time nil) ; dynamically scoped
  10431. (defvar org-read-date-overlay nil)
  10432. (defvar org-dcst nil) ; dynamically scoped
  10433. (defun org-read-date (&optional with-time to-time from-string prompt
  10434. default-time default-input)
  10435. "Read a date, possibly a time, and make things smooth for the user.
  10436. The prompt will suggest to enter an ISO date, but you can also enter anything
  10437. which will at least partially be understood by `parse-time-string'.
  10438. Unrecognized parts of the date will default to the current day, month, year,
  10439. hour and minute. If this command is called to replace a timestamp at point,
  10440. of to enter the second timestamp of a range, the default time is taken from the
  10441. existing stamp. For example,
  10442. 3-2-5 --> 2003-02-05
  10443. feb 15 --> currentyear-02-15
  10444. sep 12 9 --> 2009-09-12
  10445. 12:45 --> today 12:45
  10446. 22 sept 0:34 --> currentyear-09-22 0:34
  10447. 12 --> currentyear-currentmonth-12
  10448. Fri --> nearest Friday (today or later)
  10449. etc.
  10450. Furthermore you can specify a relative date by giving, as the *first* thing
  10451. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10452. change in days weeks, months, years.
  10453. With a single plus or minus, the date is relative to today. With a double
  10454. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10455. +4d --> four days from today
  10456. +4 --> same as above
  10457. +2w --> two weeks from today
  10458. ++5 --> five days from default date
  10459. The function understands only English month and weekday abbreviations,
  10460. but this can be configured with the variables `parse-time-months' and
  10461. `parse-time-weekdays'.
  10462. While prompting, a calendar is popped up - you can also select the
  10463. date with the mouse (button 1). The calendar shows a period of three
  10464. months. To scroll it to other months, use the keys `>' and `<'.
  10465. If you don't like the calendar, turn it off with
  10466. \(setq org-read-date-popup-calendar nil)
  10467. With optional argument TO-TIME, the date will immediately be converted
  10468. to an internal time.
  10469. With an optional argument WITH-TIME, the prompt will suggest to also
  10470. insert a time. Note that when WITH-TIME is not set, you can still
  10471. enter a time, and this function will inform the calling routine about
  10472. this change. The calling routine may then choose to change the format
  10473. used to insert the time stamp into the buffer to include the time.
  10474. With optional argument FROM-STRING, read from this string instead from
  10475. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10476. the time/date that is used for everything that is not specified by the
  10477. user."
  10478. (require 'parse-time)
  10479. (let* ((org-time-stamp-rounding-minutes
  10480. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10481. (org-dcst org-display-custom-times)
  10482. (ct (org-current-time))
  10483. (def (or org-overriding-default-time default-time ct))
  10484. (defdecode (decode-time def))
  10485. (dummy (progn
  10486. (when (< (nth 2 defdecode) org-extend-today-until)
  10487. (setcar (nthcdr 2 defdecode) -1)
  10488. (setcar (nthcdr 1 defdecode) 59)
  10489. (setq def (apply 'encode-time defdecode)
  10490. defdecode (decode-time def)))))
  10491. (calendar-move-hook nil)
  10492. (calendar-view-diary-initially-flag nil)
  10493. (view-diary-entries-initially nil)
  10494. (calendar-view-holidays-initially-flag nil)
  10495. (view-calendar-holidays-initially nil)
  10496. (timestr (format-time-string
  10497. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10498. (prompt (concat (if prompt (concat prompt " ") "")
  10499. (format "Date+time [%s]: " timestr)))
  10500. ans (org-ans0 "") org-ans1 org-ans2 final)
  10501. (cond
  10502. (from-string (setq ans from-string))
  10503. (org-read-date-popup-calendar
  10504. (save-excursion
  10505. (save-window-excursion
  10506. (calendar)
  10507. (calendar-forward-day (- (time-to-days def)
  10508. (calendar-absolute-from-gregorian
  10509. (calendar-current-date))))
  10510. (org-eval-in-calendar nil t)
  10511. (let* ((old-map (current-local-map))
  10512. (map (copy-keymap calendar-mode-map))
  10513. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10514. (org-defkey map (kbd "RET") 'org-calendar-select)
  10515. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10516. 'org-calendar-select-mouse)
  10517. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10518. 'org-calendar-select-mouse)
  10519. (org-defkey minibuffer-local-map [(meta shift left)]
  10520. (lambda () (interactive)
  10521. (org-eval-in-calendar '(calendar-backward-month 1))))
  10522. (org-defkey minibuffer-local-map [(meta shift right)]
  10523. (lambda () (interactive)
  10524. (org-eval-in-calendar '(calendar-forward-month 1))))
  10525. (org-defkey minibuffer-local-map [(meta shift up)]
  10526. (lambda () (interactive)
  10527. (org-eval-in-calendar '(calendar-backward-year 1))))
  10528. (org-defkey minibuffer-local-map [(meta shift down)]
  10529. (lambda () (interactive)
  10530. (org-eval-in-calendar '(calendar-forward-year 1))))
  10531. (org-defkey minibuffer-local-map [(shift up)]
  10532. (lambda () (interactive)
  10533. (org-eval-in-calendar '(calendar-backward-week 1))))
  10534. (org-defkey minibuffer-local-map [(shift down)]
  10535. (lambda () (interactive)
  10536. (org-eval-in-calendar '(calendar-forward-week 1))))
  10537. (org-defkey minibuffer-local-map [(shift left)]
  10538. (lambda () (interactive)
  10539. (org-eval-in-calendar '(calendar-backward-day 1))))
  10540. (org-defkey minibuffer-local-map [(shift right)]
  10541. (lambda () (interactive)
  10542. (org-eval-in-calendar '(calendar-forward-day 1))))
  10543. (org-defkey minibuffer-local-map ">"
  10544. (lambda () (interactive)
  10545. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10546. (org-defkey minibuffer-local-map "<"
  10547. (lambda () (interactive)
  10548. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10549. (unwind-protect
  10550. (progn
  10551. (use-local-map map)
  10552. (add-hook 'post-command-hook 'org-read-date-display)
  10553. (setq org-ans0 (read-string prompt default-input nil nil))
  10554. ;; org-ans0: from prompt
  10555. ;; org-ans1: from mouse click
  10556. ;; org-ans2: from calendar motion
  10557. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10558. (remove-hook 'post-command-hook 'org-read-date-display)
  10559. (use-local-map old-map)
  10560. (when org-read-date-overlay
  10561. (org-delete-overlay org-read-date-overlay)
  10562. (setq org-read-date-overlay nil)))))))
  10563. (t ; Naked prompt only
  10564. (unwind-protect
  10565. (setq ans (read-string prompt default-input nil timestr))
  10566. (when org-read-date-overlay
  10567. (org-delete-overlay org-read-date-overlay)
  10568. (setq org-read-date-overlay nil)))))
  10569. (setq final (org-read-date-analyze ans def defdecode))
  10570. (if to-time
  10571. (apply 'encode-time final)
  10572. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10573. (format "%04d-%02d-%02d %02d:%02d"
  10574. (nth 5 final) (nth 4 final) (nth 3 final)
  10575. (nth 2 final) (nth 1 final))
  10576. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10577. (defvar def)
  10578. (defvar defdecode)
  10579. (defvar with-time)
  10580. (defun org-read-date-display ()
  10581. "Display the current date prompt interpretation in the minibuffer."
  10582. (when org-read-date-display-live
  10583. (when org-read-date-overlay
  10584. (org-delete-overlay org-read-date-overlay))
  10585. (let ((p (point)))
  10586. (end-of-line 1)
  10587. (while (not (equal (buffer-substring
  10588. (max (point-min) (- (point) 4)) (point))
  10589. " "))
  10590. (insert " "))
  10591. (goto-char p))
  10592. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10593. " " (or org-ans1 org-ans2)))
  10594. (org-end-time-was-given nil)
  10595. (f (org-read-date-analyze ans def defdecode))
  10596. (fmts (if org-dcst
  10597. org-time-stamp-custom-formats
  10598. org-time-stamp-formats))
  10599. (fmt (if (or with-time
  10600. (and (boundp 'org-time-was-given) org-time-was-given))
  10601. (cdr fmts)
  10602. (car fmts)))
  10603. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10604. (when (and org-end-time-was-given
  10605. (string-match org-plain-time-of-day-regexp txt))
  10606. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10607. org-end-time-was-given
  10608. (substring txt (match-end 0)))))
  10609. (setq org-read-date-overlay
  10610. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10611. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10612. (defun org-read-date-analyze (ans def defdecode)
  10613. "Analyse the combined answer of the date prompt."
  10614. ;; FIXME: cleanup and comment
  10615. (let (delta deltan deltaw deltadef year month day
  10616. hour minute second wday pm h2 m2 tl wday1
  10617. iso-year iso-weekday iso-week iso-year iso-date)
  10618. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10619. (setq ans "+0"))
  10620. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10621. (setq ans (replace-match "" t t ans)
  10622. deltan (car delta)
  10623. deltaw (nth 1 delta)
  10624. deltadef (nth 2 delta)))
  10625. ;; Check if there is an iso week date in there
  10626. ;; If yes, sore the info and postpone interpreting it until the rest
  10627. ;; of the parsing is done
  10628. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10629. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10630. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10631. iso-week (string-to-number (match-string 2 ans)))
  10632. (setq ans (replace-match "" t t ans)))
  10633. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10634. (when (string-match
  10635. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10636. (setq year (if (match-end 2)
  10637. (string-to-number (match-string 2 ans))
  10638. (string-to-number (format-time-string "%Y")))
  10639. month (string-to-number (match-string 3 ans))
  10640. day (string-to-number (match-string 4 ans)))
  10641. (if (< year 100) (setq year (+ 2000 year)))
  10642. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10643. t nil ans)))
  10644. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10645. ;; If there is a time with am/pm, and *no* time without it, we convert
  10646. ;; so that matching will be successful.
  10647. (loop for i from 1 to 2 do ; twice, for end time as well
  10648. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10649. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10650. (setq hour (string-to-number (match-string 1 ans))
  10651. minute (if (match-end 3)
  10652. (string-to-number (match-string 3 ans))
  10653. 0)
  10654. pm (equal ?p
  10655. (string-to-char (downcase (match-string 4 ans)))))
  10656. (if (and (= hour 12) (not pm))
  10657. (setq hour 0)
  10658. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10659. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10660. t t ans))))
  10661. ;; Check if a time range is given as a duration
  10662. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10663. (setq hour (string-to-number (match-string 1 ans))
  10664. h2 (+ hour (string-to-number (match-string 3 ans)))
  10665. minute (string-to-number (match-string 2 ans))
  10666. m2 (+ minute (if (match-end 5) (string-to-number
  10667. (match-string 5 ans))0)))
  10668. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10669. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10670. t t ans)))
  10671. ;; Check if there is a time range
  10672. (when (boundp 'org-end-time-was-given)
  10673. (setq org-time-was-given nil)
  10674. (when (and (string-match org-plain-time-of-day-regexp ans)
  10675. (match-end 8))
  10676. (setq org-end-time-was-given (match-string 8 ans))
  10677. (setq ans (concat (substring ans 0 (match-beginning 7))
  10678. (substring ans (match-end 7))))))
  10679. (setq tl (parse-time-string ans)
  10680. day (or (nth 3 tl) (nth 3 defdecode))
  10681. month (or (nth 4 tl)
  10682. (if (and org-read-date-prefer-future
  10683. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10684. (1+ (nth 4 defdecode))
  10685. (nth 4 defdecode)))
  10686. year (or (nth 5 tl)
  10687. (if (and org-read-date-prefer-future
  10688. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10689. (1+ (nth 5 defdecode))
  10690. (nth 5 defdecode)))
  10691. hour (or (nth 2 tl) (nth 2 defdecode))
  10692. minute (or (nth 1 tl) (nth 1 defdecode))
  10693. second (or (nth 0 tl) 0)
  10694. wday (nth 6 tl))
  10695. ;; Special date definitions below
  10696. (cond
  10697. (iso-week
  10698. ;; There was an iso week
  10699. (setq year (or iso-year year)
  10700. day (or iso-weekday wday 1)
  10701. wday nil ; to make sure that the trigger below does not match
  10702. iso-date (calendar-gregorian-from-absolute
  10703. (calendar-absolute-from-iso
  10704. (list iso-week day year))))
  10705. ; FIXME: Should we also push ISO weeks into the future?
  10706. ; (when (and org-read-date-prefer-future
  10707. ; (not iso-year)
  10708. ; (< (calendar-absolute-from-gregorian iso-date)
  10709. ; (time-to-days (current-time))))
  10710. ; (setq year (1+ year)
  10711. ; iso-date (calendar-gregorian-from-absolute
  10712. ; (calendar-absolute-from-iso
  10713. ; (list iso-week day year)))))
  10714. (setq month (car iso-date)
  10715. year (nth 2 iso-date)
  10716. day (nth 1 iso-date)))
  10717. (deltan
  10718. (unless deltadef
  10719. (let ((now (decode-time (current-time))))
  10720. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10721. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10722. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10723. ((equal deltaw "m") (setq month (+ month deltan)))
  10724. ((equal deltaw "y") (setq year (+ year deltan)))))
  10725. ((and wday (not (nth 3 tl)))
  10726. ;; Weekday was given, but no day, so pick that day in the week
  10727. ;; on or after the derived date.
  10728. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10729. (unless (equal wday wday1)
  10730. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10731. (if (and (boundp 'org-time-was-given)
  10732. (nth 2 tl))
  10733. (setq org-time-was-given t))
  10734. (if (< year 100) (setq year (+ 2000 year)))
  10735. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10736. (list second minute hour day month year)))
  10737. (defvar parse-time-weekdays)
  10738. (defun org-read-date-get-relative (s today default)
  10739. "Check string S for special relative date string.
  10740. TODAY and DEFAULT are internal times, for today and for a default.
  10741. Return shift list (N what def-flag)
  10742. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10743. N is the number of WHATs to shift.
  10744. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10745. the DEFAULT date rather than TODAY."
  10746. (when (and
  10747. (string-match
  10748. (concat
  10749. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10750. "\\([0-9]+\\)?"
  10751. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10752. "\\([ \t]\\|$\\)") s)
  10753. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10754. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10755. (string-to-char (substring (match-string 1 s) -1))
  10756. ?+))
  10757. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10758. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10759. (what (if (match-end 3) (match-string 3 s) "d"))
  10760. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10761. (date (if rel default today))
  10762. (wday (nth 6 (decode-time date)))
  10763. delta)
  10764. (if wday1
  10765. (progn
  10766. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10767. (if (= dir ?-) (setq delta (- delta 7)))
  10768. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10769. (list delta "d" rel))
  10770. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10771. (defun org-eval-in-calendar (form &optional keepdate)
  10772. "Eval FORM in the calendar window and return to current window.
  10773. Also, store the cursor date in variable org-ans2."
  10774. (let ((sw (selected-window)))
  10775. (select-window (get-buffer-window "*Calendar*"))
  10776. (eval form)
  10777. (when (and (not keepdate) (calendar-cursor-to-date))
  10778. (let* ((date (calendar-cursor-to-date))
  10779. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10780. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10781. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10782. (select-window sw)))
  10783. (defun org-calendar-select ()
  10784. "Return to `org-read-date' with the date currently selected.
  10785. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10786. (interactive)
  10787. (when (calendar-cursor-to-date)
  10788. (let* ((date (calendar-cursor-to-date))
  10789. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10790. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10791. (if (active-minibuffer-window) (exit-minibuffer))))
  10792. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10793. "Insert a date stamp for the date given by the internal TIME.
  10794. WITH-HM means, use the stamp format that includes the time of the day.
  10795. INACTIVE means use square brackets instead of angular ones, so that the
  10796. stamp will not contribute to the agenda.
  10797. PRE and POST are optional strings to be inserted before and after the
  10798. stamp.
  10799. The command returns the inserted time stamp."
  10800. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10801. stamp)
  10802. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10803. (insert-before-markers (or pre ""))
  10804. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10805. (when (listp extra)
  10806. (setq extra (car extra))
  10807. (if (and (stringp extra)
  10808. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10809. (setq extra (format "-%02d:%02d"
  10810. (string-to-number (match-string 1 extra))
  10811. (string-to-number (match-string 2 extra))))
  10812. (setq extra nil)))
  10813. (when extra
  10814. (backward-char 1)
  10815. (insert-before-markers extra)
  10816. (forward-char 1))
  10817. (insert-before-markers (or post ""))
  10818. (setq org-last-inserted-timestamp stamp)))
  10819. (defun org-toggle-time-stamp-overlays ()
  10820. "Toggle the use of custom time stamp formats."
  10821. (interactive)
  10822. (setq org-display-custom-times (not org-display-custom-times))
  10823. (unless org-display-custom-times
  10824. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10825. (while (setq p (next-single-property-change p 'display))
  10826. (if (and (get-text-property p 'display)
  10827. (eq (get-text-property p 'face) 'org-date))
  10828. (remove-text-properties
  10829. p (setq p (next-single-property-change p 'display))
  10830. '(display t))))
  10831. (set-buffer-modified-p bmp)))
  10832. (if (featurep 'xemacs)
  10833. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10834. (org-restart-font-lock)
  10835. (setq org-table-may-need-update t)
  10836. (if org-display-custom-times
  10837. (message "Time stamps are overlayed with custom format")
  10838. (message "Time stamp overlays removed")))
  10839. (defun org-display-custom-time (beg end)
  10840. "Overlay modified time stamp format over timestamp between BEG and END."
  10841. (let* ((ts (buffer-substring beg end))
  10842. t1 w1 with-hm tf time str w2 (off 0))
  10843. (save-match-data
  10844. (setq t1 (org-parse-time-string ts t))
  10845. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10846. (setq off (- (match-end 0) (match-beginning 0)))))
  10847. (setq end (- end off))
  10848. (setq w1 (- end beg)
  10849. with-hm (and (nth 1 t1) (nth 2 t1))
  10850. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10851. time (org-fix-decoded-time t1)
  10852. str (org-add-props
  10853. (format-time-string
  10854. (substring tf 1 -1) (apply 'encode-time time))
  10855. nil 'mouse-face 'highlight)
  10856. w2 (length str))
  10857. (if (not (= w2 w1))
  10858. (add-text-properties (1+ beg) (+ 2 beg)
  10859. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10860. (if (featurep 'xemacs)
  10861. (progn
  10862. (put-text-property beg end 'invisible t)
  10863. (put-text-property beg end 'end-glyph (make-glyph str)))
  10864. (put-text-property beg end 'display str))))
  10865. (defun org-translate-time (string)
  10866. "Translate all timestamps in STRING to custom format.
  10867. But do this only if the variable `org-display-custom-times' is set."
  10868. (when org-display-custom-times
  10869. (save-match-data
  10870. (let* ((start 0)
  10871. (re org-ts-regexp-both)
  10872. t1 with-hm inactive tf time str beg end)
  10873. (while (setq start (string-match re string start))
  10874. (setq beg (match-beginning 0)
  10875. end (match-end 0)
  10876. t1 (save-match-data
  10877. (org-parse-time-string (substring string beg end) t))
  10878. with-hm (and (nth 1 t1) (nth 2 t1))
  10879. inactive (equal (substring string beg (1+ beg)) "[")
  10880. tf (funcall (if with-hm 'cdr 'car)
  10881. org-time-stamp-custom-formats)
  10882. time (org-fix-decoded-time t1)
  10883. str (format-time-string
  10884. (concat
  10885. (if inactive "[" "<") (substring tf 1 -1)
  10886. (if inactive "]" ">"))
  10887. (apply 'encode-time time))
  10888. string (replace-match str t t string)
  10889. start (+ start (length str)))))))
  10890. string)
  10891. (defun org-fix-decoded-time (time)
  10892. "Set 0 instead of nil for the first 6 elements of time.
  10893. Don't touch the rest."
  10894. (let ((n 0))
  10895. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10896. (defun org-days-to-time (timestamp-string)
  10897. "Difference between TIMESTAMP-STRING and now in days."
  10898. (- (time-to-days (org-time-string-to-time timestamp-string))
  10899. (time-to-days (current-time))))
  10900. (defun org-deadline-close (timestamp-string &optional ndays)
  10901. "Is the time in TIMESTAMP-STRING close to the current date?"
  10902. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10903. (and (< (org-days-to-time timestamp-string) ndays)
  10904. (not (org-entry-is-done-p))))
  10905. (defun org-get-wdays (ts)
  10906. "Get the deadline lead time appropriate for timestring TS."
  10907. (cond
  10908. ((<= org-deadline-warning-days 0)
  10909. ;; 0 or negative, enforce this value no matter what
  10910. (- org-deadline-warning-days))
  10911. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  10912. ;; lead time is specified.
  10913. (floor (* (string-to-number (match-string 1 ts))
  10914. (cdr (assoc (match-string 2 ts)
  10915. '(("d" . 1) ("w" . 7)
  10916. ("m" . 30.4) ("y" . 365.25)))))))
  10917. ;; go for the default.
  10918. (t org-deadline-warning-days)))
  10919. (defun org-calendar-select-mouse (ev)
  10920. "Return to `org-read-date' with the date currently selected.
  10921. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10922. (interactive "e")
  10923. (mouse-set-point ev)
  10924. (when (calendar-cursor-to-date)
  10925. (let* ((date (calendar-cursor-to-date))
  10926. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10927. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10928. (if (active-minibuffer-window) (exit-minibuffer))))
  10929. (defun org-check-deadlines (ndays)
  10930. "Check if there are any deadlines due or past due.
  10931. A deadline is considered due if it happens within `org-deadline-warning-days'
  10932. days from today's date. If the deadline appears in an entry marked DONE,
  10933. it is not shown. The prefix arg NDAYS can be used to test that many
  10934. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10935. (interactive "P")
  10936. (let* ((org-warn-days
  10937. (cond
  10938. ((equal ndays '(4)) 100000)
  10939. (ndays (prefix-numeric-value ndays))
  10940. (t (abs org-deadline-warning-days))))
  10941. (case-fold-search nil)
  10942. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10943. (callback
  10944. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10945. (message "%d deadlines past-due or due within %d days"
  10946. (org-occur regexp nil callback)
  10947. org-warn-days)))
  10948. (defun org-check-before-date (date)
  10949. "Check if there are deadlines or scheduled entries before DATE."
  10950. (interactive (list (org-read-date)))
  10951. (let ((case-fold-search nil)
  10952. (regexp (concat "\\<\\(" org-deadline-string
  10953. "\\|" org-scheduled-string
  10954. "\\) *<\\([^>]+\\)>"))
  10955. (callback
  10956. (lambda () (time-less-p
  10957. (org-time-string-to-time (match-string 2))
  10958. (org-time-string-to-time date)))))
  10959. (message "%d entries before %s"
  10960. (org-occur regexp nil callback) date)))
  10961. (defun org-evaluate-time-range (&optional to-buffer)
  10962. "Evaluate a time range by computing the difference between start and end.
  10963. Normally the result is just printed in the echo area, but with prefix arg
  10964. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10965. If the time range is actually in a table, the result is inserted into the
  10966. next column.
  10967. For time difference computation, a year is assumed to be exactly 365
  10968. days in order to avoid rounding problems."
  10969. (interactive "P")
  10970. (or
  10971. (org-clock-update-time-maybe)
  10972. (save-excursion
  10973. (unless (org-at-date-range-p t)
  10974. (goto-char (point-at-bol))
  10975. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10976. (if (not (org-at-date-range-p t))
  10977. (error "Not at a time-stamp range, and none found in current line")))
  10978. (let* ((ts1 (match-string 1))
  10979. (ts2 (match-string 2))
  10980. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10981. (match-end (match-end 0))
  10982. (time1 (org-time-string-to-time ts1))
  10983. (time2 (org-time-string-to-time ts2))
  10984. (t1 (time-to-seconds time1))
  10985. (t2 (time-to-seconds time2))
  10986. (diff (abs (- t2 t1)))
  10987. (negative (< (- t2 t1) 0))
  10988. ;; (ys (floor (* 365 24 60 60)))
  10989. (ds (* 24 60 60))
  10990. (hs (* 60 60))
  10991. (fy "%dy %dd %02d:%02d")
  10992. (fy1 "%dy %dd")
  10993. (fd "%dd %02d:%02d")
  10994. (fd1 "%dd")
  10995. (fh "%02d:%02d")
  10996. y d h m align)
  10997. (if havetime
  10998. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10999. y 0
  11000. d (floor (/ diff ds)) diff (mod diff ds)
  11001. h (floor (/ diff hs)) diff (mod diff hs)
  11002. m (floor (/ diff 60)))
  11003. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11004. y 0
  11005. d (floor (+ (/ diff ds) 0.5))
  11006. h 0 m 0))
  11007. (if (not to-buffer)
  11008. (message "%s" (org-make-tdiff-string y d h m))
  11009. (if (org-at-table-p)
  11010. (progn
  11011. (goto-char match-end)
  11012. (setq align t)
  11013. (and (looking-at " *|") (goto-char (match-end 0))))
  11014. (goto-char match-end))
  11015. (if (looking-at
  11016. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11017. (replace-match ""))
  11018. (if negative (insert " -"))
  11019. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11020. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11021. (insert " " (format fh h m))))
  11022. (if align (org-table-align))
  11023. (message "Time difference inserted")))))
  11024. (defun org-make-tdiff-string (y d h m)
  11025. (let ((fmt "")
  11026. (l nil))
  11027. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11028. l (push y l)))
  11029. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11030. l (push d l)))
  11031. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11032. l (push h l)))
  11033. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11034. l (push m l)))
  11035. (apply 'format fmt (nreverse l))))
  11036. (defun org-time-string-to-time (s)
  11037. (apply 'encode-time (org-parse-time-string s)))
  11038. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11039. "Convert a time stamp to an absolute day number.
  11040. If there is a specifyer for a cyclic time stamp, get the closest date to
  11041. DAYNR.
  11042. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11043. (cond
  11044. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11045. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11046. daynr
  11047. (+ daynr 1000)))
  11048. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11049. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11050. (time-to-days (current-time))) (match-string 0 s)
  11051. prefer show-all))
  11052. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11053. (defun org-days-to-iso-week (days)
  11054. "Return the iso week number."
  11055. (require 'cal-iso)
  11056. (car (calendar-iso-from-absolute days)))
  11057. (defun org-small-year-to-year (year)
  11058. "Convert 2-digit years into 4-digit years.
  11059. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11060. The year 2000 cannot be abbreviated. Any year larger than 99
  11061. is returned unchanged."
  11062. (if (< year 38)
  11063. (setq year (+ 2000 year))
  11064. (if (< year 100)
  11065. (setq year (+ 1900 year))))
  11066. year)
  11067. (defun org-time-from-absolute (d)
  11068. "Return the time corresponding to date D.
  11069. D may be an absolute day number, or a calendar-type list (month day year)."
  11070. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11071. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11072. (defun org-calendar-holiday ()
  11073. "List of holidays, for Diary display in Org-mode."
  11074. (require 'holidays)
  11075. (let ((hl (funcall
  11076. (if (fboundp 'calendar-check-holidays)
  11077. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11078. (if hl (mapconcat 'identity hl "; "))))
  11079. (defun org-diary-sexp-entry (sexp entry date)
  11080. "Process a SEXP diary ENTRY for DATE."
  11081. (require 'diary-lib)
  11082. (let ((result (if calendar-debug-sexp
  11083. (let ((stack-trace-on-error t))
  11084. (eval (car (read-from-string sexp))))
  11085. (condition-case nil
  11086. (eval (car (read-from-string sexp)))
  11087. (error
  11088. (beep)
  11089. (message "Bad sexp at line %d in %s: %s"
  11090. (org-current-line)
  11091. (buffer-file-name) sexp)
  11092. (sleep-for 2))))))
  11093. (cond ((stringp result) result)
  11094. ((and (consp result)
  11095. (stringp (cdr result))) (cdr result))
  11096. (result entry)
  11097. (t nil))))
  11098. (defun org-diary-to-ical-string (frombuf)
  11099. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11100. This uses the icalendar.el library."
  11101. (let* ((tmpdir (if (featurep 'xemacs)
  11102. (temp-directory)
  11103. temporary-file-directory))
  11104. (tmpfile (make-temp-name
  11105. (expand-file-name "orgics" tmpdir)))
  11106. buf rtn b e)
  11107. (save-excursion
  11108. (set-buffer frombuf)
  11109. (icalendar-export-region (point-min) (point-max) tmpfile)
  11110. (setq buf (find-buffer-visiting tmpfile))
  11111. (set-buffer buf)
  11112. (goto-char (point-min))
  11113. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11114. (setq b (match-beginning 0)))
  11115. (goto-char (point-max))
  11116. (if (re-search-backward "^END:VEVENT" nil t)
  11117. (setq e (match-end 0)))
  11118. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11119. (kill-buffer buf)
  11120. (delete-file tmpfile)
  11121. rtn))
  11122. (defun org-closest-date (start current change prefer show-all)
  11123. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11124. When PREFER is `past' return a date that is either CURRENT or past.
  11125. When PREFER is `future', return a date that is either CURRENT or future.
  11126. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11127. ;; Make the proper lists from the dates
  11128. (catch 'exit
  11129. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11130. dn dw sday cday n1 n2 n0
  11131. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11132. (setq start (org-date-to-gregorian start)
  11133. current (org-date-to-gregorian
  11134. (if show-all
  11135. current
  11136. (time-to-days (current-time))))
  11137. sday (calendar-absolute-from-gregorian start)
  11138. cday (calendar-absolute-from-gregorian current))
  11139. (if (<= cday sday) (throw 'exit sday))
  11140. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11141. (setq dn (string-to-number (match-string 1 change))
  11142. dw (cdr (assoc (match-string 2 change) a1)))
  11143. (error "Invalid change specifyer: %s" change))
  11144. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11145. (cond
  11146. ((eq dw 'day)
  11147. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11148. n2 (+ n1 dn)))
  11149. ((eq dw 'year)
  11150. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11151. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11152. (setq date1 (list m d y1)
  11153. n1 (calendar-absolute-from-gregorian date1)
  11154. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11155. n2 (calendar-absolute-from-gregorian date2)))
  11156. ((eq dw 'month)
  11157. ;; approx number of month between the two dates
  11158. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11159. ;; How often does dn fit in there?
  11160. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11161. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11162. m (+ m nm)
  11163. ny (floor (/ m 12))
  11164. y (+ y ny)
  11165. m (- m (* ny 12)))
  11166. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11167. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11168. (setq m2 (+ m dn) y2 y)
  11169. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11170. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11171. (while (<= n2 cday)
  11172. (setq n1 n2 m m2 y y2)
  11173. (setq m2 (+ m dn) y2 y)
  11174. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11175. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11176. ;; Make sure n1 is the earlier date
  11177. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11178. (if show-all
  11179. (cond
  11180. ((eq prefer 'past) n1)
  11181. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11182. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11183. (cond
  11184. ((eq prefer 'past) n1)
  11185. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11186. (t (if (= cday n1) n1 n2)))))))
  11187. (defun org-date-to-gregorian (date)
  11188. "Turn any specification of DATE into a gregorian date for the calendar."
  11189. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11190. ((and (listp date) (= (length date) 3)) date)
  11191. ((stringp date)
  11192. (setq date (org-parse-time-string date))
  11193. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11194. ((listp date)
  11195. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11196. (defun org-parse-time-string (s &optional nodefault)
  11197. "Parse the standard Org-mode time string.
  11198. This should be a lot faster than the normal `parse-time-string'.
  11199. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11200. hour and minute fields will be nil if not given."
  11201. (if (string-match org-ts-regexp0 s)
  11202. (list 0
  11203. (if (or (match-beginning 8) (not nodefault))
  11204. (string-to-number (or (match-string 8 s) "0")))
  11205. (if (or (match-beginning 7) (not nodefault))
  11206. (string-to-number (or (match-string 7 s) "0")))
  11207. (string-to-number (match-string 4 s))
  11208. (string-to-number (match-string 3 s))
  11209. (string-to-number (match-string 2 s))
  11210. nil nil nil)
  11211. (make-list 9 0)))
  11212. (defun org-timestamp-up (&optional arg)
  11213. "Increase the date item at the cursor by one.
  11214. If the cursor is on the year, change the year. If it is on the month or
  11215. the day, change that.
  11216. With prefix ARG, change by that many units."
  11217. (interactive "p")
  11218. (org-timestamp-change (prefix-numeric-value arg)))
  11219. (defun org-timestamp-down (&optional arg)
  11220. "Decrease the date item at the cursor by one.
  11221. If the cursor is on the year, change the year. If it is on the month or
  11222. the day, change that.
  11223. With prefix ARG, change by that many units."
  11224. (interactive "p")
  11225. (org-timestamp-change (- (prefix-numeric-value arg))))
  11226. (defun org-timestamp-up-day (&optional arg)
  11227. "Increase the date in the time stamp by one day.
  11228. With prefix ARG, change that many days."
  11229. (interactive "p")
  11230. (if (and (not (org-at-timestamp-p t))
  11231. (org-on-heading-p))
  11232. (org-todo 'up)
  11233. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11234. (defun org-timestamp-down-day (&optional arg)
  11235. "Decrease the date in the time stamp by one day.
  11236. With prefix ARG, change that many days."
  11237. (interactive "p")
  11238. (if (and (not (org-at-timestamp-p t))
  11239. (org-on-heading-p))
  11240. (org-todo 'down)
  11241. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11242. (defun org-at-timestamp-p (&optional inactive-ok)
  11243. "Determine if the cursor is in or at a timestamp."
  11244. (interactive)
  11245. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11246. (pos (point))
  11247. (ans (or (looking-at tsr)
  11248. (save-excursion
  11249. (skip-chars-backward "^[<\n\r\t")
  11250. (if (> (point) (point-min)) (backward-char 1))
  11251. (and (looking-at tsr)
  11252. (> (- (match-end 0) pos) -1))))))
  11253. (and ans
  11254. (boundp 'org-ts-what)
  11255. (setq org-ts-what
  11256. (cond
  11257. ((= pos (match-beginning 0)) 'bracket)
  11258. ((= pos (1- (match-end 0))) 'bracket)
  11259. ((org-pos-in-match-range pos 2) 'year)
  11260. ((org-pos-in-match-range pos 3) 'month)
  11261. ((org-pos-in-match-range pos 7) 'hour)
  11262. ((org-pos-in-match-range pos 8) 'minute)
  11263. ((or (org-pos-in-match-range pos 4)
  11264. (org-pos-in-match-range pos 5)) 'day)
  11265. ((and (> pos (or (match-end 8) (match-end 5)))
  11266. (< pos (match-end 0)))
  11267. (- pos (or (match-end 8) (match-end 5))))
  11268. (t 'day))))
  11269. ans))
  11270. (defun org-toggle-timestamp-type ()
  11271. "Toggle the type (<active> or [inactive]) of a time stamp."
  11272. (interactive)
  11273. (when (org-at-timestamp-p t)
  11274. (let ((beg (match-beginning 0)) (end (match-end 0))
  11275. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11276. (save-excursion
  11277. (goto-char beg)
  11278. (while (re-search-forward "[][<>]" end t)
  11279. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11280. t t)))
  11281. (message "Timestamp is now %sactive"
  11282. (if (equal (char-after beg) ?<) "" "in")))))
  11283. (defun org-timestamp-change (n &optional what)
  11284. "Change the date in the time stamp at point.
  11285. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11286. `year', `minute', `second'. If WHAT is not given, the cursor position
  11287. in the timestamp determines what will be changed."
  11288. (let ((pos (point))
  11289. with-hm inactive
  11290. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11291. org-ts-what
  11292. extra rem
  11293. ts time time0)
  11294. (if (not (org-at-timestamp-p t))
  11295. (error "Not at a timestamp"))
  11296. (if (and (not what) (eq org-ts-what 'bracket))
  11297. (org-toggle-timestamp-type)
  11298. (if (and (not what) (not (eq org-ts-what 'day))
  11299. org-display-custom-times
  11300. (get-text-property (point) 'display)
  11301. (not (get-text-property (1- (point)) 'display)))
  11302. (setq org-ts-what 'day))
  11303. (setq org-ts-what (or what org-ts-what)
  11304. inactive (= (char-after (match-beginning 0)) ?\[)
  11305. ts (match-string 0))
  11306. (replace-match "")
  11307. (if (string-match
  11308. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11309. ts)
  11310. (setq extra (match-string 1 ts)))
  11311. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11312. (setq with-hm t))
  11313. (setq time0 (org-parse-time-string ts))
  11314. (when (and (eq org-ts-what 'minute)
  11315. (eq current-prefix-arg nil))
  11316. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11317. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11318. (setcar (cdr time0) (+ (nth 1 time0)
  11319. (if (> n 0) (- rem) (- dm rem))))))
  11320. (setq time
  11321. (encode-time (or (car time0) 0)
  11322. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11323. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11324. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11325. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11326. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11327. (nthcdr 6 time0)))
  11328. (when (integerp org-ts-what)
  11329. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11330. (if (eq what 'calendar)
  11331. (let ((cal-date (org-get-date-from-calendar)))
  11332. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11333. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11334. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11335. (setcar time0 (or (car time0) 0))
  11336. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11337. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11338. (setq time (apply 'encode-time time0))))
  11339. (setq org-last-changed-timestamp
  11340. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11341. (org-clock-update-time-maybe)
  11342. (goto-char pos)
  11343. ;; Try to recenter the calendar window, if any
  11344. (if (and org-calendar-follow-timestamp-change
  11345. (get-buffer-window "*Calendar*" t)
  11346. (memq org-ts-what '(day month year)))
  11347. (org-recenter-calendar (time-to-days time))))))
  11348. (defun org-modify-ts-extra (s pos n dm)
  11349. "Change the different parts of the lead-time and repeat fields in timestamp."
  11350. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11351. ng h m new rem)
  11352. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11353. (cond
  11354. ((or (org-pos-in-match-range pos 2)
  11355. (org-pos-in-match-range pos 3))
  11356. (setq m (string-to-number (match-string 3 s))
  11357. h (string-to-number (match-string 2 s)))
  11358. (if (org-pos-in-match-range pos 2)
  11359. (setq h (+ h n))
  11360. (setq n (* dm (org-no-warnings (signum n))))
  11361. (when (not (= 0 (setq rem (% m dm))))
  11362. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11363. (setq m (+ m n)))
  11364. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11365. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11366. (setq h (min 24 (max 0 h)))
  11367. (setq ng 1 new (format "-%02d:%02d" h m)))
  11368. ((org-pos-in-match-range pos 6)
  11369. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11370. ((org-pos-in-match-range pos 5)
  11371. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11372. ((org-pos-in-match-range pos 9)
  11373. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11374. ((org-pos-in-match-range pos 8)
  11375. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11376. (when ng
  11377. (setq s (concat
  11378. (substring s 0 (match-beginning ng))
  11379. new
  11380. (substring s (match-end ng))))))
  11381. s))
  11382. (defun org-recenter-calendar (date)
  11383. "If the calendar is visible, recenter it to DATE."
  11384. (let* ((win (selected-window))
  11385. (cwin (get-buffer-window "*Calendar*" t))
  11386. (calendar-move-hook nil))
  11387. (when cwin
  11388. (select-window cwin)
  11389. (calendar-goto-date (if (listp date) date
  11390. (calendar-gregorian-from-absolute date)))
  11391. (select-window win))))
  11392. (defun org-goto-calendar (&optional arg)
  11393. "Go to the Emacs calendar at the current date.
  11394. If there is a time stamp in the current line, go to that date.
  11395. A prefix ARG can be used to force the current date."
  11396. (interactive "P")
  11397. (let ((tsr org-ts-regexp) diff
  11398. (calendar-move-hook nil)
  11399. (calendar-view-holidays-initially-flag nil)
  11400. (view-calendar-holidays-initially nil)
  11401. (calendar-view-diary-initially-flag nil)
  11402. (view-diary-entries-initially nil))
  11403. (if (or (org-at-timestamp-p)
  11404. (save-excursion
  11405. (beginning-of-line 1)
  11406. (looking-at (concat ".*" tsr))))
  11407. (let ((d1 (time-to-days (current-time)))
  11408. (d2 (time-to-days
  11409. (org-time-string-to-time (match-string 1)))))
  11410. (setq diff (- d2 d1))))
  11411. (calendar)
  11412. (calendar-goto-today)
  11413. (if (and diff (not arg)) (calendar-forward-day diff))))
  11414. (defun org-get-date-from-calendar ()
  11415. "Return a list (month day year) of date at point in calendar."
  11416. (with-current-buffer "*Calendar*"
  11417. (save-match-data
  11418. (calendar-cursor-to-date))))
  11419. (defun org-date-from-calendar ()
  11420. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11421. If there is already a time stamp at the cursor position, update it."
  11422. (interactive)
  11423. (if (org-at-timestamp-p t)
  11424. (org-timestamp-change 0 'calendar)
  11425. (let ((cal-date (org-get-date-from-calendar)))
  11426. (org-insert-time-stamp
  11427. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11428. (defun org-minutes-to-hh:mm-string (m)
  11429. "Compute H:MM from a number of minutes."
  11430. (let ((h (/ m 60)))
  11431. (setq m (- m (* 60 h)))
  11432. (format org-time-clocksum-format h m)))
  11433. (defun org-hh:mm-string-to-minutes (s)
  11434. "Convert a string H:MM to a number of minutes."
  11435. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11436. (+ (* (string-to-number (match-string 1 s)) 60)
  11437. (string-to-number (match-string 2 s)))
  11438. 0))
  11439. ;;;; Files
  11440. (defun org-save-all-org-buffers ()
  11441. "Save all Org-mode buffers without user confirmation."
  11442. (interactive)
  11443. (message "Saving all Org-mode buffers...")
  11444. (save-some-buffers t 'org-mode-p)
  11445. (when (featurep 'org-id) (org-id-locations-save))
  11446. (message "Saving all Org-mode buffers... done"))
  11447. (defun org-revert-all-org-buffers ()
  11448. "Revert all Org-mode buffers.
  11449. Prompt for confirmation when there are unsaved changes.
  11450. Be sure you know what you are doing before letting this function
  11451. overwrite your changes.
  11452. This function is useful in a setup where one tracks org files
  11453. with a version control system, to revert on one machine after pulling
  11454. changes from another. I believe the procedure must be like this:
  11455. 1. M-x org-save-all-org-buffers
  11456. 2. Pull changes from the other machine, resolve conflicts
  11457. 3. M-x org-revert-all-org-buffers"
  11458. (interactive)
  11459. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11460. (error "Abort"))
  11461. (save-excursion
  11462. (save-window-excursion
  11463. (mapc
  11464. (lambda (b)
  11465. (when (and (with-current-buffer b (org-mode-p))
  11466. (with-current-buffer b buffer-file-name))
  11467. (switch-to-buffer b)
  11468. (revert-buffer t 'no-confirm)))
  11469. (buffer-list))
  11470. (when (and (featurep 'org-id) org-id-track-globally)
  11471. (org-id-locations-load)))))
  11472. ;;;; Agenda files
  11473. ;;;###autoload
  11474. (defun org-iswitchb (&optional arg)
  11475. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11476. With a prefix argument, restrict available to files.
  11477. With two prefix arguments, restrict available buffers to agenda files.
  11478. Due to some yet unresolved reason, the global function
  11479. `iswitchb-mode' needs to be active for this function to work."
  11480. (interactive "P")
  11481. (require 'iswitchb)
  11482. (let ((enabled iswitchb-mode) blist)
  11483. (or enabled (iswitchb-mode 1))
  11484. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11485. ((equal arg '(16)) (org-buffer-list 'agenda))
  11486. (t (org-buffer-list))))
  11487. (unwind-protect
  11488. (let ((iswitchb-make-buflist-hook
  11489. (lambda ()
  11490. (setq iswitchb-temp-buflist
  11491. (mapcar 'buffer-name blist)))))
  11492. (switch-to-buffer
  11493. (iswitchb-read-buffer
  11494. "Switch-to: " nil t))
  11495. (or enabled (iswitchb-mode -1))))))
  11496. ;;;###autoload
  11497. (defun org-ido-switchb (&optional arg)
  11498. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11499. With a prefix argument, restrict available to files.
  11500. With two prefix arguments, restrict available buffers to agenda files."
  11501. (interactive "P")
  11502. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11503. ((equal arg '(16)) (org-buffer-list 'agenda))
  11504. (t (org-buffer-list)))))
  11505. (switch-to-buffer
  11506. (org-ido-completing-read "Org buffer: "
  11507. (mapcar 'buffer-name blist)
  11508. nil t))))
  11509. (defun org-buffer-list (&optional predicate exclude-tmp)
  11510. "Return a list of Org buffers.
  11511. PREDICATE can be `export', `files' or `agenda'.
  11512. export restrict the list to Export buffers.
  11513. files restrict the list to buffers visiting Org files.
  11514. agenda restrict the list to buffers visiting agenda files.
  11515. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11516. (let* ((bfn nil)
  11517. (agenda-files (and (eq predicate 'agenda)
  11518. (mapcar 'file-truename (org-agenda-files t))))
  11519. (filter
  11520. (cond
  11521. ((eq predicate 'files)
  11522. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11523. ((eq predicate 'export)
  11524. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11525. ((eq predicate 'agenda)
  11526. (lambda (b)
  11527. (with-current-buffer b
  11528. (and (eq major-mode 'org-mode)
  11529. (setq bfn (buffer-file-name b))
  11530. (member (file-truename bfn) agenda-files)))))
  11531. (t (lambda (b) (with-current-buffer b
  11532. (or (eq major-mode 'org-mode)
  11533. (string-match "\*Org .*Export"
  11534. (buffer-name b)))))))))
  11535. (delq nil
  11536. (mapcar
  11537. (lambda(b)
  11538. (if (and (funcall filter b)
  11539. (or (not exclude-tmp)
  11540. (not (string-match "tmp" (buffer-name b)))))
  11541. b
  11542. nil))
  11543. (buffer-list)))))
  11544. (defun org-agenda-files (&optional unrestricted archives)
  11545. "Get the list of agenda files.
  11546. Optional UNRESTRICTED means return the full list even if a restriction
  11547. is currently in place.
  11548. When ARCHIVES is t, include all archive files hat are really being
  11549. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11550. `org-agenda-archives-mode' is t."
  11551. (let ((files
  11552. (cond
  11553. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11554. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11555. ((listp org-agenda-files) org-agenda-files)
  11556. (t (error "Invalid value of `org-agenda-files'")))))
  11557. (setq files (apply 'append
  11558. (mapcar (lambda (f)
  11559. (if (file-directory-p f)
  11560. (directory-files
  11561. f t org-agenda-file-regexp)
  11562. (list f)))
  11563. files)))
  11564. (when org-agenda-skip-unavailable-files
  11565. (setq files (delq nil
  11566. (mapcar (function
  11567. (lambda (file)
  11568. (and (file-readable-p file) file)))
  11569. files))))
  11570. (when (or (eq archives t)
  11571. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11572. (setq files (org-add-archive-files files)))
  11573. files))
  11574. (defun org-edit-agenda-file-list ()
  11575. "Edit the list of agenda files.
  11576. Depending on setup, this either uses customize to edit the variable
  11577. `org-agenda-files', or it visits the file that is holding the list. In the
  11578. latter case, the buffer is set up in a way that saving it automatically kills
  11579. the buffer and restores the previous window configuration."
  11580. (interactive)
  11581. (if (stringp org-agenda-files)
  11582. (let ((cw (current-window-configuration)))
  11583. (find-file org-agenda-files)
  11584. (org-set-local 'org-window-configuration cw)
  11585. (org-add-hook 'after-save-hook
  11586. (lambda ()
  11587. (set-window-configuration
  11588. (prog1 org-window-configuration
  11589. (kill-buffer (current-buffer))))
  11590. (org-install-agenda-files-menu)
  11591. (message "New agenda file list installed"))
  11592. nil 'local)
  11593. (message "%s" (substitute-command-keys
  11594. "Edit list and finish with \\[save-buffer]")))
  11595. (customize-variable 'org-agenda-files)))
  11596. (defun org-store-new-agenda-file-list (list)
  11597. "Set new value for the agenda file list and save it correctly."
  11598. (if (stringp org-agenda-files)
  11599. (let ((f org-agenda-files) b)
  11600. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11601. (with-temp-file f
  11602. (insert (mapconcat 'identity list "\n") "\n")))
  11603. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11604. (setq org-agenda-files list)
  11605. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11606. (defun org-read-agenda-file-list ()
  11607. "Read the list of agenda files from a file."
  11608. (when (file-directory-p org-agenda-files)
  11609. (error "`org-agenda-files' cannot be a single directory"))
  11610. (when (stringp org-agenda-files)
  11611. (with-temp-buffer
  11612. (insert-file-contents org-agenda-files)
  11613. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11614. ;;;###autoload
  11615. (defun org-cycle-agenda-files ()
  11616. "Cycle through the files in `org-agenda-files'.
  11617. If the current buffer visits an agenda file, find the next one in the list.
  11618. If the current buffer does not, find the first agenda file."
  11619. (interactive)
  11620. (let* ((fs (org-agenda-files t))
  11621. (files (append fs (list (car fs))))
  11622. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11623. file)
  11624. (unless files (error "No agenda files"))
  11625. (catch 'exit
  11626. (while (setq file (pop files))
  11627. (if (equal (file-truename file) tcf)
  11628. (when (car files)
  11629. (find-file (car files))
  11630. (throw 'exit t))))
  11631. (find-file (car fs)))
  11632. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11633. (defun org-agenda-file-to-front (&optional to-end)
  11634. "Move/add the current file to the top of the agenda file list.
  11635. If the file is not present in the list, it is added to the front. If it is
  11636. present, it is moved there. With optional argument TO-END, add/move to the
  11637. end of the list."
  11638. (interactive "P")
  11639. (let ((org-agenda-skip-unavailable-files nil)
  11640. (file-alist (mapcar (lambda (x)
  11641. (cons (file-truename x) x))
  11642. (org-agenda-files t)))
  11643. (ctf (file-truename buffer-file-name))
  11644. x had)
  11645. (setq x (assoc ctf file-alist) had x)
  11646. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11647. (if to-end
  11648. (setq file-alist (append (delq x file-alist) (list x)))
  11649. (setq file-alist (cons x (delq x file-alist))))
  11650. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11651. (org-install-agenda-files-menu)
  11652. (message "File %s to %s of agenda file list"
  11653. (if had "moved" "added") (if to-end "end" "front"))))
  11654. (defun org-remove-file (&optional file)
  11655. "Remove current file from the list of files in variable `org-agenda-files'.
  11656. These are the files which are being checked for agenda entries.
  11657. Optional argument FILE means, use this file instead of the current."
  11658. (interactive)
  11659. (let* ((org-agenda-skip-unavailable-files nil)
  11660. (file (or file buffer-file-name))
  11661. (true-file (file-truename file))
  11662. (afile (abbreviate-file-name file))
  11663. (files (delq nil (mapcar
  11664. (lambda (x)
  11665. (if (equal true-file
  11666. (file-truename x))
  11667. nil x))
  11668. (org-agenda-files t)))))
  11669. (if (not (= (length files) (length (org-agenda-files t))))
  11670. (progn
  11671. (org-store-new-agenda-file-list files)
  11672. (org-install-agenda-files-menu)
  11673. (message "Removed file: %s" afile))
  11674. (message "File was not in list: %s (not removed)" afile))))
  11675. (defun org-file-menu-entry (file)
  11676. (vector file (list 'find-file file) t))
  11677. (defun org-check-agenda-file (file)
  11678. "Make sure FILE exists. If not, ask user what to do."
  11679. (when (not (file-exists-p file))
  11680. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11681. (abbreviate-file-name file))
  11682. (let ((r (downcase (read-char-exclusive))))
  11683. (cond
  11684. ((equal r ?r)
  11685. (org-remove-file file)
  11686. (throw 'nextfile t))
  11687. (t (error "Abort"))))))
  11688. (defun org-get-agenda-file-buffer (file)
  11689. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11690. it to the list of buffers which might be released later."
  11691. (let ((buf (org-find-base-buffer-visiting file)))
  11692. (if buf
  11693. buf ; just return it
  11694. ;; Make a new buffer and remember it
  11695. (setq buf (find-file-noselect file))
  11696. (if buf (push buf org-agenda-new-buffers))
  11697. buf)))
  11698. (defun org-release-buffers (blist)
  11699. "Release all buffers in list, asking the user for confirmation when needed.
  11700. When a buffer is unmodified, it is just killed. When modified, it is saved
  11701. \(if the user agrees) and then killed."
  11702. (let (buf file)
  11703. (while (setq buf (pop blist))
  11704. (setq file (buffer-file-name buf))
  11705. (when (and (buffer-modified-p buf)
  11706. file
  11707. (y-or-n-p (format "Save file %s? " file)))
  11708. (with-current-buffer buf (save-buffer)))
  11709. (kill-buffer buf))))
  11710. (defun org-prepare-agenda-buffers (files)
  11711. "Create buffers for all agenda files, protect archived trees and comments."
  11712. (interactive)
  11713. (let ((pa '(:org-archived t))
  11714. (pc '(:org-comment t))
  11715. (pall '(:org-archived t :org-comment t))
  11716. (inhibit-read-only t)
  11717. (rea (concat ":" org-archive-tag ":"))
  11718. bmp file re)
  11719. (save-excursion
  11720. (save-restriction
  11721. (while (setq file (pop files))
  11722. (if (bufferp file)
  11723. (set-buffer file)
  11724. (org-check-agenda-file file)
  11725. (set-buffer (org-get-agenda-file-buffer file)))
  11726. (widen)
  11727. (setq bmp (buffer-modified-p))
  11728. (org-refresh-category-properties)
  11729. (setq org-todo-keywords-for-agenda
  11730. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11731. (setq org-done-keywords-for-agenda
  11732. (append org-done-keywords-for-agenda org-done-keywords))
  11733. (setq org-todo-keyword-alist-for-agenda
  11734. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11735. (setq org-tag-alist-for-agenda
  11736. (append org-tag-alist-for-agenda org-tag-alist))
  11737. (save-excursion
  11738. (remove-text-properties (point-min) (point-max) pall)
  11739. (when org-agenda-skip-archived-trees
  11740. (goto-char (point-min))
  11741. (while (re-search-forward rea nil t)
  11742. (if (org-on-heading-p t)
  11743. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11744. (goto-char (point-min))
  11745. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11746. (while (re-search-forward re nil t)
  11747. (add-text-properties
  11748. (match-beginning 0) (org-end-of-subtree t) pc)))
  11749. (set-buffer-modified-p bmp))))
  11750. (setq org-todo-keyword-alist-for-agenda
  11751. (org-uniquify org-todo-keyword-alist-for-agenda)
  11752. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11753. ;;;; Embedded LaTeX
  11754. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11755. "Keymap for the minor `org-cdlatex-mode'.")
  11756. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11757. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11758. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11759. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11760. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11761. (defvar org-cdlatex-texmathp-advice-is-done nil
  11762. "Flag remembering if we have applied the advice to texmathp already.")
  11763. (define-minor-mode org-cdlatex-mode
  11764. "Toggle the minor `org-cdlatex-mode'.
  11765. This mode supports entering LaTeX environment and math in LaTeX fragments
  11766. in Org-mode.
  11767. \\{org-cdlatex-mode-map}"
  11768. nil " OCDL" nil
  11769. (when org-cdlatex-mode (require 'cdlatex))
  11770. (unless org-cdlatex-texmathp-advice-is-done
  11771. (setq org-cdlatex-texmathp-advice-is-done t)
  11772. (defadvice texmathp (around org-math-always-on activate)
  11773. "Always return t in org-mode buffers.
  11774. This is because we want to insert math symbols without dollars even outside
  11775. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11776. an embedded LaTeX fragment, let texmathp do its job.
  11777. \\[org-cdlatex-mode-map]"
  11778. (interactive)
  11779. (let (p)
  11780. (cond
  11781. ((not (org-mode-p)) ad-do-it)
  11782. ((eq this-command 'cdlatex-math-symbol)
  11783. (setq ad-return-value t
  11784. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11785. (t
  11786. (let ((p (org-inside-LaTeX-fragment-p)))
  11787. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11788. (setq ad-return-value t
  11789. texmathp-why '("Org-mode embedded math" . 0))
  11790. (if p ad-do-it)))))))))
  11791. (defun turn-on-org-cdlatex ()
  11792. "Unconditionally turn on `org-cdlatex-mode'."
  11793. (org-cdlatex-mode 1))
  11794. (defun org-inside-LaTeX-fragment-p ()
  11795. "Test if point is inside a LaTeX fragment.
  11796. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11797. sequence appearing also before point.
  11798. Even though the matchers for math are configurable, this function assumes
  11799. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11800. delimiters are skipped when they have been removed by customization.
  11801. The return value is nil, or a cons cell with the delimiter and
  11802. and the position of this delimiter.
  11803. This function does a reasonably good job, but can locally be fooled by
  11804. for example currency specifications. For example it will assume being in
  11805. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11806. fragments that are properly closed, but during editing, we have to live
  11807. with the uncertainty caused by missing closing delimiters. This function
  11808. looks only before point, not after."
  11809. (catch 'exit
  11810. (let ((pos (point))
  11811. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11812. (lim (progn
  11813. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11814. (point)))
  11815. dd-on str (start 0) m re)
  11816. (goto-char pos)
  11817. (when dodollar
  11818. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11819. re (nth 1 (assoc "$" org-latex-regexps)))
  11820. (while (string-match re str start)
  11821. (cond
  11822. ((= (match-end 0) (length str))
  11823. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11824. ((= (match-end 0) (- (length str) 5))
  11825. (throw 'exit nil))
  11826. (t (setq start (match-end 0))))))
  11827. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11828. (goto-char pos)
  11829. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11830. (and (match-beginning 2) (throw 'exit nil))
  11831. ;; count $$
  11832. (while (re-search-backward "\\$\\$" lim t)
  11833. (setq dd-on (not dd-on)))
  11834. (goto-char pos)
  11835. (if dd-on (cons "$$" m))))))
  11836. (defun org-try-cdlatex-tab ()
  11837. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11838. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11839. - inside a LaTeX fragment, or
  11840. - after the first word in a line, where an abbreviation expansion could
  11841. insert a LaTeX environment."
  11842. (when org-cdlatex-mode
  11843. (cond
  11844. ((save-excursion
  11845. (skip-chars-backward "a-zA-Z0-9*")
  11846. (skip-chars-backward " \t")
  11847. (bolp))
  11848. (cdlatex-tab) t)
  11849. ((org-inside-LaTeX-fragment-p)
  11850. (cdlatex-tab) t)
  11851. (t nil))))
  11852. (defun org-cdlatex-underscore-caret (&optional arg)
  11853. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11854. Revert to the normal definition outside of these fragments."
  11855. (interactive "P")
  11856. (if (org-inside-LaTeX-fragment-p)
  11857. (call-interactively 'cdlatex-sub-superscript)
  11858. (let (org-cdlatex-mode)
  11859. (call-interactively (key-binding (vector last-input-event))))))
  11860. (defun org-cdlatex-math-modify (&optional arg)
  11861. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11862. Revert to the normal definition outside of these fragments."
  11863. (interactive "P")
  11864. (if (org-inside-LaTeX-fragment-p)
  11865. (call-interactively 'cdlatex-math-modify)
  11866. (let (org-cdlatex-mode)
  11867. (call-interactively (key-binding (vector last-input-event))))))
  11868. (defvar org-latex-fragment-image-overlays nil
  11869. "List of overlays carrying the images of latex fragments.")
  11870. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11871. (defun org-remove-latex-fragment-image-overlays ()
  11872. "Remove all overlays with LaTeX fragment images in current buffer."
  11873. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11874. (setq org-latex-fragment-image-overlays nil))
  11875. (defun org-preview-latex-fragment (&optional subtree)
  11876. "Preview the LaTeX fragment at point, or all locally or globally.
  11877. If the cursor is in a LaTeX fragment, create the image and overlay
  11878. it over the source code. If there is no fragment at point, display
  11879. all fragments in the current text, from one headline to the next. With
  11880. prefix SUBTREE, display all fragments in the current subtree. With a
  11881. double prefix `C-u C-u', or when the cursor is before the first headline,
  11882. display all fragments in the buffer.
  11883. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11884. (interactive "P")
  11885. (org-remove-latex-fragment-image-overlays)
  11886. (save-excursion
  11887. (save-restriction
  11888. (let (beg end at msg)
  11889. (cond
  11890. ((or (equal subtree '(16))
  11891. (not (save-excursion
  11892. (re-search-backward (concat "^" outline-regexp) nil t))))
  11893. (setq beg (point-min) end (point-max)
  11894. msg "Creating images for buffer...%s"))
  11895. ((equal subtree '(4))
  11896. (org-back-to-heading)
  11897. (setq beg (point) end (org-end-of-subtree t)
  11898. msg "Creating images for subtree...%s"))
  11899. (t
  11900. (if (setq at (org-inside-LaTeX-fragment-p))
  11901. (goto-char (max (point-min) (- (cdr at) 2)))
  11902. (org-back-to-heading))
  11903. (setq beg (point) end (progn (outline-next-heading) (point))
  11904. msg (if at "Creating image...%s"
  11905. "Creating images for entry...%s"))))
  11906. (message msg "")
  11907. (narrow-to-region beg end)
  11908. (goto-char beg)
  11909. (org-format-latex
  11910. (concat "ltxpng/" (file-name-sans-extension
  11911. (file-name-nondirectory
  11912. buffer-file-name)))
  11913. default-directory 'overlays msg at 'forbuffer)
  11914. (message msg "done. Use `C-c C-c' to remove images.")))))
  11915. (defvar org-latex-regexps
  11916. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11917. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11918. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11919. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11920. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11921. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11922. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11923. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11924. "Regular expressions for matching embedded LaTeX.")
  11925. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11926. "Replace LaTeX fragments with links to an image, and produce images."
  11927. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11928. (let* ((prefixnodir (file-name-nondirectory prefix))
  11929. (absprefix (expand-file-name prefix dir))
  11930. (todir (file-name-directory absprefix))
  11931. (opt org-format-latex-options)
  11932. (matchers (plist-get opt :matchers))
  11933. (re-list org-latex-regexps)
  11934. (cnt 0) txt link beg end re e checkdir
  11935. m n block linkfile movefile ov)
  11936. ;; Check if there are old images files with this prefix, and remove them
  11937. (when (file-directory-p todir)
  11938. (mapc 'delete-file
  11939. (directory-files
  11940. todir 'full
  11941. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11942. ;; Check the different regular expressions
  11943. (while (setq e (pop re-list))
  11944. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11945. block (if (nth 3 e) "\n\n" ""))
  11946. (when (member m matchers)
  11947. (goto-char (point-min))
  11948. (while (re-search-forward re nil t)
  11949. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  11950. (not (get-text-property (match-beginning n)
  11951. 'org-protected)))
  11952. (setq txt (match-string n)
  11953. beg (match-beginning n) end (match-end n)
  11954. cnt (1+ cnt)
  11955. linkfile (format "%s_%04d.png" prefix cnt)
  11956. movefile (format "%s_%04d.png" absprefix cnt)
  11957. link (concat block "[[file:" linkfile "]]" block))
  11958. (if msg (message msg cnt))
  11959. (goto-char beg)
  11960. (unless checkdir ; make sure the directory exists
  11961. (setq checkdir t)
  11962. (or (file-directory-p todir) (make-directory todir)))
  11963. (org-create-formula-image
  11964. txt movefile opt forbuffer)
  11965. (if overlays
  11966. (progn
  11967. (setq ov (org-make-overlay beg end))
  11968. (if (featurep 'xemacs)
  11969. (progn
  11970. (org-overlay-put ov 'invisible t)
  11971. (org-overlay-put
  11972. ov 'end-glyph
  11973. (make-glyph (vector 'png :file movefile))))
  11974. (org-overlay-put
  11975. ov 'display
  11976. (list 'image :type 'png :file movefile :ascent 'center)))
  11977. (push ov org-latex-fragment-image-overlays)
  11978. (goto-char end))
  11979. (delete-region beg end)
  11980. (insert link))))))))
  11981. ;; This function borrows from Ganesh Swami's latex2png.el
  11982. (defun org-create-formula-image (string tofile options buffer)
  11983. (let* ((tmpdir (if (featurep 'xemacs)
  11984. (temp-directory)
  11985. temporary-file-directory))
  11986. (texfilebase (make-temp-name
  11987. (expand-file-name "orgtex" tmpdir)))
  11988. (texfile (concat texfilebase ".tex"))
  11989. (dvifile (concat texfilebase ".dvi"))
  11990. (pngfile (concat texfilebase ".png"))
  11991. (fnh (if (featurep 'xemacs)
  11992. (font-height (get-face-font 'default))
  11993. (face-attribute 'default :height nil)))
  11994. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11995. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11996. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11997. "Black"))
  11998. (bg (or (plist-get options (if buffer :background :html-background))
  11999. "Transparent")))
  12000. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12001. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12002. (with-temp-file texfile
  12003. (insert org-format-latex-header
  12004. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12005. (let ((dir default-directory))
  12006. (condition-case nil
  12007. (progn
  12008. (cd tmpdir)
  12009. (call-process "latex" nil nil nil texfile))
  12010. (error nil))
  12011. (cd dir))
  12012. (if (not (file-exists-p dvifile))
  12013. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12014. (condition-case nil
  12015. (call-process "dvipng" nil nil nil
  12016. "-E" "-fg" fg "-bg" bg
  12017. "-D" dpi
  12018. ;;"-x" scale "-y" scale
  12019. "-T" "tight"
  12020. "-o" pngfile
  12021. dvifile)
  12022. (error nil))
  12023. (if (not (file-exists-p pngfile))
  12024. (progn (message "Failed to create png file from %s" texfile) nil)
  12025. ;; Use the requested file name and clean up
  12026. (copy-file pngfile tofile 'replace)
  12027. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12028. (delete-file (concat texfilebase e)))
  12029. pngfile))))
  12030. (defun org-dvipng-color (attr)
  12031. "Return an rgb color specification for dvipng."
  12032. (apply 'format "rgb %s %s %s"
  12033. (mapcar 'org-normalize-color
  12034. (color-values (face-attribute 'default attr nil)))))
  12035. (defun org-normalize-color (value)
  12036. "Return string to be used as color value for an RGB component."
  12037. (format "%g" (/ value 65535.0)))
  12038. ;;;; Key bindings
  12039. ;; Make `C-c C-x' a prefix key
  12040. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12041. ;; TAB key with modifiers
  12042. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12043. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12044. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12045. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12046. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12047. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12048. ;; The following line is necessary under Suse GNU/Linux
  12049. (unless (featurep 'xemacs)
  12050. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12051. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12052. (define-key org-mode-map [backtab] 'org-shifttab)
  12053. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12054. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12055. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12056. ;; Cursor keys with modifiers
  12057. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12058. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12059. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12060. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12061. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12062. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12063. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12064. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12065. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12066. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12067. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12068. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12069. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12070. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12071. ;;; Extra keys for tty access.
  12072. ;; We only set them when really needed because otherwise the
  12073. ;; menus don't show the simple keys
  12074. (when (or org-use-extra-keys
  12075. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12076. (not window-system))
  12077. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12078. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12079. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12080. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12081. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12082. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12083. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12084. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12085. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12086. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12087. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12088. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12089. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12090. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12091. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12092. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12093. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12094. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12095. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12096. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12097. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12098. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12099. ;; All the other keys
  12100. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12101. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12102. (if (boundp 'narrow-map)
  12103. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12104. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12105. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12106. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12107. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12108. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12109. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12110. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12111. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12112. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12113. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12114. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12115. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12116. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12117. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12118. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12119. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12120. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12121. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12122. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12123. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12124. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12125. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12126. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12127. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12128. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12129. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12130. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12131. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12132. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12133. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12134. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12135. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12136. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12137. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12138. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12139. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12140. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12141. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12142. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12143. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12144. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12145. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12146. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12147. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12148. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12149. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12150. (org-defkey org-mode-map "\C-m" 'org-return)
  12151. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12152. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12153. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12154. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12155. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12156. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12157. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12158. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12159. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12160. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12161. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12162. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12163. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12164. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12165. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12166. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12167. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12168. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12169. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12170. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12171. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12172. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12173. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12174. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12175. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12176. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12177. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12178. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12179. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12180. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12181. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12182. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12183. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12184. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12185. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12186. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12187. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12188. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12189. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12190. (define-key org-mode-map "\C-c\C-xr" 'org-reload)
  12191. (when (featurep 'xemacs)
  12192. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12193. (defvar org-self-insert-command-undo-counter 0)
  12194. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12195. (defun org-self-insert-command (N)
  12196. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12197. If the cursor is in a table looking at whitespace, the whitespace is
  12198. overwritten, and the table is not marked as requiring realignment."
  12199. (interactive "p")
  12200. (if (and
  12201. (org-table-p)
  12202. (progn
  12203. ;; check if we blank the field, and if that triggers align
  12204. (and (featurep 'org-table) org-table-auto-blank-field
  12205. (member last-command
  12206. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12207. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12208. ;; got extra space, this field does not determine column width
  12209. (let (org-table-may-need-update) (org-table-blank-field))
  12210. ;; no extra space, this field may determine column width
  12211. (org-table-blank-field)))
  12212. t)
  12213. (eq N 1)
  12214. (looking-at "[^|\n]* |"))
  12215. (let (org-table-may-need-update)
  12216. (goto-char (1- (match-end 0)))
  12217. (delete-backward-char 1)
  12218. (goto-char (match-beginning 0))
  12219. (self-insert-command N))
  12220. (setq org-table-may-need-update t)
  12221. (self-insert-command N)
  12222. (org-fix-tags-on-the-fly)
  12223. (if org-self-insert-cluster-for-undo
  12224. (if (not (eq last-command 'org-self-insert-command))
  12225. (setq org-self-insert-command-undo-counter 1)
  12226. (if (>= org-self-insert-command-undo-counter 20)
  12227. (setq org-self-insert-command-undo-counter 1)
  12228. (and (> org-self-insert-command-undo-counter 0)
  12229. buffer-undo-list
  12230. (not (cadr buffer-undo-list)) ; remove nil entry
  12231. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12232. (setq org-self-insert-command-undo-counter
  12233. (1+ org-self-insert-command-undo-counter)))))))
  12234. (defun org-fix-tags-on-the-fly ()
  12235. (when (and (equal (char-after (point-at-bol)) ?*)
  12236. (org-on-heading-p))
  12237. (org-align-tags-here org-tags-column)))
  12238. (defun org-delete-backward-char (N)
  12239. "Like `delete-backward-char', insert whitespace at field end in tables.
  12240. When deleting backwards, in tables this function will insert whitespace in
  12241. front of the next \"|\" separator, to keep the table aligned. The table will
  12242. still be marked for re-alignment if the field did fill the entire column,
  12243. because, in this case the deletion might narrow the column."
  12244. (interactive "p")
  12245. (if (and (org-table-p)
  12246. (eq N 1)
  12247. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12248. (looking-at ".*?|"))
  12249. (let ((pos (point))
  12250. (noalign (looking-at "[^|\n\r]* |"))
  12251. (c org-table-may-need-update))
  12252. (backward-delete-char N)
  12253. (skip-chars-forward "^|")
  12254. (insert " ")
  12255. (goto-char (1- pos))
  12256. ;; noalign: if there were two spaces at the end, this field
  12257. ;; does not determine the width of the column.
  12258. (if noalign (setq org-table-may-need-update c)))
  12259. (backward-delete-char N)
  12260. (org-fix-tags-on-the-fly)))
  12261. (defun org-delete-char (N)
  12262. "Like `delete-char', but insert whitespace at field end in tables.
  12263. When deleting characters, in tables this function will insert whitespace in
  12264. front of the next \"|\" separator, to keep the table aligned. The table will
  12265. still be marked for re-alignment if the field did fill the entire column,
  12266. because, in this case the deletion might narrow the column."
  12267. (interactive "p")
  12268. (if (and (org-table-p)
  12269. (not (bolp))
  12270. (not (= (char-after) ?|))
  12271. (eq N 1))
  12272. (if (looking-at ".*?|")
  12273. (let ((pos (point))
  12274. (noalign (looking-at "[^|\n\r]* |"))
  12275. (c org-table-may-need-update))
  12276. (replace-match (concat
  12277. (substring (match-string 0) 1 -1)
  12278. " |"))
  12279. (goto-char pos)
  12280. ;; noalign: if there were two spaces at the end, this field
  12281. ;; does not determine the width of the column.
  12282. (if noalign (setq org-table-may-need-update c)))
  12283. (delete-char N))
  12284. (delete-char N)
  12285. (org-fix-tags-on-the-fly)))
  12286. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12287. (put 'org-self-insert-command 'delete-selection t)
  12288. (put 'orgtbl-self-insert-command 'delete-selection t)
  12289. (put 'org-delete-char 'delete-selection 'supersede)
  12290. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12291. (put 'org-yank 'delete-selection 'yank)
  12292. ;; Make `flyspell-mode' delay after some commands
  12293. (put 'org-self-insert-command 'flyspell-delayed t)
  12294. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12295. (put 'org-delete-char 'flyspell-delayed t)
  12296. (put 'org-delete-backward-char 'flyspell-delayed t)
  12297. ;; Make pabbrev-mode expand after org-mode commands
  12298. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12299. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12300. ;; How to do this: Measure non-white length of current string
  12301. ;; If equal to column width, we should realign.
  12302. (defun org-remap (map &rest commands)
  12303. "In MAP, remap the functions given in COMMANDS.
  12304. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12305. (let (new old)
  12306. (while commands
  12307. (setq old (pop commands) new (pop commands))
  12308. (if (fboundp 'command-remapping)
  12309. (org-defkey map (vector 'remap old) new)
  12310. (substitute-key-definition old new map global-map)))))
  12311. (when (eq org-enable-table-editor 'optimized)
  12312. ;; If the user wants maximum table support, we need to hijack
  12313. ;; some standard editing functions
  12314. (org-remap org-mode-map
  12315. 'self-insert-command 'org-self-insert-command
  12316. 'delete-char 'org-delete-char
  12317. 'delete-backward-char 'org-delete-backward-char)
  12318. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12319. (defvar org-ctrl-c-ctrl-c-hook nil
  12320. "Hook for functions attaching themselves to `C-c C-c'.
  12321. This can be used to add additional functionality to the C-c C-c key which
  12322. executes context-dependent commands.
  12323. Each function will be called with no arguments. The function must check
  12324. if the context is appropriate for it to act. If yes, it should do its
  12325. thing and then return a non-nil value. If the context is wrong,
  12326. just do nothing.")
  12327. (defvar org-metaleft-hook nil
  12328. "Hook for functions attaching themselves to `M-left'.
  12329. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12330. (defvar org-metaright-hook nil
  12331. "Hook for functions attaching themselves to `M-right'.
  12332. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12333. (defvar org-metaup-hook nil
  12334. "Hook for functions attaching themselves to `M-up'.
  12335. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12336. (defvar org-metadown-hook nil
  12337. "Hook for functions attaching themselves to `M-down'.
  12338. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12339. (defvar org-shiftmetaleft-hook nil
  12340. "Hook for functions attaching themselves to `M-S-left'.
  12341. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12342. (defvar org-shiftmetaright-hook nil
  12343. "Hook for functions attaching themselves to `M-S-right'.
  12344. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12345. (defvar org-shiftmetaup-hook nil
  12346. "Hook for functions attaching themselves to `M-S-up'.
  12347. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12348. (defvar org-shiftmetadown-hook nil
  12349. "Hook for functions attaching themselves to `M-S-down'.
  12350. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12351. (defvar org-metareturn-hook nil
  12352. "Hook for functions attaching themselves to `M-RET'.
  12353. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12354. (defun org-modifier-cursor-error ()
  12355. "Throw an error, a modified cursor command was applied in wrong context."
  12356. (error "This command is active in special context like tables, headlines or items"))
  12357. (defun org-shiftselect-error ()
  12358. "Throw an error because Shift-Cursor command was applied in wrong context."
  12359. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12360. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12361. (error "This command works only in special context like headlines or timestamps.")))
  12362. (defun org-call-for-shift-select (cmd)
  12363. (let ((this-command-keys-shift-translated t))
  12364. (call-interactively cmd)))
  12365. (defun org-shifttab (&optional arg)
  12366. "Global visibility cycling or move to previous table field.
  12367. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12368. on context.
  12369. See the individual commands for more information."
  12370. (interactive "P")
  12371. (cond
  12372. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12373. ((integerp arg)
  12374. (message "Content view to level: %d" arg)
  12375. (org-content (prefix-numeric-value arg))
  12376. (setq org-cycle-global-status 'overview))
  12377. (t (call-interactively 'org-global-cycle))))
  12378. (defun org-shiftmetaleft ()
  12379. "Promote subtree or delete table column.
  12380. Calls `org-promote-subtree', `org-outdent-item',
  12381. or `org-table-delete-column', depending on context.
  12382. See the individual commands for more information."
  12383. (interactive)
  12384. (cond
  12385. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12386. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12387. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12388. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12389. (t (org-modifier-cursor-error))))
  12390. (defun org-shiftmetaright ()
  12391. "Demote subtree or insert table column.
  12392. Calls `org-demote-subtree', `org-indent-item',
  12393. or `org-table-insert-column', depending on context.
  12394. See the individual commands for more information."
  12395. (interactive)
  12396. (cond
  12397. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12398. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12399. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12400. ((org-at-item-p) (call-interactively 'org-indent-item))
  12401. (t (org-modifier-cursor-error))))
  12402. (defun org-shiftmetaup (&optional arg)
  12403. "Move subtree up or kill table row.
  12404. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12405. `org-move-item-up' depending on context. See the individual commands
  12406. for more information."
  12407. (interactive "P")
  12408. (cond
  12409. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12410. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12411. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12412. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12413. (t (org-modifier-cursor-error))))
  12414. (defun org-shiftmetadown (&optional arg)
  12415. "Move subtree down or insert table row.
  12416. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12417. `org-move-item-down', depending on context. See the individual
  12418. commands for more information."
  12419. (interactive "P")
  12420. (cond
  12421. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12422. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12423. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12424. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12425. (t (org-modifier-cursor-error))))
  12426. (defun org-metaleft (&optional arg)
  12427. "Promote heading or move table column to left.
  12428. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12429. With no specific context, calls the Emacs default `backward-word'.
  12430. See the individual commands for more information."
  12431. (interactive "P")
  12432. (cond
  12433. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12434. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12435. ((or (org-on-heading-p)
  12436. (and (org-region-active-p)
  12437. (save-excursion
  12438. (goto-char (region-beginning))
  12439. (org-on-heading-p))))
  12440. (call-interactively 'org-do-promote))
  12441. ((or (org-at-item-p)
  12442. (and (org-region-active-p)
  12443. (save-excursion
  12444. (goto-char (region-beginning))
  12445. (org-at-item-p))))
  12446. (call-interactively 'org-outdent-item))
  12447. (t (call-interactively 'backward-word))))
  12448. (defun org-metaright (&optional arg)
  12449. "Demote subtree or move table column to right.
  12450. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12451. With no specific context, calls the Emacs default `forward-word'.
  12452. See the individual commands for more information."
  12453. (interactive "P")
  12454. (cond
  12455. ((run-hook-with-args-until-success 'org-metaright-hook))
  12456. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12457. ((or (org-on-heading-p)
  12458. (and (org-region-active-p)
  12459. (save-excursion
  12460. (goto-char (region-beginning))
  12461. (org-on-heading-p))))
  12462. (call-interactively 'org-do-demote))
  12463. ((or (org-at-item-p)
  12464. (and (org-region-active-p)
  12465. (save-excursion
  12466. (goto-char (region-beginning))
  12467. (org-at-item-p))))
  12468. (call-interactively 'org-indent-item))
  12469. (t (call-interactively 'forward-word))))
  12470. (defun org-metaup (&optional arg)
  12471. "Move subtree up or move table row up.
  12472. Calls `org-move-subtree-up' or `org-table-move-row' or
  12473. `org-move-item-up', depending on context. See the individual commands
  12474. for more information."
  12475. (interactive "P")
  12476. (cond
  12477. ((run-hook-with-args-until-success 'org-metaup-hook))
  12478. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12479. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12480. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12481. (t (transpose-lines 1) (beginning-of-line -1))))
  12482. (defun org-metadown (&optional arg)
  12483. "Move subtree down or move table row down.
  12484. Calls `org-move-subtree-down' or `org-table-move-row' or
  12485. `org-move-item-down', depending on context. See the individual
  12486. commands for more information."
  12487. (interactive "P")
  12488. (cond
  12489. ((run-hook-with-args-until-success 'org-metadown-hook))
  12490. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12491. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12492. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12493. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12494. (defun org-shiftup (&optional arg)
  12495. "Increase item in timestamp or increase priority of current headline.
  12496. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12497. depending on context. See the individual commands for more information."
  12498. (interactive "P")
  12499. (cond
  12500. ((and org-support-shift-select (org-region-active-p))
  12501. (org-call-for-shift-select 'previous-line))
  12502. ((org-at-timestamp-p t)
  12503. (call-interactively (if org-edit-timestamp-down-means-later
  12504. 'org-timestamp-down 'org-timestamp-up)))
  12505. ((and (not (eq org-support-shift-select 'always))
  12506. (org-on-heading-p))
  12507. (call-interactively 'org-priority-up))
  12508. ((and (not org-support-shift-select) (org-at-item-p))
  12509. (call-interactively 'org-previous-item))
  12510. ((org-clocktable-try-shift 'up arg))
  12511. (org-support-shift-select
  12512. (org-call-for-shift-select 'previous-line))
  12513. (t (org-shiftselect-error))))
  12514. (defun org-shiftdown (&optional arg)
  12515. "Decrease item in timestamp or decrease priority of current headline.
  12516. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12517. depending on context. See the individual commands for more information."
  12518. (interactive "P")
  12519. (cond
  12520. ((and org-support-shift-select (org-region-active-p))
  12521. (org-call-for-shift-select 'next-line))
  12522. ((org-at-timestamp-p t)
  12523. (call-interactively (if org-edit-timestamp-down-means-later
  12524. 'org-timestamp-up 'org-timestamp-down)))
  12525. ((and (not (eq org-support-shift-select 'always))
  12526. (org-on-heading-p))
  12527. (call-interactively 'org-priority-down))
  12528. ((and (not org-support-shift-select) (org-at-item-p))
  12529. (call-interactively 'org-next-item))
  12530. ((org-clocktable-try-shift 'down arg))
  12531. (org-support-shift-select
  12532. (org-call-for-shift-select 'next-line))
  12533. (t (org-shiftselect-error))))
  12534. (defun org-shiftright (&optional arg)
  12535. "Cycle the thing at point or in the current line, depending on context.
  12536. Depending on context, this does one of the following:
  12537. - switch a timestamp at point one day into the future
  12538. - on a headline, switch to the next TODO keyword.
  12539. - on an item, switch entire list to the next bullet type
  12540. - on a property line, switch to the next allowed value
  12541. - on a clocktable definition line, move time block into the future"
  12542. (interactive "P")
  12543. (cond
  12544. ((and org-support-shift-select (org-region-active-p))
  12545. (org-call-for-shift-select 'forward-char))
  12546. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12547. ((and (not (eq org-support-shift-select 'always))
  12548. (org-on-heading-p))
  12549. (org-call-with-arg 'org-todo 'right))
  12550. ((or (and org-support-shift-select
  12551. (not (eq org-support-shift-select 'always))
  12552. (org-at-item-bullet-p))
  12553. (and (not org-support-shift-select) (org-at-item-p)))
  12554. (org-call-with-arg 'org-cycle-list-bullet nil))
  12555. ((and (not (eq org-support-shift-select 'always))
  12556. (org-at-property-p))
  12557. (call-interactively 'org-property-next-allowed-value))
  12558. ((org-clocktable-try-shift 'right arg))
  12559. (org-support-shift-select
  12560. (org-call-for-shift-select 'forward-char))
  12561. (t (org-shiftselect-error))))
  12562. (defun org-shiftleft (&optional arg)
  12563. "Cycle the thing at point or in the current line, depending on context.
  12564. Depending on context, this does one of the following:
  12565. - switch a timestamp at point one day into the past
  12566. - on a headline, switch to the previous TODO keyword.
  12567. - on an item, switch entire list to the previous bullet type
  12568. - on a property line, switch to the previous allowed value
  12569. - on a clocktable definition line, move time block into the past"
  12570. (interactive "P")
  12571. (cond
  12572. ((and org-support-shift-select (org-region-active-p))
  12573. (org-call-for-shift-select 'backward-char))
  12574. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12575. ((and (not (eq org-support-shift-select 'always))
  12576. (org-on-heading-p))
  12577. (org-call-with-arg 'org-todo 'left))
  12578. ((or (and org-support-shift-select
  12579. (not (eq org-support-shift-select 'always))
  12580. (org-at-item-bullet-p))
  12581. (and (not org-support-shift-select) (org-at-item-p)))
  12582. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12583. ((and (not (eq org-support-shift-select 'always))
  12584. (org-at-property-p))
  12585. (call-interactively 'org-property-previous-allowed-value))
  12586. ((org-clocktable-try-shift 'left arg))
  12587. (org-support-shift-select
  12588. (org-call-for-shift-select 'backward-char))
  12589. (t (org-shiftselect-error))))
  12590. (defun org-shiftcontrolright ()
  12591. "Switch to next TODO set."
  12592. (interactive)
  12593. (cond
  12594. ((and org-support-shift-select (org-region-active-p))
  12595. (org-call-for-shift-select 'forward-word))
  12596. ((and (not (eq org-support-shift-select 'always))
  12597. (org-on-heading-p))
  12598. (org-call-with-arg 'org-todo 'nextset))
  12599. (org-support-shift-select
  12600. (org-call-for-shift-select 'forward-word))
  12601. (t (org-shiftselect-error))))
  12602. (defun org-shiftcontrolleft ()
  12603. "Switch to previous TODO set."
  12604. (interactive)
  12605. (cond
  12606. ((and org-support-shift-select (org-region-active-p))
  12607. (org-call-for-shift-select 'backward-word))
  12608. ((and (not (eq org-support-shift-select 'always))
  12609. (org-on-heading-p))
  12610. (org-call-with-arg 'org-todo 'previousset))
  12611. (org-support-shift-select
  12612. (org-call-for-shift-select 'backward-word))
  12613. (t (org-shiftselect-error))))
  12614. (defun org-ctrl-c-ret ()
  12615. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12616. (interactive)
  12617. (cond
  12618. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12619. (t (call-interactively 'org-insert-heading))))
  12620. (defun org-copy-special ()
  12621. "Copy region in table or copy current subtree.
  12622. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12623. See the individual commands for more information."
  12624. (interactive)
  12625. (call-interactively
  12626. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12627. (defun org-cut-special ()
  12628. "Cut region in table or cut current subtree.
  12629. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12630. See the individual commands for more information."
  12631. (interactive)
  12632. (call-interactively
  12633. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12634. (defun org-paste-special (arg)
  12635. "Paste rectangular region into table, or past subtree relative to level.
  12636. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12637. See the individual commands for more information."
  12638. (interactive "P")
  12639. (if (org-at-table-p)
  12640. (org-table-paste-rectangle)
  12641. (org-paste-subtree arg)))
  12642. (defun org-edit-special ()
  12643. "Call a special editor for the stuff at point.
  12644. When at a table, call the formula editor with `org-table-edit-formulas'.
  12645. When at the first line of an src example, call `org-edit-src-code'.
  12646. When in an #+include line, visit the include file. Otherwise call
  12647. `ffap' to visit the file at point."
  12648. (interactive)
  12649. (cond
  12650. ((org-at-table-p)
  12651. (call-interactively 'org-table-edit-formulas))
  12652. ((save-excursion
  12653. (beginning-of-line 1)
  12654. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12655. (find-file (org-trim (match-string 1))))
  12656. ((org-edit-src-code))
  12657. ((org-edit-fixed-width-region))
  12658. (t (call-interactively 'ffap))))
  12659. (defun org-ctrl-c-ctrl-c (&optional arg)
  12660. "Set tags in headline, or update according to changed information at point.
  12661. This command does many different things, depending on context:
  12662. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12663. this is what we do.
  12664. - If the cursor is in a headline, prompt for tags and insert them
  12665. into the current line, aligned to `org-tags-column'. When called
  12666. with prefix arg, realign all tags in the current buffer.
  12667. - If the cursor is in one of the special #+KEYWORD lines, this
  12668. triggers scanning the buffer for these lines and updating the
  12669. information.
  12670. - If the cursor is inside a table, realign the table. This command
  12671. works even if the automatic table editor has been turned off.
  12672. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12673. the entire table.
  12674. - If the cursor is at a footnote reference or definition, jump to
  12675. the corresponding definition or references, respectively.
  12676. - If the cursor is a the beginning of a dynamic block, update it.
  12677. - If the cursor is inside a table created by the table.el package,
  12678. activate that table.
  12679. - If the current buffer is a remember buffer, close note and file
  12680. it. A prefix argument of 1 files to the default location
  12681. without further interaction. A prefix argument of 2 files to
  12682. the currently clocking task.
  12683. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12684. links in this buffer.
  12685. - If the cursor is on a numbered item in a plain list, renumber the
  12686. ordered list.
  12687. - If the cursor is on a checkbox, toggle it."
  12688. (interactive "P")
  12689. (let ((org-enable-table-editor t))
  12690. (cond
  12691. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12692. org-occur-highlights
  12693. org-latex-fragment-image-overlays)
  12694. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12695. (org-remove-occur-highlights)
  12696. (org-remove-latex-fragment-image-overlays)
  12697. (message "Temporary highlights/overlays removed from current buffer"))
  12698. ((and (local-variable-p 'org-finish-function (current-buffer))
  12699. (fboundp org-finish-function))
  12700. (funcall org-finish-function))
  12701. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  12702. ((org-at-property-p)
  12703. (call-interactively 'org-property-action))
  12704. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12705. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12706. ((org-at-table.el-p)
  12707. (require 'table)
  12708. (beginning-of-line 1)
  12709. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12710. (call-interactively 'table-recognize-table))
  12711. ((org-at-table-p)
  12712. (org-table-maybe-eval-formula)
  12713. (if arg
  12714. (call-interactively 'org-table-recalculate)
  12715. (org-table-maybe-recalculate-line))
  12716. (call-interactively 'org-table-align))
  12717. ((or (org-footnote-at-reference-p)
  12718. (org-footnote-at-definition-p))
  12719. (call-interactively 'org-footnote-action))
  12720. ((org-at-item-checkbox-p)
  12721. (call-interactively 'org-toggle-checkbox))
  12722. ((org-at-item-p)
  12723. (if arg
  12724. (call-interactively 'org-toggle-checkbox)
  12725. (call-interactively 'org-maybe-renumber-ordered-list)))
  12726. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12727. ;; Dynamic block
  12728. (beginning-of-line 1)
  12729. (save-excursion (org-update-dblock)))
  12730. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12731. (cond
  12732. ((equal (match-string 1) "TBLFM")
  12733. ;; Recalculate the table before this line
  12734. (save-excursion
  12735. (beginning-of-line 1)
  12736. (skip-chars-backward " \r\n\t")
  12737. (if (org-at-table-p)
  12738. (org-call-with-arg 'org-table-recalculate t))))
  12739. (t
  12740. ; (org-set-regexps-and-options)
  12741. ; (org-restart-font-lock)
  12742. (let ((org-inhibit-startup t)) (org-mode-restart))
  12743. (message "Local setup has been refreshed"))))
  12744. (t (error "C-c C-c can do nothing useful at this location.")))))
  12745. (defun org-mode-restart ()
  12746. "Restart Org-mode, to scan again for special lines.
  12747. Also updates the keyword regular expressions."
  12748. (interactive)
  12749. (org-mode)
  12750. (message "Org-mode restarted"))
  12751. (defun org-kill-note-or-show-branches ()
  12752. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12753. (interactive)
  12754. (if (not org-finish-function)
  12755. (call-interactively 'show-branches)
  12756. (let ((org-note-abort t))
  12757. (funcall org-finish-function))))
  12758. (defun org-return (&optional indent)
  12759. "Goto next table row or insert a newline.
  12760. Calls `org-table-next-row' or `newline', depending on context.
  12761. See the individual commands for more information."
  12762. (interactive)
  12763. (cond
  12764. ((bobp) (if indent (newline-and-indent) (newline)))
  12765. ((org-at-table-p)
  12766. (org-table-justify-field-maybe)
  12767. (call-interactively 'org-table-next-row))
  12768. ((and org-return-follows-link
  12769. (eq (get-text-property (point) 'face) 'org-link))
  12770. (call-interactively 'org-open-at-point))
  12771. ((and (org-at-heading-p)
  12772. (looking-at
  12773. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12774. (org-show-entry)
  12775. (end-of-line 1)
  12776. (newline))
  12777. (t (if indent (newline-and-indent) (newline)))))
  12778. (defun org-return-indent ()
  12779. "Goto next table row or insert a newline and indent.
  12780. Calls `org-table-next-row' or `newline-and-indent', depending on
  12781. context. See the individual commands for more information."
  12782. (interactive)
  12783. (org-return t))
  12784. (defun org-ctrl-c-star ()
  12785. "Compute table, or change heading status of lines.
  12786. Calls `org-table-recalculate' or `org-toggle-heading',
  12787. depending on context."
  12788. (interactive)
  12789. (cond
  12790. ((org-at-table-p)
  12791. (call-interactively 'org-table-recalculate))
  12792. (t
  12793. ;; Convert all lines in region to list items
  12794. (call-interactively 'org-toggle-heading))))
  12795. (defun org-ctrl-c-minus ()
  12796. "Insert separator line in table or modify bullet status of line.
  12797. Also turns a plain line or a region of lines into list items.
  12798. Calls `org-table-insert-hline', `org-toggle-item', or
  12799. `org-cycle-list-bullet', depending on context."
  12800. (interactive)
  12801. (cond
  12802. ((org-at-table-p)
  12803. (call-interactively 'org-table-insert-hline))
  12804. ((org-region-active-p)
  12805. (call-interactively 'org-toggle-item))
  12806. ((org-in-item-p)
  12807. (call-interactively 'org-cycle-list-bullet))
  12808. (t
  12809. (call-interactively 'org-toggle-item))))
  12810. (defun org-toggle-item ()
  12811. "Convert headings or normal lines to items, items to normal lines.
  12812. If there is no active region, only the current line is considered.
  12813. If the first line in the region is a headline, convert all headlines to items.
  12814. If the first line in the region is an item, convert all items to normal lines.
  12815. If the first line is normal text, add an item bullet to each line."
  12816. (interactive)
  12817. (let (l2 l beg end)
  12818. (if (org-region-active-p)
  12819. (setq beg (region-beginning) end (region-end))
  12820. (setq beg (point-at-bol)
  12821. end (min (1+ (point-at-eol)) (point-max))))
  12822. (save-excursion
  12823. (goto-char end)
  12824. (setq l2 (org-current-line))
  12825. (goto-char beg)
  12826. (beginning-of-line 1)
  12827. (setq l (1- (org-current-line)))
  12828. (if (org-at-item-p)
  12829. ;; We already have items, de-itemize
  12830. (while (< (setq l (1+ l)) l2)
  12831. (when (org-at-item-p)
  12832. (goto-char (match-beginning 2))
  12833. (delete-region (match-beginning 2) (match-end 2))
  12834. (and (looking-at "[ \t]+") (replace-match "")))
  12835. (beginning-of-line 2))
  12836. (if (org-on-heading-p)
  12837. ;; Headings, convert to items
  12838. (while (< (setq l (1+ l)) l2)
  12839. (if (looking-at org-outline-regexp)
  12840. (replace-match "- " t t))
  12841. (beginning-of-line 2))
  12842. ;; normal lines, turn them into items
  12843. (while (< (setq l (1+ l)) l2)
  12844. (unless (org-at-item-p)
  12845. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12846. (replace-match "\\1- \\2")))
  12847. (beginning-of-line 2)))))))
  12848. (defun org-toggle-heading (&optional nstars)
  12849. "Convert headings to normal text, or items or text to headings.
  12850. If there is no active region, only the current line is considered.
  12851. If the first line is a heading, remove the stars from all headlines
  12852. in the region.
  12853. If the first line is a plain list item, turn all plain list items into
  12854. headings.
  12855. If the first line is a normal line, turn each and every line in the region
  12856. into a heading.
  12857. When converting a line into a heading, the number of stars is chosen
  12858. such that the lines become children of the current entry. However, when
  12859. a prefix argument is given, its value determines the number of stars to add."
  12860. (interactive "P")
  12861. (let (l2 l itemp beg end)
  12862. (if (org-region-active-p)
  12863. (setq beg (region-beginning) end (region-end))
  12864. (setq beg (point-at-bol)
  12865. end (min (1+ (point-at-eol)) (point-max))))
  12866. (save-excursion
  12867. (goto-char end)
  12868. (setq l2 (org-current-line))
  12869. (goto-char beg)
  12870. (beginning-of-line 1)
  12871. (setq l (1- (org-current-line)))
  12872. (if (org-on-heading-p)
  12873. ;; We already have headlines, de-star them
  12874. (while (< (setq l (1+ l)) l2)
  12875. (when (org-on-heading-p t)
  12876. (and (looking-at outline-regexp) (replace-match "")))
  12877. (beginning-of-line 2))
  12878. (setq itemp (org-at-item-p))
  12879. (let* ((stars
  12880. (if nstars
  12881. (make-string (prefix-numeric-value current-prefix-arg)
  12882. ?*)
  12883. (save-excursion
  12884. (re-search-backward org-complex-heading-regexp nil t)
  12885. (or (match-string 1) "*"))))
  12886. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12887. (rpl (concat stars add-stars " ")))
  12888. (while (< (setq l (1+ l)) l2)
  12889. (if itemp
  12890. (and (org-at-item-p) (replace-match rpl t t))
  12891. (unless (org-on-heading-p)
  12892. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12893. (replace-match (concat rpl (match-string 2))))))
  12894. (beginning-of-line 2)))))))
  12895. (defun org-meta-return (&optional arg)
  12896. "Insert a new heading or wrap a region in a table.
  12897. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12898. See the individual commands for more information."
  12899. (interactive "P")
  12900. (cond
  12901. ((run-hook-with-args-until-success 'org-metareturn-hook))
  12902. ((org-at-table-p)
  12903. (call-interactively 'org-table-wrap-region))
  12904. (t (call-interactively 'org-insert-heading))))
  12905. ;;; Menu entries
  12906. ;; Define the Org-mode menus
  12907. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12908. '("Tbl"
  12909. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12910. ["Next Field" org-cycle (org-at-table-p)]
  12911. ["Previous Field" org-shifttab (org-at-table-p)]
  12912. ["Next Row" org-return (org-at-table-p)]
  12913. "--"
  12914. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12915. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12916. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12917. "--"
  12918. ("Column"
  12919. ["Move Column Left" org-metaleft (org-at-table-p)]
  12920. ["Move Column Right" org-metaright (org-at-table-p)]
  12921. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12922. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12923. ("Row"
  12924. ["Move Row Up" org-metaup (org-at-table-p)]
  12925. ["Move Row Down" org-metadown (org-at-table-p)]
  12926. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12927. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12928. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12929. "--"
  12930. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12931. ("Rectangle"
  12932. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12933. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12934. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12935. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12936. "--"
  12937. ("Calculate"
  12938. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12939. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12940. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12941. "--"
  12942. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12943. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12944. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12945. "--"
  12946. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12947. "--"
  12948. ["Sum Column/Rectangle" org-table-sum
  12949. (or (org-at-table-p) (org-region-active-p))]
  12950. ["Which Column?" org-table-current-column (org-at-table-p)])
  12951. ["Debug Formulas"
  12952. org-table-toggle-formula-debugger
  12953. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12954. ["Show Col/Row Numbers"
  12955. org-table-toggle-coordinate-overlays
  12956. :style toggle
  12957. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12958. "--"
  12959. ["Create" org-table-create (and (not (org-at-table-p))
  12960. org-enable-table-editor)]
  12961. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12962. ["Import from File" org-table-import (not (org-at-table-p))]
  12963. ["Export to File" org-table-export (org-at-table-p)]
  12964. "--"
  12965. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12966. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12967. '("Org"
  12968. ("Show/Hide"
  12969. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12970. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12971. ["Sparse Tree..." org-sparse-tree t]
  12972. ["Reveal Context" org-reveal t]
  12973. ["Show All" show-all t]
  12974. "--"
  12975. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12976. "--"
  12977. ["New Heading" org-insert-heading t]
  12978. ("Navigate Headings"
  12979. ["Up" outline-up-heading t]
  12980. ["Next" outline-next-visible-heading t]
  12981. ["Previous" outline-previous-visible-heading t]
  12982. ["Next Same Level" outline-forward-same-level t]
  12983. ["Previous Same Level" outline-backward-same-level t]
  12984. "--"
  12985. ["Jump" org-goto t])
  12986. ("Edit Structure"
  12987. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12988. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12989. "--"
  12990. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12991. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12992. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12993. "--"
  12994. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12995. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12996. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12997. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12998. "--"
  12999. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13000. "--"
  13001. ["Convert to odd levels" org-convert-to-odd-levels t]
  13002. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13003. ("Editing"
  13004. ["Emphasis..." org-emphasize t]
  13005. ["Edit Source Example" org-edit-special t]
  13006. "--"
  13007. ["Footnote new/jump" org-footnote-action t]
  13008. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13009. ("Archive"
  13010. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13011. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13012. ; :active t :keys "C-u C-c C-x C-a"]
  13013. ["Sparse trees open ARCHIVE trees"
  13014. (setq org-sparse-tree-open-archived-trees
  13015. (not org-sparse-tree-open-archived-trees))
  13016. :style toggle :selected org-sparse-tree-open-archived-trees]
  13017. ["Cycling opens ARCHIVE trees"
  13018. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13019. :style toggle :selected org-cycle-open-archived-trees]
  13020. "--"
  13021. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13022. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13023. ; ["Check and Move Children" (org-archive-subtree '(4))
  13024. ; :active t :keys "C-u C-c C-x C-s"]
  13025. )
  13026. "--"
  13027. ("TODO Lists"
  13028. ["TODO/DONE/-" org-todo t]
  13029. ("Select keyword"
  13030. ["Next keyword" org-shiftright (org-on-heading-p)]
  13031. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13032. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13033. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13034. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13035. ["Show TODO Tree" org-show-todo-tree t]
  13036. ["Global TODO list" org-todo-list t]
  13037. "--"
  13038. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13039. :selected org-enforce-todo-dependencies :style toggle :active t]
  13040. "Settings for tree at point"
  13041. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13042. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13043. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13044. ["Do Children parallel" org-toggle-ordered-property :style radio
  13045. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13046. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13047. "--"
  13048. ["Set Priority" org-priority t]
  13049. ["Priority Up" org-shiftup t]
  13050. ["Priority Down" org-shiftdown t])
  13051. ("TAGS and Properties"
  13052. ["Set Tags" org-set-tags-command t]
  13053. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13054. "--"
  13055. ["Set property" org-set-property t]
  13056. ["Column view of properties" org-columns t]
  13057. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13058. ("Dates and Scheduling"
  13059. ["Timestamp" org-time-stamp t]
  13060. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13061. ("Change Date"
  13062. ["1 Day Later" org-shiftright t]
  13063. ["1 Day Earlier" org-shiftleft t]
  13064. ["1 ... Later" org-shiftup t]
  13065. ["1 ... Earlier" org-shiftdown t])
  13066. ["Compute Time Range" org-evaluate-time-range t]
  13067. ["Schedule Item" org-schedule t]
  13068. ["Deadline" org-deadline t]
  13069. "--"
  13070. ["Custom time format" org-toggle-time-stamp-overlays
  13071. :style radio :selected org-display-custom-times]
  13072. "--"
  13073. ["Goto Calendar" org-goto-calendar t]
  13074. ["Date from Calendar" org-date-from-calendar t]
  13075. "--"
  13076. ["Start/Restart Timer" org-timer-start t]
  13077. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13078. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13079. ["Insert Timer String" org-timer t]
  13080. ["Insert Timer Item" org-timer-item t])
  13081. ("Logging work"
  13082. ["Clock in" org-clock-in t]
  13083. ["Clock out" org-clock-out t]
  13084. ["Clock cancel" org-clock-cancel t]
  13085. ["Goto running clock" org-clock-goto t]
  13086. ["Display times" org-clock-display t]
  13087. ["Create clock table" org-clock-report t]
  13088. "--"
  13089. ["Record DONE time"
  13090. (progn (setq org-log-done (not org-log-done))
  13091. (message "Switching to %s will %s record a timestamp"
  13092. (car org-done-keywords)
  13093. (if org-log-done "automatically" "not")))
  13094. :style toggle :selected org-log-done])
  13095. "--"
  13096. ["Agenda Command..." org-agenda t]
  13097. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13098. ("File List for Agenda")
  13099. ("Special views current file"
  13100. ["TODO Tree" org-show-todo-tree t]
  13101. ["Check Deadlines" org-check-deadlines t]
  13102. ["Timeline" org-timeline t]
  13103. ["Tags/Property tree" org-match-sparse-tree t])
  13104. "--"
  13105. ("Hyperlinks"
  13106. ["Store Link (Global)" org-store-link t]
  13107. ["Insert Link" org-insert-link t]
  13108. ["Follow Link" org-open-at-point t]
  13109. "--"
  13110. ["Next link" org-next-link t]
  13111. ["Previous link" org-previous-link t]
  13112. "--"
  13113. ["Descriptive Links"
  13114. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13115. :style radio
  13116. :selected (member '(org-link) buffer-invisibility-spec)]
  13117. ["Literal Links"
  13118. (progn
  13119. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13120. :style radio
  13121. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13122. "--"
  13123. ["Export/Publish..." org-export t]
  13124. ("LaTeX"
  13125. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13126. :selected org-cdlatex-mode]
  13127. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13128. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13129. ["Modify math symbol" org-cdlatex-math-modify
  13130. (org-inside-LaTeX-fragment-p)]
  13131. ["Export LaTeX fragments as images"
  13132. (if (featurep 'org-exp)
  13133. (setq org-export-with-LaTeX-fragments
  13134. (not org-export-with-LaTeX-fragments))
  13135. (require 'org-exp))
  13136. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13137. org-export-with-LaTeX-fragments)])
  13138. "--"
  13139. ("Documentation"
  13140. ["Show Version" org-version t]
  13141. ["Info Documentation" org-info t])
  13142. ("Customize"
  13143. ["Browse Org Group" org-customize t]
  13144. "--"
  13145. ["Expand This Menu" org-create-customize-menu
  13146. (fboundp 'customize-menu-create)])
  13147. "--"
  13148. ("Refresh/Reload"
  13149. ["Refresh setup current buffer" org-mode-restart t]
  13150. ["Reload Org (after update)" org-reload t]
  13151. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13152. ))
  13153. (defun org-info (&optional node)
  13154. "Read documentation for Org-mode in the info system.
  13155. With optional NODE, go directly to that node."
  13156. (interactive)
  13157. (info (format "(org)%s" (or node ""))))
  13158. (defun org-install-agenda-files-menu ()
  13159. (let ((bl (buffer-list)))
  13160. (save-excursion
  13161. (while bl
  13162. (set-buffer (pop bl))
  13163. (if (org-mode-p) (setq bl nil)))
  13164. (when (org-mode-p)
  13165. (easy-menu-change
  13166. '("Org") "File List for Agenda"
  13167. (append
  13168. (list
  13169. ["Edit File List" (org-edit-agenda-file-list) t]
  13170. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13171. ["Remove Current File from List" org-remove-file t]
  13172. ["Cycle through agenda files" org-cycle-agenda-files t]
  13173. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13174. "--")
  13175. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13176. ;;;; Documentation
  13177. ;;;###autoload
  13178. (defun org-require-autoloaded-modules ()
  13179. (interactive)
  13180. (mapc 'require
  13181. '(org-agenda org-archive org-attach org-clock org-colview
  13182. org-exp org-id org-export-latex org-publish
  13183. org-remember org-table org-timer)))
  13184. ;;;###autoload
  13185. (defun org-reload (&optional uncompiled)
  13186. "Reload all org lisp files.
  13187. With prefix arg UNCOMPILED, load the uncompiled versions."
  13188. (interactive "P")
  13189. (require 'find-func)
  13190. (let* ((dir (file-name-directory (find-library-name "org")))
  13191. (files (directory-files dir t "\\.el\\'"))
  13192. (remove-re (concat (if (featurep 'xemacs)
  13193. "org-colview" "org-colview-xemacs")
  13194. "\\'")))
  13195. (setq files (mapcar 'file-name-sans-extension files))
  13196. (setq files (mapcar
  13197. (lambda (x) (if (string-match remove-re x) nil x))
  13198. files))
  13199. (setq files (delq nil files))
  13200. (mapc
  13201. (lambda (f)
  13202. (if (and (not uncompiled)
  13203. (file-exists-p (concat f ".elc")))
  13204. (load (concat f ".elc") nil nil t)
  13205. (load (concat f ".el") nil nil t)))
  13206. files)))
  13207. ;;;###autoload
  13208. (defun org-customize ()
  13209. "Call the customize function with org as argument."
  13210. (interactive)
  13211. (org-load-modules-maybe)
  13212. (org-require-autoloaded-modules)
  13213. (customize-browse 'org))
  13214. (defun org-create-customize-menu ()
  13215. "Create a full customization menu for Org-mode, insert it into the menu."
  13216. (interactive)
  13217. (org-load-modules-maybe)
  13218. (org-require-autoloaded-modules)
  13219. (if (fboundp 'customize-menu-create)
  13220. (progn
  13221. (easy-menu-change
  13222. '("Org") "Customize"
  13223. `(["Browse Org group" org-customize t]
  13224. "--"
  13225. ,(customize-menu-create 'org)
  13226. ["Set" Custom-set t]
  13227. ["Save" Custom-save t]
  13228. ["Reset to Current" Custom-reset-current t]
  13229. ["Reset to Saved" Custom-reset-saved t]
  13230. ["Reset to Standard Settings" Custom-reset-standard t]))
  13231. (message "\"Org\"-menu now contains full customization menu"))
  13232. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13233. ;;;; Miscellaneous stuff
  13234. ;;; Generally useful functions
  13235. (defun org-find-text-property-in-string (prop s)
  13236. "Return the first non-nil value of property PROP in string S."
  13237. (or (get-text-property 0 prop s)
  13238. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13239. prop s)))
  13240. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13241. "Display the given MESSAGE as a warning."
  13242. (if (fboundp 'display-warning)
  13243. (display-warning 'org message
  13244. (if (featurep 'xemacs)
  13245. 'warning
  13246. :warning))
  13247. (let ((buf (get-buffer-create "*Org warnings*")))
  13248. (with-current-buffer buf
  13249. (goto-char (point-max))
  13250. (insert "Warning (Org): " message)
  13251. (unless (bolp)
  13252. (newline)))
  13253. (display-buffer buf)
  13254. (sit-for 0))))
  13255. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13256. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13257. (if (and marker (marker-buffer marker)
  13258. (buffer-live-p (marker-buffer marker)))
  13259. (progn
  13260. (switch-to-buffer (marker-buffer marker))
  13261. (if (or (> marker (point-max)) (< marker (point-min)))
  13262. (widen))
  13263. (goto-char marker)
  13264. (org-show-context 'org-goto))
  13265. (if bookmark
  13266. (bookmark-jump bookmark)
  13267. (error "Cannot find location"))))
  13268. (defun org-quote-csv-field (s)
  13269. "Quote field for inclusion in CSV material."
  13270. (if (string-match "[\",]" s)
  13271. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13272. s))
  13273. (defun org-plist-delete (plist property)
  13274. "Delete PROPERTY from PLIST.
  13275. This is in contrast to merely setting it to 0."
  13276. (let (p)
  13277. (while plist
  13278. (if (not (eq property (car plist)))
  13279. (setq p (plist-put p (car plist) (nth 1 plist))))
  13280. (setq plist (cddr plist)))
  13281. p))
  13282. (defun org-force-self-insert (N)
  13283. "Needed to enforce self-insert under remapping."
  13284. (interactive "p")
  13285. (self-insert-command N))
  13286. (defun org-string-width (s)
  13287. "Compute width of string, ignoring invisible characters.
  13288. This ignores character with invisibility property `org-link', and also
  13289. characters with property `org-cwidth', because these will become invisible
  13290. upon the next fontification round."
  13291. (let (b l)
  13292. (when (or (eq t buffer-invisibility-spec)
  13293. (assq 'org-link buffer-invisibility-spec))
  13294. (while (setq b (text-property-any 0 (length s)
  13295. 'invisible 'org-link s))
  13296. (setq s (concat (substring s 0 b)
  13297. (substring s (or (next-single-property-change
  13298. b 'invisible s) (length s)))))))
  13299. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13300. (setq s (concat (substring s 0 b)
  13301. (substring s (or (next-single-property-change
  13302. b 'org-cwidth s) (length s))))))
  13303. (setq l (string-width s) b -1)
  13304. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13305. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13306. l))
  13307. (defun org-get-indentation (&optional line)
  13308. "Get the indentation of the current line, interpreting tabs.
  13309. When LINE is given, assume it represents a line and compute its indentation."
  13310. (if line
  13311. (if (string-match "^ *" (org-remove-tabs line))
  13312. (match-end 0))
  13313. (save-excursion
  13314. (beginning-of-line 1)
  13315. (skip-chars-forward " \t")
  13316. (current-column))))
  13317. (defun org-remove-tabs (s &optional width)
  13318. "Replace tabulators in S with spaces.
  13319. Assumes that s is a single line, starting in column 0."
  13320. (setq width (or width tab-width))
  13321. (while (string-match "\t" s)
  13322. (setq s (replace-match
  13323. (make-string
  13324. (- (* width (/ (+ (match-beginning 0) width) width))
  13325. (match-beginning 0)) ?\ )
  13326. t t s)))
  13327. s)
  13328. (defun org-fix-indentation (line ind)
  13329. "Fix indentation in LINE.
  13330. IND is a cons cell with target and minimum indentation.
  13331. If the current indentation in LINE is smaller than the minimum,
  13332. leave it alone. If it is larger than ind, set it to the target."
  13333. (let* ((l (org-remove-tabs line))
  13334. (i (org-get-indentation l))
  13335. (i1 (car ind)) (i2 (cdr ind)))
  13336. (if (>= i i2) (setq l (substring line i2)))
  13337. (if (> i1 0)
  13338. (concat (make-string i1 ?\ ) l)
  13339. l)))
  13340. (defun org-base-buffer (buffer)
  13341. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13342. (if (not buffer)
  13343. buffer
  13344. (or (buffer-base-buffer buffer)
  13345. buffer)))
  13346. (defun org-trim (s)
  13347. "Remove whitespace at beginning and end of string."
  13348. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13349. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13350. s)
  13351. (defun org-wrap (string &optional width lines)
  13352. "Wrap string to either a number of lines, or a width in characters.
  13353. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13354. that costs. If there is a word longer than WIDTH, the text is actually
  13355. wrapped to the length of that word.
  13356. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13357. many lines, whatever width that takes.
  13358. The return value is a list of lines, without newlines at the end."
  13359. (let* ((words (org-split-string string "[ \t\n]+"))
  13360. (maxword (apply 'max (mapcar 'org-string-width words)))
  13361. w ll)
  13362. (cond (width
  13363. (org-do-wrap words (max maxword width)))
  13364. (lines
  13365. (setq w maxword)
  13366. (setq ll (org-do-wrap words maxword))
  13367. (if (<= (length ll) lines)
  13368. ll
  13369. (setq ll words)
  13370. (while (> (length ll) lines)
  13371. (setq w (1+ w))
  13372. (setq ll (org-do-wrap words w)))
  13373. ll))
  13374. (t (error "Cannot wrap this")))))
  13375. (defun org-do-wrap (words width)
  13376. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13377. (let (lines line)
  13378. (while words
  13379. (setq line (pop words))
  13380. (while (and words (< (+ (length line) (length (car words))) width))
  13381. (setq line (concat line " " (pop words))))
  13382. (setq lines (push line lines)))
  13383. (nreverse lines)))
  13384. (defun org-split-string (string &optional separators)
  13385. "Splits STRING into substrings at SEPARATORS.
  13386. No empty strings are returned if there are matches at the beginning
  13387. and end of string."
  13388. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13389. (start 0)
  13390. notfirst
  13391. (list nil))
  13392. (while (and (string-match rexp string
  13393. (if (and notfirst
  13394. (= start (match-beginning 0))
  13395. (< start (length string)))
  13396. (1+ start) start))
  13397. (< (match-beginning 0) (length string)))
  13398. (setq notfirst t)
  13399. (or (eq (match-beginning 0) 0)
  13400. (and (eq (match-beginning 0) (match-end 0))
  13401. (eq (match-beginning 0) start))
  13402. (setq list
  13403. (cons (substring string start (match-beginning 0))
  13404. list)))
  13405. (setq start (match-end 0)))
  13406. (or (eq start (length string))
  13407. (setq list
  13408. (cons (substring string start)
  13409. list)))
  13410. (nreverse list)))
  13411. (defun org-context ()
  13412. "Return a list of contexts of the current cursor position.
  13413. If several contexts apply, all are returned.
  13414. Each context entry is a list with a symbol naming the context, and
  13415. two positions indicating start and end of the context. Possible
  13416. contexts are:
  13417. :headline anywhere in a headline
  13418. :headline-stars on the leading stars in a headline
  13419. :todo-keyword on a TODO keyword (including DONE) in a headline
  13420. :tags on the TAGS in a headline
  13421. :priority on the priority cookie in a headline
  13422. :item on the first line of a plain list item
  13423. :item-bullet on the bullet/number of a plain list item
  13424. :checkbox on the checkbox in a plain list item
  13425. :table in an org-mode table
  13426. :table-special on a special filed in a table
  13427. :table-table in a table.el table
  13428. :link on a hyperlink
  13429. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13430. :target on a <<target>>
  13431. :radio-target on a <<<radio-target>>>
  13432. :latex-fragment on a LaTeX fragment
  13433. :latex-preview on a LaTeX fragment with overlayed preview image
  13434. This function expects the position to be visible because it uses font-lock
  13435. faces as a help to recognize the following contexts: :table-special, :link,
  13436. and :keyword."
  13437. (let* ((f (get-text-property (point) 'face))
  13438. (faces (if (listp f) f (list f)))
  13439. (p (point)) clist o)
  13440. ;; First the large context
  13441. (cond
  13442. ((org-on-heading-p t)
  13443. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13444. (when (progn
  13445. (beginning-of-line 1)
  13446. (looking-at org-todo-line-tags-regexp))
  13447. (push (org-point-in-group p 1 :headline-stars) clist)
  13448. (push (org-point-in-group p 2 :todo-keyword) clist)
  13449. (push (org-point-in-group p 4 :tags) clist))
  13450. (goto-char p)
  13451. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13452. (if (looking-at "\\[#[A-Z0-9]\\]")
  13453. (push (org-point-in-group p 0 :priority) clist)))
  13454. ((org-at-item-p)
  13455. (push (org-point-in-group p 2 :item-bullet) clist)
  13456. (push (list :item (point-at-bol)
  13457. (save-excursion (org-end-of-item) (point)))
  13458. clist)
  13459. (and (org-at-item-checkbox-p)
  13460. (push (org-point-in-group p 0 :checkbox) clist)))
  13461. ((org-at-table-p)
  13462. (push (list :table (org-table-begin) (org-table-end)) clist)
  13463. (if (memq 'org-formula faces)
  13464. (push (list :table-special
  13465. (previous-single-property-change p 'face)
  13466. (next-single-property-change p 'face)) clist)))
  13467. ((org-at-table-p 'any)
  13468. (push (list :table-table) clist)))
  13469. (goto-char p)
  13470. ;; Now the small context
  13471. (cond
  13472. ((org-at-timestamp-p)
  13473. (push (org-point-in-group p 0 :timestamp) clist))
  13474. ((memq 'org-link faces)
  13475. (push (list :link
  13476. (previous-single-property-change p 'face)
  13477. (next-single-property-change p 'face)) clist))
  13478. ((memq 'org-special-keyword faces)
  13479. (push (list :keyword
  13480. (previous-single-property-change p 'face)
  13481. (next-single-property-change p 'face)) clist))
  13482. ((org-on-target-p)
  13483. (push (org-point-in-group p 0 :target) clist)
  13484. (goto-char (1- (match-beginning 0)))
  13485. (if (looking-at org-radio-target-regexp)
  13486. (push (org-point-in-group p 0 :radio-target) clist))
  13487. (goto-char p))
  13488. ((setq o (car (delq nil
  13489. (mapcar
  13490. (lambda (x)
  13491. (if (memq x org-latex-fragment-image-overlays) x))
  13492. (org-overlays-at (point))))))
  13493. (push (list :latex-fragment
  13494. (org-overlay-start o) (org-overlay-end o)) clist)
  13495. (push (list :latex-preview
  13496. (org-overlay-start o) (org-overlay-end o)) clist))
  13497. ((org-inside-LaTeX-fragment-p)
  13498. ;; FIXME: positions wrong.
  13499. (push (list :latex-fragment (point) (point)) clist)))
  13500. (setq clist (nreverse (delq nil clist)))
  13501. clist))
  13502. ;; FIXME: Compare with at-regexp-p Do we need both?
  13503. (defun org-in-regexp (re &optional nlines visually)
  13504. "Check if point is inside a match of regexp.
  13505. Normally only the current line is checked, but you can include NLINES extra
  13506. lines both before and after point into the search.
  13507. If VISUALLY is set, require that the cursor is not after the match but
  13508. really on, so that the block visually is on the match."
  13509. (catch 'exit
  13510. (let ((pos (point))
  13511. (eol (point-at-eol (+ 1 (or nlines 0))))
  13512. (inc (if visually 1 0)))
  13513. (save-excursion
  13514. (beginning-of-line (- 1 (or nlines 0)))
  13515. (while (re-search-forward re eol t)
  13516. (if (and (<= (match-beginning 0) pos)
  13517. (>= (+ inc (match-end 0)) pos))
  13518. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13519. (defun org-at-regexp-p (regexp)
  13520. "Is point inside a match of REGEXP in the current line?"
  13521. (catch 'exit
  13522. (save-excursion
  13523. (let ((pos (point)) (end (point-at-eol)))
  13524. (beginning-of-line 1)
  13525. (while (re-search-forward regexp end t)
  13526. (if (and (<= (match-beginning 0) pos)
  13527. (>= (match-end 0) pos))
  13528. (throw 'exit t)))
  13529. nil))))
  13530. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13531. "Call `multi-occur' with buffers for all agenda files."
  13532. (interactive "sOrg-files matching: \np")
  13533. (let* ((files (org-agenda-files))
  13534. (tnames (mapcar 'file-truename files))
  13535. (extra org-agenda-text-search-extra-files)
  13536. f)
  13537. (when (eq (car extra) 'agenda-archives)
  13538. (setq extra (cdr extra))
  13539. (setq files (org-add-archive-files files)))
  13540. (while (setq f (pop extra))
  13541. (unless (member (file-truename f) tnames)
  13542. (add-to-list 'files f 'append)
  13543. (add-to-list 'tnames (file-truename f) 'append)))
  13544. (multi-occur
  13545. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13546. regexp)))
  13547. (if (boundp 'occur-mode-find-occurrence-hook)
  13548. ;; Emacs 23
  13549. (add-hook 'occur-mode-find-occurrence-hook
  13550. (lambda ()
  13551. (when (org-mode-p)
  13552. (org-reveal))))
  13553. ;; Emacs 22
  13554. (defadvice occur-mode-goto-occurrence
  13555. (after org-occur-reveal activate)
  13556. (and (org-mode-p) (org-reveal)))
  13557. (defadvice occur-mode-goto-occurrence-other-window
  13558. (after org-occur-reveal activate)
  13559. (and (org-mode-p) (org-reveal)))
  13560. (defadvice occur-mode-display-occurrence
  13561. (after org-occur-reveal activate)
  13562. (when (org-mode-p)
  13563. (let ((pos (occur-mode-find-occurrence)))
  13564. (with-current-buffer (marker-buffer pos)
  13565. (save-excursion
  13566. (goto-char pos)
  13567. (org-reveal)))))))
  13568. (defun org-uniquify (list)
  13569. "Remove duplicate elements from LIST."
  13570. (let (res)
  13571. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13572. res))
  13573. (defun org-delete-all (elts list)
  13574. "Remove all elements in ELTS from LIST."
  13575. (while elts
  13576. (setq list (delete (pop elts) list)))
  13577. list)
  13578. (defun org-back-over-empty-lines ()
  13579. "Move backwards over whitespace, to the beginning of the first empty line.
  13580. Returns the number of empty lines passed."
  13581. (let ((pos (point)))
  13582. (skip-chars-backward " \t\n\r")
  13583. (beginning-of-line 2)
  13584. (goto-char (min (point) pos))
  13585. (count-lines (point) pos)))
  13586. (defun org-skip-whitespace ()
  13587. (skip-chars-forward " \t\n\r"))
  13588. (defun org-point-in-group (point group &optional context)
  13589. "Check if POINT is in match-group GROUP.
  13590. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13591. match. If the match group does ot exist or point is not inside it,
  13592. return nil."
  13593. (and (match-beginning group)
  13594. (>= point (match-beginning group))
  13595. (<= point (match-end group))
  13596. (if context
  13597. (list context (match-beginning group) (match-end group))
  13598. t)))
  13599. (defun org-switch-to-buffer-other-window (&rest args)
  13600. "Switch to buffer in a second window on the current frame.
  13601. In particular, do not allow pop-up frames."
  13602. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13603. special-display-function)
  13604. (apply 'switch-to-buffer-other-window args)))
  13605. (defun org-combine-plists (&rest plists)
  13606. "Create a single property list from all plists in PLISTS.
  13607. The process starts by copying the first list, and then setting properties
  13608. from the other lists. Settings in the last list are the most significant
  13609. ones and overrule settings in the other lists."
  13610. (let ((rtn (copy-sequence (pop plists)))
  13611. p v ls)
  13612. (while plists
  13613. (setq ls (pop plists))
  13614. (while ls
  13615. (setq p (pop ls) v (pop ls))
  13616. (setq rtn (plist-put rtn p v))))
  13617. rtn))
  13618. (defun org-move-line-down (arg)
  13619. "Move the current line down. With prefix argument, move it past ARG lines."
  13620. (interactive "p")
  13621. (let ((col (current-column))
  13622. beg end pos)
  13623. (beginning-of-line 1) (setq beg (point))
  13624. (beginning-of-line 2) (setq end (point))
  13625. (beginning-of-line (+ 1 arg))
  13626. (setq pos (move-marker (make-marker) (point)))
  13627. (insert (delete-and-extract-region beg end))
  13628. (goto-char pos)
  13629. (org-move-to-column col)))
  13630. (defun org-move-line-up (arg)
  13631. "Move the current line up. With prefix argument, move it past ARG lines."
  13632. (interactive "p")
  13633. (let ((col (current-column))
  13634. beg end pos)
  13635. (beginning-of-line 1) (setq beg (point))
  13636. (beginning-of-line 2) (setq end (point))
  13637. (beginning-of-line (- arg))
  13638. (setq pos (move-marker (make-marker) (point)))
  13639. (insert (delete-and-extract-region beg end))
  13640. (goto-char pos)
  13641. (org-move-to-column col)))
  13642. (defun org-replace-escapes (string table)
  13643. "Replace %-escapes in STRING with values in TABLE.
  13644. TABLE is an association list with keys like \"%a\" and string values.
  13645. The sequences in STRING may contain normal field width and padding information,
  13646. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13647. so values can contain further %-escapes if they are define later in TABLE."
  13648. (let ((case-fold-search nil)
  13649. e re rpl)
  13650. (while (setq e (pop table))
  13651. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13652. (while (string-match re string)
  13653. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13654. (cdr e)))
  13655. (setq string (replace-match rpl t t string))))
  13656. string))
  13657. (defun org-sublist (list start end)
  13658. "Return a section of LIST, from START to END.
  13659. Counting starts at 1."
  13660. (let (rtn (c start))
  13661. (setq list (nthcdr (1- start) list))
  13662. (while (and list (<= c end))
  13663. (push (pop list) rtn)
  13664. (setq c (1+ c)))
  13665. (nreverse rtn)))
  13666. (defun org-find-base-buffer-visiting (file)
  13667. "Like `find-buffer-visiting' but alway return the base buffer and
  13668. not an indirect buffer."
  13669. (let ((buf (find-buffer-visiting file)))
  13670. (if buf
  13671. (or (buffer-base-buffer buf) buf)
  13672. nil)))
  13673. (defun org-image-file-name-regexp (&optional extensions)
  13674. "Return regexp matching the file names of images.
  13675. If EXTENSIONS is given, only match these."
  13676. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13677. (image-file-name-regexp)
  13678. (let ((image-file-name-extensions
  13679. (or extensions
  13680. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13681. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13682. (concat "\\."
  13683. (regexp-opt (nconc (mapcar 'upcase
  13684. image-file-name-extensions)
  13685. image-file-name-extensions)
  13686. t)
  13687. "\\'"))))
  13688. (defun org-file-image-p (file &optional extensions)
  13689. "Return non-nil if FILE is an image."
  13690. (save-match-data
  13691. (string-match (org-image-file-name-regexp extensions) file)))
  13692. (defun org-get-cursor-date ()
  13693. "Return the date at cursor in as a time.
  13694. This works in the calendar and in the agenda, anywhere else it just
  13695. returns the current time."
  13696. (let (date day defd)
  13697. (cond
  13698. ((eq major-mode 'calendar-mode)
  13699. (setq date (calendar-cursor-to-date)
  13700. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13701. ((eq major-mode 'org-agenda-mode)
  13702. (setq day (get-text-property (point) 'day))
  13703. (if day
  13704. (setq date (calendar-gregorian-from-absolute day)
  13705. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13706. (nth 2 date))))))
  13707. (or defd (current-time))))
  13708. (defvar org-agenda-action-marker (make-marker)
  13709. "Marker pointing to the entry for the next agenda action.")
  13710. (defun org-mark-entry-for-agenda-action ()
  13711. "Mark the current entry as target of an agenda action.
  13712. Agenda actions are actions executed from the agenda with the key `k',
  13713. which make use of the date at the cursor."
  13714. (interactive)
  13715. (move-marker org-agenda-action-marker
  13716. (save-excursion (org-back-to-heading t) (point))
  13717. (current-buffer))
  13718. (message
  13719. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13720. ;;; Paragraph filling stuff.
  13721. ;; We want this to be just right, so use the full arsenal.
  13722. (defun org-indent-line-function ()
  13723. "Indent line like previous, but further if previous was headline or item."
  13724. (interactive)
  13725. (let* ((pos (point))
  13726. (itemp (org-at-item-p))
  13727. (org-drawer-regexp (or org-drawer-regexp "\000"))
  13728. column bpos bcol tpos tcol bullet btype bullet-type)
  13729. ;; Find the previous relevant line
  13730. (beginning-of-line 1)
  13731. (cond
  13732. ((looking-at "#") (setq column 0))
  13733. ((looking-at "\\*+ ") (setq column 0))
  13734. ((and (looking-at "[ \t]*:END:")
  13735. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  13736. (save-excursion
  13737. (goto-char (1- (match-beginning 1)))
  13738. (setq column (current-column))))
  13739. (t
  13740. (beginning-of-line 0)
  13741. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  13742. (not (looking-at "[ \t]*:END:"))
  13743. (not (looking-at org-drawer-regexp)))
  13744. (beginning-of-line 0))
  13745. (cond
  13746. ((looking-at "\\*+[ \t]+")
  13747. (if (not org-adapt-indentation)
  13748. (setq column 0)
  13749. (goto-char (match-end 0))
  13750. (setq column (current-column))))
  13751. ((looking-at org-drawer-regexp)
  13752. (goto-char (1- (match-beginning 1)))
  13753. (setq column (current-column)))
  13754. ((looking-at "\\([ \t]*\\):END:")
  13755. (goto-char (match-end 1))
  13756. (setq column (current-column)))
  13757. ((org-in-item-p)
  13758. (org-beginning-of-item)
  13759. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13760. (setq bpos (match-beginning 1) tpos (match-end 0)
  13761. bcol (progn (goto-char bpos) (current-column))
  13762. tcol (progn (goto-char tpos) (current-column))
  13763. bullet (match-string 1)
  13764. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13765. (if (> tcol (+ bcol org-description-max-indent))
  13766. (setq tcol (+ bcol 5)))
  13767. (if (not itemp)
  13768. (setq column tcol)
  13769. (goto-char pos)
  13770. (beginning-of-line 1)
  13771. (if (looking-at "\\S-")
  13772. (progn
  13773. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13774. (setq bullet (match-string 1)
  13775. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13776. (setq column (if (equal btype bullet-type) bcol tcol)))
  13777. (setq column (org-get-indentation)))))
  13778. (t (setq column (org-get-indentation))))))
  13779. (goto-char pos)
  13780. (if (<= (current-column) (current-indentation))
  13781. (org-indent-line-to column)
  13782. (save-excursion (org-indent-line-to column)))
  13783. (setq column (current-column))
  13784. (beginning-of-line 1)
  13785. (if (looking-at
  13786. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13787. (replace-match (concat "\\1" (format org-property-format
  13788. (match-string 2) (match-string 3)))
  13789. t nil))
  13790. (org-move-to-column column)))
  13791. (defun org-set-autofill-regexps ()
  13792. (interactive)
  13793. ;; In the paragraph separator we include headlines, because filling
  13794. ;; text in a line directly attached to a headline would otherwise
  13795. ;; fill the headline as well.
  13796. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13797. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13798. ;; The paragraph starter includes hand-formatted lists.
  13799. (org-set-local 'paragraph-start
  13800. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13801. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13802. ;; But only if the user has not turned off tables or fixed-width regions
  13803. (org-set-local
  13804. 'auto-fill-inhibit-regexp
  13805. (concat "\\*+ \\|#\\+"
  13806. "\\|[ \t]*" org-keyword-time-regexp
  13807. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13808. (concat
  13809. "\\|[ \t]*["
  13810. (if org-enable-table-editor "|" "")
  13811. (if org-enable-fixed-width-editor ":" "")
  13812. "]"))))
  13813. ;; We use our own fill-paragraph function, to make sure that tables
  13814. ;; and fixed-width regions are not wrapped. That function will pass
  13815. ;; through to `fill-paragraph' when appropriate.
  13816. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13817. ; Adaptive filling: To get full control, first make sure that
  13818. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13819. (org-set-local 'adaptive-fill-regexp "\000")
  13820. (org-set-local 'adaptive-fill-function
  13821. 'org-adaptive-fill-function)
  13822. (org-set-local
  13823. 'align-mode-rules-list
  13824. '((org-in-buffer-settings
  13825. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13826. (modes . '(org-mode))))))
  13827. (defun org-fill-paragraph (&optional justify)
  13828. "Re-align a table, pass through to fill-paragraph if no table."
  13829. (let ((table-p (org-at-table-p))
  13830. (table.el-p (org-at-table.el-p)))
  13831. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13832. (save-excursion (goto-char (point-at-bol))
  13833. (looking-at outline-regexp)))
  13834. t) ; skip headlines
  13835. (table.el-p t) ; skip table.el tables
  13836. (table-p (org-table-align) t) ; align org-mode tables
  13837. (t nil)))) ; call paragraph-fill
  13838. ;; For reference, this is the default value of adaptive-fill-regexp
  13839. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13840. (defun org-adaptive-fill-function ()
  13841. "Return a fill prefix for org-mode files.
  13842. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13843. work correctly."
  13844. (cond ((looking-at "#[ \t]+")
  13845. (match-string 0))
  13846. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13847. (save-excursion
  13848. (if (> (match-end 1) (+ (match-beginning 1)
  13849. org-description-max-indent))
  13850. (goto-char (+ (match-beginning 1) 5))
  13851. (goto-char (match-end 0)))
  13852. (make-string (current-column) ?\ )))
  13853. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13854. (save-excursion
  13855. (goto-char (match-end 0))
  13856. (make-string (current-column) ?\ )))
  13857. (t nil)))
  13858. ;;; Other stuff.
  13859. (defun org-toggle-fixed-width-section (arg)
  13860. "Toggle the fixed-width export.
  13861. If there is no active region, the QUOTE keyword at the current headline is
  13862. inserted or removed. When present, it causes the text between this headline
  13863. and the next to be exported as fixed-width text, and unmodified.
  13864. If there is an active region, this command adds or removes a colon as the
  13865. first character of this line. If the first character of a line is a colon,
  13866. this line is also exported in fixed-width font."
  13867. (interactive "P")
  13868. (let* ((cc 0)
  13869. (regionp (org-region-active-p))
  13870. (beg (if regionp (region-beginning) (point)))
  13871. (end (if regionp (region-end)))
  13872. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13873. (case-fold-search nil)
  13874. (re "[ \t]*\\(:\\)")
  13875. off)
  13876. (if regionp
  13877. (save-excursion
  13878. (goto-char beg)
  13879. (setq cc (current-column))
  13880. (beginning-of-line 1)
  13881. (setq off (looking-at re))
  13882. (while (> nlines 0)
  13883. (setq nlines (1- nlines))
  13884. (beginning-of-line 1)
  13885. (cond
  13886. (arg
  13887. (org-move-to-column cc t)
  13888. (insert ":\n")
  13889. (forward-line -1))
  13890. ((and off (looking-at re))
  13891. (replace-match "" t t nil 1))
  13892. ((not off) (org-move-to-column cc t) (insert ":")))
  13893. (forward-line 1)))
  13894. (save-excursion
  13895. (org-back-to-heading)
  13896. (if (looking-at (concat outline-regexp
  13897. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13898. (replace-match "" t t nil 1)
  13899. (if (looking-at outline-regexp)
  13900. (progn
  13901. (goto-char (match-end 0))
  13902. (insert org-quote-string " "))))))))
  13903. ;;;; Functions extending outline functionality
  13904. (defun org-beginning-of-line (&optional arg)
  13905. "Go to the beginning of the current line. If that is invisible, continue
  13906. to a visible line beginning. This makes the function of C-a more intuitive.
  13907. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13908. first attempt, and only move to after the tags when the cursor is already
  13909. beyond the end of the headline."
  13910. (interactive "P")
  13911. (let ((pos (point))
  13912. (special (if (consp org-special-ctrl-a/e)
  13913. (car org-special-ctrl-a/e)
  13914. org-special-ctrl-a/e))
  13915. refpos)
  13916. (beginning-of-line 1)
  13917. (if (and arg (fboundp 'move-beginning-of-line))
  13918. (call-interactively 'move-beginning-of-line)
  13919. (if (bobp)
  13920. nil
  13921. (backward-char 1)
  13922. (if (org-invisible-p)
  13923. (while (and (not (bobp)) (org-invisible-p))
  13924. (backward-char 1)
  13925. (beginning-of-line 1))
  13926. (forward-char 1))))
  13927. (when special
  13928. (cond
  13929. ((and (looking-at org-complex-heading-regexp)
  13930. (= (char-after (match-end 1)) ?\ ))
  13931. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13932. (point-at-eol)))
  13933. (goto-char
  13934. (if (eq special t)
  13935. (cond ((> pos refpos) refpos)
  13936. ((= pos (point)) refpos)
  13937. (t (point)))
  13938. (cond ((> pos (point)) (point))
  13939. ((not (eq last-command this-command)) (point))
  13940. (t refpos)))))
  13941. ((org-at-item-p)
  13942. (goto-char
  13943. (if (eq special t)
  13944. (cond ((> pos (match-end 4)) (match-end 4))
  13945. ((= pos (point)) (match-end 4))
  13946. (t (point)))
  13947. (cond ((> pos (point)) (point))
  13948. ((not (eq last-command this-command)) (point))
  13949. (t (match-end 4))))))))
  13950. (org-no-warnings
  13951. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13952. (defun org-end-of-line (&optional arg)
  13953. "Go to the end of the line.
  13954. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13955. first attempt, and only move to after the tags when the cursor is already
  13956. beyond the end of the headline."
  13957. (interactive "P")
  13958. (let ((special (if (consp org-special-ctrl-a/e)
  13959. (cdr org-special-ctrl-a/e)
  13960. org-special-ctrl-a/e)))
  13961. (if (or (not special)
  13962. (not (org-on-heading-p))
  13963. arg)
  13964. (call-interactively (if (fboundp 'move-end-of-line)
  13965. 'move-end-of-line
  13966. 'end-of-line))
  13967. (let ((pos (point)))
  13968. (beginning-of-line 1)
  13969. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13970. (if (eq special t)
  13971. (if (or (< pos (match-beginning 1))
  13972. (= pos (match-end 0)))
  13973. (goto-char (match-beginning 1))
  13974. (goto-char (match-end 0)))
  13975. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13976. (goto-char (match-end 0))
  13977. (goto-char (match-beginning 1))))
  13978. (call-interactively (if (fboundp 'move-end-of-line)
  13979. 'move-end-of-line
  13980. 'end-of-line)))))
  13981. (org-no-warnings
  13982. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13983. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13984. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13985. (defun org-kill-line (&optional arg)
  13986. "Kill line, to tags or end of line."
  13987. (interactive "P")
  13988. (cond
  13989. ((or (not org-special-ctrl-k)
  13990. (bolp)
  13991. (not (org-on-heading-p)))
  13992. (call-interactively 'kill-line))
  13993. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13994. (kill-region (point) (match-beginning 1))
  13995. (org-set-tags nil t))
  13996. (t (kill-region (point) (point-at-eol)))))
  13997. (define-key org-mode-map "\C-k" 'org-kill-line)
  13998. (defun org-yank (&optional arg)
  13999. "Yank. If the kill is a subtree, treat it specially.
  14000. This command will look at the current kill and check if is a single
  14001. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14002. cursor is at the beginning of a line or after the stars of a currently
  14003. empty headline, then the yank is handled specially. How exactly depends
  14004. on the value of the following variables, both set by default.
  14005. org-yank-folded-subtrees
  14006. When set, the subtree(s) will be folded after insertion, but only
  14007. if doing so would now swallow text after the yanked text.
  14008. org-yank-adjusted-subtrees
  14009. When set, the subtree will be promoted or demoted in order to
  14010. fit into the local outline tree structure, which means that the level
  14011. will be adjusted so that it becomes the smaller one of the two
  14012. *visible* surrounding headings.
  14013. Any prefix to this command will cause `yank' to be called directly with
  14014. no special treatment. In particular, a simple `C-u' prefix will just
  14015. plainly yank the text as it is.
  14016. \[1] Basically, the test checks if the first non-white line is a heading
  14017. and if there are no other headings with fewer stars."
  14018. (interactive "P")
  14019. (setq this-command 'yank)
  14020. (if arg
  14021. (call-interactively 'yank)
  14022. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14023. (and (org-kill-is-subtree-p)
  14024. (or (bolp)
  14025. (and (looking-at "[ \t]*$")
  14026. (string-match
  14027. "\\`\\*+\\'"
  14028. (buffer-substring (point-at-bol) (point)))))))
  14029. swallowp)
  14030. (cond
  14031. ((and subtreep org-yank-folded-subtrees)
  14032. (let ((beg (point))
  14033. end)
  14034. (if (and subtreep org-yank-adjusted-subtrees)
  14035. (org-paste-subtree nil nil 'for-yank)
  14036. (call-interactively 'yank))
  14037. (setq end (point))
  14038. (goto-char beg)
  14039. (when (and (bolp) subtreep
  14040. (not (setq swallowp
  14041. (org-yank-folding-would-swallow-text beg end))))
  14042. (or (looking-at outline-regexp)
  14043. (re-search-forward (concat "^" outline-regexp) end t))
  14044. (while (and (< (point) end) (looking-at outline-regexp))
  14045. (hide-subtree)
  14046. (org-cycle-show-empty-lines 'folded)
  14047. (condition-case nil
  14048. (outline-forward-same-level 1)
  14049. (error (goto-char end)))))
  14050. (when swallowp
  14051. (message
  14052. "Yanked text not folded because that would swallow text"))
  14053. (goto-char end)
  14054. (skip-chars-forward " \t\n\r")
  14055. (beginning-of-line 1)
  14056. (push-mark beg 'nomsg)))
  14057. ((and subtreep org-yank-adjusted-subtrees)
  14058. (let ((beg (point-at-bol)))
  14059. (org-paste-subtree nil nil 'for-yank)
  14060. (push-mark beg 'nomsg)))
  14061. (t
  14062. (call-interactively 'yank))))))
  14063. (defun org-yank-folding-would-swallow-text (beg end)
  14064. "Would hide-subtree at BEG swallow any text after END?"
  14065. (let (level)
  14066. (save-excursion
  14067. (goto-char beg)
  14068. (when (or (looking-at outline-regexp)
  14069. (re-search-forward (concat "^" outline-regexp) end t))
  14070. (setq level (org-outline-level)))
  14071. (goto-char end)
  14072. (skip-chars-forward " \t\r\n\v\f")
  14073. (if (or (eobp)
  14074. (and (bolp) (looking-at org-outline-regexp)
  14075. (<= (org-outline-level) level)))
  14076. nil ; Nothing would be swallowed
  14077. t)))) ; something would swallow
  14078. (define-key org-mode-map "\C-y" 'org-yank)
  14079. (defun org-invisible-p ()
  14080. "Check if point is at a character currently not visible."
  14081. ;; Early versions of noutline don't have `outline-invisible-p'.
  14082. (if (fboundp 'outline-invisible-p)
  14083. (outline-invisible-p)
  14084. (get-char-property (point) 'invisible)))
  14085. (defun org-invisible-p2 ()
  14086. "Check if point is at a character currently not visible."
  14087. (save-excursion
  14088. (if (and (eolp) (not (bobp))) (backward-char 1))
  14089. ;; Early versions of noutline don't have `outline-invisible-p'.
  14090. (if (fboundp 'outline-invisible-p)
  14091. (outline-invisible-p)
  14092. (get-char-property (point) 'invisible))))
  14093. (defun org-back-to-heading (&optional invisible-ok)
  14094. "Call `outline-back-to-heading', but provide a better error message."
  14095. (condition-case nil
  14096. (outline-back-to-heading invisible-ok)
  14097. (error (error "Before first headline at position %d in buffer %s"
  14098. (point) (current-buffer)))))
  14099. (defun org-before-first-heading-p ()
  14100. "Before first heading?"
  14101. (save-excursion
  14102. (null (re-search-backward "^\\*+ " nil t))))
  14103. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14104. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14105. (defun org-at-heading-or-item-p ()
  14106. (or (org-on-heading-p) (org-at-item-p)))
  14107. (defun org-on-target-p ()
  14108. (or (org-in-regexp org-radio-target-regexp)
  14109. (org-in-regexp org-target-regexp)))
  14110. (defun org-up-heading-all (arg)
  14111. "Move to the heading line of which the present line is a subheading.
  14112. This function considers both visible and invisible heading lines.
  14113. With argument, move up ARG levels."
  14114. (if (fboundp 'outline-up-heading-all)
  14115. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14116. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14117. (defun org-up-heading-safe ()
  14118. "Move to the heading line of which the present line is a subheading.
  14119. This version will not throw an error. It will return the level of the
  14120. headline found, or nil if no higher level is found."
  14121. (let (start-level re)
  14122. (org-back-to-heading t)
  14123. (setq start-level (funcall outline-level))
  14124. (if (equal start-level 1)
  14125. nil
  14126. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14127. (if (re-search-backward re nil t)
  14128. (funcall outline-level)))))
  14129. (defun org-first-sibling-p ()
  14130. "Is this heading the first child of its parents?"
  14131. (interactive)
  14132. (let ((re (concat "^" outline-regexp))
  14133. level l)
  14134. (unless (org-at-heading-p t)
  14135. (error "Not at a heading"))
  14136. (setq level (funcall outline-level))
  14137. (save-excursion
  14138. (if (not (re-search-backward re nil t))
  14139. t
  14140. (setq l (funcall outline-level))
  14141. (< l level)))))
  14142. (defun org-goto-sibling (&optional previous)
  14143. "Goto the next sibling, even if it is invisible.
  14144. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14145. when a sibling was found. When none is found, return nil and don't
  14146. move point."
  14147. (let ((fun (if previous 're-search-backward 're-search-forward))
  14148. (pos (point))
  14149. (re (concat "^" outline-regexp))
  14150. level l)
  14151. (when (condition-case nil (org-back-to-heading t) (error nil))
  14152. (setq level (funcall outline-level))
  14153. (catch 'exit
  14154. (or previous (forward-char 1))
  14155. (while (funcall fun re nil t)
  14156. (setq l (funcall outline-level))
  14157. (when (< l level) (goto-char pos) (throw 'exit nil))
  14158. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14159. (goto-char pos)
  14160. nil))))
  14161. (defun org-show-siblings ()
  14162. "Show all siblings of the current headline."
  14163. (save-excursion
  14164. (while (org-goto-sibling) (org-flag-heading nil)))
  14165. (save-excursion
  14166. (while (org-goto-sibling 'previous)
  14167. (org-flag-heading nil))))
  14168. (defun org-show-hidden-entry ()
  14169. "Show an entry where even the heading is hidden."
  14170. (save-excursion
  14171. (org-show-entry)))
  14172. (defun org-flag-heading (flag &optional entry)
  14173. "Flag the current heading. FLAG non-nil means make invisible.
  14174. When ENTRY is non-nil, show the entire entry."
  14175. (save-excursion
  14176. (org-back-to-heading t)
  14177. ;; Check if we should show the entire entry
  14178. (if entry
  14179. (progn
  14180. (org-show-entry)
  14181. (save-excursion
  14182. (and (outline-next-heading)
  14183. (org-flag-heading nil))))
  14184. (outline-flag-region (max (point-min) (1- (point)))
  14185. (save-excursion (outline-end-of-heading) (point))
  14186. flag))))
  14187. (defun org-forward-same-level (arg)
  14188. "Move forward to the ARG'th subheading at same level as this one.
  14189. Stop at the first and last subheadings of a superior heading.
  14190. This is like outline-forward-same-level, but invisible headings are ok."
  14191. (interactive "p")
  14192. (org-back-to-heading t)
  14193. (while (> arg 0)
  14194. (let ((point-to-move-to (save-excursion
  14195. (org-get-next-sibling))))
  14196. (if point-to-move-to
  14197. (progn
  14198. (goto-char point-to-move-to)
  14199. (setq arg (1- arg)))
  14200. (progn
  14201. (setq arg 0)
  14202. (error "No following same-level heading"))))))
  14203. (defun org-get-next-sibling ()
  14204. "Move to next heading of the same level, and return point.
  14205. If there is no such heading, return nil.
  14206. This is like outline-next-sibling, but invisible headings are ok."
  14207. (let ((level (funcall outline-level)))
  14208. (outline-next-heading)
  14209. (while (and (not (eobp)) (> (funcall outline-level) level))
  14210. (outline-next-heading))
  14211. (if (or (eobp) (< (funcall outline-level) level))
  14212. nil
  14213. (point))))
  14214. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14215. ;; This is an exact copy of the original function, but it uses
  14216. ;; `org-back-to-heading', to make it work also in invisible
  14217. ;; trees. And is uses an invisible-OK argument.
  14218. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14219. (org-back-to-heading invisible-OK)
  14220. (let ((first t)
  14221. (level (funcall outline-level)))
  14222. (while (and (not (eobp))
  14223. (or first (> (funcall outline-level) level)))
  14224. (setq first nil)
  14225. (outline-next-heading))
  14226. (unless to-heading
  14227. (if (memq (preceding-char) '(?\n ?\^M))
  14228. (progn
  14229. ;; Go to end of line before heading
  14230. (forward-char -1)
  14231. (if (memq (preceding-char) '(?\n ?\^M))
  14232. ;; leave blank line before heading
  14233. (forward-char -1))))))
  14234. (point))
  14235. (defun org-show-subtree ()
  14236. "Show everything after this heading at deeper levels."
  14237. (outline-flag-region
  14238. (point)
  14239. (save-excursion
  14240. (outline-end-of-subtree) (outline-next-heading) (point))
  14241. nil))
  14242. (defun org-show-entry ()
  14243. "Show the body directly following this heading.
  14244. Show the heading too, if it is currently invisible."
  14245. (interactive)
  14246. (save-excursion
  14247. (condition-case nil
  14248. (progn
  14249. (org-back-to-heading t)
  14250. (outline-flag-region
  14251. (max (point-min) (1- (point)))
  14252. (save-excursion
  14253. (if (re-search-forward
  14254. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14255. (match-beginning 1)
  14256. (point-max)))
  14257. nil))
  14258. (error nil))))
  14259. (defun org-make-options-regexp (kwds &optional extra)
  14260. "Make a regular expression for keyword lines."
  14261. (concat
  14262. "^"
  14263. "#?[ \t]*\\+\\("
  14264. (mapconcat 'regexp-quote kwds "\\|")
  14265. (if extra (concat "\\|" extra))
  14266. "\\):[ \t]*"
  14267. "\\(.+\\)"))
  14268. ;; Make isearch reveal the necessary context
  14269. (defun org-isearch-end ()
  14270. "Reveal context after isearch exits."
  14271. (when isearch-success ; only if search was successful
  14272. (if (featurep 'xemacs)
  14273. ;; Under XEmacs, the hook is run in the correct place,
  14274. ;; we directly show the context.
  14275. (org-show-context 'isearch)
  14276. ;; In Emacs the hook runs *before* restoring the overlays.
  14277. ;; So we have to use a one-time post-command-hook to do this.
  14278. ;; (Emacs 22 has a special variable, see function `org-mode')
  14279. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14280. isearch-mode-end-hook-quit)
  14281. ;; Only when the isearch was not quitted.
  14282. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14283. 'append 'local)))))
  14284. (defun org-isearch-post-command ()
  14285. "Remove self from hook, and show context."
  14286. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14287. (org-show-context 'isearch))
  14288. ;;;; Integration with and fixes for other packages
  14289. ;;; Imenu support
  14290. (defvar org-imenu-markers nil
  14291. "All markers currently used by Imenu.")
  14292. (make-variable-buffer-local 'org-imenu-markers)
  14293. (defun org-imenu-new-marker (&optional pos)
  14294. "Return a new marker for use by Imenu, and remember the marker."
  14295. (let ((m (make-marker)))
  14296. (move-marker m (or pos (point)))
  14297. (push m org-imenu-markers)
  14298. m))
  14299. (defun org-imenu-get-tree ()
  14300. "Produce the index for Imenu."
  14301. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14302. (setq org-imenu-markers nil)
  14303. (let* ((n org-imenu-depth)
  14304. (re (concat "^" outline-regexp))
  14305. (subs (make-vector (1+ n) nil))
  14306. (last-level 0)
  14307. m level head)
  14308. (save-excursion
  14309. (save-restriction
  14310. (widen)
  14311. (goto-char (point-max))
  14312. (while (re-search-backward re nil t)
  14313. (setq level (org-reduced-level (funcall outline-level)))
  14314. (when (<= level n)
  14315. (looking-at org-complex-heading-regexp)
  14316. (setq head (org-link-display-format
  14317. (org-match-string-no-properties 4))
  14318. m (org-imenu-new-marker))
  14319. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14320. (if (>= level last-level)
  14321. (push (cons head m) (aref subs level))
  14322. (push (cons head (aref subs (1+ level))) (aref subs level))
  14323. (loop for i from (1+ level) to n do (aset subs i nil)))
  14324. (setq last-level level)))))
  14325. (aref subs 1)))
  14326. (eval-after-load "imenu"
  14327. '(progn
  14328. (add-hook 'imenu-after-jump-hook
  14329. (lambda ()
  14330. (if (eq major-mode 'org-mode)
  14331. (org-show-context 'org-goto))))))
  14332. (defun org-link-display-format (link)
  14333. "Replace a link with either the description, or the link target
  14334. if no description is present"
  14335. (save-match-data
  14336. (if (string-match org-bracket-link-analytic-regexp link)
  14337. (replace-match (or (match-string 5 link)
  14338. (concat (match-string 1 link)
  14339. (match-string 3 link)))
  14340. nil nil link)
  14341. link)))
  14342. ;; Speedbar support
  14343. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14344. "Overlay marking the agenda restriction line in speedbar.")
  14345. (org-overlay-put org-speedbar-restriction-lock-overlay
  14346. 'face 'org-agenda-restriction-lock)
  14347. (org-overlay-put org-speedbar-restriction-lock-overlay
  14348. 'help-echo "Agendas are currently limited to this item.")
  14349. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14350. (defun org-speedbar-set-agenda-restriction ()
  14351. "Restrict future agenda commands to the location at point in speedbar.
  14352. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14353. (interactive)
  14354. (require 'org-agenda)
  14355. (let (p m tp np dir txt)
  14356. (cond
  14357. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14358. 'org-imenu t))
  14359. (setq m (get-text-property p 'org-imenu-marker))
  14360. (save-excursion
  14361. (save-restriction
  14362. (set-buffer (marker-buffer m))
  14363. (goto-char m)
  14364. (org-agenda-set-restriction-lock 'subtree))))
  14365. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14366. 'speedbar-function 'speedbar-find-file))
  14367. (setq tp (previous-single-property-change
  14368. (1+ p) 'speedbar-function)
  14369. np (next-single-property-change
  14370. tp 'speedbar-function)
  14371. dir (speedbar-line-directory)
  14372. txt (buffer-substring-no-properties (or tp (point-min))
  14373. (or np (point-max))))
  14374. (save-excursion
  14375. (save-restriction
  14376. (set-buffer (find-file-noselect
  14377. (let ((default-directory dir))
  14378. (expand-file-name txt))))
  14379. (unless (org-mode-p)
  14380. (error "Cannot restrict to non-Org-mode file"))
  14381. (org-agenda-set-restriction-lock 'file))))
  14382. (t (error "Don't know how to restrict Org-mode's agenda")))
  14383. (org-move-overlay org-speedbar-restriction-lock-overlay
  14384. (point-at-bol) (point-at-eol))
  14385. (setq current-prefix-arg nil)
  14386. (org-agenda-maybe-redo)))
  14387. (eval-after-load "speedbar"
  14388. '(progn
  14389. (speedbar-add-supported-extension ".org")
  14390. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14391. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14392. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14393. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14394. (add-hook 'speedbar-visiting-tag-hook
  14395. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14396. ;;; Fixes and Hacks for problems with other packages
  14397. ;; Make flyspell not check words in links, to not mess up our keymap
  14398. (defun org-mode-flyspell-verify ()
  14399. "Don't let flyspell put overlays at active buttons."
  14400. (not (get-text-property (point) 'keymap)))
  14401. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14402. (eval-after-load "bookmark"
  14403. '(if (boundp 'bookmark-after-jump-hook)
  14404. ;; We can use the hook
  14405. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14406. ;; Hook not available, use advice
  14407. (defadvice bookmark-jump (after org-make-visible activate)
  14408. "Make the position visible."
  14409. (org-bookmark-jump-unhide))))
  14410. ;; Make sure saveplace show the location if it was hidden
  14411. (eval-after-load "saveplace"
  14412. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14413. "Make the position visible."
  14414. (org-bookmark-jump-unhide)))
  14415. (defun org-bookmark-jump-unhide ()
  14416. "Unhide the current position, to show the bookmark location."
  14417. (and (org-mode-p)
  14418. (or (org-invisible-p)
  14419. (save-excursion (goto-char (max (point-min) (1- (point))))
  14420. (org-invisible-p)))
  14421. (org-show-context 'bookmark-jump)))
  14422. ;; Make session.el ignore our circular variable
  14423. (eval-after-load "session"
  14424. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14425. ;;;; Experimental code
  14426. (defun org-closed-in-range ()
  14427. "Sparse tree of items closed in a certain time range.
  14428. Still experimental, may disappear in the future."
  14429. (interactive)
  14430. ;; Get the time interval from the user.
  14431. (let* ((time1 (time-to-seconds
  14432. (org-read-date nil 'to-time nil "Starting date: ")))
  14433. (time2 (time-to-seconds
  14434. (org-read-date nil 'to-time nil "End date:")))
  14435. ;; callback function
  14436. (callback (lambda ()
  14437. (let ((time
  14438. (time-to-seconds
  14439. (apply 'encode-time
  14440. (org-parse-time-string
  14441. (match-string 1))))))
  14442. ;; check if time in interval
  14443. (and (>= time time1) (<= time time2))))))
  14444. ;; make tree, check each match with the callback
  14445. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14446. ;;;; Finish up
  14447. (provide 'org)
  14448. (run-hooks 'org-load-hook)
  14449. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14450. ;;; org.el ends here