org.el 574 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455
  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.22trans
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.22trans"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  150. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  151. (const :tag " mew Links to Mew folders/messages" org-mew)
  152. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  153. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  154. (const :tag " vm: Links to VM folders/messages" org-vm)
  155. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  156. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  157. (const :tag " mouse: Additional mouse support" org-mouse)
  158. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  159. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  160. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  161. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  162. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  163. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  164. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  165. (const :tag "C eval: Include command output as text" org-eval)
  166. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  167. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  168. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  169. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  170. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  171. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  172. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  173. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  174. (const :tag "C registry: A registry for Org links" org-registry)
  175. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  176. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  177. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  178. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  179. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  180. (defcustom org-support-shift-select nil
  181. "Non-nil means, make shift-cursor commands select text when possible.
  182. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  183. selecting a region, or enlarge thusly regions started in this way.
  184. In Org-mode, in special contexts, these same keys are used for other
  185. purposes, important enough to compete with shift selection. Org tries
  186. to balance these needs by supporting `shift-select-mode' outside these
  187. special contexts, under control of this variable.
  188. The default of this variable is nil, to avoid confusing behavior. Shifted
  189. cursor keys will then execute Org commands in the following contexts:
  190. - on a headline, changing TODO state (left/right) and priority (up/down)
  191. - on a time stamp, changing the time
  192. - in a plain list item, changing the bullet type
  193. - in a property definition line, switching between allowed values
  194. - in the BEGIN line of a clock table (changing the time block).
  195. Outside these contexts, the commands will throw an error.
  196. When this variable is t and the cursor is not in a special context,
  197. Org-mode will support shift-selection for making and enlarging regions.
  198. To make this more effective, the bullet cycling will no longer happen
  199. anywhere in an item line, but only if the cursor is exactly on the bullet.
  200. If you set this variable to the symbol `always', then the keys
  201. will not be special in headlines, property lines, and item lines, to make
  202. shift selection work there as well. If this is what you want, you can
  203. use the following alternative commands: `C-c C-t' and `C-c ,' to
  204. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  205. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  206. edited by hand or in column view.
  207. However, when the cursor is on a timestamp, shift-cursor commands
  208. will still edit the time stamp - this is just too good to give up.
  209. XEmacs user should have this variable set to nil, because shift-select-mode
  210. is Emacs 23 only."
  211. :group 'org
  212. :type '(choice
  213. (const :tag "Never" nil)
  214. (const :tag "When outside special context" t)
  215. (const :tag "Everywhere except timestamps" always)))
  216. (defgroup org-startup nil
  217. "Options concerning startup of Org-mode."
  218. :tag "Org Startup"
  219. :group 'org)
  220. (defcustom org-startup-folded t
  221. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  222. This can also be configured on a per-file basis by adding one of
  223. the following lines anywhere in the buffer:
  224. #+STARTUP: fold
  225. #+STARTUP: nofold
  226. #+STARTUP: content"
  227. :group 'org-startup
  228. :type '(choice
  229. (const :tag "nofold: show all" nil)
  230. (const :tag "fold: overview" t)
  231. (const :tag "content: all headlines" content)))
  232. (defcustom org-startup-truncated t
  233. "Non-nil means, entering Org-mode will set `truncate-lines'.
  234. This is useful since some lines containing links can be very long and
  235. uninteresting. Also tables look terrible when wrapped."
  236. :group 'org-startup
  237. :type 'boolean)
  238. (defcustom org-startup-align-all-tables nil
  239. "Non-nil means, align all tables when visiting a file.
  240. This is useful when the column width in tables is forced with <N> cookies
  241. in table fields. Such tables will look correct only after the first re-align.
  242. This can also be configured on a per-file basis by adding one of
  243. the following lines anywhere in the buffer:
  244. #+STARTUP: align
  245. #+STARTUP: noalign"
  246. :group 'org-startup
  247. :type 'boolean)
  248. (defcustom org-insert-mode-line-in-empty-file nil
  249. "Non-nil means insert the first line setting Org-mode in empty files.
  250. When the function `org-mode' is called interactively in an empty file, this
  251. normally means that the file name does not automatically trigger Org-mode.
  252. To ensure that the file will always be in Org-mode in the future, a
  253. line enforcing Org-mode will be inserted into the buffer, if this option
  254. has been set."
  255. :group 'org-startup
  256. :type 'boolean)
  257. (defcustom org-replace-disputed-keys nil
  258. "Non-nil means use alternative key bindings for some keys.
  259. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  260. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  261. If you want to use Org-mode together with one of these other modes,
  262. or more generally if you would like to move some Org-mode commands to
  263. other keys, set this variable and configure the keys with the variable
  264. `org-disputed-keys'.
  265. This option is only relevant at load-time of Org-mode, and must be set
  266. *before* org.el is loaded. Changing it requires a restart of Emacs to
  267. become effective."
  268. :group 'org-startup
  269. :type 'boolean)
  270. (defcustom org-use-extra-keys nil
  271. "Non-nil means use extra key sequence definitions for certain
  272. commands. This happens automatically if you run XEmacs or if
  273. window-system is nil. This variable lets you do the same
  274. manually. You must set it before loading org.
  275. Example: on Carbon Emacs 22 running graphically, with an external
  276. keyboard on a Powerbook, the default way of setting M-left might
  277. not work for either Alt or ESC. Setting this variable will make
  278. it work for ESC."
  279. :group 'org-startup
  280. :type 'boolean)
  281. (if (fboundp 'defvaralias)
  282. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  283. (defcustom org-disputed-keys
  284. '(([(shift up)] . [(meta p)])
  285. ([(shift down)] . [(meta n)])
  286. ([(shift left)] . [(meta -)])
  287. ([(shift right)] . [(meta +)])
  288. ([(control shift right)] . [(meta shift +)])
  289. ([(control shift left)] . [(meta shift -)]))
  290. "Keys for which Org-mode and other modes compete.
  291. This is an alist, cars are the default keys, second element specifies
  292. the alternative to use when `org-replace-disputed-keys' is t.
  293. Keys can be specified in any syntax supported by `define-key'.
  294. The value of this option takes effect only at Org-mode's startup,
  295. therefore you'll have to restart Emacs to apply it after changing."
  296. :group 'org-startup
  297. :type 'alist)
  298. (defun org-key (key)
  299. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  300. Or return the original if not disputed."
  301. (if org-replace-disputed-keys
  302. (let* ((nkey (key-description key))
  303. (x (org-find-if (lambda (x)
  304. (equal (key-description (car x)) nkey))
  305. org-disputed-keys)))
  306. (if x (cdr x) key))
  307. key))
  308. (defun org-find-if (predicate seq)
  309. (catch 'exit
  310. (while seq
  311. (if (funcall predicate (car seq))
  312. (throw 'exit (car seq))
  313. (pop seq)))))
  314. (defun org-defkey (keymap key def)
  315. "Define a key, possibly translated, as returned by `org-key'."
  316. (define-key keymap (org-key key) def))
  317. (defcustom org-ellipsis nil
  318. "The ellipsis to use in the Org-mode outline.
  319. When nil, just use the standard three dots. When a string, use that instead,
  320. When a face, use the standard 3 dots, but with the specified face.
  321. The change affects only Org-mode (which will then use its own display table).
  322. Changing this requires executing `M-x org-mode' in a buffer to become
  323. effective."
  324. :group 'org-startup
  325. :type '(choice (const :tag "Default" nil)
  326. (face :tag "Face" :value org-warning)
  327. (string :tag "String" :value "...#")))
  328. (defvar org-display-table nil
  329. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  330. (defgroup org-keywords nil
  331. "Keywords in Org-mode."
  332. :tag "Org Keywords"
  333. :group 'org)
  334. (defcustom org-deadline-string "DEADLINE:"
  335. "String to mark deadline entries.
  336. A deadline is this string, followed by a time stamp. Should be a word,
  337. terminated by a colon. You can insert a schedule keyword and
  338. a timestamp with \\[org-deadline].
  339. Changes become only effective after restarting Emacs."
  340. :group 'org-keywords
  341. :type 'string)
  342. (defcustom org-scheduled-string "SCHEDULED:"
  343. "String to mark scheduled TODO entries.
  344. A schedule is this string, followed by a time stamp. Should be a word,
  345. terminated by a colon. You can insert a schedule keyword and
  346. a timestamp with \\[org-schedule].
  347. Changes become only effective after restarting Emacs."
  348. :group 'org-keywords
  349. :type 'string)
  350. (defcustom org-closed-string "CLOSED:"
  351. "String used as the prefix for timestamps logging closing a TODO entry."
  352. :group 'org-keywords
  353. :type 'string)
  354. (defcustom org-clock-string "CLOCK:"
  355. "String used as prefix for timestamps clocking work hours on an item."
  356. :group 'org-keywords
  357. :type 'string)
  358. (defcustom org-comment-string "COMMENT"
  359. "Entries starting with this keyword will never be exported.
  360. An entry can be toggled between COMMENT and normal with
  361. \\[org-toggle-comment].
  362. Changes become only effective after restarting Emacs."
  363. :group 'org-keywords
  364. :type 'string)
  365. (defcustom org-quote-string "QUOTE"
  366. "Entries starting with this keyword will be exported in fixed-width font.
  367. Quoting applies only to the text in the entry following the headline, and does
  368. not extend beyond the next headline, even if that is lower level.
  369. An entry can be toggled between QUOTE and normal with
  370. \\[org-toggle-fixed-width-section]."
  371. :group 'org-keywords
  372. :type 'string)
  373. (defconst org-repeat-re
  374. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  375. "Regular expression for specifying repeated events.
  376. After a match, group 1 contains the repeat expression.")
  377. (defgroup org-structure nil
  378. "Options concerning the general structure of Org-mode files."
  379. :tag "Org Structure"
  380. :group 'org)
  381. (defgroup org-reveal-location nil
  382. "Options about how to make context of a location visible."
  383. :tag "Org Reveal Location"
  384. :group 'org-structure)
  385. (defconst org-context-choice
  386. '(choice
  387. (const :tag "Always" t)
  388. (const :tag "Never" nil)
  389. (repeat :greedy t :tag "Individual contexts"
  390. (cons
  391. (choice :tag "Context"
  392. (const agenda)
  393. (const org-goto)
  394. (const occur-tree)
  395. (const tags-tree)
  396. (const link-search)
  397. (const mark-goto)
  398. (const bookmark-jump)
  399. (const isearch)
  400. (const default))
  401. (boolean))))
  402. "Contexts for the reveal options.")
  403. (defcustom org-show-hierarchy-above '((default . t))
  404. "Non-nil means, show full hierarchy when revealing a location.
  405. Org-mode often shows locations in an org-mode file which might have
  406. been invisible before. When this is set, the hierarchy of headings
  407. above the exposed location is shown.
  408. Turning this off for example for sparse trees makes them very compact.
  409. Instead of t, this can also be an alist specifying this option for different
  410. contexts. Valid contexts are
  411. agenda when exposing an entry from the agenda
  412. org-goto when using the command `org-goto' on key C-c C-j
  413. occur-tree when using the command `org-occur' on key C-c /
  414. tags-tree when constructing a sparse tree based on tags matches
  415. link-search when exposing search matches associated with a link
  416. mark-goto when exposing the jump goal of a mark
  417. bookmark-jump when exposing a bookmark location
  418. isearch when exiting from an incremental search
  419. default default for all contexts not set explicitly"
  420. :group 'org-reveal-location
  421. :type org-context-choice)
  422. (defcustom org-show-following-heading '((default . nil))
  423. "Non-nil means, show following heading when revealing a location.
  424. Org-mode often shows locations in an org-mode file which might have
  425. been invisible before. When this is set, the heading following the
  426. match is shown.
  427. Turning this off for example for sparse trees makes them very compact,
  428. but makes it harder to edit the location of the match. In such a case,
  429. use the command \\[org-reveal] to show more context.
  430. Instead of t, this can also be an alist specifying this option for different
  431. contexts. See `org-show-hierarchy-above' for valid contexts."
  432. :group 'org-reveal-location
  433. :type org-context-choice)
  434. (defcustom org-show-siblings '((default . nil) (isearch t))
  435. "Non-nil means, show all sibling heading when revealing a location.
  436. Org-mode often shows locations in an org-mode file which might have
  437. been invisible before. When this is set, the sibling of the current entry
  438. heading are all made visible. If `org-show-hierarchy-above' is t,
  439. the same happens on each level of the hierarchy above the current entry.
  440. By default this is on for the isearch context, off for all other contexts.
  441. Turning this off for example for sparse trees makes them very compact,
  442. but makes it harder to edit the location of the match. In such a case,
  443. use the command \\[org-reveal] to show more context.
  444. Instead of t, this can also be an alist specifying this option for different
  445. contexts. See `org-show-hierarchy-above' for valid contexts."
  446. :group 'org-reveal-location
  447. :type org-context-choice)
  448. (defcustom org-show-entry-below '((default . nil))
  449. "Non-nil means, show the entry below a headline when revealing a location.
  450. Org-mode often shows locations in an org-mode file which might have
  451. been invisible before. When this is set, the text below the headline that is
  452. exposed is also shown.
  453. By default this is off for all contexts.
  454. Instead of t, this can also be an alist specifying this option for different
  455. contexts. See `org-show-hierarchy-above' for valid contexts."
  456. :group 'org-reveal-location
  457. :type org-context-choice)
  458. (defcustom org-indirect-buffer-display 'other-window
  459. "How should indirect tree buffers be displayed?
  460. This applies to indirect buffers created with the commands
  461. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  462. Valid values are:
  463. current-window Display in the current window
  464. other-window Just display in another window.
  465. dedicated-frame Create one new frame, and re-use it each time.
  466. new-frame Make a new frame each time. Note that in this case
  467. previously-made indirect buffers are kept, and you need to
  468. kill these buffers yourself."
  469. :group 'org-structure
  470. :group 'org-agenda-windows
  471. :type '(choice
  472. (const :tag "In current window" current-window)
  473. (const :tag "In current frame, other window" other-window)
  474. (const :tag "Each time a new frame" new-frame)
  475. (const :tag "One dedicated frame" dedicated-frame)))
  476. (defgroup org-cycle nil
  477. "Options concerning visibility cycling in Org-mode."
  478. :tag "Org Cycle"
  479. :group 'org-structure)
  480. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  481. "Names of drawers. Drawers are not opened by cycling on the headline above.
  482. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  483. this:
  484. :DRAWERNAME:
  485. .....
  486. :END:
  487. The drawer \"PROPERTIES\" is special for capturing properties through
  488. the property API.
  489. Drawers can be defined on the per-file basis with a line like:
  490. #+DRAWERS: HIDDEN STATE PROPERTIES"
  491. :group 'org-structure
  492. :type '(repeat (string :tag "Drawer Name")))
  493. (defcustom org-cycle-global-at-bob nil
  494. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  495. This makes it possible to do global cycling without having to use S-TAB or
  496. C-u TAB. For this special case to work, the first line of the buffer
  497. must not be a headline - it may be empty or some other text. When used in
  498. this way, `org-cycle-hook' is disables temporarily, to make sure the
  499. cursor stays at the beginning of the buffer.
  500. When this option is nil, don't do anything special at the beginning
  501. of the buffer."
  502. :group 'org-cycle
  503. :type 'boolean)
  504. (defcustom org-cycle-emulate-tab t
  505. "Where should `org-cycle' emulate TAB.
  506. nil Never
  507. white Only in completely white lines
  508. whitestart Only at the beginning of lines, before the first non-white char
  509. t Everywhere except in headlines
  510. exc-hl-bol Everywhere except at the start of a headline
  511. If TAB is used in a place where it does not emulate TAB, the current subtree
  512. visibility is cycled."
  513. :group 'org-cycle
  514. :type '(choice (const :tag "Never" nil)
  515. (const :tag "Only in completely white lines" white)
  516. (const :tag "Before first char in a line" whitestart)
  517. (const :tag "Everywhere except in headlines" t)
  518. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  519. ))
  520. (defcustom org-cycle-separator-lines 2
  521. "Number of empty lines needed to keep an empty line between collapsed trees.
  522. If you leave an empty line between the end of a subtree and the following
  523. headline, this empty line is hidden when the subtree is folded.
  524. Org-mode will leave (exactly) one empty line visible if the number of
  525. empty lines is equal or larger to the number given in this variable.
  526. So the default 2 means, at least 2 empty lines after the end of a subtree
  527. are needed to produce free space between a collapsed subtree and the
  528. following headline.
  529. Special case: when 0, never leave empty lines in collapsed view."
  530. :group 'org-cycle
  531. :type 'integer)
  532. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  533. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  534. org-cycle-hide-drawers
  535. org-cycle-show-empty-lines
  536. org-optimize-window-after-visibility-change)
  537. "Hook that is run after `org-cycle' has changed the buffer visibility.
  538. The function(s) in this hook must accept a single argument which indicates
  539. the new state that was set by the most recent `org-cycle' command. The
  540. argument is a symbol. After a global state change, it can have the values
  541. `overview', `content', or `all'. After a local state change, it can have
  542. the values `folded', `children', or `subtree'."
  543. :group 'org-cycle
  544. :type 'hook)
  545. (defgroup org-edit-structure nil
  546. "Options concerning structure editing in Org-mode."
  547. :tag "Org Edit Structure"
  548. :group 'org-structure)
  549. (defcustom org-odd-levels-only nil
  550. "Non-nil means, skip even levels and only use odd levels for the outline.
  551. This has the effect that two stars are being added/taken away in
  552. promotion/demotion commands. It also influences how levels are
  553. handled by the exporters.
  554. Changing it requires restart of `font-lock-mode' to become effective
  555. for fontification also in regions already fontified.
  556. You may also set this on a per-file basis by adding one of the following
  557. lines to the buffer:
  558. #+STARTUP: odd
  559. #+STARTUP: oddeven"
  560. :group 'org-edit-structure
  561. :group 'org-font-lock
  562. :type 'boolean)
  563. (defcustom org-adapt-indentation t
  564. "Non-nil means, adapt indentation when promoting and demoting.
  565. When this is set and the *entire* text in an entry is indented, the
  566. indentation is increased by one space in a demotion command, and
  567. decreased by one in a promotion command. If any line in the entry
  568. body starts at column 0, indentation is not changed at all."
  569. :group 'org-edit-structure
  570. :type 'boolean)
  571. (defcustom org-special-ctrl-a/e nil
  572. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  573. When t, `C-a' will bring back the cursor to the beginning of the
  574. headline text, i.e. after the stars and after a possible TODO keyword.
  575. In an item, this will be the position after the bullet.
  576. When the cursor is already at that position, another `C-a' will bring
  577. it to the beginning of the line.
  578. `C-e' will jump to the end of the headline, ignoring the presence of tags
  579. in the headline. A second `C-e' will then jump to the true end of the
  580. line, after any tags.
  581. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  582. and only a directly following, identical keypress will bring the cursor
  583. to the special positions."
  584. :group 'org-edit-structure
  585. :type '(choice
  586. (const :tag "off" nil)
  587. (const :tag "after bullet first" t)
  588. (const :tag "border first" reversed)))
  589. (if (fboundp 'defvaralias)
  590. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  591. (defcustom org-special-ctrl-k nil
  592. "Non-nil means `C-k' will behave specially in headlines.
  593. When nil, `C-k' will call the default `kill-line' command.
  594. When t, the following will happen while the cursor is in the headline:
  595. - When the cursor is at the beginning of a headline, kill the entire
  596. line and possible the folded subtree below the line.
  597. - When in the middle of the headline text, kill the headline up to the tags.
  598. - When after the headline text, kill the tags."
  599. :group 'org-edit-structure
  600. :type 'boolean)
  601. (defcustom org-yank-folded-subtrees t
  602. "Non-nil means, when yanking subtrees, fold them.
  603. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  604. it starts with a heading and all other headings in it are either children
  605. or siblings, then fold all the subtrees. However, do this only if no
  606. text after the yank would be swallowed into a folded tree by this action."
  607. :group 'org-edit-structure
  608. :type 'boolean)
  609. (defcustom org-yank-adjusted-subtrees nil
  610. "Non-nil means, when yanking subtrees, adjust the level.
  611. With this setting, `org-paste-subtree' is used to insert the subtree, see
  612. this function for details."
  613. :group 'org-edit-structure
  614. :type 'boolean)
  615. (defcustom org-M-RET-may-split-line '((default . t))
  616. "Non-nil means, M-RET will split the line at the cursor position.
  617. When nil, it will go to the end of the line before making a
  618. new line.
  619. You may also set this option in a different way for different
  620. contexts. Valid contexts are:
  621. headline when creating a new headline
  622. item when creating a new item
  623. table in a table field
  624. default the value to be used for all contexts not explicitly
  625. customized"
  626. :group 'org-structure
  627. :group 'org-table
  628. :type '(choice
  629. (const :tag "Always" t)
  630. (const :tag "Never" nil)
  631. (repeat :greedy t :tag "Individual contexts"
  632. (cons
  633. (choice :tag "Context"
  634. (const headline)
  635. (const item)
  636. (const table)
  637. (const default))
  638. (boolean)))))
  639. (defcustom org-insert-heading-respect-content nil
  640. "Non-nil means, insert new headings after the current subtree.
  641. When nil, the new heading is created directly after the current line.
  642. The commands \\[org-insert-heading-respect-content] and
  643. \\[org-insert-todo-heading-respect-content] turn this variable on
  644. for the duration of the command."
  645. :group 'org-structure
  646. :type 'boolean)
  647. (defcustom org-blank-before-new-entry '((heading . auto)
  648. (plain-list-item . auto))
  649. "Should `org-insert-heading' leave a blank line before new heading/item?
  650. The value is an alist, with `heading' and `plain-list-item' as car,
  651. and a boolean flag as cdr."
  652. :group 'org-edit-structure
  653. :type '(list
  654. (cons (const heading)
  655. (choice (const :tag "Never" nil)
  656. (const :tag "Always" t)
  657. (const :tag "Auto" auto)))
  658. (cons (const plain-list-item)
  659. (choice (const :tag "Never" nil)
  660. (const :tag "Always" t)
  661. (const :tag "Auto" auto)))))
  662. (defcustom org-insert-heading-hook nil
  663. "Hook being run after inserting a new heading."
  664. :group 'org-edit-structure
  665. :type 'hook)
  666. (defcustom org-enable-fixed-width-editor t
  667. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  668. This currently only means, they are never auto-wrapped.
  669. When nil, such lines will be treated like ordinary lines.
  670. See also the QUOTE keyword."
  671. :group 'org-edit-structure
  672. :type 'boolean)
  673. (defcustom org-edit-src-region-extra nil
  674. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  675. For examples see the function `org-edit-src-find-region-and-lang'.
  676. The regular expression identifying the begin marker should end with a newline,
  677. and the regexp marking the end line should start with a newline, to make sure
  678. there are kept outside the narrowed region."
  679. :group 'org-edit-structure
  680. :type '(repeat
  681. (list
  682. (regexp :tag "begin regexp")
  683. (regexp :tag "end regexp")
  684. (choice :tag "language"
  685. (string :tag "specify")
  686. (integer :tag "from match group")
  687. (const :tag "from `lang' element")
  688. (const :tag "from `style' element")))))
  689. (defcustom org-coderef-label-format "(ref:%s)"
  690. "The default coderef format.
  691. This format string will be used to search for coderef labels in literal
  692. examples (EXAMPLE and SRC blocks). The format can be overwritten
  693. an individual literal example with the -f option, like
  694. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  695. ...
  696. #+END_SRC
  697. If you want to use this for HTML export, make sure that the format does
  698. not introduce special font-locking, and avoid the HTML special
  699. characters `<', `>', and `&'. The reason for this restriction is that
  700. the labels are searched for only after htmlize has done its job."
  701. :group 'org-edit-structure ; FIXME this is not in the right group
  702. :type 'string)
  703. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  704. "The mode that should be used to edit fixed-width regions.
  705. These are the regions where each line starts with a colon."
  706. :group 'org-edit-structure
  707. :type '(choice
  708. (const artist-mode)
  709. (const picture-mode)
  710. (const fundamental-mode)
  711. (function :tag "Other (specify)")))
  712. (defcustom org-goto-auto-isearch t
  713. "Non-nil means, typing characters in org-goto starts incremental search."
  714. :group 'org-edit-structure
  715. :type 'boolean)
  716. (defgroup org-sparse-trees nil
  717. "Options concerning sparse trees in Org-mode."
  718. :tag "Org Sparse Trees"
  719. :group 'org-structure)
  720. (defcustom org-highlight-sparse-tree-matches t
  721. "Non-nil means, highlight all matches that define a sparse tree.
  722. The highlights will automatically disappear the next time the buffer is
  723. changed by an edit command."
  724. :group 'org-sparse-trees
  725. :type 'boolean)
  726. (defcustom org-remove-highlights-with-change t
  727. "Non-nil means, any change to the buffer will remove temporary highlights.
  728. Such highlights are created by `org-occur' and `org-clock-display'.
  729. When nil, `C-c C-c needs to be used to get rid of the highlights.
  730. The highlights created by `org-preview-latex-fragment' always need
  731. `C-c C-c' to be removed."
  732. :group 'org-sparse-trees
  733. :group 'org-time
  734. :type 'boolean)
  735. (defcustom org-occur-hook '(org-first-headline-recenter)
  736. "Hook that is run after `org-occur' has constructed a sparse tree.
  737. This can be used to recenter the window to show as much of the structure
  738. as possible."
  739. :group 'org-sparse-trees
  740. :type 'hook)
  741. (defgroup org-imenu-and-speedbar nil
  742. "Options concerning imenu and speedbar in Org-mode."
  743. :tag "Org Imenu and Speedbar"
  744. :group 'org-structure)
  745. (defcustom org-imenu-depth 2
  746. "The maximum level for Imenu access to Org-mode headlines.
  747. This also applied for speedbar access."
  748. :group 'org-imenu-and-speedbar
  749. :type 'number)
  750. (defgroup org-table nil
  751. "Options concerning tables in Org-mode."
  752. :tag "Org Table"
  753. :group 'org)
  754. (defcustom org-enable-table-editor 'optimized
  755. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  756. When nil, such lines will be treated like ordinary lines.
  757. When equal to the symbol `optimized', the table editor will be optimized to
  758. do the following:
  759. - Automatic overwrite mode in front of whitespace in table fields.
  760. This makes the structure of the table stay in tact as long as the edited
  761. field does not exceed the column width.
  762. - Minimize the number of realigns. Normally, the table is aligned each time
  763. TAB or RET are pressed to move to another field. With optimization this
  764. happens only if changes to a field might have changed the column width.
  765. Optimization requires replacing the functions `self-insert-command',
  766. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  767. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  768. very good at guessing when a re-align will be necessary, but you can always
  769. force one with \\[org-ctrl-c-ctrl-c].
  770. If you would like to use the optimized version in Org-mode, but the
  771. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  772. This variable can be used to turn on and off the table editor during a session,
  773. but in order to toggle optimization, a restart is required.
  774. See also the variable `org-table-auto-blank-field'."
  775. :group 'org-table
  776. :type '(choice
  777. (const :tag "off" nil)
  778. (const :tag "on" t)
  779. (const :tag "on, optimized" optimized)))
  780. (defcustom org-table-tab-recognizes-table.el t
  781. "Non-nil means, TAB will automatically notice a table.el table.
  782. When it sees such a table, it moves point into it and - if necessary -
  783. calls `table-recognize-table'."
  784. :group 'org-table-editing
  785. :type 'boolean)
  786. (defgroup org-link nil
  787. "Options concerning links in Org-mode."
  788. :tag "Org Link"
  789. :group 'org)
  790. (defvar org-link-abbrev-alist-local nil
  791. "Buffer-local version of `org-link-abbrev-alist', which see.
  792. The value of this is taken from the #+LINK lines.")
  793. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  794. (defcustom org-link-abbrev-alist nil
  795. "Alist of link abbreviations.
  796. The car of each element is a string, to be replaced at the start of a link.
  797. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  798. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  799. [[linkkey:tag][description]]
  800. If REPLACE is a string, the tag will simply be appended to create the link.
  801. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  802. the placeholder \"%h\" will cause a url-encoded version of the tag to
  803. be inserted at that point (see the function `url-hexify-string').
  804. REPLACE may also be a function that will be called with the tag as the
  805. only argument to create the link, which should be returned as a string.
  806. See the manual for examples."
  807. :group 'org-link
  808. :type '(repeat
  809. (cons
  810. (string :tag "Protocol")
  811. (choice
  812. (string :tag "Format")
  813. (function)))))
  814. (defcustom org-descriptive-links t
  815. "Non-nil means, hide link part and only show description of bracket links.
  816. Bracket links are like [[link][description]]. This variable sets the initial
  817. state in new org-mode buffers. The setting can then be toggled on a
  818. per-buffer basis from the Org->Hyperlinks menu."
  819. :group 'org-link
  820. :type 'boolean)
  821. (defcustom org-link-file-path-type 'adaptive
  822. "How the path name in file links should be stored.
  823. Valid values are:
  824. relative Relative to the current directory, i.e. the directory of the file
  825. into which the link is being inserted.
  826. absolute Absolute path, if possible with ~ for home directory.
  827. noabbrev Absolute path, no abbreviation of home directory.
  828. adaptive Use relative path for files in the current directory and sub-
  829. directories of it. For other files, use an absolute path."
  830. :group 'org-link
  831. :type '(choice
  832. (const relative)
  833. (const absolute)
  834. (const noabbrev)
  835. (const adaptive)))
  836. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  837. "Types of links that should be activated in Org-mode files.
  838. This is a list of symbols, each leading to the activation of a certain link
  839. type. In principle, it does not hurt to turn on most link types - there may
  840. be a small gain when turning off unused link types. The types are:
  841. bracket The recommended [[link][description]] or [[link]] links with hiding.
  842. angular Links in angular brackets that may contain whitespace like
  843. <bbdb:Carsten Dominik>.
  844. plain Plain links in normal text, no whitespace, like http://google.com.
  845. radio Text that is matched by a radio target, see manual for details.
  846. tag Tag settings in a headline (link to tag search).
  847. date Time stamps (link to calendar).
  848. footnote Footnote labels.
  849. Changing this variable requires a restart of Emacs to become effective."
  850. :group 'org-link
  851. :type '(set :greedy t
  852. (const :tag "Double bracket links (new style)" bracket)
  853. (const :tag "Angular bracket links (old style)" angular)
  854. (const :tag "Plain text links" plain)
  855. (const :tag "Radio target matches" radio)
  856. (const :tag "Tags" tag)
  857. (const :tag "Timestamps" date)
  858. (const :tag "Footnotes" footnote)))
  859. (defcustom org-make-link-description-function nil
  860. "Function to use to generate link descriptions from links. If
  861. nil the link location will be used. This function must take two
  862. parameters; the first is the link and the second the description
  863. org-insert-link has generated, and should return the description
  864. to use."
  865. :group 'org-link
  866. :type 'function)
  867. (defgroup org-link-store nil
  868. "Options concerning storing links in Org-mode."
  869. :tag "Org Store Link"
  870. :group 'org-link)
  871. (defcustom org-email-link-description-format "Email %c: %.30s"
  872. "Format of the description part of a link to an email or usenet message.
  873. The following %-escapes will be replaced by corresponding information:
  874. %F full \"From\" field
  875. %f name, taken from \"From\" field, address if no name
  876. %T full \"To\" field
  877. %t first name in \"To\" field, address if no name
  878. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  879. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  880. %s subject
  881. %m message-id.
  882. You may use normal field width specification between the % and the letter.
  883. This is for example useful to limit the length of the subject.
  884. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  885. :group 'org-link-store
  886. :type 'string)
  887. (defcustom org-from-is-user-regexp
  888. (let (r1 r2)
  889. (when (and user-mail-address (not (string= user-mail-address "")))
  890. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  891. (when (and user-full-name (not (string= user-full-name "")))
  892. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  893. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  894. "Regexp matched against the \"From:\" header of an email or usenet message.
  895. It should match if the message is from the user him/herself."
  896. :group 'org-link-store
  897. :type 'regexp)
  898. (defcustom org-link-to-org-use-id 'create-if-interactive
  899. "Non-nil means, storing a link to an Org file will use entry IDs.
  900. Note that before this variable is even considered, org-id must be loaded,
  901. to please customize `org-modules' and turn it on.
  902. The variable can have the following values:
  903. t Create an ID if needed to make a link to the current entry.
  904. create-if-interactive
  905. If `org-store-link' is called directly (interactively, as a user
  906. command), do create an ID to support the link. But when doing the
  907. job for remember, only use the ID if it already exists. The
  908. purpose of this setting is to avoid proliferation of unwanted
  909. IDs, just because you happen to be in an Org file when you
  910. call `org-remember' that automatically and preemptively
  911. creates a link. If you do want to get an ID link in a remember
  912. template to an entry not having an ID, create it first by
  913. explicitly creating a link to it, using `C-c C-l' first.
  914. use-existing
  915. Use existing ID, do not create one.
  916. nil Never use an ID to make a link, instead link using a text search for
  917. the headline text."
  918. :group 'org-link-store
  919. :type '(choice
  920. (const :tag "Create ID to make link" t)
  921. (const :tag "Create if string link interactively"
  922. 'create-if-interactive)
  923. (const :tag "Only use existing" 'use-existing)
  924. (const :tag "Do not use ID to create link" nil)))
  925. (defcustom org-context-in-file-links t
  926. "Non-nil means, file links from `org-store-link' contain context.
  927. A search string will be added to the file name with :: as separator and
  928. used to find the context when the link is activated by the command
  929. `org-open-at-point'.
  930. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  931. negates this setting for the duration of the command."
  932. :group 'org-link-store
  933. :type 'boolean)
  934. (defcustom org-keep-stored-link-after-insertion nil
  935. "Non-nil means, keep link in list for entire session.
  936. The command `org-store-link' adds a link pointing to the current
  937. location to an internal list. These links accumulate during a session.
  938. The command `org-insert-link' can be used to insert links into any
  939. Org-mode file (offering completion for all stored links). When this
  940. option is nil, every link which has been inserted once using \\[org-insert-link]
  941. will be removed from the list, to make completing the unused links
  942. more efficient."
  943. :group 'org-link-store
  944. :type 'boolean)
  945. (defgroup org-link-follow nil
  946. "Options concerning following links in Org-mode."
  947. :tag "Org Follow Link"
  948. :group 'org-link)
  949. (defcustom org-link-translation-function nil
  950. "Function to translate links with different syntax to Org syntax.
  951. This can be used to translate links created for example by the Planner
  952. or emacs-wiki packages to Org syntax.
  953. The function must accept two parameters, a TYPE containing the link
  954. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  955. which is everything after the link protocol. It should return a cons
  956. with possibly modified values of type and path.
  957. Org contains a function for this, so if you set this variable to
  958. `org-translate-link-from-planner', you should be able follow many
  959. links created by planner."
  960. :group 'org-link-follow
  961. :type 'function)
  962. (defcustom org-follow-link-hook nil
  963. "Hook that is run after a link has been followed."
  964. :group 'org-link-follow
  965. :type 'hook)
  966. (defcustom org-tab-follows-link nil
  967. "Non-nil means, on links TAB will follow the link.
  968. Needs to be set before org.el is loaded.
  969. This really should not be used, it does not make sense, and the
  970. implementation is bad."
  971. :group 'org-link-follow
  972. :type 'boolean)
  973. (defcustom org-return-follows-link nil
  974. "Non-nil means, on links RET will follow the link.
  975. Needs to be set before org.el is loaded."
  976. :group 'org-link-follow
  977. :type 'boolean)
  978. (defcustom org-mouse-1-follows-link
  979. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  980. "Non-nil means, mouse-1 on a link will follow the link.
  981. A longer mouse click will still set point. Does not work on XEmacs.
  982. Needs to be set before org.el is loaded."
  983. :group 'org-link-follow
  984. :type 'boolean)
  985. (defcustom org-mark-ring-length 4
  986. "Number of different positions to be recorded in the ring
  987. Changing this requires a restart of Emacs to work correctly."
  988. :group 'org-link-follow
  989. :type 'integer)
  990. (defcustom org-link-frame-setup
  991. '((vm . vm-visit-folder-other-frame)
  992. (gnus . gnus-other-frame)
  993. (file . find-file-other-window))
  994. "Setup the frame configuration for following links.
  995. When following a link with Emacs, it may often be useful to display
  996. this link in another window or frame. This variable can be used to
  997. set this up for the different types of links.
  998. For VM, use any of
  999. `vm-visit-folder'
  1000. `vm-visit-folder-other-frame'
  1001. For Gnus, use any of
  1002. `gnus'
  1003. `gnus-other-frame'
  1004. `org-gnus-no-new-news'
  1005. For FILE, use any of
  1006. `find-file'
  1007. `find-file-other-window'
  1008. `find-file-other-frame'
  1009. For the calendar, use the variable `calendar-setup'.
  1010. For BBDB, it is currently only possible to display the matches in
  1011. another window."
  1012. :group 'org-link-follow
  1013. :type '(list
  1014. (cons (const vm)
  1015. (choice
  1016. (const vm-visit-folder)
  1017. (const vm-visit-folder-other-window)
  1018. (const vm-visit-folder-other-frame)))
  1019. (cons (const gnus)
  1020. (choice
  1021. (const gnus)
  1022. (const gnus-other-frame)
  1023. (const org-gnus-no-new-news)))
  1024. (cons (const file)
  1025. (choice
  1026. (const find-file)
  1027. (const find-file-other-window)
  1028. (const find-file-other-frame)))))
  1029. (defcustom org-display-internal-link-with-indirect-buffer nil
  1030. "Non-nil means, use indirect buffer to display infile links.
  1031. Activating internal links (from one location in a file to another location
  1032. in the same file) normally just jumps to the location. When the link is
  1033. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1034. another window. When this option is set, the other window actually displays
  1035. an indirect buffer clone of the current buffer, to avoid any visibility
  1036. changes to the current buffer."
  1037. :group 'org-link-follow
  1038. :type 'boolean)
  1039. (defcustom org-open-non-existing-files nil
  1040. "Non-nil means, `org-open-file' will open non-existing files.
  1041. When nil, an error will be generated."
  1042. :group 'org-link-follow
  1043. :type 'boolean)
  1044. (defcustom org-open-directory-means-index-dot-org nil
  1045. "Non-nil means, a link to a directory really means to index.org.
  1046. When nil, following a directory link will run dired or open a finder/explorer
  1047. window on that directory."
  1048. :group 'org-link-follow
  1049. :type 'boolean)
  1050. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1051. "Function and arguments to call for following mailto links.
  1052. This is a list with the first element being a lisp function, and the
  1053. remaining elements being arguments to the function. In string arguments,
  1054. %a will be replaced by the address, and %s will be replaced by the subject
  1055. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1056. :group 'org-link-follow
  1057. :type '(choice
  1058. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1059. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1060. (const :tag "message-mail" (message-mail "%a" "%s"))
  1061. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1062. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1063. "Non-nil means, ask for confirmation before executing shell links.
  1064. Shell links can be dangerous: just think about a link
  1065. [[shell:rm -rf ~/*][Google Search]]
  1066. This link would show up in your Org-mode document as \"Google Search\",
  1067. but really it would remove your entire home directory.
  1068. Therefore we advise against setting this variable to nil.
  1069. Just change it to `y-or-n-p' of you want to confirm with a
  1070. single keystroke rather than having to type \"yes\"."
  1071. :group 'org-link-follow
  1072. :type '(choice
  1073. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1074. (const :tag "with y-or-n (faster)" y-or-n-p)
  1075. (const :tag "no confirmation (dangerous)" nil)))
  1076. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1077. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1078. Elisp links can be dangerous: just think about a link
  1079. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1080. This link would show up in your Org-mode document as \"Google Search\",
  1081. but really it would remove your entire home directory.
  1082. Therefore we advise against setting this variable to nil.
  1083. Just change it to `y-or-n-p' of you want to confirm with a
  1084. single keystroke rather than having to type \"yes\"."
  1085. :group 'org-link-follow
  1086. :type '(choice
  1087. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1088. (const :tag "with y-or-n (faster)" y-or-n-p)
  1089. (const :tag "no confirmation (dangerous)" nil)))
  1090. (defconst org-file-apps-defaults-gnu
  1091. '((remote . emacs)
  1092. (system . mailcap)
  1093. (t . mailcap))
  1094. "Default file applications on a UNIX or GNU/Linux system.
  1095. See `org-file-apps'.")
  1096. (defconst org-file-apps-defaults-macosx
  1097. '((remote . emacs)
  1098. (t . "open %s")
  1099. (system . "open %s")
  1100. ("ps.gz" . "gv %s")
  1101. ("eps.gz" . "gv %s")
  1102. ("dvi" . "xdvi %s")
  1103. ("fig" . "xfig %s"))
  1104. "Default file applications on a MacOS X system.
  1105. The system \"open\" is known as a default, but we use X11 applications
  1106. for some files for which the OS does not have a good default.
  1107. See `org-file-apps'.")
  1108. (defconst org-file-apps-defaults-windowsnt
  1109. (list
  1110. '(remote . emacs)
  1111. (cons t
  1112. (list (if (featurep 'xemacs)
  1113. 'mswindows-shell-execute
  1114. 'w32-shell-execute)
  1115. "open" 'file))
  1116. (cons 'system
  1117. (list (if (featurep 'xemacs)
  1118. 'mswindows-shell-execute
  1119. 'w32-shell-execute)
  1120. "open" 'file)))
  1121. "Default file applications on a Windows NT system.
  1122. The system \"open\" is used for most files.
  1123. See `org-file-apps'.")
  1124. (defcustom org-file-apps
  1125. '(
  1126. (auto-mode . emacs)
  1127. ("\\.x?html?\\'" . default)
  1128. ("\\.pdf\\'" . default)
  1129. )
  1130. "External applications for opening `file:path' items in a document.
  1131. Org-mode uses system defaults for different file types, but
  1132. you can use this variable to set the application for a given file
  1133. extension. The entries in this list are cons cells where the car identifies
  1134. files and the cdr the corresponding command. Possible values for the
  1135. file identifier are
  1136. \"regex\" Regular expression matched against the file name. For backward
  1137. compatibility, this can also be a string with only alphanumeric
  1138. characters, which is then interpreted as an extension.
  1139. `directory' Matches a directory
  1140. `remote' Matches a remote file, accessible through tramp or efs.
  1141. Remote files most likely should be visited through Emacs
  1142. because external applications cannot handle such paths.
  1143. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1144. so all files Emacs knows how to handle. Using this with
  1145. command `emacs' will open most files in Emacs. Beware that this
  1146. will also open html files inside Emacs, unless you add
  1147. (\"html\" . default) to the list as well.
  1148. t Default for files not matched by any of the other options.
  1149. `system' The system command to open files, like `open' on Windows
  1150. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1151. that will be selected if you call `C-c C-o' with a double
  1152. `C-u C-u' prefix.
  1153. Possible values for the command are:
  1154. `emacs' The file will be visited by the current Emacs process.
  1155. `default' Use the default application for this file type, which is the
  1156. association for t in the list, most likely in the system-specific
  1157. part.
  1158. This can be used to overrule an unwanted setting in the
  1159. system-specific variable.
  1160. `system' Use the system command for opening files, like \"open\".
  1161. This command is specified by the entry whose car is `system'.
  1162. Most likely, the system-specific version of this variable
  1163. does define this command, but you can overrule/replace it
  1164. here.
  1165. string A command to be executed by a shell; %s will be replaced
  1166. by the path to the file.
  1167. sexp A Lisp form which will be evaluated. The file path will
  1168. be available in the Lisp variable `file'.
  1169. For more examples, see the system specific constants
  1170. `org-file-apps-defaults-macosx'
  1171. `org-file-apps-defaults-windowsnt'
  1172. `org-file-apps-defaults-gnu'."
  1173. :group 'org-link-follow
  1174. :type '(repeat
  1175. (cons (choice :value ""
  1176. (string :tag "Extension")
  1177. (const :tag "System command to open files" system)
  1178. (const :tag "Default for unrecognized files" t)
  1179. (const :tag "Remote file" remote)
  1180. (const :tag "Links to a directory" directory)
  1181. (const :tag "Any files that have Emacs modes"
  1182. auto-mode))
  1183. (choice :value ""
  1184. (const :tag "Visit with Emacs" emacs)
  1185. (const :tag "Use default" default)
  1186. (const :tag "Use the system command" system)
  1187. (string :tag "Command")
  1188. (sexp :tag "Lisp form")))))
  1189. (defgroup org-refile nil
  1190. "Options concerning refiling entries in Org-mode."
  1191. :tag "Org Refile"
  1192. :group 'org)
  1193. (defcustom org-directory "~/org"
  1194. "Directory with org files.
  1195. This is just a default location to look for Org files. There is no need
  1196. at all to put your files into this directory. It is only used in the
  1197. following situations:
  1198. 1. When a remember template specifies a target file that is not an
  1199. absolute path. The path will then be interpreted relative to
  1200. `org-directory'
  1201. 2. When a remember note is filed away in an interactive way (when exiting the
  1202. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1203. with `org-directory' as the default path."
  1204. :group 'org-refile
  1205. :group 'org-remember
  1206. :type 'directory)
  1207. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1208. "Default target for storing notes.
  1209. Used by the hooks for remember.el. This can be a string, or nil to mean
  1210. the value of `remember-data-file'.
  1211. You can set this on a per-template basis with the variable
  1212. `org-remember-templates'."
  1213. :group 'org-refile
  1214. :group 'org-remember
  1215. :type '(choice
  1216. (const :tag "Default from remember-data-file" nil)
  1217. file))
  1218. (defcustom org-goto-interface 'outline
  1219. "The default interface to be used for `org-goto'.
  1220. Allowed values are:
  1221. outline The interface shows an outline of the relevant file
  1222. and the correct heading is found by moving through
  1223. the outline or by searching with incremental search.
  1224. outline-path-completion Headlines in the current buffer are offered via
  1225. completion. This is the interface also used by
  1226. the refile command."
  1227. :group 'org-refile
  1228. :type '(choice
  1229. (const :tag "Outline" outline)
  1230. (const :tag "Outline-path-completion" outline-path-completion)))
  1231. (defcustom org-goto-max-level 5
  1232. "Maximum level to be considered when running org-goto with refile interface."
  1233. :group 'org-refile
  1234. :type 'number)
  1235. (defcustom org-reverse-note-order nil
  1236. "Non-nil means, store new notes at the beginning of a file or entry.
  1237. When nil, new notes will be filed to the end of a file or entry.
  1238. This can also be a list with cons cells of regular expressions that
  1239. are matched against file names, and values."
  1240. :group 'org-remember
  1241. :group 'org-refile
  1242. :type '(choice
  1243. (const :tag "Reverse always" t)
  1244. (const :tag "Reverse never" nil)
  1245. (repeat :tag "By file name regexp"
  1246. (cons regexp boolean))))
  1247. (defcustom org-refile-targets nil
  1248. "Targets for refiling entries with \\[org-refile].
  1249. This is list of cons cells. Each cell contains:
  1250. - a specification of the files to be considered, either a list of files,
  1251. or a symbol whose function or variable value will be used to retrieve
  1252. a file name or a list of file names. If you use `org-agenda-files' for
  1253. that, all agenda files will be scanned for targets. Nil means, consider
  1254. headings in the current buffer.
  1255. - A specification of how to find candidate refile targets. This may be
  1256. any of:
  1257. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1258. This tag has to be present in all target headlines, inheritance will
  1259. not be considered.
  1260. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1261. todo keyword.
  1262. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1263. headlines that are refiling targets.
  1264. - a cons cell (:level . N). Any headline of level N is considered a target.
  1265. Note that, when `org-odd-levels-only' is set, level corresponds to
  1266. order in hierarchy, not to the number of stars.
  1267. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1268. Note that, when `org-odd-levels-only' is set, level corresponds to
  1269. order in hierarchy, not to the number of stars.
  1270. When this variable is nil, all top-level headlines in the current buffer
  1271. are used, equivalent to the value `((nil . (:level . 1))'."
  1272. :group 'org-refile
  1273. :type '(repeat
  1274. (cons
  1275. (choice :value org-agenda-files
  1276. (const :tag "All agenda files" org-agenda-files)
  1277. (const :tag "Current buffer" nil)
  1278. (function) (variable) (file))
  1279. (choice :tag "Identify target headline by"
  1280. (cons :tag "Specific tag" (const :value :tag) (string))
  1281. (cons :tag "TODO keyword" (const :value :todo) (string))
  1282. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1283. (cons :tag "Level number" (const :value :level) (integer))
  1284. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1285. (defcustom org-refile-use-outline-path nil
  1286. "Non-nil means, provide refile targets as paths.
  1287. So a level 3 headline will be available as level1/level2/level3.
  1288. When the value is `file', also include the file name (without directory)
  1289. into the path. When `full-file-path', include the full file path."
  1290. :group 'org-refile
  1291. :type '(choice
  1292. (const :tag "Not" nil)
  1293. (const :tag "Yes" t)
  1294. (const :tag "Start with file name" file)
  1295. (const :tag "Start with full file path" full-file-path)))
  1296. (defcustom org-outline-path-complete-in-steps t
  1297. "Non-nil means, complete the outline path in hierarchical steps.
  1298. When Org-mode uses the refile interface to select an outline path
  1299. \(see variable `org-refile-use-outline-path'), the completion of
  1300. the path can be done is a single go, or if can be done in steps down
  1301. the headline hierarchy. Going in steps is probably the best if you
  1302. do not use a special completion package like `ido' or `icicles'.
  1303. However, when using these packages, going in one step can be very
  1304. fast, while still showing the whole path to the entry."
  1305. :group 'org-refile
  1306. :type 'boolean)
  1307. (defgroup org-todo nil
  1308. "Options concerning TODO items in Org-mode."
  1309. :tag "Org TODO"
  1310. :group 'org)
  1311. (defgroup org-progress nil
  1312. "Options concerning Progress logging in Org-mode."
  1313. :tag "Org Progress"
  1314. :group 'org-time)
  1315. (defvar org-todo-interpretation-widgets
  1316. '(
  1317. (:tag "Sequence (cycling hits every state)" sequence)
  1318. (:tag "Type (cycling directly to DONE)" type))
  1319. "The available interpretation symbols for customizing
  1320. `org-todo-keywords'.
  1321. Interested libraries should add to this list.")
  1322. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1323. "List of TODO entry keyword sequences and their interpretation.
  1324. \\<org-mode-map>This is a list of sequences.
  1325. Each sequence starts with a symbol, either `sequence' or `type',
  1326. indicating if the keywords should be interpreted as a sequence of
  1327. action steps, or as different types of TODO items. The first
  1328. keywords are states requiring action - these states will select a headline
  1329. for inclusion into the global TODO list Org-mode produces. If one of
  1330. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1331. signify that no further action is necessary. If \"|\" is not found,
  1332. the last keyword is treated as the only DONE state of the sequence.
  1333. The command \\[org-todo] cycles an entry through these states, and one
  1334. additional state where no keyword is present. For details about this
  1335. cycling, see the manual.
  1336. TODO keywords and interpretation can also be set on a per-file basis with
  1337. the special #+SEQ_TODO and #+TYP_TODO lines.
  1338. Each keyword can optionally specify a character for fast state selection
  1339. \(in combination with the variable `org-use-fast-todo-selection')
  1340. and specifiers for state change logging, using the same syntax
  1341. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1342. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1343. indicates to record a time stamp each time this state is selected.
  1344. Each keyword may also specify if a timestamp or a note should be
  1345. recorded when entering or leaving the state, by adding additional
  1346. characters in the parenthesis after the keyword. This looks like this:
  1347. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1348. record only the time of the state change. With X and Y being either
  1349. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1350. Y when leaving the state if and only if the *target* state does not
  1351. define X. You may omit any of the fast-selection key or X or /Y,
  1352. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1353. For backward compatibility, this variable may also be just a list
  1354. of keywords - in this case the interpretation (sequence or type) will be
  1355. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1356. :group 'org-todo
  1357. :group 'org-keywords
  1358. :type '(choice
  1359. (repeat :tag "Old syntax, just keywords"
  1360. (string :tag "Keyword"))
  1361. (repeat :tag "New syntax"
  1362. (cons
  1363. (choice
  1364. :tag "Interpretation"
  1365. ;;Quick and dirty way to see
  1366. ;;`org-todo-interpretations'. This takes the
  1367. ;;place of item arguments
  1368. :convert-widget
  1369. (lambda (widget)
  1370. (widget-put widget
  1371. :args (mapcar
  1372. #'(lambda (x)
  1373. (widget-convert
  1374. (cons 'const x)))
  1375. org-todo-interpretation-widgets))
  1376. widget))
  1377. (repeat
  1378. (string :tag "Keyword"))))))
  1379. (defvar org-todo-keywords-1 nil
  1380. "All TODO and DONE keywords active in a buffer.")
  1381. (make-variable-buffer-local 'org-todo-keywords-1)
  1382. (defvar org-todo-keywords-for-agenda nil)
  1383. (defvar org-done-keywords-for-agenda nil)
  1384. (defvar org-todo-keyword-alist-for-agenda nil)
  1385. (defvar org-tag-alist-for-agenda nil)
  1386. (defvar org-agenda-contributing-files nil)
  1387. (defvar org-not-done-keywords nil)
  1388. (make-variable-buffer-local 'org-not-done-keywords)
  1389. (defvar org-done-keywords nil)
  1390. (make-variable-buffer-local 'org-done-keywords)
  1391. (defvar org-todo-heads nil)
  1392. (make-variable-buffer-local 'org-todo-heads)
  1393. (defvar org-todo-sets nil)
  1394. (make-variable-buffer-local 'org-todo-sets)
  1395. (defvar org-todo-log-states nil)
  1396. (make-variable-buffer-local 'org-todo-log-states)
  1397. (defvar org-todo-kwd-alist nil)
  1398. (make-variable-buffer-local 'org-todo-kwd-alist)
  1399. (defvar org-todo-key-alist nil)
  1400. (make-variable-buffer-local 'org-todo-key-alist)
  1401. (defvar org-todo-key-trigger nil)
  1402. (make-variable-buffer-local 'org-todo-key-trigger)
  1403. (defcustom org-todo-interpretation 'sequence
  1404. "Controls how TODO keywords are interpreted.
  1405. This variable is in principle obsolete and is only used for
  1406. backward compatibility, if the interpretation of todo keywords is
  1407. not given already in `org-todo-keywords'. See that variable for
  1408. more information."
  1409. :group 'org-todo
  1410. :group 'org-keywords
  1411. :type '(choice (const sequence)
  1412. (const type)))
  1413. (defcustom org-use-fast-todo-selection t
  1414. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1415. This variable describes if and under what circumstances the cycling
  1416. mechanism for TODO keywords will be replaced by a single-key, direct
  1417. selection scheme.
  1418. When nil, fast selection is never used.
  1419. When the symbol `prefix', it will be used when `org-todo' is called with
  1420. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1421. in an agenda buffer.
  1422. When t, fast selection is used by default. In this case, the prefix
  1423. argument forces cycling instead.
  1424. In all cases, the special interface is only used if access keys have actually
  1425. been assigned by the user, i.e. if keywords in the configuration are followed
  1426. by a letter in parenthesis, like TODO(t)."
  1427. :group 'org-todo
  1428. :type '(choice
  1429. (const :tag "Never" nil)
  1430. (const :tag "By default" t)
  1431. (const :tag "Only with C-u C-c C-t" prefix)))
  1432. (defcustom org-provide-todo-statistics t
  1433. "Non-nil means, update todo statistics after insert and toggle.
  1434. When this is set, todo statistics is updated in the parent of the current
  1435. entry each time a todo state is changed."
  1436. :group 'org-todo
  1437. :type 'boolean)
  1438. (defcustom org-after-todo-state-change-hook nil
  1439. "Hook which is run after the state of a TODO item was changed.
  1440. The new state (a string with a TODO keyword, or nil) is available in the
  1441. Lisp variable `state'."
  1442. :group 'org-todo
  1443. :type 'hook)
  1444. (defvar org-blocker-hook nil
  1445. "Hook for functions that are allowed to block a state change.
  1446. Each function gets as its single argument a property list, see
  1447. `org-trigger-hook' for more information about this list.
  1448. If any of the functions in this hook returns nil, the state change
  1449. is blocked.")
  1450. (defvar org-trigger-hook nil
  1451. "Hook for functions that are triggered by a state change.
  1452. Each function gets as its single argument a property list with at least
  1453. the following elements:
  1454. (:type type-of-change :position pos-at-entry-start
  1455. :from old-state :to new-state)
  1456. Depending on the type, more properties may be present.
  1457. This mechanism is currently implemented for:
  1458. TODO state changes
  1459. ------------------
  1460. :type todo-state-change
  1461. :from previous state (keyword as a string), or nil, or a symbol
  1462. 'todo' or 'done', to indicate the general type of state.
  1463. :to new state, like in :from")
  1464. (defcustom org-enforce-todo-dependencies nil
  1465. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1466. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1467. be blocked if any prior sibling is not yet done.
  1468. This variable needs to be set before org.el is loaded, and you need to
  1469. restart Emacs after a change to make the change effective. The only way
  1470. to change is while Emacs is running is through the customize interface."
  1471. :set (lambda (var val)
  1472. (set var val)
  1473. (if val
  1474. (add-hook 'org-blocker-hook
  1475. 'org-block-todo-from-children-or-siblings)
  1476. (remove-hook 'org-blocker-hook
  1477. 'org-block-todo-from-children-or-siblings)))
  1478. :group 'org-todo
  1479. :type 'boolean)
  1480. (defcustom org-enforce-todo-checkbox-dependencies nil
  1481. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1482. When this is nil, checkboxes have no influence on switching TODO states.
  1483. When non-nil, you first need to check off all check boxes before the TODO
  1484. entry can be switched to DONE.
  1485. This variable needs to be set before org.el is loaded, and you need to
  1486. restart Emacs after a change to make the change effective. The only way
  1487. to change is while Emacs is running is through the customize interface."
  1488. :set (lambda (var val)
  1489. (set var val)
  1490. (if val
  1491. (add-hook 'org-blocker-hook
  1492. 'org-block-todo-from-checkboxes)
  1493. (remove-hook 'org-blocker-hook
  1494. 'org-block-todo-from-checkboxes)))
  1495. :group 'org-todo
  1496. :type 'boolean)
  1497. (defcustom org-todo-state-tags-triggers nil
  1498. "Tag changes that should be triggered by TODO state changes.
  1499. This is a list. Each entry is
  1500. (state-change (tag . flag) .......)
  1501. State-change can be a string with a state, and empty string to indicate the
  1502. state that has no TODO keyword, or it can be one of the symbols `todo'
  1503. or `done', meaning any not-done or done state, respectively."
  1504. :group 'org-todo
  1505. :group 'org-tags
  1506. :type '(repeat
  1507. (cons (choice :tag "When changing to"
  1508. (const :tag "Not-done state" todo)
  1509. (const :tag "Done state" done)
  1510. (string :tag "State"))
  1511. (repeat
  1512. (cons :tag "Tag action"
  1513. (string :tag "Tag")
  1514. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1515. (defcustom org-log-done nil
  1516. "Information to record when a task moves to the DONE state.
  1517. Possible values are:
  1518. nil Don't add anything, just change the keyword
  1519. time Add a time stamp to the task
  1520. note Prompt a closing note and add it with template `org-log-note-headings'
  1521. This option can also be set with on a per-file-basis with
  1522. #+STARTUP: nologdone
  1523. #+STARTUP: logdone
  1524. #+STARTUP: lognotedone
  1525. You can have local logging settings for a subtree by setting the LOGGING
  1526. property to one or more of these keywords."
  1527. :group 'org-todo
  1528. :group 'org-progress
  1529. :type '(choice
  1530. (const :tag "No logging" nil)
  1531. (const :tag "Record CLOSED timestamp" time)
  1532. (const :tag "Record CLOSED timestamp with closing note." note)))
  1533. ;; Normalize old uses of org-log-done.
  1534. (cond
  1535. ((eq org-log-done t) (setq org-log-done 'time))
  1536. ((and (listp org-log-done) (memq 'done org-log-done))
  1537. (setq org-log-done 'note)))
  1538. (defcustom org-log-note-clock-out nil
  1539. "Non-nil means, record a note when clocking out of an item.
  1540. This can also be configured on a per-file basis by adding one of
  1541. the following lines anywhere in the buffer:
  1542. #+STARTUP: lognoteclock-out
  1543. #+STARTUP: nolognoteclock-out"
  1544. :group 'org-todo
  1545. :group 'org-progress
  1546. :type 'boolean)
  1547. (defcustom org-log-done-with-time t
  1548. "Non-nil means, the CLOSED time stamp will contain date and time.
  1549. When nil, only the date will be recorded."
  1550. :group 'org-progress
  1551. :type 'boolean)
  1552. (defcustom org-log-note-headings
  1553. '((done . "CLOSING NOTE %t")
  1554. (state . "State %-12s from %-12S %t")
  1555. (note . "Note taken on %t")
  1556. (clock-out . ""))
  1557. "Headings for notes added to entries.
  1558. The value is an alist, with the car being a symbol indicating the note
  1559. context, and the cdr is the heading to be used. The heading may also be the
  1560. empty string.
  1561. %t in the heading will be replaced by a time stamp.
  1562. %s will be replaced by the new TODO state, in double quotes.
  1563. %S will be replaced by the old TODO state, in double quotes.
  1564. %u will be replaced by the user name.
  1565. %U will be replaced by the full user name."
  1566. :group 'org-todo
  1567. :group 'org-progress
  1568. :type '(list :greedy t
  1569. (cons (const :tag "Heading when closing an item" done) string)
  1570. (cons (const :tag
  1571. "Heading when changing todo state (todo sequence only)"
  1572. state) string)
  1573. (cons (const :tag "Heading when just taking a note" note) string)
  1574. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1575. (unless (assq 'note org-log-note-headings)
  1576. (push '(note . "%t") org-log-note-headings))
  1577. (defcustom org-log-state-notes-into-drawer nil
  1578. "Non-nil means, insert state change notes and time stamps into a drawer.
  1579. When nil, state changes notes will be inserted after the headline and
  1580. any scheduling and clock lines, but not inside a drawer. When t,
  1581. notes
  1582. The value of this variable should be the name of the drawer to use.
  1583. LOGBOOK is proposed at the default drawer for this purpose, you can
  1584. also set this to a string to define the drawer of your choice.
  1585. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1586. will be ognored."
  1587. :group 'org-todo
  1588. :group 'org-progress
  1589. :type '(choice
  1590. (const :tag "Not into a drawer" nil)
  1591. (const :tag "LOGBOOK" "LOGBOOK")
  1592. (string :tag "Other")))
  1593. (defcustom org-log-state-notes-insert-after-drawers nil
  1594. "Non-nil means, insert state change notes after any drawers in entry.
  1595. Only the drawers that *immediately* follow the headline and the
  1596. deadline/scheduled line are skipped.
  1597. When nil, insert notes right after the heading and perhaps the line
  1598. with deadline/scheduling if present.
  1599. This variable will have no effect if `org-log-state-notes-into-drawer' is
  1600. set."
  1601. :group 'org-todo
  1602. :group 'org-progress
  1603. :type 'boolean)
  1604. (defcustom org-log-states-order-reversed t
  1605. "Non-nil means, the latest state change note will be directly after heading.
  1606. When nil, the notes will be orderer according to time."
  1607. :group 'org-todo
  1608. :group 'org-progress
  1609. :type 'boolean)
  1610. (defcustom org-log-repeat 'time
  1611. "Non-nil means, record moving through the DONE state when triggering repeat.
  1612. An auto-repeating tasks is immediately switched back to TODO when marked
  1613. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1614. the TODO keyword definition, or recording a closing note by setting
  1615. `org-log-done', there will be no record of the task moving through DONE.
  1616. This variable forces taking a note anyway. Possible values are:
  1617. nil Don't force a record
  1618. time Record a time stamp
  1619. note Record a note
  1620. This option can also be set with on a per-file-basis with
  1621. #+STARTUP: logrepeat
  1622. #+STARTUP: lognoterepeat
  1623. #+STARTUP: nologrepeat
  1624. You can have local logging settings for a subtree by setting the LOGGING
  1625. property to one or more of these keywords."
  1626. :group 'org-todo
  1627. :group 'org-progress
  1628. :type '(choice
  1629. (const :tag "Don't force a record" nil)
  1630. (const :tag "Force recording the DONE state" time)
  1631. (const :tag "Force recording a note with the DONE state" note)))
  1632. (defgroup org-priorities nil
  1633. "Priorities in Org-mode."
  1634. :tag "Org Priorities"
  1635. :group 'org-todo)
  1636. (defcustom org-highest-priority ?A
  1637. "The highest priority of TODO items. A character like ?A, ?B etc.
  1638. Must have a smaller ASCII number than `org-lowest-priority'."
  1639. :group 'org-priorities
  1640. :type 'character)
  1641. (defcustom org-lowest-priority ?C
  1642. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1643. Must have a larger ASCII number than `org-highest-priority'."
  1644. :group 'org-priorities
  1645. :type 'character)
  1646. (defcustom org-default-priority ?B
  1647. "The default priority of TODO items.
  1648. This is the priority an item get if no explicit priority is given."
  1649. :group 'org-priorities
  1650. :type 'character)
  1651. (defcustom org-priority-start-cycle-with-default t
  1652. "Non-nil means, start with default priority when starting to cycle.
  1653. When this is nil, the first step in the cycle will be (depending on the
  1654. command used) one higher or lower that the default priority."
  1655. :group 'org-priorities
  1656. :type 'boolean)
  1657. (defgroup org-time nil
  1658. "Options concerning time stamps and deadlines in Org-mode."
  1659. :tag "Org Time"
  1660. :group 'org)
  1661. (defcustom org-insert-labeled-timestamps-at-point nil
  1662. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1663. When nil, these labeled time stamps are forces into the second line of an
  1664. entry, just after the headline. When scheduling from the global TODO list,
  1665. the time stamp will always be forced into the second line."
  1666. :group 'org-time
  1667. :type 'boolean)
  1668. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1669. "Formats for `format-time-string' which are used for time stamps.
  1670. It is not recommended to change this constant.")
  1671. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1672. "Number of minutes to round time stamps to.
  1673. These are two values, the first applies when first creating a time stamp.
  1674. The second applies when changing it with the commands `S-up' and `S-down'.
  1675. When changing the time stamp, this means that it will change in steps
  1676. of N minutes, as given by the second value.
  1677. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1678. numbers should be factors of 60, so for example 5, 10, 15.
  1679. When this is larger than 1, you can still force an exact time-stamp by using
  1680. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1681. and by using a prefix arg to `S-up/down' to specify the exact number
  1682. of minutes to shift."
  1683. :group 'org-time
  1684. :get '(lambda (var) ; Make sure all entries have 5 elements
  1685. (if (integerp (default-value var))
  1686. (list (default-value var) 5)
  1687. (default-value var)))
  1688. :type '(list
  1689. (integer :tag "when inserting times")
  1690. (integer :tag "when modifying times")))
  1691. ;; Normalize old customizations of this variable.
  1692. (when (integerp org-time-stamp-rounding-minutes)
  1693. (setq org-time-stamp-rounding-minutes
  1694. (list org-time-stamp-rounding-minutes
  1695. org-time-stamp-rounding-minutes)))
  1696. (defcustom org-display-custom-times nil
  1697. "Non-nil means, overlay custom formats over all time stamps.
  1698. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1699. To turn this on on a per-file basis, insert anywhere in the file:
  1700. #+STARTUP: customtime"
  1701. :group 'org-time
  1702. :set 'set-default
  1703. :type 'sexp)
  1704. (make-variable-buffer-local 'org-display-custom-times)
  1705. (defcustom org-time-stamp-custom-formats
  1706. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1707. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1708. These are overlayed over the default ISO format if the variable
  1709. `org-display-custom-times' is set. Time like %H:%M should be at the
  1710. end of the second format."
  1711. :group 'org-time
  1712. :type 'sexp)
  1713. (defun org-time-stamp-format (&optional long inactive)
  1714. "Get the right format for a time string."
  1715. (let ((f (if long (cdr org-time-stamp-formats)
  1716. (car org-time-stamp-formats))))
  1717. (if inactive
  1718. (concat "[" (substring f 1 -1) "]")
  1719. f)))
  1720. (defcustom org-time-clocksum-format "%d:%02d"
  1721. "The format string used when creating CLOCKSUM lines, or when
  1722. org-mode generates a time duration."
  1723. :group 'org-time
  1724. :type 'string)
  1725. (defcustom org-deadline-warning-days 14
  1726. "No. of days before expiration during which a deadline becomes active.
  1727. This variable governs the display in sparse trees and in the agenda.
  1728. When 0 or negative, it means use this number (the absolute value of it)
  1729. even if a deadline has a different individual lead time specified."
  1730. :group 'org-time
  1731. :group 'org-agenda-daily/weekly
  1732. :type 'number)
  1733. (defcustom org-read-date-prefer-future t
  1734. "Non-nil means, assume future for incomplete date input from user.
  1735. This affects the following situations:
  1736. 1. The user gives a day, but no month.
  1737. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1738. read this as the third of *next* month. However, if you enter \"17\",
  1739. it will be considered as *this* month.
  1740. 2. The user gives a month but not a year.
  1741. For example, if it is april and you enter \"feb 2\", this will be read
  1742. as feb 2, *next* year. \"May 5\", however, will be this year.
  1743. Currently this does not work for ISO week specifications.
  1744. When this option is nil, the current month and year will always be used
  1745. as defaults."
  1746. :group 'org-time
  1747. :type 'boolean)
  1748. (defcustom org-read-date-display-live t
  1749. "Non-nil means, display current interpretation of date prompt live.
  1750. This display will be in an overlay, in the minibuffer."
  1751. :group 'org-time
  1752. :type 'boolean)
  1753. (defcustom org-read-date-popup-calendar t
  1754. "Non-nil means, pop up a calendar when prompting for a date.
  1755. In the calendar, the date can be selected with mouse-1. However, the
  1756. minibuffer will also be active, and you can simply enter the date as well.
  1757. When nil, only the minibuffer will be available."
  1758. :group 'org-time
  1759. :type 'boolean)
  1760. (if (fboundp 'defvaralias)
  1761. (defvaralias 'org-popup-calendar-for-date-prompt
  1762. 'org-read-date-popup-calendar))
  1763. (defcustom org-extend-today-until 0
  1764. "The hour when your day really ends. Must be an integer.
  1765. This has influence for the following applications:
  1766. - When switching the agenda to \"today\". It it is still earlier than
  1767. the time given here, the day recognized as TODAY is actually yesterday.
  1768. - When a date is read from the user and it is still before the time given
  1769. here, the current date and time will be assumed to be yesterday, 23:59.
  1770. Also, timestamps inserted in remember templates follow this rule.
  1771. IMPORTANT: This is a feature whose implementation is and likely will
  1772. remain incomplete. Really, it is only here because past midnight seems to
  1773. be the favorite working time of John Wiegley :-)"
  1774. :group 'org-time
  1775. :type 'number)
  1776. (defcustom org-edit-timestamp-down-means-later nil
  1777. "Non-nil means, S-down will increase the time in a time stamp.
  1778. When nil, S-up will increase."
  1779. :group 'org-time
  1780. :type 'boolean)
  1781. (defcustom org-calendar-follow-timestamp-change t
  1782. "Non-nil means, make the calendar window follow timestamp changes.
  1783. When a timestamp is modified and the calendar window is visible, it will be
  1784. moved to the new date."
  1785. :group 'org-time
  1786. :type 'boolean)
  1787. (defgroup org-tags nil
  1788. "Options concerning tags in Org-mode."
  1789. :tag "Org Tags"
  1790. :group 'org)
  1791. (defcustom org-tag-alist nil
  1792. "List of tags allowed in Org-mode files.
  1793. When this list is nil, Org-mode will base TAG input on what is already in the
  1794. buffer.
  1795. The value of this variable is an alist, the car of each entry must be a
  1796. keyword as a string, the cdr may be a character that is used to select
  1797. that tag through the fast-tag-selection interface.
  1798. See the manual for details."
  1799. :group 'org-tags
  1800. :type '(repeat
  1801. (choice
  1802. (cons (string :tag "Tag name")
  1803. (character :tag "Access char"))
  1804. (const :tag "Start radio group" (:startgroup))
  1805. (const :tag "End radio group" (:endgroup)))))
  1806. (defvar org-file-tags nil
  1807. "List of tags that can be inherited by all entries in the file.
  1808. The tags will be inherited if the variable `org-use-tag-inheritance'
  1809. says they should be.
  1810. This variable is populated from #+TAG lines.")
  1811. (defcustom org-use-fast-tag-selection 'auto
  1812. "Non-nil means, use fast tag selection scheme.
  1813. This is a special interface to select and deselect tags with single keys.
  1814. When nil, fast selection is never used.
  1815. When the symbol `auto', fast selection is used if and only if selection
  1816. characters for tags have been configured, either through the variable
  1817. `org-tag-alist' or through a #+TAGS line in the buffer.
  1818. When t, fast selection is always used and selection keys are assigned
  1819. automatically if necessary."
  1820. :group 'org-tags
  1821. :type '(choice
  1822. (const :tag "Always" t)
  1823. (const :tag "Never" nil)
  1824. (const :tag "When selection characters are configured" 'auto)))
  1825. (defcustom org-fast-tag-selection-single-key nil
  1826. "Non-nil means, fast tag selection exits after first change.
  1827. When nil, you have to press RET to exit it.
  1828. During fast tag selection, you can toggle this flag with `C-c'.
  1829. This variable can also have the value `expert'. In this case, the window
  1830. displaying the tags menu is not even shown, until you press C-c again."
  1831. :group 'org-tags
  1832. :type '(choice
  1833. (const :tag "No" nil)
  1834. (const :tag "Yes" t)
  1835. (const :tag "Expert" expert)))
  1836. (defvar org-fast-tag-selection-include-todo nil
  1837. "Non-nil means, fast tags selection interface will also offer TODO states.
  1838. This is an undocumented feature, you should not rely on it.")
  1839. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1840. "The column to which tags should be indented in a headline.
  1841. If this number is positive, it specifies the column. If it is negative,
  1842. it means that the tags should be flushright to that column. For example,
  1843. -80 works well for a normal 80 character screen."
  1844. :group 'org-tags
  1845. :type 'integer)
  1846. (defcustom org-auto-align-tags t
  1847. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1848. These operations change the length of a headline and therefore shift
  1849. the tags around. With this options turned on, after each such operation
  1850. the tags are again aligned to `org-tags-column'."
  1851. :group 'org-tags
  1852. :type 'boolean)
  1853. (defcustom org-use-tag-inheritance t
  1854. "Non-nil means, tags in levels apply also for sublevels.
  1855. When nil, only the tags directly given in a specific line apply there.
  1856. This may also be a list of tags that should be inherited, or a regexp that
  1857. matches tags that should be inherited. Additional control is possible
  1858. with the variable `org-tags-exclude-from-inheritance' which gives an
  1859. explicit list of tags to be excluded from inheritance., even if the value of
  1860. `org-use-tag-inheritance' would select it for inheritance.
  1861. If this option is t, a match early-on in a tree can lead to a large
  1862. number of matches in the subtree when constructing the agenda or creating
  1863. a sparse tree. If you only want to see the first match in a tree during
  1864. a search, check out the variable `org-tags-match-list-sublevels'."
  1865. :group 'org-tags
  1866. :type '(choice
  1867. (const :tag "Not" nil)
  1868. (const :tag "Always" t)
  1869. (repeat :tag "Specific tags" (string :tag "Tag"))
  1870. (regexp :tag "Tags matched by regexp")))
  1871. (defcustom org-tags-exclude-from-inheritance nil
  1872. "List of tags that should never be inherited.
  1873. This is a way to exclude a few tags from inheritance. For way to do
  1874. the opposite, to actively allow inheritance for selected tags,
  1875. see the variable `org-use-tag-inheritance'."
  1876. :group 'org-tags
  1877. :type '(repeat (string :tag "Tag")))
  1878. (defun org-tag-inherit-p (tag)
  1879. "Check if TAG is one that should be inherited."
  1880. (cond
  1881. ((member tag org-tags-exclude-from-inheritance) nil)
  1882. ((eq org-use-tag-inheritance t) t)
  1883. ((not org-use-tag-inheritance) nil)
  1884. ((stringp org-use-tag-inheritance)
  1885. (string-match org-use-tag-inheritance tag))
  1886. ((listp org-use-tag-inheritance)
  1887. (member tag org-use-tag-inheritance))
  1888. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1889. (defcustom org-tags-match-list-sublevels t
  1890. "Non-nil means list also sublevels of headlines matching tag search.
  1891. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1892. the sublevels of a headline matching a tag search often also match
  1893. the same search. Listing all of them can create very long lists.
  1894. Setting this variable to nil causes subtrees of a match to be skipped.
  1895. This option is off by default, because inheritance in on. If you turn
  1896. inheritance off, you very likely want to turn this option on.
  1897. As a special case, if the tag search is restricted to TODO items, the
  1898. value of this variable is ignored and sublevels are always checked, to
  1899. make sure all corresponding TODO items find their way into the list.
  1900. This variable is semi-obsolete and probably should always be true. It
  1901. is better to limit inheritance to certain tags using the variables
  1902. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1903. :group 'org-tags
  1904. :type 'boolean)
  1905. (defvar org-tags-history nil
  1906. "History of minibuffer reads for tags.")
  1907. (defvar org-last-tags-completion-table nil
  1908. "The last used completion table for tags.")
  1909. (defvar org-after-tags-change-hook nil
  1910. "Hook that is run after the tags in a line have changed.")
  1911. (defgroup org-properties nil
  1912. "Options concerning properties in Org-mode."
  1913. :tag "Org Properties"
  1914. :group 'org)
  1915. (defcustom org-property-format "%-10s %s"
  1916. "How property key/value pairs should be formatted by `indent-line'.
  1917. When `indent-line' hits a property definition, it will format the line
  1918. according to this format, mainly to make sure that the values are
  1919. lined-up with respect to each other."
  1920. :group 'org-properties
  1921. :type 'string)
  1922. (defcustom org-use-property-inheritance nil
  1923. "Non-nil means, properties apply also for sublevels.
  1924. This setting is chiefly used during property searches. Turning it on can
  1925. cause significant overhead when doing a search, which is why it is not
  1926. on by default.
  1927. When nil, only the properties directly given in the current entry count.
  1928. When t, every property is inherited. The value may also be a list of
  1929. properties that should have inheritance, or a regular expression matching
  1930. properties that should be inherited.
  1931. However, note that some special properties use inheritance under special
  1932. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1933. and the properties ending in \"_ALL\" when they are used as descriptor
  1934. for valid values of a property.
  1935. Note for programmers:
  1936. When querying an entry with `org-entry-get', you can control if inheritance
  1937. should be used. By default, `org-entry-get' looks only at the local
  1938. properties. You can request inheritance by setting the inherit argument
  1939. to t (to force inheritance) or to `selective' (to respect the setting
  1940. in this variable)."
  1941. :group 'org-properties
  1942. :type '(choice
  1943. (const :tag "Not" nil)
  1944. (const :tag "Always" t)
  1945. (repeat :tag "Specific properties" (string :tag "Property"))
  1946. (regexp :tag "Properties matched by regexp")))
  1947. (defun org-property-inherit-p (property)
  1948. "Check if PROPERTY is one that should be inherited."
  1949. (cond
  1950. ((eq org-use-property-inheritance t) t)
  1951. ((not org-use-property-inheritance) nil)
  1952. ((stringp org-use-property-inheritance)
  1953. (string-match org-use-property-inheritance property))
  1954. ((listp org-use-property-inheritance)
  1955. (member property org-use-property-inheritance))
  1956. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1957. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1958. "The default column format, if no other format has been defined.
  1959. This variable can be set on the per-file basis by inserting a line
  1960. #+COLUMNS: %25ITEM ....."
  1961. :group 'org-properties
  1962. :type 'string)
  1963. (defcustom org-columns-ellipses ".."
  1964. "The ellipses to be used when a field in column view is truncated.
  1965. When this is the empty string, as many characters as possible are shown,
  1966. but then there will be no visual indication that the field has been truncated.
  1967. When this is a string of length N, the last N characters of a truncated
  1968. field are replaced by this string. If the column is narrower than the
  1969. ellipses string, only part of the ellipses string will be shown."
  1970. :group 'org-properties
  1971. :type 'string)
  1972. (defcustom org-columns-modify-value-for-display-function nil
  1973. "Function that modifies values for display in column view.
  1974. For example, it can be used to cut out a certain part from a time stamp.
  1975. The function must take 2 arguments:
  1976. column-title The title of the column (*not* the property name)
  1977. value The value that should be modified.
  1978. The function should return the value that should be displayed,
  1979. or nil if the normal value should be used."
  1980. :group 'org-properties
  1981. :type 'function)
  1982. (defcustom org-effort-property "Effort"
  1983. "The property that is being used to keep track of effort estimates.
  1984. Effort estimates given in this property need to have the format H:MM."
  1985. :group 'org-properties
  1986. :group 'org-progress
  1987. :type '(string :tag "Property"))
  1988. (defconst org-global-properties-fixed
  1989. '(("VISIBILITY_ALL" . "folded children content all"))
  1990. "List of property/value pairs that can be inherited by any entry.
  1991. These are fixed values, for the preset properties.")
  1992. (defcustom org-global-properties nil
  1993. "List of property/value pairs that can be inherited by any entry.
  1994. You can set buffer-local values for the same purpose in the variable
  1995. `org-file-properties' this by adding lines like
  1996. #+PROPERTY: NAME VALUE"
  1997. :group 'org-properties
  1998. :type '(repeat
  1999. (cons (string :tag "Property")
  2000. (string :tag "Value"))))
  2001. (defvar org-file-properties nil
  2002. "List of property/value pairs that can be inherited by any entry.
  2003. Valid for the current buffer.
  2004. This variable is populated from #+PROPERTY lines.")
  2005. (make-variable-buffer-local 'org-file-properties)
  2006. (defgroup org-agenda nil
  2007. "Options concerning agenda views in Org-mode."
  2008. :tag "Org Agenda"
  2009. :group 'org)
  2010. (defvar org-category nil
  2011. "Variable used by org files to set a category for agenda display.
  2012. Such files should use a file variable to set it, for example
  2013. # -*- mode: org; org-category: \"ELisp\"
  2014. or contain a special line
  2015. #+CATEGORY: ELisp
  2016. If the file does not specify a category, then file's base name
  2017. is used instead.")
  2018. (make-variable-buffer-local 'org-category)
  2019. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2020. (defcustom org-agenda-files nil
  2021. "The files to be used for agenda display.
  2022. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2023. \\[org-remove-file]. You can also use customize to edit the list.
  2024. If an entry is a directory, all files in that directory that are matched by
  2025. `org-agenda-file-regexp' will be part of the file list.
  2026. If the value of the variable is not a list but a single file name, then
  2027. the list of agenda files is actually stored and maintained in that file, one
  2028. agenda file per line."
  2029. :group 'org-agenda
  2030. :type '(choice
  2031. (repeat :tag "List of files and directories" file)
  2032. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2033. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2034. "Regular expression to match files for `org-agenda-files'.
  2035. If any element in the list in that variable contains a directory instead
  2036. of a normal file, all files in that directory that are matched by this
  2037. regular expression will be included."
  2038. :group 'org-agenda
  2039. :type 'regexp)
  2040. (defcustom org-agenda-text-search-extra-files nil
  2041. "List of extra files to be searched by text search commands.
  2042. These files will be search in addition to the agenda files by the
  2043. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2044. Note that these files will only be searched for text search commands,
  2045. not for the other agenda views like todo lists, tag searches or the weekly
  2046. agenda. This variable is intended to list notes and possibly archive files
  2047. that should also be searched by these two commands.
  2048. In fact, if the first element in the list is the symbol `agenda-archives',
  2049. than all archive files of all agenda files will be added to the search
  2050. scope."
  2051. :group 'org-agenda
  2052. :type '(set :greedy t
  2053. (const :tag "Agenda Archives" agenda-archives)
  2054. (repeat :inline t (file))))
  2055. (if (fboundp 'defvaralias)
  2056. (defvaralias 'org-agenda-multi-occur-extra-files
  2057. 'org-agenda-text-search-extra-files))
  2058. (defcustom org-agenda-skip-unavailable-files nil
  2059. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2060. A nil value means to remove them, after a query, from the list."
  2061. :group 'org-agenda
  2062. :type 'boolean)
  2063. (defcustom org-calendar-to-agenda-key [?c]
  2064. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2065. The command `org-calendar-goto-agenda' will be bound to this key. The
  2066. default is the character `c' because then `c' can be used to switch back and
  2067. forth between agenda and calendar."
  2068. :group 'org-agenda
  2069. :type 'sexp)
  2070. (defcustom org-calendar-agenda-action-key [?k]
  2071. "The key to be installed in `calendar-mode-map' for agenda-action.
  2072. The command `org-agenda-action' will be bound to this key. The
  2073. default is the character `k' because we use the same key in the agenda."
  2074. :group 'org-agenda
  2075. :type 'sexp)
  2076. (eval-after-load "calendar"
  2077. '(progn
  2078. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2079. 'org-calendar-goto-agenda)
  2080. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2081. 'org-agenda-action)))
  2082. (defgroup org-latex nil
  2083. "Options for embedding LaTeX code into Org-mode."
  2084. :tag "Org LaTeX"
  2085. :group 'org)
  2086. (defcustom org-format-latex-options
  2087. '(:foreground default :background default :scale 1.0
  2088. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2089. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2090. "Options for creating images from LaTeX fragments.
  2091. This is a property list with the following properties:
  2092. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2093. `default' means use the foreground of the default face.
  2094. :background the background color, or \"Transparent\".
  2095. `default' means use the background of the default face.
  2096. :scale a scaling factor for the size of the images.
  2097. :html-foreground, :html-background, :html-scale
  2098. the same numbers for HTML export.
  2099. :matchers a list indicating which matchers should be used to
  2100. find LaTeX fragments. Valid members of this list are:
  2101. \"begin\" find environments
  2102. \"$1\" find single characters surrounded by $.$
  2103. \"$\" find math expressions surrounded by $...$
  2104. \"$$\" find math expressions surrounded by $$....$$
  2105. \"\\(\" find math expressions surrounded by \\(...\\)
  2106. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2107. :group 'org-latex
  2108. :type 'plist)
  2109. (defcustom org-format-latex-header "\\documentclass{article}
  2110. \\usepackage{fullpage} % do not remove
  2111. \\usepackage{amssymb}
  2112. \\usepackage[usenames]{color}
  2113. \\usepackage{amsmath}
  2114. \\usepackage{latexsym}
  2115. \\usepackage[mathscr]{eucal}
  2116. \\pagestyle{empty} % do not remove"
  2117. "The document header used for processing LaTeX fragments."
  2118. :group 'org-latex
  2119. :type 'string)
  2120. (defgroup org-font-lock nil
  2121. "Font-lock settings for highlighting in Org-mode."
  2122. :tag "Org Font Lock"
  2123. :group 'org)
  2124. (defcustom org-level-color-stars-only nil
  2125. "Non-nil means fontify only the stars in each headline.
  2126. When nil, the entire headline is fontified.
  2127. Changing it requires restart of `font-lock-mode' to become effective
  2128. also in regions already fontified."
  2129. :group 'org-font-lock
  2130. :type 'boolean)
  2131. (defcustom org-hide-leading-stars nil
  2132. "Non-nil means, hide the first N-1 stars in a headline.
  2133. This works by using the face `org-hide' for these stars. This
  2134. face is white for a light background, and black for a dark
  2135. background. You may have to customize the face `org-hide' to
  2136. make this work.
  2137. Changing it requires restart of `font-lock-mode' to become effective
  2138. also in regions already fontified.
  2139. You may also set this on a per-file basis by adding one of the following
  2140. lines to the buffer:
  2141. #+STARTUP: hidestars
  2142. #+STARTUP: showstars"
  2143. :group 'org-font-lock
  2144. :type 'boolean)
  2145. (defcustom org-fontify-done-headline nil
  2146. "Non-nil means, change the face of a headline if it is marked DONE.
  2147. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2148. When this is non-nil, the headline after the keyword is set to the
  2149. `org-headline-done' as an additional indication."
  2150. :group 'org-font-lock
  2151. :type 'boolean)
  2152. (defcustom org-fontify-emphasized-text t
  2153. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2154. Changing this variable requires a restart of Emacs to take effect."
  2155. :group 'org-font-lock
  2156. :type 'boolean)
  2157. (defcustom org-highlight-latex-fragments-and-specials nil
  2158. "Non-nil means, fontify what is treated specially by the exporters."
  2159. :group 'org-font-lock
  2160. :type 'boolean)
  2161. (defcustom org-hide-emphasis-markers nil
  2162. "Non-nil mean font-lock should hide the emphasis marker characters."
  2163. :group 'org-font-lock
  2164. :type 'boolean)
  2165. (defvar org-emph-re nil
  2166. "Regular expression for matching emphasis.")
  2167. (defvar org-verbatim-re nil
  2168. "Regular expression for matching verbatim text.")
  2169. (defvar org-emphasis-regexp-components) ; defined just below
  2170. (defvar org-emphasis-alist) ; defined just below
  2171. (defun org-set-emph-re (var val)
  2172. "Set variable and compute the emphasis regular expression."
  2173. (set var val)
  2174. (when (and (boundp 'org-emphasis-alist)
  2175. (boundp 'org-emphasis-regexp-components)
  2176. org-emphasis-alist org-emphasis-regexp-components)
  2177. (let* ((e org-emphasis-regexp-components)
  2178. (pre (car e))
  2179. (post (nth 1 e))
  2180. (border (nth 2 e))
  2181. (body (nth 3 e))
  2182. (nl (nth 4 e))
  2183. (body1 (concat body "*?"))
  2184. (markers (mapconcat 'car org-emphasis-alist ""))
  2185. (vmarkers (mapconcat
  2186. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2187. org-emphasis-alist "")))
  2188. ;; make sure special characters appear at the right position in the class
  2189. (if (string-match "\\^" markers)
  2190. (setq markers (concat (replace-match "" t t markers) "^")))
  2191. (if (string-match "-" markers)
  2192. (setq markers (concat (replace-match "" t t markers) "-")))
  2193. (if (string-match "\\^" vmarkers)
  2194. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2195. (if (string-match "-" vmarkers)
  2196. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2197. (if (> nl 0)
  2198. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2199. (int-to-string nl) "\\}")))
  2200. ;; Make the regexp
  2201. (setq org-emph-re
  2202. (concat "\\([" pre "]\\|^\\)"
  2203. "\\("
  2204. "\\([" markers "]\\)"
  2205. "\\("
  2206. "[^" border "]\\|"
  2207. "[^" border "]"
  2208. body1
  2209. "[^" border "]"
  2210. "\\)"
  2211. "\\3\\)"
  2212. "\\([" post "]\\|$\\)"))
  2213. (setq org-verbatim-re
  2214. (concat "\\([" pre "]\\|^\\)"
  2215. "\\("
  2216. "\\([" vmarkers "]\\)"
  2217. "\\("
  2218. "[^" border "]\\|"
  2219. "[^" border "]"
  2220. body1
  2221. "[^" border "]"
  2222. "\\)"
  2223. "\\3\\)"
  2224. "\\([" post "]\\|$\\)")))))
  2225. (defcustom org-emphasis-regexp-components
  2226. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2227. "Components used to build the regular expression for emphasis.
  2228. This is a list with 6 entries. Terminology: In an emphasis string
  2229. like \" *strong word* \", we call the initial space PREMATCH, the final
  2230. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2231. and \"trong wor\" is the body. The different components in this variable
  2232. specify what is allowed/forbidden in each part:
  2233. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2234. post Chars allowed as postmatch. End of line will be allowed too.
  2235. border The chars *forbidden* as border characters.
  2236. body-regexp A regexp like \".\" to match a body character. Don't use
  2237. non-shy groups here, and don't allow newline here.
  2238. newline The maximum number of newlines allowed in an emphasis exp.
  2239. Use customize to modify this, or restart Emacs after changing it."
  2240. :group 'org-font-lock
  2241. :set 'org-set-emph-re
  2242. :type '(list
  2243. (sexp :tag "Allowed chars in pre ")
  2244. (sexp :tag "Allowed chars in post ")
  2245. (sexp :tag "Forbidden chars in border ")
  2246. (sexp :tag "Regexp for body ")
  2247. (integer :tag "number of newlines allowed")
  2248. (option (boolean :tag "Please ignore this button"))))
  2249. (defcustom org-emphasis-alist
  2250. `(("*" bold "<b>" "</b>")
  2251. ("/" italic "<i>" "</i>")
  2252. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2253. ("=" org-code "<code>" "</code>" verbatim)
  2254. ("~" org-verbatim "<code>" "</code>" verbatim)
  2255. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2256. "<del>" "</del>")
  2257. )
  2258. "Special syntax for emphasized text.
  2259. Text starting and ending with a special character will be emphasized, for
  2260. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2261. characters, the face to be used by font-lock for highlighting in Org-mode
  2262. Emacs buffers, and the HTML tags to be used for this.
  2263. Use customize to modify this, or restart Emacs after changing it."
  2264. :group 'org-font-lock
  2265. :set 'org-set-emph-re
  2266. :type '(repeat
  2267. (list
  2268. (string :tag "Marker character")
  2269. (choice
  2270. (face :tag "Font-lock-face")
  2271. (plist :tag "Face property list"))
  2272. (string :tag "HTML start tag")
  2273. (string :tag "HTML end tag")
  2274. (option (const verbatim)))))
  2275. ;;; Miscellaneous options
  2276. (defgroup org-completion nil
  2277. "Completion in Org-mode."
  2278. :tag "Org Completion"
  2279. :group 'org)
  2280. (defcustom org-completion-use-ido nil
  2281. "Non-nil means, use ido completion wherever possible.
  2282. Note that `ido-mode' must be active for this variable to be relevant.
  2283. If you decide to turn this variable on, you might well want to turn off
  2284. `org-outline-path-complete-in-steps'."
  2285. :group 'org-completion
  2286. :type 'boolean)
  2287. (defcustom org-completion-fallback-command 'hippie-expand
  2288. "The expansion command called by \\[org-complete] in normal context.
  2289. Normal means, no org-mode-specific context."
  2290. :group 'org-completion
  2291. :type 'function)
  2292. ;;; Functions and variables from ther packages
  2293. ;; Declared here to avoid compiler warnings
  2294. ;; XEmacs only
  2295. (defvar outline-mode-menu-heading)
  2296. (defvar outline-mode-menu-show)
  2297. (defvar outline-mode-menu-hide)
  2298. (defvar zmacs-regions) ; XEmacs regions
  2299. ;; Emacs only
  2300. (defvar mark-active)
  2301. ;; Various packages
  2302. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2303. (declare-function calendar-forward-day "cal-move" (arg))
  2304. (declare-function calendar-goto-date "cal-move" (date))
  2305. (declare-function calendar-goto-today "cal-move" ())
  2306. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2307. (defvar calc-embedded-close-formula)
  2308. (defvar calc-embedded-open-formula)
  2309. (declare-function cdlatex-tab "ext:cdlatex" ())
  2310. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2311. (defvar font-lock-unfontify-region-function)
  2312. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2313. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2314. (defvar iswitchb-temp-buflist)
  2315. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2316. (defvar org-agenda-tags-todo-honor-ignore-options)
  2317. (declare-function org-agenda-skip "org-agenda" ())
  2318. (declare-function org-format-agenda-item "org-agenda"
  2319. (extra txt &optional category tags dotime noprefix remove-re))
  2320. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2321. (declare-function org-agenda-change-all-lines "org-agenda"
  2322. (newhead hdmarker &optional fixface just-this))
  2323. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2324. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2325. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2326. (beg end))
  2327. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2328. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2329. "org-agenda" (&optional end))
  2330. (declare-function parse-time-string "parse-time" (string))
  2331. (declare-function remember "remember" (&optional initial))
  2332. (declare-function remember-buffer-desc "remember" ())
  2333. (declare-function remember-finalize "remember" ())
  2334. (defvar remember-save-after-remembering)
  2335. (defvar remember-data-file)
  2336. (defvar remember-register)
  2337. (defvar remember-buffer)
  2338. (defvar remember-handler-functions)
  2339. (defvar remember-annotation-functions)
  2340. (defvar texmathp-why)
  2341. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2342. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2343. (defvar w3m-current-url)
  2344. (defvar w3m-current-title)
  2345. (defvar org-latex-regexps)
  2346. ;;; Autoload and prepare some org modules
  2347. ;; Some table stuff that needs to be defined here, because it is used
  2348. ;; by the functions setting up org-mode or checking for table context.
  2349. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2350. "Detects an org-type or table-type table.")
  2351. (defconst org-table-line-regexp "^[ \t]*|"
  2352. "Detects an org-type table line.")
  2353. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2354. "Detects an org-type table line.")
  2355. (defconst org-table-hline-regexp "^[ \t]*|-"
  2356. "Detects an org-type table hline.")
  2357. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2358. "Detects a table-type table hline.")
  2359. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2360. "Searching from within a table (any type) this finds the first line
  2361. outside the table.")
  2362. ;; Autoload the functions in org-table.el that are needed by functions here.
  2363. (eval-and-compile
  2364. (org-autoload "org-table"
  2365. '(org-table-align org-table-begin org-table-blank-field
  2366. org-table-convert org-table-convert-region org-table-copy-down
  2367. org-table-copy-region org-table-create
  2368. org-table-create-or-convert-from-region
  2369. org-table-create-with-table.el org-table-current-dline
  2370. org-table-cut-region org-table-delete-column org-table-edit-field
  2371. org-table-edit-formulas org-table-end org-table-eval-formula
  2372. org-table-export org-table-field-info
  2373. org-table-get-stored-formulas org-table-goto-column
  2374. org-table-hline-and-move org-table-import org-table-insert-column
  2375. org-table-insert-hline org-table-insert-row org-table-iterate
  2376. org-table-justify-field-maybe org-table-kill-row
  2377. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2378. org-table-move-column org-table-move-column-left
  2379. org-table-move-column-right org-table-move-row
  2380. org-table-move-row-down org-table-move-row-up
  2381. org-table-next-field org-table-next-row org-table-paste-rectangle
  2382. org-table-previous-field org-table-recalculate
  2383. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2384. org-table-toggle-coordinate-overlays
  2385. org-table-toggle-formula-debugger org-table-wrap-region
  2386. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2387. (defun org-at-table-p (&optional table-type)
  2388. "Return t if the cursor is inside an org-type table.
  2389. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2390. (if org-enable-table-editor
  2391. (save-excursion
  2392. (beginning-of-line 1)
  2393. (looking-at (if table-type org-table-any-line-regexp
  2394. org-table-line-regexp)))
  2395. nil))
  2396. (defsubst org-table-p () (org-at-table-p))
  2397. (defun org-at-table.el-p ()
  2398. "Return t if and only if we are at a table.el table."
  2399. (and (org-at-table-p 'any)
  2400. (save-excursion
  2401. (goto-char (org-table-begin 'any))
  2402. (looking-at org-table1-hline-regexp))))
  2403. (defun org-table-recognize-table.el ()
  2404. "If there is a table.el table nearby, recognize it and move into it."
  2405. (if org-table-tab-recognizes-table.el
  2406. (if (org-at-table.el-p)
  2407. (progn
  2408. (beginning-of-line 1)
  2409. (if (looking-at org-table-dataline-regexp)
  2410. nil
  2411. (if (looking-at org-table1-hline-regexp)
  2412. (progn
  2413. (beginning-of-line 2)
  2414. (if (looking-at org-table-any-border-regexp)
  2415. (beginning-of-line -1)))))
  2416. (if (re-search-forward "|" (org-table-end t) t)
  2417. (progn
  2418. (require 'table)
  2419. (if (table--at-cell-p (point))
  2420. t
  2421. (message "recognizing table.el table...")
  2422. (table-recognize-table)
  2423. (message "recognizing table.el table...done")))
  2424. (error "This should not happen..."))
  2425. t)
  2426. nil)
  2427. nil))
  2428. (defun org-at-table-hline-p ()
  2429. "Return t if the cursor is inside a hline in a table."
  2430. (if org-enable-table-editor
  2431. (save-excursion
  2432. (beginning-of-line 1)
  2433. (looking-at org-table-hline-regexp))
  2434. nil))
  2435. (defvar org-table-clean-did-remove-column nil)
  2436. (defun org-table-map-tables (function)
  2437. "Apply FUNCTION to the start of all tables in the buffer."
  2438. (save-excursion
  2439. (save-restriction
  2440. (widen)
  2441. (goto-char (point-min))
  2442. (while (re-search-forward org-table-any-line-regexp nil t)
  2443. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2444. (beginning-of-line 1)
  2445. (if (looking-at org-table-line-regexp)
  2446. (save-excursion (funcall function)))
  2447. (re-search-forward org-table-any-border-regexp nil 1))))
  2448. (message "Mapping tables: done"))
  2449. ;; Declare and autoload functions from org-exp.el
  2450. (declare-function org-default-export-plist "org-exp")
  2451. (declare-function org-infile-export-plist "org-exp")
  2452. (declare-function org-get-current-options "org-exp")
  2453. (eval-and-compile
  2454. (org-autoload "org-exp"
  2455. '(org-export org-export-as-ascii org-export-visible
  2456. org-insert-export-options-template org-export-as-html-and-open
  2457. org-export-as-html-batch org-export-as-html-to-buffer
  2458. org-replace-region-by-html org-export-region-as-html
  2459. org-export-as-html org-export-icalendar-this-file
  2460. org-export-icalendar-all-agenda-files
  2461. org-table-clean-before-export
  2462. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2463. ;; Declare and autoload functions from org-agenda.el
  2464. (eval-and-compile
  2465. (org-autoload "org-agenda"
  2466. '(org-agenda org-agenda-list org-search-view
  2467. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2468. org-diary org-agenda-to-appt
  2469. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2470. ;; Autoload org-remember
  2471. (eval-and-compile
  2472. (org-autoload "org-remember"
  2473. '(org-remember-insinuate org-remember-annotation
  2474. org-remember-apply-template org-remember org-remember-handler)))
  2475. ;; Autoload org-clock.el
  2476. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2477. (beg end))
  2478. (declare-function org-clock-update-mode-line "org-clock" ())
  2479. (defvar org-clock-start-time)
  2480. (defvar org-clock-marker (make-marker)
  2481. "Marker recording the last clock-in.")
  2482. (eval-and-compile
  2483. (org-autoload
  2484. "org-clock"
  2485. '(org-clock-in org-clock-out org-clock-cancel
  2486. org-clock-goto org-clock-sum org-clock-display
  2487. org-clock-remove-overlays org-clock-report
  2488. org-clocktable-shift org-dblock-write:clocktable
  2489. org-get-clocktable)))
  2490. (defun org-clock-update-time-maybe ()
  2491. "If this is a CLOCK line, update it and return t.
  2492. Otherwise, return nil."
  2493. (interactive)
  2494. (save-excursion
  2495. (beginning-of-line 1)
  2496. (skip-chars-forward " \t")
  2497. (when (looking-at org-clock-string)
  2498. (let ((re (concat "[ \t]*" org-clock-string
  2499. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2500. "\\([ \t]*=>.*\\)?\\)?"))
  2501. ts te h m s neg)
  2502. (cond
  2503. ((not (looking-at re))
  2504. nil)
  2505. ((not (match-end 2))
  2506. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2507. (> org-clock-marker (point))
  2508. (<= org-clock-marker (point-at-eol)))
  2509. ;; The clock is running here
  2510. (setq org-clock-start-time
  2511. (apply 'encode-time
  2512. (org-parse-time-string (match-string 1))))
  2513. (org-clock-update-mode-line)))
  2514. (t
  2515. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2516. (end-of-line 1)
  2517. (setq ts (match-string 1)
  2518. te (match-string 3))
  2519. (setq s (- (time-to-seconds
  2520. (apply 'encode-time (org-parse-time-string te)))
  2521. (time-to-seconds
  2522. (apply 'encode-time (org-parse-time-string ts))))
  2523. neg (< s 0)
  2524. s (abs s)
  2525. h (floor (/ s 3600))
  2526. s (- s (* 3600 h))
  2527. m (floor (/ s 60))
  2528. s (- s (* 60 s)))
  2529. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2530. t))))))
  2531. (defun org-check-running-clock ()
  2532. "Check if the current buffer contains the running clock.
  2533. If yes, offer to stop it and to save the buffer with the changes."
  2534. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2535. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2536. (buffer-name))))
  2537. (org-clock-out)
  2538. (when (y-or-n-p "Save changed buffer?")
  2539. (save-buffer))))
  2540. (defun org-clocktable-try-shift (dir n)
  2541. "Check if this line starts a clock table, if yes, shift the time block."
  2542. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2543. (org-clocktable-shift dir n)))
  2544. ;; Autoload org-timer.el
  2545. ;(declare-function org-timer "org-timer")
  2546. (eval-and-compile
  2547. (org-autoload
  2548. "org-timer"
  2549. '(org-timer-start org-timer org-timer-item
  2550. org-timer-change-times-in-region)))
  2551. ;; Autoload archiving code
  2552. ;; The stuff that is needed for cycling and tags has to be defined here.
  2553. (defgroup org-archive nil
  2554. "Options concerning archiving in Org-mode."
  2555. :tag "Org Archive"
  2556. :group 'org-structure)
  2557. (defcustom org-archive-location "%s_archive::"
  2558. "The location where subtrees should be archived.
  2559. The value of this variable is a string, consisting of two parts,
  2560. separated by a double-colon. The first part is a filename and
  2561. the second part is a headline.
  2562. When the filename is omitted, archiving happens in the same file.
  2563. %s in the filename will be replaced by the current file
  2564. name (without the directory part). Archiving to a different file
  2565. is useful to keep archived entries from contributing to the
  2566. Org-mode Agenda.
  2567. The archived entries will be filed as subtrees of the specified
  2568. headline. When the headline is omitted, the subtrees are simply
  2569. filed away at the end of the file, as top-level entries. Also in
  2570. the heading you can use %s to represent the file name, this can be
  2571. useful when using the same archive for a number of different files.
  2572. Here are a few examples:
  2573. \"%s_archive::\"
  2574. If the current file is Projects.org, archive in file
  2575. Projects.org_archive, as top-level trees. This is the default.
  2576. \"::* Archived Tasks\"
  2577. Archive in the current file, under the top-level headline
  2578. \"* Archived Tasks\".
  2579. \"~/org/archive.org::\"
  2580. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2581. \"~/org/archive.org::From %s\"
  2582. Archive in file ~/org/archive.org (absolute path), und headlines
  2583. \"From FILENAME\" where file name is the current file name.
  2584. \"basement::** Finished Tasks\"
  2585. Archive in file ./basement (relative path), as level 3 trees
  2586. below the level 2 heading \"** Finished Tasks\".
  2587. You may set this option on a per-file basis by adding to the buffer a
  2588. line like
  2589. #+ARCHIVE: basement::** Finished Tasks
  2590. You may also define it locally for a subtree by setting an ARCHIVE property
  2591. in the entry. If such a property is found in an entry, or anywhere up
  2592. the hierarchy, it will be used."
  2593. :group 'org-archive
  2594. :type 'string)
  2595. (defcustom org-archive-tag "ARCHIVE"
  2596. "The tag that marks a subtree as archived.
  2597. An archived subtree does not open during visibility cycling, and does
  2598. not contribute to the agenda listings.
  2599. After changing this, font-lock must be restarted in the relevant buffers to
  2600. get the proper fontification."
  2601. :group 'org-archive
  2602. :group 'org-keywords
  2603. :type 'string)
  2604. (defcustom org-agenda-skip-archived-trees t
  2605. "Non-nil means, the agenda will skip any items located in archived trees.
  2606. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2607. variable is no longer recommended, you should leave it at the value t.
  2608. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2609. :group 'org-archive
  2610. :group 'org-agenda-skip
  2611. :type 'boolean)
  2612. (defcustom org-cycle-open-archived-trees nil
  2613. "Non-nil means, `org-cycle' will open archived trees.
  2614. An archived tree is a tree marked with the tag ARCHIVE.
  2615. When nil, archived trees will stay folded. You can still open them with
  2616. normal outline commands like `show-all', but not with the cycling commands."
  2617. :group 'org-archive
  2618. :group 'org-cycle
  2619. :type 'boolean)
  2620. (defcustom org-sparse-tree-open-archived-trees nil
  2621. "Non-nil means sparse tree construction shows matches in archived trees.
  2622. When nil, matches in these trees are highlighted, but the trees are kept in
  2623. collapsed state."
  2624. :group 'org-archive
  2625. :group 'org-sparse-trees
  2626. :type 'boolean)
  2627. (defun org-cycle-hide-archived-subtrees (state)
  2628. "Re-hide all archived subtrees after a visibility state change."
  2629. (when (and (not org-cycle-open-archived-trees)
  2630. (not (memq state '(overview folded))))
  2631. (save-excursion
  2632. (let* ((globalp (memq state '(contents all)))
  2633. (beg (if globalp (point-min) (point)))
  2634. (end (if globalp (point-max) (org-end-of-subtree t))))
  2635. (org-hide-archived-subtrees beg end)
  2636. (goto-char beg)
  2637. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2638. (message "%s" (substitute-command-keys
  2639. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2640. (defun org-force-cycle-archived ()
  2641. "Cycle subtree even if it is archived."
  2642. (interactive)
  2643. (setq this-command 'org-cycle)
  2644. (let ((org-cycle-open-archived-trees t))
  2645. (call-interactively 'org-cycle)))
  2646. (defun org-hide-archived-subtrees (beg end)
  2647. "Re-hide all archived subtrees after a visibility state change."
  2648. (save-excursion
  2649. (let* ((re (concat ":" org-archive-tag ":")))
  2650. (goto-char beg)
  2651. (while (re-search-forward re end t)
  2652. (and (org-on-heading-p) (hide-subtree))
  2653. (org-end-of-subtree t)))))
  2654. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2655. (eval-and-compile
  2656. (org-autoload "org-archive"
  2657. '(org-add-archive-files org-archive-subtree
  2658. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2659. ;; Autoload Column View Code
  2660. (declare-function org-columns-number-to-string "org-colview")
  2661. (declare-function org-columns-get-format-and-top-level "org-colview")
  2662. (declare-function org-columns-compute "org-colview")
  2663. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2664. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2665. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2666. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2667. ;; Autoload ID code
  2668. (declare-function org-id-store-link "org-id")
  2669. (org-autoload "org-id"
  2670. '(org-id-get-create org-id-new org-id-copy org-id-get
  2671. org-id-get-with-outline-path-completion
  2672. org-id-get-with-outline-drilling
  2673. org-id-goto org-id-find org-id-store-link))
  2674. ;; Autoload Plotting Code
  2675. (org-autoload "org-plot"
  2676. '(org-plot/gnuplot))
  2677. ;;; Variables for pre-computed regular expressions, all buffer local
  2678. (defvar org-drawer-regexp nil
  2679. "Matches first line of a hidden block.")
  2680. (make-variable-buffer-local 'org-drawer-regexp)
  2681. (defvar org-todo-regexp nil
  2682. "Matches any of the TODO state keywords.")
  2683. (make-variable-buffer-local 'org-todo-regexp)
  2684. (defvar org-not-done-regexp nil
  2685. "Matches any of the TODO state keywords except the last one.")
  2686. (make-variable-buffer-local 'org-not-done-regexp)
  2687. (defvar org-todo-line-regexp nil
  2688. "Matches a headline and puts TODO state into group 2 if present.")
  2689. (make-variable-buffer-local 'org-todo-line-regexp)
  2690. (defvar org-complex-heading-regexp nil
  2691. "Matches a headline and puts everything into groups:
  2692. group 1: the stars
  2693. group 2: The todo keyword, maybe
  2694. group 3: Priority cookie
  2695. group 4: True headline
  2696. group 5: Tags")
  2697. (make-variable-buffer-local 'org-complex-heading-regexp)
  2698. (defvar org-todo-line-tags-regexp nil
  2699. "Matches a headline and puts TODO state into group 2 if present.
  2700. Also put tags into group 4 if tags are present.")
  2701. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2702. (defvar org-nl-done-regexp nil
  2703. "Matches newline followed by a headline with the DONE keyword.")
  2704. (make-variable-buffer-local 'org-nl-done-regexp)
  2705. (defvar org-looking-at-done-regexp nil
  2706. "Matches the DONE keyword a point.")
  2707. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2708. (defvar org-ds-keyword-length 12
  2709. "Maximum length of the Deadline and SCHEDULED keywords.")
  2710. (make-variable-buffer-local 'org-ds-keyword-length)
  2711. (defvar org-deadline-regexp nil
  2712. "Matches the DEADLINE keyword.")
  2713. (make-variable-buffer-local 'org-deadline-regexp)
  2714. (defvar org-deadline-time-regexp nil
  2715. "Matches the DEADLINE keyword together with a time stamp.")
  2716. (make-variable-buffer-local 'org-deadline-time-regexp)
  2717. (defvar org-deadline-line-regexp nil
  2718. "Matches the DEADLINE keyword and the rest of the line.")
  2719. (make-variable-buffer-local 'org-deadline-line-regexp)
  2720. (defvar org-scheduled-regexp nil
  2721. "Matches the SCHEDULED keyword.")
  2722. (make-variable-buffer-local 'org-scheduled-regexp)
  2723. (defvar org-scheduled-time-regexp nil
  2724. "Matches the SCHEDULED keyword together with a time stamp.")
  2725. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2726. (defvar org-closed-time-regexp nil
  2727. "Matches the CLOSED keyword together with a time stamp.")
  2728. (make-variable-buffer-local 'org-closed-time-regexp)
  2729. (defvar org-keyword-time-regexp nil
  2730. "Matches any of the 4 keywords, together with the time stamp.")
  2731. (make-variable-buffer-local 'org-keyword-time-regexp)
  2732. (defvar org-keyword-time-not-clock-regexp nil
  2733. "Matches any of the 3 keywords, together with the time stamp.")
  2734. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2735. (defvar org-maybe-keyword-time-regexp nil
  2736. "Matches a timestamp, possibly preceeded by a keyword.")
  2737. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2738. (defvar org-planning-or-clock-line-re nil
  2739. "Matches a line with planning or clock info.")
  2740. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2741. (defconst org-plain-time-of-day-regexp
  2742. (concat
  2743. "\\(\\<[012]?[0-9]"
  2744. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2745. "\\(--?"
  2746. "\\(\\<[012]?[0-9]"
  2747. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2748. "\\)?")
  2749. "Regular expression to match a plain time or time range.
  2750. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2751. groups carry important information:
  2752. 0 the full match
  2753. 1 the first time, range or not
  2754. 8 the second time, if it is a range.")
  2755. (defconst org-plain-time-extension-regexp
  2756. (concat
  2757. "\\(\\<[012]?[0-9]"
  2758. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2759. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2760. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2761. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2762. groups carry important information:
  2763. 0 the full match
  2764. 7 hours of duration
  2765. 9 minutes of duration")
  2766. (defconst org-stamp-time-of-day-regexp
  2767. (concat
  2768. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2769. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2770. "\\(--?"
  2771. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2772. "Regular expression to match a timestamp time or time range.
  2773. After a match, the following groups carry important information:
  2774. 0 the full match
  2775. 1 date plus weekday, for backreferencing to make sure both times on same day
  2776. 2 the first time, range or not
  2777. 4 the second time, if it is a range.")
  2778. (defconst org-startup-options
  2779. '(("fold" org-startup-folded t)
  2780. ("overview" org-startup-folded t)
  2781. ("nofold" org-startup-folded nil)
  2782. ("showall" org-startup-folded nil)
  2783. ("content" org-startup-folded content)
  2784. ("hidestars" org-hide-leading-stars t)
  2785. ("showstars" org-hide-leading-stars nil)
  2786. ("odd" org-odd-levels-only t)
  2787. ("oddeven" org-odd-levels-only nil)
  2788. ("align" org-startup-align-all-tables t)
  2789. ("noalign" org-startup-align-all-tables nil)
  2790. ("customtime" org-display-custom-times t)
  2791. ("logdone" org-log-done time)
  2792. ("lognotedone" org-log-done note)
  2793. ("nologdone" org-log-done nil)
  2794. ("lognoteclock-out" org-log-note-clock-out t)
  2795. ("nolognoteclock-out" org-log-note-clock-out nil)
  2796. ("logrepeat" org-log-repeat state)
  2797. ("lognoterepeat" org-log-repeat note)
  2798. ("nologrepeat" org-log-repeat nil)
  2799. ("fninline" org-footnote-define-inline t)
  2800. ("nofninline" org-footnote-define-inline nil)
  2801. ("fnlocal" org-footnote-section nil)
  2802. ("fnauto" org-footnote-auto-label t)
  2803. ("fnprompt" org-footnote-auto-label nil)
  2804. ("fnconfirm" org-footnote-auto-label confirm)
  2805. ("fnplain" org-footnote-auto-label plain)
  2806. ("constcgs" constants-unit-system cgs)
  2807. ("constSI" constants-unit-system SI))
  2808. "Variable associated with STARTUP options for org-mode.
  2809. Each element is a list of three items: The startup options as written
  2810. in the #+STARTUP line, the corresponding variable, and the value to
  2811. set this variable to if the option is found. An optional forth element PUSH
  2812. means to push this value onto the list in the variable.")
  2813. (defun org-set-regexps-and-options ()
  2814. "Precompute regular expressions for current buffer."
  2815. (when (org-mode-p)
  2816. (org-set-local 'org-todo-kwd-alist nil)
  2817. (org-set-local 'org-todo-key-alist nil)
  2818. (org-set-local 'org-todo-key-trigger nil)
  2819. (org-set-local 'org-todo-keywords-1 nil)
  2820. (org-set-local 'org-done-keywords nil)
  2821. (org-set-local 'org-todo-heads nil)
  2822. (org-set-local 'org-todo-sets nil)
  2823. (org-set-local 'org-todo-log-states nil)
  2824. (org-set-local 'org-file-properties nil)
  2825. (org-set-local 'org-file-tags nil)
  2826. (let ((re (org-make-options-regexp
  2827. '("CATEGORY" "TODO" "COLUMNS"
  2828. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2829. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2830. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2831. (splitre "[ \t]+")
  2832. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2833. tail sep kws1 prio props ftags drawers
  2834. ext-setup-or-nil setup-contents (start 0))
  2835. (save-excursion
  2836. (save-restriction
  2837. (widen)
  2838. (goto-char (point-min))
  2839. (while (or (and ext-setup-or-nil
  2840. (string-match re ext-setup-or-nil start)
  2841. (setq start (match-end 0)))
  2842. (and (setq ext-setup-or-nil nil start 0)
  2843. (re-search-forward re nil t)))
  2844. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2845. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2846. (cond
  2847. ((equal key "CATEGORY")
  2848. (if (string-match "[ \t]+$" value)
  2849. (setq value (replace-match "" t t value)))
  2850. (setq cat value))
  2851. ((member key '("SEQ_TODO" "TODO"))
  2852. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2853. ((equal key "TYP_TODO")
  2854. (push (cons 'type (org-split-string value splitre)) kwds))
  2855. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2856. ;; general TODO-like setup
  2857. (push (cons (intern (downcase (match-string 1 key)))
  2858. (org-split-string value splitre)) kwds))
  2859. ((equal key "TAGS")
  2860. (setq tags (append tags (org-split-string value splitre))))
  2861. ((equal key "COLUMNS")
  2862. (org-set-local 'org-columns-default-format value))
  2863. ((equal key "LINK")
  2864. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2865. (push (cons (match-string 1 value)
  2866. (org-trim (match-string 2 value)))
  2867. links)))
  2868. ((equal key "PRIORITIES")
  2869. (setq prio (org-split-string value " +")))
  2870. ((equal key "PROPERTY")
  2871. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2872. (push (cons (match-string 1 value) (match-string 2 value))
  2873. props)))
  2874. ((equal key "FILETAGS")
  2875. (when (string-match "\\S-" value)
  2876. (setq ftags
  2877. (append
  2878. ftags
  2879. (apply 'append
  2880. (mapcar (lambda (x) (org-split-string x ":"))
  2881. (org-split-string value)))))))
  2882. ((equal key "DRAWERS")
  2883. (setq drawers (org-split-string value splitre)))
  2884. ((equal key "CONSTANTS")
  2885. (setq const (append const (org-split-string value splitre))))
  2886. ((equal key "STARTUP")
  2887. (let ((opts (org-split-string value splitre))
  2888. l var val)
  2889. (while (setq l (pop opts))
  2890. (when (setq l (assoc l org-startup-options))
  2891. (setq var (nth 1 l) val (nth 2 l))
  2892. (if (not (nth 3 l))
  2893. (set (make-local-variable var) val)
  2894. (if (not (listp (symbol-value var)))
  2895. (set (make-local-variable var) nil))
  2896. (set (make-local-variable var) (symbol-value var))
  2897. (add-to-list var val))))))
  2898. ((equal key "ARCHIVE")
  2899. (string-match " *$" value)
  2900. (setq arch (replace-match "" t t value))
  2901. (remove-text-properties 0 (length arch)
  2902. '(face t fontified t) arch))
  2903. ((equal key "SETUPFILE")
  2904. (setq setup-contents (org-file-contents
  2905. (expand-file-name
  2906. (org-remove-double-quotes value))
  2907. 'noerror))
  2908. (if (not ext-setup-or-nil)
  2909. (setq ext-setup-or-nil setup-contents start 0)
  2910. (setq ext-setup-or-nil
  2911. (concat (substring ext-setup-or-nil 0 start)
  2912. "\n" setup-contents "\n"
  2913. (substring ext-setup-or-nil start)))))
  2914. ))))
  2915. (when cat
  2916. (org-set-local 'org-category (intern cat))
  2917. (push (cons "CATEGORY" cat) props))
  2918. (when prio
  2919. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2920. (setq prio (mapcar 'string-to-char prio))
  2921. (org-set-local 'org-highest-priority (nth 0 prio))
  2922. (org-set-local 'org-lowest-priority (nth 1 prio))
  2923. (org-set-local 'org-default-priority (nth 2 prio)))
  2924. (and props (org-set-local 'org-file-properties (nreverse props)))
  2925. (and ftags (org-set-local 'org-file-tags ftags))
  2926. (and drawers (org-set-local 'org-drawers drawers))
  2927. (and arch (org-set-local 'org-archive-location arch))
  2928. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2929. ;; Process the TODO keywords
  2930. (unless kwds
  2931. ;; Use the global values as if they had been given locally.
  2932. (setq kwds (default-value 'org-todo-keywords))
  2933. (if (stringp (car kwds))
  2934. (setq kwds (list (cons org-todo-interpretation
  2935. (default-value 'org-todo-keywords)))))
  2936. (setq kwds (reverse kwds)))
  2937. (setq kwds (nreverse kwds))
  2938. (let (inter kws kw)
  2939. (while (setq kws (pop kwds))
  2940. (let ((kws (or
  2941. (run-hook-with-args-until-success
  2942. 'org-todo-setup-filter-hook kws)
  2943. kws)))
  2944. (setq inter (pop kws) sep (member "|" kws)
  2945. kws0 (delete "|" (copy-sequence kws))
  2946. kwsa nil
  2947. kws1 (mapcar
  2948. (lambda (x)
  2949. ;; 1 2
  2950. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2951. (progn
  2952. (setq kw (match-string 1 x)
  2953. key (and (match-end 2) (match-string 2 x))
  2954. log (org-extract-log-state-settings x))
  2955. (push (cons kw (and key (string-to-char key))) kwsa)
  2956. (and log (push log org-todo-log-states))
  2957. kw)
  2958. (error "Invalid TODO keyword %s" x)))
  2959. kws0)
  2960. kwsa (if kwsa (append '((:startgroup))
  2961. (nreverse kwsa)
  2962. '((:endgroup))))
  2963. hw (car kws1)
  2964. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2965. tail (list inter hw (car dws) (org-last dws))))
  2966. (add-to-list 'org-todo-heads hw 'append)
  2967. (push kws1 org-todo-sets)
  2968. (setq org-done-keywords (append org-done-keywords dws nil))
  2969. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2970. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2971. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2972. (setq org-todo-sets (nreverse org-todo-sets)
  2973. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2974. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2975. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2976. ;; Process the constants
  2977. (when const
  2978. (let (e cst)
  2979. (while (setq e (pop const))
  2980. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2981. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2982. (setq org-table-formula-constants-local cst)))
  2983. ;; Process the tags.
  2984. (when tags
  2985. (let (e tgs)
  2986. (while (setq e (pop tags))
  2987. (cond
  2988. ((equal e "{") (push '(:startgroup) tgs))
  2989. ((equal e "}") (push '(:endgroup) tgs))
  2990. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2991. (push (cons (match-string 1 e)
  2992. (string-to-char (match-string 2 e)))
  2993. tgs))
  2994. (t (push (list e) tgs))))
  2995. (org-set-local 'org-tag-alist nil)
  2996. (while (setq e (pop tgs))
  2997. (or (and (stringp (car e))
  2998. (assoc (car e) org-tag-alist))
  2999. (push e org-tag-alist)))))
  3000. ;; Compute the regular expressions and other local variables
  3001. (if (not org-done-keywords)
  3002. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3003. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3004. (length org-scheduled-string)
  3005. (length org-clock-string)
  3006. (length org-closed-string)))
  3007. org-drawer-regexp
  3008. (concat "^[ \t]*:\\("
  3009. (mapconcat 'regexp-quote org-drawers "\\|")
  3010. "\\):[ \t]*$")
  3011. org-not-done-keywords
  3012. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3013. org-todo-regexp
  3014. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3015. "\\|") "\\)\\>")
  3016. org-not-done-regexp
  3017. (concat "\\<\\("
  3018. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3019. "\\)\\>")
  3020. org-todo-line-regexp
  3021. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3022. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3023. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3024. org-complex-heading-regexp
  3025. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3026. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3027. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3028. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3029. org-nl-done-regexp
  3030. (concat "\n\\*+[ \t]+"
  3031. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3032. "\\)" "\\>")
  3033. org-todo-line-tags-regexp
  3034. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3035. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3036. (org-re
  3037. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3038. org-looking-at-done-regexp
  3039. (concat "^" "\\(?:"
  3040. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3041. "\\>")
  3042. org-deadline-regexp (concat "\\<" org-deadline-string)
  3043. org-deadline-time-regexp
  3044. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3045. org-deadline-line-regexp
  3046. (concat "\\<\\(" org-deadline-string "\\).*")
  3047. org-scheduled-regexp
  3048. (concat "\\<" org-scheduled-string)
  3049. org-scheduled-time-regexp
  3050. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3051. org-closed-time-regexp
  3052. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3053. org-keyword-time-regexp
  3054. (concat "\\<\\(" org-scheduled-string
  3055. "\\|" org-deadline-string
  3056. "\\|" org-closed-string
  3057. "\\|" org-clock-string "\\)"
  3058. " *[[<]\\([^]>]+\\)[]>]")
  3059. org-keyword-time-not-clock-regexp
  3060. (concat "\\<\\(" org-scheduled-string
  3061. "\\|" org-deadline-string
  3062. "\\|" org-closed-string
  3063. "\\)"
  3064. " *[[<]\\([^]>]+\\)[]>]")
  3065. org-maybe-keyword-time-regexp
  3066. (concat "\\(\\<\\(" org-scheduled-string
  3067. "\\|" org-deadline-string
  3068. "\\|" org-closed-string
  3069. "\\|" org-clock-string "\\)\\)?"
  3070. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3071. org-planning-or-clock-line-re
  3072. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3073. "\\|" org-deadline-string
  3074. "\\|" org-closed-string "\\|" org-clock-string
  3075. "\\)\\>\\)")
  3076. )
  3077. (org-compute-latex-and-specials-regexp)
  3078. (org-set-font-lock-defaults))))
  3079. (defun org-file-contents (file &optional noerror)
  3080. "Return the contents of FILE, as a string."
  3081. (if (or (not file)
  3082. (not (file-readable-p file)))
  3083. (if noerror
  3084. (progn
  3085. (message "Cannot read file %s" file)
  3086. (ding) (sit-for 2)
  3087. "")
  3088. (error "Cannot read file %s" file))
  3089. (with-temp-buffer
  3090. (insert-file-contents file)
  3091. (buffer-string))))
  3092. (defun org-extract-log-state-settings (x)
  3093. "Extract the log state setting from a TODO keyword string.
  3094. This will extract info from a string like \"WAIT(w@/!)\"."
  3095. (let (kw key log1 log2)
  3096. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3097. (setq kw (match-string 1 x)
  3098. key (and (match-end 2) (match-string 2 x))
  3099. log1 (and (match-end 3) (match-string 3 x))
  3100. log2 (and (match-end 4) (match-string 4 x)))
  3101. (and (or log1 log2)
  3102. (list kw
  3103. (and log1 (if (equal log1 "!") 'time 'note))
  3104. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3105. (defun org-remove-keyword-keys (list)
  3106. "Remove a pair of parenthesis at the end of each string in LIST."
  3107. (mapcar (lambda (x)
  3108. (if (string-match "(.*)$" x)
  3109. (substring x 0 (match-beginning 0))
  3110. x))
  3111. list))
  3112. ;; FIXME: this could be done much better, using second characters etc.
  3113. (defun org-assign-fast-keys (alist)
  3114. "Assign fast keys to a keyword-key alist.
  3115. Respect keys that are already there."
  3116. (let (new e k c c1 c2 (char ?a))
  3117. (while (setq e (pop alist))
  3118. (cond
  3119. ((equal e '(:startgroup)) (push e new))
  3120. ((equal e '(:endgroup)) (push e new))
  3121. (t
  3122. (setq k (car e) c2 nil)
  3123. (if (cdr e)
  3124. (setq c (cdr e))
  3125. ;; automatically assign a character.
  3126. (setq c1 (string-to-char
  3127. (downcase (substring
  3128. k (if (= (string-to-char k) ?@) 1 0)))))
  3129. (if (or (rassoc c1 new) (rassoc c1 alist))
  3130. (while (or (rassoc char new) (rassoc char alist))
  3131. (setq char (1+ char)))
  3132. (setq c2 c1))
  3133. (setq c (or c2 char)))
  3134. (push (cons k c) new))))
  3135. (nreverse new)))
  3136. ;;; Some variables used in various places
  3137. (defvar org-window-configuration nil
  3138. "Used in various places to store a window configuration.")
  3139. (defvar org-finish-function nil
  3140. "Function to be called when `C-c C-c' is used.
  3141. This is for getting out of special buffers like remember.")
  3142. ;; FIXME: Occasionally check by commenting these, to make sure
  3143. ;; no other functions uses these, forgetting to let-bind them.
  3144. (defvar entry)
  3145. (defvar state)
  3146. (defvar last-state)
  3147. (defvar date)
  3148. (defvar description)
  3149. ;; Defined somewhere in this file, but used before definition.
  3150. (defvar org-html-entities)
  3151. (defvar org-struct-menu)
  3152. (defvar org-org-menu)
  3153. (defvar org-tbl-menu)
  3154. (defvar org-agenda-keymap)
  3155. ;;;; Define the Org-mode
  3156. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3157. (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."))
  3158. ;; We use a before-change function to check if a table might need
  3159. ;; an update.
  3160. (defvar org-table-may-need-update t
  3161. "Indicates that a table might need an update.
  3162. This variable is set by `org-before-change-function'.
  3163. `org-table-align' sets it back to nil.")
  3164. (defun org-before-change-function (beg end)
  3165. "Every change indicates that a table might need an update."
  3166. (setq org-table-may-need-update t))
  3167. (defvar org-mode-map)
  3168. (defvar org-mode-hook nil
  3169. "Mode hook for Org-mode, run after the mode was turned on.")
  3170. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3171. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3172. (defvar org-table-buffer-is-an nil)
  3173. (defconst org-outline-regexp "\\*+ ")
  3174. ;;;###autoload
  3175. (define-derived-mode org-mode outline-mode "Org"
  3176. "Outline-based notes management and organizer, alias
  3177. \"Carsten's outline-mode for keeping track of everything.\"
  3178. Org-mode develops organizational tasks around a NOTES file which
  3179. contains information about projects as plain text. Org-mode is
  3180. implemented on top of outline-mode, which is ideal to keep the content
  3181. of large files well structured. It supports ToDo items, deadlines and
  3182. time stamps, which magically appear in the diary listing of the Emacs
  3183. calendar. Tables are easily created with a built-in table editor.
  3184. Plain text URL-like links connect to websites, emails (VM), Usenet
  3185. messages (Gnus), BBDB entries, and any files related to the project.
  3186. For printing and sharing of notes, an Org-mode file (or a part of it)
  3187. can be exported as a structured ASCII or HTML file.
  3188. The following commands are available:
  3189. \\{org-mode-map}"
  3190. ;; Get rid of Outline menus, they are not needed
  3191. ;; Need to do this here because define-derived-mode sets up
  3192. ;; the keymap so late. Still, it is a waste to call this each time
  3193. ;; we switch another buffer into org-mode.
  3194. (if (featurep 'xemacs)
  3195. (when (boundp 'outline-mode-menu-heading)
  3196. ;; Assume this is Greg's port, it used easymenu
  3197. (easy-menu-remove outline-mode-menu-heading)
  3198. (easy-menu-remove outline-mode-menu-show)
  3199. (easy-menu-remove outline-mode-menu-hide))
  3200. (define-key org-mode-map [menu-bar headings] 'undefined)
  3201. (define-key org-mode-map [menu-bar hide] 'undefined)
  3202. (define-key org-mode-map [menu-bar show] 'undefined))
  3203. (org-load-modules-maybe)
  3204. (easy-menu-add org-org-menu)
  3205. (easy-menu-add org-tbl-menu)
  3206. (org-install-agenda-files-menu)
  3207. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3208. (org-add-to-invisibility-spec '(org-cwidth))
  3209. (when (featurep 'xemacs)
  3210. (org-set-local 'line-move-ignore-invisible t))
  3211. (org-set-local 'outline-regexp org-outline-regexp)
  3212. (org-set-local 'outline-level 'org-outline-level)
  3213. (when (and org-ellipsis
  3214. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3215. (fboundp 'make-glyph-code))
  3216. (unless org-display-table
  3217. (setq org-display-table (make-display-table)))
  3218. (set-display-table-slot
  3219. org-display-table 4
  3220. (vconcat (mapcar
  3221. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3222. org-ellipsis)))
  3223. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3224. (setq buffer-display-table org-display-table))
  3225. (org-set-regexps-and-options)
  3226. (when (and org-tag-faces (not org-tags-special-faces-re))
  3227. ;; tag faces set outside customize.... force initialization.
  3228. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3229. ;; Calc embedded
  3230. (org-set-local 'calc-embedded-open-mode "# ")
  3231. (modify-syntax-entry ?# "<")
  3232. (modify-syntax-entry ?@ "w")
  3233. (if org-startup-truncated (setq truncate-lines t))
  3234. (org-set-local 'font-lock-unfontify-region-function
  3235. 'org-unfontify-region)
  3236. ;; Activate before-change-function
  3237. (org-set-local 'org-table-may-need-update t)
  3238. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3239. 'local)
  3240. ;; Check for running clock before killing a buffer
  3241. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3242. ;; Paragraphs and auto-filling
  3243. (org-set-autofill-regexps)
  3244. (setq indent-line-function 'org-indent-line-function)
  3245. (org-update-radio-target-regexp)
  3246. ;; Make sure dependence stuff works reliably, even for users who set it
  3247. ;; too late :-(
  3248. (if org-enforce-todo-dependencies
  3249. (add-hook 'org-blocker-hook
  3250. 'org-block-todo-from-children-or-siblings)
  3251. (remove-hook 'org-blocker-hook
  3252. 'org-block-todo-from-children-or-siblings))
  3253. (if org-enforce-todo-checkbox-dependencies
  3254. (add-hook 'org-blocker-hook
  3255. 'org-block-todo-from-checkboxes)
  3256. (remove-hook 'org-blocker-hook
  3257. 'org-block-todo-from-checkboxes))
  3258. ;; Comment characters
  3259. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3260. (org-set-local 'comment-padding " ")
  3261. ;; Align options lines
  3262. (org-set-local
  3263. 'align-mode-rules-list
  3264. '((org-in-buffer-settings
  3265. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3266. (modes . '(org-mode)))))
  3267. ;; Imenu
  3268. (org-set-local 'imenu-create-index-function
  3269. 'org-imenu-get-tree)
  3270. ;; Make isearch reveal context
  3271. (if (or (featurep 'xemacs)
  3272. (not (boundp 'outline-isearch-open-invisible-function)))
  3273. ;; Emacs 21 and XEmacs make use of the hook
  3274. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3275. ;; Emacs 22 deals with this through a special variable
  3276. (org-set-local 'outline-isearch-open-invisible-function
  3277. (lambda (&rest ignore) (org-show-context 'isearch))))
  3278. ;; If empty file that did not turn on org-mode automatically, make it to.
  3279. (if (and org-insert-mode-line-in-empty-file
  3280. (interactive-p)
  3281. (= (point-min) (point-max)))
  3282. (insert "# -*- mode: org -*-\n\n"))
  3283. (unless org-inhibit-startup
  3284. (when org-startup-align-all-tables
  3285. (let ((bmp (buffer-modified-p)))
  3286. (org-table-map-tables 'org-table-align)
  3287. (set-buffer-modified-p bmp)))
  3288. (org-set-startup-visibility)))
  3289. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3290. (defun org-current-time ()
  3291. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3292. (if (> (car org-time-stamp-rounding-minutes) 1)
  3293. (let ((r (car org-time-stamp-rounding-minutes))
  3294. (time (decode-time)))
  3295. (apply 'encode-time
  3296. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3297. (nthcdr 2 time))))
  3298. (current-time)))
  3299. ;;;; Font-Lock stuff, including the activators
  3300. (defvar org-mouse-map (make-sparse-keymap))
  3301. (org-defkey org-mouse-map
  3302. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3303. (org-defkey org-mouse-map
  3304. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3305. (when org-mouse-1-follows-link
  3306. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3307. (when org-tab-follows-link
  3308. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3309. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3310. (require 'font-lock)
  3311. (defconst org-non-link-chars "]\t\n\r<>")
  3312. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3313. "shell" "elisp"))
  3314. (defvar org-link-types-re nil
  3315. "Matches a link that has a url-like prefix like \"http:\"")
  3316. (defvar org-link-re-with-space nil
  3317. "Matches a link with spaces, optional angular brackets around it.")
  3318. (defvar org-link-re-with-space2 nil
  3319. "Matches a link with spaces, optional angular brackets around it.")
  3320. (defvar org-link-re-with-space3 nil
  3321. "Matches a link with spaces, only for internal part in bracket links.")
  3322. (defvar org-angle-link-re nil
  3323. "Matches link with angular brackets, spaces are allowed.")
  3324. (defvar org-plain-link-re nil
  3325. "Matches plain link, without spaces.")
  3326. (defvar org-bracket-link-regexp nil
  3327. "Matches a link in double brackets.")
  3328. (defvar org-bracket-link-analytic-regexp nil
  3329. "Regular expression used to analyze links.
  3330. Here is what the match groups contain after a match:
  3331. 1: http:
  3332. 2: http
  3333. 3: path
  3334. 4: [desc]
  3335. 5: desc")
  3336. (defvar org-bracket-link-analytic-regexp++ nil
  3337. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3338. (defvar org-any-link-re nil
  3339. "Regular expression matching any link.")
  3340. (defun org-make-link-regexps ()
  3341. "Update the link regular expressions.
  3342. This should be called after the variable `org-link-types' has changed."
  3343. (setq org-link-types-re
  3344. (concat
  3345. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3346. org-link-re-with-space
  3347. (concat
  3348. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3349. "\\([^" org-non-link-chars " ]"
  3350. "[^" org-non-link-chars "]*"
  3351. "[^" org-non-link-chars " ]\\)>?")
  3352. org-link-re-with-space2
  3353. (concat
  3354. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3355. "\\([^" org-non-link-chars " ]"
  3356. "[^\t\n\r]*"
  3357. "[^" org-non-link-chars " ]\\)>?")
  3358. org-link-re-with-space3
  3359. (concat
  3360. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3361. "\\([^" org-non-link-chars " ]"
  3362. "[^\t\n\r]*\\)")
  3363. org-angle-link-re
  3364. (concat
  3365. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3366. "\\([^" org-non-link-chars " ]"
  3367. "[^" org-non-link-chars "]*"
  3368. "\\)>")
  3369. org-plain-link-re
  3370. (concat
  3371. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3372. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3373. org-bracket-link-regexp
  3374. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3375. org-bracket-link-analytic-regexp
  3376. (concat
  3377. "\\[\\["
  3378. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3379. "\\([^]]+\\)"
  3380. "\\]"
  3381. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3382. "\\]")
  3383. org-bracket-link-analytic-regexp++
  3384. (concat
  3385. "\\[\\["
  3386. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3387. "\\([^]]+\\)"
  3388. "\\]"
  3389. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3390. "\\]")
  3391. org-any-link-re
  3392. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3393. org-angle-link-re "\\)\\|\\("
  3394. org-plain-link-re "\\)")))
  3395. (org-make-link-regexps)
  3396. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3397. "Regular expression for fast time stamp matching.")
  3398. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3399. "Regular expression for fast time stamp matching.")
  3400. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3401. "Regular expression matching time strings for analysis.
  3402. This one does not require the space after the date, so it can be used
  3403. on a string that terminates immediately after the date.")
  3404. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3405. "Regular expression matching time strings for analysis.")
  3406. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3407. "Regular expression matching time stamps, with groups.")
  3408. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3409. "Regular expression matching time stamps (also [..]), with groups.")
  3410. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3411. "Regular expression matching a time stamp range.")
  3412. (defconst org-tr-regexp-both
  3413. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3414. "Regular expression matching a time stamp range.")
  3415. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3416. org-ts-regexp "\\)?")
  3417. "Regular expression matching a time stamp or time stamp range.")
  3418. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3419. org-ts-regexp-both "\\)?")
  3420. "Regular expression matching a time stamp or time stamp range.
  3421. The time stamps may be either active or inactive.")
  3422. (defvar org-emph-face nil)
  3423. (defun org-do-emphasis-faces (limit)
  3424. "Run through the buffer and add overlays to links."
  3425. (let (rtn)
  3426. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3427. (if (not (= (char-after (match-beginning 3))
  3428. (char-after (match-beginning 4))))
  3429. (progn
  3430. (setq rtn t)
  3431. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3432. 'face
  3433. (nth 1 (assoc (match-string 3)
  3434. org-emphasis-alist)))
  3435. (add-text-properties (match-beginning 2) (match-end 2)
  3436. '(font-lock-multiline t))
  3437. (when org-hide-emphasis-markers
  3438. (add-text-properties (match-end 4) (match-beginning 5)
  3439. '(invisible org-link))
  3440. (add-text-properties (match-beginning 3) (match-end 3)
  3441. '(invisible org-link)))))
  3442. (backward-char 1))
  3443. rtn))
  3444. (defun org-emphasize (&optional char)
  3445. "Insert or change an emphasis, i.e. a font like bold or italic.
  3446. If there is an active region, change that region to a new emphasis.
  3447. If there is no region, just insert the marker characters and position
  3448. the cursor between them.
  3449. CHAR should be either the marker character, or the first character of the
  3450. HTML tag associated with that emphasis. If CHAR is a space, the means
  3451. to remove the emphasis of the selected region.
  3452. If char is not given (for example in an interactive call) it
  3453. will be prompted for."
  3454. (interactive)
  3455. (let ((eal org-emphasis-alist) e det
  3456. (erc org-emphasis-regexp-components)
  3457. (prompt "")
  3458. (string "") beg end move tag c s)
  3459. (if (org-region-active-p)
  3460. (setq beg (region-beginning) end (region-end)
  3461. string (buffer-substring beg end))
  3462. (setq move t))
  3463. (while (setq e (pop eal))
  3464. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3465. c (aref tag 0))
  3466. (push (cons c (string-to-char (car e))) det)
  3467. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3468. (substring tag 1)))))
  3469. (setq det (nreverse det))
  3470. (unless char
  3471. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3472. (setq char (read-char-exclusive)))
  3473. (setq char (or (cdr (assoc char det)) char))
  3474. (if (equal char ?\ )
  3475. (setq s "" move nil)
  3476. (unless (assoc (char-to-string char) org-emphasis-alist)
  3477. (error "No such emphasis marker: \"%c\"" char))
  3478. (setq s (char-to-string char)))
  3479. (while (and (> (length string) 1)
  3480. (equal (substring string 0 1) (substring string -1))
  3481. (assoc (substring string 0 1) org-emphasis-alist))
  3482. (setq string (substring string 1 -1)))
  3483. (setq string (concat s string s))
  3484. (if beg (delete-region beg end))
  3485. (unless (or (bolp)
  3486. (string-match (concat "[" (nth 0 erc) "\n]")
  3487. (char-to-string (char-before (point)))))
  3488. (insert " "))
  3489. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3490. (char-to-string (char-after (point))))
  3491. (insert " ") (backward-char 1))
  3492. (insert string)
  3493. (and move (backward-char 1))))
  3494. (defconst org-nonsticky-props
  3495. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3496. (defun org-activate-plain-links (limit)
  3497. "Run through the buffer and add overlays to links."
  3498. (catch 'exit
  3499. (let (f)
  3500. (while (re-search-forward org-plain-link-re limit t)
  3501. (setq f (get-text-property (match-beginning 0) 'face))
  3502. (if (or (eq f 'org-tag)
  3503. (and (listp f) (memq 'org-tag f)))
  3504. nil
  3505. (add-text-properties (match-beginning 0) (match-end 0)
  3506. (list 'mouse-face 'highlight
  3507. 'rear-nonsticky org-nonsticky-props
  3508. 'keymap org-mouse-map
  3509. ))
  3510. (throw 'exit t))))))
  3511. (defun org-activate-code (limit)
  3512. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3513. (progn
  3514. (remove-text-properties (match-beginning 0) (match-end 0)
  3515. '(display t invisible t intangible t))
  3516. t)))
  3517. (defun org-activate-angle-links (limit)
  3518. "Run through the buffer and add overlays to links."
  3519. (if (re-search-forward org-angle-link-re limit t)
  3520. (progn
  3521. (add-text-properties (match-beginning 0) (match-end 0)
  3522. (list 'mouse-face 'highlight
  3523. 'rear-nonsticky org-nonsticky-props
  3524. 'keymap org-mouse-map
  3525. ))
  3526. t)))
  3527. (defun org-activate-footnote-links (limit)
  3528. "Run through the buffer and add overlays to links."
  3529. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3530. limit t)
  3531. (progn
  3532. (add-text-properties (match-beginning 2) (match-end 2)
  3533. (list 'mouse-face 'highlight
  3534. 'rear-nonsticky org-nonsticky-props
  3535. 'keymap org-mouse-map
  3536. 'help-echo
  3537. (if (= (point-at-bol) (match-beginning 2))
  3538. "Footnote definition"
  3539. "Footnote reference")
  3540. ))
  3541. t)))
  3542. (defun org-activate-bracket-links (limit)
  3543. "Run through the buffer and add overlays to bracketed links."
  3544. (if (re-search-forward org-bracket-link-regexp limit t)
  3545. (let* ((help (concat "LINK: "
  3546. (org-match-string-no-properties 1)))
  3547. ;; FIXME: above we should remove the escapes.
  3548. ;; but that requires another match, protecting match data,
  3549. ;; a lot of overhead for font-lock.
  3550. (ip (org-maybe-intangible
  3551. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3552. 'keymap org-mouse-map 'mouse-face 'highlight
  3553. 'font-lock-multiline t 'help-echo help)))
  3554. (vp (list 'rear-nonsticky org-nonsticky-props
  3555. 'keymap org-mouse-map 'mouse-face 'highlight
  3556. ' font-lock-multiline t 'help-echo help)))
  3557. ;; We need to remove the invisible property here. Table narrowing
  3558. ;; may have made some of this invisible.
  3559. (remove-text-properties (match-beginning 0) (match-end 0)
  3560. '(invisible nil))
  3561. (if (match-end 3)
  3562. (progn
  3563. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3564. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3565. (add-text-properties (match-end 3) (match-end 0) ip))
  3566. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3567. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3568. (add-text-properties (match-end 1) (match-end 0) ip))
  3569. t)))
  3570. (defun org-activate-dates (limit)
  3571. "Run through the buffer and add overlays to dates."
  3572. (if (re-search-forward org-tsr-regexp-both limit t)
  3573. (progn
  3574. (add-text-properties (match-beginning 0) (match-end 0)
  3575. (list 'mouse-face 'highlight
  3576. 'rear-nonsticky org-nonsticky-props
  3577. 'keymap org-mouse-map))
  3578. (when org-display-custom-times
  3579. (if (match-end 3)
  3580. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3581. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3582. t)))
  3583. (defvar org-target-link-regexp nil
  3584. "Regular expression matching radio targets in plain text.")
  3585. (make-variable-buffer-local 'org-target-link-regexp)
  3586. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3587. "Regular expression matching a link target.")
  3588. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3589. "Regular expression matching a radio target.")
  3590. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3591. "Regular expression matching any target.")
  3592. (defun org-activate-target-links (limit)
  3593. "Run through the buffer and add overlays to target matches."
  3594. (when org-target-link-regexp
  3595. (let ((case-fold-search t))
  3596. (if (re-search-forward org-target-link-regexp limit t)
  3597. (progn
  3598. (add-text-properties (match-beginning 0) (match-end 0)
  3599. (list 'mouse-face 'highlight
  3600. 'rear-nonsticky org-nonsticky-props
  3601. 'keymap org-mouse-map
  3602. 'help-echo "Radio target link"
  3603. 'org-linked-text t))
  3604. t)))))
  3605. (defun org-update-radio-target-regexp ()
  3606. "Find all radio targets in this file and update the regular expression."
  3607. (interactive)
  3608. (when (memq 'radio org-activate-links)
  3609. (setq org-target-link-regexp
  3610. (org-make-target-link-regexp (org-all-targets 'radio)))
  3611. (org-restart-font-lock)))
  3612. (defun org-hide-wide-columns (limit)
  3613. (let (s e)
  3614. (setq s (text-property-any (point) (or limit (point-max))
  3615. 'org-cwidth t))
  3616. (when s
  3617. (setq e (next-single-property-change s 'org-cwidth))
  3618. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3619. (goto-char e)
  3620. t)))
  3621. (defvar org-latex-and-specials-regexp nil
  3622. "Regular expression for highlighting export special stuff.")
  3623. (defvar org-match-substring-regexp)
  3624. (defvar org-match-substring-with-braces-regexp)
  3625. (defvar org-export-html-special-string-regexps)
  3626. (defun org-compute-latex-and-specials-regexp ()
  3627. "Compute regular expression for stuff treated specially by exporters."
  3628. (if (not org-highlight-latex-fragments-and-specials)
  3629. (org-set-local 'org-latex-and-specials-regexp nil)
  3630. (require 'org-exp)
  3631. (let*
  3632. ((matchers (plist-get org-format-latex-options :matchers))
  3633. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3634. org-latex-regexps)))
  3635. (options (org-combine-plists (org-default-export-plist)
  3636. (org-infile-export-plist)))
  3637. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3638. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3639. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3640. (org-export-html-expand (plist-get options :expand-quoted-html))
  3641. (org-export-with-special-strings (plist-get options :special-strings))
  3642. (re-sub
  3643. (cond
  3644. ((equal org-export-with-sub-superscripts '{})
  3645. (list org-match-substring-with-braces-regexp))
  3646. (org-export-with-sub-superscripts
  3647. (list org-match-substring-regexp))
  3648. (t nil)))
  3649. (re-latex
  3650. (if org-export-with-LaTeX-fragments
  3651. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3652. (re-macros
  3653. (if org-export-with-TeX-macros
  3654. (list (concat "\\\\"
  3655. (regexp-opt
  3656. (append (mapcar 'car org-html-entities)
  3657. (if (boundp 'org-latex-entities)
  3658. org-latex-entities nil))
  3659. 'words))) ; FIXME
  3660. ))
  3661. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3662. (re-special (if org-export-with-special-strings
  3663. (mapcar (lambda (x) (car x))
  3664. org-export-html-special-string-regexps)))
  3665. (re-rest
  3666. (delq nil
  3667. (list
  3668. (if org-export-html-expand "@<[^>\n]+>")
  3669. ))))
  3670. (org-set-local
  3671. 'org-latex-and-specials-regexp
  3672. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3673. re-rest) "\\|")))))
  3674. (defun org-do-latex-and-special-faces (limit)
  3675. "Run through the buffer and add overlays to links."
  3676. (when org-latex-and-specials-regexp
  3677. (let (rtn d)
  3678. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3679. limit t))
  3680. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3681. 'face))
  3682. '(org-code org-verbatim underline)))
  3683. (progn
  3684. (setq rtn t
  3685. d (cond ((member (char-after (1+ (match-beginning 0)))
  3686. '(?_ ?^)) 1)
  3687. (t 0)))
  3688. (font-lock-prepend-text-property
  3689. (+ d (match-beginning 0)) (match-end 0)
  3690. 'face 'org-latex-and-export-specials)
  3691. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3692. '(font-lock-multiline t)))))
  3693. rtn)))
  3694. (defun org-restart-font-lock ()
  3695. "Restart font-lock-mode, to force refontification."
  3696. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3697. (font-lock-mode -1)
  3698. (font-lock-mode 1)))
  3699. (defun org-all-targets (&optional radio)
  3700. "Return a list of all targets in this file.
  3701. With optional argument RADIO, only find radio targets."
  3702. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3703. rtn)
  3704. (save-excursion
  3705. (goto-char (point-min))
  3706. (while (re-search-forward re nil t)
  3707. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3708. rtn)))
  3709. (defun org-make-target-link-regexp (targets)
  3710. "Make regular expression matching all strings in TARGETS.
  3711. The regular expression finds the targets also if there is a line break
  3712. between words."
  3713. (and targets
  3714. (concat
  3715. "\\<\\("
  3716. (mapconcat
  3717. (lambda (x)
  3718. (while (string-match " +" x)
  3719. (setq x (replace-match "\\s-+" t t x)))
  3720. x)
  3721. targets
  3722. "\\|")
  3723. "\\)\\>")))
  3724. (defun org-activate-tags (limit)
  3725. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3726. (progn
  3727. (add-text-properties (match-beginning 1) (match-end 1)
  3728. (list 'mouse-face 'highlight
  3729. 'rear-nonsticky org-nonsticky-props
  3730. 'keymap org-mouse-map))
  3731. t)))
  3732. (defun org-outline-level ()
  3733. (save-excursion
  3734. (looking-at outline-regexp)
  3735. (if (match-beginning 1)
  3736. (+ (org-get-string-indentation (match-string 1)) 1000)
  3737. (1- (- (match-end 0) (match-beginning 0))))))
  3738. (defvar org-font-lock-keywords nil)
  3739. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3740. "Regular expression matching a property line.")
  3741. (defvar org-font-lock-hook nil
  3742. "Functions to be called for special font lock stuff.")
  3743. (defun org-font-lock-hook (limit)
  3744. (run-hook-with-args 'org-font-lock-hook limit))
  3745. (defun org-set-font-lock-defaults ()
  3746. (let* ((em org-fontify-emphasized-text)
  3747. (lk org-activate-links)
  3748. (org-font-lock-extra-keywords
  3749. (list
  3750. ;; Call the hook
  3751. '(org-font-lock-hook)
  3752. ;; Headlines
  3753. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3754. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3755. ;; Table lines
  3756. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3757. (1 'org-table t))
  3758. ;; Table internals
  3759. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3760. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3761. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3762. ;; Drawers
  3763. (list org-drawer-regexp '(0 'org-special-keyword t))
  3764. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3765. ;; Properties
  3766. (list org-property-re
  3767. '(1 'org-special-keyword t)
  3768. '(3 'org-property-value t))
  3769. (if org-format-transports-properties-p
  3770. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3771. ;; Links
  3772. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3773. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3774. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3775. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3776. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3777. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3778. (if (memq 'footnote lk) '(org-activate-footnote-links
  3779. (2 'org-footnote t)))
  3780. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3781. '(org-hide-wide-columns (0 nil append))
  3782. ;; TODO lines
  3783. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3784. '(1 (org-get-todo-face 1) t))
  3785. ;; DONE
  3786. (if org-fontify-done-headline
  3787. (list (concat "^[*]+ +\\<\\("
  3788. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3789. "\\)\\(.*\\)")
  3790. '(2 'org-headline-done t))
  3791. nil)
  3792. ;; Priorities
  3793. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3794. ;; Tags
  3795. '(org-font-lock-add-tag-faces)
  3796. ;; Special keywords
  3797. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3798. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3799. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3800. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3801. ;; Emphasis
  3802. (if em
  3803. (if (featurep 'xemacs)
  3804. '(org-do-emphasis-faces (0 nil append))
  3805. '(org-do-emphasis-faces)))
  3806. ;; Checkboxes
  3807. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3808. 2 'bold prepend)
  3809. (if org-provide-checkbox-statistics
  3810. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3811. (0 (org-get-checkbox-statistics-face) t)))
  3812. ;; Description list items
  3813. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3814. 2 'bold prepend)
  3815. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3816. '(1 'org-archived prepend))
  3817. ;; Specials
  3818. '(org-do-latex-and-special-faces)
  3819. ;; Code
  3820. '(org-activate-code (1 'org-code t))
  3821. ;; COMMENT
  3822. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3823. "\\|" org-quote-string "\\)\\>")
  3824. '(1 'org-special-keyword t))
  3825. '("^#.*" (0 'font-lock-comment-face t))
  3826. )))
  3827. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3828. ;; Now set the full font-lock-keywords
  3829. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3830. (org-set-local 'font-lock-defaults
  3831. '(org-font-lock-keywords t nil nil backward-paragraph))
  3832. (kill-local-variable 'font-lock-keywords) nil))
  3833. (defvar org-m nil)
  3834. (defvar org-l nil)
  3835. (defvar org-f nil)
  3836. (defun org-get-level-face (n)
  3837. "Get the right face for match N in font-lock matching of headlines."
  3838. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3839. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3840. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3841. (cond
  3842. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3843. ((eq n 2) org-f)
  3844. (t (if org-level-color-stars-only nil org-f))))
  3845. (defun org-get-todo-face (kwd)
  3846. "Get the right face for a TODO keyword KWD.
  3847. If KWD is a number, get the corresponding match group."
  3848. (if (numberp kwd) (setq kwd (match-string kwd)))
  3849. (or (cdr (assoc kwd org-todo-keyword-faces))
  3850. (and (member kwd org-done-keywords) 'org-done)
  3851. 'org-todo))
  3852. (defun org-font-lock-add-tag-faces (limit)
  3853. "Add the special tag faces."
  3854. (when (and org-tag-faces org-tags-special-faces-re)
  3855. (while (re-search-forward org-tags-special-faces-re limit t)
  3856. (add-text-properties (match-beginning 1) (match-end 1)
  3857. (list 'face (org-get-tag-face 1)
  3858. 'font-lock-fontified t))
  3859. (backward-char 1))))
  3860. (defun org-get-tag-face (kwd)
  3861. "Get the right face for a TODO keyword KWD.
  3862. If KWD is a number, get the corresponding match group."
  3863. (if (numberp kwd) (setq kwd (match-string kwd)))
  3864. (or (cdr (assoc kwd org-tag-faces))
  3865. 'org-tag))
  3866. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3867. "Remove fontification and activation overlays from links."
  3868. (font-lock-default-unfontify-region beg end)
  3869. (let* ((buffer-undo-list t)
  3870. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3871. (inhibit-modification-hooks t)
  3872. deactivate-mark buffer-file-name buffer-file-truename)
  3873. (remove-text-properties beg end
  3874. '(mouse-face t keymap t org-linked-text t
  3875. invisible t intangible t))))
  3876. ;;;; Visibility cycling, including org-goto and indirect buffer
  3877. ;;; Cycling
  3878. (defvar org-cycle-global-status nil)
  3879. (make-variable-buffer-local 'org-cycle-global-status)
  3880. (defvar org-cycle-subtree-status nil)
  3881. (make-variable-buffer-local 'org-cycle-subtree-status)
  3882. ;;;###autoload
  3883. (defun org-cycle (&optional arg)
  3884. "Visibility cycling for Org-mode.
  3885. - When this function is called with a prefix argument, rotate the entire
  3886. buffer through 3 states (global cycling)
  3887. 1. OVERVIEW: Show only top-level headlines.
  3888. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3889. 3. SHOW ALL: Show everything.
  3890. When called with two C-u C-u prefixes, switch to the startup visibility,
  3891. determined by the variable `org-startup-folded', and by any VISIBILITY
  3892. properties in the buffer.
  3893. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3894. including drawers.
  3895. - When point is at the beginning of a headline, rotate the subtree started
  3896. by this line through 3 different states (local cycling)
  3897. 1. FOLDED: Only the main headline is shown.
  3898. 2. CHILDREN: The main headline and the direct children are shown.
  3899. From this state, you can move to one of the children
  3900. and zoom in further.
  3901. 3. SUBTREE: Show the entire subtree, including body text.
  3902. - When there is a numeric prefix, go up to a heading with level ARG, do
  3903. a `show-subtree' and return to the previous cursor position. If ARG
  3904. is negative, go up that many levels.
  3905. - When point is not at the beginning of a headline, execute the global
  3906. binding for TAB, which is re-indenting the line. See the option
  3907. `org-cycle-emulate-tab' for details.
  3908. - Special case: if point is at the beginning of the buffer and there is
  3909. no headline in line 1, this function will act as if called with prefix arg.
  3910. But only if also the variable `org-cycle-global-at-bob' is t."
  3911. (interactive "P")
  3912. (org-load-modules-maybe)
  3913. (let* ((outline-regexp
  3914. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3915. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3916. outline-regexp))
  3917. (bob-special (and org-cycle-global-at-bob (bobp)
  3918. (not (looking-at outline-regexp))))
  3919. (org-cycle-hook
  3920. (if bob-special
  3921. (delq 'org-optimize-window-after-visibility-change
  3922. (copy-sequence org-cycle-hook))
  3923. org-cycle-hook))
  3924. (pos (point)))
  3925. (if (or bob-special (equal arg '(4)))
  3926. ;; special case: use global cycling
  3927. (setq arg t))
  3928. (cond
  3929. ((equal arg '(16))
  3930. (org-set-startup-visibility)
  3931. (message "Startup visibility, plus VISIBILITY properties"))
  3932. ((equal arg '(64))
  3933. (show-all)
  3934. (message "Entire buffer visible, including drawers"))
  3935. ((org-at-table-p 'any)
  3936. ;; Enter the table or move to the next field in the table
  3937. (or (org-table-recognize-table.el)
  3938. (progn
  3939. (if arg (org-table-edit-field t)
  3940. (org-table-justify-field-maybe)
  3941. (call-interactively 'org-table-next-field)))))
  3942. ((eq arg t) ;; Global cycling
  3943. (cond
  3944. ((and (eq last-command this-command)
  3945. (eq org-cycle-global-status 'overview))
  3946. ;; We just created the overview - now do table of contents
  3947. ;; This can be slow in very large buffers, so indicate action
  3948. (message "CONTENTS...")
  3949. (org-content)
  3950. (message "CONTENTS...done")
  3951. (setq org-cycle-global-status 'contents)
  3952. (run-hook-with-args 'org-cycle-hook 'contents))
  3953. ((and (eq last-command this-command)
  3954. (eq org-cycle-global-status 'contents))
  3955. ;; We just showed the table of contents - now show everything
  3956. (show-all)
  3957. (message "SHOW ALL")
  3958. (setq org-cycle-global-status 'all)
  3959. (run-hook-with-args 'org-cycle-hook 'all))
  3960. (t
  3961. ;; Default action: go to overview
  3962. (org-overview)
  3963. (message "OVERVIEW")
  3964. (setq org-cycle-global-status 'overview)
  3965. (run-hook-with-args 'org-cycle-hook 'overview))))
  3966. ((and org-drawers org-drawer-regexp
  3967. (save-excursion
  3968. (beginning-of-line 1)
  3969. (looking-at org-drawer-regexp)))
  3970. ;; Toggle block visibility
  3971. (org-flag-drawer
  3972. (not (get-char-property (match-end 0) 'invisible))))
  3973. ((integerp arg)
  3974. ;; Show-subtree, ARG levels up from here.
  3975. (save-excursion
  3976. (org-back-to-heading)
  3977. (outline-up-heading (if (< arg 0) (- arg)
  3978. (- (funcall outline-level) arg)))
  3979. (org-show-subtree)))
  3980. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3981. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3982. ;; At a heading: rotate between three different views
  3983. (org-back-to-heading)
  3984. (let ((goal-column 0) eoh eol eos)
  3985. ;; First, some boundaries
  3986. (save-excursion
  3987. (org-back-to-heading)
  3988. (save-excursion
  3989. (beginning-of-line 2)
  3990. (while (and (not (eobp)) ;; this is like `next-line'
  3991. (get-char-property (1- (point)) 'invisible))
  3992. (beginning-of-line 2)) (setq eol (point)))
  3993. (outline-end-of-heading) (setq eoh (point))
  3994. (org-end-of-subtree t)
  3995. (unless (eobp)
  3996. (skip-chars-forward " \t\n")
  3997. (beginning-of-line 1) ; in case this is an item
  3998. )
  3999. (setq eos (1- (point))))
  4000. ;; Find out what to do next and set `this-command'
  4001. (cond
  4002. ((= eos eoh)
  4003. ;; Nothing is hidden behind this heading
  4004. (message "EMPTY ENTRY")
  4005. (setq org-cycle-subtree-status nil)
  4006. (save-excursion
  4007. (goto-char eos)
  4008. (outline-next-heading)
  4009. (if (org-invisible-p) (org-flag-heading nil))))
  4010. ((or (>= eol eos)
  4011. (not (string-match "\\S-" (buffer-substring eol eos))))
  4012. ;; Entire subtree is hidden in one line: open it
  4013. (org-show-entry)
  4014. (show-children)
  4015. (message "CHILDREN")
  4016. (save-excursion
  4017. (goto-char eos)
  4018. (outline-next-heading)
  4019. (if (org-invisible-p) (org-flag-heading nil)))
  4020. (setq org-cycle-subtree-status 'children)
  4021. (run-hook-with-args 'org-cycle-hook 'children))
  4022. ((and (eq last-command this-command)
  4023. (eq org-cycle-subtree-status 'children))
  4024. ;; We just showed the children, now show everything.
  4025. (org-show-subtree)
  4026. (message "SUBTREE")
  4027. (setq org-cycle-subtree-status 'subtree)
  4028. (run-hook-with-args 'org-cycle-hook 'subtree))
  4029. (t
  4030. ;; Default action: hide the subtree.
  4031. (hide-subtree)
  4032. (message "FOLDED")
  4033. (setq org-cycle-subtree-status 'folded)
  4034. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4035. ;; TAB emulation and template completion
  4036. (buffer-read-only (org-back-to-heading))
  4037. ((org-try-structure-completion))
  4038. ((org-try-cdlatex-tab))
  4039. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4040. (or (not (bolp))
  4041. (not (looking-at outline-regexp))))
  4042. (call-interactively (global-key-binding "\t")))
  4043. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4044. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4045. (or (and (eq org-cycle-emulate-tab 'white)
  4046. (= (match-end 0) (point-at-eol)))
  4047. (and (eq org-cycle-emulate-tab 'whitestart)
  4048. (>= (match-end 0) pos))))
  4049. t
  4050. (eq org-cycle-emulate-tab t))
  4051. (call-interactively (global-key-binding "\t")))
  4052. (t (save-excursion
  4053. (org-back-to-heading)
  4054. (org-cycle))))))
  4055. ;;;###autoload
  4056. (defun org-global-cycle (&optional arg)
  4057. "Cycle the global visibility. For details see `org-cycle'.
  4058. With C-u prefix arg, switch to startup visibility.
  4059. With a numeric prefix, show all headlines up to that level."
  4060. (interactive "P")
  4061. (let ((org-cycle-include-plain-lists
  4062. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4063. (cond
  4064. ((integerp arg)
  4065. (show-all)
  4066. (hide-sublevels arg)
  4067. (setq org-cycle-global-status 'contents))
  4068. ((equal arg '(4))
  4069. (org-set-startup-visibility)
  4070. (message "Startup visibility, plus VISIBILITY properties."))
  4071. (t
  4072. (org-cycle '(4))))))
  4073. (defun org-set-startup-visibility ()
  4074. "Set the visibility required by startup options and properties."
  4075. (cond
  4076. ((eq org-startup-folded t)
  4077. (org-cycle '(4)))
  4078. ((eq org-startup-folded 'content)
  4079. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4080. (org-cycle '(4)) (org-cycle '(4)))))
  4081. (org-set-visibility-according-to-property 'no-cleanup)
  4082. (org-cycle-hide-archived-subtrees 'all)
  4083. (org-cycle-hide-drawers 'all)
  4084. (org-cycle-show-empty-lines 'all))
  4085. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4086. "Switch subtree visibilities according to :VISIBILITY: property."
  4087. (interactive)
  4088. (let (org-show-entry-below state)
  4089. (save-excursion
  4090. (goto-char (point-min))
  4091. (while (re-search-forward
  4092. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4093. nil t)
  4094. (setq state (match-string 1))
  4095. (save-excursion
  4096. (org-back-to-heading t)
  4097. (hide-subtree)
  4098. (org-reveal)
  4099. (cond
  4100. ((equal state '("fold" "folded"))
  4101. (hide-subtree))
  4102. ((equal state "children")
  4103. (org-show-hidden-entry)
  4104. (show-children))
  4105. ((equal state "content")
  4106. (save-excursion
  4107. (save-restriction
  4108. (org-narrow-to-subtree)
  4109. (org-content))))
  4110. ((member state '("all" "showall"))
  4111. (show-subtree)))))
  4112. (unless no-cleanup
  4113. (org-cycle-hide-archived-subtrees 'all)
  4114. (org-cycle-hide-drawers 'all)
  4115. (org-cycle-show-empty-lines 'all)))))
  4116. (defun org-overview ()
  4117. "Switch to overview mode, showing only top-level headlines.
  4118. Really, this shows all headlines with level equal or greater than the level
  4119. of the first headline in the buffer. This is important, because if the
  4120. first headline is not level one, then (hide-sublevels 1) gives confusing
  4121. results."
  4122. (interactive)
  4123. (let ((level (save-excursion
  4124. (goto-char (point-min))
  4125. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4126. (progn
  4127. (goto-char (match-beginning 0))
  4128. (funcall outline-level))))))
  4129. (and level (hide-sublevels level))))
  4130. (defun org-content (&optional arg)
  4131. "Show all headlines in the buffer, like a table of contents.
  4132. With numerical argument N, show content up to level N."
  4133. (interactive "P")
  4134. (save-excursion
  4135. ;; Visit all headings and show their offspring
  4136. (and (integerp arg) (org-overview))
  4137. (goto-char (point-max))
  4138. (catch 'exit
  4139. (while (and (progn (condition-case nil
  4140. (outline-previous-visible-heading 1)
  4141. (error (goto-char (point-min))))
  4142. t)
  4143. (looking-at outline-regexp))
  4144. (if (integerp arg)
  4145. (show-children (1- arg))
  4146. (show-branches))
  4147. (if (bobp) (throw 'exit nil))))))
  4148. (defun org-optimize-window-after-visibility-change (state)
  4149. "Adjust the window after a change in outline visibility.
  4150. This function is the default value of the hook `org-cycle-hook'."
  4151. (when (get-buffer-window (current-buffer))
  4152. (cond
  4153. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4154. ; ((eq state 'overview) (org-beginning-of-line))
  4155. ((eq state 'content) nil)
  4156. ((eq state 'all) nil)
  4157. ((eq state 'folded) nil)
  4158. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4159. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4160. (defun org-compact-display-after-subtree-move ()
  4161. "Show a compacter version of the tree of the entry's parent."
  4162. (save-excursion
  4163. (if (org-up-heading-safe)
  4164. (progn
  4165. (hide-subtree)
  4166. (show-entry)
  4167. (show-children)
  4168. (org-cycle-show-empty-lines 'children)
  4169. (org-cycle-hide-drawers 'children))
  4170. (org-overview))))
  4171. (defun org-cycle-show-empty-lines (state)
  4172. "Show empty lines above all visible headlines.
  4173. The region to be covered depends on STATE when called through
  4174. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4175. entire buffer covered. Note that an empty line is only shown if there
  4176. are at least `org-cycle-separator-lines' empty lines before the headline."
  4177. (when (> org-cycle-separator-lines 0)
  4178. (save-excursion
  4179. (let* ((n org-cycle-separator-lines)
  4180. (re (cond
  4181. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4182. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4183. (t (let ((ns (number-to-string (- n 2))))
  4184. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4185. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4186. beg end)
  4187. (cond
  4188. ((memq state '(overview contents t))
  4189. (setq beg (point-min) end (point-max)))
  4190. ((memq state '(children folded))
  4191. (setq beg (point) end (progn (org-end-of-subtree t t)
  4192. (beginning-of-line 2)
  4193. (point)))))
  4194. (when beg
  4195. (goto-char beg)
  4196. (while (re-search-forward re end t)
  4197. (if (not (get-char-property (match-end 1) 'invisible))
  4198. (outline-flag-region
  4199. (match-beginning 1) (match-end 1) nil)))))))
  4200. ;; Never hide empty lines at the end of the file.
  4201. (save-excursion
  4202. (goto-char (point-max))
  4203. (outline-previous-heading)
  4204. (outline-end-of-heading)
  4205. (if (and (looking-at "[ \t\n]+")
  4206. (= (match-end 0) (point-max)))
  4207. (outline-flag-region (point) (match-end 0) nil))))
  4208. (defun org-show-empty-lines-in-parent ()
  4209. "Move to the parent and re-show empty lines before visible headlines."
  4210. (save-excursion
  4211. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4212. (org-cycle-show-empty-lines context))))
  4213. (defun org-cycle-hide-drawers (state)
  4214. "Re-hide all drawers after a visibility state change."
  4215. (when (and (org-mode-p)
  4216. (not (memq state '(overview folded))))
  4217. (save-excursion
  4218. (let* ((globalp (memq state '(contents all)))
  4219. (beg (if globalp (point-min) (point)))
  4220. (end (if globalp (point-max) (org-end-of-subtree t))))
  4221. (goto-char beg)
  4222. (while (re-search-forward org-drawer-regexp end t)
  4223. (org-flag-drawer t))))))
  4224. (defun org-flag-drawer (flag)
  4225. (save-excursion
  4226. (beginning-of-line 1)
  4227. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4228. (let ((b (match-end 0))
  4229. (outline-regexp org-outline-regexp))
  4230. (if (re-search-forward
  4231. "^[ \t]*:END:"
  4232. (save-excursion (outline-next-heading) (point)) t)
  4233. (outline-flag-region b (point-at-eol) flag)
  4234. (error ":END: line missing"))))))
  4235. (defun org-subtree-end-visible-p ()
  4236. "Is the end of the current subtree visible?"
  4237. (pos-visible-in-window-p
  4238. (save-excursion (org-end-of-subtree t) (point))))
  4239. (defun org-first-headline-recenter (&optional N)
  4240. "Move cursor to the first headline and recenter the headline.
  4241. Optional argument N means, put the headline into the Nth line of the window."
  4242. (goto-char (point-min))
  4243. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4244. (beginning-of-line)
  4245. (recenter (prefix-numeric-value N))))
  4246. ;;; Org-goto
  4247. (defvar org-goto-window-configuration nil)
  4248. (defvar org-goto-marker nil)
  4249. (defvar org-goto-map
  4250. (let ((map (make-sparse-keymap)))
  4251. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4252. (while (setq cmd (pop cmds))
  4253. (substitute-key-definition cmd cmd map global-map)))
  4254. (suppress-keymap map)
  4255. (org-defkey map "\C-m" 'org-goto-ret)
  4256. (org-defkey map [(return)] 'org-goto-ret)
  4257. (org-defkey map [(left)] 'org-goto-left)
  4258. (org-defkey map [(right)] 'org-goto-right)
  4259. (org-defkey map [(control ?g)] 'org-goto-quit)
  4260. (org-defkey map "\C-i" 'org-cycle)
  4261. (org-defkey map [(tab)] 'org-cycle)
  4262. (org-defkey map [(down)] 'outline-next-visible-heading)
  4263. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4264. (if org-goto-auto-isearch
  4265. (if (fboundp 'define-key-after)
  4266. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4267. nil)
  4268. (org-defkey map "q" 'org-goto-quit)
  4269. (org-defkey map "n" 'outline-next-visible-heading)
  4270. (org-defkey map "p" 'outline-previous-visible-heading)
  4271. (org-defkey map "f" 'outline-forward-same-level)
  4272. (org-defkey map "b" 'outline-backward-same-level)
  4273. (org-defkey map "u" 'outline-up-heading))
  4274. (org-defkey map "/" 'org-occur)
  4275. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4276. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4277. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4278. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4279. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4280. map))
  4281. (defconst org-goto-help
  4282. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4283. RET=jump to location [Q]uit and return to previous location
  4284. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4285. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4286. ;; FIXME: Docstring doe not mention both interfaces
  4287. (defun org-goto (&optional alternative-interface)
  4288. "Look up a different location in the current file, keeping current visibility.
  4289. When you want look-up or go to a different location in a document, the
  4290. fastest way is often to fold the entire buffer and then dive into the tree.
  4291. This method has the disadvantage, that the previous location will be folded,
  4292. which may not be what you want.
  4293. This command works around this by showing a copy of the current buffer
  4294. in an indirect buffer, in overview mode. You can dive into the tree in
  4295. that copy, use org-occur and incremental search to find a location.
  4296. When pressing RET or `Q', the command returns to the original buffer in
  4297. which the visibility is still unchanged. After RET is will also jump to
  4298. the location selected in the indirect buffer and expose the
  4299. the headline hierarchy above."
  4300. (interactive "P")
  4301. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4302. (org-refile-use-outline-path t)
  4303. (interface
  4304. (if (not alternative-interface)
  4305. org-goto-interface
  4306. (if (eq org-goto-interface 'outline)
  4307. 'outline-path-completion
  4308. 'outline)))
  4309. (org-goto-start-pos (point))
  4310. (selected-point
  4311. (if (eq interface 'outline)
  4312. (car (org-get-location (current-buffer) org-goto-help))
  4313. (nth 3 (org-refile-get-location "Goto: ")))))
  4314. (if selected-point
  4315. (progn
  4316. (org-mark-ring-push org-goto-start-pos)
  4317. (goto-char selected-point)
  4318. (if (or (org-invisible-p) (org-invisible-p2))
  4319. (org-show-context 'org-goto)))
  4320. (message "Quit"))))
  4321. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4322. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4323. (defvar org-goto-local-auto-isearch-map) ; defined below
  4324. (defun org-get-location (buf help)
  4325. "Let the user select a location in the Org-mode buffer BUF.
  4326. This function uses a recursive edit. It returns the selected position
  4327. or nil."
  4328. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4329. (isearch-hide-immediately nil)
  4330. (isearch-search-fun-function
  4331. (lambda () 'org-goto-local-search-headings))
  4332. (org-goto-selected-point org-goto-exit-command))
  4333. (save-excursion
  4334. (save-window-excursion
  4335. (delete-other-windows)
  4336. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4337. (switch-to-buffer
  4338. (condition-case nil
  4339. (make-indirect-buffer (current-buffer) "*org-goto*")
  4340. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4341. (with-output-to-temp-buffer "*Help*"
  4342. (princ help))
  4343. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4344. (setq buffer-read-only nil)
  4345. (let ((org-startup-truncated t)
  4346. (org-startup-folded nil)
  4347. (org-startup-align-all-tables nil))
  4348. (org-mode)
  4349. (org-overview))
  4350. (setq buffer-read-only t)
  4351. (if (and (boundp 'org-goto-start-pos)
  4352. (integer-or-marker-p org-goto-start-pos))
  4353. (let ((org-show-hierarchy-above t)
  4354. (org-show-siblings t)
  4355. (org-show-following-heading t))
  4356. (goto-char org-goto-start-pos)
  4357. (and (org-invisible-p) (org-show-context)))
  4358. (goto-char (point-min)))
  4359. (org-beginning-of-line)
  4360. (message "Select location and press RET")
  4361. (use-local-map org-goto-map)
  4362. (recursive-edit)
  4363. ))
  4364. (kill-buffer "*org-goto*")
  4365. (cons org-goto-selected-point org-goto-exit-command)))
  4366. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4367. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4368. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4369. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4370. (defun org-goto-local-search-headings (string bound noerror)
  4371. "Search and make sure that any matches are in headlines."
  4372. (catch 'return
  4373. (while (if isearch-forward
  4374. (search-forward string bound noerror)
  4375. (search-backward string bound noerror))
  4376. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4377. (and (member :headline context)
  4378. (not (member :tags context))))
  4379. (throw 'return (point))))))
  4380. (defun org-goto-local-auto-isearch ()
  4381. "Start isearch."
  4382. (interactive)
  4383. (goto-char (point-min))
  4384. (let ((keys (this-command-keys)))
  4385. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4386. (isearch-mode t)
  4387. (isearch-process-search-char (string-to-char keys)))))
  4388. (defun org-goto-ret (&optional arg)
  4389. "Finish `org-goto' by going to the new location."
  4390. (interactive "P")
  4391. (setq org-goto-selected-point (point)
  4392. org-goto-exit-command 'return)
  4393. (throw 'exit nil))
  4394. (defun org-goto-left ()
  4395. "Finish `org-goto' by going to the new location."
  4396. (interactive)
  4397. (if (org-on-heading-p)
  4398. (progn
  4399. (beginning-of-line 1)
  4400. (setq org-goto-selected-point (point)
  4401. org-goto-exit-command 'left)
  4402. (throw 'exit nil))
  4403. (error "Not on a heading")))
  4404. (defun org-goto-right ()
  4405. "Finish `org-goto' by going to the new location."
  4406. (interactive)
  4407. (if (org-on-heading-p)
  4408. (progn
  4409. (setq org-goto-selected-point (point)
  4410. org-goto-exit-command 'right)
  4411. (throw 'exit nil))
  4412. (error "Not on a heading")))
  4413. (defun org-goto-quit ()
  4414. "Finish `org-goto' without cursor motion."
  4415. (interactive)
  4416. (setq org-goto-selected-point nil)
  4417. (setq org-goto-exit-command 'quit)
  4418. (throw 'exit nil))
  4419. ;;; Indirect buffer display of subtrees
  4420. (defvar org-indirect-dedicated-frame nil
  4421. "This is the frame being used for indirect tree display.")
  4422. (defvar org-last-indirect-buffer nil)
  4423. (defun org-tree-to-indirect-buffer (&optional arg)
  4424. "Create indirect buffer and narrow it to current subtree.
  4425. With numerical prefix ARG, go up to this level and then take that tree.
  4426. If ARG is negative, go up that many levels.
  4427. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4428. indirect buffer previously made with this command, to avoid proliferation of
  4429. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4430. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4431. is kept so that you can work with several indirect buffers at the same time.
  4432. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4433. requests that a new frame be made for the new buffer, so that the dedicated
  4434. frame is not changed."
  4435. (interactive "P")
  4436. (let ((cbuf (current-buffer))
  4437. (cwin (selected-window))
  4438. (pos (point))
  4439. beg end level heading ibuf)
  4440. (save-excursion
  4441. (org-back-to-heading t)
  4442. (when (numberp arg)
  4443. (setq level (org-outline-level))
  4444. (if (< arg 0) (setq arg (+ level arg)))
  4445. (while (> (setq level (org-outline-level)) arg)
  4446. (outline-up-heading 1 t)))
  4447. (setq beg (point)
  4448. heading (org-get-heading))
  4449. (org-end-of-subtree t) (setq end (point)))
  4450. (if (and (buffer-live-p org-last-indirect-buffer)
  4451. (not (eq org-indirect-buffer-display 'new-frame))
  4452. (not arg))
  4453. (kill-buffer org-last-indirect-buffer))
  4454. (setq ibuf (org-get-indirect-buffer cbuf)
  4455. org-last-indirect-buffer ibuf)
  4456. (cond
  4457. ((or (eq org-indirect-buffer-display 'new-frame)
  4458. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4459. (select-frame (make-frame))
  4460. (delete-other-windows)
  4461. (switch-to-buffer ibuf)
  4462. (org-set-frame-title heading))
  4463. ((eq org-indirect-buffer-display 'dedicated-frame)
  4464. (raise-frame
  4465. (select-frame (or (and org-indirect-dedicated-frame
  4466. (frame-live-p org-indirect-dedicated-frame)
  4467. org-indirect-dedicated-frame)
  4468. (setq org-indirect-dedicated-frame (make-frame)))))
  4469. (delete-other-windows)
  4470. (switch-to-buffer ibuf)
  4471. (org-set-frame-title (concat "Indirect: " heading)))
  4472. ((eq org-indirect-buffer-display 'current-window)
  4473. (switch-to-buffer ibuf))
  4474. ((eq org-indirect-buffer-display 'other-window)
  4475. (pop-to-buffer ibuf))
  4476. (t (error "Invalid value.")))
  4477. (if (featurep 'xemacs)
  4478. (save-excursion (org-mode) (turn-on-font-lock)))
  4479. (narrow-to-region beg end)
  4480. (show-all)
  4481. (goto-char pos)
  4482. (and (window-live-p cwin) (select-window cwin))))
  4483. (defun org-get-indirect-buffer (&optional buffer)
  4484. (setq buffer (or buffer (current-buffer)))
  4485. (let ((n 1) (base (buffer-name buffer)) bname)
  4486. (while (buffer-live-p
  4487. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4488. (setq n (1+ n)))
  4489. (condition-case nil
  4490. (make-indirect-buffer buffer bname 'clone)
  4491. (error (make-indirect-buffer buffer bname)))))
  4492. (defun org-set-frame-title (title)
  4493. "Set the title of the current frame to the string TITLE."
  4494. ;; FIXME: how to name a single frame in XEmacs???
  4495. (unless (featurep 'xemacs)
  4496. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4497. ;;;; Structure editing
  4498. ;;; Inserting headlines
  4499. (defun org-previous-line-empty-p ()
  4500. (save-excursion
  4501. (and (not (bobp))
  4502. (or (beginning-of-line 0) t)
  4503. (save-match-data
  4504. (looking-at "[ \t]*$")))))
  4505. (defun org-insert-heading (&optional force-heading)
  4506. "Insert a new heading or item with same depth at point.
  4507. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4508. If point is at the beginning of a headline, insert a sibling before the
  4509. current headline. If point is not at the beginning, do not split the line,
  4510. but create the new headline after the current line."
  4511. (interactive "P")
  4512. (if (= (buffer-size) 0)
  4513. (insert "\n* ")
  4514. (when (or force-heading (not (org-insert-item)))
  4515. (let* ((empty-line-p nil)
  4516. (head (save-excursion
  4517. (condition-case nil
  4518. (progn
  4519. (org-back-to-heading)
  4520. (setq empty-line-p (org-previous-line-empty-p))
  4521. (match-string 0))
  4522. (error "*"))))
  4523. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4524. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4525. pos hide-previous previous-pos)
  4526. (cond
  4527. ((and (org-on-heading-p) (bolp)
  4528. (or (bobp)
  4529. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4530. ;; insert before the current line
  4531. (open-line (if blank 2 1)))
  4532. ((and (bolp)
  4533. (or (bobp)
  4534. (save-excursion
  4535. (backward-char 1) (not (org-invisible-p)))))
  4536. ;; insert right here
  4537. nil)
  4538. (t
  4539. ;; somewhere in the line
  4540. (save-excursion
  4541. (setq previous-pos (point-at-bol))
  4542. (end-of-line)
  4543. (setq hide-previous (org-invisible-p)))
  4544. (and org-insert-heading-respect-content (org-show-subtree))
  4545. (let ((split
  4546. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4547. (save-excursion
  4548. (let ((p (point)))
  4549. (goto-char (point-at-bol))
  4550. (and (looking-at org-complex-heading-regexp)
  4551. (> p (match-beginning 4)))))))
  4552. tags pos)
  4553. (cond
  4554. (org-insert-heading-respect-content
  4555. (org-end-of-subtree nil t)
  4556. (or (bolp) (newline))
  4557. (or (org-previous-line-empty-p)
  4558. (and blank (newline)))
  4559. (open-line 1))
  4560. ((org-on-heading-p)
  4561. (when hide-previous
  4562. (show-children)
  4563. (org-show-entry))
  4564. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4565. (setq tags (and (match-end 2) (match-string 2)))
  4566. (and (match-end 1)
  4567. (delete-region (match-beginning 1) (match-end 1)))
  4568. (setq pos (point-at-bol))
  4569. (or split (end-of-line 1))
  4570. (delete-horizontal-space)
  4571. (newline (if blank 2 1))
  4572. (when tags
  4573. (save-excursion
  4574. (goto-char pos)
  4575. (end-of-line 1)
  4576. (insert " " tags)
  4577. (org-set-tags nil 'align))))
  4578. (t
  4579. (or split (end-of-line 1))
  4580. (newline (if blank 2 1)))))))
  4581. (insert head) (just-one-space)
  4582. (setq pos (point))
  4583. (end-of-line 1)
  4584. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4585. (when (and org-insert-heading-respect-content hide-previous)
  4586. (save-excursion
  4587. (goto-char previous-pos)
  4588. (hide-subtree)))
  4589. (run-hooks 'org-insert-heading-hook)))))
  4590. (defun org-get-heading (&optional no-tags)
  4591. "Return the heading of the current entry, without the stars."
  4592. (save-excursion
  4593. (org-back-to-heading t)
  4594. (if (looking-at
  4595. (if no-tags
  4596. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4597. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4598. (match-string 1) "")))
  4599. (defun org-heading-components ()
  4600. "Return the components of the current heading.
  4601. This is a list with the following elements:
  4602. - the level as an integer
  4603. - the reduced level, different if `org-odd-levels-only' is set.
  4604. - the TODO keyword, or nil
  4605. - the priority character, like ?A, or nil if no priority is given
  4606. - the headline text itself, or the tags string if no headline text
  4607. - the tags string, or nil."
  4608. (save-excursion
  4609. (org-back-to-heading t)
  4610. (if (looking-at org-complex-heading-regexp)
  4611. (list (length (match-string 1))
  4612. (org-reduced-level (length (match-string 1)))
  4613. (org-match-string-no-properties 2)
  4614. (and (match-end 3) (aref (match-string 3) 2))
  4615. (org-match-string-no-properties 4)
  4616. (org-match-string-no-properties 5)))))
  4617. (defun org-insert-heading-after-current ()
  4618. "Insert a new heading with same level as current, after current subtree."
  4619. (interactive)
  4620. (org-back-to-heading)
  4621. (org-insert-heading)
  4622. (org-move-subtree-down)
  4623. (end-of-line 1))
  4624. (defun org-insert-heading-respect-content ()
  4625. (interactive)
  4626. (let ((org-insert-heading-respect-content t))
  4627. (org-insert-heading t)))
  4628. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4629. (interactive "P")
  4630. (let ((org-insert-heading-respect-content t))
  4631. (org-insert-todo-heading force-state t)))
  4632. (defun org-insert-todo-heading (arg &optional force-heading)
  4633. "Insert a new heading with the same level and TODO state as current heading.
  4634. If the heading has no TODO state, or if the state is DONE, use the first
  4635. state (TODO by default). Also with prefix arg, force first state."
  4636. (interactive "P")
  4637. (when (or force-heading (not (org-insert-item 'checkbox)))
  4638. (org-insert-heading force-heading)
  4639. (save-excursion
  4640. (org-back-to-heading)
  4641. (outline-previous-heading)
  4642. (looking-at org-todo-line-regexp))
  4643. (let*
  4644. ((new-mark-x
  4645. (if (or arg
  4646. (not (match-beginning 2))
  4647. (member (match-string 2) org-done-keywords))
  4648. (car org-todo-keywords-1)
  4649. (match-string 2)))
  4650. (new-mark
  4651. (or
  4652. (run-hook-with-args-until-success
  4653. 'org-todo-get-default-hook new-mark-x nil)
  4654. new-mark-x)))
  4655. (insert new-mark " "))
  4656. (when org-provide-todo-statistics
  4657. (org-update-parent-todo-statistics))))
  4658. (defun org-insert-subheading (arg)
  4659. "Insert a new subheading and demote it.
  4660. Works for outline headings and for plain lists alike."
  4661. (interactive "P")
  4662. (org-insert-heading arg)
  4663. (cond
  4664. ((org-on-heading-p) (org-do-demote))
  4665. ((org-at-item-p) (org-indent-item 1))))
  4666. (defun org-insert-todo-subheading (arg)
  4667. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4668. Works for outline headings and for plain lists alike."
  4669. (interactive "P")
  4670. (org-insert-todo-heading arg)
  4671. (cond
  4672. ((org-on-heading-p) (org-do-demote))
  4673. ((org-at-item-p) (org-indent-item 1))))
  4674. ;;; Promotion and Demotion
  4675. (defun org-promote-subtree ()
  4676. "Promote the entire subtree.
  4677. See also `org-promote'."
  4678. (interactive)
  4679. (save-excursion
  4680. (org-map-tree 'org-promote))
  4681. (org-fix-position-after-promote))
  4682. (defun org-demote-subtree ()
  4683. "Demote the entire subtree. See `org-demote'.
  4684. See also `org-promote'."
  4685. (interactive)
  4686. (save-excursion
  4687. (org-map-tree 'org-demote))
  4688. (org-fix-position-after-promote))
  4689. (defun org-do-promote ()
  4690. "Promote the current heading higher up the tree.
  4691. If the region is active in `transient-mark-mode', promote all headings
  4692. in the region."
  4693. (interactive)
  4694. (save-excursion
  4695. (if (org-region-active-p)
  4696. (org-map-region 'org-promote (region-beginning) (region-end))
  4697. (org-promote)))
  4698. (org-fix-position-after-promote))
  4699. (defun org-do-demote ()
  4700. "Demote the current heading lower down the tree.
  4701. If the region is active in `transient-mark-mode', demote all headings
  4702. in the region."
  4703. (interactive)
  4704. (save-excursion
  4705. (if (org-region-active-p)
  4706. (org-map-region 'org-demote (region-beginning) (region-end))
  4707. (org-demote)))
  4708. (org-fix-position-after-promote))
  4709. (defun org-fix-position-after-promote ()
  4710. "Make sure that after pro/demotion cursor position is right."
  4711. (let ((pos (point)))
  4712. (when (save-excursion
  4713. (beginning-of-line 1)
  4714. (looking-at org-todo-line-regexp)
  4715. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4716. (cond ((eobp) (insert " "))
  4717. ((eolp) (insert " "))
  4718. ((equal (char-after) ?\ ) (forward-char 1))))))
  4719. (defun org-reduced-level (l)
  4720. "Compute the effective level of a heading.
  4721. This takes into account the setting of `org-odd-levels-only'."
  4722. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4723. (defun org-get-valid-level (level &optional change)
  4724. "Rectify a level change under the influence of `org-odd-levels-only'
  4725. LEVEL is a current level, CHANGE is by how much the level should be
  4726. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4727. even level numbers will become the next higher odd number."
  4728. (if org-odd-levels-only
  4729. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4730. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4731. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4732. (max 1 (+ level change))))
  4733. (if (boundp 'define-obsolete-function-alias)
  4734. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4735. (define-obsolete-function-alias 'org-get-legal-level
  4736. 'org-get-valid-level)
  4737. (define-obsolete-function-alias 'org-get-legal-level
  4738. 'org-get-valid-level "23.1")))
  4739. (defun org-promote ()
  4740. "Promote the current heading higher up the tree.
  4741. If the region is active in `transient-mark-mode', promote all headings
  4742. in the region."
  4743. (org-back-to-heading t)
  4744. (let* ((level (save-match-data (funcall outline-level)))
  4745. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4746. (diff (abs (- level (length up-head) -1))))
  4747. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4748. (replace-match up-head nil t)
  4749. ;; Fixup tag positioning
  4750. (and org-auto-align-tags (org-set-tags nil t))
  4751. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4752. (defun org-demote ()
  4753. "Demote the current heading lower down the tree.
  4754. If the region is active in `transient-mark-mode', demote all headings
  4755. in the region."
  4756. (org-back-to-heading t)
  4757. (let* ((level (save-match-data (funcall outline-level)))
  4758. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4759. (diff (abs (- level (length down-head) -1))))
  4760. (replace-match down-head nil t)
  4761. ;; Fixup tag positioning
  4762. (and org-auto-align-tags (org-set-tags nil t))
  4763. (if org-adapt-indentation (org-fixup-indentation diff))))
  4764. (defun org-map-tree (fun)
  4765. "Call FUN for every heading underneath the current one."
  4766. (org-back-to-heading)
  4767. (let ((level (funcall outline-level)))
  4768. (save-excursion
  4769. (funcall fun)
  4770. (while (and (progn
  4771. (outline-next-heading)
  4772. (> (funcall outline-level) level))
  4773. (not (eobp)))
  4774. (funcall fun)))))
  4775. (defun org-map-region (fun beg end)
  4776. "Call FUN for every heading between BEG and END."
  4777. (let ((org-ignore-region t))
  4778. (save-excursion
  4779. (setq end (copy-marker end))
  4780. (goto-char beg)
  4781. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4782. (< (point) end))
  4783. (funcall fun))
  4784. (while (and (progn
  4785. (outline-next-heading)
  4786. (< (point) end))
  4787. (not (eobp)))
  4788. (funcall fun)))))
  4789. (defun org-fixup-indentation (diff)
  4790. "Change the indentation in the current entry by DIFF
  4791. However, if any line in the current entry has no indentation, or if it
  4792. would end up with no indentation after the change, nothing at all is done."
  4793. (save-excursion
  4794. (let ((end (save-excursion (outline-next-heading)
  4795. (point-marker)))
  4796. (prohibit (if (> diff 0)
  4797. "^\\S-"
  4798. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4799. col)
  4800. (unless (save-excursion (end-of-line 1)
  4801. (re-search-forward prohibit end t))
  4802. (while (and (< (point) end)
  4803. (re-search-forward "^[ \t]+" end t))
  4804. (goto-char (match-end 0))
  4805. (setq col (current-column))
  4806. (if (< diff 0) (replace-match ""))
  4807. (org-indent-to-column (+ diff col))))
  4808. (move-marker end nil))))
  4809. (defun org-convert-to-odd-levels ()
  4810. "Convert an org-mode file with all levels allowed to one with odd levels.
  4811. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4812. level 5 etc."
  4813. (interactive)
  4814. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4815. (let ((org-odd-levels-only nil) n)
  4816. (save-excursion
  4817. (goto-char (point-min))
  4818. (while (re-search-forward "^\\*\\*+ " nil t)
  4819. (setq n (- (length (match-string 0)) 2))
  4820. (while (>= (setq n (1- n)) 0)
  4821. (org-demote))
  4822. (end-of-line 1))))))
  4823. (defun org-convert-to-oddeven-levels ()
  4824. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4825. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4826. section with an even level, conversion would destroy the structure of the file. An error
  4827. is signaled in this case."
  4828. (interactive)
  4829. (goto-char (point-min))
  4830. ;; First check if there are no even levels
  4831. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4832. (org-show-context t)
  4833. (error "Not all levels are odd in this file. Conversion not possible."))
  4834. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4835. (let ((org-odd-levels-only nil) n)
  4836. (save-excursion
  4837. (goto-char (point-min))
  4838. (while (re-search-forward "^\\*\\*+ " nil t)
  4839. (setq n (/ (1- (length (match-string 0))) 2))
  4840. (while (>= (setq n (1- n)) 0)
  4841. (org-promote))
  4842. (end-of-line 1))))))
  4843. (defun org-tr-level (n)
  4844. "Make N odd if required."
  4845. (if org-odd-levels-only (1+ (/ n 2)) n))
  4846. ;;; Vertical tree motion, cutting and pasting of subtrees
  4847. (defun org-move-subtree-up (&optional arg)
  4848. "Move the current subtree up past ARG headlines of the same level."
  4849. (interactive "p")
  4850. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4851. (defun org-move-subtree-down (&optional arg)
  4852. "Move the current subtree down past ARG headlines of the same level."
  4853. (interactive "p")
  4854. (setq arg (prefix-numeric-value arg))
  4855. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4856. 'outline-get-last-sibling))
  4857. (ins-point (make-marker))
  4858. (cnt (abs arg))
  4859. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4860. ;; Select the tree
  4861. (org-back-to-heading)
  4862. (setq beg0 (point))
  4863. (save-excursion
  4864. (setq ne-beg (org-back-over-empty-lines))
  4865. (setq beg (point)))
  4866. (save-match-data
  4867. (save-excursion (outline-end-of-heading)
  4868. (setq folded (org-invisible-p)))
  4869. (outline-end-of-subtree))
  4870. (outline-next-heading)
  4871. (setq ne-end (org-back-over-empty-lines))
  4872. (setq end (point))
  4873. (goto-char beg0)
  4874. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4875. ;; include less whitespace
  4876. (save-excursion
  4877. (goto-char beg)
  4878. (forward-line (- ne-beg ne-end))
  4879. (setq beg (point))))
  4880. ;; Find insertion point, with error handling
  4881. (while (> cnt 0)
  4882. (or (and (funcall movfunc) (looking-at outline-regexp))
  4883. (progn (goto-char beg0)
  4884. (error "Cannot move past superior level or buffer limit")))
  4885. (setq cnt (1- cnt)))
  4886. (if (> arg 0)
  4887. ;; Moving forward - still need to move over subtree
  4888. (progn (org-end-of-subtree t t)
  4889. (save-excursion
  4890. (org-back-over-empty-lines)
  4891. (or (bolp) (newline)))))
  4892. (setq ne-ins (org-back-over-empty-lines))
  4893. (move-marker ins-point (point))
  4894. (setq txt (buffer-substring beg end))
  4895. (org-save-markers-in-region beg end)
  4896. (delete-region beg end)
  4897. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4898. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4899. (let ((bbb (point)))
  4900. (insert-before-markers txt)
  4901. (org-reinstall-markers-in-region bbb)
  4902. (move-marker ins-point bbb))
  4903. (or (bolp) (insert "\n"))
  4904. (setq ins-end (point))
  4905. (goto-char ins-point)
  4906. (org-skip-whitespace)
  4907. (when (and (< arg 0)
  4908. (org-first-sibling-p)
  4909. (> ne-ins ne-beg))
  4910. ;; Move whitespace back to beginning
  4911. (save-excursion
  4912. (goto-char ins-end)
  4913. (let ((kill-whole-line t))
  4914. (kill-line (- ne-ins ne-beg)) (point)))
  4915. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4916. (move-marker ins-point nil)
  4917. (org-compact-display-after-subtree-move)
  4918. (org-show-empty-lines-in-parent)
  4919. (unless folded
  4920. (org-show-entry)
  4921. (show-children)
  4922. (org-cycle-hide-drawers 'children))))
  4923. (defvar org-subtree-clip ""
  4924. "Clipboard for cut and paste of subtrees.
  4925. This is actually only a copy of the kill, because we use the normal kill
  4926. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4927. (defvar org-subtree-clip-folded nil
  4928. "Was the last copied subtree folded?
  4929. This is used to fold the tree back after pasting.")
  4930. (defun org-cut-subtree (&optional n)
  4931. "Cut the current subtree into the clipboard.
  4932. With prefix arg N, cut this many sequential subtrees.
  4933. This is a short-hand for marking the subtree and then cutting it."
  4934. (interactive "p")
  4935. (org-copy-subtree n 'cut))
  4936. (defun org-copy-subtree (&optional n cut force-store-markers)
  4937. "Cut the current subtree into the clipboard.
  4938. With prefix arg N, cut this many sequential subtrees.
  4939. This is a short-hand for marking the subtree and then copying it.
  4940. If CUT is non-nil, actually cut the subtree.
  4941. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4942. of some markers in the region, even if CUT is non-nil. This is
  4943. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4944. (interactive "p")
  4945. (let (beg end folded (beg0 (point)))
  4946. (if (interactive-p)
  4947. (org-back-to-heading nil) ; take what looks like a subtree
  4948. (org-back-to-heading t)) ; take what is really there
  4949. (org-back-over-empty-lines)
  4950. (setq beg (point))
  4951. (skip-chars-forward " \t\r\n")
  4952. (save-match-data
  4953. (save-excursion (outline-end-of-heading)
  4954. (setq folded (org-invisible-p)))
  4955. (condition-case nil
  4956. (outline-forward-same-level (1- n))
  4957. (error nil))
  4958. (org-end-of-subtree t t))
  4959. (org-back-over-empty-lines)
  4960. (setq end (point))
  4961. (goto-char beg0)
  4962. (when (> end beg)
  4963. (setq org-subtree-clip-folded folded)
  4964. (when (or cut force-store-markers)
  4965. (org-save-markers-in-region beg end))
  4966. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4967. (setq org-subtree-clip (current-kill 0))
  4968. (message "%s: Subtree(s) with %d characters"
  4969. (if cut "Cut" "Copied")
  4970. (length org-subtree-clip)))))
  4971. (defun org-paste-subtree (&optional level tree for-yank)
  4972. "Paste the clipboard as a subtree, with modification of headline level.
  4973. The entire subtree is promoted or demoted in order to match a new headline
  4974. level.
  4975. If the cursor is at the beginning of a headline, the same level as
  4976. that headline is used to paste the tree
  4977. If not, the new level is derived from the *visible* headings
  4978. before and after the insertion point, and taken to be the inferior headline
  4979. level of the two. So if the previous visible heading is level 3 and the
  4980. next is level 4 (or vice versa), level 4 will be used for insertion.
  4981. This makes sure that the subtree remains an independent subtree and does
  4982. not swallow low level entries.
  4983. You can also force a different level, either by using a numeric prefix
  4984. argument, or by inserting the heading marker by hand. For example, if the
  4985. cursor is after \"*****\", then the tree will be shifted to level 5.
  4986. If optional TREE is given, use this text instead of the kill ring.
  4987. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4988. move back over whitespace before inserting, and move point to the end of
  4989. the inserted text when done."
  4990. (interactive "P")
  4991. (unless (org-kill-is-subtree-p tree)
  4992. (error "%s"
  4993. (substitute-command-keys
  4994. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4995. (let* ((visp (not (org-invisible-p)))
  4996. (txt (or tree (and kill-ring (current-kill 0))))
  4997. (^re (concat "^\\(" outline-regexp "\\)"))
  4998. (re (concat "\\(" outline-regexp "\\)"))
  4999. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5000. (old-level (if (string-match ^re txt)
  5001. (- (match-end 0) (match-beginning 0) 1)
  5002. -1))
  5003. (force-level (cond (level (prefix-numeric-value level))
  5004. ((and (looking-at "[ \t]*$")
  5005. (string-match
  5006. ^re_ (buffer-substring
  5007. (point-at-bol) (point))))
  5008. (- (match-end 1) (match-beginning 1)))
  5009. ((and (bolp)
  5010. (looking-at org-outline-regexp))
  5011. (- (match-end 0) (point) 1))
  5012. (t nil)))
  5013. (previous-level (save-excursion
  5014. (condition-case nil
  5015. (progn
  5016. (outline-previous-visible-heading 1)
  5017. (if (looking-at re)
  5018. (- (match-end 0) (match-beginning 0) 1)
  5019. 1))
  5020. (error 1))))
  5021. (next-level (save-excursion
  5022. (condition-case nil
  5023. (progn
  5024. (or (looking-at outline-regexp)
  5025. (outline-next-visible-heading 1))
  5026. (if (looking-at re)
  5027. (- (match-end 0) (match-beginning 0) 1)
  5028. 1))
  5029. (error 1))))
  5030. (new-level (or force-level (max previous-level next-level)))
  5031. (shift (if (or (= old-level -1)
  5032. (= new-level -1)
  5033. (= old-level new-level))
  5034. 0
  5035. (- new-level old-level)))
  5036. (delta (if (> shift 0) -1 1))
  5037. (func (if (> shift 0) 'org-demote 'org-promote))
  5038. (org-odd-levels-only nil)
  5039. beg end newend)
  5040. ;; Remove the forced level indicator
  5041. (if force-level
  5042. (delete-region (point-at-bol) (point)))
  5043. ;; Paste
  5044. (beginning-of-line 1)
  5045. (unless for-yank (org-back-over-empty-lines))
  5046. (setq beg (point))
  5047. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5048. (insert-before-markers txt)
  5049. (unless (string-match "\n\\'" txt) (insert "\n"))
  5050. (setq newend (point))
  5051. (org-reinstall-markers-in-region beg)
  5052. (setq end (point))
  5053. (goto-char beg)
  5054. (skip-chars-forward " \t\n\r")
  5055. (setq beg (point))
  5056. (if (and (org-invisible-p) visp)
  5057. (save-excursion (outline-show-heading)))
  5058. ;; Shift if necessary
  5059. (unless (= shift 0)
  5060. (save-restriction
  5061. (narrow-to-region beg end)
  5062. (while (not (= shift 0))
  5063. (org-map-region func (point-min) (point-max))
  5064. (setq shift (+ delta shift)))
  5065. (goto-char (point-min))
  5066. (setq newend (point-max))))
  5067. (when (or (interactive-p) for-yank)
  5068. (message "Clipboard pasted as level %d subtree" new-level))
  5069. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5070. kill-ring
  5071. (eq org-subtree-clip (current-kill 0))
  5072. org-subtree-clip-folded)
  5073. ;; The tree was folded before it was killed/copied
  5074. (hide-subtree))
  5075. (and for-yank (goto-char newend))))
  5076. (defun org-kill-is-subtree-p (&optional txt)
  5077. "Check if the current kill is an outline subtree, or a set of trees.
  5078. Returns nil if kill does not start with a headline, or if the first
  5079. headline level is not the largest headline level in the tree.
  5080. So this will actually accept several entries of equal levels as well,
  5081. which is OK for `org-paste-subtree'.
  5082. If optional TXT is given, check this string instead of the current kill."
  5083. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5084. (start-level (and kill
  5085. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5086. org-outline-regexp "\\)")
  5087. kill)
  5088. (- (match-end 2) (match-beginning 2) 1)))
  5089. (re (concat "^" org-outline-regexp))
  5090. (start (1+ (or (match-beginning 2) -1))))
  5091. (if (not start-level)
  5092. (progn
  5093. nil) ;; does not even start with a heading
  5094. (catch 'exit
  5095. (while (setq start (string-match re kill (1+ start)))
  5096. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5097. (throw 'exit nil)))
  5098. t))))
  5099. (defvar org-markers-to-move nil
  5100. "Markers that should be moved with a cut-and-paste operation.
  5101. Those markers are stored together with their positions relative to
  5102. the start of the region.")
  5103. (defun org-save-markers-in-region (beg end)
  5104. "Check markers in region.
  5105. If these markers are between BEG and END, record their position relative
  5106. to BEG, so that after moving the block of text, we can put the markers back
  5107. into place.
  5108. This function gets called just before an entry or tree gets cut from the
  5109. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5110. called immediately, to move the markers with the entries."
  5111. (setq org-markers-to-move nil)
  5112. (when (featurep 'org-clock)
  5113. (org-clock-save-markers-for-cut-and-paste beg end))
  5114. (when (featurep 'org-agenda)
  5115. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5116. (defun org-check-and-save-marker (marker beg end)
  5117. "Check if MARKER is between BEG and END.
  5118. If yes, remember the marker and the distance to BEG."
  5119. (when (and (marker-buffer marker)
  5120. (equal (marker-buffer marker) (current-buffer)))
  5121. (if (and (>= marker beg) (< marker end))
  5122. (push (cons marker (- marker beg)) org-markers-to-move))))
  5123. (defun org-reinstall-markers-in-region (beg)
  5124. "Move all remembered markers to their position relative to BEG."
  5125. (mapc (lambda (x)
  5126. (move-marker (car x) (+ beg (cdr x))))
  5127. org-markers-to-move)
  5128. (setq org-markers-to-move nil))
  5129. (defun org-narrow-to-subtree ()
  5130. "Narrow buffer to the current subtree."
  5131. (interactive)
  5132. (save-excursion
  5133. (save-match-data
  5134. (narrow-to-region
  5135. (progn (org-back-to-heading) (point))
  5136. (progn (org-end-of-subtree t) (point))))))
  5137. ;;; Outline Sorting
  5138. (defun org-sort (with-case)
  5139. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5140. Optional argument WITH-CASE means sort case-sensitively.
  5141. With a double prefix argument, also remove duplicate entries."
  5142. (interactive "P")
  5143. (if (org-at-table-p)
  5144. (org-call-with-arg 'org-table-sort-lines with-case)
  5145. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5146. (defun org-sort-remove-invisible (s)
  5147. (remove-text-properties 0 (length s) org-rm-props s)
  5148. (while (string-match org-bracket-link-regexp s)
  5149. (setq s (replace-match (if (match-end 2)
  5150. (match-string 3 s)
  5151. (match-string 1 s)) t t s)))
  5152. s)
  5153. (defvar org-priority-regexp) ; defined later in the file
  5154. (defun org-sort-entries-or-items
  5155. (&optional with-case sorting-type getkey-func compare-func property)
  5156. "Sort entries on a certain level of an outline tree.
  5157. If there is an active region, the entries in the region are sorted.
  5158. Else, if the cursor is before the first entry, sort the top-level items.
  5159. Else, the children of the entry at point are sorted.
  5160. Sorting can be alphabetically, numerically, and by date/time as given by
  5161. the first time stamp in the entry. The command prompts for the sorting
  5162. type unless it has been given to the function through the SORTING-TYPE
  5163. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5164. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5165. called with point at the beginning of the record. It must return either
  5166. a string or a number that should serve as the sorting key for that record.
  5167. Comparing entries ignores case by default. However, with an optional argument
  5168. WITH-CASE, the sorting considers case as well."
  5169. (interactive "P")
  5170. (let ((case-func (if with-case 'identity 'downcase))
  5171. start beg end stars re re2
  5172. txt what tmp plain-list-p)
  5173. ;; Find beginning and end of region to sort
  5174. (cond
  5175. ((org-region-active-p)
  5176. ;; we will sort the region
  5177. (setq end (region-end)
  5178. what "region")
  5179. (goto-char (region-beginning))
  5180. (if (not (org-on-heading-p)) (outline-next-heading))
  5181. (setq start (point)))
  5182. ((org-at-item-p)
  5183. ;; we will sort this plain list
  5184. (org-beginning-of-item-list) (setq start (point))
  5185. (org-end-of-item-list) (setq end (point))
  5186. (goto-char start)
  5187. (setq plain-list-p t
  5188. what "plain list"))
  5189. ((or (org-on-heading-p)
  5190. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5191. ;; we will sort the children of the current headline
  5192. (org-back-to-heading)
  5193. (setq start (point)
  5194. end (progn (org-end-of-subtree t t)
  5195. (org-back-over-empty-lines)
  5196. (point))
  5197. what "children")
  5198. (goto-char start)
  5199. (show-subtree)
  5200. (outline-next-heading))
  5201. (t
  5202. ;; we will sort the top-level entries in this file
  5203. (goto-char (point-min))
  5204. (or (org-on-heading-p) (outline-next-heading))
  5205. (setq start (point) end (point-max) what "top-level")
  5206. (goto-char start)
  5207. (show-all)))
  5208. (setq beg (point))
  5209. (if (>= beg end) (error "Nothing to sort"))
  5210. (unless plain-list-p
  5211. (looking-at "\\(\\*+\\)")
  5212. (setq stars (match-string 1)
  5213. re (concat "^" (regexp-quote stars) " +")
  5214. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5215. txt (buffer-substring beg end))
  5216. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5217. (if (and (not (equal stars "*")) (string-match re2 txt))
  5218. (error "Region to sort contains a level above the first entry")))
  5219. (unless sorting-type
  5220. (message
  5221. (if plain-list-p
  5222. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5223. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  5224. what)
  5225. (setq sorting-type (read-char-exclusive))
  5226. (and (= (downcase sorting-type) ?f)
  5227. (setq getkey-func
  5228. (org-ido-completing-read "Sort using function: "
  5229. obarray 'fboundp t nil nil))
  5230. (setq getkey-func (intern getkey-func)))
  5231. (and (= (downcase sorting-type) ?r)
  5232. (setq property
  5233. (org-ido-completing-read "Property: "
  5234. (mapcar 'list (org-buffer-property-keys t))
  5235. nil t))))
  5236. (message "Sorting entries...")
  5237. (save-restriction
  5238. (narrow-to-region start end)
  5239. (let ((dcst (downcase sorting-type))
  5240. (now (current-time)))
  5241. (sort-subr
  5242. (/= dcst sorting-type)
  5243. ;; This function moves to the beginning character of the "record" to
  5244. ;; be sorted.
  5245. (if plain-list-p
  5246. (lambda nil
  5247. (if (org-at-item-p) t (goto-char (point-max))))
  5248. (lambda nil
  5249. (if (re-search-forward re nil t)
  5250. (goto-char (match-beginning 0))
  5251. (goto-char (point-max)))))
  5252. ;; This function moves to the last character of the "record" being
  5253. ;; sorted.
  5254. (if plain-list-p
  5255. 'org-end-of-item
  5256. (lambda nil
  5257. (save-match-data
  5258. (condition-case nil
  5259. (outline-forward-same-level 1)
  5260. (error
  5261. (goto-char (point-max)))))))
  5262. ;; This function returns the value that gets sorted against.
  5263. (if plain-list-p
  5264. (lambda nil
  5265. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5266. (cond
  5267. ((= dcst ?n)
  5268. (string-to-number (buffer-substring (match-end 0)
  5269. (point-at-eol))))
  5270. ((= dcst ?a)
  5271. (buffer-substring (match-end 0) (point-at-eol)))
  5272. ((= dcst ?t)
  5273. (if (re-search-forward org-ts-regexp
  5274. (point-at-eol) t)
  5275. (org-time-string-to-time (match-string 0))
  5276. now))
  5277. ((= dcst ?f)
  5278. (if getkey-func
  5279. (progn
  5280. (setq tmp (funcall getkey-func))
  5281. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5282. tmp)
  5283. (error "Invalid key function `%s'" getkey-func)))
  5284. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5285. (lambda nil
  5286. (cond
  5287. ((= dcst ?n)
  5288. (if (looking-at org-complex-heading-regexp)
  5289. (string-to-number (match-string 4))
  5290. nil))
  5291. ((= dcst ?a)
  5292. (if (looking-at org-complex-heading-regexp)
  5293. (funcall case-func (match-string 4))
  5294. nil))
  5295. ((= dcst ?t)
  5296. (if (re-search-forward org-ts-regexp
  5297. (save-excursion
  5298. (forward-line 2)
  5299. (point)) t)
  5300. (org-time-string-to-time (match-string 0))
  5301. now))
  5302. ((= dcst ?p)
  5303. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5304. (string-to-char (match-string 2))
  5305. org-default-priority))
  5306. ((= dcst ?r)
  5307. (or (org-entry-get nil property) ""))
  5308. ((= dcst ?o)
  5309. (if (looking-at org-complex-heading-regexp)
  5310. (- 9999 (length (member (match-string 2)
  5311. org-todo-keywords-1)))))
  5312. ((= dcst ?f)
  5313. (if getkey-func
  5314. (progn
  5315. (setq tmp (funcall getkey-func))
  5316. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5317. tmp)
  5318. (error "Invalid key function `%s'" getkey-func)))
  5319. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5320. nil
  5321. (cond
  5322. ((= dcst ?a) 'string<)
  5323. ((= dcst ?t) 'time-less-p)
  5324. ((= dcst ?f) compare-func)
  5325. (t nil)))))
  5326. (message "Sorting entries...done")))
  5327. (defun org-do-sort (table what &optional with-case sorting-type)
  5328. "Sort TABLE of WHAT according to SORTING-TYPE.
  5329. The user will be prompted for the SORTING-TYPE if the call to this
  5330. function does not specify it. WHAT is only for the prompt, to indicate
  5331. what is being sorted. The sorting key will be extracted from
  5332. the car of the elements of the table.
  5333. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5334. (unless sorting-type
  5335. (message
  5336. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5337. what)
  5338. (setq sorting-type (read-char-exclusive)))
  5339. (let ((dcst (downcase sorting-type))
  5340. extractfun comparefun)
  5341. ;; Define the appropriate functions
  5342. (cond
  5343. ((= dcst ?n)
  5344. (setq extractfun 'string-to-number
  5345. comparefun (if (= dcst sorting-type) '< '>)))
  5346. ((= dcst ?a)
  5347. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5348. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5349. comparefun (if (= dcst sorting-type)
  5350. 'string<
  5351. (lambda (a b) (and (not (string< a b))
  5352. (not (string= a b)))))))
  5353. ((= dcst ?t)
  5354. (setq extractfun
  5355. (lambda (x)
  5356. (if (or (string-match org-ts-regexp x)
  5357. (string-match org-ts-regexp-both x))
  5358. (time-to-seconds
  5359. (org-time-string-to-time (match-string 0 x)))
  5360. 0))
  5361. comparefun (if (= dcst sorting-type) '< '>)))
  5362. (t (error "Invalid sorting type `%c'" sorting-type)))
  5363. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5364. table)
  5365. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5366. ;;; Editing source examples
  5367. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5368. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5369. (defvar org-edit-src-force-single-line nil)
  5370. (defvar org-edit-src-from-org-mode nil)
  5371. (defvar org-edit-src-picture nil)
  5372. (define-minor-mode org-exit-edit-mode
  5373. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5374. (defun org-edit-src-code ()
  5375. "Edit the source code example at point.
  5376. An indirect buffer is created, and that buffer is then narrowed to the
  5377. example at point and switched to the correct language mode. When done,
  5378. exit by killing the buffer with \\[org-edit-src-exit]."
  5379. (interactive)
  5380. (let ((line (org-current-line))
  5381. (case-fold-search t)
  5382. (msg (substitute-command-keys
  5383. "Edit, then exit with C-c ' (C-c and single quote)"))
  5384. (info (org-edit-src-find-region-and-lang))
  5385. (org-mode-p (eq major-mode 'org-mode))
  5386. beg end lang lang-f single lfmt)
  5387. (if (not info)
  5388. nil
  5389. (setq beg (nth 0 info)
  5390. end (nth 1 info)
  5391. lang (nth 2 info)
  5392. single (nth 3 info)
  5393. lfmt (nth 4 info)
  5394. lang-f (intern (concat lang "-mode")))
  5395. (unless (functionp lang-f)
  5396. (error "No such language mode: %s" lang-f))
  5397. (goto-line line)
  5398. (if (get-buffer "*Org Edit Src Example*")
  5399. (kill-buffer "*Org Edit Src Example*"))
  5400. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5401. "*Org Edit Src Example*"))
  5402. (narrow-to-region beg end)
  5403. (remove-text-properties beg end '(display nil invisible nil
  5404. intangible nil))
  5405. (let ((org-inhibit-startup t))
  5406. (funcall lang-f))
  5407. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5408. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5409. (when lfmt
  5410. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5411. (when org-mode-p
  5412. (goto-char (point-min))
  5413. (while (re-search-forward "^," nil t)
  5414. (replace-match "")))
  5415. (goto-line line)
  5416. (org-exit-edit-mode)
  5417. (org-set-local 'header-line-format msg)
  5418. (message "%s" msg)
  5419. t)))
  5420. (defun org-edit-fixed-width-region ()
  5421. "Edit the fixed-width ascii drawing at point.
  5422. This must be a region where each line starts with a colon followed by
  5423. a space character.
  5424. An indirect buffer is created, and that buffer is then narrowed to the
  5425. example at point and switched to artist-mode. When done,
  5426. exit by killing the buffer with \\[org-edit-src-exit]."
  5427. (interactive)
  5428. (let ((line (org-current-line))
  5429. (case-fold-search t)
  5430. (msg (substitute-command-keys
  5431. "Edit, then exit with C-c ' (C-c and single quote)"))
  5432. (org-mode-p (eq major-mode 'org-mode))
  5433. beg end)
  5434. (beginning-of-line 1)
  5435. (if (looking-at "[ \t]*[^:\n \t]")
  5436. nil
  5437. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5438. (setq beg (point) end beg)
  5439. (save-excursion
  5440. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5441. (setq beg (point-at-bol 2))
  5442. (setq beg (point))))
  5443. (save-excursion
  5444. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5445. (setq end (1- (match-beginning 0)))
  5446. (setq end (point))))
  5447. (goto-line line))
  5448. (if (get-buffer "*Org Edit Picture*")
  5449. (kill-buffer "*Org Edit Picture*"))
  5450. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5451. "*Org Edit Picture*"))
  5452. (narrow-to-region beg end)
  5453. (remove-text-properties beg end '(display nil invisible nil
  5454. intangible nil))
  5455. (when (fboundp 'font-lock-unfontify-region)
  5456. (font-lock-unfontify-region (point-min) (point-max)))
  5457. (cond
  5458. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5459. (fundamental-mode)
  5460. (artist-mode 1))
  5461. (t (funcall org-edit-fixed-width-region-mode)))
  5462. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5463. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5464. (set (make-local-variable 'org-edit-src-picture) t)
  5465. (goto-char (point-min))
  5466. (while (re-search-forward "^[ \t]*: ?" nil t)
  5467. (replace-match ""))
  5468. (goto-line line)
  5469. (org-exit-edit-mode)
  5470. (org-set-local 'header-line-format msg)
  5471. (message "%s" msg)
  5472. t)))
  5473. (defun org-edit-src-find-region-and-lang ()
  5474. "Find the region and language for a local edit.
  5475. Return a list with beginning and end of the region, a string representing
  5476. the language, a switch telling of the content should be in a single line."
  5477. (let ((re-list
  5478. (append
  5479. org-edit-src-region-extra
  5480. '(
  5481. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5482. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5483. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5484. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5485. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5486. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5487. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5488. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5489. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5490. ("^#\\+html:" "\n" "html" single-line)
  5491. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5492. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5493. ("^#\\+latex:" "\n" "latex" single-line)
  5494. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5495. ("^#\\+ascii:" "\n" "ascii" single-line)
  5496. )))
  5497. (pos (point))
  5498. re1 re2 single beg end lang lfmt match-re1)
  5499. (catch 'exit
  5500. (while (setq entry (pop re-list))
  5501. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5502. single (nth 3 entry))
  5503. (save-excursion
  5504. (if (or (looking-at re1)
  5505. (re-search-backward re1 nil t))
  5506. (progn
  5507. (setq match-re1 (match-string 0))
  5508. (setq beg (match-end 0)
  5509. lang (org-edit-src-get-lang lang)
  5510. lfmt (org-edit-src-get-label-format match-re1))
  5511. (if (and (re-search-forward re2 nil t)
  5512. (>= (match-end 0) pos))
  5513. (throw 'exit (list beg (match-beginning 0)
  5514. lang single lfmt))))
  5515. (if (or (looking-at re2)
  5516. (re-search-forward re2 nil t))
  5517. (progn
  5518. (setq end (match-beginning 0))
  5519. (if (and (re-search-backward re1 nil t)
  5520. (<= (match-beginning 0) pos))
  5521. (progn
  5522. (setq lfmt (org-edit-src-get-label-format
  5523. (match-string 0)))
  5524. (throw 'exit
  5525. (list (match-end 0) end
  5526. (org-edit-src-get-lang lang)
  5527. single lfmt))))))))))))
  5528. (defun org-edit-src-get-lang (lang)
  5529. "Extract the src language."
  5530. (let ((m (match-string 0)))
  5531. (cond
  5532. ((stringp lang) lang)
  5533. ((integerp lang) (match-string lang))
  5534. ((and (eq lang 'lang)
  5535. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5536. (match-string 1 m))
  5537. ((and (eq lang 'style)
  5538. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5539. (match-string 1 m))
  5540. (t "fundamental"))))
  5541. (defun org-edit-src-get-label-format (s)
  5542. "Extract the label format."
  5543. (save-match-data
  5544. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5545. (match-string 1 s))))
  5546. (defun org-edit-src-exit ()
  5547. "Exit special edit and protect problematic lines."
  5548. (interactive)
  5549. (unless (buffer-base-buffer (current-buffer))
  5550. (error "This is not an indirect buffer, something is wrong..."))
  5551. (unless (> (point-min) 1)
  5552. (error "This buffer is not narrowed, something is wrong..."))
  5553. (goto-char (point-min))
  5554. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5555. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5556. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5557. (goto-char (point-min))
  5558. (while (re-search-forward "\n" nil t)
  5559. (replace-match " "))
  5560. (goto-char (point-min))
  5561. (if (looking-at "\\s-*") (replace-match " "))
  5562. (if (re-search-forward "\\s-+\\'" nil t)
  5563. (replace-match "")))
  5564. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5565. (goto-char (point-min))
  5566. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5567. (replace-match ",\\1"))
  5568. (when font-lock-mode
  5569. (font-lock-unfontify-region (point-min) (point-max)))
  5570. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5571. (when (org-bound-and-true-p org-edit-src-picture)
  5572. (untabify (point-min) (point-max))
  5573. (goto-char (point-min))
  5574. (while (re-search-forward "^" nil t)
  5575. (replace-match ": "))
  5576. (when font-lock-mode
  5577. (font-lock-unfontify-region (point-min) (point-max)))
  5578. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5579. (kill-buffer (current-buffer))
  5580. (and (org-mode-p) (org-restart-font-lock)))
  5581. ;;; The orgstruct minor mode
  5582. ;; Define a minor mode which can be used in other modes in order to
  5583. ;; integrate the org-mode structure editing commands.
  5584. ;; This is really a hack, because the org-mode structure commands use
  5585. ;; keys which normally belong to the major mode. Here is how it
  5586. ;; works: The minor mode defines all the keys necessary to operate the
  5587. ;; structure commands, but wraps the commands into a function which
  5588. ;; tests if the cursor is currently at a headline or a plain list
  5589. ;; item. If that is the case, the structure command is used,
  5590. ;; temporarily setting many Org-mode variables like regular
  5591. ;; expressions for filling etc. However, when any of those keys is
  5592. ;; used at a different location, function uses `key-binding' to look
  5593. ;; up if the key has an associated command in another currently active
  5594. ;; keymap (minor modes, major mode, global), and executes that
  5595. ;; command. There might be problems if any of the keys is otherwise
  5596. ;; used as a prefix key.
  5597. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5598. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5599. ;; addresses this by checking explicitly for both bindings.
  5600. (defvar orgstruct-mode-map (make-sparse-keymap)
  5601. "Keymap for the minor `orgstruct-mode'.")
  5602. (defvar org-local-vars nil
  5603. "List of local variables, for use by `orgstruct-mode'")
  5604. ;;;###autoload
  5605. (define-minor-mode orgstruct-mode
  5606. "Toggle the minor more `orgstruct-mode'.
  5607. This mode is for using Org-mode structure commands in other modes.
  5608. The following key behave as if Org-mode was active, if the cursor
  5609. is on a headline, or on a plain list item (both in the definition
  5610. of Org-mode).
  5611. M-up Move entry/item up
  5612. M-down Move entry/item down
  5613. M-left Promote
  5614. M-right Demote
  5615. M-S-up Move entry/item up
  5616. M-S-down Move entry/item down
  5617. M-S-left Promote subtree
  5618. M-S-right Demote subtree
  5619. M-q Fill paragraph and items like in Org-mode
  5620. C-c ^ Sort entries
  5621. C-c - Cycle list bullet
  5622. TAB Cycle item visibility
  5623. M-RET Insert new heading/item
  5624. S-M-RET Insert new TODO heading / Checkbox item
  5625. C-c C-c Set tags / toggle checkbox"
  5626. nil " OrgStruct" nil
  5627. (org-load-modules-maybe)
  5628. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5629. ;;;###autoload
  5630. (defun turn-on-orgstruct ()
  5631. "Unconditionally turn on `orgstruct-mode'."
  5632. (orgstruct-mode 1))
  5633. ;;;###autoload
  5634. (defun turn-on-orgstruct++ ()
  5635. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5636. In addition to setting orgstruct-mode, this also exports all indentation and
  5637. autofilling variables from org-mode into the buffer. Note that turning
  5638. off orgstruct-mode will *not* remove these additional settings."
  5639. (orgstruct-mode 1)
  5640. (let (var val)
  5641. (mapc
  5642. (lambda (x)
  5643. (when (string-match
  5644. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5645. (symbol-name (car x)))
  5646. (setq var (car x) val (nth 1 x))
  5647. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5648. org-local-vars)))
  5649. (defun orgstruct-error ()
  5650. "Error when there is no default binding for a structure key."
  5651. (interactive)
  5652. (error "This key has no function outside structure elements"))
  5653. (defun orgstruct-setup ()
  5654. "Setup orgstruct keymaps."
  5655. (let ((nfunc 0)
  5656. (bindings
  5657. (list
  5658. '([(meta up)] org-metaup)
  5659. '([(meta down)] org-metadown)
  5660. '([(meta left)] org-metaleft)
  5661. '([(meta right)] org-metaright)
  5662. '([(meta shift up)] org-shiftmetaup)
  5663. '([(meta shift down)] org-shiftmetadown)
  5664. '([(meta shift left)] org-shiftmetaleft)
  5665. '([(meta shift right)] org-shiftmetaright)
  5666. '([(shift up)] org-shiftup)
  5667. '([(shift down)] org-shiftdown)
  5668. '([(shift left)] org-shiftleft)
  5669. '([(shift right)] org-shiftright)
  5670. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5671. '("\M-q" fill-paragraph)
  5672. '("\C-c^" org-sort)
  5673. '("\C-c-" org-cycle-list-bullet)))
  5674. elt key fun cmd)
  5675. (while (setq elt (pop bindings))
  5676. (setq nfunc (1+ nfunc))
  5677. (setq key (org-key (car elt))
  5678. fun (nth 1 elt)
  5679. cmd (orgstruct-make-binding fun nfunc key))
  5680. (org-defkey orgstruct-mode-map key cmd))
  5681. ;; Special treatment needed for TAB and RET
  5682. (org-defkey orgstruct-mode-map [(tab)]
  5683. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5684. (org-defkey orgstruct-mode-map "\C-i"
  5685. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5686. (org-defkey orgstruct-mode-map "\M-\C-m"
  5687. (orgstruct-make-binding 'org-insert-heading 105
  5688. "\M-\C-m" [(meta return)]))
  5689. (org-defkey orgstruct-mode-map [(meta return)]
  5690. (orgstruct-make-binding 'org-insert-heading 106
  5691. [(meta return)] "\M-\C-m"))
  5692. (org-defkey orgstruct-mode-map [(shift meta return)]
  5693. (orgstruct-make-binding 'org-insert-todo-heading 107
  5694. [(meta return)] "\M-\C-m"))
  5695. (unless org-local-vars
  5696. (setq org-local-vars (org-get-local-variables)))
  5697. t))
  5698. (defun orgstruct-make-binding (fun n &rest keys)
  5699. "Create a function for binding in the structure minor mode.
  5700. FUN is the command to call inside a table. N is used to create a unique
  5701. command name. KEYS are keys that should be checked in for a command
  5702. to execute outside of tables."
  5703. (eval
  5704. (list 'defun
  5705. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5706. '(arg)
  5707. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5708. "Outside of structure, run the binding of `"
  5709. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5710. "'.")
  5711. '(interactive "p")
  5712. (list 'if
  5713. '(org-context-p 'headline 'item)
  5714. (list 'org-run-like-in-org-mode (list 'quote fun))
  5715. (list 'let '(orgstruct-mode)
  5716. (list 'call-interactively
  5717. (append '(or)
  5718. (mapcar (lambda (k)
  5719. (list 'key-binding k))
  5720. keys)
  5721. '('orgstruct-error))))))))
  5722. (defun org-context-p (&rest contexts)
  5723. "Check if local context is any of CONTEXTS.
  5724. Possible values in the list of contexts are `table', `headline', and `item'."
  5725. (let ((pos (point)))
  5726. (goto-char (point-at-bol))
  5727. (prog1 (or (and (memq 'table contexts)
  5728. (looking-at "[ \t]*|"))
  5729. (and (memq 'headline contexts)
  5730. ;;????????? (looking-at "\\*+"))
  5731. (looking-at outline-regexp))
  5732. (and (memq 'item contexts)
  5733. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5734. (goto-char pos))))
  5735. (defun org-get-local-variables ()
  5736. "Return a list of all local variables in an org-mode buffer."
  5737. (let (varlist)
  5738. (with-current-buffer (get-buffer-create "*Org tmp*")
  5739. (erase-buffer)
  5740. (org-mode)
  5741. (setq varlist (buffer-local-variables)))
  5742. (kill-buffer "*Org tmp*")
  5743. (delq nil
  5744. (mapcar
  5745. (lambda (x)
  5746. (setq x
  5747. (if (symbolp x)
  5748. (list x)
  5749. (list (car x) (list 'quote (cdr x)))))
  5750. (if (string-match
  5751. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5752. (symbol-name (car x)))
  5753. x nil))
  5754. varlist))))
  5755. ;;;###autoload
  5756. (defun org-run-like-in-org-mode (cmd)
  5757. (org-load-modules-maybe)
  5758. (unless org-local-vars
  5759. (setq org-local-vars (org-get-local-variables)))
  5760. (eval (list 'let org-local-vars
  5761. (list 'call-interactively (list 'quote cmd)))))
  5762. ;;;; Archiving
  5763. (defun org-get-category (&optional pos)
  5764. "Get the category applying to position POS."
  5765. (get-text-property (or pos (point)) 'org-category))
  5766. (defun org-refresh-category-properties ()
  5767. "Refresh category text properties in the buffer."
  5768. (let ((def-cat (cond
  5769. ((null org-category)
  5770. (if buffer-file-name
  5771. (file-name-sans-extension
  5772. (file-name-nondirectory buffer-file-name))
  5773. "???"))
  5774. ((symbolp org-category) (symbol-name org-category))
  5775. (t org-category)))
  5776. beg end cat pos optionp)
  5777. (org-unmodified
  5778. (save-excursion
  5779. (save-restriction
  5780. (widen)
  5781. (goto-char (point-min))
  5782. (put-text-property (point) (point-max) 'org-category def-cat)
  5783. (while (re-search-forward
  5784. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5785. (setq pos (match-end 0)
  5786. optionp (equal (char-after (match-beginning 0)) ?#)
  5787. cat (org-trim (match-string 2)))
  5788. (if optionp
  5789. (setq beg (point-at-bol) end (point-max))
  5790. (org-back-to-heading t)
  5791. (setq beg (point) end (org-end-of-subtree t t)))
  5792. (put-text-property beg end 'org-category cat)
  5793. (goto-char pos)))))))
  5794. ;;;; Link Stuff
  5795. ;;; Link abbreviations
  5796. (defun org-link-expand-abbrev (link)
  5797. "Apply replacements as defined in `org-link-abbrev-alist."
  5798. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5799. (let* ((key (match-string 1 link))
  5800. (as (or (assoc key org-link-abbrev-alist-local)
  5801. (assoc key org-link-abbrev-alist)))
  5802. (tag (and (match-end 2) (match-string 3 link)))
  5803. rpl)
  5804. (if (not as)
  5805. link
  5806. (setq rpl (cdr as))
  5807. (cond
  5808. ((symbolp rpl) (funcall rpl tag))
  5809. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5810. ((string-match "%h" rpl)
  5811. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5812. (t (concat rpl tag)))))
  5813. link))
  5814. ;;; Storing and inserting links
  5815. (defvar org-insert-link-history nil
  5816. "Minibuffer history for links inserted with `org-insert-link'.")
  5817. (defvar org-stored-links nil
  5818. "Contains the links stored with `org-store-link'.")
  5819. (defvar org-store-link-plist nil
  5820. "Plist with info about the most recently link created with `org-store-link'.")
  5821. (defvar org-link-protocols nil
  5822. "Link protocols added to Org-mode using `org-add-link-type'.")
  5823. (defvar org-store-link-functions nil
  5824. "List of functions that are called to create and store a link.
  5825. Each function will be called in turn until one returns a non-nil
  5826. value. Each function should check if it is responsible for creating
  5827. this link (for example by looking at the major mode).
  5828. If not, it must exit and return nil.
  5829. If yes, it should return a non-nil value after a calling
  5830. `org-store-link-props' with a list of properties and values.
  5831. Special properties are:
  5832. :type The link prefix. like \"http\". This must be given.
  5833. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5834. This is obligatory as well.
  5835. :description Optional default description for the second pair
  5836. of brackets in an Org-mode link. The user can still change
  5837. this when inserting this link into an Org-mode buffer.
  5838. In addition to these, any additional properties can be specified
  5839. and then used in remember templates.")
  5840. (defun org-add-link-type (type &optional follow export)
  5841. "Add TYPE to the list of `org-link-types'.
  5842. Re-compute all regular expressions depending on `org-link-types'
  5843. FOLLOW and EXPORT are two functions.
  5844. FOLLOW should take the link path as the single argument and do whatever
  5845. is necessary to follow the link, for example find a file or display
  5846. a mail message.
  5847. EXPORT should format the link path for export to one of the export formats.
  5848. It should be a function accepting three arguments:
  5849. path the path of the link, the text after the prefix (like \"http:\")
  5850. desc the description of the link, if any, nil if there was no description
  5851. format the export format, a symbol like `html' or `latex'.
  5852. The function may use the FORMAT information to return different values
  5853. depending on the format. The return value will be put literally into
  5854. the exported file.
  5855. Org-mode has a built-in default for exporting links. If you are happy with
  5856. this default, there is no need to define an export function for the link
  5857. type. For a simple example of an export function, see `org-bbdb.el'."
  5858. (add-to-list 'org-link-types type t)
  5859. (org-make-link-regexps)
  5860. (if (assoc type org-link-protocols)
  5861. (setcdr (assoc type org-link-protocols) (list follow export))
  5862. (push (list type follow export) org-link-protocols)))
  5863. ;;;###autoload
  5864. (defun org-store-link (arg)
  5865. "\\<org-mode-map>Store an org-link to the current location.
  5866. This link is added to `org-stored-links' and can later be inserted
  5867. into an org-buffer with \\[org-insert-link].
  5868. For some link types, a prefix arg is interpreted:
  5869. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5870. For file links, arg negates `org-context-in-file-links'."
  5871. (interactive "P")
  5872. (org-load-modules-maybe)
  5873. (setq org-store-link-plist nil) ; reset
  5874. (let (link cpltxt desc description search txt)
  5875. (cond
  5876. ((run-hook-with-args-until-success 'org-store-link-functions)
  5877. (setq link (plist-get org-store-link-plist :link)
  5878. desc (or (plist-get org-store-link-plist :description) link)))
  5879. ((equal (buffer-name) "*Org Edit Src Example*")
  5880. (let (label gc)
  5881. (while (or (not label)
  5882. (save-excursion
  5883. (save-restriction
  5884. (widen)
  5885. (goto-char (point-min))
  5886. (re-search-forward
  5887. (regexp-quote (format org-coderef-label-format label))
  5888. nil t))))
  5889. (when label (message "Label exists already") (sit-for 2))
  5890. (setq label (read-string "Code line label: " label)))
  5891. (end-of-line 1)
  5892. (setq link (format org-coderef-label-format label))
  5893. (setq gc (- 79 (length link)))
  5894. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5895. (insert link)
  5896. (setq link (concat "(" label ")") desc nil)))
  5897. ((eq major-mode 'calendar-mode)
  5898. (let ((cd (calendar-cursor-to-date)))
  5899. (setq link
  5900. (format-time-string
  5901. (car org-time-stamp-formats)
  5902. (apply 'encode-time
  5903. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5904. nil nil nil))))
  5905. (org-store-link-props :type "calendar" :date cd)))
  5906. ((eq major-mode 'w3-mode)
  5907. (setq cpltxt (url-view-url t)
  5908. link (org-make-link cpltxt))
  5909. (org-store-link-props :type "w3" :url (url-view-url t)))
  5910. ((eq major-mode 'w3m-mode)
  5911. (setq cpltxt (or w3m-current-title w3m-current-url)
  5912. link (org-make-link w3m-current-url))
  5913. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5914. ((setq search (run-hook-with-args-until-success
  5915. 'org-create-file-search-functions))
  5916. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5917. "::" search))
  5918. (setq cpltxt (or description link)))
  5919. ((eq major-mode 'image-mode)
  5920. (setq cpltxt (concat "file:"
  5921. (abbreviate-file-name buffer-file-name))
  5922. link (org-make-link cpltxt))
  5923. (org-store-link-props :type "image" :file buffer-file-name))
  5924. ((eq major-mode 'dired-mode)
  5925. ;; link to the file in the current line
  5926. (setq cpltxt (concat "file:"
  5927. (abbreviate-file-name
  5928. (expand-file-name
  5929. (dired-get-filename nil t))))
  5930. link (org-make-link cpltxt)))
  5931. ((and buffer-file-name (org-mode-p))
  5932. (cond
  5933. ((org-in-regexp "<<\\(.*?\\)>>")
  5934. (setq cpltxt
  5935. (concat "file:"
  5936. (abbreviate-file-name buffer-file-name)
  5937. "::" (match-string 1))
  5938. link (org-make-link cpltxt)))
  5939. ((and (featurep 'org-id)
  5940. (or (eq org-link-to-org-use-id t)
  5941. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5942. (interactive-p))
  5943. (and org-link-to-org-use-id
  5944. (condition-case nil
  5945. (org-entry-get nil "ID")
  5946. (error nil)))))
  5947. ;; We can make a link using the ID.
  5948. (setq link (condition-case nil
  5949. (prog1 (org-id-store-link)
  5950. (setq desc (plist-get org-store-link-plist
  5951. :description)))
  5952. (error
  5953. ;; probably before first headline, link to file only
  5954. (concat "file:"
  5955. (abbreviate-file-name buffer-file-name))))))
  5956. (t
  5957. ;; Just link to current headline
  5958. (setq cpltxt (concat "file:"
  5959. (abbreviate-file-name buffer-file-name)))
  5960. ;; Add a context search string
  5961. (when (org-xor org-context-in-file-links arg)
  5962. (setq txt (cond
  5963. ((org-on-heading-p) nil)
  5964. ((org-region-active-p)
  5965. (buffer-substring (region-beginning) (region-end)))
  5966. (t nil)))
  5967. (when (or (null txt) (string-match "\\S-" txt))
  5968. (setq cpltxt
  5969. (concat cpltxt "::"
  5970. (condition-case nil
  5971. (org-make-org-heading-search-string txt)
  5972. (error "")))
  5973. desc "NONE")))
  5974. (if (string-match "::\\'" cpltxt)
  5975. (setq cpltxt (substring cpltxt 0 -2)))
  5976. (setq link (org-make-link cpltxt)))))
  5977. ((buffer-file-name (buffer-base-buffer))
  5978. ;; Just link to this file here.
  5979. (setq cpltxt (concat "file:"
  5980. (abbreviate-file-name
  5981. (buffer-file-name (buffer-base-buffer)))))
  5982. ;; Add a context string
  5983. (when (org-xor org-context-in-file-links arg)
  5984. (setq txt (if (org-region-active-p)
  5985. (buffer-substring (region-beginning) (region-end))
  5986. (buffer-substring (point-at-bol) (point-at-eol))))
  5987. ;; Only use search option if there is some text.
  5988. (when (string-match "\\S-" txt)
  5989. (setq cpltxt
  5990. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5991. desc "NONE")))
  5992. (setq link (org-make-link cpltxt)))
  5993. ((interactive-p)
  5994. (error "Cannot link to a buffer which is not visiting a file"))
  5995. (t (setq link nil)))
  5996. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5997. (setq link (or link cpltxt)
  5998. desc (or desc cpltxt))
  5999. (if (equal desc "NONE") (setq desc nil))
  6000. (if (and (interactive-p) link)
  6001. (progn
  6002. (setq org-stored-links
  6003. (cons (list link desc) org-stored-links))
  6004. (message "Stored: %s" (or desc link)))
  6005. (and link (org-make-link-string link desc)))))
  6006. (defun org-store-link-props (&rest plist)
  6007. "Store link properties, extract names and addresses."
  6008. (let (x adr)
  6009. (when (setq x (plist-get plist :from))
  6010. (setq adr (mail-extract-address-components x))
  6011. (setq plist (plist-put plist :fromname (car adr)))
  6012. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6013. (when (setq x (plist-get plist :to))
  6014. (setq adr (mail-extract-address-components x))
  6015. (setq plist (plist-put plist :toname (car adr)))
  6016. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6017. (let ((from (plist-get plist :from))
  6018. (to (plist-get plist :to)))
  6019. (when (and from to org-from-is-user-regexp)
  6020. (setq plist
  6021. (plist-put plist :fromto
  6022. (if (string-match org-from-is-user-regexp from)
  6023. (concat "to %t")
  6024. (concat "from %f"))))))
  6025. (setq org-store-link-plist plist))
  6026. (defun org-add-link-props (&rest plist)
  6027. "Add these properties to the link property list."
  6028. (let (key value)
  6029. (while plist
  6030. (setq key (pop plist) value (pop plist))
  6031. (setq org-store-link-plist
  6032. (plist-put org-store-link-plist key value)))))
  6033. (defun org-email-link-description (&optional fmt)
  6034. "Return the description part of an email link.
  6035. This takes information from `org-store-link-plist' and formats it
  6036. according to FMT (default from `org-email-link-description-format')."
  6037. (setq fmt (or fmt org-email-link-description-format))
  6038. (let* ((p org-store-link-plist)
  6039. (to (plist-get p :toaddress))
  6040. (from (plist-get p :fromaddress))
  6041. (table
  6042. (list
  6043. (cons "%c" (plist-get p :fromto))
  6044. (cons "%F" (plist-get p :from))
  6045. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6046. (cons "%T" (plist-get p :to))
  6047. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6048. (cons "%s" (plist-get p :subject))
  6049. (cons "%m" (plist-get p :message-id)))))
  6050. (when (string-match "%c" fmt)
  6051. ;; Check if the user wrote this message
  6052. (if (and org-from-is-user-regexp from to
  6053. (save-match-data (string-match org-from-is-user-regexp from)))
  6054. (setq fmt (replace-match "to %t" t t fmt))
  6055. (setq fmt (replace-match "from %f" t t fmt))))
  6056. (org-replace-escapes fmt table)))
  6057. (defun org-make-org-heading-search-string (&optional string heading)
  6058. "Make search string for STRING or current headline."
  6059. (interactive)
  6060. (let ((s (or string (org-get-heading))))
  6061. (unless (and string (not heading))
  6062. ;; We are using a headline, clean up garbage in there.
  6063. (if (string-match org-todo-regexp s)
  6064. (setq s (replace-match "" t t s)))
  6065. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6066. (setq s (replace-match "" t t s)))
  6067. (setq s (org-trim s))
  6068. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6069. org-comment-string "\\)") s)
  6070. (setq s (replace-match "" t t s)))
  6071. (while (string-match org-ts-regexp s)
  6072. (setq s (replace-match "" t t s))))
  6073. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6074. (setq s (replace-match " " t t s)))
  6075. (or string (setq s (concat "*" s))) ; Add * for headlines
  6076. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6077. (defun org-make-link (&rest strings)
  6078. "Concatenate STRINGS."
  6079. (apply 'concat strings))
  6080. (defun org-make-link-string (link &optional description)
  6081. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6082. (unless (string-match "\\S-" link)
  6083. (error "Empty link"))
  6084. (when (stringp description)
  6085. ;; Remove brackets from the description, they are fatal.
  6086. (while (string-match "\\[" description)
  6087. (setq description (replace-match "{" t t description)))
  6088. (while (string-match "\\]" description)
  6089. (setq description (replace-match "}" t t description))))
  6090. (when (equal (org-link-escape link) description)
  6091. ;; No description needed, it is identical
  6092. (setq description nil))
  6093. (when (and (not description)
  6094. (not (equal link (org-link-escape link))))
  6095. (setq description (org-extract-attributes link)))
  6096. (concat "[[" (org-link-escape link) "]"
  6097. (if description (concat "[" description "]") "")
  6098. "]"))
  6099. (defconst org-link-escape-chars
  6100. '((?\ . "%20")
  6101. (?\[ . "%5B")
  6102. (?\] . "%5D")
  6103. (?\340 . "%E0") ; `a
  6104. (?\342 . "%E2") ; ^a
  6105. (?\347 . "%E7") ; ,c
  6106. (?\350 . "%E8") ; `e
  6107. (?\351 . "%E9") ; 'e
  6108. (?\352 . "%EA") ; ^e
  6109. (?\356 . "%EE") ; ^i
  6110. (?\364 . "%F4") ; ^o
  6111. (?\371 . "%F9") ; `u
  6112. (?\373 . "%FB") ; ^u
  6113. (?\; . "%3B")
  6114. (?? . "%3F")
  6115. (?= . "%3D")
  6116. (?+ . "%2B")
  6117. )
  6118. "Association list of escapes for some characters problematic in links.
  6119. This is the list that is used for internal purposes.")
  6120. (defconst org-link-escape-chars-browser
  6121. '((?\ . "%20")) ; 32 for the SPC char
  6122. "Association list of escapes for some characters problematic in links.
  6123. This is the list that is used before handing over to the browser.")
  6124. (defun org-link-escape (text &optional table)
  6125. "Escape characters in TEXT that are problematic for links."
  6126. (setq table (or table org-link-escape-chars))
  6127. (when text
  6128. (let ((re (mapconcat (lambda (x) (regexp-quote
  6129. (char-to-string (car x))))
  6130. table "\\|")))
  6131. (while (string-match re text)
  6132. (setq text
  6133. (replace-match
  6134. (cdr (assoc (string-to-char (match-string 0 text))
  6135. table))
  6136. t t text)))
  6137. text)))
  6138. (defun org-link-unescape (text &optional table)
  6139. "Reverse the action of `org-link-escape'."
  6140. (setq table (or table org-link-escape-chars))
  6141. (when text
  6142. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6143. table "\\|")))
  6144. (while (string-match re text)
  6145. (setq text
  6146. (replace-match
  6147. (char-to-string (car (rassoc (match-string 0 text) table)))
  6148. t t text)))
  6149. text)))
  6150. (defun org-xor (a b)
  6151. "Exclusive or."
  6152. (if a (not b) b))
  6153. (defun org-fixup-message-id-for-http (s)
  6154. "Replace special characters in a message id, so it can be used in an http query."
  6155. (while (string-match "<" s)
  6156. (setq s (replace-match "%3C" t t s)))
  6157. (while (string-match ">" s)
  6158. (setq s (replace-match "%3E" t t s)))
  6159. (while (string-match "@" s)
  6160. (setq s (replace-match "%40" t t s)))
  6161. s)
  6162. ;;;###autoload
  6163. (defun org-insert-link-global ()
  6164. "Insert a link like Org-mode does.
  6165. This command can be called in any mode to insert a link in Org-mode syntax."
  6166. (interactive)
  6167. (org-load-modules-maybe)
  6168. (org-run-like-in-org-mode 'org-insert-link))
  6169. (defun org-insert-link (&optional complete-file link-location)
  6170. "Insert a link. At the prompt, enter the link.
  6171. Completion can be used to insert any of the link protocol prefixes like
  6172. http or ftp in use.
  6173. The history can be used to select a link previously stored with
  6174. `org-store-link'. When the empty string is entered (i.e. if you just
  6175. press RET at the prompt), the link defaults to the most recently
  6176. stored link. As SPC triggers completion in the minibuffer, you need to
  6177. use M-SPC or C-q SPC to force the insertion of a space character.
  6178. You will also be prompted for a description, and if one is given, it will
  6179. be displayed in the buffer instead of the link.
  6180. If there is already a link at point, this command will allow you to edit link
  6181. and description parts.
  6182. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6183. be selected using completion. The path to the file will be relative to the
  6184. current directory if the file is in the current directory or a subdirectory.
  6185. Otherwise, the link will be the absolute path as completed in the minibuffer
  6186. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6187. option `org-link-file-path-type'.
  6188. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6189. the current directory or below.
  6190. With three \\[universal-argument] prefixes, negate the meaning of
  6191. `org-keep-stored-link-after-insertion'.
  6192. If `org-make-link-description-function' is non-nil, this function will be
  6193. called with the link target, and the result will be the default
  6194. link description.
  6195. If the LINK-LOCATION parameter is non-nil, this value will be
  6196. used as the link location instead of reading one interactively."
  6197. (interactive "P")
  6198. (let* ((wcf (current-window-configuration))
  6199. (region (if (org-region-active-p)
  6200. (buffer-substring (region-beginning) (region-end))))
  6201. (remove (and region (list (region-beginning) (region-end))))
  6202. (desc region)
  6203. tmphist ; byte-compile incorrectly complains about this
  6204. (link link-location)
  6205. entry file)
  6206. (cond
  6207. (link-location) ; specified by arg, just use it.
  6208. ((org-in-regexp org-bracket-link-regexp 1)
  6209. ;; We do have a link at point, and we are going to edit it.
  6210. (setq remove (list (match-beginning 0) (match-end 0)))
  6211. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6212. (setq link (read-string "Link: "
  6213. (org-link-unescape
  6214. (org-match-string-no-properties 1)))))
  6215. ((or (org-in-regexp org-angle-link-re)
  6216. (org-in-regexp org-plain-link-re))
  6217. ;; Convert to bracket link
  6218. (setq remove (list (match-beginning 0) (match-end 0))
  6219. link (read-string "Link: "
  6220. (org-remove-angle-brackets (match-string 0)))))
  6221. ((member complete-file '((4) (16)))
  6222. ;; Completing read for file names.
  6223. (setq file (read-file-name "File: "))
  6224. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6225. (pwd1 (file-name-as-directory (abbreviate-file-name
  6226. (expand-file-name ".")))))
  6227. (cond
  6228. ((equal complete-file '(16))
  6229. (setq link (org-make-link
  6230. "file:"
  6231. (abbreviate-file-name (expand-file-name file)))))
  6232. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6233. (setq link (org-make-link "file:" (match-string 1 file))))
  6234. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6235. (expand-file-name file))
  6236. (setq link (org-make-link
  6237. "file:" (match-string 1 (expand-file-name file)))))
  6238. (t (setq link (org-make-link "file:" file))))))
  6239. (t
  6240. ;; Read link, with completion for stored links.
  6241. (with-output-to-temp-buffer "*Org Links*"
  6242. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6243. (when org-stored-links
  6244. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6245. (princ (mapconcat
  6246. (lambda (x)
  6247. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6248. (reverse org-stored-links) "\n"))))
  6249. (let ((cw (selected-window)))
  6250. (select-window (get-buffer-window "*Org Links*"))
  6251. (setq truncate-lines t)
  6252. (org-fit-window-to-buffer)
  6253. (select-window cw))
  6254. ;; Fake a link history, containing the stored links.
  6255. (setq tmphist (append (mapcar 'car org-stored-links)
  6256. org-insert-link-history))
  6257. (unwind-protect
  6258. (setq link
  6259. (let ((org-completion-use-ido nil))
  6260. (org-completing-read
  6261. "Link: "
  6262. (append
  6263. (mapcar (lambda (x) (list (concat (car x) ":")))
  6264. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6265. (mapcar (lambda (x) (list (concat x ":")))
  6266. org-link-types))
  6267. nil nil nil
  6268. 'tmphist
  6269. (or (car (car org-stored-links))))))
  6270. (set-window-configuration wcf)
  6271. (kill-buffer "*Org Links*"))
  6272. (setq entry (assoc link org-stored-links))
  6273. (or entry (push link org-insert-link-history))
  6274. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6275. (not org-keep-stored-link-after-insertion))
  6276. (setq org-stored-links (delq (assoc link org-stored-links)
  6277. org-stored-links)))
  6278. (setq desc (or desc (nth 1 entry)))))
  6279. (if (string-match org-plain-link-re link)
  6280. ;; URL-like link, normalize the use of angular brackets.
  6281. (setq link (org-make-link (org-remove-angle-brackets link))))
  6282. ;; Check if we are linking to the current file with a search option
  6283. ;; If yes, simplify the link by using only the search option.
  6284. (when (and buffer-file-name
  6285. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6286. (let* ((path (match-string 1 link))
  6287. (case-fold-search nil)
  6288. (search (match-string 2 link)))
  6289. (save-match-data
  6290. (if (equal (file-truename buffer-file-name) (file-truename path))
  6291. ;; We are linking to this same file, with a search option
  6292. (setq link search)))))
  6293. ;; Check if we can/should use a relative path. If yes, simplify the link
  6294. (when (string-match "^file:\\(.*\\)" link)
  6295. (let* ((path (match-string 1 link))
  6296. (origpath path)
  6297. (case-fold-search nil))
  6298. (cond
  6299. ((or (eq org-link-file-path-type 'absolute)
  6300. (equal complete-file '(16)))
  6301. (setq path (abbreviate-file-name (expand-file-name path))))
  6302. ((eq org-link-file-path-type 'noabbrev)
  6303. (setq path (expand-file-name path)))
  6304. ((eq org-link-file-path-type 'relative)
  6305. (setq path (file-relative-name path)))
  6306. (t
  6307. (save-match-data
  6308. (if (string-match (concat "^" (regexp-quote
  6309. (file-name-as-directory
  6310. (expand-file-name "."))))
  6311. (expand-file-name path))
  6312. ;; We are linking a file with relative path name.
  6313. (setq path (substring (expand-file-name path)
  6314. (match-end 0)))
  6315. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6316. (setq link (concat "file:" path))
  6317. (if (equal desc origpath)
  6318. (setq desc path))))
  6319. (if org-make-link-description-function
  6320. (setq desc (funcall org-make-link-description-function link desc)))
  6321. (setq desc (read-string "Description: " desc))
  6322. (unless (string-match "\\S-" desc) (setq desc nil))
  6323. (if remove (apply 'delete-region remove))
  6324. (insert (org-make-link-string link desc))))
  6325. (defun org-completing-read (&rest args)
  6326. "Completing-read with SPACE being a normal character."
  6327. (let ((minibuffer-local-completion-map
  6328. (copy-keymap minibuffer-local-completion-map)))
  6329. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6330. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6331. (apply 'org-ido-completing-read args)))
  6332. (defun org-ido-completing-read (&rest args)
  6333. "Completing-read using `ido-mode' speedups if available"
  6334. (if (and org-completion-use-ido
  6335. (fboundp 'ido-completing-read)
  6336. (boundp 'ido-mode) ido-mode
  6337. (listp (second args)))
  6338. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6339. (apply 'completing-read args)))
  6340. (defun org-extract-attributes (s)
  6341. "Extract the attributes cookie from a string and set as text property."
  6342. (let (a attr (start 0) key value)
  6343. (save-match-data
  6344. (when (string-match "{{\\([^}]+\\)}}$" s)
  6345. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6346. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6347. (setq key (match-string 1 a) value (match-string 2 a)
  6348. start (match-end 0)
  6349. attr (plist-put attr (intern key) value))))
  6350. (org-add-props s nil 'org-attr attr))
  6351. s))
  6352. (defun org-attributes-to-string (plist)
  6353. "Format a property list into an HTML attribute list."
  6354. (let ((s "") key value)
  6355. (while plist
  6356. (setq key (pop plist) value (pop plist))
  6357. (and value
  6358. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6359. s))
  6360. ;;; Opening/following a link
  6361. (defvar org-link-search-failed nil)
  6362. (defun org-next-link ()
  6363. "Move forward to the next link.
  6364. If the link is in hidden text, expose it."
  6365. (interactive)
  6366. (when (and org-link-search-failed (eq this-command last-command))
  6367. (goto-char (point-min))
  6368. (message "Link search wrapped back to beginning of buffer"))
  6369. (setq org-link-search-failed nil)
  6370. (let* ((pos (point))
  6371. (ct (org-context))
  6372. (a (assoc :link ct)))
  6373. (if a (goto-char (nth 2 a)))
  6374. (if (re-search-forward org-any-link-re nil t)
  6375. (progn
  6376. (goto-char (match-beginning 0))
  6377. (if (org-invisible-p) (org-show-context)))
  6378. (goto-char pos)
  6379. (setq org-link-search-failed t)
  6380. (error "No further link found"))))
  6381. (defun org-previous-link ()
  6382. "Move backward to the previous link.
  6383. If the link is in hidden text, expose it."
  6384. (interactive)
  6385. (when (and org-link-search-failed (eq this-command last-command))
  6386. (goto-char (point-max))
  6387. (message "Link search wrapped back to end of buffer"))
  6388. (setq org-link-search-failed nil)
  6389. (let* ((pos (point))
  6390. (ct (org-context))
  6391. (a (assoc :link ct)))
  6392. (if a (goto-char (nth 1 a)))
  6393. (if (re-search-backward org-any-link-re nil t)
  6394. (progn
  6395. (goto-char (match-beginning 0))
  6396. (if (org-invisible-p) (org-show-context)))
  6397. (goto-char pos)
  6398. (setq org-link-search-failed t)
  6399. (error "No further link found"))))
  6400. (defun org-translate-link (s)
  6401. "Translate a link string if a translation function has been defined."
  6402. (if (and org-link-translation-function
  6403. (fboundp org-link-translation-function)
  6404. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6405. (progn
  6406. (setq s (funcall org-link-translation-function
  6407. (match-string 1) (match-string 2)))
  6408. (concat (car s) ":" (cdr s)))
  6409. s))
  6410. (defun org-translate-link-from-planner (type path)
  6411. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6412. This is still an experimental function, your mileage may vary."
  6413. (cond
  6414. ((member type '("http" "https" "news" "ftp"))
  6415. ;; standard Internet links are the same.
  6416. nil)
  6417. ((and (equal type "irc") (string-match "^//" path))
  6418. ;; Planner has two / at the beginning of an irc link, we have 1.
  6419. ;; We should have zero, actually....
  6420. (setq path (substring path 1)))
  6421. ((and (equal type "lisp") (string-match "^/" path))
  6422. ;; Planner has a slash, we do not.
  6423. (setq type "elisp" path (substring path 1)))
  6424. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6425. ;; A typical message link. Planner has the id after the fina slash,
  6426. ;; we separate it with a hash mark
  6427. (setq path (concat (match-string 1 path) "#"
  6428. (org-remove-angle-brackets (match-string 2 path)))))
  6429. )
  6430. (cons type path))
  6431. (defun org-find-file-at-mouse (ev)
  6432. "Open file link or URL at mouse."
  6433. (interactive "e")
  6434. (mouse-set-point ev)
  6435. (org-open-at-point 'in-emacs))
  6436. (defun org-open-at-mouse (ev)
  6437. "Open file link or URL at mouse."
  6438. (interactive "e")
  6439. (mouse-set-point ev)
  6440. (if (eq major-mode 'org-agenda-mode)
  6441. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6442. (org-open-at-point))
  6443. (defvar org-window-config-before-follow-link nil
  6444. "The window configuration before following a link.
  6445. This is saved in case the need arises to restore it.")
  6446. (defvar org-open-link-marker (make-marker)
  6447. "Marker pointing to the location where `org-open-at-point; was called.")
  6448. ;;;###autoload
  6449. (defun org-open-at-point-global ()
  6450. "Follow a link like Org-mode does.
  6451. This command can be called in any mode to follow a link that has
  6452. Org-mode syntax."
  6453. (interactive)
  6454. (org-run-like-in-org-mode 'org-open-at-point))
  6455. ;;;###autoload
  6456. (defun org-open-link-from-string (s &optional arg)
  6457. "Open a link in the string S, as if it was in Org-mode."
  6458. (interactive "sLink: \nP")
  6459. (with-temp-buffer
  6460. (let ((org-inhibit-startup t))
  6461. (org-mode)
  6462. (insert s)
  6463. (goto-char (point-min))
  6464. (org-open-at-point arg))))
  6465. (defun org-open-at-point (&optional in-emacs)
  6466. "Open link at or after point.
  6467. If there is no link at point, this function will search forward up to
  6468. the end of the current subtree.
  6469. Normally, files will be opened by an appropriate application. If the
  6470. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6471. With a double prefix argument, try to open outside of Emacs, in the
  6472. application the system uses for this file type."
  6473. (interactive "P")
  6474. (org-load-modules-maybe)
  6475. (move-marker org-open-link-marker (point))
  6476. (setq org-window-config-before-follow-link (current-window-configuration))
  6477. (org-remove-occur-highlights nil nil t)
  6478. (cond
  6479. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6480. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6481. (org-footnote-action))
  6482. (t
  6483. (let (type path link line search (pos (point)))
  6484. (catch 'match
  6485. (save-excursion
  6486. (skip-chars-forward "^]\n\r")
  6487. (when (org-in-regexp org-bracket-link-regexp)
  6488. (setq link (org-extract-attributes
  6489. (org-link-unescape (org-match-string-no-properties 1))))
  6490. (while (string-match " *\n *" link)
  6491. (setq link (replace-match " " t t link)))
  6492. (setq link (org-link-expand-abbrev link))
  6493. (cond
  6494. ((or (file-name-absolute-p link)
  6495. (string-match "^\\.\\.?/" link))
  6496. (setq type "file" path link))
  6497. ((string-match org-link-re-with-space3 link)
  6498. (setq type (match-string 1 link) path (match-string 2 link)))
  6499. (t (setq type "thisfile" path link)))
  6500. (throw 'match t)))
  6501. (when (get-text-property (point) 'org-linked-text)
  6502. (setq type "thisfile"
  6503. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6504. (1+ (point)) (point))
  6505. path (buffer-substring
  6506. (previous-single-property-change pos 'org-linked-text)
  6507. (next-single-property-change pos 'org-linked-text)))
  6508. (throw 'match t))
  6509. (save-excursion
  6510. (when (or (org-in-regexp org-angle-link-re)
  6511. (org-in-regexp org-plain-link-re))
  6512. (setq type (match-string 1) path (match-string 2))
  6513. (throw 'match t)))
  6514. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6515. (setq type "tree-match"
  6516. path (match-string 1))
  6517. (throw 'match t))
  6518. (save-excursion
  6519. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6520. (setq type "tags"
  6521. path (match-string 1))
  6522. (while (string-match ":" path)
  6523. (setq path (replace-match "+" t t path)))
  6524. (throw 'match t))))
  6525. (unless path
  6526. (error "No link found"))
  6527. ;; Remove any trailing spaces in path
  6528. (if (string-match " +\\'" path)
  6529. (setq path (replace-match "" t t path)))
  6530. (if (and org-link-translation-function
  6531. (fboundp org-link-translation-function))
  6532. ;; Check if we need to translate the link
  6533. (let ((tmp (funcall org-link-translation-function type path)))
  6534. (setq type (car tmp) path (cdr tmp))))
  6535. (cond
  6536. ((assoc type org-link-protocols)
  6537. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6538. ((equal type "mailto")
  6539. (let ((cmd (car org-link-mailto-program))
  6540. (args (cdr org-link-mailto-program)) args1
  6541. (address path) (subject "") a)
  6542. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6543. (setq address (match-string 1 path)
  6544. subject (org-link-escape (match-string 2 path))))
  6545. (while args
  6546. (cond
  6547. ((not (stringp (car args))) (push (pop args) args1))
  6548. (t (setq a (pop args))
  6549. (if (string-match "%a" a)
  6550. (setq a (replace-match address t t a)))
  6551. (if (string-match "%s" a)
  6552. (setq a (replace-match subject t t a)))
  6553. (push a args1))))
  6554. (apply cmd (nreverse args1))))
  6555. ((member type '("http" "https" "ftp" "news"))
  6556. (browse-url (concat type ":" (org-link-escape
  6557. path org-link-escape-chars-browser))))
  6558. ((member type '("message"))
  6559. (browse-url (concat type ":" path)))
  6560. ((string= type "tags")
  6561. (org-tags-view in-emacs path))
  6562. ((string= type "thisfile")
  6563. (if in-emacs
  6564. (switch-to-buffer-other-window
  6565. (org-get-buffer-for-internal-link (current-buffer)))
  6566. (org-mark-ring-push))
  6567. (let ((cmd `(org-link-search
  6568. ,path
  6569. ,(cond ((equal in-emacs '(4)) 'occur)
  6570. ((equal in-emacs '(16)) 'org-occur)
  6571. (t nil))
  6572. ,pos)))
  6573. (condition-case nil (eval cmd)
  6574. (error (progn (widen) (eval cmd))))))
  6575. ((string= type "tree-match")
  6576. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6577. ((string= type "file")
  6578. (if (string-match "::\\([0-9]+\\)\\'" path)
  6579. (setq line (string-to-number (match-string 1 path))
  6580. path (substring path 0 (match-beginning 0)))
  6581. (if (string-match "::\\(.+\\)\\'" path)
  6582. (setq search (match-string 1 path)
  6583. path (substring path 0 (match-beginning 0)))))
  6584. (if (string-match "[*?{]" (file-name-nondirectory path))
  6585. (dired path)
  6586. (org-open-file path in-emacs line search)))
  6587. ((string= type "news")
  6588. (require 'org-gnus)
  6589. (org-gnus-follow-link path))
  6590. ((string= type "shell")
  6591. (let ((cmd path))
  6592. (if (or (not org-confirm-shell-link-function)
  6593. (funcall org-confirm-shell-link-function
  6594. (format "Execute \"%s\" in shell? "
  6595. (org-add-props cmd nil
  6596. 'face 'org-warning))))
  6597. (progn
  6598. (message "Executing %s" cmd)
  6599. (shell-command cmd))
  6600. (error "Abort"))))
  6601. ((string= type "elisp")
  6602. (let ((cmd path))
  6603. (if (or (not org-confirm-elisp-link-function)
  6604. (funcall org-confirm-elisp-link-function
  6605. (format "Execute \"%s\" as elisp? "
  6606. (org-add-props cmd nil
  6607. 'face 'org-warning))))
  6608. (message "%s => %s" cmd
  6609. (if (equal (string-to-char cmd) ?\()
  6610. (eval (read cmd))
  6611. (call-interactively (read cmd))))
  6612. (error "Abort"))))
  6613. (t
  6614. (browse-url-at-point))))))
  6615. (move-marker org-open-link-marker nil)
  6616. (run-hook-with-args 'org-follow-link-hook))
  6617. ;;;; Time estimates
  6618. (defun org-get-effort (&optional pom)
  6619. "Get the effort estimate for the current entry."
  6620. (org-entry-get pom org-effort-property))
  6621. ;;; File search
  6622. (defvar org-create-file-search-functions nil
  6623. "List of functions to construct the right search string for a file link.
  6624. These functions are called in turn with point at the location to
  6625. which the link should point.
  6626. A function in the hook should first test if it would like to
  6627. handle this file type, for example by checking the major-mode or
  6628. the file extension. If it decides not to handle this file, it
  6629. should just return nil to give other functions a chance. If it
  6630. does handle the file, it must return the search string to be used
  6631. when following the link. The search string will be part of the
  6632. file link, given after a double colon, and `org-open-at-point'
  6633. will automatically search for it. If special measures must be
  6634. taken to make the search successful, another function should be
  6635. added to the companion hook `org-execute-file-search-functions',
  6636. which see.
  6637. A function in this hook may also use `setq' to set the variable
  6638. `description' to provide a suggestion for the descriptive text to
  6639. be used for this link when it gets inserted into an Org-mode
  6640. buffer with \\[org-insert-link].")
  6641. (defvar org-execute-file-search-functions nil
  6642. "List of functions to execute a file search triggered by a link.
  6643. Functions added to this hook must accept a single argument, the
  6644. search string that was part of the file link, the part after the
  6645. double colon. The function must first check if it would like to
  6646. handle this search, for example by checking the major-mode or the
  6647. file extension. If it decides not to handle this search, it
  6648. should just return nil to give other functions a chance. If it
  6649. does handle the search, it must return a non-nil value to keep
  6650. other functions from trying.
  6651. Each function can access the current prefix argument through the
  6652. variable `current-prefix-argument'. Note that a single prefix is
  6653. used to force opening a link in Emacs, so it may be good to only
  6654. use a numeric or double prefix to guide the search function.
  6655. In case this is needed, a function in this hook can also restore
  6656. the window configuration before `org-open-at-point' was called using:
  6657. (set-window-configuration org-window-config-before-follow-link)")
  6658. (defun org-link-search (s &optional type avoid-pos)
  6659. "Search for a link search option.
  6660. If S is surrounded by forward slashes, it is interpreted as a
  6661. regular expression. In org-mode files, this will create an `org-occur'
  6662. sparse tree. In ordinary files, `occur' will be used to list matches.
  6663. If the current buffer is in `dired-mode', grep will be used to search
  6664. in all files. If AVOID-POS is given, ignore matches near that position."
  6665. (let ((case-fold-search t)
  6666. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6667. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6668. (append '(("") (" ") ("\t") ("\n"))
  6669. org-emphasis-alist)
  6670. "\\|") "\\)"))
  6671. (pos (point))
  6672. (pre nil) (post nil)
  6673. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6674. (cond
  6675. ;; First check if there are any special
  6676. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6677. ;; Now try the builtin stuff
  6678. ((save-excursion
  6679. (goto-char (point-min))
  6680. (and
  6681. (re-search-forward
  6682. (concat "<<" (regexp-quote s0) ">>") nil t)
  6683. (setq type 'dedicated
  6684. pos (match-beginning 0))))
  6685. ;; There is an exact target for this
  6686. (goto-char pos))
  6687. ((and (string-match "^(\\(.*\\))$" s0)
  6688. (save-excursion
  6689. (goto-char (point-min))
  6690. (and
  6691. (re-search-forward
  6692. (concat "[^[]" (regexp-quote
  6693. (format org-coderef-label-format
  6694. (match-string 1 s0))))
  6695. nil t)
  6696. (setq type 'dedicated
  6697. pos (1+ (match-beginning 0))))))
  6698. ;; There is a coderef target for this
  6699. (goto-char pos))
  6700. ((string-match "^/\\(.*\\)/$" s)
  6701. ;; A regular expression
  6702. (cond
  6703. ((org-mode-p)
  6704. (org-occur (match-string 1 s)))
  6705. ;;((eq major-mode 'dired-mode)
  6706. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6707. (t (org-do-occur (match-string 1 s)))))
  6708. (t
  6709. ;; A normal search strings
  6710. (when (equal (string-to-char s) ?*)
  6711. ;; Anchor on headlines, post may include tags.
  6712. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6713. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6714. s (substring s 1)))
  6715. (remove-text-properties
  6716. 0 (length s)
  6717. '(face nil mouse-face nil keymap nil fontified nil) s)
  6718. ;; Make a series of regular expressions to find a match
  6719. (setq words (org-split-string s "[ \n\r\t]+")
  6720. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6721. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6722. "\\)" markers)
  6723. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6724. re2a (concat "[ \t\r\n]" re2a_)
  6725. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6726. re4 (concat "[^a-zA-Z_]" re4_)
  6727. re1 (concat pre re2 post)
  6728. re3 (concat pre (if pre re4_ re4) post)
  6729. re5 (concat pre ".*" re4)
  6730. re2 (concat pre re2)
  6731. re2a (concat pre (if pre re2a_ re2a))
  6732. re4 (concat pre (if pre re4_ re4))
  6733. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6734. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6735. re5 "\\)"
  6736. ))
  6737. (cond
  6738. ((eq type 'org-occur) (org-occur reall))
  6739. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6740. (t (goto-char (point-min))
  6741. (setq type 'fuzzy)
  6742. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6743. (org-search-not-self 1 re1 nil t)
  6744. (org-search-not-self 1 re2 nil t)
  6745. (org-search-not-self 1 re2a nil t)
  6746. (org-search-not-self 1 re3 nil t)
  6747. (org-search-not-self 1 re4 nil t)
  6748. (org-search-not-self 1 re5 nil t)
  6749. )
  6750. (goto-char (match-beginning 1))
  6751. (goto-char pos)
  6752. (error "No match")))))
  6753. (t
  6754. ;; Normal string-search
  6755. (goto-char (point-min))
  6756. (if (search-forward s nil t)
  6757. (goto-char (match-beginning 0))
  6758. (error "No match"))))
  6759. (and (org-mode-p) (org-show-context 'link-search))
  6760. type))
  6761. (defun org-search-not-self (group &rest args)
  6762. "Execute `re-search-forward', but only accept matches that do not
  6763. enclose the position of `org-open-link-marker'."
  6764. (let ((m org-open-link-marker))
  6765. (catch 'exit
  6766. (while (apply 're-search-forward args)
  6767. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6768. (goto-char (match-end group))
  6769. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6770. (> (match-beginning 0) (marker-position m))
  6771. (< (match-end 0) (marker-position m)))
  6772. (save-match-data
  6773. (or (not (org-in-regexp
  6774. org-bracket-link-analytic-regexp 1))
  6775. (not (match-end 4)) ; no description
  6776. (and (<= (match-beginning 4) (point))
  6777. (>= (match-end 4) (point))))))
  6778. (throw 'exit (point))))))))
  6779. (defun org-get-buffer-for-internal-link (buffer)
  6780. "Return a buffer to be used for displaying the link target of internal links."
  6781. (cond
  6782. ((not org-display-internal-link-with-indirect-buffer)
  6783. buffer)
  6784. ((string-match "(Clone)$" (buffer-name buffer))
  6785. (message "Buffer is already a clone, not making another one")
  6786. ;; we also do not modify visibility in this case
  6787. buffer)
  6788. (t ; make a new indirect buffer for displaying the link
  6789. (let* ((bn (buffer-name buffer))
  6790. (ibn (concat bn "(Clone)"))
  6791. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6792. (with-current-buffer ib (org-overview))
  6793. ib))))
  6794. (defun org-do-occur (regexp &optional cleanup)
  6795. "Call the Emacs command `occur'.
  6796. If CLEANUP is non-nil, remove the printout of the regular expression
  6797. in the *Occur* buffer. This is useful if the regex is long and not useful
  6798. to read."
  6799. (occur regexp)
  6800. (when cleanup
  6801. (let ((cwin (selected-window)) win beg end)
  6802. (when (setq win (get-buffer-window "*Occur*"))
  6803. (select-window win))
  6804. (goto-char (point-min))
  6805. (when (re-search-forward "match[a-z]+" nil t)
  6806. (setq beg (match-end 0))
  6807. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6808. (setq end (1- (match-beginning 0)))))
  6809. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6810. (goto-char (point-min))
  6811. (select-window cwin))))
  6812. ;;; The mark ring for links jumps
  6813. (defvar org-mark-ring nil
  6814. "Mark ring for positions before jumps in Org-mode.")
  6815. (defvar org-mark-ring-last-goto nil
  6816. "Last position in the mark ring used to go back.")
  6817. ;; Fill and close the ring
  6818. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6819. (loop for i from 1 to org-mark-ring-length do
  6820. (push (make-marker) org-mark-ring))
  6821. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6822. org-mark-ring)
  6823. (defun org-mark-ring-push (&optional pos buffer)
  6824. "Put the current position or POS into the mark ring and rotate it."
  6825. (interactive)
  6826. (setq pos (or pos (point)))
  6827. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6828. (move-marker (car org-mark-ring)
  6829. (or pos (point))
  6830. (or buffer (current-buffer)))
  6831. (message "%s"
  6832. (substitute-command-keys
  6833. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6834. (defun org-mark-ring-goto (&optional n)
  6835. "Jump to the previous position in the mark ring.
  6836. With prefix arg N, jump back that many stored positions. When
  6837. called several times in succession, walk through the entire ring.
  6838. Org-mode commands jumping to a different position in the current file,
  6839. or to another Org-mode file, automatically push the old position
  6840. onto the ring."
  6841. (interactive "p")
  6842. (let (p m)
  6843. (if (eq last-command this-command)
  6844. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6845. (setq p org-mark-ring))
  6846. (setq org-mark-ring-last-goto p)
  6847. (setq m (car p))
  6848. (switch-to-buffer (marker-buffer m))
  6849. (goto-char m)
  6850. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6851. (defun org-remove-angle-brackets (s)
  6852. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6853. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6854. s)
  6855. (defun org-add-angle-brackets (s)
  6856. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6857. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6858. s)
  6859. (defun org-remove-double-quotes (s)
  6860. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6861. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6862. s)
  6863. ;;; Following specific links
  6864. (defun org-follow-timestamp-link ()
  6865. (cond
  6866. ((org-at-date-range-p t)
  6867. (let ((org-agenda-start-on-weekday)
  6868. (t1 (match-string 1))
  6869. (t2 (match-string 2)))
  6870. (setq t1 (time-to-days (org-time-string-to-time t1))
  6871. t2 (time-to-days (org-time-string-to-time t2)))
  6872. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6873. ((org-at-timestamp-p t)
  6874. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6875. (substring (match-string 1) 0 10)))
  6876. 1))
  6877. (t (error "This should not happen"))))
  6878. ;;; Following file links
  6879. (defvar org-wait nil)
  6880. (defun org-open-file (path &optional in-emacs line search)
  6881. "Open the file at PATH.
  6882. First, this expands any special file name abbreviations. Then the
  6883. configuration variable `org-file-apps' is checked if it contains an
  6884. entry for this file type, and if yes, the corresponding command is launched.
  6885. If no application is found, Emacs simply visits the file.
  6886. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6887. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6888. and o use an external application to visit the file.
  6889. Optional LINE specifies a line to go to, optional SEARCH a string to
  6890. search for. If LINE or SEARCH is given, the file will always be
  6891. opened in Emacs.
  6892. If the file does not exist, an error is thrown."
  6893. (setq in-emacs (or in-emacs line search))
  6894. (let* ((file (if (equal path "")
  6895. buffer-file-name
  6896. (substitute-in-file-name (expand-file-name path))))
  6897. (apps (append org-file-apps (org-default-apps)))
  6898. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6899. (dirp (if remp nil (file-directory-p file)))
  6900. (file (if (and dirp org-open-directory-means-index-dot-org)
  6901. (concat (file-name-as-directory file) "index.org")
  6902. file))
  6903. (a-m-a-p (assq 'auto-mode apps))
  6904. (dfile (downcase file))
  6905. (old-buffer (current-buffer))
  6906. (old-pos (point))
  6907. (old-mode major-mode)
  6908. ext cmd)
  6909. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6910. (setq ext (match-string 1 dfile))
  6911. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6912. (setq ext (match-string 1 dfile))))
  6913. (cond
  6914. ((equal in-emacs '(16))
  6915. (setq cmd (cdr (assoc 'system apps))))
  6916. (in-emacs (setq cmd 'emacs))
  6917. (t
  6918. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6919. (and dirp (cdr (assoc 'directory apps)))
  6920. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6921. 'string-match)
  6922. (cdr (assoc ext apps))
  6923. (cdr (assoc t apps))))))
  6924. (when (eq cmd 'system)
  6925. (setq cmd (cdr (assoc 'system apps))))
  6926. (when (eq cmd 'default)
  6927. (setq cmd (cdr (assoc t apps))))
  6928. (when (eq cmd 'mailcap)
  6929. (require 'mailcap)
  6930. (mailcap-parse-mailcaps)
  6931. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6932. (command (mailcap-mime-info mime-type)))
  6933. (if (stringp command)
  6934. (setq cmd command)
  6935. (setq cmd 'emacs))))
  6936. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6937. (not (file-exists-p file))
  6938. (not org-open-non-existing-files))
  6939. (error "No such file: %s" file))
  6940. (cond
  6941. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6942. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6943. (while (string-match "['\"]%s['\"]" cmd)
  6944. (setq cmd (replace-match "%s" t t cmd)))
  6945. (while (string-match "%s" cmd)
  6946. (setq cmd (replace-match
  6947. (save-match-data
  6948. (shell-quote-argument
  6949. (convert-standard-filename file)))
  6950. t t cmd)))
  6951. (save-window-excursion
  6952. (start-process-shell-command cmd nil cmd)
  6953. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6954. ))
  6955. ((or (stringp cmd)
  6956. (eq cmd 'emacs))
  6957. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6958. (widen)
  6959. (if line (goto-line line)
  6960. (if search (org-link-search search))))
  6961. ((consp cmd)
  6962. (let ((file (convert-standard-filename file)))
  6963. (eval cmd)))
  6964. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6965. (and (org-mode-p) (eq old-mode 'org-mode)
  6966. (or (not (equal old-buffer (current-buffer)))
  6967. (not (equal old-pos (point))))
  6968. (org-mark-ring-push old-pos old-buffer))))
  6969. (defun org-default-apps ()
  6970. "Return the default applications for this operating system."
  6971. (cond
  6972. ((eq system-type 'darwin)
  6973. org-file-apps-defaults-macosx)
  6974. ((eq system-type 'windows-nt)
  6975. org-file-apps-defaults-windowsnt)
  6976. (t org-file-apps-defaults-gnu)))
  6977. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6978. "Convert extensions to regular expressions in the cars of LIST.
  6979. Also, weed out any non-string entries, because the return value is used
  6980. only for regexp matching.
  6981. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6982. point to the symbol `emacs', indicating that the file should
  6983. be opened in Emacs."
  6984. (append
  6985. (delq nil
  6986. (mapcar (lambda (x)
  6987. (if (not (stringp (car x)))
  6988. nil
  6989. (if (string-match "\\W" (car x))
  6990. x
  6991. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6992. list))
  6993. (if add-auto-mode
  6994. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6995. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6996. (defun org-file-remote-p (file)
  6997. "Test whether FILE specifies a location on a remote system.
  6998. Return non-nil if the location is indeed remote.
  6999. For example, the filename \"/user@host:/foo\" specifies a location
  7000. on the system \"/user@host:\"."
  7001. (cond ((fboundp 'file-remote-p)
  7002. (file-remote-p file))
  7003. ((fboundp 'tramp-handle-file-remote-p)
  7004. (tramp-handle-file-remote-p file))
  7005. ((and (boundp 'ange-ftp-name-format)
  7006. (string-match (car ange-ftp-name-format) file))
  7007. t)
  7008. (t nil)))
  7009. ;;;; Refiling
  7010. (defun org-get-org-file ()
  7011. "Read a filename, with default directory `org-directory'."
  7012. (let ((default (or org-default-notes-file remember-data-file)))
  7013. (read-file-name (format "File name [%s]: " default)
  7014. (file-name-as-directory org-directory)
  7015. default)))
  7016. (defun org-notes-order-reversed-p ()
  7017. "Check if the current file should receive notes in reversed order."
  7018. (cond
  7019. ((not org-reverse-note-order) nil)
  7020. ((eq t org-reverse-note-order) t)
  7021. ((not (listp org-reverse-note-order)) nil)
  7022. (t (catch 'exit
  7023. (let ((all org-reverse-note-order)
  7024. entry)
  7025. (while (setq entry (pop all))
  7026. (if (string-match (car entry) buffer-file-name)
  7027. (throw 'exit (cdr entry))))
  7028. nil)))))
  7029. (defvar org-refile-target-table nil
  7030. "The list of refile targets, created by `org-refile'.")
  7031. (defvar org-agenda-new-buffers nil
  7032. "Buffers created to visit agenda files.")
  7033. (defun org-get-refile-targets (&optional default-buffer)
  7034. "Produce a table with refile targets."
  7035. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7036. targets txt re files f desc descre fast-path-p level)
  7037. (message "Getting targets...")
  7038. (with-current-buffer (or default-buffer (current-buffer))
  7039. (while (setq entry (pop entries))
  7040. (setq files (car entry) desc (cdr entry))
  7041. (setq fast-path-p nil)
  7042. (cond
  7043. ((null files) (setq files (list (current-buffer))))
  7044. ((eq files 'org-agenda-files)
  7045. (setq files (org-agenda-files 'unrestricted)))
  7046. ((and (symbolp files) (fboundp files))
  7047. (setq files (funcall files)))
  7048. ((and (symbolp files) (boundp files))
  7049. (setq files (symbol-value files))))
  7050. (if (stringp files) (setq files (list files)))
  7051. (cond
  7052. ((eq (car desc) :tag)
  7053. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7054. ((eq (car desc) :todo)
  7055. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7056. ((eq (car desc) :regexp)
  7057. (setq descre (cdr desc)))
  7058. ((eq (car desc) :level)
  7059. (setq descre (concat "^\\*\\{" (number-to-string
  7060. (if org-odd-levels-only
  7061. (1- (* 2 (cdr desc)))
  7062. (cdr desc)))
  7063. "\\}[ \t]")))
  7064. ((eq (car desc) :maxlevel)
  7065. (setq fast-path-p t)
  7066. (setq descre (concat "^\\*\\{1," (number-to-string
  7067. (if org-odd-levels-only
  7068. (1- (* 2 (cdr desc)))
  7069. (cdr desc)))
  7070. "\\}[ \t]")))
  7071. (t (error "Bad refiling target description %s" desc)))
  7072. (while (setq f (pop files))
  7073. (save-excursion
  7074. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7075. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7076. (setq f (expand-file-name f))
  7077. (save-excursion
  7078. (save-restriction
  7079. (widen)
  7080. (goto-char (point-min))
  7081. (while (re-search-forward descre nil t)
  7082. (goto-char (point-at-bol))
  7083. (when (looking-at org-complex-heading-regexp)
  7084. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7085. txt (org-link-display-format (match-string 4))
  7086. re (concat "^" (regexp-quote
  7087. (buffer-substring (match-beginning 1)
  7088. (match-end 4)))))
  7089. (if (match-end 5) (setq re (concat re "[ \t]+"
  7090. (regexp-quote
  7091. (match-string 5)))))
  7092. (setq re (concat re "[ \t]*$"))
  7093. (when org-refile-use-outline-path
  7094. (setq txt (mapconcat 'org-protect-slash
  7095. (append
  7096. (if (eq org-refile-use-outline-path 'file)
  7097. (list (file-name-nondirectory
  7098. (buffer-file-name (buffer-base-buffer))))
  7099. (if (eq org-refile-use-outline-path 'full-file-path)
  7100. (list (buffer-file-name (buffer-base-buffer)))))
  7101. (org-get-outline-path fast-path-p level txt)
  7102. (list txt))
  7103. "/")))
  7104. (push (list txt f re (point)) targets))
  7105. (goto-char (point-at-eol))))))))
  7106. (message "Getting targets...done")
  7107. (nreverse targets))))
  7108. (defun org-protect-slash (s)
  7109. (while (string-match "/" s)
  7110. (setq s (replace-match "\\" t t s)))
  7111. s)
  7112. (defvar org-olpa (make-vector 20 nil))
  7113. (defun org-get-outline-path (&optional fastp level heading)
  7114. "Return the outline path to the current entry, as a list."
  7115. (if fastp
  7116. (progn
  7117. (if (> level 19)
  7118. (error "Outline path failure, more than 19 levels."))
  7119. (loop for i from level upto 19 do
  7120. (aset org-olpa i nil))
  7121. (prog1
  7122. (delq nil (append org-olpa nil))
  7123. (aset org-olpa level heading)))
  7124. (let (rtn)
  7125. (save-excursion
  7126. (while (org-up-heading-safe)
  7127. (when (looking-at org-complex-heading-regexp)
  7128. (push (org-match-string-no-properties 4) rtn)))
  7129. rtn))))
  7130. (defvar org-refile-history nil
  7131. "History for refiling operations.")
  7132. (defun org-refile (&optional goto default-buffer)
  7133. "Move the entry at point to another heading.
  7134. The list of target headings is compiled using the information in
  7135. `org-refile-targets', which see. This list is created before each use
  7136. and will therefore always be up-to-date.
  7137. At the target location, the entry is filed as a subitem of the target heading.
  7138. Depending on `org-reverse-note-order', the new subitem will either be the
  7139. first or the last subitem.
  7140. If there is an active region, all entries in that region will be moved.
  7141. However, the region must fulfil the requirement that the first heading
  7142. is the first one sets the top-level of the moved text - at most siblings
  7143. below it are allowed.
  7144. With prefix arg GOTO, the command will only visit the target location,
  7145. not actually move anything.
  7146. With a double prefix `C-u C-u', go to the location where the last refiling
  7147. operation has put the subtree."
  7148. (interactive "P")
  7149. (let* ((cbuf (current-buffer))
  7150. (regionp (org-region-active-p))
  7151. (region-start (and regionp (region-beginning)))
  7152. (region-end (and regionp (region-end)))
  7153. (region-length (and regionp (- region-end region-start)))
  7154. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7155. pos it nbuf file re level reversed)
  7156. (when regionp (goto-char region-start)
  7157. (unless (org-kill-is-subtree-p
  7158. (buffer-substring region-start region-end))
  7159. (error "The region is not a (sequence of) subtree(s)")))
  7160. (if (equal goto '(16))
  7161. (org-refile-goto-last-stored)
  7162. (when (setq it (org-refile-get-location
  7163. (if goto "Goto: " "Refile to: ") default-buffer))
  7164. (setq file (nth 1 it)
  7165. re (nth 2 it)
  7166. pos (nth 3 it))
  7167. (if (and (equal (buffer-file-name) file)
  7168. (if regionp
  7169. (and (>= pos region-start)
  7170. (<= pos region-end))
  7171. (and (>= pos (point))
  7172. (< pos (save-excursion
  7173. (org-end-of-subtree t t))))))
  7174. (error "Cannot refile to position inside the tree or region"))
  7175. (setq nbuf (or (find-buffer-visiting file)
  7176. (find-file-noselect file)))
  7177. (if goto
  7178. (progn
  7179. (switch-to-buffer nbuf)
  7180. (goto-char pos)
  7181. (org-show-context 'org-goto))
  7182. (if regionp
  7183. (progn
  7184. (kill-new (buffer-substring region-start region-end))
  7185. (org-save-markers-in-region region-start region-end))
  7186. (org-copy-subtree 1 nil t))
  7187. (save-excursion
  7188. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7189. (find-file-noselect file))))
  7190. (setq reversed (org-notes-order-reversed-p))
  7191. (save-excursion
  7192. (save-restriction
  7193. (widen)
  7194. (goto-char pos)
  7195. (looking-at outline-regexp)
  7196. (setq level (org-get-valid-level (funcall outline-level) 1))
  7197. (goto-char
  7198. (if reversed
  7199. (or (outline-next-heading) (point-max))
  7200. (or (save-excursion (outline-get-next-sibling))
  7201. (org-end-of-subtree t t)
  7202. (point-max))))
  7203. (if (not (bolp)) (newline))
  7204. (bookmark-set "org-refile-last-stored")
  7205. (org-paste-subtree level))))
  7206. (if regionp
  7207. (delete-region (point) (+ (point) region-length))
  7208. (org-cut-subtree))
  7209. (setq org-markers-to-move nil)
  7210. (message "Refiled to \"%s\"" (car it)))))))
  7211. (defun org-refile-goto-last-stored ()
  7212. "Go to the location where the last refile was stored."
  7213. (interactive)
  7214. (bookmark-jump "org-refile-last-stored")
  7215. (message "This is the location of the last refile"))
  7216. (defun org-refile-get-location (&optional prompt default-buffer)
  7217. "Prompt the user for a refile location, using PROMPT."
  7218. (let ((org-refile-targets org-refile-targets)
  7219. (org-refile-use-outline-path org-refile-use-outline-path))
  7220. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7221. (unless org-refile-target-table
  7222. (error "No refile targets"))
  7223. (let* ((cbuf (current-buffer))
  7224. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7225. (cfunc (if (and org-refile-use-outline-path
  7226. org-outline-path-complete-in-steps)
  7227. 'org-olpath-completing-read
  7228. 'org-ido-completing-read))
  7229. (extra (if org-refile-use-outline-path "/" ""))
  7230. (filename (and cfn (expand-file-name cfn)))
  7231. (tbl (mapcar
  7232. (lambda (x)
  7233. (if (not (equal filename (nth 1 x)))
  7234. (cons (concat (car x) extra " ("
  7235. (file-name-nondirectory (nth 1 x)) ")")
  7236. (cdr x))
  7237. (cons (concat (car x) extra) (cdr x))))
  7238. org-refile-target-table))
  7239. (completion-ignore-case t))
  7240. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7241. tbl)))
  7242. (defun org-olpath-completing-read (prompt collection &rest args)
  7243. "Read an outline path like a file name."
  7244. (let ((thetable collection))
  7245. (apply
  7246. 'org-ido-completing-read prompt
  7247. (lambda (string predicate &optional flag)
  7248. (let (rtn r f (l (length string)))
  7249. (cond
  7250. ((eq flag nil)
  7251. ;; try completion
  7252. (try-completion string thetable))
  7253. ((eq flag t)
  7254. ;; all-completions
  7255. (setq rtn (all-completions string thetable predicate))
  7256. (mapcar
  7257. (lambda (x)
  7258. (setq r (substring x l))
  7259. (if (string-match " ([^)]*)$" x)
  7260. (setq f (match-string 0 x))
  7261. (setq f ""))
  7262. (if (string-match "/" r)
  7263. (concat string (substring r 0 (match-end 0)) f)
  7264. x))
  7265. rtn))
  7266. ((eq flag 'lambda)
  7267. ;; exact match?
  7268. (assoc string thetable)))
  7269. ))
  7270. args)))
  7271. ;;;; Dynamic blocks
  7272. (defun org-find-dblock (name)
  7273. "Find the first dynamic block with name NAME in the buffer.
  7274. If not found, stay at current position and return nil."
  7275. (let (pos)
  7276. (save-excursion
  7277. (goto-char (point-min))
  7278. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7279. nil t)
  7280. (match-beginning 0))))
  7281. (if pos (goto-char pos))
  7282. pos))
  7283. (defconst org-dblock-start-re
  7284. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7285. "Matches the startline of a dynamic block, with parameters.")
  7286. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7287. "Matches the end of a dynamic block.")
  7288. (defun org-create-dblock (plist)
  7289. "Create a dynamic block section, with parameters taken from PLIST.
  7290. PLIST must contain a :name entry which is used as name of the block."
  7291. (unless (bolp) (newline))
  7292. (let ((name (plist-get plist :name)))
  7293. (insert "#+BEGIN: " name)
  7294. (while plist
  7295. (if (eq (car plist) :name)
  7296. (setq plist (cddr plist))
  7297. (insert " " (prin1-to-string (pop plist)))))
  7298. (insert "\n\n#+END:\n")
  7299. (beginning-of-line -2)))
  7300. (defun org-prepare-dblock ()
  7301. "Prepare dynamic block for refresh.
  7302. This empties the block, puts the cursor at the insert position and returns
  7303. the property list including an extra property :name with the block name."
  7304. (unless (looking-at org-dblock-start-re)
  7305. (error "Not at a dynamic block"))
  7306. (let* ((begdel (1+ (match-end 0)))
  7307. (name (org-no-properties (match-string 1)))
  7308. (params (append (list :name name)
  7309. (read (concat "(" (match-string 3) ")")))))
  7310. (unless (re-search-forward org-dblock-end-re nil t)
  7311. (error "Dynamic block not terminated"))
  7312. (setq params
  7313. (append params
  7314. (list :content (buffer-substring
  7315. begdel (match-beginning 0)))))
  7316. (delete-region begdel (match-beginning 0))
  7317. (goto-char begdel)
  7318. (open-line 1)
  7319. params))
  7320. (defun org-map-dblocks (&optional command)
  7321. "Apply COMMAND to all dynamic blocks in the current buffer.
  7322. If COMMAND is not given, use `org-update-dblock'."
  7323. (let ((cmd (or command 'org-update-dblock))
  7324. pos)
  7325. (save-excursion
  7326. (goto-char (point-min))
  7327. (while (re-search-forward org-dblock-start-re nil t)
  7328. (goto-char (setq pos (match-beginning 0)))
  7329. (condition-case nil
  7330. (funcall cmd)
  7331. (error (message "Error during update of dynamic block")))
  7332. (goto-char pos)
  7333. (unless (re-search-forward org-dblock-end-re nil t)
  7334. (error "Dynamic block not terminated"))))))
  7335. (defun org-dblock-update (&optional arg)
  7336. "User command for updating dynamic blocks.
  7337. Update the dynamic block at point. With prefix ARG, update all dynamic
  7338. blocks in the buffer."
  7339. (interactive "P")
  7340. (if arg
  7341. (org-update-all-dblocks)
  7342. (or (looking-at org-dblock-start-re)
  7343. (org-beginning-of-dblock))
  7344. (org-update-dblock)))
  7345. (defun org-update-dblock ()
  7346. "Update the dynamic block at point
  7347. This means to empty the block, parse for parameters and then call
  7348. the correct writing function."
  7349. (save-window-excursion
  7350. (let* ((pos (point))
  7351. (line (org-current-line))
  7352. (params (org-prepare-dblock))
  7353. (name (plist-get params :name))
  7354. (cmd (intern (concat "org-dblock-write:" name))))
  7355. (message "Updating dynamic block `%s' at line %d..." name line)
  7356. (funcall cmd params)
  7357. (message "Updating dynamic block `%s' at line %d...done" name line)
  7358. (goto-char pos))))
  7359. (defun org-beginning-of-dblock ()
  7360. "Find the beginning of the dynamic block at point.
  7361. Error if there is no such block at point."
  7362. (let ((pos (point))
  7363. beg)
  7364. (end-of-line 1)
  7365. (if (and (re-search-backward org-dblock-start-re nil t)
  7366. (setq beg (match-beginning 0))
  7367. (re-search-forward org-dblock-end-re nil t)
  7368. (> (match-end 0) pos))
  7369. (goto-char beg)
  7370. (goto-char pos)
  7371. (error "Not in a dynamic block"))))
  7372. (defun org-update-all-dblocks ()
  7373. "Update all dynamic blocks in the buffer.
  7374. This function can be used in a hook."
  7375. (when (org-mode-p)
  7376. (org-map-dblocks 'org-update-dblock)))
  7377. ;;;; Completion
  7378. (defconst org-additional-option-like-keywords
  7379. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7380. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7381. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7382. "BEGIN_QUOTE" "END_QUOTE"
  7383. "BEGIN_VERSE" "END_VERSE"
  7384. "BEGIN_SRC" "END_SRC"
  7385. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7386. (defcustom org-structure-template-alist
  7387. '(
  7388. ("s" "#+begin_src ?\n\n#+end_src"
  7389. "<src lang=\"?\">\n\n</src>")
  7390. ("e" "#+begin_example\n?\n#+end_example"
  7391. "<example>\n?\n</example>")
  7392. ("q" "#+begin_quote\n?\n#+end_quote"
  7393. "<quote>\n?\n</quote>")
  7394. ("v" "#+begin_verse\n?\n#+end_verse"
  7395. "<verse>\n?\n/verse>")
  7396. ("l" "#+begin_latex\n?\n#+end_latex"
  7397. "<literal style=\"latex\">\n?\n</literal>")
  7398. ("L" "#+latex: "
  7399. "<literal style=\"latex\">?</literal>")
  7400. ("h" "#+begin_html\n?\n#+end_html"
  7401. "<literal style=\"html\">\n?\n</literal>")
  7402. ("H" "#+html: "
  7403. "<literal style=\"html\">?</literal>")
  7404. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7405. ("A" "#+ascii: ")
  7406. ("i" "#+include %file ?"
  7407. "<include file=%file markup=\"?\">")
  7408. )
  7409. "Structure completion elements.
  7410. This is a list of abbreviation keys and values. The value gets inserted
  7411. it you type @samp{.} followed by the key and then the completion key,
  7412. usually `M-TAB'. %file will be replaced by a file name after prompting
  7413. for the file using completion.
  7414. There are two templates for each key, the first uses the original Org syntax,
  7415. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7416. the default when the /org-mtags.el/ module has been loaded. See also the
  7417. variable `org-mtags-prefer-muse-templates'.
  7418. This is an experimental feature, it is undecided if it is going to stay in."
  7419. :group 'org-completion
  7420. :type '(repeat
  7421. (string :tag "Key")
  7422. (string :tag "Template")
  7423. (string :tag "Muse Template")))
  7424. (defun org-try-structure-completion ()
  7425. "Try to complete a structure template before point.
  7426. This looks for strings like \"<e\" on an otherwise empty line and
  7427. expands them."
  7428. (let ((l (buffer-substring (point-at-bol) (point)))
  7429. a)
  7430. (when (and (looking-at "[ \t]*$")
  7431. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7432. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7433. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7434. (match-beginning 1)) a)
  7435. t)))
  7436. (defun org-complete-expand-structure-template (start cell)
  7437. "Expand a structure template."
  7438. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7439. (rpl (nth (if musep 2 1) cell)))
  7440. (delete-region start (point))
  7441. (when (string-match "\\`#\\+" rpl)
  7442. (cond
  7443. ((bolp))
  7444. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7445. (delete-region (point-at-bol) (point)))
  7446. (t (newline))))
  7447. (setq start (point))
  7448. (if (string-match "%file" rpl)
  7449. (setq rpl (replace-match
  7450. (concat
  7451. "\""
  7452. (save-match-data
  7453. (abbreviate-file-name (read-file-name "Include file: ")))
  7454. "\"")
  7455. t t rpl)))
  7456. (insert rpl)
  7457. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7458. (defun org-complete (&optional arg)
  7459. "Perform completion on word at point.
  7460. At the beginning of a headline, this completes TODO keywords as given in
  7461. `org-todo-keywords'.
  7462. If the current word is preceded by a backslash, completes the TeX symbols
  7463. that are supported for HTML support.
  7464. If the current word is preceded by \"#+\", completes special words for
  7465. setting file options.
  7466. In the line after \"#+STARTUP:, complete valid keywords.\"
  7467. At all other locations, this simply calls the value of
  7468. `org-completion-fallback-command'."
  7469. (interactive "P")
  7470. (org-without-partial-completion
  7471. (catch 'exit
  7472. (let* ((a nil)
  7473. (end (point))
  7474. (beg1 (save-excursion
  7475. (skip-chars-backward (org-re "[:alnum:]_@"))
  7476. (point)))
  7477. (beg (save-excursion
  7478. (skip-chars-backward "a-zA-Z0-9_:$")
  7479. (point)))
  7480. (confirm (lambda (x) (stringp (car x))))
  7481. (searchhead (equal (char-before beg) ?*))
  7482. (struct
  7483. (when (and (member (char-before beg1) '(?. ?<))
  7484. (setq a (assoc (buffer-substring beg1 (point))
  7485. org-structure-template-alist)))
  7486. (org-complete-expand-structure-template (1- beg1) a)
  7487. (throw 'exit t)))
  7488. (tag (and (equal (char-before beg1) ?:)
  7489. (equal (char-after (point-at-bol)) ?*)))
  7490. (prop (and (equal (char-before beg1) ?:)
  7491. (not (equal (char-after (point-at-bol)) ?*))))
  7492. (texp (equal (char-before beg) ?\\))
  7493. (link (equal (char-before beg) ?\[))
  7494. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7495. beg)
  7496. "#+"))
  7497. (startup (string-match "^#\\+STARTUP:.*"
  7498. (buffer-substring (point-at-bol) (point))))
  7499. (completion-ignore-case opt)
  7500. (type nil)
  7501. (tbl nil)
  7502. (table (cond
  7503. (opt
  7504. (setq type :opt)
  7505. (require 'org-exp)
  7506. (append
  7507. (mapcar
  7508. (lambda (x)
  7509. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7510. (cons (match-string 2 x) (match-string 1 x)))
  7511. (org-split-string (org-get-current-options) "\n"))
  7512. (mapcar 'list org-additional-option-like-keywords)))
  7513. (startup
  7514. (setq type :startup)
  7515. org-startup-options)
  7516. (link (append org-link-abbrev-alist-local
  7517. org-link-abbrev-alist))
  7518. (texp
  7519. (setq type :tex)
  7520. org-html-entities)
  7521. ((string-match "\\`\\*+[ \t]+\\'"
  7522. (buffer-substring (point-at-bol) beg))
  7523. (setq type :todo)
  7524. (mapcar 'list org-todo-keywords-1))
  7525. (searchhead
  7526. (setq type :searchhead)
  7527. (save-excursion
  7528. (goto-char (point-min))
  7529. (while (re-search-forward org-todo-line-regexp nil t)
  7530. (push (list
  7531. (org-make-org-heading-search-string
  7532. (match-string 3) t))
  7533. tbl)))
  7534. tbl)
  7535. (tag (setq type :tag beg beg1)
  7536. (or org-tag-alist (org-get-buffer-tags)))
  7537. (prop (setq type :prop beg beg1)
  7538. (mapcar 'list (org-buffer-property-keys nil t t)))
  7539. (t (progn
  7540. (call-interactively org-completion-fallback-command)
  7541. (throw 'exit nil)))))
  7542. (pattern (buffer-substring-no-properties beg end))
  7543. (completion (try-completion pattern table confirm)))
  7544. (cond ((eq completion t)
  7545. (if (not (assoc (upcase pattern) table))
  7546. (message "Already complete")
  7547. (if (and (equal type :opt)
  7548. (not (member (car (assoc (upcase pattern) table))
  7549. org-additional-option-like-keywords)))
  7550. (insert (substring (cdr (assoc (upcase pattern) table))
  7551. (length pattern)))
  7552. (if (memq type '(:tag :prop)) (insert ":")))))
  7553. ((null completion)
  7554. (message "Can't find completion for \"%s\"" pattern)
  7555. (ding))
  7556. ((not (string= pattern completion))
  7557. (delete-region beg end)
  7558. (if (string-match " +$" completion)
  7559. (setq completion (replace-match "" t t completion)))
  7560. (insert completion)
  7561. (if (get-buffer-window "*Completions*")
  7562. (delete-window (get-buffer-window "*Completions*")))
  7563. (if (assoc completion table)
  7564. (if (eq type :todo) (insert " ")
  7565. (if (memq type '(:tag :prop)) (insert ":"))))
  7566. (if (and (equal type :opt) (assoc completion table))
  7567. (message "%s" (substitute-command-keys
  7568. "Press \\[org-complete] again to insert example settings"))))
  7569. (t
  7570. (message "Making completion list...")
  7571. (let ((list (sort (all-completions pattern table confirm)
  7572. 'string<)))
  7573. (with-output-to-temp-buffer "*Completions*"
  7574. (condition-case nil
  7575. ;; Protection needed for XEmacs and emacs 21
  7576. (display-completion-list list pattern)
  7577. (error (display-completion-list list)))))
  7578. (message "Making completion list...%s" "done")))))))
  7579. ;;;; TODO, DEADLINE, Comments
  7580. (defun org-toggle-comment ()
  7581. "Change the COMMENT state of an entry."
  7582. (interactive)
  7583. (save-excursion
  7584. (org-back-to-heading)
  7585. (let (case-fold-search)
  7586. (if (looking-at (concat outline-regexp
  7587. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7588. (replace-match "" t t nil 1)
  7589. (if (looking-at outline-regexp)
  7590. (progn
  7591. (goto-char (match-end 0))
  7592. (insert org-comment-string " ")))))))
  7593. (defvar org-last-todo-state-is-todo nil
  7594. "This is non-nil when the last TODO state change led to a TODO state.
  7595. If the last change removed the TODO tag or switched to DONE, then
  7596. this is nil.")
  7597. (defvar org-setting-tags nil) ; dynamically skipped
  7598. (defun org-parse-local-options (string var)
  7599. "Parse STRING for startup setting relevant for variable VAR."
  7600. (let ((rtn (symbol-value var))
  7601. e opts)
  7602. (save-match-data
  7603. (if (or (not string) (not (string-match "\\S-" string)))
  7604. rtn
  7605. (setq opts (delq nil (mapcar (lambda (x)
  7606. (setq e (assoc x org-startup-options))
  7607. (if (eq (nth 1 e) var) e nil))
  7608. (org-split-string string "[ \t]+"))))
  7609. (if (not opts)
  7610. rtn
  7611. (setq rtn nil)
  7612. (while (setq e (pop opts))
  7613. (if (not (nth 3 e))
  7614. (setq rtn (nth 2 e))
  7615. (if (not (listp rtn)) (setq rtn nil))
  7616. (push (nth 2 e) rtn)))
  7617. rtn)))))
  7618. (defvar org-todo-setup-filter-hook nil
  7619. "Hook for functions that pre-filter todo specs.
  7620. Each function takes a todo spec and returns either `nil' or the spec
  7621. transformed into canonical form." )
  7622. (defvar org-todo-get-default-hook nil
  7623. "Hook for functions that get a default item for todo.
  7624. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7625. `nil' or a string to be used for the todo mark." )
  7626. (defvar org-agenda-headline-snapshot-before-repeat)
  7627. (defun org-todo (&optional arg)
  7628. "Change the TODO state of an item.
  7629. The state of an item is given by a keyword at the start of the heading,
  7630. like
  7631. *** TODO Write paper
  7632. *** DONE Call mom
  7633. The different keywords are specified in the variable `org-todo-keywords'.
  7634. By default the available states are \"TODO\" and \"DONE\".
  7635. So for this example: when the item starts with TODO, it is changed to DONE.
  7636. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7637. DONE are present, add TODO at the beginning of the heading.
  7638. With C-u prefix arg, use completion to determine the new state.
  7639. With numeric prefix arg, switch to that state.
  7640. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7641. With a tripple C-u prefix, circumvent any state blocking.
  7642. For calling through lisp, arg is also interpreted in the following way:
  7643. 'none -> empty state
  7644. \"\"(empty string) -> switch to empty state
  7645. 'done -> switch to DONE
  7646. 'nextset -> switch to the next set of keywords
  7647. 'previousset -> switch to the previous set of keywords
  7648. \"WAITING\" -> switch to the specified keyword, but only if it
  7649. really is a member of `org-todo-keywords'."
  7650. (interactive "P")
  7651. (if (equal arg '(16)) (setq arg 'nextset))
  7652. (let ((org-blocker-hook org-blocker-hook))
  7653. (when (equal arg '(64))
  7654. (setq arg nil org-blocker-hook nil))
  7655. (save-excursion
  7656. (catch 'exit
  7657. (org-back-to-heading)
  7658. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7659. (or (looking-at (concat " +" org-todo-regexp " *"))
  7660. (looking-at " *"))
  7661. (let* ((match-data (match-data))
  7662. (startpos (point-at-bol))
  7663. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7664. (org-log-done org-log-done)
  7665. (org-log-repeat org-log-repeat)
  7666. (org-todo-log-states org-todo-log-states)
  7667. (this (match-string 1))
  7668. (hl-pos (match-beginning 0))
  7669. (head (org-get-todo-sequence-head this))
  7670. (ass (assoc head org-todo-kwd-alist))
  7671. (interpret (nth 1 ass))
  7672. (done-word (nth 3 ass))
  7673. (final-done-word (nth 4 ass))
  7674. (last-state (or this ""))
  7675. (completion-ignore-case t)
  7676. (member (member this org-todo-keywords-1))
  7677. (tail (cdr member))
  7678. (state (cond
  7679. ((and org-todo-key-trigger
  7680. (or (and (equal arg '(4))
  7681. (eq org-use-fast-todo-selection 'prefix))
  7682. (and (not arg) org-use-fast-todo-selection
  7683. (not (eq org-use-fast-todo-selection
  7684. 'prefix)))))
  7685. ;; Use fast selection
  7686. (org-fast-todo-selection))
  7687. ((and (equal arg '(4))
  7688. (or (not org-use-fast-todo-selection)
  7689. (not org-todo-key-trigger)))
  7690. ;; Read a state with completion
  7691. (org-ido-completing-read
  7692. "State: " (mapcar (lambda(x) (list x))
  7693. org-todo-keywords-1)
  7694. nil t))
  7695. ((eq arg 'right)
  7696. (if this
  7697. (if tail (car tail) nil)
  7698. (car org-todo-keywords-1)))
  7699. ((eq arg 'left)
  7700. (if (equal member org-todo-keywords-1)
  7701. nil
  7702. (if this
  7703. (nth (- (length org-todo-keywords-1)
  7704. (length tail) 2)
  7705. org-todo-keywords-1)
  7706. (org-last org-todo-keywords-1))))
  7707. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7708. (setq arg nil))) ; hack to fall back to cycling
  7709. (arg
  7710. ;; user or caller requests a specific state
  7711. (cond
  7712. ((equal arg "") nil)
  7713. ((eq arg 'none) nil)
  7714. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7715. ((eq arg 'nextset)
  7716. (or (car (cdr (member head org-todo-heads)))
  7717. (car org-todo-heads)))
  7718. ((eq arg 'previousset)
  7719. (let ((org-todo-heads (reverse org-todo-heads)))
  7720. (or (car (cdr (member head org-todo-heads)))
  7721. (car org-todo-heads))))
  7722. ((car (member arg org-todo-keywords-1)))
  7723. ((nth (1- (prefix-numeric-value arg))
  7724. org-todo-keywords-1))))
  7725. ((null member) (or head (car org-todo-keywords-1)))
  7726. ((equal this final-done-word) nil) ;; -> make empty
  7727. ((null tail) nil) ;; -> first entry
  7728. ((memq interpret '(type priority))
  7729. (if (eq this-command last-command)
  7730. (car tail)
  7731. (if (> (length tail) 0)
  7732. (or done-word (car org-done-keywords))
  7733. nil)))
  7734. (t
  7735. (car tail))))
  7736. (state (or
  7737. (run-hook-with-args-until-success
  7738. 'org-todo-get-default-hook state last-state)
  7739. state))
  7740. (next (if state (concat " " state " ") " "))
  7741. (change-plist (list :type 'todo-state-change :from this :to state
  7742. :position startpos))
  7743. dolog now-done-p)
  7744. (when org-blocker-hook
  7745. (setq org-last-todo-state-is-todo
  7746. (not (member this org-done-keywords)))
  7747. (unless (save-excursion
  7748. (save-match-data
  7749. (run-hook-with-args-until-failure
  7750. 'org-blocker-hook change-plist)))
  7751. (if (interactive-p)
  7752. (error "TODO state change from %s to %s blocked" this state)
  7753. ;; fail silently
  7754. (message "TODO state change from %s to %s blocked" this state)
  7755. (throw 'exit nil))))
  7756. (store-match-data match-data)
  7757. (replace-match next t t)
  7758. (unless (pos-visible-in-window-p hl-pos)
  7759. (message "TODO state changed to %s" (org-trim next)))
  7760. (unless head
  7761. (setq head (org-get-todo-sequence-head state)
  7762. ass (assoc head org-todo-kwd-alist)
  7763. interpret (nth 1 ass)
  7764. done-word (nth 3 ass)
  7765. final-done-word (nth 4 ass)))
  7766. (when (memq arg '(nextset previousset))
  7767. (message "Keyword-Set %d/%d: %s"
  7768. (- (length org-todo-sets) -1
  7769. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7770. (length org-todo-sets)
  7771. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7772. (setq org-last-todo-state-is-todo
  7773. (not (member state org-done-keywords)))
  7774. (setq now-done-p (and (member state org-done-keywords)
  7775. (not (member this org-done-keywords))))
  7776. (and logging (org-local-logging logging))
  7777. (when (and (or org-todo-log-states org-log-done)
  7778. (not (memq arg '(nextset previousset))))
  7779. ;; we need to look at recording a time and note
  7780. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7781. (nth 2 (assoc this org-todo-log-states))))
  7782. (when (and state
  7783. (member state org-not-done-keywords)
  7784. (not (member this org-not-done-keywords)))
  7785. ;; This is now a todo state and was not one before
  7786. ;; If there was a CLOSED time stamp, get rid of it.
  7787. (org-add-planning-info nil nil 'closed))
  7788. (when (and now-done-p org-log-done)
  7789. ;; It is now done, and it was not done before
  7790. (org-add-planning-info 'closed (org-current-time))
  7791. (if (and (not dolog) (eq 'note org-log-done))
  7792. (org-add-log-setup 'done state this 'findpos 'note)))
  7793. (when (and state dolog)
  7794. ;; This is a non-nil state, and we need to log it
  7795. (org-add-log-setup 'state state this 'findpos dolog)))
  7796. ;; Fixup tag positioning
  7797. (org-todo-trigger-tag-changes state)
  7798. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7799. (when org-provide-todo-statistics
  7800. (org-update-parent-todo-statistics))
  7801. (run-hooks 'org-after-todo-state-change-hook)
  7802. (if (and arg (not (member state org-done-keywords)))
  7803. (setq head (org-get-todo-sequence-head state)))
  7804. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7805. ;; Do we need to trigger a repeat?
  7806. (when now-done-p
  7807. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7808. ;; This is for the agenda, take a snapshot of the headline.
  7809. (save-match-data
  7810. (setq org-agenda-headline-snapshot-before-repeat
  7811. (org-get-heading))))
  7812. (org-auto-repeat-maybe state))
  7813. ;; Fixup cursor location if close to the keyword
  7814. (if (and (outline-on-heading-p)
  7815. (not (bolp))
  7816. (save-excursion (beginning-of-line 1)
  7817. (looking-at org-todo-line-regexp))
  7818. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7819. (progn
  7820. (goto-char (or (match-end 2) (match-end 1)))
  7821. (just-one-space)))
  7822. (when org-trigger-hook
  7823. (save-excursion
  7824. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  7825. (defun org-block-todo-from-children-or-siblings (change-plist)
  7826. "Block turning an entry into a TODO, using the hierarchy.
  7827. This checks whether the current task should be blocked from state
  7828. changes. Such blocking occurs when:
  7829. 1. The task has children which are not all in a completed state.
  7830. 2. A task has a parent with the property :ORDERED:, and there
  7831. are siblings prior to the current task with incomplete
  7832. status."
  7833. (catch 'dont-block
  7834. ;; If this is not a todo state change, or if this entry is already DONE,
  7835. ;; do not block
  7836. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7837. (member (plist-get change-plist :from)
  7838. (cons 'done org-done-keywords))
  7839. (member (plist-get change-plist :to)
  7840. (cons 'todo org-not-done-keywords)))
  7841. (throw 'dont-block t))
  7842. ;; If this task has children, and any are undone, it's blocked
  7843. (save-excursion
  7844. (org-back-to-heading t)
  7845. (let ((this-level (funcall outline-level)))
  7846. (outline-next-heading)
  7847. (let ((child-level (funcall outline-level)))
  7848. (while (and (not (eobp))
  7849. (> child-level this-level))
  7850. ;; this todo has children, check whether they are all
  7851. ;; completed
  7852. (if (and (not (org-entry-is-done-p))
  7853. (org-entry-is-todo-p))
  7854. (throw 'dont-block nil))
  7855. (outline-next-heading)
  7856. (setq child-level (funcall outline-level))))))
  7857. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7858. ;; any previous siblings are undone, it's blocked
  7859. (save-excursion
  7860. (org-back-to-heading t)
  7861. (when (save-excursion
  7862. (ignore-errors
  7863. (outline-up-heading 1)
  7864. (org-entry-get (point) "ORDERED")))
  7865. (let* ((this-level (funcall outline-level))
  7866. (current-level this-level))
  7867. (while (and (not (bobp))
  7868. (= current-level this-level))
  7869. (outline-previous-heading)
  7870. (setq current-level (funcall outline-level))
  7871. (if (= current-level this-level)
  7872. ;; this todo has children, check whether they are all
  7873. ;; completed
  7874. (if (and (not (org-entry-is-done-p))
  7875. (org-entry-is-todo-p))
  7876. (throw 'dont-block nil)))))))
  7877. t)) ; don't block
  7878. (defun org-toggle-ordered-property ()
  7879. "Toggle the ORDERED property of the current entry."
  7880. (interactive)
  7881. (save-excursion
  7882. (org-back-to-heading)
  7883. (if (org-entry-get nil "ORDERED")
  7884. (progn
  7885. (org-delete-property "ORDERED")
  7886. (message "Subtasks can be completed in arbitrary order or parallel"))
  7887. (org-entry-put nil "ORDERED" "t")
  7888. (message "Subtasks must be completed in sequence"))))
  7889. (defun org-block-todo-from-checkboxes (change-plist)
  7890. "Block turning an entry into a TODO, using checkboxes.
  7891. This checks whether the current task should be blocked from state
  7892. changes because there are uncheckd boxes in this entry."
  7893. (catch 'dont-block
  7894. ;; If this is not a todo state change, or if this entry is already DONE,
  7895. ;; do not block
  7896. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7897. (member (plist-get change-plist :from)
  7898. (cons 'done org-done-keywords))
  7899. (member (plist-get change-plist :to)
  7900. (cons 'todo org-not-done-keywords)))
  7901. (throw 'dont-block t))
  7902. ;; If this task has checkboxes that are not checked, it's blocked
  7903. (save-excursion
  7904. (org-back-to-heading t)
  7905. (let ((beg (point)) end)
  7906. (outline-next-heading)
  7907. (setq end (point))
  7908. (goto-char beg)
  7909. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  7910. end t)
  7911. (throw 'dont-block nil))))
  7912. t)) ; do not block
  7913. (defun org-update-parent-todo-statistics ()
  7914. "Update any statistics cookie in the parent of the current headline."
  7915. (interactive)
  7916. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7917. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  7918. (catch 'exit
  7919. (save-excursion
  7920. (setq level (org-up-heading-safe))
  7921. (unless level
  7922. (throw 'exit nil))
  7923. (while (re-search-forward box-re (point-at-eol) t)
  7924. (setq cnt-all 0 cnt-done 0 cookie-present t)
  7925. (setq is-percent (match-end 2))
  7926. (save-match-data
  7927. (unless (outline-next-heading) (throw 'exit nil))
  7928. (while (looking-at org-todo-line-regexp)
  7929. (setq kwd (match-string 2))
  7930. (and kwd (setq cnt-all (1+ cnt-all)))
  7931. (and (member kwd org-done-keywords)
  7932. (setq cnt-done (1+ cnt-done)))
  7933. (condition-case nil
  7934. (org-forward-same-level 1)
  7935. (error (end-of-line 1)))))
  7936. (replace-match
  7937. (if is-percent
  7938. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7939. (format "[%d/%d]" cnt-done cnt-all))))
  7940. (when cookie-present
  7941. (run-hook-with-args 'org-after-todo-statistics-hook
  7942. cnt-done (- cnt-all cnt-done)))))))
  7943. (defvar org-after-todo-statistics-hook nil
  7944. "Hook that is called after a TODO statistics cookie has been updated.
  7945. Each function is called with two arguments: the number of not-done entries
  7946. and the number of done entries.
  7947. For example, the following function, when added to this hook, will switch
  7948. an entry to DONE when all children are done, and back to TODO when new
  7949. entries are set to a TODO status. Note that this hook is only called
  7950. when there is a statistics cookie in the headline!
  7951. (defun org-summary-todo (n-done n-not-done)
  7952. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7953. (let (org-log-done org-log-states) ; turn off logging
  7954. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7955. ")
  7956. (defun org-todo-trigger-tag-changes (state)
  7957. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7958. (let ((l org-todo-state-tags-triggers)
  7959. changes)
  7960. (when (or (not state) (equal state ""))
  7961. (setq changes (append changes (cdr (assoc "" l)))))
  7962. (when (and (stringp state) (> (length state) 0))
  7963. (setq changes (append changes (cdr (assoc state l)))))
  7964. (when (member state org-not-done-keywords)
  7965. (setq changes (append changes (cdr (assoc 'todo l)))))
  7966. (when (member state org-done-keywords)
  7967. (setq changes (append changes (cdr (assoc 'done l)))))
  7968. (dolist (c changes)
  7969. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7970. (defun org-local-logging (value)
  7971. "Get logging settings from a property VALUE."
  7972. (let* (words w a)
  7973. ;; directly set the variables, they are already local.
  7974. (setq org-log-done nil
  7975. org-log-repeat nil
  7976. org-todo-log-states nil)
  7977. (setq words (org-split-string value))
  7978. (while (setq w (pop words))
  7979. (cond
  7980. ((setq a (assoc w org-startup-options))
  7981. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7982. (set (nth 1 a) (nth 2 a))))
  7983. ((setq a (org-extract-log-state-settings w))
  7984. (and (member (car a) org-todo-keywords-1)
  7985. (push a org-todo-log-states)))))))
  7986. (defun org-get-todo-sequence-head (kwd)
  7987. "Return the head of the TODO sequence to which KWD belongs.
  7988. If KWD is not set, check if there is a text property remembering the
  7989. right sequence."
  7990. (let (p)
  7991. (cond
  7992. ((not kwd)
  7993. (or (get-text-property (point-at-bol) 'org-todo-head)
  7994. (progn
  7995. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7996. nil (point-at-eol)))
  7997. (get-text-property p 'org-todo-head))))
  7998. ((not (member kwd org-todo-keywords-1))
  7999. (car org-todo-keywords-1))
  8000. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8001. (defun org-fast-todo-selection ()
  8002. "Fast TODO keyword selection with single keys.
  8003. Returns the new TODO keyword, or nil if no state change should occur."
  8004. (let* ((fulltable org-todo-key-alist)
  8005. (done-keywords org-done-keywords) ;; needed for the faces.
  8006. (maxlen (apply 'max (mapcar
  8007. (lambda (x)
  8008. (if (stringp (car x)) (string-width (car x)) 0))
  8009. fulltable)))
  8010. (expert nil)
  8011. (fwidth (+ maxlen 3 1 3))
  8012. (ncol (/ (- (window-width) 4) fwidth))
  8013. tg cnt e c tbl
  8014. groups ingroup)
  8015. (save-excursion
  8016. (save-window-excursion
  8017. (if expert
  8018. (set-buffer (get-buffer-create " *Org todo*"))
  8019. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8020. (erase-buffer)
  8021. (org-set-local 'org-done-keywords done-keywords)
  8022. (setq tbl fulltable cnt 0)
  8023. (while (setq e (pop tbl))
  8024. (cond
  8025. ((equal e '(:startgroup))
  8026. (push '() groups) (setq ingroup t)
  8027. (when (not (= cnt 0))
  8028. (setq cnt 0)
  8029. (insert "\n"))
  8030. (insert "{ "))
  8031. ((equal e '(:endgroup))
  8032. (setq ingroup nil cnt 0)
  8033. (insert "}\n"))
  8034. (t
  8035. (setq tg (car e) c (cdr e))
  8036. (if ingroup (push tg (car groups)))
  8037. (setq tg (org-add-props tg nil 'face
  8038. (org-get-todo-face tg)))
  8039. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8040. (insert "[" c "] " tg (make-string
  8041. (- fwidth 4 (length tg)) ?\ ))
  8042. (when (= (setq cnt (1+ cnt)) ncol)
  8043. (insert "\n")
  8044. (if ingroup (insert " "))
  8045. (setq cnt 0)))))
  8046. (insert "\n")
  8047. (goto-char (point-min))
  8048. (if (not expert) (org-fit-window-to-buffer))
  8049. (message "[a-z..]:Set [SPC]:clear")
  8050. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8051. (cond
  8052. ((or (= c ?\C-g)
  8053. (and (= c ?q) (not (rassoc c fulltable))))
  8054. (setq quit-flag t))
  8055. ((= c ?\ ) nil)
  8056. ((setq e (rassoc c fulltable) tg (car e))
  8057. tg)
  8058. (t (setq quit-flag t)))))))
  8059. (defun org-entry-is-todo-p ()
  8060. (member (org-get-todo-state) org-not-done-keywords))
  8061. (defun org-entry-is-done-p ()
  8062. (member (org-get-todo-state) org-done-keywords))
  8063. (defun org-get-todo-state ()
  8064. (save-excursion
  8065. (org-back-to-heading t)
  8066. (and (looking-at org-todo-line-regexp)
  8067. (match-end 2)
  8068. (match-string 2))))
  8069. (defun org-at-date-range-p (&optional inactive-ok)
  8070. "Is the cursor inside a date range?"
  8071. (interactive)
  8072. (save-excursion
  8073. (catch 'exit
  8074. (let ((pos (point)))
  8075. (skip-chars-backward "^[<\r\n")
  8076. (skip-chars-backward "<[")
  8077. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8078. (>= (match-end 0) pos)
  8079. (throw 'exit t))
  8080. (skip-chars-backward "^<[\r\n")
  8081. (skip-chars-backward "<[")
  8082. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8083. (>= (match-end 0) pos)
  8084. (throw 'exit t)))
  8085. nil)))
  8086. (defun org-get-repeat ()
  8087. "Check if there is a deadline/schedule with repeater in this entry."
  8088. (save-match-data
  8089. (save-excursion
  8090. (org-back-to-heading t)
  8091. (if (re-search-forward
  8092. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8093. (match-string 1)))))
  8094. (defvar org-last-changed-timestamp)
  8095. (defvar org-last-inserted-timestamp)
  8096. (defvar org-log-post-message)
  8097. (defvar org-log-note-purpose)
  8098. (defvar org-log-note-how)
  8099. (defvar org-log-note-extra)
  8100. (defun org-auto-repeat-maybe (done-word)
  8101. "Check if the current headline contains a repeated deadline/schedule.
  8102. If yes, set TODO state back to what it was and change the base date
  8103. of repeating deadline/scheduled time stamps to new date.
  8104. This function is run automatically after each state change to a DONE state."
  8105. ;; last-state is dynamically scoped into this function
  8106. (let* ((repeat (org-get-repeat))
  8107. (aa (assoc last-state org-todo-kwd-alist))
  8108. (interpret (nth 1 aa))
  8109. (head (nth 2 aa))
  8110. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8111. (msg "Entry repeats: ")
  8112. (org-log-done nil)
  8113. (org-todo-log-states nil)
  8114. (nshiftmax 10) (nshift 0)
  8115. re type n what ts time)
  8116. (when repeat
  8117. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8118. (org-todo (if (eq interpret 'type) last-state head))
  8119. (when org-log-repeat
  8120. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8121. (memq 'org-add-log-note post-command-hook))
  8122. ;; OK, we are already setup for some record
  8123. (if (eq org-log-repeat 'note)
  8124. ;; make sure we take a note, not only a time stamp
  8125. (setq org-log-note-how 'note))
  8126. ;; Set up for taking a record
  8127. (org-add-log-setup 'state (or done-word (car org-done-keywords)) this
  8128. 'findpos org-log-repeat)))
  8129. (org-back-to-heading t)
  8130. (org-add-planning-info nil nil 'closed)
  8131. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8132. org-deadline-time-regexp "\\)\\|\\("
  8133. org-ts-regexp "\\)"))
  8134. (while (re-search-forward
  8135. re (save-excursion (outline-next-heading) (point)) t)
  8136. (setq type (if (match-end 1) org-scheduled-string
  8137. (if (match-end 3) org-deadline-string "Plain:"))
  8138. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8139. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8140. (setq n (string-to-number (match-string 2 ts))
  8141. what (match-string 3 ts))
  8142. (if (equal what "w") (setq n (* n 7) what "d"))
  8143. ;; Preparation, see if we need to modify the start date for the change
  8144. (when (match-end 1)
  8145. (setq time (save-match-data (org-time-string-to-time ts)))
  8146. (cond
  8147. ((equal (match-string 1 ts) ".")
  8148. ;; Shift starting date to today
  8149. (org-timestamp-change
  8150. (- (time-to-days (current-time)) (time-to-days time))
  8151. 'day))
  8152. ((equal (match-string 1 ts) "+")
  8153. (while (or (= nshift 0)
  8154. (<= (time-to-days time) (time-to-days (current-time))))
  8155. (when (= (incf nshift) nshiftmax)
  8156. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8157. (error "Abort")))
  8158. (org-timestamp-change n (cdr (assoc what whata)))
  8159. (org-at-timestamp-p t)
  8160. (setq ts (match-string 1))
  8161. (setq time (save-match-data (org-time-string-to-time ts))))
  8162. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8163. ;; rematch, so that we have everything in place for the real shift
  8164. (org-at-timestamp-p t)
  8165. (setq ts (match-string 1))
  8166. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8167. (org-timestamp-change n (cdr (assoc what whata)))
  8168. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8169. (setq org-log-post-message msg)
  8170. (message "%s" msg))))
  8171. (defun org-show-todo-tree (arg)
  8172. "Make a compact tree which shows all headlines marked with TODO.
  8173. The tree will show the lines where the regexp matches, and all higher
  8174. headlines above the match.
  8175. With a \\[universal-argument] prefix, also show the DONE entries.
  8176. With a numeric prefix N, construct a sparse tree for the Nth element
  8177. of `org-todo-keywords-1'."
  8178. (interactive "P")
  8179. (let ((case-fold-search nil)
  8180. (kwd-re
  8181. (cond ((null arg) org-not-done-regexp)
  8182. ((equal arg '(4))
  8183. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8184. (mapcar 'list org-todo-keywords-1))))
  8185. (concat "\\("
  8186. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8187. "\\)\\>")))
  8188. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8189. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8190. org-todo-keywords-1)))
  8191. (t (error "Invalid prefix argument: %s" arg)))))
  8192. (message "%d TODO entries found"
  8193. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8194. (defun org-deadline (&optional remove time)
  8195. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8196. With argument REMOVE, remove any deadline from the item.
  8197. When TIME is set, it should be an internal time specification, and the
  8198. scheduling will use the corresponding date."
  8199. (interactive "P")
  8200. (if remove
  8201. (progn
  8202. (org-remove-timestamp-with-keyword org-deadline-string)
  8203. (message "Item no longer has a deadline."))
  8204. (if (org-get-repeat)
  8205. (error "Cannot change deadline on task with repeater, please do that by hand")
  8206. (org-add-planning-info 'deadline time 'closed)
  8207. (message "Deadline on %s" org-last-inserted-timestamp))))
  8208. (defun org-schedule (&optional remove time)
  8209. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8210. With argument REMOVE, remove any scheduling date from the item.
  8211. When TIME is set, it should be an internal time specification, and the
  8212. scheduling will use the corresponding date."
  8213. (interactive "P")
  8214. (if remove
  8215. (progn
  8216. (org-remove-timestamp-with-keyword org-scheduled-string)
  8217. (message "Item is no longer scheduled."))
  8218. (if (org-get-repeat)
  8219. (error "Cannot reschedule task with repeater, please do that by hand")
  8220. (org-add-planning-info 'scheduled time 'closed)
  8221. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8222. (defun org-get-scheduled-time (pom &optional inherit)
  8223. "Get the scheduled time as a time tuple, of a format suitable
  8224. for calling org-schedule with, or if there is no scheduling,
  8225. returns nil."
  8226. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8227. (when time
  8228. (apply 'encode-time (org-parse-time-string time)))))
  8229. (defun org-get-deadline-time (pom &optional inherit)
  8230. "Get the deadine as a time tuple, of a format suitable for
  8231. calling org-deadlin with, or if there is no scheduling, returns
  8232. nil."
  8233. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8234. (when time
  8235. (apply 'encode-time (org-parse-time-string time)))))
  8236. (defun org-remove-timestamp-with-keyword (keyword)
  8237. "Remove all time stamps with KEYWORD in the current entry."
  8238. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8239. beg)
  8240. (save-excursion
  8241. (org-back-to-heading t)
  8242. (setq beg (point))
  8243. (org-end-of-subtree t t)
  8244. (while (re-search-backward re beg t)
  8245. (replace-match "")
  8246. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8247. (equal (char-before) ?\ ))
  8248. (backward-delete-char 1)
  8249. (if (string-match "^[ \t]*$" (buffer-substring
  8250. (point-at-bol) (point-at-eol)))
  8251. (delete-region (point-at-bol)
  8252. (min (point-max) (1+ (point-at-eol))))))))))
  8253. (defun org-add-planning-info (what &optional time &rest remove)
  8254. "Insert new timestamp with keyword in the line directly after the headline.
  8255. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8256. If non is given, the user is prompted for a date.
  8257. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8258. be removed."
  8259. (interactive)
  8260. (let (org-time-was-given org-end-time-was-given ts
  8261. end default-time default-input)
  8262. (when (and (not time) (memq what '(scheduled deadline)))
  8263. ;; Try to get a default date/time from existing timestamp
  8264. (save-excursion
  8265. (org-back-to-heading t)
  8266. (setq end (save-excursion (outline-next-heading) (point)))
  8267. (when (re-search-forward (if (eq what 'scheduled)
  8268. org-scheduled-time-regexp
  8269. org-deadline-time-regexp)
  8270. end t)
  8271. (setq ts (match-string 1)
  8272. default-time
  8273. (apply 'encode-time (org-parse-time-string ts))
  8274. default-input (and ts (org-get-compact-tod ts))))))
  8275. (when what
  8276. ;; If necessary, get the time from the user
  8277. (setq time (or time (org-read-date nil 'to-time nil nil
  8278. default-time default-input))))
  8279. (when (and org-insert-labeled-timestamps-at-point
  8280. (member what '(scheduled deadline)))
  8281. (insert
  8282. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8283. (org-insert-time-stamp time org-time-was-given
  8284. nil nil nil (list org-end-time-was-given))
  8285. (setq what nil))
  8286. (save-excursion
  8287. (save-restriction
  8288. (let (col list elt ts buffer-invisibility-spec)
  8289. (org-back-to-heading t)
  8290. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8291. (goto-char (match-end 1))
  8292. (setq col (current-column))
  8293. (goto-char (match-end 0))
  8294. (if (eobp) (insert "\n") (forward-char 1))
  8295. (if (and (not (looking-at outline-regexp))
  8296. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8297. "[^\r\n]*"))
  8298. (not (equal (match-string 1) org-clock-string)))
  8299. (narrow-to-region (match-beginning 0) (match-end 0))
  8300. (insert-before-markers "\n")
  8301. (backward-char 1)
  8302. (narrow-to-region (point) (point))
  8303. (and org-adapt-indentation (org-indent-to-column col)))
  8304. ;; Check if we have to remove something.
  8305. (setq list (cons what remove))
  8306. (while list
  8307. (setq elt (pop list))
  8308. (goto-char (point-min))
  8309. (when (or (and (eq elt 'scheduled)
  8310. (re-search-forward org-scheduled-time-regexp nil t))
  8311. (and (eq elt 'deadline)
  8312. (re-search-forward org-deadline-time-regexp nil t))
  8313. (and (eq elt 'closed)
  8314. (re-search-forward org-closed-time-regexp nil t)))
  8315. (replace-match "")
  8316. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8317. (if (looking-at " +") (replace-match ""))))
  8318. (goto-char (point-max))
  8319. (when what
  8320. (insert
  8321. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8322. (cond ((eq what 'scheduled) org-scheduled-string)
  8323. ((eq what 'deadline) org-deadline-string)
  8324. ((eq what 'closed) org-closed-string))
  8325. " ")
  8326. (setq ts (org-insert-time-stamp
  8327. time
  8328. (or org-time-was-given
  8329. (and (eq what 'closed) org-log-done-with-time))
  8330. (eq what 'closed)
  8331. nil nil (list org-end-time-was-given)))
  8332. (end-of-line 1))
  8333. (goto-char (point-min))
  8334. (widen)
  8335. (if (and (looking-at "[ \t]+\n")
  8336. (equal (char-before) ?\n))
  8337. (delete-region (1- (point)) (point-at-eol)))
  8338. ts)))))
  8339. (defvar org-log-note-marker (make-marker))
  8340. (defvar org-log-note-purpose nil)
  8341. (defvar org-log-note-state nil)
  8342. (defvar org-log-note-previous-state nil)
  8343. (defvar org-log-note-how nil)
  8344. (defvar org-log-note-extra nil)
  8345. (defvar org-log-note-window-configuration nil)
  8346. (defvar org-log-note-return-to (make-marker))
  8347. (defvar org-log-post-message nil
  8348. "Message to be displayed after a log note has been stored.
  8349. The auto-repeater uses this.")
  8350. (defun org-add-note ()
  8351. "Add a note to the current entry.
  8352. This is done in the same way as adding a state change note."
  8353. (interactive)
  8354. (org-add-log-setup 'note nil nil 'findpos nil))
  8355. (defvar org-property-end-re)
  8356. (defun org-add-log-setup (&optional purpose state prev-state
  8357. findpos how &optional extra)
  8358. "Set up the post command hook to take a note.
  8359. If this is about to TODO state change, the new state is expected in STATE.
  8360. When FINDPOS is non-nil, find the correct position for the note in
  8361. the current entry. If not, assume that it can be inserted at point.
  8362. HOW is an indicator what kind of note should be created.
  8363. EXTRA is additional text that will be inserted into the notes buffer."
  8364. (let ((drawer (cond ((stringp org-log-state-notes-into-drawer)
  8365. org-log-state-notes-into-drawer)
  8366. (org-log-state-notes-into-drawer "LOGBOOK")
  8367. (t nil))))
  8368. (save-restriction
  8369. (save-excursion
  8370. (when findpos
  8371. (org-back-to-heading t)
  8372. (narrow-to-region (point) (save-excursion
  8373. (outline-next-heading) (point)))
  8374. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8375. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8376. "[^\r\n]*\\)?"))
  8377. (goto-char (match-end 0))
  8378. (cond
  8379. (drawer
  8380. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8381. nil t)
  8382. (goto-char (match-end 0))
  8383. (insert "\n:" drawer ":\n:END:")
  8384. (beginning-of-line 0)
  8385. (org-indent-line-function)
  8386. (beginning-of-line 2)
  8387. (org-indent-line-function)
  8388. (end-of-line 0)))
  8389. ((and org-log-state-notes-insert-after-drawers
  8390. (save-excursion
  8391. (forward-line) (looking-at org-drawer-regexp)))
  8392. (forward-line)
  8393. (while (looking-at org-drawer-regexp)
  8394. (goto-char (match-end 0))
  8395. (re-search-forward org-property-end-re (point-max) t)
  8396. (forward-line))
  8397. (forward-line -1)))
  8398. (unless org-log-states-order-reversed
  8399. (and (= (char-after) ?\n) (forward-char 1))
  8400. (org-skip-over-state-notes)
  8401. (skip-chars-backward " \t\n\r")))
  8402. (move-marker org-log-note-marker (point))
  8403. (setq org-log-note-purpose purpose
  8404. org-log-note-state state
  8405. org-log-note-previous-state prev-state
  8406. org-log-note-how how
  8407. org-log-note-extra extra)
  8408. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8409. (defun org-skip-over-state-notes ()
  8410. "Skip past the list of State notes in an entry."
  8411. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8412. (while (looking-at "[ \t]*- State")
  8413. (condition-case nil
  8414. (org-next-item)
  8415. (error (org-end-of-item)))))
  8416. (defun org-add-log-note (&optional purpose)
  8417. "Pop up a window for taking a note, and add this note later at point."
  8418. (remove-hook 'post-command-hook 'org-add-log-note)
  8419. (setq org-log-note-window-configuration (current-window-configuration))
  8420. (delete-other-windows)
  8421. (move-marker org-log-note-return-to (point))
  8422. (switch-to-buffer (marker-buffer org-log-note-marker))
  8423. (goto-char org-log-note-marker)
  8424. (org-switch-to-buffer-other-window "*Org Note*")
  8425. (erase-buffer)
  8426. (if (memq org-log-note-how '(time state))
  8427. (let (current-prefix-arg) (org-store-log-note))
  8428. (let ((org-inhibit-startup t)) (org-mode))
  8429. (insert (format "# Insert note for %s.
  8430. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8431. (cond
  8432. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8433. ((eq org-log-note-purpose 'done) "closed todo item")
  8434. ((eq org-log-note-purpose 'state)
  8435. (format "state change from \"%s\" to \"%s\""
  8436. (or org-log-note-previous-state "")
  8437. (or org-log-note-state "")))
  8438. ((eq org-log-note-purpose 'note)
  8439. "this entry")
  8440. (t (error "This should not happen")))))
  8441. (if org-log-note-extra (insert org-log-note-extra))
  8442. (org-set-local 'org-finish-function 'org-store-log-note)))
  8443. (defvar org-note-abort nil) ; dynamically scoped
  8444. (defun org-store-log-note ()
  8445. "Finish taking a log note, and insert it to where it belongs."
  8446. (let ((txt (buffer-string))
  8447. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8448. lines ind)
  8449. (kill-buffer (current-buffer))
  8450. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8451. (setq txt (replace-match "" t t txt)))
  8452. (if (string-match "\\s-+\\'" txt)
  8453. (setq txt (replace-match "" t t txt)))
  8454. (setq lines (org-split-string txt "\n"))
  8455. (when (and note (string-match "\\S-" note))
  8456. (setq note
  8457. (org-replace-escapes
  8458. note
  8459. (list (cons "%u" (user-login-name))
  8460. (cons "%U" user-full-name)
  8461. (cons "%t" (format-time-string
  8462. (org-time-stamp-format 'long 'inactive)
  8463. (current-time)))
  8464. (cons "%s" (if org-log-note-state
  8465. (concat "\"" org-log-note-state "\"")
  8466. ""))
  8467. (cons "%S" (if org-log-note-previous-state
  8468. (concat "\"" org-log-note-previous-state "\"")
  8469. "\"\"")))))
  8470. (if lines (setq note (concat note " \\\\")))
  8471. (push note lines))
  8472. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8473. (when lines
  8474. (save-excursion
  8475. (set-buffer (marker-buffer org-log-note-marker))
  8476. (save-excursion
  8477. (goto-char org-log-note-marker)
  8478. (move-marker org-log-note-marker nil)
  8479. (end-of-line 1)
  8480. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8481. (insert "- " (pop lines))
  8482. (org-indent-line-function)
  8483. (beginning-of-line 1)
  8484. (looking-at "[ \t]*")
  8485. (setq ind (concat (match-string 0) " "))
  8486. (end-of-line 1)
  8487. (while lines (insert "\n" ind (pop lines)))
  8488. (message "Note stored")
  8489. (org-back-to-heading t)
  8490. (org-cycle-hide-drawers 'children)))))
  8491. (set-window-configuration org-log-note-window-configuration)
  8492. (with-current-buffer (marker-buffer org-log-note-return-to)
  8493. (goto-char org-log-note-return-to))
  8494. (move-marker org-log-note-return-to nil)
  8495. (and org-log-post-message (message "%s" org-log-post-message)))
  8496. (defun org-sparse-tree (&optional arg)
  8497. "Create a sparse tree, prompt for the details.
  8498. This command can create sparse trees. You first need to select the type
  8499. of match used to create the tree:
  8500. t Show entries with a specific TODO keyword.
  8501. T Show entries selected by a tags match.
  8502. p Enter a property name and its value (both with completion on existing
  8503. names/values) and show entries with that property.
  8504. r Show entries matching a regular expression
  8505. d Show deadlines due within `org-deadline-warning-days'."
  8506. (interactive "P")
  8507. (let (ans kwd value)
  8508. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8509. (setq ans (read-char-exclusive))
  8510. (cond
  8511. ((equal ans ?d)
  8512. (call-interactively 'org-check-deadlines))
  8513. ((equal ans ?b)
  8514. (call-interactively 'org-check-before-date))
  8515. ((equal ans ?t)
  8516. (org-show-todo-tree '(4)))
  8517. ((equal ans ?T)
  8518. (call-interactively 'org-tags-sparse-tree))
  8519. ((member ans '(?p ?P))
  8520. (setq kwd (org-ido-completing-read "Property: "
  8521. (mapcar 'list (org-buffer-property-keys))))
  8522. (setq value (org-ido-completing-read "Value: "
  8523. (mapcar 'list (org-property-values kwd))))
  8524. (unless (string-match "\\`{.*}\\'" value)
  8525. (setq value (concat "\"" value "\"")))
  8526. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8527. ((member ans '(?r ?R ?/))
  8528. (call-interactively 'org-occur))
  8529. (t (error "No such sparse tree command \"%c\"" ans)))))
  8530. (defvar org-occur-highlights nil
  8531. "List of overlays used for occur matches.")
  8532. (make-variable-buffer-local 'org-occur-highlights)
  8533. (defvar org-occur-parameters nil
  8534. "Parameters of the active org-occur calls.
  8535. This is a list, each call to org-occur pushes as cons cell,
  8536. containing the regular expression and the callback, onto the list.
  8537. The list can contain several entries if `org-occur' has been called
  8538. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8539. will only contain one set of parameters. When the highlights are
  8540. removed (for example with `C-c C-c', or with the next edit (depending
  8541. on `org-remove-highlights-with-change'), this variable is emptied
  8542. as well.")
  8543. (make-variable-buffer-local 'org-occur-parameters)
  8544. (defun org-occur (regexp &optional keep-previous callback)
  8545. "Make a compact tree which shows all matches of REGEXP.
  8546. The tree will show the lines where the regexp matches, and all higher
  8547. headlines above the match. It will also show the heading after the match,
  8548. to make sure editing the matching entry is easy.
  8549. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8550. call to `org-occur' will be kept, to allow stacking of calls to this
  8551. command.
  8552. If CALLBACK is non-nil, it is a function which is called to confirm
  8553. that the match should indeed be shown."
  8554. (interactive "sRegexp: \nP")
  8555. (unless keep-previous
  8556. (org-remove-occur-highlights nil nil t))
  8557. (push (cons regexp callback) org-occur-parameters)
  8558. (let ((cnt 0))
  8559. (save-excursion
  8560. (goto-char (point-min))
  8561. (if (or (not keep-previous) ; do not want to keep
  8562. (not org-occur-highlights)) ; no previous matches
  8563. ;; hide everything
  8564. (org-overview))
  8565. (while (re-search-forward regexp nil t)
  8566. (when (or (not callback)
  8567. (save-match-data (funcall callback)))
  8568. (setq cnt (1+ cnt))
  8569. (when org-highlight-sparse-tree-matches
  8570. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8571. (org-show-context 'occur-tree))))
  8572. (when org-remove-highlights-with-change
  8573. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8574. nil 'local))
  8575. (unless org-sparse-tree-open-archived-trees
  8576. (org-hide-archived-subtrees (point-min) (point-max)))
  8577. (run-hooks 'org-occur-hook)
  8578. (if (interactive-p)
  8579. (message "%d match(es) for regexp %s" cnt regexp))
  8580. cnt))
  8581. (defun org-show-context (&optional key)
  8582. "Make sure point and context and visible.
  8583. How much context is shown depends upon the variables
  8584. `org-show-hierarchy-above', `org-show-following-heading'. and
  8585. `org-show-siblings'."
  8586. (let ((heading-p (org-on-heading-p t))
  8587. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8588. (following-p (org-get-alist-option org-show-following-heading key))
  8589. (entry-p (org-get-alist-option org-show-entry-below key))
  8590. (siblings-p (org-get-alist-option org-show-siblings key)))
  8591. (catch 'exit
  8592. ;; Show heading or entry text
  8593. (if (and heading-p (not entry-p))
  8594. (org-flag-heading nil) ; only show the heading
  8595. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8596. (org-show-hidden-entry))) ; show entire entry
  8597. (when following-p
  8598. ;; Show next sibling, or heading below text
  8599. (save-excursion
  8600. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8601. (org-flag-heading nil))))
  8602. (when siblings-p (org-show-siblings))
  8603. (when hierarchy-p
  8604. ;; show all higher headings, possibly with siblings
  8605. (save-excursion
  8606. (while (and (condition-case nil
  8607. (progn (org-up-heading-all 1) t)
  8608. (error nil))
  8609. (not (bobp)))
  8610. (org-flag-heading nil)
  8611. (when siblings-p (org-show-siblings))))))))
  8612. (defun org-reveal (&optional siblings)
  8613. "Show current entry, hierarchy above it, and the following headline.
  8614. This can be used to show a consistent set of context around locations
  8615. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8616. not t for the search context.
  8617. With optional argument SIBLINGS, on each level of the hierarchy all
  8618. siblings are shown. This repairs the tree structure to what it would
  8619. look like when opened with hierarchical calls to `org-cycle'."
  8620. (interactive "P")
  8621. (let ((org-show-hierarchy-above t)
  8622. (org-show-following-heading t)
  8623. (org-show-siblings (if siblings t org-show-siblings)))
  8624. (org-show-context nil)))
  8625. (defun org-highlight-new-match (beg end)
  8626. "Highlight from BEG to END and mark the highlight is an occur headline."
  8627. (let ((ov (org-make-overlay beg end)))
  8628. (org-overlay-put ov 'face 'secondary-selection)
  8629. (push ov org-occur-highlights)))
  8630. (defun org-remove-occur-highlights (&optional beg end noremove)
  8631. "Remove the occur highlights from the buffer.
  8632. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8633. from the `before-change-functions' in the current buffer."
  8634. (interactive)
  8635. (unless org-inhibit-highlight-removal
  8636. (mapc 'org-delete-overlay org-occur-highlights)
  8637. (setq org-occur-highlights nil)
  8638. (setq org-occur-parameters nil)
  8639. (unless noremove
  8640. (remove-hook 'before-change-functions
  8641. 'org-remove-occur-highlights 'local))))
  8642. ;;;; Priorities
  8643. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8644. "Regular expression matching the priority indicator.")
  8645. (defvar org-remove-priority-next-time nil)
  8646. (defun org-priority-up ()
  8647. "Increase the priority of the current item."
  8648. (interactive)
  8649. (org-priority 'up))
  8650. (defun org-priority-down ()
  8651. "Decrease the priority of the current item."
  8652. (interactive)
  8653. (org-priority 'down))
  8654. (defun org-priority (&optional action)
  8655. "Change the priority of an item by ARG.
  8656. ACTION can be `set', `up', `down', or a character."
  8657. (interactive)
  8658. (setq action (or action 'set))
  8659. (let (current new news have remove)
  8660. (save-excursion
  8661. (org-back-to-heading)
  8662. (if (looking-at org-priority-regexp)
  8663. (setq current (string-to-char (match-string 2))
  8664. have t)
  8665. (setq current org-default-priority))
  8666. (cond
  8667. ((or (eq action 'set)
  8668. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8669. (if (not (eq action 'set))
  8670. (setq new action)
  8671. (message "Priority %c-%c, SPC to remove: "
  8672. org-highest-priority org-lowest-priority)
  8673. (setq new (read-char-exclusive)))
  8674. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8675. (= (upcase org-lowest-priority) org-lowest-priority))
  8676. (setq new (upcase new)))
  8677. (cond ((equal new ?\ ) (setq remove t))
  8678. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8679. (error "Priority must be between `%c' and `%c'"
  8680. org-highest-priority org-lowest-priority))))
  8681. ((eq action 'up)
  8682. (if (and (not have) (eq last-command this-command))
  8683. (setq new org-lowest-priority)
  8684. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8685. org-default-priority (1- current)))))
  8686. ((eq action 'down)
  8687. (if (and (not have) (eq last-command this-command))
  8688. (setq new org-highest-priority)
  8689. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8690. org-default-priority (1+ current)))))
  8691. (t (error "Invalid action")))
  8692. (if (or (< (upcase new) org-highest-priority)
  8693. (> (upcase new) org-lowest-priority))
  8694. (setq remove t))
  8695. (setq news (format "%c" new))
  8696. (if have
  8697. (if remove
  8698. (replace-match "" t t nil 1)
  8699. (replace-match news t t nil 2))
  8700. (if remove
  8701. (error "No priority cookie found in line")
  8702. (looking-at org-todo-line-regexp)
  8703. (if (match-end 2)
  8704. (progn
  8705. (goto-char (match-end 2))
  8706. (insert " [#" news "]"))
  8707. (goto-char (match-beginning 3))
  8708. (insert "[#" news "] ")))))
  8709. (org-preserve-lc (org-set-tags nil 'align))
  8710. (if remove
  8711. (message "Priority removed")
  8712. (message "Priority of current item set to %s" news))))
  8713. (defun org-get-priority (s)
  8714. "Find priority cookie and return priority."
  8715. (save-match-data
  8716. (if (not (string-match org-priority-regexp s))
  8717. (* 1000 (- org-lowest-priority org-default-priority))
  8718. (* 1000 (- org-lowest-priority
  8719. (string-to-char (match-string 2 s)))))))
  8720. ;;;; Tags
  8721. (defvar org-agenda-archives-mode)
  8722. (defun org-scan-tags (action matcher &optional todo-only)
  8723. "Scan headline tags with inheritance and produce output ACTION.
  8724. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8725. or `agenda' to produce an entry list for an agenda view. It can also be
  8726. a Lisp form or a function that should be called at each matched headline, in
  8727. this case the return value is a list of all return values from these calls.
  8728. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8729. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8730. only lines with a TODO keyword are included in the output."
  8731. (require 'org-agenda)
  8732. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8733. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8734. (org-re
  8735. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8736. (props (list 'face 'default
  8737. 'done-face 'org-done
  8738. 'undone-face 'default
  8739. 'mouse-face 'highlight
  8740. 'org-not-done-regexp org-not-done-regexp
  8741. 'org-todo-regexp org-todo-regexp
  8742. 'keymap org-agenda-keymap
  8743. 'help-echo
  8744. (format "mouse-2 or RET jump to org file %s"
  8745. (abbreviate-file-name
  8746. (or (buffer-file-name (buffer-base-buffer))
  8747. (buffer-name (buffer-base-buffer)))))))
  8748. (case-fold-search nil)
  8749. lspos tags tags-list
  8750. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8751. (llast 0) rtn rtn1 level category i txt
  8752. todo marker entry priority)
  8753. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8754. (setq action (list 'lambda nil action)))
  8755. (save-excursion
  8756. (goto-char (point-min))
  8757. (when (eq action 'sparse-tree)
  8758. (org-overview)
  8759. (org-remove-occur-highlights))
  8760. (while (re-search-forward re nil t)
  8761. (catch :skip
  8762. (setq todo (if (match-end 1) (match-string 2))
  8763. tags (if (match-end 4) (match-string 4)))
  8764. (goto-char (setq lspos (1+ (match-beginning 0))))
  8765. (setq level (org-reduced-level (funcall outline-level))
  8766. category (org-get-category))
  8767. (setq i llast llast level)
  8768. ;; remove tag lists from same and sublevels
  8769. (while (>= i level)
  8770. (when (setq entry (assoc i tags-alist))
  8771. (setq tags-alist (delete entry tags-alist)))
  8772. (setq i (1- i)))
  8773. ;; add the next tags
  8774. (when tags
  8775. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8776. tags-alist
  8777. (cons (cons level tags) tags-alist)))
  8778. ;; compile tags for current headline
  8779. (setq tags-list
  8780. (if org-use-tag-inheritance
  8781. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8782. tags))
  8783. (when org-use-tag-inheritance
  8784. (setcdr (car tags-alist)
  8785. (mapcar (lambda (x)
  8786. (setq x (copy-sequence x))
  8787. (org-add-prop-inherited x))
  8788. (cdar tags-alist))))
  8789. (when (and tags org-use-tag-inheritance
  8790. (not (eq t org-use-tag-inheritance)))
  8791. ;; selective inheritance, remove uninherited ones
  8792. (setcdr (car tags-alist)
  8793. (org-remove-uniherited-tags (cdar tags-alist))))
  8794. (when (and (or (not todo-only)
  8795. (and (member todo org-not-done-keywords)
  8796. (or (not org-agenda-tags-todo-honor-ignore-options)
  8797. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8798. (let ((case-fold-search t)) (eval matcher))
  8799. (or
  8800. (not (member org-archive-tag tags-list))
  8801. ;; we have an archive tag, should we use this anyway?
  8802. (or (not org-agenda-skip-archived-trees)
  8803. (and (eq action 'agenda) org-agenda-archives-mode))))
  8804. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8805. ;; select this headline
  8806. (cond
  8807. ((eq action 'sparse-tree)
  8808. (and org-highlight-sparse-tree-matches
  8809. (org-get-heading) (match-end 0)
  8810. (org-highlight-new-match
  8811. (match-beginning 0) (match-beginning 1)))
  8812. (org-show-context 'tags-tree))
  8813. ((eq action 'agenda)
  8814. (setq txt (org-format-agenda-item
  8815. ""
  8816. (concat
  8817. (if org-tags-match-list-sublevels
  8818. (make-string (1- level) ?.) "")
  8819. (org-get-heading))
  8820. category (org-get-tags-at))
  8821. priority (org-get-priority txt))
  8822. (goto-char lspos)
  8823. (setq marker (org-agenda-new-marker))
  8824. (org-add-props txt props
  8825. 'org-marker marker 'org-hd-marker marker 'org-category category
  8826. 'priority priority 'type "tagsmatch")
  8827. (push txt rtn))
  8828. ((functionp action)
  8829. (save-excursion
  8830. (setq rtn1 (funcall action))
  8831. (push rtn1 rtn))
  8832. (goto-char (point-at-eol)))
  8833. (t (error "Invalid action")))
  8834. ;; if we are to skip sublevels, jump to end of subtree
  8835. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8836. (when (and (eq action 'sparse-tree)
  8837. (not org-sparse-tree-open-archived-trees))
  8838. (org-hide-archived-subtrees (point-min) (point-max)))
  8839. (nreverse rtn)))
  8840. (defun org-remove-uniherited-tags (tags)
  8841. "Remove all tags that are not inherited from the list TAGS."
  8842. (cond
  8843. ((eq org-use-tag-inheritance t)
  8844. (if org-tags-exclude-from-inheritance
  8845. (org-delete-all org-tags-exclude-from-inheritance tags)
  8846. tags))
  8847. ((not org-use-tag-inheritance) nil)
  8848. ((stringp org-use-tag-inheritance)
  8849. (delq nil (mapcar
  8850. (lambda (x)
  8851. (if (and (string-match org-use-tag-inheritance x)
  8852. (not (member x org-tags-exclude-from-inheritance)))
  8853. x nil))
  8854. tags)))
  8855. ((listp org-use-tag-inheritance)
  8856. (delq nil (mapcar
  8857. (lambda (x)
  8858. (if (member x org-use-tag-inheritance) x nil))
  8859. tags)))))
  8860. (defvar todo-only) ;; dynamically scoped
  8861. (defun org-tags-sparse-tree (&optional todo-only match)
  8862. "Create a sparse tree according to tags string MATCH.
  8863. MATCH can contain positive and negative selection of tags, like
  8864. \"+WORK+URGENT-WITHBOSS\".
  8865. If optional argument TODO-ONLY is non-nil, only select lines that are
  8866. also TODO lines."
  8867. (interactive "P")
  8868. (org-prepare-agenda-buffers (list (current-buffer)))
  8869. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8870. (defvar org-cached-props nil)
  8871. (defun org-cached-entry-get (pom property)
  8872. (if (or (eq t org-use-property-inheritance)
  8873. (and (stringp org-use-property-inheritance)
  8874. (string-match org-use-property-inheritance property))
  8875. (and (listp org-use-property-inheritance)
  8876. (member property org-use-property-inheritance)))
  8877. ;; Caching is not possible, check it directly
  8878. (org-entry-get pom property 'inherit)
  8879. ;; Get all properties, so that we can do complicated checks easily
  8880. (cdr (assoc property (or org-cached-props
  8881. (setq org-cached-props
  8882. (org-entry-properties pom)))))))
  8883. (defun org-global-tags-completion-table (&optional files)
  8884. "Return the list of all tags in all agenda buffer/files."
  8885. (save-excursion
  8886. (org-uniquify
  8887. (delq nil
  8888. (apply 'append
  8889. (mapcar
  8890. (lambda (file)
  8891. (set-buffer (find-file-noselect file))
  8892. (append (org-get-buffer-tags)
  8893. (mapcar (lambda (x) (if (stringp (car-safe x))
  8894. (list (car-safe x)) nil))
  8895. org-tag-alist)))
  8896. (if (and files (car files))
  8897. files
  8898. (org-agenda-files))))))))
  8899. (defun org-make-tags-matcher (match)
  8900. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8901. ;; todo-only is scoped dynamically into this function, and the function
  8902. ;; may change it if the matcher asks for it.
  8903. (unless match
  8904. ;; Get a new match request, with completion
  8905. (let ((org-last-tags-completion-table
  8906. (org-global-tags-completion-table)))
  8907. (setq match (org-completing-read
  8908. "Match: " 'org-tags-completion-function nil nil nil
  8909. 'org-tags-history))))
  8910. ;; Parse the string and create a lisp form
  8911. (let ((match0 match)
  8912. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8913. minus tag mm
  8914. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8915. orterms term orlist re-p str-p level-p level-op time-p
  8916. prop-p pn pv po cat-p gv rest)
  8917. (if (string-match "/+" match)
  8918. ;; match contains also a todo-matching request
  8919. (progn
  8920. (setq tagsmatch (substring match 0 (match-beginning 0))
  8921. todomatch (substring match (match-end 0)))
  8922. (if (string-match "^!" todomatch)
  8923. (setq todo-only t todomatch (substring todomatch 1)))
  8924. (if (string-match "^\\s-*$" todomatch)
  8925. (setq todomatch nil)))
  8926. ;; only matching tags
  8927. (setq tagsmatch match todomatch nil))
  8928. ;; Make the tags matcher
  8929. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8930. (setq tagsmatcher t)
  8931. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8932. (while (setq term (pop orterms))
  8933. (while (and (equal (substring term -1) "\\") orterms)
  8934. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8935. (while (string-match re term)
  8936. (setq rest (substring term (match-end 0))
  8937. minus (and (match-end 1)
  8938. (equal (match-string 1 term) "-"))
  8939. tag (match-string 2 term)
  8940. re-p (equal (string-to-char tag) ?{)
  8941. level-p (match-end 4)
  8942. prop-p (match-end 5)
  8943. mm (cond
  8944. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8945. (level-p
  8946. (setq level-op (org-op-to-function (match-string 3 term)))
  8947. `(,level-op level ,(string-to-number
  8948. (match-string 4 term))))
  8949. (prop-p
  8950. (setq pn (match-string 5 term)
  8951. po (match-string 6 term)
  8952. pv (match-string 7 term)
  8953. cat-p (equal pn "CATEGORY")
  8954. re-p (equal (string-to-char pv) ?{)
  8955. str-p (equal (string-to-char pv) ?\")
  8956. time-p (save-match-data
  8957. (string-match "^\"[[<].*[]>]\"$" pv))
  8958. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8959. (if time-p (setq pv (org-matcher-time pv)))
  8960. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8961. (cond
  8962. ((equal pn "CATEGORY")
  8963. (setq gv '(get-text-property (point) 'org-category)))
  8964. ((equal pn "TODO")
  8965. (setq gv 'todo))
  8966. (t
  8967. (setq gv `(org-cached-entry-get nil ,pn))))
  8968. (if re-p
  8969. (if (eq po 'org<>)
  8970. `(not (string-match ,pv (or ,gv "")))
  8971. `(string-match ,pv (or ,gv "")))
  8972. (if str-p
  8973. `(,po (or ,gv "") ,pv)
  8974. `(,po (string-to-number (or ,gv ""))
  8975. ,(string-to-number pv) ))))
  8976. (t `(member ,(downcase tag) tags-list)))
  8977. mm (if minus (list 'not mm) mm)
  8978. term rest)
  8979. (push mm tagsmatcher))
  8980. (push (if (> (length tagsmatcher) 1)
  8981. (cons 'and tagsmatcher)
  8982. (car tagsmatcher))
  8983. orlist)
  8984. (setq tagsmatcher nil))
  8985. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8986. (setq tagsmatcher
  8987. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8988. ;; Make the todo matcher
  8989. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8990. (setq todomatcher t)
  8991. (setq orterms (org-split-string todomatch "|") orlist nil)
  8992. (while (setq term (pop orterms))
  8993. (while (string-match re term)
  8994. (setq minus (and (match-end 1)
  8995. (equal (match-string 1 term) "-"))
  8996. kwd (match-string 2 term)
  8997. re-p (equal (string-to-char kwd) ?{)
  8998. term (substring term (match-end 0))
  8999. mm (if re-p
  9000. `(string-match ,(substring kwd 1 -1) todo)
  9001. (list 'equal 'todo kwd))
  9002. mm (if minus (list 'not mm) mm))
  9003. (push mm todomatcher))
  9004. (push (if (> (length todomatcher) 1)
  9005. (cons 'and todomatcher)
  9006. (car todomatcher))
  9007. orlist)
  9008. (setq todomatcher nil))
  9009. (setq todomatcher (if (> (length orlist) 1)
  9010. (cons 'or orlist) (car orlist))))
  9011. ;; Return the string and lisp forms of the matcher
  9012. (setq matcher (if todomatcher
  9013. (list 'and tagsmatcher todomatcher)
  9014. tagsmatcher))
  9015. (cons match0 matcher)))
  9016. (defun org-op-to-function (op &optional stringp)
  9017. "Turn an operator into the appropriate function."
  9018. (setq op
  9019. (cond
  9020. ((equal op "<" ) '(< string< org-time<))
  9021. ((equal op ">" ) '(> org-string> org-time>))
  9022. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9023. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9024. ((member op '("=" "==")) '(= string= org-time=))
  9025. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9026. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9027. (defun org<> (a b) (not (= a b)))
  9028. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9029. (defun org-string>= (a b) (not (string< a b)))
  9030. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9031. (defun org-string<> (a b) (not (string= a b)))
  9032. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9033. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9034. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9035. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9036. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9037. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9038. (defun org-2ft (s)
  9039. "Convert S to a floating point time.
  9040. If S is already a number, just return it. If it is a string, parse
  9041. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9042. (cond
  9043. ((numberp s) s)
  9044. ((stringp s)
  9045. (condition-case nil
  9046. (float-time (apply 'encode-time (org-parse-time-string s)))
  9047. (error 0.)))
  9048. (t 0.)))
  9049. (defun org-time-today ()
  9050. "Time in seconds today at 0:00.
  9051. Returns the float number of seconds since the beginning of the
  9052. epoch to the beginning of today (00:00)."
  9053. (float-time (apply 'encode-time
  9054. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9055. (defun org-matcher-time (s)
  9056. "Interpret a time comparison value."
  9057. (save-match-data
  9058. (cond
  9059. ((string= s "<now>") (float-time))
  9060. ((string= s "<today>") (org-time-today))
  9061. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9062. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9063. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9064. (+ (org-time-today)
  9065. (* (string-to-number (match-string 1 s))
  9066. (cdr (assoc (match-string 2 s)
  9067. '(("d" . 86400.0) ("w" . 604800.0)
  9068. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9069. (t (org-2ft s)))))
  9070. (defun org-match-any-p (re list)
  9071. "Does re match any element of list?"
  9072. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9073. (delq nil list))
  9074. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9075. (defvar org-tags-overlay (org-make-overlay 1 1))
  9076. (org-detach-overlay org-tags-overlay)
  9077. (defun org-get-local-tags-at (&optional pos)
  9078. "Get a list of tags defined in the current headline."
  9079. (org-get-tags-at pos 'local))
  9080. (defun org-get-local-tags ()
  9081. "Get a list of tags defined in the current headline."
  9082. (org-get-tags-at nil 'local))
  9083. (defun org-get-tags-at (&optional pos local)
  9084. "Get a list of all headline tags applicable at POS.
  9085. POS defaults to point. If tags are inherited, the list contains
  9086. the targets in the same sequence as the headlines appear, i.e.
  9087. the tags of the current headline come last.
  9088. When LOCAL is non-nil, only return tags from the current headline,
  9089. ignore inherited ones."
  9090. (interactive)
  9091. (let (tags ltags lastpos parent)
  9092. (save-excursion
  9093. (save-restriction
  9094. (widen)
  9095. (goto-char (or pos (point)))
  9096. (save-match-data
  9097. (catch 'done
  9098. (condition-case nil
  9099. (progn
  9100. (org-back-to-heading t)
  9101. (while (not (equal lastpos (point)))
  9102. (setq lastpos (point))
  9103. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9104. (setq ltags (org-split-string
  9105. (org-match-string-no-properties 1) ":"))
  9106. (when parent
  9107. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9108. (setq tags (append
  9109. (if parent
  9110. (org-remove-uniherited-tags ltags)
  9111. ltags)
  9112. tags)))
  9113. (or org-use-tag-inheritance (throw 'done t))
  9114. (if local (throw 'done t))
  9115. (org-up-heading-all 1)
  9116. (setq parent t)))
  9117. (error nil)))))
  9118. (append (org-remove-uniherited-tags org-file-tags) tags))))
  9119. (defun org-add-prop-inherited (s)
  9120. (add-text-properties 0 (length s) '(inherited t) s)
  9121. s)
  9122. (defun org-toggle-tag (tag &optional onoff)
  9123. "Toggle the tag TAG for the current line.
  9124. If ONOFF is `on' or `off', don't toggle but set to this state."
  9125. (let (res current)
  9126. (save-excursion
  9127. (org-back-to-heading t)
  9128. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9129. (point-at-eol) t)
  9130. (progn
  9131. (setq current (match-string 1))
  9132. (replace-match ""))
  9133. (setq current ""))
  9134. (setq current (nreverse (org-split-string current ":")))
  9135. (cond
  9136. ((eq onoff 'on)
  9137. (setq res t)
  9138. (or (member tag current) (push tag current)))
  9139. ((eq onoff 'off)
  9140. (or (not (member tag current)) (setq current (delete tag current))))
  9141. (t (if (member tag current)
  9142. (setq current (delete tag current))
  9143. (setq res t)
  9144. (push tag current))))
  9145. (end-of-line 1)
  9146. (if current
  9147. (progn
  9148. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9149. (org-set-tags nil t))
  9150. (delete-horizontal-space))
  9151. (run-hooks 'org-after-tags-change-hook))
  9152. res))
  9153. (defun org-align-tags-here (to-col)
  9154. ;; Assumes that this is a headline
  9155. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9156. (beginning-of-line 1)
  9157. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9158. (< pos (match-beginning 2)))
  9159. (progn
  9160. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9161. (goto-char (match-beginning 1))
  9162. (insert " ")
  9163. (delete-region (point) (1+ (match-beginning 2)))
  9164. (setq ncol (max (1+ (current-column))
  9165. (1+ col)
  9166. (if (> to-col 0)
  9167. to-col
  9168. (- (abs to-col) tags-l))))
  9169. (setq p (point))
  9170. (insert (make-string (- ncol (current-column)) ?\ ))
  9171. (setq ncol (current-column))
  9172. (when indent-tabs-mode (tabify p (point-at-eol)))
  9173. (org-move-to-column (min ncol col) t))
  9174. (goto-char pos))))
  9175. (defun org-set-tags-command (&optional arg just-align)
  9176. "Call the set-tags command for the current entry."
  9177. (interactive "P")
  9178. (if (org-on-heading-p)
  9179. (org-set-tags arg just-align)
  9180. (save-excursion
  9181. (org-back-to-heading t)
  9182. (org-set-tags arg just-align))))
  9183. (defun org-set-tags (&optional arg just-align)
  9184. "Set the tags for the current headline.
  9185. With prefix ARG, realign all tags in headings in the current buffer."
  9186. (interactive "P")
  9187. (let* ((re (concat "^" outline-regexp))
  9188. (current (org-get-tags-string))
  9189. (col (current-column))
  9190. (org-setting-tags t)
  9191. table current-tags inherited-tags ; computed below when needed
  9192. tags p0 c0 c1 rpl)
  9193. (if arg
  9194. (save-excursion
  9195. (goto-char (point-min))
  9196. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9197. (while (re-search-forward re nil t)
  9198. (org-set-tags nil t)
  9199. (end-of-line 1)))
  9200. (message "All tags realigned to column %d" org-tags-column))
  9201. (if just-align
  9202. (setq tags current)
  9203. ;; Get a new set of tags from the user
  9204. (save-excursion
  9205. (setq table (or org-tag-alist (org-get-buffer-tags))
  9206. org-last-tags-completion-table table
  9207. current-tags (org-split-string current ":")
  9208. inherited-tags (nreverse
  9209. (nthcdr (length current-tags)
  9210. (nreverse (org-get-tags-at))))
  9211. tags
  9212. (if (or (eq t org-use-fast-tag-selection)
  9213. (and org-use-fast-tag-selection
  9214. (delq nil (mapcar 'cdr table))))
  9215. (org-fast-tag-selection
  9216. current-tags inherited-tags table
  9217. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9218. (let ((org-add-colon-after-tag-completion t))
  9219. (org-trim
  9220. (org-without-partial-completion
  9221. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9222. nil nil current 'org-tags-history)))))))
  9223. (while (string-match "[-+&]+" tags)
  9224. ;; No boolean logic, just a list
  9225. (setq tags (replace-match ":" t t tags))))
  9226. (if (string-match "\\`[\t ]*\\'" tags)
  9227. (setq tags "")
  9228. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9229. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9230. ;; Insert new tags at the correct column
  9231. (beginning-of-line 1)
  9232. (cond
  9233. ((and (equal current "") (equal tags "")))
  9234. ((re-search-forward
  9235. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9236. (point-at-eol) t)
  9237. (if (equal tags "")
  9238. (setq rpl "")
  9239. (goto-char (match-beginning 0))
  9240. (setq c0 (current-column) p0 (point)
  9241. c1 (max (1+ c0) (if (> org-tags-column 0)
  9242. org-tags-column
  9243. (- (- org-tags-column) (length tags))))
  9244. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9245. (replace-match rpl t t)
  9246. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9247. tags)
  9248. (t (error "Tags alignment failed")))
  9249. (org-move-to-column col)
  9250. (unless just-align
  9251. (run-hooks 'org-after-tags-change-hook)))))
  9252. (defun org-change-tag-in-region (beg end tag off)
  9253. "Add or remove TAG for each entry in the region.
  9254. This works in the agenda, and also in an org-mode buffer."
  9255. (interactive
  9256. (list (region-beginning) (region-end)
  9257. (let ((org-last-tags-completion-table
  9258. (if (org-mode-p)
  9259. (org-get-buffer-tags)
  9260. (org-global-tags-completion-table))))
  9261. (org-ido-completing-read
  9262. "Tag: " 'org-tags-completion-function nil nil nil
  9263. 'org-tags-history))
  9264. (progn
  9265. (message "[s]et or [r]emove? ")
  9266. (equal (read-char-exclusive) ?r))))
  9267. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9268. (let ((agendap (equal major-mode 'org-agenda-mode))
  9269. l1 l2 m buf pos newhead (cnt 0))
  9270. (goto-char end)
  9271. (setq l2 (1- (org-current-line)))
  9272. (goto-char beg)
  9273. (setq l1 (org-current-line))
  9274. (loop for l from l1 to l2 do
  9275. (goto-line l)
  9276. (setq m (get-text-property (point) 'org-hd-marker))
  9277. (when (or (and (org-mode-p) (org-on-heading-p))
  9278. (and agendap m))
  9279. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9280. pos (if agendap m (point)))
  9281. (with-current-buffer buf
  9282. (save-excursion
  9283. (save-restriction
  9284. (goto-char pos)
  9285. (setq cnt (1+ cnt))
  9286. (org-toggle-tag tag (if off 'off 'on))
  9287. (setq newhead (org-get-heading)))))
  9288. (and agendap (org-agenda-change-all-lines newhead m))))
  9289. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9290. (defun org-tags-completion-function (string predicate &optional flag)
  9291. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9292. (confirm (lambda (x) (stringp (car x)))))
  9293. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9294. (setq s1 (match-string 1 string)
  9295. s2 (match-string 2 string))
  9296. (setq s1 "" s2 string))
  9297. (cond
  9298. ((eq flag nil)
  9299. ;; try completion
  9300. (setq rtn (try-completion s2 ctable confirm))
  9301. (if (stringp rtn)
  9302. (setq rtn
  9303. (concat s1 s2 (substring rtn (length s2))
  9304. (if (and org-add-colon-after-tag-completion
  9305. (assoc rtn ctable))
  9306. ":" ""))))
  9307. rtn)
  9308. ((eq flag t)
  9309. ;; all-completions
  9310. (all-completions s2 ctable confirm)
  9311. )
  9312. ((eq flag 'lambda)
  9313. ;; exact match?
  9314. (assoc s2 ctable)))
  9315. ))
  9316. (defun org-fast-tag-insert (kwd tags face &optional end)
  9317. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9318. (insert (format "%-12s" (concat kwd ":"))
  9319. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9320. (or end "")))
  9321. (defun org-fast-tag-show-exit (flag)
  9322. (save-excursion
  9323. (goto-line 3)
  9324. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9325. (replace-match ""))
  9326. (when flag
  9327. (end-of-line 1)
  9328. (org-move-to-column (- (window-width) 19) t)
  9329. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9330. (defun org-set-current-tags-overlay (current prefix)
  9331. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9332. (if (featurep 'xemacs)
  9333. (org-overlay-display org-tags-overlay (concat prefix s)
  9334. 'secondary-selection)
  9335. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9336. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9337. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9338. "Fast tag selection with single keys.
  9339. CURRENT is the current list of tags in the headline, INHERITED is the
  9340. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9341. possibly with grouping information. TODO-TABLE is a similar table with
  9342. TODO keywords, should these have keys assigned to them.
  9343. If the keys are nil, a-z are automatically assigned.
  9344. Returns the new tags string, or nil to not change the current settings."
  9345. (let* ((fulltable (append table todo-table))
  9346. (maxlen (apply 'max (mapcar
  9347. (lambda (x)
  9348. (if (stringp (car x)) (string-width (car x)) 0))
  9349. fulltable)))
  9350. (buf (current-buffer))
  9351. (expert (eq org-fast-tag-selection-single-key 'expert))
  9352. (buffer-tags nil)
  9353. (fwidth (+ maxlen 3 1 3))
  9354. (ncol (/ (- (window-width) 4) fwidth))
  9355. (i-face 'org-done)
  9356. (c-face 'org-todo)
  9357. tg cnt e c char c1 c2 ntable tbl rtn
  9358. ov-start ov-end ov-prefix
  9359. (exit-after-next org-fast-tag-selection-single-key)
  9360. (done-keywords org-done-keywords)
  9361. groups ingroup)
  9362. (save-excursion
  9363. (beginning-of-line 1)
  9364. (if (looking-at
  9365. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9366. (setq ov-start (match-beginning 1)
  9367. ov-end (match-end 1)
  9368. ov-prefix "")
  9369. (setq ov-start (1- (point-at-eol))
  9370. ov-end (1+ ov-start))
  9371. (skip-chars-forward "^\n\r")
  9372. (setq ov-prefix
  9373. (concat
  9374. (buffer-substring (1- (point)) (point))
  9375. (if (> (current-column) org-tags-column)
  9376. " "
  9377. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9378. (org-move-overlay org-tags-overlay ov-start ov-end)
  9379. (save-window-excursion
  9380. (if expert
  9381. (set-buffer (get-buffer-create " *Org tags*"))
  9382. (delete-other-windows)
  9383. (split-window-vertically)
  9384. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9385. (erase-buffer)
  9386. (org-set-local 'org-done-keywords done-keywords)
  9387. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9388. (org-fast-tag-insert "Current" current c-face "\n\n")
  9389. (org-fast-tag-show-exit exit-after-next)
  9390. (org-set-current-tags-overlay current ov-prefix)
  9391. (setq tbl fulltable char ?a cnt 0)
  9392. (while (setq e (pop tbl))
  9393. (cond
  9394. ((equal e '(:startgroup))
  9395. (push '() groups) (setq ingroup t)
  9396. (when (not (= cnt 0))
  9397. (setq cnt 0)
  9398. (insert "\n"))
  9399. (insert "{ "))
  9400. ((equal e '(:endgroup))
  9401. (setq ingroup nil cnt 0)
  9402. (insert "}\n"))
  9403. (t
  9404. (setq tg (car e) c2 nil)
  9405. (if (cdr e)
  9406. (setq c (cdr e))
  9407. ;; automatically assign a character.
  9408. (setq c1 (string-to-char
  9409. (downcase (substring
  9410. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9411. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9412. (while (or (rassoc char ntable) (rassoc char table))
  9413. (setq char (1+ char)))
  9414. (setq c2 c1))
  9415. (setq c (or c2 char)))
  9416. (if ingroup (push tg (car groups)))
  9417. (setq tg (org-add-props tg nil 'face
  9418. (cond
  9419. ((not (assoc tg table))
  9420. (org-get-todo-face tg))
  9421. ((member tg current) c-face)
  9422. ((member tg inherited) i-face)
  9423. (t nil))))
  9424. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9425. (insert "[" c "] " tg (make-string
  9426. (- fwidth 4 (length tg)) ?\ ))
  9427. (push (cons tg c) ntable)
  9428. (when (= (setq cnt (1+ cnt)) ncol)
  9429. (insert "\n")
  9430. (if ingroup (insert " "))
  9431. (setq cnt 0)))))
  9432. (setq ntable (nreverse ntable))
  9433. (insert "\n")
  9434. (goto-char (point-min))
  9435. (if (not expert) (org-fit-window-to-buffer))
  9436. (setq rtn
  9437. (catch 'exit
  9438. (while t
  9439. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9440. (if groups " [!] no groups" " [!]groups")
  9441. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9442. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9443. (cond
  9444. ((= c ?\r) (throw 'exit t))
  9445. ((= c ?!)
  9446. (setq groups (not groups))
  9447. (goto-char (point-min))
  9448. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9449. ((= c ?\C-c)
  9450. (if (not expert)
  9451. (org-fast-tag-show-exit
  9452. (setq exit-after-next (not exit-after-next)))
  9453. (setq expert nil)
  9454. (delete-other-windows)
  9455. (split-window-vertically)
  9456. (org-switch-to-buffer-other-window " *Org tags*")
  9457. (org-fit-window-to-buffer)))
  9458. ((or (= c ?\C-g)
  9459. (and (= c ?q) (not (rassoc c ntable))))
  9460. (org-detach-overlay org-tags-overlay)
  9461. (setq quit-flag t))
  9462. ((= c ?\ )
  9463. (setq current nil)
  9464. (if exit-after-next (setq exit-after-next 'now)))
  9465. ((= c ?\t)
  9466. (condition-case nil
  9467. (setq tg (org-ido-completing-read
  9468. "Tag: "
  9469. (or buffer-tags
  9470. (with-current-buffer buf
  9471. (org-get-buffer-tags)))))
  9472. (quit (setq tg "")))
  9473. (when (string-match "\\S-" tg)
  9474. (add-to-list 'buffer-tags (list tg))
  9475. (if (member tg current)
  9476. (setq current (delete tg current))
  9477. (push tg current)))
  9478. (if exit-after-next (setq exit-after-next 'now)))
  9479. ((setq e (rassoc c todo-table) tg (car e))
  9480. (with-current-buffer buf
  9481. (save-excursion (org-todo tg)))
  9482. (if exit-after-next (setq exit-after-next 'now)))
  9483. ((setq e (rassoc c ntable) tg (car e))
  9484. (if (member tg current)
  9485. (setq current (delete tg current))
  9486. (loop for g in groups do
  9487. (if (member tg g)
  9488. (mapc (lambda (x)
  9489. (setq current (delete x current)))
  9490. g)))
  9491. (push tg current))
  9492. (if exit-after-next (setq exit-after-next 'now))))
  9493. ;; Create a sorted list
  9494. (setq current
  9495. (sort current
  9496. (lambda (a b)
  9497. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9498. (if (eq exit-after-next 'now) (throw 'exit t))
  9499. (goto-char (point-min))
  9500. (beginning-of-line 2)
  9501. (delete-region (point) (point-at-eol))
  9502. (org-fast-tag-insert "Current" current c-face)
  9503. (org-set-current-tags-overlay current ov-prefix)
  9504. (while (re-search-forward
  9505. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9506. (setq tg (match-string 1))
  9507. (add-text-properties
  9508. (match-beginning 1) (match-end 1)
  9509. (list 'face
  9510. (cond
  9511. ((member tg current) c-face)
  9512. ((member tg inherited) i-face)
  9513. (t (get-text-property (match-beginning 1) 'face))))))
  9514. (goto-char (point-min)))))
  9515. (org-detach-overlay org-tags-overlay)
  9516. (if rtn
  9517. (mapconcat 'identity current ":")
  9518. nil))))
  9519. (defun org-get-tags-string ()
  9520. "Get the TAGS string in the current headline."
  9521. (unless (org-on-heading-p t)
  9522. (error "Not on a heading"))
  9523. (save-excursion
  9524. (beginning-of-line 1)
  9525. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9526. (org-match-string-no-properties 1)
  9527. "")))
  9528. (defun org-get-tags ()
  9529. "Get the list of tags specified in the current headline."
  9530. (org-split-string (org-get-tags-string) ":"))
  9531. (defun org-get-buffer-tags ()
  9532. "Get a table of all tags used in the buffer, for completion."
  9533. (let (tags)
  9534. (save-excursion
  9535. (goto-char (point-min))
  9536. (while (re-search-forward
  9537. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9538. (when (equal (char-after (point-at-bol 0)) ?*)
  9539. (mapc (lambda (x) (add-to-list 'tags x))
  9540. (org-split-string (org-match-string-no-properties 1) ":")))))
  9541. (mapcar 'list tags)))
  9542. ;;;; The mapping API
  9543. ;;;###autoload
  9544. (defun org-map-entries (func &optional match scope &rest skip)
  9545. "Call FUNC at each headline selected by MATCH in SCOPE.
  9546. FUNC is a function or a lisp form. The function will be called without
  9547. arguments, with the cursor positioned at the beginning of the headline.
  9548. The return values of all calls to the function will be collected and
  9549. returned as a list.
  9550. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9551. Only headlines that are matched by this query will be considered during
  9552. the iteration. When MATCH is nil or t, all headlines will be
  9553. visited by the iteration.
  9554. SCOPE determines the scope of this command. It can be any of:
  9555. nil The current buffer, respecting the restriction if any
  9556. tree The subtree started with the entry at point
  9557. file The current buffer, without restriction
  9558. file-with-archives
  9559. The current buffer, and any archives associated with it
  9560. agenda All agenda files
  9561. agenda-with-archives
  9562. All agenda files with any archive files associated with them
  9563. \(file1 file2 ...)
  9564. If this is a list, all files in the list will be scanned
  9565. The remaining args are treated as settings for the skipping facilities of
  9566. the scanner. The following items can be given here:
  9567. archive skip trees with the archive tag.
  9568. comment skip trees with the COMMENT keyword
  9569. function or Emacs Lisp form:
  9570. will be used as value for `org-agenda-skip-function', so whenever
  9571. the the function returns t, FUNC will not be called for that
  9572. entry and search will continue from the point where the
  9573. function leaves it."
  9574. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9575. (org-agenda-skip-archived-trees (memq 'archive skip))
  9576. (org-agenda-skip-comment-trees (memq 'comment skip))
  9577. (org-agenda-skip-function
  9578. (car (org-delete-all '(comment archive) skip)))
  9579. (org-tags-match-list-sublevels t)
  9580. matcher file res
  9581. org-todo-keywords-for-agenda
  9582. org-done-keywords-for-agenda
  9583. org-todo-keyword-alist-for-agenda
  9584. org-tag-alist-for-agenda)
  9585. (cond
  9586. ((eq match t) (setq matcher t))
  9587. ((eq match nil) (setq matcher t))
  9588. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9589. (save-excursion
  9590. (save-restriction
  9591. (when (eq scope 'tree)
  9592. (org-back-to-heading t)
  9593. (org-narrow-to-subtree)
  9594. (setq scope nil))
  9595. (if (not scope)
  9596. (progn
  9597. (org-prepare-agenda-buffers
  9598. (list (buffer-file-name (current-buffer))))
  9599. (setq res (org-scan-tags func matcher)))
  9600. ;; Get the right scope
  9601. (cond
  9602. ((and scope (listp scope) (symbolp (car scope)))
  9603. (setq scope (eval scope)))
  9604. ((eq scope 'agenda)
  9605. (setq scope (org-agenda-files t)))
  9606. ((eq scope 'agenda-with-archives)
  9607. (setq scope (org-agenda-files t))
  9608. (setq scope (org-add-archive-files scope)))
  9609. ((eq scope 'file)
  9610. (setq scope (list (buffer-file-name))))
  9611. ((eq scope 'file-with-archives)
  9612. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9613. (org-prepare-agenda-buffers scope)
  9614. (while (setq file (pop scope))
  9615. (with-current-buffer (org-find-base-buffer-visiting file)
  9616. (save-excursion
  9617. (save-restriction
  9618. (widen)
  9619. (goto-char (point-min))
  9620. (setq res (append res (org-scan-tags func matcher))))))))))
  9621. res))
  9622. ;;;; Properties
  9623. ;;; Setting and retrieving properties
  9624. (defconst org-special-properties
  9625. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9626. "TIMESTAMP" "TIMESTAMP_IA")
  9627. "The special properties valid in Org-mode.
  9628. These are properties that are not defined in the property drawer,
  9629. but in some other way.")
  9630. (defconst org-default-properties
  9631. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9632. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9633. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9634. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9635. "Some properties that are used by Org-mode for various purposes.
  9636. Being in this list makes sure that they are offered for completion.")
  9637. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9638. "Regular expression matching the first line of a property drawer.")
  9639. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9640. "Regular expression matching the first line of a property drawer.")
  9641. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9642. "Regular expression matching the first line of a property drawer.")
  9643. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9644. "Regular expression matching the first line of a property drawer.")
  9645. (defconst org-property-drawer-re
  9646. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9647. org-property-end-re "\\)\n?")
  9648. "Matches an entire property drawer.")
  9649. (defconst org-clock-drawer-re
  9650. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9651. org-property-end-re "\\)\n?")
  9652. "Matches an entire clock drawer.")
  9653. (defun org-property-action ()
  9654. "Do an action on properties."
  9655. (interactive)
  9656. (let (c)
  9657. (org-at-property-p)
  9658. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9659. (setq c (read-char-exclusive))
  9660. (cond
  9661. ((equal c ?s)
  9662. (call-interactively 'org-set-property))
  9663. ((equal c ?d)
  9664. (call-interactively 'org-delete-property))
  9665. ((equal c ?D)
  9666. (call-interactively 'org-delete-property-globally))
  9667. ((equal c ?c)
  9668. (call-interactively 'org-compute-property-at-point))
  9669. (t (error "No such property action %c" c)))))
  9670. (defun org-at-property-p ()
  9671. "Is the cursor in a property line?"
  9672. ;; FIXME: Does not check if we are actually in the drawer.
  9673. ;; FIXME: also returns true on any drawers.....
  9674. ;; This is used by C-c C-c for property action.
  9675. (save-excursion
  9676. (beginning-of-line 1)
  9677. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9678. (defun org-get-property-block (&optional beg end force)
  9679. "Return the (beg . end) range of the body of the property drawer.
  9680. BEG and END can be beginning and end of subtree, if not given
  9681. they will be found.
  9682. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9683. (catch 'exit
  9684. (save-excursion
  9685. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9686. (end (or end (progn (outline-next-heading) (point)))))
  9687. (goto-char beg)
  9688. (if (re-search-forward org-property-start-re end t)
  9689. (setq beg (1+ (match-end 0)))
  9690. (if force
  9691. (save-excursion
  9692. (org-insert-property-drawer)
  9693. (setq end (progn (outline-next-heading) (point))))
  9694. (throw 'exit nil))
  9695. (goto-char beg)
  9696. (if (re-search-forward org-property-start-re end t)
  9697. (setq beg (1+ (match-end 0)))))
  9698. (if (re-search-forward org-property-end-re end t)
  9699. (setq end (match-beginning 0))
  9700. (or force (throw 'exit nil))
  9701. (goto-char beg)
  9702. (setq end beg)
  9703. (org-indent-line-function)
  9704. (insert ":END:\n"))
  9705. (cons beg end)))))
  9706. (defun org-entry-properties (&optional pom which)
  9707. "Get all properties of the entry at point-or-marker POM.
  9708. This includes the TODO keyword, the tags, time strings for deadline,
  9709. scheduled, and clocking, and any additional properties defined in the
  9710. entry. The return value is an alist, keys may occur multiple times
  9711. if the property key was used several times.
  9712. POM may also be nil, in which case the current entry is used.
  9713. If WHICH is nil or `all', get all properties. If WHICH is
  9714. `special' or `standard', only get that subclass."
  9715. (setq which (or which 'all))
  9716. (org-with-point-at pom
  9717. (let ((clockstr (substring org-clock-string 0 -1))
  9718. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9719. beg end range props sum-props key value string clocksum)
  9720. (save-excursion
  9721. (when (condition-case nil
  9722. (and (org-mode-p) (org-back-to-heading t))
  9723. (error nil))
  9724. (setq beg (point))
  9725. (setq sum-props (get-text-property (point) 'org-summaries))
  9726. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9727. (outline-next-heading)
  9728. (setq end (point))
  9729. (when (memq which '(all special))
  9730. ;; Get the special properties, like TODO and tags
  9731. (goto-char beg)
  9732. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9733. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9734. (when (looking-at org-priority-regexp)
  9735. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9736. (when (and (setq value (org-get-tags-string))
  9737. (string-match "\\S-" value))
  9738. (push (cons "TAGS" value) props))
  9739. (when (setq value (org-get-tags-at))
  9740. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9741. props))
  9742. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9743. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9744. string (if (equal key clockstr)
  9745. (org-no-properties
  9746. (org-trim
  9747. (buffer-substring
  9748. (match-beginning 3) (goto-char (point-at-eol)))))
  9749. (substring (org-match-string-no-properties 3) 1 -1)))
  9750. (unless key
  9751. (if (= (char-after (match-beginning 3)) ?\[)
  9752. (setq key "TIMESTAMP_IA")
  9753. (setq key "TIMESTAMP")))
  9754. (when (or (equal key clockstr) (not (assoc key props)))
  9755. (push (cons key string) props)))
  9756. )
  9757. (when (memq which '(all standard))
  9758. ;; Get the standard properties, like :PORP: ...
  9759. (setq range (org-get-property-block beg end))
  9760. (when range
  9761. (goto-char (car range))
  9762. (while (re-search-forward
  9763. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9764. (cdr range) t)
  9765. (setq key (org-match-string-no-properties 1)
  9766. value (org-trim (or (org-match-string-no-properties 2) "")))
  9767. (unless (member key excluded)
  9768. (push (cons key (or value "")) props)))))
  9769. (if clocksum
  9770. (push (cons "CLOCKSUM"
  9771. (org-columns-number-to-string (/ (float clocksum) 60.)
  9772. 'add_times))
  9773. props))
  9774. (unless (assoc "CATEGORY" props)
  9775. (setq value (or (org-get-category)
  9776. (progn (org-refresh-category-properties)
  9777. (org-get-category))))
  9778. (push (cons "CATEGORY" value) props))
  9779. (append sum-props (nreverse props)))))))
  9780. (defun org-entry-get (pom property &optional inherit)
  9781. "Get value of PROPERTY for entry at point-or-marker POM.
  9782. If INHERIT is non-nil and the entry does not have the property,
  9783. then also check higher levels of the hierarchy.
  9784. If INHERIT is the symbol `selective', use inheritance only if the setting
  9785. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9786. If the property is present but empty, the return value is the empty string.
  9787. If the property is not present at all, nil is returned."
  9788. (org-with-point-at pom
  9789. (if (and inherit (if (eq inherit 'selective)
  9790. (org-property-inherit-p property)
  9791. t))
  9792. (org-entry-get-with-inheritance property)
  9793. (if (member property org-special-properties)
  9794. ;; We need a special property. Use brute force, get all properties.
  9795. (cdr (assoc property (org-entry-properties nil 'special)))
  9796. (let ((range (org-get-property-block)))
  9797. (if (and range
  9798. (goto-char (car range))
  9799. (re-search-forward
  9800. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9801. (cdr range) t))
  9802. ;; Found the property, return it.
  9803. (if (match-end 1)
  9804. (org-match-string-no-properties 1)
  9805. "")))))))
  9806. (defun org-property-or-variable-value (var &optional inherit)
  9807. "Check if there is a property fixing the value of VAR.
  9808. If yes, return this value. If not, return the current value of the variable."
  9809. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9810. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9811. (read prop)
  9812. (symbol-value var))))
  9813. (defun org-entry-delete (pom property)
  9814. "Delete the property PROPERTY from entry at point-or-marker POM."
  9815. (org-with-point-at pom
  9816. (if (member property org-special-properties)
  9817. nil ; cannot delete these properties.
  9818. (let ((range (org-get-property-block)))
  9819. (if (and range
  9820. (goto-char (car range))
  9821. (re-search-forward
  9822. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9823. (cdr range) t))
  9824. (progn
  9825. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9826. t)
  9827. nil)))))
  9828. ;; Multi-values properties are properties that contain multiple values
  9829. ;; These values are assumed to be single words, separated by whitespace.
  9830. (defun org-entry-add-to-multivalued-property (pom property value)
  9831. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9832. (let* ((old (org-entry-get pom property))
  9833. (values (and old (org-split-string old "[ \t]"))))
  9834. (setq value (org-entry-protect-space value))
  9835. (unless (member value values)
  9836. (setq values (cons value values))
  9837. (org-entry-put pom property
  9838. (mapconcat 'identity values " ")))))
  9839. (defun org-entry-remove-from-multivalued-property (pom property value)
  9840. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9841. (let* ((old (org-entry-get pom property))
  9842. (values (and old (org-split-string old "[ \t]"))))
  9843. (setq value (org-entry-protect-space value))
  9844. (when (member value values)
  9845. (setq values (delete value values))
  9846. (org-entry-put pom property
  9847. (mapconcat 'identity values " ")))))
  9848. (defun org-entry-member-in-multivalued-property (pom property value)
  9849. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9850. (let* ((old (org-entry-get pom property))
  9851. (values (and old (org-split-string old "[ \t]"))))
  9852. (setq value (org-entry-protect-space value))
  9853. (member value values)))
  9854. (defun org-entry-get-multivalued-property (pom property)
  9855. "Return a list of values in a multivalued property."
  9856. (let* ((value (org-entry-get pom property))
  9857. (values (and value (org-split-string value "[ \t]"))))
  9858. (mapcar 'org-entry-restore-space values)))
  9859. (defun org-entry-put-multivalued-property (pom property &rest values)
  9860. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9861. VALUES should be a list of strings. Spaces will be protected."
  9862. (org-entry-put pom property
  9863. (mapconcat 'org-entry-protect-space values " "))
  9864. (let* ((value (org-entry-get pom property))
  9865. (values (and value (org-split-string value "[ \t]"))))
  9866. (mapcar 'org-entry-restore-space values)))
  9867. (defun org-entry-protect-space (s)
  9868. "Protect spaces and newline in string S."
  9869. (while (string-match " " s)
  9870. (setq s (replace-match "%20" t t s)))
  9871. (while (string-match "\n" s)
  9872. (setq s (replace-match "%0A" t t s)))
  9873. s)
  9874. (defun org-entry-restore-space (s)
  9875. "Restore spaces and newline in string S."
  9876. (while (string-match "%20" s)
  9877. (setq s (replace-match " " t t s)))
  9878. (while (string-match "%0A" s)
  9879. (setq s (replace-match "\n" t t s)))
  9880. s)
  9881. (defvar org-entry-property-inherited-from (make-marker)
  9882. "Marker pointing to the entry from where a property was inherited.
  9883. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9884. location of the entry where the inheritance search matched. If there was
  9885. no match, the marker will point nowhere.
  9886. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9887. is set.")
  9888. (defun org-entry-get-with-inheritance (property)
  9889. "Get entry property, and search higher levels if not present."
  9890. (move-marker org-entry-property-inherited-from nil)
  9891. (let (tmp)
  9892. (save-excursion
  9893. (save-restriction
  9894. (widen)
  9895. (catch 'ex
  9896. (while t
  9897. (when (setq tmp (org-entry-get nil property))
  9898. (org-back-to-heading t)
  9899. (move-marker org-entry-property-inherited-from (point))
  9900. (throw 'ex tmp))
  9901. (or (org-up-heading-safe) (throw 'ex nil)))))
  9902. (or tmp
  9903. (cdr (assoc property org-file-properties))
  9904. (cdr (assoc property org-global-properties))
  9905. (cdr (assoc property org-global-properties-fixed))))))
  9906. (defun org-entry-put (pom property value)
  9907. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9908. (org-with-point-at pom
  9909. (org-back-to-heading t)
  9910. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9911. range)
  9912. (cond
  9913. ((equal property "TODO")
  9914. (when (and (stringp value) (string-match "\\S-" value)
  9915. (not (member value org-todo-keywords-1)))
  9916. (error "\"%s\" is not a valid TODO state" value))
  9917. (if (or (not value)
  9918. (not (string-match "\\S-" value)))
  9919. (setq value 'none))
  9920. (org-todo value)
  9921. (org-set-tags nil 'align))
  9922. ((equal property "PRIORITY")
  9923. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9924. (string-to-char value) ?\ ))
  9925. (org-set-tags nil 'align))
  9926. ((equal property "SCHEDULED")
  9927. (if (re-search-forward org-scheduled-time-regexp end t)
  9928. (cond
  9929. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9930. ((eq value 'later) (org-timestamp-change 1 'day))
  9931. (t (call-interactively 'org-schedule)))
  9932. (call-interactively 'org-schedule)))
  9933. ((equal property "DEADLINE")
  9934. (if (re-search-forward org-deadline-time-regexp end t)
  9935. (cond
  9936. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9937. ((eq value 'later) (org-timestamp-change 1 'day))
  9938. (t (call-interactively 'org-deadline)))
  9939. (call-interactively 'org-deadline)))
  9940. ((member property org-special-properties)
  9941. (error "The %s property can not yet be set with `org-entry-put'"
  9942. property))
  9943. (t ; a non-special property
  9944. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9945. (setq range (org-get-property-block beg end 'force))
  9946. (goto-char (car range))
  9947. (if (re-search-forward
  9948. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9949. (progn
  9950. (delete-region (match-beginning 1) (match-end 1))
  9951. (goto-char (match-beginning 1)))
  9952. (goto-char (cdr range))
  9953. (insert "\n")
  9954. (backward-char 1)
  9955. (org-indent-line-function)
  9956. (insert ":" property ":"))
  9957. (and value (insert " " value))
  9958. (org-indent-line-function)))))))
  9959. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9960. "Get all property keys in the current buffer.
  9961. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9962. like tags and TODO state.
  9963. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9964. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9965. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9966. formats in the current buffer."
  9967. (let (rtn range cfmt s p)
  9968. (save-excursion
  9969. (save-restriction
  9970. (widen)
  9971. (goto-char (point-min))
  9972. (while (re-search-forward org-property-start-re nil t)
  9973. (setq range (org-get-property-block))
  9974. (goto-char (car range))
  9975. (while (re-search-forward
  9976. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9977. (cdr range) t)
  9978. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9979. (outline-next-heading))))
  9980. (when include-specials
  9981. (setq rtn (append org-special-properties rtn)))
  9982. (when include-defaults
  9983. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9984. (when include-columns
  9985. (save-excursion
  9986. (save-restriction
  9987. (widen)
  9988. (goto-char (point-min))
  9989. (while (re-search-forward
  9990. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9991. nil t)
  9992. (setq cfmt (match-string 2) s 0)
  9993. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9994. cfmt s)
  9995. (setq s (match-end 0)
  9996. p (match-string 1 cfmt))
  9997. (unless (or (equal p "ITEM")
  9998. (member p org-special-properties))
  9999. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10000. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10001. (defun org-property-values (key)
  10002. "Return a list of all values of property KEY."
  10003. (save-excursion
  10004. (save-restriction
  10005. (widen)
  10006. (goto-char (point-min))
  10007. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10008. values)
  10009. (while (re-search-forward re nil t)
  10010. (add-to-list 'values (org-trim (match-string 1))))
  10011. (delete "" values)))))
  10012. (defun org-insert-property-drawer ()
  10013. "Insert a property drawer into the current entry."
  10014. (interactive)
  10015. (org-back-to-heading t)
  10016. (looking-at outline-regexp)
  10017. (let ((indent (- (match-end 0)(match-beginning 0)))
  10018. (beg (point))
  10019. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10020. end hiddenp)
  10021. (outline-next-heading)
  10022. (setq end (point))
  10023. (goto-char beg)
  10024. (while (re-search-forward re end t))
  10025. (setq hiddenp (org-invisible-p))
  10026. (end-of-line 1)
  10027. (and (equal (char-after) ?\n) (forward-char 1))
  10028. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  10029. (beginning-of-line 2))
  10030. (org-skip-over-state-notes)
  10031. (skip-chars-backward " \t\n\r")
  10032. (if (eq (char-before) ?*) (forward-char 1))
  10033. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10034. (beginning-of-line 0)
  10035. (org-indent-to-column indent)
  10036. (beginning-of-line 2)
  10037. (org-indent-to-column indent)
  10038. (beginning-of-line 0)
  10039. (if hiddenp
  10040. (save-excursion
  10041. (org-back-to-heading t)
  10042. (hide-entry))
  10043. (org-flag-drawer t))))
  10044. (defun org-set-property (property value)
  10045. "In the current entry, set PROPERTY to VALUE.
  10046. When called interactively, this will prompt for a property name, offering
  10047. completion on existing and default properties. And then it will prompt
  10048. for a value, offering completion either on allowed values (via an inherited
  10049. xxx_ALL property) or on existing values in other instances of this property
  10050. in the current file."
  10051. (interactive
  10052. (let* ((completion-ignore-case t)
  10053. (keys (org-buffer-property-keys nil t t))
  10054. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10055. (prop (if (member prop0 keys)
  10056. prop0
  10057. (or (cdr (assoc (downcase prop0)
  10058. (mapcar (lambda (x) (cons (downcase x) x))
  10059. keys)))
  10060. prop0)))
  10061. (cur (org-entry-get nil prop))
  10062. (allowed (org-property-get-allowed-values nil prop 'table))
  10063. (existing (mapcar 'list (org-property-values prop)))
  10064. (val (if allowed
  10065. (org-completing-read "Value: " allowed nil 'req-match)
  10066. (org-completing-read
  10067. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10068. (concat "[" cur "]") "")
  10069. ": ")
  10070. existing nil nil "" nil cur))))
  10071. (list prop (if (equal val "") cur val))))
  10072. (unless (equal (org-entry-get nil property) value)
  10073. (org-entry-put nil property value)))
  10074. (defun org-delete-property (property)
  10075. "In the current entry, delete PROPERTY."
  10076. (interactive
  10077. (let* ((completion-ignore-case t)
  10078. (prop (org-ido-completing-read
  10079. "Property: " (org-entry-properties nil 'standard))))
  10080. (list prop)))
  10081. (message "Property %s %s" property
  10082. (if (org-entry-delete nil property)
  10083. "deleted"
  10084. "was not present in the entry")))
  10085. (defun org-delete-property-globally (property)
  10086. "Remove PROPERTY globally, from all entries."
  10087. (interactive
  10088. (let* ((completion-ignore-case t)
  10089. (prop (org-ido-completing-read
  10090. "Globally remove property: "
  10091. (mapcar 'list (org-buffer-property-keys)))))
  10092. (list prop)))
  10093. (save-excursion
  10094. (save-restriction
  10095. (widen)
  10096. (goto-char (point-min))
  10097. (let ((cnt 0))
  10098. (while (re-search-forward
  10099. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10100. nil t)
  10101. (setq cnt (1+ cnt))
  10102. (replace-match ""))
  10103. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10104. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10105. (defun org-compute-property-at-point ()
  10106. "Compute the property at point.
  10107. This looks for an enclosing column format, extracts the operator and
  10108. then applies it to the property in the column format's scope."
  10109. (interactive)
  10110. (unless (org-at-property-p)
  10111. (error "Not at a property"))
  10112. (let ((prop (org-match-string-no-properties 2)))
  10113. (org-columns-get-format-and-top-level)
  10114. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10115. (error "No operator defined for property %s" prop))
  10116. (org-columns-compute prop)))
  10117. (defun org-property-get-allowed-values (pom property &optional table)
  10118. "Get allowed values for the property PROPERTY.
  10119. When TABLE is non-nil, return an alist that can directly be used for
  10120. completion."
  10121. (let (vals)
  10122. (cond
  10123. ((equal property "TODO")
  10124. (setq vals (org-with-point-at pom
  10125. (append org-todo-keywords-1 '("")))))
  10126. ((equal property "PRIORITY")
  10127. (let ((n org-lowest-priority))
  10128. (while (>= n org-highest-priority)
  10129. (push (char-to-string n) vals)
  10130. (setq n (1- n)))))
  10131. ((member property org-special-properties))
  10132. (t
  10133. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10134. (when (and vals (string-match "\\S-" vals))
  10135. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10136. (setq vals (mapcar (lambda (x)
  10137. (cond ((stringp x) x)
  10138. ((numberp x) (number-to-string x))
  10139. ((symbolp x) (symbol-name x))
  10140. (t "???")))
  10141. vals)))))
  10142. (if table (mapcar 'list vals) vals)))
  10143. (defun org-property-previous-allowed-value (&optional previous)
  10144. "Switch to the next allowed value for this property."
  10145. (interactive)
  10146. (org-property-next-allowed-value t))
  10147. (defun org-property-next-allowed-value (&optional previous)
  10148. "Switch to the next allowed value for this property."
  10149. (interactive)
  10150. (unless (org-at-property-p)
  10151. (error "Not at a property"))
  10152. (let* ((key (match-string 2))
  10153. (value (match-string 3))
  10154. (allowed (or (org-property-get-allowed-values (point) key)
  10155. (and (member value '("[ ]" "[-]" "[X]"))
  10156. '("[ ]" "[X]"))))
  10157. nval)
  10158. (unless allowed
  10159. (error "Allowed values for this property have not been defined"))
  10160. (if previous (setq allowed (reverse allowed)))
  10161. (if (member value allowed)
  10162. (setq nval (car (cdr (member value allowed)))))
  10163. (setq nval (or nval (car allowed)))
  10164. (if (equal nval value)
  10165. (error "Only one allowed value for this property"))
  10166. (org-at-property-p)
  10167. (replace-match (concat " :" key ": " nval) t t)
  10168. (org-indent-line-function)
  10169. (beginning-of-line 1)
  10170. (skip-chars-forward " \t")))
  10171. (defun org-find-entry-with-id (ident)
  10172. "Locate the entry that contains the ID property with exact value IDENT.
  10173. IDENT can be a string, a symbol or a number, this function will search for
  10174. the string representation of it.
  10175. Return the position where this entry starts, or nil if there is no such entry."
  10176. (interactive "sID: ")
  10177. (let ((id (cond
  10178. ((stringp ident) ident)
  10179. ((symbol-name ident) (symbol-name ident))
  10180. ((numberp ident) (number-to-string ident))
  10181. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10182. (case-fold-search nil))
  10183. (save-excursion
  10184. (save-restriction
  10185. (widen)
  10186. (goto-char (point-min))
  10187. (when (re-search-forward
  10188. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10189. nil t)
  10190. (org-back-to-heading)
  10191. (point))))))
  10192. ;;;; Timestamps
  10193. (defvar org-last-changed-timestamp nil)
  10194. (defvar org-last-inserted-timestamp nil
  10195. "The last time stamp inserted with `org-insert-time-stamp'.")
  10196. (defvar org-time-was-given) ; dynamically scoped parameter
  10197. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10198. (defvar org-ts-what) ; dynamically scoped parameter
  10199. (defun org-time-stamp (arg &optional inactive)
  10200. "Prompt for a date/time and insert a time stamp.
  10201. If the user specifies a time like HH:MM, or if this command is called
  10202. with a prefix argument, the time stamp will contain date and time.
  10203. Otherwise, only the date will be included. All parts of a date not
  10204. specified by the user will be filled in from the current date/time.
  10205. So if you press just return without typing anything, the time stamp
  10206. will represent the current date/time. If there is already a timestamp
  10207. at the cursor, it will be modified."
  10208. (interactive "P")
  10209. (let* ((ts nil)
  10210. (default-time
  10211. ;; Default time is either today, or, when entering a range,
  10212. ;; the range start.
  10213. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10214. (save-excursion
  10215. (re-search-backward
  10216. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10217. (- (point) 20) t)))
  10218. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10219. (current-time)))
  10220. (default-input (and ts (org-get-compact-tod ts)))
  10221. org-time-was-given org-end-time-was-given time)
  10222. (cond
  10223. ((and (org-at-timestamp-p t)
  10224. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10225. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10226. (insert "--")
  10227. (setq time (let ((this-command this-command))
  10228. (org-read-date arg 'totime nil nil
  10229. default-time default-input)))
  10230. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10231. ((org-at-timestamp-p t)
  10232. (setq time (let ((this-command this-command))
  10233. (org-read-date arg 'totime nil nil default-time default-input)))
  10234. (when (org-at-timestamp-p t) ; just to get the match data
  10235. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10236. (replace-match "")
  10237. (setq org-last-changed-timestamp
  10238. (org-insert-time-stamp
  10239. time (or org-time-was-given arg)
  10240. inactive nil nil (list org-end-time-was-given))))
  10241. (message "Timestamp updated"))
  10242. (t
  10243. (setq time (let ((this-command this-command))
  10244. (org-read-date arg 'totime nil nil default-time default-input)))
  10245. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10246. nil nil (list org-end-time-was-given))))))
  10247. ;; FIXME: can we use this for something else, like computing time differences?
  10248. (defun org-get-compact-tod (s)
  10249. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10250. (let* ((t1 (match-string 1 s))
  10251. (h1 (string-to-number (match-string 2 s)))
  10252. (m1 (string-to-number (match-string 3 s)))
  10253. (t2 (and (match-end 4) (match-string 5 s)))
  10254. (h2 (and t2 (string-to-number (match-string 6 s))))
  10255. (m2 (and t2 (string-to-number (match-string 7 s))))
  10256. dh dm)
  10257. (if (not t2)
  10258. t1
  10259. (setq dh (- h2 h1) dm (- m2 m1))
  10260. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10261. (concat t1 "+" (number-to-string dh)
  10262. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10263. (defun org-time-stamp-inactive (&optional arg)
  10264. "Insert an inactive time stamp.
  10265. An inactive time stamp is enclosed in square brackets instead of angle
  10266. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10267. does not link to the calendar and cannot be changed with the S-cursor keys.
  10268. So these are more for recording a certain time/date."
  10269. (interactive "P")
  10270. (org-time-stamp arg 'inactive))
  10271. (defvar org-date-ovl (org-make-overlay 1 1))
  10272. (org-overlay-put org-date-ovl 'face 'org-warning)
  10273. (org-detach-overlay org-date-ovl)
  10274. (defvar org-ans1) ; dynamically scoped parameter
  10275. (defvar org-ans2) ; dynamically scoped parameter
  10276. (defvar org-plain-time-of-day-regexp) ; defined below
  10277. (defvar org-overriding-default-time nil) ; dynamically scoped
  10278. (defvar org-read-date-overlay nil)
  10279. (defvar org-dcst nil) ; dynamically scoped
  10280. (defun org-read-date (&optional with-time to-time from-string prompt
  10281. default-time default-input)
  10282. "Read a date, possibly a time, and make things smooth for the user.
  10283. The prompt will suggest to enter an ISO date, but you can also enter anything
  10284. which will at least partially be understood by `parse-time-string'.
  10285. Unrecognized parts of the date will default to the current day, month, year,
  10286. hour and minute. If this command is called to replace a timestamp at point,
  10287. of to enter the second timestamp of a range, the default time is taken from the
  10288. existing stamp. For example,
  10289. 3-2-5 --> 2003-02-05
  10290. feb 15 --> currentyear-02-15
  10291. sep 12 9 --> 2009-09-12
  10292. 12:45 --> today 12:45
  10293. 22 sept 0:34 --> currentyear-09-22 0:34
  10294. 12 --> currentyear-currentmonth-12
  10295. Fri --> nearest Friday (today or later)
  10296. etc.
  10297. Furthermore you can specify a relative date by giving, as the *first* thing
  10298. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10299. change in days weeks, months, years.
  10300. With a single plus or minus, the date is relative to today. With a double
  10301. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10302. +4d --> four days from today
  10303. +4 --> same as above
  10304. +2w --> two weeks from today
  10305. ++5 --> five days from default date
  10306. The function understands only English month and weekday abbreviations,
  10307. but this can be configured with the variables `parse-time-months' and
  10308. `parse-time-weekdays'.
  10309. While prompting, a calendar is popped up - you can also select the
  10310. date with the mouse (button 1). The calendar shows a period of three
  10311. months. To scroll it to other months, use the keys `>' and `<'.
  10312. If you don't like the calendar, turn it off with
  10313. \(setq org-read-date-popup-calendar nil)
  10314. With optional argument TO-TIME, the date will immediately be converted
  10315. to an internal time.
  10316. With an optional argument WITH-TIME, the prompt will suggest to also
  10317. insert a time. Note that when WITH-TIME is not set, you can still
  10318. enter a time, and this function will inform the calling routine about
  10319. this change. The calling routine may then choose to change the format
  10320. used to insert the time stamp into the buffer to include the time.
  10321. With optional argument FROM-STRING, read from this string instead from
  10322. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10323. the time/date that is used for everything that is not specified by the
  10324. user."
  10325. (require 'parse-time)
  10326. (let* ((org-time-stamp-rounding-minutes
  10327. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10328. (org-dcst org-display-custom-times)
  10329. (ct (org-current-time))
  10330. (def (or org-overriding-default-time default-time ct))
  10331. (defdecode (decode-time def))
  10332. (dummy (progn
  10333. (when (< (nth 2 defdecode) org-extend-today-until)
  10334. (setcar (nthcdr 2 defdecode) -1)
  10335. (setcar (nthcdr 1 defdecode) 59)
  10336. (setq def (apply 'encode-time defdecode)
  10337. defdecode (decode-time def)))))
  10338. (calendar-move-hook nil)
  10339. (calendar-view-diary-initially-flag nil)
  10340. (view-diary-entries-initially nil)
  10341. (calendar-view-holidays-initially-flag nil)
  10342. (view-calendar-holidays-initially nil)
  10343. (timestr (format-time-string
  10344. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10345. (prompt (concat (if prompt (concat prompt " ") "")
  10346. (format "Date+time [%s]: " timestr)))
  10347. ans (org-ans0 "") org-ans1 org-ans2 final)
  10348. (cond
  10349. (from-string (setq ans from-string))
  10350. (org-read-date-popup-calendar
  10351. (save-excursion
  10352. (save-window-excursion
  10353. (calendar)
  10354. (calendar-forward-day (- (time-to-days def)
  10355. (calendar-absolute-from-gregorian
  10356. (calendar-current-date))))
  10357. (org-eval-in-calendar nil t)
  10358. (let* ((old-map (current-local-map))
  10359. (map (copy-keymap calendar-mode-map))
  10360. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10361. (org-defkey map (kbd "RET") 'org-calendar-select)
  10362. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10363. 'org-calendar-select-mouse)
  10364. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10365. 'org-calendar-select-mouse)
  10366. (org-defkey minibuffer-local-map [(meta shift left)]
  10367. (lambda () (interactive)
  10368. (org-eval-in-calendar '(calendar-backward-month 1))))
  10369. (org-defkey minibuffer-local-map [(meta shift right)]
  10370. (lambda () (interactive)
  10371. (org-eval-in-calendar '(calendar-forward-month 1))))
  10372. (org-defkey minibuffer-local-map [(meta shift up)]
  10373. (lambda () (interactive)
  10374. (org-eval-in-calendar '(calendar-backward-year 1))))
  10375. (org-defkey minibuffer-local-map [(meta shift down)]
  10376. (lambda () (interactive)
  10377. (org-eval-in-calendar '(calendar-forward-year 1))))
  10378. (org-defkey minibuffer-local-map [(shift up)]
  10379. (lambda () (interactive)
  10380. (org-eval-in-calendar '(calendar-backward-week 1))))
  10381. (org-defkey minibuffer-local-map [(shift down)]
  10382. (lambda () (interactive)
  10383. (org-eval-in-calendar '(calendar-forward-week 1))))
  10384. (org-defkey minibuffer-local-map [(shift left)]
  10385. (lambda () (interactive)
  10386. (org-eval-in-calendar '(calendar-backward-day 1))))
  10387. (org-defkey minibuffer-local-map [(shift right)]
  10388. (lambda () (interactive)
  10389. (org-eval-in-calendar '(calendar-forward-day 1))))
  10390. (org-defkey minibuffer-local-map ">"
  10391. (lambda () (interactive)
  10392. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10393. (org-defkey minibuffer-local-map "<"
  10394. (lambda () (interactive)
  10395. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10396. (unwind-protect
  10397. (progn
  10398. (use-local-map map)
  10399. (add-hook 'post-command-hook 'org-read-date-display)
  10400. (setq org-ans0 (read-string prompt default-input nil nil))
  10401. ;; org-ans0: from prompt
  10402. ;; org-ans1: from mouse click
  10403. ;; org-ans2: from calendar motion
  10404. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10405. (remove-hook 'post-command-hook 'org-read-date-display)
  10406. (use-local-map old-map)
  10407. (when org-read-date-overlay
  10408. (org-delete-overlay org-read-date-overlay)
  10409. (setq org-read-date-overlay nil)))))))
  10410. (t ; Naked prompt only
  10411. (unwind-protect
  10412. (setq ans (read-string prompt default-input nil timestr))
  10413. (when org-read-date-overlay
  10414. (org-delete-overlay org-read-date-overlay)
  10415. (setq org-read-date-overlay nil)))))
  10416. (setq final (org-read-date-analyze ans def defdecode))
  10417. (if to-time
  10418. (apply 'encode-time final)
  10419. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10420. (format "%04d-%02d-%02d %02d:%02d"
  10421. (nth 5 final) (nth 4 final) (nth 3 final)
  10422. (nth 2 final) (nth 1 final))
  10423. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10424. (defvar def)
  10425. (defvar defdecode)
  10426. (defvar with-time)
  10427. (defun org-read-date-display ()
  10428. "Display the current date prompt interpretation in the minibuffer."
  10429. (when org-read-date-display-live
  10430. (when org-read-date-overlay
  10431. (org-delete-overlay org-read-date-overlay))
  10432. (let ((p (point)))
  10433. (end-of-line 1)
  10434. (while (not (equal (buffer-substring
  10435. (max (point-min) (- (point) 4)) (point))
  10436. " "))
  10437. (insert " "))
  10438. (goto-char p))
  10439. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10440. " " (or org-ans1 org-ans2)))
  10441. (org-end-time-was-given nil)
  10442. (f (org-read-date-analyze ans def defdecode))
  10443. (fmts (if org-dcst
  10444. org-time-stamp-custom-formats
  10445. org-time-stamp-formats))
  10446. (fmt (if (or with-time
  10447. (and (boundp 'org-time-was-given) org-time-was-given))
  10448. (cdr fmts)
  10449. (car fmts)))
  10450. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10451. (when (and org-end-time-was-given
  10452. (string-match org-plain-time-of-day-regexp txt))
  10453. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10454. org-end-time-was-given
  10455. (substring txt (match-end 0)))))
  10456. (setq org-read-date-overlay
  10457. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10458. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10459. (defun org-read-date-analyze (ans def defdecode)
  10460. "Analyse the combined answer of the date prompt."
  10461. ;; FIXME: cleanup and comment
  10462. (let (delta deltan deltaw deltadef year month day
  10463. hour minute second wday pm h2 m2 tl wday1
  10464. iso-year iso-weekday iso-week iso-year iso-date)
  10465. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10466. (setq ans "+0"))
  10467. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10468. (setq ans (replace-match "" t t ans)
  10469. deltan (car delta)
  10470. deltaw (nth 1 delta)
  10471. deltadef (nth 2 delta)))
  10472. ;; Check if there is an iso week date in there
  10473. ;; If yes, sore the info and postpone interpreting it until the rest
  10474. ;; of the parsing is done
  10475. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10476. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10477. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10478. iso-week (string-to-number (match-string 2 ans)))
  10479. (setq ans (replace-match "" t t ans)))
  10480. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10481. (when (string-match
  10482. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10483. (setq year (if (match-end 2)
  10484. (string-to-number (match-string 2 ans))
  10485. (string-to-number (format-time-string "%Y")))
  10486. month (string-to-number (match-string 3 ans))
  10487. day (string-to-number (match-string 4 ans)))
  10488. (if (< year 100) (setq year (+ 2000 year)))
  10489. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10490. t nil ans)))
  10491. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10492. ;; If there is a time with am/pm, and *no* time without it, we convert
  10493. ;; so that matching will be successful.
  10494. (loop for i from 1 to 2 do ; twice, for end time as well
  10495. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10496. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10497. (setq hour (string-to-number (match-string 1 ans))
  10498. minute (if (match-end 3)
  10499. (string-to-number (match-string 3 ans))
  10500. 0)
  10501. pm (equal ?p
  10502. (string-to-char (downcase (match-string 4 ans)))))
  10503. (if (and (= hour 12) (not pm))
  10504. (setq hour 0)
  10505. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10506. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10507. t t ans))))
  10508. ;; Check if a time range is given as a duration
  10509. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10510. (setq hour (string-to-number (match-string 1 ans))
  10511. h2 (+ hour (string-to-number (match-string 3 ans)))
  10512. minute (string-to-number (match-string 2 ans))
  10513. m2 (+ minute (if (match-end 5) (string-to-number
  10514. (match-string 5 ans))0)))
  10515. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10516. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10517. t t ans)))
  10518. ;; Check if there is a time range
  10519. (when (boundp 'org-end-time-was-given)
  10520. (setq org-time-was-given nil)
  10521. (when (and (string-match org-plain-time-of-day-regexp ans)
  10522. (match-end 8))
  10523. (setq org-end-time-was-given (match-string 8 ans))
  10524. (setq ans (concat (substring ans 0 (match-beginning 7))
  10525. (substring ans (match-end 7))))))
  10526. (setq tl (parse-time-string ans)
  10527. day (or (nth 3 tl) (nth 3 defdecode))
  10528. month (or (nth 4 tl)
  10529. (if (and org-read-date-prefer-future
  10530. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10531. (1+ (nth 4 defdecode))
  10532. (nth 4 defdecode)))
  10533. year (or (nth 5 tl)
  10534. (if (and org-read-date-prefer-future
  10535. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10536. (1+ (nth 5 defdecode))
  10537. (nth 5 defdecode)))
  10538. hour (or (nth 2 tl) (nth 2 defdecode))
  10539. minute (or (nth 1 tl) (nth 1 defdecode))
  10540. second (or (nth 0 tl) 0)
  10541. wday (nth 6 tl))
  10542. ;; Special date definitions below
  10543. (cond
  10544. (iso-week
  10545. ;; There was an iso week
  10546. (setq year (or iso-year year)
  10547. day (or iso-weekday wday 1)
  10548. wday nil ; to make sure that the trigger below does not match
  10549. iso-date (calendar-gregorian-from-absolute
  10550. (calendar-absolute-from-iso
  10551. (list iso-week day year))))
  10552. ; FIXME: Should we also push ISO weeks into the future?
  10553. ; (when (and org-read-date-prefer-future
  10554. ; (not iso-year)
  10555. ; (< (calendar-absolute-from-gregorian iso-date)
  10556. ; (time-to-days (current-time))))
  10557. ; (setq year (1+ year)
  10558. ; iso-date (calendar-gregorian-from-absolute
  10559. ; (calendar-absolute-from-iso
  10560. ; (list iso-week day year)))))
  10561. (setq month (car iso-date)
  10562. year (nth 2 iso-date)
  10563. day (nth 1 iso-date)))
  10564. (deltan
  10565. (unless deltadef
  10566. (let ((now (decode-time (current-time))))
  10567. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10568. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10569. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10570. ((equal deltaw "m") (setq month (+ month deltan)))
  10571. ((equal deltaw "y") (setq year (+ year deltan)))))
  10572. ((and wday (not (nth 3 tl)))
  10573. ;; Weekday was given, but no day, so pick that day in the week
  10574. ;; on or after the derived date.
  10575. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10576. (unless (equal wday wday1)
  10577. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10578. (if (and (boundp 'org-time-was-given)
  10579. (nth 2 tl))
  10580. (setq org-time-was-given t))
  10581. (if (< year 100) (setq year (+ 2000 year)))
  10582. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10583. (list second minute hour day month year)))
  10584. (defvar parse-time-weekdays)
  10585. (defun org-read-date-get-relative (s today default)
  10586. "Check string S for special relative date string.
  10587. TODAY and DEFAULT are internal times, for today and for a default.
  10588. Return shift list (N what def-flag)
  10589. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10590. N is the number of WHATs to shift.
  10591. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10592. the DEFAULT date rather than TODAY."
  10593. (when (and
  10594. (string-match
  10595. (concat
  10596. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10597. "\\([0-9]+\\)?"
  10598. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10599. "\\([ \t]\\|$\\)") s)
  10600. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10601. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10602. (string-to-char (substring (match-string 1 s) -1))
  10603. ?+))
  10604. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10605. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10606. (what (if (match-end 3) (match-string 3 s) "d"))
  10607. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10608. (date (if rel default today))
  10609. (wday (nth 6 (decode-time date)))
  10610. delta)
  10611. (if wday1
  10612. (progn
  10613. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10614. (if (= dir ?-) (setq delta (- delta 7)))
  10615. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10616. (list delta "d" rel))
  10617. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10618. (defun org-eval-in-calendar (form &optional keepdate)
  10619. "Eval FORM in the calendar window and return to current window.
  10620. Also, store the cursor date in variable org-ans2."
  10621. (let ((sw (selected-window)))
  10622. (select-window (get-buffer-window "*Calendar*"))
  10623. (eval form)
  10624. (when (and (not keepdate) (calendar-cursor-to-date))
  10625. (let* ((date (calendar-cursor-to-date))
  10626. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10627. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10628. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10629. (select-window sw)))
  10630. (defun org-calendar-select ()
  10631. "Return to `org-read-date' with the date currently selected.
  10632. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10633. (interactive)
  10634. (when (calendar-cursor-to-date)
  10635. (let* ((date (calendar-cursor-to-date))
  10636. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10637. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10638. (if (active-minibuffer-window) (exit-minibuffer))))
  10639. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10640. "Insert a date stamp for the date given by the internal TIME.
  10641. WITH-HM means, use the stamp format that includes the time of the day.
  10642. INACTIVE means use square brackets instead of angular ones, so that the
  10643. stamp will not contribute to the agenda.
  10644. PRE and POST are optional strings to be inserted before and after the
  10645. stamp.
  10646. The command returns the inserted time stamp."
  10647. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10648. stamp)
  10649. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10650. (insert-before-markers (or pre ""))
  10651. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10652. (when (listp extra)
  10653. (setq extra (car extra))
  10654. (if (and (stringp extra)
  10655. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10656. (setq extra (format "-%02d:%02d"
  10657. (string-to-number (match-string 1 extra))
  10658. (string-to-number (match-string 2 extra))))
  10659. (setq extra nil)))
  10660. (when extra
  10661. (backward-char 1)
  10662. (insert-before-markers extra)
  10663. (forward-char 1))
  10664. (insert-before-markers (or post ""))
  10665. (setq org-last-inserted-timestamp stamp)))
  10666. (defun org-toggle-time-stamp-overlays ()
  10667. "Toggle the use of custom time stamp formats."
  10668. (interactive)
  10669. (setq org-display-custom-times (not org-display-custom-times))
  10670. (unless org-display-custom-times
  10671. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10672. (while (setq p (next-single-property-change p 'display))
  10673. (if (and (get-text-property p 'display)
  10674. (eq (get-text-property p 'face) 'org-date))
  10675. (remove-text-properties
  10676. p (setq p (next-single-property-change p 'display))
  10677. '(display t))))
  10678. (set-buffer-modified-p bmp)))
  10679. (if (featurep 'xemacs)
  10680. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10681. (org-restart-font-lock)
  10682. (setq org-table-may-need-update t)
  10683. (if org-display-custom-times
  10684. (message "Time stamps are overlayed with custom format")
  10685. (message "Time stamp overlays removed")))
  10686. (defun org-display-custom-time (beg end)
  10687. "Overlay modified time stamp format over timestamp between BEG and END."
  10688. (let* ((ts (buffer-substring beg end))
  10689. t1 w1 with-hm tf time str w2 (off 0))
  10690. (save-match-data
  10691. (setq t1 (org-parse-time-string ts t))
  10692. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10693. (setq off (- (match-end 0) (match-beginning 0)))))
  10694. (setq end (- end off))
  10695. (setq w1 (- end beg)
  10696. with-hm (and (nth 1 t1) (nth 2 t1))
  10697. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10698. time (org-fix-decoded-time t1)
  10699. str (org-add-props
  10700. (format-time-string
  10701. (substring tf 1 -1) (apply 'encode-time time))
  10702. nil 'mouse-face 'highlight)
  10703. w2 (length str))
  10704. (if (not (= w2 w1))
  10705. (add-text-properties (1+ beg) (+ 2 beg)
  10706. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10707. (if (featurep 'xemacs)
  10708. (progn
  10709. (put-text-property beg end 'invisible t)
  10710. (put-text-property beg end 'end-glyph (make-glyph str)))
  10711. (put-text-property beg end 'display str))))
  10712. (defun org-translate-time (string)
  10713. "Translate all timestamps in STRING to custom format.
  10714. But do this only if the variable `org-display-custom-times' is set."
  10715. (when org-display-custom-times
  10716. (save-match-data
  10717. (let* ((start 0)
  10718. (re org-ts-regexp-both)
  10719. t1 with-hm inactive tf time str beg end)
  10720. (while (setq start (string-match re string start))
  10721. (setq beg (match-beginning 0)
  10722. end (match-end 0)
  10723. t1 (save-match-data
  10724. (org-parse-time-string (substring string beg end) t))
  10725. with-hm (and (nth 1 t1) (nth 2 t1))
  10726. inactive (equal (substring string beg (1+ beg)) "[")
  10727. tf (funcall (if with-hm 'cdr 'car)
  10728. org-time-stamp-custom-formats)
  10729. time (org-fix-decoded-time t1)
  10730. str (format-time-string
  10731. (concat
  10732. (if inactive "[" "<") (substring tf 1 -1)
  10733. (if inactive "]" ">"))
  10734. (apply 'encode-time time))
  10735. string (replace-match str t t string)
  10736. start (+ start (length str)))))))
  10737. string)
  10738. (defun org-fix-decoded-time (time)
  10739. "Set 0 instead of nil for the first 6 elements of time.
  10740. Don't touch the rest."
  10741. (let ((n 0))
  10742. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10743. (defun org-days-to-time (timestamp-string)
  10744. "Difference between TIMESTAMP-STRING and now in days."
  10745. (- (time-to-days (org-time-string-to-time timestamp-string))
  10746. (time-to-days (current-time))))
  10747. (defun org-deadline-close (timestamp-string &optional ndays)
  10748. "Is the time in TIMESTAMP-STRING close to the current date?"
  10749. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10750. (and (< (org-days-to-time timestamp-string) ndays)
  10751. (not (org-entry-is-done-p))))
  10752. (defun org-get-wdays (ts)
  10753. "Get the deadline lead time appropriate for timestring TS."
  10754. (cond
  10755. ((<= org-deadline-warning-days 0)
  10756. ;; 0 or negative, enforce this value no matter what
  10757. (- org-deadline-warning-days))
  10758. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10759. ;; lead time is specified.
  10760. (floor (* (string-to-number (match-string 1 ts))
  10761. (cdr (assoc (match-string 2 ts)
  10762. '(("d" . 1) ("w" . 7)
  10763. ("m" . 30.4) ("y" . 365.25)))))))
  10764. ;; go for the default.
  10765. (t org-deadline-warning-days)))
  10766. (defun org-calendar-select-mouse (ev)
  10767. "Return to `org-read-date' with the date currently selected.
  10768. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10769. (interactive "e")
  10770. (mouse-set-point ev)
  10771. (when (calendar-cursor-to-date)
  10772. (let* ((date (calendar-cursor-to-date))
  10773. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10774. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10775. (if (active-minibuffer-window) (exit-minibuffer))))
  10776. (defun org-check-deadlines (ndays)
  10777. "Check if there are any deadlines due or past due.
  10778. A deadline is considered due if it happens within `org-deadline-warning-days'
  10779. days from today's date. If the deadline appears in an entry marked DONE,
  10780. it is not shown. The prefix arg NDAYS can be used to test that many
  10781. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10782. (interactive "P")
  10783. (let* ((org-warn-days
  10784. (cond
  10785. ((equal ndays '(4)) 100000)
  10786. (ndays (prefix-numeric-value ndays))
  10787. (t (abs org-deadline-warning-days))))
  10788. (case-fold-search nil)
  10789. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10790. (callback
  10791. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10792. (message "%d deadlines past-due or due within %d days"
  10793. (org-occur regexp nil callback)
  10794. org-warn-days)))
  10795. (defun org-check-before-date (date)
  10796. "Check if there are deadlines or scheduled entries before DATE."
  10797. (interactive (list (org-read-date)))
  10798. (let ((case-fold-search nil)
  10799. (regexp (concat "\\<\\(" org-deadline-string
  10800. "\\|" org-scheduled-string
  10801. "\\) *<\\([^>]+\\)>"))
  10802. (callback
  10803. (lambda () (time-less-p
  10804. (org-time-string-to-time (match-string 2))
  10805. (org-time-string-to-time date)))))
  10806. (message "%d entries before %s"
  10807. (org-occur regexp nil callback) date)))
  10808. (defun org-evaluate-time-range (&optional to-buffer)
  10809. "Evaluate a time range by computing the difference between start and end.
  10810. Normally the result is just printed in the echo area, but with prefix arg
  10811. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10812. If the time range is actually in a table, the result is inserted into the
  10813. next column.
  10814. For time difference computation, a year is assumed to be exactly 365
  10815. days in order to avoid rounding problems."
  10816. (interactive "P")
  10817. (or
  10818. (org-clock-update-time-maybe)
  10819. (save-excursion
  10820. (unless (org-at-date-range-p t)
  10821. (goto-char (point-at-bol))
  10822. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10823. (if (not (org-at-date-range-p t))
  10824. (error "Not at a time-stamp range, and none found in current line")))
  10825. (let* ((ts1 (match-string 1))
  10826. (ts2 (match-string 2))
  10827. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10828. (match-end (match-end 0))
  10829. (time1 (org-time-string-to-time ts1))
  10830. (time2 (org-time-string-to-time ts2))
  10831. (t1 (time-to-seconds time1))
  10832. (t2 (time-to-seconds time2))
  10833. (diff (abs (- t2 t1)))
  10834. (negative (< (- t2 t1) 0))
  10835. ;; (ys (floor (* 365 24 60 60)))
  10836. (ds (* 24 60 60))
  10837. (hs (* 60 60))
  10838. (fy "%dy %dd %02d:%02d")
  10839. (fy1 "%dy %dd")
  10840. (fd "%dd %02d:%02d")
  10841. (fd1 "%dd")
  10842. (fh "%02d:%02d")
  10843. y d h m align)
  10844. (if havetime
  10845. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10846. y 0
  10847. d (floor (/ diff ds)) diff (mod diff ds)
  10848. h (floor (/ diff hs)) diff (mod diff hs)
  10849. m (floor (/ diff 60)))
  10850. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10851. y 0
  10852. d (floor (+ (/ diff ds) 0.5))
  10853. h 0 m 0))
  10854. (if (not to-buffer)
  10855. (message "%s" (org-make-tdiff-string y d h m))
  10856. (if (org-at-table-p)
  10857. (progn
  10858. (goto-char match-end)
  10859. (setq align t)
  10860. (and (looking-at " *|") (goto-char (match-end 0))))
  10861. (goto-char match-end))
  10862. (if (looking-at
  10863. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10864. (replace-match ""))
  10865. (if negative (insert " -"))
  10866. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10867. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10868. (insert " " (format fh h m))))
  10869. (if align (org-table-align))
  10870. (message "Time difference inserted")))))
  10871. (defun org-make-tdiff-string (y d h m)
  10872. (let ((fmt "")
  10873. (l nil))
  10874. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10875. l (push y l)))
  10876. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10877. l (push d l)))
  10878. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10879. l (push h l)))
  10880. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10881. l (push m l)))
  10882. (apply 'format fmt (nreverse l))))
  10883. (defun org-time-string-to-time (s)
  10884. (apply 'encode-time (org-parse-time-string s)))
  10885. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10886. "Convert a time stamp to an absolute day number.
  10887. If there is a specifyer for a cyclic time stamp, get the closest date to
  10888. DAYNR.
  10889. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10890. (cond
  10891. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10892. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10893. daynr
  10894. (+ daynr 1000)))
  10895. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10896. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10897. (time-to-days (current-time))) (match-string 0 s)
  10898. prefer show-all))
  10899. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10900. (defun org-days-to-iso-week (days)
  10901. "Return the iso week number."
  10902. (require 'cal-iso)
  10903. (car (calendar-iso-from-absolute days)))
  10904. (defun org-small-year-to-year (year)
  10905. "Convert 2-digit years into 4-digit years.
  10906. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10907. The year 2000 cannot be abbreviated. Any year larger than 99
  10908. is returned unchanged."
  10909. (if (< year 38)
  10910. (setq year (+ 2000 year))
  10911. (if (< year 100)
  10912. (setq year (+ 1900 year))))
  10913. year)
  10914. (defun org-time-from-absolute (d)
  10915. "Return the time corresponding to date D.
  10916. D may be an absolute day number, or a calendar-type list (month day year)."
  10917. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10918. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10919. (defun org-calendar-holiday ()
  10920. "List of holidays, for Diary display in Org-mode."
  10921. (require 'holidays)
  10922. (let ((hl (funcall
  10923. (if (fboundp 'calendar-check-holidays)
  10924. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10925. (if hl (mapconcat 'identity hl "; "))))
  10926. (defun org-diary-sexp-entry (sexp entry date)
  10927. "Process a SEXP diary ENTRY for DATE."
  10928. (require 'diary-lib)
  10929. (let ((result (if calendar-debug-sexp
  10930. (let ((stack-trace-on-error t))
  10931. (eval (car (read-from-string sexp))))
  10932. (condition-case nil
  10933. (eval (car (read-from-string sexp)))
  10934. (error
  10935. (beep)
  10936. (message "Bad sexp at line %d in %s: %s"
  10937. (org-current-line)
  10938. (buffer-file-name) sexp)
  10939. (sleep-for 2))))))
  10940. (cond ((stringp result) result)
  10941. ((and (consp result)
  10942. (stringp (cdr result))) (cdr result))
  10943. (result entry)
  10944. (t nil))))
  10945. (defun org-diary-to-ical-string (frombuf)
  10946. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10947. This uses the icalendar.el library."
  10948. (let* ((tmpdir (if (featurep 'xemacs)
  10949. (temp-directory)
  10950. temporary-file-directory))
  10951. (tmpfile (make-temp-name
  10952. (expand-file-name "orgics" tmpdir)))
  10953. buf rtn b e)
  10954. (save-excursion
  10955. (set-buffer frombuf)
  10956. (icalendar-export-region (point-min) (point-max) tmpfile)
  10957. (setq buf (find-buffer-visiting tmpfile))
  10958. (set-buffer buf)
  10959. (goto-char (point-min))
  10960. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10961. (setq b (match-beginning 0)))
  10962. (goto-char (point-max))
  10963. (if (re-search-backward "^END:VEVENT" nil t)
  10964. (setq e (match-end 0)))
  10965. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10966. (kill-buffer buf)
  10967. (delete-file tmpfile)
  10968. rtn))
  10969. (defun org-closest-date (start current change prefer show-all)
  10970. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10971. When PREFER is `past' return a date that is either CURRENT or past.
  10972. When PREFER is `future', return a date that is either CURRENT or future.
  10973. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10974. ;; Make the proper lists from the dates
  10975. (catch 'exit
  10976. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10977. dn dw sday cday n1 n2 n0
  10978. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10979. (setq start (org-date-to-gregorian start)
  10980. current (org-date-to-gregorian
  10981. (if show-all
  10982. current
  10983. (time-to-days (current-time))))
  10984. sday (calendar-absolute-from-gregorian start)
  10985. cday (calendar-absolute-from-gregorian current))
  10986. (if (<= cday sday) (throw 'exit sday))
  10987. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10988. (setq dn (string-to-number (match-string 1 change))
  10989. dw (cdr (assoc (match-string 2 change) a1)))
  10990. (error "Invalid change specifyer: %s" change))
  10991. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10992. (cond
  10993. ((eq dw 'day)
  10994. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10995. n2 (+ n1 dn)))
  10996. ((eq dw 'year)
  10997. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10998. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10999. (setq date1 (list m d y1)
  11000. n1 (calendar-absolute-from-gregorian date1)
  11001. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11002. n2 (calendar-absolute-from-gregorian date2)))
  11003. ((eq dw 'month)
  11004. ;; approx number of month between the two dates
  11005. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11006. ;; How often does dn fit in there?
  11007. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11008. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11009. m (+ m nm)
  11010. ny (floor (/ m 12))
  11011. y (+ y ny)
  11012. m (- m (* ny 12)))
  11013. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11014. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11015. (setq m2 (+ m dn) y2 y)
  11016. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11017. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11018. (while (<= n2 cday)
  11019. (setq n1 n2 m m2 y y2)
  11020. (setq m2 (+ m dn) y2 y)
  11021. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11022. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11023. ;; Make sure n1 is the earlier date
  11024. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11025. (if show-all
  11026. (cond
  11027. ((eq prefer 'past) n1)
  11028. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11029. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11030. (cond
  11031. ((eq prefer 'past) n1)
  11032. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11033. (t (if (= cday n1) n1 n2)))))))
  11034. (defun org-date-to-gregorian (date)
  11035. "Turn any specification of DATE into a gregorian date for the calendar."
  11036. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11037. ((and (listp date) (= (length date) 3)) date)
  11038. ((stringp date)
  11039. (setq date (org-parse-time-string date))
  11040. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11041. ((listp date)
  11042. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11043. (defun org-parse-time-string (s &optional nodefault)
  11044. "Parse the standard Org-mode time string.
  11045. This should be a lot faster than the normal `parse-time-string'.
  11046. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11047. hour and minute fields will be nil if not given."
  11048. (if (string-match org-ts-regexp0 s)
  11049. (list 0
  11050. (if (or (match-beginning 8) (not nodefault))
  11051. (string-to-number (or (match-string 8 s) "0")))
  11052. (if (or (match-beginning 7) (not nodefault))
  11053. (string-to-number (or (match-string 7 s) "0")))
  11054. (string-to-number (match-string 4 s))
  11055. (string-to-number (match-string 3 s))
  11056. (string-to-number (match-string 2 s))
  11057. nil nil nil)
  11058. (make-list 9 0)))
  11059. (defun org-timestamp-up (&optional arg)
  11060. "Increase the date item at the cursor by one.
  11061. If the cursor is on the year, change the year. If it is on the month or
  11062. the day, change that.
  11063. With prefix ARG, change by that many units."
  11064. (interactive "p")
  11065. (org-timestamp-change (prefix-numeric-value arg)))
  11066. (defun org-timestamp-down (&optional arg)
  11067. "Decrease the date item at the cursor by one.
  11068. If the cursor is on the year, change the year. If it is on the month or
  11069. the day, change that.
  11070. With prefix ARG, change by that many units."
  11071. (interactive "p")
  11072. (org-timestamp-change (- (prefix-numeric-value arg))))
  11073. (defun org-timestamp-up-day (&optional arg)
  11074. "Increase the date in the time stamp by one day.
  11075. With prefix ARG, change that many days."
  11076. (interactive "p")
  11077. (if (and (not (org-at-timestamp-p t))
  11078. (org-on-heading-p))
  11079. (org-todo 'up)
  11080. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11081. (defun org-timestamp-down-day (&optional arg)
  11082. "Decrease the date in the time stamp by one day.
  11083. With prefix ARG, change that many days."
  11084. (interactive "p")
  11085. (if (and (not (org-at-timestamp-p t))
  11086. (org-on-heading-p))
  11087. (org-todo 'down)
  11088. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11089. (defun org-at-timestamp-p (&optional inactive-ok)
  11090. "Determine if the cursor is in or at a timestamp."
  11091. (interactive)
  11092. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11093. (pos (point))
  11094. (ans (or (looking-at tsr)
  11095. (save-excursion
  11096. (skip-chars-backward "^[<\n\r\t")
  11097. (if (> (point) (point-min)) (backward-char 1))
  11098. (and (looking-at tsr)
  11099. (> (- (match-end 0) pos) -1))))))
  11100. (and ans
  11101. (boundp 'org-ts-what)
  11102. (setq org-ts-what
  11103. (cond
  11104. ((= pos (match-beginning 0)) 'bracket)
  11105. ((= pos (1- (match-end 0))) 'bracket)
  11106. ((org-pos-in-match-range pos 2) 'year)
  11107. ((org-pos-in-match-range pos 3) 'month)
  11108. ((org-pos-in-match-range pos 7) 'hour)
  11109. ((org-pos-in-match-range pos 8) 'minute)
  11110. ((or (org-pos-in-match-range pos 4)
  11111. (org-pos-in-match-range pos 5)) 'day)
  11112. ((and (> pos (or (match-end 8) (match-end 5)))
  11113. (< pos (match-end 0)))
  11114. (- pos (or (match-end 8) (match-end 5))))
  11115. (t 'day))))
  11116. ans))
  11117. (defun org-toggle-timestamp-type ()
  11118. "Toggle the type (<active> or [inactive]) of a time stamp."
  11119. (interactive)
  11120. (when (org-at-timestamp-p t)
  11121. (let ((beg (match-beginning 0)) (end (match-end 0))
  11122. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11123. (save-excursion
  11124. (goto-char beg)
  11125. (while (re-search-forward "[][<>]" end t)
  11126. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11127. t t)))
  11128. (message "Timestamp is now %sactive"
  11129. (if (equal (char-after beg) ?<) "" "in")))))
  11130. (defun org-timestamp-change (n &optional what)
  11131. "Change the date in the time stamp at point.
  11132. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11133. `year', `minute', `second'. If WHAT is not given, the cursor position
  11134. in the timestamp determines what will be changed."
  11135. (let ((pos (point))
  11136. with-hm inactive
  11137. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11138. org-ts-what
  11139. extra rem
  11140. ts time time0)
  11141. (if (not (org-at-timestamp-p t))
  11142. (error "Not at a timestamp"))
  11143. (if (and (not what) (eq org-ts-what 'bracket))
  11144. (org-toggle-timestamp-type)
  11145. (if (and (not what) (not (eq org-ts-what 'day))
  11146. org-display-custom-times
  11147. (get-text-property (point) 'display)
  11148. (not (get-text-property (1- (point)) 'display)))
  11149. (setq org-ts-what 'day))
  11150. (setq org-ts-what (or what org-ts-what)
  11151. inactive (= (char-after (match-beginning 0)) ?\[)
  11152. ts (match-string 0))
  11153. (replace-match "")
  11154. (if (string-match
  11155. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11156. ts)
  11157. (setq extra (match-string 1 ts)))
  11158. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11159. (setq with-hm t))
  11160. (setq time0 (org-parse-time-string ts))
  11161. (when (and (eq org-ts-what 'minute)
  11162. (eq current-prefix-arg nil))
  11163. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11164. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11165. (setcar (cdr time0) (+ (nth 1 time0)
  11166. (if (> n 0) (- rem) (- dm rem))))))
  11167. (setq time
  11168. (encode-time (or (car time0) 0)
  11169. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11170. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11171. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11172. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11173. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11174. (nthcdr 6 time0)))
  11175. (when (integerp org-ts-what)
  11176. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11177. (if (eq what 'calendar)
  11178. (let ((cal-date (org-get-date-from-calendar)))
  11179. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11180. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11181. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11182. (setcar time0 (or (car time0) 0))
  11183. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11184. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11185. (setq time (apply 'encode-time time0))))
  11186. (setq org-last-changed-timestamp
  11187. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11188. (org-clock-update-time-maybe)
  11189. (goto-char pos)
  11190. ;; Try to recenter the calendar window, if any
  11191. (if (and org-calendar-follow-timestamp-change
  11192. (get-buffer-window "*Calendar*" t)
  11193. (memq org-ts-what '(day month year)))
  11194. (org-recenter-calendar (time-to-days time))))))
  11195. (defun org-modify-ts-extra (s pos n dm)
  11196. "Change the different parts of the lead-time and repeat fields in timestamp."
  11197. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11198. ng h m new rem)
  11199. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11200. (cond
  11201. ((or (org-pos-in-match-range pos 2)
  11202. (org-pos-in-match-range pos 3))
  11203. (setq m (string-to-number (match-string 3 s))
  11204. h (string-to-number (match-string 2 s)))
  11205. (if (org-pos-in-match-range pos 2)
  11206. (setq h (+ h n))
  11207. (setq n (* dm (org-no-warnings (signum n))))
  11208. (when (not (= 0 (setq rem (% m dm))))
  11209. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11210. (setq m (+ m n)))
  11211. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11212. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11213. (setq h (min 24 (max 0 h)))
  11214. (setq ng 1 new (format "-%02d:%02d" h m)))
  11215. ((org-pos-in-match-range pos 6)
  11216. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11217. ((org-pos-in-match-range pos 5)
  11218. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11219. ((org-pos-in-match-range pos 9)
  11220. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11221. ((org-pos-in-match-range pos 8)
  11222. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11223. (when ng
  11224. (setq s (concat
  11225. (substring s 0 (match-beginning ng))
  11226. new
  11227. (substring s (match-end ng))))))
  11228. s))
  11229. (defun org-recenter-calendar (date)
  11230. "If the calendar is visible, recenter it to DATE."
  11231. (let* ((win (selected-window))
  11232. (cwin (get-buffer-window "*Calendar*" t))
  11233. (calendar-move-hook nil))
  11234. (when cwin
  11235. (select-window cwin)
  11236. (calendar-goto-date (if (listp date) date
  11237. (calendar-gregorian-from-absolute date)))
  11238. (select-window win))))
  11239. (defun org-goto-calendar (&optional arg)
  11240. "Go to the Emacs calendar at the current date.
  11241. If there is a time stamp in the current line, go to that date.
  11242. A prefix ARG can be used to force the current date."
  11243. (interactive "P")
  11244. (let ((tsr org-ts-regexp) diff
  11245. (calendar-move-hook nil)
  11246. (calendar-view-holidays-initially-flag nil)
  11247. (view-calendar-holidays-initially nil)
  11248. (calendar-view-diary-initially-flag nil)
  11249. (view-diary-entries-initially nil))
  11250. (if (or (org-at-timestamp-p)
  11251. (save-excursion
  11252. (beginning-of-line 1)
  11253. (looking-at (concat ".*" tsr))))
  11254. (let ((d1 (time-to-days (current-time)))
  11255. (d2 (time-to-days
  11256. (org-time-string-to-time (match-string 1)))))
  11257. (setq diff (- d2 d1))))
  11258. (calendar)
  11259. (calendar-goto-today)
  11260. (if (and diff (not arg)) (calendar-forward-day diff))))
  11261. (defun org-get-date-from-calendar ()
  11262. "Return a list (month day year) of date at point in calendar."
  11263. (with-current-buffer "*Calendar*"
  11264. (save-match-data
  11265. (calendar-cursor-to-date))))
  11266. (defun org-date-from-calendar ()
  11267. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11268. If there is already a time stamp at the cursor position, update it."
  11269. (interactive)
  11270. (if (org-at-timestamp-p t)
  11271. (org-timestamp-change 0 'calendar)
  11272. (let ((cal-date (org-get-date-from-calendar)))
  11273. (org-insert-time-stamp
  11274. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11275. (defun org-minutes-to-hh:mm-string (m)
  11276. "Compute H:MM from a number of minutes."
  11277. (let ((h (/ m 60)))
  11278. (setq m (- m (* 60 h)))
  11279. (format org-time-clocksum-format h m)))
  11280. (defun org-hh:mm-string-to-minutes (s)
  11281. "Convert a string H:MM to a number of minutes."
  11282. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11283. (+ (* (string-to-number (match-string 1 s)) 60)
  11284. (string-to-number (match-string 2 s)))
  11285. 0))
  11286. ;;;; Files
  11287. (defun org-save-all-org-buffers ()
  11288. "Save all Org-mode buffers without user confirmation."
  11289. (interactive)
  11290. (message "Saving all Org-mode buffers...")
  11291. (save-some-buffers t 'org-mode-p)
  11292. (when (featurep 'org-id) (org-id-locations-save))
  11293. (message "Saving all Org-mode buffers... done"))
  11294. (defun org-revert-all-org-buffers ()
  11295. "Revert all Org-mode buffers.
  11296. Prompt for confirmation when there are unsaved changes.
  11297. Be sure you know what you are doing before letting this function
  11298. overwrite your changes.
  11299. This function is useful in a setup where one tracks org files
  11300. with a version control system, to revert on one machine after pulling
  11301. changes from another. I believe the procedure must be like this:
  11302. 1. M-x org-save-all-org-buffers
  11303. 2. Pull changes from the other machine, resolve conflicts
  11304. 3. M-x org-revert-all-org-buffers"
  11305. (interactive)
  11306. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11307. (error "Abort"))
  11308. (save-excursion
  11309. (save-window-excursion
  11310. (mapc
  11311. (lambda (b)
  11312. (when (and (with-current-buffer b (org-mode-p))
  11313. (with-current-buffer b buffer-file-name))
  11314. (switch-to-buffer b)
  11315. (revert-buffer t 'no-confirm)))
  11316. (buffer-list))
  11317. (when (and (featurep 'org-id) org-id-track-globally)
  11318. (org-id-locations-load)))))
  11319. ;;;; Agenda files
  11320. ;;;###autoload
  11321. (defun org-iswitchb (&optional arg)
  11322. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11323. With a prefix argument, restrict available to files.
  11324. With two prefix arguments, restrict available buffers to agenda files.
  11325. Due to some yet unresolved reason, the global function
  11326. `iswitchb-mode' needs to be active for this function to work."
  11327. (interactive "P")
  11328. (require 'iswitchb)
  11329. (let ((enabled iswitchb-mode) blist)
  11330. (or enabled (iswitchb-mode 1))
  11331. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11332. ((equal arg '(16)) (org-buffer-list 'agenda))
  11333. (t (org-buffer-list))))
  11334. (unwind-protect
  11335. (let ((iswitchb-make-buflist-hook
  11336. (lambda ()
  11337. (setq iswitchb-temp-buflist
  11338. (mapcar 'buffer-name blist)))))
  11339. (switch-to-buffer
  11340. (iswitchb-read-buffer
  11341. "Switch-to: " nil t))
  11342. (or enabled (iswitchb-mode -1))))))
  11343. ;;;###autoload
  11344. (defun org-ido-switchb (&optional arg)
  11345. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11346. With a prefix argument, restrict available to files.
  11347. With two prefix arguments, restrict available buffers to agenda files."
  11348. (interactive "P")
  11349. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11350. ((equal arg '(16)) (org-buffer-list 'agenda))
  11351. (t (org-buffer-list)))))
  11352. (switch-to-buffer
  11353. (org-ido-completing-read "Org buffer: "
  11354. (mapcar 'buffer-name blist)
  11355. nil t))))
  11356. (defun org-buffer-list (&optional predicate exclude-tmp)
  11357. "Return a list of Org buffers.
  11358. PREDICATE can be `export', `files' or `agenda'.
  11359. export restrict the list to Export buffers.
  11360. files restrict the list to buffers visiting Org files.
  11361. agenda restrict the list to buffers visiting agenda files.
  11362. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11363. (let* ((bfn nil)
  11364. (agenda-files (and (eq predicate 'agenda)
  11365. (mapcar 'file-truename (org-agenda-files t))))
  11366. (filter
  11367. (cond
  11368. ((eq predicate 'files)
  11369. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11370. ((eq predicate 'export)
  11371. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11372. ((eq predicate 'agenda)
  11373. (lambda (b)
  11374. (with-current-buffer b
  11375. (and (eq major-mode 'org-mode)
  11376. (setq bfn (buffer-file-name b))
  11377. (member (file-truename bfn) agenda-files)))))
  11378. (t (lambda (b) (with-current-buffer b
  11379. (or (eq major-mode 'org-mode)
  11380. (string-match "\*Org .*Export"
  11381. (buffer-name b)))))))))
  11382. (delq nil
  11383. (mapcar
  11384. (lambda(b)
  11385. (if (and (funcall filter b)
  11386. (or (not exclude-tmp)
  11387. (not (string-match "tmp" (buffer-name b)))))
  11388. b
  11389. nil))
  11390. (buffer-list)))))
  11391. (defun org-agenda-files (&optional unrestricted archives)
  11392. "Get the list of agenda files.
  11393. Optional UNRESTRICTED means return the full list even if a restriction
  11394. is currently in place.
  11395. When ARCHIVES is t, include all archive files hat are really being
  11396. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11397. `org-agenda-archives-mode' is t."
  11398. (let ((files
  11399. (cond
  11400. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11401. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11402. ((listp org-agenda-files) org-agenda-files)
  11403. (t (error "Invalid value of `org-agenda-files'")))))
  11404. (setq files (apply 'append
  11405. (mapcar (lambda (f)
  11406. (if (file-directory-p f)
  11407. (directory-files
  11408. f t org-agenda-file-regexp)
  11409. (list f)))
  11410. files)))
  11411. (when org-agenda-skip-unavailable-files
  11412. (setq files (delq nil
  11413. (mapcar (function
  11414. (lambda (file)
  11415. (and (file-readable-p file) file)))
  11416. files))))
  11417. (when (or (eq archives t)
  11418. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11419. (setq files (org-add-archive-files files)))
  11420. files))
  11421. (defun org-edit-agenda-file-list ()
  11422. "Edit the list of agenda files.
  11423. Depending on setup, this either uses customize to edit the variable
  11424. `org-agenda-files', or it visits the file that is holding the list. In the
  11425. latter case, the buffer is set up in a way that saving it automatically kills
  11426. the buffer and restores the previous window configuration."
  11427. (interactive)
  11428. (if (stringp org-agenda-files)
  11429. (let ((cw (current-window-configuration)))
  11430. (find-file org-agenda-files)
  11431. (org-set-local 'org-window-configuration cw)
  11432. (org-add-hook 'after-save-hook
  11433. (lambda ()
  11434. (set-window-configuration
  11435. (prog1 org-window-configuration
  11436. (kill-buffer (current-buffer))))
  11437. (org-install-agenda-files-menu)
  11438. (message "New agenda file list installed"))
  11439. nil 'local)
  11440. (message "%s" (substitute-command-keys
  11441. "Edit list and finish with \\[save-buffer]")))
  11442. (customize-variable 'org-agenda-files)))
  11443. (defun org-store-new-agenda-file-list (list)
  11444. "Set new value for the agenda file list and save it correctly."
  11445. (if (stringp org-agenda-files)
  11446. (let ((f org-agenda-files) b)
  11447. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11448. (with-temp-file f
  11449. (insert (mapconcat 'identity list "\n") "\n")))
  11450. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11451. (setq org-agenda-files list)
  11452. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11453. (defun org-read-agenda-file-list ()
  11454. "Read the list of agenda files from a file."
  11455. (when (file-directory-p org-agenda-files)
  11456. (error "`org-agenda-files' cannot be a single directory"))
  11457. (when (stringp org-agenda-files)
  11458. (with-temp-buffer
  11459. (insert-file-contents org-agenda-files)
  11460. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11461. ;;;###autoload
  11462. (defun org-cycle-agenda-files ()
  11463. "Cycle through the files in `org-agenda-files'.
  11464. If the current buffer visits an agenda file, find the next one in the list.
  11465. If the current buffer does not, find the first agenda file."
  11466. (interactive)
  11467. (let* ((fs (org-agenda-files t))
  11468. (files (append fs (list (car fs))))
  11469. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11470. file)
  11471. (unless files (error "No agenda files"))
  11472. (catch 'exit
  11473. (while (setq file (pop files))
  11474. (if (equal (file-truename file) tcf)
  11475. (when (car files)
  11476. (find-file (car files))
  11477. (throw 'exit t))))
  11478. (find-file (car fs)))
  11479. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11480. (defun org-agenda-file-to-front (&optional to-end)
  11481. "Move/add the current file to the top of the agenda file list.
  11482. If the file is not present in the list, it is added to the front. If it is
  11483. present, it is moved there. With optional argument TO-END, add/move to the
  11484. end of the list."
  11485. (interactive "P")
  11486. (let ((org-agenda-skip-unavailable-files nil)
  11487. (file-alist (mapcar (lambda (x)
  11488. (cons (file-truename x) x))
  11489. (org-agenda-files t)))
  11490. (ctf (file-truename buffer-file-name))
  11491. x had)
  11492. (setq x (assoc ctf file-alist) had x)
  11493. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11494. (if to-end
  11495. (setq file-alist (append (delq x file-alist) (list x)))
  11496. (setq file-alist (cons x (delq x file-alist))))
  11497. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11498. (org-install-agenda-files-menu)
  11499. (message "File %s to %s of agenda file list"
  11500. (if had "moved" "added") (if to-end "end" "front"))))
  11501. (defun org-remove-file (&optional file)
  11502. "Remove current file from the list of files in variable `org-agenda-files'.
  11503. These are the files which are being checked for agenda entries.
  11504. Optional argument FILE means, use this file instead of the current."
  11505. (interactive)
  11506. (let* ((org-agenda-skip-unavailable-files nil)
  11507. (file (or file buffer-file-name))
  11508. (true-file (file-truename file))
  11509. (afile (abbreviate-file-name file))
  11510. (files (delq nil (mapcar
  11511. (lambda (x)
  11512. (if (equal true-file
  11513. (file-truename x))
  11514. nil x))
  11515. (org-agenda-files t)))))
  11516. (if (not (= (length files) (length (org-agenda-files t))))
  11517. (progn
  11518. (org-store-new-agenda-file-list files)
  11519. (org-install-agenda-files-menu)
  11520. (message "Removed file: %s" afile))
  11521. (message "File was not in list: %s (not removed)" afile))))
  11522. (defun org-file-menu-entry (file)
  11523. (vector file (list 'find-file file) t))
  11524. (defun org-check-agenda-file (file)
  11525. "Make sure FILE exists. If not, ask user what to do."
  11526. (when (not (file-exists-p file))
  11527. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11528. (abbreviate-file-name file))
  11529. (let ((r (downcase (read-char-exclusive))))
  11530. (cond
  11531. ((equal r ?r)
  11532. (org-remove-file file)
  11533. (throw 'nextfile t))
  11534. (t (error "Abort"))))))
  11535. (defun org-get-agenda-file-buffer (file)
  11536. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11537. it to the list of buffers which might be released later."
  11538. (let ((buf (org-find-base-buffer-visiting file)))
  11539. (if buf
  11540. buf ; just return it
  11541. ;; Make a new buffer and remember it
  11542. (setq buf (find-file-noselect file))
  11543. (if buf (push buf org-agenda-new-buffers))
  11544. buf)))
  11545. (defun org-release-buffers (blist)
  11546. "Release all buffers in list, asking the user for confirmation when needed.
  11547. When a buffer is unmodified, it is just killed. When modified, it is saved
  11548. \(if the user agrees) and then killed."
  11549. (let (buf file)
  11550. (while (setq buf (pop blist))
  11551. (setq file (buffer-file-name buf))
  11552. (when (and (buffer-modified-p buf)
  11553. file
  11554. (y-or-n-p (format "Save file %s? " file)))
  11555. (with-current-buffer buf (save-buffer)))
  11556. (kill-buffer buf))))
  11557. (defun org-prepare-agenda-buffers (files)
  11558. "Create buffers for all agenda files, protect archived trees and comments."
  11559. (interactive)
  11560. (let ((pa '(:org-archived t))
  11561. (pc '(:org-comment t))
  11562. (pall '(:org-archived t :org-comment t))
  11563. (inhibit-read-only t)
  11564. (rea (concat ":" org-archive-tag ":"))
  11565. bmp file re)
  11566. (save-excursion
  11567. (save-restriction
  11568. (while (setq file (pop files))
  11569. (if (bufferp file)
  11570. (set-buffer file)
  11571. (org-check-agenda-file file)
  11572. (set-buffer (org-get-agenda-file-buffer file)))
  11573. (widen)
  11574. (setq bmp (buffer-modified-p))
  11575. (org-refresh-category-properties)
  11576. (setq org-todo-keywords-for-agenda
  11577. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11578. (setq org-done-keywords-for-agenda
  11579. (append org-done-keywords-for-agenda org-done-keywords))
  11580. (setq org-todo-keyword-alist-for-agenda
  11581. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11582. (setq org-tag-alist-for-agenda
  11583. (append org-tag-alist-for-agenda org-tag-alist))
  11584. (save-excursion
  11585. (remove-text-properties (point-min) (point-max) pall)
  11586. (when org-agenda-skip-archived-trees
  11587. (goto-char (point-min))
  11588. (while (re-search-forward rea nil t)
  11589. (if (org-on-heading-p t)
  11590. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11591. (goto-char (point-min))
  11592. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11593. (while (re-search-forward re nil t)
  11594. (add-text-properties
  11595. (match-beginning 0) (org-end-of-subtree t) pc)))
  11596. (set-buffer-modified-p bmp))))
  11597. (setq org-todo-keyword-alist-for-agenda
  11598. (org-uniquify org-todo-keyword-alist-for-agenda)
  11599. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11600. ;;;; Embedded LaTeX
  11601. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11602. "Keymap for the minor `org-cdlatex-mode'.")
  11603. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11604. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11605. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11606. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11607. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11608. (defvar org-cdlatex-texmathp-advice-is-done nil
  11609. "Flag remembering if we have applied the advice to texmathp already.")
  11610. (define-minor-mode org-cdlatex-mode
  11611. "Toggle the minor `org-cdlatex-mode'.
  11612. This mode supports entering LaTeX environment and math in LaTeX fragments
  11613. in Org-mode.
  11614. \\{org-cdlatex-mode-map}"
  11615. nil " OCDL" nil
  11616. (when org-cdlatex-mode (require 'cdlatex))
  11617. (unless org-cdlatex-texmathp-advice-is-done
  11618. (setq org-cdlatex-texmathp-advice-is-done t)
  11619. (defadvice texmathp (around org-math-always-on activate)
  11620. "Always return t in org-mode buffers.
  11621. This is because we want to insert math symbols without dollars even outside
  11622. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11623. an embedded LaTeX fragment, let texmathp do its job.
  11624. \\[org-cdlatex-mode-map]"
  11625. (interactive)
  11626. (let (p)
  11627. (cond
  11628. ((not (org-mode-p)) ad-do-it)
  11629. ((eq this-command 'cdlatex-math-symbol)
  11630. (setq ad-return-value t
  11631. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11632. (t
  11633. (let ((p (org-inside-LaTeX-fragment-p)))
  11634. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11635. (setq ad-return-value t
  11636. texmathp-why '("Org-mode embedded math" . 0))
  11637. (if p ad-do-it)))))))))
  11638. (defun turn-on-org-cdlatex ()
  11639. "Unconditionally turn on `org-cdlatex-mode'."
  11640. (org-cdlatex-mode 1))
  11641. (defun org-inside-LaTeX-fragment-p ()
  11642. "Test if point is inside a LaTeX fragment.
  11643. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11644. sequence appearing also before point.
  11645. Even though the matchers for math are configurable, this function assumes
  11646. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11647. delimiters are skipped when they have been removed by customization.
  11648. The return value is nil, or a cons cell with the delimiter and
  11649. and the position of this delimiter.
  11650. This function does a reasonably good job, but can locally be fooled by
  11651. for example currency specifications. For example it will assume being in
  11652. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11653. fragments that are properly closed, but during editing, we have to live
  11654. with the uncertainty caused by missing closing delimiters. This function
  11655. looks only before point, not after."
  11656. (catch 'exit
  11657. (let ((pos (point))
  11658. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11659. (lim (progn
  11660. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11661. (point)))
  11662. dd-on str (start 0) m re)
  11663. (goto-char pos)
  11664. (when dodollar
  11665. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11666. re (nth 1 (assoc "$" org-latex-regexps)))
  11667. (while (string-match re str start)
  11668. (cond
  11669. ((= (match-end 0) (length str))
  11670. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11671. ((= (match-end 0) (- (length str) 5))
  11672. (throw 'exit nil))
  11673. (t (setq start (match-end 0))))))
  11674. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11675. (goto-char pos)
  11676. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11677. (and (match-beginning 2) (throw 'exit nil))
  11678. ;; count $$
  11679. (while (re-search-backward "\\$\\$" lim t)
  11680. (setq dd-on (not dd-on)))
  11681. (goto-char pos)
  11682. (if dd-on (cons "$$" m))))))
  11683. (defun org-try-cdlatex-tab ()
  11684. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11685. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11686. - inside a LaTeX fragment, or
  11687. - after the first word in a line, where an abbreviation expansion could
  11688. insert a LaTeX environment."
  11689. (when org-cdlatex-mode
  11690. (cond
  11691. ((save-excursion
  11692. (skip-chars-backward "a-zA-Z0-9*")
  11693. (skip-chars-backward " \t")
  11694. (bolp))
  11695. (cdlatex-tab) t)
  11696. ((org-inside-LaTeX-fragment-p)
  11697. (cdlatex-tab) t)
  11698. (t nil))))
  11699. (defun org-cdlatex-underscore-caret (&optional arg)
  11700. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11701. Revert to the normal definition outside of these fragments."
  11702. (interactive "P")
  11703. (if (org-inside-LaTeX-fragment-p)
  11704. (call-interactively 'cdlatex-sub-superscript)
  11705. (let (org-cdlatex-mode)
  11706. (call-interactively (key-binding (vector last-input-event))))))
  11707. (defun org-cdlatex-math-modify (&optional arg)
  11708. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11709. Revert to the normal definition outside of these fragments."
  11710. (interactive "P")
  11711. (if (org-inside-LaTeX-fragment-p)
  11712. (call-interactively 'cdlatex-math-modify)
  11713. (let (org-cdlatex-mode)
  11714. (call-interactively (key-binding (vector last-input-event))))))
  11715. (defvar org-latex-fragment-image-overlays nil
  11716. "List of overlays carrying the images of latex fragments.")
  11717. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11718. (defun org-remove-latex-fragment-image-overlays ()
  11719. "Remove all overlays with LaTeX fragment images in current buffer."
  11720. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11721. (setq org-latex-fragment-image-overlays nil))
  11722. (defun org-preview-latex-fragment (&optional subtree)
  11723. "Preview the LaTeX fragment at point, or all locally or globally.
  11724. If the cursor is in a LaTeX fragment, create the image and overlay
  11725. it over the source code. If there is no fragment at point, display
  11726. all fragments in the current text, from one headline to the next. With
  11727. prefix SUBTREE, display all fragments in the current subtree. With a
  11728. double prefix `C-u C-u', or when the cursor is before the first headline,
  11729. display all fragments in the buffer.
  11730. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11731. (interactive "P")
  11732. (org-remove-latex-fragment-image-overlays)
  11733. (save-excursion
  11734. (save-restriction
  11735. (let (beg end at msg)
  11736. (cond
  11737. ((or (equal subtree '(16))
  11738. (not (save-excursion
  11739. (re-search-backward (concat "^" outline-regexp) nil t))))
  11740. (setq beg (point-min) end (point-max)
  11741. msg "Creating images for buffer...%s"))
  11742. ((equal subtree '(4))
  11743. (org-back-to-heading)
  11744. (setq beg (point) end (org-end-of-subtree t)
  11745. msg "Creating images for subtree...%s"))
  11746. (t
  11747. (if (setq at (org-inside-LaTeX-fragment-p))
  11748. (goto-char (max (point-min) (- (cdr at) 2)))
  11749. (org-back-to-heading))
  11750. (setq beg (point) end (progn (outline-next-heading) (point))
  11751. msg (if at "Creating image...%s"
  11752. "Creating images for entry...%s"))))
  11753. (message msg "")
  11754. (narrow-to-region beg end)
  11755. (goto-char beg)
  11756. (org-format-latex
  11757. (concat "ltxpng/" (file-name-sans-extension
  11758. (file-name-nondirectory
  11759. buffer-file-name)))
  11760. default-directory 'overlays msg at 'forbuffer)
  11761. (message msg "done. Use `C-c C-c' to remove images.")))))
  11762. (defvar org-latex-regexps
  11763. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11764. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11765. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11766. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11767. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11768. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11769. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11770. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11771. "Regular expressions for matching embedded LaTeX.")
  11772. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11773. "Replace LaTeX fragments with links to an image, and produce images."
  11774. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11775. (let* ((prefixnodir (file-name-nondirectory prefix))
  11776. (absprefix (expand-file-name prefix dir))
  11777. (todir (file-name-directory absprefix))
  11778. (opt org-format-latex-options)
  11779. (matchers (plist-get opt :matchers))
  11780. (re-list org-latex-regexps)
  11781. (cnt 0) txt link beg end re e checkdir
  11782. m n block linkfile movefile ov)
  11783. ;; Check if there are old images files with this prefix, and remove them
  11784. (when (file-directory-p todir)
  11785. (mapc 'delete-file
  11786. (directory-files
  11787. todir 'full
  11788. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11789. ;; Check the different regular expressions
  11790. (while (setq e (pop re-list))
  11791. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11792. block (if (nth 3 e) "\n\n" ""))
  11793. (when (member m matchers)
  11794. (goto-char (point-min))
  11795. (while (re-search-forward re nil t)
  11796. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  11797. (not (get-text-property (match-beginning n)
  11798. 'org-protected)))
  11799. (setq txt (match-string n)
  11800. beg (match-beginning n) end (match-end n)
  11801. cnt (1+ cnt)
  11802. linkfile (format "%s_%04d.png" prefix cnt)
  11803. movefile (format "%s_%04d.png" absprefix cnt)
  11804. link (concat block "[[file:" linkfile "]]" block))
  11805. (if msg (message msg cnt))
  11806. (goto-char beg)
  11807. (unless checkdir ; make sure the directory exists
  11808. (setq checkdir t)
  11809. (or (file-directory-p todir) (make-directory todir)))
  11810. (org-create-formula-image
  11811. txt movefile opt forbuffer)
  11812. (if overlays
  11813. (progn
  11814. (setq ov (org-make-overlay beg end))
  11815. (if (featurep 'xemacs)
  11816. (progn
  11817. (org-overlay-put ov 'invisible t)
  11818. (org-overlay-put
  11819. ov 'end-glyph
  11820. (make-glyph (vector 'png :file movefile))))
  11821. (org-overlay-put
  11822. ov 'display
  11823. (list 'image :type 'png :file movefile :ascent 'center)))
  11824. (push ov org-latex-fragment-image-overlays)
  11825. (goto-char end))
  11826. (delete-region beg end)
  11827. (insert link))))))))
  11828. ;; This function borrows from Ganesh Swami's latex2png.el
  11829. (defun org-create-formula-image (string tofile options buffer)
  11830. (let* ((tmpdir (if (featurep 'xemacs)
  11831. (temp-directory)
  11832. temporary-file-directory))
  11833. (texfilebase (make-temp-name
  11834. (expand-file-name "orgtex" tmpdir)))
  11835. (texfile (concat texfilebase ".tex"))
  11836. (dvifile (concat texfilebase ".dvi"))
  11837. (pngfile (concat texfilebase ".png"))
  11838. (fnh (if (featurep 'xemacs)
  11839. (font-height (get-face-font 'default))
  11840. (face-attribute 'default :height nil)))
  11841. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11842. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11843. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11844. "Black"))
  11845. (bg (or (plist-get options (if buffer :background :html-background))
  11846. "Transparent")))
  11847. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11848. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11849. (with-temp-file texfile
  11850. (insert org-format-latex-header
  11851. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11852. (let ((dir default-directory))
  11853. (condition-case nil
  11854. (progn
  11855. (cd tmpdir)
  11856. (call-process "latex" nil nil nil texfile))
  11857. (error nil))
  11858. (cd dir))
  11859. (if (not (file-exists-p dvifile))
  11860. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11861. (condition-case nil
  11862. (call-process "dvipng" nil nil nil
  11863. "-E" "-fg" fg "-bg" bg
  11864. "-D" dpi
  11865. ;;"-x" scale "-y" scale
  11866. "-T" "tight"
  11867. "-o" pngfile
  11868. dvifile)
  11869. (error nil))
  11870. (if (not (file-exists-p pngfile))
  11871. (progn (message "Failed to create png file from %s" texfile) nil)
  11872. ;; Use the requested file name and clean up
  11873. (copy-file pngfile tofile 'replace)
  11874. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11875. (delete-file (concat texfilebase e)))
  11876. pngfile))))
  11877. (defun org-dvipng-color (attr)
  11878. "Return an rgb color specification for dvipng."
  11879. (apply 'format "rgb %s %s %s"
  11880. (mapcar 'org-normalize-color
  11881. (color-values (face-attribute 'default attr nil)))))
  11882. (defun org-normalize-color (value)
  11883. "Return string to be used as color value for an RGB component."
  11884. (format "%g" (/ value 65535.0)))
  11885. ;;;; Key bindings
  11886. ;; Make `C-c C-x' a prefix key
  11887. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11888. ;; TAB key with modifiers
  11889. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11890. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11891. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11892. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11893. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11894. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11895. ;; The following line is necessary under Suse GNU/Linux
  11896. (unless (featurep 'xemacs)
  11897. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11898. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11899. (define-key org-mode-map [backtab] 'org-shifttab)
  11900. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11901. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11902. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11903. ;; Cursor keys with modifiers
  11904. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11905. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11906. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11907. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11908. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11909. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11910. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11911. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11912. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11913. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11914. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11915. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11916. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11917. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11918. ;;; Extra keys for tty access.
  11919. ;; We only set them when really needed because otherwise the
  11920. ;; menus don't show the simple keys
  11921. (when (or org-use-extra-keys
  11922. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11923. (not window-system))
  11924. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11925. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11926. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11927. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11928. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11929. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11930. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11931. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11932. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11933. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11934. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11935. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11936. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11937. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11938. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11939. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11940. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11941. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11942. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11943. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11944. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11945. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11946. ;; All the other keys
  11947. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11948. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11949. (if (boundp 'narrow-map)
  11950. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11951. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11952. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11953. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11954. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11955. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11956. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11957. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11958. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11959. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11960. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11961. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11962. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11963. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11964. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11965. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11966. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11967. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11968. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11969. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11970. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11971. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11972. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11973. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11974. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11975. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11976. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11977. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11978. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11979. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11980. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11981. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11982. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11983. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11984. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11985. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11986. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11987. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11988. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11989. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11990. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11991. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11992. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11993. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11994. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11995. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11996. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11997. (org-defkey org-mode-map "\C-m" 'org-return)
  11998. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11999. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12000. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12001. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12002. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12003. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12004. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12005. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12006. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12007. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12008. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12009. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12010. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12011. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12012. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12013. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12014. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12015. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12016. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12017. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12018. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12019. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12020. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12021. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12022. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12023. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12024. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12025. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12026. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12027. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12028. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12029. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12030. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12031. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12032. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12033. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12034. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12035. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12036. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12037. (when (featurep 'xemacs)
  12038. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12039. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12040. (defun org-self-insert-command (N)
  12041. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12042. If the cursor is in a table looking at whitespace, the whitespace is
  12043. overwritten, and the table is not marked as requiring realignment."
  12044. (interactive "p")
  12045. (if (and (org-table-p)
  12046. (progn
  12047. ;; check if we blank the field, and if that triggers align
  12048. (and (featurep 'org-table) org-table-auto-blank-field
  12049. (member last-command
  12050. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12051. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12052. ;; got extra space, this field does not determine column width
  12053. (let (org-table-may-need-update) (org-table-blank-field))
  12054. ;; no extra space, this field may determine column width
  12055. (org-table-blank-field)))
  12056. t)
  12057. (eq N 1)
  12058. (looking-at "[^|\n]* |"))
  12059. (let (org-table-may-need-update)
  12060. (goto-char (1- (match-end 0)))
  12061. (delete-backward-char 1)
  12062. (goto-char (match-beginning 0))
  12063. (self-insert-command N))
  12064. (setq org-table-may-need-update t)
  12065. (self-insert-command N)
  12066. (org-fix-tags-on-the-fly)))
  12067. (defun org-fix-tags-on-the-fly ()
  12068. (when (and (equal (char-after (point-at-bol)) ?*)
  12069. (org-on-heading-p))
  12070. (org-align-tags-here org-tags-column)))
  12071. (defun org-delete-backward-char (N)
  12072. "Like `delete-backward-char', insert whitespace at field end in tables.
  12073. When deleting backwards, in tables this function will insert whitespace in
  12074. front of the next \"|\" separator, to keep the table aligned. The table will
  12075. still be marked for re-alignment if the field did fill the entire column,
  12076. because, in this case the deletion might narrow the column."
  12077. (interactive "p")
  12078. (if (and (org-table-p)
  12079. (eq N 1)
  12080. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12081. (looking-at ".*?|"))
  12082. (let ((pos (point))
  12083. (noalign (looking-at "[^|\n\r]* |"))
  12084. (c org-table-may-need-update))
  12085. (backward-delete-char N)
  12086. (skip-chars-forward "^|")
  12087. (insert " ")
  12088. (goto-char (1- pos))
  12089. ;; noalign: if there were two spaces at the end, this field
  12090. ;; does not determine the width of the column.
  12091. (if noalign (setq org-table-may-need-update c)))
  12092. (backward-delete-char N)
  12093. (org-fix-tags-on-the-fly)))
  12094. (defun org-delete-char (N)
  12095. "Like `delete-char', but insert whitespace at field end in tables.
  12096. When deleting characters, in tables this function will insert whitespace in
  12097. front of the next \"|\" separator, to keep the table aligned. The table will
  12098. still be marked for re-alignment if the field did fill the entire column,
  12099. because, in this case the deletion might narrow the column."
  12100. (interactive "p")
  12101. (if (and (org-table-p)
  12102. (not (bolp))
  12103. (not (= (char-after) ?|))
  12104. (eq N 1))
  12105. (if (looking-at ".*?|")
  12106. (let ((pos (point))
  12107. (noalign (looking-at "[^|\n\r]* |"))
  12108. (c org-table-may-need-update))
  12109. (replace-match (concat
  12110. (substring (match-string 0) 1 -1)
  12111. " |"))
  12112. (goto-char pos)
  12113. ;; noalign: if there were two spaces at the end, this field
  12114. ;; does not determine the width of the column.
  12115. (if noalign (setq org-table-may-need-update c)))
  12116. (delete-char N))
  12117. (delete-char N)
  12118. (org-fix-tags-on-the-fly)))
  12119. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12120. (put 'org-self-insert-command 'delete-selection t)
  12121. (put 'orgtbl-self-insert-command 'delete-selection t)
  12122. (put 'org-delete-char 'delete-selection 'supersede)
  12123. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12124. (put 'org-yank 'delete-selection 'yank)
  12125. ;; Make `flyspell-mode' delay after some commands
  12126. (put 'org-self-insert-command 'flyspell-delayed t)
  12127. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12128. (put 'org-delete-char 'flyspell-delayed t)
  12129. (put 'org-delete-backward-char 'flyspell-delayed t)
  12130. ;; Make pabbrev-mode expand after org-mode commands
  12131. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12132. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12133. ;; How to do this: Measure non-white length of current string
  12134. ;; If equal to column width, we should realign.
  12135. (defun org-remap (map &rest commands)
  12136. "In MAP, remap the functions given in COMMANDS.
  12137. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12138. (let (new old)
  12139. (while commands
  12140. (setq old (pop commands) new (pop commands))
  12141. (if (fboundp 'command-remapping)
  12142. (org-defkey map (vector 'remap old) new)
  12143. (substitute-key-definition old new map global-map)))))
  12144. (when (eq org-enable-table-editor 'optimized)
  12145. ;; If the user wants maximum table support, we need to hijack
  12146. ;; some standard editing functions
  12147. (org-remap org-mode-map
  12148. 'self-insert-command 'org-self-insert-command
  12149. 'delete-char 'org-delete-char
  12150. 'delete-backward-char 'org-delete-backward-char)
  12151. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12152. (defun org-modifier-cursor-error ()
  12153. "Throw an error, a modified cursor command was applied in wrong context."
  12154. (error "This command is active in special context like tables, headlines or items"))
  12155. (defun org-shiftselect-error ()
  12156. "Throw an error because Shift-Cursor command was applied in wrong context."
  12157. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12158. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12159. (error "This command works only in special context like headlines or timestamps.")))
  12160. (defun org-call-for-shift-select (cmd)
  12161. (let ((this-command-keys-shift-translated t))
  12162. (call-interactively cmd)))
  12163. (defun org-shifttab (&optional arg)
  12164. "Global visibility cycling or move to previous table field.
  12165. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12166. on context.
  12167. See the individual commands for more information."
  12168. (interactive "P")
  12169. (cond
  12170. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12171. ((integerp arg)
  12172. (message "Content view to level: %d" arg)
  12173. (org-content (prefix-numeric-value arg))
  12174. (setq org-cycle-global-status 'overview))
  12175. (t (call-interactively 'org-global-cycle))))
  12176. (defun org-shiftmetaleft ()
  12177. "Promote subtree or delete table column.
  12178. Calls `org-promote-subtree', `org-outdent-item',
  12179. or `org-table-delete-column', depending on context.
  12180. See the individual commands for more information."
  12181. (interactive)
  12182. (cond
  12183. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12184. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12185. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12186. (t (org-modifier-cursor-error))))
  12187. (defun org-shiftmetaright ()
  12188. "Demote subtree or insert table column.
  12189. Calls `org-demote-subtree', `org-indent-item',
  12190. or `org-table-insert-column', depending on context.
  12191. See the individual commands for more information."
  12192. (interactive)
  12193. (cond
  12194. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12195. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12196. ((org-at-item-p) (call-interactively 'org-indent-item))
  12197. (t (org-modifier-cursor-error))))
  12198. (defun org-shiftmetaup (&optional arg)
  12199. "Move subtree up or kill table row.
  12200. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12201. `org-move-item-up' depending on context. See the individual commands
  12202. for more information."
  12203. (interactive "P")
  12204. (cond
  12205. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12206. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12207. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12208. (t (org-modifier-cursor-error))))
  12209. (defun org-shiftmetadown (&optional arg)
  12210. "Move subtree down or insert table row.
  12211. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12212. `org-move-item-down', depending on context. See the individual
  12213. commands for more information."
  12214. (interactive "P")
  12215. (cond
  12216. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12217. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12218. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12219. (t (org-modifier-cursor-error))))
  12220. (defun org-metaleft (&optional arg)
  12221. "Promote heading or move table column to left.
  12222. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12223. With no specific context, calls the Emacs default `backward-word'.
  12224. See the individual commands for more information."
  12225. (interactive "P")
  12226. (cond
  12227. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12228. ((or (org-on-heading-p) (org-region-active-p))
  12229. (call-interactively 'org-do-promote))
  12230. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12231. (t (call-interactively 'backward-word))))
  12232. (defun org-metaright (&optional arg)
  12233. "Demote subtree or move table column to right.
  12234. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12235. With no specific context, calls the Emacs default `forward-word'.
  12236. See the individual commands for more information."
  12237. (interactive "P")
  12238. (cond
  12239. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12240. ((or (org-on-heading-p) (org-region-active-p))
  12241. (call-interactively 'org-do-demote))
  12242. ((org-at-item-p) (call-interactively 'org-indent-item))
  12243. (t (call-interactively 'forward-word))))
  12244. (defun org-metaup (&optional arg)
  12245. "Move subtree up or move table row up.
  12246. Calls `org-move-subtree-up' or `org-table-move-row' or
  12247. `org-move-item-up', depending on context. See the individual commands
  12248. for more information."
  12249. (interactive "P")
  12250. (cond
  12251. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12252. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12253. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12254. (t (transpose-lines 1) (beginning-of-line -1))))
  12255. (defun org-metadown (&optional arg)
  12256. "Move subtree down or move table row down.
  12257. Calls `org-move-subtree-down' or `org-table-move-row' or
  12258. `org-move-item-down', depending on context. See the individual
  12259. commands for more information."
  12260. (interactive "P")
  12261. (cond
  12262. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12263. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12264. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12265. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12266. (defun org-shiftup (&optional arg)
  12267. "Increase item in timestamp or increase priority of current headline.
  12268. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12269. depending on context. See the individual commands for more information."
  12270. (interactive "P")
  12271. (cond
  12272. ((and org-support-shift-select (org-region-active-p))
  12273. (org-call-for-shift-select 'previous-line))
  12274. ((org-at-timestamp-p t)
  12275. (call-interactively (if org-edit-timestamp-down-means-later
  12276. 'org-timestamp-down 'org-timestamp-up)))
  12277. ((and (not (eq org-support-shift-select 'always))
  12278. (org-on-heading-p))
  12279. (call-interactively 'org-priority-up))
  12280. ((and (not org-support-shift-select) (org-at-item-p))
  12281. (call-interactively 'org-previous-item))
  12282. ((org-clocktable-try-shift 'up arg))
  12283. (org-support-shift-select
  12284. (org-call-for-shift-select 'previous-line))
  12285. (t (org-shiftselect-error))))
  12286. (defun org-shiftdown (&optional arg)
  12287. "Decrease item in timestamp or decrease priority of current headline.
  12288. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12289. depending on context. See the individual commands for more information."
  12290. (interactive "P")
  12291. (cond
  12292. ((and org-support-shift-select (org-region-active-p))
  12293. (org-call-for-shift-select 'next-line))
  12294. ((org-at-timestamp-p t)
  12295. (call-interactively (if org-edit-timestamp-down-means-later
  12296. 'org-timestamp-up 'org-timestamp-down)))
  12297. ((and (not (eq org-support-shift-select 'always))
  12298. (org-on-heading-p))
  12299. (call-interactively 'org-priority-down))
  12300. ((and (not org-support-shift-select) (org-at-item-p))
  12301. (call-interactively 'org-next-item))
  12302. ((org-clocktable-try-shift 'down arg))
  12303. (org-support-shift-select
  12304. (org-call-for-shift-select 'next-line))
  12305. (t (org-shiftselect-error))))
  12306. (defun org-shiftright (&optional arg)
  12307. "Cycle the thing at point or in the current line, depending on context.
  12308. Depending on context, this does one of the following:
  12309. - switch a timestamp at point one day into the future
  12310. - on a headline, switch to the next TODO keyword.
  12311. - on an item, switch entire list to the next bullet type
  12312. - on a property line, switch to the next allowed value
  12313. - on a clocktable definition line, move time block into the future"
  12314. (interactive "P")
  12315. (cond
  12316. ((and org-support-shift-select (org-region-active-p))
  12317. (org-call-for-shift-select 'forward-char))
  12318. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12319. ((and (not (eq org-support-shift-select 'always))
  12320. (org-on-heading-p))
  12321. (org-call-with-arg 'org-todo 'right))
  12322. ((or (and org-support-shift-select
  12323. (not (eq org-support-shift-select 'always))
  12324. (org-at-item-bullet-p))
  12325. (and (not org-support-shift-select) (org-at-item-p)))
  12326. (org-call-with-arg 'org-cycle-list-bullet nil))
  12327. ((and (not (eq org-support-shift-select 'always))
  12328. (org-at-property-p))
  12329. (call-interactively 'org-property-next-allowed-value))
  12330. ((org-clocktable-try-shift 'right arg))
  12331. (org-support-shift-select
  12332. (org-call-for-shift-select 'forward-char))
  12333. (t (org-shiftselect-error))))
  12334. (defun org-shiftleft (&optional arg)
  12335. "Cycle the thing at point or in the current line, depending on context.
  12336. Depending on context, this does one of the following:
  12337. - switch a timestamp at point one day into the past
  12338. - on a headline, switch to the previous TODO keyword.
  12339. - on an item, switch entire list to the previous bullet type
  12340. - on a property line, switch to the previous allowed value
  12341. - on a clocktable definition line, move time block into the past"
  12342. (interactive "P")
  12343. (cond
  12344. ((and org-support-shift-select (org-region-active-p))
  12345. (org-call-for-shift-select 'backward-char))
  12346. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12347. ((and (not (eq org-support-shift-select 'always))
  12348. (org-on-heading-p))
  12349. (org-call-with-arg 'org-todo 'left))
  12350. ((or (and org-support-shift-select
  12351. (not (eq org-support-shift-select 'always))
  12352. (org-at-item-bullet-p))
  12353. (and (not org-support-shift-select) (org-at-item-p)))
  12354. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12355. ((and (not (eq org-support-shift-select 'always))
  12356. (org-at-property-p))
  12357. (call-interactively 'org-property-previous-allowed-value))
  12358. ((org-clocktable-try-shift 'left arg))
  12359. (org-support-shift-select
  12360. (org-call-for-shift-select 'backward-char))
  12361. (t (org-shiftselect-error))))
  12362. (defun org-shiftcontrolright ()
  12363. "Switch to next TODO set."
  12364. (interactive)
  12365. (cond
  12366. ((and org-support-shift-select (org-region-active-p))
  12367. (org-call-for-shift-select 'forward-word))
  12368. ((and (not (eq org-support-shift-select 'always))
  12369. (org-on-heading-p))
  12370. (org-call-with-arg 'org-todo 'nextset))
  12371. (org-support-shift-select
  12372. (org-call-for-shift-select 'forward-word))
  12373. (t (org-shiftselect-error))))
  12374. (defun org-shiftcontrolleft ()
  12375. "Switch to previous TODO set."
  12376. (interactive)
  12377. (cond
  12378. ((and org-support-shift-select (org-region-active-p))
  12379. (org-call-for-shift-select 'backward-word))
  12380. ((and (not (eq org-support-shift-select 'always))
  12381. (org-on-heading-p))
  12382. (org-call-with-arg 'org-todo 'previousset))
  12383. (org-support-shift-select
  12384. (org-call-for-shift-select 'backward-word))
  12385. (t (org-shiftselect-error))))
  12386. (defun org-ctrl-c-ret ()
  12387. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12388. (interactive)
  12389. (cond
  12390. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12391. (t (call-interactively 'org-insert-heading))))
  12392. (defun org-copy-special ()
  12393. "Copy region in table or copy current subtree.
  12394. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12395. See the individual commands for more information."
  12396. (interactive)
  12397. (call-interactively
  12398. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12399. (defun org-cut-special ()
  12400. "Cut region in table or cut current subtree.
  12401. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12402. See the individual commands for more information."
  12403. (interactive)
  12404. (call-interactively
  12405. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12406. (defun org-paste-special (arg)
  12407. "Paste rectangular region into table, or past subtree relative to level.
  12408. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12409. See the individual commands for more information."
  12410. (interactive "P")
  12411. (if (org-at-table-p)
  12412. (org-table-paste-rectangle)
  12413. (org-paste-subtree arg)))
  12414. (defun org-edit-special ()
  12415. "Call a special editor for the stuff at point.
  12416. When at a table, call the formula editor with `org-table-edit-formulas'.
  12417. When at the first line of an src example, call `org-edit-src-code'.
  12418. When in an #+include line, visit the include file. Otherwise call
  12419. `ffap' to visit the file at point."
  12420. (interactive)
  12421. (cond
  12422. ((org-at-table-p)
  12423. (call-interactively 'org-table-edit-formulas))
  12424. ((save-excursion
  12425. (beginning-of-line 1)
  12426. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12427. (find-file (org-trim (match-string 1))))
  12428. ((org-edit-src-code))
  12429. ((org-edit-fixed-width-region))
  12430. (t (call-interactively 'ffap))))
  12431. (defun org-ctrl-c-ctrl-c (&optional arg)
  12432. "Set tags in headline, or update according to changed information at point.
  12433. This command does many different things, depending on context:
  12434. - If the cursor is in a headline, prompt for tags and insert them
  12435. into the current line, aligned to `org-tags-column'. When called
  12436. with prefix arg, realign all tags in the current buffer.
  12437. - If the cursor is in one of the special #+KEYWORD lines, this
  12438. triggers scanning the buffer for these lines and updating the
  12439. information.
  12440. - If the cursor is inside a table, realign the table. This command
  12441. works even if the automatic table editor has been turned off.
  12442. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12443. the entire table.
  12444. - If the cursor is at a footnote reference or definition, jump to
  12445. the corresponding definition or references, respectively.
  12446. - If the cursor is a the beginning of a dynamic block, update it.
  12447. - If the cursor is inside a table created by the table.el package,
  12448. activate that table.
  12449. - If the current buffer is a remember buffer, close note and file
  12450. it. A prefix argument of 1 files to the default location
  12451. without further interaction. A prefix argument of 2 files to
  12452. the currently clocking task.
  12453. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12454. links in this buffer.
  12455. - If the cursor is on a numbered item in a plain list, renumber the
  12456. ordered list.
  12457. - If the cursor is on a checkbox, toggle it."
  12458. (interactive "P")
  12459. (let ((org-enable-table-editor t))
  12460. (cond
  12461. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12462. org-occur-highlights
  12463. org-latex-fragment-image-overlays)
  12464. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12465. (org-remove-occur-highlights)
  12466. (org-remove-latex-fragment-image-overlays)
  12467. (message "Temporary highlights/overlays removed from current buffer"))
  12468. ((and (local-variable-p 'org-finish-function (current-buffer))
  12469. (fboundp org-finish-function))
  12470. (funcall org-finish-function))
  12471. ((org-at-property-p)
  12472. (call-interactively 'org-property-action))
  12473. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12474. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12475. ((org-at-table.el-p)
  12476. (require 'table)
  12477. (beginning-of-line 1)
  12478. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12479. (call-interactively 'table-recognize-table))
  12480. ((org-at-table-p)
  12481. (org-table-maybe-eval-formula)
  12482. (if arg
  12483. (call-interactively 'org-table-recalculate)
  12484. (org-table-maybe-recalculate-line))
  12485. (call-interactively 'org-table-align))
  12486. ((or (org-footnote-at-reference-p)
  12487. (org-footnote-at-definition-p))
  12488. (call-interactively 'org-footnote-action))
  12489. ((org-at-item-checkbox-p)
  12490. (call-interactively 'org-toggle-checkbox))
  12491. ((org-at-item-p)
  12492. (call-interactively 'org-maybe-renumber-ordered-list))
  12493. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12494. ;; Dynamic block
  12495. (beginning-of-line 1)
  12496. (save-excursion (org-update-dblock)))
  12497. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12498. (cond
  12499. ((equal (match-string 1) "TBLFM")
  12500. ;; Recalculate the table before this line
  12501. (save-excursion
  12502. (beginning-of-line 1)
  12503. (skip-chars-backward " \r\n\t")
  12504. (if (org-at-table-p)
  12505. (org-call-with-arg 'org-table-recalculate t))))
  12506. (t
  12507. ; (org-set-regexps-and-options)
  12508. ; (org-restart-font-lock)
  12509. (let ((org-inhibit-startup t)) (org-mode-restart))
  12510. (message "Local setup has been refreshed"))))
  12511. (t (error "C-c C-c can do nothing useful at this location.")))))
  12512. (defun org-mode-restart ()
  12513. "Restart Org-mode, to scan again for special lines.
  12514. Also updates the keyword regular expressions."
  12515. (interactive)
  12516. (org-mode)
  12517. (message "Org-mode restarted"))
  12518. (defun org-kill-note-or-show-branches ()
  12519. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12520. (interactive)
  12521. (if (not org-finish-function)
  12522. (call-interactively 'show-branches)
  12523. (let ((org-note-abort t))
  12524. (funcall org-finish-function))))
  12525. (defun org-return (&optional indent)
  12526. "Goto next table row or insert a newline.
  12527. Calls `org-table-next-row' or `newline', depending on context.
  12528. See the individual commands for more information."
  12529. (interactive)
  12530. (cond
  12531. ((bobp) (if indent (newline-and-indent) (newline)))
  12532. ((org-at-table-p)
  12533. (org-table-justify-field-maybe)
  12534. (call-interactively 'org-table-next-row))
  12535. ((and org-return-follows-link
  12536. (eq (get-text-property (point) 'face) 'org-link))
  12537. (call-interactively 'org-open-at-point))
  12538. ((and (org-at-heading-p)
  12539. (looking-at
  12540. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12541. (org-show-entry)
  12542. (end-of-line 1)
  12543. (newline))
  12544. (t (if indent (newline-and-indent) (newline)))))
  12545. (defun org-return-indent ()
  12546. "Goto next table row or insert a newline and indent.
  12547. Calls `org-table-next-row' or `newline-and-indent', depending on
  12548. context. See the individual commands for more information."
  12549. (interactive)
  12550. (org-return t))
  12551. (defun org-ctrl-c-star ()
  12552. "Compute table, or change heading status of lines.
  12553. Calls `org-table-recalculate' or `org-toggle-heading',
  12554. depending on context."
  12555. (interactive)
  12556. (cond
  12557. ((org-at-table-p)
  12558. (call-interactively 'org-table-recalculate))
  12559. (t
  12560. ;; Convert all lines in region to list items
  12561. (call-interactively 'org-toggle-heading))))
  12562. (defun org-ctrl-c-minus ()
  12563. "Insert separator line in table or modify bullet status of line.
  12564. Also turns a plain line or a region of lines into list items.
  12565. Calls `org-table-insert-hline', `org-toggle-item', or
  12566. `org-cycle-list-bullet', depending on context."
  12567. (interactive)
  12568. (cond
  12569. ((org-at-table-p)
  12570. (call-interactively 'org-table-insert-hline))
  12571. ((org-region-active-p)
  12572. (call-interactively 'org-toggle-item))
  12573. ((org-in-item-p)
  12574. (call-interactively 'org-cycle-list-bullet))
  12575. (t
  12576. (call-interactively 'org-toggle-item))))
  12577. (defun org-toggle-item ()
  12578. "Convert headings or normal lines to items, items to normal lines.
  12579. If there is no active region, only the current line is considered.
  12580. If the first line in the region is a headline, convert all headlines to items.
  12581. If the first line in the region is an item, convert all items to normal lines.
  12582. If the first line is normal text, add an item bullet to each line."
  12583. (interactive)
  12584. (let (l2 l beg end)
  12585. (if (org-region-active-p)
  12586. (setq beg (region-beginning) end (region-end))
  12587. (setq beg (point-at-bol)
  12588. end (min (1+ (point-at-eol)) (point-max))))
  12589. (save-excursion
  12590. (goto-char end)
  12591. (setq l2 (org-current-line))
  12592. (goto-char beg)
  12593. (beginning-of-line 1)
  12594. (setq l (1- (org-current-line)))
  12595. (if (org-at-item-p)
  12596. ;; We already have items, de-itemize
  12597. (while (< (setq l (1+ l)) l2)
  12598. (when (org-at-item-p)
  12599. (goto-char (match-beginning 2))
  12600. (delete-region (match-beginning 2) (match-end 2))
  12601. (and (looking-at "[ \t]+") (replace-match "")))
  12602. (beginning-of-line 2))
  12603. (if (org-on-heading-p)
  12604. ;; Headings, convert to items
  12605. (while (< (setq l (1+ l)) l2)
  12606. (if (looking-at org-outline-regexp)
  12607. (replace-match "- " t t))
  12608. (beginning-of-line 2))
  12609. ;; normal lines, turn them into items
  12610. (while (< (setq l (1+ l)) l2)
  12611. (unless (org-at-item-p)
  12612. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12613. (replace-match "\\1- \\2")))
  12614. (beginning-of-line 2)))))))
  12615. (defun org-toggle-heading (&optional nstars)
  12616. "Convert headings to normal text, or items or text to headings.
  12617. If there is no active region, only the current line is considered.
  12618. If the first line is a heading, remove the stars from all headlines
  12619. in the region.
  12620. If the first line is a plain list item, turn all plain list items into
  12621. headings.
  12622. If the first line is a normal line, turn each and every line in the region
  12623. into a heading.
  12624. When converting a line into a heading, the number of stars is chosen
  12625. such that the lines become children of the current entry. However, when
  12626. a prefix argument is given, its value determines the number of stars to add."
  12627. (interactive "P")
  12628. (let (l2 l itemp beg end)
  12629. (if (org-region-active-p)
  12630. (setq beg (region-beginning) end (region-end))
  12631. (setq beg (point-at-bol)
  12632. end (min (1+ (point-at-eol)) (point-max))))
  12633. (save-excursion
  12634. (goto-char end)
  12635. (setq l2 (org-current-line))
  12636. (goto-char beg)
  12637. (beginning-of-line 1)
  12638. (setq l (1- (org-current-line)))
  12639. (if (org-on-heading-p)
  12640. ;; We already have headlines, de-star them
  12641. (while (< (setq l (1+ l)) l2)
  12642. (when (org-on-heading-p t)
  12643. (and (looking-at outline-regexp) (replace-match "")))
  12644. (beginning-of-line 2))
  12645. (setq itemp (org-at-item-p))
  12646. (let* ((stars
  12647. (if nstars
  12648. (make-string (prefix-numeric-value current-prefix-arg)
  12649. ?*)
  12650. (save-excursion
  12651. (re-search-backward org-complex-heading-regexp nil t)
  12652. (or (match-string 1) "*"))))
  12653. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12654. (rpl (concat stars add-stars " ")))
  12655. (while (< (setq l (1+ l)) l2)
  12656. (if itemp
  12657. (and (org-at-item-p) (replace-match rpl t t))
  12658. (unless (org-on-heading-p)
  12659. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12660. (replace-match (concat rpl (match-string 2))))))
  12661. (beginning-of-line 2)))))))
  12662. (defun org-meta-return (&optional arg)
  12663. "Insert a new heading or wrap a region in a table.
  12664. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12665. See the individual commands for more information."
  12666. (interactive "P")
  12667. (cond
  12668. ((org-at-table-p)
  12669. (call-interactively 'org-table-wrap-region))
  12670. (t (call-interactively 'org-insert-heading))))
  12671. ;;; Menu entries
  12672. ;; Define the Org-mode menus
  12673. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12674. '("Tbl"
  12675. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12676. ["Next Field" org-cycle (org-at-table-p)]
  12677. ["Previous Field" org-shifttab (org-at-table-p)]
  12678. ["Next Row" org-return (org-at-table-p)]
  12679. "--"
  12680. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12681. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12682. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12683. "--"
  12684. ("Column"
  12685. ["Move Column Left" org-metaleft (org-at-table-p)]
  12686. ["Move Column Right" org-metaright (org-at-table-p)]
  12687. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12688. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12689. ("Row"
  12690. ["Move Row Up" org-metaup (org-at-table-p)]
  12691. ["Move Row Down" org-metadown (org-at-table-p)]
  12692. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12693. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12694. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12695. "--"
  12696. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12697. ("Rectangle"
  12698. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12699. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12700. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12701. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12702. "--"
  12703. ("Calculate"
  12704. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12705. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12706. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12707. "--"
  12708. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12709. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12710. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12711. "--"
  12712. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12713. "--"
  12714. ["Sum Column/Rectangle" org-table-sum
  12715. (or (org-at-table-p) (org-region-active-p))]
  12716. ["Which Column?" org-table-current-column (org-at-table-p)])
  12717. ["Debug Formulas"
  12718. org-table-toggle-formula-debugger
  12719. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12720. ["Show Col/Row Numbers"
  12721. org-table-toggle-coordinate-overlays
  12722. :style toggle
  12723. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12724. "--"
  12725. ["Create" org-table-create (and (not (org-at-table-p))
  12726. org-enable-table-editor)]
  12727. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12728. ["Import from File" org-table-import (not (org-at-table-p))]
  12729. ["Export to File" org-table-export (org-at-table-p)]
  12730. "--"
  12731. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12732. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12733. '("Org"
  12734. ("Show/Hide"
  12735. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12736. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12737. ["Sparse Tree..." org-sparse-tree t]
  12738. ["Reveal Context" org-reveal t]
  12739. ["Show All" show-all t]
  12740. "--"
  12741. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12742. "--"
  12743. ["New Heading" org-insert-heading t]
  12744. ("Navigate Headings"
  12745. ["Up" outline-up-heading t]
  12746. ["Next" outline-next-visible-heading t]
  12747. ["Previous" outline-previous-visible-heading t]
  12748. ["Next Same Level" outline-forward-same-level t]
  12749. ["Previous Same Level" outline-backward-same-level t]
  12750. "--"
  12751. ["Jump" org-goto t])
  12752. ("Edit Structure"
  12753. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12754. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12755. "--"
  12756. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12757. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12758. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12759. "--"
  12760. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12761. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12762. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12763. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12764. "--"
  12765. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12766. "--"
  12767. ["Convert to odd levels" org-convert-to-odd-levels t]
  12768. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12769. ("Editing"
  12770. ["Emphasis..." org-emphasize t]
  12771. ["Edit Source Example" org-edit-special t]
  12772. "--"
  12773. ["Footnote new/jump" org-footnote-action t]
  12774. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12775. ("Archive"
  12776. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12777. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12778. ; :active t :keys "C-u C-c C-x C-a"]
  12779. ["Sparse trees open ARCHIVE trees"
  12780. (setq org-sparse-tree-open-archived-trees
  12781. (not org-sparse-tree-open-archived-trees))
  12782. :style toggle :selected org-sparse-tree-open-archived-trees]
  12783. ["Cycling opens ARCHIVE trees"
  12784. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12785. :style toggle :selected org-cycle-open-archived-trees]
  12786. "--"
  12787. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12788. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12789. ; ["Check and Move Children" (org-archive-subtree '(4))
  12790. ; :active t :keys "C-u C-c C-x C-s"]
  12791. )
  12792. "--"
  12793. ("TODO Lists"
  12794. ["TODO/DONE/-" org-todo t]
  12795. ("Select keyword"
  12796. ["Next keyword" org-shiftright (org-on-heading-p)]
  12797. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12798. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12799. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12800. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12801. ["Show TODO Tree" org-show-todo-tree t]
  12802. ["Global TODO list" org-todo-list t]
  12803. "--"
  12804. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  12805. :selected org-enforce-todo-dependencies :style toggle :active t]
  12806. "Settings for tree at point"
  12807. ["Do Children sequentially" org-toggle-ordered-property :style radio
  12808. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  12809. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12810. ["Do Children parallel" org-toggle-ordered-property :style radio
  12811. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  12812. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12813. "--"
  12814. ["Set Priority" org-priority t]
  12815. ["Priority Up" org-shiftup t]
  12816. ["Priority Down" org-shiftdown t])
  12817. ("TAGS and Properties"
  12818. ["Set Tags" org-set-tags-command t]
  12819. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12820. "--"
  12821. ["Set property" org-set-property t]
  12822. ["Column view of properties" org-columns t]
  12823. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12824. ("Dates and Scheduling"
  12825. ["Timestamp" org-time-stamp t]
  12826. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12827. ("Change Date"
  12828. ["1 Day Later" org-shiftright t]
  12829. ["1 Day Earlier" org-shiftleft t]
  12830. ["1 ... Later" org-shiftup t]
  12831. ["1 ... Earlier" org-shiftdown t])
  12832. ["Compute Time Range" org-evaluate-time-range t]
  12833. ["Schedule Item" org-schedule t]
  12834. ["Deadline" org-deadline t]
  12835. "--"
  12836. ["Custom time format" org-toggle-time-stamp-overlays
  12837. :style radio :selected org-display-custom-times]
  12838. "--"
  12839. ["Goto Calendar" org-goto-calendar t]
  12840. ["Date from Calendar" org-date-from-calendar t]
  12841. "--"
  12842. ["Start/Restart Timer" org-timer-start t]
  12843. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  12844. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  12845. ["Insert Timer String" org-timer t]
  12846. ["Insert Timer Item" org-timer-item t])
  12847. ("Logging work"
  12848. ["Clock in" org-clock-in t]
  12849. ["Clock out" org-clock-out t]
  12850. ["Clock cancel" org-clock-cancel t]
  12851. ["Goto running clock" org-clock-goto t]
  12852. ["Display times" org-clock-display t]
  12853. ["Create clock table" org-clock-report t]
  12854. "--"
  12855. ["Record DONE time"
  12856. (progn (setq org-log-done (not org-log-done))
  12857. (message "Switching to %s will %s record a timestamp"
  12858. (car org-done-keywords)
  12859. (if org-log-done "automatically" "not")))
  12860. :style toggle :selected org-log-done])
  12861. "--"
  12862. ["Agenda Command..." org-agenda t]
  12863. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12864. ("File List for Agenda")
  12865. ("Special views current file"
  12866. ["TODO Tree" org-show-todo-tree t]
  12867. ["Check Deadlines" org-check-deadlines t]
  12868. ["Timeline" org-timeline t]
  12869. ["Tags Tree" org-tags-sparse-tree t])
  12870. "--"
  12871. ("Hyperlinks"
  12872. ["Store Link (Global)" org-store-link t]
  12873. ["Insert Link" org-insert-link t]
  12874. ["Follow Link" org-open-at-point t]
  12875. "--"
  12876. ["Next link" org-next-link t]
  12877. ["Previous link" org-previous-link t]
  12878. "--"
  12879. ["Descriptive Links"
  12880. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12881. :style radio
  12882. :selected (member '(org-link) buffer-invisibility-spec)]
  12883. ["Literal Links"
  12884. (progn
  12885. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12886. :style radio
  12887. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12888. "--"
  12889. ["Export/Publish..." org-export t]
  12890. ("LaTeX"
  12891. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12892. :selected org-cdlatex-mode]
  12893. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12894. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12895. ["Modify math symbol" org-cdlatex-math-modify
  12896. (org-inside-LaTeX-fragment-p)]
  12897. ["Export LaTeX fragments as images"
  12898. (if (featurep 'org-exp)
  12899. (setq org-export-with-LaTeX-fragments
  12900. (not org-export-with-LaTeX-fragments))
  12901. (require 'org-exp))
  12902. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12903. org-export-with-LaTeX-fragments)])
  12904. "--"
  12905. ("Documentation"
  12906. ["Show Version" org-version t]
  12907. ["Info Documentation" org-info t])
  12908. ("Customize"
  12909. ["Browse Org Group" org-customize t]
  12910. "--"
  12911. ["Expand This Menu" org-create-customize-menu
  12912. (fboundp 'customize-menu-create)])
  12913. "--"
  12914. ["Refresh setup" org-mode-restart t]
  12915. ))
  12916. (defun org-info (&optional node)
  12917. "Read documentation for Org-mode in the info system.
  12918. With optional NODE, go directly to that node."
  12919. (interactive)
  12920. (info (format "(org)%s" (or node ""))))
  12921. (defun org-install-agenda-files-menu ()
  12922. (let ((bl (buffer-list)))
  12923. (save-excursion
  12924. (while bl
  12925. (set-buffer (pop bl))
  12926. (if (org-mode-p) (setq bl nil)))
  12927. (when (org-mode-p)
  12928. (easy-menu-change
  12929. '("Org") "File List for Agenda"
  12930. (append
  12931. (list
  12932. ["Edit File List" (org-edit-agenda-file-list) t]
  12933. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12934. ["Remove Current File from List" org-remove-file t]
  12935. ["Cycle through agenda files" org-cycle-agenda-files t]
  12936. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12937. "--")
  12938. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12939. ;;;; Documentation
  12940. ;;;###autoload
  12941. (defun org-require-autoloaded-modules ()
  12942. (interactive)
  12943. (mapc 'require
  12944. '(org-agenda org-archive org-attach org-clock org-colview
  12945. org-exp org-id org-export-latex org-publish
  12946. org-remember org-table org-timer)))
  12947. ;;;###autoload
  12948. (defun org-customize ()
  12949. "Call the customize function with org as argument."
  12950. (interactive)
  12951. (org-load-modules-maybe)
  12952. (org-require-autoloaded-modules)
  12953. (customize-browse 'org))
  12954. (defun org-create-customize-menu ()
  12955. "Create a full customization menu for Org-mode, insert it into the menu."
  12956. (interactive)
  12957. (org-load-modules-maybe)
  12958. (org-require-autoloaded-modules)
  12959. (if (fboundp 'customize-menu-create)
  12960. (progn
  12961. (easy-menu-change
  12962. '("Org") "Customize"
  12963. `(["Browse Org group" org-customize t]
  12964. "--"
  12965. ,(customize-menu-create 'org)
  12966. ["Set" Custom-set t]
  12967. ["Save" Custom-save t]
  12968. ["Reset to Current" Custom-reset-current t]
  12969. ["Reset to Saved" Custom-reset-saved t]
  12970. ["Reset to Standard Settings" Custom-reset-standard t]))
  12971. (message "\"Org\"-menu now contains full customization menu"))
  12972. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12973. ;;;; Miscellaneous stuff
  12974. ;;; Generally useful functions
  12975. (defun org-find-text-property-in-string (prop s)
  12976. "Return the first non-nil value of property PROP in string S."
  12977. (or (get-text-property 0 prop s)
  12978. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12979. prop s)))
  12980. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12981. "Display the given MESSAGE as a warning."
  12982. (if (fboundp 'display-warning)
  12983. (display-warning 'org message
  12984. (if (featurep 'xemacs)
  12985. 'warning
  12986. :warning))
  12987. (let ((buf (get-buffer-create "*Org warnings*")))
  12988. (with-current-buffer buf
  12989. (goto-char (point-max))
  12990. (insert "Warning (Org): " message)
  12991. (unless (bolp)
  12992. (newline)))
  12993. (display-buffer buf)
  12994. (sit-for 0))))
  12995. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12996. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12997. (if (and marker (marker-buffer marker)
  12998. (buffer-live-p (marker-buffer marker)))
  12999. (progn
  13000. (switch-to-buffer (marker-buffer marker))
  13001. (if (or (> marker (point-max)) (< marker (point-min)))
  13002. (widen))
  13003. (goto-char marker)
  13004. (org-show-context 'org-goto))
  13005. (if bookmark
  13006. (bookmark-jump bookmark)
  13007. (error "Cannot find location"))))
  13008. (defun org-quote-csv-field (s)
  13009. "Quote field for inclusion in CSV material."
  13010. (if (string-match "[\",]" s)
  13011. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13012. s))
  13013. (defun org-plist-delete (plist property)
  13014. "Delete PROPERTY from PLIST.
  13015. This is in contrast to merely setting it to 0."
  13016. (let (p)
  13017. (while plist
  13018. (if (not (eq property (car plist)))
  13019. (setq p (plist-put p (car plist) (nth 1 plist))))
  13020. (setq plist (cddr plist)))
  13021. p))
  13022. (defun org-force-self-insert (N)
  13023. "Needed to enforce self-insert under remapping."
  13024. (interactive "p")
  13025. (self-insert-command N))
  13026. (defun org-string-width (s)
  13027. "Compute width of string, ignoring invisible characters.
  13028. This ignores character with invisibility property `org-link', and also
  13029. characters with property `org-cwidth', because these will become invisible
  13030. upon the next fontification round."
  13031. (let (b l)
  13032. (when (or (eq t buffer-invisibility-spec)
  13033. (assq 'org-link buffer-invisibility-spec))
  13034. (while (setq b (text-property-any 0 (length s)
  13035. 'invisible 'org-link s))
  13036. (setq s (concat (substring s 0 b)
  13037. (substring s (or (next-single-property-change
  13038. b 'invisible s) (length s)))))))
  13039. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13040. (setq s (concat (substring s 0 b)
  13041. (substring s (or (next-single-property-change
  13042. b 'org-cwidth s) (length s))))))
  13043. (setq l (string-width s) b -1)
  13044. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13045. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13046. l))
  13047. (defun org-get-indentation (&optional line)
  13048. "Get the indentation of the current line, interpreting tabs.
  13049. When LINE is given, assume it represents a line and compute its indentation."
  13050. (if line
  13051. (if (string-match "^ *" (org-remove-tabs line))
  13052. (match-end 0))
  13053. (save-excursion
  13054. (beginning-of-line 1)
  13055. (skip-chars-forward " \t")
  13056. (current-column))))
  13057. (defun org-remove-tabs (s &optional width)
  13058. "Replace tabulators in S with spaces.
  13059. Assumes that s is a single line, starting in column 0."
  13060. (setq width (or width tab-width))
  13061. (while (string-match "\t" s)
  13062. (setq s (replace-match
  13063. (make-string
  13064. (- (* width (/ (+ (match-beginning 0) width) width))
  13065. (match-beginning 0)) ?\ )
  13066. t t s)))
  13067. s)
  13068. (defun org-fix-indentation (line ind)
  13069. "Fix indentation in LINE.
  13070. IND is a cons cell with target and minimum indentation.
  13071. If the current indentation in LINE is smaller than the minimum,
  13072. leave it alone. If it is larger than ind, set it to the target."
  13073. (let* ((l (org-remove-tabs line))
  13074. (i (org-get-indentation l))
  13075. (i1 (car ind)) (i2 (cdr ind)))
  13076. (if (>= i i2) (setq l (substring line i2)))
  13077. (if (> i1 0)
  13078. (concat (make-string i1 ?\ ) l)
  13079. l)))
  13080. (defun org-base-buffer (buffer)
  13081. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13082. (if (not buffer)
  13083. buffer
  13084. (or (buffer-base-buffer buffer)
  13085. buffer)))
  13086. (defun org-trim (s)
  13087. "Remove whitespace at beginning and end of string."
  13088. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13089. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13090. s)
  13091. (defun org-wrap (string &optional width lines)
  13092. "Wrap string to either a number of lines, or a width in characters.
  13093. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13094. that costs. If there is a word longer than WIDTH, the text is actually
  13095. wrapped to the length of that word.
  13096. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13097. many lines, whatever width that takes.
  13098. The return value is a list of lines, without newlines at the end."
  13099. (let* ((words (org-split-string string "[ \t\n]+"))
  13100. (maxword (apply 'max (mapcar 'org-string-width words)))
  13101. w ll)
  13102. (cond (width
  13103. (org-do-wrap words (max maxword width)))
  13104. (lines
  13105. (setq w maxword)
  13106. (setq ll (org-do-wrap words maxword))
  13107. (if (<= (length ll) lines)
  13108. ll
  13109. (setq ll words)
  13110. (while (> (length ll) lines)
  13111. (setq w (1+ w))
  13112. (setq ll (org-do-wrap words w)))
  13113. ll))
  13114. (t (error "Cannot wrap this")))))
  13115. (defun org-do-wrap (words width)
  13116. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13117. (let (lines line)
  13118. (while words
  13119. (setq line (pop words))
  13120. (while (and words (< (+ (length line) (length (car words))) width))
  13121. (setq line (concat line " " (pop words))))
  13122. (setq lines (push line lines)))
  13123. (nreverse lines)))
  13124. (defun org-split-string (string &optional separators)
  13125. "Splits STRING into substrings at SEPARATORS.
  13126. No empty strings are returned if there are matches at the beginning
  13127. and end of string."
  13128. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13129. (start 0)
  13130. notfirst
  13131. (list nil))
  13132. (while (and (string-match rexp string
  13133. (if (and notfirst
  13134. (= start (match-beginning 0))
  13135. (< start (length string)))
  13136. (1+ start) start))
  13137. (< (match-beginning 0) (length string)))
  13138. (setq notfirst t)
  13139. (or (eq (match-beginning 0) 0)
  13140. (and (eq (match-beginning 0) (match-end 0))
  13141. (eq (match-beginning 0) start))
  13142. (setq list
  13143. (cons (substring string start (match-beginning 0))
  13144. list)))
  13145. (setq start (match-end 0)))
  13146. (or (eq start (length string))
  13147. (setq list
  13148. (cons (substring string start)
  13149. list)))
  13150. (nreverse list)))
  13151. (defun org-context ()
  13152. "Return a list of contexts of the current cursor position.
  13153. If several contexts apply, all are returned.
  13154. Each context entry is a list with a symbol naming the context, and
  13155. two positions indicating start and end of the context. Possible
  13156. contexts are:
  13157. :headline anywhere in a headline
  13158. :headline-stars on the leading stars in a headline
  13159. :todo-keyword on a TODO keyword (including DONE) in a headline
  13160. :tags on the TAGS in a headline
  13161. :priority on the priority cookie in a headline
  13162. :item on the first line of a plain list item
  13163. :item-bullet on the bullet/number of a plain list item
  13164. :checkbox on the checkbox in a plain list item
  13165. :table in an org-mode table
  13166. :table-special on a special filed in a table
  13167. :table-table in a table.el table
  13168. :link on a hyperlink
  13169. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13170. :target on a <<target>>
  13171. :radio-target on a <<<radio-target>>>
  13172. :latex-fragment on a LaTeX fragment
  13173. :latex-preview on a LaTeX fragment with overlayed preview image
  13174. This function expects the position to be visible because it uses font-lock
  13175. faces as a help to recognize the following contexts: :table-special, :link,
  13176. and :keyword."
  13177. (let* ((f (get-text-property (point) 'face))
  13178. (faces (if (listp f) f (list f)))
  13179. (p (point)) clist o)
  13180. ;; First the large context
  13181. (cond
  13182. ((org-on-heading-p t)
  13183. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13184. (when (progn
  13185. (beginning-of-line 1)
  13186. (looking-at org-todo-line-tags-regexp))
  13187. (push (org-point-in-group p 1 :headline-stars) clist)
  13188. (push (org-point-in-group p 2 :todo-keyword) clist)
  13189. (push (org-point-in-group p 4 :tags) clist))
  13190. (goto-char p)
  13191. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13192. (if (looking-at "\\[#[A-Z0-9]\\]")
  13193. (push (org-point-in-group p 0 :priority) clist)))
  13194. ((org-at-item-p)
  13195. (push (org-point-in-group p 2 :item-bullet) clist)
  13196. (push (list :item (point-at-bol)
  13197. (save-excursion (org-end-of-item) (point)))
  13198. clist)
  13199. (and (org-at-item-checkbox-p)
  13200. (push (org-point-in-group p 0 :checkbox) clist)))
  13201. ((org-at-table-p)
  13202. (push (list :table (org-table-begin) (org-table-end)) clist)
  13203. (if (memq 'org-formula faces)
  13204. (push (list :table-special
  13205. (previous-single-property-change p 'face)
  13206. (next-single-property-change p 'face)) clist)))
  13207. ((org-at-table-p 'any)
  13208. (push (list :table-table) clist)))
  13209. (goto-char p)
  13210. ;; Now the small context
  13211. (cond
  13212. ((org-at-timestamp-p)
  13213. (push (org-point-in-group p 0 :timestamp) clist))
  13214. ((memq 'org-link faces)
  13215. (push (list :link
  13216. (previous-single-property-change p 'face)
  13217. (next-single-property-change p 'face)) clist))
  13218. ((memq 'org-special-keyword faces)
  13219. (push (list :keyword
  13220. (previous-single-property-change p 'face)
  13221. (next-single-property-change p 'face)) clist))
  13222. ((org-on-target-p)
  13223. (push (org-point-in-group p 0 :target) clist)
  13224. (goto-char (1- (match-beginning 0)))
  13225. (if (looking-at org-radio-target-regexp)
  13226. (push (org-point-in-group p 0 :radio-target) clist))
  13227. (goto-char p))
  13228. ((setq o (car (delq nil
  13229. (mapcar
  13230. (lambda (x)
  13231. (if (memq x org-latex-fragment-image-overlays) x))
  13232. (org-overlays-at (point))))))
  13233. (push (list :latex-fragment
  13234. (org-overlay-start o) (org-overlay-end o)) clist)
  13235. (push (list :latex-preview
  13236. (org-overlay-start o) (org-overlay-end o)) clist))
  13237. ((org-inside-LaTeX-fragment-p)
  13238. ;; FIXME: positions wrong.
  13239. (push (list :latex-fragment (point) (point)) clist)))
  13240. (setq clist (nreverse (delq nil clist)))
  13241. clist))
  13242. ;; FIXME: Compare with at-regexp-p Do we need both?
  13243. (defun org-in-regexp (re &optional nlines visually)
  13244. "Check if point is inside a match of regexp.
  13245. Normally only the current line is checked, but you can include NLINES extra
  13246. lines both before and after point into the search.
  13247. If VISUALLY is set, require that the cursor is not after the match but
  13248. really on, so that the block visually is on the match."
  13249. (catch 'exit
  13250. (let ((pos (point))
  13251. (eol (point-at-eol (+ 1 (or nlines 0))))
  13252. (inc (if visually 1 0)))
  13253. (save-excursion
  13254. (beginning-of-line (- 1 (or nlines 0)))
  13255. (while (re-search-forward re eol t)
  13256. (if (and (<= (match-beginning 0) pos)
  13257. (>= (+ inc (match-end 0)) pos))
  13258. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13259. (defun org-at-regexp-p (regexp)
  13260. "Is point inside a match of REGEXP in the current line?"
  13261. (catch 'exit
  13262. (save-excursion
  13263. (let ((pos (point)) (end (point-at-eol)))
  13264. (beginning-of-line 1)
  13265. (while (re-search-forward regexp end t)
  13266. (if (and (<= (match-beginning 0) pos)
  13267. (>= (match-end 0) pos))
  13268. (throw 'exit t)))
  13269. nil))))
  13270. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13271. "Call `multi-occur' with buffers for all agenda files."
  13272. (interactive "sOrg-files matching: \np")
  13273. (let* ((files (org-agenda-files))
  13274. (tnames (mapcar 'file-truename files))
  13275. (extra org-agenda-text-search-extra-files)
  13276. f)
  13277. (when (eq (car extra) 'agenda-archives)
  13278. (setq extra (cdr extra))
  13279. (setq files (org-add-archive-files files)))
  13280. (while (setq f (pop extra))
  13281. (unless (member (file-truename f) tnames)
  13282. (add-to-list 'files f 'append)
  13283. (add-to-list 'tnames (file-truename f) 'append)))
  13284. (multi-occur
  13285. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13286. regexp)))
  13287. (if (boundp 'occur-mode-find-occurrence-hook)
  13288. ;; Emacs 23
  13289. (add-hook 'occur-mode-find-occurrence-hook
  13290. (lambda ()
  13291. (when (org-mode-p)
  13292. (org-reveal))))
  13293. ;; Emacs 22
  13294. (defadvice occur-mode-goto-occurrence
  13295. (after org-occur-reveal activate)
  13296. (and (org-mode-p) (org-reveal)))
  13297. (defadvice occur-mode-goto-occurrence-other-window
  13298. (after org-occur-reveal activate)
  13299. (and (org-mode-p) (org-reveal)))
  13300. (defadvice occur-mode-display-occurrence
  13301. (after org-occur-reveal activate)
  13302. (when (org-mode-p)
  13303. (let ((pos (occur-mode-find-occurrence)))
  13304. (with-current-buffer (marker-buffer pos)
  13305. (save-excursion
  13306. (goto-char pos)
  13307. (org-reveal)))))))
  13308. (defun org-uniquify (list)
  13309. "Remove duplicate elements from LIST."
  13310. (let (res)
  13311. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13312. res))
  13313. (defun org-delete-all (elts list)
  13314. "Remove all elements in ELTS from LIST."
  13315. (while elts
  13316. (setq list (delete (pop elts) list)))
  13317. list)
  13318. (defun org-back-over-empty-lines ()
  13319. "Move backwards over whitespace, to the beginning of the first empty line.
  13320. Returns the number of empty lines passed."
  13321. (let ((pos (point)))
  13322. (skip-chars-backward " \t\n\r")
  13323. (beginning-of-line 2)
  13324. (goto-char (min (point) pos))
  13325. (count-lines (point) pos)))
  13326. (defun org-skip-whitespace ()
  13327. (skip-chars-forward " \t\n\r"))
  13328. (defun org-point-in-group (point group &optional context)
  13329. "Check if POINT is in match-group GROUP.
  13330. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13331. match. If the match group does ot exist or point is not inside it,
  13332. return nil."
  13333. (and (match-beginning group)
  13334. (>= point (match-beginning group))
  13335. (<= point (match-end group))
  13336. (if context
  13337. (list context (match-beginning group) (match-end group))
  13338. t)))
  13339. (defun org-switch-to-buffer-other-window (&rest args)
  13340. "Switch to buffer in a second window on the current frame.
  13341. In particular, do not allow pop-up frames."
  13342. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13343. special-display-function)
  13344. (apply 'switch-to-buffer-other-window args)))
  13345. (defun org-combine-plists (&rest plists)
  13346. "Create a single property list from all plists in PLISTS.
  13347. The process starts by copying the first list, and then setting properties
  13348. from the other lists. Settings in the last list are the most significant
  13349. ones and overrule settings in the other lists."
  13350. (let ((rtn (copy-sequence (pop plists)))
  13351. p v ls)
  13352. (while plists
  13353. (setq ls (pop plists))
  13354. (while ls
  13355. (setq p (pop ls) v (pop ls))
  13356. (setq rtn (plist-put rtn p v))))
  13357. rtn))
  13358. (defun org-move-line-down (arg)
  13359. "Move the current line down. With prefix argument, move it past ARG lines."
  13360. (interactive "p")
  13361. (let ((col (current-column))
  13362. beg end pos)
  13363. (beginning-of-line 1) (setq beg (point))
  13364. (beginning-of-line 2) (setq end (point))
  13365. (beginning-of-line (+ 1 arg))
  13366. (setq pos (move-marker (make-marker) (point)))
  13367. (insert (delete-and-extract-region beg end))
  13368. (goto-char pos)
  13369. (org-move-to-column col)))
  13370. (defun org-move-line-up (arg)
  13371. "Move the current line up. With prefix argument, move it past ARG lines."
  13372. (interactive "p")
  13373. (let ((col (current-column))
  13374. beg end pos)
  13375. (beginning-of-line 1) (setq beg (point))
  13376. (beginning-of-line 2) (setq end (point))
  13377. (beginning-of-line (- arg))
  13378. (setq pos (move-marker (make-marker) (point)))
  13379. (insert (delete-and-extract-region beg end))
  13380. (goto-char pos)
  13381. (org-move-to-column col)))
  13382. (defun org-replace-escapes (string table)
  13383. "Replace %-escapes in STRING with values in TABLE.
  13384. TABLE is an association list with keys like \"%a\" and string values.
  13385. The sequences in STRING may contain normal field width and padding information,
  13386. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13387. so values can contain further %-escapes if they are define later in TABLE."
  13388. (let ((case-fold-search nil)
  13389. e re rpl)
  13390. (while (setq e (pop table))
  13391. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13392. (while (string-match re string)
  13393. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13394. (cdr e)))
  13395. (setq string (replace-match rpl t t string))))
  13396. string))
  13397. (defun org-sublist (list start end)
  13398. "Return a section of LIST, from START to END.
  13399. Counting starts at 1."
  13400. (let (rtn (c start))
  13401. (setq list (nthcdr (1- start) list))
  13402. (while (and list (<= c end))
  13403. (push (pop list) rtn)
  13404. (setq c (1+ c)))
  13405. (nreverse rtn)))
  13406. (defun org-find-base-buffer-visiting (file)
  13407. "Like `find-buffer-visiting' but alway return the base buffer and
  13408. not an indirect buffer."
  13409. (let ((buf (find-buffer-visiting file)))
  13410. (if buf
  13411. (or (buffer-base-buffer buf) buf)
  13412. nil)))
  13413. (defun org-image-file-name-regexp (&optional extensions)
  13414. "Return regexp matching the file names of images.
  13415. If EXTENSIONS is given, only match these."
  13416. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13417. (image-file-name-regexp)
  13418. (let ((image-file-name-extensions
  13419. (or extensions
  13420. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13421. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13422. (concat "\\."
  13423. (regexp-opt (nconc (mapcar 'upcase
  13424. image-file-name-extensions)
  13425. image-file-name-extensions)
  13426. t)
  13427. "\\'"))))
  13428. (defun org-file-image-p (file &optional extensions)
  13429. "Return non-nil if FILE is an image."
  13430. (save-match-data
  13431. (string-match (org-image-file-name-regexp extensions) file)))
  13432. (defun org-get-cursor-date ()
  13433. "Return the date at cursor in as a time.
  13434. This works in the calendar and in the agenda, anywhere else it just
  13435. returns the current time."
  13436. (let (date day defd)
  13437. (cond
  13438. ((eq major-mode 'calendar-mode)
  13439. (setq date (calendar-cursor-to-date)
  13440. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13441. ((eq major-mode 'org-agenda-mode)
  13442. (setq day (get-text-property (point) 'day))
  13443. (if day
  13444. (setq date (calendar-gregorian-from-absolute day)
  13445. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13446. (nth 2 date))))))
  13447. (or defd (current-time))))
  13448. (defvar org-agenda-action-marker (make-marker)
  13449. "Marker pointing to the entry for the next agenda action.")
  13450. (defun org-mark-entry-for-agenda-action ()
  13451. "Mark the current entry as target of an agenda action.
  13452. Agenda actions are actions executed from the agenda with the key `k',
  13453. which make use of the date at the cursor."
  13454. (interactive)
  13455. (move-marker org-agenda-action-marker
  13456. (save-excursion (org-back-to-heading t) (point))
  13457. (current-buffer))
  13458. (message
  13459. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13460. ;;; Paragraph filling stuff.
  13461. ;; We want this to be just right, so use the full arsenal.
  13462. (defun org-indent-line-function ()
  13463. "Indent line like previous, but further if previous was headline or item."
  13464. (interactive)
  13465. (let* ((pos (point))
  13466. (itemp (org-at-item-p))
  13467. column bpos bcol tpos tcol bullet btype bullet-type)
  13468. ;; Find the previous relevant line
  13469. (beginning-of-line 1)
  13470. (cond
  13471. ((looking-at "#") (setq column 0))
  13472. ((looking-at "\\*+ ") (setq column 0))
  13473. (t
  13474. (beginning-of-line 0)
  13475. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13476. (beginning-of-line 0))
  13477. (cond
  13478. ((looking-at "\\*+[ \t]+")
  13479. (if (not org-adapt-indentation)
  13480. (setq column 0)
  13481. (goto-char (match-end 0))
  13482. (setq column (current-column))))
  13483. ((org-in-item-p)
  13484. (org-beginning-of-item)
  13485. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13486. (setq bpos (match-beginning 1) tpos (match-end 0)
  13487. bcol (progn (goto-char bpos) (current-column))
  13488. tcol (progn (goto-char tpos) (current-column))
  13489. bullet (match-string 1)
  13490. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13491. (if (> tcol (+ bcol org-description-max-indent))
  13492. (setq tcol (+ bcol 5)))
  13493. (if (not itemp)
  13494. (setq column tcol)
  13495. (goto-char pos)
  13496. (beginning-of-line 1)
  13497. (if (looking-at "\\S-")
  13498. (progn
  13499. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13500. (setq bullet (match-string 1)
  13501. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13502. (setq column (if (equal btype bullet-type) bcol tcol)))
  13503. (setq column (org-get-indentation)))))
  13504. (t (setq column (org-get-indentation))))))
  13505. (goto-char pos)
  13506. (if (<= (current-column) (current-indentation))
  13507. (org-indent-line-to column)
  13508. (save-excursion (org-indent-line-to column)))
  13509. (setq column (current-column))
  13510. (beginning-of-line 1)
  13511. (if (looking-at
  13512. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13513. (replace-match (concat "\\1" (format org-property-format
  13514. (match-string 2) (match-string 3)))
  13515. t nil))
  13516. (org-move-to-column column)))
  13517. (defun org-set-autofill-regexps ()
  13518. (interactive)
  13519. ;; In the paragraph separator we include headlines, because filling
  13520. ;; text in a line directly attached to a headline would otherwise
  13521. ;; fill the headline as well.
  13522. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13523. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13524. ;; The paragraph starter includes hand-formatted lists.
  13525. (org-set-local 'paragraph-start
  13526. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13527. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13528. ;; But only if the user has not turned off tables or fixed-width regions
  13529. (org-set-local
  13530. 'auto-fill-inhibit-regexp
  13531. (concat "\\*+ \\|#\\+"
  13532. "\\|[ \t]*" org-keyword-time-regexp
  13533. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13534. (concat
  13535. "\\|[ \t]*["
  13536. (if org-enable-table-editor "|" "")
  13537. (if org-enable-fixed-width-editor ":" "")
  13538. "]"))))
  13539. ;; We use our own fill-paragraph function, to make sure that tables
  13540. ;; and fixed-width regions are not wrapped. That function will pass
  13541. ;; through to `fill-paragraph' when appropriate.
  13542. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13543. ; Adaptive filling: To get full control, first make sure that
  13544. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13545. (org-set-local 'adaptive-fill-regexp "\000")
  13546. (org-set-local 'adaptive-fill-function
  13547. 'org-adaptive-fill-function)
  13548. (org-set-local
  13549. 'align-mode-rules-list
  13550. '((org-in-buffer-settings
  13551. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13552. (modes . '(org-mode))))))
  13553. (defun org-fill-paragraph (&optional justify)
  13554. "Re-align a table, pass through to fill-paragraph if no table."
  13555. (let ((table-p (org-at-table-p))
  13556. (table.el-p (org-at-table.el-p)))
  13557. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13558. (save-excursion (goto-char (point-at-bol))
  13559. (looking-at outline-regexp)))
  13560. t) ; skip headlines
  13561. (table.el-p t) ; skip table.el tables
  13562. (table-p (org-table-align) t) ; align org-mode tables
  13563. (t nil)))) ; call paragraph-fill
  13564. ;; For reference, this is the default value of adaptive-fill-regexp
  13565. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13566. (defun org-adaptive-fill-function ()
  13567. "Return a fill prefix for org-mode files.
  13568. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13569. work correctly."
  13570. (cond ((looking-at "#[ \t]+")
  13571. (match-string 0))
  13572. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13573. (save-excursion
  13574. (if (> (match-end 1) (+ (match-beginning 1)
  13575. org-description-max-indent))
  13576. (goto-char (+ (match-beginning 1) 5))
  13577. (goto-char (match-end 0)))
  13578. (make-string (current-column) ?\ )))
  13579. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13580. (save-excursion
  13581. (goto-char (match-end 0))
  13582. (make-string (current-column) ?\ )))
  13583. (t nil)))
  13584. ;;; Other stuff.
  13585. (defun org-toggle-fixed-width-section (arg)
  13586. "Toggle the fixed-width export.
  13587. If there is no active region, the QUOTE keyword at the current headline is
  13588. inserted or removed. When present, it causes the text between this headline
  13589. and the next to be exported as fixed-width text, and unmodified.
  13590. If there is an active region, this command adds or removes a colon as the
  13591. first character of this line. If the first character of a line is a colon,
  13592. this line is also exported in fixed-width font."
  13593. (interactive "P")
  13594. (let* ((cc 0)
  13595. (regionp (org-region-active-p))
  13596. (beg (if regionp (region-beginning) (point)))
  13597. (end (if regionp (region-end)))
  13598. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13599. (case-fold-search nil)
  13600. (re "[ \t]*\\(:\\)")
  13601. off)
  13602. (if regionp
  13603. (save-excursion
  13604. (goto-char beg)
  13605. (setq cc (current-column))
  13606. (beginning-of-line 1)
  13607. (setq off (looking-at re))
  13608. (while (> nlines 0)
  13609. (setq nlines (1- nlines))
  13610. (beginning-of-line 1)
  13611. (cond
  13612. (arg
  13613. (org-move-to-column cc t)
  13614. (insert ":\n")
  13615. (forward-line -1))
  13616. ((and off (looking-at re))
  13617. (replace-match "" t t nil 1))
  13618. ((not off) (org-move-to-column cc t) (insert ":")))
  13619. (forward-line 1)))
  13620. (save-excursion
  13621. (org-back-to-heading)
  13622. (if (looking-at (concat outline-regexp
  13623. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13624. (replace-match "" t t nil 1)
  13625. (if (looking-at outline-regexp)
  13626. (progn
  13627. (goto-char (match-end 0))
  13628. (insert org-quote-string " "))))))))
  13629. ;;;; Functions extending outline functionality
  13630. (defun org-beginning-of-line (&optional arg)
  13631. "Go to the beginning of the current line. If that is invisible, continue
  13632. to a visible line beginning. This makes the function of C-a more intuitive.
  13633. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13634. first attempt, and only move to after the tags when the cursor is already
  13635. beyond the end of the headline."
  13636. (interactive "P")
  13637. (let ((pos (point)) refpos)
  13638. (beginning-of-line 1)
  13639. (if (bobp)
  13640. nil
  13641. (backward-char 1)
  13642. (if (org-invisible-p)
  13643. (while (and (not (bobp)) (org-invisible-p))
  13644. (backward-char 1)
  13645. (beginning-of-line 1))
  13646. (forward-char 1)))
  13647. (when org-special-ctrl-a/e
  13648. (cond
  13649. ((and (looking-at org-complex-heading-regexp)
  13650. (= (char-after (match-end 1)) ?\ ))
  13651. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13652. (point-at-eol)))
  13653. (goto-char
  13654. (if (eq org-special-ctrl-a/e t)
  13655. (cond ((> pos refpos) refpos)
  13656. ((= pos (point)) refpos)
  13657. (t (point)))
  13658. (cond ((> pos (point)) (point))
  13659. ((not (eq last-command this-command)) (point))
  13660. (t refpos)))))
  13661. ((org-at-item-p)
  13662. (goto-char
  13663. (if (eq org-special-ctrl-a/e t)
  13664. (cond ((> pos (match-end 4)) (match-end 4))
  13665. ((= pos (point)) (match-end 4))
  13666. (t (point)))
  13667. (cond ((> pos (point)) (point))
  13668. ((not (eq last-command this-command)) (point))
  13669. (t (match-end 4))))))))
  13670. (org-no-warnings
  13671. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13672. (defun org-end-of-line (&optional arg)
  13673. "Go to the end of the line.
  13674. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13675. first attempt, and only move to after the tags when the cursor is already
  13676. beyond the end of the headline."
  13677. (interactive "P")
  13678. (if (or (not org-special-ctrl-a/e)
  13679. (not (org-on-heading-p)))
  13680. (end-of-line arg)
  13681. (let ((pos (point)))
  13682. (beginning-of-line 1)
  13683. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13684. (if (eq org-special-ctrl-a/e t)
  13685. (if (or (< pos (match-beginning 1))
  13686. (= pos (match-end 0)))
  13687. (goto-char (match-beginning 1))
  13688. (goto-char (match-end 0)))
  13689. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13690. (goto-char (match-end 0))
  13691. (goto-char (match-beginning 1))))
  13692. (end-of-line arg))))
  13693. (org-no-warnings
  13694. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13695. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13696. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13697. (defun org-kill-line (&optional arg)
  13698. "Kill line, to tags or end of line."
  13699. (interactive "P")
  13700. (cond
  13701. ((or (not org-special-ctrl-k)
  13702. (bolp)
  13703. (not (org-on-heading-p)))
  13704. (call-interactively 'kill-line))
  13705. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13706. (kill-region (point) (match-beginning 1))
  13707. (org-set-tags nil t))
  13708. (t (kill-region (point) (point-at-eol)))))
  13709. (define-key org-mode-map "\C-k" 'org-kill-line)
  13710. (defun org-yank (&optional arg)
  13711. "Yank. If the kill is a subtree, treat it specially.
  13712. This command will look at the current kill and check if is a single
  13713. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13714. cursor is at the beginning of a line or after the stars of a currently
  13715. empty headline, then the yank is handled specially. How exactly depends
  13716. on the value of the following variables, both set by default.
  13717. org-yank-folded-subtrees
  13718. When set, the subtree(s) will be folded after insertion, but only
  13719. if doing so would now swallow text after the yanked text.
  13720. org-yank-adjusted-subtrees
  13721. When set, the subtree will be promoted or demoted in order to
  13722. fit into the local outline tree structure, which means that the level
  13723. will be adjusted so that it becomes the smaller one of the two
  13724. *visible* surrounding headings.
  13725. Any prefix to this command will cause `yank' to be called directly with
  13726. no special treatment. In particular, a simple `C-u' prefix will just
  13727. plainly yank the text as it is.
  13728. \[1] Basically, the test checks if the first non-white line is a heading
  13729. and if there are no other headings with fewer stars."
  13730. (interactive "P")
  13731. (setq this-command 'yank)
  13732. (if arg
  13733. (call-interactively 'yank)
  13734. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13735. (and (org-kill-is-subtree-p)
  13736. (or (bolp)
  13737. (and (looking-at "[ \t]*$")
  13738. (string-match
  13739. "\\`\\*+\\'"
  13740. (buffer-substring (point-at-bol) (point)))))))
  13741. swallowp)
  13742. (cond
  13743. ((and subtreep org-yank-folded-subtrees)
  13744. (let ((beg (point))
  13745. end)
  13746. (if (and subtreep org-yank-adjusted-subtrees)
  13747. (org-paste-subtree nil nil 'for-yank)
  13748. (call-interactively 'yank))
  13749. (setq end (point))
  13750. (goto-char beg)
  13751. (when (and (bolp) subtreep
  13752. (not (setq swallowp
  13753. (org-yank-folding-would-swallow-text beg end))))
  13754. (or (looking-at outline-regexp)
  13755. (re-search-forward (concat "^" outline-regexp) end t))
  13756. (while (and (< (point) end) (looking-at outline-regexp))
  13757. (hide-subtree)
  13758. (org-cycle-show-empty-lines 'folded)
  13759. (condition-case nil
  13760. (outline-forward-same-level 1)
  13761. (error (goto-char end)))))
  13762. (when swallowp
  13763. (message
  13764. "Yanked text not folded because that would swallow text"))
  13765. (goto-char end)
  13766. (skip-chars-forward " \t\n\r")
  13767. (beginning-of-line 1)
  13768. (push-mark beg 'nomsg)))
  13769. ((and subtreep org-yank-adjusted-subtrees)
  13770. (let ((beg (point-at-bol)))
  13771. (org-paste-subtree nil nil 'for-yank)
  13772. (push-mark beg 'nomsg)))
  13773. (t
  13774. (call-interactively 'yank))))))
  13775. (defun org-yank-folding-would-swallow-text (beg end)
  13776. "Would hide-subtree at BEG swallow any text after END?"
  13777. (let (level)
  13778. (save-excursion
  13779. (goto-char beg)
  13780. (when (or (looking-at outline-regexp)
  13781. (re-search-forward (concat "^" outline-regexp) end t))
  13782. (setq level (org-outline-level)))
  13783. (goto-char end)
  13784. (skip-chars-forward " \t\r\n\v\f")
  13785. (if (or (eobp)
  13786. (and (bolp) (looking-at org-outline-regexp)
  13787. (<= (org-outline-level) level)))
  13788. nil ; Nothing would be swallowed
  13789. t)))) ; something would swallow
  13790. (define-key org-mode-map "\C-y" 'org-yank)
  13791. (defun org-invisible-p ()
  13792. "Check if point is at a character currently not visible."
  13793. ;; Early versions of noutline don't have `outline-invisible-p'.
  13794. (if (fboundp 'outline-invisible-p)
  13795. (outline-invisible-p)
  13796. (get-char-property (point) 'invisible)))
  13797. (defun org-invisible-p2 ()
  13798. "Check if point is at a character currently not visible."
  13799. (save-excursion
  13800. (if (and (eolp) (not (bobp))) (backward-char 1))
  13801. ;; Early versions of noutline don't have `outline-invisible-p'.
  13802. (if (fboundp 'outline-invisible-p)
  13803. (outline-invisible-p)
  13804. (get-char-property (point) 'invisible))))
  13805. (defun org-back-to-heading (&optional invisible-ok)
  13806. "Call `outline-back-to-heading', but provide a better error message."
  13807. (condition-case nil
  13808. (outline-back-to-heading invisible-ok)
  13809. (error (error "Before first headline at position %d in buffer %s"
  13810. (point) (current-buffer)))))
  13811. (defun org-before-first-heading-p ()
  13812. "Before first heading?"
  13813. (save-excursion
  13814. (null (re-search-backward "^\\*+ " nil t))))
  13815. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13816. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13817. (defun org-at-heading-or-item-p ()
  13818. (or (org-on-heading-p) (org-at-item-p)))
  13819. (defun org-on-target-p ()
  13820. (or (org-in-regexp org-radio-target-regexp)
  13821. (org-in-regexp org-target-regexp)))
  13822. (defun org-up-heading-all (arg)
  13823. "Move to the heading line of which the present line is a subheading.
  13824. This function considers both visible and invisible heading lines.
  13825. With argument, move up ARG levels."
  13826. (if (fboundp 'outline-up-heading-all)
  13827. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13828. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13829. (defun org-up-heading-safe ()
  13830. "Move to the heading line of which the present line is a subheading.
  13831. This version will not throw an error. It will return the level of the
  13832. headline found, or nil if no higher level is found."
  13833. (let (start-level re)
  13834. (org-back-to-heading t)
  13835. (setq start-level (funcall outline-level))
  13836. (if (equal start-level 1)
  13837. nil
  13838. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13839. (if (re-search-backward re nil t)
  13840. (funcall outline-level)))))
  13841. (defun org-first-sibling-p ()
  13842. "Is this heading the first child of its parents?"
  13843. (interactive)
  13844. (let ((re (concat "^" outline-regexp))
  13845. level l)
  13846. (unless (org-at-heading-p t)
  13847. (error "Not at a heading"))
  13848. (setq level (funcall outline-level))
  13849. (save-excursion
  13850. (if (not (re-search-backward re nil t))
  13851. t
  13852. (setq l (funcall outline-level))
  13853. (< l level)))))
  13854. (defun org-goto-sibling (&optional previous)
  13855. "Goto the next sibling, even if it is invisible.
  13856. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13857. when a sibling was found. When none is found, return nil and don't
  13858. move point."
  13859. (let ((fun (if previous 're-search-backward 're-search-forward))
  13860. (pos (point))
  13861. (re (concat "^" outline-regexp))
  13862. level l)
  13863. (when (condition-case nil (org-back-to-heading t) (error nil))
  13864. (setq level (funcall outline-level))
  13865. (catch 'exit
  13866. (or previous (forward-char 1))
  13867. (while (funcall fun re nil t)
  13868. (setq l (funcall outline-level))
  13869. (when (< l level) (goto-char pos) (throw 'exit nil))
  13870. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13871. (goto-char pos)
  13872. nil))))
  13873. (defun org-show-siblings ()
  13874. "Show all siblings of the current headline."
  13875. (save-excursion
  13876. (while (org-goto-sibling) (org-flag-heading nil)))
  13877. (save-excursion
  13878. (while (org-goto-sibling 'previous)
  13879. (org-flag-heading nil))))
  13880. (defun org-show-hidden-entry ()
  13881. "Show an entry where even the heading is hidden."
  13882. (save-excursion
  13883. (org-show-entry)))
  13884. (defun org-flag-heading (flag &optional entry)
  13885. "Flag the current heading. FLAG non-nil means make invisible.
  13886. When ENTRY is non-nil, show the entire entry."
  13887. (save-excursion
  13888. (org-back-to-heading t)
  13889. ;; Check if we should show the entire entry
  13890. (if entry
  13891. (progn
  13892. (org-show-entry)
  13893. (save-excursion
  13894. (and (outline-next-heading)
  13895. (org-flag-heading nil))))
  13896. (outline-flag-region (max (point-min) (1- (point)))
  13897. (save-excursion (outline-end-of-heading) (point))
  13898. flag))))
  13899. (defun org-forward-same-level (arg)
  13900. "Move forward to the ARG'th subheading at same level as this one.
  13901. Stop at the first and last subheadings of a superior heading.
  13902. This is like outline-forward-same-level, but invisible headings are ok."
  13903. (interactive "p")
  13904. (org-back-to-heading t)
  13905. (while (> arg 0)
  13906. (let ((point-to-move-to (save-excursion
  13907. (org-get-next-sibling))))
  13908. (if point-to-move-to
  13909. (progn
  13910. (goto-char point-to-move-to)
  13911. (setq arg (1- arg)))
  13912. (progn
  13913. (setq arg 0)
  13914. (error "No following same-level heading"))))))
  13915. (defun org-get-next-sibling ()
  13916. "Move to next heading of the same level, and return point.
  13917. If there is no such heading, return nil.
  13918. This is like outline-next-sibling, but invisible headings are ok."
  13919. (let ((level (funcall outline-level)))
  13920. (outline-next-heading)
  13921. (while (and (not (eobp)) (> (funcall outline-level) level))
  13922. (outline-next-heading))
  13923. (if (or (eobp) (< (funcall outline-level) level))
  13924. nil
  13925. (point))))
  13926. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13927. ;; This is an exact copy of the original function, but it uses
  13928. ;; `org-back-to-heading', to make it work also in invisible
  13929. ;; trees. And is uses an invisible-OK argument.
  13930. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13931. (org-back-to-heading invisible-OK)
  13932. (let ((first t)
  13933. (level (funcall outline-level)))
  13934. (while (and (not (eobp))
  13935. (or first (> (funcall outline-level) level)))
  13936. (setq first nil)
  13937. (outline-next-heading))
  13938. (unless to-heading
  13939. (if (memq (preceding-char) '(?\n ?\^M))
  13940. (progn
  13941. ;; Go to end of line before heading
  13942. (forward-char -1)
  13943. (if (memq (preceding-char) '(?\n ?\^M))
  13944. ;; leave blank line before heading
  13945. (forward-char -1))))))
  13946. (point))
  13947. (defun org-show-subtree ()
  13948. "Show everything after this heading at deeper levels."
  13949. (outline-flag-region
  13950. (point)
  13951. (save-excursion
  13952. (outline-end-of-subtree) (outline-next-heading) (point))
  13953. nil))
  13954. (defun org-show-entry ()
  13955. "Show the body directly following this heading.
  13956. Show the heading too, if it is currently invisible."
  13957. (interactive)
  13958. (save-excursion
  13959. (condition-case nil
  13960. (progn
  13961. (org-back-to-heading t)
  13962. (outline-flag-region
  13963. (max (point-min) (1- (point)))
  13964. (save-excursion
  13965. (re-search-forward
  13966. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13967. (or (match-beginning 1) (point-max)))
  13968. nil))
  13969. (error nil))))
  13970. (defun org-make-options-regexp (kwds &optional extra)
  13971. "Make a regular expression for keyword lines."
  13972. (concat
  13973. "^"
  13974. "#?[ \t]*\\+\\("
  13975. (mapconcat 'regexp-quote kwds "\\|")
  13976. (if extra (concat "\\|" extra))
  13977. "\\):[ \t]*"
  13978. "\\(.+\\)"))
  13979. ;; Make isearch reveal the necessary context
  13980. (defun org-isearch-end ()
  13981. "Reveal context after isearch exits."
  13982. (when isearch-success ; only if search was successful
  13983. (if (featurep 'xemacs)
  13984. ;; Under XEmacs, the hook is run in the correct place,
  13985. ;; we directly show the context.
  13986. (org-show-context 'isearch)
  13987. ;; In Emacs the hook runs *before* restoring the overlays.
  13988. ;; So we have to use a one-time post-command-hook to do this.
  13989. ;; (Emacs 22 has a special variable, see function `org-mode')
  13990. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13991. isearch-mode-end-hook-quit)
  13992. ;; Only when the isearch was not quitted.
  13993. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13994. 'append 'local)))))
  13995. (defun org-isearch-post-command ()
  13996. "Remove self from hook, and show context."
  13997. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13998. (org-show-context 'isearch))
  13999. ;;;; Integration with and fixes for other packages
  14000. ;;; Imenu support
  14001. (defvar org-imenu-markers nil
  14002. "All markers currently used by Imenu.")
  14003. (make-variable-buffer-local 'org-imenu-markers)
  14004. (defun org-imenu-new-marker (&optional pos)
  14005. "Return a new marker for use by Imenu, and remember the marker."
  14006. (let ((m (make-marker)))
  14007. (move-marker m (or pos (point)))
  14008. (push m org-imenu-markers)
  14009. m))
  14010. (defun org-imenu-get-tree ()
  14011. "Produce the index for Imenu."
  14012. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14013. (setq org-imenu-markers nil)
  14014. (let* ((n org-imenu-depth)
  14015. (re (concat "^" outline-regexp))
  14016. (subs (make-vector (1+ n) nil))
  14017. (last-level 0)
  14018. m level head)
  14019. (save-excursion
  14020. (save-restriction
  14021. (widen)
  14022. (goto-char (point-max))
  14023. (while (re-search-backward re nil t)
  14024. (setq level (org-reduced-level (funcall outline-level)))
  14025. (when (<= level n)
  14026. (looking-at org-complex-heading-regexp)
  14027. (setq head (org-link-display-format
  14028. (org-match-string-no-properties 4))
  14029. m (org-imenu-new-marker))
  14030. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14031. (if (>= level last-level)
  14032. (push (cons head m) (aref subs level))
  14033. (push (cons head (aref subs (1+ level))) (aref subs level))
  14034. (loop for i from (1+ level) to n do (aset subs i nil)))
  14035. (setq last-level level)))))
  14036. (aref subs 1)))
  14037. (eval-after-load "imenu"
  14038. '(progn
  14039. (add-hook 'imenu-after-jump-hook
  14040. (lambda ()
  14041. (if (eq major-mode 'org-mode)
  14042. (org-show-context 'org-goto))))))
  14043. (defun org-link-display-format (link)
  14044. "Replace a link with either the description, or the link target
  14045. if no description is present"
  14046. (save-match-data
  14047. (if (string-match org-bracket-link-analytic-regexp link)
  14048. (replace-match (or (match-string 5 link)
  14049. (concat (match-string 1 link)
  14050. (match-string 3 link)))
  14051. nil nil link)
  14052. link)))
  14053. ;; Speedbar support
  14054. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14055. "Overlay marking the agenda restriction line in speedbar.")
  14056. (org-overlay-put org-speedbar-restriction-lock-overlay
  14057. 'face 'org-agenda-restriction-lock)
  14058. (org-overlay-put org-speedbar-restriction-lock-overlay
  14059. 'help-echo "Agendas are currently limited to this item.")
  14060. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14061. (defun org-speedbar-set-agenda-restriction ()
  14062. "Restrict future agenda commands to the location at point in speedbar.
  14063. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14064. (interactive)
  14065. (require 'org-agenda)
  14066. (let (p m tp np dir txt)
  14067. (cond
  14068. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14069. 'org-imenu t))
  14070. (setq m (get-text-property p 'org-imenu-marker))
  14071. (save-excursion
  14072. (save-restriction
  14073. (set-buffer (marker-buffer m))
  14074. (goto-char m)
  14075. (org-agenda-set-restriction-lock 'subtree))))
  14076. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14077. 'speedbar-function 'speedbar-find-file))
  14078. (setq tp (previous-single-property-change
  14079. (1+ p) 'speedbar-function)
  14080. np (next-single-property-change
  14081. tp 'speedbar-function)
  14082. dir (speedbar-line-directory)
  14083. txt (buffer-substring-no-properties (or tp (point-min))
  14084. (or np (point-max))))
  14085. (save-excursion
  14086. (save-restriction
  14087. (set-buffer (find-file-noselect
  14088. (let ((default-directory dir))
  14089. (expand-file-name txt))))
  14090. (unless (org-mode-p)
  14091. (error "Cannot restrict to non-Org-mode file"))
  14092. (org-agenda-set-restriction-lock 'file))))
  14093. (t (error "Don't know how to restrict Org-mode's agenda")))
  14094. (org-move-overlay org-speedbar-restriction-lock-overlay
  14095. (point-at-bol) (point-at-eol))
  14096. (setq current-prefix-arg nil)
  14097. (org-agenda-maybe-redo)))
  14098. (eval-after-load "speedbar"
  14099. '(progn
  14100. (speedbar-add-supported-extension ".org")
  14101. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14102. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14103. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14104. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14105. (add-hook 'speedbar-visiting-tag-hook
  14106. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14107. ;;; Fixes and Hacks for problems with other packages
  14108. ;; Make flyspell not check words in links, to not mess up our keymap
  14109. (defun org-mode-flyspell-verify ()
  14110. "Don't let flyspell put overlays at active buttons."
  14111. (not (get-text-property (point) 'keymap)))
  14112. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14113. (eval-after-load "bookmark"
  14114. '(if (boundp 'bookmark-after-jump-hook)
  14115. ;; We can use the hook
  14116. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14117. ;; Hook not available, use advice
  14118. (defadvice bookmark-jump (after org-make-visible activate)
  14119. "Make the position visible."
  14120. (org-bookmark-jump-unhide))))
  14121. ;; Make sure saveplace show the location if it was hidden
  14122. (eval-after-load "saveplace"
  14123. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14124. "Make the position visible."
  14125. (org-bookmark-jump-unhide)))
  14126. (defun org-bookmark-jump-unhide ()
  14127. "Unhide the current position, to show the bookmark location."
  14128. (and (org-mode-p)
  14129. (or (org-invisible-p)
  14130. (save-excursion (goto-char (max (point-min) (1- (point))))
  14131. (org-invisible-p)))
  14132. (org-show-context 'bookmark-jump)))
  14133. ;; Make session.el ignore our circular variable
  14134. (eval-after-load "session"
  14135. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14136. ;;;; Experimental code
  14137. (defun org-closed-in-range ()
  14138. "Sparse tree of items closed in a certain time range.
  14139. Still experimental, may disappear in the future."
  14140. (interactive)
  14141. ;; Get the time interval from the user.
  14142. (let* ((time1 (time-to-seconds
  14143. (org-read-date nil 'to-time nil "Starting date: ")))
  14144. (time2 (time-to-seconds
  14145. (org-read-date nil 'to-time nil "End date:")))
  14146. ;; callback function
  14147. (callback (lambda ()
  14148. (let ((time
  14149. (time-to-seconds
  14150. (apply 'encode-time
  14151. (org-parse-time-string
  14152. (match-string 1))))))
  14153. ;; check if time in interval
  14154. (and (>= time time1) (<= time time2))))))
  14155. ;; make tree, check each match with the callback
  14156. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14157. ;;;; Finish up
  14158. (provide 'org)
  14159. (run-hooks 'org-load-hook)
  14160. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14161. ;;; org.el ends here