org.el 627 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813
  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.28trans
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-src)
  86. (require 'org-footnote)
  87. ;;;; Customization variables
  88. ;;; Version
  89. (defconst org-version "6.28trans"
  90. "The version number of the file org.el.")
  91. (defun org-version (&optional here)
  92. "Show the org-mode version in the echo area.
  93. With prefix arg HERE, insert it at point."
  94. (interactive "P")
  95. (let ((version (format "Org-mode version %s" org-version)))
  96. (message version)
  97. (if here
  98. (insert version))))
  99. ;;; Compatibility constants
  100. ;;; The custom variables
  101. (defgroup org nil
  102. "Outline-based notes management and organizer."
  103. :tag "Org"
  104. :group 'outlines
  105. :group 'hypermedia
  106. :group 'calendar)
  107. (defcustom org-load-hook nil
  108. "Hook that is run after org.el has been loaded."
  109. :group 'org
  110. :type 'hook)
  111. (defvar org-modules) ; defined below
  112. (defvar org-modules-loaded nil
  113. "Have the modules been loaded already?")
  114. (defun org-load-modules-maybe (&optional force)
  115. "Load all extensions listed in `org-modules'."
  116. (when (or force (not org-modules-loaded))
  117. (mapc (lambda (ext)
  118. (condition-case nil (require ext)
  119. (error (message "Problems while trying to load feature `%s'" ext))))
  120. org-modules)
  121. (setq org-modules-loaded t)))
  122. (defun org-set-modules (var value)
  123. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  124. (set var value)
  125. (when (featurep 'org)
  126. (org-load-modules-maybe 'force)))
  127. (when (org-bound-and-true-p org-modules)
  128. (let ((a (member 'org-infojs org-modules)))
  129. (and a (setcar a 'org-jsinfo))))
  130. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  131. "Modules that should always be loaded together with org.el.
  132. If a description starts with <C>, the file is not part of Emacs
  133. and loading it will require that you have downloaded and properly installed
  134. the org-mode distribution.
  135. You can also use this system to load external packages (i.e. neither Org
  136. core modules, not modules from the CONTRIB directory). Just add symbols
  137. to the end of the list. If the package is called org-xyz.el, then you need
  138. to add the symbol `xyz', and the package must have a call to
  139. (provide 'org-xyz)"
  140. :group 'org
  141. :set 'org-set-modules
  142. :type
  143. '(set :greedy t
  144. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  145. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  146. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  147. (const :tag " id: Global IDs for identifying entries" org-id)
  148. (const :tag " info: Links to Info nodes" org-info)
  149. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  150. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  151. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  152. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  153. (const :tag " mew Links to Mew folders/messages" org-mew)
  154. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  155. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  156. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  157. (const :tag " vm: Links to VM folders/messages" org-vm)
  158. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  159. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  160. (const :tag " mouse: Additional mouse support" org-mouse)
  161. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  162. (const :tag "C annotation-helper: Call Remember directly from Browser (OBSOLETE, use org-protocol)" org-annotation-helper)
  163. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  164. (const :tag "C browser-url: Store link, directly from Browser (OBSOLETE, use org-protocol)" org-browser-url)
  165. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  166. (const :tag "C collector: Collect properties into tables" org-collector)
  167. (const :tag "C depend: TODO dependencies for Org-mode (PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  168. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  169. (const :tag "C eval: Include command output as text" org-eval)
  170. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  171. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  172. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  173. (const :tag "C interactive-query: Interactive modification of tags query (PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  174. (const :tag "C jira Add a jira:ticket protocol to Org" org-jira)
  175. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  176. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  177. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  178. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  179. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  180. (const :tag "C R: Computation using the R language" org-R)
  181. (const :tag "C registry: A registry for Org links" org-registry)
  182. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  183. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  184. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  185. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  186. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  187. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  188. (defcustom org-support-shift-select nil
  189. "Non-nil means, make shift-cursor commands select text when possible.
  190. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  191. selecting a region, or enlarge thusly regions started in this way.
  192. In Org-mode, in special contexts, these same keys are used for other
  193. purposes, important enough to compete with shift selection. Org tries
  194. to balance these needs by supporting `shift-select-mode' outside these
  195. special contexts, under control of this variable.
  196. The default of this variable is nil, to avoid confusing behavior. Shifted
  197. cursor keys will then execute Org commands in the following contexts:
  198. - on a headline, changing TODO state (left/right) and priority (up/down)
  199. - on a time stamp, changing the time
  200. - in a plain list item, changing the bullet type
  201. - in a property definition line, switching between allowed values
  202. - in the BEGIN line of a clock table (changing the time block).
  203. Outside these contexts, the commands will throw an error.
  204. When this variable is t and the cursor is not in a special context,
  205. Org-mode will support shift-selection for making and enlarging regions.
  206. To make this more effective, the bullet cycling will no longer happen
  207. anywhere in an item line, but only if the cursor is exactly on the bullet.
  208. If you set this variable to the symbol `always', then the keys
  209. will not be special in headlines, property lines, and item lines, to make
  210. shift selection work there as well. If this is what you want, you can
  211. use the following alternative commands: `C-c C-t' and `C-c ,' to
  212. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  213. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  214. edited by hand or in column view.
  215. However, when the cursor is on a timestamp, shift-cursor commands
  216. will still edit the time stamp - this is just too good to give up.
  217. XEmacs user should have this variable set to nil, because shift-select-mode
  218. is Emacs 23 only."
  219. :group 'org
  220. :type '(choice
  221. (const :tag "Never" nil)
  222. (const :tag "When outside special context" t)
  223. (const :tag "Everywhere except timestamps" always)))
  224. (defgroup org-startup nil
  225. "Options concerning startup of Org-mode."
  226. :tag "Org Startup"
  227. :group 'org)
  228. (defcustom org-startup-folded t
  229. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  230. This can also be configured on a per-file basis by adding one of
  231. the following lines anywhere in the buffer:
  232. #+STARTUP: fold
  233. #+STARTUP: nofold
  234. #+STARTUP: content"
  235. :group 'org-startup
  236. :type '(choice
  237. (const :tag "nofold: show all" nil)
  238. (const :tag "fold: overview" t)
  239. (const :tag "content: all headlines" content)))
  240. (defcustom org-startup-truncated t
  241. "Non-nil means, entering Org-mode will set `truncate-lines'.
  242. This is useful since some lines containing links can be very long and
  243. uninteresting. Also tables look terrible when wrapped."
  244. :group 'org-startup
  245. :type 'boolean)
  246. (defcustom org-startup-align-all-tables nil
  247. "Non-nil means, align all tables when visiting a file.
  248. This is useful when the column width in tables is forced with <N> cookies
  249. in table fields. Such tables will look correct only after the first re-align.
  250. This can also be configured on a per-file basis by adding one of
  251. the following lines anywhere in the buffer:
  252. #+STARTUP: align
  253. #+STARTUP: noalign"
  254. :group 'org-startup
  255. :type 'boolean)
  256. (defcustom org-insert-mode-line-in-empty-file nil
  257. "Non-nil means insert the first line setting Org-mode in empty files.
  258. When the function `org-mode' is called interactively in an empty file, this
  259. normally means that the file name does not automatically trigger Org-mode.
  260. To ensure that the file will always be in Org-mode in the future, a
  261. line enforcing Org-mode will be inserted into the buffer, if this option
  262. has been set."
  263. :group 'org-startup
  264. :type 'boolean)
  265. (defcustom org-replace-disputed-keys nil
  266. "Non-nil means use alternative key bindings for some keys.
  267. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  268. These keys are also used by other packages like shift-selection-mode'
  269. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  270. If you want to use Org-mode together with one of these other modes,
  271. or more generally if you would like to move some Org-mode commands to
  272. other keys, set this variable and configure the keys with the variable
  273. `org-disputed-keys'.
  274. This option is only relevant at load-time of Org-mode, and must be set
  275. *before* org.el is loaded. Changing it requires a restart of Emacs to
  276. become effective."
  277. :group 'org-startup
  278. :type 'boolean)
  279. (defcustom org-use-extra-keys nil
  280. "Non-nil means use extra key sequence definitions for certain
  281. commands. This happens automatically if you run XEmacs or if
  282. window-system is nil. This variable lets you do the same
  283. manually. You must set it before loading org.
  284. Example: on Carbon Emacs 22 running graphically, with an external
  285. keyboard on a Powerbook, the default way of setting M-left might
  286. not work for either Alt or ESC. Setting this variable will make
  287. it work for ESC."
  288. :group 'org-startup
  289. :type 'boolean)
  290. (if (fboundp 'defvaralias)
  291. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  292. (defcustom org-disputed-keys
  293. '(([(shift up)] . [(meta p)])
  294. ([(shift down)] . [(meta n)])
  295. ([(shift left)] . [(meta -)])
  296. ([(shift right)] . [(meta +)])
  297. ([(control shift right)] . [(meta shift +)])
  298. ([(control shift left)] . [(meta shift -)]))
  299. "Keys for which Org-mode and other modes compete.
  300. This is an alist, cars are the default keys, second element specifies
  301. the alternative to use when `org-replace-disputed-keys' is t.
  302. Keys can be specified in any syntax supported by `define-key'.
  303. The value of this option takes effect only at Org-mode's startup,
  304. therefore you'll have to restart Emacs to apply it after changing."
  305. :group 'org-startup
  306. :type 'alist)
  307. (defun org-key (key)
  308. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  309. Or return the original if not disputed."
  310. (if org-replace-disputed-keys
  311. (let* ((nkey (key-description key))
  312. (x (org-find-if (lambda (x)
  313. (equal (key-description (car x)) nkey))
  314. org-disputed-keys)))
  315. (if x (cdr x) key))
  316. key))
  317. (defun org-find-if (predicate seq)
  318. (catch 'exit
  319. (while seq
  320. (if (funcall predicate (car seq))
  321. (throw 'exit (car seq))
  322. (pop seq)))))
  323. (defun org-defkey (keymap key def)
  324. "Define a key, possibly translated, as returned by `org-key'."
  325. (define-key keymap (org-key key) def))
  326. (defcustom org-ellipsis nil
  327. "The ellipsis to use in the Org-mode outline.
  328. When nil, just use the standard three dots. When a string, use that instead,
  329. When a face, use the standard 3 dots, but with the specified face.
  330. The change affects only Org-mode (which will then use its own display table).
  331. Changing this requires executing `M-x org-mode' in a buffer to become
  332. effective."
  333. :group 'org-startup
  334. :type '(choice (const :tag "Default" nil)
  335. (face :tag "Face" :value org-warning)
  336. (string :tag "String" :value "...#")))
  337. (defvar org-display-table nil
  338. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  339. (defgroup org-keywords nil
  340. "Keywords in Org-mode."
  341. :tag "Org Keywords"
  342. :group 'org)
  343. (defcustom org-deadline-string "DEADLINE:"
  344. "String to mark deadline entries.
  345. A deadline is this string, followed by a time stamp. Should be a word,
  346. terminated by a colon. You can insert a schedule keyword and
  347. a timestamp with \\[org-deadline].
  348. Changes become only effective after restarting Emacs."
  349. :group 'org-keywords
  350. :type 'string)
  351. (defcustom org-scheduled-string "SCHEDULED:"
  352. "String to mark scheduled TODO entries.
  353. A schedule is this string, followed by a time stamp. Should be a word,
  354. terminated by a colon. You can insert a schedule keyword and
  355. a timestamp with \\[org-schedule].
  356. Changes become only effective after restarting Emacs."
  357. :group 'org-keywords
  358. :type 'string)
  359. (defcustom org-closed-string "CLOSED:"
  360. "String used as the prefix for timestamps logging closing a TODO entry."
  361. :group 'org-keywords
  362. :type 'string)
  363. (defcustom org-clock-string "CLOCK:"
  364. "String used as prefix for timestamps clocking work hours on an item."
  365. :group 'org-keywords
  366. :type 'string)
  367. (defcustom org-comment-string "COMMENT"
  368. "Entries starting with this keyword will never be exported.
  369. An entry can be toggled between COMMENT and normal with
  370. \\[org-toggle-comment].
  371. Changes become only effective after restarting Emacs."
  372. :group 'org-keywords
  373. :type 'string)
  374. (defcustom org-quote-string "QUOTE"
  375. "Entries starting with this keyword will be exported in fixed-width font.
  376. Quoting applies only to the text in the entry following the headline, and does
  377. not extend beyond the next headline, even if that is lower level.
  378. An entry can be toggled between QUOTE and normal with
  379. \\[org-toggle-fixed-width-section]."
  380. :group 'org-keywords
  381. :type 'string)
  382. (defconst org-repeat-re
  383. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  384. "Regular expression for specifying repeated events.
  385. After a match, group 1 contains the repeat expression.")
  386. (defgroup org-structure nil
  387. "Options concerning the general structure of Org-mode files."
  388. :tag "Org Structure"
  389. :group 'org)
  390. (defgroup org-reveal-location nil
  391. "Options about how to make context of a location visible."
  392. :tag "Org Reveal Location"
  393. :group 'org-structure)
  394. (defconst org-context-choice
  395. '(choice
  396. (const :tag "Always" t)
  397. (const :tag "Never" nil)
  398. (repeat :greedy t :tag "Individual contexts"
  399. (cons
  400. (choice :tag "Context"
  401. (const agenda)
  402. (const org-goto)
  403. (const occur-tree)
  404. (const tags-tree)
  405. (const link-search)
  406. (const mark-goto)
  407. (const bookmark-jump)
  408. (const isearch)
  409. (const default))
  410. (boolean))))
  411. "Contexts for the reveal options.")
  412. (defcustom org-show-hierarchy-above '((default . t))
  413. "Non-nil means, show full hierarchy when revealing a location.
  414. Org-mode often shows locations in an org-mode file which might have
  415. been invisible before. When this is set, the hierarchy of headings
  416. above the exposed location is shown.
  417. Turning this off for example for sparse trees makes them very compact.
  418. Instead of t, this can also be an alist specifying this option for different
  419. contexts. Valid contexts are
  420. agenda when exposing an entry from the agenda
  421. org-goto when using the command `org-goto' on key C-c C-j
  422. occur-tree when using the command `org-occur' on key C-c /
  423. tags-tree when constructing a sparse tree based on tags matches
  424. link-search when exposing search matches associated with a link
  425. mark-goto when exposing the jump goal of a mark
  426. bookmark-jump when exposing a bookmark location
  427. isearch when exiting from an incremental search
  428. default default for all contexts not set explicitly"
  429. :group 'org-reveal-location
  430. :type org-context-choice)
  431. (defcustom org-show-following-heading '((default . nil))
  432. "Non-nil means, show following heading when revealing a location.
  433. Org-mode often shows locations in an org-mode file which might have
  434. been invisible before. When this is set, the heading following the
  435. match is shown.
  436. Turning this off for example for sparse trees makes them very compact,
  437. but makes it harder to edit the location of the match. In such a case,
  438. use the command \\[org-reveal] to show more context.
  439. Instead of t, this can also be an alist specifying this option for different
  440. contexts. See `org-show-hierarchy-above' for valid contexts."
  441. :group 'org-reveal-location
  442. :type org-context-choice)
  443. (defcustom org-show-siblings '((default . nil) (isearch t))
  444. "Non-nil means, show all sibling heading when revealing a location.
  445. Org-mode often shows locations in an org-mode file which might have
  446. been invisible before. When this is set, the sibling of the current entry
  447. heading are all made visible. If `org-show-hierarchy-above' is t,
  448. the same happens on each level of the hierarchy above the current entry.
  449. By default this is on for the isearch context, off for all other contexts.
  450. Turning this off for example for sparse trees makes them very compact,
  451. but makes it harder to edit the location of the match. In such a case,
  452. use the command \\[org-reveal] to show more context.
  453. Instead of t, this can also be an alist specifying this option for different
  454. contexts. See `org-show-hierarchy-above' for valid contexts."
  455. :group 'org-reveal-location
  456. :type org-context-choice)
  457. (defcustom org-show-entry-below '((default . nil))
  458. "Non-nil means, show the entry below a headline when revealing a location.
  459. Org-mode often shows locations in an org-mode file which might have
  460. been invisible before. When this is set, the text below the headline that is
  461. exposed is also shown.
  462. By default this is off for all contexts.
  463. Instead of t, this can also be an alist specifying this option for different
  464. contexts. See `org-show-hierarchy-above' for valid contexts."
  465. :group 'org-reveal-location
  466. :type org-context-choice)
  467. (defcustom org-indirect-buffer-display 'other-window
  468. "How should indirect tree buffers be displayed?
  469. This applies to indirect buffers created with the commands
  470. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  471. Valid values are:
  472. current-window Display in the current window
  473. other-window Just display in another window.
  474. dedicated-frame Create one new frame, and re-use it each time.
  475. new-frame Make a new frame each time. Note that in this case
  476. previously-made indirect buffers are kept, and you need to
  477. kill these buffers yourself."
  478. :group 'org-structure
  479. :group 'org-agenda-windows
  480. :type '(choice
  481. (const :tag "In current window" current-window)
  482. (const :tag "In current frame, other window" other-window)
  483. (const :tag "Each time a new frame" new-frame)
  484. (const :tag "One dedicated frame" dedicated-frame)))
  485. (defgroup org-cycle nil
  486. "Options concerning visibility cycling in Org-mode."
  487. :tag "Org Cycle"
  488. :group 'org-structure)
  489. (defcustom org-cycle-max-level nil
  490. "Maximum level which should still be subject to visibility cycling.
  491. Levels higher than this will, for cycling, be treated as text, not a headline.
  492. When `org-odd-levels-only' is set, a value of N in this variable actually
  493. means 2N-1 stars as the limiting headline.
  494. When nil, cycle all levels.
  495. Note that the limiting level of cycling is also influenced by
  496. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  497. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  498. than its value."
  499. :group 'org-cycle
  500. :type '(choice
  501. (const :tag "No limit" nil)
  502. (integer :tag "Maximum level")))
  503. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  504. "Names of drawers. Drawers are not opened by cycling on the headline above.
  505. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  506. this:
  507. :DRAWERNAME:
  508. .....
  509. :END:
  510. The drawer \"PROPERTIES\" is special for capturing properties through
  511. the property API.
  512. Drawers can be defined on the per-file basis with a line like:
  513. #+DRAWERS: HIDDEN STATE PROPERTIES"
  514. :group 'org-structure
  515. :group 'org-cycle
  516. :type '(repeat (string :tag "Drawer Name")))
  517. (defcustom org-hide-block-startup nil
  518. "Non-nil means, , entering Org-mode will fold all blocks.
  519. This can also be set in on a per-file basis with
  520. #+STARTUP: hideblocks
  521. #+STARTUP: showblocks"
  522. :group 'org-startup
  523. :group 'org-cycle
  524. :type 'boolean)
  525. (defcustom org-cycle-global-at-bob nil
  526. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  527. This makes it possible to do global cycling without having to use S-TAB or
  528. C-u TAB. For this special case to work, the first line of the buffer
  529. must not be a headline - it may be empty or some other text. When used in
  530. this way, `org-cycle-hook' is disables temporarily, to make sure the
  531. cursor stays at the beginning of the buffer.
  532. When this option is nil, don't do anything special at the beginning
  533. of the buffer."
  534. :group 'org-cycle
  535. :type 'boolean)
  536. (defcustom org-cycle-emulate-tab t
  537. "Where should `org-cycle' emulate TAB.
  538. nil Never
  539. white Only in completely white lines
  540. whitestart Only at the beginning of lines, before the first non-white char
  541. t Everywhere except in headlines
  542. exc-hl-bol Everywhere except at the start of a headline
  543. If TAB is used in a place where it does not emulate TAB, the current subtree
  544. visibility is cycled."
  545. :group 'org-cycle
  546. :type '(choice (const :tag "Never" nil)
  547. (const :tag "Only in completely white lines" white)
  548. (const :tag "Before first char in a line" whitestart)
  549. (const :tag "Everywhere except in headlines" t)
  550. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  551. ))
  552. (defcustom org-cycle-separator-lines 2
  553. "Number of empty lines needed to keep an empty line between collapsed trees.
  554. If you leave an empty line between the end of a subtree and the following
  555. headline, this empty line is hidden when the subtree is folded.
  556. Org-mode will leave (exactly) one empty line visible if the number of
  557. empty lines is equal or larger to the number given in this variable.
  558. So the default 2 means, at least 2 empty lines after the end of a subtree
  559. are needed to produce free space between a collapsed subtree and the
  560. following headline.
  561. Special case: when 0, never leave empty lines in collapsed view."
  562. :group 'org-cycle
  563. :type 'integer)
  564. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  565. (defcustom org-pre-cycle-hook nil
  566. "Hook that is run before visibility cycling is happening.
  567. The function(s) in this hook must accept a single argument which indicates
  568. the new state that will be set right after running this hook. The
  569. argument is a symbol. Before a global state change, it can have the values
  570. `overview', `content', or `all'. Before a local state change, it can have
  571. the values `folded', `children', or `subtree'."
  572. :group 'org-cycle
  573. :type 'hook)
  574. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  575. org-cycle-hide-drawers
  576. org-cycle-show-empty-lines
  577. org-optimize-window-after-visibility-change)
  578. "Hook that is run after `org-cycle' has changed the buffer visibility.
  579. The function(s) in this hook must accept a single argument which indicates
  580. the new state that was set by the most recent `org-cycle' command. The
  581. argument is a symbol. After a global state change, it can have the values
  582. `overview', `content', or `all'. After a local state change, it can have
  583. the values `folded', `children', or `subtree'."
  584. :group 'org-cycle
  585. :type 'hook)
  586. (defgroup org-edit-structure nil
  587. "Options concerning structure editing in Org-mode."
  588. :tag "Org Edit Structure"
  589. :group 'org-structure)
  590. (defcustom org-odd-levels-only nil
  591. "Non-nil means, skip even levels and only use odd levels for the outline.
  592. This has the effect that two stars are being added/taken away in
  593. promotion/demotion commands. It also influences how levels are
  594. handled by the exporters.
  595. Changing it requires restart of `font-lock-mode' to become effective
  596. for fontification also in regions already fontified.
  597. You may also set this on a per-file basis by adding one of the following
  598. lines to the buffer:
  599. #+STARTUP: odd
  600. #+STARTUP: oddeven"
  601. :group 'org-edit-structure
  602. :group 'org-font-lock
  603. :type 'boolean)
  604. (defcustom org-adapt-indentation t
  605. "Non-nil means, adapt indentation to outline node level.
  606. When this variable is set, Org assumes that you write outlines by
  607. indenting text in each node to align with the headline (after the stars).
  608. The following issues are influenced by this variable:
  609. - When this is set and the *entire* text in an entry is indented, the
  610. indentation is increased by one space in a demotion command, and
  611. decreased by one in a promotion command. If any line in the entry
  612. body starts with text at column 0, indentation is not changed at all.
  613. - Property drawers and planning information is inserted indented when
  614. this variable s set. When nil, they will not be indented.
  615. - TAB indents a line relative to context. The lines below a headline
  616. will be indented when this variable is set.
  617. Note that this is all about true indentation, by adding and removing
  618. spaces. See also `org-indent.el' which does level-dependent indentation
  619. in a virtual way, i.e. only for display in Emacs."
  620. :group 'org-edit-structure
  621. :type 'boolean)
  622. (defcustom org-special-ctrl-a/e nil
  623. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  624. When t, `C-a' will bring back the cursor to the beginning of the
  625. headline text, i.e. after the stars and after a possible TODO keyword.
  626. In an item, this will be the position after the bullet.
  627. When the cursor is already at that position, another `C-a' will bring
  628. it to the beginning of the line.
  629. `C-e' will jump to the end of the headline, ignoring the presence of tags
  630. in the headline. A second `C-e' will then jump to the true end of the
  631. line, after any tags.
  632. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  633. going to the true line boundary first. Only a directly following, identical
  634. keypress will bring the cursor to the special positions.
  635. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  636. set separately."
  637. :group 'org-edit-structure
  638. :type '(choice
  639. (const :tag "off" nil)
  640. (const :tag "after stars/bullet and before tags first" t)
  641. (const :tag "true line boundary first" reversed)
  642. (cons :tag "Set C-a and C-e separately"
  643. (choice :tag "Special C-a"
  644. (const :tag "off" nil)
  645. (const :tag "after stars/bullet first" t)
  646. (const :tag "before stars/bullet first" reversed))
  647. (choice :tag "Special C-e"
  648. (const :tag "off" nil)
  649. (const :tag "before tags first" t)
  650. (const :tag "after tags first" reversed)))))
  651. (if (fboundp 'defvaralias)
  652. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  653. (defcustom org-special-ctrl-k nil
  654. "Non-nil means `C-k' will behave specially in headlines.
  655. When nil, `C-k' will call the default `kill-line' command.
  656. When t, the following will happen while the cursor is in the headline:
  657. - When the cursor is at the beginning of a headline, kill the entire
  658. line and possible the folded subtree below the line.
  659. - When in the middle of the headline text, kill the headline up to the tags.
  660. - When after the headline text, kill the tags."
  661. :group 'org-edit-structure
  662. :type 'boolean)
  663. (defcustom org-yank-folded-subtrees t
  664. "Non-nil means, when yanking subtrees, fold them.
  665. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  666. it starts with a heading and all other headings in it are either children
  667. or siblings, then fold all the subtrees. However, do this only if no
  668. text after the yank would be swallowed into a folded tree by this action."
  669. :group 'org-edit-structure
  670. :type 'boolean)
  671. (defcustom org-yank-adjusted-subtrees nil
  672. "Non-nil means, when yanking subtrees, adjust the level.
  673. With this setting, `org-paste-subtree' is used to insert the subtree, see
  674. this function for details."
  675. :group 'org-edit-structure
  676. :type 'boolean)
  677. (defcustom org-M-RET-may-split-line '((default . t))
  678. "Non-nil means, M-RET will split the line at the cursor position.
  679. When nil, it will go to the end of the line before making a
  680. new line.
  681. You may also set this option in a different way for different
  682. contexts. Valid contexts are:
  683. headline when creating a new headline
  684. item when creating a new item
  685. table in a table field
  686. default the value to be used for all contexts not explicitly
  687. customized"
  688. :group 'org-structure
  689. :group 'org-table
  690. :type '(choice
  691. (const :tag "Always" t)
  692. (const :tag "Never" nil)
  693. (repeat :greedy t :tag "Individual contexts"
  694. (cons
  695. (choice :tag "Context"
  696. (const headline)
  697. (const item)
  698. (const table)
  699. (const default))
  700. (boolean)))))
  701. (defcustom org-insert-heading-respect-content nil
  702. "Non-nil means, insert new headings after the current subtree.
  703. When nil, the new heading is created directly after the current line.
  704. The commands \\[org-insert-heading-respect-content] and
  705. \\[org-insert-todo-heading-respect-content] turn this variable on
  706. for the duration of the command."
  707. :group 'org-structure
  708. :type 'boolean)
  709. (defcustom org-blank-before-new-entry '((heading . auto)
  710. (plain-list-item . auto))
  711. "Should `org-insert-heading' leave a blank line before new heading/item?
  712. The value is an alist, with `heading' and `plain-list-item' as car,
  713. and a boolean flag as cdr. For plain lists, if the variable
  714. `org-empty-line-terminates-plain-lists' is set, the setting here
  715. is ignored and no empty line is inserted, to keep the list in tact."
  716. :group 'org-edit-structure
  717. :type '(list
  718. (cons (const heading)
  719. (choice (const :tag "Never" nil)
  720. (const :tag "Always" t)
  721. (const :tag "Auto" auto)))
  722. (cons (const plain-list-item)
  723. (choice (const :tag "Never" nil)
  724. (const :tag "Always" t)
  725. (const :tag "Auto" auto)))))
  726. (defcustom org-insert-heading-hook nil
  727. "Hook being run after inserting a new heading."
  728. :group 'org-edit-structure
  729. :type 'hook)
  730. (defcustom org-enable-fixed-width-editor t
  731. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  732. This currently only means, they are never auto-wrapped.
  733. When nil, such lines will be treated like ordinary lines.
  734. See also the QUOTE keyword."
  735. :group 'org-edit-structure
  736. :type 'boolean)
  737. (defcustom org-goto-auto-isearch t
  738. "Non-nil means, typing characters in org-goto starts incremental search."
  739. :group 'org-edit-structure
  740. :type 'boolean)
  741. (defgroup org-sparse-trees nil
  742. "Options concerning sparse trees in Org-mode."
  743. :tag "Org Sparse Trees"
  744. :group 'org-structure)
  745. (defcustom org-highlight-sparse-tree-matches t
  746. "Non-nil means, highlight all matches that define a sparse tree.
  747. The highlights will automatically disappear the next time the buffer is
  748. changed by an edit command."
  749. :group 'org-sparse-trees
  750. :type 'boolean)
  751. (defcustom org-remove-highlights-with-change t
  752. "Non-nil means, any change to the buffer will remove temporary highlights.
  753. Such highlights are created by `org-occur' and `org-clock-display'.
  754. When nil, `C-c C-c needs to be used to get rid of the highlights.
  755. The highlights created by `org-preview-latex-fragment' always need
  756. `C-c C-c' to be removed."
  757. :group 'org-sparse-trees
  758. :group 'org-time
  759. :type 'boolean)
  760. (defcustom org-occur-hook '(org-first-headline-recenter)
  761. "Hook that is run after `org-occur' has constructed a sparse tree.
  762. This can be used to recenter the window to show as much of the structure
  763. as possible."
  764. :group 'org-sparse-trees
  765. :type 'hook)
  766. (defgroup org-imenu-and-speedbar nil
  767. "Options concerning imenu and speedbar in Org-mode."
  768. :tag "Org Imenu and Speedbar"
  769. :group 'org-structure)
  770. (defcustom org-imenu-depth 2
  771. "The maximum level for Imenu access to Org-mode headlines.
  772. This also applied for speedbar access."
  773. :group 'org-imenu-and-speedbar
  774. :type 'integer)
  775. (defgroup org-table nil
  776. "Options concerning tables in Org-mode."
  777. :tag "Org Table"
  778. :group 'org)
  779. (defcustom org-enable-table-editor 'optimized
  780. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  781. When nil, such lines will be treated like ordinary lines.
  782. When equal to the symbol `optimized', the table editor will be optimized to
  783. do the following:
  784. - Automatic overwrite mode in front of whitespace in table fields.
  785. This makes the structure of the table stay in tact as long as the edited
  786. field does not exceed the column width.
  787. - Minimize the number of realigns. Normally, the table is aligned each time
  788. TAB or RET are pressed to move to another field. With optimization this
  789. happens only if changes to a field might have changed the column width.
  790. Optimization requires replacing the functions `self-insert-command',
  791. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  792. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  793. very good at guessing when a re-align will be necessary, but you can always
  794. force one with \\[org-ctrl-c-ctrl-c].
  795. If you would like to use the optimized version in Org-mode, but the
  796. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  797. This variable can be used to turn on and off the table editor during a session,
  798. but in order to toggle optimization, a restart is required.
  799. See also the variable `org-table-auto-blank-field'."
  800. :group 'org-table
  801. :type '(choice
  802. (const :tag "off" nil)
  803. (const :tag "on" t)
  804. (const :tag "on, optimized" optimized)))
  805. (defcustom org-self-insert-cluster-for-undo t
  806. "Non-nil means cluster self-insert commands for undo when possible.
  807. If this is set, then, like in the Emacs command loop, 20 consequtive
  808. characters will be undone together.
  809. This is configurable, because there is some impact on typing performance."
  810. :group 'org-table
  811. :type 'boolean)
  812. (defcustom org-table-tab-recognizes-table.el t
  813. "Non-nil means, TAB will automatically notice a table.el table.
  814. When it sees such a table, it moves point into it and - if necessary -
  815. calls `table-recognize-table'."
  816. :group 'org-table-editing
  817. :type 'boolean)
  818. (defgroup org-link nil
  819. "Options concerning links in Org-mode."
  820. :tag "Org Link"
  821. :group 'org)
  822. (defvar org-link-abbrev-alist-local nil
  823. "Buffer-local version of `org-link-abbrev-alist', which see.
  824. The value of this is taken from the #+LINK lines.")
  825. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  826. (defcustom org-link-abbrev-alist nil
  827. "Alist of link abbreviations.
  828. The car of each element is a string, to be replaced at the start of a link.
  829. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  830. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  831. [[linkkey:tag][description]]
  832. The 'linkkey' must be a word word, starting with a letter, followed
  833. by letters, numbers, '-' or '_'.
  834. If REPLACE is a string, the tag will simply be appended to create the link.
  835. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  836. the placeholder \"%h\" will cause a url-encoded version of the tag to
  837. be inserted at that point (see the function `url-hexify-string').
  838. REPLACE may also be a function that will be called with the tag as the
  839. only argument to create the link, which should be returned as a string.
  840. See the manual for examples."
  841. :group 'org-link
  842. :type '(repeat
  843. (cons
  844. (string :tag "Protocol")
  845. (choice
  846. (string :tag "Format")
  847. (function)))))
  848. (defcustom org-descriptive-links t
  849. "Non-nil means, hide link part and only show description of bracket links.
  850. Bracket links are like [[link][description]]. This variable sets the initial
  851. state in new org-mode buffers. The setting can then be toggled on a
  852. per-buffer basis from the Org->Hyperlinks menu."
  853. :group 'org-link
  854. :type 'boolean)
  855. (defcustom org-link-file-path-type 'adaptive
  856. "How the path name in file links should be stored.
  857. Valid values are:
  858. relative Relative to the current directory, i.e. the directory of the file
  859. into which the link is being inserted.
  860. absolute Absolute path, if possible with ~ for home directory.
  861. noabbrev Absolute path, no abbreviation of home directory.
  862. adaptive Use relative path for files in the current directory and sub-
  863. directories of it. For other files, use an absolute path."
  864. :group 'org-link
  865. :type '(choice
  866. (const relative)
  867. (const absolute)
  868. (const noabbrev)
  869. (const adaptive)))
  870. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  871. "Types of links that should be activated in Org-mode files.
  872. This is a list of symbols, each leading to the activation of a certain link
  873. type. In principle, it does not hurt to turn on most link types - there may
  874. be a small gain when turning off unused link types. The types are:
  875. bracket The recommended [[link][description]] or [[link]] links with hiding.
  876. angular Links in angular brackets that may contain whitespace like
  877. <bbdb:Carsten Dominik>.
  878. plain Plain links in normal text, no whitespace, like http://google.com.
  879. radio Text that is matched by a radio target, see manual for details.
  880. tag Tag settings in a headline (link to tag search).
  881. date Time stamps (link to calendar).
  882. footnote Footnote labels.
  883. Changing this variable requires a restart of Emacs to become effective."
  884. :group 'org-link
  885. :type '(set :greedy t
  886. (const :tag "Double bracket links (new style)" bracket)
  887. (const :tag "Angular bracket links (old style)" angular)
  888. (const :tag "Plain text links" plain)
  889. (const :tag "Radio target matches" radio)
  890. (const :tag "Tags" tag)
  891. (const :tag "Timestamps" date)
  892. (const :tag "Footnotes" footnote)))
  893. (defcustom org-make-link-description-function nil
  894. "Function to use to generate link descriptions from links. If
  895. nil the link location will be used. This function must take two
  896. parameters; the first is the link and the second the description
  897. org-insert-link has generated, and should return the description
  898. to use."
  899. :group 'org-link
  900. :type 'function)
  901. (defgroup org-link-store nil
  902. "Options concerning storing links in Org-mode."
  903. :tag "Org Store Link"
  904. :group 'org-link)
  905. (defcustom org-email-link-description-format "Email %c: %.30s"
  906. "Format of the description part of a link to an email or usenet message.
  907. The following %-escapes will be replaced by corresponding information:
  908. %F full \"From\" field
  909. %f name, taken from \"From\" field, address if no name
  910. %T full \"To\" field
  911. %t first name in \"To\" field, address if no name
  912. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  913. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  914. %s subject
  915. %m message-id.
  916. You may use normal field width specification between the % and the letter.
  917. This is for example useful to limit the length of the subject.
  918. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  919. :group 'org-link-store
  920. :type 'string)
  921. (defcustom org-from-is-user-regexp
  922. (let (r1 r2)
  923. (when (and user-mail-address (not (string= user-mail-address "")))
  924. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  925. (when (and user-full-name (not (string= user-full-name "")))
  926. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  927. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  928. "Regexp matched against the \"From:\" header of an email or usenet message.
  929. It should match if the message is from the user him/herself."
  930. :group 'org-link-store
  931. :type 'regexp)
  932. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  933. "Non-nil means, storing a link to an Org file will use entry IDs.
  934. Note that before this variable is even considered, org-id must be loaded,
  935. so please customize `org-modules' and turn it on.
  936. The variable can have the following values:
  937. t Create an ID if needed to make a link to the current entry.
  938. create-if-interactive
  939. If `org-store-link' is called directly (interactively, as a user
  940. command), do create an ID to support the link. But when doing the
  941. job for remember, only use the ID if it already exists. The
  942. purpose of this setting is to avoid proliferation of unwanted
  943. IDs, just because you happen to be in an Org file when you
  944. call `org-remember' that automatically and preemptively
  945. creates a link. If you do want to get an ID link in a remember
  946. template to an entry not having an ID, create it first by
  947. explicitly creating a link to it, using `C-c C-l' first.
  948. create-if-interactive-and-no-custom-id
  949. Like create-if-interactive, but do not create an ID if there is
  950. a CUSTOM_ID property defined in the entry. This is the default.
  951. use-existing
  952. Use existing ID, do not create one.
  953. nil Never use an ID to make a link, instead link using a text search for
  954. the headline text."
  955. :group 'org-link-store
  956. :type '(choice
  957. (const :tag "Create ID to make link" t)
  958. (const :tag "Create if storing link interactively"
  959. create-if-interactive)
  960. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  961. create-if-interactive-and-no-custom-id)
  962. (const :tag "Only use existing" use-existing)
  963. (const :tag "Do not use ID to create link" nil)))
  964. (defcustom org-context-in-file-links t
  965. "Non-nil means, file links from `org-store-link' contain context.
  966. A search string will be added to the file name with :: as separator and
  967. used to find the context when the link is activated by the command
  968. `org-open-at-point'.
  969. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  970. negates this setting for the duration of the command."
  971. :group 'org-link-store
  972. :type 'boolean)
  973. (defcustom org-keep-stored-link-after-insertion nil
  974. "Non-nil means, keep link in list for entire session.
  975. The command `org-store-link' adds a link pointing to the current
  976. location to an internal list. These links accumulate during a session.
  977. The command `org-insert-link' can be used to insert links into any
  978. Org-mode file (offering completion for all stored links). When this
  979. option is nil, every link which has been inserted once using \\[org-insert-link]
  980. will be removed from the list, to make completing the unused links
  981. more efficient."
  982. :group 'org-link-store
  983. :type 'boolean)
  984. (defgroup org-link-follow nil
  985. "Options concerning following links in Org-mode."
  986. :tag "Org Follow Link"
  987. :group 'org-link)
  988. (defcustom org-link-translation-function nil
  989. "Function to translate links with different syntax to Org syntax.
  990. This can be used to translate links created for example by the Planner
  991. or emacs-wiki packages to Org syntax.
  992. The function must accept two parameters, a TYPE containing the link
  993. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  994. which is everything after the link protocol. It should return a cons
  995. with possibly modified values of type and path.
  996. Org contains a function for this, so if you set this variable to
  997. `org-translate-link-from-planner', you should be able follow many
  998. links created by planner."
  999. :group 'org-link-follow
  1000. :type 'function)
  1001. (defcustom org-follow-link-hook nil
  1002. "Hook that is run after a link has been followed."
  1003. :group 'org-link-follow
  1004. :type 'hook)
  1005. (defcustom org-tab-follows-link nil
  1006. "Non-nil means, on links TAB will follow the link.
  1007. Needs to be set before org.el is loaded.
  1008. This really should not be used, it does not make sense, and the
  1009. implementation is bad."
  1010. :group 'org-link-follow
  1011. :type 'boolean)
  1012. (defcustom org-return-follows-link nil
  1013. "Non-nil means, on links RET will follow the link.
  1014. Needs to be set before org.el is loaded."
  1015. :group 'org-link-follow
  1016. :type 'boolean)
  1017. (defcustom org-mouse-1-follows-link
  1018. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1019. "Non-nil means, mouse-1 on a link will follow the link.
  1020. A longer mouse click will still set point. Does not work on XEmacs.
  1021. Needs to be set before org.el is loaded."
  1022. :group 'org-link-follow
  1023. :type 'boolean)
  1024. (defcustom org-mark-ring-length 4
  1025. "Number of different positions to be recorded in the ring
  1026. Changing this requires a restart of Emacs to work correctly."
  1027. :group 'org-link-follow
  1028. :type 'integer)
  1029. (defcustom org-link-frame-setup
  1030. '((vm . vm-visit-folder-other-frame)
  1031. (gnus . gnus-other-frame)
  1032. (file . find-file-other-window))
  1033. "Setup the frame configuration for following links.
  1034. When following a link with Emacs, it may often be useful to display
  1035. this link in another window or frame. This variable can be used to
  1036. set this up for the different types of links.
  1037. For VM, use any of
  1038. `vm-visit-folder'
  1039. `vm-visit-folder-other-frame'
  1040. For Gnus, use any of
  1041. `gnus'
  1042. `gnus-other-frame'
  1043. `org-gnus-no-new-news'
  1044. For FILE, use any of
  1045. `find-file'
  1046. `find-file-other-window'
  1047. `find-file-other-frame'
  1048. For the calendar, use the variable `calendar-setup'.
  1049. For BBDB, it is currently only possible to display the matches in
  1050. another window."
  1051. :group 'org-link-follow
  1052. :type '(list
  1053. (cons (const vm)
  1054. (choice
  1055. (const vm-visit-folder)
  1056. (const vm-visit-folder-other-window)
  1057. (const vm-visit-folder-other-frame)))
  1058. (cons (const gnus)
  1059. (choice
  1060. (const gnus)
  1061. (const gnus-other-frame)
  1062. (const org-gnus-no-new-news)))
  1063. (cons (const file)
  1064. (choice
  1065. (const find-file)
  1066. (const find-file-other-window)
  1067. (const find-file-other-frame)))))
  1068. (defcustom org-display-internal-link-with-indirect-buffer nil
  1069. "Non-nil means, use indirect buffer to display infile links.
  1070. Activating internal links (from one location in a file to another location
  1071. in the same file) normally just jumps to the location. When the link is
  1072. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1073. another window. When this option is set, the other window actually displays
  1074. an indirect buffer clone of the current buffer, to avoid any visibility
  1075. changes to the current buffer."
  1076. :group 'org-link-follow
  1077. :type 'boolean)
  1078. (defcustom org-open-non-existing-files nil
  1079. "Non-nil means, `org-open-file' will open non-existing files.
  1080. When nil, an error will be generated.
  1081. This variable applies only to external applications because they
  1082. might choke on non-existing files. If the link is to a file that
  1083. will be openend in Emacs, the variable is ignored."
  1084. :group 'org-link-follow
  1085. :type 'boolean)
  1086. (defcustom org-open-directory-means-index-dot-org nil
  1087. "Non-nil means, a link to a directory really means to index.org.
  1088. When nil, following a directory link will run dired or open a finder/explorer
  1089. window on that directory."
  1090. :group 'org-link-follow
  1091. :type 'boolean)
  1092. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1093. "Function and arguments to call for following mailto links.
  1094. This is a list with the first element being a lisp function, and the
  1095. remaining elements being arguments to the function. In string arguments,
  1096. %a will be replaced by the address, and %s will be replaced by the subject
  1097. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1098. :group 'org-link-follow
  1099. :type '(choice
  1100. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1101. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1102. (const :tag "message-mail" (message-mail "%a" "%s"))
  1103. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1104. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1105. "Non-nil means, ask for confirmation before executing shell links.
  1106. Shell links can be dangerous: just think about a link
  1107. [[shell:rm -rf ~/*][Google Search]]
  1108. This link would show up in your Org-mode document as \"Google Search\",
  1109. but really it would remove your entire home directory.
  1110. Therefore we advise against setting this variable to nil.
  1111. Just change it to `y-or-n-p' if you want to confirm with a
  1112. single keystroke rather than having to type \"yes\"."
  1113. :group 'org-link-follow
  1114. :type '(choice
  1115. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1116. (const :tag "with y-or-n (faster)" y-or-n-p)
  1117. (const :tag "no confirmation (dangerous)" nil)))
  1118. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1119. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1120. Elisp links can be dangerous: just think about a link
  1121. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1122. This link would show up in your Org-mode document as \"Google Search\",
  1123. but really it would remove your entire home directory.
  1124. Therefore we advise against setting this variable to nil.
  1125. Just change it to `y-or-n-p' if you want to confirm with a
  1126. single keystroke rather than having to type \"yes\"."
  1127. :group 'org-link-follow
  1128. :type '(choice
  1129. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1130. (const :tag "with y-or-n (faster)" y-or-n-p)
  1131. (const :tag "no confirmation (dangerous)" nil)))
  1132. (defconst org-file-apps-defaults-gnu
  1133. '((remote . emacs)
  1134. (system . mailcap)
  1135. (t . mailcap))
  1136. "Default file applications on a UNIX or GNU/Linux system.
  1137. See `org-file-apps'.")
  1138. (defconst org-file-apps-defaults-macosx
  1139. '((remote . emacs)
  1140. (t . "open %s")
  1141. (system . "open %s")
  1142. ("ps.gz" . "gv %s")
  1143. ("eps.gz" . "gv %s")
  1144. ("dvi" . "xdvi %s")
  1145. ("fig" . "xfig %s"))
  1146. "Default file applications on a MacOS X system.
  1147. The system \"open\" is known as a default, but we use X11 applications
  1148. for some files for which the OS does not have a good default.
  1149. See `org-file-apps'.")
  1150. (defconst org-file-apps-defaults-windowsnt
  1151. (list
  1152. '(remote . emacs)
  1153. (cons t
  1154. (list (if (featurep 'xemacs)
  1155. 'mswindows-shell-execute
  1156. 'w32-shell-execute)
  1157. "open" 'file))
  1158. (cons 'system
  1159. (list (if (featurep 'xemacs)
  1160. 'mswindows-shell-execute
  1161. 'w32-shell-execute)
  1162. "open" 'file)))
  1163. "Default file applications on a Windows NT system.
  1164. The system \"open\" is used for most files.
  1165. See `org-file-apps'.")
  1166. (defcustom org-file-apps
  1167. '(
  1168. (auto-mode . emacs)
  1169. ("\\.x?html?\\'" . default)
  1170. ("\\.pdf\\'" . default)
  1171. )
  1172. "External applications for opening `file:path' items in a document.
  1173. Org-mode uses system defaults for different file types, but
  1174. you can use this variable to set the application for a given file
  1175. extension. The entries in this list are cons cells where the car identifies
  1176. files and the cdr the corresponding command. Possible values for the
  1177. file identifier are
  1178. \"regex\" Regular expression matched against the file name. For backward
  1179. compatibility, this can also be a string with only alphanumeric
  1180. characters, which is then interpreted as an extension.
  1181. `directory' Matches a directory
  1182. `remote' Matches a remote file, accessible through tramp or efs.
  1183. Remote files most likely should be visited through Emacs
  1184. because external applications cannot handle such paths.
  1185. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1186. so all files Emacs knows how to handle. Using this with
  1187. command `emacs' will open most files in Emacs. Beware that this
  1188. will also open html files inside Emacs, unless you add
  1189. (\"html\" . default) to the list as well.
  1190. t Default for files not matched by any of the other options.
  1191. `system' The system command to open files, like `open' on Windows
  1192. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1193. that will be selected if you call `C-c C-o' with a double
  1194. `C-u C-u' prefix.
  1195. Possible values for the command are:
  1196. `emacs' The file will be visited by the current Emacs process.
  1197. `default' Use the default application for this file type, which is the
  1198. association for t in the list, most likely in the system-specific
  1199. part.
  1200. This can be used to overrule an unwanted setting in the
  1201. system-specific variable.
  1202. `system' Use the system command for opening files, like \"open\".
  1203. This command is specified by the entry whose car is `system'.
  1204. Most likely, the system-specific version of this variable
  1205. does define this command, but you can overrule/replace it
  1206. here.
  1207. string A command to be executed by a shell; %s will be replaced
  1208. by the path to the file.
  1209. sexp A Lisp form which will be evaluated. The file path will
  1210. be available in the Lisp variable `file'.
  1211. For more examples, see the system specific constants
  1212. `org-file-apps-defaults-macosx'
  1213. `org-file-apps-defaults-windowsnt'
  1214. `org-file-apps-defaults-gnu'."
  1215. :group 'org-link-follow
  1216. :type '(repeat
  1217. (cons (choice :value ""
  1218. (string :tag "Extension")
  1219. (const :tag "System command to open files" system)
  1220. (const :tag "Default for unrecognized files" t)
  1221. (const :tag "Remote file" remote)
  1222. (const :tag "Links to a directory" directory)
  1223. (const :tag "Any files that have Emacs modes"
  1224. auto-mode))
  1225. (choice :value ""
  1226. (const :tag "Visit with Emacs" emacs)
  1227. (const :tag "Use default" default)
  1228. (const :tag "Use the system command" system)
  1229. (string :tag "Command")
  1230. (sexp :tag "Lisp form")))))
  1231. (defgroup org-refile nil
  1232. "Options concerning refiling entries in Org-mode."
  1233. :tag "Org Refile"
  1234. :group 'org)
  1235. (defcustom org-directory "~/org"
  1236. "Directory with org files.
  1237. This is just a default location to look for Org files. There is no need
  1238. at all to put your files into this directory. It is only used in the
  1239. following situations:
  1240. 1. When a remember template specifies a target file that is not an
  1241. absolute path. The path will then be interpreted relative to
  1242. `org-directory'
  1243. 2. When a remember note is filed away in an interactive way (when exiting the
  1244. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1245. with `org-directory' as the default path."
  1246. :group 'org-refile
  1247. :group 'org-remember
  1248. :type 'directory)
  1249. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1250. "Default target for storing notes.
  1251. Used by the hooks for remember.el. This can be a string, or nil to mean
  1252. the value of `remember-data-file'.
  1253. You can set this on a per-template basis with the variable
  1254. `org-remember-templates'."
  1255. :group 'org-refile
  1256. :group 'org-remember
  1257. :type '(choice
  1258. (const :tag "Default from remember-data-file" nil)
  1259. file))
  1260. (defcustom org-goto-interface 'outline
  1261. "The default interface to be used for `org-goto'.
  1262. Allowed values are:
  1263. outline The interface shows an outline of the relevant file
  1264. and the correct heading is found by moving through
  1265. the outline or by searching with incremental search.
  1266. outline-path-completion Headlines in the current buffer are offered via
  1267. completion. This is the interface also used by
  1268. the refile command."
  1269. :group 'org-refile
  1270. :type '(choice
  1271. (const :tag "Outline" outline)
  1272. (const :tag "Outline-path-completion" outline-path-completion)))
  1273. (defcustom org-goto-max-level 5
  1274. "Maximum level to be considered when running org-goto with refile interface."
  1275. :group 'org-refile
  1276. :type 'integer)
  1277. (defcustom org-reverse-note-order nil
  1278. "Non-nil means, store new notes at the beginning of a file or entry.
  1279. When nil, new notes will be filed to the end of a file or entry.
  1280. This can also be a list with cons cells of regular expressions that
  1281. are matched against file names, and values."
  1282. :group 'org-remember
  1283. :group 'org-refile
  1284. :type '(choice
  1285. (const :tag "Reverse always" t)
  1286. (const :tag "Reverse never" nil)
  1287. (repeat :tag "By file name regexp"
  1288. (cons regexp boolean))))
  1289. (defcustom org-refile-targets nil
  1290. "Targets for refiling entries with \\[org-refile].
  1291. This is list of cons cells. Each cell contains:
  1292. - a specification of the files to be considered, either a list of files,
  1293. or a symbol whose function or variable value will be used to retrieve
  1294. a file name or a list of file names. If you use `org-agenda-files' for
  1295. that, all agenda files will be scanned for targets. Nil means, consider
  1296. headings in the current buffer.
  1297. - A specification of how to find candidate refile targets. This may be
  1298. any of:
  1299. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1300. This tag has to be present in all target headlines, inheritance will
  1301. not be considered.
  1302. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1303. todo keyword.
  1304. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1305. headlines that are refiling targets.
  1306. - a cons cell (:level . N). Any headline of level N is considered a target.
  1307. Note that, when `org-odd-levels-only' is set, level corresponds to
  1308. order in hierarchy, not to the number of stars.
  1309. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1310. Note that, when `org-odd-levels-only' is set, level corresponds to
  1311. order in hierarchy, not to the number of stars.
  1312. You can set the variable `org-refile-target-verify-function' to a function
  1313. to verify each headline found by the simple critery above.
  1314. When this variable is nil, all top-level headlines in the current buffer
  1315. are used, equivalent to the value `((nil . (:level . 1))'."
  1316. :group 'org-refile
  1317. :type '(repeat
  1318. (cons
  1319. (choice :value org-agenda-files
  1320. (const :tag "All agenda files" org-agenda-files)
  1321. (const :tag "Current buffer" nil)
  1322. (function) (variable) (file))
  1323. (choice :tag "Identify target headline by"
  1324. (cons :tag "Specific tag" (const :value :tag) (string))
  1325. (cons :tag "TODO keyword" (const :value :todo) (string))
  1326. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1327. (cons :tag "Level number" (const :value :level) (integer))
  1328. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1329. (defcustom org-refile-target-verify-function nil
  1330. "Function to verify if the headline at point should be a refile target.
  1331. The function will be called without arguments, with point at the
  1332. beginning of the headline. It should return t and leave point
  1333. where it is if the headline is a valid target for refiling.
  1334. If the target should not be selected, the function must return nil.
  1335. In addition to this, it may move point to a place from where the search
  1336. should be continued. For example, the function may decide that the entire
  1337. subtree of the current entry should be excluded and move point to the end
  1338. of the subtree."
  1339. :group 'org-refile
  1340. :type 'function)
  1341. (defcustom org-refile-use-outline-path nil
  1342. "Non-nil means, provide refile targets as paths.
  1343. So a level 3 headline will be available as level1/level2/level3.
  1344. When the value is `file', also include the file name (without directory)
  1345. into the path. In this case, you can also stop the completion after
  1346. the file name, to get entries inserted as top level in the file.
  1347. When `full-file-path', include the full file path."
  1348. :group 'org-refile
  1349. :type '(choice
  1350. (const :tag "Not" nil)
  1351. (const :tag "Yes" t)
  1352. (const :tag "Start with file name" file)
  1353. (const :tag "Start with full file path" full-file-path)))
  1354. (defcustom org-outline-path-complete-in-steps t
  1355. "Non-nil means, complete the outline path in hierarchical steps.
  1356. When Org-mode uses the refile interface to select an outline path
  1357. \(see variable `org-refile-use-outline-path'), the completion of
  1358. the path can be done is a single go, or if can be done in steps down
  1359. the headline hierarchy. Going in steps is probably the best if you
  1360. do not use a special completion package like `ido' or `icicles'.
  1361. However, when using these packages, going in one step can be very
  1362. fast, while still showing the whole path to the entry."
  1363. :group 'org-refile
  1364. :type 'boolean)
  1365. (defcustom org-refile-allow-creating-parent-nodes nil
  1366. "Non-nil means, allow to create new nodes as refile targets.
  1367. New nodes are then created by adding \"/new node name\" to the completion
  1368. of an existing node. When the value of this variable is `confirm',
  1369. new node creation must be confirmed by the user (recommended)
  1370. When nil, the completion must match an existing entry.
  1371. Note that, if the new heading is not seen by the criteria
  1372. listed in `org-refile-targets', multiple instances of the same
  1373. heading would be created by trying again to file under the new
  1374. heading."
  1375. :group 'org-refile
  1376. :type '(choice
  1377. (const :tag "Never" nil)
  1378. (const :tag "Always" t)
  1379. (const :tag "Prompt for confirmation" confirm)))
  1380. (defgroup org-todo nil
  1381. "Options concerning TODO items in Org-mode."
  1382. :tag "Org TODO"
  1383. :group 'org)
  1384. (defgroup org-progress nil
  1385. "Options concerning Progress logging in Org-mode."
  1386. :tag "Org Progress"
  1387. :group 'org-time)
  1388. (defvar org-todo-interpretation-widgets
  1389. '(
  1390. (:tag "Sequence (cycling hits every state)" sequence)
  1391. (:tag "Type (cycling directly to DONE)" type))
  1392. "The available interpretation symbols for customizing
  1393. `org-todo-keywords'.
  1394. Interested libraries should add to this list.")
  1395. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1396. "List of TODO entry keyword sequences and their interpretation.
  1397. \\<org-mode-map>This is a list of sequences.
  1398. Each sequence starts with a symbol, either `sequence' or `type',
  1399. indicating if the keywords should be interpreted as a sequence of
  1400. action steps, or as different types of TODO items. The first
  1401. keywords are states requiring action - these states will select a headline
  1402. for inclusion into the global TODO list Org-mode produces. If one of
  1403. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1404. signify that no further action is necessary. If \"|\" is not found,
  1405. the last keyword is treated as the only DONE state of the sequence.
  1406. The command \\[org-todo] cycles an entry through these states, and one
  1407. additional state where no keyword is present. For details about this
  1408. cycling, see the manual.
  1409. TODO keywords and interpretation can also be set on a per-file basis with
  1410. the special #+SEQ_TODO and #+TYP_TODO lines.
  1411. Each keyword can optionally specify a character for fast state selection
  1412. \(in combination with the variable `org-use-fast-todo-selection')
  1413. and specifiers for state change logging, using the same syntax
  1414. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1415. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1416. indicates to record a time stamp each time this state is selected.
  1417. Each keyword may also specify if a timestamp or a note should be
  1418. recorded when entering or leaving the state, by adding additional
  1419. characters in the parenthesis after the keyword. This looks like this:
  1420. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1421. record only the time of the state change. With X and Y being either
  1422. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1423. Y when leaving the state if and only if the *target* state does not
  1424. define X. You may omit any of the fast-selection key or X or /Y,
  1425. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1426. For backward compatibility, this variable may also be just a list
  1427. of keywords - in this case the interpretation (sequence or type) will be
  1428. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1429. :group 'org-todo
  1430. :group 'org-keywords
  1431. :type '(choice
  1432. (repeat :tag "Old syntax, just keywords"
  1433. (string :tag "Keyword"))
  1434. (repeat :tag "New syntax"
  1435. (cons
  1436. (choice
  1437. :tag "Interpretation"
  1438. ;;Quick and dirty way to see
  1439. ;;`org-todo-interpretations'. This takes the
  1440. ;;place of item arguments
  1441. :convert-widget
  1442. (lambda (widget)
  1443. (widget-put widget
  1444. :args (mapcar
  1445. #'(lambda (x)
  1446. (widget-convert
  1447. (cons 'const x)))
  1448. org-todo-interpretation-widgets))
  1449. widget))
  1450. (repeat
  1451. (string :tag "Keyword"))))))
  1452. (defvar org-todo-keywords-1 nil
  1453. "All TODO and DONE keywords active in a buffer.")
  1454. (make-variable-buffer-local 'org-todo-keywords-1)
  1455. (defvar org-todo-keywords-for-agenda nil)
  1456. (defvar org-done-keywords-for-agenda nil)
  1457. (defvar org-todo-keyword-alist-for-agenda nil)
  1458. (defvar org-tag-alist-for-agenda nil)
  1459. (defvar org-agenda-contributing-files nil)
  1460. (defvar org-not-done-keywords nil)
  1461. (make-variable-buffer-local 'org-not-done-keywords)
  1462. (defvar org-done-keywords nil)
  1463. (make-variable-buffer-local 'org-done-keywords)
  1464. (defvar org-todo-heads nil)
  1465. (make-variable-buffer-local 'org-todo-heads)
  1466. (defvar org-todo-sets nil)
  1467. (make-variable-buffer-local 'org-todo-sets)
  1468. (defvar org-todo-log-states nil)
  1469. (make-variable-buffer-local 'org-todo-log-states)
  1470. (defvar org-todo-kwd-alist nil)
  1471. (make-variable-buffer-local 'org-todo-kwd-alist)
  1472. (defvar org-todo-key-alist nil)
  1473. (make-variable-buffer-local 'org-todo-key-alist)
  1474. (defvar org-todo-key-trigger nil)
  1475. (make-variable-buffer-local 'org-todo-key-trigger)
  1476. (defcustom org-todo-interpretation 'sequence
  1477. "Controls how TODO keywords are interpreted.
  1478. This variable is in principle obsolete and is only used for
  1479. backward compatibility, if the interpretation of todo keywords is
  1480. not given already in `org-todo-keywords'. See that variable for
  1481. more information."
  1482. :group 'org-todo
  1483. :group 'org-keywords
  1484. :type '(choice (const sequence)
  1485. (const type)))
  1486. (defcustom org-use-fast-todo-selection t
  1487. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1488. This variable describes if and under what circumstances the cycling
  1489. mechanism for TODO keywords will be replaced by a single-key, direct
  1490. selection scheme.
  1491. When nil, fast selection is never used.
  1492. When the symbol `prefix', it will be used when `org-todo' is called with
  1493. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1494. in an agenda buffer.
  1495. When t, fast selection is used by default. In this case, the prefix
  1496. argument forces cycling instead.
  1497. In all cases, the special interface is only used if access keys have actually
  1498. been assigned by the user, i.e. if keywords in the configuration are followed
  1499. by a letter in parenthesis, like TODO(t)."
  1500. :group 'org-todo
  1501. :type '(choice
  1502. (const :tag "Never" nil)
  1503. (const :tag "By default" t)
  1504. (const :tag "Only with C-u C-c C-t" prefix)))
  1505. (defcustom org-provide-todo-statistics t
  1506. "Non-nil means, update todo statistics after insert and toggle.
  1507. ALL-HEADLINES means update todo statistics by including headlines
  1508. with no TODO keyword as well, counting them as not done.
  1509. A list of TODO keywords means the same, but skip keywords that are
  1510. not in this list.
  1511. When this is set, todo statistics is updated in the parent of the
  1512. current entry each time a todo state is changed."
  1513. :group 'org-todo
  1514. :type '(choice
  1515. (const :tag "Yes, only for TODO entries" t)
  1516. (const :tag "Yes, including all entries" 'all-headlines)
  1517. (repeat :tag "Yes, for TODOs in this list"
  1518. (string :tag "TODO keyword"))
  1519. (other :tag "No TODO statistics" nil)))
  1520. (defcustom org-hierarchical-todo-statistics t
  1521. "Non-nil means, TODO statistics covers just direct children.
  1522. When nil, all entries in the subtree are considered.
  1523. This has only an effect if `org-provide-todo-statistics' is set."
  1524. :group 'org-todo
  1525. :type 'boolean)
  1526. (defcustom org-after-todo-state-change-hook nil
  1527. "Hook which is run after the state of a TODO item was changed.
  1528. The new state (a string with a TODO keyword, or nil) is available in the
  1529. Lisp variable `state'."
  1530. :group 'org-todo
  1531. :type 'hook)
  1532. (defvar org-blocker-hook nil
  1533. "Hook for functions that are allowed to block a state change.
  1534. Each function gets as its single argument a property list, see
  1535. `org-trigger-hook' for more information about this list.
  1536. If any of the functions in this hook returns nil, the state change
  1537. is blocked.")
  1538. (defvar org-trigger-hook nil
  1539. "Hook for functions that are triggered by a state change.
  1540. Each function gets as its single argument a property list with at least
  1541. the following elements:
  1542. (:type type-of-change :position pos-at-entry-start
  1543. :from old-state :to new-state)
  1544. Depending on the type, more properties may be present.
  1545. This mechanism is currently implemented for:
  1546. TODO state changes
  1547. ------------------
  1548. :type todo-state-change
  1549. :from previous state (keyword as a string), or nil, or a symbol
  1550. 'todo' or 'done', to indicate the general type of state.
  1551. :to new state, like in :from")
  1552. (defcustom org-enforce-todo-dependencies nil
  1553. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1554. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1555. be blocked if any prior sibling is not yet done.
  1556. Finally, if the parent is blocked because of ordered siblings of its own,
  1557. the child will also be blocked.
  1558. This variable needs to be set before org.el is loaded, and you need to
  1559. restart Emacs after a change to make the change effective. The only way
  1560. to change is while Emacs is running is through the customize interface."
  1561. :set (lambda (var val)
  1562. (set var val)
  1563. (if val
  1564. (add-hook 'org-blocker-hook
  1565. 'org-block-todo-from-children-or-siblings-or-parent)
  1566. (remove-hook 'org-blocker-hook
  1567. 'org-block-todo-from-children-or-siblings-or-parent)))
  1568. :group 'org-todo
  1569. :type 'boolean)
  1570. (defcustom org-enforce-todo-checkbox-dependencies nil
  1571. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1572. When this is nil, checkboxes have no influence on switching TODO states.
  1573. When non-nil, you first need to check off all check boxes before the TODO
  1574. entry can be switched to DONE.
  1575. This variable needs to be set before org.el is loaded, and you need to
  1576. restart Emacs after a change to make the change effective. The only way
  1577. to change is while Emacs is running is through the customize interface."
  1578. :set (lambda (var val)
  1579. (set var val)
  1580. (if val
  1581. (add-hook 'org-blocker-hook
  1582. 'org-block-todo-from-checkboxes)
  1583. (remove-hook 'org-blocker-hook
  1584. 'org-block-todo-from-checkboxes)))
  1585. :group 'org-todo
  1586. :type 'boolean)
  1587. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1588. "Non-nil means, inserting a TODO heading is treated as state change.
  1589. So when the command \\[org-insert-todo-heading] is used, state change
  1590. logging will apply if appropriate. When nil, the new TODO item will
  1591. be inserted directly, and no logging will take place."
  1592. :group 'org-todo
  1593. :type 'boolean)
  1594. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1595. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1596. This is the default behavior. However, setting this to nil allows a
  1597. convenient way to select a TODO state and bypass any logging associated
  1598. with that."
  1599. :group 'org-todo
  1600. :type 'boolean)
  1601. (defcustom org-todo-state-tags-triggers nil
  1602. "Tag changes that should be triggered by TODO state changes.
  1603. This is a list. Each entry is
  1604. (state-change (tag . flag) .......)
  1605. State-change can be a string with a state, and empty string to indicate the
  1606. state that has no TODO keyword, or it can be one of the symbols `todo'
  1607. or `done', meaning any not-done or done state, respectively."
  1608. :group 'org-todo
  1609. :group 'org-tags
  1610. :type '(repeat
  1611. (cons (choice :tag "When changing to"
  1612. (const :tag "Not-done state" todo)
  1613. (const :tag "Done state" done)
  1614. (string :tag "State"))
  1615. (repeat
  1616. (cons :tag "Tag action"
  1617. (string :tag "Tag")
  1618. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1619. (defcustom org-log-done nil
  1620. "Information to record when a task moves to the DONE state.
  1621. Possible values are:
  1622. nil Don't add anything, just change the keyword
  1623. time Add a time stamp to the task
  1624. note Prompt a closing note and add it with template `org-log-note-headings'
  1625. This option can also be set with on a per-file-basis with
  1626. #+STARTUP: nologdone
  1627. #+STARTUP: logdone
  1628. #+STARTUP: lognotedone
  1629. You can have local logging settings for a subtree by setting the LOGGING
  1630. property to one or more of these keywords."
  1631. :group 'org-todo
  1632. :group 'org-progress
  1633. :type '(choice
  1634. (const :tag "No logging" nil)
  1635. (const :tag "Record CLOSED timestamp" time)
  1636. (const :tag "Record CLOSED timestamp with closing note." note)))
  1637. ;; Normalize old uses of org-log-done.
  1638. (cond
  1639. ((eq org-log-done t) (setq org-log-done 'time))
  1640. ((and (listp org-log-done) (memq 'done org-log-done))
  1641. (setq org-log-done 'note)))
  1642. (defcustom org-log-note-clock-out nil
  1643. "Non-nil means, record a note when clocking out of an item.
  1644. This can also be configured on a per-file basis by adding one of
  1645. the following lines anywhere in the buffer:
  1646. #+STARTUP: lognoteclock-out
  1647. #+STARTUP: nolognoteclock-out"
  1648. :group 'org-todo
  1649. :group 'org-progress
  1650. :type 'boolean)
  1651. (defcustom org-log-done-with-time t
  1652. "Non-nil means, the CLOSED time stamp will contain date and time.
  1653. When nil, only the date will be recorded."
  1654. :group 'org-progress
  1655. :type 'boolean)
  1656. (defcustom org-log-note-headings
  1657. '((done . "CLOSING NOTE %t")
  1658. (state . "State %-12s from %-12S %t")
  1659. (note . "Note taken on %t")
  1660. (clock-out . ""))
  1661. "Headings for notes added to entries.
  1662. The value is an alist, with the car being a symbol indicating the note
  1663. context, and the cdr is the heading to be used. The heading may also be the
  1664. empty string.
  1665. %t in the heading will be replaced by a time stamp.
  1666. %s will be replaced by the new TODO state, in double quotes.
  1667. %S will be replaced by the old TODO state, in double quotes.
  1668. %u will be replaced by the user name.
  1669. %U will be replaced by the full user name."
  1670. :group 'org-todo
  1671. :group 'org-progress
  1672. :type '(list :greedy t
  1673. (cons (const :tag "Heading when closing an item" done) string)
  1674. (cons (const :tag
  1675. "Heading when changing todo state (todo sequence only)"
  1676. state) string)
  1677. (cons (const :tag "Heading when just taking a note" note) string)
  1678. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1679. (unless (assq 'note org-log-note-headings)
  1680. (push '(note . "%t") org-log-note-headings))
  1681. (defcustom org-log-into-drawer nil
  1682. "Non-nil means, insert state change notes and time stamps into a drawer.
  1683. When nil, state changes notes will be inserted after the headline and
  1684. any scheduling and clock lines, but not inside a drawer.
  1685. The value of this variable should be the name of the drawer to use.
  1686. LOGBOOK is proposed at the default drawer for this purpose, you can
  1687. also set this to a string to define the drawer of your choice.
  1688. A value of t is also allowed, representing \"LOGBOOK\".
  1689. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1690. will be ignored.
  1691. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1692. a subtree."
  1693. :group 'org-todo
  1694. :group 'org-progress
  1695. :type '(choice
  1696. (const :tag "Not into a drawer" nil)
  1697. (const :tag "LOGBOOK" t)
  1698. (string :tag "Other")))
  1699. (if (fboundp 'defvaralias)
  1700. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1701. (defun org-log-into-drawer ()
  1702. "Return the value of `org-log-into-drawer', but let properties overrule.
  1703. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1704. used instead of the default value."
  1705. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1706. (cond
  1707. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1708. ((equal p "t") "LOGBOOK")
  1709. (t p))))
  1710. (defcustom org-log-state-notes-insert-after-drawers nil
  1711. "Non-nil means, insert state change notes after any drawers in entry.
  1712. Only the drawers that *immediately* follow the headline and the
  1713. deadline/scheduled line are skipped.
  1714. When nil, insert notes right after the heading and perhaps the line
  1715. with deadline/scheduling if present.
  1716. This variable will have no effect if `org-log-into-drawer' is
  1717. set."
  1718. :group 'org-todo
  1719. :group 'org-progress
  1720. :type 'boolean)
  1721. (defcustom org-log-states-order-reversed t
  1722. "Non-nil means, the latest state change note will be directly after heading.
  1723. When nil, the notes will be orderer according to time."
  1724. :group 'org-todo
  1725. :group 'org-progress
  1726. :type 'boolean)
  1727. (defcustom org-log-repeat 'time
  1728. "Non-nil means, record moving through the DONE state when triggering repeat.
  1729. An auto-repeating tasks is immediately switched back to TODO when marked
  1730. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1731. the TODO keyword definition, or recording a closing note by setting
  1732. `org-log-done', there will be no record of the task moving through DONE.
  1733. This variable forces taking a note anyway. Possible values are:
  1734. nil Don't force a record
  1735. time Record a time stamp
  1736. note Record a note
  1737. This option can also be set with on a per-file-basis with
  1738. #+STARTUP: logrepeat
  1739. #+STARTUP: lognoterepeat
  1740. #+STARTUP: nologrepeat
  1741. You can have local logging settings for a subtree by setting the LOGGING
  1742. property to one or more of these keywords."
  1743. :group 'org-todo
  1744. :group 'org-progress
  1745. :type '(choice
  1746. (const :tag "Don't force a record" nil)
  1747. (const :tag "Force recording the DONE state" time)
  1748. (const :tag "Force recording a note with the DONE state" note)))
  1749. (defgroup org-priorities nil
  1750. "Priorities in Org-mode."
  1751. :tag "Org Priorities"
  1752. :group 'org-todo)
  1753. (defcustom org-enable-priority-commands t
  1754. "Non-nil means, priority commands are active.
  1755. When nil, these commands will be disabled, so that you never accidentally
  1756. set a priority."
  1757. :group 'org-priorities
  1758. :type 'boolean)
  1759. (defcustom org-highest-priority ?A
  1760. "The highest priority of TODO items. A character like ?A, ?B etc.
  1761. Must have a smaller ASCII number than `org-lowest-priority'."
  1762. :group 'org-priorities
  1763. :type 'character)
  1764. (defcustom org-lowest-priority ?C
  1765. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1766. Must have a larger ASCII number than `org-highest-priority'."
  1767. :group 'org-priorities
  1768. :type 'character)
  1769. (defcustom org-default-priority ?B
  1770. "The default priority of TODO items.
  1771. This is the priority an item get if no explicit priority is given."
  1772. :group 'org-priorities
  1773. :type 'character)
  1774. (defcustom org-priority-start-cycle-with-default t
  1775. "Non-nil means, start with default priority when starting to cycle.
  1776. When this is nil, the first step in the cycle will be (depending on the
  1777. command used) one higher or lower that the default priority."
  1778. :group 'org-priorities
  1779. :type 'boolean)
  1780. (defgroup org-time nil
  1781. "Options concerning time stamps and deadlines in Org-mode."
  1782. :tag "Org Time"
  1783. :group 'org)
  1784. (defcustom org-insert-labeled-timestamps-at-point nil
  1785. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1786. When nil, these labeled time stamps are forces into the second line of an
  1787. entry, just after the headline. When scheduling from the global TODO list,
  1788. the time stamp will always be forced into the second line."
  1789. :group 'org-time
  1790. :type 'boolean)
  1791. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1792. "Formats for `format-time-string' which are used for time stamps.
  1793. It is not recommended to change this constant.")
  1794. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1795. "Number of minutes to round time stamps to.
  1796. These are two values, the first applies when first creating a time stamp.
  1797. The second applies when changing it with the commands `S-up' and `S-down'.
  1798. When changing the time stamp, this means that it will change in steps
  1799. of N minutes, as given by the second value.
  1800. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1801. numbers should be factors of 60, so for example 5, 10, 15.
  1802. When this is larger than 1, you can still force an exact time-stamp by using
  1803. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1804. and by using a prefix arg to `S-up/down' to specify the exact number
  1805. of minutes to shift."
  1806. :group 'org-time
  1807. :get '(lambda (var) ; Make sure all entries have 5 elements
  1808. (if (integerp (default-value var))
  1809. (list (default-value var) 5)
  1810. (default-value var)))
  1811. :type '(list
  1812. (integer :tag "when inserting times")
  1813. (integer :tag "when modifying times")))
  1814. ;; Normalize old customizations of this variable.
  1815. (when (integerp org-time-stamp-rounding-minutes)
  1816. (setq org-time-stamp-rounding-minutes
  1817. (list org-time-stamp-rounding-minutes
  1818. org-time-stamp-rounding-minutes)))
  1819. (defcustom org-display-custom-times nil
  1820. "Non-nil means, overlay custom formats over all time stamps.
  1821. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1822. To turn this on on a per-file basis, insert anywhere in the file:
  1823. #+STARTUP: customtime"
  1824. :group 'org-time
  1825. :set 'set-default
  1826. :type 'sexp)
  1827. (make-variable-buffer-local 'org-display-custom-times)
  1828. (defcustom org-time-stamp-custom-formats
  1829. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1830. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1831. These are overlayed over the default ISO format if the variable
  1832. `org-display-custom-times' is set. Time like %H:%M should be at the
  1833. end of the second format. The custom formats are also honored by export
  1834. commands, if custom time display is turned on at the time of export."
  1835. :group 'org-time
  1836. :type 'sexp)
  1837. (defun org-time-stamp-format (&optional long inactive)
  1838. "Get the right format for a time string."
  1839. (let ((f (if long (cdr org-time-stamp-formats)
  1840. (car org-time-stamp-formats))))
  1841. (if inactive
  1842. (concat "[" (substring f 1 -1) "]")
  1843. f)))
  1844. (defcustom org-time-clocksum-format "%d:%02d"
  1845. "The format string used when creating CLOCKSUM lines, or when
  1846. org-mode generates a time duration."
  1847. :group 'org-time
  1848. :type 'string)
  1849. (defcustom org-deadline-warning-days 14
  1850. "No. of days before expiration during which a deadline becomes active.
  1851. This variable governs the display in sparse trees and in the agenda.
  1852. When 0 or negative, it means use this number (the absolute value of it)
  1853. even if a deadline has a different individual lead time specified.
  1854. Custom commands can set this variable in the options section."
  1855. :group 'org-time
  1856. :group 'org-agenda-daily/weekly
  1857. :type 'integer)
  1858. (defcustom org-read-date-prefer-future t
  1859. "Non-nil means, assume future for incomplete date input from user.
  1860. This affects the following situations:
  1861. 1. The user gives a day, but no month.
  1862. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1863. read this as the third of *next* month. However, if you enter \"17\",
  1864. it will be considered as *this* month.
  1865. 2. The user gives a month but not a year.
  1866. For example, if it is april and you enter \"feb 2\", this will be read
  1867. as feb 2, *next* year. \"May 5\", however, will be this year.
  1868. Currently this does not work for ISO week specifications.
  1869. When this option is nil, the current month and year will always be used
  1870. as defaults."
  1871. :group 'org-time
  1872. :type 'boolean)
  1873. (defcustom org-read-date-display-live t
  1874. "Non-nil means, display current interpretation of date prompt live.
  1875. This display will be in an overlay, in the minibuffer."
  1876. :group 'org-time
  1877. :type 'boolean)
  1878. (defcustom org-read-date-popup-calendar t
  1879. "Non-nil means, pop up a calendar when prompting for a date.
  1880. In the calendar, the date can be selected with mouse-1. However, the
  1881. minibuffer will also be active, and you can simply enter the date as well.
  1882. When nil, only the minibuffer will be available."
  1883. :group 'org-time
  1884. :type 'boolean)
  1885. (if (fboundp 'defvaralias)
  1886. (defvaralias 'org-popup-calendar-for-date-prompt
  1887. 'org-read-date-popup-calendar))
  1888. (defcustom org-read-date-minibuffer-setup-hook nil
  1889. "Hook to be used to set up keys for the date/time interface.
  1890. Add key definitions to `minibuffer-local-map', which will be a temporary
  1891. copy."
  1892. :group 'org-time
  1893. :type 'hook)
  1894. (defcustom org-extend-today-until 0
  1895. "The hour when your day really ends. Must be an integer.
  1896. This has influence for the following applications:
  1897. - When switching the agenda to \"today\". It it is still earlier than
  1898. the time given here, the day recognized as TODAY is actually yesterday.
  1899. - When a date is read from the user and it is still before the time given
  1900. here, the current date and time will be assumed to be yesterday, 23:59.
  1901. Also, timestamps inserted in remember templates follow this rule.
  1902. IMPORTANT: This is a feature whose implementation is and likely will
  1903. remain incomplete. Really, it is only here because past midnight seems to
  1904. be the favorite working time of John Wiegley :-)"
  1905. :group 'org-time
  1906. :type 'integer)
  1907. (defcustom org-edit-timestamp-down-means-later nil
  1908. "Non-nil means, S-down will increase the time in a time stamp.
  1909. When nil, S-up will increase."
  1910. :group 'org-time
  1911. :type 'boolean)
  1912. (defcustom org-calendar-follow-timestamp-change t
  1913. "Non-nil means, make the calendar window follow timestamp changes.
  1914. When a timestamp is modified and the calendar window is visible, it will be
  1915. moved to the new date."
  1916. :group 'org-time
  1917. :type 'boolean)
  1918. (defgroup org-tags nil
  1919. "Options concerning tags in Org-mode."
  1920. :tag "Org Tags"
  1921. :group 'org)
  1922. (defcustom org-tag-alist nil
  1923. "List of tags allowed in Org-mode files.
  1924. When this list is nil, Org-mode will base TAG input on what is already in the
  1925. buffer.
  1926. The value of this variable is an alist, the car of each entry must be a
  1927. keyword as a string, the cdr may be a character that is used to select
  1928. that tag through the fast-tag-selection interface.
  1929. See the manual for details."
  1930. :group 'org-tags
  1931. :type '(repeat
  1932. (choice
  1933. (cons (string :tag "Tag name")
  1934. (character :tag "Access char"))
  1935. (const :tag "Start radio group" (:startgroup))
  1936. (const :tag "End radio group" (:endgroup))
  1937. (const :tag "New line" (:newline)))))
  1938. (defcustom org-tag-persistent-alist nil
  1939. "List of tags that will always appear in all Org-mode files.
  1940. This is in addition to any in buffer settings or customizations
  1941. of `org-tag-alist'.
  1942. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1943. The value of this variable is an alist, the car of each entry must be a
  1944. keyword as a string, the cdr may be a character that is used to select
  1945. that tag through the fast-tag-selection interface.
  1946. See the manual for details.
  1947. To disable these tags on a per-file basis, insert anywhere in the file:
  1948. #+STARTUP: noptag"
  1949. :group 'org-tags
  1950. :type '(repeat
  1951. (choice
  1952. (cons (string :tag "Tag name")
  1953. (character :tag "Access char"))
  1954. (const :tag "Start radio group" (:startgroup))
  1955. (const :tag "End radio group" (:endgroup))
  1956. (const :tag "New line" (:newline)))))
  1957. (defvar org-file-tags nil
  1958. "List of tags that can be inherited by all entries in the file.
  1959. The tags will be inherited if the variable `org-use-tag-inheritance'
  1960. says they should be.
  1961. This variable is populated from #+TAG lines.")
  1962. (defcustom org-use-fast-tag-selection 'auto
  1963. "Non-nil means, use fast tag selection scheme.
  1964. This is a special interface to select and deselect tags with single keys.
  1965. When nil, fast selection is never used.
  1966. When the symbol `auto', fast selection is used if and only if selection
  1967. characters for tags have been configured, either through the variable
  1968. `org-tag-alist' or through a #+TAGS line in the buffer.
  1969. When t, fast selection is always used and selection keys are assigned
  1970. automatically if necessary."
  1971. :group 'org-tags
  1972. :type '(choice
  1973. (const :tag "Always" t)
  1974. (const :tag "Never" nil)
  1975. (const :tag "When selection characters are configured" 'auto)))
  1976. (defcustom org-fast-tag-selection-single-key nil
  1977. "Non-nil means, fast tag selection exits after first change.
  1978. When nil, you have to press RET to exit it.
  1979. During fast tag selection, you can toggle this flag with `C-c'.
  1980. This variable can also have the value `expert'. In this case, the window
  1981. displaying the tags menu is not even shown, until you press C-c again."
  1982. :group 'org-tags
  1983. :type '(choice
  1984. (const :tag "No" nil)
  1985. (const :tag "Yes" t)
  1986. (const :tag "Expert" expert)))
  1987. (defvar org-fast-tag-selection-include-todo nil
  1988. "Non-nil means, fast tags selection interface will also offer TODO states.
  1989. This is an undocumented feature, you should not rely on it.")
  1990. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1991. "The column to which tags should be indented in a headline.
  1992. If this number is positive, it specifies the column. If it is negative,
  1993. it means that the tags should be flushright to that column. For example,
  1994. -80 works well for a normal 80 character screen."
  1995. :group 'org-tags
  1996. :type 'integer)
  1997. (defcustom org-auto-align-tags t
  1998. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1999. These operations change the length of a headline and therefore shift
  2000. the tags around. With this options turned on, after each such operation
  2001. the tags are again aligned to `org-tags-column'."
  2002. :group 'org-tags
  2003. :type 'boolean)
  2004. (defcustom org-use-tag-inheritance t
  2005. "Non-nil means, tags in levels apply also for sublevels.
  2006. When nil, only the tags directly given in a specific line apply there.
  2007. This may also be a list of tags that should be inherited, or a regexp that
  2008. matches tags that should be inherited. Additional control is possible
  2009. with the variable `org-tags-exclude-from-inheritance' which gives an
  2010. explicit list of tags to be excluded from inheritance., even if the value of
  2011. `org-use-tag-inheritance' would select it for inheritance.
  2012. If this option is t, a match early-on in a tree can lead to a large
  2013. number of matches in the subtree when constructing the agenda or creating
  2014. a sparse tree. If you only want to see the first match in a tree during
  2015. a search, check out the variable `org-tags-match-list-sublevels'."
  2016. :group 'org-tags
  2017. :type '(choice
  2018. (const :tag "Not" nil)
  2019. (const :tag "Always" t)
  2020. (repeat :tag "Specific tags" (string :tag "Tag"))
  2021. (regexp :tag "Tags matched by regexp")))
  2022. (defcustom org-tags-exclude-from-inheritance nil
  2023. "List of tags that should never be inherited.
  2024. This is a way to exclude a few tags from inheritance. For way to do
  2025. the opposite, to actively allow inheritance for selected tags,
  2026. see the variable `org-use-tag-inheritance'."
  2027. :group 'org-tags
  2028. :type '(repeat (string :tag "Tag")))
  2029. (defun org-tag-inherit-p (tag)
  2030. "Check if TAG is one that should be inherited."
  2031. (cond
  2032. ((member tag org-tags-exclude-from-inheritance) nil)
  2033. ((eq org-use-tag-inheritance t) t)
  2034. ((not org-use-tag-inheritance) nil)
  2035. ((stringp org-use-tag-inheritance)
  2036. (string-match org-use-tag-inheritance tag))
  2037. ((listp org-use-tag-inheritance)
  2038. (member tag org-use-tag-inheritance))
  2039. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2040. (defcustom org-tags-match-list-sublevels t
  2041. "Non-nil means list also sublevels of headlines matching a search.
  2042. This variable applies to tags/property searches, and also to stuck
  2043. projects because this search is based on a tags match as well.
  2044. When set to the symbol `indented', sublevels are indented with
  2045. leading dots.
  2046. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2047. the sublevels of a headline matching a tag search often also match
  2048. the same search. Listing all of them can create very long lists.
  2049. Setting this variable to nil causes subtrees of a match to be skipped.
  2050. This variable is semi-obsolete and probably should always be true. It
  2051. is better to limit inheritance to certain tags using the variables
  2052. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2053. :group 'org-tags
  2054. :type '(choice
  2055. (const :tag "No, don't list them" nil)
  2056. (const :tag "Yes, do list them" t)
  2057. (const :tag "List them, indented with leading dots" indented)))
  2058. (defcustom org-tags-sort-function nil
  2059. "When set, tags are sorted using this function as a comparator"
  2060. :group 'org-tags
  2061. :type '(choice
  2062. (const :tag "No sorting" nil)
  2063. (const :tag "Alphabetical" string<)
  2064. (const :tag "Reverse alphabetical" string>)
  2065. (function :tag "Custom function" nil)))
  2066. (defvar org-tags-history nil
  2067. "History of minibuffer reads for tags.")
  2068. (defvar org-last-tags-completion-table nil
  2069. "The last used completion table for tags.")
  2070. (defvar org-after-tags-change-hook nil
  2071. "Hook that is run after the tags in a line have changed.")
  2072. (defgroup org-properties nil
  2073. "Options concerning properties in Org-mode."
  2074. :tag "Org Properties"
  2075. :group 'org)
  2076. (defcustom org-property-format "%-10s %s"
  2077. "How property key/value pairs should be formatted by `indent-line'.
  2078. When `indent-line' hits a property definition, it will format the line
  2079. according to this format, mainly to make sure that the values are
  2080. lined-up with respect to each other."
  2081. :group 'org-properties
  2082. :type 'string)
  2083. (defcustom org-use-property-inheritance nil
  2084. "Non-nil means, properties apply also for sublevels.
  2085. This setting is chiefly used during property searches. Turning it on can
  2086. cause significant overhead when doing a search, which is why it is not
  2087. on by default.
  2088. When nil, only the properties directly given in the current entry count.
  2089. When t, every property is inherited. The value may also be a list of
  2090. properties that should have inheritance, or a regular expression matching
  2091. properties that should be inherited.
  2092. However, note that some special properties use inheritance under special
  2093. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2094. and the properties ending in \"_ALL\" when they are used as descriptor
  2095. for valid values of a property.
  2096. Note for programmers:
  2097. When querying an entry with `org-entry-get', you can control if inheritance
  2098. should be used. By default, `org-entry-get' looks only at the local
  2099. properties. You can request inheritance by setting the inherit argument
  2100. to t (to force inheritance) or to `selective' (to respect the setting
  2101. in this variable)."
  2102. :group 'org-properties
  2103. :type '(choice
  2104. (const :tag "Not" nil)
  2105. (const :tag "Always" t)
  2106. (repeat :tag "Specific properties" (string :tag "Property"))
  2107. (regexp :tag "Properties matched by regexp")))
  2108. (defun org-property-inherit-p (property)
  2109. "Check if PROPERTY is one that should be inherited."
  2110. (cond
  2111. ((eq org-use-property-inheritance t) t)
  2112. ((not org-use-property-inheritance) nil)
  2113. ((stringp org-use-property-inheritance)
  2114. (string-match org-use-property-inheritance property))
  2115. ((listp org-use-property-inheritance)
  2116. (member property org-use-property-inheritance))
  2117. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2118. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2119. "The default column format, if no other format has been defined.
  2120. This variable can be set on the per-file basis by inserting a line
  2121. #+COLUMNS: %25ITEM ....."
  2122. :group 'org-properties
  2123. :type 'string)
  2124. (defcustom org-columns-ellipses ".."
  2125. "The ellipses to be used when a field in column view is truncated.
  2126. When this is the empty string, as many characters as possible are shown,
  2127. but then there will be no visual indication that the field has been truncated.
  2128. When this is a string of length N, the last N characters of a truncated
  2129. field are replaced by this string. If the column is narrower than the
  2130. ellipses string, only part of the ellipses string will be shown."
  2131. :group 'org-properties
  2132. :type 'string)
  2133. (defcustom org-columns-modify-value-for-display-function nil
  2134. "Function that modifies values for display in column view.
  2135. For example, it can be used to cut out a certain part from a time stamp.
  2136. The function must take 2 arguments:
  2137. column-title The title of the column (*not* the property name)
  2138. value The value that should be modified.
  2139. The function should return the value that should be displayed,
  2140. or nil if the normal value should be used."
  2141. :group 'org-properties
  2142. :type 'function)
  2143. (defcustom org-effort-property "Effort"
  2144. "The property that is being used to keep track of effort estimates.
  2145. Effort estimates given in this property need to have the format H:MM."
  2146. :group 'org-properties
  2147. :group 'org-progress
  2148. :type '(string :tag "Property"))
  2149. (defconst org-global-properties-fixed
  2150. '(("VISIBILITY_ALL" . "folded children content all")
  2151. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2152. "List of property/value pairs that can be inherited by any entry.
  2153. These are fixed values, for the preset properties. The user variable
  2154. that can be used to add to this list is `org-global-properties'.
  2155. The entries in this list are cons cells where the car is a property
  2156. name and cdr is a string with the value. If the value represents
  2157. multiple items like an \"_ALL\" property, separate the items by
  2158. spaces.")
  2159. (defcustom org-global-properties nil
  2160. "List of property/value pairs that can be inherited by any entry.
  2161. This list will be combined with the constant `org-global-properties-fixed'.
  2162. The entries in this list are cons cells where the car is a property
  2163. name and cdr is a string with the value.
  2164. You can set buffer-local values for the same purpose in the variable
  2165. `org-file-properties' this by adding lines like
  2166. #+PROPERTY: NAME VALUE"
  2167. :group 'org-properties
  2168. :type '(repeat
  2169. (cons (string :tag "Property")
  2170. (string :tag "Value"))))
  2171. (defvar org-file-properties nil
  2172. "List of property/value pairs that can be inherited by any entry.
  2173. Valid for the current buffer.
  2174. This variable is populated from #+PROPERTY lines.")
  2175. (make-variable-buffer-local 'org-file-properties)
  2176. (defgroup org-agenda nil
  2177. "Options concerning agenda views in Org-mode."
  2178. :tag "Org Agenda"
  2179. :group 'org)
  2180. (defvar org-category nil
  2181. "Variable used by org files to set a category for agenda display.
  2182. Such files should use a file variable to set it, for example
  2183. # -*- mode: org; org-category: \"ELisp\"
  2184. or contain a special line
  2185. #+CATEGORY: ELisp
  2186. If the file does not specify a category, then file's base name
  2187. is used instead.")
  2188. (make-variable-buffer-local 'org-category)
  2189. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2190. (defcustom org-agenda-files nil
  2191. "The files to be used for agenda display.
  2192. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2193. \\[org-remove-file]. You can also use customize to edit the list.
  2194. If an entry is a directory, all files in that directory that are matched by
  2195. `org-agenda-file-regexp' will be part of the file list.
  2196. If the value of the variable is not a list but a single file name, then
  2197. the list of agenda files is actually stored and maintained in that file, one
  2198. agenda file per line."
  2199. :group 'org-agenda
  2200. :type '(choice
  2201. (repeat :tag "List of files and directories" file)
  2202. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2203. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2204. "Regular expression to match files for `org-agenda-files'.
  2205. If any element in the list in that variable contains a directory instead
  2206. of a normal file, all files in that directory that are matched by this
  2207. regular expression will be included."
  2208. :group 'org-agenda
  2209. :type 'regexp)
  2210. (defcustom org-agenda-text-search-extra-files nil
  2211. "List of extra files to be searched by text search commands.
  2212. These files will be search in addition to the agenda files by the
  2213. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2214. Note that these files will only be searched for text search commands,
  2215. not for the other agenda views like todo lists, tag searches or the weekly
  2216. agenda. This variable is intended to list notes and possibly archive files
  2217. that should also be searched by these two commands.
  2218. In fact, if the first element in the list is the symbol `agenda-archives',
  2219. than all archive files of all agenda files will be added to the search
  2220. scope."
  2221. :group 'org-agenda
  2222. :type '(set :greedy t
  2223. (const :tag "Agenda Archives" agenda-archives)
  2224. (repeat :inline t (file))))
  2225. (if (fboundp 'defvaralias)
  2226. (defvaralias 'org-agenda-multi-occur-extra-files
  2227. 'org-agenda-text-search-extra-files))
  2228. (defcustom org-agenda-skip-unavailable-files nil
  2229. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2230. A nil value means to remove them, after a query, from the list."
  2231. :group 'org-agenda
  2232. :type 'boolean)
  2233. (defcustom org-calendar-to-agenda-key [?c]
  2234. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2235. The command `org-calendar-goto-agenda' will be bound to this key. The
  2236. default is the character `c' because then `c' can be used to switch back and
  2237. forth between agenda and calendar."
  2238. :group 'org-agenda
  2239. :type 'sexp)
  2240. (defcustom org-calendar-agenda-action-key [?k]
  2241. "The key to be installed in `calendar-mode-map' for agenda-action.
  2242. The command `org-agenda-action' will be bound to this key. The
  2243. default is the character `k' because we use the same key in the agenda."
  2244. :group 'org-agenda
  2245. :type 'sexp)
  2246. (eval-after-load "calendar"
  2247. '(progn
  2248. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2249. 'org-calendar-goto-agenda)
  2250. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2251. 'org-agenda-action)))
  2252. (defgroup org-latex nil
  2253. "Options for embedding LaTeX code into Org-mode."
  2254. :tag "Org LaTeX"
  2255. :group 'org)
  2256. (defcustom org-format-latex-options
  2257. '(:foreground default :background default :scale 1.0
  2258. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2259. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2260. "Options for creating images from LaTeX fragments.
  2261. This is a property list with the following properties:
  2262. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2263. `default' means use the foreground of the default face.
  2264. :background the background color, or \"Transparent\".
  2265. `default' means use the background of the default face.
  2266. :scale a scaling factor for the size of the images.
  2267. :html-foreground, :html-background, :html-scale
  2268. the same numbers for HTML export.
  2269. :matchers a list indicating which matchers should be used to
  2270. find LaTeX fragments. Valid members of this list are:
  2271. \"begin\" find environments
  2272. \"$1\" find single characters surrounded by $.$
  2273. \"$\" find math expressions surrounded by $...$
  2274. \"$$\" find math expressions surrounded by $$....$$
  2275. \"\\(\" find math expressions surrounded by \\(...\\)
  2276. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2277. :group 'org-latex
  2278. :type 'plist)
  2279. (defcustom org-format-latex-header "\\documentclass{article}
  2280. \\usepackage{fullpage} % do not remove
  2281. \\usepackage{amssymb}
  2282. \\usepackage[usenames]{color}
  2283. \\usepackage{amsmath}
  2284. \\usepackage{latexsym}
  2285. \\usepackage[mathscr]{eucal}
  2286. \\pagestyle{empty} % do not remove"
  2287. "The document header used for processing LaTeX fragments."
  2288. :group 'org-latex
  2289. :type 'string)
  2290. (defgroup org-font-lock nil
  2291. "Font-lock settings for highlighting in Org-mode."
  2292. :tag "Org Font Lock"
  2293. :group 'org)
  2294. (defcustom org-level-color-stars-only nil
  2295. "Non-nil means fontify only the stars in each headline.
  2296. When nil, the entire headline is fontified.
  2297. Changing it requires restart of `font-lock-mode' to become effective
  2298. also in regions already fontified."
  2299. :group 'org-font-lock
  2300. :type 'boolean)
  2301. (defcustom org-hide-leading-stars nil
  2302. "Non-nil means, hide the first N-1 stars in a headline.
  2303. This works by using the face `org-hide' for these stars. This
  2304. face is white for a light background, and black for a dark
  2305. background. You may have to customize the face `org-hide' to
  2306. make this work.
  2307. Changing it requires restart of `font-lock-mode' to become effective
  2308. also in regions already fontified.
  2309. You may also set this on a per-file basis by adding one of the following
  2310. lines to the buffer:
  2311. #+STARTUP: hidestars
  2312. #+STARTUP: showstars"
  2313. :group 'org-font-lock
  2314. :type 'boolean)
  2315. (defcustom org-fontify-done-headline nil
  2316. "Non-nil means, change the face of a headline if it is marked DONE.
  2317. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2318. When this is non-nil, the headline after the keyword is set to the
  2319. `org-headline-done' as an additional indication."
  2320. :group 'org-font-lock
  2321. :type 'boolean)
  2322. (defcustom org-fontify-emphasized-text t
  2323. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2324. Changing this variable requires a restart of Emacs to take effect."
  2325. :group 'org-font-lock
  2326. :type 'boolean)
  2327. (defcustom org-fontify-whole-heading-line nil
  2328. "Non-nil means fontify the whole line for headings.
  2329. This is useful when setting a background color for the
  2330. org-leve-* faces."
  2331. :group 'org-font-lock
  2332. :type 'boolean)
  2333. (defcustom org-highlight-latex-fragments-and-specials nil
  2334. "Non-nil means, fontify what is treated specially by the exporters."
  2335. :group 'org-font-lock
  2336. :type 'boolean)
  2337. (defcustom org-hide-emphasis-markers nil
  2338. "Non-nil mean font-lock should hide the emphasis marker characters."
  2339. :group 'org-font-lock
  2340. :type 'boolean)
  2341. (defvar org-emph-re nil
  2342. "Regular expression for matching emphasis.")
  2343. (defvar org-verbatim-re nil
  2344. "Regular expression for matching verbatim text.")
  2345. (defvar org-emphasis-regexp-components) ; defined just below
  2346. (defvar org-emphasis-alist) ; defined just below
  2347. (defun org-set-emph-re (var val)
  2348. "Set variable and compute the emphasis regular expression."
  2349. (set var val)
  2350. (when (and (boundp 'org-emphasis-alist)
  2351. (boundp 'org-emphasis-regexp-components)
  2352. org-emphasis-alist org-emphasis-regexp-components)
  2353. (let* ((e org-emphasis-regexp-components)
  2354. (pre (car e))
  2355. (post (nth 1 e))
  2356. (border (nth 2 e))
  2357. (body (nth 3 e))
  2358. (nl (nth 4 e))
  2359. (body1 (concat body "*?"))
  2360. (markers (mapconcat 'car org-emphasis-alist ""))
  2361. (vmarkers (mapconcat
  2362. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2363. org-emphasis-alist "")))
  2364. ;; make sure special characters appear at the right position in the class
  2365. (if (string-match "\\^" markers)
  2366. (setq markers (concat (replace-match "" t t markers) "^")))
  2367. (if (string-match "-" markers)
  2368. (setq markers (concat (replace-match "" t t markers) "-")))
  2369. (if (string-match "\\^" vmarkers)
  2370. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2371. (if (string-match "-" vmarkers)
  2372. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2373. (if (> nl 0)
  2374. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2375. (int-to-string nl) "\\}")))
  2376. ;; Make the regexp
  2377. (setq org-emph-re
  2378. (concat "\\([" pre "]\\|^\\)"
  2379. "\\("
  2380. "\\([" markers "]\\)"
  2381. "\\("
  2382. "[^" border "]\\|"
  2383. "[^" border "]"
  2384. body1
  2385. "[^" border "]"
  2386. "\\)"
  2387. "\\3\\)"
  2388. "\\([" post "]\\|$\\)"))
  2389. (setq org-verbatim-re
  2390. (concat "\\([" pre "]\\|^\\)"
  2391. "\\("
  2392. "\\([" vmarkers "]\\)"
  2393. "\\("
  2394. "[^" border "]\\|"
  2395. "[^" border "]"
  2396. body1
  2397. "[^" border "]"
  2398. "\\)"
  2399. "\\3\\)"
  2400. "\\([" post "]\\|$\\)")))))
  2401. (defcustom org-emphasis-regexp-components
  2402. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2403. "Components used to build the regular expression for emphasis.
  2404. This is a list with 6 entries. Terminology: In an emphasis string
  2405. like \" *strong word* \", we call the initial space PREMATCH, the final
  2406. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2407. and \"trong wor\" is the body. The different components in this variable
  2408. specify what is allowed/forbidden in each part:
  2409. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2410. post Chars allowed as postmatch. End of line will be allowed too.
  2411. border The chars *forbidden* as border characters.
  2412. body-regexp A regexp like \".\" to match a body character. Don't use
  2413. non-shy groups here, and don't allow newline here.
  2414. newline The maximum number of newlines allowed in an emphasis exp.
  2415. Use customize to modify this, or restart Emacs after changing it."
  2416. :group 'org-font-lock
  2417. :set 'org-set-emph-re
  2418. :type '(list
  2419. (sexp :tag "Allowed chars in pre ")
  2420. (sexp :tag "Allowed chars in post ")
  2421. (sexp :tag "Forbidden chars in border ")
  2422. (sexp :tag "Regexp for body ")
  2423. (integer :tag "number of newlines allowed")
  2424. (option (boolean :tag "Please ignore this button"))))
  2425. (defcustom org-emphasis-alist
  2426. `(("*" bold "<b>" "</b>")
  2427. ("/" italic "<i>" "</i>")
  2428. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2429. ("=" org-code "<code>" "</code>" verbatim)
  2430. ("~" org-verbatim "<code>" "</code>" verbatim)
  2431. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2432. "<del>" "</del>")
  2433. )
  2434. "Special syntax for emphasized text.
  2435. Text starting and ending with a special character will be emphasized, for
  2436. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2437. characters, the face to be used by font-lock for highlighting in Org-mode
  2438. Emacs buffers, and the HTML tags to be used for this.
  2439. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2440. Use customize to modify this, or restart Emacs after changing it."
  2441. :group 'org-font-lock
  2442. :set 'org-set-emph-re
  2443. :type '(repeat
  2444. (list
  2445. (string :tag "Marker character")
  2446. (choice
  2447. (face :tag "Font-lock-face")
  2448. (plist :tag "Face property list"))
  2449. (string :tag "HTML start tag")
  2450. (string :tag "HTML end tag")
  2451. (option (const verbatim)))))
  2452. (defvar org-protecting-blocks
  2453. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2454. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2455. This is needed for font-lock setup.")
  2456. ;;; Miscellaneous options
  2457. (defgroup org-completion nil
  2458. "Completion in Org-mode."
  2459. :tag "Org Completion"
  2460. :group 'org)
  2461. (defcustom org-completion-use-ido nil
  2462. "Non-nil means, use ido completion wherever possible.
  2463. Note that `ido-mode' must be active for this variable to be relevant.
  2464. If you decide to turn this variable on, you might well want to turn off
  2465. `org-outline-path-complete-in-steps'."
  2466. :group 'org-completion
  2467. :type 'boolean)
  2468. (defcustom org-completion-fallback-command 'hippie-expand
  2469. "The expansion command called by \\[org-complete] in normal context.
  2470. Normal means, no org-mode-specific context."
  2471. :group 'org-completion
  2472. :type 'function)
  2473. ;;; Functions and variables from ther packages
  2474. ;; Declared here to avoid compiler warnings
  2475. ;; XEmacs only
  2476. (defvar outline-mode-menu-heading)
  2477. (defvar outline-mode-menu-show)
  2478. (defvar outline-mode-menu-hide)
  2479. (defvar zmacs-regions) ; XEmacs regions
  2480. ;; Emacs only
  2481. (defvar mark-active)
  2482. ;; Various packages
  2483. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2484. (declare-function calendar-forward-day "cal-move" (arg))
  2485. (declare-function calendar-goto-date "cal-move" (date))
  2486. (declare-function calendar-goto-today "cal-move" ())
  2487. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2488. (defvar calc-embedded-close-formula)
  2489. (defvar calc-embedded-open-formula)
  2490. (declare-function cdlatex-tab "ext:cdlatex" ())
  2491. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2492. (defvar font-lock-unfontify-region-function)
  2493. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2494. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2495. (defvar iswitchb-temp-buflist)
  2496. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2497. (defvar org-agenda-tags-todo-honor-ignore-options)
  2498. (declare-function org-agenda-skip "org-agenda" ())
  2499. (declare-function org-format-agenda-item "org-agenda"
  2500. (extra txt &optional category tags dotime noprefix remove-re))
  2501. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2502. (declare-function org-agenda-change-all-lines "org-agenda"
  2503. (newhead hdmarker &optional fixface just-this))
  2504. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2505. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2506. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2507. (beg end))
  2508. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2509. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2510. "org-agenda" (&optional end))
  2511. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2512. (declare-function parse-time-string "parse-time" (string))
  2513. (declare-function remember "remember" (&optional initial))
  2514. (declare-function remember-buffer-desc "remember" ())
  2515. (declare-function remember-finalize "remember" ())
  2516. (defvar remember-save-after-remembering)
  2517. (defvar remember-data-file)
  2518. (defvar remember-register)
  2519. (defvar remember-buffer)
  2520. (defvar remember-handler-functions)
  2521. (defvar remember-annotation-functions)
  2522. (defvar texmathp-why)
  2523. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2524. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2525. (defvar w3m-current-url)
  2526. (defvar w3m-current-title)
  2527. (defvar org-latex-regexps)
  2528. ;;; Autoload and prepare some org modules
  2529. ;; Some table stuff that needs to be defined here, because it is used
  2530. ;; by the functions setting up org-mode or checking for table context.
  2531. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2532. "Detects an org-type or table-type table.")
  2533. (defconst org-table-line-regexp "^[ \t]*|"
  2534. "Detects an org-type table line.")
  2535. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2536. "Detects an org-type table line.")
  2537. (defconst org-table-hline-regexp "^[ \t]*|-"
  2538. "Detects an org-type table hline.")
  2539. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2540. "Detects a table-type table hline.")
  2541. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2542. "Searching from within a table (any type) this finds the first line
  2543. outside the table.")
  2544. ;; Autoload the functions in org-table.el that are needed by functions here.
  2545. (eval-and-compile
  2546. (org-autoload "org-table"
  2547. '(org-table-align org-table-begin org-table-blank-field
  2548. org-table-convert org-table-convert-region org-table-copy-down
  2549. org-table-copy-region org-table-create
  2550. org-table-create-or-convert-from-region
  2551. org-table-create-with-table.el org-table-current-dline
  2552. org-table-cut-region org-table-delete-column org-table-edit-field
  2553. org-table-edit-formulas org-table-end org-table-eval-formula
  2554. org-table-export org-table-field-info
  2555. org-table-get-stored-formulas org-table-goto-column
  2556. org-table-hline-and-move org-table-import org-table-insert-column
  2557. org-table-insert-hline org-table-insert-row org-table-iterate
  2558. org-table-justify-field-maybe org-table-kill-row
  2559. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2560. org-table-move-column org-table-move-column-left
  2561. org-table-move-column-right org-table-move-row
  2562. org-table-move-row-down org-table-move-row-up
  2563. org-table-next-field org-table-next-row org-table-paste-rectangle
  2564. org-table-previous-field org-table-recalculate
  2565. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2566. org-table-toggle-coordinate-overlays
  2567. org-table-toggle-formula-debugger org-table-wrap-region
  2568. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2569. (defun org-at-table-p (&optional table-type)
  2570. "Return t if the cursor is inside an org-type table.
  2571. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2572. (if org-enable-table-editor
  2573. (save-excursion
  2574. (beginning-of-line 1)
  2575. (looking-at (if table-type org-table-any-line-regexp
  2576. org-table-line-regexp)))
  2577. nil))
  2578. (defsubst org-table-p () (org-at-table-p))
  2579. (defun org-at-table.el-p ()
  2580. "Return t if and only if we are at a table.el table."
  2581. (and (org-at-table-p 'any)
  2582. (save-excursion
  2583. (goto-char (org-table-begin 'any))
  2584. (looking-at org-table1-hline-regexp))))
  2585. (defun org-table-recognize-table.el ()
  2586. "If there is a table.el table nearby, recognize it and move into it."
  2587. (if org-table-tab-recognizes-table.el
  2588. (if (org-at-table.el-p)
  2589. (progn
  2590. (beginning-of-line 1)
  2591. (if (looking-at org-table-dataline-regexp)
  2592. nil
  2593. (if (looking-at org-table1-hline-regexp)
  2594. (progn
  2595. (beginning-of-line 2)
  2596. (if (looking-at org-table-any-border-regexp)
  2597. (beginning-of-line -1)))))
  2598. (if (re-search-forward "|" (org-table-end t) t)
  2599. (progn
  2600. (require 'table)
  2601. (if (table--at-cell-p (point))
  2602. t
  2603. (message "recognizing table.el table...")
  2604. (table-recognize-table)
  2605. (message "recognizing table.el table...done")))
  2606. (error "This should not happen..."))
  2607. t)
  2608. nil)
  2609. nil))
  2610. (defun org-at-table-hline-p ()
  2611. "Return t if the cursor is inside a hline in a table."
  2612. (if org-enable-table-editor
  2613. (save-excursion
  2614. (beginning-of-line 1)
  2615. (looking-at org-table-hline-regexp))
  2616. nil))
  2617. (defvar org-table-clean-did-remove-column nil)
  2618. (defun org-table-map-tables (function)
  2619. "Apply FUNCTION to the start of all tables in the buffer."
  2620. (save-excursion
  2621. (save-restriction
  2622. (widen)
  2623. (goto-char (point-min))
  2624. (while (re-search-forward org-table-any-line-regexp nil t)
  2625. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2626. (beginning-of-line 1)
  2627. (if (looking-at org-table-line-regexp)
  2628. (save-excursion (funcall function)))
  2629. (re-search-forward org-table-any-border-regexp nil 1))))
  2630. (message "Mapping tables: done"))
  2631. ;; Declare and autoload functions from org-exp.el & Co
  2632. (declare-function org-default-export-plist "org-exp")
  2633. (declare-function org-infile-export-plist "org-exp")
  2634. (declare-function org-get-current-options "org-exp")
  2635. (eval-and-compile
  2636. (org-autoload "org-exp"
  2637. '(org-export org-export-visible
  2638. org-insert-export-options-template
  2639. org-table-clean-before-export))
  2640. (org-autoload "org-ascii"
  2641. '(org-export-as-ascii org-export-ascii-preprocess
  2642. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2643. org-export-region-as-ascii))
  2644. (org-autoload "org-html"
  2645. '(org-export-as-html-and-open
  2646. org-export-as-html-batch org-export-as-html-to-buffer
  2647. org-replace-region-by-html org-export-region-as-html
  2648. org-export-as-html))
  2649. (org-autoload "org-icalendar"
  2650. '(org-export-icalendar-this-file
  2651. org-export-icalendar-all-agenda-files
  2652. org-export-icalendar-combine-agenda-files))
  2653. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2654. ;; Declare and autoload functions from org-agenda.el
  2655. (eval-and-compile
  2656. (org-autoload "org-agenda"
  2657. '(org-agenda org-agenda-list org-search-view
  2658. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2659. org-diary org-agenda-to-appt
  2660. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2661. ;; Autoload org-remember
  2662. (eval-and-compile
  2663. (org-autoload "org-remember"
  2664. '(org-remember-insinuate org-remember-annotation
  2665. org-remember-apply-template org-remember org-remember-handler)))
  2666. ;; Autoload org-clock.el
  2667. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2668. (beg end))
  2669. (declare-function org-clock-update-mode-line "org-clock" ())
  2670. (defvar org-clock-start-time)
  2671. (defvar org-clock-marker (make-marker)
  2672. "Marker recording the last clock-in.")
  2673. (defun org-clock-is-active ()
  2674. "Return non-nil if clock is currently running.
  2675. The return value is actually the clock marker."
  2676. (marker-buffer org-clock-marker))
  2677. (eval-and-compile
  2678. (org-autoload
  2679. "org-clock"
  2680. '(org-clock-in org-clock-out org-clock-cancel
  2681. org-clock-goto org-clock-sum org-clock-display
  2682. org-clock-remove-overlays org-clock-report
  2683. org-clocktable-shift org-dblock-write:clocktable
  2684. org-get-clocktable)))
  2685. (defun org-clock-update-time-maybe ()
  2686. "If this is a CLOCK line, update it and return t.
  2687. Otherwise, return nil."
  2688. (interactive)
  2689. (save-excursion
  2690. (beginning-of-line 1)
  2691. (skip-chars-forward " \t")
  2692. (when (looking-at org-clock-string)
  2693. (let ((re (concat "[ \t]*" org-clock-string
  2694. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2695. "\\([ \t]*=>.*\\)?\\)?"))
  2696. ts te h m s neg)
  2697. (cond
  2698. ((not (looking-at re))
  2699. nil)
  2700. ((not (match-end 2))
  2701. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2702. (> org-clock-marker (point))
  2703. (<= org-clock-marker (point-at-eol)))
  2704. ;; The clock is running here
  2705. (setq org-clock-start-time
  2706. (apply 'encode-time
  2707. (org-parse-time-string (match-string 1))))
  2708. (org-clock-update-mode-line)))
  2709. (t
  2710. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2711. (end-of-line 1)
  2712. (setq ts (match-string 1)
  2713. te (match-string 3))
  2714. (setq s (- (time-to-seconds
  2715. (apply 'encode-time (org-parse-time-string te)))
  2716. (time-to-seconds
  2717. (apply 'encode-time (org-parse-time-string ts))))
  2718. neg (< s 0)
  2719. s (abs s)
  2720. h (floor (/ s 3600))
  2721. s (- s (* 3600 h))
  2722. m (floor (/ s 60))
  2723. s (- s (* 60 s)))
  2724. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2725. t))))))
  2726. (defun org-check-running-clock ()
  2727. "Check if the current buffer contains the running clock.
  2728. If yes, offer to stop it and to save the buffer with the changes."
  2729. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2730. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2731. (buffer-name))))
  2732. (org-clock-out)
  2733. (when (y-or-n-p "Save changed buffer?")
  2734. (save-buffer))))
  2735. (defun org-clocktable-try-shift (dir n)
  2736. "Check if this line starts a clock table, if yes, shift the time block."
  2737. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2738. (org-clocktable-shift dir n)))
  2739. ;; Autoload org-timer.el
  2740. (eval-and-compile
  2741. (org-autoload
  2742. "org-timer"
  2743. '(org-timer-start org-timer org-timer-item
  2744. org-timer-change-times-in-region
  2745. org-timer-set-timer
  2746. org-timer-reset-timers
  2747. org-timer-show-remaining-time)))
  2748. ;; Autoload org-feed.el
  2749. (eval-and-compile
  2750. (org-autoload
  2751. "org-feed"
  2752. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2753. ;; Autoload archiving code
  2754. ;; The stuff that is needed for cycling and tags has to be defined here.
  2755. (defgroup org-archive nil
  2756. "Options concerning archiving in Org-mode."
  2757. :tag "Org Archive"
  2758. :group 'org-structure)
  2759. (defcustom org-archive-location "%s_archive::"
  2760. "The location where subtrees should be archived.
  2761. The value of this variable is a string, consisting of two parts,
  2762. separated by a double-colon. The first part is a filename and
  2763. the second part is a headline.
  2764. When the filename is omitted, archiving happens in the same file.
  2765. %s in the filename will be replaced by the current file
  2766. name (without the directory part). Archiving to a different file
  2767. is useful to keep archived entries from contributing to the
  2768. Org-mode Agenda.
  2769. The archived entries will be filed as subtrees of the specified
  2770. headline. When the headline is omitted, the subtrees are simply
  2771. filed away at the end of the file, as top-level entries. Also in
  2772. the heading you can use %s to represent the file name, this can be
  2773. useful when using the same archive for a number of different files.
  2774. Here are a few examples:
  2775. \"%s_archive::\"
  2776. If the current file is Projects.org, archive in file
  2777. Projects.org_archive, as top-level trees. This is the default.
  2778. \"::* Archived Tasks\"
  2779. Archive in the current file, under the top-level headline
  2780. \"* Archived Tasks\".
  2781. \"~/org/archive.org::\"
  2782. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2783. \"~/org/archive.org::From %s\"
  2784. Archive in file ~/org/archive.org (absolute path), und headlines
  2785. \"From FILENAME\" where file name is the current file name.
  2786. \"basement::** Finished Tasks\"
  2787. Archive in file ./basement (relative path), as level 3 trees
  2788. below the level 2 heading \"** Finished Tasks\".
  2789. You may set this option on a per-file basis by adding to the buffer a
  2790. line like
  2791. #+ARCHIVE: basement::** Finished Tasks
  2792. You may also define it locally for a subtree by setting an ARCHIVE property
  2793. in the entry. If such a property is found in an entry, or anywhere up
  2794. the hierarchy, it will be used."
  2795. :group 'org-archive
  2796. :type 'string)
  2797. (defcustom org-archive-tag "ARCHIVE"
  2798. "The tag that marks a subtree as archived.
  2799. An archived subtree does not open during visibility cycling, and does
  2800. not contribute to the agenda listings.
  2801. After changing this, font-lock must be restarted in the relevant buffers to
  2802. get the proper fontification."
  2803. :group 'org-archive
  2804. :group 'org-keywords
  2805. :type 'string)
  2806. (defcustom org-agenda-skip-archived-trees t
  2807. "Non-nil means, the agenda will skip any items located in archived trees.
  2808. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2809. variable is no longer recommended, you should leave it at the value t.
  2810. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2811. :group 'org-archive
  2812. :group 'org-agenda-skip
  2813. :type 'boolean)
  2814. (defcustom org-columns-skip-arrchived-trees t
  2815. "Non-nil means, irgnore archived trees when creating column view."
  2816. :group 'org-archive
  2817. :group 'org-properties
  2818. :type 'boolean)
  2819. (defcustom org-cycle-open-archived-trees nil
  2820. "Non-nil means, `org-cycle' will open archived trees.
  2821. An archived tree is a tree marked with the tag ARCHIVE.
  2822. When nil, archived trees will stay folded. You can still open them with
  2823. normal outline commands like `show-all', but not with the cycling commands."
  2824. :group 'org-archive
  2825. :group 'org-cycle
  2826. :type 'boolean)
  2827. (defcustom org-sparse-tree-open-archived-trees nil
  2828. "Non-nil means sparse tree construction shows matches in archived trees.
  2829. When nil, matches in these trees are highlighted, but the trees are kept in
  2830. collapsed state."
  2831. :group 'org-archive
  2832. :group 'org-sparse-trees
  2833. :type 'boolean)
  2834. (defun org-cycle-hide-archived-subtrees (state)
  2835. "Re-hide all archived subtrees after a visibility state change."
  2836. (when (and (not org-cycle-open-archived-trees)
  2837. (not (memq state '(overview folded))))
  2838. (save-excursion
  2839. (let* ((globalp (memq state '(contents all)))
  2840. (beg (if globalp (point-min) (point)))
  2841. (end (if globalp (point-max) (org-end-of-subtree t))))
  2842. (org-hide-archived-subtrees beg end)
  2843. (goto-char beg)
  2844. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2845. (message "%s" (substitute-command-keys
  2846. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2847. (defun org-force-cycle-archived ()
  2848. "Cycle subtree even if it is archived."
  2849. (interactive)
  2850. (setq this-command 'org-cycle)
  2851. (let ((org-cycle-open-archived-trees t))
  2852. (call-interactively 'org-cycle)))
  2853. (defun org-hide-archived-subtrees (beg end)
  2854. "Re-hide all archived subtrees after a visibility state change."
  2855. (save-excursion
  2856. (let* ((re (concat ":" org-archive-tag ":")))
  2857. (goto-char beg)
  2858. (while (re-search-forward re end t)
  2859. (and (org-on-heading-p) (hide-subtree))
  2860. (org-end-of-subtree t)))))
  2861. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2862. (eval-and-compile
  2863. (org-autoload "org-archive"
  2864. '(org-add-archive-files org-archive-subtree
  2865. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2866. ;; Autoload Column View Code
  2867. (declare-function org-columns-number-to-string "org-colview")
  2868. (declare-function org-columns-get-format-and-top-level "org-colview")
  2869. (declare-function org-columns-compute "org-colview")
  2870. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2871. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2872. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2873. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2874. ;; Autoload ID code
  2875. (declare-function org-id-store-link "org-id")
  2876. (declare-function org-id-locations-load "org-id")
  2877. (declare-function org-id-locations-save "org-id")
  2878. (defvar org-id-track-globally)
  2879. (org-autoload "org-id"
  2880. '(org-id-get-create org-id-new org-id-copy org-id-get
  2881. org-id-get-with-outline-path-completion
  2882. org-id-get-with-outline-drilling
  2883. org-id-goto org-id-find org-id-store-link))
  2884. ;; Autoload Plotting Code
  2885. (org-autoload "org-plot"
  2886. '(org-plot/gnuplot))
  2887. ;;; Variables for pre-computed regular expressions, all buffer local
  2888. (defvar org-drawer-regexp nil
  2889. "Matches first line of a hidden block.")
  2890. (make-variable-buffer-local 'org-drawer-regexp)
  2891. (defvar org-todo-regexp nil
  2892. "Matches any of the TODO state keywords.")
  2893. (make-variable-buffer-local 'org-todo-regexp)
  2894. (defvar org-not-done-regexp nil
  2895. "Matches any of the TODO state keywords except the last one.")
  2896. (make-variable-buffer-local 'org-not-done-regexp)
  2897. (defvar org-not-done-heading-regexp nil
  2898. "Matches a TODO headline that is not done.")
  2899. (make-variable-buffer-local 'org-not-done-regexp)
  2900. (defvar org-todo-line-regexp nil
  2901. "Matches a headline and puts TODO state into group 2 if present.")
  2902. (make-variable-buffer-local 'org-todo-line-regexp)
  2903. (defvar org-complex-heading-regexp nil
  2904. "Matches a headline and puts everything into groups:
  2905. group 1: the stars
  2906. group 2: The todo keyword, maybe
  2907. group 3: Priority cookie
  2908. group 4: True headline
  2909. group 5: Tags")
  2910. (make-variable-buffer-local 'org-complex-heading-regexp)
  2911. (defvar org-todo-line-tags-regexp nil
  2912. "Matches a headline and puts TODO state into group 2 if present.
  2913. Also put tags into group 4 if tags are present.")
  2914. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2915. (defvar org-nl-done-regexp nil
  2916. "Matches newline followed by a headline with the DONE keyword.")
  2917. (make-variable-buffer-local 'org-nl-done-regexp)
  2918. (defvar org-looking-at-done-regexp nil
  2919. "Matches the DONE keyword a point.")
  2920. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2921. (defvar org-ds-keyword-length 12
  2922. "Maximum length of the Deadline and SCHEDULED keywords.")
  2923. (make-variable-buffer-local 'org-ds-keyword-length)
  2924. (defvar org-deadline-regexp nil
  2925. "Matches the DEADLINE keyword.")
  2926. (make-variable-buffer-local 'org-deadline-regexp)
  2927. (defvar org-deadline-time-regexp nil
  2928. "Matches the DEADLINE keyword together with a time stamp.")
  2929. (make-variable-buffer-local 'org-deadline-time-regexp)
  2930. (defvar org-deadline-line-regexp nil
  2931. "Matches the DEADLINE keyword and the rest of the line.")
  2932. (make-variable-buffer-local 'org-deadline-line-regexp)
  2933. (defvar org-scheduled-regexp nil
  2934. "Matches the SCHEDULED keyword.")
  2935. (make-variable-buffer-local 'org-scheduled-regexp)
  2936. (defvar org-scheduled-time-regexp nil
  2937. "Matches the SCHEDULED keyword together with a time stamp.")
  2938. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2939. (defvar org-closed-time-regexp nil
  2940. "Matches the CLOSED keyword together with a time stamp.")
  2941. (make-variable-buffer-local 'org-closed-time-regexp)
  2942. (defvar org-keyword-time-regexp nil
  2943. "Matches any of the 4 keywords, together with the time stamp.")
  2944. (make-variable-buffer-local 'org-keyword-time-regexp)
  2945. (defvar org-keyword-time-not-clock-regexp nil
  2946. "Matches any of the 3 keywords, together with the time stamp.")
  2947. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2948. (defvar org-maybe-keyword-time-regexp nil
  2949. "Matches a timestamp, possibly preceeded by a keyword.")
  2950. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2951. (defvar org-planning-or-clock-line-re nil
  2952. "Matches a line with planning or clock info.")
  2953. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2954. (defconst org-plain-time-of-day-regexp
  2955. (concat
  2956. "\\(\\<[012]?[0-9]"
  2957. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2958. "\\(--?"
  2959. "\\(\\<[012]?[0-9]"
  2960. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2961. "\\)?")
  2962. "Regular expression to match a plain time or time range.
  2963. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2964. groups carry important information:
  2965. 0 the full match
  2966. 1 the first time, range or not
  2967. 8 the second time, if it is a range.")
  2968. (defconst org-plain-time-extension-regexp
  2969. (concat
  2970. "\\(\\<[012]?[0-9]"
  2971. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2972. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2973. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2974. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2975. groups carry important information:
  2976. 0 the full match
  2977. 7 hours of duration
  2978. 9 minutes of duration")
  2979. (defconst org-stamp-time-of-day-regexp
  2980. (concat
  2981. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2982. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2983. "\\(--?"
  2984. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2985. "Regular expression to match a timestamp time or time range.
  2986. After a match, the following groups carry important information:
  2987. 0 the full match
  2988. 1 date plus weekday, for backreferencing to make sure both times on same day
  2989. 2 the first time, range or not
  2990. 4 the second time, if it is a range.")
  2991. (defconst org-startup-options
  2992. '(("fold" org-startup-folded t)
  2993. ("overview" org-startup-folded t)
  2994. ("nofold" org-startup-folded nil)
  2995. ("showall" org-startup-folded nil)
  2996. ("content" org-startup-folded content)
  2997. ("hidestars" org-hide-leading-stars t)
  2998. ("showstars" org-hide-leading-stars nil)
  2999. ("odd" org-odd-levels-only t)
  3000. ("oddeven" org-odd-levels-only nil)
  3001. ("align" org-startup-align-all-tables t)
  3002. ("noalign" org-startup-align-all-tables nil)
  3003. ("customtime" org-display-custom-times t)
  3004. ("logdone" org-log-done time)
  3005. ("lognotedone" org-log-done note)
  3006. ("nologdone" org-log-done nil)
  3007. ("lognoteclock-out" org-log-note-clock-out t)
  3008. ("nolognoteclock-out" org-log-note-clock-out nil)
  3009. ("logrepeat" org-log-repeat state)
  3010. ("lognoterepeat" org-log-repeat note)
  3011. ("nologrepeat" org-log-repeat nil)
  3012. ("fninline" org-footnote-define-inline t)
  3013. ("nofninline" org-footnote-define-inline nil)
  3014. ("fnlocal" org-footnote-section nil)
  3015. ("fnauto" org-footnote-auto-label t)
  3016. ("fnprompt" org-footnote-auto-label nil)
  3017. ("fnconfirm" org-footnote-auto-label confirm)
  3018. ("fnplain" org-footnote-auto-label plain)
  3019. ("fnadjust" org-footnote-auto-adjust t)
  3020. ("nofnadjust" org-footnote-auto-adjust nil)
  3021. ("constcgs" constants-unit-system cgs)
  3022. ("constSI" constants-unit-system SI)
  3023. ("noptag" org-tag-persistent-alist nil)
  3024. ("hideblocks" org-hide-block-startup t)
  3025. ("nohideblocks" org-hide-block-startup nil))
  3026. "Variable associated with STARTUP options for org-mode.
  3027. Each element is a list of three items: The startup options as written
  3028. in the #+STARTUP line, the corresponding variable, and the value to
  3029. set this variable to if the option is found. An optional forth element PUSH
  3030. means to push this value onto the list in the variable.")
  3031. (defun org-set-regexps-and-options ()
  3032. "Precompute regular expressions for current buffer."
  3033. (when (org-mode-p)
  3034. (org-set-local 'org-todo-kwd-alist nil)
  3035. (org-set-local 'org-todo-key-alist nil)
  3036. (org-set-local 'org-todo-key-trigger nil)
  3037. (org-set-local 'org-todo-keywords-1 nil)
  3038. (org-set-local 'org-done-keywords nil)
  3039. (org-set-local 'org-todo-heads nil)
  3040. (org-set-local 'org-todo-sets nil)
  3041. (org-set-local 'org-todo-log-states nil)
  3042. (org-set-local 'org-file-properties nil)
  3043. (org-set-local 'org-file-tags nil)
  3044. (let ((re (org-make-options-regexp
  3045. '("CATEGORY" "TODO" "COLUMNS"
  3046. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3047. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3048. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3049. (splitre "[ \t]+")
  3050. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3051. tail sep kws1 prio props ftags drawers
  3052. ext-setup-or-nil setup-contents (start 0))
  3053. (save-excursion
  3054. (save-restriction
  3055. (widen)
  3056. (goto-char (point-min))
  3057. (while (or (and ext-setup-or-nil
  3058. (string-match re ext-setup-or-nil start)
  3059. (setq start (match-end 0)))
  3060. (and (setq ext-setup-or-nil nil start 0)
  3061. (re-search-forward re nil t)))
  3062. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3063. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3064. (cond
  3065. ((equal key "CATEGORY")
  3066. (if (string-match "[ \t]+$" value)
  3067. (setq value (replace-match "" t t value)))
  3068. (setq cat value))
  3069. ((member key '("SEQ_TODO" "TODO"))
  3070. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3071. ((equal key "TYP_TODO")
  3072. (push (cons 'type (org-split-string value splitre)) kwds))
  3073. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3074. ;; general TODO-like setup
  3075. (push (cons (intern (downcase (match-string 1 key)))
  3076. (org-split-string value splitre)) kwds))
  3077. ((equal key "TAGS")
  3078. (setq tags (append tags (if tags '("\\n") nil)
  3079. (org-split-string value splitre))))
  3080. ((equal key "COLUMNS")
  3081. (org-set-local 'org-columns-default-format value))
  3082. ((equal key "LINK")
  3083. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3084. (push (cons (match-string 1 value)
  3085. (org-trim (match-string 2 value)))
  3086. links)))
  3087. ((equal key "PRIORITIES")
  3088. (setq prio (org-split-string value " +")))
  3089. ((equal key "PROPERTY")
  3090. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3091. (push (cons (match-string 1 value) (match-string 2 value))
  3092. props)))
  3093. ((equal key "FILETAGS")
  3094. (when (string-match "\\S-" value)
  3095. (setq ftags
  3096. (append
  3097. ftags
  3098. (apply 'append
  3099. (mapcar (lambda (x) (org-split-string x ":"))
  3100. (org-split-string value)))))))
  3101. ((equal key "DRAWERS")
  3102. (setq drawers (org-split-string value splitre)))
  3103. ((equal key "CONSTANTS")
  3104. (setq const (append const (org-split-string value splitre))))
  3105. ((equal key "STARTUP")
  3106. (let ((opts (org-split-string value splitre))
  3107. l var val)
  3108. (while (setq l (pop opts))
  3109. (when (setq l (assoc l org-startup-options))
  3110. (setq var (nth 1 l) val (nth 2 l))
  3111. (if (not (nth 3 l))
  3112. (set (make-local-variable var) val)
  3113. (if (not (listp (symbol-value var)))
  3114. (set (make-local-variable var) nil))
  3115. (set (make-local-variable var) (symbol-value var))
  3116. (add-to-list var val))))))
  3117. ((equal key "ARCHIVE")
  3118. (string-match " *$" value)
  3119. (setq arch (replace-match "" t t value))
  3120. (remove-text-properties 0 (length arch)
  3121. '(face t fontified t) arch))
  3122. ((equal key "SETUPFILE")
  3123. (setq setup-contents (org-file-contents
  3124. (expand-file-name
  3125. (org-remove-double-quotes value))
  3126. 'noerror))
  3127. (if (not ext-setup-or-nil)
  3128. (setq ext-setup-or-nil setup-contents start 0)
  3129. (setq ext-setup-or-nil
  3130. (concat (substring ext-setup-or-nil 0 start)
  3131. "\n" setup-contents "\n"
  3132. (substring ext-setup-or-nil start)))))
  3133. ))))
  3134. (when cat
  3135. (org-set-local 'org-category (intern cat))
  3136. (push (cons "CATEGORY" cat) props))
  3137. (when prio
  3138. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3139. (setq prio (mapcar 'string-to-char prio))
  3140. (org-set-local 'org-highest-priority (nth 0 prio))
  3141. (org-set-local 'org-lowest-priority (nth 1 prio))
  3142. (org-set-local 'org-default-priority (nth 2 prio)))
  3143. (and props (org-set-local 'org-file-properties (nreverse props)))
  3144. (and ftags (org-set-local 'org-file-tags
  3145. (mapcar 'org-add-prop-inherited ftags)))
  3146. (and drawers (org-set-local 'org-drawers drawers))
  3147. (and arch (org-set-local 'org-archive-location arch))
  3148. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3149. ;; Process the TODO keywords
  3150. (unless kwds
  3151. ;; Use the global values as if they had been given locally.
  3152. (setq kwds (default-value 'org-todo-keywords))
  3153. (if (stringp (car kwds))
  3154. (setq kwds (list (cons org-todo-interpretation
  3155. (default-value 'org-todo-keywords)))))
  3156. (setq kwds (reverse kwds)))
  3157. (setq kwds (nreverse kwds))
  3158. (let (inter kws kw)
  3159. (while (setq kws (pop kwds))
  3160. (let ((kws (or
  3161. (run-hook-with-args-until-success
  3162. 'org-todo-setup-filter-hook kws)
  3163. kws)))
  3164. (setq inter (pop kws) sep (member "|" kws)
  3165. kws0 (delete "|" (copy-sequence kws))
  3166. kwsa nil
  3167. kws1 (mapcar
  3168. (lambda (x)
  3169. ;; 1 2
  3170. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3171. (progn
  3172. (setq kw (match-string 1 x)
  3173. key (and (match-end 2) (match-string 2 x))
  3174. log (org-extract-log-state-settings x))
  3175. (push (cons kw (and key (string-to-char key))) kwsa)
  3176. (and log (push log org-todo-log-states))
  3177. kw)
  3178. (error "Invalid TODO keyword %s" x)))
  3179. kws0)
  3180. kwsa (if kwsa (append '((:startgroup))
  3181. (nreverse kwsa)
  3182. '((:endgroup))))
  3183. hw (car kws1)
  3184. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3185. tail (list inter hw (car dws) (org-last dws))))
  3186. (add-to-list 'org-todo-heads hw 'append)
  3187. (push kws1 org-todo-sets)
  3188. (setq org-done-keywords (append org-done-keywords dws nil))
  3189. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3190. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3191. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3192. (setq org-todo-sets (nreverse org-todo-sets)
  3193. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3194. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3195. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3196. ;; Process the constants
  3197. (when const
  3198. (let (e cst)
  3199. (while (setq e (pop const))
  3200. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3201. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3202. (setq org-table-formula-constants-local cst)))
  3203. ;; Process the tags.
  3204. (when tags
  3205. (let (e tgs)
  3206. (while (setq e (pop tags))
  3207. (cond
  3208. ((equal e "{") (push '(:startgroup) tgs))
  3209. ((equal e "}") (push '(:endgroup) tgs))
  3210. ((equal e "\\n") (push '(:newline) tgs))
  3211. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3212. (push (cons (match-string 1 e)
  3213. (string-to-char (match-string 2 e)))
  3214. tgs))
  3215. (t (push (list e) tgs))))
  3216. (org-set-local 'org-tag-alist nil)
  3217. (while (setq e (pop tgs))
  3218. (or (and (stringp (car e))
  3219. (assoc (car e) org-tag-alist))
  3220. (push e org-tag-alist)))))
  3221. ;; Compute the regular expressions and other local variables
  3222. (if (not org-done-keywords)
  3223. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3224. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3225. (length org-scheduled-string)
  3226. (length org-clock-string)
  3227. (length org-closed-string)))
  3228. org-drawer-regexp
  3229. (concat "^[ \t]*:\\("
  3230. (mapconcat 'regexp-quote org-drawers "\\|")
  3231. "\\):[ \t]*$")
  3232. org-not-done-keywords
  3233. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3234. org-todo-regexp
  3235. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3236. "\\|") "\\)\\>")
  3237. org-not-done-regexp
  3238. (concat "\\<\\("
  3239. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3240. "\\)\\>")
  3241. org-not-done-heading-regexp
  3242. (concat "^\\(\\*+\\)[ \t]+\\("
  3243. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3244. "\\)\\>")
  3245. org-todo-line-regexp
  3246. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3247. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3248. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3249. org-complex-heading-regexp
  3250. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3251. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3252. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3253. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3254. org-nl-done-regexp
  3255. (concat "\n\\*+[ \t]+"
  3256. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3257. "\\)" "\\>")
  3258. org-todo-line-tags-regexp
  3259. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3260. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3261. (org-re
  3262. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3263. org-looking-at-done-regexp
  3264. (concat "^" "\\(?:"
  3265. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3266. "\\>")
  3267. org-deadline-regexp (concat "\\<" org-deadline-string)
  3268. org-deadline-time-regexp
  3269. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3270. org-deadline-line-regexp
  3271. (concat "\\<\\(" org-deadline-string "\\).*")
  3272. org-scheduled-regexp
  3273. (concat "\\<" org-scheduled-string)
  3274. org-scheduled-time-regexp
  3275. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3276. org-closed-time-regexp
  3277. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3278. org-keyword-time-regexp
  3279. (concat "\\<\\(" org-scheduled-string
  3280. "\\|" org-deadline-string
  3281. "\\|" org-closed-string
  3282. "\\|" org-clock-string "\\)"
  3283. " *[[<]\\([^]>]+\\)[]>]")
  3284. org-keyword-time-not-clock-regexp
  3285. (concat "\\<\\(" org-scheduled-string
  3286. "\\|" org-deadline-string
  3287. "\\|" org-closed-string
  3288. "\\)"
  3289. " *[[<]\\([^]>]+\\)[]>]")
  3290. org-maybe-keyword-time-regexp
  3291. (concat "\\(\\<\\(" org-scheduled-string
  3292. "\\|" org-deadline-string
  3293. "\\|" org-closed-string
  3294. "\\|" org-clock-string "\\)\\)?"
  3295. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3296. org-planning-or-clock-line-re
  3297. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3298. "\\|" org-deadline-string
  3299. "\\|" org-closed-string "\\|" org-clock-string
  3300. "\\)\\>\\)")
  3301. )
  3302. (org-compute-latex-and-specials-regexp)
  3303. (org-set-font-lock-defaults))))
  3304. (defun org-file-contents (file &optional noerror)
  3305. "Return the contents of FILE, as a string."
  3306. (if (or (not file)
  3307. (not (file-readable-p file)))
  3308. (if noerror
  3309. (progn
  3310. (message "Cannot read file %s" file)
  3311. (ding) (sit-for 2)
  3312. "")
  3313. (error "Cannot read file %s" file))
  3314. (with-temp-buffer
  3315. (insert-file-contents file)
  3316. (buffer-string))))
  3317. (defun org-extract-log-state-settings (x)
  3318. "Extract the log state setting from a TODO keyword string.
  3319. This will extract info from a string like \"WAIT(w@/!)\"."
  3320. (let (kw key log1 log2)
  3321. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3322. (setq kw (match-string 1 x)
  3323. key (and (match-end 2) (match-string 2 x))
  3324. log1 (and (match-end 3) (match-string 3 x))
  3325. log2 (and (match-end 4) (match-string 4 x)))
  3326. (and (or log1 log2)
  3327. (list kw
  3328. (and log1 (if (equal log1 "!") 'time 'note))
  3329. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3330. (defun org-remove-keyword-keys (list)
  3331. "Remove a pair of parenthesis at the end of each string in LIST."
  3332. (mapcar (lambda (x)
  3333. (if (string-match "(.*)$" x)
  3334. (substring x 0 (match-beginning 0))
  3335. x))
  3336. list))
  3337. ;; FIXME: this could be done much better, using second characters etc.
  3338. (defun org-assign-fast-keys (alist)
  3339. "Assign fast keys to a keyword-key alist.
  3340. Respect keys that are already there."
  3341. (let (new e k c c1 c2 (char ?a))
  3342. (while (setq e (pop alist))
  3343. (cond
  3344. ((equal e '(:startgroup)) (push e new))
  3345. ((equal e '(:endgroup)) (push e new))
  3346. ((equal e '(:newline)) (push e new))
  3347. (t
  3348. (setq k (car e) c2 nil)
  3349. (if (cdr e)
  3350. (setq c (cdr e))
  3351. ;; automatically assign a character.
  3352. (setq c1 (string-to-char
  3353. (downcase (substring
  3354. k (if (= (string-to-char k) ?@) 1 0)))))
  3355. (if (or (rassoc c1 new) (rassoc c1 alist))
  3356. (while (or (rassoc char new) (rassoc char alist))
  3357. (setq char (1+ char)))
  3358. (setq c2 c1))
  3359. (setq c (or c2 char)))
  3360. (push (cons k c) new))))
  3361. (nreverse new)))
  3362. ;;; Some variables used in various places
  3363. (defvar org-window-configuration nil
  3364. "Used in various places to store a window configuration.")
  3365. (defvar org-finish-function nil
  3366. "Function to be called when `C-c C-c' is used.
  3367. This is for getting out of special buffers like remember.")
  3368. ;; FIXME: Occasionally check by commenting these, to make sure
  3369. ;; no other functions uses these, forgetting to let-bind them.
  3370. (defvar entry)
  3371. (defvar last-state)
  3372. (defvar date)
  3373. ;; Defined somewhere in this file, but used before definition.
  3374. (defvar org-html-entities)
  3375. (defvar org-struct-menu)
  3376. (defvar org-org-menu)
  3377. (defvar org-tbl-menu)
  3378. (defvar org-agenda-keymap)
  3379. ;;;; Define the Org-mode
  3380. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3381. (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."))
  3382. ;; We use a before-change function to check if a table might need
  3383. ;; an update.
  3384. (defvar org-table-may-need-update t
  3385. "Indicates that a table might need an update.
  3386. This variable is set by `org-before-change-function'.
  3387. `org-table-align' sets it back to nil.")
  3388. (defun org-before-change-function (beg end)
  3389. "Every change indicates that a table might need an update."
  3390. (setq org-table-may-need-update t))
  3391. (defvar org-mode-map)
  3392. (defvar org-mode-hook nil
  3393. "Mode hook for Org-mode, run after the mode was turned on.")
  3394. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3395. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3396. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3397. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3398. (defvar org-table-buffer-is-an nil)
  3399. (defconst org-outline-regexp "\\*+ ")
  3400. ;;;###autoload
  3401. (define-derived-mode org-mode outline-mode "Org"
  3402. "Outline-based notes management and organizer, alias
  3403. \"Carsten's outline-mode for keeping track of everything.\"
  3404. Org-mode develops organizational tasks around a NOTES file which
  3405. contains information about projects as plain text. Org-mode is
  3406. implemented on top of outline-mode, which is ideal to keep the content
  3407. of large files well structured. It supports ToDo items, deadlines and
  3408. time stamps, which magically appear in the diary listing of the Emacs
  3409. calendar. Tables are easily created with a built-in table editor.
  3410. Plain text URL-like links connect to websites, emails (VM), Usenet
  3411. messages (Gnus), BBDB entries, and any files related to the project.
  3412. For printing and sharing of notes, an Org-mode file (or a part of it)
  3413. can be exported as a structured ASCII or HTML file.
  3414. The following commands are available:
  3415. \\{org-mode-map}"
  3416. ;; Get rid of Outline menus, they are not needed
  3417. ;; Need to do this here because define-derived-mode sets up
  3418. ;; the keymap so late. Still, it is a waste to call this each time
  3419. ;; we switch another buffer into org-mode.
  3420. (if (featurep 'xemacs)
  3421. (when (boundp 'outline-mode-menu-heading)
  3422. ;; Assume this is Greg's port, it used easymenu
  3423. (easy-menu-remove outline-mode-menu-heading)
  3424. (easy-menu-remove outline-mode-menu-show)
  3425. (easy-menu-remove outline-mode-menu-hide))
  3426. (define-key org-mode-map [menu-bar headings] 'undefined)
  3427. (define-key org-mode-map [menu-bar hide] 'undefined)
  3428. (define-key org-mode-map [menu-bar show] 'undefined))
  3429. (org-load-modules-maybe)
  3430. (easy-menu-add org-org-menu)
  3431. (easy-menu-add org-tbl-menu)
  3432. (org-install-agenda-files-menu)
  3433. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3434. (org-add-to-invisibility-spec '(org-cwidth))
  3435. (org-add-to-invisibility-spec '(org-hide-block . t))
  3436. (when (featurep 'xemacs)
  3437. (org-set-local 'line-move-ignore-invisible t))
  3438. (org-set-local 'outline-regexp org-outline-regexp)
  3439. (org-set-local 'outline-level 'org-outline-level)
  3440. (when (and org-ellipsis
  3441. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3442. (fboundp 'make-glyph-code))
  3443. (unless org-display-table
  3444. (setq org-display-table (make-display-table)))
  3445. (set-display-table-slot
  3446. org-display-table 4
  3447. (vconcat (mapcar
  3448. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3449. org-ellipsis)))
  3450. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3451. (setq buffer-display-table org-display-table))
  3452. (org-set-regexps-and-options)
  3453. (when (and org-tag-faces (not org-tags-special-faces-re))
  3454. ;; tag faces set outside customize.... force initialization.
  3455. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3456. ;; Calc embedded
  3457. (org-set-local 'calc-embedded-open-mode "# ")
  3458. (modify-syntax-entry ?# "<")
  3459. (modify-syntax-entry ?@ "w")
  3460. (if org-startup-truncated (setq truncate-lines t))
  3461. (org-set-local 'font-lock-unfontify-region-function
  3462. 'org-unfontify-region)
  3463. ;; Activate before-change-function
  3464. (org-set-local 'org-table-may-need-update t)
  3465. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3466. 'local)
  3467. ;; Check for running clock before killing a buffer
  3468. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3469. ;; Paragraphs and auto-filling
  3470. (org-set-autofill-regexps)
  3471. (setq indent-line-function 'org-indent-line-function)
  3472. (org-update-radio-target-regexp)
  3473. ;; Make sure dependence stuff works reliably, even for users who set it
  3474. ;; too late :-(
  3475. (if org-enforce-todo-dependencies
  3476. (add-hook 'org-blocker-hook
  3477. 'org-block-todo-from-children-or-siblings-or-parent)
  3478. (remove-hook 'org-blocker-hook
  3479. 'org-block-todo-from-children-or-siblings-or-parent))
  3480. (if org-enforce-todo-checkbox-dependencies
  3481. (add-hook 'org-blocker-hook
  3482. 'org-block-todo-from-checkboxes)
  3483. (remove-hook 'org-blocker-hook
  3484. 'org-block-todo-from-checkboxes))
  3485. ;; Comment characters
  3486. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3487. (org-set-local 'comment-padding " ")
  3488. ;; Align options lines
  3489. (org-set-local
  3490. 'align-mode-rules-list
  3491. '((org-in-buffer-settings
  3492. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3493. (modes . '(org-mode)))))
  3494. ;; Imenu
  3495. (org-set-local 'imenu-create-index-function
  3496. 'org-imenu-get-tree)
  3497. ;; Make isearch reveal context
  3498. (if (or (featurep 'xemacs)
  3499. (not (boundp 'outline-isearch-open-invisible-function)))
  3500. ;; Emacs 21 and XEmacs make use of the hook
  3501. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3502. ;; Emacs 22 deals with this through a special variable
  3503. (org-set-local 'outline-isearch-open-invisible-function
  3504. (lambda (&rest ignore) (org-show-context 'isearch))))
  3505. ;; If empty file that did not turn on org-mode automatically, make it to.
  3506. (if (and org-insert-mode-line-in-empty-file
  3507. (interactive-p)
  3508. (= (point-min) (point-max)))
  3509. (insert "# -*- mode: org -*-\n\n"))
  3510. (unless org-inhibit-startup
  3511. (when org-startup-align-all-tables
  3512. (let ((bmp (buffer-modified-p)))
  3513. (org-table-map-tables 'org-table-align)
  3514. (set-buffer-modified-p bmp)))
  3515. (org-set-startup-visibility)))
  3516. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3517. (defun org-current-time ()
  3518. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3519. (if (> (car org-time-stamp-rounding-minutes) 1)
  3520. (let ((r (car org-time-stamp-rounding-minutes))
  3521. (time (decode-time)))
  3522. (apply 'encode-time
  3523. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3524. (nthcdr 2 time))))
  3525. (current-time)))
  3526. ;;;; Font-Lock stuff, including the activators
  3527. (defvar org-mouse-map (make-sparse-keymap))
  3528. (org-defkey org-mouse-map
  3529. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3530. (org-defkey org-mouse-map
  3531. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3532. (when org-mouse-1-follows-link
  3533. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3534. (when org-tab-follows-link
  3535. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3536. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3537. (require 'font-lock)
  3538. (defconst org-non-link-chars "]\t\n\r<>")
  3539. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3540. "shell" "elisp"))
  3541. (defvar org-link-types-re nil
  3542. "Matches a link that has a url-like prefix like \"http:\"")
  3543. (defvar org-link-re-with-space nil
  3544. "Matches a link with spaces, optional angular brackets around it.")
  3545. (defvar org-link-re-with-space2 nil
  3546. "Matches a link with spaces, optional angular brackets around it.")
  3547. (defvar org-link-re-with-space3 nil
  3548. "Matches a link with spaces, only for internal part in bracket links.")
  3549. (defvar org-angle-link-re nil
  3550. "Matches link with angular brackets, spaces are allowed.")
  3551. (defvar org-plain-link-re nil
  3552. "Matches plain link, without spaces.")
  3553. (defvar org-bracket-link-regexp nil
  3554. "Matches a link in double brackets.")
  3555. (defvar org-bracket-link-analytic-regexp nil
  3556. "Regular expression used to analyze links.
  3557. Here is what the match groups contain after a match:
  3558. 1: http:
  3559. 2: http
  3560. 3: path
  3561. 4: [desc]
  3562. 5: desc")
  3563. (defvar org-bracket-link-analytic-regexp++ nil
  3564. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3565. (defvar org-any-link-re nil
  3566. "Regular expression matching any link.")
  3567. (defun org-make-link-regexps ()
  3568. "Update the link regular expressions.
  3569. This should be called after the variable `org-link-types' has changed."
  3570. (setq org-link-types-re
  3571. (concat
  3572. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3573. org-link-re-with-space
  3574. (concat
  3575. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3576. "\\([^" org-non-link-chars " ]"
  3577. "[^" org-non-link-chars "]*"
  3578. "[^" org-non-link-chars " ]\\)>?")
  3579. org-link-re-with-space2
  3580. (concat
  3581. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3582. "\\([^" org-non-link-chars " ]"
  3583. "[^\t\n\r]*"
  3584. "[^" org-non-link-chars " ]\\)>?")
  3585. org-link-re-with-space3
  3586. (concat
  3587. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3588. "\\([^" org-non-link-chars " ]"
  3589. "[^\t\n\r]*\\)")
  3590. org-angle-link-re
  3591. (concat
  3592. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3593. "\\([^" org-non-link-chars " ]"
  3594. "[^" org-non-link-chars "]*"
  3595. "\\)>")
  3596. org-plain-link-re
  3597. (concat
  3598. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3599. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3600. org-bracket-link-regexp
  3601. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3602. org-bracket-link-analytic-regexp
  3603. (concat
  3604. "\\[\\["
  3605. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3606. "\\([^]]+\\)"
  3607. "\\]"
  3608. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3609. "\\]")
  3610. org-bracket-link-analytic-regexp++
  3611. (concat
  3612. "\\[\\["
  3613. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3614. "\\([^]]+\\)"
  3615. "\\]"
  3616. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3617. "\\]")
  3618. org-any-link-re
  3619. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3620. org-angle-link-re "\\)\\|\\("
  3621. org-plain-link-re "\\)")))
  3622. (org-make-link-regexps)
  3623. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3624. "Regular expression for fast time stamp matching.")
  3625. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3626. "Regular expression for fast time stamp matching.")
  3627. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3628. "Regular expression matching time strings for analysis.
  3629. This one does not require the space after the date, so it can be used
  3630. on a string that terminates immediately after the date.")
  3631. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3632. "Regular expression matching time strings for analysis.")
  3633. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3634. "Regular expression matching time stamps, with groups.")
  3635. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3636. "Regular expression matching time stamps (also [..]), with groups.")
  3637. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3638. "Regular expression matching a time stamp range.")
  3639. (defconst org-tr-regexp-both
  3640. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3641. "Regular expression matching a time stamp range.")
  3642. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3643. org-ts-regexp "\\)?")
  3644. "Regular expression matching a time stamp or time stamp range.")
  3645. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3646. org-ts-regexp-both "\\)?")
  3647. "Regular expression matching a time stamp or time stamp range.
  3648. The time stamps may be either active or inactive.")
  3649. (defvar org-emph-face nil)
  3650. (defun org-do-emphasis-faces (limit)
  3651. "Run through the buffer and add overlays to links."
  3652. (let (rtn a)
  3653. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3654. (if (not (= (char-after (match-beginning 3))
  3655. (char-after (match-beginning 4))))
  3656. (progn
  3657. (setq rtn t)
  3658. (setq a (assoc (match-string 3) org-emphasis-alist))
  3659. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3660. 'face
  3661. (nth 1 a))
  3662. (and (nth 4 a)
  3663. (org-remove-flyspell-overlays-in
  3664. (match-beginning 0) (match-end 0)))
  3665. (add-text-properties (match-beginning 2) (match-end 2)
  3666. '(font-lock-multiline t))
  3667. (when org-hide-emphasis-markers
  3668. (add-text-properties (match-end 4) (match-beginning 5)
  3669. '(invisible org-link))
  3670. (add-text-properties (match-beginning 3) (match-end 3)
  3671. '(invisible org-link)))))
  3672. (backward-char 1))
  3673. rtn))
  3674. (defun org-emphasize (&optional char)
  3675. "Insert or change an emphasis, i.e. a font like bold or italic.
  3676. If there is an active region, change that region to a new emphasis.
  3677. If there is no region, just insert the marker characters and position
  3678. the cursor between them.
  3679. CHAR should be either the marker character, or the first character of the
  3680. HTML tag associated with that emphasis. If CHAR is a space, the means
  3681. to remove the emphasis of the selected region.
  3682. If char is not given (for example in an interactive call) it
  3683. will be prompted for."
  3684. (interactive)
  3685. (let ((eal org-emphasis-alist) e det
  3686. (erc org-emphasis-regexp-components)
  3687. (prompt "")
  3688. (string "") beg end move tag c s)
  3689. (if (org-region-active-p)
  3690. (setq beg (region-beginning) end (region-end)
  3691. string (buffer-substring beg end))
  3692. (setq move t))
  3693. (while (setq e (pop eal))
  3694. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3695. c (aref tag 0))
  3696. (push (cons c (string-to-char (car e))) det)
  3697. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3698. (substring tag 1)))))
  3699. (setq det (nreverse det))
  3700. (unless char
  3701. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3702. (setq char (read-char-exclusive)))
  3703. (setq char (or (cdr (assoc char det)) char))
  3704. (if (equal char ?\ )
  3705. (setq s "" move nil)
  3706. (unless (assoc (char-to-string char) org-emphasis-alist)
  3707. (error "No such emphasis marker: \"%c\"" char))
  3708. (setq s (char-to-string char)))
  3709. (while (and (> (length string) 1)
  3710. (equal (substring string 0 1) (substring string -1))
  3711. (assoc (substring string 0 1) org-emphasis-alist))
  3712. (setq string (substring string 1 -1)))
  3713. (setq string (concat s string s))
  3714. (if beg (delete-region beg end))
  3715. (unless (or (bolp)
  3716. (string-match (concat "[" (nth 0 erc) "\n]")
  3717. (char-to-string (char-before (point)))))
  3718. (insert " "))
  3719. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3720. (char-to-string (char-after (point))))
  3721. (insert " ") (backward-char 1))
  3722. (insert string)
  3723. (and move (backward-char 1))))
  3724. (defconst org-nonsticky-props
  3725. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3726. (defsubst org-rear-nonsticky-at (pos)
  3727. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3728. (defun org-activate-plain-links (limit)
  3729. "Run through the buffer and add overlays to links."
  3730. (catch 'exit
  3731. (let (f)
  3732. (if (re-search-forward org-plain-link-re limit t)
  3733. (progn
  3734. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3735. (setq f (get-text-property (match-beginning 0) 'face))
  3736. (if (or (eq f 'org-tag)
  3737. (and (listp f) (memq 'org-tag f)))
  3738. nil
  3739. (add-text-properties (match-beginning 0) (match-end 0)
  3740. (list 'mouse-face 'highlight
  3741. 'keymap org-mouse-map))
  3742. (org-rear-nonsticky-at (match-end 0)))
  3743. t)))))
  3744. (defun org-activate-code (limit)
  3745. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3746. (progn
  3747. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3748. (remove-text-properties (match-beginning 0) (match-end 0)
  3749. '(display t invisible t intangible t))
  3750. t)))
  3751. (defun org-fontify-meta-lines-and-blocks (limit)
  3752. "Fontify #+ lines and blocks, in the correct ways."
  3753. (let ((case-fold-search t))
  3754. (if (re-search-forward
  3755. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3756. limit t)
  3757. (let ((beg (match-beginning 0))
  3758. (beg1 (line-beginning-position 2))
  3759. (dc1 (downcase (match-string 2)))
  3760. (dc3 (downcase (match-string 3)))
  3761. end end1 quoting)
  3762. (cond
  3763. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3764. ;; a single line of backend-specific content
  3765. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3766. (remove-text-properties (match-beginning 0) (match-end 0)
  3767. '(display t invisible t intangible t))
  3768. (add-text-properties (match-beginning 1) (match-end 3)
  3769. '(font-lock-fontified t face org-meta-line))
  3770. (add-text-properties (match-beginning 6) (match-end 6)
  3771. '(font-lock-fontified t face org-block))
  3772. t)
  3773. ((and (match-end 4) (equal dc3 "begin"))
  3774. ;; Truely a block
  3775. (setq quoting (member (downcase (match-string 5))
  3776. org-protecting-blocks))
  3777. (when (re-search-forward
  3778. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3779. nil t) ;; on purpose, we look further than LIMIT
  3780. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3781. (when quoting
  3782. (remove-text-properties beg end
  3783. '(display t invisible t intangible t)))
  3784. (add-text-properties
  3785. beg end
  3786. '(font-lock-fontified t font-lock-multiline t))
  3787. (add-text-properties beg beg1 '(face org-meta-line))
  3788. (add-text-properties end1 end '(face org-meta-line))
  3789. (when quoting
  3790. (add-text-properties beg1 end1 '(face org-block)))
  3791. t))
  3792. ((not (member (char-after beg) '(?\ ?\t)))
  3793. ;; just any other in-buffer setting, but not indented
  3794. (add-text-properties
  3795. beg (match-end 0)
  3796. '(font-lock-fontified t face org-meta-line))
  3797. t)
  3798. ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:" "tblname:"))
  3799. (and (match-end 4) (equal dc3 "attr")))
  3800. (add-text-properties
  3801. beg (match-end 0)
  3802. '(font-lock-fontified t face org-meta-line))
  3803. t)
  3804. (t nil))))))
  3805. (defun org-activate-angle-links (limit)
  3806. "Run through the buffer and add overlays to links."
  3807. (if (re-search-forward org-angle-link-re limit t)
  3808. (progn
  3809. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3810. (add-text-properties (match-beginning 0) (match-end 0)
  3811. (list 'mouse-face 'highlight
  3812. 'keymap org-mouse-map))
  3813. (org-rear-nonsticky-at (match-end 0))
  3814. t)))
  3815. (defun org-activate-footnote-links (limit)
  3816. "Run through the buffer and add overlays to links."
  3817. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3818. limit t)
  3819. (progn
  3820. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3821. (add-text-properties (match-beginning 2) (match-end 2)
  3822. (list 'mouse-face 'highlight
  3823. 'keymap org-mouse-map
  3824. 'help-echo
  3825. (if (= (point-at-bol) (match-beginning 2))
  3826. "Footnote definition"
  3827. "Footnote reference")
  3828. ))
  3829. (org-rear-nonsticky-at (match-end 2))
  3830. t)))
  3831. (defun org-activate-bracket-links (limit)
  3832. "Run through the buffer and add overlays to bracketed links."
  3833. (if (re-search-forward org-bracket-link-regexp limit t)
  3834. (let* ((help (concat "LINK: "
  3835. (org-match-string-no-properties 1)))
  3836. ;; FIXME: above we should remove the escapes.
  3837. ;; but that requires another match, protecting match data,
  3838. ;; a lot of overhead for font-lock.
  3839. (ip (org-maybe-intangible
  3840. (list 'invisible 'org-link
  3841. 'keymap org-mouse-map 'mouse-face 'highlight
  3842. 'font-lock-multiline t 'help-echo help)))
  3843. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3844. 'font-lock-multiline t 'help-echo help)))
  3845. ;; We need to remove the invisible property here. Table narrowing
  3846. ;; may have made some of this invisible.
  3847. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3848. (remove-text-properties (match-beginning 0) (match-end 0)
  3849. '(invisible nil))
  3850. (if (match-end 3)
  3851. (progn
  3852. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3853. (org-rear-nonsticky-at (match-beginning 3))
  3854. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3855. (org-rear-nonsticky-at (match-end 3))
  3856. (add-text-properties (match-end 3) (match-end 0) ip)
  3857. (org-rear-nonsticky-at (match-end 0)))
  3858. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3859. (org-rear-nonsticky-at (match-beginning 1))
  3860. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3861. (org-rear-nonsticky-at (match-end 1))
  3862. (add-text-properties (match-end 1) (match-end 0) ip)
  3863. (org-rear-nonsticky-at (match-end 0)))
  3864. t)))
  3865. (defun org-activate-dates (limit)
  3866. "Run through the buffer and add overlays to dates."
  3867. (if (re-search-forward org-tsr-regexp-both limit t)
  3868. (progn
  3869. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3870. (add-text-properties (match-beginning 0) (match-end 0)
  3871. (list 'mouse-face 'highlight
  3872. 'keymap org-mouse-map))
  3873. (org-rear-nonsticky-at (match-end 0))
  3874. (when org-display-custom-times
  3875. (if (match-end 3)
  3876. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3877. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3878. t)))
  3879. (defvar org-target-link-regexp nil
  3880. "Regular expression matching radio targets in plain text.")
  3881. (make-variable-buffer-local 'org-target-link-regexp)
  3882. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3883. "Regular expression matching a link target.")
  3884. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3885. "Regular expression matching a radio target.")
  3886. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3887. "Regular expression matching any target.")
  3888. (defun org-activate-target-links (limit)
  3889. "Run through the buffer and add overlays to target matches."
  3890. (when org-target-link-regexp
  3891. (let ((case-fold-search t))
  3892. (if (re-search-forward org-target-link-regexp limit t)
  3893. (progn
  3894. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3895. (add-text-properties (match-beginning 0) (match-end 0)
  3896. (list 'mouse-face 'highlight
  3897. 'keymap org-mouse-map
  3898. 'help-echo "Radio target link"
  3899. 'org-linked-text t))
  3900. (org-rear-nonsticky-at (match-end 0))
  3901. t)))))
  3902. (defun org-update-radio-target-regexp ()
  3903. "Find all radio targets in this file and update the regular expression."
  3904. (interactive)
  3905. (when (memq 'radio org-activate-links)
  3906. (setq org-target-link-regexp
  3907. (org-make-target-link-regexp (org-all-targets 'radio)))
  3908. (org-restart-font-lock)))
  3909. (defun org-hide-wide-columns (limit)
  3910. (let (s e)
  3911. (setq s (text-property-any (point) (or limit (point-max))
  3912. 'org-cwidth t))
  3913. (when s
  3914. (setq e (next-single-property-change s 'org-cwidth))
  3915. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3916. (goto-char e)
  3917. t)))
  3918. (defvar org-latex-and-specials-regexp nil
  3919. "Regular expression for highlighting export special stuff.")
  3920. (defvar org-match-substring-regexp)
  3921. (defvar org-match-substring-with-braces-regexp)
  3922. (defvar org-export-html-special-string-regexps)
  3923. (defun org-compute-latex-and-specials-regexp ()
  3924. "Compute regular expression for stuff treated specially by exporters."
  3925. (if (not org-highlight-latex-fragments-and-specials)
  3926. (org-set-local 'org-latex-and-specials-regexp nil)
  3927. (require 'org-exp)
  3928. (let*
  3929. ((matchers (plist-get org-format-latex-options :matchers))
  3930. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3931. org-latex-regexps)))
  3932. (options (org-combine-plists (org-default-export-plist)
  3933. (org-infile-export-plist)))
  3934. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3935. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3936. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3937. (org-export-html-expand (plist-get options :expand-quoted-html))
  3938. (org-export-with-special-strings (plist-get options :special-strings))
  3939. (re-sub
  3940. (cond
  3941. ((equal org-export-with-sub-superscripts '{})
  3942. (list org-match-substring-with-braces-regexp))
  3943. (org-export-with-sub-superscripts
  3944. (list org-match-substring-regexp))
  3945. (t nil)))
  3946. (re-latex
  3947. (if org-export-with-LaTeX-fragments
  3948. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3949. (re-macros
  3950. (if org-export-with-TeX-macros
  3951. (list (concat "\\\\"
  3952. (regexp-opt
  3953. (append (mapcar 'car org-html-entities)
  3954. (if (boundp 'org-latex-entities)
  3955. (mapcar (lambda (x)
  3956. (or (car-safe x) x))
  3957. org-latex-entities)
  3958. nil))
  3959. 'words))) ; FIXME
  3960. ))
  3961. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3962. (re-special (if org-export-with-special-strings
  3963. (mapcar (lambda (x) (car x))
  3964. org-export-html-special-string-regexps)))
  3965. (re-rest
  3966. (delq nil
  3967. (list
  3968. (if org-export-html-expand "@<[^>\n]+>")
  3969. ))))
  3970. (org-set-local
  3971. 'org-latex-and-specials-regexp
  3972. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3973. re-rest) "\\|")))))
  3974. (defun org-do-latex-and-special-faces (limit)
  3975. "Run through the buffer and add overlays to links."
  3976. (when org-latex-and-specials-regexp
  3977. (let (rtn d)
  3978. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3979. limit t))
  3980. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3981. 'face))
  3982. '(org-code org-verbatim underline)))
  3983. (progn
  3984. (setq rtn t
  3985. d (cond ((member (char-after (1+ (match-beginning 0)))
  3986. '(?_ ?^)) 1)
  3987. (t 0)))
  3988. (font-lock-prepend-text-property
  3989. (+ d (match-beginning 0)) (match-end 0)
  3990. 'face 'org-latex-and-export-specials)
  3991. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3992. '(font-lock-multiline t)))))
  3993. rtn)))
  3994. (defun org-restart-font-lock ()
  3995. "Restart font-lock-mode, to force refontification."
  3996. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3997. (font-lock-mode -1)
  3998. (font-lock-mode 1)))
  3999. (defun org-all-targets (&optional radio)
  4000. "Return a list of all targets in this file.
  4001. With optional argument RADIO, only find radio targets."
  4002. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4003. rtn)
  4004. (save-excursion
  4005. (goto-char (point-min))
  4006. (while (re-search-forward re nil t)
  4007. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4008. rtn)))
  4009. (defun org-make-target-link-regexp (targets)
  4010. "Make regular expression matching all strings in TARGETS.
  4011. The regular expression finds the targets also if there is a line break
  4012. between words."
  4013. (and targets
  4014. (concat
  4015. "\\<\\("
  4016. (mapconcat
  4017. (lambda (x)
  4018. (while (string-match " +" x)
  4019. (setq x (replace-match "\\s-+" t t x)))
  4020. x)
  4021. targets
  4022. "\\|")
  4023. "\\)\\>")))
  4024. (defun org-activate-tags (limit)
  4025. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4026. (progn
  4027. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4028. (add-text-properties (match-beginning 1) (match-end 1)
  4029. (list 'mouse-face 'highlight
  4030. 'keymap org-mouse-map))
  4031. (org-rear-nonsticky-at (match-end 1))
  4032. t)))
  4033. (defun org-outline-level ()
  4034. (save-excursion
  4035. (looking-at outline-regexp)
  4036. (if (match-beginning 1)
  4037. (+ (org-get-string-indentation (match-string 1)) 1000)
  4038. (1- (- (match-end 0) (match-beginning 0))))))
  4039. (defvar org-font-lock-keywords nil)
  4040. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4041. "Regular expression matching a property line.")
  4042. (defvar org-font-lock-hook nil
  4043. "Functions to be called for special font lock stuff.")
  4044. (defun org-font-lock-hook (limit)
  4045. (run-hook-with-args 'org-font-lock-hook limit))
  4046. (defun org-set-font-lock-defaults ()
  4047. (let* ((em org-fontify-emphasized-text)
  4048. (lk org-activate-links)
  4049. (org-font-lock-extra-keywords
  4050. (list
  4051. ;; Call the hook
  4052. '(org-font-lock-hook)
  4053. ;; Headlines
  4054. `(,(if org-fontify-whole-heading-line
  4055. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4056. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4057. (1 (org-get-level-face 1))
  4058. (2 (org-get-level-face 2))
  4059. (3 (org-get-level-face 3)))
  4060. ;; Table lines
  4061. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4062. (1 'org-table t))
  4063. ;; Table internals
  4064. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4065. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4066. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4067. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4068. ;; Drawers
  4069. (list org-drawer-regexp '(0 'org-special-keyword t))
  4070. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4071. ;; Properties
  4072. (list org-property-re
  4073. '(1 'org-special-keyword t)
  4074. '(3 'org-property-value t))
  4075. ;; Links
  4076. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4077. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4078. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4079. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4080. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4081. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4082. (if (memq 'footnote lk) '(org-activate-footnote-links
  4083. (2 'org-footnote t)))
  4084. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4085. '(org-hide-wide-columns (0 nil append))
  4086. ;; TODO lines
  4087. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4088. '(1 (org-get-todo-face 1) t))
  4089. ;; DONE
  4090. (if org-fontify-done-headline
  4091. (list (concat "^[*]+ +\\<\\("
  4092. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4093. "\\)\\(.*\\)")
  4094. '(2 'org-headline-done t))
  4095. nil)
  4096. ;; Priorities
  4097. '(org-font-lock-add-priority-faces)
  4098. ;; Tags
  4099. '(org-font-lock-add-tag-faces)
  4100. ;; Special keywords
  4101. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4102. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4103. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4104. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4105. ;; Emphasis
  4106. (if em
  4107. (if (featurep 'xemacs)
  4108. '(org-do-emphasis-faces (0 nil append))
  4109. '(org-do-emphasis-faces)))
  4110. ;; Checkboxes
  4111. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4112. 2 'org-checkbox prepend)
  4113. (if org-provide-checkbox-statistics
  4114. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4115. (0 (org-get-checkbox-statistics-face) t)))
  4116. ;; Description list items
  4117. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4118. 2 'bold prepend)
  4119. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4120. '(1 'org-archived prepend))
  4121. ;; Specials
  4122. '(org-do-latex-and-special-faces)
  4123. ;; Code
  4124. '(org-activate-code (1 'org-code t))
  4125. ;; COMMENT
  4126. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4127. "\\|" org-quote-string "\\)\\>")
  4128. '(1 'org-special-keyword t))
  4129. '("^#.*" (0 'font-lock-comment-face t))
  4130. ;; Blocks and meta lines
  4131. '(org-fontify-meta-lines-and-blocks)
  4132. )))
  4133. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4134. ;; Now set the full font-lock-keywords
  4135. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4136. (org-set-local 'font-lock-defaults
  4137. '(org-font-lock-keywords t nil nil backward-paragraph))
  4138. (kill-local-variable 'font-lock-keywords) nil))
  4139. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4140. "Fontify string S like in Org-mode"
  4141. (with-temp-buffer
  4142. (insert s)
  4143. (let ((org-odd-levels-only odd-levels))
  4144. (org-mode)
  4145. (font-lock-fontify-buffer)
  4146. (buffer-string))))
  4147. (defvar org-m nil)
  4148. (defvar org-l nil)
  4149. (defvar org-f nil)
  4150. (defun org-get-level-face (n)
  4151. "Get the right face for match N in font-lock matching of headlines."
  4152. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4153. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4154. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4155. (cond
  4156. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4157. ((eq n 2) org-f)
  4158. (t (if org-level-color-stars-only nil org-f))))
  4159. (defun org-get-todo-face (kwd)
  4160. "Get the right face for a TODO keyword KWD.
  4161. If KWD is a number, get the corresponding match group."
  4162. (if (numberp kwd) (setq kwd (match-string kwd)))
  4163. (or (cdr (assoc kwd org-todo-keyword-faces))
  4164. (and (member kwd org-done-keywords) 'org-done)
  4165. 'org-todo))
  4166. (defun org-font-lock-add-tag-faces (limit)
  4167. "Add the special tag faces."
  4168. (when (and org-tag-faces org-tags-special-faces-re)
  4169. (while (re-search-forward org-tags-special-faces-re limit t)
  4170. (add-text-properties (match-beginning 1) (match-end 1)
  4171. (list 'face (org-get-tag-face 1)
  4172. 'font-lock-fontified t))
  4173. (backward-char 1))))
  4174. (defun org-font-lock-add-priority-faces (limit)
  4175. "Add the special priority faces."
  4176. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4177. (add-text-properties
  4178. (match-beginning 0) (match-end 0)
  4179. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4180. org-priority-faces))
  4181. 'org-special-keyword)
  4182. 'font-lock-fontified t))))
  4183. (defun org-get-tag-face (kwd)
  4184. "Get the right face for a TODO keyword KWD.
  4185. If KWD is a number, get the corresponding match group."
  4186. (if (numberp kwd) (setq kwd (match-string kwd)))
  4187. (or (cdr (assoc kwd org-tag-faces))
  4188. 'org-tag))
  4189. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4190. "Remove fontification and activation overlays from links."
  4191. (font-lock-default-unfontify-region beg end)
  4192. (let* ((buffer-undo-list t)
  4193. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4194. (inhibit-modification-hooks t)
  4195. deactivate-mark buffer-file-name buffer-file-truename)
  4196. (remove-text-properties beg end
  4197. '(mouse-face t keymap t org-linked-text t
  4198. invisible t intangible t
  4199. org-no-flyspell t))))
  4200. ;;;; Visibility cycling, including org-goto and indirect buffer
  4201. ;;; Cycling
  4202. (defvar org-cycle-global-status nil)
  4203. (make-variable-buffer-local 'org-cycle-global-status)
  4204. (defvar org-cycle-subtree-status nil)
  4205. (make-variable-buffer-local 'org-cycle-subtree-status)
  4206. ;;;###autoload
  4207. (defvar org-inlinetask-min-level)
  4208. (defun org-cycle (&optional arg)
  4209. "TAB-action and visibility cycling for Org-mode.
  4210. This is the command invoked in Org-moe by the TAB key. It's main purpose
  4211. is outine visibility cycling, but it also invokes other actions
  4212. in special contexts.
  4213. - When this function is called with a prefix argument, rotate the entire
  4214. buffer through 3 states (global cycling)
  4215. 1. OVERVIEW: Show only top-level headlines.
  4216. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4217. 3. SHOW ALL: Show everything.
  4218. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4219. determined by the variable `org-startup-folded', and by any VISIBILITY
  4220. properties in the buffer.
  4221. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4222. including any drawers.
  4223. - When inside a table, re-align the table and move to the next field.
  4224. - When point is at the beginning of a headline, rotate the subtree started
  4225. by this line through 3 different states (local cycling)
  4226. 1. FOLDED: Only the main headline is shown.
  4227. 2. CHILDREN: The main headline and the direct children are shown.
  4228. From this state, you can move to one of the children
  4229. and zoom in further.
  4230. 3. SUBTREE: Show the entire subtree, including body text.
  4231. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4232. - When there is a numeric prefix, go up to a heading with level ARG, do
  4233. a `show-subtree' and return to the previous cursor position. If ARG
  4234. is negative, go up that many levels.
  4235. - When point is not at the beginning of a headline, execute the global
  4236. binding for TAB, which is re-indenting the line. See the option
  4237. `org-cycle-emulate-tab' for details.
  4238. - Special case: if point is at the beginning of the buffer and there is
  4239. no headline in line 1, this function will act as if called with prefix arg.
  4240. But only if also the variable `org-cycle-global-at-bob' is t."
  4241. (interactive "P")
  4242. (org-load-modules-maybe)
  4243. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4244. (let* ((limit-level
  4245. (or org-cycle-max-level
  4246. (and (boundp 'org-inlinetask-min-level)
  4247. org-inlinetask-min-level
  4248. (1- org-inlinetask-min-level))))
  4249. (nstars (and limit-level
  4250. (if org-odd-levels-only
  4251. (and limit-level (1- (* limit-level 2)))
  4252. limit-level)))
  4253. (outline-regexp
  4254. (cond
  4255. ((not (org-mode-p)) outline-regexp)
  4256. ((or (eq org-cycle-include-plain-lists 'integrate)
  4257. (and org-cycle-include-plain-lists (org-at-item-p)))
  4258. (concat "\\(?:\\*"
  4259. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4260. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4261. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4262. (bob-special (and org-cycle-global-at-bob (bobp)
  4263. (not (looking-at outline-regexp))))
  4264. (org-cycle-hook
  4265. (if bob-special
  4266. (delq 'org-optimize-window-after-visibility-change
  4267. (copy-sequence org-cycle-hook))
  4268. org-cycle-hook))
  4269. (pos (point)))
  4270. (if (or bob-special (equal arg '(4)))
  4271. ;; special case: use global cycling
  4272. (setq arg t))
  4273. (cond
  4274. ((equal arg '(16))
  4275. (org-set-startup-visibility)
  4276. (message "Startup visibility, plus VISIBILITY properties"))
  4277. ((equal arg '(64))
  4278. (show-all)
  4279. (message "Entire buffer visible, including drawers"))
  4280. ((org-at-table-p 'any)
  4281. ;; Enter the table or move to the next field in the table
  4282. (or (org-table-recognize-table.el)
  4283. (progn
  4284. (if arg (org-table-edit-field t)
  4285. (org-table-justify-field-maybe)
  4286. (call-interactively 'org-table-next-field)))))
  4287. ((run-hook-with-args-until-success
  4288. 'org-tab-after-check-for-table-hook))
  4289. ((eq arg t) ;; Global cycling
  4290. (org-cycle-internal-global))
  4291. ((and org-drawers org-drawer-regexp
  4292. (save-excursion
  4293. (beginning-of-line 1)
  4294. (looking-at org-drawer-regexp)))
  4295. ;; Toggle block visibility
  4296. (org-flag-drawer
  4297. (not (get-char-property (match-end 0) 'invisible))))
  4298. ((integerp arg)
  4299. ;; Show-subtree, ARG levels up from here.
  4300. (save-excursion
  4301. (org-back-to-heading)
  4302. (outline-up-heading (if (< arg 0) (- arg)
  4303. (- (funcall outline-level) arg)))
  4304. (org-show-subtree)))
  4305. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4306. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4307. (org-cycle-internal-local))
  4308. ;; TAB emulation and template completion
  4309. (buffer-read-only (org-back-to-heading))
  4310. ((run-hook-with-args-until-success
  4311. 'org-tab-after-check-for-cycling-hook))
  4312. ((org-try-structure-completion))
  4313. ((org-try-cdlatex-tab))
  4314. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4315. (or (not (bolp))
  4316. (not (looking-at outline-regexp))))
  4317. (call-interactively (global-key-binding "\t")))
  4318. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4319. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4320. (or (and (eq org-cycle-emulate-tab 'white)
  4321. (= (match-end 0) (point-at-eol)))
  4322. (and (eq org-cycle-emulate-tab 'whitestart)
  4323. (>= (match-end 0) pos))))
  4324. t
  4325. (eq org-cycle-emulate-tab t))
  4326. (call-interactively (global-key-binding "\t")))
  4327. (t (save-excursion
  4328. (org-back-to-heading)
  4329. (org-cycle)))))))
  4330. (defun org-cycle-internal-global ()
  4331. "Do the global cycling action."
  4332. (cond
  4333. ((and (eq last-command this-command)
  4334. (eq org-cycle-global-status 'overview))
  4335. ;; We just created the overview - now do table of contents
  4336. ;; This can be slow in very large buffers, so indicate action
  4337. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4338. (message "CONTENTS...")
  4339. (org-content)
  4340. (message "CONTENTS...done")
  4341. (setq org-cycle-global-status 'contents)
  4342. (run-hook-with-args 'org-cycle-hook 'contents))
  4343. ((and (eq last-command this-command)
  4344. (eq org-cycle-global-status 'contents))
  4345. ;; We just showed the table of contents - now show everything
  4346. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4347. (show-all)
  4348. (message "SHOW ALL")
  4349. (setq org-cycle-global-status 'all)
  4350. (run-hook-with-args 'org-cycle-hook 'all))
  4351. (t
  4352. ;; Default action: go to overview
  4353. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4354. (org-overview)
  4355. (message "OVERVIEW")
  4356. (setq org-cycle-global-status 'overview)
  4357. (run-hook-with-args 'org-cycle-hook 'overview))))
  4358. (defun org-cycle-internal-local ()
  4359. "Do the local cycling action."
  4360. (org-back-to-heading)
  4361. (let ((goal-column 0) eoh eol eos)
  4362. ;; First, some boundaries
  4363. (save-excursion
  4364. (org-back-to-heading)
  4365. (save-excursion
  4366. (beginning-of-line 2)
  4367. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4368. ; XEmacs does not have `next-single-char-property-change'
  4369. ; I'm not sure about Emacs 21.
  4370. (while (and (not (eobp)) ;; this is like `next-line'
  4371. (get-char-property (1- (point)) 'invisible))
  4372. (beginning-of-line 2))
  4373. (while (and (not (eobp)) ;; this is like `next-line'
  4374. (get-char-property (1- (point)) 'invisible))
  4375. (goto-char (next-single-char-property-change (point) 'invisible))
  4376. (or (bolp) (beginning-of-line 2))))
  4377. (setq eol (point)))
  4378. (outline-end-of-heading) (setq eoh (point))
  4379. (org-end-of-subtree t)
  4380. (unless (eobp)
  4381. (skip-chars-forward " \t\n")
  4382. (beginning-of-line 1) ; in case this is an item
  4383. )
  4384. (setq eos (1- (point))))
  4385. ;; Find out what to do next and set `this-command'
  4386. (cond
  4387. ((= eos eoh)
  4388. ;; Nothing is hidden behind this heading
  4389. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4390. (message "EMPTY ENTRY")
  4391. (setq org-cycle-subtree-status nil)
  4392. (save-excursion
  4393. (goto-char eos)
  4394. (outline-next-heading)
  4395. (if (org-invisible-p) (org-flag-heading nil))))
  4396. ((or (>= eol eos)
  4397. (not (string-match "\\S-" (buffer-substring eol eos))))
  4398. ;; Entire subtree is hidden in one line: open it
  4399. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4400. (org-show-entry)
  4401. (show-children)
  4402. (message "CHILDREN")
  4403. (save-excursion
  4404. (goto-char eos)
  4405. (outline-next-heading)
  4406. (if (org-invisible-p) (org-flag-heading nil)))
  4407. (setq org-cycle-subtree-status 'children)
  4408. (run-hook-with-args 'org-cycle-hook 'children))
  4409. ((and (eq last-command this-command)
  4410. (eq org-cycle-subtree-status 'children))
  4411. ;; We just showed the children, now show everything.
  4412. (if (save-excursion
  4413. (beginning-of-line 2)
  4414. (re-search-forward org-complex-heading-regexp eos t))
  4415. (progn
  4416. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4417. (org-show-subtree)
  4418. (message "SUBTREE")
  4419. (setq org-cycle-subtree-status 'subtree)
  4420. (run-hook-with-args 'org-cycle-hook 'subtree))
  4421. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4422. (hide-subtree)
  4423. (message "FOLDED (NO SUBTREE)")
  4424. (setq org-cycle-subtree-status 'folded)
  4425. (run-hook-with-args 'org-cycle-hook 'folded)))
  4426. (t
  4427. ;; Default action: hide the subtree.
  4428. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4429. (hide-subtree)
  4430. (message "FOLDED")
  4431. (setq org-cycle-subtree-status 'folded)
  4432. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4433. ;;;###autoload
  4434. (defun org-global-cycle (&optional arg)
  4435. "Cycle the global visibility. For details see `org-cycle'.
  4436. With C-u prefix arg, switch to startup visibility.
  4437. With a numeric prefix, show all headlines up to that level."
  4438. (interactive "P")
  4439. (let ((org-cycle-include-plain-lists
  4440. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4441. (cond
  4442. ((integerp arg)
  4443. (show-all)
  4444. (hide-sublevels arg)
  4445. (setq org-cycle-global-status 'contents))
  4446. ((equal arg '(4))
  4447. (org-set-startup-visibility)
  4448. (message "Startup visibility, plus VISIBILITY properties."))
  4449. (t
  4450. (org-cycle '(4))))))
  4451. (defun org-set-startup-visibility ()
  4452. "Set the visibility required by startup options and properties."
  4453. (cond
  4454. ((eq org-startup-folded t)
  4455. (org-cycle '(4)))
  4456. ((eq org-startup-folded 'content)
  4457. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4458. (org-cycle '(4)) (org-cycle '(4)))))
  4459. (if org-hide-block-startup (org-hide-block-all))
  4460. (org-set-visibility-according-to-property 'no-cleanup)
  4461. (org-cycle-hide-archived-subtrees 'all)
  4462. (org-cycle-hide-drawers 'all)
  4463. (org-cycle-show-empty-lines 'all))
  4464. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4465. "Switch subtree visibilities according to :VISIBILITY: property."
  4466. (interactive)
  4467. (let (org-show-entry-below state)
  4468. (save-excursion
  4469. (goto-char (point-min))
  4470. (while (re-search-forward
  4471. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4472. nil t)
  4473. (setq state (match-string 1))
  4474. (save-excursion
  4475. (org-back-to-heading t)
  4476. (hide-subtree)
  4477. (org-reveal)
  4478. (cond
  4479. ((equal state '("fold" "folded"))
  4480. (hide-subtree))
  4481. ((equal state "children")
  4482. (org-show-hidden-entry)
  4483. (show-children))
  4484. ((equal state "content")
  4485. (save-excursion
  4486. (save-restriction
  4487. (org-narrow-to-subtree)
  4488. (org-content))))
  4489. ((member state '("all" "showall"))
  4490. (show-subtree)))))
  4491. (unless no-cleanup
  4492. (org-cycle-hide-archived-subtrees 'all)
  4493. (org-cycle-hide-drawers 'all)
  4494. (org-cycle-show-empty-lines 'all)))))
  4495. (defun org-overview ()
  4496. "Switch to overview mode, showing only top-level headlines.
  4497. Really, this shows all headlines with level equal or greater than the level
  4498. of the first headline in the buffer. This is important, because if the
  4499. first headline is not level one, then (hide-sublevels 1) gives confusing
  4500. results."
  4501. (interactive)
  4502. (let ((level (save-excursion
  4503. (goto-char (point-min))
  4504. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4505. (progn
  4506. (goto-char (match-beginning 0))
  4507. (funcall outline-level))))))
  4508. (and level (hide-sublevels level))))
  4509. (defun org-content (&optional arg)
  4510. "Show all headlines in the buffer, like a table of contents.
  4511. With numerical argument N, show content up to level N."
  4512. (interactive "P")
  4513. (save-excursion
  4514. ;; Visit all headings and show their offspring
  4515. (and (integerp arg) (org-overview))
  4516. (goto-char (point-max))
  4517. (catch 'exit
  4518. (while (and (progn (condition-case nil
  4519. (outline-previous-visible-heading 1)
  4520. (error (goto-char (point-min))))
  4521. t)
  4522. (looking-at outline-regexp))
  4523. (if (integerp arg)
  4524. (show-children (1- arg))
  4525. (show-branches))
  4526. (if (bobp) (throw 'exit nil))))))
  4527. (defun org-optimize-window-after-visibility-change (state)
  4528. "Adjust the window after a change in outline visibility.
  4529. This function is the default value of the hook `org-cycle-hook'."
  4530. (when (get-buffer-window (current-buffer))
  4531. (cond
  4532. ((eq state 'content) nil)
  4533. ((eq state 'all) nil)
  4534. ((eq state 'folded) nil)
  4535. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4536. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4537. ;; FIXME: no longer in use
  4538. (defun org-compact-display-after-subtree-move ()
  4539. "Show a compacter version of the tree of the entry's parent."
  4540. (save-excursion
  4541. (if (org-up-heading-safe)
  4542. (progn
  4543. (hide-subtree)
  4544. (show-entry)
  4545. (show-children)
  4546. (org-cycle-show-empty-lines 'children)
  4547. (org-cycle-hide-drawers 'children))
  4548. (org-overview))))
  4549. (defun org-remove-empty-overlays-at (pos)
  4550. "Remove outline overlays that do not contain non-white stuff."
  4551. (mapc
  4552. (lambda (o)
  4553. (and (eq 'outline (org-overlay-get o 'invisible))
  4554. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4555. (org-overlay-end o))))
  4556. (org-delete-overlay o)))
  4557. (org-overlays-at pos)))
  4558. (defun org-clean-visibility-after-subtree-move ()
  4559. "Fix visibility issues after moving a subtree."
  4560. ;; First, find a reasonable region to look at:
  4561. ;; Start two siblings above, end three below
  4562. (let* ((beg (save-excursion
  4563. (and (outline-get-last-sibling)
  4564. (outline-get-last-sibling))
  4565. (point)))
  4566. (end (save-excursion
  4567. (and (outline-get-next-sibling)
  4568. (outline-get-next-sibling)
  4569. (outline-get-next-sibling))
  4570. (if (org-at-heading-p)
  4571. (point-at-eol)
  4572. (point))))
  4573. (level (looking-at "\\*+"))
  4574. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4575. (save-excursion
  4576. (save-restriction
  4577. (narrow-to-region beg end)
  4578. (when re
  4579. ;; Properly fold already folded siblings
  4580. (goto-char (point-min))
  4581. (while (re-search-forward re nil t)
  4582. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4583. (hide-entry))))
  4584. (org-cycle-show-empty-lines 'overview)
  4585. (org-cycle-hide-drawers 'overview)))))
  4586. (defun org-cycle-show-empty-lines (state)
  4587. "Show empty lines above all visible headlines.
  4588. The region to be covered depends on STATE when called through
  4589. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4590. entire buffer covered. Note that an empty line is only shown if there
  4591. are at least `org-cycle-separator-lines' empty lines before the headline."
  4592. (when (> org-cycle-separator-lines 0)
  4593. (save-excursion
  4594. (let* ((n org-cycle-separator-lines)
  4595. (re (cond
  4596. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4597. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4598. (t (let ((ns (number-to-string (- n 2))))
  4599. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4600. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4601. beg end)
  4602. (cond
  4603. ((memq state '(overview contents t))
  4604. (setq beg (point-min) end (point-max)))
  4605. ((memq state '(children folded))
  4606. (setq beg (point) end (progn (org-end-of-subtree t t)
  4607. (beginning-of-line 2)
  4608. (point)))))
  4609. (when beg
  4610. (goto-char beg)
  4611. (while (re-search-forward re end t)
  4612. (if (not (get-char-property (match-end 1) 'invisible))
  4613. (outline-flag-region
  4614. (match-beginning 1) (match-end 1) nil)))))))
  4615. ;; Never hide empty lines at the end of the file.
  4616. (save-excursion
  4617. (goto-char (point-max))
  4618. (outline-previous-heading)
  4619. (outline-end-of-heading)
  4620. (if (and (looking-at "[ \t\n]+")
  4621. (= (match-end 0) (point-max)))
  4622. (outline-flag-region (point) (match-end 0) nil))))
  4623. (defun org-show-empty-lines-in-parent ()
  4624. "Move to the parent and re-show empty lines before visible headlines."
  4625. (save-excursion
  4626. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4627. (org-cycle-show-empty-lines context))))
  4628. (defun org-cycle-hide-drawers (state)
  4629. "Re-hide all drawers after a visibility state change."
  4630. (when (and (org-mode-p)
  4631. (not (memq state '(overview folded contents))))
  4632. (save-excursion
  4633. (let* ((globalp (memq state '(contents all)))
  4634. (beg (if globalp (point-min) (point)))
  4635. (end (if globalp (point-max)
  4636. (if (eq state 'children)
  4637. (save-excursion (outline-next-heading) (point))
  4638. (org-end-of-subtree t)))))
  4639. (goto-char beg)
  4640. (while (re-search-forward org-drawer-regexp end t)
  4641. (org-flag-drawer t))))))
  4642. (defun org-flag-drawer (flag)
  4643. (save-excursion
  4644. (beginning-of-line 1)
  4645. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4646. (let ((b (match-end 0))
  4647. (outline-regexp org-outline-regexp))
  4648. (if (re-search-forward
  4649. "^[ \t]*:END:"
  4650. (save-excursion (outline-next-heading) (point)) t)
  4651. (outline-flag-region b (point-at-eol) flag)
  4652. (error ":END: line missing"))))))
  4653. (defun org-subtree-end-visible-p ()
  4654. "Is the end of the current subtree visible?"
  4655. (pos-visible-in-window-p
  4656. (save-excursion (org-end-of-subtree t) (point))))
  4657. (defun org-first-headline-recenter (&optional N)
  4658. "Move cursor to the first headline and recenter the headline.
  4659. Optional argument N means, put the headline into the Nth line of the window."
  4660. (goto-char (point-min))
  4661. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4662. (beginning-of-line)
  4663. (recenter (prefix-numeric-value N))))
  4664. ;;; Folding of blocks
  4665. (defconst org-block-regexp
  4666. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4667. "Regular expression for hiding blocks.")
  4668. (defvar org-hide-block-overlays nil
  4669. "Overays hiding blocks.")
  4670. (make-variable-buffer-local 'org-hide-block-overlays)
  4671. (defun org-block-map (function &optional start end)
  4672. "Call func at the head of all source blocks in the current
  4673. buffer. Optional arguments START and END can be used to limit
  4674. the range."
  4675. (let ((start (or start (point-min)))
  4676. (end (or end (point-max))))
  4677. (save-excursion
  4678. (goto-char start)
  4679. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4680. (save-excursion
  4681. (save-match-data
  4682. (goto-char (match-beginning 0))
  4683. (funcall function)))))))
  4684. (defun org-hide-block-toggle-all ()
  4685. "Toggle the visibility of all blocks in the current buffer."
  4686. (org-block-map #'org-hide-block-toggle))
  4687. (defun org-hide-block-all ()
  4688. "Fold all blocks in the current buffer."
  4689. (interactive)
  4690. (org-show-block-all)
  4691. (org-block-map #'org-hide-block-toggle-maybe))
  4692. (defun org-show-block-all ()
  4693. "Unfold all blocks in the current buffer."
  4694. (mapc 'org-delete-overlay org-hide-block-overlays)
  4695. (setq org-hide-block-overlays nil))
  4696. (defun org-hide-block-toggle-maybe ()
  4697. "Toggle visibility of block at point."
  4698. (interactive)
  4699. (let ((case-fold-search t))
  4700. (if (save-excursion
  4701. (beginning-of-line 1)
  4702. (looking-at org-block-regexp))
  4703. (progn (org-hide-block-toggle)
  4704. t) ;; to signal that we took action
  4705. nil))) ;; to signal that we did not
  4706. (defun org-hide-block-toggle (&optional force)
  4707. "Toggle the visibility of the current block."
  4708. (interactive)
  4709. (save-excursion
  4710. (beginning-of-line)
  4711. (if (re-search-forward org-block-regexp nil t)
  4712. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4713. (end (match-end 0))
  4714. ov) ;; end of entire body
  4715. (if (memq t (mapcar (lambda (overlay)
  4716. (eq (org-overlay-get overlay 'invisible)
  4717. 'org-hide-block))
  4718. (org-overlays-at start)))
  4719. (if (or (not force) (eq force 'off))
  4720. (mapc (lambda (ov)
  4721. (when (member ov org-hide-block-overlays)
  4722. (setq org-hide-block-overlays
  4723. (delq ov org-hide-block-overlays)))
  4724. (when (eq (org-overlay-get ov 'invisible)
  4725. 'org-hide-block)
  4726. (org-delete-overlay ov)))
  4727. (org-overlays-at start)))
  4728. (setq ov (org-make-overlay start end))
  4729. (org-overlay-put ov 'invisible 'org-hide-block)
  4730. (push ov org-hide-block-overlays)))
  4731. (error "Not looking at a source block"))))
  4732. ;; org-tab-after-check-for-cycling-hook
  4733. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4734. ;; Remove overlays when changing major mode
  4735. (add-hook 'org-mode-hook
  4736. (lambda () (org-add-hook 'change-major-mode-hook
  4737. 'org-show-block-all 'append 'local)))
  4738. ;;; Org-goto
  4739. (defvar org-goto-window-configuration nil)
  4740. (defvar org-goto-marker nil)
  4741. (defvar org-goto-map
  4742. (let ((map (make-sparse-keymap)))
  4743. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4744. (while (setq cmd (pop cmds))
  4745. (substitute-key-definition cmd cmd map global-map)))
  4746. (suppress-keymap map)
  4747. (org-defkey map "\C-m" 'org-goto-ret)
  4748. (org-defkey map [(return)] 'org-goto-ret)
  4749. (org-defkey map [(left)] 'org-goto-left)
  4750. (org-defkey map [(right)] 'org-goto-right)
  4751. (org-defkey map [(control ?g)] 'org-goto-quit)
  4752. (org-defkey map "\C-i" 'org-cycle)
  4753. (org-defkey map [(tab)] 'org-cycle)
  4754. (org-defkey map [(down)] 'outline-next-visible-heading)
  4755. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4756. (if org-goto-auto-isearch
  4757. (if (fboundp 'define-key-after)
  4758. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4759. nil)
  4760. (org-defkey map "q" 'org-goto-quit)
  4761. (org-defkey map "n" 'outline-next-visible-heading)
  4762. (org-defkey map "p" 'outline-previous-visible-heading)
  4763. (org-defkey map "f" 'outline-forward-same-level)
  4764. (org-defkey map "b" 'outline-backward-same-level)
  4765. (org-defkey map "u" 'outline-up-heading))
  4766. (org-defkey map "/" 'org-occur)
  4767. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4768. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4769. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4770. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4771. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4772. map))
  4773. (defconst org-goto-help
  4774. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4775. RET=jump to location [Q]uit and return to previous location
  4776. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4777. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4778. ;; FIXME: Docstring doe not mention both interfaces
  4779. (defun org-goto (&optional alternative-interface)
  4780. "Look up a different location in the current file, keeping current visibility.
  4781. When you want look-up or go to a different location in a document, the
  4782. fastest way is often to fold the entire buffer and then dive into the tree.
  4783. This method has the disadvantage, that the previous location will be folded,
  4784. which may not be what you want.
  4785. This command works around this by showing a copy of the current buffer
  4786. in an indirect buffer, in overview mode. You can dive into the tree in
  4787. that copy, use org-occur and incremental search to find a location.
  4788. When pressing RET or `Q', the command returns to the original buffer in
  4789. which the visibility is still unchanged. After RET is will also jump to
  4790. the location selected in the indirect buffer and expose the
  4791. the headline hierarchy above."
  4792. (interactive "P")
  4793. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4794. (org-refile-use-outline-path t)
  4795. (org-refile-target-verify-function nil)
  4796. (interface
  4797. (if (not alternative-interface)
  4798. org-goto-interface
  4799. (if (eq org-goto-interface 'outline)
  4800. 'outline-path-completion
  4801. 'outline)))
  4802. (org-goto-start-pos (point))
  4803. (selected-point
  4804. (if (eq interface 'outline)
  4805. (car (org-get-location (current-buffer) org-goto-help))
  4806. (nth 3 (org-refile-get-location "Goto: ")))))
  4807. (if selected-point
  4808. (progn
  4809. (org-mark-ring-push org-goto-start-pos)
  4810. (goto-char selected-point)
  4811. (if (or (org-invisible-p) (org-invisible-p2))
  4812. (org-show-context 'org-goto)))
  4813. (message "Quit"))))
  4814. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4815. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4816. (defvar org-goto-local-auto-isearch-map) ; defined below
  4817. (defun org-get-location (buf help)
  4818. "Let the user select a location in the Org-mode buffer BUF.
  4819. This function uses a recursive edit. It returns the selected position
  4820. or nil."
  4821. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4822. (isearch-hide-immediately nil)
  4823. (isearch-search-fun-function
  4824. (lambda () 'org-goto-local-search-headings))
  4825. (org-goto-selected-point org-goto-exit-command))
  4826. (save-excursion
  4827. (save-window-excursion
  4828. (delete-other-windows)
  4829. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4830. (switch-to-buffer
  4831. (condition-case nil
  4832. (make-indirect-buffer (current-buffer) "*org-goto*")
  4833. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4834. (with-output-to-temp-buffer "*Help*"
  4835. (princ help))
  4836. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4837. (setq buffer-read-only nil)
  4838. (let ((org-startup-truncated t)
  4839. (org-startup-folded nil)
  4840. (org-startup-align-all-tables nil))
  4841. (org-mode)
  4842. (org-overview))
  4843. (setq buffer-read-only t)
  4844. (if (and (boundp 'org-goto-start-pos)
  4845. (integer-or-marker-p org-goto-start-pos))
  4846. (let ((org-show-hierarchy-above t)
  4847. (org-show-siblings t)
  4848. (org-show-following-heading t))
  4849. (goto-char org-goto-start-pos)
  4850. (and (org-invisible-p) (org-show-context)))
  4851. (goto-char (point-min)))
  4852. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4853. (message "Select location and press RET")
  4854. (use-local-map org-goto-map)
  4855. (recursive-edit)
  4856. ))
  4857. (kill-buffer "*org-goto*")
  4858. (cons org-goto-selected-point org-goto-exit-command)))
  4859. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4860. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4861. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4862. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4863. (defun org-goto-local-search-headings (string bound noerror)
  4864. "Search and make sure that any matches are in headlines."
  4865. (catch 'return
  4866. (while (if isearch-forward
  4867. (search-forward string bound noerror)
  4868. (search-backward string bound noerror))
  4869. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4870. (and (member :headline context)
  4871. (not (member :tags context))))
  4872. (throw 'return (point))))))
  4873. (defun org-goto-local-auto-isearch ()
  4874. "Start isearch."
  4875. (interactive)
  4876. (goto-char (point-min))
  4877. (let ((keys (this-command-keys)))
  4878. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4879. (isearch-mode t)
  4880. (isearch-process-search-char (string-to-char keys)))))
  4881. (defun org-goto-ret (&optional arg)
  4882. "Finish `org-goto' by going to the new location."
  4883. (interactive "P")
  4884. (setq org-goto-selected-point (point)
  4885. org-goto-exit-command 'return)
  4886. (throw 'exit nil))
  4887. (defun org-goto-left ()
  4888. "Finish `org-goto' by going to the new location."
  4889. (interactive)
  4890. (if (org-on-heading-p)
  4891. (progn
  4892. (beginning-of-line 1)
  4893. (setq org-goto-selected-point (point)
  4894. org-goto-exit-command 'left)
  4895. (throw 'exit nil))
  4896. (error "Not on a heading")))
  4897. (defun org-goto-right ()
  4898. "Finish `org-goto' by going to the new location."
  4899. (interactive)
  4900. (if (org-on-heading-p)
  4901. (progn
  4902. (setq org-goto-selected-point (point)
  4903. org-goto-exit-command 'right)
  4904. (throw 'exit nil))
  4905. (error "Not on a heading")))
  4906. (defun org-goto-quit ()
  4907. "Finish `org-goto' without cursor motion."
  4908. (interactive)
  4909. (setq org-goto-selected-point nil)
  4910. (setq org-goto-exit-command 'quit)
  4911. (throw 'exit nil))
  4912. ;;; Indirect buffer display of subtrees
  4913. (defvar org-indirect-dedicated-frame nil
  4914. "This is the frame being used for indirect tree display.")
  4915. (defvar org-last-indirect-buffer nil)
  4916. (defun org-tree-to-indirect-buffer (&optional arg)
  4917. "Create indirect buffer and narrow it to current subtree.
  4918. With numerical prefix ARG, go up to this level and then take that tree.
  4919. If ARG is negative, go up that many levels.
  4920. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4921. indirect buffer previously made with this command, to avoid proliferation of
  4922. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4923. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4924. is kept so that you can work with several indirect buffers at the same time.
  4925. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4926. requests that a new frame be made for the new buffer, so that the dedicated
  4927. frame is not changed."
  4928. (interactive "P")
  4929. (let ((cbuf (current-buffer))
  4930. (cwin (selected-window))
  4931. (pos (point))
  4932. beg end level heading ibuf)
  4933. (save-excursion
  4934. (org-back-to-heading t)
  4935. (when (numberp arg)
  4936. (setq level (org-outline-level))
  4937. (if (< arg 0) (setq arg (+ level arg)))
  4938. (while (> (setq level (org-outline-level)) arg)
  4939. (outline-up-heading 1 t)))
  4940. (setq beg (point)
  4941. heading (org-get-heading))
  4942. (org-end-of-subtree t) (setq end (point)))
  4943. (if (and (buffer-live-p org-last-indirect-buffer)
  4944. (not (eq org-indirect-buffer-display 'new-frame))
  4945. (not arg))
  4946. (kill-buffer org-last-indirect-buffer))
  4947. (setq ibuf (org-get-indirect-buffer cbuf)
  4948. org-last-indirect-buffer ibuf)
  4949. (cond
  4950. ((or (eq org-indirect-buffer-display 'new-frame)
  4951. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4952. (select-frame (make-frame))
  4953. (delete-other-windows)
  4954. (switch-to-buffer ibuf)
  4955. (org-set-frame-title heading))
  4956. ((eq org-indirect-buffer-display 'dedicated-frame)
  4957. (raise-frame
  4958. (select-frame (or (and org-indirect-dedicated-frame
  4959. (frame-live-p org-indirect-dedicated-frame)
  4960. org-indirect-dedicated-frame)
  4961. (setq org-indirect-dedicated-frame (make-frame)))))
  4962. (delete-other-windows)
  4963. (switch-to-buffer ibuf)
  4964. (org-set-frame-title (concat "Indirect: " heading)))
  4965. ((eq org-indirect-buffer-display 'current-window)
  4966. (switch-to-buffer ibuf))
  4967. ((eq org-indirect-buffer-display 'other-window)
  4968. (pop-to-buffer ibuf))
  4969. (t (error "Invalid value.")))
  4970. (if (featurep 'xemacs)
  4971. (save-excursion (org-mode) (turn-on-font-lock)))
  4972. (narrow-to-region beg end)
  4973. (show-all)
  4974. (goto-char pos)
  4975. (and (window-live-p cwin) (select-window cwin))))
  4976. (defun org-get-indirect-buffer (&optional buffer)
  4977. (setq buffer (or buffer (current-buffer)))
  4978. (let ((n 1) (base (buffer-name buffer)) bname)
  4979. (while (buffer-live-p
  4980. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4981. (setq n (1+ n)))
  4982. (condition-case nil
  4983. (make-indirect-buffer buffer bname 'clone)
  4984. (error (make-indirect-buffer buffer bname)))))
  4985. (defun org-set-frame-title (title)
  4986. "Set the title of the current frame to the string TITLE."
  4987. ;; FIXME: how to name a single frame in XEmacs???
  4988. (unless (featurep 'xemacs)
  4989. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4990. ;;;; Structure editing
  4991. ;;; Inserting headlines
  4992. (defun org-previous-line-empty-p ()
  4993. (save-excursion
  4994. (and (not (bobp))
  4995. (or (beginning-of-line 0) t)
  4996. (save-match-data
  4997. (looking-at "[ \t]*$")))))
  4998. (defun org-insert-heading (&optional force-heading)
  4999. "Insert a new heading or item with same depth at point.
  5000. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5001. If point is at the beginning of a headline, insert a sibling before the
  5002. current headline. If point is not at the beginning, do not split the line,
  5003. but create the new headline after the current line."
  5004. (interactive "P")
  5005. (if (= (buffer-size) 0)
  5006. (insert "\n* ")
  5007. (when (or force-heading (not (org-insert-item)))
  5008. (let* ((empty-line-p nil)
  5009. (head (save-excursion
  5010. (condition-case nil
  5011. (progn
  5012. (org-back-to-heading)
  5013. (setq empty-line-p (org-previous-line-empty-p))
  5014. (match-string 0))
  5015. (error "*"))))
  5016. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5017. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5018. pos hide-previous previous-pos)
  5019. (cond
  5020. ((and (org-on-heading-p) (bolp)
  5021. (or (bobp)
  5022. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5023. ;; insert before the current line
  5024. (open-line (if blank 2 1)))
  5025. ((and (bolp)
  5026. (or (bobp)
  5027. (save-excursion
  5028. (backward-char 1) (not (org-invisible-p)))))
  5029. ;; insert right here
  5030. nil)
  5031. (t
  5032. ;; somewhere in the line
  5033. (save-excursion
  5034. (setq previous-pos (point-at-bol))
  5035. (end-of-line)
  5036. (setq hide-previous (org-invisible-p)))
  5037. (and org-insert-heading-respect-content (org-show-subtree))
  5038. (let ((split
  5039. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5040. (save-excursion
  5041. (let ((p (point)))
  5042. (goto-char (point-at-bol))
  5043. (and (looking-at org-complex-heading-regexp)
  5044. (> p (match-beginning 4)))))))
  5045. tags pos)
  5046. (cond
  5047. (org-insert-heading-respect-content
  5048. (org-end-of-subtree nil t)
  5049. (or (bolp) (newline))
  5050. (or (org-previous-line-empty-p)
  5051. (and blank (newline)))
  5052. (open-line 1))
  5053. ((org-on-heading-p)
  5054. (when hide-previous
  5055. (show-children)
  5056. (org-show-entry))
  5057. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5058. (setq tags (and (match-end 2) (match-string 2)))
  5059. (and (match-end 1)
  5060. (delete-region (match-beginning 1) (match-end 1)))
  5061. (setq pos (point-at-bol))
  5062. (or split (end-of-line 1))
  5063. (delete-horizontal-space)
  5064. (newline (if blank 2 1))
  5065. (when tags
  5066. (save-excursion
  5067. (goto-char pos)
  5068. (end-of-line 1)
  5069. (insert " " tags)
  5070. (org-set-tags nil 'align))))
  5071. (t
  5072. (or split (end-of-line 1))
  5073. (newline (if blank 2 1)))))))
  5074. (insert head) (just-one-space)
  5075. (setq pos (point))
  5076. (end-of-line 1)
  5077. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5078. (when (and org-insert-heading-respect-content hide-previous)
  5079. (save-excursion
  5080. (goto-char previous-pos)
  5081. (hide-subtree)))
  5082. (run-hooks 'org-insert-heading-hook)))))
  5083. (defun org-get-heading (&optional no-tags)
  5084. "Return the heading of the current entry, without the stars."
  5085. (save-excursion
  5086. (org-back-to-heading t)
  5087. (if (looking-at
  5088. (if no-tags
  5089. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5090. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5091. (match-string 1) "")))
  5092. (defun org-heading-components ()
  5093. "Return the components of the current heading.
  5094. This is a list with the following elements:
  5095. - the level as an integer
  5096. - the reduced level, different if `org-odd-levels-only' is set.
  5097. - the TODO keyword, or nil
  5098. - the priority character, like ?A, or nil if no priority is given
  5099. - the headline text itself, or the tags string if no headline text
  5100. - the tags string, or nil."
  5101. (save-excursion
  5102. (org-back-to-heading t)
  5103. (if (looking-at org-complex-heading-regexp)
  5104. (list (length (match-string 1))
  5105. (org-reduced-level (length (match-string 1)))
  5106. (org-match-string-no-properties 2)
  5107. (and (match-end 3) (aref (match-string 3) 2))
  5108. (org-match-string-no-properties 4)
  5109. (org-match-string-no-properties 5)))))
  5110. (defun org-get-entry ()
  5111. "Get the entry text, after heading, entire subtree."
  5112. (save-excursion
  5113. (org-back-to-heading t)
  5114. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5115. (defun org-insert-heading-after-current ()
  5116. "Insert a new heading with same level as current, after current subtree."
  5117. (interactive)
  5118. (org-back-to-heading)
  5119. (org-insert-heading)
  5120. (org-move-subtree-down)
  5121. (end-of-line 1))
  5122. (defun org-insert-heading-respect-content ()
  5123. (interactive)
  5124. (let ((org-insert-heading-respect-content t))
  5125. (org-insert-heading t)))
  5126. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5127. (interactive "P")
  5128. (let ((org-insert-heading-respect-content t))
  5129. (org-insert-todo-heading force-state t)))
  5130. (defun org-insert-todo-heading (arg &optional force-heading)
  5131. "Insert a new heading with the same level and TODO state as current heading.
  5132. If the heading has no TODO state, or if the state is DONE, use the first
  5133. state (TODO by default). Also with prefix arg, force first state."
  5134. (interactive "P")
  5135. (when (or force-heading (not (org-insert-item 'checkbox)))
  5136. (org-insert-heading force-heading)
  5137. (save-excursion
  5138. (org-back-to-heading)
  5139. (outline-previous-heading)
  5140. (looking-at org-todo-line-regexp))
  5141. (let*
  5142. ((new-mark-x
  5143. (if (or arg
  5144. (not (match-beginning 2))
  5145. (member (match-string 2) org-done-keywords))
  5146. (car org-todo-keywords-1)
  5147. (match-string 2)))
  5148. (new-mark
  5149. (or
  5150. (run-hook-with-args-until-success
  5151. 'org-todo-get-default-hook new-mark-x nil)
  5152. new-mark-x)))
  5153. (beginning-of-line 1)
  5154. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5155. (if org-treat-insert-todo-heading-as-state-change
  5156. (org-todo new-mark)
  5157. (insert new-mark " "))))
  5158. (when org-provide-todo-statistics
  5159. (org-update-parent-todo-statistics))))
  5160. (defun org-insert-subheading (arg)
  5161. "Insert a new subheading and demote it.
  5162. Works for outline headings and for plain lists alike."
  5163. (interactive "P")
  5164. (org-insert-heading arg)
  5165. (cond
  5166. ((org-on-heading-p) (org-do-demote))
  5167. ((org-at-item-p) (org-indent-item 1))))
  5168. (defun org-insert-todo-subheading (arg)
  5169. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5170. Works for outline headings and for plain lists alike."
  5171. (interactive "P")
  5172. (org-insert-todo-heading arg)
  5173. (cond
  5174. ((org-on-heading-p) (org-do-demote))
  5175. ((org-at-item-p) (org-indent-item 1))))
  5176. ;;; Promotion and Demotion
  5177. (defun org-promote-subtree ()
  5178. "Promote the entire subtree.
  5179. See also `org-promote'."
  5180. (interactive)
  5181. (save-excursion
  5182. (org-map-tree 'org-promote))
  5183. (org-fix-position-after-promote))
  5184. (defun org-demote-subtree ()
  5185. "Demote the entire subtree. See `org-demote'.
  5186. See also `org-promote'."
  5187. (interactive)
  5188. (save-excursion
  5189. (org-map-tree 'org-demote))
  5190. (org-fix-position-after-promote))
  5191. (defun org-do-promote ()
  5192. "Promote the current heading higher up the tree.
  5193. If the region is active in `transient-mark-mode', promote all headings
  5194. in the region."
  5195. (interactive)
  5196. (save-excursion
  5197. (if (org-region-active-p)
  5198. (org-map-region 'org-promote (region-beginning) (region-end))
  5199. (org-promote)))
  5200. (org-fix-position-after-promote))
  5201. (defun org-do-demote ()
  5202. "Demote the current heading lower down the tree.
  5203. If the region is active in `transient-mark-mode', demote all headings
  5204. in the region."
  5205. (interactive)
  5206. (save-excursion
  5207. (if (org-region-active-p)
  5208. (org-map-region 'org-demote (region-beginning) (region-end))
  5209. (org-demote)))
  5210. (org-fix-position-after-promote))
  5211. (defun org-fix-position-after-promote ()
  5212. "Make sure that after pro/demotion cursor position is right."
  5213. (let ((pos (point)))
  5214. (when (save-excursion
  5215. (beginning-of-line 1)
  5216. (looking-at org-todo-line-regexp)
  5217. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5218. (cond ((eobp) (insert " "))
  5219. ((eolp) (insert " "))
  5220. ((equal (char-after) ?\ ) (forward-char 1))))))
  5221. (defun org-reduced-level (l)
  5222. "Compute the effective level of a heading.
  5223. This takes into account the setting of `org-odd-levels-only'."
  5224. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5225. (defun org-get-valid-level (level &optional change)
  5226. "Rectify a level change under the influence of `org-odd-levels-only'
  5227. LEVEL is a current level, CHANGE is by how much the level should be
  5228. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5229. even level numbers will become the next higher odd number."
  5230. (if org-odd-levels-only
  5231. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5232. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5233. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5234. (max 1 (+ level (or change 0)))))
  5235. (if (boundp 'define-obsolete-function-alias)
  5236. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5237. (define-obsolete-function-alias 'org-get-legal-level
  5238. 'org-get-valid-level)
  5239. (define-obsolete-function-alias 'org-get-legal-level
  5240. 'org-get-valid-level "23.1")))
  5241. (defun org-promote ()
  5242. "Promote the current heading higher up the tree.
  5243. If the region is active in `transient-mark-mode', promote all headings
  5244. in the region."
  5245. (org-back-to-heading t)
  5246. (let* ((level (save-match-data (funcall outline-level)))
  5247. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5248. (diff (abs (- level (length up-head) -1))))
  5249. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5250. (replace-match up-head nil t)
  5251. ;; Fixup tag positioning
  5252. (and org-auto-align-tags (org-set-tags nil t))
  5253. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5254. (defun org-demote ()
  5255. "Demote the current heading lower down the tree.
  5256. If the region is active in `transient-mark-mode', demote all headings
  5257. in the region."
  5258. (org-back-to-heading t)
  5259. (let* ((level (save-match-data (funcall outline-level)))
  5260. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5261. (diff (abs (- level (length down-head) -1))))
  5262. (replace-match down-head nil t)
  5263. ;; Fixup tag positioning
  5264. (and org-auto-align-tags (org-set-tags nil t))
  5265. (if org-adapt-indentation (org-fixup-indentation diff))))
  5266. (defun org-map-tree (fun)
  5267. "Call FUN for every heading underneath the current one."
  5268. (org-back-to-heading)
  5269. (let ((level (funcall outline-level)))
  5270. (save-excursion
  5271. (funcall fun)
  5272. (while (and (progn
  5273. (outline-next-heading)
  5274. (> (funcall outline-level) level))
  5275. (not (eobp)))
  5276. (funcall fun)))))
  5277. (defun org-map-region (fun beg end)
  5278. "Call FUN for every heading between BEG and END."
  5279. (let ((org-ignore-region t))
  5280. (save-excursion
  5281. (setq end (copy-marker end))
  5282. (goto-char beg)
  5283. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5284. (< (point) end))
  5285. (funcall fun))
  5286. (while (and (progn
  5287. (outline-next-heading)
  5288. (< (point) end))
  5289. (not (eobp)))
  5290. (funcall fun)))))
  5291. (defun org-fixup-indentation (diff)
  5292. "Change the indentation in the current entry by DIFF
  5293. However, if any line in the current entry has no indentation, or if it
  5294. would end up with no indentation after the change, nothing at all is done."
  5295. (save-excursion
  5296. (let ((end (save-excursion (outline-next-heading)
  5297. (point-marker)))
  5298. (prohibit (if (> diff 0)
  5299. "^\\S-"
  5300. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5301. col)
  5302. (unless (save-excursion (end-of-line 1)
  5303. (re-search-forward prohibit end t))
  5304. (while (and (< (point) end)
  5305. (re-search-forward "^[ \t]+" end t))
  5306. (goto-char (match-end 0))
  5307. (setq col (current-column))
  5308. (if (< diff 0) (replace-match ""))
  5309. (org-indent-to-column (+ diff col))))
  5310. (move-marker end nil))))
  5311. (defun org-convert-to-odd-levels ()
  5312. "Convert an org-mode file with all levels allowed to one with odd levels.
  5313. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5314. level 5 etc."
  5315. (interactive)
  5316. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5317. (let ((org-odd-levels-only nil) n)
  5318. (save-excursion
  5319. (goto-char (point-min))
  5320. (while (re-search-forward "^\\*\\*+ " nil t)
  5321. (setq n (- (length (match-string 0)) 2))
  5322. (while (>= (setq n (1- n)) 0)
  5323. (org-demote))
  5324. (end-of-line 1))))))
  5325. (defun org-convert-to-oddeven-levels ()
  5326. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5327. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5328. section with an even level, conversion would destroy the structure of the file. An error
  5329. is signaled in this case."
  5330. (interactive)
  5331. (goto-char (point-min))
  5332. ;; First check if there are no even levels
  5333. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5334. (org-show-context t)
  5335. (error "Not all levels are odd in this file. Conversion not possible."))
  5336. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5337. (let ((org-odd-levels-only nil) n)
  5338. (save-excursion
  5339. (goto-char (point-min))
  5340. (while (re-search-forward "^\\*\\*+ " nil t)
  5341. (setq n (/ (1- (length (match-string 0))) 2))
  5342. (while (>= (setq n (1- n)) 0)
  5343. (org-promote))
  5344. (end-of-line 1))))))
  5345. (defun org-tr-level (n)
  5346. "Make N odd if required."
  5347. (if org-odd-levels-only (1+ (/ n 2)) n))
  5348. ;;; Vertical tree motion, cutting and pasting of subtrees
  5349. (defun org-move-subtree-up (&optional arg)
  5350. "Move the current subtree up past ARG headlines of the same level."
  5351. (interactive "p")
  5352. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5353. (defun org-move-subtree-down (&optional arg)
  5354. "Move the current subtree down past ARG headlines of the same level."
  5355. (interactive "p")
  5356. (setq arg (prefix-numeric-value arg))
  5357. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5358. 'outline-get-last-sibling))
  5359. (ins-point (make-marker))
  5360. (cnt (abs arg))
  5361. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5362. ;; Select the tree
  5363. (org-back-to-heading)
  5364. (setq beg0 (point))
  5365. (save-excursion
  5366. (setq ne-beg (org-back-over-empty-lines))
  5367. (setq beg (point)))
  5368. (save-match-data
  5369. (save-excursion (outline-end-of-heading)
  5370. (setq folded (org-invisible-p)))
  5371. (outline-end-of-subtree))
  5372. (outline-next-heading)
  5373. (setq ne-end (org-back-over-empty-lines))
  5374. (setq end (point))
  5375. (goto-char beg0)
  5376. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5377. ;; include less whitespace
  5378. (save-excursion
  5379. (goto-char beg)
  5380. (forward-line (- ne-beg ne-end))
  5381. (setq beg (point))))
  5382. ;; Find insertion point, with error handling
  5383. (while (> cnt 0)
  5384. (or (and (funcall movfunc) (looking-at outline-regexp))
  5385. (progn (goto-char beg0)
  5386. (error "Cannot move past superior level or buffer limit")))
  5387. (setq cnt (1- cnt)))
  5388. (if (> arg 0)
  5389. ;; Moving forward - still need to move over subtree
  5390. (progn (org-end-of-subtree t t)
  5391. (save-excursion
  5392. (org-back-over-empty-lines)
  5393. (or (bolp) (newline)))))
  5394. (setq ne-ins (org-back-over-empty-lines))
  5395. (move-marker ins-point (point))
  5396. (setq txt (buffer-substring beg end))
  5397. (org-save-markers-in-region beg end)
  5398. (delete-region beg end)
  5399. (org-remove-empty-overlays-at beg)
  5400. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5401. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5402. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5403. (let ((bbb (point)))
  5404. (insert-before-markers txt)
  5405. (org-reinstall-markers-in-region bbb)
  5406. (move-marker ins-point bbb))
  5407. (or (bolp) (insert "\n"))
  5408. (setq ins-end (point))
  5409. (goto-char ins-point)
  5410. (org-skip-whitespace)
  5411. (when (and (< arg 0)
  5412. (org-first-sibling-p)
  5413. (> ne-ins ne-beg))
  5414. ;; Move whitespace back to beginning
  5415. (save-excursion
  5416. (goto-char ins-end)
  5417. (let ((kill-whole-line t))
  5418. (kill-line (- ne-ins ne-beg)) (point)))
  5419. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5420. (move-marker ins-point nil)
  5421. (if folded
  5422. (hide-entry)
  5423. (org-show-entry)
  5424. (show-children)
  5425. (org-cycle-hide-drawers 'children))
  5426. (org-clean-visibility-after-subtree-move)))
  5427. (defvar org-subtree-clip ""
  5428. "Clipboard for cut and paste of subtrees.
  5429. This is actually only a copy of the kill, because we use the normal kill
  5430. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5431. (defvar org-subtree-clip-folded nil
  5432. "Was the last copied subtree folded?
  5433. This is used to fold the tree back after pasting.")
  5434. (defun org-cut-subtree (&optional n)
  5435. "Cut the current subtree into the clipboard.
  5436. With prefix arg N, cut this many sequential subtrees.
  5437. This is a short-hand for marking the subtree and then cutting it."
  5438. (interactive "p")
  5439. (org-copy-subtree n 'cut))
  5440. (defun org-copy-subtree (&optional n cut force-store-markers)
  5441. "Cut the current subtree into the clipboard.
  5442. With prefix arg N, cut this many sequential subtrees.
  5443. This is a short-hand for marking the subtree and then copying it.
  5444. If CUT is non-nil, actually cut the subtree.
  5445. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5446. of some markers in the region, even if CUT is non-nil. This is
  5447. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5448. (interactive "p")
  5449. (let (beg end folded (beg0 (point)))
  5450. (if (interactive-p)
  5451. (org-back-to-heading nil) ; take what looks like a subtree
  5452. (org-back-to-heading t)) ; take what is really there
  5453. (org-back-over-empty-lines)
  5454. (setq beg (point))
  5455. (skip-chars-forward " \t\r\n")
  5456. (save-match-data
  5457. (save-excursion (outline-end-of-heading)
  5458. (setq folded (org-invisible-p)))
  5459. (condition-case nil
  5460. (outline-forward-same-level (1- n))
  5461. (error nil))
  5462. (org-end-of-subtree t t))
  5463. (org-back-over-empty-lines)
  5464. (setq end (point))
  5465. (goto-char beg0)
  5466. (when (> end beg)
  5467. (setq org-subtree-clip-folded folded)
  5468. (when (or cut force-store-markers)
  5469. (org-save-markers-in-region beg end))
  5470. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5471. (setq org-subtree-clip (current-kill 0))
  5472. (message "%s: Subtree(s) with %d characters"
  5473. (if cut "Cut" "Copied")
  5474. (length org-subtree-clip)))))
  5475. (defun org-paste-subtree (&optional level tree for-yank)
  5476. "Paste the clipboard as a subtree, with modification of headline level.
  5477. The entire subtree is promoted or demoted in order to match a new headline
  5478. level.
  5479. If the cursor is at the beginning of a headline, the same level as
  5480. that headline is used to paste the tree
  5481. If not, the new level is derived from the *visible* headings
  5482. before and after the insertion point, and taken to be the inferior headline
  5483. level of the two. So if the previous visible heading is level 3 and the
  5484. next is level 4 (or vice versa), level 4 will be used for insertion.
  5485. This makes sure that the subtree remains an independent subtree and does
  5486. not swallow low level entries.
  5487. You can also force a different level, either by using a numeric prefix
  5488. argument, or by inserting the heading marker by hand. For example, if the
  5489. cursor is after \"*****\", then the tree will be shifted to level 5.
  5490. If optional TREE is given, use this text instead of the kill ring.
  5491. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5492. move back over whitespace before inserting, and move point to the end of
  5493. the inserted text when done."
  5494. (interactive "P")
  5495. (unless (org-kill-is-subtree-p tree)
  5496. (error "%s"
  5497. (substitute-command-keys
  5498. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5499. (let* ((visp (not (org-invisible-p)))
  5500. (txt (or tree (and kill-ring (current-kill 0))))
  5501. (^re (concat "^\\(" outline-regexp "\\)"))
  5502. (re (concat "\\(" outline-regexp "\\)"))
  5503. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5504. (old-level (if (string-match ^re txt)
  5505. (- (match-end 0) (match-beginning 0) 1)
  5506. -1))
  5507. (force-level (cond (level (prefix-numeric-value level))
  5508. ((and (looking-at "[ \t]*$")
  5509. (string-match
  5510. ^re_ (buffer-substring
  5511. (point-at-bol) (point))))
  5512. (- (match-end 1) (match-beginning 1)))
  5513. ((and (bolp)
  5514. (looking-at org-outline-regexp))
  5515. (- (match-end 0) (point) 1))
  5516. (t nil)))
  5517. (previous-level (save-excursion
  5518. (condition-case nil
  5519. (progn
  5520. (outline-previous-visible-heading 1)
  5521. (if (looking-at re)
  5522. (- (match-end 0) (match-beginning 0) 1)
  5523. 1))
  5524. (error 1))))
  5525. (next-level (save-excursion
  5526. (condition-case nil
  5527. (progn
  5528. (or (looking-at outline-regexp)
  5529. (outline-next-visible-heading 1))
  5530. (if (looking-at re)
  5531. (- (match-end 0) (match-beginning 0) 1)
  5532. 1))
  5533. (error 1))))
  5534. (new-level (or force-level (max previous-level next-level)))
  5535. (shift (if (or (= old-level -1)
  5536. (= new-level -1)
  5537. (= old-level new-level))
  5538. 0
  5539. (- new-level old-level)))
  5540. (delta (if (> shift 0) -1 1))
  5541. (func (if (> shift 0) 'org-demote 'org-promote))
  5542. (org-odd-levels-only nil)
  5543. beg end newend)
  5544. ;; Remove the forced level indicator
  5545. (if force-level
  5546. (delete-region (point-at-bol) (point)))
  5547. ;; Paste
  5548. (beginning-of-line 1)
  5549. (unless for-yank (org-back-over-empty-lines))
  5550. (setq beg (point))
  5551. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5552. (insert-before-markers txt)
  5553. (unless (string-match "\n\\'" txt) (insert "\n"))
  5554. (setq newend (point))
  5555. (org-reinstall-markers-in-region beg)
  5556. (setq end (point))
  5557. (goto-char beg)
  5558. (skip-chars-forward " \t\n\r")
  5559. (setq beg (point))
  5560. (if (and (org-invisible-p) visp)
  5561. (save-excursion (outline-show-heading)))
  5562. ;; Shift if necessary
  5563. (unless (= shift 0)
  5564. (save-restriction
  5565. (narrow-to-region beg end)
  5566. (while (not (= shift 0))
  5567. (org-map-region func (point-min) (point-max))
  5568. (setq shift (+ delta shift)))
  5569. (goto-char (point-min))
  5570. (setq newend (point-max))))
  5571. (when (or (interactive-p) for-yank)
  5572. (message "Clipboard pasted as level %d subtree" new-level))
  5573. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5574. kill-ring
  5575. (eq org-subtree-clip (current-kill 0))
  5576. org-subtree-clip-folded)
  5577. ;; The tree was folded before it was killed/copied
  5578. (hide-subtree))
  5579. (and for-yank (goto-char newend))))
  5580. (defun org-kill-is-subtree-p (&optional txt)
  5581. "Check if the current kill is an outline subtree, or a set of trees.
  5582. Returns nil if kill does not start with a headline, or if the first
  5583. headline level is not the largest headline level in the tree.
  5584. So this will actually accept several entries of equal levels as well,
  5585. which is OK for `org-paste-subtree'.
  5586. If optional TXT is given, check this string instead of the current kill."
  5587. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5588. (start-level (and kill
  5589. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5590. org-outline-regexp "\\)")
  5591. kill)
  5592. (- (match-end 2) (match-beginning 2) 1)))
  5593. (re (concat "^" org-outline-regexp))
  5594. (start (1+ (or (match-beginning 2) -1))))
  5595. (if (not start-level)
  5596. (progn
  5597. nil) ;; does not even start with a heading
  5598. (catch 'exit
  5599. (while (setq start (string-match re kill (1+ start)))
  5600. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5601. (throw 'exit nil)))
  5602. t))))
  5603. (defvar org-markers-to-move nil
  5604. "Markers that should be moved with a cut-and-paste operation.
  5605. Those markers are stored together with their positions relative to
  5606. the start of the region.")
  5607. (defun org-save-markers-in-region (beg end)
  5608. "Check markers in region.
  5609. If these markers are between BEG and END, record their position relative
  5610. to BEG, so that after moving the block of text, we can put the markers back
  5611. into place.
  5612. This function gets called just before an entry or tree gets cut from the
  5613. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5614. called immediately, to move the markers with the entries."
  5615. (setq org-markers-to-move nil)
  5616. (when (featurep 'org-clock)
  5617. (org-clock-save-markers-for-cut-and-paste beg end))
  5618. (when (featurep 'org-agenda)
  5619. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5620. (defun org-check-and-save-marker (marker beg end)
  5621. "Check if MARKER is between BEG and END.
  5622. If yes, remember the marker and the distance to BEG."
  5623. (when (and (marker-buffer marker)
  5624. (equal (marker-buffer marker) (current-buffer)))
  5625. (if (and (>= marker beg) (< marker end))
  5626. (push (cons marker (- marker beg)) org-markers-to-move))))
  5627. (defun org-reinstall-markers-in-region (beg)
  5628. "Move all remembered markers to their position relative to BEG."
  5629. (mapc (lambda (x)
  5630. (move-marker (car x) (+ beg (cdr x))))
  5631. org-markers-to-move)
  5632. (setq org-markers-to-move nil))
  5633. (defun org-narrow-to-subtree ()
  5634. "Narrow buffer to the current subtree."
  5635. (interactive)
  5636. (save-excursion
  5637. (save-match-data
  5638. (narrow-to-region
  5639. (progn (org-back-to-heading t) (point))
  5640. (progn (org-end-of-subtree t) (point))))))
  5641. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5642. "Clone the task (subtree) at point N times.
  5643. The clones will be inserted as siblings.
  5644. In interactive use, the user will be prompted for the number of clones
  5645. to be produced, and for a time SHIFT, which may be a repeater as used
  5646. in time stamps, for example `+3d'.
  5647. When a valid repeater is given and the entry contains any time stamps,
  5648. the clones will become a sequence in time, with time stamps in the
  5649. subtree shifted for each clone produced. If SHIFT is nil or the
  5650. empty string, time stamps will be left alone.
  5651. If the original subtree did contain time stamps with a repeater,
  5652. the following will happen:
  5653. - the repeater will be removed in each clone
  5654. - an additional clone will be produced, with the current, unshifted
  5655. date(s) in the entry.
  5656. - the original entry will be placed *after* all the clones, with
  5657. repeater intact.
  5658. - the start days in the repeater in the original entry will be shifted
  5659. to past the last clone.
  5660. I this way you can spell out a number of instances of a repeating task,
  5661. and still retain the repeater to cover future instances of the task."
  5662. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5663. (let (beg end template task
  5664. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5665. (if (not (and (integerp n) (> n 0)))
  5666. (error "Invalid number of replications %s" n))
  5667. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5668. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5669. shift)))
  5670. (error "Invalid shift specification %s" shift))
  5671. (when doshift
  5672. (setq shift-n (string-to-number (match-string 1 shift))
  5673. shift-what (cdr (assoc (match-string 2 shift)
  5674. '(("d" . day) ("w" . week)
  5675. ("m" . month) ("y" . year))))))
  5676. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5677. (setq nmin 1 nmax n)
  5678. (org-back-to-heading t)
  5679. (setq beg (point))
  5680. (org-end-of-subtree t t)
  5681. (setq end (point))
  5682. (setq template (buffer-substring beg end))
  5683. (when (and doshift
  5684. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5685. (delete-region beg end)
  5686. (setq end beg)
  5687. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5688. (goto-char end)
  5689. (loop for n from nmin to nmax do
  5690. (if (not doshift)
  5691. (setq task template)
  5692. (with-temp-buffer
  5693. (insert template)
  5694. (org-mode)
  5695. (goto-char (point-min))
  5696. (while (re-search-forward org-ts-regexp-both nil t)
  5697. (org-timestamp-change (* n shift-n) shift-what))
  5698. (unless (= n n-no-remove)
  5699. (goto-char (point-min))
  5700. (while (re-search-forward org-ts-regexp nil t)
  5701. (save-excursion
  5702. (goto-char (match-beginning 0))
  5703. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5704. (delete-region (match-beginning 1) (match-end 1))))))
  5705. (setq task (buffer-string))))
  5706. (insert task))
  5707. (goto-char beg)))
  5708. ;;; Outline Sorting
  5709. (defun org-sort (with-case)
  5710. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5711. Optional argument WITH-CASE means sort case-sensitively.
  5712. With a double prefix argument, also remove duplicate entries."
  5713. (interactive "P")
  5714. (if (org-at-table-p)
  5715. (org-call-with-arg 'org-table-sort-lines with-case)
  5716. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5717. (defun org-sort-remove-invisible (s)
  5718. (remove-text-properties 0 (length s) org-rm-props s)
  5719. (while (string-match org-bracket-link-regexp s)
  5720. (setq s (replace-match (if (match-end 2)
  5721. (match-string 3 s)
  5722. (match-string 1 s)) t t s)))
  5723. s)
  5724. (defvar org-priority-regexp) ; defined later in the file
  5725. (defvar org-after-sorting-entries-or-items-hook nil
  5726. "Hook that is run after a bunch of entries or items have been sorted.
  5727. When children are sorted, the cursor is in the parent line when this
  5728. hook gets called. When a region or a plain list is sorted, the cursor
  5729. will be in the first entry of the sorted region/list.")
  5730. (defun org-sort-entries-or-items
  5731. (&optional with-case sorting-type getkey-func compare-func property)
  5732. "Sort entries on a certain level of an outline tree, or plain list items.
  5733. If there is an active region, the entries in the region are sorted.
  5734. Else, if the cursor is before the first entry, sort the top-level items.
  5735. Else, the children of the entry at point are sorted.
  5736. If the cursor is at the first item in a plain list, the list items will be
  5737. sorted.
  5738. Sorting can be alphabetically, numerically, by date/time as given by
  5739. a time stamp, by a property or by priority.
  5740. The command prompts for the sorting type unless it has been given to the
  5741. function through the SORTING-TYPE argument, which needs to a character,
  5742. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5743. precise meaning of each character:
  5744. n Numerically, by converting the beginning of the entry/item to a number.
  5745. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5746. t By date/time, either the first active time stamp in the entry, or, if
  5747. none exist, by the first inactive one.
  5748. In items, only the first line will be chekced.
  5749. s By the scheduled date/time.
  5750. d By deadline date/time.
  5751. c By creation time, which is assumed to be the first inactive time stamp
  5752. at the beginning of a line.
  5753. p By priority according to the cookie.
  5754. r By the value of a property.
  5755. Capital letters will reverse the sort order.
  5756. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5757. called with point at the beginning of the record. It must return either
  5758. a string or a number that should serve as the sorting key for that record.
  5759. Comparing entries ignores case by default. However, with an optional argument
  5760. WITH-CASE, the sorting considers case as well."
  5761. (interactive "P")
  5762. (let ((case-func (if with-case 'identity 'downcase))
  5763. start beg end stars re re2
  5764. txt what tmp plain-list-p)
  5765. ;; Find beginning and end of region to sort
  5766. (cond
  5767. ((org-region-active-p)
  5768. ;; we will sort the region
  5769. (setq end (region-end)
  5770. what "region")
  5771. (goto-char (region-beginning))
  5772. (if (not (org-on-heading-p)) (outline-next-heading))
  5773. (setq start (point)))
  5774. ((org-at-item-p)
  5775. ;; we will sort this plain list
  5776. (org-beginning-of-item-list) (setq start (point))
  5777. (org-end-of-item-list) (setq end (point))
  5778. (goto-char start)
  5779. (setq plain-list-p t
  5780. what "plain list"))
  5781. ((or (org-on-heading-p)
  5782. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5783. ;; we will sort the children of the current headline
  5784. (org-back-to-heading)
  5785. (setq start (point)
  5786. end (progn (org-end-of-subtree t t)
  5787. (org-back-over-empty-lines)
  5788. (point))
  5789. what "children")
  5790. (goto-char start)
  5791. (show-subtree)
  5792. (outline-next-heading))
  5793. (t
  5794. ;; we will sort the top-level entries in this file
  5795. (goto-char (point-min))
  5796. (or (org-on-heading-p) (outline-next-heading))
  5797. (setq start (point) end (point-max) what "top-level")
  5798. (goto-char start)
  5799. (show-all)))
  5800. (setq beg (point))
  5801. (if (>= beg end) (error "Nothing to sort"))
  5802. (unless plain-list-p
  5803. (looking-at "\\(\\*+\\)")
  5804. (setq stars (match-string 1)
  5805. re (concat "^" (regexp-quote stars) " +")
  5806. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5807. txt (buffer-substring beg end))
  5808. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5809. (if (and (not (equal stars "*")) (string-match re2 txt))
  5810. (error "Region to sort contains a level above the first entry")))
  5811. (unless sorting-type
  5812. (message
  5813. (if plain-list-p
  5814. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5815. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5816. [t]ime [s]cheduled [d]eadline [c]reated
  5817. A/N/T/S/D/C/P/O/F means reversed:")
  5818. what)
  5819. (setq sorting-type (read-char-exclusive))
  5820. (and (= (downcase sorting-type) ?f)
  5821. (setq getkey-func
  5822. (org-ido-completing-read "Sort using function: "
  5823. obarray 'fboundp t nil nil))
  5824. (setq getkey-func (intern getkey-func)))
  5825. (and (= (downcase sorting-type) ?r)
  5826. (setq property
  5827. (org-ido-completing-read "Property: "
  5828. (mapcar 'list (org-buffer-property-keys t))
  5829. nil t))))
  5830. (message "Sorting entries...")
  5831. (save-restriction
  5832. (narrow-to-region start end)
  5833. (let ((dcst (downcase sorting-type))
  5834. (case-fold-search nil)
  5835. (now (current-time)))
  5836. (sort-subr
  5837. (/= dcst sorting-type)
  5838. ;; This function moves to the beginning character of the "record" to
  5839. ;; be sorted.
  5840. (if plain-list-p
  5841. (lambda nil
  5842. (if (org-at-item-p) t (goto-char (point-max))))
  5843. (lambda nil
  5844. (if (re-search-forward re nil t)
  5845. (goto-char (match-beginning 0))
  5846. (goto-char (point-max)))))
  5847. ;; This function moves to the last character of the "record" being
  5848. ;; sorted.
  5849. (if plain-list-p
  5850. 'org-end-of-item
  5851. (lambda nil
  5852. (save-match-data
  5853. (condition-case nil
  5854. (outline-forward-same-level 1)
  5855. (error
  5856. (goto-char (point-max)))))))
  5857. ;; This function returns the value that gets sorted against.
  5858. (if plain-list-p
  5859. (lambda nil
  5860. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5861. (cond
  5862. ((= dcst ?n)
  5863. (string-to-number (buffer-substring (match-end 0)
  5864. (point-at-eol))))
  5865. ((= dcst ?a)
  5866. (buffer-substring (match-end 0) (point-at-eol)))
  5867. ((= dcst ?t)
  5868. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5869. (re-search-forward org-ts-regexp-both
  5870. (point-at-eol) t))
  5871. (org-time-string-to-seconds (match-string 0))
  5872. (time-to-seconds now)))
  5873. ((= dcst ?f)
  5874. (if getkey-func
  5875. (progn
  5876. (setq tmp (funcall getkey-func))
  5877. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5878. tmp)
  5879. (error "Invalid key function `%s'" getkey-func)))
  5880. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5881. (lambda nil
  5882. (cond
  5883. ((= dcst ?n)
  5884. (if (looking-at org-complex-heading-regexp)
  5885. (string-to-number (match-string 4))
  5886. nil))
  5887. ((= dcst ?a)
  5888. (if (looking-at org-complex-heading-regexp)
  5889. (funcall case-func (match-string 4))
  5890. nil))
  5891. ((= dcst ?t)
  5892. (let ((end (save-excursion (outline-next-heading) (point))))
  5893. (if (or (re-search-forward org-ts-regexp end t)
  5894. (re-search-forward org-ts-regexp-both end t))
  5895. (org-time-string-to-seconds (match-string 0))
  5896. (time-to-seconds now))))
  5897. ((= dcst ?c)
  5898. (let ((end (save-excursion (outline-next-heading) (point))))
  5899. (if (re-search-forward
  5900. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  5901. end t)
  5902. (org-time-string-to-seconds (match-string 0))
  5903. (time-to-seconds now))))
  5904. ((= dcst ?s)
  5905. (let ((end (save-excursion (outline-next-heading) (point))))
  5906. (if (re-search-forward org-scheduled-time-regexp end t)
  5907. (org-time-string-to-seconds (match-string 1))
  5908. (time-to-seconds now))))
  5909. ((= dcst ?d)
  5910. (let ((end (save-excursion (outline-next-heading) (point))))
  5911. (if (re-search-forward org-deadline-time-regexp end t)
  5912. (org-time-string-to-seconds (match-string 1))
  5913. (time-to-seconds now))))
  5914. ((= dcst ?p)
  5915. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5916. (string-to-char (match-string 2))
  5917. org-default-priority))
  5918. ((= dcst ?r)
  5919. (or (org-entry-get nil property) ""))
  5920. ((= dcst ?o)
  5921. (if (looking-at org-complex-heading-regexp)
  5922. (- 9999 (length (member (match-string 2)
  5923. org-todo-keywords-1)))))
  5924. ((= dcst ?f)
  5925. (if getkey-func
  5926. (progn
  5927. (setq tmp (funcall getkey-func))
  5928. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5929. tmp)
  5930. (error "Invalid key function `%s'" getkey-func)))
  5931. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5932. nil
  5933. (cond
  5934. ((= dcst ?a) 'string<)
  5935. ((= dcst ?f) compare-func)
  5936. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  5937. (t nil)))))
  5938. (run-hooks 'org-after-sorting-entries-or-items-hook)
  5939. (message "Sorting entries...done")))
  5940. (defun org-do-sort (table what &optional with-case sorting-type)
  5941. "Sort TABLE of WHAT according to SORTING-TYPE.
  5942. The user will be prompted for the SORTING-TYPE if the call to this
  5943. function does not specify it. WHAT is only for the prompt, to indicate
  5944. what is being sorted. The sorting key will be extracted from
  5945. the car of the elements of the table.
  5946. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5947. (unless sorting-type
  5948. (message
  5949. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5950. what)
  5951. (setq sorting-type (read-char-exclusive)))
  5952. (let ((dcst (downcase sorting-type))
  5953. extractfun comparefun)
  5954. ;; Define the appropriate functions
  5955. (cond
  5956. ((= dcst ?n)
  5957. (setq extractfun 'string-to-number
  5958. comparefun (if (= dcst sorting-type) '< '>)))
  5959. ((= dcst ?a)
  5960. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5961. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5962. comparefun (if (= dcst sorting-type)
  5963. 'string<
  5964. (lambda (a b) (and (not (string< a b))
  5965. (not (string= a b)))))))
  5966. ((= dcst ?t)
  5967. (setq extractfun
  5968. (lambda (x)
  5969. (if (or (string-match org-ts-regexp x)
  5970. (string-match org-ts-regexp-both x))
  5971. (time-to-seconds
  5972. (org-time-string-to-time (match-string 0 x)))
  5973. 0))
  5974. comparefun (if (= dcst sorting-type) '< '>)))
  5975. (t (error "Invalid sorting type `%c'" sorting-type)))
  5976. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5977. table)
  5978. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5979. ;;; The orgstruct minor mode
  5980. ;; Define a minor mode which can be used in other modes in order to
  5981. ;; integrate the org-mode structure editing commands.
  5982. ;; This is really a hack, because the org-mode structure commands use
  5983. ;; keys which normally belong to the major mode. Here is how it
  5984. ;; works: The minor mode defines all the keys necessary to operate the
  5985. ;; structure commands, but wraps the commands into a function which
  5986. ;; tests if the cursor is currently at a headline or a plain list
  5987. ;; item. If that is the case, the structure command is used,
  5988. ;; temporarily setting many Org-mode variables like regular
  5989. ;; expressions for filling etc. However, when any of those keys is
  5990. ;; used at a different location, function uses `key-binding' to look
  5991. ;; up if the key has an associated command in another currently active
  5992. ;; keymap (minor modes, major mode, global), and executes that
  5993. ;; command. There might be problems if any of the keys is otherwise
  5994. ;; used as a prefix key.
  5995. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5996. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5997. ;; addresses this by checking explicitly for both bindings.
  5998. (defvar orgstruct-mode-map (make-sparse-keymap)
  5999. "Keymap for the minor `orgstruct-mode'.")
  6000. (defvar org-local-vars nil
  6001. "List of local variables, for use by `orgstruct-mode'")
  6002. ;;;###autoload
  6003. (define-minor-mode orgstruct-mode
  6004. "Toggle the minor more `orgstruct-mode'.
  6005. This mode is for using Org-mode structure commands in other modes.
  6006. The following key behave as if Org-mode was active, if the cursor
  6007. is on a headline, or on a plain list item (both in the definition
  6008. of Org-mode).
  6009. M-up Move entry/item up
  6010. M-down Move entry/item down
  6011. M-left Promote
  6012. M-right Demote
  6013. M-S-up Move entry/item up
  6014. M-S-down Move entry/item down
  6015. M-S-left Promote subtree
  6016. M-S-right Demote subtree
  6017. M-q Fill paragraph and items like in Org-mode
  6018. C-c ^ Sort entries
  6019. C-c - Cycle list bullet
  6020. TAB Cycle item visibility
  6021. M-RET Insert new heading/item
  6022. S-M-RET Insert new TODO heading / Checkbox item
  6023. C-c C-c Set tags / toggle checkbox"
  6024. nil " OrgStruct" nil
  6025. (org-load-modules-maybe)
  6026. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6027. ;;;###autoload
  6028. (defun turn-on-orgstruct ()
  6029. "Unconditionally turn on `orgstruct-mode'."
  6030. (orgstruct-mode 1))
  6031. (defun orgstruct++-mode (&optional arg)
  6032. "Toggle `orgstruct-mode', the enhanced version of it.
  6033. In addition to setting orgstruct-mode, this also exports all indentation
  6034. and autofilling variables from org-mode into the buffer. It will also
  6035. recognize item context in multiline items.
  6036. Note that turning off orgstruct-mode will *not* remove the
  6037. indentation/paragraph settings. This can only be done by refreshing the
  6038. major mode, for example with \\[normal-mode]."
  6039. (interactive "P")
  6040. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6041. (if (< arg 1)
  6042. (orgstruct-mode -1)
  6043. (orgstruct-mode 1)
  6044. (let (var val)
  6045. (mapc
  6046. (lambda (x)
  6047. (when (string-match
  6048. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6049. (symbol-name (car x)))
  6050. (setq var (car x) val (nth 1 x))
  6051. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6052. org-local-vars)
  6053. (org-set-local 'orgstruct-is-++ t))))
  6054. (defvar orgstruct-is-++ nil
  6055. "Is orgstruct-mode in ++ version in the current-buffer?")
  6056. (make-variable-buffer-local 'orgstruct-is-++)
  6057. ;;;###autoload
  6058. (defun turn-on-orgstruct++ ()
  6059. "Unconditionally turn on `orgstruct++-mode'."
  6060. (orgstruct++-mode 1))
  6061. (defun orgstruct-error ()
  6062. "Error when there is no default binding for a structure key."
  6063. (interactive)
  6064. (error "This key has no function outside structure elements"))
  6065. (defun orgstruct-setup ()
  6066. "Setup orgstruct keymaps."
  6067. (let ((nfunc 0)
  6068. (bindings
  6069. (list
  6070. '([(meta up)] org-metaup)
  6071. '([(meta down)] org-metadown)
  6072. '([(meta left)] org-metaleft)
  6073. '([(meta right)] org-metaright)
  6074. '([(meta shift up)] org-shiftmetaup)
  6075. '([(meta shift down)] org-shiftmetadown)
  6076. '([(meta shift left)] org-shiftmetaleft)
  6077. '([(meta shift right)] org-shiftmetaright)
  6078. '([?\e (up)] org-metaup)
  6079. '([?\e (down)] org-metadown)
  6080. '([?\e (left)] org-metaleft)
  6081. '([?\e (right)] org-metaright)
  6082. '([?\e (shift up)] org-shiftmetaup)
  6083. '([?\e (shift down)] org-shiftmetadown)
  6084. '([?\e (shift left)] org-shiftmetaleft)
  6085. '([?\e (shift right)] org-shiftmetaright)
  6086. '([(shift up)] org-shiftup)
  6087. '([(shift down)] org-shiftdown)
  6088. '([(shift left)] org-shiftleft)
  6089. '([(shift right)] org-shiftright)
  6090. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6091. '("\M-q" fill-paragraph)
  6092. '("\C-c^" org-sort)
  6093. '("\C-c-" org-cycle-list-bullet)))
  6094. elt key fun cmd)
  6095. (while (setq elt (pop bindings))
  6096. (setq nfunc (1+ nfunc))
  6097. (setq key (org-key (car elt))
  6098. fun (nth 1 elt)
  6099. cmd (orgstruct-make-binding fun nfunc key))
  6100. (org-defkey orgstruct-mode-map key cmd))
  6101. ;; Special treatment needed for TAB and RET
  6102. (org-defkey orgstruct-mode-map [(tab)]
  6103. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6104. (org-defkey orgstruct-mode-map "\C-i"
  6105. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6106. (org-defkey orgstruct-mode-map "\M-\C-m"
  6107. (orgstruct-make-binding 'org-insert-heading 105
  6108. "\M-\C-m" [(meta return)]))
  6109. (org-defkey orgstruct-mode-map [(meta return)]
  6110. (orgstruct-make-binding 'org-insert-heading 106
  6111. [(meta return)] "\M-\C-m"))
  6112. (org-defkey orgstruct-mode-map [(shift meta return)]
  6113. (orgstruct-make-binding 'org-insert-todo-heading 107
  6114. [(meta return)] "\M-\C-m"))
  6115. (org-defkey orgstruct-mode-map "\e\C-m"
  6116. (orgstruct-make-binding 'org-insert-heading 108
  6117. "\e\C-m" [?\e (return)]))
  6118. (org-defkey orgstruct-mode-map [?\e (return)]
  6119. (orgstruct-make-binding 'org-insert-heading 109
  6120. [?\e (return)] "\e\C-m"))
  6121. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6122. (orgstruct-make-binding 'org-insert-todo-heading 110
  6123. [?\e (return)] "\e\C-m"))
  6124. (unless org-local-vars
  6125. (setq org-local-vars (org-get-local-variables)))
  6126. t))
  6127. (defun orgstruct-make-binding (fun n &rest keys)
  6128. "Create a function for binding in the structure minor mode.
  6129. FUN is the command to call inside a table. N is used to create a unique
  6130. command name. KEYS are keys that should be checked in for a command
  6131. to execute outside of tables."
  6132. (eval
  6133. (list 'defun
  6134. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6135. '(arg)
  6136. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6137. "Outside of structure, run the binding of `"
  6138. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6139. "'.")
  6140. '(interactive "p")
  6141. (list 'if
  6142. `(org-context-p 'headline 'item
  6143. (and orgstruct-is-++
  6144. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6145. 'item-body))
  6146. (list 'org-run-like-in-org-mode (list 'quote fun))
  6147. (list 'let '(orgstruct-mode)
  6148. (list 'call-interactively
  6149. (append '(or)
  6150. (mapcar (lambda (k)
  6151. (list 'key-binding k))
  6152. keys)
  6153. '('orgstruct-error))))))))
  6154. (defun org-context-p (&rest contexts)
  6155. "Check if local context is any of CONTEXTS.
  6156. Possible values in the list of contexts are `table', `headline', and `item'."
  6157. (let ((pos (point)))
  6158. (goto-char (point-at-bol))
  6159. (prog1 (or (and (memq 'table contexts)
  6160. (looking-at "[ \t]*|"))
  6161. (and (memq 'headline contexts)
  6162. ;;????????? (looking-at "\\*+"))
  6163. (looking-at outline-regexp))
  6164. (and (memq 'item contexts)
  6165. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6166. (and (memq 'item-body contexts)
  6167. (org-in-item-p)))
  6168. (goto-char pos))))
  6169. (defun org-get-local-variables ()
  6170. "Return a list of all local variables in an org-mode buffer."
  6171. (let (varlist)
  6172. (with-current-buffer (get-buffer-create "*Org tmp*")
  6173. (erase-buffer)
  6174. (org-mode)
  6175. (setq varlist (buffer-local-variables)))
  6176. (kill-buffer "*Org tmp*")
  6177. (delq nil
  6178. (mapcar
  6179. (lambda (x)
  6180. (setq x
  6181. (if (symbolp x)
  6182. (list x)
  6183. (list (car x) (list 'quote (cdr x)))))
  6184. (if (string-match
  6185. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6186. (symbol-name (car x)))
  6187. x nil))
  6188. varlist))))
  6189. ;;;###autoload
  6190. (defun org-run-like-in-org-mode (cmd)
  6191. "Run a command, pretending that the current buffer is in Org-mode.
  6192. This will temporarily bind local variables that are typically bound in
  6193. Org-mode to the values they have in Org-mode, and then interactively
  6194. call CMD."
  6195. (org-load-modules-maybe)
  6196. (unless org-local-vars
  6197. (setq org-local-vars (org-get-local-variables)))
  6198. (eval (list 'let org-local-vars
  6199. (list 'call-interactively (list 'quote cmd)))))
  6200. ;;;; Archiving
  6201. (defun org-get-category (&optional pos)
  6202. "Get the category applying to position POS."
  6203. (get-text-property (or pos (point)) 'org-category))
  6204. (defun org-refresh-category-properties ()
  6205. "Refresh category text properties in the buffer."
  6206. (let ((def-cat (cond
  6207. ((null org-category)
  6208. (if buffer-file-name
  6209. (file-name-sans-extension
  6210. (file-name-nondirectory buffer-file-name))
  6211. "???"))
  6212. ((symbolp org-category) (symbol-name org-category))
  6213. (t org-category)))
  6214. beg end cat pos optionp)
  6215. (org-unmodified
  6216. (save-excursion
  6217. (save-restriction
  6218. (widen)
  6219. (goto-char (point-min))
  6220. (put-text-property (point) (point-max) 'org-category def-cat)
  6221. (while (re-search-forward
  6222. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6223. (setq pos (match-end 0)
  6224. optionp (equal (char-after (match-beginning 0)) ?#)
  6225. cat (org-trim (match-string 2)))
  6226. (if optionp
  6227. (setq beg (point-at-bol) end (point-max))
  6228. (org-back-to-heading t)
  6229. (setq beg (point) end (org-end-of-subtree t t)))
  6230. (put-text-property beg end 'org-category cat)
  6231. (goto-char pos)))))))
  6232. ;;;; Link Stuff
  6233. ;;; Link abbreviations
  6234. (defun org-link-expand-abbrev (link)
  6235. "Apply replacements as defined in `org-link-abbrev-alist."
  6236. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6237. (let* ((key (match-string 1 link))
  6238. (as (or (assoc key org-link-abbrev-alist-local)
  6239. (assoc key org-link-abbrev-alist)))
  6240. (tag (and (match-end 2) (match-string 3 link)))
  6241. rpl)
  6242. (if (not as)
  6243. link
  6244. (setq rpl (cdr as))
  6245. (cond
  6246. ((symbolp rpl) (funcall rpl tag))
  6247. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6248. ((string-match "%h" rpl)
  6249. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6250. (t (concat rpl tag)))))
  6251. link))
  6252. ;;; Storing and inserting links
  6253. (defvar org-insert-link-history nil
  6254. "Minibuffer history for links inserted with `org-insert-link'.")
  6255. (defvar org-stored-links nil
  6256. "Contains the links stored with `org-store-link'.")
  6257. (defvar org-store-link-plist nil
  6258. "Plist with info about the most recently link created with `org-store-link'.")
  6259. (defvar org-link-protocols nil
  6260. "Link protocols added to Org-mode using `org-add-link-type'.")
  6261. (defvar org-store-link-functions nil
  6262. "List of functions that are called to create and store a link.
  6263. Each function will be called in turn until one returns a non-nil
  6264. value. Each function should check if it is responsible for creating
  6265. this link (for example by looking at the major mode).
  6266. If not, it must exit and return nil.
  6267. If yes, it should return a non-nil value after a calling
  6268. `org-store-link-props' with a list of properties and values.
  6269. Special properties are:
  6270. :type The link prefix. like \"http\". This must be given.
  6271. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6272. This is obligatory as well.
  6273. :description Optional default description for the second pair
  6274. of brackets in an Org-mode link. The user can still change
  6275. this when inserting this link into an Org-mode buffer.
  6276. In addition to these, any additional properties can be specified
  6277. and then used in remember templates.")
  6278. (defun org-add-link-type (type &optional follow export)
  6279. "Add TYPE to the list of `org-link-types'.
  6280. Re-compute all regular expressions depending on `org-link-types'
  6281. FOLLOW and EXPORT are two functions.
  6282. FOLLOW should take the link path as the single argument and do whatever
  6283. is necessary to follow the link, for example find a file or display
  6284. a mail message.
  6285. EXPORT should format the link path for export to one of the export formats.
  6286. It should be a function accepting three arguments:
  6287. path the path of the link, the text after the prefix (like \"http:\")
  6288. desc the description of the link, if any, nil if there was no description
  6289. format the export format, a symbol like `html' or `latex'.
  6290. The function may use the FORMAT information to return different values
  6291. depending on the format. The return value will be put literally into
  6292. the exported file.
  6293. Org-mode has a built-in default for exporting links. If you are happy with
  6294. this default, there is no need to define an export function for the link
  6295. type. For a simple example of an export function, see `org-bbdb.el'."
  6296. (add-to-list 'org-link-types type t)
  6297. (org-make-link-regexps)
  6298. (if (assoc type org-link-protocols)
  6299. (setcdr (assoc type org-link-protocols) (list follow export))
  6300. (push (list type follow export) org-link-protocols)))
  6301. ;;;###autoload
  6302. (defun org-store-link (arg)
  6303. "\\<org-mode-map>Store an org-link to the current location.
  6304. This link is added to `org-stored-links' and can later be inserted
  6305. into an org-buffer with \\[org-insert-link].
  6306. For some link types, a prefix arg is interpreted:
  6307. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6308. For file links, arg negates `org-context-in-file-links'."
  6309. (interactive "P")
  6310. (org-load-modules-maybe)
  6311. (setq org-store-link-plist nil) ; reset
  6312. (let ((outline-regexp (org-get-limited-outline-regexp))
  6313. link cpltxt desc description search txt custom-id)
  6314. (cond
  6315. ((run-hook-with-args-until-success 'org-store-link-functions)
  6316. (setq link (plist-get org-store-link-plist :link)
  6317. desc (or (plist-get org-store-link-plist :description) link)))
  6318. ((equal (buffer-name) "*Org Edit Src Example*")
  6319. (let (label gc)
  6320. (while (or (not label)
  6321. (save-excursion
  6322. (save-restriction
  6323. (widen)
  6324. (goto-char (point-min))
  6325. (re-search-forward
  6326. (regexp-quote (format org-coderef-label-format label))
  6327. nil t))))
  6328. (when label (message "Label exists already") (sit-for 2))
  6329. (setq label (read-string "Code line label: " label)))
  6330. (end-of-line 1)
  6331. (setq link (format org-coderef-label-format label))
  6332. (setq gc (- 79 (length link)))
  6333. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6334. (insert link)
  6335. (setq link (concat "(" label ")") desc nil)))
  6336. ((eq major-mode 'calendar-mode)
  6337. (let ((cd (calendar-cursor-to-date)))
  6338. (setq link
  6339. (format-time-string
  6340. (car org-time-stamp-formats)
  6341. (apply 'encode-time
  6342. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6343. nil nil nil))))
  6344. (org-store-link-props :type "calendar" :date cd)))
  6345. ((eq major-mode 'w3-mode)
  6346. (setq cpltxt (if (and (buffer-name)
  6347. (not (string-match "Untitled" (buffer-name))))
  6348. (buffer-name)
  6349. (url-view-url t))
  6350. link (org-make-link (url-view-url t)))
  6351. (org-store-link-props :type "w3" :url (url-view-url t)))
  6352. ((eq major-mode 'w3m-mode)
  6353. (setq cpltxt (or w3m-current-title w3m-current-url)
  6354. link (org-make-link w3m-current-url))
  6355. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6356. ((setq search (run-hook-with-args-until-success
  6357. 'org-create-file-search-functions))
  6358. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6359. "::" search))
  6360. (setq cpltxt (or description link)))
  6361. ((eq major-mode 'image-mode)
  6362. (setq cpltxt (concat "file:"
  6363. (abbreviate-file-name buffer-file-name))
  6364. link (org-make-link cpltxt))
  6365. (org-store-link-props :type "image" :file buffer-file-name))
  6366. ((eq major-mode 'dired-mode)
  6367. ;; link to the file in the current line
  6368. (setq cpltxt (concat "file:"
  6369. (abbreviate-file-name
  6370. (expand-file-name
  6371. (dired-get-filename nil t))))
  6372. link (org-make-link cpltxt)))
  6373. ((and buffer-file-name (org-mode-p))
  6374. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6375. (cond
  6376. ((org-in-regexp "<<\\(.*?\\)>>")
  6377. (setq cpltxt
  6378. (concat "file:"
  6379. (abbreviate-file-name buffer-file-name)
  6380. "::" (match-string 1))
  6381. link (org-make-link cpltxt)))
  6382. ((and (featurep 'org-id)
  6383. (or (eq org-link-to-org-use-id t)
  6384. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6385. (interactive-p))
  6386. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6387. (interactive-p)
  6388. (not custom-id))
  6389. (and org-link-to-org-use-id
  6390. (condition-case nil
  6391. (org-entry-get nil "ID")
  6392. (error nil)))))
  6393. ;; We can make a link using the ID.
  6394. (setq link (condition-case nil
  6395. (prog1 (org-id-store-link)
  6396. (setq desc (plist-get org-store-link-plist
  6397. :description)))
  6398. (error
  6399. ;; probably before first headline, link to file only
  6400. (concat "file:"
  6401. (abbreviate-file-name buffer-file-name))))))
  6402. (t
  6403. ;; Just link to current headline
  6404. (setq cpltxt (concat "file:"
  6405. (abbreviate-file-name buffer-file-name)))
  6406. ;; Add a context search string
  6407. (when (org-xor org-context-in-file-links arg)
  6408. (setq txt (cond
  6409. ((org-on-heading-p) nil)
  6410. ((org-region-active-p)
  6411. (buffer-substring (region-beginning) (region-end)))
  6412. (t nil)))
  6413. (when (or (null txt) (string-match "\\S-" txt))
  6414. (setq cpltxt
  6415. (concat cpltxt "::"
  6416. (condition-case nil
  6417. (org-make-org-heading-search-string txt)
  6418. (error "")))
  6419. desc (or (nth 4 (org-heading-components)) "NONE"))))
  6420. (if (string-match "::\\'" cpltxt)
  6421. (setq cpltxt (substring cpltxt 0 -2)))
  6422. (setq link (org-make-link cpltxt)))))
  6423. ((buffer-file-name (buffer-base-buffer))
  6424. ;; Just link to this file here.
  6425. (setq cpltxt (concat "file:"
  6426. (abbreviate-file-name
  6427. (buffer-file-name (buffer-base-buffer)))))
  6428. ;; Add a context string
  6429. (when (org-xor org-context-in-file-links arg)
  6430. (setq txt (if (org-region-active-p)
  6431. (buffer-substring (region-beginning) (region-end))
  6432. (buffer-substring (point-at-bol) (point-at-eol))))
  6433. ;; Only use search option if there is some text.
  6434. (when (string-match "\\S-" txt)
  6435. (setq cpltxt
  6436. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6437. desc "NONE")))
  6438. (setq link (org-make-link cpltxt)))
  6439. ((interactive-p)
  6440. (error "Cannot link to a buffer which is not visiting a file"))
  6441. (t (setq link nil)))
  6442. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6443. (setq link (or link cpltxt)
  6444. desc (or desc cpltxt))
  6445. (if (equal desc "NONE") (setq desc nil))
  6446. (if (and (or (interactive-p) executing-kbd-macro) link)
  6447. (progn
  6448. (setq org-stored-links
  6449. (cons (list link desc) org-stored-links))
  6450. (message "Stored: %s" (or desc link))
  6451. (when custom-id
  6452. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6453. "::#" custom-id))
  6454. (setq org-stored-links
  6455. (cons (list link desc) org-stored-links))))
  6456. (and link (org-make-link-string link desc)))))
  6457. (defun org-store-link-props (&rest plist)
  6458. "Store link properties, extract names and addresses."
  6459. (let (x adr)
  6460. (when (setq x (plist-get plist :from))
  6461. (setq adr (mail-extract-address-components x))
  6462. (setq plist (plist-put plist :fromname (car adr)))
  6463. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6464. (when (setq x (plist-get plist :to))
  6465. (setq adr (mail-extract-address-components x))
  6466. (setq plist (plist-put plist :toname (car adr)))
  6467. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6468. (let ((from (plist-get plist :from))
  6469. (to (plist-get plist :to)))
  6470. (when (and from to org-from-is-user-regexp)
  6471. (setq plist
  6472. (plist-put plist :fromto
  6473. (if (string-match org-from-is-user-regexp from)
  6474. (concat "to %t")
  6475. (concat "from %f"))))))
  6476. (setq org-store-link-plist plist))
  6477. (defun org-add-link-props (&rest plist)
  6478. "Add these properties to the link property list."
  6479. (let (key value)
  6480. (while plist
  6481. (setq key (pop plist) value (pop plist))
  6482. (setq org-store-link-plist
  6483. (plist-put org-store-link-plist key value)))))
  6484. (defun org-email-link-description (&optional fmt)
  6485. "Return the description part of an email link.
  6486. This takes information from `org-store-link-plist' and formats it
  6487. according to FMT (default from `org-email-link-description-format')."
  6488. (setq fmt (or fmt org-email-link-description-format))
  6489. (let* ((p org-store-link-plist)
  6490. (to (plist-get p :toaddress))
  6491. (from (plist-get p :fromaddress))
  6492. (table
  6493. (list
  6494. (cons "%c" (plist-get p :fromto))
  6495. (cons "%F" (plist-get p :from))
  6496. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6497. (cons "%T" (plist-get p :to))
  6498. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6499. (cons "%s" (plist-get p :subject))
  6500. (cons "%m" (plist-get p :message-id)))))
  6501. (when (string-match "%c" fmt)
  6502. ;; Check if the user wrote this message
  6503. (if (and org-from-is-user-regexp from to
  6504. (save-match-data (string-match org-from-is-user-regexp from)))
  6505. (setq fmt (replace-match "to %t" t t fmt))
  6506. (setq fmt (replace-match "from %f" t t fmt))))
  6507. (org-replace-escapes fmt table)))
  6508. (defun org-make-org-heading-search-string (&optional string heading)
  6509. "Make search string for STRING or current headline."
  6510. (interactive)
  6511. (let ((s (or string (org-get-heading))))
  6512. (unless (and string (not heading))
  6513. ;; We are using a headline, clean up garbage in there.
  6514. (if (string-match org-todo-regexp s)
  6515. (setq s (replace-match "" t t s)))
  6516. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6517. (setq s (replace-match "" t t s)))
  6518. (setq s (org-trim s))
  6519. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6520. org-comment-string "\\)") s)
  6521. (setq s (replace-match "" t t s)))
  6522. (while (string-match org-ts-regexp s)
  6523. (setq s (replace-match "" t t s))))
  6524. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6525. (setq s (replace-match " " t t s)))
  6526. (or string (setq s (concat "*" s))) ; Add * for headlines
  6527. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6528. (defun org-make-link (&rest strings)
  6529. "Concatenate STRINGS."
  6530. (apply 'concat strings))
  6531. (defun org-make-link-string (link &optional description)
  6532. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6533. (unless (string-match "\\S-" link)
  6534. (error "Empty link"))
  6535. (when (stringp description)
  6536. ;; Remove brackets from the description, they are fatal.
  6537. (while (string-match "\\[" description)
  6538. (setq description (replace-match "{" t t description)))
  6539. (while (string-match "\\]" description)
  6540. (setq description (replace-match "}" t t description))))
  6541. (when (equal (org-link-escape link) description)
  6542. ;; No description needed, it is identical
  6543. (setq description nil))
  6544. (when (and (not description)
  6545. (not (equal link (org-link-escape link))))
  6546. (setq description (org-extract-attributes link)))
  6547. (concat "[[" (org-link-escape link) "]"
  6548. (if description (concat "[" description "]") "")
  6549. "]"))
  6550. (defconst org-link-escape-chars
  6551. '((?\ . "%20")
  6552. (?\[ . "%5B")
  6553. (?\] . "%5D")
  6554. (?\340 . "%E0") ; `a
  6555. (?\342 . "%E2") ; ^a
  6556. (?\347 . "%E7") ; ,c
  6557. (?\350 . "%E8") ; `e
  6558. (?\351 . "%E9") ; 'e
  6559. (?\352 . "%EA") ; ^e
  6560. (?\356 . "%EE") ; ^i
  6561. (?\364 . "%F4") ; ^o
  6562. (?\371 . "%F9") ; `u
  6563. (?\373 . "%FB") ; ^u
  6564. (?\; . "%3B")
  6565. (?? . "%3F")
  6566. (?= . "%3D")
  6567. (?+ . "%2B")
  6568. )
  6569. "Association list of escapes for some characters problematic in links.
  6570. This is the list that is used for internal purposes.")
  6571. (defvar org-url-encoding-use-url-hexify nil)
  6572. (defconst org-link-escape-chars-browser
  6573. '((?\ . "%20")) ; 32 for the SPC char
  6574. "Association list of escapes for some characters problematic in links.
  6575. This is the list that is used before handing over to the browser.")
  6576. (defun org-link-escape (text &optional table)
  6577. "Escape characters in TEXT that are problematic for links."
  6578. (if org-url-encoding-use-url-hexify
  6579. (url-hexify-string text)
  6580. (setq table (or table org-link-escape-chars))
  6581. (when text
  6582. (let ((re (mapconcat (lambda (x) (regexp-quote
  6583. (char-to-string (car x))))
  6584. table "\\|")))
  6585. (while (string-match re text)
  6586. (setq text
  6587. (replace-match
  6588. (cdr (assoc (string-to-char (match-string 0 text))
  6589. table))
  6590. t t text)))
  6591. text))))
  6592. (defun org-link-unescape (text &optional table)
  6593. "Reverse the action of `org-link-escape'."
  6594. (if org-url-encoding-use-url-hexify
  6595. (url-unhex-string text)
  6596. (setq table (or table org-link-escape-chars))
  6597. (when text
  6598. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6599. table "\\|")))
  6600. (while (string-match re text)
  6601. (setq text
  6602. (replace-match
  6603. (char-to-string (car (rassoc (match-string 0 text) table)))
  6604. t t text)))
  6605. text))))
  6606. (defun org-xor (a b)
  6607. "Exclusive or."
  6608. (if a (not b) b))
  6609. (defun org-fixup-message-id-for-http (s)
  6610. "Replace special characters in a message id, so it can be used in an http query."
  6611. (while (string-match "<" s)
  6612. (setq s (replace-match "%3C" t t s)))
  6613. (while (string-match ">" s)
  6614. (setq s (replace-match "%3E" t t s)))
  6615. (while (string-match "@" s)
  6616. (setq s (replace-match "%40" t t s)))
  6617. s)
  6618. ;;;###autoload
  6619. (defun org-insert-link-global ()
  6620. "Insert a link like Org-mode does.
  6621. This command can be called in any mode to insert a link in Org-mode syntax."
  6622. (interactive)
  6623. (org-load-modules-maybe)
  6624. (org-run-like-in-org-mode 'org-insert-link))
  6625. (defun org-insert-link (&optional complete-file link-location)
  6626. "Insert a link. At the prompt, enter the link.
  6627. Completion can be used to insert any of the link protocol prefixes like
  6628. http or ftp in use.
  6629. The history can be used to select a link previously stored with
  6630. `org-store-link'. When the empty string is entered (i.e. if you just
  6631. press RET at the prompt), the link defaults to the most recently
  6632. stored link. As SPC triggers completion in the minibuffer, you need to
  6633. use M-SPC or C-q SPC to force the insertion of a space character.
  6634. You will also be prompted for a description, and if one is given, it will
  6635. be displayed in the buffer instead of the link.
  6636. If there is already a link at point, this command will allow you to edit link
  6637. and description parts.
  6638. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6639. be selected using completion. The path to the file will be relative to the
  6640. current directory if the file is in the current directory or a subdirectory.
  6641. Otherwise, the link will be the absolute path as completed in the minibuffer
  6642. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6643. option `org-link-file-path-type'.
  6644. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6645. the current directory or below.
  6646. With three \\[universal-argument] prefixes, negate the meaning of
  6647. `org-keep-stored-link-after-insertion'.
  6648. If `org-make-link-description-function' is non-nil, this function will be
  6649. called with the link target, and the result will be the default
  6650. link description.
  6651. If the LINK-LOCATION parameter is non-nil, this value will be
  6652. used as the link location instead of reading one interactively."
  6653. (interactive "P")
  6654. (let* ((wcf (current-window-configuration))
  6655. (region (if (org-region-active-p)
  6656. (buffer-substring (region-beginning) (region-end))))
  6657. (remove (and region (list (region-beginning) (region-end))))
  6658. (desc region)
  6659. tmphist ; byte-compile incorrectly complains about this
  6660. (link link-location)
  6661. entry file all-prefixes)
  6662. (cond
  6663. (link-location) ; specified by arg, just use it.
  6664. ((org-in-regexp org-bracket-link-regexp 1)
  6665. ;; We do have a link at point, and we are going to edit it.
  6666. (setq remove (list (match-beginning 0) (match-end 0)))
  6667. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6668. (setq link (read-string "Link: "
  6669. (org-link-unescape
  6670. (org-match-string-no-properties 1)))))
  6671. ((or (org-in-regexp org-angle-link-re)
  6672. (org-in-regexp org-plain-link-re))
  6673. ;; Convert to bracket link
  6674. (setq remove (list (match-beginning 0) (match-end 0))
  6675. link (read-string "Link: "
  6676. (org-remove-angle-brackets (match-string 0)))))
  6677. ((member complete-file '((4) (16)))
  6678. ;; Completing read for file names.
  6679. (setq link (org-file-complete-link complete-file)))
  6680. (t
  6681. ;; Read link, with completion for stored links.
  6682. (with-output-to-temp-buffer "*Org Links*"
  6683. (princ "Insert a link.
  6684. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6685. (when org-stored-links
  6686. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6687. (princ (mapconcat
  6688. (lambda (x)
  6689. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6690. (reverse org-stored-links) "\n"))))
  6691. (let ((cw (selected-window)))
  6692. (select-window (get-buffer-window "*Org Links*"))
  6693. (setq truncate-lines t)
  6694. (unless (pos-visible-in-window-p (point-max))
  6695. (org-fit-window-to-buffer))
  6696. (and (window-live-p cw) (select-window cw)))
  6697. ;; Fake a link history, containing the stored links.
  6698. (setq tmphist (append (mapcar 'car org-stored-links)
  6699. org-insert-link-history))
  6700. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6701. (mapcar 'car org-link-abbrev-alist)
  6702. org-link-types))
  6703. (unwind-protect
  6704. (progn
  6705. (setq link
  6706. (let ((org-completion-use-ido nil))
  6707. (org-completing-read
  6708. "Link: "
  6709. (append
  6710. (mapcar (lambda (x) (list (concat x ":")))
  6711. all-prefixes)
  6712. (mapcar 'car org-stored-links))
  6713. nil nil nil
  6714. 'tmphist
  6715. (car (car org-stored-links)))))
  6716. (if (or (member link all-prefixes)
  6717. (and (equal ":" (substring link -1))
  6718. (member (substring link 0 -1) all-prefixes)
  6719. (setq link (substring link 0 -1))))
  6720. (setq link (org-link-try-special-completion link))))
  6721. (set-window-configuration wcf)
  6722. (kill-buffer "*Org Links*"))
  6723. (setq entry (assoc link org-stored-links))
  6724. (or entry (push link org-insert-link-history))
  6725. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6726. (not org-keep-stored-link-after-insertion))
  6727. (setq org-stored-links (delq (assoc link org-stored-links)
  6728. org-stored-links)))
  6729. (setq desc (or desc (nth 1 entry)))))
  6730. (if (string-match org-plain-link-re link)
  6731. ;; URL-like link, normalize the use of angular brackets.
  6732. (setq link (org-make-link (org-remove-angle-brackets link))))
  6733. ;; Check if we are linking to the current file with a search option
  6734. ;; If yes, simplify the link by using only the search option.
  6735. (when (and buffer-file-name
  6736. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6737. (let* ((path (match-string 1 link))
  6738. (case-fold-search nil)
  6739. (search (match-string 2 link)))
  6740. (save-match-data
  6741. (if (equal (file-truename buffer-file-name) (file-truename path))
  6742. ;; We are linking to this same file, with a search option
  6743. (setq link search)))))
  6744. ;; Check if we can/should use a relative path. If yes, simplify the link
  6745. (when (string-match "^file:\\(.*\\)" link)
  6746. (let* ((path (match-string 1 link))
  6747. (origpath path)
  6748. (case-fold-search nil))
  6749. (cond
  6750. ((or (eq org-link-file-path-type 'absolute)
  6751. (equal complete-file '(16)))
  6752. (setq path (abbreviate-file-name (expand-file-name path))))
  6753. ((eq org-link-file-path-type 'noabbrev)
  6754. (setq path (expand-file-name path)))
  6755. ((eq org-link-file-path-type 'relative)
  6756. (setq path (file-relative-name path)))
  6757. (t
  6758. (save-match-data
  6759. (if (string-match (concat "^" (regexp-quote
  6760. (file-name-as-directory
  6761. (expand-file-name "."))))
  6762. (expand-file-name path))
  6763. ;; We are linking a file with relative path name.
  6764. (setq path (substring (expand-file-name path)
  6765. (match-end 0)))
  6766. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6767. (setq link (concat "file:" path))
  6768. (if (equal desc origpath)
  6769. (setq desc path))))
  6770. (if org-make-link-description-function
  6771. (setq desc (funcall org-make-link-description-function link desc)))
  6772. (setq desc (read-string "Description: " desc))
  6773. (unless (string-match "\\S-" desc) (setq desc nil))
  6774. (if remove (apply 'delete-region remove))
  6775. (insert (org-make-link-string link desc))))
  6776. (defun org-link-try-special-completion (type)
  6777. "If there is completion support for link type TYPE, offer it."
  6778. (let ((fun (intern (concat "org-" type "-complete-link"))))
  6779. (if (functionp fun)
  6780. (funcall fun)
  6781. (read-string "Link (no completion support): " (concat type ":")))))
  6782. (defun org-file-complete-link (&optional arg)
  6783. "Create a file link using completion."
  6784. (let (file link)
  6785. (setq file (read-file-name "File: "))
  6786. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6787. (pwd1 (file-name-as-directory (abbreviate-file-name
  6788. (expand-file-name ".")))))
  6789. (cond
  6790. ((equal arg '(16))
  6791. (setq link (org-make-link
  6792. "file:"
  6793. (abbreviate-file-name (expand-file-name file)))))
  6794. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6795. (setq link (org-make-link "file:" (match-string 1 file))))
  6796. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6797. (expand-file-name file))
  6798. (setq link (org-make-link
  6799. "file:" (match-string 1 (expand-file-name file)))))
  6800. (t (setq link (org-make-link "file:" file)))))
  6801. link))
  6802. (defun org-completing-read (&rest args)
  6803. "Completing-read with SPACE being a normal character."
  6804. (let ((minibuffer-local-completion-map
  6805. (copy-keymap minibuffer-local-completion-map)))
  6806. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6807. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6808. (apply 'org-ido-completing-read args)))
  6809. (defun org-completing-read-no-ido (&rest args)
  6810. (let (org-completion-use-ido)
  6811. (apply 'org-completing-read args)))
  6812. (defun org-ido-completing-read (&rest args)
  6813. "Completing-read using `ido-mode' speedups if available"
  6814. (if (and org-completion-use-ido
  6815. (fboundp 'ido-completing-read)
  6816. (boundp 'ido-mode) ido-mode
  6817. (listp (second args)))
  6818. (let ((ido-enter-matching-directory nil))
  6819. (apply 'ido-completing-read (concat (car args))
  6820. (mapcar (lambda (x) (car x)) (nth 1 args))
  6821. (cddr args)))
  6822. (apply 'completing-read args)))
  6823. (defun org-extract-attributes (s)
  6824. "Extract the attributes cookie from a string and set as text property."
  6825. (let (a attr (start 0) key value)
  6826. (save-match-data
  6827. (when (string-match "{{\\([^}]+\\)}}$" s)
  6828. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6829. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6830. (setq key (match-string 1 a) value (match-string 2 a)
  6831. start (match-end 0)
  6832. attr (plist-put attr (intern key) value))))
  6833. (org-add-props s nil 'org-attr attr))
  6834. s))
  6835. (defun org-extract-attributes-from-string (tag)
  6836. (let (key value attr)
  6837. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6838. (setq key (match-string 1 tag) value (match-string 2 tag)
  6839. tag (replace-match "" t t tag)
  6840. attr (plist-put attr (intern key) value)))
  6841. (cons tag attr)))
  6842. (defun org-attributes-to-string (plist)
  6843. "Format a property list into an HTML attribute list."
  6844. (let ((s "") key value)
  6845. (while plist
  6846. (setq key (pop plist) value (pop plist))
  6847. (and value
  6848. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6849. s))
  6850. ;;; Opening/following a link
  6851. (defvar org-link-search-failed nil)
  6852. (defun org-next-link ()
  6853. "Move forward to the next link.
  6854. If the link is in hidden text, expose it."
  6855. (interactive)
  6856. (when (and org-link-search-failed (eq this-command last-command))
  6857. (goto-char (point-min))
  6858. (message "Link search wrapped back to beginning of buffer"))
  6859. (setq org-link-search-failed nil)
  6860. (let* ((pos (point))
  6861. (ct (org-context))
  6862. (a (assoc :link ct)))
  6863. (if a (goto-char (nth 2 a)))
  6864. (if (re-search-forward org-any-link-re nil t)
  6865. (progn
  6866. (goto-char (match-beginning 0))
  6867. (if (org-invisible-p) (org-show-context)))
  6868. (goto-char pos)
  6869. (setq org-link-search-failed t)
  6870. (error "No further link found"))))
  6871. (defun org-previous-link ()
  6872. "Move backward to the previous link.
  6873. If the link is in hidden text, expose it."
  6874. (interactive)
  6875. (when (and org-link-search-failed (eq this-command last-command))
  6876. (goto-char (point-max))
  6877. (message "Link search wrapped back to end of buffer"))
  6878. (setq org-link-search-failed nil)
  6879. (let* ((pos (point))
  6880. (ct (org-context))
  6881. (a (assoc :link ct)))
  6882. (if a (goto-char (nth 1 a)))
  6883. (if (re-search-backward org-any-link-re nil t)
  6884. (progn
  6885. (goto-char (match-beginning 0))
  6886. (if (org-invisible-p) (org-show-context)))
  6887. (goto-char pos)
  6888. (setq org-link-search-failed t)
  6889. (error "No further link found"))))
  6890. (defun org-translate-link (s)
  6891. "Translate a link string if a translation function has been defined."
  6892. (if (and org-link-translation-function
  6893. (fboundp org-link-translation-function)
  6894. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6895. (progn
  6896. (setq s (funcall org-link-translation-function
  6897. (match-string 1) (match-string 2)))
  6898. (concat (car s) ":" (cdr s)))
  6899. s))
  6900. (defun org-translate-link-from-planner (type path)
  6901. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6902. This is still an experimental function, your mileage may vary."
  6903. (cond
  6904. ((member type '("http" "https" "news" "ftp"))
  6905. ;; standard Internet links are the same.
  6906. nil)
  6907. ((and (equal type "irc") (string-match "^//" path))
  6908. ;; Planner has two / at the beginning of an irc link, we have 1.
  6909. ;; We should have zero, actually....
  6910. (setq path (substring path 1)))
  6911. ((and (equal type "lisp") (string-match "^/" path))
  6912. ;; Planner has a slash, we do not.
  6913. (setq type "elisp" path (substring path 1)))
  6914. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6915. ;; A typical message link. Planner has the id after the fina slash,
  6916. ;; we separate it with a hash mark
  6917. (setq path (concat (match-string 1 path) "#"
  6918. (org-remove-angle-brackets (match-string 2 path)))))
  6919. )
  6920. (cons type path))
  6921. (defun org-find-file-at-mouse (ev)
  6922. "Open file link or URL at mouse."
  6923. (interactive "e")
  6924. (mouse-set-point ev)
  6925. (org-open-at-point 'in-emacs))
  6926. (defun org-open-at-mouse (ev)
  6927. "Open file link or URL at mouse."
  6928. (interactive "e")
  6929. (mouse-set-point ev)
  6930. (if (eq major-mode 'org-agenda-mode)
  6931. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6932. (org-open-at-point))
  6933. (defvar org-window-config-before-follow-link nil
  6934. "The window configuration before following a link.
  6935. This is saved in case the need arises to restore it.")
  6936. (defvar org-open-link-marker (make-marker)
  6937. "Marker pointing to the location where `org-open-at-point; was called.")
  6938. ;;;###autoload
  6939. (defun org-open-at-point-global ()
  6940. "Follow a link like Org-mode does.
  6941. This command can be called in any mode to follow a link that has
  6942. Org-mode syntax."
  6943. (interactive)
  6944. (org-run-like-in-org-mode 'org-open-at-point))
  6945. ;;;###autoload
  6946. (defun org-open-link-from-string (s &optional arg)
  6947. "Open a link in the string S, as if it was in Org-mode."
  6948. (interactive "sLink: \nP")
  6949. (let ((reference-buffer (current-buffer)))
  6950. (with-temp-buffer
  6951. (let ((org-inhibit-startup t))
  6952. (org-mode)
  6953. (insert s)
  6954. (goto-char (point-min))
  6955. (org-open-at-point arg reference-buffer)))))
  6956. (defun org-open-at-point (&optional in-emacs reference-buffer)
  6957. "Open link at or after point.
  6958. If there is no link at point, this function will search forward up to
  6959. the end of the current line.
  6960. Normally, files will be opened by an appropriate application. If the
  6961. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6962. With a double prefix argument, try to open outside of Emacs, in the
  6963. application the system uses for this file type."
  6964. (interactive "P")
  6965. (org-load-modules-maybe)
  6966. (move-marker org-open-link-marker (point))
  6967. (setq org-window-config-before-follow-link (current-window-configuration))
  6968. (org-remove-occur-highlights nil nil t)
  6969. (cond
  6970. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6971. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6972. (org-footnote-action))
  6973. (t
  6974. (let (type path link line search (pos (point)))
  6975. (catch 'match
  6976. (save-excursion
  6977. (skip-chars-forward "^]\n\r")
  6978. (when (org-in-regexp org-bracket-link-regexp)
  6979. (setq link (org-extract-attributes
  6980. (org-link-unescape (org-match-string-no-properties 1))))
  6981. (while (string-match " *\n *" link)
  6982. (setq link (replace-match " " t t link)))
  6983. (setq link (org-link-expand-abbrev link))
  6984. (cond
  6985. ((or (file-name-absolute-p link)
  6986. (string-match "^\\.\\.?/" link))
  6987. (setq type "file" path link))
  6988. ((string-match org-link-re-with-space3 link)
  6989. (setq type (match-string 1 link) path (match-string 2 link)))
  6990. (t (setq type "thisfile" path link)))
  6991. (throw 'match t)))
  6992. (when (get-text-property (point) 'org-linked-text)
  6993. (setq type "thisfile"
  6994. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6995. (1+ (point)) (point))
  6996. path (buffer-substring
  6997. (previous-single-property-change pos 'org-linked-text)
  6998. (next-single-property-change pos 'org-linked-text)))
  6999. (throw 'match t))
  7000. (save-excursion
  7001. (when (or (org-in-regexp org-angle-link-re)
  7002. (org-in-regexp org-plain-link-re))
  7003. (setq type (match-string 1) path (match-string 2))
  7004. (throw 'match t)))
  7005. (save-excursion
  7006. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7007. (setq type "tags"
  7008. path (match-string 1))
  7009. (while (string-match ":" path)
  7010. (setq path (replace-match "+" t t path)))
  7011. (throw 'match t)))
  7012. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7013. (setq type "tree-match"
  7014. path (match-string 1))
  7015. (throw 'match t)))
  7016. (unless path
  7017. (error "No link found"))
  7018. ;; switch back to reference buffer
  7019. ;; needed when if called in a temporary buffer through
  7020. ;; org-open-link-from-string
  7021. (and reference-buffer (switch-to-buffer reference-buffer))
  7022. ;; Remove any trailing spaces in path
  7023. (if (string-match " +\\'" path)
  7024. (setq path (replace-match "" t t path)))
  7025. (if (and org-link-translation-function
  7026. (fboundp org-link-translation-function))
  7027. ;; Check if we need to translate the link
  7028. (let ((tmp (funcall org-link-translation-function type path)))
  7029. (setq type (car tmp) path (cdr tmp))))
  7030. (cond
  7031. ((assoc type org-link-protocols)
  7032. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7033. ((equal type "mailto")
  7034. (let ((cmd (car org-link-mailto-program))
  7035. (args (cdr org-link-mailto-program)) args1
  7036. (address path) (subject "") a)
  7037. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7038. (setq address (match-string 1 path)
  7039. subject (org-link-escape (match-string 2 path))))
  7040. (while args
  7041. (cond
  7042. ((not (stringp (car args))) (push (pop args) args1))
  7043. (t (setq a (pop args))
  7044. (if (string-match "%a" a)
  7045. (setq a (replace-match address t t a)))
  7046. (if (string-match "%s" a)
  7047. (setq a (replace-match subject t t a)))
  7048. (push a args1))))
  7049. (apply cmd (nreverse args1))))
  7050. ((member type '("http" "https" "ftp" "news"))
  7051. (browse-url (concat type ":" (org-link-escape
  7052. path org-link-escape-chars-browser))))
  7053. ((member type '("message"))
  7054. (browse-url (concat type ":" path)))
  7055. ((string= type "tags")
  7056. (org-tags-view in-emacs path))
  7057. ((string= type "thisfile")
  7058. (if in-emacs
  7059. (switch-to-buffer-other-window
  7060. (org-get-buffer-for-internal-link (current-buffer)))
  7061. (org-mark-ring-push))
  7062. (let ((cmd `(org-link-search
  7063. ,path
  7064. ,(cond ((equal in-emacs '(4)) 'occur)
  7065. ((equal in-emacs '(16)) 'org-occur)
  7066. (t nil))
  7067. ,pos)))
  7068. (condition-case nil (eval cmd)
  7069. (error (progn (widen) (eval cmd))))))
  7070. ((string= type "tree-match")
  7071. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7072. ((string= type "file")
  7073. (if (string-match "::\\([0-9]+\\)\\'" path)
  7074. (setq line (string-to-number (match-string 1 path))
  7075. path (substring path 0 (match-beginning 0)))
  7076. (if (string-match "::\\(.+\\)\\'" path)
  7077. (setq search (match-string 1 path)
  7078. path (substring path 0 (match-beginning 0)))))
  7079. (if (string-match "[*?{]" (file-name-nondirectory path))
  7080. (dired path)
  7081. (org-open-file path in-emacs line search)))
  7082. ((string= type "news")
  7083. (require 'org-gnus)
  7084. (org-gnus-follow-link path))
  7085. ((string= type "shell")
  7086. (let ((cmd path))
  7087. (if (or (not org-confirm-shell-link-function)
  7088. (funcall org-confirm-shell-link-function
  7089. (format "Execute \"%s\" in shell? "
  7090. (org-add-props cmd nil
  7091. 'face 'org-warning))))
  7092. (progn
  7093. (message "Executing %s" cmd)
  7094. (shell-command cmd))
  7095. (error "Abort"))))
  7096. ((string= type "elisp")
  7097. (let ((cmd path))
  7098. (if (or (not org-confirm-elisp-link-function)
  7099. (funcall org-confirm-elisp-link-function
  7100. (format "Execute \"%s\" as elisp? "
  7101. (org-add-props cmd nil
  7102. 'face 'org-warning))))
  7103. (message "%s => %s" cmd
  7104. (if (equal (string-to-char cmd) ?\()
  7105. (eval (read cmd))
  7106. (call-interactively (read cmd))))
  7107. (error "Abort"))))
  7108. (t
  7109. (browse-url-at-point))))))
  7110. (move-marker org-open-link-marker nil)
  7111. (run-hook-with-args 'org-follow-link-hook))
  7112. ;;;; Time estimates
  7113. (defun org-get-effort (&optional pom)
  7114. "Get the effort estimate for the current entry."
  7115. (org-entry-get pom org-effort-property))
  7116. ;;; File search
  7117. (defvar org-create-file-search-functions nil
  7118. "List of functions to construct the right search string for a file link.
  7119. These functions are called in turn with point at the location to
  7120. which the link should point.
  7121. A function in the hook should first test if it would like to
  7122. handle this file type, for example by checking the major-mode or
  7123. the file extension. If it decides not to handle this file, it
  7124. should just return nil to give other functions a chance. If it
  7125. does handle the file, it must return the search string to be used
  7126. when following the link. The search string will be part of the
  7127. file link, given after a double colon, and `org-open-at-point'
  7128. will automatically search for it. If special measures must be
  7129. taken to make the search successful, another function should be
  7130. added to the companion hook `org-execute-file-search-functions',
  7131. which see.
  7132. A function in this hook may also use `setq' to set the variable
  7133. `description' to provide a suggestion for the descriptive text to
  7134. be used for this link when it gets inserted into an Org-mode
  7135. buffer with \\[org-insert-link].")
  7136. (defvar org-execute-file-search-functions nil
  7137. "List of functions to execute a file search triggered by a link.
  7138. Functions added to this hook must accept a single argument, the
  7139. search string that was part of the file link, the part after the
  7140. double colon. The function must first check if it would like to
  7141. handle this search, for example by checking the major-mode or the
  7142. file extension. If it decides not to handle this search, it
  7143. should just return nil to give other functions a chance. If it
  7144. does handle the search, it must return a non-nil value to keep
  7145. other functions from trying.
  7146. Each function can access the current prefix argument through the
  7147. variable `current-prefix-argument'. Note that a single prefix is
  7148. used to force opening a link in Emacs, so it may be good to only
  7149. use a numeric or double prefix to guide the search function.
  7150. In case this is needed, a function in this hook can also restore
  7151. the window configuration before `org-open-at-point' was called using:
  7152. (set-window-configuration org-window-config-before-follow-link)")
  7153. (defun org-link-search (s &optional type avoid-pos)
  7154. "Search for a link search option.
  7155. If S is surrounded by forward slashes, it is interpreted as a
  7156. regular expression. In org-mode files, this will create an `org-occur'
  7157. sparse tree. In ordinary files, `occur' will be used to list matches.
  7158. If the current buffer is in `dired-mode', grep will be used to search
  7159. in all files. If AVOID-POS is given, ignore matches near that position."
  7160. (let ((case-fold-search t)
  7161. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7162. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7163. (append '(("") (" ") ("\t") ("\n"))
  7164. org-emphasis-alist)
  7165. "\\|") "\\)"))
  7166. (pos (point))
  7167. (pre nil) (post nil)
  7168. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7169. (cond
  7170. ;; First check if there are any special
  7171. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7172. ;; Now try the builtin stuff
  7173. ((and (equal (string-to-char s0) ?#)
  7174. (> (length s0) 1)
  7175. (save-excursion
  7176. (goto-char (point-min))
  7177. (and
  7178. (re-search-forward
  7179. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7180. (setq type 'dedicated
  7181. pos (match-beginning 0))))
  7182. ;; There is an exact target for this
  7183. (goto-char pos)
  7184. (org-back-to-heading t)))
  7185. ((save-excursion
  7186. (goto-char (point-min))
  7187. (and
  7188. (re-search-forward
  7189. (concat "<<" (regexp-quote s0) ">>") nil t)
  7190. (setq type 'dedicated
  7191. pos (match-beginning 0))))
  7192. ;; There is an exact target for this
  7193. (goto-char pos))
  7194. ((and (string-match "^(\\(.*\\))$" s0)
  7195. (save-excursion
  7196. (goto-char (point-min))
  7197. (and
  7198. (re-search-forward
  7199. (concat "[^[]" (regexp-quote
  7200. (format org-coderef-label-format
  7201. (match-string 1 s0))))
  7202. nil t)
  7203. (setq type 'dedicated
  7204. pos (1+ (match-beginning 0))))))
  7205. ;; There is a coderef target for this
  7206. (goto-char pos))
  7207. ((string-match "^/\\(.*\\)/$" s)
  7208. ;; A regular expression
  7209. (cond
  7210. ((org-mode-p)
  7211. (org-occur (match-string 1 s)))
  7212. ;;((eq major-mode 'dired-mode)
  7213. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7214. (t (org-do-occur (match-string 1 s)))))
  7215. (t
  7216. ;; A normal search strings
  7217. (when (equal (string-to-char s) ?*)
  7218. ;; Anchor on headlines, post may include tags.
  7219. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7220. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7221. s (substring s 1)))
  7222. (remove-text-properties
  7223. 0 (length s)
  7224. '(face nil mouse-face nil keymap nil fontified nil) s)
  7225. ;; Make a series of regular expressions to find a match
  7226. (setq words (org-split-string s "[ \n\r\t]+")
  7227. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7228. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7229. "\\)" markers)
  7230. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7231. re2a (concat "[ \t\r\n]" re2a_)
  7232. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7233. re4 (concat "[^a-zA-Z_]" re4_)
  7234. re1 (concat pre re2 post)
  7235. re3 (concat pre (if pre re4_ re4) post)
  7236. re5 (concat pre ".*" re4)
  7237. re2 (concat pre re2)
  7238. re2a (concat pre (if pre re2a_ re2a))
  7239. re4 (concat pre (if pre re4_ re4))
  7240. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7241. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7242. re5 "\\)"
  7243. ))
  7244. (cond
  7245. ((eq type 'org-occur) (org-occur reall))
  7246. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7247. (t (goto-char (point-min))
  7248. (setq type 'fuzzy)
  7249. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7250. (org-search-not-self 1 re1 nil t)
  7251. (org-search-not-self 1 re2 nil t)
  7252. (org-search-not-self 1 re2a nil t)
  7253. (org-search-not-self 1 re3 nil t)
  7254. (org-search-not-self 1 re4 nil t)
  7255. (org-search-not-self 1 re5 nil t)
  7256. )
  7257. (goto-char (match-beginning 1))
  7258. (goto-char pos)
  7259. (error "No match")))))
  7260. (t
  7261. ;; Normal string-search
  7262. (goto-char (point-min))
  7263. (if (search-forward s nil t)
  7264. (goto-char (match-beginning 0))
  7265. (error "No match"))))
  7266. (and (org-mode-p) (org-show-context 'link-search))
  7267. type))
  7268. (defun org-search-not-self (group &rest args)
  7269. "Execute `re-search-forward', but only accept matches that do not
  7270. enclose the position of `org-open-link-marker'."
  7271. (let ((m org-open-link-marker))
  7272. (catch 'exit
  7273. (while (apply 're-search-forward args)
  7274. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7275. (goto-char (match-end group))
  7276. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7277. (> (match-beginning 0) (marker-position m))
  7278. (< (match-end 0) (marker-position m)))
  7279. (save-match-data
  7280. (or (not (org-in-regexp
  7281. org-bracket-link-analytic-regexp 1))
  7282. (not (match-end 4)) ; no description
  7283. (and (<= (match-beginning 4) (point))
  7284. (>= (match-end 4) (point))))))
  7285. (throw 'exit (point))))))))
  7286. (defun org-get-buffer-for-internal-link (buffer)
  7287. "Return a buffer to be used for displaying the link target of internal links."
  7288. (cond
  7289. ((not org-display-internal-link-with-indirect-buffer)
  7290. buffer)
  7291. ((string-match "(Clone)$" (buffer-name buffer))
  7292. (message "Buffer is already a clone, not making another one")
  7293. ;; we also do not modify visibility in this case
  7294. buffer)
  7295. (t ; make a new indirect buffer for displaying the link
  7296. (let* ((bn (buffer-name buffer))
  7297. (ibn (concat bn "(Clone)"))
  7298. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7299. (with-current-buffer ib (org-overview))
  7300. ib))))
  7301. (defun org-do-occur (regexp &optional cleanup)
  7302. "Call the Emacs command `occur'.
  7303. If CLEANUP is non-nil, remove the printout of the regular expression
  7304. in the *Occur* buffer. This is useful if the regex is long and not useful
  7305. to read."
  7306. (occur regexp)
  7307. (when cleanup
  7308. (let ((cwin (selected-window)) win beg end)
  7309. (when (setq win (get-buffer-window "*Occur*"))
  7310. (select-window win))
  7311. (goto-char (point-min))
  7312. (when (re-search-forward "match[a-z]+" nil t)
  7313. (setq beg (match-end 0))
  7314. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7315. (setq end (1- (match-beginning 0)))))
  7316. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7317. (goto-char (point-min))
  7318. (select-window cwin))))
  7319. ;;; The mark ring for links jumps
  7320. (defvar org-mark-ring nil
  7321. "Mark ring for positions before jumps in Org-mode.")
  7322. (defvar org-mark-ring-last-goto nil
  7323. "Last position in the mark ring used to go back.")
  7324. ;; Fill and close the ring
  7325. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7326. (loop for i from 1 to org-mark-ring-length do
  7327. (push (make-marker) org-mark-ring))
  7328. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7329. org-mark-ring)
  7330. (defun org-mark-ring-push (&optional pos buffer)
  7331. "Put the current position or POS into the mark ring and rotate it."
  7332. (interactive)
  7333. (setq pos (or pos (point)))
  7334. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7335. (move-marker (car org-mark-ring)
  7336. (or pos (point))
  7337. (or buffer (current-buffer)))
  7338. (message "%s"
  7339. (substitute-command-keys
  7340. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7341. (defun org-mark-ring-goto (&optional n)
  7342. "Jump to the previous position in the mark ring.
  7343. With prefix arg N, jump back that many stored positions. When
  7344. called several times in succession, walk through the entire ring.
  7345. Org-mode commands jumping to a different position in the current file,
  7346. or to another Org-mode file, automatically push the old position
  7347. onto the ring."
  7348. (interactive "p")
  7349. (let (p m)
  7350. (if (eq last-command this-command)
  7351. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7352. (setq p org-mark-ring))
  7353. (setq org-mark-ring-last-goto p)
  7354. (setq m (car p))
  7355. (switch-to-buffer (marker-buffer m))
  7356. (goto-char m)
  7357. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7358. (defun org-remove-angle-brackets (s)
  7359. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7360. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7361. s)
  7362. (defun org-add-angle-brackets (s)
  7363. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7364. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7365. s)
  7366. (defun org-remove-double-quotes (s)
  7367. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7368. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7369. s)
  7370. ;;; Following specific links
  7371. (defun org-follow-timestamp-link ()
  7372. (cond
  7373. ((org-at-date-range-p t)
  7374. (let ((org-agenda-start-on-weekday)
  7375. (t1 (match-string 1))
  7376. (t2 (match-string 2)))
  7377. (setq t1 (time-to-days (org-time-string-to-time t1))
  7378. t2 (time-to-days (org-time-string-to-time t2)))
  7379. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7380. ((org-at-timestamp-p t)
  7381. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7382. (substring (match-string 1) 0 10)))
  7383. 1))
  7384. (t (error "This should not happen"))))
  7385. ;;; Following file links
  7386. (defvar org-wait nil)
  7387. (defun org-open-file (path &optional in-emacs line search)
  7388. "Open the file at PATH.
  7389. First, this expands any special file name abbreviations. Then the
  7390. configuration variable `org-file-apps' is checked if it contains an
  7391. entry for this file type, and if yes, the corresponding command is launched.
  7392. If no application is found, Emacs simply visits the file.
  7393. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7394. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7395. and o use an external application to visit the file.
  7396. Optional LINE specifies a line to go to, optional SEARCH a string to
  7397. search for. If LINE or SEARCH is given, the file will always be
  7398. opened in Emacs.
  7399. If the file does not exist, an error is thrown."
  7400. (setq in-emacs (or in-emacs line search))
  7401. (let* ((file (if (equal path "")
  7402. buffer-file-name
  7403. (substitute-in-file-name (expand-file-name path))))
  7404. (apps (append org-file-apps (org-default-apps)))
  7405. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7406. (dirp (if remp nil (file-directory-p file)))
  7407. (file (if (and dirp org-open-directory-means-index-dot-org)
  7408. (concat (file-name-as-directory file) "index.org")
  7409. file))
  7410. (a-m-a-p (assq 'auto-mode apps))
  7411. (dfile (downcase file))
  7412. (old-buffer (current-buffer))
  7413. (old-pos (point))
  7414. (old-mode major-mode)
  7415. ext cmd)
  7416. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7417. (setq ext (match-string 1 dfile))
  7418. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7419. (setq ext (match-string 1 dfile))))
  7420. (cond
  7421. ((equal in-emacs '(16))
  7422. (setq cmd (cdr (assoc 'system apps))))
  7423. (in-emacs (setq cmd 'emacs))
  7424. (t
  7425. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7426. (and dirp (cdr (assoc 'directory apps)))
  7427. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7428. 'string-match)
  7429. (cdr (assoc ext apps))
  7430. (cdr (assoc t apps))))))
  7431. (when (eq cmd 'system)
  7432. (setq cmd (cdr (assoc 'system apps))))
  7433. (when (eq cmd 'default)
  7434. (setq cmd (cdr (assoc t apps))))
  7435. (when (eq cmd 'mailcap)
  7436. (require 'mailcap)
  7437. (mailcap-parse-mailcaps)
  7438. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7439. (command (mailcap-mime-info mime-type)))
  7440. (if (stringp command)
  7441. (setq cmd command)
  7442. (setq cmd 'emacs))))
  7443. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7444. (not (file-exists-p file))
  7445. (not org-open-non-existing-files))
  7446. (error "No such file: %s" file))
  7447. (cond
  7448. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7449. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7450. (while (string-match "['\"]%s['\"]" cmd)
  7451. (setq cmd (replace-match "%s" t t cmd)))
  7452. (while (string-match "%s" cmd)
  7453. (setq cmd (replace-match
  7454. (save-match-data
  7455. (shell-quote-argument
  7456. (convert-standard-filename file)))
  7457. t t cmd)))
  7458. (save-window-excursion
  7459. (start-process-shell-command cmd nil cmd)
  7460. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7461. ))
  7462. ((or (stringp cmd)
  7463. (eq cmd 'emacs))
  7464. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7465. (widen)
  7466. (if line (goto-line line)
  7467. (if search (org-link-search search))))
  7468. ((consp cmd)
  7469. (let ((file (convert-standard-filename file)))
  7470. (eval cmd)))
  7471. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7472. (and (org-mode-p) (eq old-mode 'org-mode)
  7473. (or (not (equal old-buffer (current-buffer)))
  7474. (not (equal old-pos (point))))
  7475. (org-mark-ring-push old-pos old-buffer))))
  7476. (defun org-default-apps ()
  7477. "Return the default applications for this operating system."
  7478. (cond
  7479. ((eq system-type 'darwin)
  7480. org-file-apps-defaults-macosx)
  7481. ((eq system-type 'windows-nt)
  7482. org-file-apps-defaults-windowsnt)
  7483. (t org-file-apps-defaults-gnu)))
  7484. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7485. "Convert extensions to regular expressions in the cars of LIST.
  7486. Also, weed out any non-string entries, because the return value is used
  7487. only for regexp matching.
  7488. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7489. point to the symbol `emacs', indicating that the file should
  7490. be opened in Emacs."
  7491. (append
  7492. (delq nil
  7493. (mapcar (lambda (x)
  7494. (if (not (stringp (car x)))
  7495. nil
  7496. (if (string-match "\\W" (car x))
  7497. x
  7498. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7499. list))
  7500. (if add-auto-mode
  7501. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7502. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7503. (defun org-file-remote-p (file)
  7504. "Test whether FILE specifies a location on a remote system.
  7505. Return non-nil if the location is indeed remote.
  7506. For example, the filename \"/user@host:/foo\" specifies a location
  7507. on the system \"/user@host:\"."
  7508. (cond ((fboundp 'file-remote-p)
  7509. (file-remote-p file))
  7510. ((fboundp 'tramp-handle-file-remote-p)
  7511. (tramp-handle-file-remote-p file))
  7512. ((and (boundp 'ange-ftp-name-format)
  7513. (string-match (car ange-ftp-name-format) file))
  7514. t)
  7515. (t nil)))
  7516. ;;;; Refiling
  7517. (defun org-get-org-file ()
  7518. "Read a filename, with default directory `org-directory'."
  7519. (let ((default (or org-default-notes-file remember-data-file)))
  7520. (read-file-name (format "File name [%s]: " default)
  7521. (file-name-as-directory org-directory)
  7522. default)))
  7523. (defun org-notes-order-reversed-p ()
  7524. "Check if the current file should receive notes in reversed order."
  7525. (cond
  7526. ((not org-reverse-note-order) nil)
  7527. ((eq t org-reverse-note-order) t)
  7528. ((not (listp org-reverse-note-order)) nil)
  7529. (t (catch 'exit
  7530. (let ((all org-reverse-note-order)
  7531. entry)
  7532. (while (setq entry (pop all))
  7533. (if (string-match (car entry) buffer-file-name)
  7534. (throw 'exit (cdr entry))))
  7535. nil)))))
  7536. (defvar org-refile-target-table nil
  7537. "The list of refile targets, created by `org-refile'.")
  7538. (defvar org-agenda-new-buffers nil
  7539. "Buffers created to visit agenda files.")
  7540. (defun org-get-refile-targets (&optional default-buffer)
  7541. "Produce a table with refile targets."
  7542. (let ((case-fold-search nil)
  7543. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7544. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7545. targets txt re files f desc descre fast-path-p level pos0)
  7546. (message "Getting targets...")
  7547. (with-current-buffer (or default-buffer (current-buffer))
  7548. (while (setq entry (pop entries))
  7549. (setq files (car entry) desc (cdr entry))
  7550. (setq fast-path-p nil)
  7551. (cond
  7552. ((null files) (setq files (list (current-buffer))))
  7553. ((eq files 'org-agenda-files)
  7554. (setq files (org-agenda-files 'unrestricted)))
  7555. ((and (symbolp files) (fboundp files))
  7556. (setq files (funcall files)))
  7557. ((and (symbolp files) (boundp files))
  7558. (setq files (symbol-value files))))
  7559. (if (stringp files) (setq files (list files)))
  7560. (cond
  7561. ((eq (car desc) :tag)
  7562. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7563. ((eq (car desc) :todo)
  7564. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7565. ((eq (car desc) :regexp)
  7566. (setq descre (cdr desc)))
  7567. ((eq (car desc) :level)
  7568. (setq descre (concat "^\\*\\{" (number-to-string
  7569. (if org-odd-levels-only
  7570. (1- (* 2 (cdr desc)))
  7571. (cdr desc)))
  7572. "\\}[ \t]")))
  7573. ((eq (car desc) :maxlevel)
  7574. (setq fast-path-p t)
  7575. (setq descre (concat "^\\*\\{1," (number-to-string
  7576. (if org-odd-levels-only
  7577. (1- (* 2 (cdr desc)))
  7578. (cdr desc)))
  7579. "\\}[ \t]")))
  7580. (t (error "Bad refiling target description %s" desc)))
  7581. (while (setq f (pop files))
  7582. (save-excursion
  7583. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7584. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7585. (setq f (expand-file-name f))
  7586. (if (eq org-refile-use-outline-path 'file)
  7587. (push (list (file-name-nondirectory f) f nil nil) targets))
  7588. (save-excursion
  7589. (save-restriction
  7590. (widen)
  7591. (goto-char (point-min))
  7592. (while (re-search-forward descre nil t)
  7593. (goto-char (setq pos0 (point-at-bol)))
  7594. (catch 'next
  7595. (when org-refile-target-verify-function
  7596. (save-match-data
  7597. (or (funcall org-refile-target-verify-function)
  7598. (throw 'next t))))
  7599. (when (looking-at org-complex-heading-regexp)
  7600. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7601. txt (org-link-display-format (match-string 4))
  7602. re (concat "^" (regexp-quote
  7603. (buffer-substring (match-beginning 1)
  7604. (match-end 4)))))
  7605. (if (match-end 5) (setq re (concat re "[ \t]+"
  7606. (regexp-quote
  7607. (match-string 5)))))
  7608. (setq re (concat re "[ \t]*$"))
  7609. (when org-refile-use-outline-path
  7610. (setq txt (mapconcat 'org-protect-slash
  7611. (append
  7612. (if (eq org-refile-use-outline-path 'file)
  7613. (list (file-name-nondirectory
  7614. (buffer-file-name (buffer-base-buffer))))
  7615. (if (eq org-refile-use-outline-path 'full-file-path)
  7616. (list (buffer-file-name (buffer-base-buffer)))))
  7617. (org-get-outline-path fast-path-p level txt)
  7618. (list txt))
  7619. "/")))
  7620. (push (list txt f re (point)) targets)))
  7621. (when (= (point) pos0)
  7622. ;; verification function has not moved point
  7623. (goto-char (point-at-eol))))))))))
  7624. (message "Getting targets...done")
  7625. (nreverse targets)))
  7626. (defun org-protect-slash (s)
  7627. (while (string-match "/" s)
  7628. (setq s (replace-match "\\" t t s)))
  7629. s)
  7630. (defvar org-olpa (make-vector 20 nil))
  7631. (defun org-get-outline-path (&optional fastp level heading)
  7632. "Return the outline path to the current entry, as a list."
  7633. (if fastp
  7634. (progn
  7635. (if (> level 19)
  7636. (error "Outline path failure, more than 19 levels."))
  7637. (loop for i from level upto 19 do
  7638. (aset org-olpa i nil))
  7639. (prog1
  7640. (delq nil (append org-olpa nil))
  7641. (aset org-olpa level heading)))
  7642. (let (rtn)
  7643. (save-excursion
  7644. (while (org-up-heading-safe)
  7645. (when (looking-at org-complex-heading-regexp)
  7646. (push (org-match-string-no-properties 4) rtn)))
  7647. rtn))))
  7648. (defvar org-refile-history nil
  7649. "History for refiling operations.")
  7650. (defvar org-after-refile-insert-hook nil
  7651. "Hook run after `org-refile' has inserted its stuff at the new location.
  7652. Note that this is still *before* the stuff will be removed from
  7653. the *old* location.")
  7654. (defun org-refile (&optional goto default-buffer rfloc)
  7655. "Move the entry at point to another heading.
  7656. The list of target headings is compiled using the information in
  7657. `org-refile-targets', which see. This list is created before each use
  7658. and will therefore always be up-to-date.
  7659. At the target location, the entry is filed as a subitem of the target heading.
  7660. Depending on `org-reverse-note-order', the new subitem will either be the
  7661. first or the last subitem.
  7662. If there is an active region, all entries in that region will be moved.
  7663. However, the region must fulfil the requirement that the first heading
  7664. is the first one sets the top-level of the moved text - at most siblings
  7665. below it are allowed.
  7666. With prefix arg GOTO, the command will only visit the target location,
  7667. not actually move anything.
  7668. With a double prefix `C-u C-u', go to the location where the last refiling
  7669. operation has put the subtree.
  7670. RFLOC can be a refile location obtained in a different way.
  7671. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7672. (interactive "P")
  7673. (let* ((cbuf (current-buffer))
  7674. (regionp (org-region-active-p))
  7675. (region-start (and regionp (region-beginning)))
  7676. (region-end (and regionp (region-end)))
  7677. (region-length (and regionp (- region-end region-start)))
  7678. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7679. pos it nbuf file re level reversed)
  7680. (when regionp (goto-char region-start)
  7681. (unless (org-kill-is-subtree-p
  7682. (buffer-substring region-start region-end))
  7683. (error "The region is not a (sequence of) subtree(s)")))
  7684. (if (equal goto '(16))
  7685. (org-refile-goto-last-stored)
  7686. (when (setq it (or rfloc
  7687. (save-excursion
  7688. (org-refile-get-location
  7689. (if goto "Goto: " "Refile to: ") default-buffer
  7690. org-refile-allow-creating-parent-nodes))))
  7691. (setq file (nth 1 it)
  7692. re (nth 2 it)
  7693. pos (nth 3 it))
  7694. (if (and (not goto)
  7695. pos
  7696. (equal (buffer-file-name) file)
  7697. (if regionp
  7698. (and (>= pos region-start)
  7699. (<= pos region-end))
  7700. (and (>= pos (point))
  7701. (< pos (save-excursion
  7702. (org-end-of-subtree t t))))))
  7703. (error "Cannot refile to position inside the tree or region"))
  7704. (setq nbuf (or (find-buffer-visiting file)
  7705. (find-file-noselect file)))
  7706. (if goto
  7707. (progn
  7708. (switch-to-buffer nbuf)
  7709. (goto-char pos)
  7710. (org-show-context 'org-goto))
  7711. (if regionp
  7712. (progn
  7713. (kill-new (buffer-substring region-start region-end))
  7714. (org-save-markers-in-region region-start region-end))
  7715. (org-copy-subtree 1 nil t))
  7716. (save-excursion
  7717. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7718. (find-file-noselect file))))
  7719. (setq reversed (org-notes-order-reversed-p))
  7720. (save-excursion
  7721. (save-restriction
  7722. (widen)
  7723. (if pos
  7724. (progn
  7725. (goto-char pos)
  7726. (looking-at outline-regexp)
  7727. (setq level (org-get-valid-level (funcall outline-level) 1))
  7728. (goto-char
  7729. (if reversed
  7730. (or (outline-next-heading) (point-max))
  7731. (or (save-excursion (outline-get-next-sibling))
  7732. (org-end-of-subtree t t)
  7733. (point-max)))))
  7734. (setq level 1)
  7735. (if (not reversed)
  7736. (goto-char (point-max))
  7737. (goto-char (point-min))
  7738. (or (outline-next-heading) (goto-char (point-max)))))
  7739. (if (not (bolp)) (newline))
  7740. (bookmark-set "org-refile-last-stored")
  7741. (org-paste-subtree level)
  7742. (if (fboundp 'deactivate-mark) (deactivate-mark))
  7743. (run-hooks 'org-after-refile-insert-hook))))
  7744. (if regionp
  7745. (delete-region (point) (+ (point) region-length))
  7746. (org-cut-subtree))
  7747. (when (featurep 'org-inlinetask)
  7748. (org-inlinetask-remove-END-maybe))
  7749. (setq org-markers-to-move nil)
  7750. (message "Refiled to \"%s\"" (car it))))))
  7751. (org-reveal))
  7752. (defun org-refile-goto-last-stored ()
  7753. "Go to the location where the last refile was stored."
  7754. (interactive)
  7755. (bookmark-jump "org-refile-last-stored")
  7756. (message "This is the location of the last refile"))
  7757. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7758. "Prompt the user for a refile location, using PROMPT."
  7759. (let ((org-refile-targets org-refile-targets)
  7760. (org-refile-use-outline-path org-refile-use-outline-path))
  7761. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7762. (unless org-refile-target-table
  7763. (error "No refile targets"))
  7764. (let* ((cbuf (current-buffer))
  7765. (partial-completion-mode nil)
  7766. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7767. (cfunc (if (and org-refile-use-outline-path
  7768. org-outline-path-complete-in-steps)
  7769. 'org-olpath-completing-read
  7770. 'org-ido-completing-read))
  7771. (extra (if org-refile-use-outline-path "/" ""))
  7772. (filename (and cfn (expand-file-name cfn)))
  7773. (tbl (mapcar
  7774. (lambda (x)
  7775. (if (and (not (member org-refile-use-outline-path
  7776. '(file full-file-path)))
  7777. (not (equal filename (nth 1 x))))
  7778. (cons (concat (car x) extra " ("
  7779. (file-name-nondirectory (nth 1 x)) ")")
  7780. (cdr x))
  7781. (cons (concat (car x) extra) (cdr x))))
  7782. org-refile-target-table))
  7783. (completion-ignore-case t)
  7784. pa answ parent-target child parent)
  7785. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7786. nil 'org-refile-history))
  7787. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7788. (if pa
  7789. (progn
  7790. (setcar org-refile-history (car pa))
  7791. pa)
  7792. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7793. (setq parent (match-string 1 answ)
  7794. child (match-string 2 answ))
  7795. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7796. (when (and parent-target
  7797. (or (eq new-nodes t)
  7798. (and (eq new-nodes 'confirm)
  7799. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7800. (org-refile-new-child parent-target child))))))
  7801. (defun org-refile-new-child (parent-target child)
  7802. "Use refile target PARENT-TARGET to add new CHILD below it."
  7803. (unless parent-target
  7804. (error "Cannot find parent for new node"))
  7805. (let ((file (nth 1 parent-target))
  7806. (pos (nth 3 parent-target))
  7807. level)
  7808. (with-current-buffer (or (find-buffer-visiting file)
  7809. (find-file-noselect file))
  7810. (save-excursion
  7811. (save-restriction
  7812. (widen)
  7813. (if pos
  7814. (goto-char pos)
  7815. (goto-char (point-max))
  7816. (if (not (bolp)) (newline)))
  7817. (when (looking-at outline-regexp)
  7818. (setq level (funcall outline-level))
  7819. (org-end-of-subtree t t))
  7820. (org-back-over-empty-lines)
  7821. (insert "\n" (make-string
  7822. (if pos (org-get-valid-level level 1) 1) ?*)
  7823. " " child "\n")
  7824. (beginning-of-line 0)
  7825. (list (concat (car parent-target) "/" child) file "" (point)))))))
  7826. (defun org-olpath-completing-read (prompt collection &rest args)
  7827. "Read an outline path like a file name."
  7828. (let ((thetable collection)
  7829. (org-completion-use-ido nil)) ; does not work with ido.
  7830. (apply
  7831. 'org-ido-completing-read prompt
  7832. (lambda (string predicate &optional flag)
  7833. (let (rtn r f (l (length string)))
  7834. (cond
  7835. ((eq flag nil)
  7836. ;; try completion
  7837. (try-completion string thetable))
  7838. ((eq flag t)
  7839. ;; all-completions
  7840. (setq rtn (all-completions string thetable predicate))
  7841. (mapcar
  7842. (lambda (x)
  7843. (setq r (substring x l))
  7844. (if (string-match " ([^)]*)$" x)
  7845. (setq f (match-string 0 x))
  7846. (setq f ""))
  7847. (if (string-match "/" r)
  7848. (concat string (substring r 0 (match-end 0)) f)
  7849. x))
  7850. rtn))
  7851. ((eq flag 'lambda)
  7852. ;; exact match?
  7853. (assoc string thetable)))
  7854. ))
  7855. args)))
  7856. ;;;; Dynamic blocks
  7857. (defun org-find-dblock (name)
  7858. "Find the first dynamic block with name NAME in the buffer.
  7859. If not found, stay at current position and return nil."
  7860. (let (pos)
  7861. (save-excursion
  7862. (goto-char (point-min))
  7863. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7864. nil t)
  7865. (match-beginning 0))))
  7866. (if pos (goto-char pos))
  7867. pos))
  7868. (defconst org-dblock-start-re
  7869. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7870. "Matches the startline of a dynamic block, with parameters.")
  7871. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7872. "Matches the end of a dynamic block.")
  7873. (defun org-create-dblock (plist)
  7874. "Create a dynamic block section, with parameters taken from PLIST.
  7875. PLIST must contain a :name entry which is used as name of the block."
  7876. (unless (bolp) (newline))
  7877. (let ((name (plist-get plist :name)))
  7878. (insert "#+BEGIN: " name)
  7879. (while plist
  7880. (if (eq (car plist) :name)
  7881. (setq plist (cddr plist))
  7882. (insert " " (prin1-to-string (pop plist)))))
  7883. (insert "\n\n#+END:\n")
  7884. (beginning-of-line -2)))
  7885. (defun org-prepare-dblock ()
  7886. "Prepare dynamic block for refresh.
  7887. This empties the block, puts the cursor at the insert position and returns
  7888. the property list including an extra property :name with the block name."
  7889. (unless (looking-at org-dblock-start-re)
  7890. (error "Not at a dynamic block"))
  7891. (let* ((begdel (1+ (match-end 0)))
  7892. (name (org-no-properties (match-string 1)))
  7893. (params (append (list :name name)
  7894. (read (concat "(" (match-string 3) ")")))))
  7895. (unless (re-search-forward org-dblock-end-re nil t)
  7896. (error "Dynamic block not terminated"))
  7897. (setq params
  7898. (append params
  7899. (list :content (buffer-substring
  7900. begdel (match-beginning 0)))))
  7901. (delete-region begdel (match-beginning 0))
  7902. (goto-char begdel)
  7903. (open-line 1)
  7904. params))
  7905. (defun org-map-dblocks (&optional command)
  7906. "Apply COMMAND to all dynamic blocks in the current buffer.
  7907. If COMMAND is not given, use `org-update-dblock'."
  7908. (let ((cmd (or command 'org-update-dblock))
  7909. pos)
  7910. (save-excursion
  7911. (goto-char (point-min))
  7912. (while (re-search-forward org-dblock-start-re nil t)
  7913. (goto-char (setq pos (match-beginning 0)))
  7914. (condition-case nil
  7915. (funcall cmd)
  7916. (error (message "Error during update of dynamic block")))
  7917. (goto-char pos)
  7918. (unless (re-search-forward org-dblock-end-re nil t)
  7919. (error "Dynamic block not terminated"))))))
  7920. (defun org-dblock-update (&optional arg)
  7921. "User command for updating dynamic blocks.
  7922. Update the dynamic block at point. With prefix ARG, update all dynamic
  7923. blocks in the buffer."
  7924. (interactive "P")
  7925. (if arg
  7926. (org-update-all-dblocks)
  7927. (or (looking-at org-dblock-start-re)
  7928. (org-beginning-of-dblock))
  7929. (org-update-dblock)))
  7930. (defun org-update-dblock ()
  7931. "Update the dynamic block at point
  7932. This means to empty the block, parse for parameters and then call
  7933. the correct writing function."
  7934. (save-window-excursion
  7935. (let* ((pos (point))
  7936. (line (org-current-line))
  7937. (params (org-prepare-dblock))
  7938. (name (plist-get params :name))
  7939. (cmd (intern (concat "org-dblock-write:" name))))
  7940. (message "Updating dynamic block `%s' at line %d..." name line)
  7941. (funcall cmd params)
  7942. (message "Updating dynamic block `%s' at line %d...done" name line)
  7943. (goto-char pos))))
  7944. (defun org-beginning-of-dblock ()
  7945. "Find the beginning of the dynamic block at point.
  7946. Error if there is no such block at point."
  7947. (let ((pos (point))
  7948. beg)
  7949. (end-of-line 1)
  7950. (if (and (re-search-backward org-dblock-start-re nil t)
  7951. (setq beg (match-beginning 0))
  7952. (re-search-forward org-dblock-end-re nil t)
  7953. (> (match-end 0) pos))
  7954. (goto-char beg)
  7955. (goto-char pos)
  7956. (error "Not in a dynamic block"))))
  7957. (defun org-update-all-dblocks ()
  7958. "Update all dynamic blocks in the buffer.
  7959. This function can be used in a hook."
  7960. (when (org-mode-p)
  7961. (org-map-dblocks 'org-update-dblock)))
  7962. ;;;; Completion
  7963. (defconst org-additional-option-like-keywords
  7964. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  7965. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  7966. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "ATTR_LaTeX"
  7967. "BEGIN:" "END:"
  7968. "ORGTBL" "TBLFM:" "TBLNAME:"
  7969. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7970. "BEGIN_QUOTE" "END_QUOTE"
  7971. "BEGIN_VERSE" "END_VERSE"
  7972. "BEGIN_CENTER" "END_CENTER"
  7973. "BEGIN_SRC" "END_SRC"
  7974. "CATEGORY" "COLUMNS"
  7975. "CAPTION" "LABEL"
  7976. "BIND"))
  7977. (defcustom org-structure-template-alist
  7978. '(
  7979. ("s" "#+begin_src ?\n\n#+end_src"
  7980. "<src lang=\"?\">\n\n</src>")
  7981. ("e" "#+begin_example\n?\n#+end_example"
  7982. "<example>\n?\n</example>")
  7983. ("q" "#+begin_quote\n?\n#+end_quote"
  7984. "<quote>\n?\n</quote>")
  7985. ("v" "#+begin_verse\n?\n#+end_verse"
  7986. "<verse>\n?\n/verse>")
  7987. ("c" "#+begin_center\n?\n#+end_center"
  7988. "<center>\n?\n/center>")
  7989. ("l" "#+begin_latex\n?\n#+end_latex"
  7990. "<literal style=\"latex\">\n?\n</literal>")
  7991. ("L" "#+latex: "
  7992. "<literal style=\"latex\">?</literal>")
  7993. ("h" "#+begin_html\n?\n#+end_html"
  7994. "<literal style=\"html\">\n?\n</literal>")
  7995. ("H" "#+html: "
  7996. "<literal style=\"html\">?</literal>")
  7997. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7998. ("A" "#+ascii: ")
  7999. ("i" "#+include %file ?"
  8000. "<include file=%file markup=\"?\">")
  8001. )
  8002. "Structure completion elements.
  8003. This is a list of abbreviation keys and values. The value gets inserted
  8004. it you type @samp{.} followed by the key and then the completion key,
  8005. usually `M-TAB'. %file will be replaced by a file name after prompting
  8006. for the file using completion.
  8007. There are two templates for each key, the first uses the original Org syntax,
  8008. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8009. the default when the /org-mtags.el/ module has been loaded. See also the
  8010. variable `org-mtags-prefer-muse-templates'.
  8011. This is an experimental feature, it is undecided if it is going to stay in."
  8012. :group 'org-completion
  8013. :type '(repeat
  8014. (string :tag "Key")
  8015. (string :tag "Template")
  8016. (string :tag "Muse Template")))
  8017. (defun org-try-structure-completion ()
  8018. "Try to complete a structure template before point.
  8019. This looks for strings like \"<e\" on an otherwise empty line and
  8020. expands them."
  8021. (let ((l (buffer-substring (point-at-bol) (point)))
  8022. a)
  8023. (when (and (looking-at "[ \t]*$")
  8024. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8025. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8026. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8027. (match-beginning 1)) a)
  8028. t)))
  8029. (defun org-complete-expand-structure-template (start cell)
  8030. "Expand a structure template."
  8031. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8032. (rpl (nth (if musep 2 1) cell))
  8033. (ind ""))
  8034. (delete-region start (point))
  8035. (when (string-match "\\`#\\+" rpl)
  8036. (cond
  8037. ((bolp))
  8038. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8039. (setq ind (buffer-substring (point-at-bol) (point))))
  8040. (t (newline))))
  8041. (setq start (point))
  8042. (if (string-match "%file" rpl)
  8043. (setq rpl (replace-match
  8044. (concat
  8045. "\""
  8046. (save-match-data
  8047. (abbreviate-file-name (read-file-name "Include file: ")))
  8048. "\"")
  8049. t t rpl)))
  8050. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8051. (concat "\n" ind)))
  8052. (insert rpl)
  8053. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8054. (defun org-complete (&optional arg)
  8055. "Perform completion on word at point.
  8056. At the beginning of a headline, this completes TODO keywords as given in
  8057. `org-todo-keywords'.
  8058. If the current word is preceded by a backslash, completes the TeX symbols
  8059. that are supported for HTML support.
  8060. If the current word is preceded by \"#+\", completes special words for
  8061. setting file options.
  8062. In the line after \"#+STARTUP:, complete valid keywords.\"
  8063. At all other locations, this simply calls the value of
  8064. `org-completion-fallback-command'."
  8065. (interactive "P")
  8066. (org-without-partial-completion
  8067. (catch 'exit
  8068. (let* ((a nil)
  8069. (end (point))
  8070. (beg1 (save-excursion
  8071. (skip-chars-backward (org-re "[:alnum:]_@"))
  8072. (point)))
  8073. (beg (save-excursion
  8074. (skip-chars-backward "a-zA-Z0-9_:$")
  8075. (point)))
  8076. (confirm (lambda (x) (stringp (car x))))
  8077. (searchhead (equal (char-before beg) ?*))
  8078. (struct
  8079. (when (and (member (char-before beg1) '(?. ?<))
  8080. (setq a (assoc (buffer-substring beg1 (point))
  8081. org-structure-template-alist)))
  8082. (org-complete-expand-structure-template (1- beg1) a)
  8083. (throw 'exit t)))
  8084. (tag (and (equal (char-before beg1) ?:)
  8085. (equal (char-after (point-at-bol)) ?*)))
  8086. (prop (and (equal (char-before beg1) ?:)
  8087. (not (equal (char-after (point-at-bol)) ?*))))
  8088. (texp (equal (char-before beg) ?\\))
  8089. (link (equal (char-before beg) ?\[))
  8090. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8091. beg)
  8092. "#+"))
  8093. (startup (string-match "^#\\+STARTUP:.*"
  8094. (buffer-substring (point-at-bol) (point))))
  8095. (completion-ignore-case opt)
  8096. (type nil)
  8097. (tbl nil)
  8098. (table (cond
  8099. (opt
  8100. (setq type :opt)
  8101. (require 'org-exp)
  8102. (append
  8103. (mapcar
  8104. (lambda (x)
  8105. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8106. (cons (match-string 2 x) (match-string 1 x)))
  8107. (org-split-string (org-get-current-options) "\n"))
  8108. (mapcar 'list org-additional-option-like-keywords)))
  8109. (startup
  8110. (setq type :startup)
  8111. org-startup-options)
  8112. (link (append org-link-abbrev-alist-local
  8113. org-link-abbrev-alist))
  8114. (texp
  8115. (setq type :tex)
  8116. org-html-entities)
  8117. ((string-match "\\`\\*+[ \t]+\\'"
  8118. (buffer-substring (point-at-bol) beg))
  8119. (setq type :todo)
  8120. (mapcar 'list org-todo-keywords-1))
  8121. (searchhead
  8122. (setq type :searchhead)
  8123. (save-excursion
  8124. (goto-char (point-min))
  8125. (while (re-search-forward org-todo-line-regexp nil t)
  8126. (push (list
  8127. (org-make-org-heading-search-string
  8128. (match-string 3) t))
  8129. tbl)))
  8130. tbl)
  8131. (tag (setq type :tag beg beg1)
  8132. (or org-tag-alist (org-get-buffer-tags)))
  8133. (prop (setq type :prop beg beg1)
  8134. (mapcar 'list (org-buffer-property-keys nil t t)))
  8135. (t (progn
  8136. (call-interactively org-completion-fallback-command)
  8137. (throw 'exit nil)))))
  8138. (pattern (buffer-substring-no-properties beg end))
  8139. (completion (try-completion pattern table confirm)))
  8140. (cond ((eq completion t)
  8141. (if (not (assoc (upcase pattern) table))
  8142. (message "Already complete")
  8143. (if (and (equal type :opt)
  8144. (not (member (car (assoc (upcase pattern) table))
  8145. org-additional-option-like-keywords)))
  8146. (insert (substring (cdr (assoc (upcase pattern) table))
  8147. (length pattern)))
  8148. (if (memq type '(:tag :prop)) (insert ":")))))
  8149. ((null completion)
  8150. (message "Can't find completion for \"%s\"" pattern)
  8151. (ding))
  8152. ((not (string= pattern completion))
  8153. (delete-region beg end)
  8154. (if (string-match " +$" completion)
  8155. (setq completion (replace-match "" t t completion)))
  8156. (insert completion)
  8157. (if (get-buffer-window "*Completions*")
  8158. (delete-window (get-buffer-window "*Completions*")))
  8159. (if (assoc completion table)
  8160. (if (eq type :todo) (insert " ")
  8161. (if (memq type '(:tag :prop)) (insert ":"))))
  8162. (if (and (equal type :opt) (assoc completion table))
  8163. (message "%s" (substitute-command-keys
  8164. "Press \\[org-complete] again to insert example settings"))))
  8165. (t
  8166. (message "Making completion list...")
  8167. (let ((list (sort (all-completions pattern table confirm)
  8168. 'string<)))
  8169. (with-output-to-temp-buffer "*Completions*"
  8170. (condition-case nil
  8171. ;; Protection needed for XEmacs and emacs 21
  8172. (display-completion-list list pattern)
  8173. (error (display-completion-list list)))))
  8174. (message "Making completion list...%s" "done")))))))
  8175. ;;;; TODO, DEADLINE, Comments
  8176. (defun org-toggle-comment ()
  8177. "Change the COMMENT state of an entry."
  8178. (interactive)
  8179. (save-excursion
  8180. (org-back-to-heading)
  8181. (let (case-fold-search)
  8182. (if (looking-at (concat outline-regexp
  8183. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8184. (replace-match "" t t nil 1)
  8185. (if (looking-at outline-regexp)
  8186. (progn
  8187. (goto-char (match-end 0))
  8188. (insert org-comment-string " ")))))))
  8189. (defvar org-last-todo-state-is-todo nil
  8190. "This is non-nil when the last TODO state change led to a TODO state.
  8191. If the last change removed the TODO tag or switched to DONE, then
  8192. this is nil.")
  8193. (defvar org-setting-tags nil) ; dynamically skipped
  8194. (defun org-parse-local-options (string var)
  8195. "Parse STRING for startup setting relevant for variable VAR."
  8196. (let ((rtn (symbol-value var))
  8197. e opts)
  8198. (save-match-data
  8199. (if (or (not string) (not (string-match "\\S-" string)))
  8200. rtn
  8201. (setq opts (delq nil (mapcar (lambda (x)
  8202. (setq e (assoc x org-startup-options))
  8203. (if (eq (nth 1 e) var) e nil))
  8204. (org-split-string string "[ \t]+"))))
  8205. (if (not opts)
  8206. rtn
  8207. (setq rtn nil)
  8208. (while (setq e (pop opts))
  8209. (if (not (nth 3 e))
  8210. (setq rtn (nth 2 e))
  8211. (if (not (listp rtn)) (setq rtn nil))
  8212. (push (nth 2 e) rtn)))
  8213. rtn)))))
  8214. (defvar org-todo-setup-filter-hook nil
  8215. "Hook for functions that pre-filter todo specs.
  8216. Each function takes a todo spec and returns either `nil' or the spec
  8217. transformed into canonical form." )
  8218. (defvar org-todo-get-default-hook nil
  8219. "Hook for functions that get a default item for todo.
  8220. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8221. `nil' or a string to be used for the todo mark." )
  8222. (defvar org-agenda-headline-snapshot-before-repeat)
  8223. (defun org-todo (&optional arg)
  8224. "Change the TODO state of an item.
  8225. The state of an item is given by a keyword at the start of the heading,
  8226. like
  8227. *** TODO Write paper
  8228. *** DONE Call mom
  8229. The different keywords are specified in the variable `org-todo-keywords'.
  8230. By default the available states are \"TODO\" and \"DONE\".
  8231. So for this example: when the item starts with TODO, it is changed to DONE.
  8232. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8233. DONE are present, add TODO at the beginning of the heading.
  8234. With C-u prefix arg, use completion to determine the new state.
  8235. With numeric prefix arg, switch to that state.
  8236. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8237. With a tripple C-u prefix, circumvent any state blocking.
  8238. For calling through lisp, arg is also interpreted in the following way:
  8239. 'none -> empty state
  8240. \"\"(empty string) -> switch to empty state
  8241. 'done -> switch to DONE
  8242. 'nextset -> switch to the next set of keywords
  8243. 'previousset -> switch to the previous set of keywords
  8244. \"WAITING\" -> switch to the specified keyword, but only if it
  8245. really is a member of `org-todo-keywords'."
  8246. (interactive "P")
  8247. (if (equal arg '(16)) (setq arg 'nextset))
  8248. (let ((org-blocker-hook org-blocker-hook)
  8249. (case-fold-search nil))
  8250. (when (equal arg '(64))
  8251. (setq arg nil org-blocker-hook nil))
  8252. (when (and org-blocker-hook
  8253. (or org-inhibit-blocking
  8254. (org-entry-get nil "NOBLOCKING")))
  8255. (setq org-blocker-hook nil))
  8256. (save-excursion
  8257. (catch 'exit
  8258. (org-back-to-heading)
  8259. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8260. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8261. (looking-at " *"))
  8262. (let* ((match-data (match-data))
  8263. (startpos (point-at-bol))
  8264. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8265. (org-log-done org-log-done)
  8266. (org-log-repeat org-log-repeat)
  8267. (org-todo-log-states org-todo-log-states)
  8268. (this (match-string 1))
  8269. (hl-pos (match-beginning 0))
  8270. (head (org-get-todo-sequence-head this))
  8271. (ass (assoc head org-todo-kwd-alist))
  8272. (interpret (nth 1 ass))
  8273. (done-word (nth 3 ass))
  8274. (final-done-word (nth 4 ass))
  8275. (last-state (or this ""))
  8276. (completion-ignore-case t)
  8277. (member (member this org-todo-keywords-1))
  8278. (tail (cdr member))
  8279. (state (cond
  8280. ((and org-todo-key-trigger
  8281. (or (and (equal arg '(4))
  8282. (eq org-use-fast-todo-selection 'prefix))
  8283. (and (not arg) org-use-fast-todo-selection
  8284. (not (eq org-use-fast-todo-selection
  8285. 'prefix)))))
  8286. ;; Use fast selection
  8287. (org-fast-todo-selection))
  8288. ((and (equal arg '(4))
  8289. (or (not org-use-fast-todo-selection)
  8290. (not org-todo-key-trigger)))
  8291. ;; Read a state with completion
  8292. (org-ido-completing-read
  8293. "State: " (mapcar (lambda(x) (list x))
  8294. org-todo-keywords-1)
  8295. nil t))
  8296. ((eq arg 'right)
  8297. (if this
  8298. (if tail (car tail) nil)
  8299. (car org-todo-keywords-1)))
  8300. ((eq arg 'left)
  8301. (if (equal member org-todo-keywords-1)
  8302. nil
  8303. (if this
  8304. (nth (- (length org-todo-keywords-1)
  8305. (length tail) 2)
  8306. org-todo-keywords-1)
  8307. (org-last org-todo-keywords-1))))
  8308. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8309. (setq arg nil))) ; hack to fall back to cycling
  8310. (arg
  8311. ;; user or caller requests a specific state
  8312. (cond
  8313. ((equal arg "") nil)
  8314. ((eq arg 'none) nil)
  8315. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8316. ((eq arg 'nextset)
  8317. (or (car (cdr (member head org-todo-heads)))
  8318. (car org-todo-heads)))
  8319. ((eq arg 'previousset)
  8320. (let ((org-todo-heads (reverse org-todo-heads)))
  8321. (or (car (cdr (member head org-todo-heads)))
  8322. (car org-todo-heads))))
  8323. ((car (member arg org-todo-keywords-1)))
  8324. ((nth (1- (prefix-numeric-value arg))
  8325. org-todo-keywords-1))))
  8326. ((null member) (or head (car org-todo-keywords-1)))
  8327. ((equal this final-done-word) nil) ;; -> make empty
  8328. ((null tail) nil) ;; -> first entry
  8329. ((memq interpret '(type priority))
  8330. (if (eq this-command last-command)
  8331. (car tail)
  8332. (if (> (length tail) 0)
  8333. (or done-word (car org-done-keywords))
  8334. nil)))
  8335. (t
  8336. (car tail))))
  8337. (state (or
  8338. (run-hook-with-args-until-success
  8339. 'org-todo-get-default-hook state last-state)
  8340. state))
  8341. (next (if state (concat " " state " ") " "))
  8342. (change-plist (list :type 'todo-state-change :from this :to state
  8343. :position startpos))
  8344. dolog now-done-p)
  8345. (when org-blocker-hook
  8346. (setq org-last-todo-state-is-todo
  8347. (not (member this org-done-keywords)))
  8348. (unless (save-excursion
  8349. (save-match-data
  8350. (run-hook-with-args-until-failure
  8351. 'org-blocker-hook change-plist)))
  8352. (if (interactive-p)
  8353. (error "TODO state change from %s to %s blocked" this state)
  8354. ;; fail silently
  8355. (message "TODO state change from %s to %s blocked" this state)
  8356. (throw 'exit nil))))
  8357. (store-match-data match-data)
  8358. (replace-match next t t)
  8359. (unless (pos-visible-in-window-p hl-pos)
  8360. (message "TODO state changed to %s" (org-trim next)))
  8361. (unless head
  8362. (setq head (org-get-todo-sequence-head state)
  8363. ass (assoc head org-todo-kwd-alist)
  8364. interpret (nth 1 ass)
  8365. done-word (nth 3 ass)
  8366. final-done-word (nth 4 ass)))
  8367. (when (memq arg '(nextset previousset))
  8368. (message "Keyword-Set %d/%d: %s"
  8369. (- (length org-todo-sets) -1
  8370. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8371. (length org-todo-sets)
  8372. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8373. (setq org-last-todo-state-is-todo
  8374. (not (member state org-done-keywords)))
  8375. (setq now-done-p (and (member state org-done-keywords)
  8376. (not (member this org-done-keywords))))
  8377. (and logging (org-local-logging logging))
  8378. (when (and (or org-todo-log-states org-log-done)
  8379. (not (eq org-inhibit-logging t))
  8380. (not (memq arg '(nextset previousset))))
  8381. ;; we need to look at recording a time and note
  8382. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8383. (nth 2 (assoc this org-todo-log-states))))
  8384. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8385. (setq dolog 'time))
  8386. (when (and state
  8387. (member state org-not-done-keywords)
  8388. (not (member this org-not-done-keywords)))
  8389. ;; This is now a todo state and was not one before
  8390. ;; If there was a CLOSED time stamp, get rid of it.
  8391. (org-add-planning-info nil nil 'closed))
  8392. (when (and now-done-p org-log-done)
  8393. ;; It is now done, and it was not done before
  8394. (org-add-planning-info 'closed (org-current-time))
  8395. (if (and (not dolog) (eq 'note org-log-done))
  8396. (org-add-log-setup 'done state this 'findpos 'note)))
  8397. (when (and state dolog)
  8398. ;; This is a non-nil state, and we need to log it
  8399. (org-add-log-setup 'state state this 'findpos dolog)))
  8400. ;; Fixup tag positioning
  8401. (org-todo-trigger-tag-changes state)
  8402. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8403. (when org-provide-todo-statistics
  8404. (org-update-parent-todo-statistics))
  8405. (run-hooks 'org-after-todo-state-change-hook)
  8406. (if (and arg (not (member state org-done-keywords)))
  8407. (setq head (org-get-todo-sequence-head state)))
  8408. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8409. ;; Do we need to trigger a repeat?
  8410. (when now-done-p
  8411. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8412. ;; This is for the agenda, take a snapshot of the headline.
  8413. (save-match-data
  8414. (setq org-agenda-headline-snapshot-before-repeat
  8415. (org-get-heading))))
  8416. (org-auto-repeat-maybe state))
  8417. ;; Fixup cursor location if close to the keyword
  8418. (if (and (outline-on-heading-p)
  8419. (not (bolp))
  8420. (save-excursion (beginning-of-line 1)
  8421. (looking-at org-todo-line-regexp))
  8422. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8423. (progn
  8424. (goto-char (or (match-end 2) (match-end 1)))
  8425. (and (looking-at " ") (just-one-space))))
  8426. (when org-trigger-hook
  8427. (save-excursion
  8428. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8429. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8430. "Block turning an entry into a TODO, using the hierarchy.
  8431. This checks whether the current task should be blocked from state
  8432. changes. Such blocking occurs when:
  8433. 1. The task has children which are not all in a completed state.
  8434. 2. A task has a parent with the property :ORDERED:, and there
  8435. are siblings prior to the current task with incomplete
  8436. status.
  8437. 3. The parent of the task is blocked because it has siblings that should
  8438. be done first, or is child of a block grandparent TODO entry."
  8439. (catch 'dont-block
  8440. ;; If this is not a todo state change, or if this entry is already DONE,
  8441. ;; do not block
  8442. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8443. (member (plist-get change-plist :from)
  8444. (cons 'done org-done-keywords))
  8445. (member (plist-get change-plist :to)
  8446. (cons 'todo org-not-done-keywords)))
  8447. (throw 'dont-block t))
  8448. ;; If this task has children, and any are undone, it's blocked
  8449. (save-excursion
  8450. (org-back-to-heading t)
  8451. (let ((this-level (funcall outline-level)))
  8452. (outline-next-heading)
  8453. (let ((child-level (funcall outline-level)))
  8454. (while (and (not (eobp))
  8455. (> child-level this-level))
  8456. ;; this todo has children, check whether they are all
  8457. ;; completed
  8458. (if (and (not (org-entry-is-done-p))
  8459. (org-entry-is-todo-p))
  8460. (throw 'dont-block nil))
  8461. (outline-next-heading)
  8462. (setq child-level (funcall outline-level))))))
  8463. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8464. ;; any previous siblings are undone, it's blocked
  8465. (save-excursion
  8466. (org-back-to-heading t)
  8467. (let* ((pos (point))
  8468. (parent-pos (and (org-up-heading-safe) (point))))
  8469. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8470. (when (and (org-entry-get (point) "ORDERED")
  8471. (forward-line 1)
  8472. (re-search-forward org-not-done-heading-regexp pos t))
  8473. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8474. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8475. (while t
  8476. (goto-char parent-pos)
  8477. (if (not (looking-at org-not-done-heading-regexp))
  8478. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8479. (setq pos (point))
  8480. (setq parent-pos (and (org-up-heading-safe) (point)))
  8481. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8482. (when (and (org-entry-get (point) "ORDERED")
  8483. (forward-line 1)
  8484. (re-search-forward org-not-done-heading-regexp pos t))
  8485. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8486. (defcustom org-track-ordered-property-with-tag nil
  8487. "Should the ORDERED property also be shown as a tag?
  8488. The ORDERED property decides if an entry should require subtasks to be
  8489. completed in sequence. Since a property is not very visible, setting
  8490. this option means that toggling the ORDERED property with the command
  8491. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8492. not relevant for the behavior, but it makes things more visible.
  8493. Note that toggling the tag with tags commands will not change the property
  8494. and therefore not influence behavior!
  8495. This can be t, meaning the tag ORDERED should be used, It can also be a
  8496. string to select a different tag for this task."
  8497. :group 'org-todo
  8498. :type '(choice
  8499. (const :tag "No tracking" nil)
  8500. (const :tag "Track with ORDERED tag" t)
  8501. (string :tag "Use other tag")))
  8502. (defun org-toggle-ordered-property ()
  8503. "Toggle the ORDERED property of the current entry.
  8504. For better visibility, you can track the value of this property with a tag.
  8505. See variable `org-track-ordered-property-with-tag'."
  8506. (interactive)
  8507. (let* ((t1 org-track-ordered-property-with-tag)
  8508. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8509. (save-excursion
  8510. (org-back-to-heading)
  8511. (if (org-entry-get nil "ORDERED")
  8512. (progn
  8513. (org-delete-property "ORDERED")
  8514. (and tag (org-toggle-tag tag 'off))
  8515. (message "Subtasks can be completed in arbitrary order"))
  8516. (org-entry-put nil "ORDERED" "t")
  8517. (and tag (org-toggle-tag tag 'on))
  8518. (message "Subtasks must be completed in sequence")))))
  8519. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8520. (defun org-block-todo-from-checkboxes (change-plist)
  8521. "Block turning an entry into a TODO, using checkboxes.
  8522. This checks whether the current task should be blocked from state
  8523. changes because there are uncheckd boxes in this entry."
  8524. (catch 'dont-block
  8525. ;; If this is not a todo state change, or if this entry is already DONE,
  8526. ;; do not block
  8527. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8528. (member (plist-get change-plist :from)
  8529. (cons 'done org-done-keywords))
  8530. (member (plist-get change-plist :to)
  8531. (cons 'todo org-not-done-keywords)))
  8532. (throw 'dont-block t))
  8533. ;; If this task has checkboxes that are not checked, it's blocked
  8534. (save-excursion
  8535. (org-back-to-heading t)
  8536. (let ((beg (point)) end)
  8537. (outline-next-heading)
  8538. (setq end (point))
  8539. (goto-char beg)
  8540. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8541. end t)
  8542. (progn
  8543. (if (boundp 'org-blocked-by-checkboxes)
  8544. (setq org-blocked-by-checkboxes t))
  8545. (throw 'dont-block nil)))))
  8546. t)) ; do not block
  8547. (defvar org-entry-property-inherited-from) ;; defined below
  8548. (defun org-update-parent-todo-statistics ()
  8549. "Update any statistics cookie in the parent of the current headline.
  8550. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8551. the entire subtree and this will travel up the hierarchy and update
  8552. statistics everywhere."
  8553. (interactive)
  8554. (let* ((lim 0) prop
  8555. (recursive (or (not org-hierarchical-todo-statistics)
  8556. (string-match
  8557. "\\<recursive\\>"
  8558. (or (setq prop (org-entry-get
  8559. nil "COOKIE_DATA" 'inherit)) ""))))
  8560. (lim (or (and prop (marker-position
  8561. org-entry-property-inherited-from))
  8562. lim))
  8563. (first t)
  8564. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8565. level ltoggle l1
  8566. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8567. (catch 'exit
  8568. (save-excursion
  8569. (beginning-of-line 1)
  8570. (if (org-at-heading-p)
  8571. (setq ltoggle (funcall outline-level))
  8572. (error "This should not happen"))
  8573. (while (and (setq level (org-up-heading-safe))
  8574. (or recursive first)
  8575. (>= (point) lim))
  8576. (setq first nil)
  8577. (unless (and level
  8578. (not (string-match
  8579. "\\<checkbox\\>"
  8580. (downcase
  8581. (or (org-entry-get
  8582. nil "COOKIE_DATA")
  8583. "")))))
  8584. (throw 'exit nil))
  8585. (while (re-search-forward box-re (point-at-eol) t)
  8586. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8587. (setq is-percent (match-end 2))
  8588. (save-match-data
  8589. (unless (outline-next-heading) (throw 'exit nil))
  8590. (while (and (looking-at org-complex-heading-regexp)
  8591. (> (setq l1 (length (match-string 1))) level))
  8592. (setq kwd (and (or recursive (= l1 ltoggle))
  8593. (match-string 2)))
  8594. (if (or (eq org-provide-todo-statistics 'all-headlines)
  8595. (and (listp org-provide-todo-statistics)
  8596. (or (member kwd org-provide-todo-statistics)
  8597. (member kwd org-done-keywords))))
  8598. (setq cnt-all (1+ cnt-all))
  8599. (if (eq org-provide-todo-statistics t)
  8600. (and kwd (setq cnt-all (1+ cnt-all)))))
  8601. (and (member kwd org-done-keywords)
  8602. (setq cnt-done (1+ cnt-done)))
  8603. (outline-next-heading)))
  8604. (replace-match
  8605. (if is-percent
  8606. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8607. (format "[%d/%d]" cnt-done cnt-all)))))
  8608. (when cookie-present
  8609. (run-hook-with-args 'org-after-todo-statistics-hook
  8610. cnt-done (- cnt-all cnt-done)))))
  8611. (run-hooks 'org-todo-statistics-hook)))
  8612. (defvar org-after-todo-statistics-hook nil
  8613. "Hook that is called after a TODO statistics cookie has been updated.
  8614. Each function is called with two arguments: the number of not-done entries
  8615. and the number of done entries.
  8616. For example, the following function, when added to this hook, will switch
  8617. an entry to DONE when all children are done, and back to TODO when new
  8618. entries are set to a TODO status. Note that this hook is only called
  8619. when there is a statistics cookie in the headline!
  8620. (defun org-summary-todo (n-done n-not-done)
  8621. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8622. (let (org-log-done org-log-states) ; turn off logging
  8623. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8624. ")
  8625. (defvar org-todo-statistics-hook nil
  8626. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8627. This hook runs even if there is no statisics cookie present, in which case
  8628. `org-after-todo-statistics-hook' would not run.")
  8629. (defun org-todo-trigger-tag-changes (state)
  8630. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8631. (let ((l org-todo-state-tags-triggers)
  8632. changes)
  8633. (when (or (not state) (equal state ""))
  8634. (setq changes (append changes (cdr (assoc "" l)))))
  8635. (when (and (stringp state) (> (length state) 0))
  8636. (setq changes (append changes (cdr (assoc state l)))))
  8637. (when (member state org-not-done-keywords)
  8638. (setq changes (append changes (cdr (assoc 'todo l)))))
  8639. (when (member state org-done-keywords)
  8640. (setq changes (append changes (cdr (assoc 'done l)))))
  8641. (dolist (c changes)
  8642. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8643. (defun org-local-logging (value)
  8644. "Get logging settings from a property VALUE."
  8645. (let* (words w a)
  8646. ;; directly set the variables, they are already local.
  8647. (setq org-log-done nil
  8648. org-log-repeat nil
  8649. org-todo-log-states nil)
  8650. (setq words (org-split-string value))
  8651. (while (setq w (pop words))
  8652. (cond
  8653. ((setq a (assoc w org-startup-options))
  8654. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8655. (set (nth 1 a) (nth 2 a))))
  8656. ((setq a (org-extract-log-state-settings w))
  8657. (and (member (car a) org-todo-keywords-1)
  8658. (push a org-todo-log-states)))))))
  8659. (defun org-get-todo-sequence-head (kwd)
  8660. "Return the head of the TODO sequence to which KWD belongs.
  8661. If KWD is not set, check if there is a text property remembering the
  8662. right sequence."
  8663. (let (p)
  8664. (cond
  8665. ((not kwd)
  8666. (or (get-text-property (point-at-bol) 'org-todo-head)
  8667. (progn
  8668. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8669. nil (point-at-eol)))
  8670. (get-text-property p 'org-todo-head))))
  8671. ((not (member kwd org-todo-keywords-1))
  8672. (car org-todo-keywords-1))
  8673. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8674. (defun org-fast-todo-selection ()
  8675. "Fast TODO keyword selection with single keys.
  8676. Returns the new TODO keyword, or nil if no state change should occur."
  8677. (let* ((fulltable org-todo-key-alist)
  8678. (done-keywords org-done-keywords) ;; needed for the faces.
  8679. (maxlen (apply 'max (mapcar
  8680. (lambda (x)
  8681. (if (stringp (car x)) (string-width (car x)) 0))
  8682. fulltable)))
  8683. (expert nil)
  8684. (fwidth (+ maxlen 3 1 3))
  8685. (ncol (/ (- (window-width) 4) fwidth))
  8686. tg cnt e c tbl
  8687. groups ingroup)
  8688. (save-excursion
  8689. (save-window-excursion
  8690. (if expert
  8691. (set-buffer (get-buffer-create " *Org todo*"))
  8692. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8693. (erase-buffer)
  8694. (org-set-local 'org-done-keywords done-keywords)
  8695. (setq tbl fulltable cnt 0)
  8696. (while (setq e (pop tbl))
  8697. (cond
  8698. ((equal e '(:startgroup))
  8699. (push '() groups) (setq ingroup t)
  8700. (when (not (= cnt 0))
  8701. (setq cnt 0)
  8702. (insert "\n"))
  8703. (insert "{ "))
  8704. ((equal e '(:endgroup))
  8705. (setq ingroup nil cnt 0)
  8706. (insert "}\n"))
  8707. ((equal e '(:newline))
  8708. (when (not (= cnt 0))
  8709. (setq cnt 0)
  8710. (insert "\n")
  8711. (setq e (car tbl))
  8712. (while (equal (car tbl) '(:newline))
  8713. (insert "\n")
  8714. (setq tbl (cdr tbl)))))
  8715. (t
  8716. (setq tg (car e) c (cdr e))
  8717. (if ingroup (push tg (car groups)))
  8718. (setq tg (org-add-props tg nil 'face
  8719. (org-get-todo-face tg)))
  8720. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8721. (insert "[" c "] " tg (make-string
  8722. (- fwidth 4 (length tg)) ?\ ))
  8723. (when (= (setq cnt (1+ cnt)) ncol)
  8724. (insert "\n")
  8725. (if ingroup (insert " "))
  8726. (setq cnt 0)))))
  8727. (insert "\n")
  8728. (goto-char (point-min))
  8729. (if (not expert) (org-fit-window-to-buffer))
  8730. (message "[a-z..]:Set [SPC]:clear")
  8731. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8732. (cond
  8733. ((or (= c ?\C-g)
  8734. (and (= c ?q) (not (rassoc c fulltable))))
  8735. (setq quit-flag t))
  8736. ((= c ?\ ) nil)
  8737. ((setq e (rassoc c fulltable) tg (car e))
  8738. tg)
  8739. (t (setq quit-flag t)))))))
  8740. (defun org-entry-is-todo-p ()
  8741. (member (org-get-todo-state) org-not-done-keywords))
  8742. (defun org-entry-is-done-p ()
  8743. (member (org-get-todo-state) org-done-keywords))
  8744. (defun org-get-todo-state ()
  8745. (save-excursion
  8746. (org-back-to-heading t)
  8747. (and (looking-at org-todo-line-regexp)
  8748. (match-end 2)
  8749. (match-string 2))))
  8750. (defun org-at-date-range-p (&optional inactive-ok)
  8751. "Is the cursor inside a date range?"
  8752. (interactive)
  8753. (save-excursion
  8754. (catch 'exit
  8755. (let ((pos (point)))
  8756. (skip-chars-backward "^[<\r\n")
  8757. (skip-chars-backward "<[")
  8758. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8759. (>= (match-end 0) pos)
  8760. (throw 'exit t))
  8761. (skip-chars-backward "^<[\r\n")
  8762. (skip-chars-backward "<[")
  8763. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8764. (>= (match-end 0) pos)
  8765. (throw 'exit t)))
  8766. nil)))
  8767. (defun org-get-repeat ()
  8768. "Check if there is a deadline/schedule with repeater in this entry."
  8769. (save-match-data
  8770. (save-excursion
  8771. (org-back-to-heading t)
  8772. (if (re-search-forward
  8773. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8774. (match-string 1)))))
  8775. (defvar org-last-changed-timestamp)
  8776. (defvar org-last-inserted-timestamp)
  8777. (defvar org-log-post-message)
  8778. (defvar org-log-note-purpose)
  8779. (defvar org-log-note-how)
  8780. (defvar org-log-note-extra)
  8781. (defun org-auto-repeat-maybe (done-word)
  8782. "Check if the current headline contains a repeated deadline/schedule.
  8783. If yes, set TODO state back to what it was and change the base date
  8784. of repeating deadline/scheduled time stamps to new date.
  8785. This function is run automatically after each state change to a DONE state."
  8786. ;; last-state is dynamically scoped into this function
  8787. (let* ((repeat (org-get-repeat))
  8788. (aa (assoc last-state org-todo-kwd-alist))
  8789. (interpret (nth 1 aa))
  8790. (head (nth 2 aa))
  8791. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8792. (msg "Entry repeats: ")
  8793. (org-log-done nil)
  8794. (org-todo-log-states nil)
  8795. (nshiftmax 10) (nshift 0)
  8796. re type n what ts time)
  8797. (when repeat
  8798. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8799. (org-todo (if (eq interpret 'type) last-state head))
  8800. (org-entry-put nil "LAST_REPEAT" (format-time-string
  8801. (org-time-stamp-format t t)))
  8802. (when org-log-repeat
  8803. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8804. (memq 'org-add-log-note post-command-hook))
  8805. ;; OK, we are already setup for some record
  8806. (if (eq org-log-repeat 'note)
  8807. ;; make sure we take a note, not only a time stamp
  8808. (setq org-log-note-how 'note))
  8809. ;; Set up for taking a record
  8810. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8811. last-state
  8812. 'findpos org-log-repeat)))
  8813. (org-back-to-heading t)
  8814. (org-add-planning-info nil nil 'closed)
  8815. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8816. org-deadline-time-regexp "\\)\\|\\("
  8817. org-ts-regexp "\\)"))
  8818. (while (re-search-forward
  8819. re (save-excursion (outline-next-heading) (point)) t)
  8820. (setq type (if (match-end 1) org-scheduled-string
  8821. (if (match-end 3) org-deadline-string "Plain:"))
  8822. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8823. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8824. (setq n (string-to-number (match-string 2 ts))
  8825. what (match-string 3 ts))
  8826. (if (equal what "w") (setq n (* n 7) what "d"))
  8827. ;; Preparation, see if we need to modify the start date for the change
  8828. (when (match-end 1)
  8829. (setq time (save-match-data (org-time-string-to-time ts)))
  8830. (cond
  8831. ((equal (match-string 1 ts) ".")
  8832. ;; Shift starting date to today
  8833. (org-timestamp-change
  8834. (- (time-to-days (current-time)) (time-to-days time))
  8835. 'day))
  8836. ((equal (match-string 1 ts) "+")
  8837. (while (or (= nshift 0)
  8838. (<= (time-to-days time) (time-to-days (current-time))))
  8839. (when (= (incf nshift) nshiftmax)
  8840. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8841. (error "Abort")))
  8842. (org-timestamp-change n (cdr (assoc what whata)))
  8843. (org-at-timestamp-p t)
  8844. (setq ts (match-string 1))
  8845. (setq time (save-match-data (org-time-string-to-time ts))))
  8846. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8847. ;; rematch, so that we have everything in place for the real shift
  8848. (org-at-timestamp-p t)
  8849. (setq ts (match-string 1))
  8850. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8851. (org-timestamp-change n (cdr (assoc what whata)))
  8852. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8853. (setq org-log-post-message msg)
  8854. (message "%s" msg))))
  8855. (defun org-show-todo-tree (arg)
  8856. "Make a compact tree which shows all headlines marked with TODO.
  8857. The tree will show the lines where the regexp matches, and all higher
  8858. headlines above the match.
  8859. With a \\[universal-argument] prefix, prompt for a regexp to match.
  8860. With a numeric prefix N, construct a sparse tree for the Nth element
  8861. of `org-todo-keywords-1'."
  8862. (interactive "P")
  8863. (let ((case-fold-search nil)
  8864. (kwd-re
  8865. (cond ((null arg) org-not-done-regexp)
  8866. ((equal arg '(4))
  8867. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8868. (mapcar 'list org-todo-keywords-1))))
  8869. (concat "\\("
  8870. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8871. "\\)\\>")))
  8872. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8873. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8874. org-todo-keywords-1)))
  8875. (t (error "Invalid prefix argument: %s" arg)))))
  8876. (message "%d TODO entries found"
  8877. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8878. (defun org-deadline (&optional remove time)
  8879. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8880. With argument REMOVE, remove any deadline from the item.
  8881. When TIME is set, it should be an internal time specification, and the
  8882. scheduling will use the corresponding date."
  8883. (interactive "P")
  8884. (if remove
  8885. (progn
  8886. (org-remove-timestamp-with-keyword org-deadline-string)
  8887. (message "Item no longer has a deadline."))
  8888. (if (org-get-repeat)
  8889. (error "Cannot change deadline on task with repeater, please do that by hand")
  8890. (org-add-planning-info 'deadline time 'closed)
  8891. (message "Deadline on %s" org-last-inserted-timestamp))))
  8892. (defun org-schedule (&optional remove time)
  8893. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8894. With argument REMOVE, remove any scheduling date from the item.
  8895. When TIME is set, it should be an internal time specification, and the
  8896. scheduling will use the corresponding date."
  8897. (interactive "P")
  8898. (if remove
  8899. (progn
  8900. (org-remove-timestamp-with-keyword org-scheduled-string)
  8901. (message "Item is no longer scheduled."))
  8902. (if (org-get-repeat)
  8903. (error "Cannot reschedule task with repeater, please do that by hand")
  8904. (org-add-planning-info 'scheduled time 'closed)
  8905. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8906. (defun org-get-scheduled-time (pom &optional inherit)
  8907. "Get the scheduled time as a time tuple, of a format suitable
  8908. for calling org-schedule with, or if there is no scheduling,
  8909. returns nil."
  8910. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8911. (when time
  8912. (apply 'encode-time (org-parse-time-string time)))))
  8913. (defun org-get-deadline-time (pom &optional inherit)
  8914. "Get the deadine as a time tuple, of a format suitable for
  8915. calling org-deadlin with, or if there is no scheduling, returns
  8916. nil."
  8917. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8918. (when time
  8919. (apply 'encode-time (org-parse-time-string time)))))
  8920. (defun org-remove-timestamp-with-keyword (keyword)
  8921. "Remove all time stamps with KEYWORD in the current entry."
  8922. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8923. beg)
  8924. (save-excursion
  8925. (org-back-to-heading t)
  8926. (setq beg (point))
  8927. (org-end-of-subtree t t)
  8928. (while (re-search-backward re beg t)
  8929. (replace-match "")
  8930. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8931. (equal (char-before) ?\ ))
  8932. (backward-delete-char 1)
  8933. (if (string-match "^[ \t]*$" (buffer-substring
  8934. (point-at-bol) (point-at-eol)))
  8935. (delete-region (point-at-bol)
  8936. (min (point-max) (1+ (point-at-eol))))))))))
  8937. (defun org-add-planning-info (what &optional time &rest remove)
  8938. "Insert new timestamp with keyword in the line directly after the headline.
  8939. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8940. If non is given, the user is prompted for a date.
  8941. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8942. be removed."
  8943. (interactive)
  8944. (let (org-time-was-given org-end-time-was-given ts
  8945. end default-time default-input)
  8946. (catch 'exit
  8947. (when (and (not time) (memq what '(scheduled deadline)))
  8948. ;; Try to get a default date/time from existing timestamp
  8949. (save-excursion
  8950. (org-back-to-heading t)
  8951. (setq end (save-excursion (outline-next-heading) (point)))
  8952. (when (re-search-forward (if (eq what 'scheduled)
  8953. org-scheduled-time-regexp
  8954. org-deadline-time-regexp)
  8955. end t)
  8956. (setq ts (match-string 1)
  8957. default-time
  8958. (apply 'encode-time (org-parse-time-string ts))
  8959. default-input (and ts (org-get-compact-tod ts))))))
  8960. (when what
  8961. ;; If necessary, get the time from the user
  8962. (setq time (or time (org-read-date nil 'to-time nil nil
  8963. default-time default-input))))
  8964. (when (and org-insert-labeled-timestamps-at-point
  8965. (member what '(scheduled deadline)))
  8966. (insert
  8967. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8968. (org-insert-time-stamp time org-time-was-given
  8969. nil nil nil (list org-end-time-was-given))
  8970. (setq what nil))
  8971. (save-excursion
  8972. (save-restriction
  8973. (let (col list elt ts buffer-invisibility-spec)
  8974. (org-back-to-heading t)
  8975. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8976. (goto-char (match-end 1))
  8977. (setq col (current-column))
  8978. (goto-char (match-end 0))
  8979. (if (eobp) (insert "\n") (forward-char 1))
  8980. (when (and (not what)
  8981. (not (looking-at
  8982. (concat "[ \t]*"
  8983. org-keyword-time-not-clock-regexp))))
  8984. ;; Nothing to add, nothing to remove...... :-)
  8985. (throw 'exit nil))
  8986. (if (and (not (looking-at outline-regexp))
  8987. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8988. "[^\r\n]*"))
  8989. (not (equal (match-string 1) org-clock-string)))
  8990. (narrow-to-region (match-beginning 0) (match-end 0))
  8991. (insert-before-markers "\n")
  8992. (backward-char 1)
  8993. (narrow-to-region (point) (point))
  8994. (and org-adapt-indentation (org-indent-to-column col)))
  8995. ;; Check if we have to remove something.
  8996. (setq list (cons what remove))
  8997. (while list
  8998. (setq elt (pop list))
  8999. (goto-char (point-min))
  9000. (when (or (and (eq elt 'scheduled)
  9001. (re-search-forward org-scheduled-time-regexp nil t))
  9002. (and (eq elt 'deadline)
  9003. (re-search-forward org-deadline-time-regexp nil t))
  9004. (and (eq elt 'closed)
  9005. (re-search-forward org-closed-time-regexp nil t)))
  9006. (replace-match "")
  9007. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9008. (if (looking-at " +") (replace-match ""))))
  9009. (goto-char (point-max))
  9010. (when what
  9011. (insert
  9012. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9013. (cond ((eq what 'scheduled) org-scheduled-string)
  9014. ((eq what 'deadline) org-deadline-string)
  9015. ((eq what 'closed) org-closed-string))
  9016. " ")
  9017. (setq ts (org-insert-time-stamp
  9018. time
  9019. (or org-time-was-given
  9020. (and (eq what 'closed) org-log-done-with-time))
  9021. (eq what 'closed)
  9022. nil nil (list org-end-time-was-given)))
  9023. (end-of-line 1))
  9024. (goto-char (point-min))
  9025. (widen)
  9026. (if (and (looking-at "[ \t]+\n")
  9027. (equal (char-before) ?\n))
  9028. (delete-region (1- (point)) (point-at-eol)))
  9029. ts))))))
  9030. (defvar org-log-note-marker (make-marker))
  9031. (defvar org-log-note-purpose nil)
  9032. (defvar org-log-note-state nil)
  9033. (defvar org-log-note-previous-state nil)
  9034. (defvar org-log-note-how nil)
  9035. (defvar org-log-note-extra nil)
  9036. (defvar org-log-note-window-configuration nil)
  9037. (defvar org-log-note-return-to (make-marker))
  9038. (defvar org-log-post-message nil
  9039. "Message to be displayed after a log note has been stored.
  9040. The auto-repeater uses this.")
  9041. (defun org-add-note ()
  9042. "Add a note to the current entry.
  9043. This is done in the same way as adding a state change note."
  9044. (interactive)
  9045. (org-add-log-setup 'note nil nil 'findpos nil))
  9046. (defvar org-property-end-re)
  9047. (defun org-add-log-setup (&optional purpose state prev-state
  9048. findpos how &optional extra)
  9049. "Set up the post command hook to take a note.
  9050. If this is about to TODO state change, the new state is expected in STATE.
  9051. When FINDPOS is non-nil, find the correct position for the note in
  9052. the current entry. If not, assume that it can be inserted at point.
  9053. HOW is an indicator what kind of note should be created.
  9054. EXTRA is additional text that will be inserted into the notes buffer."
  9055. (let* ((org-log-into-drawer (org-log-into-drawer))
  9056. (drawer (cond ((stringp org-log-into-drawer)
  9057. org-log-into-drawer)
  9058. (org-log-into-drawer "LOGBOOK")
  9059. (t nil))))
  9060. (save-restriction
  9061. (save-excursion
  9062. (when findpos
  9063. (org-back-to-heading t)
  9064. (narrow-to-region (point) (save-excursion
  9065. (outline-next-heading) (point)))
  9066. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9067. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9068. "[^\r\n]*\\)?"))
  9069. (goto-char (match-end 0))
  9070. (cond
  9071. (drawer
  9072. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9073. nil t)
  9074. (progn
  9075. (goto-char (match-end 0))
  9076. (or org-log-states-order-reversed
  9077. (and (re-search-forward org-property-end-re nil t)
  9078. (goto-char (1- (match-beginning 0))))))
  9079. (insert "\n:" drawer ":\n:END:")
  9080. (beginning-of-line 0)
  9081. (org-indent-line-function)
  9082. (beginning-of-line 2)
  9083. (org-indent-line-function)
  9084. (end-of-line 0)))
  9085. ((and org-log-state-notes-insert-after-drawers
  9086. (save-excursion
  9087. (forward-line) (looking-at org-drawer-regexp)))
  9088. (forward-line)
  9089. (while (looking-at org-drawer-regexp)
  9090. (goto-char (match-end 0))
  9091. (re-search-forward org-property-end-re (point-max) t)
  9092. (forward-line))
  9093. (forward-line -1)))
  9094. (unless org-log-states-order-reversed
  9095. (and (= (char-after) ?\n) (forward-char 1))
  9096. (org-skip-over-state-notes)
  9097. (skip-chars-backward " \t\n\r")))
  9098. (move-marker org-log-note-marker (point))
  9099. (setq org-log-note-purpose purpose
  9100. org-log-note-state state
  9101. org-log-note-previous-state prev-state
  9102. org-log-note-how how
  9103. org-log-note-extra extra)
  9104. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9105. (defun org-skip-over-state-notes ()
  9106. "Skip past the list of State notes in an entry."
  9107. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9108. (while (looking-at "[ \t]*- State")
  9109. (condition-case nil
  9110. (org-next-item)
  9111. (error (org-end-of-item)))))
  9112. (defun org-add-log-note (&optional purpose)
  9113. "Pop up a window for taking a note, and add this note later at point."
  9114. (remove-hook 'post-command-hook 'org-add-log-note)
  9115. (setq org-log-note-window-configuration (current-window-configuration))
  9116. (delete-other-windows)
  9117. (move-marker org-log-note-return-to (point))
  9118. (switch-to-buffer (marker-buffer org-log-note-marker))
  9119. (goto-char org-log-note-marker)
  9120. (org-switch-to-buffer-other-window "*Org Note*")
  9121. (erase-buffer)
  9122. (if (memq org-log-note-how '(time state))
  9123. (let (current-prefix-arg) (org-store-log-note))
  9124. (let ((org-inhibit-startup t)) (org-mode))
  9125. (insert (format "# Insert note for %s.
  9126. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9127. (cond
  9128. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9129. ((eq org-log-note-purpose 'done) "closed todo item")
  9130. ((eq org-log-note-purpose 'state)
  9131. (format "state change from \"%s\" to \"%s\""
  9132. (or org-log-note-previous-state "")
  9133. (or org-log-note-state "")))
  9134. ((eq org-log-note-purpose 'note)
  9135. "this entry")
  9136. (t (error "This should not happen")))))
  9137. (if org-log-note-extra (insert org-log-note-extra))
  9138. (org-set-local 'org-finish-function 'org-store-log-note)))
  9139. (defvar org-note-abort nil) ; dynamically scoped
  9140. (defun org-store-log-note ()
  9141. "Finish taking a log note, and insert it to where it belongs."
  9142. (let ((txt (buffer-string))
  9143. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9144. lines ind)
  9145. (kill-buffer (current-buffer))
  9146. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9147. (setq txt (replace-match "" t t txt)))
  9148. (if (string-match "\\s-+\\'" txt)
  9149. (setq txt (replace-match "" t t txt)))
  9150. (setq lines (org-split-string txt "\n"))
  9151. (when (and note (string-match "\\S-" note))
  9152. (setq note
  9153. (org-replace-escapes
  9154. note
  9155. (list (cons "%u" (user-login-name))
  9156. (cons "%U" user-full-name)
  9157. (cons "%t" (format-time-string
  9158. (org-time-stamp-format 'long 'inactive)
  9159. (current-time)))
  9160. (cons "%s" (if org-log-note-state
  9161. (concat "\"" org-log-note-state "\"")
  9162. ""))
  9163. (cons "%S" (if org-log-note-previous-state
  9164. (concat "\"" org-log-note-previous-state "\"")
  9165. "\"\"")))))
  9166. (if lines (setq note (concat note " \\\\")))
  9167. (push note lines))
  9168. (when (or current-prefix-arg org-note-abort)
  9169. (when org-log-into-drawer
  9170. (org-remove-empty-drawer-at
  9171. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9172. org-log-note-marker))
  9173. (setq lines nil))
  9174. (when lines
  9175. (save-excursion
  9176. (set-buffer (marker-buffer org-log-note-marker))
  9177. (save-excursion
  9178. (goto-char org-log-note-marker)
  9179. (move-marker org-log-note-marker nil)
  9180. (end-of-line 1)
  9181. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9182. (insert "- " (pop lines))
  9183. (org-indent-line-function)
  9184. (beginning-of-line 1)
  9185. (looking-at "[ \t]*")
  9186. (setq ind (concat (match-string 0) " "))
  9187. (end-of-line 1)
  9188. (while lines (insert "\n" ind (pop lines)))
  9189. (message "Note stored")
  9190. (org-back-to-heading t)
  9191. (org-cycle-hide-drawers 'children)))))
  9192. (set-window-configuration org-log-note-window-configuration)
  9193. (with-current-buffer (marker-buffer org-log-note-return-to)
  9194. (goto-char org-log-note-return-to))
  9195. (move-marker org-log-note-return-to nil)
  9196. (and org-log-post-message (message "%s" org-log-post-message)))
  9197. (defun org-remove-empty-drawer-at (drawer pos)
  9198. "Remove an emptyr DARWER drawer at position POS.
  9199. POS may also be a marker."
  9200. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9201. (save-excursion
  9202. (save-restriction
  9203. (widen)
  9204. (goto-char pos)
  9205. (if (org-in-regexp
  9206. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9207. (replace-match ""))))))
  9208. (defun org-sparse-tree (&optional arg)
  9209. "Create a sparse tree, prompt for the details.
  9210. This command can create sparse trees. You first need to select the type
  9211. of match used to create the tree:
  9212. t Show entries with a specific TODO keyword.
  9213. m Show entries selected by a tags/property match.
  9214. p Enter a property name and its value (both with completion on existing
  9215. names/values) and show entries with that property.
  9216. r Show entries matching a regular expression.
  9217. d Show deadlines due within `org-deadline-warning-days'.
  9218. b Show deadlines and scheduled items before a date.
  9219. a Show deadlines and scheduled items after a date."
  9220. (interactive "P")
  9221. (let (ans kwd value)
  9222. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9223. (setq ans (read-char-exclusive))
  9224. (cond
  9225. ((equal ans ?d)
  9226. (call-interactively 'org-check-deadlines))
  9227. ((equal ans ?b)
  9228. (call-interactively 'org-check-before-date))
  9229. ((equal ans ?a)
  9230. (call-interactively 'org-check-after-date))
  9231. ((equal ans ?t)
  9232. (org-show-todo-tree '(4)))
  9233. ((member ans '(?T ?m))
  9234. (call-interactively 'org-match-sparse-tree))
  9235. ((member ans '(?p ?P))
  9236. (setq kwd (org-ido-completing-read "Property: "
  9237. (mapcar 'list (org-buffer-property-keys))))
  9238. (setq value (org-ido-completing-read "Value: "
  9239. (mapcar 'list (org-property-values kwd))))
  9240. (unless (string-match "\\`{.*}\\'" value)
  9241. (setq value (concat "\"" value "\"")))
  9242. (org-match-sparse-tree arg (concat kwd "=" value)))
  9243. ((member ans '(?r ?R ?/))
  9244. (call-interactively 'org-occur))
  9245. (t (error "No such sparse tree command \"%c\"" ans)))))
  9246. (defvar org-occur-highlights nil
  9247. "List of overlays used for occur matches.")
  9248. (make-variable-buffer-local 'org-occur-highlights)
  9249. (defvar org-occur-parameters nil
  9250. "Parameters of the active org-occur calls.
  9251. This is a list, each call to org-occur pushes as cons cell,
  9252. containing the regular expression and the callback, onto the list.
  9253. The list can contain several entries if `org-occur' has been called
  9254. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9255. will only contain one set of parameters. When the highlights are
  9256. removed (for example with `C-c C-c', or with the next edit (depending
  9257. on `org-remove-highlights-with-change'), this variable is emptied
  9258. as well.")
  9259. (make-variable-buffer-local 'org-occur-parameters)
  9260. (defun org-occur (regexp &optional keep-previous callback)
  9261. "Make a compact tree which shows all matches of REGEXP.
  9262. The tree will show the lines where the regexp matches, and all higher
  9263. headlines above the match. It will also show the heading after the match,
  9264. to make sure editing the matching entry is easy.
  9265. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9266. call to `org-occur' will be kept, to allow stacking of calls to this
  9267. command.
  9268. If CALLBACK is non-nil, it is a function which is called to confirm
  9269. that the match should indeed be shown."
  9270. (interactive "sRegexp: \nP")
  9271. (when (equal regexp "")
  9272. (error "Regexp cannot be empty"))
  9273. (unless keep-previous
  9274. (org-remove-occur-highlights nil nil t))
  9275. (push (cons regexp callback) org-occur-parameters)
  9276. (let ((cnt 0))
  9277. (save-excursion
  9278. (goto-char (point-min))
  9279. (if (or (not keep-previous) ; do not want to keep
  9280. (not org-occur-highlights)) ; no previous matches
  9281. ;; hide everything
  9282. (org-overview))
  9283. (while (re-search-forward regexp nil t)
  9284. (when (or (not callback)
  9285. (save-match-data (funcall callback)))
  9286. (setq cnt (1+ cnt))
  9287. (when org-highlight-sparse-tree-matches
  9288. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9289. (org-show-context 'occur-tree))))
  9290. (when org-remove-highlights-with-change
  9291. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9292. nil 'local))
  9293. (unless org-sparse-tree-open-archived-trees
  9294. (org-hide-archived-subtrees (point-min) (point-max)))
  9295. (run-hooks 'org-occur-hook)
  9296. (if (interactive-p)
  9297. (message "%d match(es) for regexp %s" cnt regexp))
  9298. cnt))
  9299. (defun org-show-context (&optional key)
  9300. "Make sure point and context and visible.
  9301. How much context is shown depends upon the variables
  9302. `org-show-hierarchy-above', `org-show-following-heading'. and
  9303. `org-show-siblings'."
  9304. (let ((heading-p (org-on-heading-p t))
  9305. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9306. (following-p (org-get-alist-option org-show-following-heading key))
  9307. (entry-p (org-get-alist-option org-show-entry-below key))
  9308. (siblings-p (org-get-alist-option org-show-siblings key)))
  9309. (catch 'exit
  9310. ;; Show heading or entry text
  9311. (if (and heading-p (not entry-p))
  9312. (org-flag-heading nil) ; only show the heading
  9313. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9314. (org-show-hidden-entry))) ; show entire entry
  9315. (when following-p
  9316. ;; Show next sibling, or heading below text
  9317. (save-excursion
  9318. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9319. (org-flag-heading nil))))
  9320. (when siblings-p (org-show-siblings))
  9321. (when hierarchy-p
  9322. ;; show all higher headings, possibly with siblings
  9323. (save-excursion
  9324. (while (and (condition-case nil
  9325. (progn (org-up-heading-all 1) t)
  9326. (error nil))
  9327. (not (bobp)))
  9328. (org-flag-heading nil)
  9329. (when siblings-p (org-show-siblings))))))))
  9330. (defun org-reveal (&optional siblings)
  9331. "Show current entry, hierarchy above it, and the following headline.
  9332. This can be used to show a consistent set of context around locations
  9333. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9334. not t for the search context.
  9335. With optional argument SIBLINGS, on each level of the hierarchy all
  9336. siblings are shown. This repairs the tree structure to what it would
  9337. look like when opened with hierarchical calls to `org-cycle'."
  9338. (interactive "P")
  9339. (let ((org-show-hierarchy-above t)
  9340. (org-show-following-heading t)
  9341. (org-show-siblings (if siblings t org-show-siblings)))
  9342. (org-show-context nil)))
  9343. (defun org-highlight-new-match (beg end)
  9344. "Highlight from BEG to END and mark the highlight is an occur headline."
  9345. (let ((ov (org-make-overlay beg end)))
  9346. (org-overlay-put ov 'face 'secondary-selection)
  9347. (push ov org-occur-highlights)))
  9348. (defun org-remove-occur-highlights (&optional beg end noremove)
  9349. "Remove the occur highlights from the buffer.
  9350. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9351. from the `before-change-functions' in the current buffer."
  9352. (interactive)
  9353. (unless org-inhibit-highlight-removal
  9354. (mapc 'org-delete-overlay org-occur-highlights)
  9355. (setq org-occur-highlights nil)
  9356. (setq org-occur-parameters nil)
  9357. (unless noremove
  9358. (remove-hook 'before-change-functions
  9359. 'org-remove-occur-highlights 'local))))
  9360. ;;;; Priorities
  9361. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9362. "Regular expression matching the priority indicator.")
  9363. (defvar org-remove-priority-next-time nil)
  9364. (defun org-priority-up ()
  9365. "Increase the priority of the current item."
  9366. (interactive)
  9367. (org-priority 'up))
  9368. (defun org-priority-down ()
  9369. "Decrease the priority of the current item."
  9370. (interactive)
  9371. (org-priority 'down))
  9372. (defun org-priority (&optional action)
  9373. "Change the priority of an item by ARG.
  9374. ACTION can be `set', `up', `down', or a character."
  9375. (interactive)
  9376. (unless org-enable-priority-commands
  9377. (error "Priority commands are disabled"))
  9378. (setq action (or action 'set))
  9379. (let (current new news have remove)
  9380. (save-excursion
  9381. (org-back-to-heading t)
  9382. (if (looking-at org-priority-regexp)
  9383. (setq current (string-to-char (match-string 2))
  9384. have t)
  9385. (setq current org-default-priority))
  9386. (cond
  9387. ((or (eq action 'set)
  9388. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9389. (if (not (eq action 'set))
  9390. (setq new action)
  9391. (message "Priority %c-%c, SPC to remove: "
  9392. org-highest-priority org-lowest-priority)
  9393. (setq new (read-char-exclusive)))
  9394. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9395. (= (upcase org-lowest-priority) org-lowest-priority))
  9396. (setq new (upcase new)))
  9397. (cond ((equal new ?\ ) (setq remove t))
  9398. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9399. (error "Priority must be between `%c' and `%c'"
  9400. org-highest-priority org-lowest-priority))))
  9401. ((eq action 'up)
  9402. (if (and (not have) (eq last-command this-command))
  9403. (setq new org-lowest-priority)
  9404. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9405. org-default-priority (1- current)))))
  9406. ((eq action 'down)
  9407. (if (and (not have) (eq last-command this-command))
  9408. (setq new org-highest-priority)
  9409. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9410. org-default-priority (1+ current)))))
  9411. (t (error "Invalid action")))
  9412. (if (or (< (upcase new) org-highest-priority)
  9413. (> (upcase new) org-lowest-priority))
  9414. (setq remove t))
  9415. (setq news (format "%c" new))
  9416. (if have
  9417. (if remove
  9418. (replace-match "" t t nil 1)
  9419. (replace-match news t t nil 2))
  9420. (if remove
  9421. (error "No priority cookie found in line")
  9422. (let ((case-fold-search nil))
  9423. (looking-at org-todo-line-regexp))
  9424. (if (match-end 2)
  9425. (progn
  9426. (goto-char (match-end 2))
  9427. (insert " [#" news "]"))
  9428. (goto-char (match-beginning 3))
  9429. (insert "[#" news "] "))))
  9430. (org-preserve-lc (org-set-tags nil 'align)))
  9431. (if remove
  9432. (message "Priority removed")
  9433. (message "Priority of current item set to %s" news))))
  9434. (defun org-get-priority (s)
  9435. "Find priority cookie and return priority."
  9436. (save-match-data
  9437. (if (not (string-match org-priority-regexp s))
  9438. (* 1000 (- org-lowest-priority org-default-priority))
  9439. (* 1000 (- org-lowest-priority
  9440. (string-to-char (match-string 2 s)))))))
  9441. ;;;; Tags
  9442. (defvar org-agenda-archives-mode)
  9443. (defvar org-map-continue-from nil
  9444. "Position from where mapping should continue.
  9445. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9446. (defvar org-scanner-tags nil
  9447. "The current tag list while the tags scanner is running.")
  9448. (defvar org-trust-scanner-tags nil
  9449. "Should `org-get-tags-at' use the tags fro the scanner.
  9450. This is for internal dynamical scoping only.
  9451. When this is non-nil, the function `org-get-tags-at' will return the value
  9452. of `org-scanner-tags' instead of building the list by itself. This
  9453. can lead to large speed-ups when the tags scanner is used in a file with
  9454. many entries, and when the list of tags is retrieved, for example to
  9455. obtain a list of properties. Building the tags list for each entry in such
  9456. a file becomes an N^2 operation - but with this variable set, it scales
  9457. as N.")
  9458. (defun org-scan-tags (action matcher &optional todo-only)
  9459. "Scan headline tags with inheritance and produce output ACTION.
  9460. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9461. or `agenda' to produce an entry list for an agenda view. It can also be
  9462. a Lisp form or a function that should be called at each matched headline, in
  9463. this case the return value is a list of all return values from these calls.
  9464. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9465. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9466. only lines with a TODO keyword are included in the output."
  9467. (require 'org-agenda)
  9468. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9469. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9470. (org-re
  9471. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9472. (props (list 'face 'default
  9473. 'done-face 'org-agenda-done
  9474. 'undone-face 'default
  9475. 'mouse-face 'highlight
  9476. 'org-not-done-regexp org-not-done-regexp
  9477. 'org-todo-regexp org-todo-regexp
  9478. 'keymap org-agenda-keymap
  9479. 'help-echo
  9480. (format "mouse-2 or RET jump to org file %s"
  9481. (abbreviate-file-name
  9482. (or (buffer-file-name (buffer-base-buffer))
  9483. (buffer-name (buffer-base-buffer)))))))
  9484. (case-fold-search nil)
  9485. (org-map-continue-from nil)
  9486. lspos tags tags-list
  9487. (tags-alist (list (cons 0 org-file-tags)))
  9488. (llast 0) rtn rtn1 level category i txt
  9489. todo marker entry priority)
  9490. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9491. (setq action (list 'lambda nil action)))
  9492. (save-excursion
  9493. (goto-char (point-min))
  9494. (when (eq action 'sparse-tree)
  9495. (org-overview)
  9496. (org-remove-occur-highlights))
  9497. (while (re-search-forward re nil t)
  9498. (catch :skip
  9499. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9500. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9501. (goto-char (setq lspos (match-beginning 0)))
  9502. (setq level (org-reduced-level (funcall outline-level))
  9503. category (org-get-category))
  9504. (setq i llast llast level)
  9505. ;; remove tag lists from same and sublevels
  9506. (while (>= i level)
  9507. (when (setq entry (assoc i tags-alist))
  9508. (setq tags-alist (delete entry tags-alist)))
  9509. (setq i (1- i)))
  9510. ;; add the next tags
  9511. (when tags
  9512. (setq tags (org-split-string tags ":")
  9513. tags-alist
  9514. (cons (cons level tags) tags-alist)))
  9515. ;; compile tags for current headline
  9516. (setq tags-list
  9517. (if org-use-tag-inheritance
  9518. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9519. tags)
  9520. org-scanner-tags tags-list)
  9521. (when org-use-tag-inheritance
  9522. (setcdr (car tags-alist)
  9523. (mapcar (lambda (x)
  9524. (setq x (copy-sequence x))
  9525. (org-add-prop-inherited x))
  9526. (cdar tags-alist))))
  9527. (when (and tags org-use-tag-inheritance
  9528. (or (not (eq t org-use-tag-inheritance))
  9529. org-tags-exclude-from-inheritance))
  9530. ;; selective inheritance, remove uninherited ones
  9531. (setcdr (car tags-alist)
  9532. (org-remove-uniherited-tags (cdar tags-alist))))
  9533. (when (and (or (not todo-only)
  9534. (and (member todo org-not-done-keywords)
  9535. (or (not org-agenda-tags-todo-honor-ignore-options)
  9536. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9537. (let ((case-fold-search t)) (eval matcher))
  9538. (or
  9539. (not (member org-archive-tag tags-list))
  9540. ;; we have an archive tag, should we use this anyway?
  9541. (or (not org-agenda-skip-archived-trees)
  9542. (and (eq action 'agenda) org-agenda-archives-mode))))
  9543. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9544. ;; select this headline
  9545. (cond
  9546. ((eq action 'sparse-tree)
  9547. (and org-highlight-sparse-tree-matches
  9548. (org-get-heading) (match-end 0)
  9549. (org-highlight-new-match
  9550. (match-beginning 0) (match-beginning 1)))
  9551. (org-show-context 'tags-tree))
  9552. ((eq action 'agenda)
  9553. (setq txt (org-format-agenda-item
  9554. ""
  9555. (concat
  9556. (if (eq org-tags-match-list-sublevels 'indented)
  9557. (make-string (1- level) ?.) "")
  9558. (org-get-heading))
  9559. category
  9560. tags-list
  9561. )
  9562. priority (org-get-priority txt))
  9563. (goto-char lspos)
  9564. (setq marker (org-agenda-new-marker))
  9565. (org-add-props txt props
  9566. 'org-marker marker 'org-hd-marker marker 'org-category category
  9567. 'todo-state todo
  9568. 'priority priority 'type "tagsmatch")
  9569. (push txt rtn))
  9570. ((functionp action)
  9571. (setq org-map-continue-from nil)
  9572. (save-excursion
  9573. (setq rtn1 (funcall action))
  9574. (push rtn1 rtn)))
  9575. (t (error "Invalid action")))
  9576. ;; if we are to skip sublevels, jump to end of subtree
  9577. (unless org-tags-match-list-sublevels
  9578. (org-end-of-subtree t)
  9579. (backward-char 1))))
  9580. ;; Get the correct position from where to continue
  9581. (if org-map-continue-from
  9582. (goto-char org-map-continue-from)
  9583. (and (= (point) lspos) (end-of-line 1)))))
  9584. (when (and (eq action 'sparse-tree)
  9585. (not org-sparse-tree-open-archived-trees))
  9586. (org-hide-archived-subtrees (point-min) (point-max)))
  9587. (nreverse rtn)))
  9588. (defun org-remove-uniherited-tags (tags)
  9589. "Remove all tags that are not inherited from the list TAGS."
  9590. (cond
  9591. ((eq org-use-tag-inheritance t)
  9592. (if org-tags-exclude-from-inheritance
  9593. (org-delete-all org-tags-exclude-from-inheritance tags)
  9594. tags))
  9595. ((not org-use-tag-inheritance) nil)
  9596. ((stringp org-use-tag-inheritance)
  9597. (delq nil (mapcar
  9598. (lambda (x)
  9599. (if (and (string-match org-use-tag-inheritance x)
  9600. (not (member x org-tags-exclude-from-inheritance)))
  9601. x nil))
  9602. tags)))
  9603. ((listp org-use-tag-inheritance)
  9604. (delq nil (mapcar
  9605. (lambda (x)
  9606. (if (member x org-use-tag-inheritance) x nil))
  9607. tags)))))
  9608. (defvar todo-only) ;; dynamically scoped
  9609. (defun org-match-sparse-tree (&optional todo-only match)
  9610. "Create a sparse tree according to tags string MATCH.
  9611. MATCH can contain positive and negative selection of tags, like
  9612. \"+WORK+URGENT-WITHBOSS\".
  9613. If optional argument TODO-ONLY is non-nil, only select lines that are
  9614. also TODO lines."
  9615. (interactive "P")
  9616. (org-prepare-agenda-buffers (list (current-buffer)))
  9617. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9618. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9619. (defvar org-cached-props nil)
  9620. (defun org-cached-entry-get (pom property)
  9621. (if (or (eq t org-use-property-inheritance)
  9622. (and (stringp org-use-property-inheritance)
  9623. (string-match org-use-property-inheritance property))
  9624. (and (listp org-use-property-inheritance)
  9625. (member property org-use-property-inheritance)))
  9626. ;; Caching is not possible, check it directly
  9627. (org-entry-get pom property 'inherit)
  9628. ;; Get all properties, so that we can do complicated checks easily
  9629. (cdr (assoc property (or org-cached-props
  9630. (setq org-cached-props
  9631. (org-entry-properties pom)))))))
  9632. (defun org-global-tags-completion-table (&optional files)
  9633. "Return the list of all tags in all agenda buffer/files."
  9634. (save-excursion
  9635. (org-uniquify
  9636. (delq nil
  9637. (apply 'append
  9638. (mapcar
  9639. (lambda (file)
  9640. (set-buffer (find-file-noselect file))
  9641. (append (org-get-buffer-tags)
  9642. (mapcar (lambda (x) (if (stringp (car-safe x))
  9643. (list (car-safe x)) nil))
  9644. org-tag-alist)))
  9645. (if (and files (car files))
  9646. files
  9647. (org-agenda-files))))))))
  9648. (defun org-make-tags-matcher (match)
  9649. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9650. ;; todo-only is scoped dynamically into this function, and the function
  9651. ;; may change it if the matcher asks for it.
  9652. (unless match
  9653. ;; Get a new match request, with completion
  9654. (let ((org-last-tags-completion-table
  9655. (org-global-tags-completion-table)))
  9656. (setq match (org-completing-read-no-ido
  9657. "Match: " 'org-tags-completion-function nil nil nil
  9658. 'org-tags-history))))
  9659. ;; Parse the string and create a lisp form
  9660. (let ((match0 match)
  9661. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9662. minus tag mm
  9663. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9664. orterms term orlist re-p str-p level-p level-op time-p
  9665. prop-p pn pv po cat-p gv rest)
  9666. (if (string-match "/+" match)
  9667. ;; match contains also a todo-matching request
  9668. (progn
  9669. (setq tagsmatch (substring match 0 (match-beginning 0))
  9670. todomatch (substring match (match-end 0)))
  9671. (if (string-match "^!" todomatch)
  9672. (setq todo-only t todomatch (substring todomatch 1)))
  9673. (if (string-match "^\\s-*$" todomatch)
  9674. (setq todomatch nil)))
  9675. ;; only matching tags
  9676. (setq tagsmatch match todomatch nil))
  9677. ;; Make the tags matcher
  9678. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9679. (setq tagsmatcher t)
  9680. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9681. (while (setq term (pop orterms))
  9682. (while (and (equal (substring term -1) "\\") orterms)
  9683. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9684. (while (string-match re term)
  9685. (setq rest (substring term (match-end 0))
  9686. minus (and (match-end 1)
  9687. (equal (match-string 1 term) "-"))
  9688. tag (match-string 2 term)
  9689. re-p (equal (string-to-char tag) ?{)
  9690. level-p (match-end 4)
  9691. prop-p (match-end 5)
  9692. mm (cond
  9693. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9694. (level-p
  9695. (setq level-op (org-op-to-function (match-string 3 term)))
  9696. `(,level-op level ,(string-to-number
  9697. (match-string 4 term))))
  9698. (prop-p
  9699. (setq pn (match-string 5 term)
  9700. po (match-string 6 term)
  9701. pv (match-string 7 term)
  9702. cat-p (equal pn "CATEGORY")
  9703. re-p (equal (string-to-char pv) ?{)
  9704. str-p (equal (string-to-char pv) ?\")
  9705. time-p (save-match-data
  9706. (string-match "^\"[[<].*[]>]\"$" pv))
  9707. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9708. (if time-p (setq pv (org-matcher-time pv)))
  9709. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9710. (cond
  9711. ((equal pn "CATEGORY")
  9712. (setq gv '(get-text-property (point) 'org-category)))
  9713. ((equal pn "TODO")
  9714. (setq gv 'todo))
  9715. (t
  9716. (setq gv `(org-cached-entry-get nil ,pn))))
  9717. (if re-p
  9718. (if (eq po 'org<>)
  9719. `(not (string-match ,pv (or ,gv "")))
  9720. `(string-match ,pv (or ,gv "")))
  9721. (if str-p
  9722. `(,po (or ,gv "") ,pv)
  9723. `(,po (string-to-number (or ,gv ""))
  9724. ,(string-to-number pv) ))))
  9725. (t `(member ,tag tags-list)))
  9726. mm (if minus (list 'not mm) mm)
  9727. term rest)
  9728. (push mm tagsmatcher))
  9729. (push (if (> (length tagsmatcher) 1)
  9730. (cons 'and tagsmatcher)
  9731. (car tagsmatcher))
  9732. orlist)
  9733. (setq tagsmatcher nil))
  9734. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9735. (setq tagsmatcher
  9736. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9737. ;; Make the todo matcher
  9738. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9739. (setq todomatcher t)
  9740. (setq orterms (org-split-string todomatch "|") orlist nil)
  9741. (while (setq term (pop orterms))
  9742. (while (string-match re term)
  9743. (setq minus (and (match-end 1)
  9744. (equal (match-string 1 term) "-"))
  9745. kwd (match-string 2 term)
  9746. re-p (equal (string-to-char kwd) ?{)
  9747. term (substring term (match-end 0))
  9748. mm (if re-p
  9749. `(string-match ,(substring kwd 1 -1) todo)
  9750. (list 'equal 'todo kwd))
  9751. mm (if minus (list 'not mm) mm))
  9752. (push mm todomatcher))
  9753. (push (if (> (length todomatcher) 1)
  9754. (cons 'and todomatcher)
  9755. (car todomatcher))
  9756. orlist)
  9757. (setq todomatcher nil))
  9758. (setq todomatcher (if (> (length orlist) 1)
  9759. (cons 'or orlist) (car orlist))))
  9760. ;; Return the string and lisp forms of the matcher
  9761. (setq matcher (if todomatcher
  9762. (list 'and tagsmatcher todomatcher)
  9763. tagsmatcher))
  9764. (cons match0 matcher)))
  9765. (defun org-op-to-function (op &optional stringp)
  9766. "Turn an operator into the appropriate function."
  9767. (setq op
  9768. (cond
  9769. ((equal op "<" ) '(< string< org-time<))
  9770. ((equal op ">" ) '(> org-string> org-time>))
  9771. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9772. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9773. ((member op '("=" "==")) '(= string= org-time=))
  9774. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9775. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9776. (defun org<> (a b) (not (= a b)))
  9777. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9778. (defun org-string>= (a b) (not (string< a b)))
  9779. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9780. (defun org-string<> (a b) (not (string= a b)))
  9781. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9782. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9783. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9784. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9785. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9786. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9787. (defun org-2ft (s)
  9788. "Convert S to a floating point time.
  9789. If S is already a number, just return it. If it is a string, parse
  9790. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9791. (cond
  9792. ((numberp s) s)
  9793. ((stringp s)
  9794. (condition-case nil
  9795. (float-time (apply 'encode-time (org-parse-time-string s)))
  9796. (error 0.)))
  9797. (t 0.)))
  9798. (defun org-time-today ()
  9799. "Time in seconds today at 0:00.
  9800. Returns the float number of seconds since the beginning of the
  9801. epoch to the beginning of today (00:00)."
  9802. (float-time (apply 'encode-time
  9803. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9804. (defun org-matcher-time (s)
  9805. "Interpret a time comparison value."
  9806. (save-match-data
  9807. (cond
  9808. ((string= s "<now>") (float-time))
  9809. ((string= s "<today>") (org-time-today))
  9810. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9811. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9812. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9813. (+ (org-time-today)
  9814. (* (string-to-number (match-string 1 s))
  9815. (cdr (assoc (match-string 2 s)
  9816. '(("d" . 86400.0) ("w" . 604800.0)
  9817. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9818. (t (org-2ft s)))))
  9819. (defun org-match-any-p (re list)
  9820. "Does re match any element of list?"
  9821. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9822. (delq nil list))
  9823. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9824. (defvar org-tags-overlay (org-make-overlay 1 1))
  9825. (org-detach-overlay org-tags-overlay)
  9826. (defun org-get-local-tags-at (&optional pos)
  9827. "Get a list of tags defined in the current headline."
  9828. (org-get-tags-at pos 'local))
  9829. (defun org-get-local-tags ()
  9830. "Get a list of tags defined in the current headline."
  9831. (org-get-tags-at nil 'local))
  9832. (defun org-get-tags-at (&optional pos local)
  9833. "Get a list of all headline tags applicable at POS.
  9834. POS defaults to point. If tags are inherited, the list contains
  9835. the targets in the same sequence as the headlines appear, i.e.
  9836. the tags of the current headline come last.
  9837. When LOCAL is non-nil, only return tags from the current headline,
  9838. ignore inherited ones."
  9839. (interactive)
  9840. (if (and org-trust-scanner-tags
  9841. (or (not pos) (equal pos (point)))
  9842. (not local))
  9843. org-scanner-tags
  9844. (let (tags ltags lastpos parent)
  9845. (save-excursion
  9846. (save-restriction
  9847. (widen)
  9848. (goto-char (or pos (point)))
  9849. (save-match-data
  9850. (catch 'done
  9851. (condition-case nil
  9852. (progn
  9853. (org-back-to-heading t)
  9854. (while (not (equal lastpos (point)))
  9855. (setq lastpos (point))
  9856. (when (looking-at
  9857. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9858. (setq ltags (org-split-string
  9859. (org-match-string-no-properties 1) ":"))
  9860. (when parent
  9861. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9862. (setq tags (append
  9863. (if parent
  9864. (org-remove-uniherited-tags ltags)
  9865. ltags)
  9866. tags)))
  9867. (or org-use-tag-inheritance (throw 'done t))
  9868. (if local (throw 'done t))
  9869. (or (org-up-heading-safe) (error nil))
  9870. (setq parent t)))
  9871. (error nil)))))
  9872. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9873. (defun org-add-prop-inherited (s)
  9874. (add-text-properties 0 (length s) '(inherited t) s)
  9875. s)
  9876. (defun org-toggle-tag (tag &optional onoff)
  9877. "Toggle the tag TAG for the current line.
  9878. If ONOFF is `on' or `off', don't toggle but set to this state."
  9879. (let (res current)
  9880. (save-excursion
  9881. (org-back-to-heading t)
  9882. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9883. (point-at-eol) t)
  9884. (progn
  9885. (setq current (match-string 1))
  9886. (replace-match ""))
  9887. (setq current ""))
  9888. (setq current (nreverse (org-split-string current ":")))
  9889. (cond
  9890. ((eq onoff 'on)
  9891. (setq res t)
  9892. (or (member tag current) (push tag current)))
  9893. ((eq onoff 'off)
  9894. (or (not (member tag current)) (setq current (delete tag current))))
  9895. (t (if (member tag current)
  9896. (setq current (delete tag current))
  9897. (setq res t)
  9898. (push tag current))))
  9899. (end-of-line 1)
  9900. (if current
  9901. (progn
  9902. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9903. (org-set-tags nil t))
  9904. (delete-horizontal-space))
  9905. (run-hooks 'org-after-tags-change-hook))
  9906. res))
  9907. (defun org-align-tags-here (to-col)
  9908. ;; Assumes that this is a headline
  9909. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9910. (beginning-of-line 1)
  9911. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9912. (< pos (match-beginning 2)))
  9913. (progn
  9914. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9915. (goto-char (match-beginning 1))
  9916. (insert " ")
  9917. (delete-region (point) (1+ (match-beginning 2)))
  9918. (setq ncol (max (1+ (current-column))
  9919. (1+ col)
  9920. (if (> to-col 0)
  9921. to-col
  9922. (- (abs to-col) tags-l))))
  9923. (setq p (point))
  9924. (insert (make-string (- ncol (current-column)) ?\ ))
  9925. (setq ncol (current-column))
  9926. (when indent-tabs-mode (tabify p (point-at-eol)))
  9927. (org-move-to-column (min ncol col) t))
  9928. (goto-char pos))))
  9929. (defun org-set-tags-command (&optional arg just-align)
  9930. "Call the set-tags command for the current entry."
  9931. (interactive "P")
  9932. (if (org-on-heading-p)
  9933. (org-set-tags arg just-align)
  9934. (save-excursion
  9935. (org-back-to-heading t)
  9936. (org-set-tags arg just-align))))
  9937. (defun org-set-tags (&optional arg just-align)
  9938. "Set the tags for the current headline.
  9939. With prefix ARG, realign all tags in headings in the current buffer."
  9940. (interactive "P")
  9941. (let* ((re (concat "^" outline-regexp))
  9942. (current (org-get-tags-string))
  9943. (col (current-column))
  9944. (org-setting-tags t)
  9945. table current-tags inherited-tags ; computed below when needed
  9946. tags p0 c0 c1 rpl)
  9947. (if arg
  9948. (save-excursion
  9949. (goto-char (point-min))
  9950. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9951. (while (re-search-forward re nil t)
  9952. (org-set-tags nil t)
  9953. (end-of-line 1)))
  9954. (message "All tags realigned to column %d" org-tags-column))
  9955. (if just-align
  9956. (setq tags current)
  9957. ;; Get a new set of tags from the user
  9958. (save-excursion
  9959. (setq table (append org-tag-persistent-alist
  9960. (or org-tag-alist (org-get-buffer-tags)))
  9961. org-last-tags-completion-table table
  9962. current-tags (org-split-string current ":")
  9963. inherited-tags (nreverse
  9964. (nthcdr (length current-tags)
  9965. (nreverse (org-get-tags-at))))
  9966. tags
  9967. (if (or (eq t org-use-fast-tag-selection)
  9968. (and org-use-fast-tag-selection
  9969. (delq nil (mapcar 'cdr table))))
  9970. (org-fast-tag-selection
  9971. current-tags inherited-tags table
  9972. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9973. (let ((org-add-colon-after-tag-completion t))
  9974. (org-trim
  9975. (org-without-partial-completion
  9976. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9977. nil nil current 'org-tags-history)))))))
  9978. (while (string-match "[-+&]+" tags)
  9979. ;; No boolean logic, just a list
  9980. (setq tags (replace-match ":" t t tags))))
  9981. (if org-tags-sort-function
  9982. (setq tags (mapconcat 'identity
  9983. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  9984. org-tags-sort-function) ":")))
  9985. (if (string-match "\\`[\t ]*\\'" tags)
  9986. (setq tags "")
  9987. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9988. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9989. ;; Insert new tags at the correct column
  9990. (beginning-of-line 1)
  9991. (cond
  9992. ((and (equal current "") (equal tags "")))
  9993. ((re-search-forward
  9994. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9995. (point-at-eol) t)
  9996. (if (equal tags "")
  9997. (setq rpl "")
  9998. (goto-char (match-beginning 0))
  9999. (setq c0 (current-column) p0 (point)
  10000. c1 (max (1+ c0) (if (> org-tags-column 0)
  10001. org-tags-column
  10002. (- (- org-tags-column) (length tags))))
  10003. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10004. (replace-match rpl t t)
  10005. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10006. tags)
  10007. (t (error "Tags alignment failed")))
  10008. (org-move-to-column col)
  10009. (unless just-align
  10010. (run-hooks 'org-after-tags-change-hook)))))
  10011. (defun org-change-tag-in-region (beg end tag off)
  10012. "Add or remove TAG for each entry in the region.
  10013. This works in the agenda, and also in an org-mode buffer."
  10014. (interactive
  10015. (list (region-beginning) (region-end)
  10016. (let ((org-last-tags-completion-table
  10017. (if (org-mode-p)
  10018. (org-get-buffer-tags)
  10019. (org-global-tags-completion-table))))
  10020. (org-ido-completing-read
  10021. "Tag: " 'org-tags-completion-function nil nil nil
  10022. 'org-tags-history))
  10023. (progn
  10024. (message "[s]et or [r]emove? ")
  10025. (equal (read-char-exclusive) ?r))))
  10026. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10027. (let ((agendap (equal major-mode 'org-agenda-mode))
  10028. l1 l2 m buf pos newhead (cnt 0))
  10029. (goto-char end)
  10030. (setq l2 (1- (org-current-line)))
  10031. (goto-char beg)
  10032. (setq l1 (org-current-line))
  10033. (loop for l from l1 to l2 do
  10034. (goto-line l)
  10035. (setq m (get-text-property (point) 'org-hd-marker))
  10036. (when (or (and (org-mode-p) (org-on-heading-p))
  10037. (and agendap m))
  10038. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10039. pos (if agendap m (point)))
  10040. (with-current-buffer buf
  10041. (save-excursion
  10042. (save-restriction
  10043. (goto-char pos)
  10044. (setq cnt (1+ cnt))
  10045. (org-toggle-tag tag (if off 'off 'on))
  10046. (setq newhead (org-get-heading)))))
  10047. (and agendap (org-agenda-change-all-lines newhead m))))
  10048. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10049. (defun org-tags-completion-function (string predicate &optional flag)
  10050. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10051. (confirm (lambda (x) (stringp (car x)))))
  10052. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10053. (setq s1 (match-string 1 string)
  10054. s2 (match-string 2 string))
  10055. (setq s1 "" s2 string))
  10056. (cond
  10057. ((eq flag nil)
  10058. ;; try completion
  10059. (setq rtn (try-completion s2 ctable confirm))
  10060. (if (stringp rtn)
  10061. (setq rtn
  10062. (concat s1 s2 (substring rtn (length s2))
  10063. (if (and org-add-colon-after-tag-completion
  10064. (assoc rtn ctable))
  10065. ":" ""))))
  10066. rtn)
  10067. ((eq flag t)
  10068. ;; all-completions
  10069. (all-completions s2 ctable confirm)
  10070. )
  10071. ((eq flag 'lambda)
  10072. ;; exact match?
  10073. (assoc s2 ctable)))
  10074. ))
  10075. (defun org-fast-tag-insert (kwd tags face &optional end)
  10076. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10077. (insert (format "%-12s" (concat kwd ":"))
  10078. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10079. (or end "")))
  10080. (defun org-fast-tag-show-exit (flag)
  10081. (save-excursion
  10082. (goto-line 3)
  10083. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10084. (replace-match ""))
  10085. (when flag
  10086. (end-of-line 1)
  10087. (org-move-to-column (- (window-width) 19) t)
  10088. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10089. (defun org-set-current-tags-overlay (current prefix)
  10090. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10091. (if (featurep 'xemacs)
  10092. (org-overlay-display org-tags-overlay (concat prefix s)
  10093. 'secondary-selection)
  10094. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10095. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10096. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10097. "Fast tag selection with single keys.
  10098. CURRENT is the current list of tags in the headline, INHERITED is the
  10099. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10100. possibly with grouping information. TODO-TABLE is a similar table with
  10101. TODO keywords, should these have keys assigned to them.
  10102. If the keys are nil, a-z are automatically assigned.
  10103. Returns the new tags string, or nil to not change the current settings."
  10104. (let* ((fulltable (append table todo-table))
  10105. (maxlen (apply 'max (mapcar
  10106. (lambda (x)
  10107. (if (stringp (car x)) (string-width (car x)) 0))
  10108. fulltable)))
  10109. (buf (current-buffer))
  10110. (expert (eq org-fast-tag-selection-single-key 'expert))
  10111. (buffer-tags nil)
  10112. (fwidth (+ maxlen 3 1 3))
  10113. (ncol (/ (- (window-width) 4) fwidth))
  10114. (i-face 'org-done)
  10115. (c-face 'org-todo)
  10116. tg cnt e c char c1 c2 ntable tbl rtn
  10117. ov-start ov-end ov-prefix
  10118. (exit-after-next org-fast-tag-selection-single-key)
  10119. (done-keywords org-done-keywords)
  10120. groups ingroup)
  10121. (save-excursion
  10122. (beginning-of-line 1)
  10123. (if (looking-at
  10124. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10125. (setq ov-start (match-beginning 1)
  10126. ov-end (match-end 1)
  10127. ov-prefix "")
  10128. (setq ov-start (1- (point-at-eol))
  10129. ov-end (1+ ov-start))
  10130. (skip-chars-forward "^\n\r")
  10131. (setq ov-prefix
  10132. (concat
  10133. (buffer-substring (1- (point)) (point))
  10134. (if (> (current-column) org-tags-column)
  10135. " "
  10136. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10137. (org-move-overlay org-tags-overlay ov-start ov-end)
  10138. (save-window-excursion
  10139. (if expert
  10140. (set-buffer (get-buffer-create " *Org tags*"))
  10141. (delete-other-windows)
  10142. (split-window-vertically)
  10143. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10144. (erase-buffer)
  10145. (org-set-local 'org-done-keywords done-keywords)
  10146. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10147. (org-fast-tag-insert "Current" current c-face "\n\n")
  10148. (org-fast-tag-show-exit exit-after-next)
  10149. (org-set-current-tags-overlay current ov-prefix)
  10150. (setq tbl fulltable char ?a cnt 0)
  10151. (while (setq e (pop tbl))
  10152. (cond
  10153. ((equal e '(:startgroup))
  10154. (push '() groups) (setq ingroup t)
  10155. (when (not (= cnt 0))
  10156. (setq cnt 0)
  10157. (insert "\n"))
  10158. (insert "{ "))
  10159. ((equal e '(:endgroup))
  10160. (setq ingroup nil cnt 0)
  10161. (insert "}\n"))
  10162. ((equal e '(:newline))
  10163. (when (not (= cnt 0))
  10164. (setq cnt 0)
  10165. (insert "\n")
  10166. (setq e (car tbl))
  10167. (while (equal (car tbl) '(:newline))
  10168. (insert "\n")
  10169. (setq tbl (cdr tbl)))))
  10170. (t
  10171. (setq tg (car e) c2 nil)
  10172. (if (cdr e)
  10173. (setq c (cdr e))
  10174. ;; automatically assign a character.
  10175. (setq c1 (string-to-char
  10176. (downcase (substring
  10177. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10178. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10179. (while (or (rassoc char ntable) (rassoc char table))
  10180. (setq char (1+ char)))
  10181. (setq c2 c1))
  10182. (setq c (or c2 char)))
  10183. (if ingroup (push tg (car groups)))
  10184. (setq tg (org-add-props tg nil 'face
  10185. (cond
  10186. ((not (assoc tg table))
  10187. (org-get-todo-face tg))
  10188. ((member tg current) c-face)
  10189. ((member tg inherited) i-face)
  10190. (t nil))))
  10191. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10192. (insert "[" c "] " tg (make-string
  10193. (- fwidth 4 (length tg)) ?\ ))
  10194. (push (cons tg c) ntable)
  10195. (when (= (setq cnt (1+ cnt)) ncol)
  10196. (insert "\n")
  10197. (if ingroup (insert " "))
  10198. (setq cnt 0)))))
  10199. (setq ntable (nreverse ntable))
  10200. (insert "\n")
  10201. (goto-char (point-min))
  10202. (if (not expert) (org-fit-window-to-buffer))
  10203. (setq rtn
  10204. (catch 'exit
  10205. (while t
  10206. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10207. (if groups " [!] no groups" " [!]groups")
  10208. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10209. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10210. (cond
  10211. ((= c ?\r) (throw 'exit t))
  10212. ((= c ?!)
  10213. (setq groups (not groups))
  10214. (goto-char (point-min))
  10215. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10216. ((= c ?\C-c)
  10217. (if (not expert)
  10218. (org-fast-tag-show-exit
  10219. (setq exit-after-next (not exit-after-next)))
  10220. (setq expert nil)
  10221. (delete-other-windows)
  10222. (split-window-vertically)
  10223. (org-switch-to-buffer-other-window " *Org tags*")
  10224. (org-fit-window-to-buffer)))
  10225. ((or (= c ?\C-g)
  10226. (and (= c ?q) (not (rassoc c ntable))))
  10227. (org-detach-overlay org-tags-overlay)
  10228. (setq quit-flag t))
  10229. ((= c ?\ )
  10230. (setq current nil)
  10231. (if exit-after-next (setq exit-after-next 'now)))
  10232. ((= c ?\t)
  10233. (condition-case nil
  10234. (setq tg (org-ido-completing-read
  10235. "Tag: "
  10236. (or buffer-tags
  10237. (with-current-buffer buf
  10238. (org-get-buffer-tags)))))
  10239. (quit (setq tg "")))
  10240. (when (string-match "\\S-" tg)
  10241. (add-to-list 'buffer-tags (list tg))
  10242. (if (member tg current)
  10243. (setq current (delete tg current))
  10244. (push tg current)))
  10245. (if exit-after-next (setq exit-after-next 'now)))
  10246. ((setq e (rassoc c todo-table) tg (car e))
  10247. (with-current-buffer buf
  10248. (save-excursion (org-todo tg)))
  10249. (if exit-after-next (setq exit-after-next 'now)))
  10250. ((setq e (rassoc c ntable) tg (car e))
  10251. (if (member tg current)
  10252. (setq current (delete tg current))
  10253. (loop for g in groups do
  10254. (if (member tg g)
  10255. (mapc (lambda (x)
  10256. (setq current (delete x current)))
  10257. g)))
  10258. (push tg current))
  10259. (if exit-after-next (setq exit-after-next 'now))))
  10260. ;; Create a sorted list
  10261. (setq current
  10262. (sort current
  10263. (lambda (a b)
  10264. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10265. (if (eq exit-after-next 'now) (throw 'exit t))
  10266. (goto-char (point-min))
  10267. (beginning-of-line 2)
  10268. (delete-region (point) (point-at-eol))
  10269. (org-fast-tag-insert "Current" current c-face)
  10270. (org-set-current-tags-overlay current ov-prefix)
  10271. (while (re-search-forward
  10272. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10273. (setq tg (match-string 1))
  10274. (add-text-properties
  10275. (match-beginning 1) (match-end 1)
  10276. (list 'face
  10277. (cond
  10278. ((member tg current) c-face)
  10279. ((member tg inherited) i-face)
  10280. (t (get-text-property (match-beginning 1) 'face))))))
  10281. (goto-char (point-min)))))
  10282. (org-detach-overlay org-tags-overlay)
  10283. (if rtn
  10284. (mapconcat 'identity current ":")
  10285. nil))))
  10286. (defun org-get-tags-string ()
  10287. "Get the TAGS string in the current headline."
  10288. (unless (org-on-heading-p t)
  10289. (error "Not on a heading"))
  10290. (save-excursion
  10291. (beginning-of-line 1)
  10292. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10293. (org-match-string-no-properties 1)
  10294. "")))
  10295. (defun org-get-tags ()
  10296. "Get the list of tags specified in the current headline."
  10297. (org-split-string (org-get-tags-string) ":"))
  10298. (defun org-get-buffer-tags ()
  10299. "Get a table of all tags used in the buffer, for completion."
  10300. (let (tags)
  10301. (save-excursion
  10302. (goto-char (point-min))
  10303. (while (re-search-forward
  10304. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10305. (when (equal (char-after (point-at-bol 0)) ?*)
  10306. (mapc (lambda (x) (add-to-list 'tags x))
  10307. (org-split-string (org-match-string-no-properties 1) ":")))))
  10308. (mapcar 'list tags)))
  10309. ;;;; The mapping API
  10310. ;;;###autoload
  10311. (defun org-map-entries (func &optional match scope &rest skip)
  10312. "Call FUNC at each headline selected by MATCH in SCOPE.
  10313. FUNC is a function or a lisp form. The function will be called without
  10314. arguments, with the cursor positioned at the beginning of the headline.
  10315. The return values of all calls to the function will be collected and
  10316. returned as a list.
  10317. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10318. does not need to preserve point. After evaluation, the cursor will be
  10319. moved to the end of the line (presumably of the headline of the
  10320. processed entry) and search continues from there. Under some
  10321. circumstances, this may not produce the wanted results. For example,
  10322. if you have removed (e.g. archived) the current (sub)tree it could
  10323. mean that the next entry will be skipped entirely. In such cases, you
  10324. can specify the position from where search should continue by making
  10325. FUNC set the variable `org-map-continue-from' to the desired buffer
  10326. position.
  10327. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10328. Only headlines that are matched by this query will be considered during
  10329. the iteration. When MATCH is nil or t, all headlines will be
  10330. visited by the iteration.
  10331. SCOPE determines the scope of this command. It can be any of:
  10332. nil The current buffer, respecting the restriction if any
  10333. tree The subtree started with the entry at point
  10334. file The current buffer, without restriction
  10335. file-with-archives
  10336. The current buffer, and any archives associated with it
  10337. agenda All agenda files
  10338. agenda-with-archives
  10339. All agenda files with any archive files associated with them
  10340. \(file1 file2 ...)
  10341. If this is a list, all files in the list will be scanned
  10342. The remaining args are treated as settings for the skipping facilities of
  10343. the scanner. The following items can be given here:
  10344. archive skip trees with the archive tag.
  10345. comment skip trees with the COMMENT keyword
  10346. function or Emacs Lisp form:
  10347. will be used as value for `org-agenda-skip-function', so whenever
  10348. the the function returns t, FUNC will not be called for that
  10349. entry and search will continue from the point where the
  10350. function leaves it.
  10351. If your function needs to retrieve the tags including inherited tags
  10352. at the *current* entry, you can use the value of the variable
  10353. `org-scanner-tags' which will be much faster than getting the value
  10354. with `org-get-tags-at'. If your function gets properties with
  10355. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10356. to t around the call to `org-entry-properties' to get the same speedup.
  10357. Note that if your function moves around to retrieve tags and properties at
  10358. a *different* entry, you cannot use these techniques."
  10359. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10360. (org-agenda-skip-archived-trees (memq 'archive skip))
  10361. (org-agenda-skip-comment-trees (memq 'comment skip))
  10362. (org-agenda-skip-function
  10363. (car (org-delete-all '(comment archive) skip)))
  10364. (org-tags-match-list-sublevels t)
  10365. matcher file res
  10366. org-todo-keywords-for-agenda
  10367. org-done-keywords-for-agenda
  10368. org-todo-keyword-alist-for-agenda
  10369. org-tag-alist-for-agenda)
  10370. (cond
  10371. ((eq match t) (setq matcher t))
  10372. ((eq match nil) (setq matcher t))
  10373. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10374. (save-excursion
  10375. (save-restriction
  10376. (when (eq scope 'tree)
  10377. (org-back-to-heading t)
  10378. (org-narrow-to-subtree)
  10379. (setq scope nil))
  10380. (if (not scope)
  10381. (progn
  10382. (org-prepare-agenda-buffers
  10383. (list (buffer-file-name (current-buffer))))
  10384. (setq res (org-scan-tags func matcher)))
  10385. ;; Get the right scope
  10386. (cond
  10387. ((and scope (listp scope) (symbolp (car scope)))
  10388. (setq scope (eval scope)))
  10389. ((eq scope 'agenda)
  10390. (setq scope (org-agenda-files t)))
  10391. ((eq scope 'agenda-with-archives)
  10392. (setq scope (org-agenda-files t))
  10393. (setq scope (org-add-archive-files scope)))
  10394. ((eq scope 'file)
  10395. (setq scope (list (buffer-file-name))))
  10396. ((eq scope 'file-with-archives)
  10397. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10398. (org-prepare-agenda-buffers scope)
  10399. (while (setq file (pop scope))
  10400. (with-current-buffer (org-find-base-buffer-visiting file)
  10401. (save-excursion
  10402. (save-restriction
  10403. (widen)
  10404. (goto-char (point-min))
  10405. (setq res (append res (org-scan-tags func matcher))))))))))
  10406. res))
  10407. ;;;; Properties
  10408. ;;; Setting and retrieving properties
  10409. (defconst org-special-properties
  10410. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10411. "TIMESTAMP" "TIMESTAMP_IA")
  10412. "The special properties valid in Org-mode.
  10413. These are properties that are not defined in the property drawer,
  10414. but in some other way.")
  10415. (defconst org-default-properties
  10416. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10417. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10418. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10419. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10420. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10421. "CLOCK_MODELINE_TOTAL")
  10422. "Some properties that are used by Org-mode for various purposes.
  10423. Being in this list makes sure that they are offered for completion.")
  10424. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10425. "Regular expression matching the first line of a property drawer.")
  10426. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10427. "Regular expression matching the first line of a property drawer.")
  10428. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10429. "Regular expression matching the first line of a property drawer.")
  10430. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10431. "Regular expression matching the first line of a property drawer.")
  10432. (defconst org-property-drawer-re
  10433. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10434. org-property-end-re "\\)\n?")
  10435. "Matches an entire property drawer.")
  10436. (defconst org-clock-drawer-re
  10437. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10438. org-property-end-re "\\)\n?")
  10439. "Matches an entire clock drawer.")
  10440. (defun org-property-action ()
  10441. "Do an action on properties."
  10442. (interactive)
  10443. (let (c)
  10444. (org-at-property-p)
  10445. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10446. (setq c (read-char-exclusive))
  10447. (cond
  10448. ((equal c ?s)
  10449. (call-interactively 'org-set-property))
  10450. ((equal c ?d)
  10451. (call-interactively 'org-delete-property))
  10452. ((equal c ?D)
  10453. (call-interactively 'org-delete-property-globally))
  10454. ((equal c ?c)
  10455. (call-interactively 'org-compute-property-at-point))
  10456. (t (error "No such property action %c" c)))))
  10457. (defun org-at-property-p ()
  10458. "Is the cursor in a property line?"
  10459. ;; FIXME: Does not check if we are actually in the drawer.
  10460. ;; FIXME: also returns true on any drawers.....
  10461. ;; This is used by C-c C-c for property action.
  10462. (save-excursion
  10463. (beginning-of-line 1)
  10464. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10465. (defun org-get-property-block (&optional beg end force)
  10466. "Return the (beg . end) range of the body of the property drawer.
  10467. BEG and END can be beginning and end of subtree, if not given
  10468. they will be found.
  10469. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10470. (catch 'exit
  10471. (save-excursion
  10472. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10473. (end (or end (progn (outline-next-heading) (point)))))
  10474. (goto-char beg)
  10475. (if (re-search-forward org-property-start-re end t)
  10476. (setq beg (1+ (match-end 0)))
  10477. (if force
  10478. (save-excursion
  10479. (org-insert-property-drawer)
  10480. (setq end (progn (outline-next-heading) (point))))
  10481. (throw 'exit nil))
  10482. (goto-char beg)
  10483. (if (re-search-forward org-property-start-re end t)
  10484. (setq beg (1+ (match-end 0)))))
  10485. (if (re-search-forward org-property-end-re end t)
  10486. (setq end (match-beginning 0))
  10487. (or force (throw 'exit nil))
  10488. (goto-char beg)
  10489. (setq end beg)
  10490. (org-indent-line-function)
  10491. (insert ":END:\n"))
  10492. (cons beg end)))))
  10493. (defun org-entry-properties (&optional pom which)
  10494. "Get all properties of the entry at point-or-marker POM.
  10495. This includes the TODO keyword, the tags, time strings for deadline,
  10496. scheduled, and clocking, and any additional properties defined in the
  10497. entry. The return value is an alist, keys may occur multiple times
  10498. if the property key was used several times.
  10499. POM may also be nil, in which case the current entry is used.
  10500. If WHICH is nil or `all', get all properties. If WHICH is
  10501. `special' or `standard', only get that subclass."
  10502. (setq which (or which 'all))
  10503. (org-with-point-at pom
  10504. (let ((clockstr (substring org-clock-string 0 -1))
  10505. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10506. beg end range props sum-props key value string clocksum)
  10507. (save-excursion
  10508. (when (condition-case nil
  10509. (and (org-mode-p) (org-back-to-heading t))
  10510. (error nil))
  10511. (setq beg (point))
  10512. (setq sum-props (get-text-property (point) 'org-summaries))
  10513. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10514. (outline-next-heading)
  10515. (setq end (point))
  10516. (when (memq which '(all special))
  10517. ;; Get the special properties, like TODO and tags
  10518. (goto-char beg)
  10519. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10520. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10521. (when (looking-at org-priority-regexp)
  10522. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10523. (when (and (setq value (org-get-tags-string))
  10524. (string-match "\\S-" value))
  10525. (push (cons "TAGS" value) props))
  10526. (when (setq value (org-get-tags-at))
  10527. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10528. props))
  10529. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10530. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10531. string (if (equal key clockstr)
  10532. (org-no-properties
  10533. (org-trim
  10534. (buffer-substring
  10535. (match-beginning 3) (goto-char (point-at-eol)))))
  10536. (substring (org-match-string-no-properties 3) 1 -1)))
  10537. (unless key
  10538. (if (= (char-after (match-beginning 3)) ?\[)
  10539. (setq key "TIMESTAMP_IA")
  10540. (setq key "TIMESTAMP")))
  10541. (when (or (equal key clockstr) (not (assoc key props)))
  10542. (push (cons key string) props)))
  10543. )
  10544. (when (memq which '(all standard))
  10545. ;; Get the standard properties, like :PROP: ...
  10546. (setq range (org-get-property-block beg end))
  10547. (when range
  10548. (goto-char (car range))
  10549. (while (re-search-forward
  10550. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10551. (cdr range) t)
  10552. (setq key (org-match-string-no-properties 1)
  10553. value (org-trim (or (org-match-string-no-properties 2) "")))
  10554. (unless (member key excluded)
  10555. (push (cons key (or value "")) props)))))
  10556. (if clocksum
  10557. (push (cons "CLOCKSUM"
  10558. (org-columns-number-to-string (/ (float clocksum) 60.)
  10559. 'add_times))
  10560. props))
  10561. (unless (assoc "CATEGORY" props)
  10562. (setq value (or (org-get-category)
  10563. (progn (org-refresh-category-properties)
  10564. (org-get-category))))
  10565. (push (cons "CATEGORY" value) props))
  10566. (append sum-props (nreverse props)))))))
  10567. (defun org-entry-get (pom property &optional inherit)
  10568. "Get value of PROPERTY for entry at point-or-marker POM.
  10569. If INHERIT is non-nil and the entry does not have the property,
  10570. then also check higher levels of the hierarchy.
  10571. If INHERIT is the symbol `selective', use inheritance only if the setting
  10572. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10573. If the property is present but empty, the return value is the empty string.
  10574. If the property is not present at all, nil is returned."
  10575. (org-with-point-at pom
  10576. (if (and inherit (if (eq inherit 'selective)
  10577. (org-property-inherit-p property)
  10578. t))
  10579. (org-entry-get-with-inheritance property)
  10580. (if (member property org-special-properties)
  10581. ;; We need a special property. Use brute force, get all properties.
  10582. (cdr (assoc property (org-entry-properties nil 'special)))
  10583. (let ((range (org-get-property-block)))
  10584. (if (and range
  10585. (goto-char (car range))
  10586. (re-search-forward
  10587. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10588. (cdr range) t))
  10589. ;; Found the property, return it.
  10590. (if (match-end 1)
  10591. (org-match-string-no-properties 1)
  10592. "")))))))
  10593. (defun org-property-or-variable-value (var &optional inherit)
  10594. "Check if there is a property fixing the value of VAR.
  10595. If yes, return this value. If not, return the current value of the variable."
  10596. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10597. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10598. (read prop)
  10599. (symbol-value var))))
  10600. (defun org-entry-delete (pom property)
  10601. "Delete the property PROPERTY from entry at point-or-marker POM."
  10602. (org-with-point-at pom
  10603. (if (member property org-special-properties)
  10604. nil ; cannot delete these properties.
  10605. (let ((range (org-get-property-block)))
  10606. (if (and range
  10607. (goto-char (car range))
  10608. (re-search-forward
  10609. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10610. (cdr range) t))
  10611. (progn
  10612. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10613. t)
  10614. nil)))))
  10615. ;; Multi-values properties are properties that contain multiple values
  10616. ;; These values are assumed to be single words, separated by whitespace.
  10617. (defun org-entry-add-to-multivalued-property (pom property value)
  10618. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10619. (let* ((old (org-entry-get pom property))
  10620. (values (and old (org-split-string old "[ \t]"))))
  10621. (setq value (org-entry-protect-space value))
  10622. (unless (member value values)
  10623. (setq values (cons value values))
  10624. (org-entry-put pom property
  10625. (mapconcat 'identity values " ")))))
  10626. (defun org-entry-remove-from-multivalued-property (pom property value)
  10627. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10628. (let* ((old (org-entry-get pom property))
  10629. (values (and old (org-split-string old "[ \t]"))))
  10630. (setq value (org-entry-protect-space value))
  10631. (when (member value values)
  10632. (setq values (delete value values))
  10633. (org-entry-put pom property
  10634. (mapconcat 'identity values " ")))))
  10635. (defun org-entry-member-in-multivalued-property (pom property value)
  10636. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10637. (let* ((old (org-entry-get pom property))
  10638. (values (and old (org-split-string old "[ \t]"))))
  10639. (setq value (org-entry-protect-space value))
  10640. (member value values)))
  10641. (defun org-entry-get-multivalued-property (pom property)
  10642. "Return a list of values in a multivalued property."
  10643. (let* ((value (org-entry-get pom property))
  10644. (values (and value (org-split-string value "[ \t]"))))
  10645. (mapcar 'org-entry-restore-space values)))
  10646. (defun org-entry-put-multivalued-property (pom property &rest values)
  10647. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10648. VALUES should be a list of strings. Spaces will be protected."
  10649. (org-entry-put pom property
  10650. (mapconcat 'org-entry-protect-space values " "))
  10651. (let* ((value (org-entry-get pom property))
  10652. (values (and value (org-split-string value "[ \t]"))))
  10653. (mapcar 'org-entry-restore-space values)))
  10654. (defun org-entry-protect-space (s)
  10655. "Protect spaces and newline in string S."
  10656. (while (string-match " " s)
  10657. (setq s (replace-match "%20" t t s)))
  10658. (while (string-match "\n" s)
  10659. (setq s (replace-match "%0A" t t s)))
  10660. s)
  10661. (defun org-entry-restore-space (s)
  10662. "Restore spaces and newline in string S."
  10663. (while (string-match "%20" s)
  10664. (setq s (replace-match " " t t s)))
  10665. (while (string-match "%0A" s)
  10666. (setq s (replace-match "\n" t t s)))
  10667. s)
  10668. (defvar org-entry-property-inherited-from (make-marker)
  10669. "Marker pointing to the entry from where a property was inherited.
  10670. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10671. location of the entry where the inheritance search matched. If there was
  10672. no match, the marker will point nowhere.
  10673. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10674. is set.")
  10675. (defun org-entry-get-with-inheritance (property)
  10676. "Get entry property, and search higher levels if not present."
  10677. (move-marker org-entry-property-inherited-from nil)
  10678. (let (tmp)
  10679. (save-excursion
  10680. (save-restriction
  10681. (widen)
  10682. (catch 'ex
  10683. (while t
  10684. (when (setq tmp (org-entry-get nil property))
  10685. (org-back-to-heading t)
  10686. (move-marker org-entry-property-inherited-from (point))
  10687. (throw 'ex tmp))
  10688. (or (org-up-heading-safe) (throw 'ex nil)))))
  10689. (or tmp
  10690. (cdr (assoc property org-file-properties))
  10691. (cdr (assoc property org-global-properties))
  10692. (cdr (assoc property org-global-properties-fixed))))))
  10693. (defun org-entry-put (pom property value)
  10694. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10695. (org-with-point-at pom
  10696. (org-back-to-heading t)
  10697. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10698. range)
  10699. (cond
  10700. ((equal property "TODO")
  10701. (when (and (stringp value) (string-match "\\S-" value)
  10702. (not (member value org-todo-keywords-1)))
  10703. (error "\"%s\" is not a valid TODO state" value))
  10704. (if (or (not value)
  10705. (not (string-match "\\S-" value)))
  10706. (setq value 'none))
  10707. (org-todo value)
  10708. (org-set-tags nil 'align))
  10709. ((equal property "PRIORITY")
  10710. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10711. (string-to-char value) ?\ ))
  10712. (org-set-tags nil 'align))
  10713. ((equal property "SCHEDULED")
  10714. (if (re-search-forward org-scheduled-time-regexp end t)
  10715. (cond
  10716. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10717. ((eq value 'later) (org-timestamp-change 1 'day))
  10718. (t (call-interactively 'org-schedule)))
  10719. (call-interactively 'org-schedule)))
  10720. ((equal property "DEADLINE")
  10721. (if (re-search-forward org-deadline-time-regexp end t)
  10722. (cond
  10723. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10724. ((eq value 'later) (org-timestamp-change 1 'day))
  10725. (t (call-interactively 'org-deadline)))
  10726. (call-interactively 'org-deadline)))
  10727. ((member property org-special-properties)
  10728. (error "The %s property can not yet be set with `org-entry-put'"
  10729. property))
  10730. (t ; a non-special property
  10731. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10732. (setq range (org-get-property-block beg end 'force))
  10733. (goto-char (car range))
  10734. (if (re-search-forward
  10735. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10736. (progn
  10737. (delete-region (match-beginning 1) (match-end 1))
  10738. (goto-char (match-beginning 1)))
  10739. (goto-char (cdr range))
  10740. (insert "\n")
  10741. (backward-char 1)
  10742. (org-indent-line-function)
  10743. (insert ":" property ":"))
  10744. (and value (insert " " value))
  10745. (org-indent-line-function)))))))
  10746. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10747. "Get all property keys in the current buffer.
  10748. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10749. like tags and TODO state.
  10750. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10751. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10752. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10753. formats in the current buffer."
  10754. (let (rtn range cfmt s p)
  10755. (save-excursion
  10756. (save-restriction
  10757. (widen)
  10758. (goto-char (point-min))
  10759. (while (re-search-forward org-property-start-re nil t)
  10760. (setq range (org-get-property-block))
  10761. (goto-char (car range))
  10762. (while (re-search-forward
  10763. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10764. (cdr range) t)
  10765. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10766. (outline-next-heading))))
  10767. (when include-specials
  10768. (setq rtn (append org-special-properties rtn)))
  10769. (when include-defaults
  10770. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  10771. (add-to-list 'rtn org-effort-property))
  10772. (when include-columns
  10773. (save-excursion
  10774. (save-restriction
  10775. (widen)
  10776. (goto-char (point-min))
  10777. (while (re-search-forward
  10778. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10779. nil t)
  10780. (setq cfmt (match-string 2) s 0)
  10781. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10782. cfmt s)
  10783. (setq s (match-end 0)
  10784. p (match-string 1 cfmt))
  10785. (unless (or (equal p "ITEM")
  10786. (member p org-special-properties))
  10787. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10788. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10789. (defun org-property-values (key)
  10790. "Return a list of all values of property KEY."
  10791. (save-excursion
  10792. (save-restriction
  10793. (widen)
  10794. (goto-char (point-min))
  10795. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10796. values)
  10797. (while (re-search-forward re nil t)
  10798. (add-to-list 'values (org-trim (match-string 1))))
  10799. (delete "" values)))))
  10800. (defun org-insert-property-drawer ()
  10801. "Insert a property drawer into the current entry."
  10802. (interactive)
  10803. (org-back-to-heading t)
  10804. (looking-at outline-regexp)
  10805. (let ((indent (if org-adapt-indentation
  10806. (- (match-end 0)(match-beginning 0))
  10807. 0))
  10808. (beg (point))
  10809. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10810. end hiddenp)
  10811. (outline-next-heading)
  10812. (setq end (point))
  10813. (goto-char beg)
  10814. (while (re-search-forward re end t))
  10815. (setq hiddenp (org-invisible-p))
  10816. (end-of-line 1)
  10817. (and (equal (char-after) ?\n) (forward-char 1))
  10818. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10819. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10820. ;; just skip this line
  10821. (beginning-of-line 2)
  10822. ;; Drawer start, find the end
  10823. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10824. (beginning-of-line 1)))
  10825. (org-skip-over-state-notes)
  10826. (skip-chars-backward " \t\n\r")
  10827. (if (eq (char-before) ?*) (forward-char 1))
  10828. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10829. (beginning-of-line 0)
  10830. (org-indent-to-column indent)
  10831. (beginning-of-line 2)
  10832. (org-indent-to-column indent)
  10833. (beginning-of-line 0)
  10834. (if hiddenp
  10835. (save-excursion
  10836. (org-back-to-heading t)
  10837. (hide-entry))
  10838. (org-flag-drawer t))))
  10839. (defun org-set-property (property value)
  10840. "In the current entry, set PROPERTY to VALUE.
  10841. When called interactively, this will prompt for a property name, offering
  10842. completion on existing and default properties. And then it will prompt
  10843. for a value, offering completion either on allowed values (via an inherited
  10844. xxx_ALL property) or on existing values in other instances of this property
  10845. in the current file."
  10846. (interactive
  10847. (let* ((completion-ignore-case t)
  10848. (keys (org-buffer-property-keys nil t t))
  10849. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10850. (prop (if (member prop0 keys)
  10851. prop0
  10852. (or (cdr (assoc (downcase prop0)
  10853. (mapcar (lambda (x) (cons (downcase x) x))
  10854. keys)))
  10855. prop0)))
  10856. (cur (org-entry-get nil prop))
  10857. (allowed (org-property-get-allowed-values nil prop 'table))
  10858. (existing (mapcar 'list (org-property-values prop)))
  10859. (val (if allowed
  10860. (org-completing-read "Value: " allowed nil 'req-match)
  10861. (let (org-completion-use-ido)
  10862. (org-completing-read
  10863. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10864. (concat "[" cur "]") "")
  10865. ": ")
  10866. existing nil nil "" nil cur)))))
  10867. (list prop (if (equal val "") cur val))))
  10868. (unless (equal (org-entry-get nil property) value)
  10869. (org-entry-put nil property value)))
  10870. (defun org-delete-property (property)
  10871. "In the current entry, delete PROPERTY."
  10872. (interactive
  10873. (let* ((completion-ignore-case t)
  10874. (prop (org-ido-completing-read
  10875. "Property: " (org-entry-properties nil 'standard))))
  10876. (list prop)))
  10877. (message "Property %s %s" property
  10878. (if (org-entry-delete nil property)
  10879. "deleted"
  10880. "was not present in the entry")))
  10881. (defun org-delete-property-globally (property)
  10882. "Remove PROPERTY globally, from all entries."
  10883. (interactive
  10884. (let* ((completion-ignore-case t)
  10885. (prop (org-ido-completing-read
  10886. "Globally remove property: "
  10887. (mapcar 'list (org-buffer-property-keys)))))
  10888. (list prop)))
  10889. (save-excursion
  10890. (save-restriction
  10891. (widen)
  10892. (goto-char (point-min))
  10893. (let ((cnt 0))
  10894. (while (re-search-forward
  10895. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10896. nil t)
  10897. (setq cnt (1+ cnt))
  10898. (replace-match ""))
  10899. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10900. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10901. (defun org-compute-property-at-point ()
  10902. "Compute the property at point.
  10903. This looks for an enclosing column format, extracts the operator and
  10904. then applies it to the property in the column format's scope."
  10905. (interactive)
  10906. (unless (org-at-property-p)
  10907. (error "Not at a property"))
  10908. (let ((prop (org-match-string-no-properties 2)))
  10909. (org-columns-get-format-and-top-level)
  10910. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10911. (error "No operator defined for property %s" prop))
  10912. (org-columns-compute prop)))
  10913. (defun org-property-get-allowed-values (pom property &optional table)
  10914. "Get allowed values for the property PROPERTY.
  10915. When TABLE is non-nil, return an alist that can directly be used for
  10916. completion."
  10917. (let (vals)
  10918. (cond
  10919. ((equal property "TODO")
  10920. (setq vals (org-with-point-at pom
  10921. (append org-todo-keywords-1 '("")))))
  10922. ((equal property "PRIORITY")
  10923. (let ((n org-lowest-priority))
  10924. (while (>= n org-highest-priority)
  10925. (push (char-to-string n) vals)
  10926. (setq n (1- n)))))
  10927. ((member property org-special-properties))
  10928. (t
  10929. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10930. (when (and vals (string-match "\\S-" vals))
  10931. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10932. (setq vals (mapcar (lambda (x)
  10933. (cond ((stringp x) x)
  10934. ((numberp x) (number-to-string x))
  10935. ((symbolp x) (symbol-name x))
  10936. (t "???")))
  10937. vals)))))
  10938. (if table (mapcar 'list vals) vals)))
  10939. (defun org-property-previous-allowed-value (&optional previous)
  10940. "Switch to the next allowed value for this property."
  10941. (interactive)
  10942. (org-property-next-allowed-value t))
  10943. (defun org-property-next-allowed-value (&optional previous)
  10944. "Switch to the next allowed value for this property."
  10945. (interactive)
  10946. (unless (org-at-property-p)
  10947. (error "Not at a property"))
  10948. (let* ((key (match-string 2))
  10949. (value (match-string 3))
  10950. (allowed (or (org-property-get-allowed-values (point) key)
  10951. (and (member value '("[ ]" "[-]" "[X]"))
  10952. '("[ ]" "[X]"))))
  10953. nval)
  10954. (unless allowed
  10955. (error "Allowed values for this property have not been defined"))
  10956. (if previous (setq allowed (reverse allowed)))
  10957. (if (member value allowed)
  10958. (setq nval (car (cdr (member value allowed)))))
  10959. (setq nval (or nval (car allowed)))
  10960. (if (equal nval value)
  10961. (error "Only one allowed value for this property"))
  10962. (org-at-property-p)
  10963. (replace-match (concat " :" key ": " nval) t t)
  10964. (org-indent-line-function)
  10965. (beginning-of-line 1)
  10966. (skip-chars-forward " \t")))
  10967. (defun org-find-entry-with-id (ident)
  10968. "Locate the entry that contains the ID property with exact value IDENT.
  10969. IDENT can be a string, a symbol or a number, this function will search for
  10970. the string representation of it.
  10971. Return the position where this entry starts, or nil if there is no such entry."
  10972. (interactive "sID: ")
  10973. (let ((id (cond
  10974. ((stringp ident) ident)
  10975. ((symbol-name ident) (symbol-name ident))
  10976. ((numberp ident) (number-to-string ident))
  10977. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10978. (case-fold-search nil))
  10979. (save-excursion
  10980. (save-restriction
  10981. (widen)
  10982. (goto-char (point-min))
  10983. (when (re-search-forward
  10984. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10985. nil t)
  10986. (org-back-to-heading t)
  10987. (point))))))
  10988. ;;;; Timestamps
  10989. (defvar org-last-changed-timestamp nil)
  10990. (defvar org-last-inserted-timestamp nil
  10991. "The last time stamp inserted with `org-insert-time-stamp'.")
  10992. (defvar org-time-was-given) ; dynamically scoped parameter
  10993. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10994. (defvar org-ts-what) ; dynamically scoped parameter
  10995. (defun org-time-stamp (arg &optional inactive)
  10996. "Prompt for a date/time and insert a time stamp.
  10997. If the user specifies a time like HH:MM, or if this command is called
  10998. with a prefix argument, the time stamp will contain date and time.
  10999. Otherwise, only the date will be included. All parts of a date not
  11000. specified by the user will be filled in from the current date/time.
  11001. So if you press just return without typing anything, the time stamp
  11002. will represent the current date/time. If there is already a timestamp
  11003. at the cursor, it will be modified."
  11004. (interactive "P")
  11005. (let* ((ts nil)
  11006. (default-time
  11007. ;; Default time is either today, or, when entering a range,
  11008. ;; the range start.
  11009. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11010. (save-excursion
  11011. (re-search-backward
  11012. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11013. (- (point) 20) t)))
  11014. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11015. (current-time)))
  11016. (default-input (and ts (org-get-compact-tod ts)))
  11017. org-time-was-given org-end-time-was-given time)
  11018. (cond
  11019. ((and (org-at-timestamp-p t)
  11020. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11021. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11022. (insert "--")
  11023. (setq time (let ((this-command this-command))
  11024. (org-read-date arg 'totime nil nil
  11025. default-time default-input)))
  11026. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11027. ((org-at-timestamp-p t)
  11028. (setq time (let ((this-command this-command))
  11029. (org-read-date arg 'totime nil nil default-time default-input)))
  11030. (when (org-at-timestamp-p t) ; just to get the match data
  11031. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11032. (replace-match "")
  11033. (setq org-last-changed-timestamp
  11034. (org-insert-time-stamp
  11035. time (or org-time-was-given arg)
  11036. inactive nil nil (list org-end-time-was-given))))
  11037. (message "Timestamp updated"))
  11038. (t
  11039. (setq time (let ((this-command this-command))
  11040. (org-read-date arg 'totime nil nil default-time default-input)))
  11041. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11042. nil nil (list org-end-time-was-given))))))
  11043. ;; FIXME: can we use this for something else, like computing time differences?
  11044. (defun org-get-compact-tod (s)
  11045. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11046. (let* ((t1 (match-string 1 s))
  11047. (h1 (string-to-number (match-string 2 s)))
  11048. (m1 (string-to-number (match-string 3 s)))
  11049. (t2 (and (match-end 4) (match-string 5 s)))
  11050. (h2 (and t2 (string-to-number (match-string 6 s))))
  11051. (m2 (and t2 (string-to-number (match-string 7 s))))
  11052. dh dm)
  11053. (if (not t2)
  11054. t1
  11055. (setq dh (- h2 h1) dm (- m2 m1))
  11056. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11057. (concat t1 "+" (number-to-string dh)
  11058. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11059. (defun org-time-stamp-inactive (&optional arg)
  11060. "Insert an inactive time stamp.
  11061. An inactive time stamp is enclosed in square brackets instead of angle
  11062. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11063. does not link to the calendar and cannot be changed with the S-cursor keys.
  11064. So these are more for recording a certain time/date."
  11065. (interactive "P")
  11066. (org-time-stamp arg 'inactive))
  11067. (defvar org-date-ovl (org-make-overlay 1 1))
  11068. (org-overlay-put org-date-ovl 'face 'org-warning)
  11069. (org-detach-overlay org-date-ovl)
  11070. (defvar org-ans1) ; dynamically scoped parameter
  11071. (defvar org-ans2) ; dynamically scoped parameter
  11072. (defvar org-plain-time-of-day-regexp) ; defined below
  11073. (defvar org-overriding-default-time nil) ; dynamically scoped
  11074. (defvar org-read-date-overlay nil)
  11075. (defvar org-dcst nil) ; dynamically scoped
  11076. (defvar org-read-date-history nil)
  11077. (defvar org-read-date-final-answer nil)
  11078. (defun org-read-date (&optional with-time to-time from-string prompt
  11079. default-time default-input)
  11080. "Read a date, possibly a time, and make things smooth for the user.
  11081. The prompt will suggest to enter an ISO date, but you can also enter anything
  11082. which will at least partially be understood by `parse-time-string'.
  11083. Unrecognized parts of the date will default to the current day, month, year,
  11084. hour and minute. If this command is called to replace a timestamp at point,
  11085. of to enter the second timestamp of a range, the default time is taken from the
  11086. existing stamp. For example,
  11087. 3-2-5 --> 2003-02-05
  11088. feb 15 --> currentyear-02-15
  11089. sep 12 9 --> 2009-09-12
  11090. 12:45 --> today 12:45
  11091. 22 sept 0:34 --> currentyear-09-22 0:34
  11092. 12 --> currentyear-currentmonth-12
  11093. Fri --> nearest Friday (today or later)
  11094. etc.
  11095. Furthermore you can specify a relative date by giving, as the *first* thing
  11096. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11097. change in days weeks, months, years.
  11098. With a single plus or minus, the date is relative to today. With a double
  11099. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11100. +4d --> four days from today
  11101. +4 --> same as above
  11102. +2w --> two weeks from today
  11103. ++5 --> five days from default date
  11104. The function understands only English month and weekday abbreviations,
  11105. but this can be configured with the variables `parse-time-months' and
  11106. `parse-time-weekdays'.
  11107. While prompting, a calendar is popped up - you can also select the
  11108. date with the mouse (button 1). The calendar shows a period of three
  11109. months. To scroll it to other months, use the keys `>' and `<'.
  11110. If you don't like the calendar, turn it off with
  11111. \(setq org-read-date-popup-calendar nil)
  11112. With optional argument TO-TIME, the date will immediately be converted
  11113. to an internal time.
  11114. With an optional argument WITH-TIME, the prompt will suggest to also
  11115. insert a time. Note that when WITH-TIME is not set, you can still
  11116. enter a time, and this function will inform the calling routine about
  11117. this change. The calling routine may then choose to change the format
  11118. used to insert the time stamp into the buffer to include the time.
  11119. With optional argument FROM-STRING, read from this string instead from
  11120. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11121. the time/date that is used for everything that is not specified by the
  11122. user."
  11123. (require 'parse-time)
  11124. (let* ((org-time-stamp-rounding-minutes
  11125. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11126. (org-dcst org-display-custom-times)
  11127. (ct (org-current-time))
  11128. (def (or org-overriding-default-time default-time ct))
  11129. (defdecode (decode-time def))
  11130. (dummy (progn
  11131. (when (< (nth 2 defdecode) org-extend-today-until)
  11132. (setcar (nthcdr 2 defdecode) -1)
  11133. (setcar (nthcdr 1 defdecode) 59)
  11134. (setq def (apply 'encode-time defdecode)
  11135. defdecode (decode-time def)))))
  11136. (calendar-frame-setup nil)
  11137. (calendar-move-hook nil)
  11138. (calendar-view-diary-initially-flag nil)
  11139. (view-diary-entries-initially nil)
  11140. (calendar-view-holidays-initially-flag nil)
  11141. (view-calendar-holidays-initially nil)
  11142. (timestr (format-time-string
  11143. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11144. (prompt (concat (if prompt (concat prompt " ") "")
  11145. (format "Date+time [%s]: " timestr)))
  11146. ans (org-ans0 "") org-ans1 org-ans2 final)
  11147. (cond
  11148. (from-string (setq ans from-string))
  11149. (org-read-date-popup-calendar
  11150. (save-excursion
  11151. (save-window-excursion
  11152. (calendar)
  11153. (calendar-forward-day (- (time-to-days def)
  11154. (calendar-absolute-from-gregorian
  11155. (calendar-current-date))))
  11156. (org-eval-in-calendar nil t)
  11157. (let* ((old-map (current-local-map))
  11158. (map (copy-keymap calendar-mode-map))
  11159. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11160. (org-defkey map (kbd "RET") 'org-calendar-select)
  11161. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11162. 'org-calendar-select-mouse)
  11163. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11164. 'org-calendar-select-mouse)
  11165. (org-defkey minibuffer-local-map [(meta shift left)]
  11166. (lambda () (interactive)
  11167. (org-eval-in-calendar '(calendar-backward-month 1))))
  11168. (org-defkey minibuffer-local-map [(meta shift right)]
  11169. (lambda () (interactive)
  11170. (org-eval-in-calendar '(calendar-forward-month 1))))
  11171. (org-defkey minibuffer-local-map [(meta shift up)]
  11172. (lambda () (interactive)
  11173. (org-eval-in-calendar '(calendar-backward-year 1))))
  11174. (org-defkey minibuffer-local-map [(meta shift down)]
  11175. (lambda () (interactive)
  11176. (org-eval-in-calendar '(calendar-forward-year 1))))
  11177. (org-defkey minibuffer-local-map [?\e (shift left)]
  11178. (lambda () (interactive)
  11179. (org-eval-in-calendar '(calendar-backward-month 1))))
  11180. (org-defkey minibuffer-local-map [?\e (shift right)]
  11181. (lambda () (interactive)
  11182. (org-eval-in-calendar '(calendar-forward-month 1))))
  11183. (org-defkey minibuffer-local-map [?\e (shift up)]
  11184. (lambda () (interactive)
  11185. (org-eval-in-calendar '(calendar-backward-year 1))))
  11186. (org-defkey minibuffer-local-map [?\e (shift down)]
  11187. (lambda () (interactive)
  11188. (org-eval-in-calendar '(calendar-forward-year 1))))
  11189. (org-defkey minibuffer-local-map [(shift up)]
  11190. (lambda () (interactive)
  11191. (org-eval-in-calendar '(calendar-backward-week 1))))
  11192. (org-defkey minibuffer-local-map [(shift down)]
  11193. (lambda () (interactive)
  11194. (org-eval-in-calendar '(calendar-forward-week 1))))
  11195. (org-defkey minibuffer-local-map [(shift left)]
  11196. (lambda () (interactive)
  11197. (org-eval-in-calendar '(calendar-backward-day 1))))
  11198. (org-defkey minibuffer-local-map [(shift right)]
  11199. (lambda () (interactive)
  11200. (org-eval-in-calendar '(calendar-forward-day 1))))
  11201. (org-defkey minibuffer-local-map ">"
  11202. (lambda () (interactive)
  11203. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11204. (org-defkey minibuffer-local-map "<"
  11205. (lambda () (interactive)
  11206. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11207. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11208. (unwind-protect
  11209. (progn
  11210. (use-local-map map)
  11211. (add-hook 'post-command-hook 'org-read-date-display)
  11212. (setq org-ans0 (read-string prompt default-input
  11213. 'org-read-date-history nil))
  11214. ;; org-ans0: from prompt
  11215. ;; org-ans1: from mouse click
  11216. ;; org-ans2: from calendar motion
  11217. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11218. (remove-hook 'post-command-hook 'org-read-date-display)
  11219. (use-local-map old-map)
  11220. (when org-read-date-overlay
  11221. (org-delete-overlay org-read-date-overlay)
  11222. (setq org-read-date-overlay nil)))))))
  11223. (t ; Naked prompt only
  11224. (unwind-protect
  11225. (setq ans (read-string prompt default-input
  11226. 'org-read-date-history timestr))
  11227. (when org-read-date-overlay
  11228. (org-delete-overlay org-read-date-overlay)
  11229. (setq org-read-date-overlay nil)))))
  11230. (setq final (org-read-date-analyze ans def defdecode))
  11231. (setq org-read-date-final-answer ans)
  11232. (if to-time
  11233. (apply 'encode-time final)
  11234. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11235. (format "%04d-%02d-%02d %02d:%02d"
  11236. (nth 5 final) (nth 4 final) (nth 3 final)
  11237. (nth 2 final) (nth 1 final))
  11238. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11239. (defvar def)
  11240. (defvar defdecode)
  11241. (defvar with-time)
  11242. (defun org-read-date-display ()
  11243. "Display the current date prompt interpretation in the minibuffer."
  11244. (when org-read-date-display-live
  11245. (when org-read-date-overlay
  11246. (org-delete-overlay org-read-date-overlay))
  11247. (let ((p (point)))
  11248. (end-of-line 1)
  11249. (while (not (equal (buffer-substring
  11250. (max (point-min) (- (point) 4)) (point))
  11251. " "))
  11252. (insert " "))
  11253. (goto-char p))
  11254. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11255. " " (or org-ans1 org-ans2)))
  11256. (org-end-time-was-given nil)
  11257. (f (org-read-date-analyze ans def defdecode))
  11258. (fmts (if org-dcst
  11259. org-time-stamp-custom-formats
  11260. org-time-stamp-formats))
  11261. (fmt (if (or with-time
  11262. (and (boundp 'org-time-was-given) org-time-was-given))
  11263. (cdr fmts)
  11264. (car fmts)))
  11265. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11266. (when (and org-end-time-was-given
  11267. (string-match org-plain-time-of-day-regexp txt))
  11268. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11269. org-end-time-was-given
  11270. (substring txt (match-end 0)))))
  11271. (setq org-read-date-overlay
  11272. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11273. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11274. (defun org-read-date-analyze (ans def defdecode)
  11275. "Analyse the combined answer of the date prompt."
  11276. ;; FIXME: cleanup and comment
  11277. (let (delta deltan deltaw deltadef year month day
  11278. hour minute second wday pm h2 m2 tl wday1
  11279. iso-year iso-weekday iso-week iso-year iso-date)
  11280. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11281. (setq ans "+0"))
  11282. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11283. (setq ans (replace-match "" t t ans)
  11284. deltan (car delta)
  11285. deltaw (nth 1 delta)
  11286. deltadef (nth 2 delta)))
  11287. ;; Check if there is an iso week date in there
  11288. ;; If yes, sore the info and postpone interpreting it until the rest
  11289. ;; of the parsing is done
  11290. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11291. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11292. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11293. iso-week (string-to-number (match-string 2 ans)))
  11294. (setq ans (replace-match "" t t ans)))
  11295. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11296. (when (string-match
  11297. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11298. (setq year (if (match-end 2)
  11299. (string-to-number (match-string 2 ans))
  11300. (string-to-number (format-time-string "%Y")))
  11301. month (string-to-number (match-string 3 ans))
  11302. day (string-to-number (match-string 4 ans)))
  11303. (if (< year 100) (setq year (+ 2000 year)))
  11304. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11305. t nil ans)))
  11306. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11307. ;; If there is a time with am/pm, and *no* time without it, we convert
  11308. ;; so that matching will be successful.
  11309. (loop for i from 1 to 2 do ; twice, for end time as well
  11310. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11311. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11312. (setq hour (string-to-number (match-string 1 ans))
  11313. minute (if (match-end 3)
  11314. (string-to-number (match-string 3 ans))
  11315. 0)
  11316. pm (equal ?p
  11317. (string-to-char (downcase (match-string 4 ans)))))
  11318. (if (and (= hour 12) (not pm))
  11319. (setq hour 0)
  11320. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11321. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11322. t t ans))))
  11323. ;; Check if a time range is given as a duration
  11324. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11325. (setq hour (string-to-number (match-string 1 ans))
  11326. h2 (+ hour (string-to-number (match-string 3 ans)))
  11327. minute (string-to-number (match-string 2 ans))
  11328. m2 (+ minute (if (match-end 5) (string-to-number
  11329. (match-string 5 ans))0)))
  11330. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11331. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11332. t t ans)))
  11333. ;; Check if there is a time range
  11334. (when (boundp 'org-end-time-was-given)
  11335. (setq org-time-was-given nil)
  11336. (when (and (string-match org-plain-time-of-day-regexp ans)
  11337. (match-end 8))
  11338. (setq org-end-time-was-given (match-string 8 ans))
  11339. (setq ans (concat (substring ans 0 (match-beginning 7))
  11340. (substring ans (match-end 7))))))
  11341. (setq tl (parse-time-string ans)
  11342. day (or (nth 3 tl) (nth 3 defdecode))
  11343. month (or (nth 4 tl)
  11344. (if (and org-read-date-prefer-future
  11345. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11346. (1+ (nth 4 defdecode))
  11347. (nth 4 defdecode)))
  11348. year (or (nth 5 tl)
  11349. (if (and org-read-date-prefer-future
  11350. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11351. (1+ (nth 5 defdecode))
  11352. (nth 5 defdecode)))
  11353. hour (or (nth 2 tl) (nth 2 defdecode))
  11354. minute (or (nth 1 tl) (nth 1 defdecode))
  11355. second (or (nth 0 tl) 0)
  11356. wday (nth 6 tl))
  11357. ;; Special date definitions below
  11358. (cond
  11359. (iso-week
  11360. ;; There was an iso week
  11361. (setq year (or iso-year year)
  11362. day (or iso-weekday wday 1)
  11363. wday nil ; to make sure that the trigger below does not match
  11364. iso-date (calendar-gregorian-from-absolute
  11365. (calendar-absolute-from-iso
  11366. (list iso-week day year))))
  11367. ; FIXME: Should we also push ISO weeks into the future?
  11368. ; (when (and org-read-date-prefer-future
  11369. ; (not iso-year)
  11370. ; (< (calendar-absolute-from-gregorian iso-date)
  11371. ; (time-to-days (current-time))))
  11372. ; (setq year (1+ year)
  11373. ; iso-date (calendar-gregorian-from-absolute
  11374. ; (calendar-absolute-from-iso
  11375. ; (list iso-week day year)))))
  11376. (setq month (car iso-date)
  11377. year (nth 2 iso-date)
  11378. day (nth 1 iso-date)))
  11379. (deltan
  11380. (unless deltadef
  11381. (let ((now (decode-time (current-time))))
  11382. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11383. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11384. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11385. ((equal deltaw "m") (setq month (+ month deltan)))
  11386. ((equal deltaw "y") (setq year (+ year deltan)))))
  11387. ((and wday (not (nth 3 tl)))
  11388. ;; Weekday was given, but no day, so pick that day in the week
  11389. ;; on or after the derived date.
  11390. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11391. (unless (equal wday wday1)
  11392. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11393. (if (and (boundp 'org-time-was-given)
  11394. (nth 2 tl))
  11395. (setq org-time-was-given t))
  11396. (if (< year 100) (setq year (+ 2000 year)))
  11397. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11398. (list second minute hour day month year)))
  11399. (defvar parse-time-weekdays)
  11400. (defun org-read-date-get-relative (s today default)
  11401. "Check string S for special relative date string.
  11402. TODAY and DEFAULT are internal times, for today and for a default.
  11403. Return shift list (N what def-flag)
  11404. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11405. N is the number of WHATs to shift.
  11406. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11407. the DEFAULT date rather than TODAY."
  11408. (when (and
  11409. (string-match
  11410. (concat
  11411. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11412. "\\([0-9]+\\)?"
  11413. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11414. "\\([ \t]\\|$\\)") s)
  11415. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11416. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11417. (string-to-char (substring (match-string 1 s) -1))
  11418. ?+))
  11419. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11420. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11421. (what (if (match-end 3) (match-string 3 s) "d"))
  11422. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11423. (date (if rel default today))
  11424. (wday (nth 6 (decode-time date)))
  11425. delta)
  11426. (if wday1
  11427. (progn
  11428. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11429. (if (= dir ?-) (setq delta (- delta 7)))
  11430. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11431. (list delta "d" rel))
  11432. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11433. (defun org-eval-in-calendar (form &optional keepdate)
  11434. "Eval FORM in the calendar window and return to current window.
  11435. Also, store the cursor date in variable org-ans2."
  11436. (let ((sf (selected-frame))
  11437. (sw (selected-window)))
  11438. (select-window (get-buffer-window "*Calendar*" t))
  11439. (eval form)
  11440. (when (and (not keepdate) (calendar-cursor-to-date))
  11441. (let* ((date (calendar-cursor-to-date))
  11442. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11443. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11444. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11445. (select-window sw)
  11446. (select-frame-set-input-focus sf)))
  11447. (defun org-calendar-select ()
  11448. "Return to `org-read-date' with the date currently selected.
  11449. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11450. (interactive)
  11451. (when (calendar-cursor-to-date)
  11452. (let* ((date (calendar-cursor-to-date))
  11453. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11454. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11455. (if (active-minibuffer-window) (exit-minibuffer))))
  11456. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11457. "Insert a date stamp for the date given by the internal TIME.
  11458. WITH-HM means, use the stamp format that includes the time of the day.
  11459. INACTIVE means use square brackets instead of angular ones, so that the
  11460. stamp will not contribute to the agenda.
  11461. PRE and POST are optional strings to be inserted before and after the
  11462. stamp.
  11463. The command returns the inserted time stamp."
  11464. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11465. stamp)
  11466. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11467. (insert-before-markers (or pre ""))
  11468. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11469. (when (listp extra)
  11470. (setq extra (car extra))
  11471. (if (and (stringp extra)
  11472. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11473. (setq extra (format "-%02d:%02d"
  11474. (string-to-number (match-string 1 extra))
  11475. (string-to-number (match-string 2 extra))))
  11476. (setq extra nil)))
  11477. (when extra
  11478. (backward-char 1)
  11479. (insert-before-markers extra)
  11480. (forward-char 1))
  11481. (insert-before-markers (or post ""))
  11482. (setq org-last-inserted-timestamp stamp)))
  11483. (defun org-toggle-time-stamp-overlays ()
  11484. "Toggle the use of custom time stamp formats."
  11485. (interactive)
  11486. (setq org-display-custom-times (not org-display-custom-times))
  11487. (unless org-display-custom-times
  11488. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11489. (while (setq p (next-single-property-change p 'display))
  11490. (if (and (get-text-property p 'display)
  11491. (eq (get-text-property p 'face) 'org-date))
  11492. (remove-text-properties
  11493. p (setq p (next-single-property-change p 'display))
  11494. '(display t))))
  11495. (set-buffer-modified-p bmp)))
  11496. (if (featurep 'xemacs)
  11497. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11498. (org-restart-font-lock)
  11499. (setq org-table-may-need-update t)
  11500. (if org-display-custom-times
  11501. (message "Time stamps are overlayed with custom format")
  11502. (message "Time stamp overlays removed")))
  11503. (defun org-display-custom-time (beg end)
  11504. "Overlay modified time stamp format over timestamp between BEG and END."
  11505. (let* ((ts (buffer-substring beg end))
  11506. t1 w1 with-hm tf time str w2 (off 0))
  11507. (save-match-data
  11508. (setq t1 (org-parse-time-string ts t))
  11509. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11510. (setq off (- (match-end 0) (match-beginning 0)))))
  11511. (setq end (- end off))
  11512. (setq w1 (- end beg)
  11513. with-hm (and (nth 1 t1) (nth 2 t1))
  11514. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11515. time (org-fix-decoded-time t1)
  11516. str (org-add-props
  11517. (format-time-string
  11518. (substring tf 1 -1) (apply 'encode-time time))
  11519. nil 'mouse-face 'highlight)
  11520. w2 (length str))
  11521. (if (not (= w2 w1))
  11522. (add-text-properties (1+ beg) (+ 2 beg)
  11523. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11524. (if (featurep 'xemacs)
  11525. (progn
  11526. (put-text-property beg end 'invisible t)
  11527. (put-text-property beg end 'end-glyph (make-glyph str)))
  11528. (put-text-property beg end 'display str))))
  11529. (defun org-translate-time (string)
  11530. "Translate all timestamps in STRING to custom format.
  11531. But do this only if the variable `org-display-custom-times' is set."
  11532. (when org-display-custom-times
  11533. (save-match-data
  11534. (let* ((start 0)
  11535. (re org-ts-regexp-both)
  11536. t1 with-hm inactive tf time str beg end)
  11537. (while (setq start (string-match re string start))
  11538. (setq beg (match-beginning 0)
  11539. end (match-end 0)
  11540. t1 (save-match-data
  11541. (org-parse-time-string (substring string beg end) t))
  11542. with-hm (and (nth 1 t1) (nth 2 t1))
  11543. inactive (equal (substring string beg (1+ beg)) "[")
  11544. tf (funcall (if with-hm 'cdr 'car)
  11545. org-time-stamp-custom-formats)
  11546. time (org-fix-decoded-time t1)
  11547. str (format-time-string
  11548. (concat
  11549. (if inactive "[" "<") (substring tf 1 -1)
  11550. (if inactive "]" ">"))
  11551. (apply 'encode-time time))
  11552. string (replace-match str t t string)
  11553. start (+ start (length str)))))))
  11554. string)
  11555. (defun org-fix-decoded-time (time)
  11556. "Set 0 instead of nil for the first 6 elements of time.
  11557. Don't touch the rest."
  11558. (let ((n 0))
  11559. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11560. (defun org-days-to-time (timestamp-string)
  11561. "Difference between TIMESTAMP-STRING and now in days."
  11562. (- (time-to-days (org-time-string-to-time timestamp-string))
  11563. (time-to-days (current-time))))
  11564. (defun org-deadline-close (timestamp-string &optional ndays)
  11565. "Is the time in TIMESTAMP-STRING close to the current date?"
  11566. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11567. (and (< (org-days-to-time timestamp-string) ndays)
  11568. (not (org-entry-is-done-p))))
  11569. (defun org-get-wdays (ts)
  11570. "Get the deadline lead time appropriate for timestring TS."
  11571. (cond
  11572. ((<= org-deadline-warning-days 0)
  11573. ;; 0 or negative, enforce this value no matter what
  11574. (- org-deadline-warning-days))
  11575. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11576. ;; lead time is specified.
  11577. (floor (* (string-to-number (match-string 1 ts))
  11578. (cdr (assoc (match-string 2 ts)
  11579. '(("d" . 1) ("w" . 7)
  11580. ("m" . 30.4) ("y" . 365.25)))))))
  11581. ;; go for the default.
  11582. (t org-deadline-warning-days)))
  11583. (defun org-calendar-select-mouse (ev)
  11584. "Return to `org-read-date' with the date currently selected.
  11585. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11586. (interactive "e")
  11587. (mouse-set-point ev)
  11588. (when (calendar-cursor-to-date)
  11589. (let* ((date (calendar-cursor-to-date))
  11590. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11591. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11592. (if (active-minibuffer-window) (exit-minibuffer))))
  11593. (defun org-check-deadlines (ndays)
  11594. "Check if there are any deadlines due or past due.
  11595. A deadline is considered due if it happens within `org-deadline-warning-days'
  11596. days from today's date. If the deadline appears in an entry marked DONE,
  11597. it is not shown. The prefix arg NDAYS can be used to test that many
  11598. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11599. (interactive "P")
  11600. (let* ((org-warn-days
  11601. (cond
  11602. ((equal ndays '(4)) 100000)
  11603. (ndays (prefix-numeric-value ndays))
  11604. (t (abs org-deadline-warning-days))))
  11605. (case-fold-search nil)
  11606. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11607. (callback
  11608. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11609. (message "%d deadlines past-due or due within %d days"
  11610. (org-occur regexp nil callback)
  11611. org-warn-days)))
  11612. (defun org-check-before-date (date)
  11613. "Check if there are deadlines or scheduled entries before DATE."
  11614. (interactive (list (org-read-date)))
  11615. (let ((case-fold-search nil)
  11616. (regexp (concat "\\<\\(" org-deadline-string
  11617. "\\|" org-scheduled-string
  11618. "\\) *<\\([^>]+\\)>"))
  11619. (callback
  11620. (lambda () (time-less-p
  11621. (org-time-string-to-time (match-string 2))
  11622. (org-time-string-to-time date)))))
  11623. (message "%d entries before %s"
  11624. (org-occur regexp nil callback) date)))
  11625. (defun org-check-after-date (date)
  11626. "Check if there are deadlines or scheduled entries after DATE."
  11627. (interactive (list (org-read-date)))
  11628. (let ((case-fold-search nil)
  11629. (regexp (concat "\\<\\(" org-deadline-string
  11630. "\\|" org-scheduled-string
  11631. "\\) *<\\([^>]+\\)>"))
  11632. (callback
  11633. (lambda () (not
  11634. (time-less-p
  11635. (org-time-string-to-time (match-string 2))
  11636. (org-time-string-to-time date))))))
  11637. (message "%d entries after %s"
  11638. (org-occur regexp nil callback) date)))
  11639. (defun org-evaluate-time-range (&optional to-buffer)
  11640. "Evaluate a time range by computing the difference between start and end.
  11641. Normally the result is just printed in the echo area, but with prefix arg
  11642. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11643. If the time range is actually in a table, the result is inserted into the
  11644. next column.
  11645. For time difference computation, a year is assumed to be exactly 365
  11646. days in order to avoid rounding problems."
  11647. (interactive "P")
  11648. (or
  11649. (org-clock-update-time-maybe)
  11650. (save-excursion
  11651. (unless (org-at-date-range-p t)
  11652. (goto-char (point-at-bol))
  11653. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11654. (if (not (org-at-date-range-p t))
  11655. (error "Not at a time-stamp range, and none found in current line")))
  11656. (let* ((ts1 (match-string 1))
  11657. (ts2 (match-string 2))
  11658. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11659. (match-end (match-end 0))
  11660. (time1 (org-time-string-to-time ts1))
  11661. (time2 (org-time-string-to-time ts2))
  11662. (t1 (time-to-seconds time1))
  11663. (t2 (time-to-seconds time2))
  11664. (diff (abs (- t2 t1)))
  11665. (negative (< (- t2 t1) 0))
  11666. ;; (ys (floor (* 365 24 60 60)))
  11667. (ds (* 24 60 60))
  11668. (hs (* 60 60))
  11669. (fy "%dy %dd %02d:%02d")
  11670. (fy1 "%dy %dd")
  11671. (fd "%dd %02d:%02d")
  11672. (fd1 "%dd")
  11673. (fh "%02d:%02d")
  11674. y d h m align)
  11675. (if havetime
  11676. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11677. y 0
  11678. d (floor (/ diff ds)) diff (mod diff ds)
  11679. h (floor (/ diff hs)) diff (mod diff hs)
  11680. m (floor (/ diff 60)))
  11681. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11682. y 0
  11683. d (floor (+ (/ diff ds) 0.5))
  11684. h 0 m 0))
  11685. (if (not to-buffer)
  11686. (message "%s" (org-make-tdiff-string y d h m))
  11687. (if (org-at-table-p)
  11688. (progn
  11689. (goto-char match-end)
  11690. (setq align t)
  11691. (and (looking-at " *|") (goto-char (match-end 0))))
  11692. (goto-char match-end))
  11693. (if (looking-at
  11694. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11695. (replace-match ""))
  11696. (if negative (insert " -"))
  11697. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11698. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11699. (insert " " (format fh h m))))
  11700. (if align (org-table-align))
  11701. (message "Time difference inserted")))))
  11702. (defun org-make-tdiff-string (y d h m)
  11703. (let ((fmt "")
  11704. (l nil))
  11705. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11706. l (push y l)))
  11707. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11708. l (push d l)))
  11709. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11710. l (push h l)))
  11711. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11712. l (push m l)))
  11713. (apply 'format fmt (nreverse l))))
  11714. (defun org-time-string-to-time (s)
  11715. (apply 'encode-time (org-parse-time-string s)))
  11716. (defun org-time-string-to-seconds (s)
  11717. (time-to-seconds (org-time-string-to-time s)))
  11718. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11719. "Convert a time stamp to an absolute day number.
  11720. If there is a specifyer for a cyclic time stamp, get the closest date to
  11721. DAYNR.
  11722. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  11723. the variable date is bound by the calendar when this is called."
  11724. (cond
  11725. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11726. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11727. daynr
  11728. (+ daynr 1000)))
  11729. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11730. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11731. (time-to-days (current-time))) (match-string 0 s)
  11732. prefer show-all))
  11733. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11734. (defun org-days-to-iso-week (days)
  11735. "Return the iso week number."
  11736. (require 'cal-iso)
  11737. (car (calendar-iso-from-absolute days)))
  11738. (defun org-small-year-to-year (year)
  11739. "Convert 2-digit years into 4-digit years.
  11740. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11741. The year 2000 cannot be abbreviated. Any year larger than 99
  11742. is returned unchanged."
  11743. (if (< year 38)
  11744. (setq year (+ 2000 year))
  11745. (if (< year 100)
  11746. (setq year (+ 1900 year))))
  11747. year)
  11748. (defun org-time-from-absolute (d)
  11749. "Return the time corresponding to date D.
  11750. D may be an absolute day number, or a calendar-type list (month day year)."
  11751. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11752. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11753. (defun org-calendar-holiday ()
  11754. "List of holidays, for Diary display in Org-mode."
  11755. (require 'holidays)
  11756. (let ((hl (funcall
  11757. (if (fboundp 'calendar-check-holidays)
  11758. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11759. (if hl (mapconcat 'identity hl "; "))))
  11760. (defun org-diary-sexp-entry (sexp entry date)
  11761. "Process a SEXP diary ENTRY for DATE."
  11762. (require 'diary-lib)
  11763. (let ((result (if calendar-debug-sexp
  11764. (let ((stack-trace-on-error t))
  11765. (eval (car (read-from-string sexp))))
  11766. (condition-case nil
  11767. (eval (car (read-from-string sexp)))
  11768. (error
  11769. (beep)
  11770. (message "Bad sexp at line %d in %s: %s"
  11771. (org-current-line)
  11772. (buffer-file-name) sexp)
  11773. (sleep-for 2))))))
  11774. (cond ((stringp result) result)
  11775. ((and (consp result)
  11776. (stringp (cdr result))) (cdr result))
  11777. (result entry)
  11778. (t nil))))
  11779. (defun org-diary-to-ical-string (frombuf)
  11780. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11781. This uses the icalendar.el library."
  11782. (let* ((tmpdir (if (featurep 'xemacs)
  11783. (temp-directory)
  11784. temporary-file-directory))
  11785. (tmpfile (make-temp-name
  11786. (expand-file-name "orgics" tmpdir)))
  11787. buf rtn b e)
  11788. (save-excursion
  11789. (set-buffer frombuf)
  11790. (icalendar-export-region (point-min) (point-max) tmpfile)
  11791. (setq buf (find-buffer-visiting tmpfile))
  11792. (set-buffer buf)
  11793. (goto-char (point-min))
  11794. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11795. (setq b (match-beginning 0)))
  11796. (goto-char (point-max))
  11797. (if (re-search-backward "^END:VEVENT" nil t)
  11798. (setq e (match-end 0)))
  11799. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11800. (kill-buffer buf)
  11801. (delete-file tmpfile)
  11802. rtn))
  11803. (defun org-closest-date (start current change prefer show-all)
  11804. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11805. When PREFER is `past' return a date that is either CURRENT or past.
  11806. When PREFER is `future', return a date that is either CURRENT or future.
  11807. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11808. ;; Make the proper lists from the dates
  11809. (catch 'exit
  11810. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11811. dn dw sday cday n1 n2 n0
  11812. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11813. (setq start (org-date-to-gregorian start)
  11814. current (org-date-to-gregorian
  11815. (if show-all
  11816. current
  11817. (time-to-days (current-time))))
  11818. sday (calendar-absolute-from-gregorian start)
  11819. cday (calendar-absolute-from-gregorian current))
  11820. (if (<= cday sday) (throw 'exit sday))
  11821. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11822. (setq dn (string-to-number (match-string 1 change))
  11823. dw (cdr (assoc (match-string 2 change) a1)))
  11824. (error "Invalid change specifyer: %s" change))
  11825. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11826. (cond
  11827. ((eq dw 'day)
  11828. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11829. n2 (+ n1 dn)))
  11830. ((eq dw 'year)
  11831. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11832. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11833. (setq date1 (list m d y1)
  11834. n1 (calendar-absolute-from-gregorian date1)
  11835. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11836. n2 (calendar-absolute-from-gregorian date2)))
  11837. ((eq dw 'month)
  11838. ;; approx number of month between the two dates
  11839. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11840. ;; How often does dn fit in there?
  11841. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11842. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11843. m (+ m nm)
  11844. ny (floor (/ m 12))
  11845. y (+ y ny)
  11846. m (- m (* ny 12)))
  11847. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11848. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11849. (setq m2 (+ m dn) y2 y)
  11850. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11851. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11852. (while (<= n2 cday)
  11853. (setq n1 n2 m m2 y y2)
  11854. (setq m2 (+ m dn) y2 y)
  11855. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11856. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11857. ;; Make sure n1 is the earlier date
  11858. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11859. (if show-all
  11860. (cond
  11861. ((eq prefer 'past) n1)
  11862. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11863. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11864. (cond
  11865. ((eq prefer 'past) n1)
  11866. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11867. (t (if (= cday n1) n1 n2)))))))
  11868. (defun org-date-to-gregorian (date)
  11869. "Turn any specification of DATE into a gregorian date for the calendar."
  11870. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11871. ((and (listp date) (= (length date) 3)) date)
  11872. ((stringp date)
  11873. (setq date (org-parse-time-string date))
  11874. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11875. ((listp date)
  11876. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11877. (defun org-parse-time-string (s &optional nodefault)
  11878. "Parse the standard Org-mode time string.
  11879. This should be a lot faster than the normal `parse-time-string'.
  11880. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11881. hour and minute fields will be nil if not given."
  11882. (if (string-match org-ts-regexp0 s)
  11883. (list 0
  11884. (if (or (match-beginning 8) (not nodefault))
  11885. (string-to-number (or (match-string 8 s) "0")))
  11886. (if (or (match-beginning 7) (not nodefault))
  11887. (string-to-number (or (match-string 7 s) "0")))
  11888. (string-to-number (match-string 4 s))
  11889. (string-to-number (match-string 3 s))
  11890. (string-to-number (match-string 2 s))
  11891. nil nil nil)
  11892. (make-list 9 0)))
  11893. (defun org-timestamp-up (&optional arg)
  11894. "Increase the date item at the cursor by one.
  11895. If the cursor is on the year, change the year. If it is on the month or
  11896. the day, change that.
  11897. With prefix ARG, change by that many units."
  11898. (interactive "p")
  11899. (org-timestamp-change (prefix-numeric-value arg)))
  11900. (defun org-timestamp-down (&optional arg)
  11901. "Decrease the date item at the cursor by one.
  11902. If the cursor is on the year, change the year. If it is on the month or
  11903. the day, change that.
  11904. With prefix ARG, change by that many units."
  11905. (interactive "p")
  11906. (org-timestamp-change (- (prefix-numeric-value arg))))
  11907. (defun org-timestamp-up-day (&optional arg)
  11908. "Increase the date in the time stamp by one day.
  11909. With prefix ARG, change that many days."
  11910. (interactive "p")
  11911. (if (and (not (org-at-timestamp-p t))
  11912. (org-on-heading-p))
  11913. (org-todo 'up)
  11914. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11915. (defun org-timestamp-down-day (&optional arg)
  11916. "Decrease the date in the time stamp by one day.
  11917. With prefix ARG, change that many days."
  11918. (interactive "p")
  11919. (if (and (not (org-at-timestamp-p t))
  11920. (org-on-heading-p))
  11921. (org-todo 'down)
  11922. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11923. (defun org-at-timestamp-p (&optional inactive-ok)
  11924. "Determine if the cursor is in or at a timestamp."
  11925. (interactive)
  11926. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11927. (pos (point))
  11928. (ans (or (looking-at tsr)
  11929. (save-excursion
  11930. (skip-chars-backward "^[<\n\r\t")
  11931. (if (> (point) (point-min)) (backward-char 1))
  11932. (and (looking-at tsr)
  11933. (> (- (match-end 0) pos) -1))))))
  11934. (and ans
  11935. (boundp 'org-ts-what)
  11936. (setq org-ts-what
  11937. (cond
  11938. ((= pos (match-beginning 0)) 'bracket)
  11939. ((= pos (1- (match-end 0))) 'bracket)
  11940. ((org-pos-in-match-range pos 2) 'year)
  11941. ((org-pos-in-match-range pos 3) 'month)
  11942. ((org-pos-in-match-range pos 7) 'hour)
  11943. ((org-pos-in-match-range pos 8) 'minute)
  11944. ((or (org-pos-in-match-range pos 4)
  11945. (org-pos-in-match-range pos 5)) 'day)
  11946. ((and (> pos (or (match-end 8) (match-end 5)))
  11947. (< pos (match-end 0)))
  11948. (- pos (or (match-end 8) (match-end 5))))
  11949. (t 'day))))
  11950. ans))
  11951. (defun org-toggle-timestamp-type ()
  11952. "Toggle the type (<active> or [inactive]) of a time stamp."
  11953. (interactive)
  11954. (when (org-at-timestamp-p t)
  11955. (let ((beg (match-beginning 0)) (end (match-end 0))
  11956. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11957. (save-excursion
  11958. (goto-char beg)
  11959. (while (re-search-forward "[][<>]" end t)
  11960. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11961. t t)))
  11962. (message "Timestamp is now %sactive"
  11963. (if (equal (char-after beg) ?<) "" "in")))))
  11964. (defun org-timestamp-change (n &optional what)
  11965. "Change the date in the time stamp at point.
  11966. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11967. `year', `minute', `second'. If WHAT is not given, the cursor position
  11968. in the timestamp determines what will be changed."
  11969. (let ((pos (point))
  11970. with-hm inactive
  11971. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11972. org-ts-what
  11973. extra rem
  11974. ts time time0)
  11975. (if (not (org-at-timestamp-p t))
  11976. (error "Not at a timestamp"))
  11977. (if (and (not what) (eq org-ts-what 'bracket))
  11978. (org-toggle-timestamp-type)
  11979. (if (and (not what) (not (eq org-ts-what 'day))
  11980. org-display-custom-times
  11981. (get-text-property (point) 'display)
  11982. (not (get-text-property (1- (point)) 'display)))
  11983. (setq org-ts-what 'day))
  11984. (setq org-ts-what (or what org-ts-what)
  11985. inactive (= (char-after (match-beginning 0)) ?\[)
  11986. ts (match-string 0))
  11987. (replace-match "")
  11988. (if (string-match
  11989. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11990. ts)
  11991. (setq extra (match-string 1 ts)))
  11992. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11993. (setq with-hm t))
  11994. (setq time0 (org-parse-time-string ts))
  11995. (when (and (eq org-ts-what 'minute)
  11996. (eq current-prefix-arg nil))
  11997. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11998. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11999. (setcar (cdr time0) (+ (nth 1 time0)
  12000. (if (> n 0) (- rem) (- dm rem))))))
  12001. (setq time
  12002. (encode-time (or (car time0) 0)
  12003. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12004. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12005. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12006. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12007. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12008. (nthcdr 6 time0)))
  12009. (when (and (member org-ts-what '(hour minute))
  12010. extra
  12011. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12012. (setq extra (org-modify-ts-extra
  12013. extra
  12014. (if (eq org-ts-what 'hour) 2 5)
  12015. n dm)))
  12016. (when (integerp org-ts-what)
  12017. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12018. (if (eq what 'calendar)
  12019. (let ((cal-date (org-get-date-from-calendar)))
  12020. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12021. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12022. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12023. (setcar time0 (or (car time0) 0))
  12024. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12025. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12026. (setq time (apply 'encode-time time0))))
  12027. (setq org-last-changed-timestamp
  12028. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12029. (org-clock-update-time-maybe)
  12030. (goto-char pos)
  12031. ;; Try to recenter the calendar window, if any
  12032. (if (and org-calendar-follow-timestamp-change
  12033. (get-buffer-window "*Calendar*" t)
  12034. (memq org-ts-what '(day month year)))
  12035. (org-recenter-calendar (time-to-days time))))))
  12036. (defun org-modify-ts-extra (s pos n dm)
  12037. "Change the different parts of the lead-time and repeat fields in timestamp."
  12038. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12039. ng h m new rem)
  12040. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12041. (cond
  12042. ((or (org-pos-in-match-range pos 2)
  12043. (org-pos-in-match-range pos 3))
  12044. (setq m (string-to-number (match-string 3 s))
  12045. h (string-to-number (match-string 2 s)))
  12046. (if (org-pos-in-match-range pos 2)
  12047. (setq h (+ h n))
  12048. (setq n (* dm (org-no-warnings (signum n))))
  12049. (when (not (= 0 (setq rem (% m dm))))
  12050. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12051. (setq m (+ m n)))
  12052. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12053. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12054. (setq h (min 24 (max 0 h)))
  12055. (setq ng 1 new (format "-%02d:%02d" h m)))
  12056. ((org-pos-in-match-range pos 6)
  12057. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12058. ((org-pos-in-match-range pos 5)
  12059. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12060. ((org-pos-in-match-range pos 9)
  12061. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12062. ((org-pos-in-match-range pos 8)
  12063. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12064. (when ng
  12065. (setq s (concat
  12066. (substring s 0 (match-beginning ng))
  12067. new
  12068. (substring s (match-end ng))))))
  12069. s))
  12070. (defun org-recenter-calendar (date)
  12071. "If the calendar is visible, recenter it to DATE."
  12072. (let* ((win (selected-window))
  12073. (cwin (get-buffer-window "*Calendar*" t))
  12074. (calendar-move-hook nil))
  12075. (when cwin
  12076. (select-window cwin)
  12077. (calendar-goto-date (if (listp date) date
  12078. (calendar-gregorian-from-absolute date)))
  12079. (select-window win))))
  12080. (defun org-goto-calendar (&optional arg)
  12081. "Go to the Emacs calendar at the current date.
  12082. If there is a time stamp in the current line, go to that date.
  12083. A prefix ARG can be used to force the current date."
  12084. (interactive "P")
  12085. (let ((tsr org-ts-regexp) diff
  12086. (calendar-move-hook nil)
  12087. (calendar-view-holidays-initially-flag nil)
  12088. (view-calendar-holidays-initially nil)
  12089. (calendar-view-diary-initially-flag nil)
  12090. (view-diary-entries-initially nil))
  12091. (if (or (org-at-timestamp-p)
  12092. (save-excursion
  12093. (beginning-of-line 1)
  12094. (looking-at (concat ".*" tsr))))
  12095. (let ((d1 (time-to-days (current-time)))
  12096. (d2 (time-to-days
  12097. (org-time-string-to-time (match-string 1)))))
  12098. (setq diff (- d2 d1))))
  12099. (calendar)
  12100. (calendar-goto-today)
  12101. (if (and diff (not arg)) (calendar-forward-day diff))))
  12102. (defun org-get-date-from-calendar ()
  12103. "Return a list (month day year) of date at point in calendar."
  12104. (with-current-buffer "*Calendar*"
  12105. (save-match-data
  12106. (calendar-cursor-to-date))))
  12107. (defun org-date-from-calendar ()
  12108. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12109. If there is already a time stamp at the cursor position, update it."
  12110. (interactive)
  12111. (if (org-at-timestamp-p t)
  12112. (org-timestamp-change 0 'calendar)
  12113. (let ((cal-date (org-get-date-from-calendar)))
  12114. (org-insert-time-stamp
  12115. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12116. (defun org-minutes-to-hh:mm-string (m)
  12117. "Compute H:MM from a number of minutes."
  12118. (let ((h (/ m 60)))
  12119. (setq m (- m (* 60 h)))
  12120. (format org-time-clocksum-format h m)))
  12121. (defun org-hh:mm-string-to-minutes (s)
  12122. "Convert a string H:MM to a number of minutes.
  12123. If the string is just a number, interprete it as minutes.
  12124. In fact, the first hh:mm or number in the string will be taken,
  12125. there can be extra stuff in the string.
  12126. If no number is found, the return value is 0."
  12127. (cond
  12128. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12129. (+ (* (string-to-number (match-string 1 s)) 60)
  12130. (string-to-number (match-string 2 s))))
  12131. ((string-match "\\([0-9]+\\)" s)
  12132. (string-to-number (match-string 1 s)))
  12133. (t 0)))
  12134. ;;;; Files
  12135. (defun org-save-all-org-buffers ()
  12136. "Save all Org-mode buffers without user confirmation."
  12137. (interactive)
  12138. (message "Saving all Org-mode buffers...")
  12139. (save-some-buffers t 'org-mode-p)
  12140. (when (featurep 'org-id) (org-id-locations-save))
  12141. (message "Saving all Org-mode buffers... done"))
  12142. (defun org-revert-all-org-buffers ()
  12143. "Revert all Org-mode buffers.
  12144. Prompt for confirmation when there are unsaved changes.
  12145. Be sure you know what you are doing before letting this function
  12146. overwrite your changes.
  12147. This function is useful in a setup where one tracks org files
  12148. with a version control system, to revert on one machine after pulling
  12149. changes from another. I believe the procedure must be like this:
  12150. 1. M-x org-save-all-org-buffers
  12151. 2. Pull changes from the other machine, resolve conflicts
  12152. 3. M-x org-revert-all-org-buffers"
  12153. (interactive)
  12154. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12155. (error "Abort"))
  12156. (save-excursion
  12157. (save-window-excursion
  12158. (mapc
  12159. (lambda (b)
  12160. (when (and (with-current-buffer b (org-mode-p))
  12161. (with-current-buffer b buffer-file-name))
  12162. (switch-to-buffer b)
  12163. (revert-buffer t 'no-confirm)))
  12164. (buffer-list))
  12165. (when (and (featurep 'org-id) org-id-track-globally)
  12166. (org-id-locations-load)))))
  12167. ;;;; Agenda files
  12168. ;;;###autoload
  12169. (defun org-iswitchb (&optional arg)
  12170. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  12171. With a prefix argument, restrict available to files.
  12172. With two prefix arguments, restrict available buffers to agenda files.
  12173. Due to some yet unresolved reason, the global function
  12174. `iswitchb-mode' needs to be active for this function to work."
  12175. (interactive "P")
  12176. (require 'iswitchb)
  12177. (let ((enabled iswitchb-mode) blist)
  12178. (or enabled (iswitchb-mode 1))
  12179. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12180. ((equal arg '(16)) (org-buffer-list 'agenda))
  12181. (t (org-buffer-list))))
  12182. (unwind-protect
  12183. (let ((iswitchb-make-buflist-hook
  12184. (lambda ()
  12185. (setq iswitchb-temp-buflist
  12186. (mapcar 'buffer-name blist)))))
  12187. (switch-to-buffer
  12188. (iswitchb-read-buffer
  12189. "Switch-to: " nil t))
  12190. (or enabled (iswitchb-mode -1))))))
  12191. ;;;###autoload
  12192. (defun org-ido-switchb (&optional arg)
  12193. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  12194. With a prefix argument, restrict available to files.
  12195. With two prefix arguments, restrict available buffers to agenda files."
  12196. (interactive "P")
  12197. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12198. ((equal arg '(16)) (org-buffer-list 'agenda))
  12199. (t (org-buffer-list)))))
  12200. (switch-to-buffer
  12201. (org-ido-completing-read "Org buffer: "
  12202. (mapcar 'list (mapcar 'buffer-name blist))
  12203. nil t))))
  12204. (defun org-buffer-list (&optional predicate exclude-tmp)
  12205. "Return a list of Org buffers.
  12206. PREDICATE can be `export', `files' or `agenda'.
  12207. export restrict the list to Export buffers.
  12208. files restrict the list to buffers visiting Org files.
  12209. agenda restrict the list to buffers visiting agenda files.
  12210. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12211. (let* ((bfn nil)
  12212. (agenda-files (and (eq predicate 'agenda)
  12213. (mapcar 'file-truename (org-agenda-files t))))
  12214. (filter
  12215. (cond
  12216. ((eq predicate 'files)
  12217. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12218. ((eq predicate 'export)
  12219. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12220. ((eq predicate 'agenda)
  12221. (lambda (b)
  12222. (with-current-buffer b
  12223. (and (eq major-mode 'org-mode)
  12224. (setq bfn (buffer-file-name b))
  12225. (member (file-truename bfn) agenda-files)))))
  12226. (t (lambda (b) (with-current-buffer b
  12227. (or (eq major-mode 'org-mode)
  12228. (string-match "\*Org .*Export"
  12229. (buffer-name b)))))))))
  12230. (delq nil
  12231. (mapcar
  12232. (lambda(b)
  12233. (if (and (funcall filter b)
  12234. (or (not exclude-tmp)
  12235. (not (string-match "tmp" (buffer-name b)))))
  12236. b
  12237. nil))
  12238. (buffer-list)))))
  12239. (defun org-agenda-files (&optional unrestricted archives)
  12240. "Get the list of agenda files.
  12241. Optional UNRESTRICTED means return the full list even if a restriction
  12242. is currently in place.
  12243. When ARCHIVES is t, include all archive files hat are really being
  12244. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12245. `org-agenda-archives-mode' is t."
  12246. (let ((files
  12247. (cond
  12248. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12249. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12250. ((listp org-agenda-files) org-agenda-files)
  12251. (t (error "Invalid value of `org-agenda-files'")))))
  12252. (setq files (apply 'append
  12253. (mapcar (lambda (f)
  12254. (if (file-directory-p f)
  12255. (directory-files
  12256. f t org-agenda-file-regexp)
  12257. (list f)))
  12258. files)))
  12259. (when org-agenda-skip-unavailable-files
  12260. (setq files (delq nil
  12261. (mapcar (function
  12262. (lambda (file)
  12263. (and (file-readable-p file) file)))
  12264. files))))
  12265. (when (or (eq archives t)
  12266. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12267. (setq files (org-add-archive-files files)))
  12268. files))
  12269. (defun org-edit-agenda-file-list ()
  12270. "Edit the list of agenda files.
  12271. Depending on setup, this either uses customize to edit the variable
  12272. `org-agenda-files', or it visits the file that is holding the list. In the
  12273. latter case, the buffer is set up in a way that saving it automatically kills
  12274. the buffer and restores the previous window configuration."
  12275. (interactive)
  12276. (if (stringp org-agenda-files)
  12277. (let ((cw (current-window-configuration)))
  12278. (find-file org-agenda-files)
  12279. (org-set-local 'org-window-configuration cw)
  12280. (org-add-hook 'after-save-hook
  12281. (lambda ()
  12282. (set-window-configuration
  12283. (prog1 org-window-configuration
  12284. (kill-buffer (current-buffer))))
  12285. (org-install-agenda-files-menu)
  12286. (message "New agenda file list installed"))
  12287. nil 'local)
  12288. (message "%s" (substitute-command-keys
  12289. "Edit list and finish with \\[save-buffer]")))
  12290. (customize-variable 'org-agenda-files)))
  12291. (defun org-store-new-agenda-file-list (list)
  12292. "Set new value for the agenda file list and save it correctly."
  12293. (if (stringp org-agenda-files)
  12294. (let ((f org-agenda-files) b)
  12295. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12296. (with-temp-file f
  12297. (insert (mapconcat 'identity list "\n") "\n")))
  12298. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12299. (setq org-agenda-files list)
  12300. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12301. (defun org-read-agenda-file-list ()
  12302. "Read the list of agenda files from a file."
  12303. (when (file-directory-p org-agenda-files)
  12304. (error "`org-agenda-files' cannot be a single directory"))
  12305. (when (stringp org-agenda-files)
  12306. (with-temp-buffer
  12307. (insert-file-contents org-agenda-files)
  12308. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12309. ;;;###autoload
  12310. (defun org-cycle-agenda-files ()
  12311. "Cycle through the files in `org-agenda-files'.
  12312. If the current buffer visits an agenda file, find the next one in the list.
  12313. If the current buffer does not, find the first agenda file."
  12314. (interactive)
  12315. (let* ((fs (org-agenda-files t))
  12316. (files (append fs (list (car fs))))
  12317. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12318. file)
  12319. (unless files (error "No agenda files"))
  12320. (catch 'exit
  12321. (while (setq file (pop files))
  12322. (if (equal (file-truename file) tcf)
  12323. (when (car files)
  12324. (find-file (car files))
  12325. (throw 'exit t))))
  12326. (find-file (car fs)))
  12327. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12328. (defun org-agenda-file-to-front (&optional to-end)
  12329. "Move/add the current file to the top of the agenda file list.
  12330. If the file is not present in the list, it is added to the front. If it is
  12331. present, it is moved there. With optional argument TO-END, add/move to the
  12332. end of the list."
  12333. (interactive "P")
  12334. (let ((org-agenda-skip-unavailable-files nil)
  12335. (file-alist (mapcar (lambda (x)
  12336. (cons (file-truename x) x))
  12337. (org-agenda-files t)))
  12338. (ctf (file-truename buffer-file-name))
  12339. x had)
  12340. (setq x (assoc ctf file-alist) had x)
  12341. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12342. (if to-end
  12343. (setq file-alist (append (delq x file-alist) (list x)))
  12344. (setq file-alist (cons x (delq x file-alist))))
  12345. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12346. (org-install-agenda-files-menu)
  12347. (message "File %s to %s of agenda file list"
  12348. (if had "moved" "added") (if to-end "end" "front"))))
  12349. (defun org-remove-file (&optional file)
  12350. "Remove current file from the list of files in variable `org-agenda-files'.
  12351. These are the files which are being checked for agenda entries.
  12352. Optional argument FILE means, use this file instead of the current."
  12353. (interactive)
  12354. (let* ((org-agenda-skip-unavailable-files nil)
  12355. (file (or file buffer-file-name))
  12356. (true-file (file-truename file))
  12357. (afile (abbreviate-file-name file))
  12358. (files (delq nil (mapcar
  12359. (lambda (x)
  12360. (if (equal true-file
  12361. (file-truename x))
  12362. nil x))
  12363. (org-agenda-files t)))))
  12364. (if (not (= (length files) (length (org-agenda-files t))))
  12365. (progn
  12366. (org-store-new-agenda-file-list files)
  12367. (org-install-agenda-files-menu)
  12368. (message "Removed file: %s" afile))
  12369. (message "File was not in list: %s (not removed)" afile))))
  12370. (defun org-file-menu-entry (file)
  12371. (vector file (list 'find-file file) t))
  12372. (defun org-check-agenda-file (file)
  12373. "Make sure FILE exists. If not, ask user what to do."
  12374. (when (not (file-exists-p file))
  12375. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12376. (abbreviate-file-name file))
  12377. (let ((r (downcase (read-char-exclusive))))
  12378. (cond
  12379. ((equal r ?r)
  12380. (org-remove-file file)
  12381. (throw 'nextfile t))
  12382. (t (error "Abort"))))))
  12383. (defun org-get-agenda-file-buffer (file)
  12384. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12385. it to the list of buffers which might be released later."
  12386. (let ((buf (org-find-base-buffer-visiting file)))
  12387. (if buf
  12388. buf ; just return it
  12389. ;; Make a new buffer and remember it
  12390. (setq buf (find-file-noselect file))
  12391. (if buf (push buf org-agenda-new-buffers))
  12392. buf)))
  12393. (defun org-release-buffers (blist)
  12394. "Release all buffers in list, asking the user for confirmation when needed.
  12395. When a buffer is unmodified, it is just killed. When modified, it is saved
  12396. \(if the user agrees) and then killed."
  12397. (let (buf file)
  12398. (while (setq buf (pop blist))
  12399. (setq file (buffer-file-name buf))
  12400. (when (and (buffer-modified-p buf)
  12401. file
  12402. (y-or-n-p (format "Save file %s? " file)))
  12403. (with-current-buffer buf (save-buffer)))
  12404. (kill-buffer buf))))
  12405. (defun org-prepare-agenda-buffers (files)
  12406. "Create buffers for all agenda files, protect archived trees and comments."
  12407. (interactive)
  12408. (let ((pa '(:org-archived t))
  12409. (pc '(:org-comment t))
  12410. (pall '(:org-archived t :org-comment t))
  12411. (inhibit-read-only t)
  12412. (rea (concat ":" org-archive-tag ":"))
  12413. bmp file re)
  12414. (save-excursion
  12415. (save-restriction
  12416. (while (setq file (pop files))
  12417. (catch 'nextfile
  12418. (if (bufferp file)
  12419. (set-buffer file)
  12420. (org-check-agenda-file file)
  12421. (set-buffer (org-get-agenda-file-buffer file)))
  12422. (widen)
  12423. (setq bmp (buffer-modified-p))
  12424. (org-refresh-category-properties)
  12425. (setq org-todo-keywords-for-agenda
  12426. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12427. (setq org-done-keywords-for-agenda
  12428. (append org-done-keywords-for-agenda org-done-keywords))
  12429. (setq org-todo-keyword-alist-for-agenda
  12430. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12431. (setq org-tag-alist-for-agenda
  12432. (append org-tag-alist-for-agenda org-tag-alist))
  12433. (save-excursion
  12434. (remove-text-properties (point-min) (point-max) pall)
  12435. (when org-agenda-skip-archived-trees
  12436. (goto-char (point-min))
  12437. (while (re-search-forward rea nil t)
  12438. (if (org-on-heading-p t)
  12439. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12440. (goto-char (point-min))
  12441. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12442. (while (re-search-forward re nil t)
  12443. (add-text-properties
  12444. (match-beginning 0) (org-end-of-subtree t) pc)))
  12445. (set-buffer-modified-p bmp)))))
  12446. (setq org-todo-keyword-alist-for-agenda
  12447. (org-uniquify org-todo-keyword-alist-for-agenda)
  12448. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12449. ;;;; Embedded LaTeX
  12450. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12451. "Keymap for the minor `org-cdlatex-mode'.")
  12452. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12453. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12454. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12455. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12456. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12457. (defvar org-cdlatex-texmathp-advice-is-done nil
  12458. "Flag remembering if we have applied the advice to texmathp already.")
  12459. (define-minor-mode org-cdlatex-mode
  12460. "Toggle the minor `org-cdlatex-mode'.
  12461. This mode supports entering LaTeX environment and math in LaTeX fragments
  12462. in Org-mode.
  12463. \\{org-cdlatex-mode-map}"
  12464. nil " OCDL" nil
  12465. (when org-cdlatex-mode (require 'cdlatex))
  12466. (unless org-cdlatex-texmathp-advice-is-done
  12467. (setq org-cdlatex-texmathp-advice-is-done t)
  12468. (defadvice texmathp (around org-math-always-on activate)
  12469. "Always return t in org-mode buffers.
  12470. This is because we want to insert math symbols without dollars even outside
  12471. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12472. an embedded LaTeX fragment, let texmathp do its job.
  12473. \\[org-cdlatex-mode-map]"
  12474. (interactive)
  12475. (let (p)
  12476. (cond
  12477. ((not (org-mode-p)) ad-do-it)
  12478. ((eq this-command 'cdlatex-math-symbol)
  12479. (setq ad-return-value t
  12480. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12481. (t
  12482. (let ((p (org-inside-LaTeX-fragment-p)))
  12483. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12484. (setq ad-return-value t
  12485. texmathp-why '("Org-mode embedded math" . 0))
  12486. (if p ad-do-it)))))))))
  12487. (defun turn-on-org-cdlatex ()
  12488. "Unconditionally turn on `org-cdlatex-mode'."
  12489. (org-cdlatex-mode 1))
  12490. (defun org-inside-LaTeX-fragment-p ()
  12491. "Test if point is inside a LaTeX fragment.
  12492. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12493. sequence appearing also before point.
  12494. Even though the matchers for math are configurable, this function assumes
  12495. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12496. delimiters are skipped when they have been removed by customization.
  12497. The return value is nil, or a cons cell with the delimiter and
  12498. and the position of this delimiter.
  12499. This function does a reasonably good job, but can locally be fooled by
  12500. for example currency specifications. For example it will assume being in
  12501. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12502. fragments that are properly closed, but during editing, we have to live
  12503. with the uncertainty caused by missing closing delimiters. This function
  12504. looks only before point, not after."
  12505. (catch 'exit
  12506. (let ((pos (point))
  12507. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12508. (lim (progn
  12509. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12510. (point)))
  12511. dd-on str (start 0) m re)
  12512. (goto-char pos)
  12513. (when dodollar
  12514. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12515. re (nth 1 (assoc "$" org-latex-regexps)))
  12516. (while (string-match re str start)
  12517. (cond
  12518. ((= (match-end 0) (length str))
  12519. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12520. ((= (match-end 0) (- (length str) 5))
  12521. (throw 'exit nil))
  12522. (t (setq start (match-end 0))))))
  12523. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12524. (goto-char pos)
  12525. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12526. (and (match-beginning 2) (throw 'exit nil))
  12527. ;; count $$
  12528. (while (re-search-backward "\\$\\$" lim t)
  12529. (setq dd-on (not dd-on)))
  12530. (goto-char pos)
  12531. (if dd-on (cons "$$" m))))))
  12532. (defun org-try-cdlatex-tab ()
  12533. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12534. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12535. - inside a LaTeX fragment, or
  12536. - after the first word in a line, where an abbreviation expansion could
  12537. insert a LaTeX environment."
  12538. (when org-cdlatex-mode
  12539. (cond
  12540. ((save-excursion
  12541. (skip-chars-backward "a-zA-Z0-9*")
  12542. (skip-chars-backward " \t")
  12543. (bolp))
  12544. (cdlatex-tab) t)
  12545. ((org-inside-LaTeX-fragment-p)
  12546. (cdlatex-tab) t)
  12547. (t nil))))
  12548. (defun org-cdlatex-underscore-caret (&optional arg)
  12549. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12550. Revert to the normal definition outside of these fragments."
  12551. (interactive "P")
  12552. (if (org-inside-LaTeX-fragment-p)
  12553. (call-interactively 'cdlatex-sub-superscript)
  12554. (let (org-cdlatex-mode)
  12555. (call-interactively (key-binding (vector last-input-event))))))
  12556. (defun org-cdlatex-math-modify (&optional arg)
  12557. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12558. Revert to the normal definition outside of these fragments."
  12559. (interactive "P")
  12560. (if (org-inside-LaTeX-fragment-p)
  12561. (call-interactively 'cdlatex-math-modify)
  12562. (let (org-cdlatex-mode)
  12563. (call-interactively (key-binding (vector last-input-event))))))
  12564. (defvar org-latex-fragment-image-overlays nil
  12565. "List of overlays carrying the images of latex fragments.")
  12566. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12567. (defun org-remove-latex-fragment-image-overlays ()
  12568. "Remove all overlays with LaTeX fragment images in current buffer."
  12569. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12570. (setq org-latex-fragment-image-overlays nil))
  12571. (defun org-preview-latex-fragment (&optional subtree)
  12572. "Preview the LaTeX fragment at point, or all locally or globally.
  12573. If the cursor is in a LaTeX fragment, create the image and overlay
  12574. it over the source code. If there is no fragment at point, display
  12575. all fragments in the current text, from one headline to the next. With
  12576. prefix SUBTREE, display all fragments in the current subtree. With a
  12577. double prefix `C-u C-u', or when the cursor is before the first headline,
  12578. display all fragments in the buffer.
  12579. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12580. (interactive "P")
  12581. (org-remove-latex-fragment-image-overlays)
  12582. (save-excursion
  12583. (save-restriction
  12584. (let (beg end at msg)
  12585. (cond
  12586. ((or (equal subtree '(16))
  12587. (not (save-excursion
  12588. (re-search-backward (concat "^" outline-regexp) nil t))))
  12589. (setq beg (point-min) end (point-max)
  12590. msg "Creating images for buffer...%s"))
  12591. ((equal subtree '(4))
  12592. (org-back-to-heading)
  12593. (setq beg (point) end (org-end-of-subtree t)
  12594. msg "Creating images for subtree...%s"))
  12595. (t
  12596. (if (setq at (org-inside-LaTeX-fragment-p))
  12597. (goto-char (max (point-min) (- (cdr at) 2)))
  12598. (org-back-to-heading))
  12599. (setq beg (point) end (progn (outline-next-heading) (point))
  12600. msg (if at "Creating image...%s"
  12601. "Creating images for entry...%s"))))
  12602. (message msg "")
  12603. (narrow-to-region beg end)
  12604. (goto-char beg)
  12605. (org-format-latex
  12606. (concat "ltxpng/" (file-name-sans-extension
  12607. (file-name-nondirectory
  12608. buffer-file-name)))
  12609. default-directory 'overlays msg at 'forbuffer)
  12610. (message msg "done. Use `C-c C-c' to remove images.")))))
  12611. (defvar org-latex-regexps
  12612. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12613. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12614. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12615. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12616. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12617. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12618. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12619. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12620. "Regular expressions for matching embedded LaTeX.")
  12621. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12622. "Replace LaTeX fragments with links to an image, and produce images."
  12623. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12624. (let* ((prefixnodir (file-name-nondirectory prefix))
  12625. (absprefix (expand-file-name prefix dir))
  12626. (todir (file-name-directory absprefix))
  12627. (opt org-format-latex-options)
  12628. (matchers (plist-get opt :matchers))
  12629. (re-list org-latex-regexps)
  12630. (cnt 0) txt link beg end re e checkdir
  12631. executables-checked
  12632. m n block linkfile movefile ov)
  12633. ;; Check if there are old images files with this prefix, and remove them
  12634. (when (file-directory-p todir)
  12635. (mapc 'delete-file
  12636. (directory-files
  12637. todir 'full
  12638. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12639. ;; Check the different regular expressions
  12640. (while (setq e (pop re-list))
  12641. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12642. block (if (nth 3 e) "\n\n" ""))
  12643. (when (member m matchers)
  12644. (goto-char (point-min))
  12645. (while (re-search-forward re nil t)
  12646. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12647. (not (get-text-property (match-beginning n)
  12648. 'org-protected)))
  12649. (setq txt (match-string n)
  12650. beg (match-beginning n) end (match-end n)
  12651. cnt (1+ cnt)
  12652. linkfile (format "%s_%04d.png" prefix cnt)
  12653. movefile (format "%s_%04d.png" absprefix cnt)
  12654. link (concat block "[[file:" linkfile "]]" block))
  12655. (if msg (message msg cnt))
  12656. (goto-char beg)
  12657. (unless checkdir ; make sure the directory exists
  12658. (setq checkdir t)
  12659. (or (file-directory-p todir) (make-directory todir)))
  12660. (unless executables-checked
  12661. (org-check-external-command
  12662. "latex" "needed to convert LaTeX fragments to images")
  12663. (org-check-external-command
  12664. "dvipng" "needed to convert LaTeX fragments to images")
  12665. (setq executables-checked t))
  12666. (org-create-formula-image
  12667. txt movefile opt forbuffer)
  12668. (if overlays
  12669. (progn
  12670. (setq ov (org-make-overlay beg end))
  12671. (if (featurep 'xemacs)
  12672. (progn
  12673. (org-overlay-put ov 'invisible t)
  12674. (org-overlay-put
  12675. ov 'end-glyph
  12676. (make-glyph (vector 'png :file movefile))))
  12677. (org-overlay-put
  12678. ov 'display
  12679. (list 'image :type 'png :file movefile :ascent 'center)))
  12680. (push ov org-latex-fragment-image-overlays)
  12681. (goto-char end))
  12682. (delete-region beg end)
  12683. (insert link))))))))
  12684. ;; This function borrows from Ganesh Swami's latex2png.el
  12685. (defun org-create-formula-image (string tofile options buffer)
  12686. (let* ((tmpdir (if (featurep 'xemacs)
  12687. (temp-directory)
  12688. temporary-file-directory))
  12689. (texfilebase (make-temp-name
  12690. (expand-file-name "orgtex" tmpdir)))
  12691. (texfile (concat texfilebase ".tex"))
  12692. (dvifile (concat texfilebase ".dvi"))
  12693. (pngfile (concat texfilebase ".png"))
  12694. (fnh (if (featurep 'xemacs)
  12695. (font-height (get-face-font 'default))
  12696. (face-attribute 'default :height nil)))
  12697. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12698. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12699. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12700. "Black"))
  12701. (bg (or (plist-get options (if buffer :background :html-background))
  12702. "Transparent")))
  12703. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12704. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12705. (with-temp-file texfile
  12706. (insert org-format-latex-header
  12707. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12708. (let ((dir default-directory))
  12709. (condition-case nil
  12710. (progn
  12711. (cd tmpdir)
  12712. (call-process "latex" nil nil nil texfile))
  12713. (error nil))
  12714. (cd dir))
  12715. (if (not (file-exists-p dvifile))
  12716. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12717. (condition-case nil
  12718. (call-process "dvipng" nil nil nil
  12719. "-E" "-fg" fg "-bg" bg
  12720. "-D" dpi
  12721. ;;"-x" scale "-y" scale
  12722. "-T" "tight"
  12723. "-o" pngfile
  12724. dvifile)
  12725. (error nil))
  12726. (if (not (file-exists-p pngfile))
  12727. (progn (message "Failed to create png file from %s" texfile) nil)
  12728. ;; Use the requested file name and clean up
  12729. (copy-file pngfile tofile 'replace)
  12730. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12731. (delete-file (concat texfilebase e)))
  12732. pngfile))))
  12733. (defun org-dvipng-color (attr)
  12734. "Return an rgb color specification for dvipng."
  12735. (apply 'format "rgb %s %s %s"
  12736. (mapcar 'org-normalize-color
  12737. (color-values (face-attribute 'default attr nil)))))
  12738. (defun org-normalize-color (value)
  12739. "Return string to be used as color value for an RGB component."
  12740. (format "%g" (/ value 65535.0)))
  12741. ;;;; Key bindings
  12742. ;; Make `C-c C-x' a prefix key
  12743. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12744. ;; TAB key with modifiers
  12745. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12746. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12747. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12748. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12749. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12750. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12751. ;; The following line is necessary under Suse GNU/Linux
  12752. (unless (featurep 'xemacs)
  12753. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12754. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12755. (define-key org-mode-map [backtab] 'org-shifttab)
  12756. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12757. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12758. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12759. ;; Cursor keys with modifiers
  12760. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12761. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12762. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12763. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12764. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12765. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12766. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12767. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12768. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12769. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12770. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12771. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12772. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12773. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12774. ;;; Extra keys for tty access.
  12775. ;; We only set them when really needed because otherwise the
  12776. ;; menus don't show the simple keys
  12777. (when (or org-use-extra-keys
  12778. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12779. (not window-system))
  12780. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12781. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12782. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12783. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12784. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12785. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12786. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12787. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12788. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12789. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12790. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12791. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12792. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12793. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12794. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12795. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12796. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12797. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12798. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12799. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12800. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12801. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  12802. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  12803. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  12804. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  12805. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  12806. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  12807. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  12808. ;; All the other keys
  12809. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12810. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12811. (if (boundp 'narrow-map)
  12812. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12813. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12814. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  12815. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  12816. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12817. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12818. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12819. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12820. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12821. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12822. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12823. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12824. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12825. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12826. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12827. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12828. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12829. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12830. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12831. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12832. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12833. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12834. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12835. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12836. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12837. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12838. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12839. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12840. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12841. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12842. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12843. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12844. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12845. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12846. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12847. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12848. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12849. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12850. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12851. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12852. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12853. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12854. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12855. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12856. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12857. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12858. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12859. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12860. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12861. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12862. (org-defkey org-mode-map "\C-m" 'org-return)
  12863. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12864. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12865. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12866. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12867. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12868. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12869. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12870. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12871. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12872. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12873. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12874. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12875. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12876. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12877. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12878. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12879. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12880. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  12881. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  12882. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12883. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12884. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12885. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12886. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12887. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12888. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12889. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12890. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12891. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12892. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12893. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12894. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12895. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12896. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12897. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12898. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12899. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  12900. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12901. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12902. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12903. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12904. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12905. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  12906. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12907. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12908. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12909. (when (featurep 'xemacs)
  12910. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12911. (defvar org-self-insert-command-undo-counter 0)
  12912. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12913. (defun org-self-insert-command (N)
  12914. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12915. If the cursor is in a table looking at whitespace, the whitespace is
  12916. overwritten, and the table is not marked as requiring realignment."
  12917. (interactive "p")
  12918. (if (and
  12919. (org-table-p)
  12920. (progn
  12921. ;; check if we blank the field, and if that triggers align
  12922. (and (featurep 'org-table) org-table-auto-blank-field
  12923. (member last-command
  12924. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  12925. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12926. ;; got extra space, this field does not determine column width
  12927. (let (org-table-may-need-update) (org-table-blank-field))
  12928. ;; no extra space, this field may determine column width
  12929. (org-table-blank-field)))
  12930. t)
  12931. (eq N 1)
  12932. (looking-at "[^|\n]* |"))
  12933. (let (org-table-may-need-update)
  12934. (goto-char (1- (match-end 0)))
  12935. (delete-backward-char 1)
  12936. (goto-char (match-beginning 0))
  12937. (self-insert-command N))
  12938. (setq org-table-may-need-update t)
  12939. (self-insert-command N)
  12940. (org-fix-tags-on-the-fly)
  12941. (if org-self-insert-cluster-for-undo
  12942. (if (not (eq last-command 'org-self-insert-command))
  12943. (setq org-self-insert-command-undo-counter 1)
  12944. (if (>= org-self-insert-command-undo-counter 20)
  12945. (setq org-self-insert-command-undo-counter 1)
  12946. (and (> org-self-insert-command-undo-counter 0)
  12947. buffer-undo-list
  12948. (not (cadr buffer-undo-list)) ; remove nil entry
  12949. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12950. (setq org-self-insert-command-undo-counter
  12951. (1+ org-self-insert-command-undo-counter)))))))
  12952. (defun org-fix-tags-on-the-fly ()
  12953. (when (and (equal (char-after (point-at-bol)) ?*)
  12954. (org-on-heading-p))
  12955. (org-align-tags-here org-tags-column)))
  12956. (defun org-delete-backward-char (N)
  12957. "Like `delete-backward-char', insert whitespace at field end in tables.
  12958. When deleting backwards, in tables this function will insert whitespace in
  12959. front of the next \"|\" separator, to keep the table aligned. The table will
  12960. still be marked for re-alignment if the field did fill the entire column,
  12961. because, in this case the deletion might narrow the column."
  12962. (interactive "p")
  12963. (if (and (org-table-p)
  12964. (eq N 1)
  12965. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12966. (looking-at ".*?|"))
  12967. (let ((pos (point))
  12968. (noalign (looking-at "[^|\n\r]* |"))
  12969. (c org-table-may-need-update))
  12970. (backward-delete-char N)
  12971. (skip-chars-forward "^|")
  12972. (insert " ")
  12973. (goto-char (1- pos))
  12974. ;; noalign: if there were two spaces at the end, this field
  12975. ;; does not determine the width of the column.
  12976. (if noalign (setq org-table-may-need-update c)))
  12977. (backward-delete-char N)
  12978. (org-fix-tags-on-the-fly)))
  12979. (defun org-delete-char (N)
  12980. "Like `delete-char', but insert whitespace at field end in tables.
  12981. When deleting characters, in tables this function will insert whitespace in
  12982. front of the next \"|\" separator, to keep the table aligned. The table will
  12983. still be marked for re-alignment if the field did fill the entire column,
  12984. because, in this case the deletion might narrow the column."
  12985. (interactive "p")
  12986. (if (and (org-table-p)
  12987. (not (bolp))
  12988. (not (= (char-after) ?|))
  12989. (eq N 1))
  12990. (if (looking-at ".*?|")
  12991. (let ((pos (point))
  12992. (noalign (looking-at "[^|\n\r]* |"))
  12993. (c org-table-may-need-update))
  12994. (replace-match (concat
  12995. (substring (match-string 0) 1 -1)
  12996. " |"))
  12997. (goto-char pos)
  12998. ;; noalign: if there were two spaces at the end, this field
  12999. ;; does not determine the width of the column.
  13000. (if noalign (setq org-table-may-need-update c)))
  13001. (delete-char N))
  13002. (delete-char N)
  13003. (org-fix-tags-on-the-fly)))
  13004. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13005. (put 'org-self-insert-command 'delete-selection t)
  13006. (put 'orgtbl-self-insert-command 'delete-selection t)
  13007. (put 'org-delete-char 'delete-selection 'supersede)
  13008. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13009. (put 'org-yank 'delete-selection 'yank)
  13010. ;; Make `flyspell-mode' delay after some commands
  13011. (put 'org-self-insert-command 'flyspell-delayed t)
  13012. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13013. (put 'org-delete-char 'flyspell-delayed t)
  13014. (put 'org-delete-backward-char 'flyspell-delayed t)
  13015. ;; Make pabbrev-mode expand after org-mode commands
  13016. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13017. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13018. ;; How to do this: Measure non-white length of current string
  13019. ;; If equal to column width, we should realign.
  13020. (defun org-remap (map &rest commands)
  13021. "In MAP, remap the functions given in COMMANDS.
  13022. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13023. (let (new old)
  13024. (while commands
  13025. (setq old (pop commands) new (pop commands))
  13026. (if (fboundp 'command-remapping)
  13027. (org-defkey map (vector 'remap old) new)
  13028. (substitute-key-definition old new map global-map)))))
  13029. (when (eq org-enable-table-editor 'optimized)
  13030. ;; If the user wants maximum table support, we need to hijack
  13031. ;; some standard editing functions
  13032. (org-remap org-mode-map
  13033. 'self-insert-command 'org-self-insert-command
  13034. 'delete-char 'org-delete-char
  13035. 'delete-backward-char 'org-delete-backward-char)
  13036. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13037. (defvar org-ctrl-c-ctrl-c-hook nil
  13038. "Hook for functions attaching themselves to `C-c C-c'.
  13039. This can be used to add additional functionality to the C-c C-c key which
  13040. executes context-dependent commands.
  13041. Each function will be called with no arguments. The function must check
  13042. if the context is appropriate for it to act. If yes, it should do its
  13043. thing and then return a non-nil value. If the context is wrong,
  13044. just do nothing and return nil.")
  13045. (defvar org-tab-first-hook nil
  13046. "Hook for functions to attach themselves to TAB.
  13047. See `org-ctrl-c-ctrl-c-hook' for more information.
  13048. This hook runs as the first action when TAB is pressed, even before
  13049. `org-cycle' messes around with the `outline-regexp' to cater for
  13050. inline tasks and plain list item folding.
  13051. If any function in this hook returns t, not other actions like table
  13052. field motion visibility cycling will be done.")
  13053. (defvar org-tab-after-check-for-table-hook nil
  13054. "Hook for functions to attach themselves to TAB.
  13055. See `org-ctrl-c-ctrl-c-hook' for more information.
  13056. This hook runs after it has been established that the cursor is not in a
  13057. table, but before checking if the cursor is in a headline or if global cycling
  13058. should be done.
  13059. If any function in this hook returns t, not other actions like visibility
  13060. cycling will be done.")
  13061. (defvar org-tab-after-check-for-cycling-hook nil
  13062. "Hook for functions to attach themselves to TAB.
  13063. See `org-ctrl-c-ctrl-c-hook' for more information.
  13064. This hook runs after it has been established that not table field motion and
  13065. not visibility should be done because of current context. This is probably
  13066. the place where a package like yasnippets can hook in.")
  13067. (defvar org-metaleft-hook nil
  13068. "Hook for functions attaching themselves to `M-left'.
  13069. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13070. (defvar org-metaright-hook nil
  13071. "Hook for functions attaching themselves to `M-right'.
  13072. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13073. (defvar org-metaup-hook nil
  13074. "Hook for functions attaching themselves to `M-up'.
  13075. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13076. (defvar org-metadown-hook nil
  13077. "Hook for functions attaching themselves to `M-down'.
  13078. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13079. (defvar org-shiftmetaleft-hook nil
  13080. "Hook for functions attaching themselves to `M-S-left'.
  13081. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13082. (defvar org-shiftmetaright-hook nil
  13083. "Hook for functions attaching themselves to `M-S-right'.
  13084. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13085. (defvar org-shiftmetaup-hook nil
  13086. "Hook for functions attaching themselves to `M-S-up'.
  13087. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13088. (defvar org-shiftmetadown-hook nil
  13089. "Hook for functions attaching themselves to `M-S-down'.
  13090. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13091. (defvar org-metareturn-hook nil
  13092. "Hook for functions attaching themselves to `M-RET'.
  13093. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13094. (defun org-modifier-cursor-error ()
  13095. "Throw an error, a modified cursor command was applied in wrong context."
  13096. (error "This command is active in special context like tables, headlines or items"))
  13097. (defun org-shiftselect-error ()
  13098. "Throw an error because Shift-Cursor command was applied in wrong context."
  13099. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13100. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  13101. (error "This command works only in special context like headlines or timestamps.")))
  13102. (defun org-call-for-shift-select (cmd)
  13103. (let ((this-command-keys-shift-translated t))
  13104. (call-interactively cmd)))
  13105. (defun org-shifttab (&optional arg)
  13106. "Global visibility cycling or move to previous table field.
  13107. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13108. on context.
  13109. See the individual commands for more information."
  13110. (interactive "P")
  13111. (cond
  13112. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13113. ((integerp arg)
  13114. (message "Content view to level: %d" arg)
  13115. (org-content (prefix-numeric-value arg))
  13116. (setq org-cycle-global-status 'overview))
  13117. (t (call-interactively 'org-global-cycle))))
  13118. (defun org-shiftmetaleft ()
  13119. "Promote subtree or delete table column.
  13120. Calls `org-promote-subtree', `org-outdent-item',
  13121. or `org-table-delete-column', depending on context.
  13122. See the individual commands for more information."
  13123. (interactive)
  13124. (cond
  13125. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13126. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13127. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13128. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13129. (t (org-modifier-cursor-error))))
  13130. (defun org-shiftmetaright ()
  13131. "Demote subtree or insert table column.
  13132. Calls `org-demote-subtree', `org-indent-item',
  13133. or `org-table-insert-column', depending on context.
  13134. See the individual commands for more information."
  13135. (interactive)
  13136. (cond
  13137. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13138. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13139. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13140. ((org-at-item-p) (call-interactively 'org-indent-item))
  13141. (t (org-modifier-cursor-error))))
  13142. (defun org-shiftmetaup (&optional arg)
  13143. "Move subtree up or kill table row.
  13144. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13145. `org-move-item-up' depending on context. See the individual commands
  13146. for more information."
  13147. (interactive "P")
  13148. (cond
  13149. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13150. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13151. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13152. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13153. (t (org-modifier-cursor-error))))
  13154. (defun org-shiftmetadown (&optional arg)
  13155. "Move subtree down or insert table row.
  13156. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13157. `org-move-item-down', depending on context. See the individual
  13158. commands for more information."
  13159. (interactive "P")
  13160. (cond
  13161. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13162. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13163. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13164. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13165. (t (org-modifier-cursor-error))))
  13166. (defun org-metaleft (&optional arg)
  13167. "Promote heading or move table column to left.
  13168. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13169. With no specific context, calls the Emacs default `backward-word'.
  13170. See the individual commands for more information."
  13171. (interactive "P")
  13172. (cond
  13173. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13174. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13175. ((or (org-on-heading-p)
  13176. (and (org-region-active-p)
  13177. (save-excursion
  13178. (goto-char (region-beginning))
  13179. (org-on-heading-p))))
  13180. (call-interactively 'org-do-promote))
  13181. ((or (org-at-item-p)
  13182. (and (org-region-active-p)
  13183. (save-excursion
  13184. (goto-char (region-beginning))
  13185. (org-at-item-p))))
  13186. (call-interactively 'org-outdent-item))
  13187. (t (call-interactively 'backward-word))))
  13188. (defun org-metaright (&optional arg)
  13189. "Demote subtree or move table column to right.
  13190. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13191. With no specific context, calls the Emacs default `forward-word'.
  13192. See the individual commands for more information."
  13193. (interactive "P")
  13194. (cond
  13195. ((run-hook-with-args-until-success 'org-metaright-hook))
  13196. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13197. ((or (org-on-heading-p)
  13198. (and (org-region-active-p)
  13199. (save-excursion
  13200. (goto-char (region-beginning))
  13201. (org-on-heading-p))))
  13202. (call-interactively 'org-do-demote))
  13203. ((or (org-at-item-p)
  13204. (and (org-region-active-p)
  13205. (save-excursion
  13206. (goto-char (region-beginning))
  13207. (org-at-item-p))))
  13208. (call-interactively 'org-indent-item))
  13209. (t (call-interactively 'forward-word))))
  13210. (defun org-metaup (&optional arg)
  13211. "Move subtree up or move table row up.
  13212. Calls `org-move-subtree-up' or `org-table-move-row' or
  13213. `org-move-item-up', depending on context. See the individual commands
  13214. for more information."
  13215. (interactive "P")
  13216. (cond
  13217. ((run-hook-with-args-until-success 'org-metaup-hook))
  13218. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13219. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13220. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13221. (t (transpose-lines 1) (beginning-of-line -1))))
  13222. (defun org-metadown (&optional arg)
  13223. "Move subtree down or move table row down.
  13224. Calls `org-move-subtree-down' or `org-table-move-row' or
  13225. `org-move-item-down', depending on context. See the individual
  13226. commands for more information."
  13227. (interactive "P")
  13228. (cond
  13229. ((run-hook-with-args-until-success 'org-metadown-hook))
  13230. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13231. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13232. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13233. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13234. (defun org-shiftup (&optional arg)
  13235. "Increase item in timestamp or increase priority of current headline.
  13236. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13237. depending on context. See the individual commands for more information."
  13238. (interactive "P")
  13239. (cond
  13240. ((and org-support-shift-select (org-region-active-p))
  13241. (org-call-for-shift-select 'previous-line))
  13242. ((org-at-timestamp-p t)
  13243. (call-interactively (if org-edit-timestamp-down-means-later
  13244. 'org-timestamp-down 'org-timestamp-up)))
  13245. ((and (not (eq org-support-shift-select 'always))
  13246. org-enable-priority-commands
  13247. (org-on-heading-p))
  13248. (call-interactively 'org-priority-up))
  13249. ((and (not org-support-shift-select) (org-at-item-p))
  13250. (call-interactively 'org-previous-item))
  13251. ((org-clocktable-try-shift 'up arg))
  13252. (org-support-shift-select
  13253. (org-call-for-shift-select 'previous-line))
  13254. (t (org-shiftselect-error))))
  13255. (defun org-shiftdown (&optional arg)
  13256. "Decrease item in timestamp or decrease priority of current headline.
  13257. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13258. depending on context. See the individual commands for more information."
  13259. (interactive "P")
  13260. (cond
  13261. ((and org-support-shift-select (org-region-active-p))
  13262. (org-call-for-shift-select 'next-line))
  13263. ((org-at-timestamp-p t)
  13264. (call-interactively (if org-edit-timestamp-down-means-later
  13265. 'org-timestamp-up 'org-timestamp-down)))
  13266. ((and (not (eq org-support-shift-select 'always))
  13267. org-enable-priority-commands
  13268. (org-on-heading-p))
  13269. (call-interactively 'org-priority-down))
  13270. ((and (not org-support-shift-select) (org-at-item-p))
  13271. (call-interactively 'org-next-item))
  13272. ((org-clocktable-try-shift 'down arg))
  13273. (org-support-shift-select
  13274. (org-call-for-shift-select 'next-line))
  13275. (t (org-shiftselect-error))))
  13276. (defun org-shiftright (&optional arg)
  13277. "Cycle the thing at point or in the current line, depending on context.
  13278. Depending on context, this does one of the following:
  13279. - switch a timestamp at point one day into the future
  13280. - on a headline, switch to the next TODO keyword.
  13281. - on an item, switch entire list to the next bullet type
  13282. - on a property line, switch to the next allowed value
  13283. - on a clocktable definition line, move time block into the future"
  13284. (interactive "P")
  13285. (cond
  13286. ((and org-support-shift-select (org-region-active-p))
  13287. (org-call-for-shift-select 'forward-char))
  13288. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13289. ((and (not (eq org-support-shift-select 'always))
  13290. (org-on-heading-p))
  13291. (let ((org-inhibit-logging
  13292. (not org-treat-S-cursor-todo-selection-as-state-change))
  13293. (org-inhibit-blocking
  13294. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13295. (org-call-with-arg 'org-todo 'right)))
  13296. ((or (and org-support-shift-select
  13297. (not (eq org-support-shift-select 'always))
  13298. (org-at-item-bullet-p))
  13299. (and (not org-support-shift-select) (org-at-item-p)))
  13300. (org-call-with-arg 'org-cycle-list-bullet nil))
  13301. ((and (not (eq org-support-shift-select 'always))
  13302. (org-at-property-p))
  13303. (call-interactively 'org-property-next-allowed-value))
  13304. ((org-clocktable-try-shift 'right arg))
  13305. (org-support-shift-select
  13306. (org-call-for-shift-select 'forward-char))
  13307. (t (org-shiftselect-error))))
  13308. (defun org-shiftleft (&optional arg)
  13309. "Cycle the thing at point or in the current line, depending on context.
  13310. Depending on context, this does one of the following:
  13311. - switch a timestamp at point one day into the past
  13312. - on a headline, switch to the previous TODO keyword.
  13313. - on an item, switch entire list to the previous bullet type
  13314. - on a property line, switch to the previous allowed value
  13315. - on a clocktable definition line, move time block into the past"
  13316. (interactive "P")
  13317. (cond
  13318. ((and org-support-shift-select (org-region-active-p))
  13319. (org-call-for-shift-select 'backward-char))
  13320. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13321. ((and (not (eq org-support-shift-select 'always))
  13322. (org-on-heading-p))
  13323. (let ((org-inhibit-logging
  13324. (not org-treat-S-cursor-todo-selection-as-state-change))
  13325. (org-inhibit-blocking
  13326. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13327. (org-call-with-arg 'org-todo 'left)))
  13328. ((or (and org-support-shift-select
  13329. (not (eq org-support-shift-select 'always))
  13330. (org-at-item-bullet-p))
  13331. (and (not org-support-shift-select) (org-at-item-p)))
  13332. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13333. ((and (not (eq org-support-shift-select 'always))
  13334. (org-at-property-p))
  13335. (call-interactively 'org-property-previous-allowed-value))
  13336. ((org-clocktable-try-shift 'left arg))
  13337. (org-support-shift-select
  13338. (org-call-for-shift-select 'backward-char))
  13339. (t (org-shiftselect-error))))
  13340. (defun org-shiftcontrolright ()
  13341. "Switch to next TODO set."
  13342. (interactive)
  13343. (cond
  13344. ((and org-support-shift-select (org-region-active-p))
  13345. (org-call-for-shift-select 'forward-word))
  13346. ((and (not (eq org-support-shift-select 'always))
  13347. (org-on-heading-p))
  13348. (org-call-with-arg 'org-todo 'nextset))
  13349. (org-support-shift-select
  13350. (org-call-for-shift-select 'forward-word))
  13351. (t (org-shiftselect-error))))
  13352. (defun org-shiftcontrolleft ()
  13353. "Switch to previous TODO set."
  13354. (interactive)
  13355. (cond
  13356. ((and org-support-shift-select (org-region-active-p))
  13357. (org-call-for-shift-select 'backward-word))
  13358. ((and (not (eq org-support-shift-select 'always))
  13359. (org-on-heading-p))
  13360. (org-call-with-arg 'org-todo 'previousset))
  13361. (org-support-shift-select
  13362. (org-call-for-shift-select 'backward-word))
  13363. (t (org-shiftselect-error))))
  13364. (defun org-ctrl-c-ret ()
  13365. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13366. (interactive)
  13367. (cond
  13368. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13369. (t (call-interactively 'org-insert-heading))))
  13370. (defun org-copy-special ()
  13371. "Copy region in table or copy current subtree.
  13372. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13373. See the individual commands for more information."
  13374. (interactive)
  13375. (call-interactively
  13376. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13377. (defun org-cut-special ()
  13378. "Cut region in table or cut current subtree.
  13379. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13380. See the individual commands for more information."
  13381. (interactive)
  13382. (call-interactively
  13383. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13384. (defun org-paste-special (arg)
  13385. "Paste rectangular region into table, or past subtree relative to level.
  13386. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13387. See the individual commands for more information."
  13388. (interactive "P")
  13389. (if (org-at-table-p)
  13390. (org-table-paste-rectangle)
  13391. (org-paste-subtree arg)))
  13392. (defun org-edit-special ()
  13393. "Call a special editor for the stuff at point.
  13394. When at a table, call the formula editor with `org-table-edit-formulas'.
  13395. When at the first line of an src example, call `org-edit-src-code'.
  13396. When in an #+include line, visit the include file. Otherwise call
  13397. `ffap' to visit the file at point."
  13398. (interactive)
  13399. (cond
  13400. ((org-at-table-p)
  13401. (call-interactively 'org-table-edit-formulas))
  13402. ((save-excursion
  13403. (beginning-of-line 1)
  13404. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13405. (find-file (org-trim (match-string 1))))
  13406. ((org-edit-src-code))
  13407. ((org-edit-fixed-width-region))
  13408. (t (call-interactively 'ffap))))
  13409. (defun org-ctrl-c-ctrl-c (&optional arg)
  13410. "Set tags in headline, or update according to changed information at point.
  13411. This command does many different things, depending on context:
  13412. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13413. this is what we do.
  13414. - If the cursor is in a headline, prompt for tags and insert them
  13415. into the current line, aligned to `org-tags-column'. When called
  13416. with prefix arg, realign all tags in the current buffer.
  13417. - If the cursor is in one of the special #+KEYWORD lines, this
  13418. triggers scanning the buffer for these lines and updating the
  13419. information.
  13420. - If the cursor is inside a table, realign the table. This command
  13421. works even if the automatic table editor has been turned off.
  13422. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13423. the entire table.
  13424. - If the cursor is at a footnote reference or definition, jump to
  13425. the corresponding definition or references, respectively.
  13426. - If the cursor is a the beginning of a dynamic block, update it.
  13427. - If the cursor is inside a table created by the table.el package,
  13428. activate that table.
  13429. - If the current buffer is a remember buffer, close note and file
  13430. it. A prefix argument of 1 files to the default location
  13431. without further interaction. A prefix argument of 2 files to
  13432. the currently clocking task.
  13433. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13434. links in this buffer.
  13435. - If the cursor is on a numbered item in a plain list, renumber the
  13436. ordered list.
  13437. - If the cursor is on a checkbox, toggle it."
  13438. (interactive "P")
  13439. (let ((org-enable-table-editor t))
  13440. (cond
  13441. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13442. org-occur-highlights
  13443. org-latex-fragment-image-overlays)
  13444. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13445. (org-remove-occur-highlights)
  13446. (org-remove-latex-fragment-image-overlays)
  13447. (message "Temporary highlights/overlays removed from current buffer"))
  13448. ((and (local-variable-p 'org-finish-function (current-buffer))
  13449. (fboundp org-finish-function))
  13450. (funcall org-finish-function))
  13451. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13452. ((org-at-property-p)
  13453. (call-interactively 'org-property-action))
  13454. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13455. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13456. ((org-at-table.el-p)
  13457. (require 'table)
  13458. (beginning-of-line 1)
  13459. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13460. (call-interactively 'table-recognize-table))
  13461. ((org-at-table-p)
  13462. (org-table-maybe-eval-formula)
  13463. (if arg
  13464. (call-interactively 'org-table-recalculate)
  13465. (org-table-maybe-recalculate-line))
  13466. (call-interactively 'org-table-align))
  13467. ((or (org-footnote-at-reference-p)
  13468. (org-footnote-at-definition-p))
  13469. (call-interactively 'org-footnote-action))
  13470. ((org-at-item-checkbox-p)
  13471. (call-interactively 'org-toggle-checkbox))
  13472. ((org-at-item-p)
  13473. (if arg
  13474. (call-interactively 'org-toggle-checkbox)
  13475. (call-interactively 'org-maybe-renumber-ordered-list)))
  13476. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13477. ;; Dynamic block
  13478. (beginning-of-line 1)
  13479. (save-excursion (org-update-dblock)))
  13480. ((save-excursion
  13481. (beginning-of-line 1)
  13482. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13483. (cond
  13484. ((equal (match-string 1) "TBLFM")
  13485. ;; Recalculate the table before this line
  13486. (save-excursion
  13487. (beginning-of-line 1)
  13488. (skip-chars-backward " \r\n\t")
  13489. (if (org-at-table-p)
  13490. (org-call-with-arg 'org-table-recalculate t))))
  13491. (t
  13492. ; (org-set-regexps-and-options)
  13493. ; (org-restart-font-lock)
  13494. (let ((org-inhibit-startup t)) (org-mode-restart))
  13495. (message "Local setup has been refreshed"))))
  13496. ((org-clock-update-time-maybe))
  13497. (t (error "C-c C-c can do nothing useful at this location.")))))
  13498. (defun org-mode-restart ()
  13499. "Restart Org-mode, to scan again for special lines.
  13500. Also updates the keyword regular expressions."
  13501. (interactive)
  13502. (org-mode)
  13503. (message "Org-mode restarted"))
  13504. (defun org-kill-note-or-show-branches ()
  13505. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13506. (interactive)
  13507. (if (not org-finish-function)
  13508. (call-interactively 'show-branches)
  13509. (let ((org-note-abort t))
  13510. (funcall org-finish-function))))
  13511. (defun org-return (&optional indent)
  13512. "Goto next table row or insert a newline.
  13513. Calls `org-table-next-row' or `newline', depending on context.
  13514. See the individual commands for more information."
  13515. (interactive)
  13516. (cond
  13517. ((bobp) (if indent (newline-and-indent) (newline)))
  13518. ((org-at-table-p)
  13519. (org-table-justify-field-maybe)
  13520. (call-interactively 'org-table-next-row))
  13521. ((and org-return-follows-link
  13522. (eq (get-text-property (point) 'face) 'org-link))
  13523. (call-interactively 'org-open-at-point))
  13524. ((and (org-at-heading-p)
  13525. (looking-at
  13526. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13527. (org-show-entry)
  13528. (end-of-line 1)
  13529. (newline))
  13530. (t (if indent (newline-and-indent) (newline)))))
  13531. (defun org-return-indent ()
  13532. "Goto next table row or insert a newline and indent.
  13533. Calls `org-table-next-row' or `newline-and-indent', depending on
  13534. context. See the individual commands for more information."
  13535. (interactive)
  13536. (org-return t))
  13537. (defun org-ctrl-c-star ()
  13538. "Compute table, or change heading status of lines.
  13539. Calls `org-table-recalculate' or `org-toggle-heading',
  13540. depending on context."
  13541. (interactive)
  13542. (cond
  13543. ((org-at-table-p)
  13544. (call-interactively 'org-table-recalculate))
  13545. (t
  13546. ;; Convert all lines in region to list items
  13547. (call-interactively 'org-toggle-heading))))
  13548. (defun org-ctrl-c-minus ()
  13549. "Insert separator line in table or modify bullet status of line.
  13550. Also turns a plain line or a region of lines into list items.
  13551. Calls `org-table-insert-hline', `org-toggle-item', or
  13552. `org-cycle-list-bullet', depending on context."
  13553. (interactive)
  13554. (cond
  13555. ((org-at-table-p)
  13556. (call-interactively 'org-table-insert-hline))
  13557. ((org-region-active-p)
  13558. (call-interactively 'org-toggle-item))
  13559. ((org-in-item-p)
  13560. (call-interactively 'org-cycle-list-bullet))
  13561. (t
  13562. (call-interactively 'org-toggle-item))))
  13563. (defun org-toggle-item ()
  13564. "Convert headings or normal lines to items, items to normal lines.
  13565. If there is no active region, only the current line is considered.
  13566. If the first line in the region is a headline, convert all headlines to items.
  13567. If the first line in the region is an item, convert all items to normal lines.
  13568. If the first line is normal text, add an item bullet to each line."
  13569. (interactive)
  13570. (let (l2 l beg end)
  13571. (if (org-region-active-p)
  13572. (setq beg (region-beginning) end (region-end))
  13573. (setq beg (point-at-bol)
  13574. end (min (1+ (point-at-eol)) (point-max))))
  13575. (save-excursion
  13576. (goto-char end)
  13577. (setq l2 (org-current-line))
  13578. (goto-char beg)
  13579. (beginning-of-line 1)
  13580. (setq l (1- (org-current-line)))
  13581. (if (org-at-item-p)
  13582. ;; We already have items, de-itemize
  13583. (while (< (setq l (1+ l)) l2)
  13584. (when (org-at-item-p)
  13585. (goto-char (match-beginning 2))
  13586. (delete-region (match-beginning 2) (match-end 2))
  13587. (and (looking-at "[ \t]+") (replace-match "")))
  13588. (beginning-of-line 2))
  13589. (if (org-on-heading-p)
  13590. ;; Headings, convert to items
  13591. (while (< (setq l (1+ l)) l2)
  13592. (if (looking-at org-outline-regexp)
  13593. (replace-match "- " t t))
  13594. (beginning-of-line 2))
  13595. ;; normal lines, turn them into items
  13596. (while (< (setq l (1+ l)) l2)
  13597. (unless (org-at-item-p)
  13598. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13599. (replace-match "\\1- \\2")))
  13600. (beginning-of-line 2)))))))
  13601. (defun org-toggle-heading (&optional nstars)
  13602. "Convert headings to normal text, or items or text to headings.
  13603. If there is no active region, only the current line is considered.
  13604. If the first line is a heading, remove the stars from all headlines
  13605. in the region.
  13606. If the first line is a plain list item, turn all plain list items
  13607. into headings.
  13608. If the first line is a normal line, turn each and every line in the
  13609. region into a heading.
  13610. When converting a line into a heading, the number of stars is chosen
  13611. such that the lines become children of the current entry. However,
  13612. when a prefix argument is given, its value determines the number of
  13613. stars to add."
  13614. (interactive "P")
  13615. (let (l2 l itemp beg end)
  13616. (if (org-region-active-p)
  13617. (setq beg (region-beginning) end (region-end))
  13618. (setq beg (point-at-bol)
  13619. end (min (1+ (point-at-eol)) (point-max))))
  13620. (save-excursion
  13621. (goto-char end)
  13622. (setq l2 (org-current-line))
  13623. (goto-char beg)
  13624. (beginning-of-line 1)
  13625. (setq l (1- (org-current-line)))
  13626. (if (org-on-heading-p)
  13627. ;; We already have headlines, de-star them
  13628. (while (< (setq l (1+ l)) l2)
  13629. (when (org-on-heading-p t)
  13630. (and (looking-at outline-regexp) (replace-match "")))
  13631. (beginning-of-line 2))
  13632. (setq itemp (org-at-item-p))
  13633. (let* ((stars
  13634. (if nstars
  13635. (make-string (prefix-numeric-value current-prefix-arg)
  13636. ?*)
  13637. (save-excursion
  13638. (if (re-search-backward org-complex-heading-regexp nil t)
  13639. (match-string 1) ""))))
  13640. (add-stars (cond (nstars "")
  13641. ((equal stars "") "*")
  13642. (org-odd-levels-only "**")
  13643. (t "*")))
  13644. (rpl (concat stars add-stars " ")))
  13645. (while (< (setq l (1+ l)) l2)
  13646. (if itemp
  13647. (and (org-at-item-p) (replace-match rpl t t))
  13648. (unless (org-on-heading-p)
  13649. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13650. (replace-match (concat rpl (match-string 2))))))
  13651. (beginning-of-line 2)))))))
  13652. (defun org-meta-return (&optional arg)
  13653. "Insert a new heading or wrap a region in a table.
  13654. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13655. See the individual commands for more information."
  13656. (interactive "P")
  13657. (cond
  13658. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13659. ((org-at-table-p)
  13660. (call-interactively 'org-table-wrap-region))
  13661. (t (call-interactively 'org-insert-heading))))
  13662. ;;; Menu entries
  13663. ;; Define the Org-mode menus
  13664. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13665. '("Tbl"
  13666. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13667. ["Next Field" org-cycle (org-at-table-p)]
  13668. ["Previous Field" org-shifttab (org-at-table-p)]
  13669. ["Next Row" org-return (org-at-table-p)]
  13670. "--"
  13671. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13672. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13673. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13674. "--"
  13675. ("Column"
  13676. ["Move Column Left" org-metaleft (org-at-table-p)]
  13677. ["Move Column Right" org-metaright (org-at-table-p)]
  13678. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13679. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13680. ("Row"
  13681. ["Move Row Up" org-metaup (org-at-table-p)]
  13682. ["Move Row Down" org-metadown (org-at-table-p)]
  13683. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13684. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13685. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13686. "--"
  13687. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13688. ("Rectangle"
  13689. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13690. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13691. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13692. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13693. "--"
  13694. ("Calculate"
  13695. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13696. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13697. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13698. "--"
  13699. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13700. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13701. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13702. "--"
  13703. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13704. "--"
  13705. ["Sum Column/Rectangle" org-table-sum
  13706. (or (org-at-table-p) (org-region-active-p))]
  13707. ["Which Column?" org-table-current-column (org-at-table-p)])
  13708. ["Debug Formulas"
  13709. org-table-toggle-formula-debugger
  13710. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13711. ["Show Col/Row Numbers"
  13712. org-table-toggle-coordinate-overlays
  13713. :style toggle
  13714. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13715. "--"
  13716. ["Create" org-table-create (and (not (org-at-table-p))
  13717. org-enable-table-editor)]
  13718. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13719. ["Import from File" org-table-import (not (org-at-table-p))]
  13720. ["Export to File" org-table-export (org-at-table-p)]
  13721. "--"
  13722. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13723. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13724. '("Org"
  13725. ("Show/Hide"
  13726. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13727. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13728. ["Sparse Tree..." org-sparse-tree t]
  13729. ["Reveal Context" org-reveal t]
  13730. ["Show All" show-all t]
  13731. "--"
  13732. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13733. "--"
  13734. ["New Heading" org-insert-heading t]
  13735. ("Navigate Headings"
  13736. ["Up" outline-up-heading t]
  13737. ["Next" outline-next-visible-heading t]
  13738. ["Previous" outline-previous-visible-heading t]
  13739. ["Next Same Level" outline-forward-same-level t]
  13740. ["Previous Same Level" outline-backward-same-level t]
  13741. "--"
  13742. ["Jump" org-goto t])
  13743. ("Edit Structure"
  13744. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13745. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13746. "--"
  13747. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13748. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13749. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13750. "--"
  13751. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13752. "--"
  13753. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13754. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13755. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13756. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13757. "--"
  13758. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13759. "--"
  13760. ["Convert to odd levels" org-convert-to-odd-levels t]
  13761. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13762. ("Editing"
  13763. ["Emphasis..." org-emphasize t]
  13764. ["Edit Source Example" org-edit-special t]
  13765. "--"
  13766. ["Footnote new/jump" org-footnote-action t]
  13767. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13768. ("Archive"
  13769. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13770. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13771. ; :active t :keys "C-u C-c C-x C-a"]
  13772. ["Sparse trees open ARCHIVE trees"
  13773. (setq org-sparse-tree-open-archived-trees
  13774. (not org-sparse-tree-open-archived-trees))
  13775. :style toggle :selected org-sparse-tree-open-archived-trees]
  13776. ["Cycling opens ARCHIVE trees"
  13777. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13778. :style toggle :selected org-cycle-open-archived-trees]
  13779. "--"
  13780. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13781. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13782. ; ["Check and Move Children" (org-archive-subtree '(4))
  13783. ; :active t :keys "C-u C-c C-x C-s"]
  13784. )
  13785. "--"
  13786. ("Hyperlinks"
  13787. ["Store Link (Global)" org-store-link t]
  13788. ["Find existing link to here" org-occur-link-in-agenda-files t]
  13789. ["Insert Link" org-insert-link t]
  13790. ["Follow Link" org-open-at-point t]
  13791. "--"
  13792. ["Next link" org-next-link t]
  13793. ["Previous link" org-previous-link t]
  13794. "--"
  13795. ["Descriptive Links"
  13796. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13797. :style radio
  13798. :selected (member '(org-link) buffer-invisibility-spec)]
  13799. ["Literal Links"
  13800. (progn
  13801. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13802. :style radio
  13803. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13804. "--"
  13805. ("TODO Lists"
  13806. ["TODO/DONE/-" org-todo t]
  13807. ("Select keyword"
  13808. ["Next keyword" org-shiftright (org-on-heading-p)]
  13809. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13810. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13811. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13812. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13813. ["Show TODO Tree" org-show-todo-tree t]
  13814. ["Global TODO list" org-todo-list t]
  13815. "--"
  13816. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13817. :selected org-enforce-todo-dependencies :style toggle :active t]
  13818. "Settings for tree at point"
  13819. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13820. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13821. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13822. ["Do Children parallel" org-toggle-ordered-property :style radio
  13823. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13824. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13825. "--"
  13826. ["Set Priority" org-priority t]
  13827. ["Priority Up" org-shiftup t]
  13828. ["Priority Down" org-shiftdown t]
  13829. "--"
  13830. ["Get news from all feeds" org-feed-update-all t]
  13831. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13832. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13833. ("TAGS and Properties"
  13834. ["Set Tags" org-set-tags-command t]
  13835. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13836. "--"
  13837. ["Set property" org-set-property t]
  13838. ["Column view of properties" org-columns t]
  13839. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13840. ("Dates and Scheduling"
  13841. ["Timestamp" org-time-stamp t]
  13842. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13843. ("Change Date"
  13844. ["1 Day Later" org-shiftright t]
  13845. ["1 Day Earlier" org-shiftleft t]
  13846. ["1 ... Later" org-shiftup t]
  13847. ["1 ... Earlier" org-shiftdown t])
  13848. ["Compute Time Range" org-evaluate-time-range t]
  13849. ["Schedule Item" org-schedule t]
  13850. ["Deadline" org-deadline t]
  13851. "--"
  13852. ["Custom time format" org-toggle-time-stamp-overlays
  13853. :style radio :selected org-display-custom-times]
  13854. "--"
  13855. ["Goto Calendar" org-goto-calendar t]
  13856. ["Date from Calendar" org-date-from-calendar t]
  13857. "--"
  13858. ["Start/Restart Timer" org-timer-start t]
  13859. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13860. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13861. ["Insert Timer String" org-timer t]
  13862. ["Insert Timer Item" org-timer-item t])
  13863. ("Logging work"
  13864. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  13865. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  13866. ["Clock out" org-clock-out t]
  13867. ["Clock cancel" org-clock-cancel t]
  13868. "--"
  13869. ["Mark as default task" org-clock-mark-default-task t]
  13870. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  13871. ["Goto running clock" org-clock-goto t]
  13872. "--"
  13873. ["Display times" org-clock-display t]
  13874. ["Create clock table" org-clock-report t]
  13875. "--"
  13876. ["Record DONE time"
  13877. (progn (setq org-log-done (not org-log-done))
  13878. (message "Switching to %s will %s record a timestamp"
  13879. (car org-done-keywords)
  13880. (if org-log-done "automatically" "not")))
  13881. :style toggle :selected org-log-done])
  13882. "--"
  13883. ["Agenda Command..." org-agenda t]
  13884. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13885. ("File List for Agenda")
  13886. ("Special views current file"
  13887. ["TODO Tree" org-show-todo-tree t]
  13888. ["Check Deadlines" org-check-deadlines t]
  13889. ["Timeline" org-timeline t]
  13890. ["Tags/Property tree" org-match-sparse-tree t])
  13891. "--"
  13892. ["Export/Publish..." org-export t]
  13893. ("LaTeX"
  13894. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13895. :selected org-cdlatex-mode]
  13896. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13897. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13898. ["Modify math symbol" org-cdlatex-math-modify
  13899. (org-inside-LaTeX-fragment-p)]
  13900. ["Insert citation" org-reftex-citation t]
  13901. "--"
  13902. ["Export LaTeX fragments as images"
  13903. (if (featurep 'org-exp)
  13904. (setq org-export-with-LaTeX-fragments
  13905. (not org-export-with-LaTeX-fragments))
  13906. (require 'org-exp))
  13907. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13908. org-export-with-LaTeX-fragments)])
  13909. "--"
  13910. ("Documentation"
  13911. ["Show Version" org-version t]
  13912. ["Info Documentation" org-info t])
  13913. ("Customize"
  13914. ["Browse Org Group" org-customize t]
  13915. "--"
  13916. ["Expand This Menu" org-create-customize-menu
  13917. (fboundp 'customize-menu-create)])
  13918. "--"
  13919. ("Refresh/Reload"
  13920. ["Refresh setup current buffer" org-mode-restart t]
  13921. ["Reload Org (after update)" org-reload t]
  13922. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13923. ))
  13924. (defun org-info (&optional node)
  13925. "Read documentation for Org-mode in the info system.
  13926. With optional NODE, go directly to that node."
  13927. (interactive)
  13928. (info (format "(org)%s" (or node ""))))
  13929. (defun org-install-agenda-files-menu ()
  13930. (let ((bl (buffer-list)))
  13931. (save-excursion
  13932. (while bl
  13933. (set-buffer (pop bl))
  13934. (if (org-mode-p) (setq bl nil)))
  13935. (when (org-mode-p)
  13936. (easy-menu-change
  13937. '("Org") "File List for Agenda"
  13938. (append
  13939. (list
  13940. ["Edit File List" (org-edit-agenda-file-list) t]
  13941. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13942. ["Remove Current File from List" org-remove-file t]
  13943. ["Cycle through agenda files" org-cycle-agenda-files t]
  13944. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13945. "--")
  13946. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13947. ;;;; Documentation
  13948. ;;;###autoload
  13949. (defun org-require-autoloaded-modules ()
  13950. (interactive)
  13951. (mapc 'require
  13952. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  13953. org-docbook org-exp org-html org-icalendar
  13954. org-id org-latex
  13955. org-publish org-remember org-table
  13956. org-timer org-xoxo)))
  13957. ;;;###autoload
  13958. (defun org-reload (&optional uncompiled)
  13959. "Reload all org lisp files.
  13960. With prefix arg UNCOMPILED, load the uncompiled versions."
  13961. (interactive "P")
  13962. (require 'find-func)
  13963. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  13964. (dir-org (file-name-directory (org-find-library-name "org")))
  13965. (dir-org-contrib (ignore-errors
  13966. (file-name-directory
  13967. (org-find-library-name "org-contribdir"))))
  13968. (files
  13969. (append (directory-files dir-org t file-re)
  13970. (and dir-org-contrib
  13971. (directory-files dir-org-contrib t file-re))))
  13972. (remove-re (concat (if (featurep 'xemacs)
  13973. "org-colview" "org-colview-xemacs")
  13974. "\\'")))
  13975. (setq files (mapcar 'file-name-sans-extension files))
  13976. (setq files (mapcar
  13977. (lambda (x) (if (string-match remove-re x) nil x))
  13978. files))
  13979. (setq files (delq nil files))
  13980. (mapc
  13981. (lambda (f)
  13982. (when (featurep (intern (file-name-nondirectory f)))
  13983. (if (and (not uncompiled)
  13984. (file-exists-p (concat f ".elc")))
  13985. (load (concat f ".elc") nil nil t)
  13986. (load (concat f ".el") nil nil t))))
  13987. files))
  13988. (org-version))
  13989. ;;;###autoload
  13990. (defun org-customize ()
  13991. "Call the customize function with org as argument."
  13992. (interactive)
  13993. (org-load-modules-maybe)
  13994. (org-require-autoloaded-modules)
  13995. (customize-browse 'org))
  13996. (defun org-create-customize-menu ()
  13997. "Create a full customization menu for Org-mode, insert it into the menu."
  13998. (interactive)
  13999. (org-load-modules-maybe)
  14000. (org-require-autoloaded-modules)
  14001. (if (fboundp 'customize-menu-create)
  14002. (progn
  14003. (easy-menu-change
  14004. '("Org") "Customize"
  14005. `(["Browse Org group" org-customize t]
  14006. "--"
  14007. ,(customize-menu-create 'org)
  14008. ["Set" Custom-set t]
  14009. ["Save" Custom-save t]
  14010. ["Reset to Current" Custom-reset-current t]
  14011. ["Reset to Saved" Custom-reset-saved t]
  14012. ["Reset to Standard Settings" Custom-reset-standard t]))
  14013. (message "\"Org\"-menu now contains full customization menu"))
  14014. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14015. ;;;; Miscellaneous stuff
  14016. ;;; Generally useful functions
  14017. (defun org-find-text-property-in-string (prop s)
  14018. "Return the first non-nil value of property PROP in string S."
  14019. (or (get-text-property 0 prop s)
  14020. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14021. prop s)))
  14022. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14023. "Display the given MESSAGE as a warning."
  14024. (if (fboundp 'display-warning)
  14025. (display-warning 'org message
  14026. (if (featurep 'xemacs)
  14027. 'warning
  14028. :warning))
  14029. (let ((buf (get-buffer-create "*Org warnings*")))
  14030. (with-current-buffer buf
  14031. (goto-char (point-max))
  14032. (insert "Warning (Org): " message)
  14033. (unless (bolp)
  14034. (newline)))
  14035. (display-buffer buf)
  14036. (sit-for 0))))
  14037. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14038. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14039. (if (and marker (marker-buffer marker)
  14040. (buffer-live-p (marker-buffer marker)))
  14041. (progn
  14042. (switch-to-buffer (marker-buffer marker))
  14043. (if (or (> marker (point-max)) (< marker (point-min)))
  14044. (widen))
  14045. (goto-char marker)
  14046. (org-show-context 'org-goto))
  14047. (if bookmark
  14048. (bookmark-jump bookmark)
  14049. (error "Cannot find location"))))
  14050. (defun org-quote-csv-field (s)
  14051. "Quote field for inclusion in CSV material."
  14052. (if (string-match "[\",]" s)
  14053. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14054. s))
  14055. (defun org-plist-delete (plist property)
  14056. "Delete PROPERTY from PLIST.
  14057. This is in contrast to merely setting it to 0."
  14058. (let (p)
  14059. (while plist
  14060. (if (not (eq property (car plist)))
  14061. (setq p (plist-put p (car plist) (nth 1 plist))))
  14062. (setq plist (cddr plist)))
  14063. p))
  14064. (defun org-force-self-insert (N)
  14065. "Needed to enforce self-insert under remapping."
  14066. (interactive "p")
  14067. (self-insert-command N))
  14068. (defun org-string-width (s)
  14069. "Compute width of string, ignoring invisible characters.
  14070. This ignores character with invisibility property `org-link', and also
  14071. characters with property `org-cwidth', because these will become invisible
  14072. upon the next fontification round."
  14073. (let (b l)
  14074. (when (or (eq t buffer-invisibility-spec)
  14075. (assq 'org-link buffer-invisibility-spec))
  14076. (while (setq b (text-property-any 0 (length s)
  14077. 'invisible 'org-link s))
  14078. (setq s (concat (substring s 0 b)
  14079. (substring s (or (next-single-property-change
  14080. b 'invisible s) (length s)))))))
  14081. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14082. (setq s (concat (substring s 0 b)
  14083. (substring s (or (next-single-property-change
  14084. b 'org-cwidth s) (length s))))))
  14085. (setq l (string-width s) b -1)
  14086. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14087. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14088. l))
  14089. (defun org-get-indentation (&optional line)
  14090. "Get the indentation of the current line, interpreting tabs.
  14091. When LINE is given, assume it represents a line and compute its indentation."
  14092. (if line
  14093. (if (string-match "^ *" (org-remove-tabs line))
  14094. (match-end 0))
  14095. (save-excursion
  14096. (beginning-of-line 1)
  14097. (skip-chars-forward " \t")
  14098. (current-column))))
  14099. (defun org-remove-tabs (s &optional width)
  14100. "Replace tabulators in S with spaces.
  14101. Assumes that s is a single line, starting in column 0."
  14102. (setq width (or width tab-width))
  14103. (while (string-match "\t" s)
  14104. (setq s (replace-match
  14105. (make-string
  14106. (- (* width (/ (+ (match-beginning 0) width) width))
  14107. (match-beginning 0)) ?\ )
  14108. t t s)))
  14109. s)
  14110. (defun org-fix-indentation (line ind)
  14111. "Fix indentation in LINE.
  14112. IND is a cons cell with target and minimum indentation.
  14113. If the current indentation in LINE is smaller than the minimum,
  14114. leave it alone. If it is larger than ind, set it to the target."
  14115. (let* ((l (org-remove-tabs line))
  14116. (i (org-get-indentation l))
  14117. (i1 (car ind)) (i2 (cdr ind)))
  14118. (if (>= i i2) (setq l (substring line i2)))
  14119. (if (> i1 0)
  14120. (concat (make-string i1 ?\ ) l)
  14121. l)))
  14122. (defun org-remove-indentation (code &optional n)
  14123. "Remove the maximum common indentation from the lines in CODE.
  14124. N may optionally be the number of spaces to remove."
  14125. (with-temp-buffer
  14126. (insert code)
  14127. (org-do-remove-indentation n)
  14128. (buffer-string)))
  14129. (defun org-do-remove-indentation (&optional n)
  14130. "Remove the maximum common indentation from the buffer."
  14131. (untabify (point-min) (point-max))
  14132. (let ((min 10000) re)
  14133. (if n
  14134. (setq min n)
  14135. (goto-char (point-min))
  14136. (while (re-search-forward "^ *[^ \n]" nil t)
  14137. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14138. (unless (or (= min 0) (= min 10000))
  14139. (setq re (format "^ \\{%d\\}" min))
  14140. (goto-char (point-min))
  14141. (while (re-search-forward re nil t)
  14142. (replace-match "")
  14143. (end-of-line 1))
  14144. min)))
  14145. (defun org-base-buffer (buffer)
  14146. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14147. (if (not buffer)
  14148. buffer
  14149. (or (buffer-base-buffer buffer)
  14150. buffer)))
  14151. (defun org-trim (s)
  14152. "Remove whitespace at beginning and end of string."
  14153. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14154. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14155. s)
  14156. (defun org-wrap (string &optional width lines)
  14157. "Wrap string to either a number of lines, or a width in characters.
  14158. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14159. that costs. If there is a word longer than WIDTH, the text is actually
  14160. wrapped to the length of that word.
  14161. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14162. many lines, whatever width that takes.
  14163. The return value is a list of lines, without newlines at the end."
  14164. (let* ((words (org-split-string string "[ \t\n]+"))
  14165. (maxword (apply 'max (mapcar 'org-string-width words)))
  14166. w ll)
  14167. (cond (width
  14168. (org-do-wrap words (max maxword width)))
  14169. (lines
  14170. (setq w maxword)
  14171. (setq ll (org-do-wrap words maxword))
  14172. (if (<= (length ll) lines)
  14173. ll
  14174. (setq ll words)
  14175. (while (> (length ll) lines)
  14176. (setq w (1+ w))
  14177. (setq ll (org-do-wrap words w)))
  14178. ll))
  14179. (t (error "Cannot wrap this")))))
  14180. (defun org-do-wrap (words width)
  14181. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14182. (let (lines line)
  14183. (while words
  14184. (setq line (pop words))
  14185. (while (and words (< (+ (length line) (length (car words))) width))
  14186. (setq line (concat line " " (pop words))))
  14187. (setq lines (push line lines)))
  14188. (nreverse lines)))
  14189. (defun org-split-string (string &optional separators)
  14190. "Splits STRING into substrings at SEPARATORS.
  14191. No empty strings are returned if there are matches at the beginning
  14192. and end of string."
  14193. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14194. (start 0)
  14195. notfirst
  14196. (list nil))
  14197. (while (and (string-match rexp string
  14198. (if (and notfirst
  14199. (= start (match-beginning 0))
  14200. (< start (length string)))
  14201. (1+ start) start))
  14202. (< (match-beginning 0) (length string)))
  14203. (setq notfirst t)
  14204. (or (eq (match-beginning 0) 0)
  14205. (and (eq (match-beginning 0) (match-end 0))
  14206. (eq (match-beginning 0) start))
  14207. (setq list
  14208. (cons (substring string start (match-beginning 0))
  14209. list)))
  14210. (setq start (match-end 0)))
  14211. (or (eq start (length string))
  14212. (setq list
  14213. (cons (substring string start)
  14214. list)))
  14215. (nreverse list)))
  14216. (defun org-quote-vert (s)
  14217. "Replace \"|\" with \"\\vert\"."
  14218. (while (string-match "|" s)
  14219. (setq s (replace-match "\\vert" t t s)))
  14220. s)
  14221. (defun org-uuidgen-p (s)
  14222. "Is S an ID created by UUIDGEN?"
  14223. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14224. (defun org-context ()
  14225. "Return a list of contexts of the current cursor position.
  14226. If several contexts apply, all are returned.
  14227. Each context entry is a list with a symbol naming the context, and
  14228. two positions indicating start and end of the context. Possible
  14229. contexts are:
  14230. :headline anywhere in a headline
  14231. :headline-stars on the leading stars in a headline
  14232. :todo-keyword on a TODO keyword (including DONE) in a headline
  14233. :tags on the TAGS in a headline
  14234. :priority on the priority cookie in a headline
  14235. :item on the first line of a plain list item
  14236. :item-bullet on the bullet/number of a plain list item
  14237. :checkbox on the checkbox in a plain list item
  14238. :table in an org-mode table
  14239. :table-special on a special filed in a table
  14240. :table-table in a table.el table
  14241. :link on a hyperlink
  14242. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14243. :target on a <<target>>
  14244. :radio-target on a <<<radio-target>>>
  14245. :latex-fragment on a LaTeX fragment
  14246. :latex-preview on a LaTeX fragment with overlayed preview image
  14247. This function expects the position to be visible because it uses font-lock
  14248. faces as a help to recognize the following contexts: :table-special, :link,
  14249. and :keyword."
  14250. (let* ((f (get-text-property (point) 'face))
  14251. (faces (if (listp f) f (list f)))
  14252. (p (point)) clist o)
  14253. ;; First the large context
  14254. (cond
  14255. ((org-on-heading-p t)
  14256. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14257. (when (progn
  14258. (beginning-of-line 1)
  14259. (looking-at org-todo-line-tags-regexp))
  14260. (push (org-point-in-group p 1 :headline-stars) clist)
  14261. (push (org-point-in-group p 2 :todo-keyword) clist)
  14262. (push (org-point-in-group p 4 :tags) clist))
  14263. (goto-char p)
  14264. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14265. (if (looking-at "\\[#[A-Z0-9]\\]")
  14266. (push (org-point-in-group p 0 :priority) clist)))
  14267. ((org-at-item-p)
  14268. (push (org-point-in-group p 2 :item-bullet) clist)
  14269. (push (list :item (point-at-bol)
  14270. (save-excursion (org-end-of-item) (point)))
  14271. clist)
  14272. (and (org-at-item-checkbox-p)
  14273. (push (org-point-in-group p 0 :checkbox) clist)))
  14274. ((org-at-table-p)
  14275. (push (list :table (org-table-begin) (org-table-end)) clist)
  14276. (if (memq 'org-formula faces)
  14277. (push (list :table-special
  14278. (previous-single-property-change p 'face)
  14279. (next-single-property-change p 'face)) clist)))
  14280. ((org-at-table-p 'any)
  14281. (push (list :table-table) clist)))
  14282. (goto-char p)
  14283. ;; Now the small context
  14284. (cond
  14285. ((org-at-timestamp-p)
  14286. (push (org-point-in-group p 0 :timestamp) clist))
  14287. ((memq 'org-link faces)
  14288. (push (list :link
  14289. (previous-single-property-change p 'face)
  14290. (next-single-property-change p 'face)) clist))
  14291. ((memq 'org-special-keyword faces)
  14292. (push (list :keyword
  14293. (previous-single-property-change p 'face)
  14294. (next-single-property-change p 'face)) clist))
  14295. ((org-on-target-p)
  14296. (push (org-point-in-group p 0 :target) clist)
  14297. (goto-char (1- (match-beginning 0)))
  14298. (if (looking-at org-radio-target-regexp)
  14299. (push (org-point-in-group p 0 :radio-target) clist))
  14300. (goto-char p))
  14301. ((setq o (car (delq nil
  14302. (mapcar
  14303. (lambda (x)
  14304. (if (memq x org-latex-fragment-image-overlays) x))
  14305. (org-overlays-at (point))))))
  14306. (push (list :latex-fragment
  14307. (org-overlay-start o) (org-overlay-end o)) clist)
  14308. (push (list :latex-preview
  14309. (org-overlay-start o) (org-overlay-end o)) clist))
  14310. ((org-inside-LaTeX-fragment-p)
  14311. ;; FIXME: positions wrong.
  14312. (push (list :latex-fragment (point) (point)) clist)))
  14313. (setq clist (nreverse (delq nil clist)))
  14314. clist))
  14315. ;; FIXME: Compare with at-regexp-p Do we need both?
  14316. (defun org-in-regexp (re &optional nlines visually)
  14317. "Check if point is inside a match of regexp.
  14318. Normally only the current line is checked, but you can include NLINES extra
  14319. lines both before and after point into the search.
  14320. If VISUALLY is set, require that the cursor is not after the match but
  14321. really on, so that the block visually is on the match."
  14322. (catch 'exit
  14323. (let ((pos (point))
  14324. (eol (point-at-eol (+ 1 (or nlines 0))))
  14325. (inc (if visually 1 0)))
  14326. (save-excursion
  14327. (beginning-of-line (- 1 (or nlines 0)))
  14328. (while (re-search-forward re eol t)
  14329. (if (and (<= (match-beginning 0) pos)
  14330. (>= (+ inc (match-end 0)) pos))
  14331. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14332. (defun org-at-regexp-p (regexp)
  14333. "Is point inside a match of REGEXP in the current line?"
  14334. (catch 'exit
  14335. (save-excursion
  14336. (let ((pos (point)) (end (point-at-eol)))
  14337. (beginning-of-line 1)
  14338. (while (re-search-forward regexp end t)
  14339. (if (and (<= (match-beginning 0) pos)
  14340. (>= (match-end 0) pos))
  14341. (throw 'exit t)))
  14342. nil))))
  14343. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14344. "Call `multi-occur' with buffers for all agenda files."
  14345. (interactive "sOrg-files matching: \np")
  14346. (let* ((files (org-agenda-files))
  14347. (tnames (mapcar 'file-truename files))
  14348. (extra org-agenda-text-search-extra-files)
  14349. f)
  14350. (when (eq (car extra) 'agenda-archives)
  14351. (setq extra (cdr extra))
  14352. (setq files (org-add-archive-files files)))
  14353. (while (setq f (pop extra))
  14354. (unless (member (file-truename f) tnames)
  14355. (add-to-list 'files f 'append)
  14356. (add-to-list 'tnames (file-truename f) 'append)))
  14357. (multi-occur
  14358. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14359. regexp)))
  14360. (if (boundp 'occur-mode-find-occurrence-hook)
  14361. ;; Emacs 23
  14362. (add-hook 'occur-mode-find-occurrence-hook
  14363. (lambda ()
  14364. (when (org-mode-p)
  14365. (org-reveal))))
  14366. ;; Emacs 22
  14367. (defadvice occur-mode-goto-occurrence
  14368. (after org-occur-reveal activate)
  14369. (and (org-mode-p) (org-reveal)))
  14370. (defadvice occur-mode-goto-occurrence-other-window
  14371. (after org-occur-reveal activate)
  14372. (and (org-mode-p) (org-reveal)))
  14373. (defadvice occur-mode-display-occurrence
  14374. (after org-occur-reveal activate)
  14375. (when (org-mode-p)
  14376. (let ((pos (occur-mode-find-occurrence)))
  14377. (with-current-buffer (marker-buffer pos)
  14378. (save-excursion
  14379. (goto-char pos)
  14380. (org-reveal)))))))
  14381. (defun org-occur-link-in-agenda-files ()
  14382. "Create a link and search for it in the agendas.
  14383. The link is not stored in `org-stored-links', it is just created
  14384. for the search purpose."
  14385. (interactive)
  14386. (let ((link (condition-case nil
  14387. (org-store-link nil)
  14388. (error "Unable to create a link to here"))))
  14389. (org-occur-in-agenda-files (regexp-quote link))))
  14390. (defun org-uniquify (list)
  14391. "Remove duplicate elements from LIST."
  14392. (let (res)
  14393. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14394. res))
  14395. (defun org-delete-all (elts list)
  14396. "Remove all elements in ELTS from LIST."
  14397. (while elts
  14398. (setq list (delete (pop elts) list)))
  14399. list)
  14400. (defun org-back-over-empty-lines ()
  14401. "Move backwards over whitespace, to the beginning of the first empty line.
  14402. Returns the number of empty lines passed."
  14403. (let ((pos (point)))
  14404. (skip-chars-backward " \t\n\r")
  14405. (beginning-of-line 2)
  14406. (goto-char (min (point) pos))
  14407. (count-lines (point) pos)))
  14408. (defun org-skip-whitespace ()
  14409. (skip-chars-forward " \t\n\r"))
  14410. (defun org-point-in-group (point group &optional context)
  14411. "Check if POINT is in match-group GROUP.
  14412. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14413. match. If the match group does ot exist or point is not inside it,
  14414. return nil."
  14415. (and (match-beginning group)
  14416. (>= point (match-beginning group))
  14417. (<= point (match-end group))
  14418. (if context
  14419. (list context (match-beginning group) (match-end group))
  14420. t)))
  14421. (defun org-switch-to-buffer-other-window (&rest args)
  14422. "Switch to buffer in a second window on the current frame.
  14423. In particular, do not allow pop-up frames."
  14424. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14425. special-display-function)
  14426. (apply 'switch-to-buffer-other-window args)))
  14427. (defun org-combine-plists (&rest plists)
  14428. "Create a single property list from all plists in PLISTS.
  14429. The process starts by copying the first list, and then setting properties
  14430. from the other lists. Settings in the last list are the most significant
  14431. ones and overrule settings in the other lists."
  14432. (let ((rtn (copy-sequence (pop plists)))
  14433. p v ls)
  14434. (while plists
  14435. (setq ls (pop plists))
  14436. (while ls
  14437. (setq p (pop ls) v (pop ls))
  14438. (setq rtn (plist-put rtn p v))))
  14439. rtn))
  14440. (defun org-move-line-down (arg)
  14441. "Move the current line down. With prefix argument, move it past ARG lines."
  14442. (interactive "p")
  14443. (let ((col (current-column))
  14444. beg end pos)
  14445. (beginning-of-line 1) (setq beg (point))
  14446. (beginning-of-line 2) (setq end (point))
  14447. (beginning-of-line (+ 1 arg))
  14448. (setq pos (move-marker (make-marker) (point)))
  14449. (insert (delete-and-extract-region beg end))
  14450. (goto-char pos)
  14451. (org-move-to-column col)))
  14452. (defun org-move-line-up (arg)
  14453. "Move the current line up. With prefix argument, move it past ARG lines."
  14454. (interactive "p")
  14455. (let ((col (current-column))
  14456. beg end pos)
  14457. (beginning-of-line 1) (setq beg (point))
  14458. (beginning-of-line 2) (setq end (point))
  14459. (beginning-of-line (- arg))
  14460. (setq pos (move-marker (make-marker) (point)))
  14461. (insert (delete-and-extract-region beg end))
  14462. (goto-char pos)
  14463. (org-move-to-column col)))
  14464. (defun org-replace-escapes (string table)
  14465. "Replace %-escapes in STRING with values in TABLE.
  14466. TABLE is an association list with keys like \"%a\" and string values.
  14467. The sequences in STRING may contain normal field width and padding information,
  14468. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14469. so values can contain further %-escapes if they are define later in TABLE."
  14470. (let ((case-fold-search nil)
  14471. e re rpl)
  14472. (while (setq e (pop table))
  14473. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14474. (while (string-match re string)
  14475. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14476. (cdr e)))
  14477. (setq string (replace-match rpl t t string))))
  14478. string))
  14479. (defun org-sublist (list start end)
  14480. "Return a section of LIST, from START to END.
  14481. Counting starts at 1."
  14482. (let (rtn (c start))
  14483. (setq list (nthcdr (1- start) list))
  14484. (while (and list (<= c end))
  14485. (push (pop list) rtn)
  14486. (setq c (1+ c)))
  14487. (nreverse rtn)))
  14488. (defun org-find-base-buffer-visiting (file)
  14489. "Like `find-buffer-visiting' but always return the base buffer and
  14490. not an indirect buffer."
  14491. (let ((buf (or (get-file-buffer file)
  14492. (find-buffer-visiting file))))
  14493. (if buf
  14494. (or (buffer-base-buffer buf) buf)
  14495. nil)))
  14496. (defun org-image-file-name-regexp (&optional extensions)
  14497. "Return regexp matching the file names of images.
  14498. If EXTENSIONS is given, only match these."
  14499. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14500. (image-file-name-regexp)
  14501. (let ((image-file-name-extensions
  14502. (or extensions
  14503. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14504. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14505. (concat "\\."
  14506. (regexp-opt (nconc (mapcar 'upcase
  14507. image-file-name-extensions)
  14508. image-file-name-extensions)
  14509. t)
  14510. "\\'"))))
  14511. (defun org-file-image-p (file &optional extensions)
  14512. "Return non-nil if FILE is an image."
  14513. (save-match-data
  14514. (string-match (org-image-file-name-regexp extensions) file)))
  14515. (defun org-get-cursor-date ()
  14516. "Return the date at cursor in as a time.
  14517. This works in the calendar and in the agenda, anywhere else it just
  14518. returns the current time."
  14519. (let (date day defd)
  14520. (cond
  14521. ((eq major-mode 'calendar-mode)
  14522. (setq date (calendar-cursor-to-date)
  14523. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14524. ((eq major-mode 'org-agenda-mode)
  14525. (setq day (get-text-property (point) 'day))
  14526. (if day
  14527. (setq date (calendar-gregorian-from-absolute day)
  14528. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14529. (nth 2 date))))))
  14530. (or defd (current-time))))
  14531. (defvar org-agenda-action-marker (make-marker)
  14532. "Marker pointing to the entry for the next agenda action.")
  14533. (defun org-mark-entry-for-agenda-action ()
  14534. "Mark the current entry as target of an agenda action.
  14535. Agenda actions are actions executed from the agenda with the key `k',
  14536. which make use of the date at the cursor."
  14537. (interactive)
  14538. (move-marker org-agenda-action-marker
  14539. (save-excursion (org-back-to-heading t) (point))
  14540. (current-buffer))
  14541. (message
  14542. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14543. ;;; Paragraph filling stuff.
  14544. ;; We want this to be just right, so use the full arsenal.
  14545. (defun org-indent-line-function ()
  14546. "Indent line like previous, but further if previous was headline or item."
  14547. (interactive)
  14548. (let* ((pos (point))
  14549. (itemp (org-at-item-p))
  14550. (case-fold-search t)
  14551. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14552. column bpos bcol tpos tcol bullet btype bullet-type)
  14553. ;; Find the previous relevant line
  14554. (beginning-of-line 1)
  14555. (cond
  14556. ((looking-at "#") (setq column 0))
  14557. ((looking-at "\\*+ ") (setq column 0))
  14558. ((and (looking-at "[ \t]*:END:")
  14559. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14560. (save-excursion
  14561. (goto-char (1- (match-beginning 1)))
  14562. (setq column (current-column))))
  14563. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  14564. (save-excursion
  14565. (re-search-backward
  14566. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  14567. (setq column (org-get-indentation (match-string 0))))
  14568. (t
  14569. (beginning-of-line 0)
  14570. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14571. (not (looking-at "[ \t]*:END:"))
  14572. (not (looking-at org-drawer-regexp)))
  14573. (beginning-of-line 0))
  14574. (cond
  14575. ((looking-at "\\*+[ \t]+")
  14576. (if (not org-adapt-indentation)
  14577. (setq column 0)
  14578. (goto-char (match-end 0))
  14579. (setq column (current-column))))
  14580. ((looking-at org-drawer-regexp)
  14581. (goto-char (1- (match-beginning 1)))
  14582. (setq column (current-column)))
  14583. ((looking-at "\\([ \t]*\\):END:")
  14584. (goto-char (match-end 1))
  14585. (setq column (current-column)))
  14586. ((org-in-item-p)
  14587. (org-beginning-of-item)
  14588. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14589. (setq bpos (match-beginning 1) tpos (match-end 0)
  14590. bcol (progn (goto-char bpos) (current-column))
  14591. tcol (progn (goto-char tpos) (current-column))
  14592. bullet (match-string 1)
  14593. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14594. (if (> tcol (+ bcol org-description-max-indent))
  14595. (setq tcol (+ bcol 5)))
  14596. (if (not itemp)
  14597. (setq column tcol)
  14598. (goto-char pos)
  14599. (beginning-of-line 1)
  14600. (if (looking-at "\\S-")
  14601. (progn
  14602. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14603. (setq bullet (match-string 1)
  14604. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14605. (setq column (if (equal btype bullet-type) bcol tcol)))
  14606. (setq column (org-get-indentation)))))
  14607. (t (setq column (org-get-indentation))))))
  14608. (goto-char pos)
  14609. (if (<= (current-column) (current-indentation))
  14610. (org-indent-line-to column)
  14611. (save-excursion (org-indent-line-to column)))
  14612. (setq column (current-column))
  14613. (beginning-of-line 1)
  14614. (if (looking-at
  14615. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14616. (replace-match (concat "\\1" (format org-property-format
  14617. (match-string 2) (match-string 3)))
  14618. t nil))
  14619. (org-move-to-column column)))
  14620. (defun org-set-autofill-regexps ()
  14621. (interactive)
  14622. ;; In the paragraph separator we include headlines, because filling
  14623. ;; text in a line directly attached to a headline would otherwise
  14624. ;; fill the headline as well.
  14625. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14626. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14627. ;; The paragraph starter includes hand-formatted lists.
  14628. (org-set-local
  14629. 'paragraph-start
  14630. (concat
  14631. "\f" "\\|"
  14632. "[ ]*$" "\\|"
  14633. "\\*+ " "\\|"
  14634. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  14635. "[ \t]*[:|]" "\\|"
  14636. "\\$\\$" "\\|"
  14637. "\\\\\\(begin\\|end\\|[][]\\)"))
  14638. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14639. ;; But only if the user has not turned off tables or fixed-width regions
  14640. (org-set-local
  14641. 'auto-fill-inhibit-regexp
  14642. (concat "\\*+ \\|#\\+"
  14643. "\\|[ \t]*" org-keyword-time-regexp
  14644. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14645. (concat
  14646. "\\|[ \t]*["
  14647. (if org-enable-table-editor "|" "")
  14648. (if org-enable-fixed-width-editor ":" "")
  14649. "]"))))
  14650. ;; We use our own fill-paragraph function, to make sure that tables
  14651. ;; and fixed-width regions are not wrapped. That function will pass
  14652. ;; through to `fill-paragraph' when appropriate.
  14653. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14654. ; Adaptive filling: To get full control, first make sure that
  14655. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14656. (org-set-local 'adaptive-fill-regexp "\000")
  14657. (org-set-local 'adaptive-fill-function
  14658. 'org-adaptive-fill-function)
  14659. (org-set-local
  14660. 'align-mode-rules-list
  14661. '((org-in-buffer-settings
  14662. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14663. (modes . '(org-mode))))))
  14664. (defun org-fill-paragraph (&optional justify)
  14665. "Re-align a table, pass through to fill-paragraph if no table."
  14666. (let ((table-p (org-at-table-p))
  14667. (table.el-p (org-at-table.el-p)))
  14668. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14669. (save-excursion (goto-char (point-at-bol))
  14670. (looking-at outline-regexp)))
  14671. t) ; skip headlines
  14672. (table.el-p t) ; skip table.el tables
  14673. (table-p (org-table-align) t) ; align org-mode tables
  14674. (t nil)))) ; call paragraph-fill
  14675. ;; For reference, this is the default value of adaptive-fill-regexp
  14676. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14677. (defun org-adaptive-fill-function ()
  14678. "Return a fill prefix for org-mode files.
  14679. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14680. work correctly."
  14681. (cond ((looking-at "#[ \t]+")
  14682. (match-string 0))
  14683. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14684. (save-excursion
  14685. (if (> (match-end 1) (+ (match-beginning 1)
  14686. org-description-max-indent))
  14687. (goto-char (+ (match-beginning 1) 5))
  14688. (goto-char (match-end 0)))
  14689. (make-string (current-column) ?\ )))
  14690. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14691. (save-excursion
  14692. (goto-char (match-end 0))
  14693. (make-string (current-column) ?\ )))
  14694. (t nil)))
  14695. ;;; Other stuff.
  14696. (defun org-toggle-fixed-width-section (arg)
  14697. "Toggle the fixed-width export.
  14698. If there is no active region, the QUOTE keyword at the current headline is
  14699. inserted or removed. When present, it causes the text between this headline
  14700. and the next to be exported as fixed-width text, and unmodified.
  14701. If there is an active region, this command adds or removes a colon as the
  14702. first character of this line. If the first character of a line is a colon,
  14703. this line is also exported in fixed-width font."
  14704. (interactive "P")
  14705. (let* ((cc 0)
  14706. (regionp (org-region-active-p))
  14707. (beg (if regionp (region-beginning) (point)))
  14708. (end (if regionp (region-end)))
  14709. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14710. (case-fold-search nil)
  14711. (re "[ \t]*\\(: \\)")
  14712. off)
  14713. (if regionp
  14714. (save-excursion
  14715. (goto-char beg)
  14716. (setq cc (current-column))
  14717. (beginning-of-line 1)
  14718. (setq off (looking-at re))
  14719. (while (> nlines 0)
  14720. (setq nlines (1- nlines))
  14721. (beginning-of-line 1)
  14722. (cond
  14723. (arg
  14724. (org-move-to-column cc t)
  14725. (insert ": \n")
  14726. (forward-line -1))
  14727. ((and off (looking-at re))
  14728. (replace-match "" t t nil 1))
  14729. ((not off) (org-move-to-column cc t) (insert ": ")))
  14730. (forward-line 1)))
  14731. (save-excursion
  14732. (org-back-to-heading)
  14733. (if (looking-at (concat outline-regexp
  14734. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14735. (replace-match "" t t nil 1)
  14736. (if (looking-at outline-regexp)
  14737. (progn
  14738. (goto-char (match-end 0))
  14739. (insert org-quote-string " "))))))))
  14740. (defun org-reftex-citation ()
  14741. "Use reftex-citation to insert a citation into the buffer.
  14742. This looks for a line like
  14743. #+BIBLIOGRAPHY: foo plain option:-d
  14744. and derives from it that foo.bib is the bbliography file relevant
  14745. for this document. It then installs the necessary environment for RefTeX
  14746. to work in this buffer and calls `reftex-citation' to insert a citation
  14747. into the buffer.
  14748. Export of such citations to both LaTeX and HTML is handled by the contributed
  14749. package org-exp-bibtex by Taru Karttunen."
  14750. (interactive)
  14751. (let ((reftex-docstruct-symbol 'rds)
  14752. (reftex-cite-format "\\cite{%l}")
  14753. rds bib)
  14754. (save-excursion
  14755. (save-restriction
  14756. (widen)
  14757. (let ((case-fold-search t)
  14758. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14759. (if (not (save-excursion
  14760. (or (re-search-forward re nil t)
  14761. (re-search-backward re nil t))))
  14762. (error "No bibliography defined in file")
  14763. (setq bib (concat (match-string 1) ".bib")
  14764. rds (list (list 'bib bib)))))))
  14765. (call-interactively 'reftex-citation)))
  14766. ;;;; Functions extending outline functionality
  14767. (defun org-beginning-of-line (&optional arg)
  14768. "Go to the beginning of the current line. If that is invisible, continue
  14769. to a visible line beginning. This makes the function of C-a more intuitive.
  14770. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14771. first attempt, and only move to after the tags when the cursor is already
  14772. beyond the end of the headline."
  14773. (interactive "P")
  14774. (let ((pos (point))
  14775. (special (if (consp org-special-ctrl-a/e)
  14776. (car org-special-ctrl-a/e)
  14777. org-special-ctrl-a/e))
  14778. refpos)
  14779. (if (org-bound-and-true-p line-move-visual)
  14780. (beginning-of-visual-line 1)
  14781. (beginning-of-line 1))
  14782. (if (and arg (fboundp 'move-beginning-of-line))
  14783. (call-interactively 'move-beginning-of-line)
  14784. (if (bobp)
  14785. nil
  14786. (backward-char 1)
  14787. (if (org-invisible-p)
  14788. (while (and (not (bobp)) (org-invisible-p))
  14789. (backward-char 1)
  14790. (beginning-of-line 1))
  14791. (forward-char 1))))
  14792. (when special
  14793. (cond
  14794. ((and (looking-at org-complex-heading-regexp)
  14795. (= (char-after (match-end 1)) ?\ ))
  14796. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14797. (point-at-eol)))
  14798. (goto-char
  14799. (if (eq special t)
  14800. (cond ((> pos refpos) refpos)
  14801. ((= pos (point)) refpos)
  14802. (t (point)))
  14803. (cond ((> pos (point)) (point))
  14804. ((not (eq last-command this-command)) (point))
  14805. (t refpos)))))
  14806. ((org-at-item-p)
  14807. (goto-char
  14808. (if (eq special t)
  14809. (cond ((> pos (match-end 4)) (match-end 4))
  14810. ((= pos (point)) (match-end 4))
  14811. (t (point)))
  14812. (cond ((> pos (point)) (point))
  14813. ((not (eq last-command this-command)) (point))
  14814. (t (match-end 4))))))))
  14815. (org-no-warnings
  14816. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14817. (defun org-end-of-line (&optional arg)
  14818. "Go to the end of the line.
  14819. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14820. first attempt, and only move to after the tags when the cursor is already
  14821. beyond the end of the headline."
  14822. (interactive "P")
  14823. (let ((special (if (consp org-special-ctrl-a/e)
  14824. (cdr org-special-ctrl-a/e)
  14825. org-special-ctrl-a/e)))
  14826. (if (or (not special)
  14827. (not (org-on-heading-p))
  14828. arg)
  14829. (call-interactively
  14830. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  14831. ((fboundp 'move-end-of-line) 'move-end-of-line)
  14832. (t 'end-of-line)))
  14833. (let ((pos (point)))
  14834. (beginning-of-line 1)
  14835. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14836. (if (eq special t)
  14837. (if (or (< pos (match-beginning 1))
  14838. (= pos (match-end 0)))
  14839. (goto-char (match-beginning 1))
  14840. (goto-char (match-end 0)))
  14841. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14842. (goto-char (match-end 0))
  14843. (goto-char (match-beginning 1))))
  14844. (call-interactively (if (fboundp 'move-end-of-line)
  14845. 'move-end-of-line
  14846. 'end-of-line)))))
  14847. (org-no-warnings
  14848. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14849. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14850. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14851. (defun org-backward-sentence (&optional arg)
  14852. "Go to beginning of sentence, or beginning of table field.
  14853. This will call `backward-sentence' or `org-table-beginning-of-field',
  14854. depending on context."
  14855. (interactive "P")
  14856. (cond
  14857. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14858. (t (call-interactively 'backward-sentence))))
  14859. (defun org-forward-sentence (&optional arg)
  14860. "Go to end of sentence, or end of table field.
  14861. This will call `forward-sentence' or `org-table-end-of-field',
  14862. depending on context."
  14863. (interactive "P")
  14864. (cond
  14865. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14866. (t (call-interactively 'forward-sentence))))
  14867. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14868. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14869. (defun org-kill-line (&optional arg)
  14870. "Kill line, to tags or end of line."
  14871. (interactive "P")
  14872. (cond
  14873. ((or (not org-special-ctrl-k)
  14874. (bolp)
  14875. (not (org-on-heading-p)))
  14876. (call-interactively 'kill-line))
  14877. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14878. (kill-region (point) (match-beginning 1))
  14879. (org-set-tags nil t))
  14880. (t (kill-region (point) (point-at-eol)))))
  14881. (define-key org-mode-map "\C-k" 'org-kill-line)
  14882. (defun org-yank (&optional arg)
  14883. "Yank. If the kill is a subtree, treat it specially.
  14884. This command will look at the current kill and check if is a single
  14885. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14886. cursor is at the beginning of a line or after the stars of a currently
  14887. empty headline, then the yank is handled specially. How exactly depends
  14888. on the value of the following variables, both set by default.
  14889. org-yank-folded-subtrees
  14890. When set, the subtree(s) will be folded after insertion, but only
  14891. if doing so would now swallow text after the yanked text.
  14892. org-yank-adjusted-subtrees
  14893. When set, the subtree will be promoted or demoted in order to
  14894. fit into the local outline tree structure, which means that the level
  14895. will be adjusted so that it becomes the smaller one of the two
  14896. *visible* surrounding headings.
  14897. Any prefix to this command will cause `yank' to be called directly with
  14898. no special treatment. In particular, a simple `C-u' prefix will just
  14899. plainly yank the text as it is.
  14900. \[1] The test checks if the first non-white line is a heading
  14901. and if there are no other headings with fewer stars."
  14902. (interactive "P")
  14903. (org-yank-generic 'yank arg))
  14904. (defun org-yank-generic (command arg)
  14905. "Perform some yank-like command.
  14906. This function implements the behavior described in the `org-yank'
  14907. documentation. However, it has been generalized to work for any
  14908. interactive command with similar behavior."
  14909. ;; pretend to be command COMMAND
  14910. (setq this-command command)
  14911. (if arg
  14912. (call-interactively command)
  14913. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14914. (and (org-kill-is-subtree-p)
  14915. (or (bolp)
  14916. (and (looking-at "[ \t]*$")
  14917. (string-match
  14918. "\\`\\*+\\'"
  14919. (buffer-substring (point-at-bol) (point)))))))
  14920. swallowp)
  14921. (cond
  14922. ((and subtreep org-yank-folded-subtrees)
  14923. (let ((beg (point))
  14924. end)
  14925. (if (and subtreep org-yank-adjusted-subtrees)
  14926. (org-paste-subtree nil nil 'for-yank)
  14927. (call-interactively command))
  14928. (setq end (point))
  14929. (goto-char beg)
  14930. (when (and (bolp) subtreep
  14931. (not (setq swallowp
  14932. (org-yank-folding-would-swallow-text beg end))))
  14933. (or (looking-at outline-regexp)
  14934. (re-search-forward (concat "^" outline-regexp) end t))
  14935. (while (and (< (point) end) (looking-at outline-regexp))
  14936. (hide-subtree)
  14937. (org-cycle-show-empty-lines 'folded)
  14938. (condition-case nil
  14939. (outline-forward-same-level 1)
  14940. (error (goto-char end)))))
  14941. (when swallowp
  14942. (message
  14943. "Inserted text not folded because that would swallow text"))
  14944. (goto-char end)
  14945. (skip-chars-forward " \t\n\r")
  14946. (beginning-of-line 1)
  14947. (push-mark beg 'nomsg)))
  14948. ((and subtreep org-yank-adjusted-subtrees)
  14949. (let ((beg (point-at-bol)))
  14950. (org-paste-subtree nil nil 'for-yank)
  14951. (push-mark beg 'nomsg)))
  14952. (t
  14953. (call-interactively command))))))
  14954. (defun org-yank-folding-would-swallow-text (beg end)
  14955. "Would hide-subtree at BEG swallow any text after END?"
  14956. (let (level)
  14957. (save-excursion
  14958. (goto-char beg)
  14959. (when (or (looking-at outline-regexp)
  14960. (re-search-forward (concat "^" outline-regexp) end t))
  14961. (setq level (org-outline-level)))
  14962. (goto-char end)
  14963. (skip-chars-forward " \t\r\n\v\f")
  14964. (if (or (eobp)
  14965. (and (bolp) (looking-at org-outline-regexp)
  14966. (<= (org-outline-level) level)))
  14967. nil ; Nothing would be swallowed
  14968. t)))) ; something would swallow
  14969. (define-key org-mode-map "\C-y" 'org-yank)
  14970. (defun org-invisible-p ()
  14971. "Check if point is at a character currently not visible."
  14972. ;; Early versions of noutline don't have `outline-invisible-p'.
  14973. (if (fboundp 'outline-invisible-p)
  14974. (outline-invisible-p)
  14975. (get-char-property (point) 'invisible)))
  14976. (defun org-invisible-p2 ()
  14977. "Check if point is at a character currently not visible."
  14978. (save-excursion
  14979. (if (and (eolp) (not (bobp))) (backward-char 1))
  14980. ;; Early versions of noutline don't have `outline-invisible-p'.
  14981. (if (fboundp 'outline-invisible-p)
  14982. (outline-invisible-p)
  14983. (get-char-property (point) 'invisible))))
  14984. (defun org-back-to-heading (&optional invisible-ok)
  14985. "Call `outline-back-to-heading', but provide a better error message."
  14986. (condition-case nil
  14987. (outline-back-to-heading invisible-ok)
  14988. (error (error "Before first headline at position %d in buffer %s"
  14989. (point) (current-buffer)))))
  14990. (defun org-before-first-heading-p ()
  14991. "Before first heading?"
  14992. (save-excursion
  14993. (null (re-search-backward "^\\*+ " nil t))))
  14994. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14995. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14996. (defun org-at-heading-or-item-p ()
  14997. (or (org-on-heading-p) (org-at-item-p)))
  14998. (defun org-on-target-p ()
  14999. (or (org-in-regexp org-radio-target-regexp)
  15000. (org-in-regexp org-target-regexp)))
  15001. (defun org-up-heading-all (arg)
  15002. "Move to the heading line of which the present line is a subheading.
  15003. This function considers both visible and invisible heading lines.
  15004. With argument, move up ARG levels."
  15005. (if (fboundp 'outline-up-heading-all)
  15006. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15007. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15008. (defun org-up-heading-safe ()
  15009. "Move to the heading line of which the present line is a subheading.
  15010. This version will not throw an error. It will return the level of the
  15011. headline found, or nil if no higher level is found.
  15012. Also, this function will be a lot faster than `outline-up-heading',
  15013. because it relies on stars being the outline starters. This can really
  15014. make a significant difference in outlines with very many siblings."
  15015. (let (start-level re)
  15016. (org-back-to-heading t)
  15017. (setq start-level (funcall outline-level))
  15018. (if (equal start-level 1)
  15019. nil
  15020. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15021. (if (re-search-backward re nil t)
  15022. (funcall outline-level)))))
  15023. (defun org-first-sibling-p ()
  15024. "Is this heading the first child of its parents?"
  15025. (interactive)
  15026. (let ((re (concat "^" outline-regexp))
  15027. level l)
  15028. (unless (org-at-heading-p t)
  15029. (error "Not at a heading"))
  15030. (setq level (funcall outline-level))
  15031. (save-excursion
  15032. (if (not (re-search-backward re nil t))
  15033. t
  15034. (setq l (funcall outline-level))
  15035. (< l level)))))
  15036. (defun org-goto-sibling (&optional previous)
  15037. "Goto the next sibling, even if it is invisible.
  15038. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15039. when a sibling was found. When none is found, return nil and don't
  15040. move point."
  15041. (let ((fun (if previous 're-search-backward 're-search-forward))
  15042. (pos (point))
  15043. (re (concat "^" outline-regexp))
  15044. level l)
  15045. (when (condition-case nil (org-back-to-heading t) (error nil))
  15046. (setq level (funcall outline-level))
  15047. (catch 'exit
  15048. (or previous (forward-char 1))
  15049. (while (funcall fun re nil t)
  15050. (setq l (funcall outline-level))
  15051. (when (< l level) (goto-char pos) (throw 'exit nil))
  15052. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15053. (goto-char pos)
  15054. nil))))
  15055. (defun org-show-siblings ()
  15056. "Show all siblings of the current headline."
  15057. (save-excursion
  15058. (while (org-goto-sibling) (org-flag-heading nil)))
  15059. (save-excursion
  15060. (while (org-goto-sibling 'previous)
  15061. (org-flag-heading nil))))
  15062. (defun org-show-hidden-entry ()
  15063. "Show an entry where even the heading is hidden."
  15064. (save-excursion
  15065. (org-show-entry)))
  15066. (defun org-flag-heading (flag &optional entry)
  15067. "Flag the current heading. FLAG non-nil means make invisible.
  15068. When ENTRY is non-nil, show the entire entry."
  15069. (save-excursion
  15070. (org-back-to-heading t)
  15071. ;; Check if we should show the entire entry
  15072. (if entry
  15073. (progn
  15074. (org-show-entry)
  15075. (save-excursion
  15076. (and (outline-next-heading)
  15077. (org-flag-heading nil))))
  15078. (outline-flag-region (max (point-min) (1- (point)))
  15079. (save-excursion (outline-end-of-heading) (point))
  15080. flag))))
  15081. (defun org-get-next-sibling ()
  15082. "Move to next heading of the same level, and return point.
  15083. If there is no such heading, return nil.
  15084. This is like outline-next-sibling, but invisible headings are ok."
  15085. (let ((level (funcall outline-level)))
  15086. (outline-next-heading)
  15087. (while (and (not (eobp)) (> (funcall outline-level) level))
  15088. (outline-next-heading))
  15089. (if (or (eobp) (< (funcall outline-level) level))
  15090. nil
  15091. (point))))
  15092. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15093. ;; This contains an exact copy of the original function, but it uses
  15094. ;; `org-back-to-heading', to make it work also in invisible
  15095. ;; trees. And is uses an invisible-OK argument.
  15096. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15097. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15098. ;; with many children and grandchildren etc, this can be much faster
  15099. ;; than the outline version.
  15100. (org-back-to-heading invisible-OK)
  15101. (let ((first t)
  15102. (level (funcall outline-level)))
  15103. (if (and (org-mode-p) (< level 1000))
  15104. ;; A true heading (not a plain list item), in Org-mode
  15105. ;; This means we can easily find the end by looking
  15106. ;; only for the right number of stars. Using a regexp to do
  15107. ;; this is so much faster than using a Lisp loop.
  15108. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15109. (forward-char 1)
  15110. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15111. ;; something else, do it the slow way
  15112. (while (and (not (eobp))
  15113. (or first (> (funcall outline-level) level)))
  15114. (setq first nil)
  15115. (outline-next-heading)))
  15116. (unless to-heading
  15117. (if (memq (preceding-char) '(?\n ?\^M))
  15118. (progn
  15119. ;; Go to end of line before heading
  15120. (forward-char -1)
  15121. (if (memq (preceding-char) '(?\n ?\^M))
  15122. ;; leave blank line before heading
  15123. (forward-char -1))))))
  15124. (point))
  15125. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  15126. "Use Org version in org-mode, for dramatic speed-up."
  15127. (if (eq major-mode 'org-mode)
  15128. (org-end-of-subtree)
  15129. ad-do-it))
  15130. (defun org-forward-same-level (arg &optional invisible-ok)
  15131. "Move forward to the arg'th subheading at same level as this one.
  15132. Stop at the first and last subheadings of a superior heading."
  15133. (interactive "p")
  15134. (org-back-to-heading)
  15135. (org-on-heading-p)
  15136. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15137. (re (format "^\\*\\{1,%d\\} " level))
  15138. l)
  15139. (forward-char 1)
  15140. (while (> arg 0)
  15141. (while (and (re-search-forward re nil 'move)
  15142. (setq l (- (match-end 0) (match-beginning 0) 1))
  15143. (= l level)
  15144. (not invisible-ok)
  15145. (org-invisible-p))
  15146. (if (< l level) (setq arg 1)))
  15147. (setq arg (1- arg)))
  15148. (beginning-of-line 1)))
  15149. (defun org-backward-same-level (arg &optional invisible-ok)
  15150. "Move backward to the arg'th subheading at same level as this one.
  15151. Stop at the first and last subheadings of a superior heading."
  15152. (interactive "p")
  15153. (org-back-to-heading)
  15154. (org-on-heading-p)
  15155. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15156. (re (format "^\\*\\{1,%d\\} " level))
  15157. l)
  15158. (while (> arg 0)
  15159. (while (and (re-search-backward re nil 'move)
  15160. (setq l (- (match-end 0) (match-beginning 0) 1))
  15161. (= l level)
  15162. (not invisible-ok)
  15163. (org-invisible-p))
  15164. (if (< l level) (setq arg 1)))
  15165. (setq arg (1- arg)))))
  15166. (defun org-show-subtree ()
  15167. "Show everything after this heading at deeper levels."
  15168. (outline-flag-region
  15169. (point)
  15170. (save-excursion
  15171. (outline-end-of-subtree) (outline-next-heading) (point))
  15172. nil))
  15173. (defun org-show-entry ()
  15174. "Show the body directly following this heading.
  15175. Show the heading too, if it is currently invisible."
  15176. (interactive)
  15177. (save-excursion
  15178. (condition-case nil
  15179. (progn
  15180. (org-back-to-heading t)
  15181. (outline-flag-region
  15182. (max (point-min) (1- (point)))
  15183. (save-excursion
  15184. (if (re-search-forward
  15185. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15186. (match-beginning 1)
  15187. (point-max)))
  15188. nil)
  15189. (org-cycle-hide-drawers 'children))
  15190. (error nil))))
  15191. (defun org-make-options-regexp (kwds &optional extra)
  15192. "Make a regular expression for keyword lines."
  15193. (concat
  15194. "^"
  15195. "#?[ \t]*\\+\\("
  15196. (mapconcat 'regexp-quote kwds "\\|")
  15197. (if extra (concat "\\|" extra))
  15198. "\\):[ \t]*"
  15199. "\\(.*\\)"))
  15200. ;; Make isearch reveal the necessary context
  15201. (defun org-isearch-end ()
  15202. "Reveal context after isearch exits."
  15203. (when isearch-success ; only if search was successful
  15204. (if (featurep 'xemacs)
  15205. ;; Under XEmacs, the hook is run in the correct place,
  15206. ;; we directly show the context.
  15207. (org-show-context 'isearch)
  15208. ;; In Emacs the hook runs *before* restoring the overlays.
  15209. ;; So we have to use a one-time post-command-hook to do this.
  15210. ;; (Emacs 22 has a special variable, see function `org-mode')
  15211. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15212. isearch-mode-end-hook-quit)
  15213. ;; Only when the isearch was not quitted.
  15214. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15215. 'append 'local)))))
  15216. (defun org-isearch-post-command ()
  15217. "Remove self from hook, and show context."
  15218. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15219. (org-show-context 'isearch))
  15220. ;;;; Integration with and fixes for other packages
  15221. ;;; Imenu support
  15222. (defvar org-imenu-markers nil
  15223. "All markers currently used by Imenu.")
  15224. (make-variable-buffer-local 'org-imenu-markers)
  15225. (defun org-imenu-new-marker (&optional pos)
  15226. "Return a new marker for use by Imenu, and remember the marker."
  15227. (let ((m (make-marker)))
  15228. (move-marker m (or pos (point)))
  15229. (push m org-imenu-markers)
  15230. m))
  15231. (defun org-imenu-get-tree ()
  15232. "Produce the index for Imenu."
  15233. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15234. (setq org-imenu-markers nil)
  15235. (let* ((n org-imenu-depth)
  15236. (re (concat "^" outline-regexp))
  15237. (subs (make-vector (1+ n) nil))
  15238. (last-level 0)
  15239. m level head)
  15240. (save-excursion
  15241. (save-restriction
  15242. (widen)
  15243. (goto-char (point-max))
  15244. (while (re-search-backward re nil t)
  15245. (setq level (org-reduced-level (funcall outline-level)))
  15246. (when (<= level n)
  15247. (looking-at org-complex-heading-regexp)
  15248. (setq head (org-link-display-format
  15249. (org-match-string-no-properties 4))
  15250. m (org-imenu-new-marker))
  15251. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15252. (if (>= level last-level)
  15253. (push (cons head m) (aref subs level))
  15254. (push (cons head (aref subs (1+ level))) (aref subs level))
  15255. (loop for i from (1+ level) to n do (aset subs i nil)))
  15256. (setq last-level level)))))
  15257. (aref subs 1)))
  15258. (eval-after-load "imenu"
  15259. '(progn
  15260. (add-hook 'imenu-after-jump-hook
  15261. (lambda ()
  15262. (if (eq major-mode 'org-mode)
  15263. (org-show-context 'org-goto))))))
  15264. (defun org-link-display-format (link)
  15265. "Replace a link with either the description, or the link target
  15266. if no description is present"
  15267. (save-match-data
  15268. (if (string-match org-bracket-link-analytic-regexp link)
  15269. (replace-match (or (match-string 5 link)
  15270. (concat (match-string 1 link)
  15271. (match-string 3 link)))
  15272. nil nil link)
  15273. link)))
  15274. ;; Speedbar support
  15275. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15276. "Overlay marking the agenda restriction line in speedbar.")
  15277. (org-overlay-put org-speedbar-restriction-lock-overlay
  15278. 'face 'org-agenda-restriction-lock)
  15279. (org-overlay-put org-speedbar-restriction-lock-overlay
  15280. 'help-echo "Agendas are currently limited to this item.")
  15281. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15282. (defun org-speedbar-set-agenda-restriction ()
  15283. "Restrict future agenda commands to the location at point in speedbar.
  15284. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15285. (interactive)
  15286. (require 'org-agenda)
  15287. (let (p m tp np dir txt)
  15288. (cond
  15289. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15290. 'org-imenu t))
  15291. (setq m (get-text-property p 'org-imenu-marker))
  15292. (save-excursion
  15293. (save-restriction
  15294. (set-buffer (marker-buffer m))
  15295. (goto-char m)
  15296. (org-agenda-set-restriction-lock 'subtree))))
  15297. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15298. 'speedbar-function 'speedbar-find-file))
  15299. (setq tp (previous-single-property-change
  15300. (1+ p) 'speedbar-function)
  15301. np (next-single-property-change
  15302. tp 'speedbar-function)
  15303. dir (speedbar-line-directory)
  15304. txt (buffer-substring-no-properties (or tp (point-min))
  15305. (or np (point-max))))
  15306. (save-excursion
  15307. (save-restriction
  15308. (set-buffer (find-file-noselect
  15309. (let ((default-directory dir))
  15310. (expand-file-name txt))))
  15311. (unless (org-mode-p)
  15312. (error "Cannot restrict to non-Org-mode file"))
  15313. (org-agenda-set-restriction-lock 'file))))
  15314. (t (error "Don't know how to restrict Org-mode's agenda")))
  15315. (org-move-overlay org-speedbar-restriction-lock-overlay
  15316. (point-at-bol) (point-at-eol))
  15317. (setq current-prefix-arg nil)
  15318. (org-agenda-maybe-redo)))
  15319. (eval-after-load "speedbar"
  15320. '(progn
  15321. (speedbar-add-supported-extension ".org")
  15322. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15323. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15324. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15325. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15326. (add-hook 'speedbar-visiting-tag-hook
  15327. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15328. ;;; Fixes and Hacks for problems with other packages
  15329. ;; Make flyspell not check words in links, to not mess up our keymap
  15330. (defun org-mode-flyspell-verify ()
  15331. "Don't let flyspell put overlays at active buttons."
  15332. (and (not (get-text-property (point) 'keymap))
  15333. (not (get-text-property (point) 'org-no-flyspell))))
  15334. (defun org-remove-flyspell-overlays-in (beg end)
  15335. "Remove flyspell overlays in region."
  15336. (and (org-bound-and-true-p flyspell-mode)
  15337. (fboundp 'flyspell-delete-region-overlays)
  15338. (flyspell-delete-region-overlays beg end))
  15339. (add-text-properties beg end '(org-no-flyspell t)))
  15340. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15341. (eval-after-load "bookmark"
  15342. '(if (boundp 'bookmark-after-jump-hook)
  15343. ;; We can use the hook
  15344. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15345. ;; Hook not available, use advice
  15346. (defadvice bookmark-jump (after org-make-visible activate)
  15347. "Make the position visible."
  15348. (org-bookmark-jump-unhide))))
  15349. ;; Make sure saveplace show the location if it was hidden
  15350. (eval-after-load "saveplace"
  15351. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15352. "Make the position visible."
  15353. (org-bookmark-jump-unhide)))
  15354. (defun org-bookmark-jump-unhide ()
  15355. "Unhide the current position, to show the bookmark location."
  15356. (and (org-mode-p)
  15357. (or (org-invisible-p)
  15358. (save-excursion (goto-char (max (point-min) (1- (point))))
  15359. (org-invisible-p)))
  15360. (org-show-context 'bookmark-jump)))
  15361. ;; Make session.el ignore our circular variable
  15362. (eval-after-load "session"
  15363. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15364. ;;;; Experimental code
  15365. (defun org-closed-in-range ()
  15366. "Sparse tree of items closed in a certain time range.
  15367. Still experimental, may disappear in the future."
  15368. (interactive)
  15369. ;; Get the time interval from the user.
  15370. (let* ((time1 (time-to-seconds
  15371. (org-read-date nil 'to-time nil "Starting date: ")))
  15372. (time2 (time-to-seconds
  15373. (org-read-date nil 'to-time nil "End date:")))
  15374. ;; callback function
  15375. (callback (lambda ()
  15376. (let ((time
  15377. (time-to-seconds
  15378. (apply 'encode-time
  15379. (org-parse-time-string
  15380. (match-string 1))))))
  15381. ;; check if time in interval
  15382. (and (>= time time1) (<= time time2))))))
  15383. ;; make tree, check each match with the callback
  15384. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15385. ;;;; Finish up
  15386. (provide 'org)
  15387. (run-hooks 'org-load-hook)
  15388. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15389. ;;; org.el ends here