org.el.orig 1024 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781
  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 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 5.18a
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 3, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. ;;;; Require other packages
  65. (eval-when-compile
  66. (require 'cl)
  67. (require 'gnus-sum)
  68. (require 'calendar))
  69. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  70. ;; the file noutline.el being loaded.
  71. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  72. ;; We require noutline, which might be provided in outline.el
  73. (require 'outline) (require 'noutline)
  74. ;; Other stuff we need.
  75. (require 'time-date)
  76. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  77. (require 'easymenu)
  78. ;;;; Customization variables
  79. ;;; Version
  80. (defconst org-version "5.17a"
  81. "The version number of the file org.el.")
  82. (defun org-version ()
  83. (interactive)
  84. (message "Org-mode version %s" org-version))
  85. ;;; Compatibility constants
  86. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  87. (defconst org-format-transports-properties-p
  88. (let ((x "a"))
  89. (add-text-properties 0 1 '(test t) x)
  90. (get-text-property 0 'test (format "%s" x)))
  91. "Does format transport text properties?")
  92. (defmacro org-bound-and-true-p (var)
  93. "Return the value of symbol VAR if it is bound, else nil."
  94. `(and (boundp (quote ,var)) ,var))
  95. (defmacro org-unmodified (&rest body)
  96. "Execute body without changing buffer-modified-p."
  97. `(set-buffer-modified-p
  98. (prog1 (buffer-modified-p) ,@body)))
  99. (defmacro org-re (s)
  100. "Replace posix classes in regular expression."
  101. (if (featurep 'xemacs)
  102. (let ((ss s))
  103. (save-match-data
  104. (while (string-match "\\[:alnum:\\]" ss)
  105. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  106. (while (string-match "\\[:alpha:\\]" ss)
  107. (setq ss (replace-match "a-zA-Z" t t ss)))
  108. ss))
  109. s))
  110. (defmacro org-preserve-lc (&rest body)
  111. `(let ((_line (org-current-line))
  112. (_col (current-column)))
  113. (unwind-protect
  114. (progn ,@body)
  115. (goto-line _line)
  116. (move-to-column _col))))
  117. (defmacro org-without-partial-completion (&rest body)
  118. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  119. partial-completion-mode)))
  120. (unwind-protect
  121. (progn
  122. (if pc-mode (partial-completion-mode -1))
  123. ,@body)
  124. (if pc-mode (partial-completion-mode 1)))))
  125. ;;; The custom variables
  126. (defgroup org nil
  127. "Outline-based notes management and organizer."
  128. :tag "Org"
  129. :group 'outlines
  130. :group 'hypermedia
  131. :group 'calendar)
  132. ;; FIXME: Needs a separate group...
  133. (defcustom org-completion-fallback-command 'hippie-expand
  134. "The expansion command called by \\[org-complete] in normal context.
  135. Normal means, no org-mode-specific context."
  136. :group 'org
  137. :type 'function)
  138. (defgroup org-startup nil
  139. "Options concerning startup of Org-mode."
  140. :tag "Org Startup"
  141. :group 'org)
  142. (defcustom org-startup-folded t
  143. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  144. This can also be configured on a per-file basis by adding one of
  145. the following lines anywhere in the buffer:
  146. #+STARTUP: fold
  147. #+STARTUP: nofold
  148. #+STARTUP: content"
  149. :group 'org-startup
  150. :type '(choice
  151. (const :tag "nofold: show all" nil)
  152. (const :tag "fold: overview" t)
  153. (const :tag "content: all headlines" content)))
  154. (defcustom org-startup-truncated t
  155. "Non-nil means, entering Org-mode will set `truncate-lines'.
  156. This is useful since some lines containing links can be very long and
  157. uninteresting. Also tables look terrible when wrapped."
  158. :group 'org-startup
  159. :type 'boolean)
  160. (defcustom org-startup-align-all-tables nil
  161. "Non-nil means, align all tables when visiting a file.
  162. This is useful when the column width in tables is forced with <N> cookies
  163. in table fields. Such tables will look correct only after the first re-align.
  164. This can also be configured on a per-file basis by adding one of
  165. the following lines anywhere in the buffer:
  166. #+STARTUP: align
  167. #+STARTUP: noalign"
  168. :group 'org-startup
  169. :type 'boolean)
  170. (defcustom org-insert-mode-line-in-empty-file nil
  171. "Non-nil means insert the first line setting Org-mode in empty files.
  172. When the function `org-mode' is called interactively in an empty file, this
  173. normally means that the file name does not automatically trigger Org-mode.
  174. To ensure that the file will always be in Org-mode in the future, a
  175. line enforcing Org-mode will be inserted into the buffer, if this option
  176. has been set."
  177. :group 'org-startup
  178. :type 'boolean)
  179. (defcustom org-replace-disputed-keys nil
  180. "Non-nil means use alternative key bindings for some keys.
  181. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  182. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  183. If you want to use Org-mode together with one of these other modes,
  184. or more generally if you would like to move some Org-mode commands to
  185. other keys, set this variable and configure the keys with the variable
  186. `org-disputed-keys'.
  187. This option is only relevant at load-time of Org-mode, and must be set
  188. *before* org.el is loaded. Changing it requires a restart of Emacs to
  189. become effective."
  190. :group 'org-startup
  191. :type 'boolean)
  192. (if (fboundp 'defvaralias)
  193. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  194. (defcustom org-disputed-keys
  195. '(([(shift up)] . [(meta p)])
  196. ([(shift down)] . [(meta n)])
  197. ([(shift left)] . [(meta -)])
  198. ([(shift right)] . [(meta +)])
  199. ([(control shift right)] . [(meta shift +)])
  200. ([(control shift left)] . [(meta shift -)]))
  201. "Keys for which Org-mode and other modes compete.
  202. This is an alist, cars are the default keys, second element specifies
  203. the alternative to use when `org-replace-disputed-keys' is t.
  204. Keys can be specified in any syntax supported by `define-key'.
  205. The value of this option takes effect only at Org-mode's startup,
  206. therefore you'll have to restart Emacs to apply it after changing."
  207. :group 'org-startup
  208. :type 'alist)
  209. (defun org-key (key)
  210. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  211. Or return the original if not disputed."
  212. (if org-replace-disputed-keys
  213. (let* ((nkey (key-description key))
  214. (x (org-find-if (lambda (x)
  215. (equal (key-description (car x)) nkey))
  216. org-disputed-keys)))
  217. (if x (cdr x) key))
  218. key))
  219. (defun org-find-if (predicate seq)
  220. (catch 'exit
  221. (while seq
  222. (if (funcall predicate (car seq))
  223. (throw 'exit (car seq))
  224. (pop seq)))))
  225. (defun org-defkey (keymap key def)
  226. "Define a key, possibly translated, as returned by `org-key'."
  227. (define-key keymap (org-key key) def))
  228. (defcustom org-ellipsis nil
  229. "The ellipsis to use in the Org-mode outline.
  230. When nil, just use the standard three dots. When a string, use that instead,
  231. When a face, use the standart 3 dots, but with the specified face.
  232. The change affects only Org-mode (which will then use its own display table).
  233. Changing this requires executing `M-x org-mode' in a buffer to become
  234. effective."
  235. :group 'org-startup
  236. :type '(choice (const :tag "Default" nil)
  237. (face :tag "Face" :value org-warning)
  238. (string :tag "String" :value "...#")))
  239. (defvar org-display-table nil
  240. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  241. (defgroup org-keywords nil
  242. "Keywords in Org-mode."
  243. :tag "Org Keywords"
  244. :group 'org)
  245. (defcustom org-deadline-string "DEADLINE:"
  246. "String to mark deadline entries.
  247. A deadline is this string, followed by a time stamp. Should be a word,
  248. terminated by a colon. You can insert a schedule keyword and
  249. a timestamp with \\[org-deadline].
  250. Changes become only effective after restarting Emacs."
  251. :group 'org-keywords
  252. :type 'string)
  253. (defcustom org-scheduled-string "SCHEDULED:"
  254. "String to mark scheduled TODO entries.
  255. A schedule is this string, followed by a time stamp. Should be a word,
  256. terminated by a colon. You can insert a schedule keyword and
  257. a timestamp with \\[org-schedule].
  258. Changes become only effective after restarting Emacs."
  259. :group 'org-keywords
  260. :type 'string)
  261. (defcustom org-closed-string "CLOSED:"
  262. "String used as the prefix for timestamps logging closing a TODO entry."
  263. :group 'org-keywords
  264. :type 'string)
  265. (defcustom org-clock-string "CLOCK:"
  266. "String used as prefix for timestamps clocking work hours on an item."
  267. :group 'org-keywords
  268. :type 'string)
  269. (defcustom org-comment-string "COMMENT"
  270. "Entries starting with this keyword will never be exported.
  271. An entry can be toggled between COMMENT and normal with
  272. \\[org-toggle-comment].
  273. Changes become only effective after restarting Emacs."
  274. :group 'org-keywords
  275. :type 'string)
  276. (defcustom org-quote-string "QUOTE"
  277. "Entries starting with this keyword will be exported in fixed-width font.
  278. Quoting applies only to the text in the entry following the headline, and does
  279. not extend beyond the next headline, even if that is lower level.
  280. An entry can be toggled between QUOTE and normal with
  281. \\[org-toggle-fixed-width-section]."
  282. :group 'org-keywords
  283. :type 'string)
  284. (defconst org-repeat-re
  285. (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  286. " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  287. "Regular expression for specifying repeated events.
  288. After a match, group 1 contains the repeat expression.")
  289. (defgroup org-structure nil
  290. "Options concerning the general structure of Org-mode files."
  291. :tag "Org Structure"
  292. :group 'org)
  293. (defgroup org-reveal-location nil
  294. "Options about how to make context of a location visible."
  295. :tag "Org Reveal Location"
  296. :group 'org-structure)
  297. (defconst org-context-choice
  298. '(choice
  299. (const :tag "Always" t)
  300. (const :tag "Never" nil)
  301. (repeat :greedy t :tag "Individual contexts"
  302. (cons
  303. (choice :tag "Context"
  304. (const agenda)
  305. (const org-goto)
  306. (const occur-tree)
  307. (const tags-tree)
  308. (const link-search)
  309. (const mark-goto)
  310. (const bookmark-jump)
  311. (const isearch)
  312. (const default))
  313. (boolean))))
  314. "Contexts for the reveal options.")
  315. (defcustom org-show-hierarchy-above '((default . t))
  316. "Non-nil means, show full hierarchy when revealing a location.
  317. Org-mode often shows locations in an org-mode file which might have
  318. been invisible before. When this is set, the hierarchy of headings
  319. above the exposed location is shown.
  320. Turning this off for example for sparse trees makes them very compact.
  321. Instead of t, this can also be an alist specifying this option for different
  322. contexts. Valid contexts are
  323. agenda when exposing an entry from the agenda
  324. org-goto when using the command `org-goto' on key C-c C-j
  325. occur-tree when using the command `org-occur' on key C-c /
  326. tags-tree when constructing a sparse tree based on tags matches
  327. link-search when exposing search matches associated with a link
  328. mark-goto when exposing the jump goal of a mark
  329. bookmark-jump when exposing a bookmark location
  330. isearch when exiting from an incremental search
  331. default default for all contexts not set explicitly"
  332. :group 'org-reveal-location
  333. :type org-context-choice)
  334. (defcustom org-show-following-heading '((default . nil))
  335. "Non-nil means, show following heading when revealing a location.
  336. Org-mode often shows locations in an org-mode file which might have
  337. been invisible before. When this is set, the heading following the
  338. match is shown.
  339. Turning this off for example for sparse trees makes them very compact,
  340. but makes it harder to edit the location of the match. In such a case,
  341. use the command \\[org-reveal] to show more context.
  342. Instead of t, this can also be an alist specifying this option for different
  343. contexts. See `org-show-hierarchy-above' for valid contexts."
  344. :group 'org-reveal-location
  345. :type org-context-choice)
  346. (defcustom org-show-siblings '((default . nil) (isearch t))
  347. "Non-nil means, show all sibling heading when revealing a location.
  348. Org-mode often shows locations in an org-mode file which might have
  349. been invisible before. When this is set, the sibling of the current entry
  350. heading are all made visible. If `org-show-hierarchy-above' is t,
  351. the same happens on each level of the hierarchy above the current entry.
  352. By default this is on for the isearch context, off for all other contexts.
  353. Turning this off for example for sparse trees makes them very compact,
  354. but makes it harder to edit the location of the match. In such a case,
  355. use the command \\[org-reveal] to show more context.
  356. Instead of t, this can also be an alist specifying this option for different
  357. contexts. See `org-show-hierarchy-above' for valid contexts."
  358. :group 'org-reveal-location
  359. :type org-context-choice)
  360. (defcustom org-show-entry-below '((default . nil))
  361. "Non-nil means, show the entry below a headline when revealing a location.
  362. Org-mode often shows locations in an org-mode file which might have
  363. been invisible before. When this is set, the text below the headline that is
  364. exposed is also shown.
  365. By default this is off for all contexts.
  366. Instead of t, this can also be an alist specifying this option for different
  367. contexts. See `org-show-hierarchy-above' for valid contexts."
  368. :group 'org-reveal-location
  369. :type org-context-choice)
  370. (defgroup org-cycle nil
  371. "Options concerning visibility cycling in Org-mode."
  372. :tag "Org Cycle"
  373. :group 'org-structure)
  374. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  375. "Names of drawers. Drawers are not opened by cycling on the headline above.
  376. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  377. this:
  378. :DRAWERNAME:
  379. .....
  380. :END:
  381. The drawer \"PROPERTIES\" is special for capturing properties through
  382. the property API.
  383. Drawers can be defined on the per-file basis with a line like:
  384. #+DRAWERS: HIDDEN STATE PROPERTIES"
  385. :group 'org-structure
  386. :type '(repeat (string :tag "Drawer Name")))
  387. (defcustom org-cycle-global-at-bob nil
  388. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  389. This makes it possible to do global cycling without having to use S-TAB or
  390. C-u TAB. For this special case to work, the first line of the buffer
  391. must not be a headline - it may be empty ot some other text. When used in
  392. this way, `org-cycle-hook' is disables temporarily, to make sure the
  393. cursor stays at the beginning of the buffer.
  394. When this option is nil, don't do anything special at the beginning
  395. of the buffer."
  396. :group 'org-cycle
  397. :type 'boolean)
  398. (defcustom org-cycle-emulate-tab t
  399. "Where should `org-cycle' emulate TAB.
  400. nil Never
  401. white Only in completely white lines
  402. whitestart Only at the beginning of lines, before the first non-white char.
  403. t Everywhere except in headlines
  404. exc-hl-bol Everywhere except at the start of a headline
  405. If TAB is used in a place where it does not emulate TAB, the current subtree
  406. visibility is cycled."
  407. :group 'org-cycle
  408. :type '(choice (const :tag "Never" nil)
  409. (const :tag "Only in completely white lines" white)
  410. (const :tag "Before first char in a line" whitestart)
  411. (const :tag "Everywhere except in headlines" t)
  412. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  413. ))
  414. (defcustom org-cycle-separator-lines 2
  415. "Number of empty lines needed to keep an empty line between collapsed trees.
  416. If you leave an empty line between the end of a subtree and the following
  417. headline, this empty line is hidden when the subtree is folded.
  418. Org-mode will leave (exactly) one empty line visible if the number of
  419. empty lines is equal or larger to the number given in this variable.
  420. So the default 2 means, at least 2 empty lines after the end of a subtree
  421. are needed to produce free space between a collapsed subtree and the
  422. following headline.
  423. Special case: when 0, never leave empty lines in collapsed view."
  424. :group 'org-cycle
  425. :type 'integer)
  426. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  427. org-cycle-hide-drawers
  428. org-cycle-show-empty-lines
  429. org-optimize-window-after-visibility-change)
  430. "Hook that is run after `org-cycle' has changed the buffer visibility.
  431. The function(s) in this hook must accept a single argument which indicates
  432. the new state that was set by the most recent `org-cycle' command. The
  433. argument is a symbol. After a global state change, it can have the values
  434. `overview', `content', or `all'. After a local state change, it can have
  435. the values `folded', `children', or `subtree'."
  436. :group 'org-cycle
  437. :type 'hook)
  438. (defgroup org-edit-structure nil
  439. "Options concerning structure editing in Org-mode."
  440. :tag "Org Edit Structure"
  441. :group 'org-structure)
  442. (defcustom org-special-ctrl-a/e nil
  443. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  444. When t, `C-a' will bring back the cursor to the beginning of the
  445. headline text, i.e. after the stars and after a possible TODO keyword.
  446. In an item, this will be the position after the bullet.
  447. When the cursor is already at that position, another `C-a' will bring
  448. it to the beginning of the line.
  449. `C-e' will jump to the end of the headline, ignoring the presence of tags
  450. in the headline. A second `C-e' will then jump to the true end of the
  451. line, after any tags.
  452. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  453. and only a directly following, identical keypress will bring the cursor
  454. to the special positions."
  455. :group 'org-edit-structure
  456. :type '(choice
  457. (const :tag "off" nil)
  458. (const :tag "after bullet first" t)
  459. (const :tag "border first" reversed)))
  460. (if (fboundp 'defvaralias)
  461. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  462. (defcustom org-odd-levels-only nil
  463. "Non-nil means, skip even levels and only use odd levels for the outline.
  464. This has the effect that two stars are being added/taken away in
  465. promotion/demotion commands. It also influences how levels are
  466. handled by the exporters.
  467. Changing it requires restart of `font-lock-mode' to become effective
  468. for fontification also in regions already fontified.
  469. You may also set this on a per-file basis by adding one of the following
  470. lines to the buffer:
  471. #+STARTUP: odd
  472. #+STARTUP: oddeven"
  473. :group 'org-edit-structure
  474. :group 'org-font-lock
  475. :type 'boolean)
  476. (defcustom org-adapt-indentation t
  477. "Non-nil means, adapt indentation when promoting and demoting.
  478. When this is set and the *entire* text in an entry is indented, the
  479. indentation is increased by one space in a demotion command, and
  480. decreased by one in a promotion command. If any line in the entry
  481. body starts at column 0, indentation is not changed at all."
  482. :group 'org-edit-structure
  483. :type 'boolean)
  484. (defcustom org-blank-before-new-entry '((heading . nil)
  485. (plain-list-item . nil))
  486. "Should `org-insert-heading' leave a blank line before new heading/item?
  487. The value is an alist, with `heading' and `plain-list-item' as car,
  488. and a boolean flag as cdr."
  489. :group 'org-edit-structure
  490. :type '(list
  491. (cons (const heading) (boolean))
  492. (cons (const plain-list-item) (boolean))))
  493. (defcustom org-insert-heading-hook nil
  494. "Hook being run after inserting a new heading."
  495. :group 'org-edit-structure
  496. :type 'hook)
  497. (defcustom org-enable-fixed-width-editor t
  498. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  499. This currently only means, they are never auto-wrapped.
  500. When nil, such lines will be treated like ordinary lines.
  501. See also the QUOTE keyword."
  502. :group 'org-edit-structure
  503. :type 'boolean)
  504. (defgroup org-sparse-trees nil
  505. "Options concerning sparse trees in Org-mode."
  506. :tag "Org Sparse Trees"
  507. :group 'org-structure)
  508. (defcustom org-highlight-sparse-tree-matches t
  509. "Non-nil means, highlight all matches that define a sparse tree.
  510. The highlights will automatically disappear the next time the buffer is
  511. changed by an edit command."
  512. :group 'org-sparse-trees
  513. :type 'boolean)
  514. (defcustom org-remove-highlights-with-change t
  515. "Non-nil means, any change to the buffer will remove temporary highlights.
  516. Such highlights are created by `org-occur' and `org-clock-display'.
  517. When nil, `C-c C-c needs to be used to get rid of the highlights.
  518. The highlights created by `org-preview-latex-fragment' always need
  519. `C-c C-c' to be removed."
  520. :group 'org-sparse-trees
  521. :group 'org-time
  522. :type 'boolean)
  523. (defcustom org-occur-hook '(org-first-headline-recenter)
  524. "Hook that is run after `org-occur' has constructed a sparse tree.
  525. This can be used to recenter the window to show as much of the structure
  526. as possible."
  527. :group 'org-sparse-trees
  528. :type 'hook)
  529. (defgroup org-plain-lists nil
  530. "Options concerning plain lists in Org-mode."
  531. :tag "Org Plain lists"
  532. :group 'org-structure)
  533. (defcustom org-cycle-include-plain-lists nil
  534. "Non-nil means, include plain lists into visibility cycling.
  535. This means that during cycling, plain list items will *temporarily* be
  536. interpreted as outline headlines with a level given by 1000+i where i is the
  537. indentation of the bullet. In all other operations, plain list items are
  538. not seen as headlines. For example, you cannot assign a TODO keyword to
  539. such an item."
  540. :group 'org-plain-lists
  541. :type 'boolean)
  542. (defcustom org-plain-list-ordered-item-terminator t
  543. "The character that makes a line with leading number an ordered list item.
  544. Valid values are ?. and ?\). To get both terminators, use t. While
  545. ?. may look nicer, it creates the danger that a line with leading
  546. number may be incorrectly interpreted as an item. ?\) therefore is
  547. the safe choice."
  548. :group 'org-plain-lists
  549. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  550. (const :tag "paren like in \"2)\"" ?\))
  551. (const :tab "both" t)))
  552. (defcustom org-auto-renumber-ordered-lists t
  553. "Non-nil means, automatically renumber ordered plain lists.
  554. Renumbering happens when the sequence have been changed with
  555. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  556. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  557. :group 'org-plain-lists
  558. :type 'boolean)
  559. (defcustom org-provide-checkbox-statistics t
  560. "Non-nil means, update checkbox statistics after insert and toggle.
  561. When this is set, checkbox statistics is updated each time you either insert
  562. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  563. with \\[org-ctrl-c-ctrl-c\\]."
  564. :group 'org-plain-lists
  565. :type 'boolean)
  566. (defgroup org-archive nil
  567. "Options concerning archiving in Org-mode."
  568. :tag "Org Archive"
  569. :group 'org-structure)
  570. (defcustom org-archive-tag "ARCHIVE"
  571. "The tag that marks a subtree as archived.
  572. An archived subtree does not open during visibility cycling, and does
  573. not contribute to the agenda listings.
  574. After changing this, font-lock must be restarted in the relevant buffers to
  575. get the proper fontification."
  576. :group 'org-archive
  577. :group 'org-keywords
  578. :type 'string)
  579. (defcustom org-agenda-skip-archived-trees t
  580. "Non-nil means, the agenda will skip any items located in archived trees.
  581. An archived tree is a tree marked with the tag ARCHIVE."
  582. :group 'org-archive
  583. :group 'org-agenda-skip
  584. :type 'boolean)
  585. (defcustom org-cycle-open-archived-trees nil
  586. "Non-nil means, `org-cycle' will open archived trees.
  587. An archived tree is a tree marked with the tag ARCHIVE.
  588. When nil, archived trees will stay folded. You can still open them with
  589. normal outline commands like `show-all', but not with the cycling commands."
  590. :group 'org-archive
  591. :group 'org-cycle
  592. :type 'boolean)
  593. (defcustom org-sparse-tree-open-archived-trees nil
  594. "Non-nil means sparse tree construction shows matches in archived trees.
  595. When nil, matches in these trees are highlighted, but the trees are kept in
  596. collapsed state."
  597. :group 'org-archive
  598. :group 'org-sparse-trees
  599. :type 'boolean)
  600. (defcustom org-archive-location "%s_archive::"
  601. "The location where subtrees should be archived.
  602. This string consists of two parts, separated by a double-colon.
  603. The first part is a file name - when omitted, archiving happens in the same
  604. file. %s will be replaced by the current file name (without directory part).
  605. Archiving to a different file is useful to keep archived entries from
  606. contributing to the Org-mode Agenda.
  607. The part after the double colon is a headline. The archived entries will be
  608. filed under that headline. When omitted, the subtrees are simply filed away
  609. at the end of the file, as top-level entries.
  610. Here are a few examples:
  611. \"%s_archive::\"
  612. If the current file is Projects.org, archive in file
  613. Projects.org_archive, as top-level trees. This is the default.
  614. \"::* Archived Tasks\"
  615. Archive in the current file, under the top-level headline
  616. \"* Archived Tasks\".
  617. \"~/org/archive.org::\"
  618. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  619. \"basement::** Finished Tasks\"
  620. Archive in file ./basement (relative path), as level 3 trees
  621. below the level 2 heading \"** Finished Tasks\".
  622. You may set this option on a per-file basis by adding to the buffer a
  623. line like
  624. #+ARCHIVE: basement::** Finished Tasks"
  625. :group 'org-archive
  626. :type 'string)
  627. (defcustom org-archive-mark-done t
  628. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  629. This can be a string to set the keyword to use. When t, Org-mode will
  630. use the first keyword in its list that means done."
  631. :group 'org-archive
  632. :type '(choice
  633. (const :tag "No" nil)
  634. (const :tag "Yes" t)
  635. (string :tag "Use this keyword")))
  636. (defcustom org-archive-stamp-time t
  637. "Non-nil means, add a time stamp to entries moved to an archive file.
  638. This variable is obsolete and has no effect anymore, instead add ot remove
  639. `time' from the variablle `org-archive-save-context-info'."
  640. :group 'org-archive
  641. :type 'boolean)
  642. (defcustom org-archive-save-context-info '(time file category todo itags)
  643. "Parts of context info that should be stored as properties when archiving.
  644. When a subtree is moved to an archive file, it looses information given by
  645. context, like inherited tags, the category, and possibly also the TODO
  646. state (depending on the variable `org-archive-mark-done').
  647. This variable can be a list of any of the following symbols:
  648. time The time of archiving.
  649. file The file where the entry originates.
  650. itags The local tags, in the headline of the subtree.
  651. ltags The tags the subtree inherits from further up the hierarchy.
  652. todo The pre-archive TODO state.
  653. category The category, taken from file name or #+CATEGORY lines.
  654. For each symbol present in the list, a property will be created in
  655. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  656. information."
  657. :group 'org-archive
  658. :type '(set :greedy t
  659. (const :tag "Time" time)
  660. (const :tag "File" file)
  661. (const :tag "Category" category)
  662. (const :tag "TODO state" todo)
  663. (const :tag "TODO state" priority)
  664. (const :tag "Inherited tags" itags)
  665. (const :tag "Local tags" ltags)))
  666. (defgroup org-imenu-and-speedbar nil
  667. "Options concerning imenu and speedbar in Org-mode."
  668. :tag "Org Imenu and Speedbar"
  669. :group 'org-structure)
  670. (defcustom org-imenu-depth 2
  671. "The maximum level for Imenu access to Org-mode headlines.
  672. This also applied for speedbar access."
  673. :group 'org-imenu-and-speedbar
  674. :type 'number)
  675. (defgroup org-table nil
  676. "Options concerning tables in Org-mode."
  677. :tag "Org Table"
  678. :group 'org)
  679. (defcustom org-enable-table-editor 'optimized
  680. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  681. When nil, such lines will be treated like ordinary lines.
  682. When equal to the symbol `optimized', the table editor will be optimized to
  683. do the following:
  684. - Automatic overwrite mode in front of whitespace in table fields.
  685. This makes the structure of the table stay in tact as long as the edited
  686. field does not exceed the column width.
  687. - Minimize the number of realigns. Normally, the table is aligned each time
  688. TAB or RET are pressed to move to another field. With optimization this
  689. happens only if changes to a field might have changed the column width.
  690. Optimization requires replacing the functions `self-insert-command',
  691. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  692. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  693. very good at guessing when a re-align will be necessary, but you can always
  694. force one with \\[org-ctrl-c-ctrl-c].
  695. If you would like to use the optimized version in Org-mode, but the
  696. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  697. This variable can be used to turn on and off the table editor during a session,
  698. but in order to toggle optimization, a restart is required.
  699. See also the variable `org-table-auto-blank-field'."
  700. :group 'org-table
  701. :type '(choice
  702. (const :tag "off" nil)
  703. (const :tag "on" t)
  704. (const :tag "on, optimized" optimized)))
  705. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  706. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  707. In the optimized version, the table editor takes over all simple keys that
  708. normally just insert a character. In tables, the characters are inserted
  709. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  710. for empty fields). Outside tables, the correct binding of the keys is
  711. restored.
  712. The default for this option is t if the optimized version is also used in
  713. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  714. this variable requires a restart of Emacs to become effective."
  715. :group 'org-table
  716. :type 'boolean)
  717. (defcustom orgtbl-radio-table-templates
  718. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  719. % END RECEIVE ORGTBL %n
  720. \\begin{comment}
  721. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  722. | | |
  723. \\end{comment}\n")
  724. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  725. @c END RECEIVE ORGTBL %n
  726. @ignore
  727. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  728. | | |
  729. @end ignore\n")
  730. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  731. <!-- END RECEIVE ORGTBL %n -->
  732. <!--
  733. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  734. | | |
  735. -->\n"))
  736. "Templates for radio tables in different major modes.
  737. All occurrences of %n in a template will be replaced with the name of the
  738. table, obtained by prompting the user."
  739. :group 'org-table
  740. :type '(repeat
  741. (list (symbol :tag "Major mode")
  742. (string :tag "Format"))))
  743. (defgroup org-table-settings nil
  744. "Settings for tables in Org-mode."
  745. :tag "Org Table Settings"
  746. :group 'org-table)
  747. (defcustom org-table-default-size "5x2"
  748. "The default size for newly created tables, Columns x Rows."
  749. :group 'org-table-settings
  750. :type 'string)
  751. (defcustom org-table-number-regexp
  752. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  753. "Regular expression for recognizing numbers in table columns.
  754. If a table column contains mostly numbers, it will be aligned to the
  755. right. If not, it will be aligned to the left.
  756. The default value of this option is a regular expression which allows
  757. anything which looks remotely like a number as used in scientific
  758. context. For example, all of the following will be considered a
  759. number:
  760. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  761. Other options offered by the customize interface are more restrictive."
  762. :group 'org-table-settings
  763. :type '(choice
  764. (const :tag "Positive Integers"
  765. "^[0-9]+$")
  766. (const :tag "Integers"
  767. "^[-+]?[0-9]+$")
  768. (const :tag "Floating Point Numbers"
  769. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  770. (const :tag "Floating Point Number or Integer"
  771. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  772. (const :tag "Exponential, Floating point, Integer"
  773. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  774. (const :tag "Very General Number-Like, including hex"
  775. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  776. (string :tag "Regexp:")))
  777. (defcustom org-table-number-fraction 0.5
  778. "Fraction of numbers in a column required to make the column align right.
  779. In a column all non-white fields are considered. If at least this
  780. fraction of fields is matched by `org-table-number-fraction',
  781. alignment to the right border applies."
  782. :group 'org-table-settings
  783. :type 'number)
  784. (defgroup org-table-editing nil
  785. "Behavior of tables during editing in Org-mode."
  786. :tag "Org Table Editing"
  787. :group 'org-table)
  788. (defcustom org-table-automatic-realign t
  789. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  790. When nil, aligning is only done with \\[org-table-align], or after column
  791. removal/insertion."
  792. :group 'org-table-editing
  793. :type 'boolean)
  794. (defcustom org-table-auto-blank-field t
  795. "Non-nil means, automatically blank table field when starting to type into it.
  796. This only happens when typing immediately after a field motion
  797. command (TAB, S-TAB or RET).
  798. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  799. :group 'org-table-editing
  800. :type 'boolean)
  801. (defcustom org-table-tab-jumps-over-hlines t
  802. "Non-nil means, tab in the last column of a table with jump over a hline.
  803. If a horizontal separator line is following the current line,
  804. `org-table-next-field' can either create a new row before that line, or jump
  805. over the line. When this option is nil, a new line will be created before
  806. this line."
  807. :group 'org-table-editing
  808. :type 'boolean)
  809. (defcustom org-table-tab-recognizes-table.el t
  810. "Non-nil means, TAB will automatically notice a table.el table.
  811. When it sees such a table, it moves point into it and - if necessary -
  812. calls `table-recognize-table'."
  813. :group 'org-table-editing
  814. :type 'boolean)
  815. (defgroup org-table-calculation nil
  816. "Options concerning tables in Org-mode."
  817. :tag "Org Table Calculation"
  818. :group 'org-table)
  819. (defcustom org-table-use-standard-references t
  820. "Should org-mode work with table refrences like B3 instead of @3$2?
  821. Possible values are:
  822. nil never use them
  823. from accept as input, do not present for editing
  824. t: accept as input and present for editing"
  825. :group 'org-table-calculation
  826. :type '(choice
  827. (const :tag "Never, don't even check unser input for them" nil)
  828. (const :tag "Always, both as user input, and when editing" t)
  829. (const :tag "Convert user input, don't offer during editing" 'from)))
  830. (defcustom org-table-copy-increment t
  831. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  832. :group 'org-table-calculation
  833. :type 'boolean)
  834. (defcustom org-calc-default-modes
  835. '(calc-internal-prec 12
  836. calc-float-format (float 5)
  837. calc-angle-mode deg
  838. calc-prefer-frac nil
  839. calc-symbolic-mode nil
  840. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  841. calc-display-working-message t
  842. )
  843. "List with Calc mode settings for use in calc-eval for table formulas.
  844. The list must contain alternating symbols (Calc modes variables and values).
  845. Don't remove any of the default settings, just change the values. Org-mode
  846. relies on the variables to be present in the list."
  847. :group 'org-table-calculation
  848. :type 'plist)
  849. (defcustom org-table-formula-evaluate-inline t
  850. "Non-nil means, TAB and RET evaluate a formula in current table field.
  851. If the current field starts with an equal sign, it is assumed to be a formula
  852. which should be evaluated as described in the manual and in the documentation
  853. string of the command `org-table-eval-formula'. This feature requires the
  854. Emacs calc package.
  855. When this variable is nil, formula calculation is only available through
  856. the command \\[org-table-eval-formula]."
  857. :group 'org-table-calculation
  858. :type 'boolean)
  859. (defcustom org-table-formula-use-constants t
  860. "Non-nil means, interpret constants in formulas in tables.
  861. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  862. by the value given in `org-table-formula-constants', or by a value obtained
  863. from the `constants.el' package."
  864. :group 'org-table-calculation
  865. :type 'boolean)
  866. (defcustom org-table-formula-constants nil
  867. "Alist with constant names and values, for use in table formulas.
  868. The car of each element is a name of a constant, without the `$' before it.
  869. The cdr is the value as a string. For example, if you'd like to use the
  870. speed of light in a formula, you would configure
  871. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  872. and then use it in an equation like `$1*$c'.
  873. Constants can also be defined on a per-file basis using a line like
  874. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  875. :group 'org-table-calculation
  876. :type '(repeat
  877. (cons (string :tag "name")
  878. (string :tag "value"))))
  879. (defvar org-table-formula-constants-local nil
  880. "Local version of `org-table-formula-constants'.")
  881. (make-variable-buffer-local 'org-table-formula-constants-local)
  882. (defcustom org-table-allow-automatic-line-recalculation t
  883. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  884. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  885. :group 'org-table-calculation
  886. :type 'boolean)
  887. (defgroup org-link nil
  888. "Options concerning links in Org-mode."
  889. :tag "Org Link"
  890. :group 'org)
  891. (defvar org-link-abbrev-alist-local nil
  892. "Buffer-local version of `org-link-abbrev-alist', which see.
  893. The value of this is taken from the #+LINK lines.")
  894. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  895. (defcustom org-link-abbrev-alist nil
  896. "Alist of link abbreviations.
  897. The car of each element is a string, to be replaced at the start of a link.
  898. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  899. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  900. [[linkkey:tag][description]]
  901. If REPLACE is a string, the tag will simply be appended to create the link.
  902. If the string contains \"%s\", the tag will be inserted there.
  903. REPLACE may also be a function that will be called with the tag as the
  904. only argument to create the link, which should be returned as a string.
  905. See the manual for examples."
  906. :group 'org-link
  907. :type 'alist)
  908. (defcustom org-descriptive-links t
  909. "Non-nil means, hide link part and only show description of bracket links.
  910. Bracket links are like [[link][descritpion]]. This variable sets the initial
  911. state in new org-mode buffers. The setting can then be toggled on a
  912. per-buffer basis from the Org->Hyperlinks menu."
  913. :group 'org-link
  914. :type 'boolean)
  915. (defcustom org-link-file-path-type 'adaptive
  916. "How the path name in file links should be stored.
  917. Valid values are:
  918. relative relative to the current directory, i.e. the directory of the file
  919. into which the link is being inserted.
  920. absolute absolute path, if possible with ~ for home directory.
  921. noabbrev absolute path, no abbreviation of home directory.
  922. adaptive Use relative path for files in the current directory and sub-
  923. directories of it. For other files, use an absolute path."
  924. :group 'org-link
  925. :type '(choice
  926. (const relative)
  927. (const absolute)
  928. (const noabbrev)
  929. (const adaptive)))
  930. (defcustom org-activate-links '(bracket angle plain radio tag date)
  931. "Types of links that should be activated in Org-mode files.
  932. This is a list of symbols, each leading to the activation of a certain link
  933. type. In principle, it does not hurt to turn on most link types - there may
  934. be a small gain when turning off unused link types. The types are:
  935. bracket The recommended [[link][description]] or [[link]] links with hiding.
  936. angular Links in angular brackes that may contain whitespace like
  937. <bbdb:Carsten Dominik>.
  938. plain Plain links in normal text, no whitespace, like http://google.com.
  939. radio Text that is matched by a radio target, see manual for details.
  940. tag Tag settings in a headline (link to tag search).
  941. date Time stamps (link to calendar).
  942. Changing this variable requires a restart of Emacs to become effective."
  943. :group 'org-link
  944. :type '(set (const :tag "Double bracket links (new style)" bracket)
  945. (const :tag "Angular bracket links (old style)" angular)
  946. (const :tag "plain text links" plain)
  947. (const :tag "Radio target matches" radio)
  948. (const :tag "Tags" tag)
  949. (const :tag "Tags" target)
  950. (const :tag "Timestamps" date)))
  951. (defgroup org-link-store nil
  952. "Options concerning storing links in Org-mode"
  953. :tag "Org Store Link"
  954. :group 'org-link)
  955. (defcustom org-email-link-description-format "Email %c: %.30s"
  956. "Format of the description part of a link to an email or usenet message.
  957. The following %-excapes will be replaced by corresponding information:
  958. %F full \"From\" field
  959. %f name, taken from \"From\" field, address if no name
  960. %T full \"To\" field
  961. %t first name in \"To\" field, address if no name
  962. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  963. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  964. %s subject
  965. %m message-id.
  966. You may use normal field width specification between the % and the letter.
  967. This is for example useful to limit the length of the subject.
  968. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  969. :group 'org-link-store
  970. :type 'string)
  971. (defcustom org-from-is-user-regexp
  972. (let (r1 r2)
  973. (when (and user-mail-address (not (string= user-mail-address "")))
  974. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  975. (when (and user-full-name (not (string= user-full-name "")))
  976. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  977. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  978. "Regexp mached against the \"From:\" header of an email or usenet message.
  979. It should match if the message is from the user him/herself."
  980. :group 'org-link-store
  981. :type 'regexp)
  982. (defcustom org-context-in-file-links t
  983. "Non-nil means, file links from `org-store-link' contain context.
  984. A search string will be added to the file name with :: as separator and
  985. used to find the context when the link is activated by the command
  986. `org-open-at-point'.
  987. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  988. negates this setting for the duration of the command."
  989. :group 'org-link-store
  990. :type 'boolean)
  991. (defcustom org-keep-stored-link-after-insertion nil
  992. "Non-nil means, keep link in list for entire session.
  993. The command `org-store-link' adds a link pointing to the current
  994. location to an internal list. These links accumulate during a session.
  995. The command `org-insert-link' can be used to insert links into any
  996. Org-mode file (offering completion for all stored links). When this
  997. option is nil, every link which has been inserted once using \\[org-insert-link]
  998. will be removed from the list, to make completing the unused links
  999. more efficient."
  1000. :group 'org-link-store
  1001. :type 'boolean)
  1002. (defcustom org-usenet-links-prefer-google nil
  1003. "Non-nil means, `org-store-link' will create web links to Google groups.
  1004. When nil, Gnus will be used for such links.
  1005. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1006. negates this setting for the duration of the command."
  1007. :group 'org-link-store
  1008. :type 'boolean)
  1009. (defgroup org-link-follow nil
  1010. "Options concerning following links in Org-mode"
  1011. :tag "Org Follow Link"
  1012. :group 'org-link)
  1013. (defcustom org-tab-follows-link nil
  1014. "Non-nil means, on links TAB will follow the link.
  1015. Needs to be set before org.el is loaded."
  1016. :group 'org-link-follow
  1017. :type 'boolean)
  1018. (defcustom org-return-follows-link nil
  1019. "Non-nil means, on links RET will follow the link.
  1020. Needs to be set before org.el is loaded."
  1021. :group 'org-link-follow
  1022. :type 'boolean)
  1023. (defcustom org-mouse-1-follows-link t
  1024. "Non-nil means, mouse-1 on a link will follow the link.
  1025. A longer mouse click will still set point. Does not wortk on XEmacs.
  1026. Needs to be set before org.el is loaded."
  1027. :group 'org-link-follow
  1028. :type 'boolean)
  1029. (defcustom org-mark-ring-length 4
  1030. "Number of different positions to be recorded in the ring
  1031. Changing this requires a restart of Emacs to work correctly."
  1032. :group 'org-link-follow
  1033. :type 'interger)
  1034. (defcustom org-link-frame-setup
  1035. '((vm . vm-visit-folder-other-frame)
  1036. (gnus . gnus-other-frame)
  1037. (file . find-file-other-window))
  1038. "Setup the frame configuration for following links.
  1039. When following a link with Emacs, it may often be useful to display
  1040. this link in another window or frame. This variable can be used to
  1041. set this up for the different types of links.
  1042. For VM, use any of
  1043. `vm-visit-folder'
  1044. `vm-visit-folder-other-frame'
  1045. For Gnus, use any of
  1046. `gnus'
  1047. `gnus-other-frame'
  1048. For FILE, use any of
  1049. `find-file'
  1050. `find-file-other-window'
  1051. `find-file-other-frame'
  1052. For the calendar, use the variable `calendar-setup'.
  1053. For BBDB, it is currently only possible to display the matches in
  1054. another window."
  1055. :group 'org-link-follow
  1056. :type '(list
  1057. (cons (const vm)
  1058. (choice
  1059. (const vm-visit-folder)
  1060. (const vm-visit-folder-other-window)
  1061. (const vm-visit-folder-other-frame)))
  1062. (cons (const gnus)
  1063. (choice
  1064. (const gnus)
  1065. (const gnus-other-frame)))
  1066. (cons (const file)
  1067. (choice
  1068. (const find-file)
  1069. (const find-file-other-window)
  1070. (const find-file-other-frame)))))
  1071. (defcustom org-display-internal-link-with-indirect-buffer nil
  1072. "Non-nil means, use indirect buffer to display infile links.
  1073. Activating internal links (from one location in a file to another location
  1074. in the same file) normally just jumps to the location. When the link is
  1075. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1076. another window. When this option is set, the other window actually displays
  1077. an indirect buffer clone of the current buffer, to avoid any visibility
  1078. changes to the current buffer."
  1079. :group 'org-link-follow
  1080. :type 'boolean)
  1081. (defcustom org-open-non-existing-files nil
  1082. "Non-nil means, `org-open-file' will open non-existing files.
  1083. When nil, an error will be generated."
  1084. :group 'org-link-follow
  1085. :type 'boolean)
  1086. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1087. "Function and arguments to call for following mailto links.
  1088. This is a list with the first element being a lisp function, and the
  1089. remaining elements being arguments to the function. In string arguments,
  1090. %a will be replaced by the address, and %s will be replaced by the subject
  1091. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1092. :group 'org-link-follow
  1093. :type '(choice
  1094. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1095. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1096. (const :tag "message-mail" (message-mail "%a" "%s"))
  1097. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1098. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1099. "Non-nil means, ask for confirmation before executing shell links.
  1100. Shell links can be dangerous: just think about a link
  1101. [[shell:rm -rf ~/*][Google Search]]
  1102. This link would show up in your Org-mode document as \"Google Search\",
  1103. but really it would remove your entire home directory.
  1104. Therefore we advise against setting this variable to nil.
  1105. Just change it to `y-or-n-p' of you want to confirm with a
  1106. single keystroke rather than having to type \"yes\"."
  1107. :group 'org-link-follow
  1108. :type '(choice
  1109. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1110. (const :tag "with y-or-n (faster)" y-or-n-p)
  1111. (const :tag "no confirmation (dangerous)" nil)))
  1112. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1113. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1114. Elisp links can be dangerous: just think about a link
  1115. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1116. This link would show up in your Org-mode document as \"Google Search\",
  1117. but really it would remove your entire home directory.
  1118. Therefore we advise against setting this variable to nil.
  1119. Just change it to `y-or-n-p' of you want to confirm with a
  1120. single keystroke rather than having to type \"yes\"."
  1121. :group 'org-link-follow
  1122. :type '(choice
  1123. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1124. (const :tag "with y-or-n (faster)" y-or-n-p)
  1125. (const :tag "no confirmation (dangerous)" nil)))
  1126. (defconst org-file-apps-defaults-gnu
  1127. '((remote . emacs)
  1128. (t . mailcap))
  1129. "Default file applications on a UNIX or GNU/Linux system.
  1130. See `org-file-apps'.")
  1131. (defconst org-file-apps-defaults-macosx
  1132. '((remote . emacs)
  1133. (t . "open %s")
  1134. ("ps" . "gv %s")
  1135. ("ps.gz" . "gv %s")
  1136. ("eps" . "gv %s")
  1137. ("eps.gz" . "gv %s")
  1138. ("dvi" . "xdvi %s")
  1139. ("fig" . "xfig %s"))
  1140. "Default file applications on a MacOS X system.
  1141. The system \"open\" is known as a default, but we use X11 applications
  1142. for some files for which the OS does not have a good default.
  1143. See `org-file-apps'.")
  1144. (defconst org-file-apps-defaults-windowsnt
  1145. (list
  1146. '(remote . emacs)
  1147. (cons t
  1148. (list (if (featurep 'xemacs)
  1149. 'mswindows-shell-execute
  1150. 'w32-shell-execute)
  1151. "open" 'file)))
  1152. "Default file applications on a Windows NT system.
  1153. The system \"open\" is used for most files.
  1154. See `org-file-apps'.")
  1155. (defcustom org-file-apps
  1156. '(
  1157. ("txt" . emacs)
  1158. ("tex" . emacs)
  1159. ("ltx" . emacs)
  1160. ("org" . emacs)
  1161. ("el" . emacs)
  1162. ("bib" . emacs)
  1163. )
  1164. "External applications for opening `file:path' items in a document.
  1165. Org-mode uses system defaults for different file types, but
  1166. you can use this variable to set the application for a given file
  1167. extension. The entries in this list are cons cells where the car identifies
  1168. files and the cdr the corresponding command. Possible values for the
  1169. file identifier are
  1170. \"ext\" A string identifying an extension
  1171. `directory' Matches a directory
  1172. `remote' Matches a remote file, accessible through tramp or efs.
  1173. Remote files most likely should be visited through Emacs
  1174. because external applications cannot handle such paths.
  1175. t Default for all remaining files
  1176. Possible values for the command are:
  1177. `emacs' The file will be visited by the current Emacs process.
  1178. `default' Use the default application for this file type.
  1179. string A command to be executed by a shell; %s will be replaced
  1180. by the path to the file.
  1181. sexp A Lisp form which will be evaluated. The file path will
  1182. be available in the Lisp variable `file'.
  1183. For more examples, see the system specific constants
  1184. `org-file-apps-defaults-macosx'
  1185. `org-file-apps-defaults-windowsnt'
  1186. `org-file-apps-defaults-gnu'."
  1187. :group 'org-link-follow
  1188. :type '(repeat
  1189. (cons (choice :value ""
  1190. (string :tag "Extension")
  1191. (const :tag "Default for unrecognized files" t)
  1192. (const :tag "Remote file" remote)
  1193. (const :tag "Links to a directory" directory))
  1194. (choice :value ""
  1195. (const :tag "Visit with Emacs" emacs)
  1196. (const :tag "Use system default" default)
  1197. (string :tag "Command")
  1198. (sexp :tag "Lisp form")))))
  1199. (defcustom org-mhe-search-all-folders nil
  1200. "Non-nil means, that the search for the mh-message will be extended to
  1201. all folders if the message cannot be found in the folder given in the link.
  1202. Searching all folders is very efficient with one of the search engines
  1203. supported by MH-E, but will be slow with pick."
  1204. :group 'org-link-follow
  1205. :type 'boolean)
  1206. (defgroup org-remember nil
  1207. "Options concerning interaction with remember.el."
  1208. :tag "Org Remember"
  1209. :group 'org)
  1210. (defcustom org-directory "~/org"
  1211. "Directory with org files.
  1212. This directory will be used as default to prompt for org files.
  1213. Used by the hooks for remember.el."
  1214. :group 'org-remember
  1215. :type 'directory)
  1216. (defcustom org-default-notes-file "~/.notes"
  1217. "Default target for storing notes.
  1218. Used by the hooks for remember.el. This can be a string, or nil to mean
  1219. the value of `remember-data-file'.
  1220. You can set this on a per-template basis with the variable
  1221. `org-remember-templates'."
  1222. :group 'org-remember
  1223. :type '(choice
  1224. (const :tag "Default from remember-data-file" nil)
  1225. file))
  1226. (defcustom org-remember-store-without-prompt t
  1227. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1228. In this case, you need `C-u C-c C-c' to get the prompts for
  1229. note file and headline.
  1230. When this variable is nil, `C-c C-c' give you the prompts, and
  1231. `C-u C-c C-c' trigger the fasttrack."
  1232. :group 'org-remember
  1233. :type 'boolean)
  1234. (defcustom org-remember-default-headline ""
  1235. "The headline that should be the default location in the notes file.
  1236. When filing remember notes, the cursor will start at that position.
  1237. You can set this on a per-template basis with the variable
  1238. `org-remember-templates'."
  1239. :group 'org-remember
  1240. :type 'string)
  1241. (defcustom org-remember-templates nil
  1242. "Templates for the creation of remember buffers.
  1243. When nil, just let remember make the buffer.
  1244. When not nil, this is a list of 5-element lists. In each entry, the first
  1245. element is a the name of the template, It should be a single short word.
  1246. The second element is a character, a unique key to select this template.
  1247. The third element is the template. The forth element is optional and can
  1248. specify a destination file for remember items created with this template.
  1249. The default file is given by `org-default-notes-file'. An optional fifth
  1250. element can specify the headline in that file that should be offered
  1251. first when the user is asked to file the entry. The default headline is
  1252. given in the variable `org-remember-default-headline'.
  1253. The template specifies the structure of the remember buffer. It should have
  1254. a first line starting with a star, to act as the org-mode headline.
  1255. Furthermore, the following %-escapes will be replaced with content:
  1256. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1257. A default value and a completion table ca be specified like this:
  1258. %^{prompt|default|completion2|completion3|...}
  1259. %t time stamp, date only
  1260. %T time stamp with date and time
  1261. %u, %U like the above, but inactive time stamps
  1262. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1263. You may define a prompt like %^{Please specify birthday}t
  1264. %n user name (taken from `user-full-name')
  1265. %a annotation, normally the link created with org-store-link
  1266. %i initial content, the region when remember is called with C-u.
  1267. If %i is indented, the entire inserted text will be indented
  1268. as well.
  1269. %c content of the clipboard, or current kill ring head
  1270. %^g prompt for tags, with completion on tags in target file
  1271. %^G prompt for tags, with completion all tags in all agenda files
  1272. %:keyword specific information for certain link types, see below
  1273. %[pathname] insert the contents of the file given by `pathname'
  1274. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1275. %! Store this note immediately after filling the template
  1276. %? After completing the template, position cursor here.
  1277. Apart from these general escapes, you can access information specific to the
  1278. link type that is created. For example, calling `remember' in emails or gnus
  1279. will record the author and the subject of the message, which you can access
  1280. with %:author and %:subject, respectively. Here is a complete list of what
  1281. is recorded for each link type.
  1282. Link type | Available information
  1283. -------------------+------------------------------------------------------
  1284. bbdb | %:type %:name %:company
  1285. vm, wl, mh, rmail | %:type %:subject %:message-id
  1286. | %:from %:fromname %:fromaddress
  1287. | %:to %:toname %:toaddress
  1288. | %:fromto (either \"to NAME\" or \"from NAME\")
  1289. gnus | %:group, for messages also all email fields
  1290. w3, w3m | %:type %:url
  1291. info | %:type %:file %:node
  1292. calendar | %:type %:date"
  1293. :group 'org-remember
  1294. :get (lambda (var) ; Make sure all entries have 5 elements
  1295. (mapcar (lambda (x)
  1296. (if (not (stringp (car x))) (setq x (cons "" x)))
  1297. (cond ((= (length x) 4) (append x '("")))
  1298. ((= (length x) 3) (append x '("" "")))
  1299. (t x)))
  1300. (default-value var)))
  1301. :type '(repeat
  1302. :tag "enabled"
  1303. (list :value ("" ?a "\n" nil nil)
  1304. (string :tag "Name")
  1305. (character :tag "Selection Key")
  1306. (string :tag "Template")
  1307. (choice
  1308. (file :tag "Destination file")
  1309. (const :tag "Prompt for file" nil))
  1310. (choice
  1311. (string :tag "Destination headline")
  1312. (const :tag "Selection interface for heading")))))
  1313. (defcustom org-reverse-note-order nil
  1314. "Non-nil means, store new notes at the beginning of a file or entry.
  1315. When nil, new notes will be filed to the end of a file or entry.
  1316. This can also be a list with cons cells of regular expressions that
  1317. are matched against file names, and values."
  1318. :group 'org-remember
  1319. :type '(choice
  1320. (const :tag "Reverse always" t)
  1321. (const :tag "Reverse never" nil)
  1322. (repeat :tag "By file name regexp"
  1323. (cons regexp boolean))))
  1324. (defcustom org-refile-targets '((nil . (:level . 1)))
  1325. "Targets for refiling entries with \\[org-refile].
  1326. This is list of cons cells. Each cell contains:
  1327. - a specification of the files to be considered, either a list of files,
  1328. or a symbol whose function or value fields will be used to retrieve
  1329. a file name or a list of file names. Nil means, refile to a different
  1330. heading in the current buffer.
  1331. - A specification of how to find candidate refile targets. This may be
  1332. any of
  1333. - a cons cell (:tag . \"TAG\") to identify refile targes by a tag.
  1334. This tag has to be present in all target headlines, inheritance will
  1335. not be considered.
  1336. - a cons cell (:todo . \"KEYWORD\" to identify refile targets by
  1337. todo keyword.
  1338. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1339. headlines that are refiling targets.
  1340. - a cons cell (:level . N). Any headline of level N is considered a target.
  1341. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1342. ;; FIXME: what if there are a var and func with same name???
  1343. :group 'org-remember
  1344. :type '(repeat
  1345. (cons
  1346. (choice :value org-agenda-files
  1347. (const :tag "All agenda files" org-agenda-files)
  1348. (const :tag "Current buffer" nil)
  1349. (function) (variable) (file))
  1350. (choice :tag "Identify target headline by"
  1351. (cons :tag "Specific tag" (const :tag) (string))
  1352. (cons :tag "TODO keyword" (const :todo) (string))
  1353. (cons :tag "Regular expression" (const :regexp) (regexp))
  1354. (cons :tag "Level number" (const :level) (integer))
  1355. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1356. (defcustom org-refile-use-outline-path nil
  1357. "Non-nil means, provide refile targets as paths.
  1358. So a level 3 headline will be available as level1/level2/level3.
  1359. When the value is `file', also include the file name (without directory)
  1360. into the path. When `full-file-path', include the full file path."
  1361. :group 'org-remember
  1362. :type '(choice
  1363. (const :tag "Not" nil)
  1364. (const :tag "Yes" t)
  1365. (const :tag "Start with file name" file)
  1366. (const :tag "Start with full file path" full-file-path)))
  1367. (defgroup org-todo nil
  1368. "Options concerning TODO items in Org-mode."
  1369. :tag "Org TODO"
  1370. :group 'org)
  1371. (defgroup org-progress nil
  1372. "Options concerning Progress logging in Org-mode."
  1373. :tag "Org Progress"
  1374. :group 'org-time)
  1375. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1376. "List of TODO entry keyword sequences and their interpretation.
  1377. \\<org-mode-map>This is a list of sequences.
  1378. Each sequence starts with a symbol, either `sequence' or `type',
  1379. indicating if the keywords should be interpreted as a sequence of
  1380. action steps, or as different types of TODO items. The first
  1381. keywords are states requiring action - these states will select a headline
  1382. for inclusion into the global TODO list Org-mode produces. If one of
  1383. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1384. signify that no further action is necessary. If \"|\" is not found,
  1385. the last keyword is treated as the only DONE state of the sequence.
  1386. The command \\[org-todo] cycles an entry through these states, and one
  1387. additional state where no keyword is present. For details about this
  1388. cycling, see the manual.
  1389. TODO keywords and interpretation can also be set on a per-file basis with
  1390. the special #+SEQ_TODO and #+TYP_TODO lines.
  1391. For backward compatibility, this variable may also be just a list
  1392. of keywords - in this case the interptetation (sequence or type) will be
  1393. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1394. :group 'org-todo
  1395. :group 'org-keywords
  1396. :type '(choice
  1397. (repeat :tag "Old syntax, just keywords"
  1398. (string :tag "Keyword"))
  1399. (repeat :tag "New syntax"
  1400. (cons
  1401. (choice
  1402. :tag "Interpretation"
  1403. (const :tag "Sequence (cycling hits every state)" sequence)
  1404. (const :tag "Type (cycling directly to DONE)" type))
  1405. (repeat
  1406. (string :tag "Keyword"))))))
  1407. (defvar org-todo-keywords-1 nil)
  1408. (make-variable-buffer-local 'org-todo-keywords-1)
  1409. (defvar org-todo-keywords-for-agenda nil)
  1410. (defvar org-done-keywords-for-agenda nil)
  1411. (defvar org-not-done-keywords nil)
  1412. (make-variable-buffer-local 'org-not-done-keywords)
  1413. (defvar org-done-keywords nil)
  1414. (make-variable-buffer-local 'org-done-keywords)
  1415. (defvar org-todo-heads nil)
  1416. (make-variable-buffer-local 'org-todo-heads)
  1417. (defvar org-todo-sets nil)
  1418. (make-variable-buffer-local 'org-todo-sets)
  1419. (defvar org-todo-log-states nil)
  1420. (make-variable-buffer-local 'org-todo-log-states)
  1421. (defvar org-todo-kwd-alist nil)
  1422. (make-variable-buffer-local 'org-todo-kwd-alist)
  1423. (defvar org-todo-key-alist nil)
  1424. (make-variable-buffer-local 'org-todo-key-alist)
  1425. (defvar org-todo-key-trigger nil)
  1426. (make-variable-buffer-local 'org-todo-key-trigger)
  1427. (defcustom org-todo-interpretation 'sequence
  1428. "Controls how TODO keywords are interpreted.
  1429. This variable is in principle obsolete and is only used for
  1430. backward compatibility, if the interpretation of todo keywords is
  1431. not given already in `org-todo-keywords'. See that variable for
  1432. more information."
  1433. :group 'org-todo
  1434. :group 'org-keywords
  1435. :type '(choice (const sequence)
  1436. (const type)))
  1437. (defcustom org-use-fast-todo-selection 'prefix
  1438. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1439. This variable describes if and under what circumstances the cycling
  1440. mechanism for TODO keywords will be replaced by a single-key, direct
  1441. selection scheme.
  1442. When nil, fast selection is never used.
  1443. When the symbol `prefix', it will be used when `org-todo' is called with
  1444. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1445. in an agenda buffer.
  1446. When t, fast selection is used by default. In this case, the prefix
  1447. argument forces cycling instead.
  1448. In all cases, the special interface is only used if access keys have actually
  1449. been assigned by the user, i.e. if keywords in the configuration are followed
  1450. by a letter in parenthesis, like TODO(t)."
  1451. :group 'org-todo
  1452. :type '(choice
  1453. (const :tag "Never" nil)
  1454. (const :tag "By default" t)
  1455. (const :tag "Only with C-u C-c C-t" prefix)))
  1456. (defcustom org-after-todo-state-change-hook nil
  1457. "Hook which is run after the state of a TODO item was changed.
  1458. The new state (a string with a TODO keyword, or nil) is available in the
  1459. Lisp variable `state'."
  1460. :group 'org-todo
  1461. :type 'hook)
  1462. (defcustom org-log-done nil
  1463. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1464. When the state of an entry is changed from nothing or a DONE state to
  1465. a not-done TODO state, remove a previous closing date.
  1466. This can also be a list of symbols indicating under which conditions
  1467. the time stamp recording the action should be annotated with a short note.
  1468. Valid members of this list are
  1469. done Offer to record a note when marking entries done
  1470. state Offer to record a note whenever changing the TODO state
  1471. of an item. This is only relevant if TODO keywords are
  1472. interpreted as sequence, see variable `org-todo-interpretation'.
  1473. When `state' is set, this includes tracking `done'.
  1474. clock-out Offer to record a note when clocking out of an item.
  1475. A separate window will then pop up and allow you to type a note.
  1476. After finishing with C-c C-c, the note will be added directly after the
  1477. timestamp, as a plain list item. See also the variable
  1478. `org-log-note-headings'.
  1479. Logging can also be configured on a per-file basis by adding one of
  1480. the following lines anywhere in the buffer:
  1481. #+STARTUP: logdone
  1482. #+STARTUP: nologging
  1483. #+STARTUP: lognotedone
  1484. #+STARTUP: lognotestate
  1485. #+STARTUP: lognoteclock-out
  1486. You can have local logging settings for a subtree by setting the LOGGING
  1487. property to one or more of these keywords."
  1488. :group 'org-todo
  1489. :group 'org-progress
  1490. :type '(choice
  1491. (const :tag "off" nil)
  1492. (const :tag "on" t)
  1493. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1494. (const :tag "when item is marked DONE" done)
  1495. (const :tag "when TODO state changes" state)
  1496. (const :tag "when clocking out" clock-out))))
  1497. (defcustom org-log-done-with-time t
  1498. "Non-nil means, the CLOSED time stamp will contain date and time.
  1499. When nil, only the date will be recorded."
  1500. :group 'org-progress
  1501. :type 'boolean)
  1502. (defcustom org-log-note-headings
  1503. '((done . "CLOSING NOTE %t")
  1504. (state . "State %-12s %t")
  1505. (clock-out . ""))
  1506. "Headings for notes added when clocking out or closing TODO items.
  1507. The value is an alist, with the car being a symbol indicating the note
  1508. context, and the cdr is the heading to be used. The heading may also be the
  1509. empty string.
  1510. %t in the heading will be replaced by a time stamp.
  1511. %s will be replaced by the new TODO state, in double quotes.
  1512. %u will be replaced by the user name.
  1513. %U will be replaced by the full user name."
  1514. :group 'org-todo
  1515. :group 'org-progress
  1516. :type '(list :greedy t
  1517. (cons (const :tag "Heading when closing an item" done) string)
  1518. (cons (const :tag
  1519. "Heading when changing todo state (todo sequence only)"
  1520. state) string)
  1521. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1522. (defcustom org-log-states-order-reversed t
  1523. "Non-nil means, the latest state change note will be directly after heading.
  1524. When nil, the notes will be orderer according to time."
  1525. :group 'org-todo
  1526. :group 'org-progress
  1527. :type 'boolean)
  1528. (defcustom org-log-repeat t
  1529. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1530. When nil, no note will be taken.
  1531. This option can also be set with on a per-file-basis with
  1532. #+STARTUP: logrepeat
  1533. #+STARTUP: nologrepeat
  1534. You can have local logging settings for a subtree by setting the LOGGING
  1535. property to one or more of these keywords."
  1536. :group 'org-todo
  1537. :group 'org-progress
  1538. :type 'boolean)
  1539. (defcustom org-clock-into-drawer 2
  1540. "Should clocking info be wrapped into a drawer?
  1541. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1542. If necessary, the drawer will be created.
  1543. When nil, the drawer will not be created, but used when present.
  1544. When an integer and the number of clocking entries in an item
  1545. reaches or exceeds this number, a drawer will be created."
  1546. :group 'org-todo
  1547. :group 'org-progress
  1548. :type '(choice
  1549. (const :tag "Always" t)
  1550. (const :tag "Only when drawer exists" nil)
  1551. (integer :tag "When at least N clock entries")))
  1552. (defcustom org-clock-out-when-done t
  1553. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1554. Nil means, clock will keep running until stopped explicitly with
  1555. `C-c C-x C-o', or until the clock is started in a different item."
  1556. :group 'org-progress
  1557. :type 'boolean)
  1558. (defcustom org-clock-in-switch-to-state nil
  1559. "Set task to a special todo state while clocking it.
  1560. The value should be the state to which the entry should be switched."
  1561. :group 'org-progress
  1562. :group 'org-todo
  1563. :type '(choice
  1564. (const :tag "Don't force a state" nil)
  1565. (string :tag "State")))
  1566. (defgroup org-priorities nil
  1567. "Priorities in Org-mode."
  1568. :tag "Org Priorities"
  1569. :group 'org-todo)
  1570. (defcustom org-highest-priority ?A
  1571. "The highest priority of TODO items. A character like ?A, ?B etc.
  1572. Must have a smaller ASCII number than `org-lowest-priority'."
  1573. :group 'org-priorities
  1574. :type 'character)
  1575. (defcustom org-lowest-priority ?C
  1576. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1577. Must have a larger ASCII number than `org-highest-priority'."
  1578. :group 'org-priorities
  1579. :type 'character)
  1580. (defcustom org-default-priority ?B
  1581. "The default priority of TODO items.
  1582. This is the priority an item get if no explicit priority is given."
  1583. :group 'org-priorities
  1584. :type 'character)
  1585. (defcustom org-priority-start-cycle-with-default t
  1586. "Non-nil means, start with default priority when starting to cycle.
  1587. When this is nil, the first step in the cycle will be (depending on the
  1588. command used) one higher or lower that the default priority."
  1589. :group 'org-priorities
  1590. :type 'boolean)
  1591. (defgroup org-time nil
  1592. "Options concerning time stamps and deadlines in Org-mode."
  1593. :tag "Org Time"
  1594. :group 'org)
  1595. (defcustom org-insert-labeled-timestamps-at-point nil
  1596. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1597. When nil, these labeled time stamps are forces into the second line of an
  1598. entry, just after the headline. When scheduling from the global TODO list,
  1599. the time stamp will always be forced into the second line."
  1600. :group 'org-time
  1601. :type 'boolean)
  1602. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1603. "Formats for `format-time-string' which are used for time stamps.
  1604. It is not recommended to change this constant.")
  1605. (defcustom org-time-stamp-rounding-minutes 0
  1606. "Number of minutes to round time stamps to upon insertion.
  1607. When zero, insert the time unmodified. Useful rounding numbers
  1608. should be factors of 60, so for example 5, 10, 15.
  1609. When this is not zero, you can still force an exact time-stamp by using
  1610. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1611. :group 'org-time
  1612. :type 'integer)
  1613. (defcustom org-display-custom-times nil
  1614. "Non-nil means, overlay custom formats over all time stamps.
  1615. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1616. To turn this on on a per-file basis, insert anywhere in the file:
  1617. #+STARTUP: customtime"
  1618. :group 'org-time
  1619. :set 'set-default
  1620. :type 'sexp)
  1621. (make-variable-buffer-local 'org-display-custom-times)
  1622. (defcustom org-time-stamp-custom-formats
  1623. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1624. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1625. These are overlayed over the default ISO format if the variable
  1626. `org-display-custom-times' is set. Time like %H:%M should be at the
  1627. end of the second format."
  1628. :group 'org-time
  1629. :type 'sexp)
  1630. (defun org-time-stamp-format (&optional long inactive)
  1631. "Get the right format for a time string."
  1632. (let ((f (if long (cdr org-time-stamp-formats)
  1633. (car org-time-stamp-formats))))
  1634. (if inactive
  1635. (concat "[" (substring f 1 -1) "]")
  1636. f)))
  1637. (defcustom org-read-date-prefer-future t
  1638. "Non-nil means, assume future for incomplete date input from user.
  1639. This affects the following situations:
  1640. 1. The user gives a day, but no month.
  1641. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1642. read this as the third of *next* month. However, if you enter \"17\",
  1643. it will be considered as *this* month.
  1644. 2. The user gives a month but not a year.
  1645. For example, if it is april and you enter \"feb 2\", this will be read
  1646. as feb 2, *next* year. \"May 5\", however, will be this year.
  1647. When this option is nil, the current month and year will always be used
  1648. as defaults."
  1649. :group 'org-time
  1650. :type 'boolean)
  1651. (defcustom org-read-date-display-live t
  1652. "Non-nil means, display current interpretation of date prompt live.
  1653. This display will be in an overlay, in the minibuffer."
  1654. :group 'org-time
  1655. :type 'boolean)
  1656. (defcustom org-read-date-popup-calendar t
  1657. "Non-nil means, pop up a calendar when prompting for a date.
  1658. In the calendar, the date can be selected with mouse-1. However, the
  1659. minibuffer will also be active, and you can simply enter the date as well.
  1660. When nil, only the minibuffer will be available."
  1661. :group 'org-time
  1662. :type 'boolean)
  1663. (if (fboundp 'defvaralias)
  1664. (defvaralias 'org-popup-calendar-for-date-prompt
  1665. 'org-read-date-popup-calendar))
  1666. (defcustom org-extend-today-until 0
  1667. "The hour when your day really ends.
  1668. This has influence for the following applications:
  1669. - When switching the agenda to \"today\". It it is still earlier than
  1670. the time given here, the day recognized as TODAY is actually yesterday.
  1671. - When a date is read from the user and it is still before the time given
  1672. here, the current date and time will be assumed to be yesterday, 23:59.
  1673. FIXME:
  1674. IMPORTANT: This is still a very experimental feature, it may disappear
  1675. again or it may be extended to mean more things."
  1676. :group 'org-time
  1677. :type 'number)
  1678. (defcustom org-edit-timestamp-down-means-later nil
  1679. "Non-nil means, S-down will increase the time in a time stamp.
  1680. When nil, S-up will increase."
  1681. :group 'org-time
  1682. :type 'boolean)
  1683. (defcustom org-calendar-follow-timestamp-change t
  1684. "Non-nil means, make the calendar window follow timestamp changes.
  1685. When a timestamp is modified and the calendar window is visible, it will be
  1686. moved to the new date."
  1687. :group 'org-time
  1688. :type 'boolean)
  1689. (defcustom org-clock-heading-function nil
  1690. "When non-nil, should be a function to create `org-clock-heading'.
  1691. This is the string shown in the mode line when a clock is running.
  1692. The function is called with point at the beginning of the headline."
  1693. :group 'org-time ; FIXME: Should we have a separate group????
  1694. :type 'function)
  1695. (defgroup org-tags nil
  1696. "Options concerning tags in Org-mode."
  1697. :tag "Org Tags"
  1698. :group 'org)
  1699. (defcustom org-tag-alist nil
  1700. "List of tags allowed in Org-mode files.
  1701. When this list is nil, Org-mode will base TAG input on what is already in the
  1702. buffer.
  1703. The value of this variable is an alist, the car of each entry must be a
  1704. keyword as a string, the cdr may be a character that is used to select
  1705. that tag through the fast-tag-selection interface.
  1706. See the manual for details."
  1707. :group 'org-tags
  1708. :type '(repeat
  1709. (choice
  1710. (cons (string :tag "Tag name")
  1711. (character :tag "Access char"))
  1712. (const :tag "Start radio group" (:startgroup))
  1713. (const :tag "End radio group" (:endgroup)))))
  1714. (defcustom org-use-fast-tag-selection 'auto
  1715. "Non-nil means, use fast tag selection scheme.
  1716. This is a special interface to select and deselect tags with single keys.
  1717. When nil, fast selection is never used.
  1718. When the symbol `auto', fast selection is used if and only if selection
  1719. characters for tags have been configured, either through the variable
  1720. `org-tag-alist' or through a #+TAGS line in the buffer.
  1721. When t, fast selection is always used and selection keys are assigned
  1722. automatically if necessary."
  1723. :group 'org-tags
  1724. :type '(choice
  1725. (const :tag "Always" t)
  1726. (const :tag "Never" nil)
  1727. (const :tag "When selection characters are configured" 'auto)))
  1728. (defcustom org-fast-tag-selection-single-key nil
  1729. "Non-nil means, fast tag selection exits after first change.
  1730. When nil, you have to press RET to exit it.
  1731. During fast tag selection, you can toggle this flag with `C-c'.
  1732. This variable can also have the value `expert'. In this case, the window
  1733. displaying the tags menu is not even shown, until you press C-c again."
  1734. :group 'org-tags
  1735. :type '(choice
  1736. (const :tag "No" nil)
  1737. (const :tag "Yes" t)
  1738. (const :tag "Expert" expert)))
  1739. (defvar org-fast-tag-selection-include-todo nil
  1740. "Non-nil means, fast tags selection interface will also offer TODO states.
  1741. This is an undocumented feature, you should not rely on it.")
  1742. (defcustom org-tags-column -80
  1743. "The column to which tags should be indented in a headline.
  1744. If this number is positive, it specifies the column. If it is negative,
  1745. it means that the tags should be flushright to that column. For example,
  1746. -80 works well for a normal 80 character screen."
  1747. :group 'org-tags
  1748. :type 'integer)
  1749. (defcustom org-auto-align-tags t
  1750. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1751. These operations change the length of a headline and therefore shift
  1752. the tags around. With this options turned on, after each such operation
  1753. the tags are again aligned to `org-tags-column'."
  1754. :group 'org-tags
  1755. :type 'boolean)
  1756. (defcustom org-use-tag-inheritance t
  1757. "Non-nil means, tags in levels apply also for sublevels.
  1758. When nil, only the tags directly given in a specific line apply there.
  1759. If you turn off this option, you very likely want to turn on the
  1760. companion option `org-tags-match-list-sublevels'."
  1761. :group 'org-tags
  1762. :type 'boolean)
  1763. (defcustom org-tags-match-list-sublevels nil
  1764. "Non-nil means list also sublevels of headlines matching tag search.
  1765. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1766. the sublevels of a headline matching a tag search often also match
  1767. the same search. Listing all of them can create very long lists.
  1768. Setting this variable to nil causes subtrees of a match to be skipped.
  1769. This option is off by default, because inheritance in on. If you turn
  1770. inheritance off, you very likely want to turn this option on.
  1771. As a special case, if the tag search is restricted to TODO items, the
  1772. value of this variable is ignored and sublevels are always checked, to
  1773. make sure all corresponding TODO items find their way into the list."
  1774. :group 'org-tags
  1775. :type 'boolean)
  1776. (defvar org-tags-history nil
  1777. "History of minibuffer reads for tags.")
  1778. (defvar org-last-tags-completion-table nil
  1779. "The last used completion table for tags.")
  1780. (defvar org-after-tags-change-hook nil
  1781. "Hook that is run after the tags in a line have changed.")
  1782. (defgroup org-properties nil
  1783. "Options concerning properties in Org-mode."
  1784. :tag "Org Properties"
  1785. :group 'org)
  1786. (defcustom org-property-format "%-10s %s"
  1787. "How property key/value pairs should be formatted by `indent-line'.
  1788. When `indent-line' hits a property definition, it will format the line
  1789. according to this format, mainly to make sure that the values are
  1790. lined-up with respect to each other."
  1791. :group 'org-properties
  1792. :type 'string)
  1793. (defcustom org-use-property-inheritance nil
  1794. "Non-nil means, properties apply also for sublevels.
  1795. This setting is only relevant during property searches, not when querying
  1796. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1797. you need to call `org-entry-get' with the inheritance flag.
  1798. Turning this on can cause significant overhead when doing a search, so
  1799. this is turned off by default.
  1800. When nil, only the properties directly given in the current entry count.
  1801. The value may also be a list of properties that shouldhave inheritance.
  1802. However, note that some special properties use inheritance under special
  1803. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1804. and the properties ending in \"_ALL\" when they are used as descriptor
  1805. for valid values of a property."
  1806. :group 'org-properties
  1807. :type '(choice
  1808. (const :tag "Not" nil)
  1809. (const :tag "Always" nil)
  1810. (repeat :tag "Specific properties" (string :tag "Property"))))
  1811. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1812. "The default column format, if no other format has been defined.
  1813. This variable can be set on the per-file basis by inserting a line
  1814. #+COLUMNS: %25ITEM ....."
  1815. :group 'org-properties
  1816. :type 'string)
  1817. (defcustom org-global-properties nil
  1818. "List of property/value pairs that can be inherited by any entry.
  1819. You can set buffer-local values for this by adding lines like
  1820. #+PROPERTY: NAME VALUE"
  1821. :group 'org-properties
  1822. :type '(repeat
  1823. (cons (string :tag "Property")
  1824. (string :tag "Value"))))
  1825. (defvar org-local-properties nil
  1826. "List of property/value pairs that can be inherited by any entry.
  1827. Valid for the current buffer.
  1828. This variable is populated from #+PROPERTY lines.")
  1829. (defgroup org-agenda nil
  1830. "Options concerning agenda views in Org-mode."
  1831. :tag "Org Agenda"
  1832. :group 'org)
  1833. (defvar org-category nil
  1834. "Variable used by org files to set a category for agenda display.
  1835. Such files should use a file variable to set it, for example
  1836. # -*- mode: org; org-category: \"ELisp\"
  1837. or contain a special line
  1838. #+CATEGORY: ELisp
  1839. If the file does not specify a category, then file's base name
  1840. is used instead.")
  1841. (make-variable-buffer-local 'org-category)
  1842. (defcustom org-agenda-files nil
  1843. "The files to be used for agenda display.
  1844. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1845. \\[org-remove-file]. You can also use customize to edit the list.
  1846. If an entry is a directory, all files in that directory that are matched by
  1847. `org-agenda-file-regexp' will be part of the file list.
  1848. If the value of the variable is not a list but a single file name, then
  1849. the list of agenda files is actually stored and maintained in that file, one
  1850. agenda file per line."
  1851. :group 'org-agenda
  1852. :type '(choice
  1853. (repeat :tag "List of files and directories" file)
  1854. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1855. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1856. "Regular expression to match files for `org-agenda-files'.
  1857. If any element in the list in that variable contains a directory instead
  1858. of a normal file, all files in that directory that are matched by this
  1859. regular expression will be included."
  1860. :group 'org-agenda
  1861. :type 'regexp)
  1862. (defcustom org-agenda-skip-unavailable-files nil
  1863. "t means to just skip non-reachable files in `org-agenda-files'.
  1864. Nil means to remove them, after a query, from the list."
  1865. :group 'org-agenda
  1866. :type 'boolean)
  1867. (defcustom org-agenda-multi-occur-extra-files nil
  1868. "List of extra files to be searched by `org-occur-in-agenda-files'.
  1869. The files in `org-agenda-files' are always searched."
  1870. :group 'org-agenda
  1871. :type '(repeat file))
  1872. (defcustom org-agenda-confirm-kill 1
  1873. "When set, remote killing from the agenda buffer needs confirmation.
  1874. When t, a confirmation is always needed. When a number N, confirmation is
  1875. only needed when the text to be killed contains more than N non-white lines."
  1876. :group 'org-agenda
  1877. :type '(choice
  1878. (const :tag "Never" nil)
  1879. (const :tag "Always" t)
  1880. (number :tag "When more than N lines")))
  1881. (defcustom org-calendar-to-agenda-key [?c]
  1882. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1883. The command `org-calendar-goto-agenda' will be bound to this key. The
  1884. default is the character `c' because then `c' can be used to switch back and
  1885. forth between agenda and calendar."
  1886. :group 'org-agenda
  1887. :type 'sexp)
  1888. (defcustom org-agenda-compact-blocks nil
  1889. "Non-nil means, make the block agenda more compact.
  1890. This is done by leaving out unnecessary lines."
  1891. :group 'org-agenda
  1892. :type nil)
  1893. (defgroup org-agenda-export nil
  1894. "Options concerning exporting agenda views in Org-mode."
  1895. :tag "Org Agenda Export"
  1896. :group 'org-agenda)
  1897. (defcustom org-agenda-with-colors t
  1898. "Non-nil means, use colors in agenda views."
  1899. :group 'org-agenda-export
  1900. :type 'boolean)
  1901. (defcustom org-agenda-exporter-settings nil
  1902. "Alist of variable/value pairs that should be active during agenda export.
  1903. This is a good place to set uptions for ps-print and for htmlize."
  1904. :group 'org-agenda-export
  1905. :type '(repeat
  1906. (list
  1907. (variable)
  1908. (sexp :tag "Value"))))
  1909. (defcustom org-agenda-export-html-style ""
  1910. "The style specification for exported HTML Agenda files.
  1911. If this variable contains a string, it will replace the default <style>
  1912. section as produced by `htmlize'.
  1913. Since there are different ways of setting style information, this variable
  1914. needs to contain the full HTML structure to provide a style, including the
  1915. surrounding HTML tags. The style specifications should include definitions
  1916. the fonts used by the agenda, here is an example:
  1917. <style type=\"text/css\">
  1918. p { font-weight: normal; color: gray; }
  1919. .org-agenda-structure {
  1920. font-size: 110%;
  1921. color: #003399;
  1922. font-weight: 600;
  1923. }
  1924. .org-todo {
  1925. color: #cc6666;Week-agenda:
  1926. font-weight: bold;
  1927. }
  1928. .org-done {
  1929. color: #339933;
  1930. }
  1931. .title { text-align: center; }
  1932. .todo, .deadline { color: red; }
  1933. .done { color: green; }
  1934. </style>
  1935. or, if you want to keep the style in a file,
  1936. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1937. As the value of this option simply gets inserted into the HTML <head> header,
  1938. you can \"misuse\" it to also add other text to the header. However,
  1939. <style>...</style> is required, if not present the variable will be ignored."
  1940. :group 'org-agenda-export
  1941. :group 'org-export-html
  1942. :type 'string)
  1943. (defgroup org-agenda-custom-commands nil
  1944. "Options concerning agenda views in Org-mode."
  1945. :tag "Org Agenda Custom Commands"
  1946. :group 'org-agenda)
  1947. (defcustom org-agenda-custom-commands nil
  1948. "Custom commands for the agenda.
  1949. These commands will be offered on the splash screen displayed by the
  1950. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1951. (key desc type match options files)
  1952. key The key (one or more characters as a string) to be associated
  1953. with the command.
  1954. desc A description of the commend, when omitted or nil, a default
  1955. description is built using MATCH.
  1956. type The command type, any of the following symbols:
  1957. todo Entries with a specific TODO keyword, in all agenda files.
  1958. tags Tags match in all agenda files.
  1959. tags-todo Tags match in all agenda files, TODO entries only.
  1960. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1961. tags-tree Sparse tree with all tags matches in *current* file.
  1962. occur-tree Occur sparse tree for *current* file.
  1963. ... A user-defined function.
  1964. match What to search for:
  1965. - a single keyword for TODO keyword searches
  1966. - a tags match expression for tags searches
  1967. - a regular expression for occur searches
  1968. options A list of option settings, similar to that in a let form, so like
  1969. this: ((opt1 val1) (opt2 val2) ...)
  1970. files A list of files file to write the produced agenda buffer to
  1971. with the command `org-store-agenda-views'.
  1972. If a file name ends in \".html\", an HTML version of the buffer
  1973. is written out. If it ends in \".ps\", a postscript version is
  1974. produced. Otherwide, only the plain text is written to the file.
  1975. You can also define a set of commands, to create a composite agenda buffer.
  1976. In this case, an entry looks like this:
  1977. (key desc (cmd1 cmd2 ...) general-options file)
  1978. where
  1979. desc A description string to be displayed in the dispatcher menu.
  1980. cmd An agenda command, similar to the above. However, tree commands
  1981. are no allowed, but instead you can get agenda and global todo list.
  1982. So valid commands for a set are:
  1983. (agenda)
  1984. (alltodo)
  1985. (stuck)
  1986. (todo \"match\" options files)
  1987. (tags \"match\" options files)
  1988. (tags-todo \"match\" options files)
  1989. Each command can carry a list of options, and another set of options can be
  1990. given for the whole set of commands. Individual command options take
  1991. precedence over the general options.
  1992. When using several characters as key to a command, the first characters
  1993. are prefix commands. For the dispatcher to display useful information, you
  1994. should provide a description for the prefix, like
  1995. (setq org-agenda-custom-commands
  1996. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  1997. (\"hl\" tags \"+HOME+Lisa\")
  1998. (\"hp\" tags \"+HOME+Peter\")
  1999. (\"hk\" tags \"+HOME+Kim\")))"
  2000. :group 'org-agenda-custom-commands
  2001. :type '(repeat
  2002. (choice :value ("a" "" tags "" nil)
  2003. (list :tag "Single command"
  2004. (string :tag "Access Key(s) ")
  2005. (option (string :tag "Description"))
  2006. (choice
  2007. (const :tag "Agenda" agenda)
  2008. (const :tag "TODO list" alltodo)
  2009. (const :tag "Stuck projects" stuck)
  2010. (const :tag "Tags search (all agenda files)" tags)
  2011. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2012. (const :tag "TODO keyword search (all agenda files)" todo)
  2013. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2014. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2015. (const :tag "Occur tree (current buffer)" occur-tree)
  2016. (sexp :tag "Other, user-defined function"))
  2017. (string :tag "Match")
  2018. (repeat :tag "Local options"
  2019. (list (variable :tag "Option") (sexp :tag "Value")))
  2020. (option (repeat :tag "Export" (file :tag "Export to"))))
  2021. (list :tag "Command series, all agenda files"
  2022. (string :tag "Access Key(s)")
  2023. (string :tag "Description ")
  2024. (repeat
  2025. (choice
  2026. (const :tag "Agenda" (agenda))
  2027. (const :tag "TODO list" (alltodo))
  2028. (const :tag "Stuck projects" (stuck))
  2029. (list :tag "Tags search"
  2030. (const :format "" tags)
  2031. (string :tag "Match")
  2032. (repeat :tag "Local options"
  2033. (list (variable :tag "Option")
  2034. (sexp :tag "Value"))))
  2035. (list :tag "Tags search, TODO entries only"
  2036. (const :format "" tags-todo)
  2037. (string :tag "Match")
  2038. (repeat :tag "Local options"
  2039. (list (variable :tag "Option")
  2040. (sexp :tag "Value"))))
  2041. (list :tag "TODO keyword search"
  2042. (const :format "" todo)
  2043. (string :tag "Match")
  2044. (repeat :tag "Local options"
  2045. (list (variable :tag "Option")
  2046. (sexp :tag "Value"))))
  2047. (list :tag "Other, user-defined function"
  2048. (symbol :tag "function")
  2049. (string :tag "Match")
  2050. (repeat :tag "Local options"
  2051. (list (variable :tag "Option")
  2052. (sexp :tag "Value"))))))
  2053. (repeat :tag "General options"
  2054. (list (variable :tag "Option")
  2055. (sexp :tag "Value")))
  2056. (option (repeat :tag "Export" (file :tag "Export to"))))
  2057. (cons :tag "Prefix key documentation"
  2058. (string :tag "Access Key(s)")
  2059. (string :tag "Description ")))))
  2060. (defcustom org-stuck-projects
  2061. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2062. "How to identify stuck projects.
  2063. This is a list of four items:
  2064. 1. A tags/todo matcher string that is used to identify a project.
  2065. The entire tree below a headline matched by this is considered one project.
  2066. 2. A list of TODO keywords identifying non-stuck projects.
  2067. If the project subtree contains any headline with one of these todo
  2068. keywords, the project is considered to be not stuck. If you specify
  2069. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2070. 3. A list of tags identifying non-stuck projects.
  2071. If the project subtree contains any headline with one of these tags,
  2072. the project is considered to be not stuck. If you specify \"*\" as
  2073. a tag, any tag will mark the project unstuck.
  2074. 4. An arbitrary regular expression matching non-stuck projects.
  2075. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2076. or `C-c a #' to produce the list."
  2077. :group 'org-agenda-custom-commands
  2078. :type '(list
  2079. (string :tag "Tags/TODO match to identify a project")
  2080. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2081. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2082. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2083. (defgroup org-agenda-skip nil
  2084. "Options concerning skipping parts of agenda files."
  2085. :tag "Org Agenda Skip"
  2086. :group 'org-agenda)
  2087. (defcustom org-agenda-todo-list-sublevels t
  2088. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2089. When nil, the sublevels of a TODO entry are not checked, resulting in
  2090. potentially much shorter TODO lists."
  2091. :group 'org-agenda-skip
  2092. :group 'org-todo
  2093. :type 'boolean)
  2094. (defcustom org-agenda-todo-ignore-with-date nil
  2095. "Non-nil means, don't show entries with a date in the global todo list.
  2096. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2097. but don't want them to show up in the TODO list.
  2098. When this is set, it also covers deadlines and scheduled items, the settings
  2099. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2100. will be ignored."
  2101. :group 'org-agenda-skip
  2102. :group 'org-todo
  2103. :type 'boolean)
  2104. (defcustom org-agenda-todo-ignore-scheduled nil
  2105. "Non-nil means, don't show scheduled entries in the global todo list.
  2106. The idea behind this is that by scheduling it, you have already taken care
  2107. of this item.
  2108. See also `org-agenda-todo-ignore-with-date'."
  2109. :group 'org-agenda-skip
  2110. :group 'org-todo
  2111. :type 'boolean)
  2112. (defcustom org-agenda-todo-ignore-deadlines nil
  2113. "Non-nil means, don't show near deadline entries in the global todo list.
  2114. Near means closer than `org-deadline-warning-days' days.
  2115. The idea behind this is that such items will appear in the agenda anyway.
  2116. See also `org-agenda-todo-ignore-with-date'."
  2117. :group 'org-agenda-skip
  2118. :group 'org-todo
  2119. :type 'boolean)
  2120. (defcustom org-agenda-skip-scheduled-if-done nil
  2121. "Non-nil means don't show scheduled items in agenda when they are done.
  2122. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2123. it applies only to the actual date of the scheduling. Warnings about
  2124. an item with a past scheduling dates are always turned off when the item
  2125. is DONE."
  2126. :group 'org-agenda-skip
  2127. :type 'boolean)
  2128. (defcustom org-agenda-skip-deadline-if-done nil
  2129. "Non-nil means don't show deadines when the corresponding item is done.
  2130. When nil, the deadline is still shown and should give you a happy feeling.
  2131. This is relevant for the daily/weekly agenda. And it applied only to the
  2132. actualy date of the deadline. Warnings about approching and past-due
  2133. deadlines are always turned off when the item is DONE."
  2134. :group 'org-agenda-skip
  2135. :type 'boolean)
  2136. (defcustom org-agenda-skip-timestamp-if-done nil
  2137. "Non-nil means don't don't select item by timestamp or -range if it is DONE."
  2138. :group 'org-agenda-skip
  2139. :type 'boolean)
  2140. (defcustom org-timeline-show-empty-dates 3
  2141. "Non-nil means, `org-timeline' also shows dates without an entry.
  2142. When nil, only the days which actually have entries are shown.
  2143. When t, all days between the first and the last date are shown.
  2144. When an integer, show also empty dates, but if there is a gap of more than
  2145. N days, just insert a special line indicating the size of the gap."
  2146. :group 'org-agenda-skip
  2147. :type '(choice
  2148. (const :tag "None" nil)
  2149. (const :tag "All" t)
  2150. (number :tag "at most")))
  2151. (defgroup org-agenda-startup nil
  2152. "Options concerning initial settings in the Agenda in Org Mode."
  2153. :tag "Org Agenda Startup"
  2154. :group 'org-agenda)
  2155. (defcustom org-finalize-agenda-hook nil
  2156. "Hook run just before displaying an agenda buffer."
  2157. :group 'org-agenda-startup
  2158. :type 'hook)
  2159. (defcustom org-agenda-mouse-1-follows-link nil
  2160. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2161. A longer mouse click will still set point. Does not wortk on XEmacs.
  2162. Needs to be set before org.el is loaded."
  2163. :group 'org-agenda-startup
  2164. :type 'boolean)
  2165. (defcustom org-agenda-start-with-follow-mode nil
  2166. "The initial value of follow-mode in a newly created agenda window."
  2167. :group 'org-agenda-startup
  2168. :type 'boolean)
  2169. (defgroup org-agenda-windows nil
  2170. "Options concerning the windows used by the Agenda in Org Mode."
  2171. :tag "Org Agenda Windows"
  2172. :group 'org-agenda)
  2173. (defcustom org-agenda-window-setup 'reorganize-frame
  2174. "How the agenda buffer should be displayed.
  2175. Possible values for this option are:
  2176. current-window Show agenda in the current window, keeping all other windows.
  2177. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2178. other-window Use `switch-to-buffer-other-window' to display agenda.
  2179. reorganize-frame Show only two windows on the current frame, the current
  2180. window and the agenda.
  2181. See also the variable `org-agenda-restore-windows-after-quit'."
  2182. :group 'org-agenda-windows
  2183. :type '(choice
  2184. (const current-window)
  2185. (const other-frame)
  2186. (const other-window)
  2187. (const reorganize-frame)))
  2188. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2189. "The min and max height of the agenda window as a fraction of frame height.
  2190. The value of the variable is a cons cell with two numbers between 0 and 1.
  2191. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2192. :group 'org-agenda-windows
  2193. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2194. (defcustom org-agenda-restore-windows-after-quit nil
  2195. "Non-nil means, restore window configuration open exiting agenda.
  2196. Before the window configuration is changed for displaying the agenda,
  2197. the current status is recorded. When the agenda is exited with
  2198. `q' or `x' and this option is set, the old state is restored. If
  2199. `org-agenda-window-setup' is `other-frame', the value of this
  2200. option will be ignored.."
  2201. :group 'org-agenda-windows
  2202. :type 'boolean)
  2203. (defcustom org-indirect-buffer-display 'other-window
  2204. "How should indirect tree buffers be displayed?
  2205. This applies to indirect buffers created with the commands
  2206. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2207. Valid values are:
  2208. current-window Display in the current window
  2209. other-window Just display in another window.
  2210. dedicated-frame Create one new frame, and re-use it each time.
  2211. new-frame Make a new frame each time. Note that in this case
  2212. previously-made indirect buffers are kept, and you need to
  2213. kill these buffers yourself."
  2214. :group 'org-structure
  2215. :group 'org-agenda-windows
  2216. :type '(choice
  2217. (const :tag "In current window" current-window)
  2218. (const :tag "In current frame, other window" other-window)
  2219. (const :tag "Each time a new frame" new-frame)
  2220. (const :tag "One dedicated frame" dedicated-frame)))
  2221. (defgroup org-agenda-daily/weekly nil
  2222. "Options concerning the daily/weekly agenda."
  2223. :tag "Org Agenda Daily/Weekly"
  2224. :group 'org-agenda)
  2225. (defcustom org-agenda-ndays 7
  2226. "Number of days to include in overview display.
  2227. Should be 1 or 7."
  2228. :group 'org-agenda-daily/weekly
  2229. :type 'number)
  2230. (defcustom org-agenda-start-on-weekday 1
  2231. "Non-nil means, start the overview always on the specified weekday.
  2232. 0 denotes Sunday, 1 denotes Monday etc.
  2233. When nil, always start on the current day."
  2234. :group 'org-agenda-daily/weekly
  2235. :type '(choice (const :tag "Today" nil)
  2236. (number :tag "Weekday No.")))
  2237. (defcustom org-agenda-show-all-dates t
  2238. "Non-nil means, `org-agenda' shows every day in the selected range.
  2239. When nil, only the days which actually have entries are shown."
  2240. :group 'org-agenda-daily/weekly
  2241. :type 'boolean)
  2242. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2243. "Format string for displaying dates in the agenda.
  2244. Used by the daily/weekly agenda and by the timeline. This should be
  2245. a format string understood by `format-time-string', or a function returning
  2246. the formatted date as a string. The function must take a single argument,
  2247. a calendar-style date list like (month day year)."
  2248. :group 'org-agenda-daily/weekly
  2249. :type '(choice
  2250. (string :tag "Format string")
  2251. (function :tag "Function")))
  2252. (defun org-agenda-format-date-aligned (date)
  2253. "Format a date string for display in the daily/weekly agenda, or timeline.
  2254. This function makes sure that dates are aligned for easy reading."
  2255. (format "%-9s %2d %s %4d"
  2256. (calendar-day-name date)
  2257. (extract-calendar-day date)
  2258. (calendar-month-name (extract-calendar-month date))
  2259. (extract-calendar-year date)))
  2260. (defcustom org-agenda-include-diary nil
  2261. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2262. :group 'org-agenda-daily/weekly
  2263. :type 'boolean)
  2264. (defcustom org-agenda-include-all-todo nil
  2265. "Set means weekly/daily agenda will always contain all TODO entries.
  2266. The TODO entries will be listed at the top of the agenda, before
  2267. the entries for specific days."
  2268. :group 'org-agenda-daily/weekly
  2269. :type 'boolean)
  2270. (defcustom org-agenda-repeating-timestamp-show-all t
  2271. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2272. When nil, only one occurence is shown, either today or the
  2273. nearest into the future."
  2274. :group 'org-agenda-daily/weekly
  2275. :type 'boolean)
  2276. (defcustom org-deadline-warning-days 14
  2277. "No. of days before expiration during which a deadline becomes active.
  2278. This variable governs the display in sparse trees and in the agenda.
  2279. When negative, it means use this number (the absolute value of it)
  2280. even if a deadline has a different individual lead time specified."
  2281. :group 'org-time
  2282. :group 'org-agenda-daily/weekly
  2283. :type 'number)
  2284. (defcustom org-scheduled-past-days 10000
  2285. "No. of days to continue listing scheduled items that are not marked DONE.
  2286. When an item is scheduled on a date, it shows up in the agenda on this
  2287. day and will be listed until it is marked done for the number of days
  2288. given here."
  2289. :group 'org-agenda-daily/weekly
  2290. :type 'number)
  2291. (defgroup org-agenda-time-grid nil
  2292. "Options concerning the time grid in the Org-mode Agenda."
  2293. :tag "Org Agenda Time Grid"
  2294. :group 'org-agenda)
  2295. (defcustom org-agenda-use-time-grid t
  2296. "Non-nil means, show a time grid in the agenda schedule.
  2297. A time grid is a set of lines for specific times (like every two hours between
  2298. 8:00 and 20:00). The items scheduled for a day at specific times are
  2299. sorted in between these lines.
  2300. For details about when the grid will be shown, and what it will look like, see
  2301. the variable `org-agenda-time-grid'."
  2302. :group 'org-agenda-time-grid
  2303. :type 'boolean)
  2304. (defcustom org-agenda-time-grid
  2305. '((daily today require-timed)
  2306. "----------------"
  2307. (800 1000 1200 1400 1600 1800 2000))
  2308. "The settings for time grid for agenda display.
  2309. This is a list of three items. The first item is again a list. It contains
  2310. symbols specifying conditions when the grid should be displayed:
  2311. daily if the agenda shows a single day
  2312. weekly if the agenda shows an entire week
  2313. today show grid on current date, independent of daily/weekly display
  2314. require-timed show grid only if at least one item has a time specification
  2315. The second item is a string which will be places behing the grid time.
  2316. The third item is a list of integers, indicating the times that should have
  2317. a grid line."
  2318. :group 'org-agenda-time-grid
  2319. :type
  2320. '(list
  2321. (set :greedy t :tag "Grid Display Options"
  2322. (const :tag "Show grid in single day agenda display" daily)
  2323. (const :tag "Show grid in weekly agenda display" weekly)
  2324. (const :tag "Always show grid for today" today)
  2325. (const :tag "Show grid only if any timed entries are present"
  2326. require-timed)
  2327. (const :tag "Skip grid times already present in an entry"
  2328. remove-match))
  2329. (string :tag "Grid String")
  2330. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2331. (defgroup org-agenda-sorting nil
  2332. "Options concerning sorting in the Org-mode Agenda."
  2333. :tag "Org Agenda Sorting"
  2334. :group 'org-agenda)
  2335. (defconst org-sorting-choice
  2336. '(choice
  2337. (const time-up) (const time-down)
  2338. (const category-keep) (const category-up) (const category-down)
  2339. (const tag-down) (const tag-up)
  2340. (const priority-up) (const priority-down))
  2341. "Sorting choices.")
  2342. (defcustom org-agenda-sorting-strategy
  2343. '((agenda time-up category-keep priority-down)
  2344. (todo category-keep priority-down)
  2345. (tags category-keep priority-down))
  2346. "Sorting structure for the agenda items of a single day.
  2347. This is a list of symbols which will be used in sequence to determine
  2348. if an entry should be listed before another entry. The following
  2349. symbols are recognized:
  2350. time-up Put entries with time-of-day indications first, early first
  2351. time-down Put entries with time-of-day indications first, late first
  2352. category-keep Keep the default order of categories, corresponding to the
  2353. sequence in `org-agenda-files'.
  2354. category-up Sort alphabetically by category, A-Z.
  2355. category-down Sort alphabetically by category, Z-A.
  2356. tag-up Sort alphabetically by last tag, A-Z.
  2357. tag-down Sort alphabetically by last tag, Z-A.
  2358. priority-up Sort numerically by priority, high priority last.
  2359. priority-down Sort numerically by priority, high priority first.
  2360. The different possibilities will be tried in sequence, and testing stops
  2361. if one comparison returns a \"not-equal\". For example, the default
  2362. '(time-up category-keep priority-down)
  2363. means: Pull out all entries having a specified time of day and sort them,
  2364. in order to make a time schedule for the current day the first thing in the
  2365. agenda listing for the day. Of the entries without a time indication, keep
  2366. the grouped in categories, don't sort the categories, but keep them in
  2367. the sequence given in `org-agenda-files'. Within each category sort by
  2368. priority.
  2369. Leaving out `category-keep' would mean that items will be sorted across
  2370. categories by priority.
  2371. Instead of a single list, this can also be a set of list for specific
  2372. contents, with a context symbol in the car of the list, any of
  2373. `agenda', `todo', `tags' for the corresponding agenda views."
  2374. :group 'org-agenda-sorting
  2375. :type `(choice
  2376. (repeat :tag "General" ,org-sorting-choice)
  2377. (list :tag "Individually"
  2378. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2379. (repeat ,org-sorting-choice))
  2380. (cons (const :tag "Strategy for TODO lists" todo)
  2381. (repeat ,org-sorting-choice))
  2382. (cons (const :tag "Strategy for Tags matches" tags)
  2383. (repeat ,org-sorting-choice)))))
  2384. (defcustom org-sort-agenda-notime-is-late t
  2385. "Non-nil means, items without time are considered late.
  2386. This is only relevant for sorting. When t, items which have no explicit
  2387. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2388. do have a time. When nil, the default time is before 0:00. You can use this
  2389. option to decide if the schedule for today should come before or after timeless
  2390. agenda entries."
  2391. :group 'org-agenda-sorting
  2392. :type 'boolean)
  2393. (defgroup org-agenda-line-format nil
  2394. "Options concerning the entry prefix in the Org-mode agenda display."
  2395. :tag "Org Agenda Line Format"
  2396. :group 'org-agenda)
  2397. (defcustom org-agenda-prefix-format
  2398. '((agenda . " %-12:c%?-12t% s")
  2399. (timeline . " % s")
  2400. (todo . " %-12:c")
  2401. (tags . " %-12:c"))
  2402. "Format specifications for the prefix of items in the agenda views.
  2403. An alist with four entries, for the different agenda types. The keys to the
  2404. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2405. are format strings.
  2406. This format works similar to a printf format, with the following meaning:
  2407. %c the category of the item, \"Diary\" for entries from the diary, or
  2408. as given by the CATEGORY keyword or derived from the file name.
  2409. %T the *last* tag of the item. Last because inherited tags come
  2410. first in the list.
  2411. %t the time-of-day specification if one applies to the entry, in the
  2412. format HH:MM
  2413. %s Scheduling/Deadline information, a short string
  2414. All specifiers work basically like the standard `%s' of printf, but may
  2415. contain two additional characters: A question mark just after the `%' and
  2416. a whitespace/punctuation character just before the final letter.
  2417. If the first character after `%' is a question mark, the entire field
  2418. will only be included if the corresponding value applies to the
  2419. current entry. This is useful for fields which should have fixed
  2420. width when present, but zero width when absent. For example,
  2421. \"%?-12t\" will result in a 12 character time field if a time of the
  2422. day is specified, but will completely disappear in entries which do
  2423. not contain a time.
  2424. If there is punctuation or whitespace character just before the final
  2425. format letter, this character will be appended to the field value if
  2426. the value is not empty. For example, the format \"%-12:c\" leads to
  2427. \"Diary: \" if the category is \"Diary\". If the category were be
  2428. empty, no additional colon would be interted.
  2429. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2430. - Indent the line with two space characters
  2431. - Give the category in a 12 chars wide field, padded with whitespace on
  2432. the right (because of `-'). Append a colon if there is a category
  2433. (because of `:').
  2434. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2435. time, don't put in an empty field, just skip it (because of '?').
  2436. - Finally, put the scheduling information and append a whitespace.
  2437. As another example, if you don't want the time-of-day of entries in
  2438. the prefix, you could use:
  2439. (setq org-agenda-prefix-format \" %-11:c% s\")
  2440. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2441. `org-agenda-remove-tags'."
  2442. :type '(choice
  2443. (string :tag "General format")
  2444. (list :greedy t :tag "View dependent"
  2445. (cons (const agenda) (string :tag "Format"))
  2446. (cons (const timeline) (string :tag "Format"))
  2447. (cons (const todo) (string :tag "Format"))
  2448. (cons (const tags) (string :tag "Format"))))
  2449. :group 'org-agenda-line-format)
  2450. (defvar org-prefix-format-compiled nil
  2451. "The compiled version of the most recently used prefix format.
  2452. See the variable `org-agenda-prefix-format'.")
  2453. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2454. "Text preceeding scheduled items in the agenda view.
  2455. THis is a list with two strings. The first applies when the item is
  2456. scheduled on the current day. The second applies when it has been scheduled
  2457. previously, it may contain a %d to capture how many days ago the item was
  2458. scheduled."
  2459. :group 'org-agenda-line-format
  2460. :type '(list
  2461. (string :tag "Scheduled today ")
  2462. (string :tag "Scheduled previously")))
  2463. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2464. "Text preceeding deadline items in the agenda view.
  2465. This is a list with two strings. The first applies when the item has its
  2466. deadline on the current day. The second applies when it is in the past or
  2467. in the future, it may contain %d to capture how many days away the deadline
  2468. is (was)."
  2469. :group 'org-agenda-line-format
  2470. :type '(list
  2471. (string :tag "Deadline today ")
  2472. (string :tag "Deadline relative")))
  2473. (defcustom org-agenda-remove-times-when-in-prefix t
  2474. "Non-nil means, remove duplicate time specifications in agenda items.
  2475. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2476. time-of-day specification in a headline or diary entry is extracted and
  2477. placed into the prefix. If this option is non-nil, the original specification
  2478. \(a timestamp or -range, or just a plain time(range) specification like
  2479. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2480. cluttered.
  2481. The option can be t or nil. It may also be the symbol `beg', indicating
  2482. that the time should only be removed what it is located at the beginning of
  2483. the headline/diary entry."
  2484. :group 'org-agenda-line-format
  2485. :type '(choice
  2486. (const :tag "Always" t)
  2487. (const :tag "Never" nil)
  2488. (const :tag "When at beginning of entry" beg)))
  2489. (defcustom org-agenda-default-appointment-duration nil
  2490. "Default duration for appointments that only have a starting time.
  2491. When nil, no duration is specified in such cases.
  2492. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2493. :group 'org-agenda-line-format
  2494. :type '(choice
  2495. (integer :tag "Minutes")
  2496. (const :tag "No default duration")))
  2497. (defcustom org-agenda-remove-tags nil
  2498. "Non-nil means, remove the tags from the headline copy in the agenda.
  2499. When this is the symbol `prefix', only remove tags when
  2500. `org-agenda-prefix-format' contains a `%T' specifier."
  2501. :group 'org-agenda-line-format
  2502. :type '(choice
  2503. (const :tag "Always" t)
  2504. (const :tag "Never" nil)
  2505. (const :tag "When prefix format contains %T" prefix)))
  2506. (if (fboundp 'defvaralias)
  2507. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2508. 'org-agenda-remove-tags))
  2509. (defcustom org-agenda-tags-column -80
  2510. "Shift tags in agenda items to this column.
  2511. If this number is positive, it specifies the column. If it is negative,
  2512. it means that the tags should be flushright to that column. For example,
  2513. -80 works well for a normal 80 character screen."
  2514. :group 'org-agenda-line-format
  2515. :type 'integer)
  2516. (if (fboundp 'defvaralias)
  2517. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2518. (defcustom org-agenda-fontify-priorities t
  2519. "Non-nil means, highlight low and high priorities in agenda.
  2520. When t, the highest priority entries are bold, lowest priority italic.
  2521. This may also be an association list of priority faces. The face may be
  2522. a names face, or a list like `(:background \"Red\")'."
  2523. :group 'org-agenda-line-format
  2524. :type '(choice
  2525. (const :tag "Never" nil)
  2526. (const :tag "Defaults" t)
  2527. (repeat :tag "Specify"
  2528. (list (character :tag "Priority" :value ?A)
  2529. (sexp :tag "face")))))
  2530. (defgroup org-latex nil
  2531. "Options for embedding LaTeX code into Org-mode"
  2532. :tag "Org LaTeX"
  2533. :group 'org)
  2534. (defcustom org-format-latex-options
  2535. '(:foreground default :background default :scale 1.0
  2536. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2537. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2538. "Options for creating images from LaTeX fragments.
  2539. This is a property list with the following properties:
  2540. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2541. `default' means use the forground of the default face.
  2542. :background the background color, or \"Transparent\".
  2543. `default' means use the background of the default face.
  2544. :scale a scaling factor for the size of the images
  2545. :html-foreground, :html-background, :html-scale
  2546. The same numbers for HTML export.
  2547. :matchers a list indicating which matchers should be used to
  2548. find LaTeX fragments. Valid members of this list are:
  2549. \"begin\" find environments
  2550. \"$\" find math expressions surrounded by $...$
  2551. \"$$\" find math expressions surrounded by $$....$$
  2552. \"\\(\" find math expressions surrounded by \\(...\\)
  2553. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2554. :group 'org-latex
  2555. :type 'plist)
  2556. (defcustom org-format-latex-header "\\documentclass{article}
  2557. \\usepackage{fullpage} % do not remove
  2558. \\usepackage{amssymb}
  2559. \\usepackage[usenames]{color}
  2560. \\usepackage{amsmath}
  2561. \\usepackage{latexsym}
  2562. \\usepackage[mathscr]{eucal}
  2563. \\pagestyle{empty} % do not remove"
  2564. "The document header used for processing LaTeX fragments."
  2565. :group 'org-latex
  2566. :type 'string)
  2567. (defgroup org-export nil
  2568. "Options for exporting org-listings."
  2569. :tag "Org Export"
  2570. :group 'org)
  2571. (defgroup org-export-general nil
  2572. "General options for exporting Org-mode files."
  2573. :tag "Org Export General"
  2574. :group 'org-export)
  2575. ;; FIXME
  2576. (defvar org-export-publishing-directory nil)
  2577. (defcustom org-export-with-special-strings t
  2578. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2579. When this option is turned on, these strings will be exported as:
  2580. Org HTML LaTeX
  2581. -----+----------+--------
  2582. \\- &shy; \\-
  2583. -- &ndash; --
  2584. --- &mdash; ---
  2585. ... &hellip; \ldots
  2586. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2587. :group 'org-export-translation
  2588. :type 'boolean)
  2589. (defcustom org-export-language-setup
  2590. '(("en" "Author" "Date" "Table of Contents")
  2591. ("cs" "Autor" "Datum" "Obsah")
  2592. ("da" "Ophavsmand" "Dato" "Indhold")
  2593. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2594. ("es" "Autor" "Fecha" "\xcdndice")
  2595. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2596. ("it" "Autore" "Data" "Indice")
  2597. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2598. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2599. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2600. "Terms used in export text, translated to different languages.
  2601. Use the variable `org-export-default-language' to set the language,
  2602. or use the +OPTION lines for a per-file setting."
  2603. :group 'org-export-general
  2604. :type '(repeat
  2605. (list
  2606. (string :tag "HTML language tag")
  2607. (string :tag "Author")
  2608. (string :tag "Date")
  2609. (string :tag "Table of Contents"))))
  2610. (defcustom org-export-default-language "en"
  2611. "The default language of HTML export, as a string.
  2612. This should have an association in `org-export-language-setup'."
  2613. :group 'org-export-general
  2614. :type 'string)
  2615. (defcustom org-export-skip-text-before-1st-heading t
  2616. "Non-nil means, skip all text before the first headline when exporting.
  2617. When nil, that text is exported as well."
  2618. :group 'org-export-general
  2619. :type 'boolean)
  2620. (defcustom org-export-headline-levels 3
  2621. "The last level which is still exported as a headline.
  2622. Inferior levels will produce itemize lists when exported.
  2623. Note that a numeric prefix argument to an exporter function overrides
  2624. this setting.
  2625. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2626. :group 'org-export-general
  2627. :type 'number)
  2628. (defcustom org-export-with-section-numbers t
  2629. "Non-nil means, add section numbers to headlines when exporting.
  2630. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2631. :group 'org-export-general
  2632. :type 'boolean)
  2633. (defcustom org-export-with-toc t
  2634. "Non-nil means, create a table of contents in exported files.
  2635. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2636. When an integer, include levels up to N in the toc, this may then be
  2637. different from `org-export-headline-levels', but it will not be allowed
  2638. to be larger than the number of headline levels.
  2639. When nil, no table of contents is made.
  2640. Headlines which contain any TODO items will be marked with \"(*)\" in
  2641. ASCII export, and with red color in HTML output, if the option
  2642. `org-export-mark-todo-in-toc' is set.
  2643. In HTML output, the TOC will be clickable.
  2644. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2645. or \"toc:3\"."
  2646. :group 'org-export-general
  2647. :type '(choice
  2648. (const :tag "No Table of Contents" nil)
  2649. (const :tag "Full Table of Contents" t)
  2650. (integer :tag "TOC to level")))
  2651. (defcustom org-export-mark-todo-in-toc nil
  2652. "Non-nil means, mark TOC lines that contain any open TODO items."
  2653. :group 'org-export-general
  2654. :type 'boolean)
  2655. (defcustom org-export-preserve-breaks nil
  2656. "Non-nil means, preserve all line breaks when exporting.
  2657. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2658. export, line breaks will always be preserved, regardless of this variable.
  2659. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2660. :group 'org-export-general
  2661. :type 'boolean)
  2662. (defcustom org-export-with-archived-trees 'headline
  2663. "Whether subtrees with the ARCHIVE tag should be exported.
  2664. This can have three different values
  2665. nil Do not export, pretend this tree is not present
  2666. t Do export the entire tree
  2667. headline Only export the headline, but skip the tree below it."
  2668. :group 'org-export-general
  2669. :group 'org-archive
  2670. :type '(choice
  2671. (const :tag "not at all" nil)
  2672. (const :tag "headline only" 'headline)
  2673. (const :tag "entirely" t)))
  2674. (defcustom org-export-author-info t
  2675. "Non-nil means, insert author name and email into the exported file.
  2676. This option can also be set with the +OPTIONS line,
  2677. e.g. \"author-info:nil\"."
  2678. :group 'org-export-general
  2679. :type 'boolean)
  2680. (defcustom org-export-time-stamp-file t
  2681. "Non-nil means, insert a time stamp into the exported file.
  2682. The time stamp shows when the file was created.
  2683. This option can also be set with the +OPTIONS line,
  2684. e.g. \"timestamp:nil\"."
  2685. :group 'org-export-general
  2686. :type 'boolean)
  2687. (defcustom org-export-with-timestamps t
  2688. "If nil, do not export time stamps and associated keywords."
  2689. :group 'org-export-general
  2690. :type 'boolean)
  2691. (defcustom org-export-remove-timestamps-from-toc t
  2692. "If nil, remove timestamps from the table of contents entries."
  2693. :group 'org-export-general
  2694. :type 'boolean)
  2695. (defcustom org-export-with-tags 'not-in-toc
  2696. "If nil, do not export tags, just remove them from headlines.
  2697. If this is the symbol `not-in-toc', tags will be removed from table of
  2698. contents entries, but still be shown in the headlines of the document.
  2699. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2700. :group 'org-export-general
  2701. :type '(choice
  2702. (const :tag "Off" nil)
  2703. (const :tag "Not in TOC" not-in-toc)
  2704. (const :tag "On" t)))
  2705. (defcustom org-export-with-drawers nil
  2706. "Non-nil means, export with drawers like the property drawer.
  2707. When t, all drawers are exported. This may also be a list of
  2708. drawer names to export."
  2709. :group 'org-export-general
  2710. :type '(choice
  2711. (const :tag "All drawers" t)
  2712. (const :tag "None" nil)
  2713. (repeat :tag "Selected drawers"
  2714. (string :tag "Drawer name"))))
  2715. (defgroup org-export-translation nil
  2716. "Options for translating special ascii sequences for the export backends."
  2717. :tag "Org Export Translation"
  2718. :group 'org-export)
  2719. (defcustom org-export-with-emphasize t
  2720. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2721. If the export target supports emphasizing text, the word will be
  2722. typeset in bold, italic, or underlined, respectively. Works only for
  2723. single words, but you can say: I *really* *mean* *this*.
  2724. Not all export backends support this.
  2725. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2726. :group 'org-export-translation
  2727. :type 'boolean)
  2728. (defcustom org-export-with-footnotes t
  2729. "If nil, export [1] as a footnote marker.
  2730. Lines starting with [1] will be formatted as footnotes.
  2731. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2732. :group 'org-export-translation
  2733. :type 'boolean)
  2734. (defcustom org-export-with-sub-superscripts t
  2735. "Non-nil means, interpret \"_\" and \"^\" for export.
  2736. When this option is turned on, you can use TeX-like syntax for sub- and
  2737. superscripts. Several characters after \"_\" or \"^\" will be
  2738. considered as a single item - so grouping with {} is normally not
  2739. needed. For example, the following things will be parsed as single
  2740. sub- or superscripts.
  2741. 10^24 or 10^tau several digits will be considered 1 item.
  2742. 10^-12 or 10^-tau a leading sign with digits or a word
  2743. x^2-y^3 will be read as x^2 - y^3, because items are
  2744. terminated by almost any nonword/nondigit char.
  2745. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2746. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2747. sub/superscript. If you set this variable to the symbol `{}',
  2748. the braces are *required* in order to trigger interpretations as
  2749. sub/superscript. This can be helpful in documents that need \"_\"
  2750. frequently in plain text.
  2751. Not all export backends support this, but HTML does.
  2752. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2753. :group 'org-export-translation
  2754. :type '(choice
  2755. (const :tag "Always interpret" t)
  2756. (const :tag "Only with braces" {})
  2757. (const :tag "Never interpret" nil)))
  2758. (defcustom org-export-with-special-strings t
  2759. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2760. When this option is turned on, these strings will be exported as:
  2761. \\- : &shy;
  2762. -- : &ndash;
  2763. --- : &mdash;
  2764. Not all export backends support this, but HTML does.
  2765. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2766. :group 'org-export-translation
  2767. :type 'boolean)
  2768. (defcustom org-export-with-TeX-macros t
  2769. "Non-nil means, interpret simple TeX-like macros when exporting.
  2770. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2771. No only real TeX macros will work here, but the standard HTML entities
  2772. for math can be used as macro names as well. For a list of supported
  2773. names in HTML export, see the constant `org-html-entities'.
  2774. Not all export backends support this.
  2775. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2776. :group 'org-export-translation
  2777. :group 'org-export-latex
  2778. :type 'boolean)
  2779. (defcustom org-export-with-LaTeX-fragments nil
  2780. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2781. When set, the exporter will find LaTeX environments if the \\begin line is
  2782. the first non-white thing on a line. It will also find the math delimiters
  2783. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2784. display math.
  2785. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2786. :group 'org-export-translation
  2787. :group 'org-export-latex
  2788. :type 'boolean)
  2789. (defcustom org-export-with-fixed-width t
  2790. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2791. This can be used to have pre-formatted text, fragments of code etc. For
  2792. example:
  2793. : ;; Some Lisp examples
  2794. : (while (defc cnt)
  2795. : (ding))
  2796. will be looking just like this in also HTML. See also the QUOTE keyword.
  2797. Not all export backends support this.
  2798. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2799. :group 'org-export-translation
  2800. :type 'boolean)
  2801. (defcustom org-match-sexp-depth 3
  2802. "Number of stacked braces for sub/superscript matching.
  2803. This has to be set before loading org.el to be effective."
  2804. :group 'org-export-translation
  2805. :type 'integer)
  2806. (defgroup org-export-tables nil
  2807. "Options for exporting tables in Org-mode."
  2808. :tag "Org Export Tables"
  2809. :group 'org-export)
  2810. (defcustom org-export-with-tables t
  2811. "If non-nil, lines starting with \"|\" define a table.
  2812. For example:
  2813. | Name | Address | Birthday |
  2814. |-------------+----------+-----------|
  2815. | Arthur Dent | England | 29.2.2100 |
  2816. Not all export backends support this.
  2817. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2818. :group 'org-export-tables
  2819. :type 'boolean)
  2820. (defcustom org-export-highlight-first-table-line t
  2821. "Non-nil means, highlight the first table line.
  2822. In HTML export, this means use <th> instead of <td>.
  2823. In tables created with table.el, this applies to the first table line.
  2824. In Org-mode tables, all lines before the first horizontal separator
  2825. line will be formatted with <th> tags."
  2826. :group 'org-export-tables
  2827. :type 'boolean)
  2828. (defcustom org-export-table-remove-special-lines t
  2829. "Remove special lines and marking characters in calculating tables.
  2830. This removes the special marking character column from tables that are set
  2831. up for spreadsheet calculations. It also removes the entire lines
  2832. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2833. the values of constants may be useful to have."
  2834. :group 'org-export-tables
  2835. :type 'boolean)
  2836. (defcustom org-export-prefer-native-exporter-for-tables nil
  2837. "Non-nil means, always export tables created with table.el natively.
  2838. Natively means, use the HTML code generator in table.el.
  2839. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2840. the table does not use row- or column-spanning). This has the
  2841. advantage, that the automatic HTML conversions for math symbols and
  2842. sub/superscripts can be applied. Org-mode's HTML generator is also
  2843. much faster."
  2844. :group 'org-export-tables
  2845. :type 'boolean)
  2846. (defgroup org-export-ascii nil
  2847. "Options specific for ASCII export of Org-mode files."
  2848. :tag "Org Export ASCII"
  2849. :group 'org-export)
  2850. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2851. "Characters for underlining headings in ASCII export.
  2852. In the given sequence, these characters will be used for level 1, 2, ..."
  2853. :group 'org-export-ascii
  2854. :type '(repeat character))
  2855. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2856. "Bullet characters for headlines converted to lists in ASCII export.
  2857. The first character is is used for the first lest level generated in this
  2858. way, and so on. If there are more levels than characters given here,
  2859. the list will be repeated.
  2860. Note that plain lists will keep the same bullets as the have in the
  2861. Org-mode file."
  2862. :group 'org-export-ascii
  2863. :type '(repeat character))
  2864. (defgroup org-export-xml nil
  2865. "Options specific for XML export of Org-mode files."
  2866. :tag "Org Export XML"
  2867. :group 'org-export)
  2868. (defgroup org-export-html nil
  2869. "Options specific for HTML export of Org-mode files."
  2870. :tag "Org Export HTML"
  2871. :group 'org-export)
  2872. (defcustom org-export-html-coding-system nil
  2873. ""
  2874. :group 'org-export-html
  2875. :type 'coding-system)
  2876. (defcustom org-export-html-extension "html"
  2877. "The extension for exported HTML files."
  2878. :group 'org-export-html
  2879. :type 'string)
  2880. (defcustom org-export-html-style
  2881. "<style type=\"text/css\">
  2882. html {
  2883. font-family: Times, serif;
  2884. font-size: 12pt;
  2885. }
  2886. .title { text-align: center; }
  2887. .todo { color: red; }
  2888. .done { color: green; }
  2889. .timestamp { color: grey }
  2890. .timestamp-kwd { color: CadetBlue }
  2891. .tag { background-color:lightblue; font-weight:normal }
  2892. .target { background-color: lavender; }
  2893. pre {
  2894. border: 1pt solid #AEBDCC;
  2895. background-color: #F3F5F7;
  2896. padding: 5pt;
  2897. font-family: courier, monospace;
  2898. }
  2899. table { border-collapse: collapse; }
  2900. td, th {
  2901. vertical-align: top;
  2902. <!--border: 1pt solid #ADB9CC;-->
  2903. }
  2904. </style>"
  2905. "The default style specification for exported HTML files.
  2906. Since there are different ways of setting style information, this variable
  2907. needs to contain the full HTML structure to provide a style, including the
  2908. surrounding HTML tags. The style specifications should include definitions
  2909. for new classes todo, done, title, and deadline. For example, legal values
  2910. would be:
  2911. <style type=\"text/css\">
  2912. p { font-weight: normal; color: gray; }
  2913. h1 { color: black; }
  2914. .title { text-align: center; }
  2915. .todo, .deadline { color: red; }
  2916. .done { color: green; }
  2917. </style>
  2918. or, if you want to keep the style in a file,
  2919. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2920. As the value of this option simply gets inserted into the HTML <head> header,
  2921. you can \"misuse\" it to add arbitrary text to the header."
  2922. :group 'org-export-html
  2923. :type 'string)
  2924. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2925. "Format for typesetting the document title in HTML export."
  2926. :group 'org-export-html
  2927. :type 'string)
  2928. (defcustom org-export-html-toplevel-hlevel 2
  2929. "The <H> level for level 1 headings in HTML export."
  2930. :group 'org-export-html
  2931. :type 'string)
  2932. (defcustom org-export-html-link-org-files-as-html t
  2933. "Non-nil means, make file links to `file.org' point to `file.html'.
  2934. When org-mode is exporting an org-mode file to HTML, links to
  2935. non-html files are directly put into a href tag in HTML.
  2936. However, links to other Org-mode files (recognized by the
  2937. extension `.org.) should become links to the corresponding html
  2938. file, assuming that the linked org-mode file will also be
  2939. converted to HTML.
  2940. When nil, the links still point to the plain `.org' file."
  2941. :group 'org-export-html
  2942. :type 'boolean)
  2943. (defcustom org-export-html-inline-images 'maybe
  2944. "Non-nil means, inline images into exported HTML pages.
  2945. This is done using an <img> tag. When nil, an anchor with href is used to
  2946. link to the image. If this option is `maybe', then images in links with
  2947. an empty description will be inlined, while images with a description will
  2948. be linked only."
  2949. :group 'org-export-html
  2950. :type '(choice (const :tag "Never" nil)
  2951. (const :tag "Always" t)
  2952. (const :tag "When there is no description" maybe)))
  2953. ;; FIXME: rename
  2954. (defcustom org-export-html-expand t
  2955. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  2956. When nil, these tags will be exported as plain text and therefore
  2957. not be interpreted by a browser.
  2958. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  2959. :group 'org-export-html
  2960. :type 'boolean)
  2961. (defcustom org-export-html-table-tag
  2962. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  2963. "The HTML tag that is used to start a table.
  2964. This must be a <table> tag, but you may change the options like
  2965. borders and spacing."
  2966. :group 'org-export-html
  2967. :type 'string)
  2968. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  2969. "The opening tag for table header fields.
  2970. This is customizable so that alignment options can be specified."
  2971. :group 'org-export-tables
  2972. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2973. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  2974. "The opening tag for table data fields.
  2975. This is customizable so that alignment options can be specified."
  2976. :group 'org-export-tables
  2977. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  2978. (defcustom org-export-html-with-timestamp nil
  2979. "If non-nil, write `org-export-html-html-helper-timestamp'
  2980. into the exported HTML text. Otherwise, the buffer will just be saved
  2981. to a file."
  2982. :group 'org-export-html
  2983. :type 'boolean)
  2984. (defcustom org-export-html-html-helper-timestamp
  2985. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  2986. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  2987. :group 'org-export-html
  2988. :type 'string)
  2989. (defgroup org-export-icalendar nil
  2990. "Options specific for iCalendar export of Org-mode files."
  2991. :tag "Org Export iCalendar"
  2992. :group 'org-export)
  2993. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  2994. "The file name for the iCalendar file covering all agenda files.
  2995. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  2996. The file name should be absolute, the file will be overwritten without warning."
  2997. :group 'org-export-icalendar
  2998. :type 'file)
  2999. (defcustom org-icalendar-include-todo nil
  3000. "Non-nil means, export to iCalendar files should also cover TODO items."
  3001. :group 'org-export-icalendar
  3002. :type '(choice
  3003. (const :tag "None" nil)
  3004. (const :tag "Unfinished" t)
  3005. (const :tag "All" all)))
  3006. (defcustom org-icalendar-include-sexps t
  3007. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3008. These are entries like in the diary, but directly in an Org-mode file."
  3009. :group 'org-export-icalendar
  3010. :type 'boolean)
  3011. (defcustom org-icalendar-include-body 100
  3012. "Amount of text below headline to be included in iCalendar export.
  3013. This is a number of characters that should maximally be included.
  3014. Properties, scheduling and clocking lines will always be removed.
  3015. The text will be inserted into the DESCRIPTION field."
  3016. :group 'org-export-icalendar
  3017. :type '(choice
  3018. (const :tag "Nothing" nil)
  3019. (const :tag "Everything" t)
  3020. (integer :tag "Max characters")))
  3021. (defcustom org-icalendar-combined-name "OrgMode"
  3022. "Calendar name for the combined iCalendar representing all agenda files."
  3023. :group 'org-export-icalendar
  3024. :type 'string)
  3025. (defgroup org-font-lock nil
  3026. "Font-lock settings for highlighting in Org-mode."
  3027. :tag "Org Font Lock"
  3028. :group 'org)
  3029. (defcustom org-level-color-stars-only nil
  3030. "Non-nil means fontify only the stars in each headline.
  3031. When nil, the entire headline is fontified.
  3032. Changing it requires restart of `font-lock-mode' to become effective
  3033. also in regions already fontified."
  3034. :group 'org-font-lock
  3035. :type 'boolean)
  3036. (defcustom org-hide-leading-stars nil
  3037. "Non-nil means, hide the first N-1 stars in a headline.
  3038. This works by using the face `org-hide' for these stars. This
  3039. face is white for a light background, and black for a dark
  3040. background. You may have to customize the face `org-hide' to
  3041. make this work.
  3042. Changing it requires restart of `font-lock-mode' to become effective
  3043. also in regions already fontified.
  3044. You may also set this on a per-file basis by adding one of the following
  3045. lines to the buffer:
  3046. #+STARTUP: hidestars
  3047. #+STARTUP: showstars"
  3048. :group 'org-font-lock
  3049. :type 'boolean)
  3050. (defcustom org-fontify-done-headline nil
  3051. "Non-nil means, change the face of a headline if it is marked DONE.
  3052. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3053. When this is non-nil, the headline after the keyword is set to the
  3054. `org-headline-done' as an additional indication."
  3055. :group 'org-font-lock
  3056. :type 'boolean)
  3057. (defcustom org-fontify-emphasized-text t
  3058. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3059. Changing this variable requires a restart of Emacs to take effect."
  3060. :group 'org-font-lock
  3061. :type 'boolean)
  3062. (defcustom org-highlight-latex-fragments-and-specials nil
  3063. "Non-nil means, fontify what is treated specially by the exporters."
  3064. :group 'org-font-lock
  3065. :type 'boolean)
  3066. (defcustom org-hide-emphasis-markers nil
  3067. "Non-nil mean font-lock should hide the emphasis marker characters."
  3068. :group 'org-font-lock
  3069. :type 'boolean)
  3070. (defvar org-emph-re nil
  3071. "Regular expression for matching emphasis.")
  3072. (defvar org-verbatim-re nil
  3073. "Regular expression for matching verbatim text.")
  3074. (defvar org-emphasis-regexp-components) ; defined just below
  3075. (defvar org-emphasis-alist) ; defined just below
  3076. (defun org-set-emph-re (var val)
  3077. "Set variable and compute the emphasis regular expression."
  3078. (set var val)
  3079. (when (and (boundp 'org-emphasis-alist)
  3080. (boundp 'org-emphasis-regexp-components)
  3081. org-emphasis-alist org-emphasis-regexp-components)
  3082. (let* ((e org-emphasis-regexp-components)
  3083. (pre (car e))
  3084. (post (nth 1 e))
  3085. (border (nth 2 e))
  3086. (body (nth 3 e))
  3087. (nl (nth 4 e))
  3088. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3089. (body1 (concat body "*?"))
  3090. (markers (mapconcat 'car org-emphasis-alist ""))
  3091. (vmarkers (mapconcat
  3092. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3093. org-emphasis-alist "")))
  3094. ;; make sure special characters appear at the right position in the class
  3095. (if (string-match "\\^" markers)
  3096. (setq markers (concat (replace-match "" t t markers) "^")))
  3097. (if (string-match "-" markers)
  3098. (setq markers (concat (replace-match "" t t markers) "-")))
  3099. (if (string-match "\\^" vmarkers)
  3100. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3101. (if (string-match "-" vmarkers)
  3102. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3103. (if (> nl 0)
  3104. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3105. (int-to-string nl) "\\}")))
  3106. ;; Make the regexp
  3107. (setq org-emph-re
  3108. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3109. "\\("
  3110. "\\([" markers "]\\)"
  3111. "\\("
  3112. "[^" border "]\\|"
  3113. "[^" border (if (and nil stacked) markers) "]"
  3114. body1
  3115. "[^" border (if (and nil stacked) markers) "]"
  3116. "\\)"
  3117. "\\3\\)"
  3118. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3119. (setq org-verbatim-re
  3120. (concat "\\([" pre "]\\|^\\)"
  3121. "\\("
  3122. "\\([" vmarkers "]\\)"
  3123. "\\("
  3124. "[^" border "]\\|"
  3125. "[^" border "]"
  3126. body1
  3127. "[^" border "]"
  3128. "\\)"
  3129. "\\3\\)"
  3130. "\\([" post "]\\|$\\)")))))
  3131. (defcustom org-emphasis-regexp-components
  3132. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3133. "Components used to build the regular expression for emphasis.
  3134. This is a list with 6 entries. Terminology: In an emphasis string
  3135. like \" *strong word* \", we call the initial space PREMATCH, the final
  3136. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3137. and \"trong wor\" is the body. The different components in this variable
  3138. specify what is allowed/forbidden in each part:
  3139. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3140. post Chars allowed as postmatch. End of line will be allowed too.
  3141. border The chars *forbidden* as border characters.
  3142. body-regexp A regexp like \".\" to match a body character. Don't use
  3143. non-shy groups here, and don't allow newline here.
  3144. newline The maximum number of newlines allowed in an emphasis exp.
  3145. Use customize to modify this, or restart Emacs after changing it."
  3146. :group 'org-font-lock
  3147. :set 'org-set-emph-re
  3148. :type '(list
  3149. (sexp :tag "Allowed chars in pre ")
  3150. (sexp :tag "Allowed chars in post ")
  3151. (sexp :tag "Forbidden chars in border ")
  3152. (sexp :tag "Regexp for body ")
  3153. (integer :tag "number of newlines allowed")
  3154. (option (boolean :tag "Stacking (DISABLED) "))))
  3155. (defcustom org-emphasis-alist
  3156. '(("*" bold "<b>" "</b>")
  3157. ("/" italic "<i>" "</i>")
  3158. ("_" underline "<u>" "</u>")
  3159. ("=" org-code "<code>" "</code>" verbatim)
  3160. ("~" org-verbatim "" "" verbatim)
  3161. ("+" (:strike-through t) "<del>" "</del>")
  3162. )
  3163. "Special syntax for emphasized text.
  3164. Text starting and ending with a special character will be emphasized, for
  3165. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3166. characters, the face to be used by font-lock for highlighting in Org-mode
  3167. Emacs buffers, and the HTML tags to be used for this.
  3168. Use customize to modify this, or restart Emacs after changing it."
  3169. :group 'org-font-lock
  3170. :set 'org-set-emph-re
  3171. :type '(repeat
  3172. (list
  3173. (string :tag "Marker character")
  3174. (choice
  3175. (face :tag "Font-lock-face")
  3176. (plist :tag "Face property list"))
  3177. (string :tag "HTML start tag")
  3178. (string :tag "HTML end tag")
  3179. (option (const verbatim)))))
  3180. ;;; The faces
  3181. (defgroup org-faces nil
  3182. "Faces in Org-mode."
  3183. :tag "Org Faces"
  3184. :group 'org-font-lock)
  3185. (defun org-compatible-face (inherits specs)
  3186. "Make a compatible face specification.
  3187. If INHERITS is an existing face and if the Emacs version supports it,
  3188. just inherit the face. If not, use SPECS to define the face.
  3189. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3190. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3191. to the top of the list. The `min-colors' attribute will be removed from
  3192. any other entries, and any resulting duplicates will be removed entirely."
  3193. (cond
  3194. ((and inherits (facep inherits)
  3195. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3196. ;; In Emacs 23, we use inheritance where possible.
  3197. ;; We only do this in Emacs 23, because only there the outline
  3198. ;; faces have been changed to the original org-mode-level-faces.
  3199. (list (list t :inherit inherits)))
  3200. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3201. ;; These do not understand the `min-colors' attribute.
  3202. (let (r e a)
  3203. (while (setq e (pop specs))
  3204. (cond
  3205. ((memq (car e) '(t default)) (push e r))
  3206. ((setq a (member '(min-colors 8) (car e)))
  3207. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3208. (cdr e)))))
  3209. ((setq a (assq 'min-colors (car e)))
  3210. (setq e (cons (delq a (car e)) (cdr e)))
  3211. (or (assoc (car e) r) (push e r)))
  3212. (t (or (assoc (car e) r) (push e r)))))
  3213. (nreverse r)))
  3214. (t specs)))
  3215. (put 'org-compatible-face 'lisp-indent-function 1)
  3216. (defface org-hide
  3217. '((((background light)) (:foreground "white"))
  3218. (((background dark)) (:foreground "black")))
  3219. "Face used to hide leading stars in headlines.
  3220. The forground color of this face should be equal to the background
  3221. color of the frame."
  3222. :group 'org-faces)
  3223. (defface org-level-1 ;; font-lock-function-name-face
  3224. (org-compatible-face 'outline-1
  3225. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3226. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3227. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3228. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3229. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3230. (t (:bold t))))
  3231. "Face used for level 1 headlines."
  3232. :group 'org-faces)
  3233. (defface org-level-2 ;; font-lock-variable-name-face
  3234. (org-compatible-face 'outline-2
  3235. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3236. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3237. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3238. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3239. (t (:bold t))))
  3240. "Face used for level 2 headlines."
  3241. :group 'org-faces)
  3242. (defface org-level-3 ;; font-lock-keyword-face
  3243. (org-compatible-face 'outline-3
  3244. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3245. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3246. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3247. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3248. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3249. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3250. (t (:bold t))))
  3251. "Face used for level 3 headlines."
  3252. :group 'org-faces)
  3253. (defface org-level-4 ;; font-lock-comment-face
  3254. (org-compatible-face 'outline-4
  3255. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3256. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3257. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3258. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3259. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3260. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3261. (t (:bold t))))
  3262. "Face used for level 4 headlines."
  3263. :group 'org-faces)
  3264. (defface org-level-5 ;; font-lock-type-face
  3265. (org-compatible-face 'outline-5
  3266. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3267. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3268. (((class color) (min-colors 8)) (:foreground "green"))))
  3269. "Face used for level 5 headlines."
  3270. :group 'org-faces)
  3271. (defface org-level-6 ;; font-lock-constant-face
  3272. (org-compatible-face 'outline-6
  3273. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3274. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3275. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3276. "Face used for level 6 headlines."
  3277. :group 'org-faces)
  3278. (defface org-level-7 ;; font-lock-builtin-face
  3279. (org-compatible-face 'outline-7
  3280. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3281. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3282. (((class color) (min-colors 8)) (:foreground "blue"))))
  3283. "Face used for level 7 headlines."
  3284. :group 'org-faces)
  3285. (defface org-level-8 ;; font-lock-string-face
  3286. (org-compatible-face 'outline-8
  3287. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3288. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3289. (((class color) (min-colors 8)) (:foreground "green"))))
  3290. "Face used for level 8 headlines."
  3291. :group 'org-faces)
  3292. (defface org-special-keyword ;; font-lock-string-face
  3293. (org-compatible-face nil
  3294. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3295. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3296. (t (:italic t))))
  3297. "Face used for special keywords."
  3298. :group 'org-faces)
  3299. (defface org-drawer ;; font-lock-function-name-face
  3300. (org-compatible-face nil
  3301. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3302. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3303. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3304. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3305. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3306. (t (:bold t))))
  3307. "Face used for drawers."
  3308. :group 'org-faces)
  3309. (defface org-property-value nil
  3310. "Face used for the value of a property."
  3311. :group 'org-faces)
  3312. (defface org-column
  3313. (org-compatible-face nil
  3314. '((((class color) (min-colors 16) (background light))
  3315. (:background "grey90"))
  3316. (((class color) (min-colors 16) (background dark))
  3317. (:background "grey30"))
  3318. (((class color) (min-colors 8))
  3319. (:background "cyan" :foreground "black"))
  3320. (t (:inverse-video t))))
  3321. "Face for column display of entry properties."
  3322. :group 'org-faces)
  3323. (when (fboundp 'set-face-attribute)
  3324. ;; Make sure that a fixed-width face is used when we have a column table.
  3325. (set-face-attribute 'org-column nil
  3326. :height (face-attribute 'default :height)
  3327. :family (face-attribute 'default :family)))
  3328. (defface org-warning
  3329. (org-compatible-face 'font-lock-warning-face
  3330. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3331. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3332. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3333. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3334. (t (:bold t))))
  3335. "Face for deadlines and TODO keywords."
  3336. :group 'org-faces)
  3337. (defface org-archived ; similar to shadow
  3338. (org-compatible-face 'shadow
  3339. '((((class color grayscale) (min-colors 88) (background light))
  3340. (:foreground "grey50"))
  3341. (((class color grayscale) (min-colors 88) (background dark))
  3342. (:foreground "grey70"))
  3343. (((class color) (min-colors 8) (background light))
  3344. (:foreground "green"))
  3345. (((class color) (min-colors 8) (background dark))
  3346. (:foreground "yellow"))))
  3347. "Face for headline with the ARCHIVE tag."
  3348. :group 'org-faces)
  3349. (defface org-link
  3350. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3351. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3352. (t (:underline t)))
  3353. "Face for links."
  3354. :group 'org-faces)
  3355. (defface org-ellipsis
  3356. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3357. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3358. (t (:strike-through t)))
  3359. "Face for the ellipsis in folded text."
  3360. :group 'org-faces)
  3361. (defface org-target
  3362. '((((class color) (background light)) (:underline t))
  3363. (((class color) (background dark)) (:underline t))
  3364. (t (:underline t)))
  3365. "Face for links."
  3366. :group 'org-faces)
  3367. (defface org-date
  3368. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3369. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3370. (t (:underline t)))
  3371. "Face for links."
  3372. :group 'org-faces)
  3373. (defface org-sexp-date
  3374. '((((class color) (background light)) (:foreground "Purple"))
  3375. (((class color) (background dark)) (:foreground "Cyan"))
  3376. (t (:underline t)))
  3377. "Face for links."
  3378. :group 'org-faces)
  3379. (defface org-tag
  3380. '((t (:bold t)))
  3381. "Face for tags."
  3382. :group 'org-faces)
  3383. (defface org-todo ; font-lock-warning-face
  3384. (org-compatible-face nil
  3385. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3386. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3387. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3388. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3389. (t (:inverse-video t :bold t))))
  3390. "Face for TODO keywords."
  3391. :group 'org-faces)
  3392. (defface org-done ;; font-lock-type-face
  3393. (org-compatible-face nil
  3394. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3395. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3396. (((class color) (min-colors 8)) (:foreground "green"))
  3397. (t (:bold t))))
  3398. "Face used for todo keywords that indicate DONE items."
  3399. :group 'org-faces)
  3400. (defface org-headline-done ;; font-lock-string-face
  3401. (org-compatible-face nil
  3402. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3403. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3404. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3405. "Face used to indicate that a headline is DONE.
  3406. This face is only used if `org-fontify-done-headline' is set. If applies
  3407. to the part of the headline after the DONE keyword."
  3408. :group 'org-faces)
  3409. (defcustom org-todo-keyword-faces nil
  3410. "Faces for specific TODO keywords.
  3411. This is a list of cons cells, with TODO keywords in the car
  3412. and faces in the cdr. The face can be a symbol, or a property
  3413. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3414. :group 'org-faces
  3415. :group 'org-todo
  3416. :type '(repeat
  3417. (cons
  3418. (string :tag "keyword")
  3419. (sexp :tag "face"))))
  3420. (defface org-table ;; font-lock-function-name-face
  3421. (org-compatible-face nil
  3422. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3423. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3424. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3425. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3426. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3427. (((class color) (min-colors 8) (background dark)))))
  3428. "Face used for tables."
  3429. :group 'org-faces)
  3430. (defface org-formula
  3431. (org-compatible-face nil
  3432. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3433. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3434. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3435. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3436. (t (:bold t :italic t))))
  3437. "Face for formulas."
  3438. :group 'org-faces)
  3439. (defface org-code
  3440. (org-compatible-face nil
  3441. '((((class color grayscale) (min-colors 88) (background light))
  3442. (:foreground "grey50"))
  3443. (((class color grayscale) (min-colors 88) (background dark))
  3444. (:foreground "grey70"))
  3445. (((class color) (min-colors 8) (background light))
  3446. (:foreground "green"))
  3447. (((class color) (min-colors 8) (background dark))
  3448. (:foreground "yellow"))))
  3449. "Face for fixed-with text like code snippets."
  3450. :group 'org-faces
  3451. :version "22.1")
  3452. (defface org-verbatim
  3453. (org-compatible-face nil
  3454. '((((class color grayscale) (min-colors 88) (background light))
  3455. (:foreground "grey50" :underline t))
  3456. (((class color grayscale) (min-colors 88) (background dark))
  3457. (:foreground "grey70" :underline t))
  3458. (((class color) (min-colors 8) (background light))
  3459. (:foreground "green" :underline t))
  3460. (((class color) (min-colors 8) (background dark))
  3461. (:foreground "yellow" :underline t))))
  3462. "Face for fixed-with text like code snippets."
  3463. :group 'org-faces
  3464. :version "22.1")
  3465. (defface org-agenda-structure ;; font-lock-function-name-face
  3466. (org-compatible-face nil
  3467. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3468. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3469. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3470. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3471. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3472. (t (:bold t))))
  3473. "Face used in agenda for captions and dates."
  3474. :group 'org-faces)
  3475. (defface org-scheduled-today
  3476. (org-compatible-face nil
  3477. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3478. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3479. (((class color) (min-colors 8)) (:foreground "green"))
  3480. (t (:bold t :italic t))))
  3481. "Face for items scheduled for a certain day."
  3482. :group 'org-faces)
  3483. (defface org-scheduled-previously
  3484. (org-compatible-face nil
  3485. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3486. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3487. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3488. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3489. (t (:bold t))))
  3490. "Face for items scheduled previously, and not yet done."
  3491. :group 'org-faces)
  3492. (defface org-upcoming-deadline
  3493. (org-compatible-face nil
  3494. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3495. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3496. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3497. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3498. (t (:bold t))))
  3499. "Face for items scheduled previously, and not yet done."
  3500. :group 'org-faces)
  3501. (defcustom org-agenda-deadline-faces
  3502. '((1.0 . org-warning)
  3503. (0.5 . org-upcoming-deadline)
  3504. (0.0 . default))
  3505. "Faces for showing deadlines in the agenda.
  3506. This is a list of cons cells. The cdr of each cess is a face to be used,
  3507. and it can also just be a like like '(:foreground \"yellow\").
  3508. Each car is a fraction of the head-warning time that must have passed for
  3509. this the face in the cdr to be used for display. The numbers must be
  3510. given in descending order. The head-warning time is normally taken
  3511. from `org-deadline-warning-days', but can also be specified in the deadline
  3512. timestamp itself, like this:
  3513. DEADLINE: <2007-08-13 Mon -8d>
  3514. You may use d for days, w for weeks, m for months and y for years. Months
  3515. and years will only be treated in an approximate fashion (30.4 days for a
  3516. month and 365.24 days for a year)."
  3517. :group 'org-faces
  3518. :group 'org-agenda-daily/weekly
  3519. :type '(repeat
  3520. (cons
  3521. (number :tag "Fraction of head-warning time passed")
  3522. (sexp :tag "Face"))))
  3523. ;; FIXME: this is not a good face yet.
  3524. (defface org-agenda-restriction-lock
  3525. (org-compatible-face nil
  3526. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3527. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3528. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3529. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3530. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3531. (t (:inverse-video t))))
  3532. "Face for showing the agenda restriction lock."
  3533. :group 'org-faces)
  3534. (defface org-time-grid ;; font-lock-variable-name-face
  3535. (org-compatible-face nil
  3536. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3537. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3538. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3539. "Face used for time grids."
  3540. :group 'org-faces)
  3541. (defconst org-level-faces
  3542. '(org-level-1 org-level-2 org-level-3 org-level-4
  3543. org-level-5 org-level-6 org-level-7 org-level-8
  3544. ))
  3545. (defcustom org-n-level-faces (length org-level-faces)
  3546. "The number different faces to be used for headlines.
  3547. Org-mode defines 8 different headline faces, so this can be at most 8.
  3548. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3549. :type 'number
  3550. :group 'org-faces)
  3551. ;;; Functions and variables from ther packages
  3552. ;; Declared here to avoid compiler warnings
  3553. (eval-and-compile
  3554. (unless (fboundp 'declare-function)
  3555. (defmacro declare-function (fn file &optional arglist fileonly))))
  3556. ;; XEmacs only
  3557. (defvar outline-mode-menu-heading)
  3558. (defvar outline-mode-menu-show)
  3559. (defvar outline-mode-menu-hide)
  3560. (defvar zmacs-regions) ; XEmacs regions
  3561. ;; Emacs only
  3562. (defvar mark-active)
  3563. ;; Various packages
  3564. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3565. (declare-function add-to-diary-list "diary-lib"
  3566. (date string specifier &optional marker globcolor literal))
  3567. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3568. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3569. (declare-function Info-goto-node "info" (nodename &optional fork))
  3570. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3571. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3572. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3573. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3574. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3575. (declare-function bbdb-record-name "ext:bbdb" (record))
  3576. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3577. (declare-function bibtex-generate-autokey "bibtex" ())
  3578. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3579. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3580. (defvar calc-embedded-close-formula)
  3581. (defvar calc-embedded-open-formula)
  3582. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3583. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3584. (declare-function calendar-check-holidays "holidays" (date))
  3585. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3586. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3587. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3588. (declare-function calendar-forward-day "cal-move" (arg))
  3589. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3590. (declare-function calendar-goto-date "cal-move" (date))
  3591. (declare-function calendar-goto-today "cal-move" ())
  3592. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3593. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3594. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3595. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3596. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3597. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3598. (defvar calendar-mode-map)
  3599. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3600. (declare-function cdlatex-tab "ext:cdlatex" ())
  3601. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3602. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3603. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3604. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3605. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3606. (defvar font-lock-unfontify-region-function)
  3607. (declare-function gnus-article-show-summary "gnus-art" ())
  3608. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3609. (defvar gnus-other-frame-object)
  3610. (defvar gnus-group-name)
  3611. (defvar gnus-article-current)
  3612. (defvar Info-current-file)
  3613. (defvar Info-current-node)
  3614. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3615. (declare-function mh-find-path "mh-utils" ())
  3616. (declare-function mh-get-header-field "mh-utils" (field))
  3617. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3618. (declare-function mh-header-display "mh-show" ())
  3619. (declare-function mh-index-previous-folder "mh-search" ())
  3620. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3621. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3622. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3623. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3624. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3625. (declare-function mh-show-header-display "mh-show" t t)
  3626. (declare-function mh-show-msg "mh-show" (msg))
  3627. (declare-function mh-show-show "mh-show" t t)
  3628. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3629. (defvar mh-progs)
  3630. (defvar mh-current-folder)
  3631. (defvar mh-show-folder-buffer)
  3632. (defvar mh-index-folder)
  3633. (defvar mh-searcher)
  3634. (declare-function org-export-latex-cleaned-string "org-export-latex" (&optional commentsp))
  3635. (declare-function parse-time-string "parse-time" (string))
  3636. (declare-function remember "remember" (&optional initial))
  3637. (declare-function remember-buffer-desc "remember" ())
  3638. (defvar remember-save-after-remembering)
  3639. (defvar remember-data-file)
  3640. (defvar remember-register)
  3641. (defvar remember-buffer)
  3642. (defvar remember-handler-functions)
  3643. (defvar remember-annotation-functions)
  3644. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3645. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3646. (declare-function rmail-what-message "rmail" ())
  3647. (defvar texmathp-why)
  3648. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3649. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3650. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3651. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3652. (declare-function vm-isearch-update "ext:vm-search" ())
  3653. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3654. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3655. (declare-function vm-su-subject "ext:vm-summary" (m))
  3656. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3657. (defvar vm-message-pointer)
  3658. (defvar vm-folder-directory)
  3659. (defvar w3m-current-url)
  3660. (defvar w3m-current-title)
  3661. ;; backward compatibility to old version of wl
  3662. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3663. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3664. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3665. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3666. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3667. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3668. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3669. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3670. (defvar wl-summary-buffer-elmo-folder)
  3671. (defvar wl-summary-buffer-folder-name)
  3672. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3673. (defvar org-latex-regexps)
  3674. (defvar constants-unit-system)
  3675. ;;; Variables for pre-computed regular expressions, all buffer local
  3676. (defvar org-drawer-regexp nil
  3677. "Matches first line of a hidden block.")
  3678. (make-variable-buffer-local 'org-drawer-regexp)
  3679. (defvar org-todo-regexp nil
  3680. "Matches any of the TODO state keywords.")
  3681. (make-variable-buffer-local 'org-todo-regexp)
  3682. (defvar org-not-done-regexp nil
  3683. "Matches any of the TODO state keywords except the last one.")
  3684. (make-variable-buffer-local 'org-not-done-regexp)
  3685. (defvar org-todo-line-regexp nil
  3686. "Matches a headline and puts TODO state into group 2 if present.")
  3687. (make-variable-buffer-local 'org-todo-line-regexp)
  3688. (defvar org-complex-heading-regexp nil
  3689. "Matches a headline and puts everything into groups:
  3690. group 1: the stars
  3691. group 2: The todo keyword, maybe
  3692. group 3: Priority cookie
  3693. group 4: True headline
  3694. group 5: Tags")
  3695. (make-variable-buffer-local 'org-complex-heading-regexp)
  3696. (defvar org-todo-line-tags-regexp nil
  3697. "Matches a headline and puts TODO state into group 2 if present.
  3698. Also put tags into group 4 if tags are present.")
  3699. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3700. (defvar org-nl-done-regexp nil
  3701. "Matches newline followed by a headline with the DONE keyword.")
  3702. (make-variable-buffer-local 'org-nl-done-regexp)
  3703. (defvar org-looking-at-done-regexp nil
  3704. "Matches the DONE keyword a point.")
  3705. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3706. (defvar org-ds-keyword-length 12
  3707. "Maximum length of the Deadline and SCHEDULED keywords.")
  3708. (make-variable-buffer-local 'org-ds-keyword-length)
  3709. (defvar org-deadline-regexp nil
  3710. "Matches the DEADLINE keyword.")
  3711. (make-variable-buffer-local 'org-deadline-regexp)
  3712. (defvar org-deadline-time-regexp nil
  3713. "Matches the DEADLINE keyword together with a time stamp.")
  3714. (make-variable-buffer-local 'org-deadline-time-regexp)
  3715. (defvar org-deadline-line-regexp nil
  3716. "Matches the DEADLINE keyword and the rest of the line.")
  3717. (make-variable-buffer-local 'org-deadline-line-regexp)
  3718. (defvar org-scheduled-regexp nil
  3719. "Matches the SCHEDULED keyword.")
  3720. (make-variable-buffer-local 'org-scheduled-regexp)
  3721. (defvar org-scheduled-time-regexp nil
  3722. "Matches the SCHEDULED keyword together with a time stamp.")
  3723. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3724. (defvar org-closed-time-regexp nil
  3725. "Matches the CLOSED keyword together with a time stamp.")
  3726. (make-variable-buffer-local 'org-closed-time-regexp)
  3727. (defvar org-keyword-time-regexp nil
  3728. "Matches any of the 4 keywords, together with the time stamp.")
  3729. (make-variable-buffer-local 'org-keyword-time-regexp)
  3730. (defvar org-keyword-time-not-clock-regexp nil
  3731. "Matches any of the 3 keywords, together with the time stamp.")
  3732. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3733. (defvar org-maybe-keyword-time-regexp nil
  3734. "Matches a timestamp, possibly preceeded by a keyword.")
  3735. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3736. (defvar org-planning-or-clock-line-re nil
  3737. "Matches a line with planning or clock info.")
  3738. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3739. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3740. rear-nonsticky t mouse-map t fontified t)
  3741. "Properties to remove when a string without properties is wanted.")
  3742. (defsubst org-match-string-no-properties (num &optional string)
  3743. (if (featurep 'xemacs)
  3744. (let ((s (match-string num string)))
  3745. (remove-text-properties 0 (length s) org-rm-props s)
  3746. s)
  3747. (match-string-no-properties num string)))
  3748. (defsubst org-no-properties (s)
  3749. (if (fboundp 'set-text-properties)
  3750. (set-text-properties 0 (length s) nil s)
  3751. (remove-text-properties 0 (length s) org-rm-props s))
  3752. s)
  3753. (defsubst org-get-alist-option (option key)
  3754. (cond ((eq key t) t)
  3755. ((eq option t) t)
  3756. ((assoc key option) (cdr (assoc key option)))
  3757. (t (cdr (assq 'default option)))))
  3758. (defsubst org-inhibit-invisibility ()
  3759. "Modified `buffer-invisibility-spec' for Emacs 21.
  3760. Some ops with invisible text do not work correctly on Emacs 21. For these
  3761. we turn off invisibility temporarily. Use this in a `let' form."
  3762. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3763. (defsubst org-set-local (var value)
  3764. "Make VAR local in current buffer and set it to VALUE."
  3765. (set (make-variable-buffer-local var) value))
  3766. (defsubst org-mode-p ()
  3767. "Check if the current buffer is in Org-mode."
  3768. (eq major-mode 'org-mode))
  3769. (defsubst org-last (list)
  3770. "Return the last element of LIST."
  3771. (car (last list)))
  3772. (defun org-let (list &rest body)
  3773. (eval (cons 'let (cons list body))))
  3774. (put 'org-let 'lisp-indent-function 1)
  3775. (defun org-let2 (list1 list2 &rest body)
  3776. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3777. (put 'org-let2 'lisp-indent-function 2)
  3778. (defconst org-startup-options
  3779. '(("fold" org-startup-folded t)
  3780. ("overview" org-startup-folded t)
  3781. ("nofold" org-startup-folded nil)
  3782. ("showall" org-startup-folded nil)
  3783. ("content" org-startup-folded content)
  3784. ("hidestars" org-hide-leading-stars t)
  3785. ("showstars" org-hide-leading-stars nil)
  3786. ("odd" org-odd-levels-only t)
  3787. ("oddeven" org-odd-levels-only nil)
  3788. ("align" org-startup-align-all-tables t)
  3789. ("noalign" org-startup-align-all-tables nil)
  3790. ("customtime" org-display-custom-times t)
  3791. ("logging" org-log-done t)
  3792. ("logdone" org-log-done t)
  3793. ("nologging" org-log-done nil)
  3794. ("lognotedone" org-log-done done push)
  3795. ("lognotestate" org-log-done state push)
  3796. ("lognoteclock-out" org-log-done clock-out push)
  3797. ("logrepeat" org-log-repeat t)
  3798. ("nologrepeat" org-log-repeat nil)
  3799. ("constcgs" constants-unit-system cgs)
  3800. ("constSI" constants-unit-system SI))
  3801. "Variable associated with STARTUP options for org-mode.
  3802. Each element is a list of three items: The startup options as written
  3803. in the #+STARTUP line, the corresponding variable, and the value to
  3804. set this variable to if the option is found. An optional forth element PUSH
  3805. means to push this value onto the list in the variable.")
  3806. (defun org-set-regexps-and-options ()
  3807. "Precompute regular expressions for current buffer."
  3808. (when (org-mode-p)
  3809. (org-set-local 'org-todo-kwd-alist nil)
  3810. (org-set-local 'org-todo-key-alist nil)
  3811. (org-set-local 'org-todo-key-trigger nil)
  3812. (org-set-local 'org-todo-keywords-1 nil)
  3813. (org-set-local 'org-done-keywords nil)
  3814. (org-set-local 'org-todo-heads nil)
  3815. (org-set-local 'org-todo-sets nil)
  3816. (org-set-local 'org-todo-log-states nil)
  3817. (let ((re (org-make-options-regexp
  3818. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3819. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3820. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3821. (splitre "[ \t]+")
  3822. kwds kws0 kwsa key value cat arch tags const links hw dws
  3823. tail sep kws1 prio props drawers
  3824. ex log)
  3825. (save-excursion
  3826. (save-restriction
  3827. (widen)
  3828. (goto-char (point-min))
  3829. (while (re-search-forward re nil t)
  3830. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3831. (cond
  3832. ((equal key "CATEGORY")
  3833. (if (string-match "[ \t]+$" value)
  3834. (setq value (replace-match "" t t value)))
  3835. (setq cat value))
  3836. ((member key '("SEQ_TODO" "TODO"))
  3837. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3838. ((equal key "TYP_TODO")
  3839. (push (cons 'type (org-split-string value splitre)) kwds))
  3840. ((equal key "TAGS")
  3841. (setq tags (append tags (org-split-string value splitre))))
  3842. ((equal key "COLUMNS")
  3843. (org-set-local 'org-columns-default-format value))
  3844. ((equal key "LINK")
  3845. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3846. (push (cons (match-string 1 value)
  3847. (org-trim (match-string 2 value)))
  3848. links)))
  3849. ((equal key "PRIORITIES")
  3850. (setq prio (org-split-string value " +")))
  3851. ((equal key "PROPERTY")
  3852. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3853. (push (cons (match-string 1 value) (match-string 2 value))
  3854. props)))
  3855. ((equal key "DRAWERS")
  3856. (setq drawers (org-split-string value splitre)))
  3857. ((equal key "CONSTANTS")
  3858. (setq const (append const (org-split-string value splitre))))
  3859. ((equal key "STARTUP")
  3860. (let ((opts (org-split-string value splitre))
  3861. l var val)
  3862. (while (setq l (pop opts))
  3863. (when (setq l (assoc l org-startup-options))
  3864. (setq var (nth 1 l) val (nth 2 l))
  3865. (if (not (nth 3 l))
  3866. (set (make-local-variable var) val)
  3867. (if (not (listp (symbol-value var)))
  3868. (set (make-local-variable var) nil))
  3869. (set (make-local-variable var) (symbol-value var))
  3870. (add-to-list var val))))))
  3871. ((equal key "ARCHIVE")
  3872. (string-match " *$" value)
  3873. (setq arch (replace-match "" t t value))
  3874. (remove-text-properties 0 (length arch)
  3875. '(face t fontified t) arch)))
  3876. )))
  3877. (when cat
  3878. (org-set-local 'org-category (intern cat))
  3879. (push (cons "CATEGORY" cat) props))
  3880. (when prio
  3881. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3882. (setq prio (mapcar 'string-to-char prio))
  3883. (org-set-local 'org-highest-priority (nth 0 prio))
  3884. (org-set-local 'org-lowest-priority (nth 1 prio))
  3885. (org-set-local 'org-default-priority (nth 2 prio)))
  3886. (and props (org-set-local 'org-local-properties (nreverse props)))
  3887. (and drawers (org-set-local 'org-drawers drawers))
  3888. (and arch (org-set-local 'org-archive-location arch))
  3889. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3890. ;; Process the TODO keywords
  3891. (unless kwds
  3892. ;; Use the global values as if they had been given locally.
  3893. (setq kwds (default-value 'org-todo-keywords))
  3894. (if (stringp (car kwds))
  3895. (setq kwds (list (cons org-todo-interpretation
  3896. (default-value 'org-todo-keywords)))))
  3897. (setq kwds (reverse kwds)))
  3898. (setq kwds (nreverse kwds))
  3899. (let (inter kws kw)
  3900. (while (setq kws (pop kwds))
  3901. (setq inter (pop kws) sep (member "|" kws)
  3902. kws0 (delete "|" (copy-sequence kws))
  3903. kwsa nil
  3904. kws1 (mapcar
  3905. (lambda (x)
  3906. (if (string-match "^\\(.*?\\)\\(?:(\\(..?\\))\\)?$" x)
  3907. (progn
  3908. (setq kw (match-string 1 x)
  3909. ex (and (match-end 2) (match-string 2 x))
  3910. log (and ex (string-match "@" ex))
  3911. key (and ex (substring ex 0 1)))
  3912. (if (equal key "@") (setq key nil))
  3913. (push (cons kw (and key (string-to-char key))) kwsa)
  3914. (and log (push kw org-todo-log-states))
  3915. kw)
  3916. (error "Invalid TODO keyword %s" x)))
  3917. kws0)
  3918. kwsa (if kwsa (append '((:startgroup))
  3919. (nreverse kwsa)
  3920. '((:endgroup))))
  3921. hw (car kws1)
  3922. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3923. tail (list inter hw (car dws) (org-last dws)))
  3924. (add-to-list 'org-todo-heads hw 'append)
  3925. (push kws1 org-todo-sets)
  3926. (setq org-done-keywords (append org-done-keywords dws nil))
  3927. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3928. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3929. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3930. (setq org-todo-sets (nreverse org-todo-sets)
  3931. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3932. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3933. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3934. ;; Process the constants
  3935. (when const
  3936. (let (e cst)
  3937. (while (setq e (pop const))
  3938. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3939. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3940. (setq org-table-formula-constants-local cst)))
  3941. ;; Process the tags.
  3942. (when tags
  3943. (let (e tgs)
  3944. (while (setq e (pop tags))
  3945. (cond
  3946. ((equal e "{") (push '(:startgroup) tgs))
  3947. ((equal e "}") (push '(:endgroup) tgs))
  3948. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3949. (push (cons (match-string 1 e)
  3950. (string-to-char (match-string 2 e)))
  3951. tgs))
  3952. (t (push (list e) tgs))))
  3953. (org-set-local 'org-tag-alist nil)
  3954. (while (setq e (pop tgs))
  3955. (or (and (stringp (car e))
  3956. (assoc (car e) org-tag-alist))
  3957. (push e org-tag-alist))))))
  3958. ;; Compute the regular expressions and other local variables
  3959. (if (not org-done-keywords)
  3960. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3961. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3962. (length org-scheduled-string)))
  3963. org-drawer-regexp
  3964. (concat "^[ \t]*:\\("
  3965. (mapconcat 'regexp-quote org-drawers "\\|")
  3966. "\\):[ \t]*$")
  3967. org-not-done-keywords
  3968. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3969. org-todo-regexp
  3970. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3971. "\\|") "\\)\\>")
  3972. org-not-done-regexp
  3973. (concat "\\<\\("
  3974. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3975. "\\)\\>")
  3976. org-todo-line-regexp
  3977. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3978. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3979. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3980. org-complex-heading-regexp
  3981. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  3982. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3983. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3984. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3985. org-nl-done-regexp
  3986. (concat "\n\\*+[ \t]+"
  3987. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3988. "\\)" "\\>")
  3989. org-todo-line-tags-regexp
  3990. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3991. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3992. (org-re
  3993. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3994. org-looking-at-done-regexp
  3995. (concat "^" "\\(?:"
  3996. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3997. "\\>")
  3998. org-deadline-regexp (concat "\\<" org-deadline-string)
  3999. org-deadline-time-regexp
  4000. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4001. org-deadline-line-regexp
  4002. (concat "\\<\\(" org-deadline-string "\\).*")
  4003. org-scheduled-regexp
  4004. (concat "\\<" org-scheduled-string)
  4005. org-scheduled-time-regexp
  4006. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4007. org-closed-time-regexp
  4008. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4009. org-keyword-time-regexp
  4010. (concat "\\<\\(" org-scheduled-string
  4011. "\\|" org-deadline-string
  4012. "\\|" org-closed-string
  4013. "\\|" org-clock-string "\\)"
  4014. " *[[<]\\([^]>]+\\)[]>]")
  4015. org-keyword-time-not-clock-regexp
  4016. (concat "\\<\\(" org-scheduled-string
  4017. "\\|" org-deadline-string
  4018. "\\|" org-closed-string
  4019. "\\)"
  4020. " *[[<]\\([^]>]+\\)[]>]")
  4021. org-maybe-keyword-time-regexp
  4022. (concat "\\(\\<\\(" org-scheduled-string
  4023. "\\|" org-deadline-string
  4024. "\\|" org-closed-string
  4025. "\\|" org-clock-string "\\)\\)?"
  4026. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4027. org-planning-or-clock-line-re
  4028. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4029. "\\|" org-deadline-string
  4030. "\\|" org-closed-string "\\|" org-clock-string
  4031. "\\)\\>\\)")
  4032. )
  4033. (org-compute-latex-and-specials-regexp)
  4034. (org-set-font-lock-defaults)))
  4035. (defun org-remove-keyword-keys (list)
  4036. (mapcar (lambda (x)
  4037. (if (string-match "(..?)$" x)
  4038. (substring x 0 (match-beginning 0))
  4039. x))
  4040. list))
  4041. ;; FIXME: this could be done much better, using second characters etc.
  4042. (defun org-assign-fast-keys (alist)
  4043. "Assign fast keys to a keyword-key alist.
  4044. Respect keys that are already there."
  4045. (let (new e k c c1 c2 (char ?a))
  4046. (while (setq e (pop alist))
  4047. (cond
  4048. ((equal e '(:startgroup)) (push e new))
  4049. ((equal e '(:endgroup)) (push e new))
  4050. (t
  4051. (setq k (car e) c2 nil)
  4052. (if (cdr e)
  4053. (setq c (cdr e))
  4054. ;; automatically assign a character.
  4055. (setq c1 (string-to-char
  4056. (downcase (substring
  4057. k (if (= (string-to-char k) ?@) 1 0)))))
  4058. (if (or (rassoc c1 new) (rassoc c1 alist))
  4059. (while (or (rassoc char new) (rassoc char alist))
  4060. (setq char (1+ char)))
  4061. (setq c2 c1))
  4062. (setq c (or c2 char)))
  4063. (push (cons k c) new))))
  4064. (nreverse new)))
  4065. ;;; Some variables ujsed in various places
  4066. (defvar org-window-configuration nil
  4067. "Used in various places to store a window configuration.")
  4068. (defvar org-finish-function nil
  4069. "Function to be called when `C-c C-c' is used.
  4070. This is for getting out of special buffers like remember.")
  4071. ;; FIXME: Occasionally check by commenting these, to make sure
  4072. ;; no other functions uses these, forgetting to let-bind them.
  4073. (defvar entry)
  4074. (defvar state)
  4075. (defvar last-state)
  4076. (defvar date)
  4077. (defvar description)
  4078. ;; Defined somewhere in this file, but used before definition.
  4079. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4080. (defvar org-agenda-buffer-name)
  4081. (defvar org-agenda-undo-list)
  4082. (defvar org-agenda-pending-undo-list)
  4083. (defvar org-agenda-overriding-header)
  4084. (defvar orgtbl-mode)
  4085. (defvar org-html-entities)
  4086. (defvar org-struct-menu)
  4087. (defvar org-org-menu)
  4088. (defvar org-tbl-menu)
  4089. (defvar org-agenda-keymap)
  4090. ;;;; Emacs/XEmacs compatibility
  4091. ;; Overlay compatibility functions
  4092. (defun org-make-overlay (beg end &optional buffer)
  4093. (if (featurep 'xemacs)
  4094. (make-extent beg end buffer)
  4095. (make-overlay beg end buffer)))
  4096. (defun org-delete-overlay (ovl)
  4097. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4098. (defun org-detach-overlay (ovl)
  4099. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4100. (defun org-move-overlay (ovl beg end &optional buffer)
  4101. (if (featurep 'xemacs)
  4102. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4103. (move-overlay ovl beg end buffer)))
  4104. (defun org-overlay-put (ovl prop value)
  4105. (if (featurep 'xemacs)
  4106. (set-extent-property ovl prop value)
  4107. (overlay-put ovl prop value)))
  4108. (defun org-overlay-display (ovl text &optional face evap)
  4109. "Make overlay OVL display TEXT with face FACE."
  4110. (if (featurep 'xemacs)
  4111. (let ((gl (make-glyph text)))
  4112. (and face (set-glyph-face gl face))
  4113. (set-extent-property ovl 'invisible t)
  4114. (set-extent-property ovl 'end-glyph gl))
  4115. (overlay-put ovl 'display text)
  4116. (if face (overlay-put ovl 'face face))
  4117. (if evap (overlay-put ovl 'evaporate t))))
  4118. (defun org-overlay-before-string (ovl text &optional face evap)
  4119. "Make overlay OVL display TEXT with face FACE."
  4120. (if (featurep 'xemacs)
  4121. (let ((gl (make-glyph text)))
  4122. (and face (set-glyph-face gl face))
  4123. (set-extent-property ovl 'begin-glyph gl))
  4124. (if face (org-add-props text nil 'face face))
  4125. (overlay-put ovl 'before-string text)
  4126. (if evap (overlay-put ovl 'evaporate t))))
  4127. (defun org-overlay-get (ovl prop)
  4128. (if (featurep 'xemacs)
  4129. (extent-property ovl prop)
  4130. (overlay-get ovl prop)))
  4131. (defun org-overlays-at (pos)
  4132. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4133. (defun org-overlays-in (&optional start end)
  4134. (if (featurep 'xemacs)
  4135. (extent-list nil start end)
  4136. (overlays-in start end)))
  4137. (defun org-overlay-start (o)
  4138. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4139. (defun org-overlay-end (o)
  4140. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4141. (defun org-find-overlays (prop &optional pos delete)
  4142. "Find all overlays specifying PROP at POS or point.
  4143. If DELETE is non-nil, delete all those overlays."
  4144. (let ((overlays (org-overlays-at (or pos (point))))
  4145. ov found)
  4146. (while (setq ov (pop overlays))
  4147. (if (org-overlay-get ov prop)
  4148. (if delete (org-delete-overlay ov) (push ov found))))
  4149. found))
  4150. ;; Region compatibility
  4151. (defun org-add-hook (hook function &optional append local)
  4152. "Add-hook, compatible with both Emacsen."
  4153. (if (and local (featurep 'xemacs))
  4154. (add-local-hook hook function append)
  4155. (add-hook hook function append local)))
  4156. (defvar org-ignore-region nil
  4157. "To temporarily disable the active region.")
  4158. (defun org-region-active-p ()
  4159. "Is `transient-mark-mode' on and the region active?
  4160. Works on both Emacs and XEmacs."
  4161. (if org-ignore-region
  4162. nil
  4163. (if (featurep 'xemacs)
  4164. (and zmacs-regions (region-active-p))
  4165. (if (fboundp 'use-region-p)
  4166. (use-region-p)
  4167. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4168. ;; Invisibility compatibility
  4169. (defun org-add-to-invisibility-spec (arg)
  4170. "Add elements to `buffer-invisibility-spec'.
  4171. See documentation for `buffer-invisibility-spec' for the kind of elements
  4172. that can be added."
  4173. (cond
  4174. ((fboundp 'add-to-invisibility-spec)
  4175. (add-to-invisibility-spec arg))
  4176. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4177. (setq buffer-invisibility-spec (list arg)))
  4178. (t
  4179. (setq buffer-invisibility-spec
  4180. (cons arg buffer-invisibility-spec)))))
  4181. (defun org-remove-from-invisibility-spec (arg)
  4182. "Remove elements from `buffer-invisibility-spec'."
  4183. (if (fboundp 'remove-from-invisibility-spec)
  4184. (remove-from-invisibility-spec arg)
  4185. (if (consp buffer-invisibility-spec)
  4186. (setq buffer-invisibility-spec
  4187. (delete arg buffer-invisibility-spec)))))
  4188. (defun org-in-invisibility-spec-p (arg)
  4189. "Is ARG a member of `buffer-invisibility-spec'?"
  4190. (if (consp buffer-invisibility-spec)
  4191. (member arg buffer-invisibility-spec)
  4192. nil))
  4193. ;;;; Define the Org-mode
  4194. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4195. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or ugrade to newer allout, for example by switching to Emacs 22."))
  4196. ;; We use a before-change function to check if a table might need
  4197. ;; an update.
  4198. (defvar org-table-may-need-update t
  4199. "Indicates that a table might need an update.
  4200. This variable is set by `org-before-change-function'.
  4201. `org-table-align' sets it back to nil.")
  4202. (defvar org-mode-map)
  4203. (defvar org-mode-hook nil)
  4204. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4205. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4206. (defvar org-table-buffer-is-an nil)
  4207. (defconst org-outline-regexp "\\*+ ")
  4208. ;;;###autoload
  4209. (define-derived-mode org-mode outline-mode "Org"
  4210. "Outline-based notes management and organizer, alias
  4211. \"Carsten's outline-mode for keeping track of everything.\"
  4212. Org-mode develops organizational tasks around a NOTES file which
  4213. contains information about projects as plain text. Org-mode is
  4214. implemented on top of outline-mode, which is ideal to keep the content
  4215. of large files well structured. It supports ToDo items, deadlines and
  4216. time stamps, which magically appear in the diary listing of the Emacs
  4217. calendar. Tables are easily created with a built-in table editor.
  4218. Plain text URL-like links connect to websites, emails (VM), Usenet
  4219. messages (Gnus), BBDB entries, and any files related to the project.
  4220. For printing and sharing of notes, an Org-mode file (or a part of it)
  4221. can be exported as a structured ASCII or HTML file.
  4222. The following commands are available:
  4223. \\{org-mode-map}"
  4224. ;; Get rid of Outline menus, they are not needed
  4225. ;; Need to do this here because define-derived-mode sets up
  4226. ;; the keymap so late. Still, it is a waste to call this each time
  4227. ;; we switch another buffer into org-mode.
  4228. (if (featurep 'xemacs)
  4229. (when (boundp 'outline-mode-menu-heading)
  4230. ;; Assume this is Greg's port, it used easymenu
  4231. (easy-menu-remove outline-mode-menu-heading)
  4232. (easy-menu-remove outline-mode-menu-show)
  4233. (easy-menu-remove outline-mode-menu-hide))
  4234. (define-key org-mode-map [menu-bar headings] 'undefined)
  4235. (define-key org-mode-map [menu-bar hide] 'undefined)
  4236. (define-key org-mode-map [menu-bar show] 'undefined))
  4237. (easy-menu-add org-org-menu)
  4238. (easy-menu-add org-tbl-menu)
  4239. (org-install-agenda-files-menu)
  4240. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4241. (org-add-to-invisibility-spec '(org-cwidth))
  4242. (when (featurep 'xemacs)
  4243. (org-set-local 'line-move-ignore-invisible t))
  4244. (org-set-local 'outline-regexp org-outline-regexp)
  4245. (org-set-local 'outline-level 'org-outline-level)
  4246. (when (and org-ellipsis
  4247. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4248. (fboundp 'make-glyph-code))
  4249. (unless org-display-table
  4250. (setq org-display-table (make-display-table)))
  4251. (set-display-table-slot
  4252. org-display-table 4
  4253. (vconcat (mapcar
  4254. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4255. org-ellipsis)))
  4256. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4257. (setq buffer-display-table org-display-table))
  4258. (org-set-regexps-and-options)
  4259. ;; Calc embedded
  4260. (org-set-local 'calc-embedded-open-mode "# ")
  4261. (modify-syntax-entry ?# "<")
  4262. (modify-syntax-entry ?@ "w")
  4263. (if org-startup-truncated (setq truncate-lines t))
  4264. (org-set-local 'font-lock-unfontify-region-function
  4265. 'org-unfontify-region)
  4266. ;; Activate before-change-function
  4267. (org-set-local 'org-table-may-need-update t)
  4268. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4269. 'local)
  4270. ;; Check for running clock before killing a buffer
  4271. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4272. ;; Paragraphs and auto-filling
  4273. (org-set-autofill-regexps)
  4274. (setq indent-line-function 'org-indent-line-function)
  4275. (org-update-radio-target-regexp)
  4276. ;; Comment characters
  4277. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4278. (org-set-local 'comment-padding " ")
  4279. ;; Imenu
  4280. (org-set-local 'imenu-create-index-function
  4281. 'org-imenu-get-tree)
  4282. ;; Make isearch reveal context
  4283. (if (or (featurep 'xemacs)
  4284. (not (boundp 'outline-isearch-open-invisible-function)))
  4285. ;; Emacs 21 and XEmacs make use of the hook
  4286. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4287. ;; Emacs 22 deals with this through a special variable
  4288. (org-set-local 'outline-isearch-open-invisible-function
  4289. (lambda (&rest ignore) (org-show-context 'isearch))))
  4290. ;; If empty file that did not turn on org-mode automatically, make it to.
  4291. (if (and org-insert-mode-line-in-empty-file
  4292. (interactive-p)
  4293. (= (point-min) (point-max)))
  4294. (insert "# -*- mode: org -*-\n\n"))
  4295. (unless org-inhibit-startup
  4296. (when org-startup-align-all-tables
  4297. (let ((bmp (buffer-modified-p)))
  4298. (org-table-map-tables 'org-table-align)
  4299. (set-buffer-modified-p bmp)))
  4300. (org-cycle-hide-drawers 'all)
  4301. (cond
  4302. ((eq org-startup-folded t)
  4303. (org-cycle '(4)))
  4304. ((eq org-startup-folded 'content)
  4305. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4306. (org-cycle '(4)) (org-cycle '(4)))))))
  4307. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4308. (defsubst org-call-with-arg (command arg)
  4309. "Call COMMAND interactively, but pretend prefix are was ARG."
  4310. (let ((current-prefix-arg arg)) (call-interactively command)))
  4311. (defsubst org-current-line (&optional pos)
  4312. (save-excursion
  4313. (and pos (goto-char pos))
  4314. ;; works also in narrowed buffer, because we start at 1, not point-min
  4315. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4316. (defun org-current-time ()
  4317. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4318. (if (> org-time-stamp-rounding-minutes 0)
  4319. (let ((r org-time-stamp-rounding-minutes)
  4320. (time (decode-time)))
  4321. (apply 'encode-time
  4322. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4323. (nthcdr 2 time))))
  4324. (current-time)))
  4325. (defun org-add-props (string plist &rest props)
  4326. "Add text properties to entire string, from beginning to end.
  4327. PLIST may be a list of properties, PROPS are individual properties and values
  4328. that will be added to PLIST. Returns the string that was modified."
  4329. (add-text-properties
  4330. 0 (length string) (if props (append plist props) plist) string)
  4331. string)
  4332. (put 'org-add-props 'lisp-indent-function 2)
  4333. ;;;; Font-Lock stuff, including the activators
  4334. (defvar org-mouse-map (make-sparse-keymap))
  4335. (org-defkey org-mouse-map
  4336. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4337. (org-defkey org-mouse-map
  4338. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4339. (when org-mouse-1-follows-link
  4340. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4341. (when org-tab-follows-link
  4342. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4343. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4344. (when org-return-follows-link
  4345. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4346. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4347. (require 'font-lock)
  4348. (defconst org-non-link-chars "]\t\n\r<>")
  4349. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4350. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  4351. (defvar org-link-re-with-space nil
  4352. "Matches a link with spaces, optional angular brackets around it.")
  4353. (defvar org-link-re-with-space2 nil
  4354. "Matches a link with spaces, optional angular brackets around it.")
  4355. (defvar org-angle-link-re nil
  4356. "Matches link with angular brackets, spaces are allowed.")
  4357. (defvar org-plain-link-re nil
  4358. "Matches plain link, without spaces.")
  4359. (defvar org-bracket-link-regexp nil
  4360. "Matches a link in double brackets.")
  4361. (defvar org-bracket-link-analytic-regexp nil
  4362. "Regular expression used to analyze links.
  4363. Here is what the match groups contain after a match:
  4364. 1: http:
  4365. 2: http
  4366. 3: path
  4367. 4: [desc]
  4368. 5: desc")
  4369. (defvar org-any-link-re nil
  4370. "Regular expression matching any link.")
  4371. (defun org-make-link-regexps ()
  4372. "Update the link regular expressions.
  4373. This should be called after the variable `org-link-types' has changed."
  4374. (setq org-link-re-with-space
  4375. (concat
  4376. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4377. "\\([^" org-non-link-chars " ]"
  4378. "[^" org-non-link-chars "]*"
  4379. "[^" org-non-link-chars " ]\\)>?")
  4380. org-link-re-with-space2
  4381. (concat
  4382. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4383. "\\([^" org-non-link-chars " ]"
  4384. "[^]\t\n\r]*"
  4385. "[^" org-non-link-chars " ]\\)>?")
  4386. org-angle-link-re
  4387. (concat
  4388. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4389. "\\([^" org-non-link-chars " ]"
  4390. "[^" org-non-link-chars "]*"
  4391. "\\)>")
  4392. org-plain-link-re
  4393. (concat
  4394. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4395. "\\([^]\t\n\r<>,;() ]+\\)")
  4396. org-bracket-link-regexp
  4397. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4398. org-bracket-link-analytic-regexp
  4399. (concat
  4400. "\\[\\["
  4401. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4402. "\\([^]]+\\)"
  4403. "\\]"
  4404. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4405. "\\]")
  4406. org-any-link-re
  4407. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4408. org-angle-link-re "\\)\\|\\("
  4409. org-plain-link-re "\\)")))
  4410. (org-make-link-regexps)
  4411. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4412. "Regular expression for fast time stamp matching.")
  4413. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4414. "Regular expression for fast time stamp matching.")
  4415. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4416. "Regular expression matching time strings for analysis.
  4417. This one does not require the space after the date.")
  4418. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4419. "Regular expression matching time strings for analysis.")
  4420. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4421. "Regular expression matching time stamps, with groups.")
  4422. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4423. "Regular expression matching time stamps (also [..]), with groups.")
  4424. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4425. "Regular expression matching a time stamp range.")
  4426. (defconst org-tr-regexp-both
  4427. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4428. "Regular expression matching a time stamp range.")
  4429. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4430. org-ts-regexp "\\)?")
  4431. "Regular expression matching a time stamp or time stamp range.")
  4432. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4433. org-ts-regexp-both "\\)?")
  4434. "Regular expression matching a time stamp or time stamp range.
  4435. The time stamps may be either active or inactive.")
  4436. (defvar org-emph-face nil)
  4437. (defun org-do-emphasis-faces (limit)
  4438. "Run through the buffer and add overlays to links."
  4439. (let (rtn)
  4440. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4441. (if (not (= (char-after (match-beginning 3))
  4442. (char-after (match-beginning 4))))
  4443. (progn
  4444. (setq rtn t)
  4445. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4446. 'face
  4447. (nth 1 (assoc (match-string 3)
  4448. org-emphasis-alist)))
  4449. (add-text-properties (match-beginning 2) (match-end 2)
  4450. '(font-lock-multiline t))
  4451. (when org-hide-emphasis-markers
  4452. (add-text-properties (match-end 4) (match-beginning 5)
  4453. '(invisible org-link))
  4454. (add-text-properties (match-beginning 3) (match-end 3)
  4455. '(invisible org-link)))))
  4456. (backward-char 1))
  4457. rtn))
  4458. (defun org-emphasize (&optional char)
  4459. "Insert or change an emphasis, i.e. a font like bold or italic.
  4460. If there is an active region, change that region to a new emphasis.
  4461. If there is no region, just insert the marker characters and position
  4462. the cursor between them.
  4463. CHAR should be either the marker character, or the first character of the
  4464. HTML tag associated with that emphasis. If CHAR is a space, the means
  4465. to remove the emphasis of the selected region.
  4466. If char is not given (for example in an interactive call) it
  4467. will be prompted for."
  4468. (interactive)
  4469. (let ((eal org-emphasis-alist) e det
  4470. (erc org-emphasis-regexp-components)
  4471. (prompt "")
  4472. (string "") beg end move tag c s)
  4473. (if (org-region-active-p)
  4474. (setq beg (region-beginning) end (region-end)
  4475. string (buffer-substring beg end))
  4476. (setq move t))
  4477. (while (setq e (pop eal))
  4478. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4479. c (aref tag 0))
  4480. (push (cons c (string-to-char (car e))) det)
  4481. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4482. (substring tag 1)))))
  4483. (unless char
  4484. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4485. (setq char (read-char-exclusive)))
  4486. (setq char (or (cdr (assoc char det)) char))
  4487. (if (equal char ?\ )
  4488. (setq s "" move nil)
  4489. (unless (assoc (char-to-string char) org-emphasis-alist)
  4490. (error "No such emphasis marker: \"%c\"" char))
  4491. (setq s (char-to-string char)))
  4492. (while (and (> (length string) 1)
  4493. (equal (substring string 0 1) (substring string -1))
  4494. (assoc (substring string 0 1) org-emphasis-alist))
  4495. (setq string (substring string 1 -1)))
  4496. (setq string (concat s string s))
  4497. (if beg (delete-region beg end))
  4498. (unless (or (bolp)
  4499. (string-match (concat "[" (nth 0 erc) "\n]")
  4500. (char-to-string (char-before (point)))))
  4501. (insert " "))
  4502. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4503. (char-to-string (char-after (point))))
  4504. (insert " ") (backward-char 1))
  4505. (insert string)
  4506. (and move (backward-char 1))))
  4507. (defconst org-nonsticky-props
  4508. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4509. (defun org-activate-plain-links (limit)
  4510. "Run through the buffer and add overlays to links."
  4511. (catch 'exit
  4512. (let (f)
  4513. (while (re-search-forward org-plain-link-re limit t)
  4514. (setq f (get-text-property (match-beginning 0) 'face))
  4515. (if (or (eq f 'org-tag)
  4516. (and (listp f) (memq 'org-tag f)))
  4517. nil
  4518. (add-text-properties (match-beginning 0) (match-end 0)
  4519. (list 'mouse-face 'highlight
  4520. 'rear-nonsticky org-nonsticky-props
  4521. 'keymap org-mouse-map
  4522. ))
  4523. (throw 'exit t))))))
  4524. (defun org-activate-code (limit)
  4525. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4526. (unless (get-text-property (match-beginning 1) 'face)
  4527. (remove-text-properties (match-beginning 0) (match-end 0)
  4528. '(display t invisible t intangible t))
  4529. t)))
  4530. (defun org-activate-angle-links (limit)
  4531. "Run through the buffer and add overlays to links."
  4532. (if (re-search-forward org-angle-link-re limit t)
  4533. (progn
  4534. (add-text-properties (match-beginning 0) (match-end 0)
  4535. (list 'mouse-face 'highlight
  4536. 'rear-nonsticky org-nonsticky-props
  4537. 'keymap org-mouse-map
  4538. ))
  4539. t)))
  4540. (defmacro org-maybe-intangible (props)
  4541. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4542. In emacs 21, invisible text is not avoided by the command loop, so the
  4543. intangible property is needed to make sure point skips this text.
  4544. In Emacs 22, this is not necessary. The intangible text property has
  4545. led to problems with flyspell. These problems are fixed in flyspell.el,
  4546. but we still avoid setting the property in Emacs 22 and later.
  4547. We use a macro so that the test can happen at compilation time."
  4548. (if (< emacs-major-version 22)
  4549. `(append '(intangible t) ,props)
  4550. props))
  4551. (defun org-activate-bracket-links (limit)
  4552. "Run through the buffer and add overlays to bracketed links."
  4553. (if (re-search-forward org-bracket-link-regexp limit t)
  4554. (let* ((help (concat "LINK: "
  4555. (org-match-string-no-properties 1)))
  4556. ;; FIXME: above we should remove the escapes.
  4557. ;; but that requires another match, protecting match data,
  4558. ;; a lot of overhead for font-lock.
  4559. (ip (org-maybe-intangible
  4560. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4561. 'keymap org-mouse-map 'mouse-face 'highlight
  4562. 'font-lock-multiline t 'help-echo help)))
  4563. (vp (list 'rear-nonsticky org-nonsticky-props
  4564. 'keymap org-mouse-map 'mouse-face 'highlight
  4565. ' font-lock-multiline t 'help-echo help)))
  4566. ;; We need to remove the invisible property here. Table narrowing
  4567. ;; may have made some of this invisible.
  4568. (remove-text-properties (match-beginning 0) (match-end 0)
  4569. '(invisible nil))
  4570. (if (match-end 3)
  4571. (progn
  4572. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4573. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4574. (add-text-properties (match-end 3) (match-end 0) ip))
  4575. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4576. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4577. (add-text-properties (match-end 1) (match-end 0) ip))
  4578. t)))
  4579. (defun org-activate-dates (limit)
  4580. "Run through the buffer and add overlays to dates."
  4581. (if (re-search-forward org-tsr-regexp-both limit t)
  4582. (progn
  4583. (add-text-properties (match-beginning 0) (match-end 0)
  4584. (list 'mouse-face 'highlight
  4585. 'rear-nonsticky org-nonsticky-props
  4586. 'keymap org-mouse-map))
  4587. (when org-display-custom-times
  4588. (if (match-end 3)
  4589. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4590. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4591. t)))
  4592. (defvar org-target-link-regexp nil
  4593. "Regular expression matching radio targets in plain text.")
  4594. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4595. "Regular expression matching a link target.")
  4596. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4597. "Regular expression matching a radio target.")
  4598. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4599. "Regular expression matching any target.")
  4600. (defun org-activate-target-links (limit)
  4601. "Run through the buffer and add overlays to target matches."
  4602. (when org-target-link-regexp
  4603. (let ((case-fold-search t))
  4604. (if (re-search-forward org-target-link-regexp limit t)
  4605. (progn
  4606. (add-text-properties (match-beginning 0) (match-end 0)
  4607. (list 'mouse-face 'highlight
  4608. 'rear-nonsticky org-nonsticky-props
  4609. 'keymap org-mouse-map
  4610. 'help-echo "Radio target link"
  4611. 'org-linked-text t))
  4612. t)))))
  4613. (defun org-update-radio-target-regexp ()
  4614. "Find all radio targets in this file and update the regular expression."
  4615. (interactive)
  4616. (when (memq 'radio org-activate-links)
  4617. (setq org-target-link-regexp
  4618. (org-make-target-link-regexp (org-all-targets 'radio)))
  4619. (org-restart-font-lock)))
  4620. (defun org-hide-wide-columns (limit)
  4621. (let (s e)
  4622. (setq s (text-property-any (point) (or limit (point-max))
  4623. 'org-cwidth t))
  4624. (when s
  4625. (setq e (next-single-property-change s 'org-cwidth))
  4626. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4627. (goto-char e)
  4628. t)))
  4629. (defvar org-latex-and-specials-regexp nil
  4630. "Regular expression for highlighting export special stuff.")
  4631. (defvar org-match-substring-regexp)
  4632. (defvar org-match-substring-with-braces-regexp)
  4633. (defvar org-export-html-special-string-regexps)
  4634. (defun org-compute-latex-and-specials-regexp ()
  4635. "Compute regular expression for stuff treated specially by exporters."
  4636. (if (not org-highlight-latex-fragments-and-specials)
  4637. (org-set-local 'org-latex-and-specials-regexp nil)
  4638. (let*
  4639. ((matchers (plist-get org-format-latex-options :matchers))
  4640. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4641. org-latex-regexps)))
  4642. (options (org-combine-plists (org-default-export-plist)
  4643. (org-infile-export-plist)))
  4644. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4645. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4646. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4647. (org-export-html-expand (plist-get options :expand-quoted-html))
  4648. (org-export-with-special-strings (plist-get options :special-strings))
  4649. (re-sub
  4650. (cond
  4651. ((equal org-export-with-sub-superscripts '{})
  4652. (list org-match-substring-with-braces-regexp))
  4653. (org-export-with-sub-superscripts
  4654. (list org-match-substring-regexp))
  4655. (t nil)))
  4656. (re-latex
  4657. (if org-export-with-LaTeX-fragments
  4658. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4659. (re-macros
  4660. (if org-export-with-TeX-macros
  4661. (list (concat "\\\\"
  4662. (regexp-opt
  4663. (append (mapcar 'car org-html-entities)
  4664. (if (boundp 'org-latex-entities)
  4665. org-latex-entities nil))
  4666. 'words))) ; FIXME
  4667. ))
  4668. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4669. (re-special (if org-export-with-special-strings
  4670. (mapcar (lambda (x) (car x))
  4671. org-export-html-special-string-regexps)))
  4672. (re-rest
  4673. (delq nil
  4674. (list
  4675. (if org-export-html-expand "@<[^>\n]+>")
  4676. ))))
  4677. (org-set-local
  4678. 'org-latex-and-specials-regexp
  4679. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4680. re-rest) "\\|")))))
  4681. (defface org-latex-and-export-specials
  4682. (let ((font (cond ((assq :inherit custom-face-attributes)
  4683. '(:inherit underline))
  4684. (t '(:underline t)))))
  4685. `((((class grayscale) (background light))
  4686. (:foreground "DimGray" ,@font))
  4687. (((class grayscale) (background dark))
  4688. (:foreground "LightGray" ,@font))
  4689. (((class color) (background light))
  4690. (:foreground "SaddleBrown"))
  4691. (((class color) (background dark))
  4692. (:foreground "burlywood"))
  4693. (t (,@font))))
  4694. "Face used to highlight math latex and other special exporter stuff."
  4695. :group 'org-faces)
  4696. (defun org-do-latex-and-special-faces (limit)
  4697. "Run through the buffer and add overlays to links."
  4698. (when org-latex-and-specials-regexp
  4699. (let (rtn d)
  4700. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4701. limit t))
  4702. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4703. 'face))
  4704. '(org-code org-verbatim underline)))
  4705. (progn
  4706. (setq rtn t
  4707. d (cond ((member (char-after (1+ (match-beginning 0)))
  4708. '(?_ ?^)) 1)
  4709. (t 0)))
  4710. (font-lock-prepend-text-property
  4711. (+ d (match-beginning 0)) (match-end 0)
  4712. 'face 'org-latex-and-export-specials)
  4713. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4714. '(font-lock-multiline t)))))
  4715. rtn)))
  4716. (defun org-restart-font-lock ()
  4717. "Restart font-lock-mode, to force refontification."
  4718. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4719. (font-lock-mode -1)
  4720. (font-lock-mode 1)))
  4721. (defun org-all-targets (&optional radio)
  4722. "Return a list of all targets in this file.
  4723. With optional argument RADIO, only find radio targets."
  4724. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4725. rtn)
  4726. (save-excursion
  4727. (goto-char (point-min))
  4728. (while (re-search-forward re nil t)
  4729. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4730. rtn)))
  4731. (defun org-make-target-link-regexp (targets)
  4732. "Make regular expression matching all strings in TARGETS.
  4733. The regular expression finds the targets also if there is a line break
  4734. between words."
  4735. (and targets
  4736. (concat
  4737. "\\<\\("
  4738. (mapconcat
  4739. (lambda (x)
  4740. (while (string-match " +" x)
  4741. (setq x (replace-match "\\s-+" t t x)))
  4742. x)
  4743. targets
  4744. "\\|")
  4745. "\\)\\>")))
  4746. (defun org-activate-tags (limit)
  4747. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4748. (progn
  4749. (add-text-properties (match-beginning 1) (match-end 1)
  4750. (list 'mouse-face 'highlight
  4751. 'rear-nonsticky org-nonsticky-props
  4752. 'keymap org-mouse-map))
  4753. t)))
  4754. (defun org-outline-level ()
  4755. (save-excursion
  4756. (looking-at outline-regexp)
  4757. (if (match-beginning 1)
  4758. (+ (org-get-string-indentation (match-string 1)) 1000)
  4759. (1- (- (match-end 0) (match-beginning 0))))))
  4760. (defvar org-font-lock-keywords nil)
  4761. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4762. "Regular expression matching a property line.")
  4763. (defun org-set-font-lock-defaults ()
  4764. (let* ((em org-fontify-emphasized-text)
  4765. (lk org-activate-links)
  4766. (org-font-lock-extra-keywords
  4767. (list
  4768. ;; Headlines
  4769. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4770. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4771. ;; Table lines
  4772. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4773. (1 'org-table t))
  4774. ;; Table internals
  4775. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4776. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4777. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4778. ;; Drawers
  4779. (list org-drawer-regexp '(0 'org-special-keyword t))
  4780. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4781. ;; Properties
  4782. (list org-property-re
  4783. '(1 'org-special-keyword t)
  4784. '(3 'org-property-value t))
  4785. (if org-format-transports-properties-p
  4786. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4787. ;; Links
  4788. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4789. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4790. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4791. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4792. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4793. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4794. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4795. '(org-hide-wide-columns (0 nil append))
  4796. ;; TODO lines
  4797. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4798. '(1 (org-get-todo-face 1) t))
  4799. ;; DONE
  4800. (if org-fontify-done-headline
  4801. (list (concat "^[*]+ +\\<\\("
  4802. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4803. "\\)\\(.*\\)")
  4804. '(2 'org-headline-done t))
  4805. nil)
  4806. ;; Priorities
  4807. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4808. ;; Special keywords
  4809. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4810. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4811. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4812. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4813. ;; Emphasis
  4814. (if em
  4815. (if (featurep 'xemacs)
  4816. '(org-do-emphasis-faces (0 nil append))
  4817. '(org-do-emphasis-faces)))
  4818. ;; Checkboxes
  4819. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4820. 2 'bold prepend)
  4821. (if org-provide-checkbox-statistics
  4822. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4823. (0 (org-get-checkbox-statistics-face) t)))
  4824. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4825. '(1 'org-archived prepend))
  4826. ;; Specials
  4827. '(org-do-latex-and-special-faces)
  4828. ;; Code
  4829. '(org-activate-code (1 'org-code t))
  4830. ;; COMMENT
  4831. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4832. "\\|" org-quote-string "\\)\\>")
  4833. '(1 'org-special-keyword t))
  4834. '("^#.*" (0 'font-lock-comment-face t))
  4835. )))
  4836. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4837. ;; Now set the full font-lock-keywords
  4838. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4839. (org-set-local 'font-lock-defaults
  4840. '(org-font-lock-keywords t nil nil backward-paragraph))
  4841. (kill-local-variable 'font-lock-keywords) nil))
  4842. (defvar org-m nil)
  4843. (defvar org-l nil)
  4844. (defvar org-f nil)
  4845. (defun org-get-level-face (n)
  4846. "Get the right face for match N in font-lock matching of healdines."
  4847. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4848. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4849. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4850. (cond
  4851. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4852. ((eq n 2) org-f)
  4853. (t (if org-level-color-stars-only nil org-f))))
  4854. (defun org-get-todo-face (kwd)
  4855. "Get the right face for a TODO keyword KWD.
  4856. If KWD is a number, get the corresponding match group."
  4857. (if (numberp kwd) (setq kwd (match-string kwd)))
  4858. (or (cdr (assoc kwd org-todo-keyword-faces))
  4859. (and (member kwd org-done-keywords) 'org-done)
  4860. 'org-todo))
  4861. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4862. "Remove fontification and activation overlays from links."
  4863. (font-lock-default-unfontify-region beg end)
  4864. (let* ((buffer-undo-list t)
  4865. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4866. (inhibit-modification-hooks t)
  4867. deactivate-mark buffer-file-name buffer-file-truename)
  4868. (remove-text-properties beg end
  4869. '(mouse-face t keymap t org-linked-text t
  4870. invisible t intangible t))))
  4871. ;;;; Visibility cycling, including org-goto and indirect buffer
  4872. ;;; Cycling
  4873. (defvar org-cycle-global-status nil)
  4874. (make-variable-buffer-local 'org-cycle-global-status)
  4875. (defvar org-cycle-subtree-status nil)
  4876. (make-variable-buffer-local 'org-cycle-subtree-status)
  4877. ;;;###autoload
  4878. (defun org-cycle (&optional arg)
  4879. "Visibility cycling for Org-mode.
  4880. - When this function is called with a prefix argument, rotate the entire
  4881. buffer through 3 states (global cycling)
  4882. 1. OVERVIEW: Show only top-level headlines.
  4883. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4884. 3. SHOW ALL: Show everything.
  4885. - When point is at the beginning of a headline, rotate the subtree started
  4886. by this line through 3 different states (local cycling)
  4887. 1. FOLDED: Only the main headline is shown.
  4888. 2. CHILDREN: The main headline and the direct children are shown.
  4889. From this state, you can move to one of the children
  4890. and zoom in further.
  4891. 3. SUBTREE: Show the entire subtree, including body text.
  4892. - When there is a numeric prefix, go up to a heading with level ARG, do
  4893. a `show-subtree' and return to the previous cursor position. If ARG
  4894. is negative, go up that many levels.
  4895. - When point is not at the beginning of a headline, execute
  4896. `indent-relative', like TAB normally does. See the option
  4897. `org-cycle-emulate-tab' for details.
  4898. - Special case: if point is at the beginning of the buffer and there is
  4899. no headline in line 1, this function will act as if called with prefix arg.
  4900. But only if also the variable `org-cycle-global-at-bob' is t."
  4901. (interactive "P")
  4902. (let* ((outline-regexp
  4903. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4904. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4905. outline-regexp))
  4906. (bob-special (and org-cycle-global-at-bob (bobp)
  4907. (not (looking-at outline-regexp))))
  4908. (org-cycle-hook
  4909. (if bob-special
  4910. (delq 'org-optimize-window-after-visibility-change
  4911. (copy-sequence org-cycle-hook))
  4912. org-cycle-hook))
  4913. (pos (point)))
  4914. (if (or bob-special (equal arg '(4)))
  4915. ;; special case: use global cycling
  4916. (setq arg t))
  4917. (cond
  4918. ((org-at-table-p 'any)
  4919. ;; Enter the table or move to the next field in the table
  4920. (or (org-table-recognize-table.el)
  4921. (progn
  4922. (if arg (org-table-edit-field t)
  4923. (org-table-justify-field-maybe)
  4924. (call-interactively 'org-table-next-field)))))
  4925. ((eq arg t) ;; Global cycling
  4926. (cond
  4927. ((and (eq last-command this-command)
  4928. (eq org-cycle-global-status 'overview))
  4929. ;; We just created the overview - now do table of contents
  4930. ;; This can be slow in very large buffers, so indicate action
  4931. (message "CONTENTS...")
  4932. (org-content)
  4933. (message "CONTENTS...done")
  4934. (setq org-cycle-global-status 'contents)
  4935. (run-hook-with-args 'org-cycle-hook 'contents))
  4936. ((and (eq last-command this-command)
  4937. (eq org-cycle-global-status 'contents))
  4938. ;; We just showed the table of contents - now show everything
  4939. (show-all)
  4940. (message "SHOW ALL")
  4941. (setq org-cycle-global-status 'all)
  4942. (run-hook-with-args 'org-cycle-hook 'all))
  4943. (t
  4944. ;; Default action: go to overview
  4945. (org-overview)
  4946. (message "OVERVIEW")
  4947. (setq org-cycle-global-status 'overview)
  4948. (run-hook-with-args 'org-cycle-hook 'overview))))
  4949. ((and org-drawers org-drawer-regexp
  4950. (save-excursion
  4951. (beginning-of-line 1)
  4952. (looking-at org-drawer-regexp)))
  4953. ;; Toggle block visibility
  4954. (org-flag-drawer
  4955. (not (get-char-property (match-end 0) 'invisible))))
  4956. ((integerp arg)
  4957. ;; Show-subtree, ARG levels up from here.
  4958. (save-excursion
  4959. (org-back-to-heading)
  4960. (outline-up-heading (if (< arg 0) (- arg)
  4961. (- (funcall outline-level) arg)))
  4962. (org-show-subtree)))
  4963. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4964. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4965. ;; At a heading: rotate between three different views
  4966. (org-back-to-heading)
  4967. (let ((goal-column 0) eoh eol eos)
  4968. ;; First, some boundaries
  4969. (save-excursion
  4970. (org-back-to-heading)
  4971. (save-excursion
  4972. (beginning-of-line 2)
  4973. (while (and (not (eobp)) ;; this is like `next-line'
  4974. (get-char-property (1- (point)) 'invisible))
  4975. (beginning-of-line 2)) (setq eol (point)))
  4976. (outline-end-of-heading) (setq eoh (point))
  4977. (org-end-of-subtree t)
  4978. (unless (eobp)
  4979. (skip-chars-forward " \t\n")
  4980. (beginning-of-line 1) ; in case this is an item
  4981. )
  4982. (setq eos (1- (point))))
  4983. ;; Find out what to do next and set `this-command'
  4984. (cond
  4985. ((= eos eoh)
  4986. ;; Nothing is hidden behind this heading
  4987. (message "EMPTY ENTRY")
  4988. (setq org-cycle-subtree-status nil)
  4989. (save-excursion
  4990. (goto-char eos)
  4991. (outline-next-heading)
  4992. (if (org-invisible-p) (org-flag-heading nil))))
  4993. ((or (>= eol eos)
  4994. (not (string-match "\\S-" (buffer-substring eol eos))))
  4995. ;; Entire subtree is hidden in one line: open it
  4996. (org-show-entry)
  4997. (show-children)
  4998. (message "CHILDREN")
  4999. (save-excursion
  5000. (goto-char eos)
  5001. (outline-next-heading)
  5002. (if (org-invisible-p) (org-flag-heading nil)))
  5003. (setq org-cycle-subtree-status 'children)
  5004. (run-hook-with-args 'org-cycle-hook 'children))
  5005. ((and (eq last-command this-command)
  5006. (eq org-cycle-subtree-status 'children))
  5007. ;; We just showed the children, now show everything.
  5008. (org-show-subtree)
  5009. (message "SUBTREE")
  5010. (setq org-cycle-subtree-status 'subtree)
  5011. (run-hook-with-args 'org-cycle-hook 'subtree))
  5012. (t
  5013. ;; Default action: hide the subtree.
  5014. (hide-subtree)
  5015. (message "FOLDED")
  5016. (setq org-cycle-subtree-status 'folded)
  5017. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5018. ;; TAB emulation
  5019. (buffer-read-only (org-back-to-heading))
  5020. ((org-try-cdlatex-tab))
  5021. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5022. (or (not (bolp))
  5023. (not (looking-at outline-regexp))))
  5024. (call-interactively (global-key-binding "\t")))
  5025. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5026. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5027. (or (and (eq org-cycle-emulate-tab 'white)
  5028. (= (match-end 0) (point-at-eol)))
  5029. (and (eq org-cycle-emulate-tab 'whitestart)
  5030. (>= (match-end 0) pos))))
  5031. t
  5032. (eq org-cycle-emulate-tab t))
  5033. ; (if (and (looking-at "[ \n\r\t]")
  5034. ; (string-match "^[ \t]*$" (buffer-substring
  5035. ; (point-at-bol) (point))))
  5036. ; (progn
  5037. ; (beginning-of-line 1)
  5038. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5039. (call-interactively (global-key-binding "\t")))
  5040. (t (save-excursion
  5041. (org-back-to-heading)
  5042. (org-cycle))))))
  5043. ;;;###autoload
  5044. (defun org-global-cycle (&optional arg)
  5045. "Cycle the global visibility. For details see `org-cycle'."
  5046. (interactive "P")
  5047. (let ((org-cycle-include-plain-lists
  5048. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5049. (if (integerp arg)
  5050. (progn
  5051. (show-all)
  5052. (hide-sublevels arg)
  5053. (setq org-cycle-global-status 'contents))
  5054. (org-cycle '(4)))))
  5055. (defun org-overview ()
  5056. "Switch to overview mode, shoing only top-level headlines.
  5057. Really, this shows all headlines with level equal or greater than the level
  5058. of the first headline in the buffer. This is important, because if the
  5059. first headline is not level one, then (hide-sublevels 1) gives confusing
  5060. results."
  5061. (interactive)
  5062. (let ((level (save-excursion
  5063. (goto-char (point-min))
  5064. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5065. (progn
  5066. (goto-char (match-beginning 0))
  5067. (funcall outline-level))))))
  5068. (and level (hide-sublevels level))))
  5069. (defun org-content (&optional arg)
  5070. "Show all headlines in the buffer, like a table of contents.
  5071. With numerical argument N, show content up to level N."
  5072. (interactive "P")
  5073. (save-excursion
  5074. ;; Visit all headings and show their offspring
  5075. (and (integerp arg) (org-overview))
  5076. (goto-char (point-max))
  5077. (catch 'exit
  5078. (while (and (progn (condition-case nil
  5079. (outline-previous-visible-heading 1)
  5080. (error (goto-char (point-min))))
  5081. t)
  5082. (looking-at outline-regexp))
  5083. (if (integerp arg)
  5084. (show-children (1- arg))
  5085. (show-branches))
  5086. (if (bobp) (throw 'exit nil))))))
  5087. (defun org-optimize-window-after-visibility-change (state)
  5088. "Adjust the window after a change in outline visibility.
  5089. This function is the default value of the hook `org-cycle-hook'."
  5090. (when (get-buffer-window (current-buffer))
  5091. (cond
  5092. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5093. ; ((eq state 'overview) (org-beginning-of-line))
  5094. ((eq state 'content) nil)
  5095. ((eq state 'all) nil)
  5096. ((eq state 'folded) nil)
  5097. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5098. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5099. (defun org-compact-display-after-subtree-move ()
  5100. (let (beg end)
  5101. (save-excursion
  5102. (if (org-up-heading-safe)
  5103. (progn
  5104. (hide-subtree)
  5105. (show-entry)
  5106. (show-children)
  5107. (org-cycle-show-empty-lines 'children)
  5108. (org-cycle-hide-drawers 'children))
  5109. (org-overview)))))
  5110. (defun org-cycle-show-empty-lines (state)
  5111. "Show empty lines above all visible headlines.
  5112. The region to be covered depends on STATE when called through
  5113. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5114. entire buffer covered. Note that an empty line is only shown if there
  5115. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5116. (when (> org-cycle-separator-lines 0)
  5117. (save-excursion
  5118. (let* ((n org-cycle-separator-lines)
  5119. (re (cond
  5120. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5121. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5122. (t (let ((ns (number-to-string (- n 2))))
  5123. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5124. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5125. beg end)
  5126. (cond
  5127. ((memq state '(overview contents t))
  5128. (setq beg (point-min) end (point-max)))
  5129. ((memq state '(children folded))
  5130. (setq beg (point) end (progn (org-end-of-subtree t t)
  5131. (beginning-of-line 2)
  5132. (point)))))
  5133. (when beg
  5134. (goto-char beg)
  5135. (while (re-search-forward re end t)
  5136. (if (not (get-char-property (match-end 1) 'invisible))
  5137. (outline-flag-region
  5138. (match-beginning 1) (match-end 1) nil)))))))
  5139. ;; Never hide empty lines at the end of the file.
  5140. (save-excursion
  5141. (goto-char (point-max))
  5142. (outline-previous-heading)
  5143. (outline-end-of-heading)
  5144. (if (and (looking-at "[ \t\n]+")
  5145. (= (match-end 0) (point-max)))
  5146. (outline-flag-region (point) (match-end 0) nil))))
  5147. (defun org-subtree-end-visible-p ()
  5148. "Is the end of the current subtree visible?"
  5149. (pos-visible-in-window-p
  5150. (save-excursion (org-end-of-subtree t) (point))))
  5151. (defun org-first-headline-recenter (&optional N)
  5152. "Move cursor to the first headline and recenter the headline.
  5153. Optional argument N means, put the headline into the Nth line of the window."
  5154. (goto-char (point-min))
  5155. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5156. (beginning-of-line)
  5157. (recenter (prefix-numeric-value N))))
  5158. ;;; Org-goto
  5159. (defvar org-goto-window-configuration nil)
  5160. (defvar org-goto-marker nil)
  5161. (defvar org-goto-map
  5162. (let ((map (make-sparse-keymap)))
  5163. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5164. (while (setq cmd (pop cmds))
  5165. (substitute-key-definition cmd cmd map global-map)))
  5166. (suppress-keymap map)
  5167. (org-defkey map "\C-m" 'org-goto-ret)
  5168. (org-defkey map [(left)] 'org-goto-left)
  5169. (org-defkey map [(right)] 'org-goto-right)
  5170. (org-defkey map [(?q)] 'org-goto-quit)
  5171. (org-defkey map [(control ?g)] 'org-goto-quit)
  5172. (org-defkey map "\C-i" 'org-cycle)
  5173. (org-defkey map [(tab)] 'org-cycle)
  5174. (org-defkey map [(down)] 'outline-next-visible-heading)
  5175. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5176. (org-defkey map "n" 'outline-next-visible-heading)
  5177. (org-defkey map "p" 'outline-previous-visible-heading)
  5178. (org-defkey map "f" 'outline-forward-same-level)
  5179. (org-defkey map "b" 'outline-backward-same-level)
  5180. (org-defkey map "u" 'outline-up-heading)
  5181. (org-defkey map "/" 'org-occur)
  5182. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5183. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5184. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5185. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5186. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5187. map))
  5188. (defconst org-goto-help
  5189. "Browse copy of buffer to find location or copy text.
  5190. RET=jump to location [Q]uit and return to previous location
  5191. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur"
  5192. )
  5193. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5194. (defun org-goto ()
  5195. "Look up a different location in the current file, keeping current visibility.
  5196. When you want look-up or go to a different location in a document, the
  5197. fastest way is often to fold the entire buffer and then dive into the tree.
  5198. This method has the disadvantage, that the previous location will be folded,
  5199. which may not be what you want.
  5200. This command works around this by showing a copy of the current buffer
  5201. in an indirect buffer, in overview mode. You can dive into the tree in
  5202. that copy, use org-occur and incremental search to find a location.
  5203. When pressing RET or `Q', the command returns to the original buffer in
  5204. which the visibility is still unchanged. After RET is will also jump to
  5205. the location selected in the indirect buffer and expose the
  5206. the headline hierarchy above."
  5207. (interactive)
  5208. (let* ((org-goto-start-pos (point))
  5209. (selected-point
  5210. (car (org-get-location (current-buffer) org-goto-help))))
  5211. (if selected-point
  5212. (progn
  5213. (org-mark-ring-push org-goto-start-pos)
  5214. (goto-char selected-point)
  5215. (if (or (org-invisible-p) (org-invisible-p2))
  5216. (org-show-context 'org-goto)))
  5217. (message "Quit"))))
  5218. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5219. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5220. (defun org-get-location (buf help)
  5221. "Let the user select a location in the Org-mode buffer BUF.
  5222. This function uses a recursive edit. It returns the selected position
  5223. or nil."
  5224. (let (org-goto-selected-point org-goto-exit-command)
  5225. (save-excursion
  5226. (save-window-excursion
  5227. (delete-other-windows)
  5228. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5229. (switch-to-buffer
  5230. (condition-case nil
  5231. (make-indirect-buffer (current-buffer) "*org-goto*")
  5232. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5233. (with-output-to-temp-buffer "*Help*"
  5234. (princ help))
  5235. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5236. (setq buffer-read-only nil)
  5237. (let ((org-startup-truncated t)
  5238. (org-startup-folded nil)
  5239. (org-startup-align-all-tables nil))
  5240. (org-mode)
  5241. (org-overview))
  5242. (setq buffer-read-only t)
  5243. (if (and (boundp 'org-goto-start-pos)
  5244. (integer-or-marker-p org-goto-start-pos))
  5245. (let ((org-show-hierarchy-above t)
  5246. (org-show-siblings t)
  5247. (org-show-following-heading t))
  5248. (goto-char org-goto-start-pos)
  5249. (and (org-invisible-p) (org-show-context)))
  5250. (goto-char (point-min)))
  5251. (org-beginning-of-line)
  5252. (message "Select location and press RET")
  5253. ;; now we make sure that during selection, ony very few keys work
  5254. ;; and that it is impossible to switch to another window.
  5255. ; (let ((gm (current-global-map))
  5256. ; (overriding-local-map org-goto-map))
  5257. ; (unwind-protect
  5258. ; (progn
  5259. ; (use-global-map org-goto-map)
  5260. ; (recursive-edit))
  5261. ; (use-global-map gm)))
  5262. (use-local-map org-goto-map)
  5263. (recursive-edit)
  5264. ))
  5265. (kill-buffer "*org-goto*")
  5266. (cons org-goto-selected-point org-goto-exit-command)))
  5267. (defun org-goto-ret (&optional arg)
  5268. "Finish `org-goto' by going to the new location."
  5269. (interactive "P")
  5270. (setq org-goto-selected-point (point)
  5271. org-goto-exit-command 'return)
  5272. (throw 'exit nil))
  5273. (defun org-goto-left ()
  5274. "Finish `org-goto' by going to the new location."
  5275. (interactive)
  5276. (if (org-on-heading-p)
  5277. (progn
  5278. (beginning-of-line 1)
  5279. (setq org-goto-selected-point (point)
  5280. org-goto-exit-command 'left)
  5281. (throw 'exit nil))
  5282. (error "Not on a heading")))
  5283. (defun org-goto-right ()
  5284. "Finish `org-goto' by going to the new location."
  5285. (interactive)
  5286. (if (org-on-heading-p)
  5287. (progn
  5288. (setq org-goto-selected-point (point)
  5289. org-goto-exit-command 'right)
  5290. (throw 'exit nil))
  5291. (error "Not on a heading")))
  5292. (defun org-goto-quit ()
  5293. "Finish `org-goto' without cursor motion."
  5294. (interactive)
  5295. (setq org-goto-selected-point nil)
  5296. (setq org-goto-exit-command 'quit)
  5297. (throw 'exit nil))
  5298. ;;; Indirect buffer display of subtrees
  5299. (defvar org-indirect-dedicated-frame nil
  5300. "This is the frame being used for indirect tree display.")
  5301. (defvar org-last-indirect-buffer nil)
  5302. (defun org-tree-to-indirect-buffer (&optional arg)
  5303. "Create indirect buffer and narrow it to current subtree.
  5304. With numerical prefix ARG, go up to this level and then take that tree.
  5305. If ARG is negative, go up that many levels.
  5306. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5307. indirect buffer previously made with this command, to avoid proliferation of
  5308. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5309. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5310. is kept so that you can work with several indirect buffers at the same time.
  5311. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5312. requests that a new frame be made for the new buffer, so that the dedicated
  5313. frame is not changed."
  5314. (interactive "P")
  5315. (let ((cbuf (current-buffer))
  5316. (cwin (selected-window))
  5317. (pos (point))
  5318. beg end level heading ibuf)
  5319. (save-excursion
  5320. (org-back-to-heading t)
  5321. (when (numberp arg)
  5322. (setq level (org-outline-level))
  5323. (if (< arg 0) (setq arg (+ level arg)))
  5324. (while (> (setq level (org-outline-level)) arg)
  5325. (outline-up-heading 1 t)))
  5326. (setq beg (point)
  5327. heading (org-get-heading))
  5328. (org-end-of-subtree t) (setq end (point)))
  5329. (if (and (buffer-live-p org-last-indirect-buffer)
  5330. (not (eq org-indirect-buffer-display 'new-frame))
  5331. (not arg))
  5332. (kill-buffer org-last-indirect-buffer))
  5333. (setq ibuf (org-get-indirect-buffer cbuf)
  5334. org-last-indirect-buffer ibuf)
  5335. (cond
  5336. ((or (eq org-indirect-buffer-display 'new-frame)
  5337. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5338. (select-frame (make-frame))
  5339. (delete-other-windows)
  5340. (switch-to-buffer ibuf)
  5341. (org-set-frame-title heading))
  5342. ((eq org-indirect-buffer-display 'dedicated-frame)
  5343. (raise-frame
  5344. (select-frame (or (and org-indirect-dedicated-frame
  5345. (frame-live-p org-indirect-dedicated-frame)
  5346. org-indirect-dedicated-frame)
  5347. (setq org-indirect-dedicated-frame (make-frame)))))
  5348. (delete-other-windows)
  5349. (switch-to-buffer ibuf)
  5350. (org-set-frame-title (concat "Indirect: " heading)))
  5351. ((eq org-indirect-buffer-display 'current-window)
  5352. (switch-to-buffer ibuf))
  5353. ((eq org-indirect-buffer-display 'other-window)
  5354. (pop-to-buffer ibuf))
  5355. (t (error "Invalid value.")))
  5356. (if (featurep 'xemacs)
  5357. (save-excursion (org-mode) (turn-on-font-lock)))
  5358. (narrow-to-region beg end)
  5359. (show-all)
  5360. (goto-char pos)
  5361. (and (window-live-p cwin) (select-window cwin))))
  5362. (defun org-get-indirect-buffer (&optional buffer)
  5363. (setq buffer (or buffer (current-buffer)))
  5364. (let ((n 1) (base (buffer-name buffer)) bname)
  5365. (while (buffer-live-p
  5366. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5367. (setq n (1+ n)))
  5368. (condition-case nil
  5369. (make-indirect-buffer buffer bname 'clone)
  5370. (error (make-indirect-buffer buffer bname)))))
  5371. (defun org-set-frame-title (title)
  5372. "Set the title of the current frame to the string TITLE."
  5373. ;; FIXME: how to name a single frame in XEmacs???
  5374. (unless (featurep 'xemacs)
  5375. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5376. ;;;; Structure editing
  5377. ;;; Inserting headlines
  5378. (defun org-insert-heading (&optional force-heading)
  5379. "Insert a new heading or item with same depth at point.
  5380. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5381. If point is at the beginning of a headline, insert a sibling before the
  5382. current headline. If point is in the middle of a headline, split the headline
  5383. at that position and make the rest of the headline part of the sibling below
  5384. the current headline."
  5385. (interactive "P")
  5386. (if (= (buffer-size) 0)
  5387. (insert "\n* ")
  5388. (when (or force-heading (not (org-insert-item)))
  5389. (let* ((head (save-excursion
  5390. (condition-case nil
  5391. (progn
  5392. (org-back-to-heading)
  5393. (match-string 0))
  5394. (error "*"))))
  5395. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5396. pos)
  5397. (cond
  5398. ((and (org-on-heading-p) (bolp)
  5399. (or (bobp)
  5400. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5401. (open-line (if blank 2 1)))
  5402. ((and (bolp)
  5403. (or (bobp)
  5404. (save-excursion
  5405. (backward-char 1) (not (org-invisible-p)))))
  5406. nil)
  5407. (t (newline (if blank 2 1))))
  5408. (insert head) (just-one-space)
  5409. (setq pos (point))
  5410. (end-of-line 1)
  5411. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5412. (run-hooks 'org-insert-heading-hook)))))
  5413. (defun org-insert-heading-after-current ()
  5414. "Insert a new heading with same level as current, after current subtree."
  5415. (interactive)
  5416. (org-back-to-heading)
  5417. (org-insert-heading)
  5418. (org-move-subtree-down)
  5419. (end-of-line 1))
  5420. (defun org-insert-todo-heading (arg)
  5421. "Insert a new heading with the same level and TODO state as current heading.
  5422. If the heading has no TODO state, or if the state is DONE, use the first
  5423. state (TODO by default). Also with prefix arg, force first state."
  5424. (interactive "P")
  5425. (when (not (org-insert-item 'checkbox))
  5426. (org-insert-heading)
  5427. (save-excursion
  5428. (org-back-to-heading)
  5429. (outline-previous-heading)
  5430. (looking-at org-todo-line-regexp))
  5431. (if (or arg
  5432. (not (match-beginning 2))
  5433. (member (match-string 2) org-done-keywords))
  5434. (insert (car org-todo-keywords-1) " ")
  5435. (insert (match-string 2) " "))))
  5436. (defun org-insert-subheading (arg)
  5437. "Insert a new subheading and demote it.
  5438. Works for outline headings and for plain lists alike."
  5439. (interactive "P")
  5440. (org-insert-heading arg)
  5441. (cond
  5442. ((org-on-heading-p) (org-do-demote))
  5443. ((org-at-item-p) (org-indent-item 1))))
  5444. (defun org-insert-todo-subheading (arg)
  5445. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5446. Works for outline headings and for plain lists alike."
  5447. (interactive "P")
  5448. (org-insert-todo-heading arg)
  5449. (cond
  5450. ((org-on-heading-p) (org-do-demote))
  5451. ((org-at-item-p) (org-indent-item 1))))
  5452. ;;; Promotion and Demotion
  5453. (defun org-promote-subtree ()
  5454. "Promote the entire subtree.
  5455. See also `org-promote'."
  5456. (interactive)
  5457. (save-excursion
  5458. (org-map-tree 'org-promote))
  5459. (org-fix-position-after-promote))
  5460. (defun org-demote-subtree ()
  5461. "Demote the entire subtree. See `org-demote'.
  5462. See also `org-promote'."
  5463. (interactive)
  5464. (save-excursion
  5465. (org-map-tree 'org-demote))
  5466. (org-fix-position-after-promote))
  5467. (defun org-do-promote ()
  5468. "Promote the current heading higher up the tree.
  5469. If the region is active in `transient-mark-mode', promote all headings
  5470. in the region."
  5471. (interactive)
  5472. (save-excursion
  5473. (if (org-region-active-p)
  5474. (org-map-region 'org-promote (region-beginning) (region-end))
  5475. (org-promote)))
  5476. (org-fix-position-after-promote))
  5477. (defun org-do-demote ()
  5478. "Demote the current heading lower down the tree.
  5479. If the region is active in `transient-mark-mode', demote all headings
  5480. in the region."
  5481. (interactive)
  5482. (save-excursion
  5483. (if (org-region-active-p)
  5484. (org-map-region 'org-demote (region-beginning) (region-end))
  5485. (org-demote)))
  5486. (org-fix-position-after-promote))
  5487. (defun org-fix-position-after-promote ()
  5488. "Make sure that after pro/demotion cursor position is right."
  5489. (let ((pos (point)))
  5490. (when (save-excursion
  5491. (beginning-of-line 1)
  5492. (looking-at org-todo-line-regexp)
  5493. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5494. (cond ((eobp) (insert " "))
  5495. ((eolp) (insert " "))
  5496. ((equal (char-after) ?\ ) (forward-char 1))))))
  5497. (defun org-reduced-level (l)
  5498. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5499. (defun org-get-legal-level (level &optional change)
  5500. "Rectify a level change under the influence of `org-odd-levels-only'
  5501. LEVEL is a current level, CHANGE is by how much the level should be
  5502. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5503. even level numbers will become the next higher odd number."
  5504. (if org-odd-levels-only
  5505. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5506. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5507. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5508. (max 1 (+ level change))))
  5509. (defun org-promote ()
  5510. "Promote the current heading higher up the tree.
  5511. If the region is active in `transient-mark-mode', promote all headings
  5512. in the region."
  5513. (org-back-to-heading t)
  5514. (let* ((level (save-match-data (funcall outline-level)))
  5515. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5516. (diff (abs (- level (length up-head) -1))))
  5517. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5518. (replace-match up-head nil t)
  5519. ;; Fixup tag positioning
  5520. (and org-auto-align-tags (org-set-tags nil t))
  5521. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5522. (defun org-demote ()
  5523. "Demote the current heading lower down the tree.
  5524. If the region is active in `transient-mark-mode', demote all headings
  5525. in the region."
  5526. (org-back-to-heading t)
  5527. (let* ((level (save-match-data (funcall outline-level)))
  5528. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5529. (diff (abs (- level (length down-head) -1))))
  5530. (replace-match down-head nil t)
  5531. ;; Fixup tag positioning
  5532. (and org-auto-align-tags (org-set-tags nil t))
  5533. (if org-adapt-indentation (org-fixup-indentation diff))))
  5534. (defun org-map-tree (fun)
  5535. "Call FUN for every heading underneath the current one."
  5536. (org-back-to-heading)
  5537. (let ((level (funcall outline-level)))
  5538. (save-excursion
  5539. (funcall fun)
  5540. (while (and (progn
  5541. (outline-next-heading)
  5542. (> (funcall outline-level) level))
  5543. (not (eobp)))
  5544. (funcall fun)))))
  5545. (defun org-map-region (fun beg end)
  5546. "Call FUN for every heading between BEG and END."
  5547. (let ((org-ignore-region t))
  5548. (save-excursion
  5549. (setq end (copy-marker end))
  5550. (goto-char beg)
  5551. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5552. (< (point) end))
  5553. (funcall fun))
  5554. (while (and (progn
  5555. (outline-next-heading)
  5556. (< (point) end))
  5557. (not (eobp)))
  5558. (funcall fun)))))
  5559. (defun org-fixup-indentation (diff)
  5560. "Change the indentation in the current entry by DIFF
  5561. However, if any line in the current entry has no indentation, or if it
  5562. would end up with no indentation after the change, nothing at all is done."
  5563. (save-excursion
  5564. (let ((end (save-excursion (outline-next-heading)
  5565. (point-marker)))
  5566. (prohibit (if (> diff 0)
  5567. "^\\S-"
  5568. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5569. col)
  5570. (unless (save-excursion (end-of-line 1)
  5571. (re-search-forward prohibit end t))
  5572. (while (and (< (point) end)
  5573. (re-search-forward "^[ \t]+" end t))
  5574. (goto-char (match-end 0))
  5575. (setq col (current-column))
  5576. (if (< diff 0) (replace-match ""))
  5577. (indent-to (+ diff col))))
  5578. (move-marker end nil))))
  5579. (defun org-convert-to-odd-levels ()
  5580. "Convert an org-mode file with all levels allowed to one with odd levels.
  5581. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5582. level 5 etc."
  5583. (interactive)
  5584. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5585. (let ((org-odd-levels-only nil) n)
  5586. (save-excursion
  5587. (goto-char (point-min))
  5588. (while (re-search-forward "^\\*\\*+ " nil t)
  5589. (setq n (- (length (match-string 0)) 2))
  5590. (while (>= (setq n (1- n)) 0)
  5591. (org-demote))
  5592. (end-of-line 1))))))
  5593. (defun org-convert-to-oddeven-levels ()
  5594. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5595. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5596. section with an even level, conversion would destroy the structure of the file. An error
  5597. is signaled in this case."
  5598. (interactive)
  5599. (goto-char (point-min))
  5600. ;; First check if there are no even levels
  5601. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5602. (org-show-context t)
  5603. (error "Not all levels are odd in this file. Conversion not possible."))
  5604. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5605. (let ((org-odd-levels-only nil) n)
  5606. (save-excursion
  5607. (goto-char (point-min))
  5608. (while (re-search-forward "^\\*\\*+ " nil t)
  5609. (setq n (/ (1- (length (match-string 0))) 2))
  5610. (while (>= (setq n (1- n)) 0)
  5611. (org-promote))
  5612. (end-of-line 1))))))
  5613. (defun org-tr-level (n)
  5614. "Make N odd if required."
  5615. (if org-odd-levels-only (1+ (/ n 2)) n))
  5616. ;;; Vertical tree motion, cutting and pasting of subtrees
  5617. (defun org-move-subtree-up (&optional arg)
  5618. "Move the current subtree up past ARG headlines of the same level."
  5619. (interactive "p")
  5620. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5621. (defun org-move-subtree-down (&optional arg)
  5622. "Move the current subtree down past ARG headlines of the same level."
  5623. (interactive "p")
  5624. (setq arg (prefix-numeric-value arg))
  5625. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5626. 'outline-get-last-sibling))
  5627. (ins-point (make-marker))
  5628. (cnt (abs arg))
  5629. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5630. ;; Select the tree
  5631. (org-back-to-heading)
  5632. (setq beg0 (point))
  5633. (save-excursion
  5634. (setq ne-beg (org-back-over-empty-lines))
  5635. (setq beg (point)))
  5636. (save-match-data
  5637. (save-excursion (outline-end-of-heading)
  5638. (setq folded (org-invisible-p)))
  5639. (outline-end-of-subtree))
  5640. (outline-next-heading)
  5641. (setq ne-end (org-back-over-empty-lines))
  5642. (setq end (point))
  5643. (goto-char beg0)
  5644. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5645. ;; include less whitespace
  5646. (save-excursion
  5647. (goto-char beg)
  5648. (forward-line (- ne-beg ne-end))
  5649. (setq beg (point))))
  5650. ;; Find insertion point, with error handling
  5651. (while (> cnt 0)
  5652. (or (and (funcall movfunc) (looking-at outline-regexp))
  5653. (progn (goto-char beg0)
  5654. (error "Cannot move past superior level or buffer limit")))
  5655. (setq cnt (1- cnt)))
  5656. (if (> arg 0)
  5657. ;; Moving forward - still need to move over subtree
  5658. (progn (org-end-of-subtree t t)
  5659. (save-excursion
  5660. (org-back-over-empty-lines)
  5661. (or (bolp) (newline)))))
  5662. (setq ne-ins (org-back-over-empty-lines))
  5663. (move-marker ins-point (point))
  5664. (setq txt (buffer-substring beg end))
  5665. (delete-region beg end)
  5666. (outline-flag-region (1- beg) beg nil)
  5667. (outline-flag-region (1- (point)) (point) nil)
  5668. (insert txt)
  5669. (or (bolp) (insert "\n"))
  5670. (setq ins-end (point))
  5671. (goto-char ins-point)
  5672. (org-skip-whitespace)
  5673. (when (and (< arg 0)
  5674. (org-first-sibling-p)
  5675. (> ne-ins ne-beg))
  5676. ;; Move whitespace back to beginning
  5677. (save-excursion
  5678. (goto-char ins-end)
  5679. (let ((kill-whole-line t))
  5680. (kill-line (- ne-ins ne-beg)) (point)))
  5681. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5682. (move-marker ins-point nil)
  5683. (org-compact-display-after-subtree-move)
  5684. (unless folded
  5685. (org-show-entry)
  5686. (show-children)
  5687. (org-cycle-hide-drawers 'children))))
  5688. (defvar org-subtree-clip ""
  5689. "Clipboard for cut and paste of subtrees.
  5690. This is actually only a copy of the kill, because we use the normal kill
  5691. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5692. (defvar org-subtree-clip-folded nil
  5693. "Was the last copied subtree folded?
  5694. This is used to fold the tree back after pasting.")
  5695. (defun org-cut-subtree (&optional n)
  5696. "Cut the current subtree into the clipboard.
  5697. With prefix arg N, cut this many sequential subtrees.
  5698. This is a short-hand for marking the subtree and then cutting it."
  5699. (interactive "p")
  5700. (org-copy-subtree n 'cut))
  5701. (defun org-copy-subtree (&optional n cut)
  5702. "Cut the current subtree into the clipboard.
  5703. With prefix arg N, cut this many sequential subtrees.
  5704. This is a short-hand for marking the subtree and then copying it.
  5705. If CUT is non-nil, actually cut the subtree."
  5706. (interactive "p")
  5707. (let (beg end folded (beg0 (point)))
  5708. (if (interactive-p)
  5709. (org-back-to-heading nil) ; take what looks like a subtree
  5710. (org-back-to-heading t)) ; take what is really there
  5711. (org-back-over-empty-lines)
  5712. (setq beg (point))
  5713. (skip-chars-forward " \t\r\n")
  5714. (save-match-data
  5715. (save-excursion (outline-end-of-heading)
  5716. (setq folded (org-invisible-p)))
  5717. (condition-case nil
  5718. (outline-forward-same-level (1- n))
  5719. (error nil))
  5720. (org-end-of-subtree t t))
  5721. (org-back-over-empty-lines)
  5722. (setq end (point))
  5723. (goto-char beg0)
  5724. (when (> end beg)
  5725. (setq org-subtree-clip-folded folded)
  5726. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5727. (setq org-subtree-clip (current-kill 0))
  5728. (message "%s: Subtree(s) with %d characters"
  5729. (if cut "Cut" "Copied")
  5730. (length org-subtree-clip)))))
  5731. (defun org-paste-subtree (&optional level tree)
  5732. "Paste the clipboard as a subtree, with modification of headline level.
  5733. The entire subtree is promoted or demoted in order to match a new headline
  5734. level. By default, the new level is derived from the visible headings
  5735. before and after the insertion point, and taken to be the inferior headline
  5736. level of the two. So if the previous visible heading is level 3 and the
  5737. next is level 4 (or vice versa), level 4 will be used for insertion.
  5738. This makes sure that the subtree remains an independent subtree and does
  5739. not swallow low level entries.
  5740. You can also force a different level, either by using a numeric prefix
  5741. argument, or by inserting the heading marker by hand. For example, if the
  5742. cursor is after \"*****\", then the tree will be shifted to level 5.
  5743. If you want to insert the tree as is, just use \\[yank].
  5744. If optional TREE is given, use this text instead of the kill ring."
  5745. (interactive "P")
  5746. (unless (org-kill-is-subtree-p tree)
  5747. (error "%s"
  5748. (substitute-command-keys
  5749. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5750. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5751. (^re (concat "^\\(" outline-regexp "\\)"))
  5752. (re (concat "\\(" outline-regexp "\\)"))
  5753. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5754. (old-level (if (string-match ^re txt)
  5755. (- (match-end 0) (match-beginning 0) 1)
  5756. -1))
  5757. (force-level (cond (level (prefix-numeric-value level))
  5758. ((string-match
  5759. ^re_ (buffer-substring (point-at-bol) (point)))
  5760. (- (match-end 1) (match-beginning 1)))
  5761. (t nil)))
  5762. (previous-level (save-excursion
  5763. (condition-case nil
  5764. (progn
  5765. (outline-previous-visible-heading 1)
  5766. (if (looking-at re)
  5767. (- (match-end 0) (match-beginning 0) 1)
  5768. 1))
  5769. (error 1))))
  5770. (next-level (save-excursion
  5771. (condition-case nil
  5772. (progn
  5773. (or (looking-at outline-regexp)
  5774. (outline-next-visible-heading 1))
  5775. (if (looking-at re)
  5776. (- (match-end 0) (match-beginning 0) 1)
  5777. 1))
  5778. (error 1))))
  5779. (new-level (or force-level (max previous-level next-level)))
  5780. (shift (if (or (= old-level -1)
  5781. (= new-level -1)
  5782. (= old-level new-level))
  5783. 0
  5784. (- new-level old-level)))
  5785. (delta (if (> shift 0) -1 1))
  5786. (func (if (> shift 0) 'org-demote 'org-promote))
  5787. (org-odd-levels-only nil)
  5788. beg end)
  5789. ;; Remove the forced level indicator
  5790. (if force-level
  5791. (delete-region (point-at-bol) (point)))
  5792. ;; Paste
  5793. (beginning-of-line 1)
  5794. (org-back-over-empty-lines) ;; FIXME: correct fix????
  5795. (setq beg (point))
  5796. (insert-before-markers txt) ;; FIXME: correct fix????
  5797. (unless (string-match "\n\\'" txt) (insert "\n"))
  5798. (setq end (point))
  5799. (goto-char beg)
  5800. (skip-chars-forward " \t\n\r")
  5801. (setq beg (point))
  5802. ;; Shift if necessary
  5803. (unless (= shift 0)
  5804. (save-restriction
  5805. (narrow-to-region beg end)
  5806. (while (not (= shift 0))
  5807. (org-map-region func (point-min) (point-max))
  5808. (setq shift (+ delta shift)))
  5809. (goto-char (point-min))))
  5810. (when (interactive-p)
  5811. (message "Clipboard pasted as level %d subtree" new-level))
  5812. (if (and kill-ring
  5813. (eq org-subtree-clip (current-kill 0))
  5814. org-subtree-clip-folded)
  5815. ;; The tree was folded before it was killed/copied
  5816. (hide-subtree))))
  5817. (defun org-kill-is-subtree-p (&optional txt)
  5818. "Check if the current kill is an outline subtree, or a set of trees.
  5819. Returns nil if kill does not start with a headline, or if the first
  5820. headline level is not the largest headline level in the tree.
  5821. So this will actually accept several entries of equal levels as well,
  5822. which is OK for `org-paste-subtree'.
  5823. If optional TXT is given, check this string instead of the current kill."
  5824. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5825. (start-level (and kill
  5826. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5827. org-outline-regexp "\\)")
  5828. kill)
  5829. (- (match-end 2) (match-beginning 2) 1)))
  5830. (re (concat "^" org-outline-regexp))
  5831. (start (1+ (match-beginning 2))))
  5832. (if (not start-level)
  5833. (progn
  5834. nil) ;; does not even start with a heading
  5835. (catch 'exit
  5836. (while (setq start (string-match re kill (1+ start)))
  5837. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5838. (throw 'exit nil)))
  5839. t))))
  5840. (defun org-narrow-to-subtree ()
  5841. "Narrow buffer to the current subtree."
  5842. (interactive)
  5843. (save-excursion
  5844. (narrow-to-region
  5845. (progn (org-back-to-heading) (point))
  5846. (progn (org-end-of-subtree t t) (point)))))
  5847. ;;; Outline Sorting
  5848. (defun org-sort (with-case)
  5849. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5850. Optional argument WITH-CASE means sort case-sensitively."
  5851. (interactive "P")
  5852. (if (org-at-table-p)
  5853. (org-call-with-arg 'org-table-sort-lines with-case)
  5854. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5855. (defvar org-priority-regexp) ; defined later in the file
  5856. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  5857. "Sort entries on a certain level of an outline tree.
  5858. If there is an active region, the entries in the region are sorted.
  5859. Else, if the cursor is before the first entry, sort the top-level items.
  5860. Else, the children of the entry at point are sorted.
  5861. Sorting can be alphabetically, numerically, and by date/time as given by
  5862. the first time stamp in the entry. The command prompts for the sorting
  5863. type unless it has been given to the function through the SORTING-TYPE
  5864. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5865. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5866. called with point at the beginning of the record. It must return either
  5867. a string or a number that should serve as the sorting key for that record.
  5868. Comparing entries ignores case by default. However, with an optional argument
  5869. WITH-CASE, the sorting considers case as well."
  5870. (interactive "P")
  5871. (let ((case-func (if with-case 'identity 'downcase))
  5872. start beg end stars re re2
  5873. txt what tmp plain-list-p)
  5874. ;; Find beginning and end of region to sort
  5875. (cond
  5876. ((org-region-active-p)
  5877. ;; we will sort the region
  5878. (setq end (region-end)
  5879. what "region")
  5880. (goto-char (region-beginning))
  5881. (if (not (org-on-heading-p)) (outline-next-heading))
  5882. (setq start (point)))
  5883. ((org-at-item-p)
  5884. ;; we will sort this plain list
  5885. (org-beginning-of-item-list) (setq start (point))
  5886. (org-end-of-item-list) (setq end (point))
  5887. (goto-char start)
  5888. (setq plain-list-p t
  5889. what "plain list"))
  5890. ((or (org-on-heading-p)
  5891. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5892. ;; we will sort the children of the current headline
  5893. (org-back-to-heading)
  5894. (setq start (point)
  5895. end (progn (org-end-of-subtree t t)
  5896. (org-back-over-empty-lines)
  5897. (point))
  5898. what "children")
  5899. (goto-char start)
  5900. (show-subtree)
  5901. (outline-next-heading))
  5902. (t
  5903. ;; we will sort the top-level entries in this file
  5904. (goto-char (point-min))
  5905. (or (org-on-heading-p) (outline-next-heading))
  5906. (setq start (point) end (point-max) what "top-level")
  5907. (goto-char start)
  5908. (show-all)))
  5909. (setq beg (point))
  5910. (if (>= beg end) (error "Nothing to sort"))
  5911. (unless plain-list-p
  5912. (looking-at "\\(\\*+\\)")
  5913. (setq stars (match-string 1)
  5914. re (concat "^" (regexp-quote stars) " +")
  5915. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5916. txt (buffer-substring beg end))
  5917. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5918. (if (and (not (equal stars "*")) (string-match re2 txt))
  5919. (error "Region to sort contains a level above the first entry")))
  5920. (unless sorting-type
  5921. (message
  5922. (if plain-list-p
  5923. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5924. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  5925. what)
  5926. (setq sorting-type (read-char-exclusive))
  5927. (and (= (downcase sorting-type) ?f)
  5928. (setq getkey-func
  5929. (completing-read "Sort using function: "
  5930. obarray 'fboundp t nil nil))
  5931. (setq getkey-func (intern getkey-func)))
  5932. (and (= (downcase sorting-type) ?r)
  5933. (setq property
  5934. (completing-read "Property: "
  5935. (mapcar 'list (org-buffer-property-keys t))
  5936. nil t))))
  5937. (message "Sorting entries...")
  5938. (save-restriction
  5939. (narrow-to-region start end)
  5940. (let ((dcst (downcase sorting-type))
  5941. (now (current-time)))
  5942. (sort-subr
  5943. (/= dcst sorting-type)
  5944. ;; This function moves to the beginning character of the "record" to
  5945. ;; be sorted.
  5946. (if plain-list-p
  5947. (lambda nil
  5948. (if (org-at-item-p) t (goto-char (point-max))))
  5949. (lambda nil
  5950. (if (re-search-forward re nil t)
  5951. (goto-char (match-beginning 0))
  5952. (goto-char (point-max)))))
  5953. ;; This function moves to the last character of the "record" being
  5954. ;; sorted.
  5955. (if plain-list-p
  5956. 'org-end-of-item
  5957. (lambda nil
  5958. (save-match-data
  5959. (condition-case nil
  5960. (outline-forward-same-level 1)
  5961. (error
  5962. (goto-char (point-max)))))))
  5963. ;; This function returns the value that gets sorted against.
  5964. (if plain-list-p
  5965. (lambda nil
  5966. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5967. (cond
  5968. ((= dcst ?n)
  5969. (string-to-number (buffer-substring (match-end 0)
  5970. (point-at-eol))))
  5971. ((= dcst ?a)
  5972. (buffer-substring (match-end 0) (point-at-eol)))
  5973. ((= dcst ?t)
  5974. (if (re-search-forward org-ts-regexp
  5975. (point-at-eol) t)
  5976. (org-time-string-to-time (match-string 0))
  5977. now))
  5978. ((= dcst ?f)
  5979. (if getkey-func
  5980. (progn
  5981. (setq tmp (funcall getkey-func))
  5982. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5983. tmp)
  5984. (error "Invalid key function `%s'" getkey-func)))
  5985. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5986. (lambda nil
  5987. (cond
  5988. ((= dcst ?n)
  5989. (if (looking-at outline-regexp)
  5990. (string-to-number (buffer-substring (match-end 0)
  5991. (point-at-eol)))
  5992. nil))
  5993. ((= dcst ?a)
  5994. (funcall case-func (buffer-substring (point-at-bol)
  5995. (point-at-eol))))
  5996. ((= dcst ?t)
  5997. (if (re-search-forward org-ts-regexp
  5998. (save-excursion
  5999. (forward-line 2)
  6000. (point)) t)
  6001. (org-time-string-to-time (match-string 0))
  6002. now))
  6003. ((= dcst ?p)
  6004. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6005. (string-to-char (match-string 2))
  6006. org-default-priority))
  6007. ((= dcst ?r)
  6008. (or (org-entry-get nil property) ""))
  6009. ((= dcst ?f)
  6010. (if getkey-func
  6011. (progn
  6012. (setq tmp (funcall getkey-func))
  6013. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6014. tmp)
  6015. (error "Invalid key function `%s'" getkey-func)))
  6016. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6017. nil
  6018. (cond
  6019. ((= dcst ?a) 'string<)
  6020. ((= dcst ?t) 'time-less-p)
  6021. (t nil)))))
  6022. (message "Sorting entries...done")))
  6023. (defun org-do-sort (table what &optional with-case sorting-type)
  6024. "Sort TABLE of WHAT according to SORTING-TYPE.
  6025. The user will be prompted for the SORTING-TYPE if the call to this
  6026. function does not specify it. WHAT is only for the prompt, to indicate
  6027. what is being sorted. The sorting key will be extracted from
  6028. the car of the elements of the table.
  6029. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6030. (unless sorting-type
  6031. (message
  6032. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6033. what)
  6034. (setq sorting-type (read-char-exclusive)))
  6035. (let ((dcst (downcase sorting-type))
  6036. extractfun comparefun)
  6037. ;; Define the appropriate functions
  6038. (cond
  6039. ((= dcst ?n)
  6040. (setq extractfun 'string-to-number
  6041. comparefun (if (= dcst sorting-type) '< '>)))
  6042. ((= dcst ?a)
  6043. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6044. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6045. comparefun (if (= dcst sorting-type)
  6046. 'string<
  6047. (lambda (a b) (and (not (string< a b))
  6048. (not (string= a b)))))))
  6049. ((= dcst ?t)
  6050. (setq extractfun
  6051. (lambda (x)
  6052. (if (string-match org-ts-regexp x)
  6053. (time-to-seconds
  6054. (org-time-string-to-time (match-string 0 x)))
  6055. 0))
  6056. comparefun (if (= dcst sorting-type) '< '>)))
  6057. (t (error "Invalid sorting type `%c'" sorting-type)))
  6058. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6059. table)
  6060. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6061. ;;;; Plain list items, including checkboxes
  6062. ;;; Plain list items
  6063. (defun org-at-item-p ()
  6064. "Is point in a line starting a hand-formatted item?"
  6065. (let ((llt org-plain-list-ordered-item-terminator))
  6066. (save-excursion
  6067. (goto-char (point-at-bol))
  6068. (looking-at
  6069. (cond
  6070. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6071. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6072. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6073. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6074. (defun org-in-item-p ()
  6075. "It the cursor inside a plain list item.
  6076. Does not have to be the first line."
  6077. (save-excursion
  6078. (condition-case nil
  6079. (progn
  6080. (org-beginning-of-item)
  6081. (org-at-item-p)
  6082. t)
  6083. (error nil))))
  6084. (defun org-insert-item (&optional checkbox)
  6085. "Insert a new item at the current level.
  6086. Return t when things worked, nil when we are not in an item."
  6087. (when (save-excursion
  6088. (condition-case nil
  6089. (progn
  6090. (org-beginning-of-item)
  6091. (org-at-item-p)
  6092. (if (org-invisible-p) (error "Invisible item"))
  6093. t)
  6094. (error nil)))
  6095. (let* ((bul (match-string 0))
  6096. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6097. (match-end 0)))
  6098. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6099. pos)
  6100. (cond
  6101. ((and (org-at-item-p) (<= (point) eow))
  6102. ;; before the bullet
  6103. (beginning-of-line 1)
  6104. (open-line (if blank 2 1)))
  6105. ((<= (point) eow)
  6106. (beginning-of-line 1))
  6107. (t (newline (if blank 2 1))))
  6108. (insert bul (if checkbox "[ ]" ""))
  6109. (just-one-space)
  6110. (setq pos (point))
  6111. (end-of-line 1)
  6112. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6113. (org-maybe-renumber-ordered-list)
  6114. (and checkbox (org-update-checkbox-count-maybe))
  6115. t))
  6116. ;;; Checkboxes
  6117. (defun org-at-item-checkbox-p ()
  6118. "Is point at a line starting a plain-list item with a checklet?"
  6119. (and (org-at-item-p)
  6120. (save-excursion
  6121. (goto-char (match-end 0))
  6122. (skip-chars-forward " \t")
  6123. (looking-at "\\[[- X]\\]"))))
  6124. (defun org-toggle-checkbox (&optional arg)
  6125. "Toggle the checkbox in the current line."
  6126. (interactive "P")
  6127. (catch 'exit
  6128. (let (beg end status (firstnew 'unknown))
  6129. (cond
  6130. ((org-region-active-p)
  6131. (setq beg (region-beginning) end (region-end)))
  6132. ((org-on-heading-p)
  6133. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6134. ((org-at-item-checkbox-p)
  6135. (let ((pos (point)))
  6136. (replace-match
  6137. (cond (arg "[-]")
  6138. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6139. (t "[ ]"))
  6140. t t)
  6141. (goto-char pos))
  6142. (throw 'exit t))
  6143. (t (error "Not at a checkbox or heading, and no active region")))
  6144. (save-excursion
  6145. (goto-char beg)
  6146. (while (< (point) end)
  6147. (when (org-at-item-checkbox-p)
  6148. (setq status (equal (match-string 0) "[X]"))
  6149. (when (eq firstnew 'unknown)
  6150. (setq firstnew (not status)))
  6151. (replace-match
  6152. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6153. (beginning-of-line 2)))))
  6154. (org-update-checkbox-count-maybe))
  6155. (defun org-update-checkbox-count-maybe ()
  6156. "Update checkbox statistics unless turned off by user."
  6157. (when org-provide-checkbox-statistics
  6158. (org-update-checkbox-count)))
  6159. (defun org-update-checkbox-count (&optional all)
  6160. "Update the checkbox statistics in the current section.
  6161. This will find all statistic cookies like [57%] and [6/12] and update them
  6162. with the current numbers. With optional prefix argument ALL, do this for
  6163. the whole buffer."
  6164. (interactive "P")
  6165. (save-excursion
  6166. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6167. (beg (condition-case nil
  6168. (progn (outline-back-to-heading) (point))
  6169. (error (point-min))))
  6170. (end (move-marker (make-marker)
  6171. (progn (outline-next-heading) (point))))
  6172. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  6173. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6174. b1 e1 f1 c-on c-off lim (cstat 0))
  6175. (when all
  6176. (goto-char (point-min))
  6177. (outline-next-heading)
  6178. (setq beg (point) end (point-max)))
  6179. (goto-char beg)
  6180. (while (re-search-forward re end t)
  6181. (setq cstat (1+ cstat)
  6182. b1 (match-beginning 0)
  6183. e1 (match-end 0)
  6184. f1 (match-beginning 1)
  6185. lim (cond
  6186. ((org-on-heading-p) (outline-next-heading) (point))
  6187. ((org-at-item-p) (org-end-of-item) (point))
  6188. (t nil))
  6189. c-on 0 c-off 0)
  6190. (goto-char e1)
  6191. (when lim
  6192. (while (re-search-forward re-box lim t)
  6193. (if (member (match-string 2) '("[ ]" "[-]"))
  6194. (setq c-off (1+ c-off))
  6195. (setq c-on (1+ c-on))))
  6196. ; (delete-region b1 e1)
  6197. (goto-char b1)
  6198. (insert (if f1
  6199. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6200. (format "[%d/%d]" c-on (+ c-on c-off))))
  6201. (and (looking-at "\\[.*?\\]")
  6202. (replace-match ""))))
  6203. (when (interactive-p)
  6204. (message "Checkbox satistics updated %s (%d places)"
  6205. (if all "in entire file" "in current outline entry") cstat)))))
  6206. (defun org-get-checkbox-statistics-face ()
  6207. "Select the face for checkbox statistics.
  6208. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6209. it will be `org-todo'."
  6210. (if (match-end 1)
  6211. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6212. (if (and (> (match-end 2) (match-beginning 2))
  6213. (equal (match-string 2) (match-string 3)))
  6214. 'org-done
  6215. 'org-todo)))
  6216. (defun org-get-indentation (&optional line)
  6217. "Get the indentation of the current line, interpreting tabs.
  6218. When LINE is given, assume it represents a line and compute its indentation."
  6219. (if line
  6220. (if (string-match "^ *" (org-remove-tabs line))
  6221. (match-end 0))
  6222. (save-excursion
  6223. (beginning-of-line 1)
  6224. (skip-chars-forward " \t")
  6225. (current-column))))
  6226. (defun org-remove-tabs (s &optional width)
  6227. "Replace tabulators in S with spaces.
  6228. Assumes that s is a single line, starting in column 0."
  6229. (setq width (or width tab-width))
  6230. (while (string-match "\t" s)
  6231. (setq s (replace-match
  6232. (make-string
  6233. (- (* width (/ (+ (match-beginning 0) width) width))
  6234. (match-beginning 0)) ?\ )
  6235. t t s)))
  6236. s)
  6237. (defun org-fix-indentation (line ind)
  6238. "Fix indentation in LINE.
  6239. IND is a cons cell with target and minimum indentation.
  6240. If the current indenation in LINE is smaller than the minimum,
  6241. leave it alone. If it is larger than ind, set it to the target."
  6242. (let* ((l (org-remove-tabs line))
  6243. (i (org-get-indentation l))
  6244. (i1 (car ind)) (i2 (cdr ind)))
  6245. (if (>= i i2) (setq l (substring line i2)))
  6246. (if (> i1 0)
  6247. (concat (make-string i1 ?\ ) l)
  6248. l)))
  6249. (defcustom org-empty-line-terminates-plain-lists nil
  6250. "Non-nil means, an empty line ends all plain list levels.
  6251. When nil, empty lines are part of the preceeding item."
  6252. :group 'org-plain-lists
  6253. :type 'boolean)
  6254. (defun org-beginning-of-item ()
  6255. "Go to the beginning of the current hand-formatted item.
  6256. If the cursor is not in an item, throw an error."
  6257. (interactive)
  6258. (let ((pos (point))
  6259. (limit (save-excursion
  6260. (condition-case nil
  6261. (progn
  6262. (org-back-to-heading)
  6263. (beginning-of-line 2) (point))
  6264. (error (point-min)))))
  6265. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6266. ind ind1)
  6267. (if (org-at-item-p)
  6268. (beginning-of-line 1)
  6269. (beginning-of-line 1)
  6270. (skip-chars-forward " \t")
  6271. (setq ind (current-column))
  6272. (if (catch 'exit
  6273. (while t
  6274. (beginning-of-line 0)
  6275. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6276. (if (looking-at "[ \t]*$")
  6277. (setq ind1 ind-empty)
  6278. (skip-chars-forward " \t")
  6279. (setq ind1 (current-column)))
  6280. (if (< ind1 ind)
  6281. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6282. nil
  6283. (goto-char pos)
  6284. (error "Not in an item")))))
  6285. (defun org-end-of-item ()
  6286. "Go to the end of the current hand-formatted item.
  6287. If the cursor is not in an item, throw an error."
  6288. (interactive)
  6289. (let* ((pos (point))
  6290. ind1
  6291. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6292. (limit (save-excursion (outline-next-heading) (point)))
  6293. (ind (save-excursion
  6294. (org-beginning-of-item)
  6295. (skip-chars-forward " \t")
  6296. (current-column)))
  6297. (end (catch 'exit
  6298. (while t
  6299. (beginning-of-line 2)
  6300. (if (eobp) (throw 'exit (point)))
  6301. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6302. (if (looking-at "[ \t]*$")
  6303. (setq ind1 ind-empty)
  6304. (skip-chars-forward " \t")
  6305. (setq ind1 (current-column)))
  6306. (if (<= ind1 ind)
  6307. (throw 'exit (point-at-bol)))))))
  6308. (if end
  6309. (goto-char end)
  6310. (goto-char pos)
  6311. (error "Not in an item"))))
  6312. (defun org-next-item ()
  6313. "Move to the beginning of the next item in the current plain list.
  6314. Error if not at a plain list, or if this is the last item in the list."
  6315. (interactive)
  6316. (let (ind ind1 (pos (point)))
  6317. (org-beginning-of-item)
  6318. (setq ind (org-get-indentation))
  6319. (org-end-of-item)
  6320. (setq ind1 (org-get-indentation))
  6321. (unless (and (org-at-item-p) (= ind ind1))
  6322. (goto-char pos)
  6323. (error "On last item"))))
  6324. (defun org-previous-item ()
  6325. "Move to the beginning of the previous item in the current plain list.
  6326. Error if not at a plain list, or if this is the first item in the list."
  6327. (interactive)
  6328. (let (beg ind ind1 (pos (point)))
  6329. (org-beginning-of-item)
  6330. (setq beg (point))
  6331. (setq ind (org-get-indentation))
  6332. (goto-char beg)
  6333. (catch 'exit
  6334. (while t
  6335. (beginning-of-line 0)
  6336. (if (looking-at "[ \t]*$")
  6337. nil
  6338. (if (<= (setq ind1 (org-get-indentation)) ind)
  6339. (throw 'exit t)))))
  6340. (condition-case nil
  6341. (if (or (not (org-at-item-p))
  6342. (< ind1 (1- ind)))
  6343. (error "")
  6344. (org-beginning-of-item))
  6345. (error (goto-char pos)
  6346. (error "On first item")))))
  6347. (defun org-first-list-item-p ()
  6348. "Is this heading the item in a plain list?"
  6349. (unless (org-at-item-p)
  6350. (error "Not at a plain list item"))
  6351. (org-beginning-of-item)
  6352. (= (point) (save-excursion (org-beginning-of-item-list))))
  6353. (defun org-move-item-down ()
  6354. "Move the plain list item at point down, i.e. swap with following item.
  6355. Subitems (items with larger indentation) are considered part of the item,
  6356. so this really moves item trees."
  6357. (interactive)
  6358. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6359. (org-beginning-of-item)
  6360. (setq beg0 (point))
  6361. (save-excursion
  6362. (setq ne-beg (org-back-over-empty-lines))
  6363. (setq beg (point)))
  6364. (goto-char beg0)
  6365. (setq ind (org-get-indentation))
  6366. (org-end-of-item)
  6367. (setq end0 (point))
  6368. (setq ind1 (org-get-indentation))
  6369. (setq ne-end (org-back-over-empty-lines))
  6370. (setq end (point))
  6371. (goto-char beg0)
  6372. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6373. ;; include less whitespace
  6374. (save-excursion
  6375. (goto-char beg)
  6376. (forward-line (- ne-beg ne-end))
  6377. (setq beg (point))))
  6378. (goto-char end0)
  6379. (if (and (org-at-item-p) (= ind ind1))
  6380. (progn
  6381. (org-end-of-item)
  6382. (org-back-over-empty-lines)
  6383. (setq txt (buffer-substring beg end))
  6384. (save-excursion
  6385. (delete-region beg end))
  6386. (setq pos (point))
  6387. (insert txt)
  6388. (goto-char pos) (org-skip-whitespace)
  6389. (org-maybe-renumber-ordered-list))
  6390. (goto-char pos)
  6391. (error "Cannot move this item further down"))))
  6392. (defun org-move-item-up (arg)
  6393. "Move the plain list item at point up, i.e. swap with previous item.
  6394. Subitems (items with larger indentation) are considered part of the item,
  6395. so this really moves item trees."
  6396. (interactive "p")
  6397. (let (beg beg0 end end0 ind ind1 (pos (point)) txt
  6398. ne-beg ne-end ne-ins ins-end)
  6399. (org-beginning-of-item)
  6400. (setq beg0 (point))
  6401. (setq ind (org-get-indentation))
  6402. (save-excursion
  6403. (setq ne-beg (org-back-over-empty-lines))
  6404. (setq beg (point)))
  6405. (goto-char beg0)
  6406. (org-end-of-item)
  6407. (setq ne-end (org-back-over-empty-lines))
  6408. (setq end (point))
  6409. (goto-char beg0)
  6410. (catch 'exit
  6411. (while t
  6412. (beginning-of-line 0)
  6413. (if (looking-at "[ \t]*$")
  6414. (if org-empty-line-terminates-plain-lists
  6415. (progn
  6416. (goto-char pos)
  6417. (error "Cannot move this item further up"))
  6418. nil)
  6419. (if (<= (setq ind1 (org-get-indentation)) ind)
  6420. (throw 'exit t)))))
  6421. (condition-case nil
  6422. (org-beginning-of-item)
  6423. (error (goto-char beg)
  6424. (error "Cannot move this item further up")))
  6425. (setq ind1 (org-get-indentation))
  6426. (if (and (org-at-item-p) (= ind ind1))
  6427. (progn
  6428. (setq ne-ins (org-back-over-empty-lines))
  6429. (setq txt (buffer-substring beg end))
  6430. (save-excursion
  6431. (delete-region beg end))
  6432. (setq pos (point))
  6433. (insert txt)
  6434. (setq ins-end (point))
  6435. (goto-char pos) (org-skip-whitespace)
  6436. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6437. ;; Move whitespace back to beginning
  6438. (save-excursion
  6439. (goto-char ins-end)
  6440. (let ((kill-whole-line t))
  6441. (kill-line (- ne-ins ne-beg)) (point)))
  6442. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6443. (org-maybe-renumber-ordered-list))
  6444. (goto-char pos)
  6445. (error "Cannot move this item further up"))))
  6446. (defun org-maybe-renumber-ordered-list ()
  6447. "Renumber the ordered list at point if setup allows it.
  6448. This tests the user option `org-auto-renumber-ordered-lists' before
  6449. doing the renumbering."
  6450. (interactive)
  6451. (when (and org-auto-renumber-ordered-lists
  6452. (org-at-item-p))
  6453. (if (match-beginning 3)
  6454. (org-renumber-ordered-list 1)
  6455. (org-fix-bullet-type))))
  6456. (defun org-maybe-renumber-ordered-list-safe ()
  6457. (condition-case nil
  6458. (save-excursion
  6459. (org-maybe-renumber-ordered-list))
  6460. (error nil)))
  6461. (defun org-cycle-list-bullet (&optional which)
  6462. "Cycle through the different itemize/enumerate bullets.
  6463. This cycle the entire list level through the sequence:
  6464. `-' -> `+' -> `*' -> `1.' -> `1)'
  6465. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6466. 0 meand `-', 1 means `+' etc."
  6467. (interactive "P")
  6468. (org-preserve-lc
  6469. (org-beginning-of-item-list)
  6470. (org-at-item-p)
  6471. (beginning-of-line 1)
  6472. (let ((current (match-string 0))
  6473. (prevp (eq which 'previous))
  6474. new)
  6475. (setq new (cond
  6476. ((and (numberp which)
  6477. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6478. ((string-match "-" current) (if prevp "1)" "+"))
  6479. ((string-match "\\+" current)
  6480. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6481. ((string-match "\\*" current) (if prevp "+" "1."))
  6482. ((string-match "\\." current) (if prevp "*" "1)"))
  6483. ((string-match ")" current) (if prevp "1." "-"))
  6484. (t (error "This should not happen"))))
  6485. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6486. (org-fix-bullet-type)
  6487. (org-maybe-renumber-ordered-list))))
  6488. (defun org-get-string-indentation (s)
  6489. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6490. (let ((n -1) (i 0) (w tab-width) c)
  6491. (catch 'exit
  6492. (while (< (setq n (1+ n)) (length s))
  6493. (setq c (aref s n))
  6494. (cond ((= c ?\ ) (setq i (1+ i)))
  6495. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6496. (t (throw 'exit t)))))
  6497. i))
  6498. (defun org-renumber-ordered-list (arg)
  6499. "Renumber an ordered plain list.
  6500. Cursor needs to be in the first line of an item, the line that starts
  6501. with something like \"1.\" or \"2)\"."
  6502. (interactive "p")
  6503. (unless (and (org-at-item-p)
  6504. (match-beginning 3))
  6505. (error "This is not an ordered list"))
  6506. (let ((line (org-current-line))
  6507. (col (current-column))
  6508. (ind (org-get-string-indentation
  6509. (buffer-substring (point-at-bol) (match-beginning 3))))
  6510. ;; (term (substring (match-string 3) -1))
  6511. ind1 (n (1- arg))
  6512. fmt)
  6513. ;; find where this list begins
  6514. (org-beginning-of-item-list)
  6515. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6516. (setq fmt (concat "%d" (match-string 1)))
  6517. (beginning-of-line 0)
  6518. ;; walk forward and replace these numbers
  6519. (catch 'exit
  6520. (while t
  6521. (catch 'next
  6522. (beginning-of-line 2)
  6523. (if (eobp) (throw 'exit nil))
  6524. (if (looking-at "[ \t]*$") (throw 'next nil))
  6525. (skip-chars-forward " \t") (setq ind1 (current-column))
  6526. (if (> ind1 ind) (throw 'next t))
  6527. (if (< ind1 ind) (throw 'exit t))
  6528. (if (not (org-at-item-p)) (throw 'exit nil))
  6529. (delete-region (match-beginning 2) (match-end 2))
  6530. (goto-char (match-beginning 2))
  6531. (insert (format fmt (setq n (1+ n)))))))
  6532. (goto-line line)
  6533. (move-to-column col)))
  6534. (defun org-fix-bullet-type ()
  6535. "Make sure all items in this list have the same bullet as the firsst item."
  6536. (interactive)
  6537. (unless (org-at-item-p) (error "This is not a list"))
  6538. (let ((line (org-current-line))
  6539. (col (current-column))
  6540. (ind (current-indentation))
  6541. ind1 bullet)
  6542. ;; find where this list begins
  6543. (org-beginning-of-item-list)
  6544. (beginning-of-line 1)
  6545. ;; find out what the bullet type is
  6546. (looking-at "[ \t]*\\(\\S-+\\)")
  6547. (setq bullet (match-string 1))
  6548. ;; walk forward and replace these numbers
  6549. (beginning-of-line 0)
  6550. (catch 'exit
  6551. (while t
  6552. (catch 'next
  6553. (beginning-of-line 2)
  6554. (if (eobp) (throw 'exit nil))
  6555. (if (looking-at "[ \t]*$") (throw 'next nil))
  6556. (skip-chars-forward " \t") (setq ind1 (current-column))
  6557. (if (> ind1 ind) (throw 'next t))
  6558. (if (< ind1 ind) (throw 'exit t))
  6559. (if (not (org-at-item-p)) (throw 'exit nil))
  6560. (skip-chars-forward " \t")
  6561. (looking-at "\\S-+")
  6562. (replace-match bullet))))
  6563. (goto-line line)
  6564. (move-to-column col)
  6565. (if (string-match "[0-9]" bullet)
  6566. (org-renumber-ordered-list 1))))
  6567. (defun org-beginning-of-item-list ()
  6568. "Go to the beginning of the current item list.
  6569. I.e. to the first item in this list."
  6570. (interactive)
  6571. (org-beginning-of-item)
  6572. (let ((pos (point-at-bol))
  6573. (ind (org-get-indentation))
  6574. ind1)
  6575. ;; find where this list begins
  6576. (catch 'exit
  6577. (while t
  6578. (catch 'next
  6579. (beginning-of-line 0)
  6580. (if (looking-at "[ \t]*$")
  6581. (throw (if (bobp) 'exit 'next) t))
  6582. (skip-chars-forward " \t") (setq ind1 (current-column))
  6583. (if (or (< ind1 ind)
  6584. (and (= ind1 ind)
  6585. (not (org-at-item-p)))
  6586. (bobp))
  6587. (throw 'exit t)
  6588. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6589. (goto-char pos)))
  6590. (defun org-end-of-item-list ()
  6591. "Go to the end of the current item list.
  6592. I.e. to the text after the last item."
  6593. (interactive)
  6594. (org-beginning-of-item)
  6595. (let ((pos (point-at-bol))
  6596. (ind (org-get-indentation))
  6597. ind1)
  6598. ;; find where this list begins
  6599. (catch 'exit
  6600. (while t
  6601. (catch 'next
  6602. (beginning-of-line 2)
  6603. (if (looking-at "[ \t]*$")
  6604. (throw (if (eobp) 'exit 'next) t))
  6605. (skip-chars-forward " \t") (setq ind1 (current-column))
  6606. (if (or (< ind1 ind)
  6607. (and (= ind1 ind)
  6608. (not (org-at-item-p)))
  6609. (eobp))
  6610. (progn
  6611. (setq pos (point-at-bol))
  6612. (throw 'exit t))))))
  6613. (goto-char pos)))
  6614. (defvar org-last-indent-begin-marker (make-marker))
  6615. (defvar org-last-indent-end-marker (make-marker))
  6616. (defun org-outdent-item (arg)
  6617. "Outdent a local list item."
  6618. (interactive "p")
  6619. (org-indent-item (- arg)))
  6620. (defun org-indent-item (arg)
  6621. "Indent a local list item."
  6622. (interactive "p")
  6623. (unless (org-at-item-p)
  6624. (error "Not on an item"))
  6625. (save-excursion
  6626. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6627. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6628. (setq beg org-last-indent-begin-marker
  6629. end org-last-indent-end-marker)
  6630. (org-beginning-of-item)
  6631. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6632. (org-end-of-item)
  6633. (setq end (move-marker org-last-indent-end-marker (point))))
  6634. (goto-char beg)
  6635. (setq tmp (org-item-indent-positions)
  6636. ind (car tmp)
  6637. ind-down (nth 2 tmp)
  6638. ind-up (nth 1 tmp)
  6639. delta (if (> arg 0)
  6640. (if ind-down (- ind-down ind) 2)
  6641. (if ind-up (- ind-up ind) -2)))
  6642. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6643. (while (< (point) end)
  6644. (beginning-of-line 1)
  6645. (skip-chars-forward " \t") (setq ind1 (current-column))
  6646. (delete-region (point-at-bol) (point))
  6647. (or (eolp) (indent-to-column (+ ind1 delta)))
  6648. (beginning-of-line 2))))
  6649. (org-fix-bullet-type)
  6650. (org-maybe-renumber-ordered-list-safe)
  6651. (save-excursion
  6652. (beginning-of-line 0)
  6653. (condition-case nil (org-beginning-of-item) (error nil))
  6654. (org-maybe-renumber-ordered-list-safe)))
  6655. (defun org-item-indent-positions ()
  6656. "Return indentation for plain list items.
  6657. This returns a list with three values: The current indentation, the
  6658. parent indentation and the indentation a child should habe.
  6659. Assumes cursor in item line."
  6660. (let* ((bolpos (point-at-bol))
  6661. (ind (org-get-indentation))
  6662. ind-down ind-up pos)
  6663. (save-excursion
  6664. (org-beginning-of-item-list)
  6665. (skip-chars-backward "\n\r \t")
  6666. (when (org-in-item-p)
  6667. (org-beginning-of-item)
  6668. (setq ind-up (org-get-indentation))))
  6669. (setq pos (point))
  6670. (save-excursion
  6671. (cond
  6672. ((and (condition-case nil (progn (org-previous-item) t)
  6673. (error nil))
  6674. (or (forward-char 1) t)
  6675. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6676. (setq ind-down (org-get-indentation)))
  6677. ((and (goto-char pos)
  6678. (org-at-item-p))
  6679. (goto-char (match-end 0))
  6680. (skip-chars-forward " \t")
  6681. (setq ind-down (current-column)))))
  6682. (list ind ind-up ind-down)))
  6683. ;;; The orgstruct minor mode
  6684. ;; Define a minor mode which can be used in other modes in order to
  6685. ;; integrate the org-mode structure editing commands.
  6686. ;; This is really a hack, because the org-mode structure commands use
  6687. ;; keys which normally belong to the major mode. Here is how it
  6688. ;; works: The minor mode defines all the keys necessary to operate the
  6689. ;; structure commands, but wraps the commands into a function which
  6690. ;; tests if the cursor is currently at a headline or a plain list
  6691. ;; item. If that is the case, the structure command is used,
  6692. ;; temporarily setting many Org-mode variables like regular
  6693. ;; expressions for filling etc. However, when any of those keys is
  6694. ;; used at a different location, function uses `key-binding' to look
  6695. ;; up if the key has an associated command in another currently active
  6696. ;; keymap (minor modes, major mode, global), and executes that
  6697. ;; command. There might be problems if any of the keys is otherwise
  6698. ;; used as a prefix key.
  6699. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6700. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6701. ;; addresses this by checking explicitly for both bindings.
  6702. (defvar orgstruct-mode-map (make-sparse-keymap)
  6703. "Keymap for the minor `orgstruct-mode'.")
  6704. (defvar org-local-vars nil
  6705. "List of local variables, for use by `orgstruct-mode'")
  6706. ;;;###autoload
  6707. (define-minor-mode orgstruct-mode
  6708. "Toggle the minor more `orgstruct-mode'.
  6709. This mode is for using Org-mode structure commands in other modes.
  6710. The following key behave as if Org-mode was active, if the cursor
  6711. is on a headline, or on a plain list item (both in the definition
  6712. of Org-mode).
  6713. M-up Move entry/item up
  6714. M-down Move entry/item down
  6715. M-left Promote
  6716. M-right Demote
  6717. M-S-up Move entry/item up
  6718. M-S-down Move entry/item down
  6719. M-S-left Promote subtree
  6720. M-S-right Demote subtree
  6721. M-q Fill paragraph and items like in Org-mode
  6722. C-c ^ Sort entries
  6723. C-c - Cycle list bullet
  6724. TAB Cycle item visibility
  6725. M-RET Insert new heading/item
  6726. S-M-RET Insert new TODO heading / Chekbox item
  6727. C-c C-c Set tags / toggle checkbox"
  6728. nil " OrgStruct" nil
  6729. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6730. ;;;###autoload
  6731. (defun turn-on-orgstruct ()
  6732. "Unconditionally turn on `orgstruct-mode'."
  6733. (orgstruct-mode 1))
  6734. ;;;###autoload
  6735. (defun turn-on-orgstruct++ ()
  6736. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  6737. In addition to setting orgstruct-mode, this also exports all indentation and
  6738. autofilling variables from org-mode into the buffer. Note that turning
  6739. off orgstruct-mode will *not* remove these additonal settings."
  6740. (orgstruct-mode 1)
  6741. (let (var val)
  6742. (mapc
  6743. (lambda (x)
  6744. (when (string-match
  6745. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6746. (symbol-name (car x)))
  6747. (setq var (car x) val (nth 1 x))
  6748. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6749. org-local-vars)))
  6750. (defun orgstruct-error ()
  6751. "Error when there is no default binding for a structure key."
  6752. (interactive)
  6753. (error "This key has no function outside structure elements"))
  6754. (defun orgstruct-setup ()
  6755. "Setup orgstruct keymaps."
  6756. (let ((nfunc 0)
  6757. (bindings
  6758. (list
  6759. '([(meta up)] org-metaup)
  6760. '([(meta down)] org-metadown)
  6761. '([(meta left)] org-metaleft)
  6762. '([(meta right)] org-metaright)
  6763. '([(meta shift up)] org-shiftmetaup)
  6764. '([(meta shift down)] org-shiftmetadown)
  6765. '([(meta shift left)] org-shiftmetaleft)
  6766. '([(meta shift right)] org-shiftmetaright)
  6767. '([(shift up)] org-shiftup)
  6768. '([(shift down)] org-shiftdown)
  6769. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6770. '("\M-q" fill-paragraph)
  6771. '("\C-c^" org-sort)
  6772. '("\C-c-" org-cycle-list-bullet)))
  6773. elt key fun cmd)
  6774. (while (setq elt (pop bindings))
  6775. (setq nfunc (1+ nfunc))
  6776. (setq key (org-key (car elt))
  6777. fun (nth 1 elt)
  6778. cmd (orgstruct-make-binding fun nfunc key))
  6779. (org-defkey orgstruct-mode-map key cmd))
  6780. ;; Special treatment needed for TAB and RET
  6781. (org-defkey orgstruct-mode-map [(tab)]
  6782. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6783. (org-defkey orgstruct-mode-map "\C-i"
  6784. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6785. (org-defkey orgstruct-mode-map "\M-\C-m"
  6786. (orgstruct-make-binding 'org-insert-heading 105
  6787. "\M-\C-m" [(meta return)]))
  6788. (org-defkey orgstruct-mode-map [(meta return)]
  6789. (orgstruct-make-binding 'org-insert-heading 106
  6790. [(meta return)] "\M-\C-m"))
  6791. (org-defkey orgstruct-mode-map [(shift meta return)]
  6792. (orgstruct-make-binding 'org-insert-todo-heading 107
  6793. [(meta return)] "\M-\C-m"))
  6794. (unless org-local-vars
  6795. (setq org-local-vars (org-get-local-variables)))
  6796. t))
  6797. (defun orgstruct-make-binding (fun n &rest keys)
  6798. "Create a function for binding in the structure minor mode.
  6799. FUN is the command to call inside a table. N is used to create a unique
  6800. command name. KEYS are keys that should be checked in for a command
  6801. to execute outside of tables."
  6802. (eval
  6803. (list 'defun
  6804. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6805. '(arg)
  6806. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6807. "Outside of structure, run the binding of `"
  6808. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6809. "'.")
  6810. '(interactive "p")
  6811. (list 'if
  6812. '(org-context-p 'headline 'item)
  6813. (list 'org-run-like-in-org-mode (list 'quote fun))
  6814. (list 'let '(orgstruct-mode)
  6815. (list 'call-interactively
  6816. (append '(or)
  6817. (mapcar (lambda (k)
  6818. (list 'key-binding k))
  6819. keys)
  6820. '('orgstruct-error))))))))
  6821. (defun org-context-p (&rest contexts)
  6822. "Check if local context is and of CONTEXTS.
  6823. Possible values in the list of contexts are `table', `headline', and `item'."
  6824. (let ((pos (point)))
  6825. (goto-char (point-at-bol))
  6826. (prog1 (or (and (memq 'table contexts)
  6827. (looking-at "[ \t]*|"))
  6828. (and (memq 'headline contexts)
  6829. (looking-at "\\*+"))
  6830. (and (memq 'item contexts)
  6831. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  6832. (goto-char pos))))
  6833. (defun org-get-local-variables ()
  6834. "Return a list of all local variables in an org-mode buffer."
  6835. (let (varlist)
  6836. (with-current-buffer (get-buffer-create "*Org tmp*")
  6837. (erase-buffer)
  6838. (org-mode)
  6839. (setq varlist (buffer-local-variables)))
  6840. (kill-buffer "*Org tmp*")
  6841. (delq nil
  6842. (mapcar
  6843. (lambda (x)
  6844. (setq x
  6845. (if (symbolp x)
  6846. (list x)
  6847. (list (car x) (list 'quote (cdr x)))))
  6848. (if (string-match
  6849. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6850. (symbol-name (car x)))
  6851. x nil))
  6852. varlist))))
  6853. ;;;###autoload
  6854. (defun org-run-like-in-org-mode (cmd)
  6855. (unless org-local-vars
  6856. (setq org-local-vars (org-get-local-variables)))
  6857. (eval (list 'let org-local-vars
  6858. (list 'call-interactively (list 'quote cmd)))))
  6859. ;;;; Archiving
  6860. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  6861. (defun org-archive-subtree (&optional find-done)
  6862. "Move the current subtree to the archive.
  6863. The archive can be a certain top-level heading in the current file, or in
  6864. a different file. The tree will be moved to that location, the subtree
  6865. heading be marked DONE, and the current time will be added.
  6866. When called with prefix argument FIND-DONE, find whole trees without any
  6867. open TODO items and archive them (after getting confirmation from the user).
  6868. If the cursor is not at a headline when this comand is called, try all level
  6869. 1 trees. If the cursor is on a headline, only try the direct children of
  6870. this heading."
  6871. (interactive "P")
  6872. (if find-done
  6873. (org-archive-all-done)
  6874. ;; Save all relevant TODO keyword-relatex variables
  6875. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  6876. (tr-org-todo-keywords-1 org-todo-keywords-1)
  6877. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  6878. (tr-org-done-keywords org-done-keywords)
  6879. (tr-org-todo-regexp org-todo-regexp)
  6880. (tr-org-todo-line-regexp org-todo-line-regexp)
  6881. (tr-org-odd-levels-only org-odd-levels-only)
  6882. (this-buffer (current-buffer))
  6883. (org-archive-location org-archive-location)
  6884. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  6885. ;; start of variables that will be used for saving context
  6886. ;; The compiler complains about them - keep them anyway!
  6887. (file (abbreviate-file-name (buffer-file-name)))
  6888. (time (format-time-string
  6889. (substring (cdr org-time-stamp-formats) 1 -1)
  6890. (current-time)))
  6891. afile heading buffer level newfile-p
  6892. category todo priority
  6893. ;; start of variables that will be used for savind context
  6894. ltags itags prop)
  6895. ;; Try to find a local archive location
  6896. (save-excursion
  6897. (save-restriction
  6898. (widen)
  6899. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  6900. (if (and prop (string-match "\\S-" prop))
  6901. (setq org-archive-location prop)
  6902. (if (or (re-search-backward re nil t)
  6903. (re-search-forward re nil t))
  6904. (setq org-archive-location (match-string 1))))))
  6905. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  6906. (progn
  6907. (setq afile (format (match-string 1 org-archive-location)
  6908. (file-name-nondirectory buffer-file-name))
  6909. heading (match-string 2 org-archive-location)))
  6910. (error "Invalid `org-archive-location'"))
  6911. (if (> (length afile) 0)
  6912. (setq newfile-p (not (file-exists-p afile))
  6913. buffer (find-file-noselect afile))
  6914. (setq buffer (current-buffer)))
  6915. (unless buffer
  6916. (error "Cannot access file \"%s\"" afile))
  6917. (if (and (> (length heading) 0)
  6918. (string-match "^\\*+" heading))
  6919. (setq level (match-end 0))
  6920. (setq heading nil level 0))
  6921. (save-excursion
  6922. (org-back-to-heading t)
  6923. ;; Get context information that will be lost by moving the tree
  6924. (org-refresh-category-properties)
  6925. (setq category (org-get-category)
  6926. todo (and (looking-at org-todo-line-regexp)
  6927. (match-string 2))
  6928. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  6929. ltags (org-get-tags)
  6930. itags (org-delete-all ltags (org-get-tags-at)))
  6931. (setq ltags (mapconcat 'identity ltags " ")
  6932. itags (mapconcat 'identity itags " "))
  6933. ;; We first only copy, in case something goes wrong
  6934. ;; we need to protect this-command, to avoid kill-region sets it,
  6935. ;; which would lead to duplication of subtrees
  6936. (let (this-command) (org-copy-subtree))
  6937. (set-buffer buffer)
  6938. ;; Enforce org-mode for the archive buffer
  6939. (if (not (org-mode-p))
  6940. ;; Force the mode for future visits.
  6941. (let ((org-insert-mode-line-in-empty-file t)
  6942. (org-inhibit-startup t))
  6943. (call-interactively 'org-mode)))
  6944. (when newfile-p
  6945. (goto-char (point-max))
  6946. (insert (format "\nArchived entries from file %s\n\n"
  6947. (buffer-file-name this-buffer))))
  6948. ;; Force the TODO keywords of the original buffer
  6949. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  6950. (org-todo-keywords-1 tr-org-todo-keywords-1)
  6951. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  6952. (org-done-keywords tr-org-done-keywords)
  6953. (org-todo-regexp tr-org-todo-regexp)
  6954. (org-todo-line-regexp tr-org-todo-line-regexp)
  6955. (org-odd-levels-only
  6956. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  6957. org-odd-levels-only
  6958. tr-org-odd-levels-only)))
  6959. (goto-char (point-min))
  6960. (if heading
  6961. (progn
  6962. (if (re-search-forward
  6963. (concat "^" (regexp-quote heading)
  6964. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  6965. nil t)
  6966. (goto-char (match-end 0))
  6967. ;; Heading not found, just insert it at the end
  6968. (goto-char (point-max))
  6969. (or (bolp) (insert "\n"))
  6970. (insert "\n" heading "\n")
  6971. (end-of-line 0))
  6972. ;; Make the subtree visible
  6973. (show-subtree)
  6974. (org-end-of-subtree t)
  6975. (skip-chars-backward " \t\r\n")
  6976. (and (looking-at "[ \t\r\n]*")
  6977. (replace-match "\n\n")))
  6978. ;; No specific heading, just go to end of file.
  6979. (goto-char (point-max)) (insert "\n"))
  6980. ;; Paste
  6981. (org-paste-subtree (org-get-legal-level level 1))
  6982. ;; Mark the entry as done
  6983. (when (and org-archive-mark-done
  6984. (looking-at org-todo-line-regexp)
  6985. (or (not (match-end 2))
  6986. (not (member (match-string 2) org-done-keywords))))
  6987. (let (org-log-done)
  6988. (org-todo
  6989. (car (or (member org-archive-mark-done org-done-keywords)
  6990. org-done-keywords)))))
  6991. ;; Add the context info
  6992. (when org-archive-save-context-info
  6993. (let ((l org-archive-save-context-info) e n v)
  6994. (while (setq e (pop l))
  6995. (when (and (setq v (symbol-value e))
  6996. (stringp v) (string-match "\\S-" v))
  6997. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  6998. (org-entry-put (point) n v)))))
  6999. ;; Save the buffer, if it is not the same buffer.
  7000. (if (not (eq this-buffer buffer)) (save-buffer))))
  7001. ;; Here we are back in the original buffer. Everything seems to have
  7002. ;; worked. So now cut the tree and finish up.
  7003. (let (this-command) (org-cut-subtree))
  7004. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7005. (message "Subtree archived %s"
  7006. (if (eq this-buffer buffer)
  7007. (concat "under heading: " heading)
  7008. (concat "in file: " (abbreviate-file-name afile)))))))
  7009. (defun org-refresh-category-properties ()
  7010. "Refresh category text properties in teh buffer."
  7011. (let ((def-cat (cond
  7012. ((null org-category)
  7013. (if buffer-file-name
  7014. (file-name-sans-extension
  7015. (file-name-nondirectory buffer-file-name))
  7016. "???"))
  7017. ((symbolp org-category) (symbol-name org-category))
  7018. (t org-category)))
  7019. beg end cat pos optionp)
  7020. (org-unmodified
  7021. (save-excursion
  7022. (save-restriction
  7023. (widen)
  7024. (goto-char (point-min))
  7025. (put-text-property (point) (point-max) 'org-category def-cat)
  7026. (while (re-search-forward
  7027. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7028. (setq pos (match-end 0)
  7029. optionp (equal (char-after (match-beginning 0)) ?#)
  7030. cat (org-trim (match-string 2)))
  7031. (if optionp
  7032. (setq beg (point-at-bol) end (point-max))
  7033. (org-back-to-heading t)
  7034. (setq beg (point) end (org-end-of-subtree t t)))
  7035. (put-text-property beg end 'org-category cat)
  7036. (goto-char pos)))))))
  7037. (defun org-archive-all-done (&optional tag)
  7038. "Archive sublevels of the current tree without open TODO items.
  7039. If the cursor is not on a headline, try all level 1 trees. If
  7040. it is on a headline, try all direct children.
  7041. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7042. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7043. (rea (concat ".*:" org-archive-tag ":"))
  7044. (begm (make-marker))
  7045. (endm (make-marker))
  7046. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7047. "Move subtree to archive (no open TODO items)? "))
  7048. beg end (cntarch 0))
  7049. (if (org-on-heading-p)
  7050. (progn
  7051. (setq re1 (concat "^" (regexp-quote
  7052. (make-string
  7053. (1+ (- (match-end 0) (match-beginning 0)))
  7054. ?*))
  7055. " "))
  7056. (move-marker begm (point))
  7057. (move-marker endm (org-end-of-subtree t)))
  7058. (setq re1 "^* ")
  7059. (move-marker begm (point-min))
  7060. (move-marker endm (point-max)))
  7061. (save-excursion
  7062. (goto-char begm)
  7063. (while (re-search-forward re1 endm t)
  7064. (setq beg (match-beginning 0)
  7065. end (save-excursion (org-end-of-subtree t) (point)))
  7066. (goto-char beg)
  7067. (if (re-search-forward re end t)
  7068. (goto-char end)
  7069. (goto-char beg)
  7070. (if (and (or (not tag) (not (looking-at rea)))
  7071. (y-or-n-p question))
  7072. (progn
  7073. (if tag
  7074. (org-toggle-tag org-archive-tag 'on)
  7075. (org-archive-subtree))
  7076. (setq cntarch (1+ cntarch)))
  7077. (goto-char end)))))
  7078. (message "%d trees archived" cntarch)))
  7079. (defun org-cycle-hide-drawers (state)
  7080. "Re-hide all drawers after a visibility state change."
  7081. (when (and (org-mode-p)
  7082. (not (memq state '(overview folded))))
  7083. (save-excursion
  7084. (let* ((globalp (memq state '(contents all)))
  7085. (beg (if globalp (point-min) (point)))
  7086. (end (if globalp (point-max) (org-end-of-subtree t))))
  7087. (goto-char beg)
  7088. (while (re-search-forward org-drawer-regexp end t)
  7089. (org-flag-drawer t))))))
  7090. (defun org-flag-drawer (flag)
  7091. (save-excursion
  7092. (beginning-of-line 1)
  7093. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7094. (let ((b (match-end 0)))
  7095. (if (re-search-forward
  7096. "^[ \t]*:END:"
  7097. (save-excursion (outline-next-heading) (point)) t)
  7098. (outline-flag-region b (point-at-eol) flag)
  7099. (error ":END: line missing"))))))
  7100. (defun org-cycle-hide-archived-subtrees (state)
  7101. "Re-hide all archived subtrees after a visibility state change."
  7102. (when (and (not org-cycle-open-archived-trees)
  7103. (not (memq state '(overview folded))))
  7104. (save-excursion
  7105. (let* ((globalp (memq state '(contents all)))
  7106. (beg (if globalp (point-min) (point)))
  7107. (end (if globalp (point-max) (org-end-of-subtree t))))
  7108. (org-hide-archived-subtrees beg end)
  7109. (goto-char beg)
  7110. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7111. (message "%s" (substitute-command-keys
  7112. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7113. (defun org-force-cycle-archived ()
  7114. "Cycle subtree even if it is archived."
  7115. (interactive)
  7116. (setq this-command 'org-cycle)
  7117. (let ((org-cycle-open-archived-trees t))
  7118. (call-interactively 'org-cycle)))
  7119. (defun org-hide-archived-subtrees (beg end)
  7120. "Re-hide all archived subtrees after a visibility state change."
  7121. (save-excursion
  7122. (let* ((re (concat ":" org-archive-tag ":")))
  7123. (goto-char beg)
  7124. (while (re-search-forward re end t)
  7125. (and (org-on-heading-p) (hide-subtree))
  7126. (org-end-of-subtree t)))))
  7127. (defun org-toggle-tag (tag &optional onoff)
  7128. "Toggle the tag TAG for the current line.
  7129. If ONOFF is `on' or `off', don't toggle but set to this state."
  7130. (unless (org-on-heading-p t) (error "Not on headling"))
  7131. (let (res current)
  7132. (save-excursion
  7133. (beginning-of-line)
  7134. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7135. (point-at-eol) t)
  7136. (progn
  7137. (setq current (match-string 1))
  7138. (replace-match ""))
  7139. (setq current ""))
  7140. (setq current (nreverse (org-split-string current ":")))
  7141. (cond
  7142. ((eq onoff 'on)
  7143. (setq res t)
  7144. (or (member tag current) (push tag current)))
  7145. ((eq onoff 'off)
  7146. (or (not (member tag current)) (setq current (delete tag current))))
  7147. (t (if (member tag current)
  7148. (setq current (delete tag current))
  7149. (setq res t)
  7150. (push tag current))))
  7151. (end-of-line 1)
  7152. (if current
  7153. (progn
  7154. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7155. (org-set-tags nil t))
  7156. (delete-horizontal-space))
  7157. (run-hooks 'org-after-tags-change-hook))
  7158. res))
  7159. (defun org-toggle-archive-tag (&optional arg)
  7160. "Toggle the archive tag for the current headline.
  7161. With prefix ARG, check all children of current headline and offer tagging
  7162. the children that do not contain any open TODO items."
  7163. (interactive "P")
  7164. (if arg
  7165. (org-archive-all-done 'tag)
  7166. (let (set)
  7167. (save-excursion
  7168. (org-back-to-heading t)
  7169. (setq set (org-toggle-tag org-archive-tag))
  7170. (when set (hide-subtree)))
  7171. (and set (beginning-of-line 1))
  7172. (message "Subtree %s" (if set "archived" "unarchived")))))
  7173. ;;;; Tables
  7174. ;;; The table editor
  7175. ;; Watch out: Here we are talking about two different kind of tables.
  7176. ;; Most of the code is for the tables created with the Org-mode table editor.
  7177. ;; Sometimes, we talk about tables created and edited with the table.el
  7178. ;; Emacs package. We call the former org-type tables, and the latter
  7179. ;; table.el-type tables.
  7180. (defun org-before-change-function (beg end)
  7181. "Every change indicates that a table might need an update."
  7182. (setq org-table-may-need-update t))
  7183. (defconst org-table-line-regexp "^[ \t]*|"
  7184. "Detects an org-type table line.")
  7185. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7186. "Detects an org-type table line.")
  7187. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7188. "Detects a table line marked for automatic recalculation.")
  7189. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7190. "Detects a table line marked for automatic recalculation.")
  7191. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7192. "Detects a table line marked for automatic recalculation.")
  7193. (defconst org-table-hline-regexp "^[ \t]*|-"
  7194. "Detects an org-type table hline.")
  7195. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7196. "Detects a table-type table hline.")
  7197. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7198. "Detects an org-type or table-type table.")
  7199. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7200. "Searching from within a table (any type) this finds the first line
  7201. outside the table.")
  7202. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7203. "Searching from within a table (any type) this finds the first line
  7204. outside the table.")
  7205. (defvar org-table-last-highlighted-reference nil)
  7206. (defvar org-table-formula-history nil)
  7207. (defvar org-table-column-names nil
  7208. "Alist with column names, derived from the `!' line.")
  7209. (defvar org-table-column-name-regexp nil
  7210. "Regular expression matching the current column names.")
  7211. (defvar org-table-local-parameters nil
  7212. "Alist with parameter names, derived from the `$' line.")
  7213. (defvar org-table-named-field-locations nil
  7214. "Alist with locations of named fields.")
  7215. (defvar org-table-current-line-types nil
  7216. "Table row types, non-nil only for the duration of a comand.")
  7217. (defvar org-table-current-begin-line nil
  7218. "Table begin line, non-nil only for the duration of a comand.")
  7219. (defvar org-table-current-begin-pos nil
  7220. "Table begin position, non-nil only for the duration of a comand.")
  7221. (defvar org-table-dlines nil
  7222. "Vector of data line line numbers in the current table.")
  7223. (defvar org-table-hlines nil
  7224. "Vector of hline line numbers in the current table.")
  7225. (defconst org-table-range-regexp
  7226. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7227. ;; 1 2 3 4 5
  7228. "Regular expression for matching ranges in formulas.")
  7229. (defconst org-table-range-regexp2
  7230. (concat
  7231. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7232. "\\.\\."
  7233. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7234. "Match a range for reference display.")
  7235. (defconst org-table-translate-regexp
  7236. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7237. "Match a reference that needs translation, for reference display.")
  7238. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7239. (defun org-table-create-with-table.el ()
  7240. "Use the table.el package to insert a new table.
  7241. If there is already a table at point, convert between Org-mode tables
  7242. and table.el tables."
  7243. (interactive)
  7244. (require 'table)
  7245. (cond
  7246. ((org-at-table.el-p)
  7247. (if (y-or-n-p "Convert table to Org-mode table? ")
  7248. (org-table-convert)))
  7249. ((org-at-table-p)
  7250. (if (y-or-n-p "Convert table to table.el table? ")
  7251. (org-table-convert)))
  7252. (t (call-interactively 'table-insert))))
  7253. (defun org-table-create-or-convert-from-region (arg)
  7254. "Convert region to table, or create an empty table.
  7255. If there is an active region, convert it to a table, using the function
  7256. `org-table-convert-region'. See the documentation of that function
  7257. to learn how the prefix argument is interpreted to determine the field
  7258. separator.
  7259. If there is no such region, create an empty table with `org-table-create'."
  7260. (interactive "P")
  7261. (if (org-region-active-p)
  7262. (org-table-convert-region (region-beginning) (region-end) arg)
  7263. (org-table-create arg)))
  7264. (defun org-table-create (&optional size)
  7265. "Query for a size and insert a table skeleton.
  7266. SIZE is a string Columns x Rows like for example \"3x2\"."
  7267. (interactive "P")
  7268. (unless size
  7269. (setq size (read-string
  7270. (concat "Table size Columns x Rows [e.g. "
  7271. org-table-default-size "]: ")
  7272. "" nil org-table-default-size)))
  7273. (let* ((pos (point))
  7274. (indent (make-string (current-column) ?\ ))
  7275. (split (org-split-string size " *x *"))
  7276. (rows (string-to-number (nth 1 split)))
  7277. (columns (string-to-number (car split)))
  7278. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7279. "\n")))
  7280. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7281. (point-at-bol) (point)))
  7282. (beginning-of-line 1)
  7283. (newline))
  7284. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7285. (dotimes (i rows) (insert line))
  7286. (goto-char pos)
  7287. (if (> rows 1)
  7288. ;; Insert a hline after the first row.
  7289. (progn
  7290. (end-of-line 1)
  7291. (insert "\n|-")
  7292. (goto-char pos)))
  7293. (org-table-align)))
  7294. (defun org-table-convert-region (beg0 end0 &optional separator)
  7295. "Convert region to a table.
  7296. The region goes from BEG0 to END0, but these borders will be moved
  7297. slightly, to make sure a beginning of line in the first line is included.
  7298. SEPARATOR specifies the field separator in the lines. It can have the
  7299. following values:
  7300. '(4) Use the comma as a field separator
  7301. '(16) Use a TAB as field separator
  7302. integer When a number, use that many spaces as field separator
  7303. nil When nil, the command tries to be smart and figure out the
  7304. separator in the following way:
  7305. - when each line contains a TAB, assume TAB-separated material
  7306. - when each line contains a comme, assume CSV material
  7307. - else, assume one or more SPACE charcters as separator."
  7308. (interactive "rP")
  7309. (let* ((beg (min beg0 end0))
  7310. (end (max beg0 end0))
  7311. re)
  7312. (goto-char beg)
  7313. (beginning-of-line 1)
  7314. (setq beg (move-marker (make-marker) (point)))
  7315. (goto-char end)
  7316. (if (bolp) (backward-char 1) (end-of-line 1))
  7317. (setq end (move-marker (make-marker) (point)))
  7318. ;; Get the right field separator
  7319. (unless separator
  7320. (goto-char beg)
  7321. (setq separator
  7322. (cond
  7323. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7324. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7325. (t 1))))
  7326. (setq re (cond
  7327. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7328. ((equal separator '(16)) "^\\|\t")
  7329. ((integerp separator)
  7330. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7331. (t (error "This should not happen"))))
  7332. (goto-char beg)
  7333. (while (re-search-forward re end t)
  7334. (replace-match "| " t t))
  7335. (goto-char beg)
  7336. (insert " ")
  7337. (org-table-align)))
  7338. (defun org-table-import (file arg)
  7339. "Import FILE as a table.
  7340. The file is assumed to be tab-separated. Such files can be produced by most
  7341. spreadsheet and database applications. If no tabs (at least one per line)
  7342. are found, lines will be split on whitespace into fields."
  7343. (interactive "f\nP")
  7344. (or (bolp) (newline))
  7345. (let ((beg (point))
  7346. (pm (point-max)))
  7347. (insert-file-contents file)
  7348. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7349. (defun org-table-export ()
  7350. "Export table as a tab-separated file.
  7351. Such a file can be imported into a spreadsheet program like Excel."
  7352. (interactive)
  7353. (let* ((beg (org-table-begin))
  7354. (end (org-table-end))
  7355. (table (buffer-substring beg end))
  7356. (file (read-file-name "Export table to: "))
  7357. buf)
  7358. (unless (or (not (file-exists-p file))
  7359. (y-or-n-p (format "Overwrite file %s? " file)))
  7360. (error "Abort"))
  7361. (with-current-buffer (find-file-noselect file)
  7362. (setq buf (current-buffer))
  7363. (erase-buffer)
  7364. (fundamental-mode)
  7365. (insert table)
  7366. (goto-char (point-min))
  7367. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7368. (replace-match "" t t)
  7369. (end-of-line 1))
  7370. (goto-char (point-min))
  7371. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7372. (replace-match "" t t)
  7373. (goto-char (min (1+ (point)) (point-max))))
  7374. (goto-char (point-min))
  7375. (while (re-search-forward "^-[-+]*$" nil t)
  7376. (replace-match "")
  7377. (if (looking-at "\n")
  7378. (delete-char 1)))
  7379. (goto-char (point-min))
  7380. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7381. (replace-match "\t" t t))
  7382. (save-buffer))
  7383. (kill-buffer buf)))
  7384. (defvar org-table-aligned-begin-marker (make-marker)
  7385. "Marker at the beginning of the table last aligned.
  7386. Used to check if cursor still is in that table, to minimize realignment.")
  7387. (defvar org-table-aligned-end-marker (make-marker)
  7388. "Marker at the end of the table last aligned.
  7389. Used to check if cursor still is in that table, to minimize realignment.")
  7390. (defvar org-table-last-alignment nil
  7391. "List of flags for flushright alignment, from the last re-alignment.
  7392. This is being used to correctly align a single field after TAB or RET.")
  7393. (defvar org-table-last-column-widths nil
  7394. "List of max width of fields in each column.
  7395. This is being used to correctly align a single field after TAB or RET.")
  7396. (defvar org-table-overlay-coordinates nil
  7397. "Overlay coordinates after each align of a table.")
  7398. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7399. (defvar org-last-recalc-line nil)
  7400. (defconst org-narrow-column-arrow "=>"
  7401. "Used as display property in narrowed table columns.")
  7402. (defun org-table-align ()
  7403. "Align the table at point by aligning all vertical bars."
  7404. (interactive)
  7405. (let* (
  7406. ;; Limits of table
  7407. (beg (org-table-begin))
  7408. (end (org-table-end))
  7409. ;; Current cursor position
  7410. (linepos (org-current-line))
  7411. (colpos (org-table-current-column))
  7412. (winstart (window-start))
  7413. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7414. lines (new "") lengths l typenums ty fields maxfields i
  7415. column
  7416. (indent "") cnt frac
  7417. rfmt hfmt
  7418. (spaces '(1 . 1))
  7419. (sp1 (car spaces))
  7420. (sp2 (cdr spaces))
  7421. (rfmt1 (concat
  7422. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7423. (hfmt1 (concat
  7424. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7425. emptystrings links dates emph narrow fmax f1 len c e)
  7426. (untabify beg end)
  7427. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7428. ;; Check if we have links or dates
  7429. (goto-char beg)
  7430. (setq links (re-search-forward org-bracket-link-regexp end t))
  7431. (goto-char beg)
  7432. (setq emph (and org-hide-emphasis-markers
  7433. (re-search-forward org-emph-re end t)))
  7434. (goto-char beg)
  7435. (setq dates (and org-display-custom-times
  7436. (re-search-forward org-ts-regexp-both end t)))
  7437. ;; Make sure the link properties are right
  7438. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7439. ;; Make sure the date properties are right
  7440. (when dates (goto-char beg) (while (org-activate-dates end)))
  7441. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7442. ;; Check if we are narrowing any columns
  7443. (goto-char beg)
  7444. (setq narrow (and org-format-transports-properties-p
  7445. (re-search-forward "<[0-9]+>" end t)))
  7446. ;; Get the rows
  7447. (setq lines (org-split-string
  7448. (buffer-substring beg end) "\n"))
  7449. ;; Store the indentation of the first line
  7450. (if (string-match "^ *" (car lines))
  7451. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7452. ;; Mark the hlines by setting the corresponding element to nil
  7453. ;; At the same time, we remove trailing space.
  7454. (setq lines (mapcar (lambda (l)
  7455. (if (string-match "^ *|-" l)
  7456. nil
  7457. (if (string-match "[ \t]+$" l)
  7458. (substring l 0 (match-beginning 0))
  7459. l)))
  7460. lines))
  7461. ;; Get the data fields by splitting the lines.
  7462. (setq fields (mapcar
  7463. (lambda (l)
  7464. (org-split-string l " *| *"))
  7465. (delq nil (copy-sequence lines))))
  7466. ;; How many fields in the longest line?
  7467. (condition-case nil
  7468. (setq maxfields (apply 'max (mapcar 'length fields)))
  7469. (error
  7470. (kill-region beg end)
  7471. (org-table-create org-table-default-size)
  7472. (error "Empty table - created default table")))
  7473. ;; A list of empty strings to fill any short rows on output
  7474. (setq emptystrings (make-list maxfields ""))
  7475. ;; Check for special formatting.
  7476. (setq i -1)
  7477. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7478. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7479. ;; Check if there is an explicit width specified
  7480. (when narrow
  7481. (setq c column fmax nil)
  7482. (while c
  7483. (setq e (pop c))
  7484. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7485. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7486. ;; Find fields that are wider than fmax, and shorten them
  7487. (when fmax
  7488. (loop for xx in column do
  7489. (when (and (stringp xx)
  7490. (> (org-string-width xx) fmax))
  7491. (org-add-props xx nil
  7492. 'help-echo
  7493. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7494. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7495. (unless (> f1 1)
  7496. (error "Cannot narrow field starting with wide link \"%s\""
  7497. (match-string 0 xx)))
  7498. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7499. (add-text-properties (- f1 2) f1
  7500. (list 'display org-narrow-column-arrow)
  7501. xx)))))
  7502. ;; Get the maximum width for each column
  7503. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7504. ;; Get the fraction of numbers, to decide about alignment of the column
  7505. (setq cnt 0 frac 0.0)
  7506. (loop for x in column do
  7507. (if (equal x "")
  7508. nil
  7509. (setq frac ( / (+ (* frac cnt)
  7510. (if (string-match org-table-number-regexp x) 1 0))
  7511. (setq cnt (1+ cnt))))))
  7512. (push (>= frac org-table-number-fraction) typenums))
  7513. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7514. ;; Store the alignment of this table, for later editing of single fields
  7515. (setq org-table-last-alignment typenums
  7516. org-table-last-column-widths lengths)
  7517. ;; With invisible characters, `format' does not get the field width right
  7518. ;; So we need to make these fields wide by hand.
  7519. (when (or links emph)
  7520. (loop for i from 0 upto (1- maxfields) do
  7521. (setq len (nth i lengths))
  7522. (loop for j from 0 upto (1- (length fields)) do
  7523. (setq c (nthcdr i (car (nthcdr j fields))))
  7524. (if (and (stringp (car c))
  7525. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7526. ; (string-match org-bracket-link-regexp (car c))
  7527. (< (org-string-width (car c)) len))
  7528. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7529. ;; Compute the formats needed for output of the table
  7530. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7531. (while (setq l (pop lengths))
  7532. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7533. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7534. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7535. (setq rfmt (concat rfmt "\n")
  7536. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7537. (setq new (mapconcat
  7538. (lambda (l)
  7539. (if l (apply 'format rfmt
  7540. (append (pop fields) emptystrings))
  7541. hfmt))
  7542. lines ""))
  7543. ;; Replace the old one
  7544. (delete-region beg end)
  7545. (move-marker end nil)
  7546. (move-marker org-table-aligned-begin-marker (point))
  7547. (insert new)
  7548. (move-marker org-table-aligned-end-marker (point))
  7549. (when (and orgtbl-mode (not (org-mode-p)))
  7550. (goto-char org-table-aligned-begin-marker)
  7551. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7552. ;; Try to move to the old location
  7553. (goto-line winstartline)
  7554. (setq winstart (point-at-bol))
  7555. (goto-line linepos)
  7556. (set-window-start (selected-window) winstart 'noforce)
  7557. (org-table-goto-column colpos)
  7558. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7559. (setq org-table-may-need-update nil)
  7560. ))
  7561. (defun org-string-width (s)
  7562. "Compute width of string, ignoring invisible characters.
  7563. This ignores character with invisibility property `org-link', and also
  7564. characters with property `org-cwidth', because these will become invisible
  7565. upon the next fontification round."
  7566. (let (b l)
  7567. (when (or (eq t buffer-invisibility-spec)
  7568. (assq 'org-link buffer-invisibility-spec))
  7569. (while (setq b (text-property-any 0 (length s)
  7570. 'invisible 'org-link s))
  7571. (setq s (concat (substring s 0 b)
  7572. (substring s (or (next-single-property-change
  7573. b 'invisible s) (length s)))))))
  7574. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7575. (setq s (concat (substring s 0 b)
  7576. (substring s (or (next-single-property-change
  7577. b 'org-cwidth s) (length s))))))
  7578. (setq l (string-width s) b -1)
  7579. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7580. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7581. l))
  7582. (defun org-table-begin (&optional table-type)
  7583. "Find the beginning of the table and return its position.
  7584. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7585. (save-excursion
  7586. (if (not (re-search-backward
  7587. (if table-type org-table-any-border-regexp
  7588. org-table-border-regexp)
  7589. nil t))
  7590. (progn (goto-char (point-min)) (point))
  7591. (goto-char (match-beginning 0))
  7592. (beginning-of-line 2)
  7593. (point))))
  7594. (defun org-table-end (&optional table-type)
  7595. "Find the end of the table and return its position.
  7596. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7597. (save-excursion
  7598. (if (not (re-search-forward
  7599. (if table-type org-table-any-border-regexp
  7600. org-table-border-regexp)
  7601. nil t))
  7602. (goto-char (point-max))
  7603. (goto-char (match-beginning 0)))
  7604. (point-marker)))
  7605. (defun org-table-justify-field-maybe (&optional new)
  7606. "Justify the current field, text to left, number to right.
  7607. Optional argument NEW may specify text to replace the current field content."
  7608. (cond
  7609. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7610. ((org-at-table-hline-p))
  7611. ((and (not new)
  7612. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7613. (current-buffer)))
  7614. (< (point) org-table-aligned-begin-marker)
  7615. (>= (point) org-table-aligned-end-marker)))
  7616. ;; This is not the same table, force a full re-align
  7617. (setq org-table-may-need-update t))
  7618. (t ;; realign the current field, based on previous full realign
  7619. (let* ((pos (point)) s
  7620. (col (org-table-current-column))
  7621. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7622. l f n o e)
  7623. (when (> col 0)
  7624. (skip-chars-backward "^|\n")
  7625. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7626. (progn
  7627. (setq s (match-string 1)
  7628. o (match-string 0)
  7629. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7630. e (not (= (match-beginning 2) (match-end 2))))
  7631. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7632. l (if e "|" (setq org-table-may-need-update t) ""))
  7633. n (format f s))
  7634. (if new
  7635. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7636. (setq n (format f new))
  7637. (setq n (concat new "|") org-table-may-need-update t)))
  7638. (or (equal n o)
  7639. (let (org-table-may-need-update)
  7640. (replace-match n t t))))
  7641. (setq org-table-may-need-update t))
  7642. (goto-char pos))))))
  7643. (defun org-table-next-field ()
  7644. "Go to the next field in the current table, creating new lines as needed.
  7645. Before doing so, re-align the table if necessary."
  7646. (interactive)
  7647. (org-table-maybe-eval-formula)
  7648. (org-table-maybe-recalculate-line)
  7649. (if (and org-table-automatic-realign
  7650. org-table-may-need-update)
  7651. (org-table-align))
  7652. (let ((end (org-table-end)))
  7653. (if (org-at-table-hline-p)
  7654. (end-of-line 1))
  7655. (condition-case nil
  7656. (progn
  7657. (re-search-forward "|" end)
  7658. (if (looking-at "[ \t]*$")
  7659. (re-search-forward "|" end))
  7660. (if (and (looking-at "-")
  7661. org-table-tab-jumps-over-hlines
  7662. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7663. (goto-char (match-beginning 1)))
  7664. (if (looking-at "-")
  7665. (progn
  7666. (beginning-of-line 0)
  7667. (org-table-insert-row 'below))
  7668. (if (looking-at " ") (forward-char 1))))
  7669. (error
  7670. (org-table-insert-row 'below)))))
  7671. (defun org-table-previous-field ()
  7672. "Go to the previous field in the table.
  7673. Before doing so, re-align the table if necessary."
  7674. (interactive)
  7675. (org-table-justify-field-maybe)
  7676. (org-table-maybe-recalculate-line)
  7677. (if (and org-table-automatic-realign
  7678. org-table-may-need-update)
  7679. (org-table-align))
  7680. (if (org-at-table-hline-p)
  7681. (end-of-line 1))
  7682. (re-search-backward "|" (org-table-begin))
  7683. (re-search-backward "|" (org-table-begin))
  7684. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7685. (re-search-backward "|" (org-table-begin)))
  7686. (if (looking-at "| ?")
  7687. (goto-char (match-end 0))))
  7688. (defun org-table-next-row ()
  7689. "Go to the next row (same column) in the current table.
  7690. Before doing so, re-align the table if necessary."
  7691. (interactive)
  7692. (org-table-maybe-eval-formula)
  7693. (org-table-maybe-recalculate-line)
  7694. (if (or (looking-at "[ \t]*$")
  7695. (save-excursion (skip-chars-backward " \t") (bolp)))
  7696. (newline)
  7697. (if (and org-table-automatic-realign
  7698. org-table-may-need-update)
  7699. (org-table-align))
  7700. (let ((col (org-table-current-column)))
  7701. (beginning-of-line 2)
  7702. (if (or (not (org-at-table-p))
  7703. (org-at-table-hline-p))
  7704. (progn
  7705. (beginning-of-line 0)
  7706. (org-table-insert-row 'below)))
  7707. (org-table-goto-column col)
  7708. (skip-chars-backward "^|\n\r")
  7709. (if (looking-at " ") (forward-char 1)))))
  7710. (defun org-table-copy-down (n)
  7711. "Copy a field down in the current column.
  7712. If the field at the cursor is empty, copy into it the content of the nearest
  7713. non-empty field above. With argument N, use the Nth non-empty field.
  7714. If the current field is not empty, it is copied down to the next row, and
  7715. the cursor is moved with it. Therefore, repeating this command causes the
  7716. column to be filled row-by-row.
  7717. If the variable `org-table-copy-increment' is non-nil and the field is an
  7718. integer or a timestamp, it will be incremented while copying. In the case of
  7719. a timestamp, if the cursor is on the year, change the year. If it is on the
  7720. month or the day, change that. Point will stay on the current date field
  7721. in order to easily repeat the interval."
  7722. (interactive "p")
  7723. (let* ((colpos (org-table-current-column))
  7724. (col (current-column))
  7725. (field (org-table-get-field))
  7726. (non-empty (string-match "[^ \t]" field))
  7727. (beg (org-table-begin))
  7728. txt)
  7729. (org-table-check-inside-data-field)
  7730. (if non-empty
  7731. (progn
  7732. (setq txt (org-trim field))
  7733. (org-table-next-row)
  7734. (org-table-blank-field))
  7735. (save-excursion
  7736. (setq txt
  7737. (catch 'exit
  7738. (while (progn (beginning-of-line 1)
  7739. (re-search-backward org-table-dataline-regexp
  7740. beg t))
  7741. (org-table-goto-column colpos t)
  7742. (if (and (looking-at
  7743. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  7744. (= (setq n (1- n)) 0))
  7745. (throw 'exit (match-string 1))))))))
  7746. (if txt
  7747. (progn
  7748. (if (and org-table-copy-increment
  7749. (string-match "^[0-9]+$" txt))
  7750. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  7751. (insert txt)
  7752. (move-to-column col)
  7753. (if (and org-table-copy-increment (org-at-timestamp-p t))
  7754. (org-timestamp-up 1)
  7755. (org-table-maybe-recalculate-line))
  7756. (org-table-align)
  7757. (move-to-column col))
  7758. (error "No non-empty field found"))))
  7759. (defun org-table-check-inside-data-field ()
  7760. "Is point inside a table data field?
  7761. I.e. not on a hline or before the first or after the last column?
  7762. This actually throws an error, so it aborts the current command."
  7763. (if (or (not (org-at-table-p))
  7764. (= (org-table-current-column) 0)
  7765. (org-at-table-hline-p)
  7766. (looking-at "[ \t]*$"))
  7767. (error "Not in table data field")))
  7768. (defvar org-table-clip nil
  7769. "Clipboard for table regions.")
  7770. (defun org-table-blank-field ()
  7771. "Blank the current table field or active region."
  7772. (interactive)
  7773. (org-table-check-inside-data-field)
  7774. (if (and (interactive-p) (org-region-active-p))
  7775. (let (org-table-clip)
  7776. (org-table-cut-region (region-beginning) (region-end)))
  7777. (skip-chars-backward "^|")
  7778. (backward-char 1)
  7779. (if (looking-at "|[^|\n]+")
  7780. (let* ((pos (match-beginning 0))
  7781. (match (match-string 0))
  7782. (len (org-string-width match)))
  7783. (replace-match (concat "|" (make-string (1- len) ?\ )))
  7784. (goto-char (+ 2 pos))
  7785. (substring match 1)))))
  7786. (defun org-table-get-field (&optional n replace)
  7787. "Return the value of the field in column N of current row.
  7788. N defaults to current field.
  7789. If REPLACE is a string, replace field with this value. The return value
  7790. is always the old value."
  7791. (and n (org-table-goto-column n))
  7792. (skip-chars-backward "^|\n")
  7793. (backward-char 1)
  7794. (if (looking-at "|[^|\r\n]*")
  7795. (let* ((pos (match-beginning 0))
  7796. (val (buffer-substring (1+ pos) (match-end 0))))
  7797. (if replace
  7798. (replace-match (concat "|" replace) t t))
  7799. (goto-char (min (point-at-eol) (+ 2 pos)))
  7800. val)
  7801. (forward-char 1) ""))
  7802. (defun org-table-field-info (arg)
  7803. "Show info about the current field, and highlight any reference at point."
  7804. (interactive "P")
  7805. (org-table-get-specials)
  7806. (save-excursion
  7807. (let* ((pos (point))
  7808. (col (org-table-current-column))
  7809. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  7810. (name (car (rassoc (list (org-current-line) col)
  7811. org-table-named-field-locations)))
  7812. (eql (org-table-get-stored-formulas))
  7813. (dline (org-table-current-dline))
  7814. (ref (format "@%d$%d" dline col))
  7815. (ref1 (org-table-convert-refs-to-an ref))
  7816. (fequation (or (assoc name eql) (assoc ref eql)))
  7817. (cequation (assoc (int-to-string col) eql))
  7818. (eqn (or fequation cequation)))
  7819. (goto-char pos)
  7820. (condition-case nil
  7821. (org-table-show-reference 'local)
  7822. (error nil))
  7823. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  7824. dline col
  7825. (if cname (concat " or $" cname) "")
  7826. dline col ref1
  7827. (if name (concat " or $" name) "")
  7828. ;; FIXME: formula info not correct if special table line
  7829. (if eqn
  7830. (concat ", formula: "
  7831. (org-table-formula-to-user
  7832. (concat
  7833. (if (string-match "^[$@]"(car eqn)) "" "$")
  7834. (car eqn) "=" (cdr eqn))))
  7835. "")))))
  7836. (defun org-table-current-column ()
  7837. "Find out which column we are in.
  7838. When called interactively, column is also displayed in echo area."
  7839. (interactive)
  7840. (if (interactive-p) (org-table-check-inside-data-field))
  7841. (save-excursion
  7842. (let ((cnt 0) (pos (point)))
  7843. (beginning-of-line 1)
  7844. (while (search-forward "|" pos t)
  7845. (setq cnt (1+ cnt)))
  7846. (if (interactive-p) (message "This is table column %d" cnt))
  7847. cnt)))
  7848. (defun org-table-current-dline ()
  7849. "Find out what table data line we are in.
  7850. Only datalins count for this."
  7851. (interactive)
  7852. (if (interactive-p) (org-table-check-inside-data-field))
  7853. (save-excursion
  7854. (let ((cnt 0) (pos (point)))
  7855. (goto-char (org-table-begin))
  7856. (while (<= (point) pos)
  7857. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  7858. (beginning-of-line 2))
  7859. (if (interactive-p) (message "This is table line %d" cnt))
  7860. cnt)))
  7861. (defun org-table-goto-column (n &optional on-delim force)
  7862. "Move the cursor to the Nth column in the current table line.
  7863. With optional argument ON-DELIM, stop with point before the left delimiter
  7864. of the field.
  7865. If there are less than N fields, just go to after the last delimiter.
  7866. However, when FORCE is non-nil, create new columns if necessary."
  7867. (interactive "p")
  7868. (let ((pos (point-at-eol)))
  7869. (beginning-of-line 1)
  7870. (when (> n 0)
  7871. (while (and (> (setq n (1- n)) -1)
  7872. (or (search-forward "|" pos t)
  7873. (and force
  7874. (progn (end-of-line 1)
  7875. (skip-chars-backward "^|")
  7876. (insert " | "))))))
  7877. ; (backward-char 2) t)))))
  7878. (when (and force (not (looking-at ".*|")))
  7879. (save-excursion (end-of-line 1) (insert " | ")))
  7880. (if on-delim
  7881. (backward-char 1)
  7882. (if (looking-at " ") (forward-char 1))))))
  7883. (defun org-at-table-p (&optional table-type)
  7884. "Return t if the cursor is inside an org-type table.
  7885. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  7886. (if org-enable-table-editor
  7887. (save-excursion
  7888. (beginning-of-line 1)
  7889. (looking-at (if table-type org-table-any-line-regexp
  7890. org-table-line-regexp)))
  7891. nil))
  7892. (defun org-at-table.el-p ()
  7893. "Return t if and only if we are at a table.el table."
  7894. (and (org-at-table-p 'any)
  7895. (save-excursion
  7896. (goto-char (org-table-begin 'any))
  7897. (looking-at org-table1-hline-regexp))))
  7898. (defun org-table-recognize-table.el ()
  7899. "If there is a table.el table nearby, recognize it and move into it."
  7900. (if org-table-tab-recognizes-table.el
  7901. (if (org-at-table.el-p)
  7902. (progn
  7903. (beginning-of-line 1)
  7904. (if (looking-at org-table-dataline-regexp)
  7905. nil
  7906. (if (looking-at org-table1-hline-regexp)
  7907. (progn
  7908. (beginning-of-line 2)
  7909. (if (looking-at org-table-any-border-regexp)
  7910. (beginning-of-line -1)))))
  7911. (if (re-search-forward "|" (org-table-end t) t)
  7912. (progn
  7913. (require 'table)
  7914. (if (table--at-cell-p (point))
  7915. t
  7916. (message "recognizing table.el table...")
  7917. (table-recognize-table)
  7918. (message "recognizing table.el table...done")))
  7919. (error "This should not happen..."))
  7920. t)
  7921. nil)
  7922. nil))
  7923. (defun org-at-table-hline-p ()
  7924. "Return t if the cursor is inside a hline in a table."
  7925. (if org-enable-table-editor
  7926. (save-excursion
  7927. (beginning-of-line 1)
  7928. (looking-at org-table-hline-regexp))
  7929. nil))
  7930. (defun org-table-insert-column ()
  7931. "Insert a new column into the table."
  7932. (interactive)
  7933. (if (not (org-at-table-p))
  7934. (error "Not at a table"))
  7935. (org-table-find-dataline)
  7936. (let* ((col (max 1 (org-table-current-column)))
  7937. (beg (org-table-begin))
  7938. (end (org-table-end))
  7939. ;; Current cursor position
  7940. (linepos (org-current-line))
  7941. (colpos col))
  7942. (goto-char beg)
  7943. (while (< (point) end)
  7944. (if (org-at-table-hline-p)
  7945. nil
  7946. (org-table-goto-column col t)
  7947. (insert "| "))
  7948. (beginning-of-line 2))
  7949. (move-marker end nil)
  7950. (goto-line linepos)
  7951. (org-table-goto-column colpos)
  7952. (org-table-align)
  7953. (org-table-fix-formulas "$" nil (1- col) 1)))
  7954. (defun org-table-find-dataline ()
  7955. "Find a dataline in the current table, which is needed for column commands."
  7956. (if (and (org-at-table-p)
  7957. (not (org-at-table-hline-p)))
  7958. t
  7959. (let ((col (current-column))
  7960. (end (org-table-end)))
  7961. (move-to-column col)
  7962. (while (and (< (point) end)
  7963. (or (not (= (current-column) col))
  7964. (org-at-table-hline-p)))
  7965. (beginning-of-line 2)
  7966. (move-to-column col))
  7967. (if (and (org-at-table-p)
  7968. (not (org-at-table-hline-p)))
  7969. t
  7970. (error
  7971. "Please position cursor in a data line for column operations")))))
  7972. (defun org-table-delete-column ()
  7973. "Delete a column from the table."
  7974. (interactive)
  7975. (if (not (org-at-table-p))
  7976. (error "Not at a table"))
  7977. (org-table-find-dataline)
  7978. (org-table-check-inside-data-field)
  7979. (let* ((col (org-table-current-column))
  7980. (beg (org-table-begin))
  7981. (end (org-table-end))
  7982. ;; Current cursor position
  7983. (linepos (org-current-line))
  7984. (colpos col))
  7985. (goto-char beg)
  7986. (while (< (point) end)
  7987. (if (org-at-table-hline-p)
  7988. nil
  7989. (org-table-goto-column col t)
  7990. (and (looking-at "|[^|\n]+|")
  7991. (replace-match "|")))
  7992. (beginning-of-line 2))
  7993. (move-marker end nil)
  7994. (goto-line linepos)
  7995. (org-table-goto-column colpos)
  7996. (org-table-align)
  7997. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  7998. col -1 col)))
  7999. (defun org-table-move-column-right ()
  8000. "Move column to the right."
  8001. (interactive)
  8002. (org-table-move-column nil))
  8003. (defun org-table-move-column-left ()
  8004. "Move column to the left."
  8005. (interactive)
  8006. (org-table-move-column 'left))
  8007. (defun org-table-move-column (&optional left)
  8008. "Move the current column to the right. With arg LEFT, move to the left."
  8009. (interactive "P")
  8010. (if (not (org-at-table-p))
  8011. (error "Not at a table"))
  8012. (org-table-find-dataline)
  8013. (org-table-check-inside-data-field)
  8014. (let* ((col (org-table-current-column))
  8015. (col1 (if left (1- col) col))
  8016. (beg (org-table-begin))
  8017. (end (org-table-end))
  8018. ;; Current cursor position
  8019. (linepos (org-current-line))
  8020. (colpos (if left (1- col) (1+ col))))
  8021. (if (and left (= col 1))
  8022. (error "Cannot move column further left"))
  8023. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8024. (error "Cannot move column further right"))
  8025. (goto-char beg)
  8026. (while (< (point) end)
  8027. (if (org-at-table-hline-p)
  8028. nil
  8029. (org-table-goto-column col1 t)
  8030. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8031. (replace-match "|\\2|\\1|")))
  8032. (beginning-of-line 2))
  8033. (move-marker end nil)
  8034. (goto-line linepos)
  8035. (org-table-goto-column colpos)
  8036. (org-table-align)
  8037. (org-table-fix-formulas
  8038. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8039. (cons (number-to-string colpos) (number-to-string col))))))
  8040. (defun org-table-move-row-down ()
  8041. "Move table row down."
  8042. (interactive)
  8043. (org-table-move-row nil))
  8044. (defun org-table-move-row-up ()
  8045. "Move table row up."
  8046. (interactive)
  8047. (org-table-move-row 'up))
  8048. (defun org-table-move-row (&optional up)
  8049. "Move the current table line down. With arg UP, move it up."
  8050. (interactive "P")
  8051. (let* ((col (current-column))
  8052. (pos (point))
  8053. (hline1p (save-excursion (beginning-of-line 1)
  8054. (looking-at org-table-hline-regexp)))
  8055. (dline1 (org-table-current-dline))
  8056. (dline2 (+ dline1 (if up -1 1)))
  8057. (tonew (if up 0 2))
  8058. txt hline2p)
  8059. (beginning-of-line tonew)
  8060. (unless (org-at-table-p)
  8061. (goto-char pos)
  8062. (error "Cannot move row further"))
  8063. (setq hline2p (looking-at org-table-hline-regexp))
  8064. (goto-char pos)
  8065. (beginning-of-line 1)
  8066. (setq pos (point))
  8067. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8068. (delete-region (point) (1+ (point-at-eol)))
  8069. (beginning-of-line tonew)
  8070. (insert txt)
  8071. (beginning-of-line 0)
  8072. (move-to-column col)
  8073. (unless (or hline1p hline2p)
  8074. (org-table-fix-formulas
  8075. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8076. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8077. (defun org-table-insert-row (&optional arg)
  8078. "Insert a new row above the current line into the table.
  8079. With prefix ARG, insert below the current line."
  8080. (interactive "P")
  8081. (if (not (org-at-table-p))
  8082. (error "Not at a table"))
  8083. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8084. (new (org-table-clean-line line)))
  8085. ;; Fix the first field if necessary
  8086. (if (string-match "^[ \t]*| *[#$] *|" line)
  8087. (setq new (replace-match (match-string 0 line) t t new)))
  8088. (beginning-of-line (if arg 2 1))
  8089. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8090. (beginning-of-line 0)
  8091. (re-search-forward "| ?" (point-at-eol) t)
  8092. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8093. (org-table-align))
  8094. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8095. (defun org-table-insert-hline (&optional above)
  8096. "Insert a horizontal-line below the current line into the table.
  8097. With prefix ABOVE, insert above the current line."
  8098. (interactive "P")
  8099. (if (not (org-at-table-p))
  8100. (error "Not at a table"))
  8101. (let ((line (org-table-clean-line
  8102. (buffer-substring (point-at-bol) (point-at-eol))))
  8103. (col (current-column)))
  8104. (while (string-match "|\\( +\\)|" line)
  8105. (setq line (replace-match
  8106. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8107. ?-) "|") t t line)))
  8108. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8109. (beginning-of-line (if above 1 2))
  8110. (insert line "\n")
  8111. (beginning-of-line (if above 1 -1))
  8112. (move-to-column col)
  8113. (and org-table-overlay-coordinates (org-table-align))))
  8114. (defun org-table-hline-and-move (&optional same-column)
  8115. "Insert a hline and move to the row below that line."
  8116. (interactive "P")
  8117. (let ((col (org-table-current-column)))
  8118. (org-table-maybe-eval-formula)
  8119. (org-table-maybe-recalculate-line)
  8120. (org-table-insert-hline)
  8121. (end-of-line 2)
  8122. (if (looking-at "\n[ \t]*|-")
  8123. (progn (insert "\n|") (org-table-align))
  8124. (org-table-next-field))
  8125. (if same-column (org-table-goto-column col))))
  8126. (defun org-table-clean-line (s)
  8127. "Convert a table line S into a string with only \"|\" and space.
  8128. In particular, this does handle wide and invisible characters."
  8129. (if (string-match "^[ \t]*|-" s)
  8130. ;; It's a hline, just map the characters
  8131. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8132. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8133. (setq s (replace-match
  8134. (concat "|" (make-string (org-string-width (match-string 1 s))
  8135. ?\ ) "|")
  8136. t t s)))
  8137. s))
  8138. (defun org-table-kill-row ()
  8139. "Delete the current row or horizontal line from the table."
  8140. (interactive)
  8141. (if (not (org-at-table-p))
  8142. (error "Not at a table"))
  8143. (let ((col (current-column))
  8144. (dline (org-table-current-dline)))
  8145. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8146. (if (not (org-at-table-p)) (beginning-of-line 0))
  8147. (move-to-column col)
  8148. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8149. dline -1 dline)))
  8150. (defun org-table-sort-lines (with-case &optional sorting-type)
  8151. "Sort table lines according to the column at point.
  8152. The position of point indicates the column to be used for
  8153. sorting, and the range of lines is the range between the nearest
  8154. horizontal separator lines, or the entire table of no such lines
  8155. exist. If point is before the first column, you will be prompted
  8156. for the sorting column. If there is an active region, the mark
  8157. specifies the first line and the sorting column, while point
  8158. should be in the last line to be included into the sorting.
  8159. The command then prompts for the sorting type which can be
  8160. alphabetically, numerically, or by time (as given in a time stamp
  8161. in the field). Sorting in reverse order is also possible.
  8162. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8163. If SORTING-TYPE is specified when this function is called from a Lisp
  8164. program, no prompting will take place. SORTING-TYPE must be a character,
  8165. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8166. should be done in reverse order."
  8167. (interactive "P")
  8168. (let* ((thisline (org-current-line))
  8169. (thiscol (org-table-current-column))
  8170. beg end bcol ecol tend tbeg column lns pos)
  8171. (when (equal thiscol 0)
  8172. (if (interactive-p)
  8173. (setq thiscol
  8174. (string-to-number
  8175. (read-string "Use column N for sorting: ")))
  8176. (setq thiscol 1))
  8177. (org-table-goto-column thiscol))
  8178. (org-table-check-inside-data-field)
  8179. (if (org-region-active-p)
  8180. (progn
  8181. (setq beg (region-beginning) end (region-end))
  8182. (goto-char beg)
  8183. (setq column (org-table-current-column)
  8184. beg (point-at-bol))
  8185. (goto-char end)
  8186. (setq end (point-at-bol 2)))
  8187. (setq column (org-table-current-column)
  8188. pos (point)
  8189. tbeg (org-table-begin)
  8190. tend (org-table-end))
  8191. (if (re-search-backward org-table-hline-regexp tbeg t)
  8192. (setq beg (point-at-bol 2))
  8193. (goto-char tbeg)
  8194. (setq beg (point-at-bol 1)))
  8195. (goto-char pos)
  8196. (if (re-search-forward org-table-hline-regexp tend t)
  8197. (setq end (point-at-bol 1))
  8198. (goto-char tend)
  8199. (setq end (point-at-bol))))
  8200. (setq beg (move-marker (make-marker) beg)
  8201. end (move-marker (make-marker) end))
  8202. (untabify beg end)
  8203. (goto-char beg)
  8204. (org-table-goto-column column)
  8205. (skip-chars-backward "^|")
  8206. (setq bcol (current-column))
  8207. (org-table-goto-column (1+ column))
  8208. (skip-chars-backward "^|")
  8209. (setq ecol (1- (current-column)))
  8210. (org-table-goto-column column)
  8211. (setq lns (mapcar (lambda(x) (cons
  8212. (org-sort-remove-invisible
  8213. (nth (1- column)
  8214. (org-split-string x "[ \t]*|[ \t]*")))
  8215. x))
  8216. (org-split-string (buffer-substring beg end) "\n")))
  8217. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8218. (delete-region beg end)
  8219. (move-marker beg nil)
  8220. (move-marker end nil)
  8221. (insert (mapconcat 'cdr lns "\n") "\n")
  8222. (goto-line thisline)
  8223. (org-table-goto-column thiscol)
  8224. (message "%d lines sorted, based on column %d" (length lns) column)))
  8225. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8226. (defun org-sort-remove-invisible (s)
  8227. (remove-text-properties 0 (length s) org-rm-props s)
  8228. (while (string-match org-bracket-link-regexp s)
  8229. (setq s (replace-match (if (match-end 2)
  8230. (match-string 3 s)
  8231. (match-string 1 s)) t t s)))
  8232. s)
  8233. (defun org-table-cut-region (beg end)
  8234. "Copy region in table to the clipboard and blank all relevant fields."
  8235. (interactive "r")
  8236. (org-table-copy-region beg end 'cut))
  8237. (defun org-table-copy-region (beg end &optional cut)
  8238. "Copy rectangular region in table to clipboard.
  8239. A special clipboard is used which can only be accessed
  8240. with `org-table-paste-rectangle'."
  8241. (interactive "rP")
  8242. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8243. region cols
  8244. (rpl (if cut " " nil)))
  8245. (goto-char beg)
  8246. (org-table-check-inside-data-field)
  8247. (setq l01 (org-current-line)
  8248. c01 (org-table-current-column))
  8249. (goto-char end)
  8250. (org-table-check-inside-data-field)
  8251. (setq l02 (org-current-line)
  8252. c02 (org-table-current-column))
  8253. (setq l1 (min l01 l02) l2 (max l01 l02)
  8254. c1 (min c01 c02) c2 (max c01 c02))
  8255. (catch 'exit
  8256. (while t
  8257. (catch 'nextline
  8258. (if (> l1 l2) (throw 'exit t))
  8259. (goto-line l1)
  8260. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8261. (setq cols nil ic1 c1 ic2 c2)
  8262. (while (< ic1 (1+ ic2))
  8263. (push (org-table-get-field ic1 rpl) cols)
  8264. (setq ic1 (1+ ic1)))
  8265. (push (nreverse cols) region)
  8266. (setq l1 (1+ l1)))))
  8267. (setq org-table-clip (nreverse region))
  8268. (if cut (org-table-align))
  8269. org-table-clip))
  8270. (defun org-table-paste-rectangle ()
  8271. "Paste a rectangular region into a table.
  8272. The upper right corner ends up in the current field. All involved fields
  8273. will be overwritten. If the rectangle does not fit into the present table,
  8274. the table is enlarged as needed. The process ignores horizontal separator
  8275. lines."
  8276. (interactive)
  8277. (unless (and org-table-clip (listp org-table-clip))
  8278. (error "First cut/copy a region to paste!"))
  8279. (org-table-check-inside-data-field)
  8280. (let* ((clip org-table-clip)
  8281. (line (org-current-line))
  8282. (col (org-table-current-column))
  8283. (org-enable-table-editor t)
  8284. (org-table-automatic-realign nil)
  8285. c cols field)
  8286. (while (setq cols (pop clip))
  8287. (while (org-at-table-hline-p) (beginning-of-line 2))
  8288. (if (not (org-at-table-p))
  8289. (progn (end-of-line 0) (org-table-next-field)))
  8290. (setq c col)
  8291. (while (setq field (pop cols))
  8292. (org-table-goto-column c nil 'force)
  8293. (org-table-get-field nil field)
  8294. (setq c (1+ c)))
  8295. (beginning-of-line 2))
  8296. (goto-line line)
  8297. (org-table-goto-column col)
  8298. (org-table-align)))
  8299. (defun org-table-convert ()
  8300. "Convert from `org-mode' table to table.el and back.
  8301. Obviously, this only works within limits. When an Org-mode table is
  8302. converted to table.el, all horizontal separator lines get lost, because
  8303. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8304. A table.el table can be converted to an Org-mode table only if it does not
  8305. do row or column spanning. Multiline cells will become multiple cells.
  8306. Beware, Org-mode does not test if the table can be successfully converted - it
  8307. blindly applies a recipe that works for simple tables."
  8308. (interactive)
  8309. (require 'table)
  8310. (if (org-at-table.el-p)
  8311. ;; convert to Org-mode table
  8312. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8313. (end (move-marker (make-marker) (org-table-end t))))
  8314. (table-unrecognize-region beg end)
  8315. (goto-char beg)
  8316. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8317. (replace-match ""))
  8318. (goto-char beg))
  8319. (if (org-at-table-p)
  8320. ;; convert to table.el table
  8321. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8322. (end (move-marker (make-marker) (org-table-end))))
  8323. ;; first, get rid of all horizontal lines
  8324. (goto-char beg)
  8325. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8326. (replace-match ""))
  8327. ;; insert a hline before first
  8328. (goto-char beg)
  8329. (org-table-insert-hline 'above)
  8330. (beginning-of-line -1)
  8331. ;; insert a hline after each line
  8332. (while (progn (beginning-of-line 3) (< (point) end))
  8333. (org-table-insert-hline))
  8334. (goto-char beg)
  8335. (setq end (move-marker end (org-table-end)))
  8336. ;; replace "+" at beginning and ending of hlines
  8337. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8338. (replace-match "\\1+-"))
  8339. (goto-char beg)
  8340. (while (re-search-forward "-|[ \t]*$" end t)
  8341. (replace-match "-+"))
  8342. (goto-char beg)))))
  8343. (defun org-table-wrap-region (arg)
  8344. "Wrap several fields in a column like a paragraph.
  8345. This is useful if you'd like to spread the contents of a field over several
  8346. lines, in order to keep the table compact.
  8347. If there is an active region, and both point and mark are in the same column,
  8348. the text in the column is wrapped to minimum width for the given number of
  8349. lines. Generally, this makes the table more compact. A prefix ARG may be
  8350. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8351. formats the selected text to two lines. If the region was longer than two
  8352. lines, the remaining lines remain empty. A negative prefix argument reduces
  8353. the current number of lines by that amount. The wrapped text is pasted back
  8354. into the table. If you formatted it to more lines than it was before, fields
  8355. further down in the table get overwritten - so you might need to make space in
  8356. the table first.
  8357. If there is no region, the current field is split at the cursor position and
  8358. the text fragment to the right of the cursor is prepended to the field one
  8359. line down.
  8360. If there is no region, but you specify a prefix ARG, the current field gets
  8361. blank, and the content is appended to the field above."
  8362. (interactive "P")
  8363. (org-table-check-inside-data-field)
  8364. (if (org-region-active-p)
  8365. ;; There is a region: fill as a paragraph
  8366. (let* ((beg (region-beginning))
  8367. (cline (save-excursion (goto-char beg) (org-current-line)))
  8368. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8369. nlines)
  8370. (org-table-cut-region (region-beginning) (region-end))
  8371. (if (> (length (car org-table-clip)) 1)
  8372. (error "Region must be limited to single column"))
  8373. (setq nlines (if arg
  8374. (if (< arg 1)
  8375. (+ (length org-table-clip) arg)
  8376. arg)
  8377. (length org-table-clip)))
  8378. (setq org-table-clip
  8379. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8380. nil nlines)))
  8381. (goto-line cline)
  8382. (org-table-goto-column ccol)
  8383. (org-table-paste-rectangle))
  8384. ;; No region, split the current field at point
  8385. (if arg
  8386. ;; combine with field above
  8387. (let ((s (org-table-blank-field))
  8388. (col (org-table-current-column)))
  8389. (beginning-of-line 0)
  8390. (while (org-at-table-hline-p) (beginning-of-line 0))
  8391. (org-table-goto-column col)
  8392. (skip-chars-forward "^|")
  8393. (skip-chars-backward " ")
  8394. (insert " " (org-trim s))
  8395. (org-table-align))
  8396. ;; split field
  8397. (when (looking-at "\\([^|]+\\)+|")
  8398. (let ((s (match-string 1)))
  8399. (replace-match " |")
  8400. (goto-char (match-beginning 0))
  8401. (org-table-next-row)
  8402. (insert (org-trim s) " ")
  8403. (org-table-align))))))
  8404. (defvar org-field-marker nil)
  8405. (defun org-table-edit-field (arg)
  8406. "Edit table field in a different window.
  8407. This is mainly useful for fields that contain hidden parts.
  8408. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8409. it can be edited in place."
  8410. (interactive "P")
  8411. (if arg
  8412. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8413. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8414. (remove-text-properties b e '(org-cwidth t invisible t
  8415. display t intangible t))
  8416. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8417. (font-lock-fontify-block)))
  8418. (let ((pos (move-marker (make-marker) (point)))
  8419. (field (org-table-get-field))
  8420. (cw (current-window-configuration))
  8421. p)
  8422. (org-switch-to-buffer-other-window "*Org tmp*")
  8423. (erase-buffer)
  8424. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8425. (let ((org-inhibit-startup t)) (org-mode))
  8426. (goto-char (setq p (point-max)))
  8427. (insert (org-trim field))
  8428. (remove-text-properties p (point-max)
  8429. '(invisible t org-cwidth t display t
  8430. intangible t))
  8431. (goto-char p)
  8432. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8433. (org-set-local 'org-window-configuration cw)
  8434. (org-set-local 'org-field-marker pos)
  8435. (message "Edit and finish with C-c C-c"))))
  8436. (defun org-table-finish-edit-field ()
  8437. "Finish editing a table data field.
  8438. Remove all newline characters, insert the result into the table, realign
  8439. the table and kill the editing buffer."
  8440. (let ((pos org-field-marker)
  8441. (cw org-window-configuration)
  8442. (cb (current-buffer))
  8443. text)
  8444. (goto-char (point-min))
  8445. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8446. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8447. (replace-match " "))
  8448. (setq text (org-trim (buffer-string)))
  8449. (set-window-configuration cw)
  8450. (kill-buffer cb)
  8451. (select-window (get-buffer-window (marker-buffer pos)))
  8452. (goto-char pos)
  8453. (move-marker pos nil)
  8454. (org-table-check-inside-data-field)
  8455. (org-table-get-field nil text)
  8456. (org-table-align)
  8457. (message "New field value inserted")))
  8458. (defun org-trim (s)
  8459. "Remove whitespace at beginning and end of string."
  8460. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8461. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8462. s)
  8463. (defun org-wrap (string &optional width lines)
  8464. "Wrap string to either a number of lines, or a width in characters.
  8465. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8466. that costs. If there is a word longer than WIDTH, the text is actually
  8467. wrapped to the length of that word.
  8468. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8469. many lines, whatever width that takes.
  8470. The return value is a list of lines, without newlines at the end."
  8471. (let* ((words (org-split-string string "[ \t\n]+"))
  8472. (maxword (apply 'max (mapcar 'org-string-width words)))
  8473. w ll)
  8474. (cond (width
  8475. (org-do-wrap words (max maxword width)))
  8476. (lines
  8477. (setq w maxword)
  8478. (setq ll (org-do-wrap words maxword))
  8479. (if (<= (length ll) lines)
  8480. ll
  8481. (setq ll words)
  8482. (while (> (length ll) lines)
  8483. (setq w (1+ w))
  8484. (setq ll (org-do-wrap words w)))
  8485. ll))
  8486. (t (error "Cannot wrap this")))))
  8487. (defun org-do-wrap (words width)
  8488. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8489. (let (lines line)
  8490. (while words
  8491. (setq line (pop words))
  8492. (while (and words (< (+ (length line) (length (car words))) width))
  8493. (setq line (concat line " " (pop words))))
  8494. (setq lines (push line lines)))
  8495. (nreverse lines)))
  8496. (defun org-split-string (string &optional separators)
  8497. "Splits STRING into substrings at SEPARATORS.
  8498. No empty strings are returned if there are matches at the beginning
  8499. and end of string."
  8500. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8501. (start 0)
  8502. notfirst
  8503. (list nil))
  8504. (while (and (string-match rexp string
  8505. (if (and notfirst
  8506. (= start (match-beginning 0))
  8507. (< start (length string)))
  8508. (1+ start) start))
  8509. (< (match-beginning 0) (length string)))
  8510. (setq notfirst t)
  8511. (or (eq (match-beginning 0) 0)
  8512. (and (eq (match-beginning 0) (match-end 0))
  8513. (eq (match-beginning 0) start))
  8514. (setq list
  8515. (cons (substring string start (match-beginning 0))
  8516. list)))
  8517. (setq start (match-end 0)))
  8518. (or (eq start (length string))
  8519. (setq list
  8520. (cons (substring string start)
  8521. list)))
  8522. (nreverse list)))
  8523. (defun org-table-map-tables (function)
  8524. "Apply FUNCTION to the start of all tables in the buffer."
  8525. (save-excursion
  8526. (save-restriction
  8527. (widen)
  8528. (goto-char (point-min))
  8529. (while (re-search-forward org-table-any-line-regexp nil t)
  8530. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8531. (beginning-of-line 1)
  8532. (if (looking-at org-table-line-regexp)
  8533. (save-excursion (funcall function)))
  8534. (re-search-forward org-table-any-border-regexp nil 1))))
  8535. (message "Mapping tables: done"))
  8536. (defvar org-timecnt) ; dynamically scoped parameter
  8537. (defun org-table-sum (&optional beg end nlast)
  8538. "Sum numbers in region of current table column.
  8539. The result will be displayed in the echo area, and will be available
  8540. as kill to be inserted with \\[yank].
  8541. If there is an active region, it is interpreted as a rectangle and all
  8542. numbers in that rectangle will be summed. If there is no active
  8543. region and point is located in a table column, sum all numbers in that
  8544. column.
  8545. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8546. numbers are assumed to be times as well (in decimal hours) and the
  8547. numbers are added as such.
  8548. If NLAST is a number, only the NLAST fields will actually be summed."
  8549. (interactive)
  8550. (save-excursion
  8551. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8552. (cond
  8553. ((and beg end)) ; beg and end given explicitly
  8554. ((org-region-active-p)
  8555. (setq beg (region-beginning) end (region-end)))
  8556. (t
  8557. (setq col (org-table-current-column))
  8558. (goto-char (org-table-begin))
  8559. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8560. (error "No table data"))
  8561. (org-table-goto-column col)
  8562. (setq beg (point))
  8563. (goto-char (org-table-end))
  8564. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8565. (error "No table data"))
  8566. (org-table-goto-column col)
  8567. (setq end (point))))
  8568. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8569. (items1 (cond ((not nlast) items)
  8570. ((>= nlast (length items)) items)
  8571. (t (setq items (reverse items))
  8572. (setcdr (nthcdr (1- nlast) items) nil)
  8573. (nreverse items))))
  8574. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8575. items1)))
  8576. (res (apply '+ numbers))
  8577. (sres (if (= org-timecnt 0)
  8578. (format "%g" res)
  8579. (setq diff (* 3600 res)
  8580. h (floor (/ diff 3600)) diff (mod diff 3600)
  8581. m (floor (/ diff 60)) diff (mod diff 60)
  8582. s diff)
  8583. (format "%d:%02d:%02d" h m s))))
  8584. (kill-new sres)
  8585. (if (interactive-p)
  8586. (message "%s"
  8587. (substitute-command-keys
  8588. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8589. (length numbers) sres))))
  8590. sres))))
  8591. (defun org-table-get-number-for-summing (s)
  8592. (let (n)
  8593. (if (string-match "^ *|? *" s)
  8594. (setq s (replace-match "" nil nil s)))
  8595. (if (string-match " *|? *$" s)
  8596. (setq s (replace-match "" nil nil s)))
  8597. (setq n (string-to-number s))
  8598. (cond
  8599. ((and (string-match "0" s)
  8600. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8601. ((string-match "\\`[ \t]+\\'" s) nil)
  8602. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8603. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8604. (m (string-to-number (or (match-string 2 s) "0")))
  8605. (s (string-to-number (or (match-string 4 s) "0"))))
  8606. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8607. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8608. ((equal n 0) nil)
  8609. (t n))))
  8610. (defun org-table-current-field-formula (&optional key noerror)
  8611. "Return the formula active for the current field.
  8612. Assumes that specials are in place.
  8613. If KEY is given, return the key to this formula.
  8614. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8615. (let* ((name (car (rassoc (list (org-current-line)
  8616. (org-table-current-column))
  8617. org-table-named-field-locations)))
  8618. (col (org-table-current-column))
  8619. (scol (int-to-string col))
  8620. (ref (format "@%d$%d" (org-table-current-dline) col))
  8621. (stored-list (org-table-get-stored-formulas noerror))
  8622. (ass (or (assoc name stored-list)
  8623. (assoc ref stored-list)
  8624. (assoc scol stored-list))))
  8625. (if key
  8626. (car ass)
  8627. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8628. (cdr ass))))))
  8629. (defun org-table-get-formula (&optional equation named)
  8630. "Read a formula from the minibuffer, offer stored formula as default.
  8631. When NAMED is non-nil, look for a named equation."
  8632. (let* ((stored-list (org-table-get-stored-formulas))
  8633. (name (car (rassoc (list (org-current-line)
  8634. (org-table-current-column))
  8635. org-table-named-field-locations)))
  8636. (ref (format "@%d$%d" (org-table-current-dline)
  8637. (org-table-current-column)))
  8638. (refass (assoc ref stored-list))
  8639. (scol (if named
  8640. (if name name ref)
  8641. (int-to-string (org-table-current-column))))
  8642. (dummy (and (or name refass) (not named)
  8643. (not (y-or-n-p "Replace field formula with column formula? " ))
  8644. (error "Abort")))
  8645. (name (or name ref))
  8646. (org-table-may-need-update nil)
  8647. (stored (cdr (assoc scol stored-list)))
  8648. (eq (cond
  8649. ((and stored equation (string-match "^ *=? *$" equation))
  8650. stored)
  8651. ((stringp equation)
  8652. equation)
  8653. (t (org-table-formula-from-user
  8654. (read-string
  8655. (org-table-formula-to-user
  8656. (format "%s formula %s%s="
  8657. (if named "Field" "Column")
  8658. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8659. scol))
  8660. (if stored (org-table-formula-to-user stored) "")
  8661. 'org-table-formula-history
  8662. )))))
  8663. mustsave)
  8664. (when (not (string-match "\\S-" eq))
  8665. ;; remove formula
  8666. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8667. (org-table-store-formulas stored-list)
  8668. (error "Formula removed"))
  8669. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8670. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8671. (if (and name (not named))
  8672. ;; We set the column equation, delete the named one.
  8673. (setq stored-list (delq (assoc name stored-list) stored-list)
  8674. mustsave t))
  8675. (if stored
  8676. (setcdr (assoc scol stored-list) eq)
  8677. (setq stored-list (cons (cons scol eq) stored-list)))
  8678. (if (or mustsave (not (equal stored eq)))
  8679. (org-table-store-formulas stored-list))
  8680. eq))
  8681. (defun org-table-store-formulas (alist)
  8682. "Store the list of formulas below the current table."
  8683. (setq alist (sort alist 'org-table-formula-less-p))
  8684. (save-excursion
  8685. (goto-char (org-table-end))
  8686. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8687. (progn
  8688. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8689. (goto-char (match-beginning 2))
  8690. (delete-region (match-beginning 2) (match-end 0)))
  8691. (insert "#+TBLFM:"))
  8692. (insert " "
  8693. (mapconcat (lambda (x)
  8694. (concat
  8695. (if (equal (string-to-char (car x)) ?@) "" "$")
  8696. (car x) "=" (cdr x)))
  8697. alist "::")
  8698. "\n")))
  8699. (defsubst org-table-formula-make-cmp-string (a)
  8700. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8701. (concat
  8702. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8703. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8704. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8705. (defun org-table-formula-less-p (a b)
  8706. "Compare two formulas for sorting."
  8707. (let ((as (org-table-formula-make-cmp-string (car a)))
  8708. (bs (org-table-formula-make-cmp-string (car b))))
  8709. (and as bs (string< as bs))))
  8710. (defun org-table-get-stored-formulas (&optional noerror)
  8711. "Return an alist with the stored formulas directly after current table."
  8712. (interactive)
  8713. (let (scol eq eq-alist strings string seen)
  8714. (save-excursion
  8715. (goto-char (org-table-end))
  8716. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8717. (setq strings (org-split-string (match-string 2) " *:: *"))
  8718. (while (setq string (pop strings))
  8719. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  8720. (setq scol (if (match-end 2)
  8721. (match-string 2 string)
  8722. (match-string 1 string))
  8723. eq (match-string 3 string)
  8724. eq-alist (cons (cons scol eq) eq-alist))
  8725. (if (member scol seen)
  8726. (if noerror
  8727. (progn
  8728. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8729. (ding)
  8730. (sit-for 2))
  8731. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  8732. (push scol seen))))))
  8733. (nreverse eq-alist)))
  8734. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  8735. "Modify the equations after the table structure has been edited.
  8736. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  8737. For all numbers larger than LIMIT, shift them by DELTA."
  8738. (save-excursion
  8739. (goto-char (org-table-end))
  8740. (when (looking-at "#\\+TBLFM:")
  8741. (let ((re (concat key "\\([0-9]+\\)"))
  8742. (re2
  8743. (when remove
  8744. (if (equal key "$")
  8745. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  8746. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  8747. s n a)
  8748. (when remove
  8749. (while (re-search-forward re2 (point-at-eol) t)
  8750. (replace-match "")))
  8751. (while (re-search-forward re (point-at-eol) t)
  8752. (setq s (match-string 1) n (string-to-number s))
  8753. (cond
  8754. ((setq a (assoc s replace))
  8755. (replace-match (concat key (cdr a)) t t))
  8756. ((and limit (> n limit))
  8757. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  8758. (defun org-table-get-specials ()
  8759. "Get the column names and local parameters for this table."
  8760. (save-excursion
  8761. (let ((beg (org-table-begin)) (end (org-table-end))
  8762. names name fields fields1 field cnt
  8763. c v l line col types dlines hlines)
  8764. (setq org-table-column-names nil
  8765. org-table-local-parameters nil
  8766. org-table-named-field-locations nil
  8767. org-table-current-begin-line nil
  8768. org-table-current-begin-pos nil
  8769. org-table-current-line-types nil)
  8770. (goto-char beg)
  8771. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  8772. (setq names (org-split-string (match-string 1) " *| *")
  8773. cnt 1)
  8774. (while (setq name (pop names))
  8775. (setq cnt (1+ cnt))
  8776. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  8777. (push (cons name (int-to-string cnt)) org-table-column-names))))
  8778. (setq org-table-column-names (nreverse org-table-column-names))
  8779. (setq org-table-column-name-regexp
  8780. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  8781. (goto-char beg)
  8782. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  8783. (setq fields (org-split-string (match-string 1) " *| *"))
  8784. (while (setq field (pop fields))
  8785. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  8786. (push (cons (match-string 1 field) (match-string 2 field))
  8787. org-table-local-parameters))))
  8788. (goto-char beg)
  8789. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  8790. (setq c (match-string 1)
  8791. fields (org-split-string (match-string 2) " *| *"))
  8792. (save-excursion
  8793. (beginning-of-line (if (equal c "_") 2 0))
  8794. (setq line (org-current-line) col 1)
  8795. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  8796. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  8797. (while (and fields1 (setq field (pop fields)))
  8798. (setq v (pop fields1) col (1+ col))
  8799. (when (and (stringp field) (stringp v)
  8800. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  8801. (push (cons field v) org-table-local-parameters)
  8802. (push (list field line col) org-table-named-field-locations))))
  8803. ;; Analyse the line types
  8804. (goto-char beg)
  8805. (setq org-table-current-begin-line (org-current-line)
  8806. org-table-current-begin-pos (point)
  8807. l org-table-current-begin-line)
  8808. (while (looking-at "[ \t]*|\\(-\\)?")
  8809. (push (if (match-end 1) 'hline 'dline) types)
  8810. (if (match-end 1) (push l hlines) (push l dlines))
  8811. (beginning-of-line 2)
  8812. (setq l (1+ l)))
  8813. (setq org-table-current-line-types (apply 'vector (nreverse types))
  8814. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  8815. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  8816. (defun org-table-maybe-eval-formula ()
  8817. "Check if the current field starts with \"=\" or \":=\".
  8818. If yes, store the formula and apply it."
  8819. ;; We already know we are in a table. Get field will only return a formula
  8820. ;; when appropriate. It might return a separator line, but no problem.
  8821. (when org-table-formula-evaluate-inline
  8822. (let* ((field (org-trim (or (org-table-get-field) "")))
  8823. named eq)
  8824. (when (string-match "^:?=\\(.*\\)" field)
  8825. (setq named (equal (string-to-char field) ?:)
  8826. eq (match-string 1 field))
  8827. (if (or (fboundp 'calc-eval)
  8828. (equal (substring eq 0 (min 2 (length eq))) "'("))
  8829. (org-table-eval-formula (if named '(4) nil)
  8830. (org-table-formula-from-user eq))
  8831. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  8832. (defvar org-recalc-commands nil
  8833. "List of commands triggering the recalculation of a line.
  8834. Will be filled automatically during use.")
  8835. (defvar org-recalc-marks
  8836. '((" " . "Unmarked: no special line, no automatic recalculation")
  8837. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  8838. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  8839. ("!" . "Column name definition line. Reference in formula as $name.")
  8840. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  8841. ("_" . "Names for values in row below this one.")
  8842. ("^" . "Names for values in row above this one.")))
  8843. (defun org-table-rotate-recalc-marks (&optional newchar)
  8844. "Rotate the recalculation mark in the first column.
  8845. If in any row, the first field is not consistent with a mark,
  8846. insert a new column for the markers.
  8847. When there is an active region, change all the lines in the region,
  8848. after prompting for the marking character.
  8849. After each change, a message will be displayed indicating the meaning
  8850. of the new mark."
  8851. (interactive)
  8852. (unless (org-at-table-p) (error "Not at a table"))
  8853. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  8854. (beg (org-table-begin))
  8855. (end (org-table-end))
  8856. (l (org-current-line))
  8857. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  8858. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  8859. (have-col
  8860. (save-excursion
  8861. (goto-char beg)
  8862. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  8863. (col (org-table-current-column))
  8864. (forcenew (car (assoc newchar org-recalc-marks)))
  8865. epos new)
  8866. (when l1
  8867. (message "Change region to what mark? Type # * ! $ or SPC: ")
  8868. (setq newchar (char-to-string (read-char-exclusive))
  8869. forcenew (car (assoc newchar org-recalc-marks))))
  8870. (if (and newchar (not forcenew))
  8871. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  8872. newchar))
  8873. (if l1 (goto-line l1))
  8874. (save-excursion
  8875. (beginning-of-line 1)
  8876. (unless (looking-at org-table-dataline-regexp)
  8877. (error "Not at a table data line")))
  8878. (unless have-col
  8879. (org-table-goto-column 1)
  8880. (org-table-insert-column)
  8881. (org-table-goto-column (1+ col)))
  8882. (setq epos (point-at-eol))
  8883. (save-excursion
  8884. (beginning-of-line 1)
  8885. (org-table-get-field
  8886. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  8887. (concat " "
  8888. (setq new (or forcenew
  8889. (cadr (member (match-string 1) marks))))
  8890. " ")
  8891. " # ")))
  8892. (if (and l1 l2)
  8893. (progn
  8894. (goto-line l1)
  8895. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  8896. (and (looking-at org-table-dataline-regexp)
  8897. (org-table-get-field 1 (concat " " new " "))))
  8898. (goto-line l1)))
  8899. (if (not (= epos (point-at-eol))) (org-table-align))
  8900. (goto-line l)
  8901. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  8902. (defun org-table-maybe-recalculate-line ()
  8903. "Recompute the current line if marked for it, and if we haven't just done it."
  8904. (interactive)
  8905. (and org-table-allow-automatic-line-recalculation
  8906. (not (and (memq last-command org-recalc-commands)
  8907. (equal org-last-recalc-line (org-current-line))))
  8908. (save-excursion (beginning-of-line 1)
  8909. (looking-at org-table-auto-recalculate-regexp))
  8910. (org-table-recalculate) t))
  8911. (defvar org-table-formula-debug nil
  8912. "Non-nil means, debug table formulas.
  8913. When nil, simply write \"#ERROR\" in corrupted fields.")
  8914. (make-variable-buffer-local 'org-table-formula-debug)
  8915. (defvar modes)
  8916. (defsubst org-set-calc-mode (var &optional value)
  8917. (if (stringp var)
  8918. (setq var (assoc var '(("D" calc-angle-mode deg)
  8919. ("R" calc-angle-mode rad)
  8920. ("F" calc-prefer-frac t)
  8921. ("S" calc-symbolic-mode t)))
  8922. value (nth 2 var) var (nth 1 var)))
  8923. (if (memq var modes)
  8924. (setcar (cdr (memq var modes)) value)
  8925. (cons var (cons value modes)))
  8926. modes)
  8927. (defun org-table-eval-formula (&optional arg equation
  8928. suppress-align suppress-const
  8929. suppress-store suppress-analysis)
  8930. "Replace the table field value at the cursor by the result of a calculation.
  8931. This function makes use of Dave Gillespie's Calc package, in my view the
  8932. most exciting program ever written for GNU Emacs. So you need to have Calc
  8933. installed in order to use this function.
  8934. In a table, this command replaces the value in the current field with the
  8935. result of a formula. It also installs the formula as the \"current\" column
  8936. formula, by storing it in a special line below the table. When called
  8937. with a `C-u' prefix, the current field must ba a named field, and the
  8938. formula is installed as valid in only this specific field.
  8939. When called with two `C-u' prefixes, insert the active equation
  8940. for the field back into the current field, so that it can be
  8941. edited there. This is useful in order to use \\[org-table-show-reference]
  8942. to check the referenced fields.
  8943. When called, the command first prompts for a formula, which is read in
  8944. the minibuffer. Previously entered formulas are available through the
  8945. history list, and the last used formula is offered as a default.
  8946. These stored formulas are adapted correctly when moving, inserting, or
  8947. deleting columns with the corresponding commands.
  8948. The formula can be any algebraic expression understood by the Calc package.
  8949. For details, see the Org-mode manual.
  8950. This function can also be called from Lisp programs and offers
  8951. additional arguments: EQUATION can be the formula to apply. If this
  8952. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  8953. used to speed-up recursive calls by by-passing unnecessary aligns.
  8954. SUPPRESS-CONST suppresses the interpretation of constants in the
  8955. formula, assuming that this has been done already outside the function.
  8956. SUPPRESS-STORE means the formula should not be stored, either because
  8957. it is already stored, or because it is a modified equation that should
  8958. not overwrite the stored one."
  8959. (interactive "P")
  8960. (org-table-check-inside-data-field)
  8961. (or suppress-analysis (org-table-get-specials))
  8962. (if (equal arg '(16))
  8963. (let ((eq (org-table-current-field-formula)))
  8964. (or eq (error "No equation active for current field"))
  8965. (org-table-get-field nil eq)
  8966. (org-table-align)
  8967. (setq org-table-may-need-update t))
  8968. (let* (fields
  8969. (ndown (if (integerp arg) arg 1))
  8970. (org-table-automatic-realign nil)
  8971. (case-fold-search nil)
  8972. (down (> ndown 1))
  8973. (formula (if (and equation suppress-store)
  8974. equation
  8975. (org-table-get-formula equation (equal arg '(4)))))
  8976. (n0 (org-table-current-column))
  8977. (modes (copy-sequence org-calc-default-modes))
  8978. (numbers nil) ; was a variable, now fixed default
  8979. (keep-empty nil)
  8980. n form form0 bw fmt x ev orig c lispp literal)
  8981. ;; Parse the format string. Since we have a lot of modes, this is
  8982. ;; a lot of work. However, I think calc still uses most of the time.
  8983. (if (string-match ";" formula)
  8984. (let ((tmp (org-split-string formula ";")))
  8985. (setq formula (car tmp)
  8986. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  8987. (nth 1 tmp)))
  8988. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  8989. (setq c (string-to-char (match-string 1 fmt))
  8990. n (string-to-number (match-string 2 fmt)))
  8991. (if (= c ?p)
  8992. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  8993. (setq modes (org-set-calc-mode
  8994. 'calc-float-format
  8995. (list (cdr (assoc c '((?n . float) (?f . fix)
  8996. (?s . sci) (?e . eng))))
  8997. n))))
  8998. (setq fmt (replace-match "" t t fmt)))
  8999. (if (string-match "[NT]" fmt)
  9000. (setq numbers (equal (match-string 0 fmt) "N")
  9001. fmt (replace-match "" t t fmt)))
  9002. (if (string-match "L" fmt)
  9003. (setq literal t
  9004. fmt (replace-match "" t t fmt)))
  9005. (if (string-match "E" fmt)
  9006. (setq keep-empty t
  9007. fmt (replace-match "" t t fmt)))
  9008. (while (string-match "[DRFS]" fmt)
  9009. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9010. (setq fmt (replace-match "" t t fmt)))
  9011. (unless (string-match "\\S-" fmt)
  9012. (setq fmt nil))))
  9013. (if (and (not suppress-const) org-table-formula-use-constants)
  9014. (setq formula (org-table-formula-substitute-names formula)))
  9015. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9016. (while (> ndown 0)
  9017. (setq fields (org-split-string
  9018. (org-no-properties
  9019. (buffer-substring (point-at-bol) (point-at-eol)))
  9020. " *| *"))
  9021. (if (eq numbers t)
  9022. (setq fields (mapcar
  9023. (lambda (x) (number-to-string (string-to-number x)))
  9024. fields)))
  9025. (setq ndown (1- ndown))
  9026. (setq form (copy-sequence formula)
  9027. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9028. (if (and lispp literal) (setq lispp 'literal))
  9029. ;; Check for old vertical references
  9030. (setq form (org-rewrite-old-row-references form))
  9031. ;; Insert complex ranges
  9032. (while (string-match org-table-range-regexp form)
  9033. (setq form
  9034. (replace-match
  9035. (save-match-data
  9036. (org-table-make-reference
  9037. (org-table-get-range (match-string 0 form) nil n0)
  9038. keep-empty numbers lispp))
  9039. t t form)))
  9040. ;; Insert simple ranges
  9041. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9042. (setq form
  9043. (replace-match
  9044. (save-match-data
  9045. (org-table-make-reference
  9046. (org-sublist
  9047. fields (string-to-number (match-string 1 form))
  9048. (string-to-number (match-string 2 form)))
  9049. keep-empty numbers lispp))
  9050. t t form)))
  9051. (setq form0 form)
  9052. ;; Insert the references to fields in same row
  9053. (while (string-match "\\$\\([0-9]+\\)" form)
  9054. (setq n (string-to-number (match-string 1 form))
  9055. x (nth (1- (if (= n 0) n0 n)) fields))
  9056. (unless x (error "Invalid field specifier \"%s\""
  9057. (match-string 0 form)))
  9058. (setq form (replace-match
  9059. (save-match-data
  9060. (org-table-make-reference x nil numbers lispp))
  9061. t t form)))
  9062. (if lispp
  9063. (setq ev (condition-case nil
  9064. (eval (eval (read form)))
  9065. (error "#ERROR"))
  9066. ev (if (numberp ev) (number-to-string ev) ev))
  9067. (or (fboundp 'calc-eval)
  9068. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9069. (setq ev (calc-eval (cons form modes)
  9070. (if numbers 'num))))
  9071. (when org-table-formula-debug
  9072. (with-output-to-temp-buffer "*Substitution History*"
  9073. (princ (format "Substitution history of formula
  9074. Orig: %s
  9075. $xyz-> %s
  9076. @r$c-> %s
  9077. $1-> %s\n" orig formula form0 form))
  9078. (if (listp ev)
  9079. (princ (format " %s^\nError: %s"
  9080. (make-string (car ev) ?\-) (nth 1 ev)))
  9081. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9082. ev (or fmt "NONE")
  9083. (if fmt (format fmt (string-to-number ev)) ev)))))
  9084. (setq bw (get-buffer-window "*Substitution History*"))
  9085. (shrink-window-if-larger-than-buffer bw)
  9086. (unless (and (interactive-p) (not ndown))
  9087. (unless (let (inhibit-redisplay)
  9088. (y-or-n-p "Debugging Formula. Continue to next? "))
  9089. (org-table-align)
  9090. (error "Abort"))
  9091. (delete-window bw)
  9092. (message "")))
  9093. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9094. (org-table-justify-field-maybe
  9095. (if fmt (format fmt (string-to-number ev)) ev))
  9096. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9097. (call-interactively 'org-return)
  9098. (setq ndown 0)))
  9099. (and down (org-table-maybe-recalculate-line))
  9100. (or suppress-align (and org-table-may-need-update
  9101. (org-table-align))))))
  9102. (defun org-table-put-field-property (prop value)
  9103. (save-excursion
  9104. (put-text-property (progn (skip-chars-backward "^|") (point))
  9105. (progn (skip-chars-forward "^|") (point))
  9106. prop value)))
  9107. (defun org-table-get-range (desc &optional tbeg col highlight)
  9108. "Get a calc vector from a column, accorting to descriptor DESC.
  9109. Optional arguments TBEG and COL can give the beginning of the table and
  9110. the current column, to avoid unnecessary parsing.
  9111. HIGHLIGHT means, just highlight the range."
  9112. (if (not (equal (string-to-char desc) ?@))
  9113. (setq desc (concat "@" desc)))
  9114. (save-excursion
  9115. (or tbeg (setq tbeg (org-table-begin)))
  9116. (or col (setq col (org-table-current-column)))
  9117. (let ((thisline (org-current-line))
  9118. beg end c1 c2 r1 r2 rangep tmp)
  9119. (unless (string-match org-table-range-regexp desc)
  9120. (error "Invalid table range specifier `%s'" desc))
  9121. (setq rangep (match-end 3)
  9122. r1 (and (match-end 1) (match-string 1 desc))
  9123. r2 (and (match-end 4) (match-string 4 desc))
  9124. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9125. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9126. (and c1 (setq c1 (+ (string-to-number c1)
  9127. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9128. (and c2 (setq c2 (+ (string-to-number c2)
  9129. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9130. (if (equal r1 "") (setq r1 nil))
  9131. (if (equal r2 "") (setq r2 nil))
  9132. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9133. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9134. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9135. (if (not r1) (setq r1 thisline))
  9136. (if (not r2) (setq r2 thisline))
  9137. (if (not c1) (setq c1 col))
  9138. (if (not c2) (setq c2 col))
  9139. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9140. ;; just one field
  9141. (progn
  9142. (goto-line r1)
  9143. (while (not (looking-at org-table-dataline-regexp))
  9144. (beginning-of-line 2))
  9145. (prog1 (org-trim (org-table-get-field c1))
  9146. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9147. ;; A range, return a vector
  9148. ;; First sort the numbers to get a regular ractangle
  9149. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9150. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9151. (goto-line r1)
  9152. (while (not (looking-at org-table-dataline-regexp))
  9153. (beginning-of-line 2))
  9154. (org-table-goto-column c1)
  9155. (setq beg (point))
  9156. (goto-line r2)
  9157. (while (not (looking-at org-table-dataline-regexp))
  9158. (beginning-of-line 0))
  9159. (org-table-goto-column c2)
  9160. (setq end (point))
  9161. (if highlight
  9162. (org-table-highlight-rectangle
  9163. beg (progn (skip-chars-forward "^|\n") (point))))
  9164. ;; return string representation of calc vector
  9165. (mapcar 'org-trim
  9166. (apply 'append (org-table-copy-region beg end)))))))
  9167. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9168. "Analyze descriptor DESC and retrieve the corresponding line number.
  9169. The cursor is currently in line CLINE, the table begins in line BLINE,
  9170. and TABLE is a vector with line types."
  9171. (if (string-match "^[0-9]+$" desc)
  9172. (aref org-table-dlines (string-to-number desc))
  9173. (setq cline (or cline (org-current-line))
  9174. bline (or bline org-table-current-begin-line)
  9175. table (or table org-table-current-line-types))
  9176. (if (or
  9177. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9178. ;; 1 2 3 4 5 6
  9179. (and (not (match-end 3)) (not (match-end 6)))
  9180. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9181. (error "invalid row descriptor `%s'" desc))
  9182. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9183. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9184. (odir (and (match-end 5) (match-string 5 desc)))
  9185. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9186. (i (- cline bline))
  9187. (rel (and (match-end 6)
  9188. (or (and (match-end 1) (not (match-end 3)))
  9189. (match-end 5)))))
  9190. (if (and hn (not hdir))
  9191. (progn
  9192. (setq i 0 hdir "+")
  9193. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9194. (if (and (not hn) on (not odir))
  9195. (error "should never happen");;(aref org-table-dlines on)
  9196. (if (and hn (> hn 0))
  9197. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9198. (if on
  9199. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9200. (+ bline i)))))
  9201. (defun org-find-row-type (table i type backwards relative n)
  9202. (let ((l (length table)))
  9203. (while (> n 0)
  9204. (while (and (setq i (+ i (if backwards -1 1)))
  9205. (>= i 0) (< i l)
  9206. (not (eq (aref table i) type))
  9207. (if (and relative (eq (aref table i) 'hline))
  9208. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9209. t)))
  9210. (setq n (1- n)))
  9211. (if (or (< i 0) (>= i l))
  9212. (error "Row descriptior leads outside table")
  9213. i)))
  9214. (defun org-rewrite-old-row-references (s)
  9215. (if (string-match "&[-+0-9I]" s)
  9216. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9217. s))
  9218. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9219. "Convert list ELEMENTS to something appropriate to insert into formula.
  9220. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9221. NUMBERS indicates that everything should be converted to numbers.
  9222. LISPP means to return something appropriate for a Lisp list."
  9223. (if (stringp elements) ; just a single val
  9224. (if lispp
  9225. (if (eq lispp 'literal)
  9226. elements
  9227. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9228. (if (equal elements "") (setq elements "0"))
  9229. (if numbers (number-to-string (string-to-number elements)) elements))
  9230. (unless keep-empty
  9231. (setq elements
  9232. (delq nil
  9233. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9234. elements))))
  9235. (setq elements (or elements '("0")))
  9236. (if lispp
  9237. (mapconcat
  9238. (lambda (x)
  9239. (if (eq lispp 'literal)
  9240. x
  9241. (prin1-to-string (if numbers (string-to-number x) x))))
  9242. elements " ")
  9243. (concat "[" (mapconcat
  9244. (lambda (x)
  9245. (if numbers (number-to-string (string-to-number x)) x))
  9246. elements
  9247. ",") "]"))))
  9248. (defun org-table-recalculate (&optional all noalign)
  9249. "Recalculate the current table line by applying all stored formulas.
  9250. With prefix arg ALL, do this for all lines in the table."
  9251. (interactive "P")
  9252. (or (memq this-command org-recalc-commands)
  9253. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9254. (unless (org-at-table-p) (error "Not at a table"))
  9255. (if (equal all '(16))
  9256. (org-table-iterate)
  9257. (org-table-get-specials)
  9258. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9259. (lambda (a b) (string< (car a) (car b)))))
  9260. (inhibit-redisplay (not debug-on-error))
  9261. (line-re org-table-dataline-regexp)
  9262. (thisline (org-current-line))
  9263. (thiscol (org-table-current-column))
  9264. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9265. ;; Insert constants in all formulas
  9266. (setq eqlist
  9267. (mapcar (lambda (x)
  9268. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9269. x)
  9270. eqlist))
  9271. ;; Split the equation list
  9272. (while (setq eq (pop eqlist))
  9273. (if (<= (string-to-char (car eq)) ?9)
  9274. (push eq eqlnum)
  9275. (push eq eqlname)))
  9276. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9277. (if all
  9278. (progn
  9279. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9280. (goto-char (setq beg (org-table-begin)))
  9281. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9282. ;; This is a table with marked lines, compute selected lines
  9283. (setq line-re org-table-recalculate-regexp)
  9284. ;; Move forward to the first non-header line
  9285. (if (and (re-search-forward org-table-dataline-regexp end t)
  9286. (re-search-forward org-table-hline-regexp end t)
  9287. (re-search-forward org-table-dataline-regexp end t))
  9288. (setq beg (match-beginning 0))
  9289. nil))) ;; just leave beg where it is
  9290. (setq beg (point-at-bol)
  9291. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9292. (goto-char beg)
  9293. (and all (message "Re-applying formulas to full table..."))
  9294. ;; First find the named fields, and mark them untouchanble
  9295. (remove-text-properties beg end '(org-untouchable t))
  9296. (while (setq eq (pop eqlname))
  9297. (setq name (car eq)
  9298. a (assoc name org-table-named-field-locations))
  9299. (and (not a)
  9300. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9301. (setq a (list name
  9302. (aref org-table-dlines
  9303. (string-to-number (match-string 1 name)))
  9304. (string-to-number (match-string 2 name)))))
  9305. (when (and a (or all (equal (nth 1 a) thisline)))
  9306. (message "Re-applying formula to field: %s" name)
  9307. (goto-line (nth 1 a))
  9308. (org-table-goto-column (nth 2 a))
  9309. (push (append a (list (cdr eq))) eqlname1)
  9310. (org-table-put-field-property :org-untouchable t)))
  9311. ;; Now evauluate the column formulas, but skip fields covered by
  9312. ;; field formulas
  9313. (goto-char beg)
  9314. (while (re-search-forward line-re end t)
  9315. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9316. ;; Unprotected line, recalculate
  9317. (and all (message "Re-applying formulas to full table...(line %d)"
  9318. (setq cnt (1+ cnt))))
  9319. (setq org-last-recalc-line (org-current-line))
  9320. (setq eql eqlnum)
  9321. (while (setq entry (pop eql))
  9322. (goto-line org-last-recalc-line)
  9323. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9324. (unless (get-text-property (point) :org-untouchable)
  9325. (org-table-eval-formula nil (cdr entry)
  9326. 'noalign 'nocst 'nostore 'noanalysis)))))
  9327. ;; Now evaluate the field formulas
  9328. (while (setq eq (pop eqlname1))
  9329. (message "Re-applying formula to field: %s" (car eq))
  9330. (goto-line (nth 1 eq))
  9331. (org-table-goto-column (nth 2 eq))
  9332. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9333. 'nostore 'noanalysis))
  9334. (goto-line thisline)
  9335. (org-table-goto-column thiscol)
  9336. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9337. (or noalign (and org-table-may-need-update (org-table-align))
  9338. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9339. ;; back to initial position
  9340. (message "Re-applying formulas...done")
  9341. (goto-line thisline)
  9342. (org-table-goto-column thiscol)
  9343. (or noalign (and org-table-may-need-update (org-table-align))
  9344. (and all (message "Re-applying formulas...done"))))))
  9345. (defun org-table-iterate (&optional arg)
  9346. "Recalculate the table until it does not change anymore."
  9347. (interactive "P")
  9348. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9349. (i 0)
  9350. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9351. thistbl)
  9352. (catch 'exit
  9353. (while (< i imax)
  9354. (setq i (1+ i))
  9355. (org-table-recalculate 'all)
  9356. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9357. (if (not (string= lasttbl thistbl))
  9358. (setq lasttbl thistbl)
  9359. (if (> i 1)
  9360. (message "Convergence after %d iterations" i)
  9361. (message "Table was already stable"))
  9362. (throw 'exit t)))
  9363. (error "No convergence after %d iterations" i))))
  9364. (defun org-table-formula-substitute-names (f)
  9365. "Replace $const with values in string F."
  9366. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9367. ;; First, check for column names
  9368. (while (setq start (string-match org-table-column-name-regexp f start))
  9369. (setq start (1+ start))
  9370. (setq a (assoc (match-string 1 f) org-table-column-names))
  9371. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9372. ;; Parameters and constants
  9373. (setq start 0)
  9374. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9375. (setq start (1+ start))
  9376. (if (setq a (save-match-data
  9377. (org-table-get-constant (match-string 1 f))))
  9378. (setq f (replace-match
  9379. (concat (if pp "(") a (if pp ")")) t t f))))
  9380. (if org-table-formula-debug
  9381. (put-text-property 0 (length f) :orig-formula f1 f))
  9382. f))
  9383. (defun org-table-get-constant (const)
  9384. "Find the value for a parameter or constant in a formula.
  9385. Parameters get priority."
  9386. (or (cdr (assoc const org-table-local-parameters))
  9387. (cdr (assoc const org-table-formula-constants-local))
  9388. (cdr (assoc const org-table-formula-constants))
  9389. (and (fboundp 'constants-get) (constants-get const))
  9390. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9391. (org-entry-get nil (substring const 5) 'inherit))
  9392. "#UNDEFINED_NAME"))
  9393. (defvar org-table-fedit-map
  9394. (let ((map (make-sparse-keymap)))
  9395. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9396. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9397. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9398. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9399. (org-defkey map "\C-c?" 'org-table-show-reference)
  9400. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9401. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9402. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9403. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9404. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9405. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9406. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9407. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9408. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9409. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9410. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9411. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9412. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9413. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9414. map))
  9415. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9416. '("Edit-Formulas"
  9417. ["Finish and Install" org-table-fedit-finish t]
  9418. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9419. ["Abort" org-table-fedit-abort t]
  9420. "--"
  9421. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9422. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9423. "--"
  9424. "Shift Reference at Point"
  9425. ["Up" org-table-fedit-ref-up t]
  9426. ["Down" org-table-fedit-ref-down t]
  9427. ["Left" org-table-fedit-ref-left t]
  9428. ["Right" org-table-fedit-ref-right t]
  9429. "-"
  9430. "Change Test Row for Column Formulas"
  9431. ["Up" org-table-fedit-line-up t]
  9432. ["Down" org-table-fedit-line-down t]
  9433. "--"
  9434. ["Scroll Table Window" org-table-fedit-scroll t]
  9435. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9436. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9437. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9438. org-table-overlay-coordinates)]
  9439. "--"
  9440. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9441. :style toggle :selected org-table-buffer-is-an]))
  9442. (defvar org-pos)
  9443. (defun org-table-edit-formulas ()
  9444. "Edit the formulas of the current table in a separate buffer."
  9445. (interactive)
  9446. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9447. (beginning-of-line 0))
  9448. (unless (org-at-table-p) (error "Not at a table"))
  9449. (org-table-get-specials)
  9450. (let ((key (org-table-current-field-formula 'key 'noerror))
  9451. (eql (sort (org-table-get-stored-formulas 'noerror)
  9452. 'org-table-formula-less-p))
  9453. (pos (move-marker (make-marker) (point)))
  9454. (startline 1)
  9455. (wc (current-window-configuration))
  9456. (titles '((column . "# Column Formulas\n")
  9457. (field . "# Field Formulas\n")
  9458. (named . "# Named Field Formulas\n")))
  9459. entry s type title)
  9460. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9461. (erase-buffer)
  9462. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9463. (let ((font-lock-global-modes '(not fundamental-mode)))
  9464. (fundamental-mode))
  9465. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9466. (org-set-local 'org-pos pos)
  9467. (org-set-local 'org-window-configuration wc)
  9468. (use-local-map org-table-fedit-map)
  9469. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9470. (easy-menu-add org-table-fedit-menu)
  9471. (setq startline (org-current-line))
  9472. (while (setq entry (pop eql))
  9473. (setq type (cond
  9474. ((equal (string-to-char (car entry)) ?@) 'field)
  9475. ((string-match "^[0-9]" (car entry)) 'column)
  9476. (t 'named)))
  9477. (when (setq title (assq type titles))
  9478. (or (bobp) (insert "\n"))
  9479. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9480. (setq titles (delq title titles)))
  9481. (if (equal key (car entry)) (setq startline (org-current-line)))
  9482. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9483. (car entry) " = " (cdr entry) "\n"))
  9484. (remove-text-properties 0 (length s) '(face nil) s)
  9485. (insert s))
  9486. (if (eq org-table-use-standard-references t)
  9487. (org-table-fedit-toggle-ref-type))
  9488. (goto-line startline)
  9489. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9490. (defun org-table-fedit-post-command ()
  9491. (when (not (memq this-command '(lisp-complete-symbol)))
  9492. (let ((win (selected-window)))
  9493. (save-excursion
  9494. (condition-case nil
  9495. (org-table-show-reference)
  9496. (error nil))
  9497. (select-window win)))))
  9498. (defun org-table-formula-to-user (s)
  9499. "Convert a formula from internal to user representation."
  9500. (if (eq org-table-use-standard-references t)
  9501. (org-table-convert-refs-to-an s)
  9502. s))
  9503. (defun org-table-formula-from-user (s)
  9504. "Convert a formula from user to internal representation."
  9505. (if org-table-use-standard-references
  9506. (org-table-convert-refs-to-rc s)
  9507. s))
  9508. (defun org-table-convert-refs-to-rc (s)
  9509. "Convert spreadsheet references from AB7 to @7$28.
  9510. Works for single references, but also for entire formulas and even the
  9511. full TBLFM line."
  9512. (let ((start 0))
  9513. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9514. (cond
  9515. ((match-end 3)
  9516. ;; format match, just advance
  9517. (setq start (match-end 0)))
  9518. ((and (> (match-beginning 0) 0)
  9519. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9520. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9521. ;; 3.e5 or something like this.
  9522. (setq start (match-end 0)))
  9523. (t
  9524. (setq start (match-beginning 0)
  9525. s (replace-match
  9526. (if (equal (match-string 2 s) "&")
  9527. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9528. (format "@%d$%d"
  9529. (string-to-number (match-string 2 s))
  9530. (org-letters-to-number (match-string 1 s))))
  9531. t t s)))))
  9532. s))
  9533. (defun org-table-convert-refs-to-an (s)
  9534. "Convert spreadsheet references from to @7$28 to AB7.
  9535. Works for single references, but also for entire formulas and even the
  9536. full TBLFM line."
  9537. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9538. (setq s (replace-match
  9539. (format "%s%d"
  9540. (org-number-to-letters
  9541. (string-to-number (match-string 2 s)))
  9542. (string-to-number (match-string 1 s)))
  9543. t t s)))
  9544. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9545. (setq s (replace-match (concat "\\1"
  9546. (org-number-to-letters
  9547. (string-to-number (match-string 2 s))) "&")
  9548. t nil s)))
  9549. s)
  9550. (defun org-letters-to-number (s)
  9551. "Convert a base 26 number represented by letters into an integer.
  9552. For example: AB -> 28."
  9553. (let ((n 0))
  9554. (setq s (upcase s))
  9555. (while (> (length s) 0)
  9556. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9557. s (substring s 1)))
  9558. n))
  9559. (defun org-number-to-letters (n)
  9560. "Convert an integer into a base 26 number represented by letters.
  9561. For example: 28 -> AB."
  9562. (let ((s ""))
  9563. (while (> n 0)
  9564. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9565. n (/ (1- n) 26)))
  9566. s))
  9567. (defun org-table-fedit-convert-buffer (function)
  9568. "Convert all references in this buffer, using FUNTION."
  9569. (let ((line (org-current-line)))
  9570. (goto-char (point-min))
  9571. (while (not (eobp))
  9572. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9573. (delete-region (point) (point-at-eol))
  9574. (or (eobp) (forward-char 1)))
  9575. (goto-line line)))
  9576. (defun org-table-fedit-toggle-ref-type ()
  9577. "Convert all references in the buffer from B3 to @3$2 and back."
  9578. (interactive)
  9579. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9580. (org-table-fedit-convert-buffer
  9581. (if org-table-buffer-is-an
  9582. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9583. (message "Reference type switched to %s"
  9584. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9585. (defun org-table-fedit-ref-up ()
  9586. "Shift the reference at point one row/hline up."
  9587. (interactive)
  9588. (org-table-fedit-shift-reference 'up))
  9589. (defun org-table-fedit-ref-down ()
  9590. "Shift the reference at point one row/hline down."
  9591. (interactive)
  9592. (org-table-fedit-shift-reference 'down))
  9593. (defun org-table-fedit-ref-left ()
  9594. "Shift the reference at point one field to the left."
  9595. (interactive)
  9596. (org-table-fedit-shift-reference 'left))
  9597. (defun org-table-fedit-ref-right ()
  9598. "Shift the reference at point one field to the right."
  9599. (interactive)
  9600. (org-table-fedit-shift-reference 'right))
  9601. (defun org-table-fedit-shift-reference (dir)
  9602. (cond
  9603. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9604. (if (memq dir '(left right))
  9605. (org-rematch-and-replace 1 (eq dir 'left))
  9606. (error "Cannot shift reference in this direction")))
  9607. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9608. ;; A B3-like reference
  9609. (if (memq dir '(up down))
  9610. (org-rematch-and-replace 2 (eq dir 'up))
  9611. (org-rematch-and-replace 1 (eq dir 'left))))
  9612. ((org-at-regexp-p
  9613. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9614. ;; An internal reference
  9615. (if (memq dir '(up down))
  9616. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9617. (org-rematch-and-replace 5 (eq dir 'left))))))
  9618. (defun org-rematch-and-replace (n &optional decr hline)
  9619. "Re-match the group N, and replace it with the shifted refrence."
  9620. (or (match-end n) (error "Cannot shift reference in this direction"))
  9621. (goto-char (match-beginning n))
  9622. (and (looking-at (regexp-quote (match-string n)))
  9623. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9624. t t)))
  9625. (defun org-shift-refpart (ref &optional decr hline)
  9626. "Shift a refrence part REF.
  9627. If DECR is set, decrease the references row/column, else increase.
  9628. If HLINE is set, this may be a hline reference, it certainly is not
  9629. a translation reference."
  9630. (save-match-data
  9631. (let* ((sign (string-match "^[-+]" ref)) n)
  9632. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9633. (cond
  9634. ((and hline (string-match "^I+" ref))
  9635. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9636. (setq n (+ n (if decr -1 1)))
  9637. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9638. (if sign
  9639. (setq sign (if (< n 0) "-" "+") n (abs n))
  9640. (setq n (max 1 n)))
  9641. (concat sign (make-string n ?I)))
  9642. ((string-match "^[0-9]+" ref)
  9643. (setq n (string-to-number (concat sign ref)))
  9644. (setq n (+ n (if decr -1 1)))
  9645. (if sign
  9646. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9647. (number-to-string (max 1 n))))
  9648. ((string-match "^[a-zA-Z]+" ref)
  9649. (org-number-to-letters
  9650. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9651. (t (error "Cannot shift reference"))))))
  9652. (defun org-table-fedit-toggle-coordinates ()
  9653. "Toggle the display of coordinates in the refrenced table."
  9654. (interactive)
  9655. (let ((pos (marker-position org-pos)))
  9656. (with-current-buffer (marker-buffer org-pos)
  9657. (save-excursion
  9658. (goto-char pos)
  9659. (org-table-toggle-coordinate-overlays)))))
  9660. (defun org-table-fedit-finish (&optional arg)
  9661. "Parse the buffer for formula definitions and install them.
  9662. With prefix ARG, apply the new formulas to the table."
  9663. (interactive "P")
  9664. (org-table-remove-rectangle-highlight)
  9665. (if org-table-use-standard-references
  9666. (progn
  9667. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9668. (setq org-table-buffer-is-an nil)))
  9669. (let ((pos org-pos) eql var form)
  9670. (goto-char (point-min))
  9671. (while (re-search-forward
  9672. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9673. nil t)
  9674. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9675. form (match-string 3))
  9676. (setq form (org-trim form))
  9677. (when (not (equal form ""))
  9678. (while (string-match "[ \t]*\n[ \t]*" form)
  9679. (setq form (replace-match " " t t form)))
  9680. (when (assoc var eql)
  9681. (error "Double formulas for %s" var))
  9682. (push (cons var form) eql)))
  9683. (setq org-pos nil)
  9684. (set-window-configuration org-window-configuration)
  9685. (select-window (get-buffer-window (marker-buffer pos)))
  9686. (goto-char pos)
  9687. (unless (org-at-table-p)
  9688. (error "Lost table position - cannot install formulae"))
  9689. (org-table-store-formulas eql)
  9690. (move-marker pos nil)
  9691. (kill-buffer "*Edit Formulas*")
  9692. (if arg
  9693. (org-table-recalculate 'all)
  9694. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9695. (defun org-table-fedit-abort ()
  9696. "Abort editing formulas, without installing the changes."
  9697. (interactive)
  9698. (org-table-remove-rectangle-highlight)
  9699. (let ((pos org-pos))
  9700. (set-window-configuration org-window-configuration)
  9701. (select-window (get-buffer-window (marker-buffer pos)))
  9702. (goto-char pos)
  9703. (move-marker pos nil)
  9704. (message "Formula editing aborted without installing changes")))
  9705. (defun org-table-fedit-lisp-indent ()
  9706. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9707. (interactive)
  9708. (let ((pos (point)) beg end ind)
  9709. (beginning-of-line 1)
  9710. (cond
  9711. ((looking-at "[ \t]")
  9712. (goto-char pos)
  9713. (call-interactively 'lisp-indent-line))
  9714. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9715. ((not (fboundp 'pp-buffer))
  9716. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9717. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9718. (goto-char (- (match-end 0) 2))
  9719. (setq beg (point))
  9720. (setq ind (make-string (current-column) ?\ ))
  9721. (condition-case nil (forward-sexp 1)
  9722. (error
  9723. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  9724. (setq end (point))
  9725. (save-restriction
  9726. (narrow-to-region beg end)
  9727. (if (eq last-command this-command)
  9728. (progn
  9729. (goto-char (point-min))
  9730. (setq this-command nil)
  9731. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  9732. (replace-match " ")))
  9733. (pp-buffer)
  9734. (untabify (point-min) (point-max))
  9735. (goto-char (1+ (point-min)))
  9736. (while (re-search-forward "^." nil t)
  9737. (beginning-of-line 1)
  9738. (insert ind))
  9739. (goto-char (point-max))
  9740. (backward-delete-char 1)))
  9741. (goto-char beg))
  9742. (t nil))))
  9743. (defvar org-show-positions nil)
  9744. (defun org-table-show-reference (&optional local)
  9745. "Show the location/value of the $ expression at point."
  9746. (interactive)
  9747. (org-table-remove-rectangle-highlight)
  9748. (catch 'exit
  9749. (let ((pos (if local (point) org-pos))
  9750. (face2 'highlight)
  9751. (org-inhibit-highlight-removal t)
  9752. (win (selected-window))
  9753. (org-show-positions nil)
  9754. var name e what match dest)
  9755. (if local (org-table-get-specials))
  9756. (setq what (cond
  9757. ((or (org-at-regexp-p org-table-range-regexp2)
  9758. (org-at-regexp-p org-table-translate-regexp)
  9759. (org-at-regexp-p org-table-range-regexp))
  9760. (setq match
  9761. (save-match-data
  9762. (org-table-convert-refs-to-rc (match-string 0))))
  9763. 'range)
  9764. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  9765. ((org-at-regexp-p "\\$[0-9]+") 'column)
  9766. ((not local) nil)
  9767. (t (error "No reference at point")))
  9768. match (and what (or match (match-string 0))))
  9769. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  9770. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  9771. 'secondary-selection))
  9772. (org-add-hook 'before-change-functions
  9773. 'org-table-remove-rectangle-highlight)
  9774. (if (eq what 'name) (setq var (substring match 1)))
  9775. (when (eq what 'range)
  9776. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  9777. (setq match (org-table-formula-substitute-names match)))
  9778. (unless local
  9779. (save-excursion
  9780. (end-of-line 1)
  9781. (re-search-backward "^\\S-" nil t)
  9782. (beginning-of-line 1)
  9783. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  9784. (setq dest
  9785. (save-match-data
  9786. (org-table-convert-refs-to-rc (match-string 1))))
  9787. (org-table-add-rectangle-overlay
  9788. (match-beginning 1) (match-end 1) face2))))
  9789. (if (and (markerp pos) (marker-buffer pos))
  9790. (if (get-buffer-window (marker-buffer pos))
  9791. (select-window (get-buffer-window (marker-buffer pos)))
  9792. (org-switch-to-buffer-other-window (get-buffer-window
  9793. (marker-buffer pos)))))
  9794. (goto-char pos)
  9795. (org-table-force-dataline)
  9796. (when dest
  9797. (setq name (substring dest 1))
  9798. (cond
  9799. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  9800. (setq e (assoc name org-table-named-field-locations))
  9801. (goto-line (nth 1 e))
  9802. (org-table-goto-column (nth 2 e)))
  9803. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  9804. (let ((l (string-to-number (match-string 1 dest)))
  9805. (c (string-to-number (match-string 2 dest))))
  9806. (goto-line (aref org-table-dlines l))
  9807. (org-table-goto-column c)))
  9808. (t (org-table-goto-column (string-to-number name))))
  9809. (move-marker pos (point))
  9810. (org-table-highlight-rectangle nil nil face2))
  9811. (cond
  9812. ((equal dest match))
  9813. ((not match))
  9814. ((eq what 'range)
  9815. (condition-case nil
  9816. (save-excursion
  9817. (org-table-get-range match nil nil 'highlight))
  9818. (error nil)))
  9819. ((setq e (assoc var org-table-named-field-locations))
  9820. (goto-line (nth 1 e))
  9821. (org-table-goto-column (nth 2 e))
  9822. (org-table-highlight-rectangle (point) (point))
  9823. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  9824. ((setq e (assoc var org-table-column-names))
  9825. (org-table-goto-column (string-to-number (cdr e)))
  9826. (org-table-highlight-rectangle (point) (point))
  9827. (goto-char (org-table-begin))
  9828. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  9829. (org-table-end) t)
  9830. (progn
  9831. (goto-char (match-beginning 1))
  9832. (org-table-highlight-rectangle)
  9833. (message "Named column (column %s)" (cdr e)))
  9834. (error "Column name not found")))
  9835. ((eq what 'column)
  9836. ;; column number
  9837. (org-table-goto-column (string-to-number (substring match 1)))
  9838. (org-table-highlight-rectangle (point) (point))
  9839. (message "Column %s" (substring match 1)))
  9840. ((setq e (assoc var org-table-local-parameters))
  9841. (goto-char (org-table-begin))
  9842. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  9843. (progn
  9844. (goto-char (match-beginning 1))
  9845. (org-table-highlight-rectangle)
  9846. (message "Local parameter."))
  9847. (error "Parameter not found")))
  9848. (t
  9849. (cond
  9850. ((not var) (error "No reference at point"))
  9851. ((setq e (assoc var org-table-formula-constants-local))
  9852. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  9853. var (cdr e)))
  9854. ((setq e (assoc var org-table-formula-constants))
  9855. (message "Constant: $%s=%s in `org-table-formula-constants'."
  9856. var (cdr e)))
  9857. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  9858. (message "Constant: $%s=%s, from `constants.el'%s."
  9859. var e (format " (%s units)" constants-unit-system)))
  9860. (t (error "Undefined name $%s" var)))))
  9861. (goto-char pos)
  9862. (when (and org-show-positions
  9863. (not (memq this-command '(org-table-fedit-scroll
  9864. org-table-fedit-scroll-down))))
  9865. (push pos org-show-positions)
  9866. (push org-table-current-begin-pos org-show-positions)
  9867. (let ((min (apply 'min org-show-positions))
  9868. (max (apply 'max org-show-positions)))
  9869. (goto-char min) (recenter 0)
  9870. (goto-char max)
  9871. (or (pos-visible-in-window-p max) (recenter -1))))
  9872. (select-window win))))
  9873. (defun org-table-force-dataline ()
  9874. "Make sure the cursor is in a dataline in a table."
  9875. (unless (save-excursion
  9876. (beginning-of-line 1)
  9877. (looking-at org-table-dataline-regexp))
  9878. (let* ((re org-table-dataline-regexp)
  9879. (p1 (save-excursion (re-search-forward re nil 'move)))
  9880. (p2 (save-excursion (re-search-backward re nil 'move))))
  9881. (cond ((and p1 p2)
  9882. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  9883. p1 p2)))
  9884. ((or p1 p2) (goto-char (or p1 p2)))
  9885. (t (error "No table dataline around here"))))))
  9886. (defun org-table-fedit-line-up ()
  9887. "Move cursor one line up in the window showing the table."
  9888. (interactive)
  9889. (org-table-fedit-move 'previous-line))
  9890. (defun org-table-fedit-line-down ()
  9891. "Move cursor one line down in the window showing the table."
  9892. (interactive)
  9893. (org-table-fedit-move 'next-line))
  9894. (defun org-table-fedit-move (command)
  9895. "Move the cursor in the window shoinw the table.
  9896. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  9897. (let ((org-table-allow-automatic-line-recalculation nil)
  9898. (pos org-pos) (win (selected-window)) p)
  9899. (select-window (get-buffer-window (marker-buffer org-pos)))
  9900. (setq p (point))
  9901. (call-interactively command)
  9902. (while (and (org-at-table-p)
  9903. (org-at-table-hline-p))
  9904. (call-interactively command))
  9905. (or (org-at-table-p) (goto-char p))
  9906. (move-marker pos (point))
  9907. (select-window win)))
  9908. (defun org-table-fedit-scroll (N)
  9909. (interactive "p")
  9910. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  9911. (scroll-other-window N)))
  9912. (defun org-table-fedit-scroll-down (N)
  9913. (interactive "p")
  9914. (org-table-fedit-scroll (- N)))
  9915. (defvar org-table-rectangle-overlays nil)
  9916. (defun org-table-add-rectangle-overlay (beg end &optional face)
  9917. "Add a new overlay."
  9918. (let ((ov (org-make-overlay beg end)))
  9919. (org-overlay-put ov 'face (or face 'secondary-selection))
  9920. (push ov org-table-rectangle-overlays)))
  9921. (defun org-table-highlight-rectangle (&optional beg end face)
  9922. "Highlight rectangular region in a table."
  9923. (setq beg (or beg (point)) end (or end (point)))
  9924. (let ((b (min beg end))
  9925. (e (max beg end))
  9926. l1 c1 l2 c2 tmp)
  9927. (and (boundp 'org-show-positions)
  9928. (setq org-show-positions (cons b (cons e org-show-positions))))
  9929. (goto-char (min beg end))
  9930. (setq l1 (org-current-line)
  9931. c1 (org-table-current-column))
  9932. (goto-char (max beg end))
  9933. (setq l2 (org-current-line)
  9934. c2 (org-table-current-column))
  9935. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  9936. (goto-line l1)
  9937. (beginning-of-line 1)
  9938. (loop for line from l1 to l2 do
  9939. (when (looking-at org-table-dataline-regexp)
  9940. (org-table-goto-column c1)
  9941. (skip-chars-backward "^|\n") (setq beg (point))
  9942. (org-table-goto-column c2)
  9943. (skip-chars-forward "^|\n") (setq end (point))
  9944. (org-table-add-rectangle-overlay beg end face))
  9945. (beginning-of-line 2))
  9946. (goto-char b))
  9947. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  9948. (defun org-table-remove-rectangle-highlight (&rest ignore)
  9949. "Remove the rectangle overlays."
  9950. (unless org-inhibit-highlight-removal
  9951. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  9952. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  9953. (setq org-table-rectangle-overlays nil)))
  9954. (defvar org-table-coordinate-overlays nil
  9955. "Collects the cooordinate grid overlays, so that they can be removed.")
  9956. (make-variable-buffer-local 'org-table-coordinate-overlays)
  9957. (defun org-table-overlay-coordinates ()
  9958. "Add overlays to the table at point, to show row/column coordinates."
  9959. (interactive)
  9960. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  9961. (setq org-table-coordinate-overlays nil)
  9962. (save-excursion
  9963. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  9964. (goto-char (org-table-begin))
  9965. (while (org-at-table-p)
  9966. (setq eol (point-at-eol))
  9967. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  9968. (push ov org-table-coordinate-overlays)
  9969. (setq hline (looking-at org-table-hline-regexp))
  9970. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  9971. (format "%4d" (setq id (1+ id)))))
  9972. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  9973. (when hline
  9974. (setq ic 0)
  9975. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  9976. (setq beg (1+ (match-beginning 0))
  9977. ic (1+ ic)
  9978. s1 (concat "$" (int-to-string ic))
  9979. s2 (org-number-to-letters ic)
  9980. str (if (eq org-table-use-standard-references t) s2 s1))
  9981. (setq ov (org-make-overlay beg (+ beg (length str))))
  9982. (push ov org-table-coordinate-overlays)
  9983. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  9984. (beginning-of-line 2)))))
  9985. (defun org-table-toggle-coordinate-overlays ()
  9986. "Toggle the display of Row/Column numbers in tables."
  9987. (interactive)
  9988. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  9989. (message "Row/Column number display turned %s"
  9990. (if org-table-overlay-coordinates "on" "off"))
  9991. (if (and (org-at-table-p) org-table-overlay-coordinates)
  9992. (org-table-align))
  9993. (unless org-table-overlay-coordinates
  9994. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  9995. (setq org-table-coordinate-overlays nil)))
  9996. (defun org-table-toggle-formula-debugger ()
  9997. "Toggle the formula debugger in tables."
  9998. (interactive)
  9999. (setq org-table-formula-debug (not org-table-formula-debug))
  10000. (message "Formula debugging has been turned %s"
  10001. (if org-table-formula-debug "on" "off")))
  10002. ;;; The orgtbl minor mode
  10003. ;; Define a minor mode which can be used in other modes in order to
  10004. ;; integrate the org-mode table editor.
  10005. ;; This is really a hack, because the org-mode table editor uses several
  10006. ;; keys which normally belong to the major mode, for example the TAB and
  10007. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10008. ;; necessary to operate the table editor, but wraps the commands into a
  10009. ;; function which tests if the cursor is currently inside a table. If that
  10010. ;; is the case, the table editor command is executed. However, when any of
  10011. ;; those keys is used outside a table, the function uses `key-binding' to
  10012. ;; look up if the key has an associated command in another currently active
  10013. ;; keymap (minor modes, major mode, global), and executes that command.
  10014. ;; There might be problems if any of the keys used by the table editor is
  10015. ;; otherwise used as a prefix key.
  10016. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10017. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10018. ;; addresses this by checking explicitly for both bindings.
  10019. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10020. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10021. ;; Some modes bind other commands to simple characters, for example
  10022. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10023. ;; active, this binding is ignored inside tables and replaced with a
  10024. ;; modified self-insert.
  10025. (defvar orgtbl-mode nil
  10026. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10027. table editor in arbitrary modes.")
  10028. (make-variable-buffer-local 'orgtbl-mode)
  10029. (defvar orgtbl-mode-map (make-keymap)
  10030. "Keymap for `orgtbl-mode'.")
  10031. ;;;###autoload
  10032. (defun turn-on-orgtbl ()
  10033. "Unconditionally turn on `orgtbl-mode'."
  10034. (orgtbl-mode 1))
  10035. (defvar org-old-auto-fill-inhibit-regexp nil
  10036. "Local variable used by `orgtbl-mode'")
  10037. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10038. "Matches a line belonging to an orgtbl.")
  10039. (defconst orgtbl-extra-font-lock-keywords
  10040. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10041. 0 (quote 'org-table) 'prepend))
  10042. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10043. ;;;###autoload
  10044. (defun orgtbl-mode (&optional arg)
  10045. "The `org-mode' table editor as a minor mode for use in other modes."
  10046. (interactive)
  10047. (if (org-mode-p)
  10048. ;; Exit without error, in case some hook functions calls this
  10049. ;; by accident in org-mode.
  10050. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10051. (setq orgtbl-mode
  10052. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10053. (if orgtbl-mode
  10054. (progn
  10055. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10056. ;; Make sure we are first in minor-mode-map-alist
  10057. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10058. (and c (setq minor-mode-map-alist
  10059. (cons c (delq c minor-mode-map-alist)))))
  10060. (org-set-local (quote org-table-may-need-update) t)
  10061. (org-add-hook 'before-change-functions 'org-before-change-function
  10062. nil 'local)
  10063. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10064. auto-fill-inhibit-regexp)
  10065. (org-set-local 'auto-fill-inhibit-regexp
  10066. (if auto-fill-inhibit-regexp
  10067. (concat orgtbl-line-start-regexp "\\|"
  10068. auto-fill-inhibit-regexp)
  10069. orgtbl-line-start-regexp))
  10070. (org-add-to-invisibility-spec '(org-cwidth))
  10071. (when (fboundp 'font-lock-add-keywords)
  10072. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10073. (org-restart-font-lock))
  10074. (easy-menu-add orgtbl-mode-menu)
  10075. (run-hooks 'orgtbl-mode-hook))
  10076. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10077. (org-cleanup-narrow-column-properties)
  10078. (org-remove-from-invisibility-spec '(org-cwidth))
  10079. (remove-hook 'before-change-functions 'org-before-change-function t)
  10080. (when (fboundp 'font-lock-remove-keywords)
  10081. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10082. (org-restart-font-lock))
  10083. (easy-menu-remove orgtbl-mode-menu)
  10084. (force-mode-line-update 'all))))
  10085. (defun org-cleanup-narrow-column-properties ()
  10086. "Remove all properties related to narrow-column invisibility."
  10087. (let ((s 1))
  10088. (while (setq s (text-property-any s (point-max)
  10089. 'display org-narrow-column-arrow))
  10090. (remove-text-properties s (1+ s) '(display t)))
  10091. (setq s 1)
  10092. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10093. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10094. (setq s 1)
  10095. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10096. (remove-text-properties s (1+ s) '(invisible t)))))
  10097. ;; Install it as a minor mode.
  10098. (put 'orgtbl-mode :included t)
  10099. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10100. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10101. (defun orgtbl-make-binding (fun n &rest keys)
  10102. "Create a function for binding in the table minor mode.
  10103. FUN is the command to call inside a table. N is used to create a unique
  10104. command name. KEYS are keys that should be checked in for a command
  10105. to execute outside of tables."
  10106. (eval
  10107. (list 'defun
  10108. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10109. '(arg)
  10110. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10111. "Outside of tables, run the binding of `"
  10112. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10113. "'.")
  10114. '(interactive "p")
  10115. (list 'if
  10116. '(org-at-table-p)
  10117. (list 'call-interactively (list 'quote fun))
  10118. (list 'let '(orgtbl-mode)
  10119. (list 'call-interactively
  10120. (append '(or)
  10121. (mapcar (lambda (k)
  10122. (list 'key-binding k))
  10123. keys)
  10124. '('orgtbl-error))))))))
  10125. (defun orgtbl-error ()
  10126. "Error when there is no default binding for a table key."
  10127. (interactive)
  10128. (error "This key has no function outside tables"))
  10129. (defun orgtbl-setup ()
  10130. "Setup orgtbl keymaps."
  10131. (let ((nfunc 0)
  10132. (bindings
  10133. (list
  10134. '([(meta shift left)] org-table-delete-column)
  10135. '([(meta left)] org-table-move-column-left)
  10136. '([(meta right)] org-table-move-column-right)
  10137. '([(meta shift right)] org-table-insert-column)
  10138. '([(meta shift up)] org-table-kill-row)
  10139. '([(meta shift down)] org-table-insert-row)
  10140. '([(meta up)] org-table-move-row-up)
  10141. '([(meta down)] org-table-move-row-down)
  10142. '("\C-c\C-w" org-table-cut-region)
  10143. '("\C-c\M-w" org-table-copy-region)
  10144. '("\C-c\C-y" org-table-paste-rectangle)
  10145. '("\C-c-" org-table-insert-hline)
  10146. '("\C-c}" org-table-toggle-coordinate-overlays)
  10147. '("\C-c{" org-table-toggle-formula-debugger)
  10148. '("\C-m" org-table-next-row)
  10149. '([(shift return)] org-table-copy-down)
  10150. '("\C-c\C-q" org-table-wrap-region)
  10151. '("\C-c?" org-table-field-info)
  10152. '("\C-c " org-table-blank-field)
  10153. '("\C-c+" org-table-sum)
  10154. '("\C-c=" org-table-eval-formula)
  10155. '("\C-c'" org-table-edit-formulas)
  10156. '("\C-c`" org-table-edit-field)
  10157. '("\C-c*" org-table-recalculate)
  10158. '("\C-c|" org-table-create-or-convert-from-region)
  10159. '("\C-c^" org-table-sort-lines)
  10160. '([(control ?#)] org-table-rotate-recalc-marks)))
  10161. elt key fun cmd)
  10162. (while (setq elt (pop bindings))
  10163. (setq nfunc (1+ nfunc))
  10164. (setq key (org-key (car elt))
  10165. fun (nth 1 elt)
  10166. cmd (orgtbl-make-binding fun nfunc key))
  10167. (org-defkey orgtbl-mode-map key cmd))
  10168. ;; Special treatment needed for TAB and RET
  10169. (org-defkey orgtbl-mode-map [(return)]
  10170. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10171. (org-defkey orgtbl-mode-map "\C-m"
  10172. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10173. (org-defkey orgtbl-mode-map [(tab)]
  10174. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10175. (org-defkey orgtbl-mode-map "\C-i"
  10176. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10177. (org-defkey orgtbl-mode-map [(shift tab)]
  10178. (orgtbl-make-binding 'org-table-previous-field 104
  10179. [(shift tab)] [(tab)] "\C-i"))
  10180. (org-defkey orgtbl-mode-map "\M-\C-m"
  10181. (orgtbl-make-binding 'org-table-wrap-region 105
  10182. "\M-\C-m" [(meta return)]))
  10183. (org-defkey orgtbl-mode-map [(meta return)]
  10184. (orgtbl-make-binding 'org-table-wrap-region 106
  10185. [(meta return)] "\M-\C-m"))
  10186. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10187. (when orgtbl-optimized
  10188. ;; If the user wants maximum table support, we need to hijack
  10189. ;; some standard editing functions
  10190. (org-remap orgtbl-mode-map
  10191. 'self-insert-command 'orgtbl-self-insert-command
  10192. 'delete-char 'org-delete-char
  10193. 'delete-backward-char 'org-delete-backward-char)
  10194. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10195. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10196. '("OrgTbl"
  10197. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10198. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10199. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10200. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10201. "--"
  10202. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10203. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10204. ["Copy Field from Above"
  10205. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10206. "--"
  10207. ("Column"
  10208. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10209. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10210. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10211. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10212. ("Row"
  10213. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10214. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10215. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10216. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10217. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10218. "--"
  10219. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10220. ("Rectangle"
  10221. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10222. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10223. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10224. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10225. "--"
  10226. ("Radio tables"
  10227. ["Insert table template" orgtbl-insert-radio-table
  10228. (assq major-mode orgtbl-radio-table-templates)]
  10229. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10230. "--"
  10231. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10232. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10233. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10234. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10235. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10236. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10237. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10238. ["Sum Column/Rectangle" org-table-sum
  10239. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10240. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10241. ["Debug Formulas"
  10242. org-table-toggle-formula-debugger :active (org-at-table-p)
  10243. :keys "C-c {"
  10244. :style toggle :selected org-table-formula-debug]
  10245. ["Show Col/Row Numbers"
  10246. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10247. :keys "C-c }"
  10248. :style toggle :selected org-table-overlay-coordinates]
  10249. ))
  10250. t))
  10251. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10252. "If the cursor is inside a table, realign the table.
  10253. It it is a table to be sent away to a receiver, do it.
  10254. With prefix arg, also recompute table."
  10255. (interactive "P")
  10256. (let ((pos (point)) action)
  10257. (save-excursion
  10258. (beginning-of-line 1)
  10259. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10260. ((looking-at "[ \t]*|") pos)
  10261. ((looking-at "#\\+TBLFM:") 'recalc))))
  10262. (cond
  10263. ((integerp action)
  10264. (goto-char action)
  10265. (org-table-maybe-eval-formula)
  10266. (if arg
  10267. (call-interactively 'org-table-recalculate)
  10268. (org-table-maybe-recalculate-line))
  10269. (call-interactively 'org-table-align)
  10270. (orgtbl-send-table 'maybe))
  10271. ((eq action 'recalc)
  10272. (save-excursion
  10273. (beginning-of-line 1)
  10274. (skip-chars-backward " \r\n\t")
  10275. (if (org-at-table-p)
  10276. (org-call-with-arg 'org-table-recalculate t))))
  10277. (t (let (orgtbl-mode)
  10278. (call-interactively (key-binding "\C-c\C-c")))))))
  10279. (defun orgtbl-tab (arg)
  10280. "Justification and field motion for `orgtbl-mode'."
  10281. (interactive "P")
  10282. (if arg (org-table-edit-field t)
  10283. (org-table-justify-field-maybe)
  10284. (org-table-next-field)))
  10285. (defun orgtbl-ret ()
  10286. "Justification and field motion for `orgtbl-mode'."
  10287. (interactive)
  10288. (org-table-justify-field-maybe)
  10289. (org-table-next-row))
  10290. (defun orgtbl-self-insert-command (N)
  10291. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10292. If the cursor is in a table looking at whitespace, the whitespace is
  10293. overwritten, and the table is not marked as requiring realignment."
  10294. (interactive "p")
  10295. (if (and (org-at-table-p)
  10296. (or
  10297. (and org-table-auto-blank-field
  10298. (member last-command
  10299. '(orgtbl-hijacker-command-100
  10300. orgtbl-hijacker-command-101
  10301. orgtbl-hijacker-command-102
  10302. orgtbl-hijacker-command-103
  10303. orgtbl-hijacker-command-104
  10304. orgtbl-hijacker-command-105))
  10305. (org-table-blank-field))
  10306. t)
  10307. (eq N 1)
  10308. (looking-at "[^|\n]* +|"))
  10309. (let (org-table-may-need-update)
  10310. (goto-char (1- (match-end 0)))
  10311. (delete-backward-char 1)
  10312. (goto-char (match-beginning 0))
  10313. (self-insert-command N))
  10314. (setq org-table-may-need-update t)
  10315. (let (orgtbl-mode)
  10316. (call-interactively (key-binding (vector last-input-event))))))
  10317. (defun org-force-self-insert (N)
  10318. "Needed to enforce self-insert under remapping."
  10319. (interactive "p")
  10320. (self-insert-command N))
  10321. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10322. "Regula expression matching exponentials as produced by calc.")
  10323. (defvar org-table-clean-did-remove-column nil)
  10324. (defun orgtbl-export (table target)
  10325. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10326. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10327. org-table-last-alignment org-table-last-column-widths
  10328. maxcol column)
  10329. (if (not (fboundp func))
  10330. (error "Cannot export orgtbl table to %s" target))
  10331. (setq lines (org-table-clean-before-export lines))
  10332. (setq table
  10333. (mapcar
  10334. (lambda (x)
  10335. (if (string-match org-table-hline-regexp x)
  10336. 'hline
  10337. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10338. lines))
  10339. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10340. table)))
  10341. (loop for i from (1- maxcol) downto 0 do
  10342. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10343. (setq column (delq nil column))
  10344. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10345. (push (> (/ (apply '+ (mapcar (lambda (x) (if (string-match org-table-number-regexp x) 1 0)) column)) maxcol) org-table-number-fraction) org-table-last-alignment))
  10346. (funcall func table nil)))
  10347. (defun orgtbl-send-table (&optional maybe)
  10348. "Send a tranformed version of this table to the receiver position.
  10349. With argument MAYBE, fail quietly if no transformation is defined for
  10350. this table."
  10351. (interactive)
  10352. (catch 'exit
  10353. (unless (org-at-table-p) (error "Not at a table"))
  10354. ;; when non-interactive, we assume align has just happened.
  10355. (when (interactive-p) (org-table-align))
  10356. (save-excursion
  10357. (goto-char (org-table-begin))
  10358. (beginning-of-line 0)
  10359. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10360. (if maybe
  10361. (throw 'exit nil)
  10362. (error "Don't know how to transform this table."))))
  10363. (let* ((name (match-string 1))
  10364. beg
  10365. (transform (intern (match-string 2)))
  10366. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10367. (skip (plist-get params :skip))
  10368. (skipcols (plist-get params :skipcols))
  10369. (txt (buffer-substring-no-properties
  10370. (org-table-begin) (org-table-end)))
  10371. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10372. (lines (org-table-clean-before-export lines))
  10373. (i0 (if org-table-clean-did-remove-column 2 1))
  10374. (table (mapcar
  10375. (lambda (x)
  10376. (if (string-match org-table-hline-regexp x)
  10377. 'hline
  10378. (org-remove-by-index
  10379. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10380. skipcols i0)))
  10381. lines))
  10382. (fun (if (= i0 2) 'cdr 'identity))
  10383. (org-table-last-alignment
  10384. (org-remove-by-index (funcall fun org-table-last-alignment)
  10385. skipcols i0))
  10386. (org-table-last-column-widths
  10387. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10388. skipcols i0)))
  10389. (unless (fboundp transform)
  10390. (error "No such transformation function %s" transform))
  10391. (setq txt (funcall transform table params))
  10392. ;; Find the insertion place
  10393. (save-excursion
  10394. (goto-char (point-min))
  10395. (unless (re-search-forward
  10396. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10397. (error "Don't know where to insert translated table"))
  10398. (goto-char (match-beginning 0))
  10399. (beginning-of-line 2)
  10400. (setq beg (point))
  10401. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10402. (error "Cannot find end of insertion region"))
  10403. (beginning-of-line 1)
  10404. (delete-region beg (point))
  10405. (goto-char beg)
  10406. (insert txt "\n"))
  10407. (message "Table converted and installed at receiver location"))))
  10408. (defun org-remove-by-index (list indices &optional i0)
  10409. "Remove the elements in LIST with indices in INDICES.
  10410. First element has index 0, or I0 if given."
  10411. (if (not indices)
  10412. list
  10413. (if (integerp indices) (setq indices (list indices)))
  10414. (setq i0 (1- (or i0 0)))
  10415. (delq :rm (mapcar (lambda (x)
  10416. (setq i0 (1+ i0))
  10417. (if (memq i0 indices) :rm x))
  10418. list))))
  10419. (defun orgtbl-toggle-comment ()
  10420. "Comment or uncomment the orgtbl at point."
  10421. (interactive)
  10422. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10423. (re2 (concat "^" orgtbl-line-start-regexp))
  10424. (commented (save-excursion (beginning-of-line 1)
  10425. (cond ((looking-at re1) t)
  10426. ((looking-at re2) nil)
  10427. (t (error "Not at an org table")))))
  10428. (re (if commented re1 re2))
  10429. beg end)
  10430. (save-excursion
  10431. (beginning-of-line 1)
  10432. (while (looking-at re) (beginning-of-line 0))
  10433. (beginning-of-line 2)
  10434. (setq beg (point))
  10435. (while (looking-at re) (beginning-of-line 2))
  10436. (setq end (point)))
  10437. (comment-region beg end (if commented '(4) nil))))
  10438. (defun orgtbl-insert-radio-table ()
  10439. "Insert a radio table template appropriate for this major mode."
  10440. (interactive)
  10441. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10442. (txt (nth 1 e))
  10443. name pos)
  10444. (unless e (error "No radio table setup defined for %s" major-mode))
  10445. (setq name (read-string "Table name: "))
  10446. (while (string-match "%n" txt)
  10447. (setq txt (replace-match name t t txt)))
  10448. (or (bolp) (insert "\n"))
  10449. (setq pos (point))
  10450. (insert txt)
  10451. (goto-char pos)))
  10452. (defun org-get-param (params header i sym &optional hsym)
  10453. "Get parameter value for symbol SYM.
  10454. If this is a header line, actually get the value for the symbol with an
  10455. additional \"h\" inserted after the colon.
  10456. If the value is a protperty list, get the element for the current column.
  10457. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10458. (let ((val (plist-get params sym)))
  10459. (and hsym header (setq val (or (plist-get params hsym) val)))
  10460. (if (consp val) (plist-get val i) val)))
  10461. (defun orgtbl-to-generic (table params)
  10462. "Convert the orgtbl-mode TABLE to some other format.
  10463. This generic routine can be used for many standard cases.
  10464. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10465. separator line, or a list of fields for that line.
  10466. PARAMS is a property list of parameters that can influence the conversion.
  10467. For the generic converter, some parameters are obligatory: You need to
  10468. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10469. :splice, you must have :tstart and :tend.
  10470. Valid parameters are
  10471. :tstart String to start the table. Ignored when :splice is t.
  10472. :tend String to end the table. Ignored when :splice is t.
  10473. :splice When set to t, return only table body lines, don't wrap
  10474. them into :tstart and :tend. Default is nil.
  10475. :hline String to be inserted on horizontal separation lines.
  10476. May be nil to ignore hlines.
  10477. :lstart String to start a new table line.
  10478. :lend String to end a table line
  10479. :sep Separator between two fields
  10480. :lfmt Format for entire line, with enough %s to capture all fields.
  10481. If this is present, :lstart, :lend, and :sep are ignored.
  10482. :fmt A format to be used to wrap the field, should contain
  10483. %s for the original field value. For example, to wrap
  10484. everything in dollars, you could use :fmt \"$%s$\".
  10485. This may also be a property list with column numbers and
  10486. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10487. :hlstart :hlend :hlsep :hlfmt :hfmt
  10488. Same as above, specific for the header lines in the table.
  10489. All lines before the first hline are treated as header.
  10490. If any of these is not present, the data line value is used.
  10491. :efmt Use this format to print numbers with exponentials.
  10492. The format should have %s twice for inserting mantissa
  10493. and exponent, for example \"%s\\\\times10^{%s}\". This
  10494. may also be a property list with column numbers and
  10495. formats. :fmt will still be applied after :efmt.
  10496. In addition to this, the parameters :skip and :skipcols are always handled
  10497. directly by `orgtbl-send-table'. See manual."
  10498. (interactive)
  10499. (let* ((p params)
  10500. (splicep (plist-get p :splice))
  10501. (hline (plist-get p :hline))
  10502. rtn line i fm efm lfmt h)
  10503. ;; Do we have a header?
  10504. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10505. (setq h t))
  10506. ;; Put header
  10507. (unless splicep
  10508. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10509. ;; Now loop over all lines
  10510. (while (setq line (pop table))
  10511. (if (eq line 'hline)
  10512. ;; A horizontal separator line
  10513. (progn (if hline (push hline rtn))
  10514. (setq h nil)) ; no longer in header
  10515. ;; A normal line. Convert the fields, push line onto the result list
  10516. (setq i 0)
  10517. (setq line
  10518. (mapcar
  10519. (lambda (f)
  10520. (setq i (1+ i)
  10521. fm (org-get-param p h i :fmt :hfmt)
  10522. efm (org-get-param p h i :efmt))
  10523. (if (and efm (string-match orgtbl-exp-regexp f))
  10524. (setq f (format
  10525. efm (match-string 1 f) (match-string 2 f))))
  10526. (if fm (setq f (format fm f)))
  10527. f)
  10528. line))
  10529. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10530. (push (apply 'format lfmt line) rtn)
  10531. (push (concat
  10532. (org-get-param p h i :lstart :hlstart)
  10533. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10534. (org-get-param p h i :lend :hlend))
  10535. rtn))))
  10536. (unless splicep
  10537. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10538. (mapconcat 'identity (nreverse rtn) "\n")))
  10539. (defun orgtbl-to-latex (table params)
  10540. "Convert the orgtbl-mode TABLE to LaTeX.
  10541. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10542. separator line, or a list of fields for that line.
  10543. PARAMS is a property list of parameters that can influence the conversion.
  10544. Supports all parameters from `orgtbl-to-generic'. Most important for
  10545. LaTeX are:
  10546. :splice When set to t, return only table body lines, don't wrap
  10547. them into a tabular environment. Default is nil.
  10548. :fmt A format to be used to wrap the field, should contain %s for the
  10549. original field value. For example, to wrap everything in dollars,
  10550. use :fmt \"$%s$\". This may also be a property list with column
  10551. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10552. :efmt Format for transforming numbers with exponentials. The format
  10553. should have %s twice for inserting mantissa and exponent, for
  10554. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10555. This may also be a property list with column numbers and formats.
  10556. The general parameters :skip and :skipcols have already been applied when
  10557. this function is called."
  10558. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10559. org-table-last-alignment ""))
  10560. (params2
  10561. (list
  10562. :tstart (concat "\\begin{tabular}{" alignment "}")
  10563. :tend "\\end{tabular}"
  10564. :lstart "" :lend " \\\\" :sep " & "
  10565. :efmt "%s\\,(%s)" :hline "\\hline")))
  10566. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10567. (defun orgtbl-to-html (table params)
  10568. "Convert the orgtbl-mode TABLE to LaTeX.
  10569. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10570. separator line, or a list of fields for that line.
  10571. PARAMS is a property list of parameters that can influence the conversion.
  10572. Currently this function recognizes the following parameters:
  10573. :splice When set to t, return only table body lines, don't wrap
  10574. them into a <table> environment. Default is nil.
  10575. The general parameters :skip and :skipcols have already been applied when
  10576. this function is called. The function does *not* use `orgtbl-to-generic',
  10577. so you cannot specify parameters for it."
  10578. (let* ((splicep (plist-get params :splice))
  10579. html)
  10580. ;; Just call the formatter we already have
  10581. ;; We need to make text lines for it, so put the fields back together.
  10582. (setq html (org-format-org-table-html
  10583. (mapcar
  10584. (lambda (x)
  10585. (if (eq x 'hline)
  10586. "|----+----|"
  10587. (concat "| " (mapconcat 'identity x " | ") " |")))
  10588. table)
  10589. splicep))
  10590. (if (string-match "\n+\\'" html)
  10591. (setq html (replace-match "" t t html)))
  10592. html))
  10593. (defun orgtbl-to-texinfo (table params)
  10594. "Convert the orgtbl-mode TABLE to TeXInfo.
  10595. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10596. separator line, or a list of fields for that line.
  10597. PARAMS is a property list of parameters that can influence the conversion.
  10598. Supports all parameters from `orgtbl-to-generic'. Most important for
  10599. TeXInfo are:
  10600. :splice nil/t When set to t, return only table body lines, don't wrap
  10601. them into a multitable environment. Default is nil.
  10602. :fmt fmt A format to be used to wrap the field, should contain
  10603. %s for the original field value. For example, to wrap
  10604. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10605. This may also be a property list with column numbers and
  10606. formats. for example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10607. :cf \"f1 f2..\" The column fractions for the table. Bye default these
  10608. are computed automatically from the width of the columns
  10609. under org-mode.
  10610. The general parameters :skip and :skipcols have already been applied when
  10611. this function is called."
  10612. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10613. (colfrac (or (plist-get params :cf)
  10614. (mapconcat
  10615. (lambda (x) (format "%.3f" (/ (float x) total)))
  10616. org-table-last-column-widths " ")))
  10617. (params2
  10618. (list
  10619. :tstart (concat "@multitable @columnfractions " colfrac)
  10620. :tend "@end multitable"
  10621. :lstart "@item " :lend "" :sep " @tab "
  10622. :hlstart "@headitem ")))
  10623. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10624. ;;;; Link Stuff
  10625. ;;; Link abbreviations
  10626. (defun org-link-expand-abbrev (link)
  10627. "Apply replacements as defined in `org-link-abbrev-alist."
  10628. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10629. (let* ((key (match-string 1 link))
  10630. (as (or (assoc key org-link-abbrev-alist-local)
  10631. (assoc key org-link-abbrev-alist)))
  10632. (tag (and (match-end 2) (match-string 3 link)))
  10633. rpl)
  10634. (if (not as)
  10635. link
  10636. (setq rpl (cdr as))
  10637. (cond
  10638. ((symbolp rpl) (funcall rpl tag))
  10639. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10640. (t (concat rpl tag)))))
  10641. link))
  10642. ;;; Storing and inserting links
  10643. (defvar org-insert-link-history nil
  10644. "Minibuffer history for links inserted with `org-insert-link'.")
  10645. (defvar org-stored-links nil
  10646. "Contains the links stored with `org-store-link'.")
  10647. (defvar org-store-link-plist nil
  10648. "Plist with info about the most recently link created with `org-store-link'.")
  10649. (defvar org-link-protocols nil
  10650. "Link protocols added to Org-mode using `org-add-link-type'.")
  10651. (defvar org-store-link-functions nil
  10652. "List of functions that are called to create and store a link.
  10653. Each function will be called in turn until one returns a non-nil
  10654. value. Each function should check if it is responsible for creating
  10655. this link (for example by looking at the major mode).
  10656. If not, it must exit and return nil.
  10657. If yes, it should return a non-nil value after a calling
  10658. `org-store-link-props' with a list of properties and values.
  10659. Special properties are:
  10660. :type The link prefix. like \"http\". This must be given.
  10661. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10662. This is obligatory as well.
  10663. :description Optional default description for the second pair
  10664. of brackets in an Org-mode link. The user can still change
  10665. this when inserting this link into an Org-mode buffer.
  10666. In addition to these, any additional properties can be specified
  10667. and then used in remember templates.")
  10668. (defun org-add-link-type (type &optional follow publish)
  10669. "Add TYPE to the list of `org-link-types'.
  10670. Re-compute all regular expressions depending on `org-link-types'
  10671. FOLLOW and PUBLISH are two functions. Both take the link path as
  10672. an argument.
  10673. FOLLOW should do whatever is necessary to follow the link, for example
  10674. to find a file or display a mail message.
  10675. PUBLISH takes the path and retuns the string that should be used when
  10676. this document is published. FIMXE: This is actually not yet implemented."
  10677. (add-to-list 'org-link-types type t)
  10678. (org-make-link-regexps)
  10679. (add-to-list 'org-link-protocols
  10680. (list type follow publish)))
  10681. (defun org-add-agenda-custom-command (entry)
  10682. "Replace or add a command in `org-agenda-custom-commands'.
  10683. This is mostly for hacking and trying a new command - once the command
  10684. works you probably want to add it to `org-agenda-custom-commands' for good."
  10685. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10686. (if ass
  10687. (setcdr ass (cdr entry))
  10688. (push entry org-agenda-custom-commands))))
  10689. ;;;###autoload
  10690. (defun org-store-link (arg)
  10691. "\\<org-mode-map>Store an org-link to the current location.
  10692. This link can later be inserted into an org-buffer with
  10693. \\[org-insert-link].
  10694. For some link types, a prefix arg is interpreted:
  10695. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10696. For file links, arg negates `org-context-in-file-links'."
  10697. (interactive "P")
  10698. (setq org-store-link-plist nil) ; reset
  10699. (let (link cpltxt desc description search txt)
  10700. (cond
  10701. ((run-hook-with-args-until-success 'org-store-link-functions)
  10702. (setq link (plist-get org-store-link-plist :link)
  10703. desc (or (plist-get org-store-link-plist :description) link)))
  10704. ((eq major-mode 'bbdb-mode)
  10705. (let ((name (bbdb-record-name (bbdb-current-record)))
  10706. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10707. (setq cpltxt (concat "bbdb:" (or name company))
  10708. link (org-make-link cpltxt))
  10709. (org-store-link-props :type "bbdb" :name name :company company)))
  10710. ((eq major-mode 'Info-mode)
  10711. (setq link (org-make-link "info:"
  10712. (file-name-nondirectory Info-current-file)
  10713. ":" Info-current-node))
  10714. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10715. ":" Info-current-node))
  10716. (org-store-link-props :type "info" :file Info-current-file
  10717. :node Info-current-node))
  10718. ((eq major-mode 'calendar-mode)
  10719. (let ((cd (calendar-cursor-to-date)))
  10720. (setq link
  10721. (format-time-string
  10722. (car org-time-stamp-formats)
  10723. (apply 'encode-time
  10724. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10725. nil nil nil))))
  10726. (org-store-link-props :type "calendar" :date cd)))
  10727. ((or (eq major-mode 'vm-summary-mode)
  10728. (eq major-mode 'vm-presentation-mode))
  10729. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10730. (vm-follow-summary-cursor)
  10731. (save-excursion
  10732. (vm-select-folder-buffer)
  10733. (let* ((message (car vm-message-pointer))
  10734. (folder buffer-file-name)
  10735. (subject (vm-su-subject message))
  10736. (to (vm-get-header-contents message "To"))
  10737. (from (vm-get-header-contents message "From"))
  10738. (message-id (vm-su-message-id message)))
  10739. (org-store-link-props :type "vm" :from from :to to :subject subject
  10740. :message-id message-id)
  10741. (setq message-id (org-remove-angle-brackets message-id))
  10742. (setq folder (abbreviate-file-name folder))
  10743. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10744. folder)
  10745. (setq folder (replace-match "" t t folder)))
  10746. (setq cpltxt (org-email-link-description))
  10747. (setq link (org-make-link "vm:" folder "#" message-id)))))
  10748. ((eq major-mode 'wl-summary-mode)
  10749. (let* ((msgnum (wl-summary-message-number))
  10750. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  10751. msgnum 'message-id))
  10752. (wl-message-entity
  10753. (if (fboundp 'elmo-message-entity)
  10754. (elmo-message-entity
  10755. wl-summary-buffer-elmo-folder msgnum)
  10756. (elmo-msgdb-overview-get-entity
  10757. msgnum (wl-summary-buffer-msgdb))))
  10758. (from (wl-summary-line-from))
  10759. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  10760. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  10761. (wl-summary-line-subject))))
  10762. (org-store-link-props :type "wl" :from from :to to
  10763. :subject subject :message-id message-id)
  10764. (setq message-id (org-remove-angle-brackets message-id))
  10765. (setq cpltxt (org-email-link-description))
  10766. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  10767. "#" message-id))))
  10768. ((or (equal major-mode 'mh-folder-mode)
  10769. (equal major-mode 'mh-show-mode))
  10770. (let ((from (org-mhe-get-header "From:"))
  10771. (to (org-mhe-get-header "To:"))
  10772. (message-id (org-mhe-get-header "Message-Id:"))
  10773. (subject (org-mhe-get-header "Subject:")))
  10774. (org-store-link-props :type "mh" :from from :to to
  10775. :subject subject :message-id message-id)
  10776. (setq cpltxt (org-email-link-description))
  10777. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  10778. (org-remove-angle-brackets message-id)))))
  10779. ((eq major-mode 'rmail-mode)
  10780. (save-excursion
  10781. (save-restriction
  10782. (rmail-narrow-to-non-pruned-header)
  10783. (let ((folder buffer-file-name)
  10784. (message-id (mail-fetch-field "message-id"))
  10785. (from (mail-fetch-field "from"))
  10786. (to (mail-fetch-field "to"))
  10787. (subject (mail-fetch-field "subject")))
  10788. (org-store-link-props
  10789. :type "rmail" :from from :to to
  10790. :subject subject :message-id message-id)
  10791. (setq message-id (org-remove-angle-brackets message-id))
  10792. (setq cpltxt (org-email-link-description))
  10793. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  10794. ((eq major-mode 'gnus-group-mode)
  10795. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  10796. (gnus-group-group-name)) ; version
  10797. ((fboundp 'gnus-group-name)
  10798. (gnus-group-name))
  10799. (t "???"))))
  10800. (unless group (error "Not on a group"))
  10801. (org-store-link-props :type "gnus" :group group)
  10802. (setq cpltxt (concat
  10803. (if (org-xor arg org-usenet-links-prefer-google)
  10804. "http://groups.google.com/groups?group="
  10805. "gnus:")
  10806. group)
  10807. link (org-make-link cpltxt))))
  10808. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  10809. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  10810. (let* ((group gnus-newsgroup-name)
  10811. (article (gnus-summary-article-number))
  10812. (header (gnus-summary-article-header article))
  10813. (from (mail-header-from header))
  10814. (message-id (mail-header-id header))
  10815. (date (mail-header-date header))
  10816. (subject (gnus-summary-subject-string)))
  10817. (org-store-link-props :type "gnus" :from from :subject subject
  10818. :message-id message-id :group group)
  10819. (setq cpltxt (org-email-link-description))
  10820. (if (org-xor arg org-usenet-links-prefer-google)
  10821. (setq link
  10822. (concat
  10823. cpltxt "\n "
  10824. (format "http://groups.google.com/groups?as_umsgid=%s"
  10825. (org-fixup-message-id-for-http message-id))))
  10826. (setq link (org-make-link "gnus:" group
  10827. "#" (number-to-string article))))))
  10828. ((eq major-mode 'w3-mode)
  10829. (setq cpltxt (url-view-url t)
  10830. link (org-make-link cpltxt))
  10831. (org-store-link-props :type "w3" :url (url-view-url t)))
  10832. ((eq major-mode 'w3m-mode)
  10833. (setq cpltxt (or w3m-current-title w3m-current-url)
  10834. link (org-make-link w3m-current-url))
  10835. (org-store-link-props :type "w3m" :url (url-view-url t)))
  10836. ((setq search (run-hook-with-args-until-success
  10837. 'org-create-file-search-functions))
  10838. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  10839. "::" search))
  10840. (setq cpltxt (or description link)))
  10841. ((eq major-mode 'image-mode)
  10842. (setq cpltxt (concat "file:"
  10843. (abbreviate-file-name buffer-file-name))
  10844. link (org-make-link cpltxt))
  10845. (org-store-link-props :type "image" :file buffer-file-name))
  10846. ((eq major-mode 'dired-mode)
  10847. ;; link to the file in the current line
  10848. (setq cpltxt (concat "file:"
  10849. (abbreviate-file-name
  10850. (expand-file-name
  10851. (dired-get-filename nil t))))
  10852. link (org-make-link cpltxt)))
  10853. ((and buffer-file-name (org-mode-p))
  10854. ;; Just link to current headline
  10855. (setq cpltxt (concat "file:"
  10856. (abbreviate-file-name buffer-file-name)))
  10857. ;; Add a context search string
  10858. (when (org-xor org-context-in-file-links arg)
  10859. ;; Check if we are on a target
  10860. (if (org-in-regexp "<<\\(.*?\\)>>")
  10861. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  10862. (setq txt (cond
  10863. ((org-on-heading-p) nil)
  10864. ((org-region-active-p)
  10865. (buffer-substring (region-beginning) (region-end)))
  10866. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  10867. (when (or (null txt) (string-match "\\S-" txt))
  10868. (setq cpltxt
  10869. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10870. desc "NONE"))))
  10871. (if (string-match "::\\'" cpltxt)
  10872. (setq cpltxt (substring cpltxt 0 -2)))
  10873. (setq link (org-make-link cpltxt)))
  10874. ((buffer-file-name (buffer-base-buffer))
  10875. ;; Just link to this file here.
  10876. (setq cpltxt (concat "file:"
  10877. (abbreviate-file-name
  10878. (buffer-file-name (buffer-base-buffer)))))
  10879. ;; Add a context string
  10880. (when (org-xor org-context-in-file-links arg)
  10881. (setq txt (if (org-region-active-p)
  10882. (buffer-substring (region-beginning) (region-end))
  10883. (buffer-substring (point-at-bol) (point-at-eol))))
  10884. ;; Only use search option if there is some text.
  10885. (when (string-match "\\S-" txt)
  10886. (setq cpltxt
  10887. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10888. desc "NONE")))
  10889. (setq link (org-make-link cpltxt)))
  10890. ((interactive-p)
  10891. (error "Cannot link to a buffer which is not visiting a file"))
  10892. (t (setq link nil)))
  10893. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  10894. (setq link (or link cpltxt)
  10895. desc (or desc cpltxt))
  10896. (if (equal desc "NONE") (setq desc nil))
  10897. (if (and (interactive-p) link)
  10898. (progn
  10899. (setq org-stored-links
  10900. (cons (list link desc) org-stored-links))
  10901. (message "Stored: %s" (or desc link)))
  10902. (and link (org-make-link-string link desc)))))
  10903. (defun org-store-link-props (&rest plist)
  10904. "Store link properties, extract names and addresses."
  10905. (let (x adr)
  10906. (when (setq x (plist-get plist :from))
  10907. (setq adr (mail-extract-address-components x))
  10908. (plist-put plist :fromname (car adr))
  10909. (plist-put plist :fromaddress (nth 1 adr)))
  10910. (when (setq x (plist-get plist :to))
  10911. (setq adr (mail-extract-address-components x))
  10912. (plist-put plist :toname (car adr))
  10913. (plist-put plist :toaddress (nth 1 adr))))
  10914. (let ((from (plist-get plist :from))
  10915. (to (plist-get plist :to)))
  10916. (when (and from to org-from-is-user-regexp)
  10917. (plist-put plist :fromto
  10918. (if (string-match org-from-is-user-regexp from)
  10919. (concat "to %t")
  10920. (concat "from %f")))))
  10921. (setq org-store-link-plist plist))
  10922. (defun org-email-link-description (&optional fmt)
  10923. "Return the description part of an email link.
  10924. This takes information from `org-store-link-plist' and formats it
  10925. according to FMT (default from `org-email-link-description-format')."
  10926. (setq fmt (or fmt org-email-link-description-format))
  10927. (let* ((p org-store-link-plist)
  10928. (to (plist-get p :toaddress))
  10929. (from (plist-get p :fromaddress))
  10930. (table
  10931. (list
  10932. (cons "%c" (plist-get p :fromto))
  10933. (cons "%F" (plist-get p :from))
  10934. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  10935. (cons "%T" (plist-get p :to))
  10936. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  10937. (cons "%s" (plist-get p :subject))
  10938. (cons "%m" (plist-get p :message-id)))))
  10939. (when (string-match "%c" fmt)
  10940. ;; Check if the user wrote this message
  10941. (if (and org-from-is-user-regexp from to
  10942. (save-match-data (string-match org-from-is-user-regexp from)))
  10943. (setq fmt (replace-match "to %t" t t fmt))
  10944. (setq fmt (replace-match "from %f" t t fmt))))
  10945. (org-replace-escapes fmt table)))
  10946. (defun org-make-org-heading-search-string (&optional string heading)
  10947. "Make search string for STRING or current headline."
  10948. (interactive)
  10949. (let ((s (or string (org-get-heading))))
  10950. (unless (and string (not heading))
  10951. ;; We are using a headline, clean up garbage in there.
  10952. (if (string-match org-todo-regexp s)
  10953. (setq s (replace-match "" t t s)))
  10954. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  10955. (setq s (replace-match "" t t s)))
  10956. (setq s (org-trim s))
  10957. (if (string-match (concat "^\\(" org-quote-string "\\|"
  10958. org-comment-string "\\)") s)
  10959. (setq s (replace-match "" t t s)))
  10960. (while (string-match org-ts-regexp s)
  10961. (setq s (replace-match "" t t s))))
  10962. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  10963. (setq s (replace-match " " t t s)))
  10964. (or string (setq s (concat "*" s))) ; Add * for headlines
  10965. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  10966. (defun org-make-link (&rest strings)
  10967. "Concatenate STRINGS."
  10968. (apply 'concat strings))
  10969. (defun org-make-link-string (link &optional description)
  10970. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  10971. (unless (string-match "\\S-" link)
  10972. (error "Empty link"))
  10973. (when (stringp description)
  10974. ;; Remove brackets from the description, they are fatal.
  10975. (while (string-match "\\[" description)
  10976. (setq description (replace-match "{" t t description)))
  10977. (while (string-match "\\]" description)
  10978. (setq description (replace-match "}" t t description))))
  10979. (when (equal (org-link-escape link) description)
  10980. ;; No description needed, it is identical
  10981. (setq description nil))
  10982. (when (and (not description)
  10983. (not (equal link (org-link-escape link))))
  10984. (setq description link))
  10985. (concat "[[" (org-link-escape link) "]"
  10986. (if description (concat "[" description "]") "")
  10987. "]"))
  10988. (defconst org-link-escape-chars
  10989. '((?\ . "%20")
  10990. (?\[ . "%5B")
  10991. (?\] . "%5D")
  10992. (?\340 . "%E0") ; `a
  10993. (?\342 . "%E2") ; ^a
  10994. (?\347 . "%E7") ; ,c
  10995. (?\350 . "%E8") ; `e
  10996. (?\351 . "%E9") ; 'e
  10997. (?\352 . "%EA") ; ^e
  10998. (?\356 . "%EE") ; ^i
  10999. (?\364 . "%F4") ; ^o
  11000. (?\371 . "%F9") ; `u
  11001. (?\373 . "%FB") ; ^u
  11002. (?\; . "%3B")
  11003. (?? . "%3F")
  11004. (?= . "%3D")
  11005. (?+ . "%2B")
  11006. )
  11007. "Association list of escapes for some characters problematic in links.
  11008. This is the list that is used for internal purposes.")
  11009. (defconst org-link-escape-chars-browser
  11010. '((?\ . "%20")) ; 32 for the SPC char
  11011. "Association list of escapes for some characters problematic in links.
  11012. This is the list that is used before handing over to the browser.")
  11013. (defun org-link-escape (text &optional table)
  11014. "Escape charaters in TEXT that are problematic for links."
  11015. (setq table (or table org-link-escape-chars))
  11016. (when text
  11017. (let ((re (mapconcat (lambda (x) (regexp-quote
  11018. (char-to-string (car x))))
  11019. table "\\|")))
  11020. (while (string-match re text)
  11021. (setq text
  11022. (replace-match
  11023. (cdr (assoc (string-to-char (match-string 0 text))
  11024. table))
  11025. t t text)))
  11026. text)))
  11027. (defun org-link-unescape (text &optional table)
  11028. "Reverse the action of `org-link-escape'."
  11029. (setq table (or table org-link-escape-chars))
  11030. (when text
  11031. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11032. table "\\|")))
  11033. (while (string-match re text)
  11034. (setq text
  11035. (replace-match
  11036. (char-to-string (car (rassoc (match-string 0 text) table)))
  11037. t t text)))
  11038. text)))
  11039. (defun org-xor (a b)
  11040. "Exclusive or."
  11041. (if a (not b) b))
  11042. (defun org-get-header (header)
  11043. "Find a header field in the current buffer."
  11044. (save-excursion
  11045. (goto-char (point-min))
  11046. (let ((case-fold-search t) s)
  11047. (cond
  11048. ((eq header 'from)
  11049. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11050. (setq s (match-string 1)))
  11051. (while (string-match "\"" s)
  11052. (setq s (replace-match "" t t s)))
  11053. (if (string-match "[<(].*" s)
  11054. (setq s (replace-match "" t t s))))
  11055. ((eq header 'message-id)
  11056. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11057. (setq s (match-string 1))))
  11058. ((eq header 'subject)
  11059. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11060. (setq s (match-string 1)))))
  11061. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11062. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11063. s)))
  11064. (defun org-fixup-message-id-for-http (s)
  11065. "Replace special characters in a message id, so it can be used in an http query."
  11066. (while (string-match "<" s)
  11067. (setq s (replace-match "%3C" t t s)))
  11068. (while (string-match ">" s)
  11069. (setq s (replace-match "%3E" t t s)))
  11070. (while (string-match "@" s)
  11071. (setq s (replace-match "%40" t t s)))
  11072. s)
  11073. ;;;###autoload
  11074. (defun org-insert-link-global ()
  11075. "Insert a link like Org-mode does.
  11076. This command can be called in any mode to insert a link in Org-mode syntax."
  11077. (interactive)
  11078. (org-run-like-in-org-mode 'org-insert-link))
  11079. (defun org-insert-link (&optional complete-file)
  11080. "Insert a link. At the prompt, enter the link.
  11081. Completion can be used to select a link previously stored with
  11082. `org-store-link'. When the empty string is entered (i.e. if you just
  11083. press RET at the prompt), the link defaults to the most recently
  11084. stored link. As SPC triggers completion in the minibuffer, you need to
  11085. use M-SPC or C-q SPC to force the insertion of a space character.
  11086. You will also be prompted for a description, and if one is given, it will
  11087. be displayed in the buffer instead of the link.
  11088. If there is already a link at point, this command will allow you to edit link
  11089. and description parts.
  11090. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11091. selected using completion. The path to the file will be relative to
  11092. the current directory if the file is in the current directory or a
  11093. subdirectory. Otherwise, the link will be the absolute path as
  11094. completed in the minibuffer (i.e. normally ~/path/to/file).
  11095. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11096. is in the current directory or below.
  11097. With three \\[universal-argument] prefixes, negate the meaning of
  11098. `org-keep-stored-link-after-insertion'."
  11099. (interactive "P")
  11100. (let* ((wcf (current-window-configuration))
  11101. (region (if (org-region-active-p)
  11102. (buffer-substring (region-beginning) (region-end))))
  11103. (remove (and region (list (region-beginning) (region-end))))
  11104. (desc region)
  11105. tmphist ; byte-compile incorrectly complains about this
  11106. link entry file)
  11107. (cond
  11108. ((org-in-regexp org-bracket-link-regexp 1)
  11109. ;; We do have a link at point, and we are going to edit it.
  11110. (setq remove (list (match-beginning 0) (match-end 0)))
  11111. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11112. (setq link (read-string "Link: "
  11113. (org-link-unescape
  11114. (org-match-string-no-properties 1)))))
  11115. ((or (org-in-regexp org-angle-link-re)
  11116. (org-in-regexp org-plain-link-re))
  11117. ;; Convert to bracket link
  11118. (setq remove (list (match-beginning 0) (match-end 0))
  11119. link (read-string "Link: "
  11120. (org-remove-angle-brackets (match-string 0)))))
  11121. ((equal complete-file '(4))
  11122. ;; Completing read for file names.
  11123. (setq file (read-file-name "File: "))
  11124. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11125. (pwd1 (file-name-as-directory (abbreviate-file-name
  11126. (expand-file-name ".")))))
  11127. (cond
  11128. ((equal complete-file '(16))
  11129. (setq link (org-make-link
  11130. "file:"
  11131. (abbreviate-file-name (expand-file-name file)))))
  11132. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11133. (setq link (org-make-link "file:" (match-string 1 file))))
  11134. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11135. (expand-file-name file))
  11136. (setq link (org-make-link
  11137. "file:" (match-string 1 (expand-file-name file)))))
  11138. (t (setq link (org-make-link "file:" file))))))
  11139. (t
  11140. ;; Read link, with completion for stored links.
  11141. (with-output-to-temp-buffer "*Org Links*"
  11142. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11143. (when org-stored-links
  11144. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11145. (princ (mapconcat
  11146. (lambda (x)
  11147. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11148. (reverse org-stored-links) "\n"))))
  11149. (let ((cw (selected-window)))
  11150. (select-window (get-buffer-window "*Org Links*"))
  11151. (shrink-window-if-larger-than-buffer)
  11152. (setq truncate-lines t)
  11153. (select-window cw))
  11154. ;; Fake a link history, containing the stored links.
  11155. (setq tmphist (append (mapcar 'car org-stored-links)
  11156. org-insert-link-history))
  11157. (unwind-protect
  11158. (setq link (org-completing-read
  11159. "Link: "
  11160. (append
  11161. (mapcar (lambda (x) (list (concat (car x) ":")))
  11162. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11163. (mapcar (lambda (x) (list (concat x ":")))
  11164. org-link-types))
  11165. nil nil nil
  11166. 'tmphist
  11167. (or (car (car org-stored-links)))))
  11168. (set-window-configuration wcf)
  11169. (kill-buffer "*Org Links*"))
  11170. (setq entry (assoc link org-stored-links))
  11171. (or entry (push link org-insert-link-history))
  11172. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11173. (not org-keep-stored-link-after-insertion))
  11174. (setq org-stored-links (delq (assoc link org-stored-links)
  11175. org-stored-links)))
  11176. (setq desc (or desc (nth 1 entry)))))
  11177. (if (string-match org-plain-link-re link)
  11178. ;; URL-like link, normalize the use of angular brackets.
  11179. (setq link (org-make-link (org-remove-angle-brackets link))))
  11180. ;; Check if we are linking to the current file with a search option
  11181. ;; If yes, simplify the link by using only the search option.
  11182. (when (and buffer-file-name
  11183. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11184. (let* ((path (match-string 1 link))
  11185. (case-fold-search nil)
  11186. (search (match-string 2 link)))
  11187. (save-match-data
  11188. (if (equal (file-truename buffer-file-name) (file-truename path))
  11189. ;; We are linking to this same file, with a search option
  11190. (setq link search)))))
  11191. ;; Check if we can/should use a relative path. If yes, simplify the link
  11192. (when (string-match "\\<file:\\(.*\\)" link)
  11193. (let* ((path (match-string 1 link))
  11194. (origpath path)
  11195. (desc-is-link (equal link desc))
  11196. (case-fold-search nil))
  11197. (cond
  11198. ((eq org-link-file-path-type 'absolute)
  11199. (setq path (abbreviate-file-name (expand-file-name path))))
  11200. ((eq org-link-file-path-type 'noabbrev)
  11201. (setq path (expand-file-name path)))
  11202. ((eq org-link-file-path-type 'relative)
  11203. (setq path (file-relative-name path)))
  11204. (t
  11205. (save-match-data
  11206. (if (string-match (concat "^" (regexp-quote
  11207. (file-name-as-directory
  11208. (expand-file-name "."))))
  11209. (expand-file-name path))
  11210. ;; We are linking a file with relative path name.
  11211. (setq path (substring (expand-file-name path)
  11212. (match-end 0)))))))
  11213. (setq link (concat "file:" path))
  11214. (if (equal desc origpath)
  11215. (setq desc path))))
  11216. (setq desc (read-string "Description: " desc))
  11217. (unless (string-match "\\S-" desc) (setq desc nil))
  11218. (if remove (apply 'delete-region remove))
  11219. (insert (org-make-link-string link desc))))
  11220. (defun org-completing-read (&rest args)
  11221. (let ((minibuffer-local-completion-map
  11222. (copy-keymap minibuffer-local-completion-map)))
  11223. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11224. (apply 'completing-read args)))
  11225. ;;; Opening/following a link
  11226. (defvar org-link-search-failed nil)
  11227. (defun org-next-link ()
  11228. "Move forward to the next link.
  11229. If the link is in hidden text, expose it."
  11230. (interactive)
  11231. (when (and org-link-search-failed (eq this-command last-command))
  11232. (goto-char (point-min))
  11233. (message "Link search wrapped back to beginning of buffer"))
  11234. (setq org-link-search-failed nil)
  11235. (let* ((pos (point))
  11236. (ct (org-context))
  11237. (a (assoc :link ct)))
  11238. (if a (goto-char (nth 2 a)))
  11239. (if (re-search-forward org-any-link-re nil t)
  11240. (progn
  11241. (goto-char (match-beginning 0))
  11242. (if (org-invisible-p) (org-show-context)))
  11243. (goto-char pos)
  11244. (setq org-link-search-failed t)
  11245. (error "No further link found"))))
  11246. (defun org-previous-link ()
  11247. "Move backward to the previous link.
  11248. If the link is in hidden text, expose it."
  11249. (interactive)
  11250. (when (and org-link-search-failed (eq this-command last-command))
  11251. (goto-char (point-max))
  11252. (message "Link search wrapped back to end of buffer"))
  11253. (setq org-link-search-failed nil)
  11254. (let* ((pos (point))
  11255. (ct (org-context))
  11256. (a (assoc :link ct)))
  11257. (if a (goto-char (nth 1 a)))
  11258. (if (re-search-backward org-any-link-re nil t)
  11259. (progn
  11260. (goto-char (match-beginning 0))
  11261. (if (org-invisible-p) (org-show-context)))
  11262. (goto-char pos)
  11263. (setq org-link-search-failed t)
  11264. (error "No further link found"))))
  11265. (defun org-find-file-at-mouse (ev)
  11266. "Open file link or URL at mouse."
  11267. (interactive "e")
  11268. (mouse-set-point ev)
  11269. (org-open-at-point 'in-emacs))
  11270. (defun org-open-at-mouse (ev)
  11271. "Open file link or URL at mouse."
  11272. (interactive "e")
  11273. (mouse-set-point ev)
  11274. (org-open-at-point))
  11275. (defvar org-window-config-before-follow-link nil
  11276. "The window configuration before following a link.
  11277. This is saved in case the need arises to restore it.")
  11278. (defvar org-open-link-marker (make-marker)
  11279. "Marker pointing to the location where `org-open-at-point; was called.")
  11280. ;;;###autoload
  11281. (defun org-open-at-point-global ()
  11282. "Follow a link like Org-mode does.
  11283. This command can be called in any mode to follow a link that has
  11284. Org-mode syntax."
  11285. (interactive)
  11286. (org-run-like-in-org-mode 'org-open-at-point))
  11287. (defun org-open-at-point (&optional in-emacs)
  11288. "Open link at or after point.
  11289. If there is no link at point, this function will search forward up to
  11290. the end of the current subtree.
  11291. Normally, files will be opened by an appropriate application. If the
  11292. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11293. (interactive "P")
  11294. (catch 'abort
  11295. (move-marker org-open-link-marker (point))
  11296. (setq org-window-config-before-follow-link (current-window-configuration))
  11297. (org-remove-occur-highlights nil nil t)
  11298. (if (org-at-timestamp-p t)
  11299. (org-follow-timestamp-link)
  11300. (let (type path link line search (pos (point)))
  11301. (catch 'match
  11302. (save-excursion
  11303. (skip-chars-forward "^]\n\r")
  11304. (when (org-in-regexp org-bracket-link-regexp)
  11305. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11306. (while (string-match " *\n *" link)
  11307. (setq link (replace-match " " t t link)))
  11308. (setq link (org-link-expand-abbrev link))
  11309. (if (string-match org-link-re-with-space2 link)
  11310. (setq type (match-string 1 link) path (match-string 2 link))
  11311. (setq type "thisfile" path link))
  11312. (throw 'match t)))
  11313. (when (get-text-property (point) 'org-linked-text)
  11314. (setq type "thisfile"
  11315. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11316. (1+ (point)) (point))
  11317. path (buffer-substring
  11318. (previous-single-property-change pos 'org-linked-text)
  11319. (next-single-property-change pos 'org-linked-text)))
  11320. (throw 'match t))
  11321. (save-excursion
  11322. (when (or (org-in-regexp org-angle-link-re)
  11323. (org-in-regexp org-plain-link-re))
  11324. (setq type (match-string 1) path (match-string 2))
  11325. (throw 'match t)))
  11326. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11327. (setq type "tree-match"
  11328. path (match-string 1))
  11329. (throw 'match t))
  11330. (save-excursion
  11331. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11332. (setq type "tags"
  11333. path (match-string 1))
  11334. (while (string-match ":" path)
  11335. (setq path (replace-match "+" t t path)))
  11336. (throw 'match t))))
  11337. (unless path
  11338. (error "No link found"))
  11339. ;; Remove any trailing spaces in path
  11340. (if (string-match " +\\'" path)
  11341. (setq path (replace-match "" t t path)))
  11342. (cond
  11343. ((assoc type org-link-protocols)
  11344. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11345. ((equal type "mailto")
  11346. (let ((cmd (car org-link-mailto-program))
  11347. (args (cdr org-link-mailto-program)) args1
  11348. (address path) (subject "") a)
  11349. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11350. (setq address (match-string 1 path)
  11351. subject (org-link-escape (match-string 2 path))))
  11352. (while args
  11353. (cond
  11354. ((not (stringp (car args))) (push (pop args) args1))
  11355. (t (setq a (pop args))
  11356. (if (string-match "%a" a)
  11357. (setq a (replace-match address t t a)))
  11358. (if (string-match "%s" a)
  11359. (setq a (replace-match subject t t a)))
  11360. (push a args1))))
  11361. (apply cmd (nreverse args1))))
  11362. ((member type '("http" "https" "ftp" "news"))
  11363. (browse-url (concat type ":" (org-link-escape
  11364. path org-link-escape-chars-browser))))
  11365. ((string= type "tags")
  11366. (org-tags-view in-emacs path))
  11367. ((string= type "thisfile")
  11368. (if in-emacs
  11369. (switch-to-buffer-other-window
  11370. (org-get-buffer-for-internal-link (current-buffer)))
  11371. (org-mark-ring-push))
  11372. (let ((cmd `(org-link-search
  11373. ,path
  11374. ,(cond ((equal in-emacs '(4)) 'occur)
  11375. ((equal in-emacs '(16)) 'org-occur)
  11376. (t nil))
  11377. ,pos)))
  11378. (condition-case nil (eval cmd)
  11379. (error (progn (widen) (eval cmd))))))
  11380. ((string= type "tree-match")
  11381. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11382. ((string= type "file")
  11383. (if (string-match "::\\([0-9]+\\)\\'" path)
  11384. (setq line (string-to-number (match-string 1 path))
  11385. path (substring path 0 (match-beginning 0)))
  11386. (if (string-match "::\\(.+\\)\\'" path)
  11387. (setq search (match-string 1 path)
  11388. path (substring path 0 (match-beginning 0)))))
  11389. (if (string-match "[*?{]" (file-name-nondirectory path))
  11390. (dired path)
  11391. (org-open-file path in-emacs line search)))
  11392. ((string= type "news")
  11393. (org-follow-gnus-link path))
  11394. ((string= type "bbdb")
  11395. (org-follow-bbdb-link path))
  11396. ((string= type "info")
  11397. (org-follow-info-link path))
  11398. ((string= type "gnus")
  11399. (let (group article)
  11400. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11401. (error "Error in Gnus link"))
  11402. (setq group (match-string 1 path)
  11403. article (match-string 3 path))
  11404. (org-follow-gnus-link group article)))
  11405. ((string= type "vm")
  11406. (let (folder article)
  11407. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11408. (error "Error in VM link"))
  11409. (setq folder (match-string 1 path)
  11410. article (match-string 3 path))
  11411. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11412. (org-follow-vm-link folder article in-emacs)))
  11413. ((string= type "wl")
  11414. (let (folder article)
  11415. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11416. (error "Error in Wanderlust link"))
  11417. (setq folder (match-string 1 path)
  11418. article (match-string 3 path))
  11419. (org-follow-wl-link folder article)))
  11420. ((string= type "mhe")
  11421. (let (folder article)
  11422. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11423. (error "Error in MHE link"))
  11424. (setq folder (match-string 1 path)
  11425. article (match-string 3 path))
  11426. (org-follow-mhe-link folder article)))
  11427. ((string= type "rmail")
  11428. (let (folder article)
  11429. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11430. (error "Error in RMAIL link"))
  11431. (setq folder (match-string 1 path)
  11432. article (match-string 3 path))
  11433. (org-follow-rmail-link folder article)))
  11434. ((string= type "shell")
  11435. (let ((cmd path))
  11436. (if (or (not org-confirm-shell-link-function)
  11437. (funcall org-confirm-shell-link-function
  11438. (format "Execute \"%s\" in shell? "
  11439. (org-add-props cmd nil
  11440. 'face 'org-warning))))
  11441. (progn
  11442. (message "Executing %s" cmd)
  11443. (shell-command cmd))
  11444. (error "Abort"))))
  11445. ((string= type "elisp")
  11446. (let ((cmd path))
  11447. (if (or (not org-confirm-elisp-link-function)
  11448. (funcall org-confirm-elisp-link-function
  11449. (format "Execute \"%s\" as elisp? "
  11450. (org-add-props cmd nil
  11451. 'face 'org-warning))))
  11452. (message "%s => %s" cmd (eval (read cmd)))
  11453. (error "Abort"))))
  11454. (t
  11455. (browse-url-at-point)))))
  11456. (move-marker org-open-link-marker nil)))
  11457. ;;; File search
  11458. (defvar org-create-file-search-functions nil
  11459. "List of functions to construct the right search string for a file link.
  11460. These functions are called in turn with point at the location to
  11461. which the link should point.
  11462. A function in the hook should first test if it would like to
  11463. handle this file type, for example by checking the major-mode or
  11464. the file extension. If it decides not to handle this file, it
  11465. should just return nil to give other functions a chance. If it
  11466. does handle the file, it must return the search string to be used
  11467. when following the link. The search string will be part of the
  11468. file link, given after a double colon, and `org-open-at-point'
  11469. will automatically search for it. If special measures must be
  11470. taken to make the search successful, another function should be
  11471. added to the companion hook `org-execute-file-search-functions',
  11472. which see.
  11473. A function in this hook may also use `setq' to set the variable
  11474. `description' to provide a suggestion for the descriptive text to
  11475. be used for this link when it gets inserted into an Org-mode
  11476. buffer with \\[org-insert-link].")
  11477. (defvar org-execute-file-search-functions nil
  11478. "List of functions to execute a file search triggered by a link.
  11479. Functions added to this hook must accept a single argument, the
  11480. search string that was part of the file link, the part after the
  11481. double colon. The function must first check if it would like to
  11482. handle this search, for example by checking the major-mode or the
  11483. file extension. If it decides not to handle this search, it
  11484. should just return nil to give other functions a chance. If it
  11485. does handle the search, it must return a non-nil value to keep
  11486. other functions from trying.
  11487. Each function can access the current prefix argument through the
  11488. variable `current-prefix-argument'. Note that a single prefix is
  11489. used to force opening a link in Emacs, so it may be good to only
  11490. use a numeric or double prefix to guide the search function.
  11491. In case this is needed, a function in this hook can also restore
  11492. the window configuration before `org-open-at-point' was called using:
  11493. (set-window-configuration org-window-config-before-follow-link)")
  11494. (defun org-link-search (s &optional type avoid-pos)
  11495. "Search for a link search option.
  11496. If S is surrounded by forward slashes, it is interpreted as a
  11497. regular expression. In org-mode files, this will create an `org-occur'
  11498. sparse tree. In ordinary files, `occur' will be used to list matches.
  11499. If the current buffer is in `dired-mode', grep will be used to search
  11500. in all files. If AVOID-POS is given, ignore matches near that position."
  11501. (let ((case-fold-search t)
  11502. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11503. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11504. (append '(("") (" ") ("\t") ("\n"))
  11505. org-emphasis-alist)
  11506. "\\|") "\\)"))
  11507. (pos (point))
  11508. (pre "") (post "")
  11509. words re0 re1 re2 re3 re4 re5 re2a reall)
  11510. (cond
  11511. ;; First check if there are any special
  11512. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11513. ;; Now try the builtin stuff
  11514. ((save-excursion
  11515. (goto-char (point-min))
  11516. (and
  11517. (re-search-forward
  11518. (concat "<<" (regexp-quote s0) ">>") nil t)
  11519. (setq pos (match-beginning 0))))
  11520. ;; There is an exact target for this
  11521. (goto-char pos))
  11522. ((string-match "^/\\(.*\\)/$" s)
  11523. ;; A regular expression
  11524. (cond
  11525. ((org-mode-p)
  11526. (org-occur (match-string 1 s)))
  11527. ;;((eq major-mode 'dired-mode)
  11528. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11529. (t (org-do-occur (match-string 1 s)))))
  11530. (t
  11531. ;; A normal search strings
  11532. (when (equal (string-to-char s) ?*)
  11533. ;; Anchor on headlines, post may include tags.
  11534. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11535. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11536. s (substring s 1)))
  11537. (remove-text-properties
  11538. 0 (length s)
  11539. '(face nil mouse-face nil keymap nil fontified nil) s)
  11540. ;; Make a series of regular expressions to find a match
  11541. (setq words (org-split-string s "[ \n\r\t]+")
  11542. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11543. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11544. "\\)" markers)
  11545. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11546. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11547. re1 (concat pre re2 post)
  11548. re3 (concat pre re4 post)
  11549. re5 (concat pre ".*" re4)
  11550. re2 (concat pre re2)
  11551. re2a (concat pre re2a)
  11552. re4 (concat pre re4)
  11553. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11554. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11555. re5 "\\)"
  11556. ))
  11557. (cond
  11558. ((eq type 'org-occur) (org-occur reall))
  11559. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11560. (t (goto-char (point-min))
  11561. (if (or (org-search-not-self 1 re0 nil t)
  11562. (org-search-not-self 1 re1 nil t)
  11563. (org-search-not-self 1 re2 nil t)
  11564. (org-search-not-self 1 re2a nil t)
  11565. (org-search-not-self 1 re3 nil t)
  11566. (org-search-not-self 1 re4 nil t)
  11567. (org-search-not-self 1 re5 nil t)
  11568. )
  11569. (goto-char (match-beginning 1))
  11570. (goto-char pos)
  11571. (error "No match")))))
  11572. (t
  11573. ;; Normal string-search
  11574. (goto-char (point-min))
  11575. (if (search-forward s nil t)
  11576. (goto-char (match-beginning 0))
  11577. (error "No match"))))
  11578. (and (org-mode-p) (org-show-context 'link-search))))
  11579. (defun org-search-not-self (group &rest args)
  11580. "Execute `re-search-forward', but only accept matches that do not
  11581. enclose the position of `org-open-link-marker'."
  11582. (let ((m org-open-link-marker))
  11583. (catch 'exit
  11584. (while (apply 're-search-forward args)
  11585. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11586. (goto-char (match-end group))
  11587. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11588. (> (match-beginning 0) (marker-position m))
  11589. (< (match-end 0) (marker-position m)))
  11590. (save-match-data
  11591. (or (not (org-in-regexp
  11592. org-bracket-link-analytic-regexp 1))
  11593. (not (match-end 4)) ; no description
  11594. (and (<= (match-beginning 4) (point))
  11595. (>= (match-end 4) (point))))))
  11596. (throw 'exit (point))))))))
  11597. (defun org-get-buffer-for-internal-link (buffer)
  11598. "Return a buffer to be used for displaying the link target of internal links."
  11599. (cond
  11600. ((not org-display-internal-link-with-indirect-buffer)
  11601. buffer)
  11602. ((string-match "(Clone)$" (buffer-name buffer))
  11603. (message "Buffer is already a clone, not making another one")
  11604. ;; we also do not modify visibility in this case
  11605. buffer)
  11606. (t ; make a new indirect buffer for displaying the link
  11607. (let* ((bn (buffer-name buffer))
  11608. (ibn (concat bn "(Clone)"))
  11609. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11610. (with-current-buffer ib (org-overview))
  11611. ib))))
  11612. (defun org-do-occur (regexp &optional cleanup)
  11613. "Call the Emacs command `occur'.
  11614. If CLEANUP is non-nil, remove the printout of the regular expression
  11615. in the *Occur* buffer. This is useful if the regex is long and not useful
  11616. to read."
  11617. (occur regexp)
  11618. (when cleanup
  11619. (let ((cwin (selected-window)) win beg end)
  11620. (when (setq win (get-buffer-window "*Occur*"))
  11621. (select-window win))
  11622. (goto-char (point-min))
  11623. (when (re-search-forward "match[a-z]+" nil t)
  11624. (setq beg (match-end 0))
  11625. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11626. (setq end (1- (match-beginning 0)))))
  11627. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11628. (goto-char (point-min))
  11629. (select-window cwin))))
  11630. ;;; The mark ring for links jumps
  11631. (defvar org-mark-ring nil
  11632. "Mark ring for positions before jumps in Org-mode.")
  11633. (defvar org-mark-ring-last-goto nil
  11634. "Last position in the mark ring used to go back.")
  11635. ;; Fill and close the ring
  11636. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11637. (loop for i from 1 to org-mark-ring-length do
  11638. (push (make-marker) org-mark-ring))
  11639. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11640. org-mark-ring)
  11641. (defun org-mark-ring-push (&optional pos buffer)
  11642. "Put the current position or POS into the mark ring and rotate it."
  11643. (interactive)
  11644. (setq pos (or pos (point)))
  11645. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11646. (move-marker (car org-mark-ring)
  11647. (or pos (point))
  11648. (or buffer (current-buffer)))
  11649. (message "%s"
  11650. (substitute-command-keys
  11651. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11652. (defun org-mark-ring-goto (&optional n)
  11653. "Jump to the previous position in the mark ring.
  11654. With prefix arg N, jump back that many stored positions. When
  11655. called several times in succession, walk through the entire ring.
  11656. Org-mode commands jumping to a different position in the current file,
  11657. or to another Org-mode file, automatically push the old position
  11658. onto the ring."
  11659. (interactive "p")
  11660. (let (p m)
  11661. (if (eq last-command this-command)
  11662. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11663. (setq p org-mark-ring))
  11664. (setq org-mark-ring-last-goto p)
  11665. (setq m (car p))
  11666. (switch-to-buffer (marker-buffer m))
  11667. (goto-char m)
  11668. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11669. (defun org-remove-angle-brackets (s)
  11670. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11671. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11672. s)
  11673. (defun org-add-angle-brackets (s)
  11674. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11675. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11676. s)
  11677. ;;; Following specific links
  11678. (defun org-follow-timestamp-link ()
  11679. (cond
  11680. ((org-at-date-range-p t)
  11681. (let ((org-agenda-start-on-weekday)
  11682. (t1 (match-string 1))
  11683. (t2 (match-string 2)))
  11684. (setq t1 (time-to-days (org-time-string-to-time t1))
  11685. t2 (time-to-days (org-time-string-to-time t2)))
  11686. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11687. ((org-at-timestamp-p t)
  11688. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11689. (substring (match-string 1) 0 10)))
  11690. 1))
  11691. (t (error "This should not happen"))))
  11692. (defun org-follow-bbdb-link (name)
  11693. "Follow a BBDB link to NAME."
  11694. (require 'bbdb)
  11695. (let ((inhibit-redisplay (not debug-on-error))
  11696. (bbdb-electric-p nil))
  11697. (catch 'exit
  11698. ;; Exact match on name
  11699. (bbdb-name (concat "\\`" name "\\'") nil)
  11700. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11701. ;; Exact match on name
  11702. (bbdb-company (concat "\\`" name "\\'") nil)
  11703. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11704. ;; Partial match on name
  11705. (bbdb-name name nil)
  11706. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11707. ;; Partial match on company
  11708. (bbdb-company name nil)
  11709. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11710. ;; General match including network address and notes
  11711. (bbdb name nil)
  11712. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11713. (delete-window (get-buffer-window "*BBDB*"))
  11714. (error "No matching BBDB record")))))
  11715. (defun org-follow-info-link (name)
  11716. "Follow an info file & node link to NAME."
  11717. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11718. (string-match "\\(.*\\)" name))
  11719. (progn
  11720. (require 'info)
  11721. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11722. (Info-find-node (match-string 1 name) (match-string 2 name))
  11723. (Info-find-node (match-string 1 name) "Top")))
  11724. (message "Could not open: %s" name)))
  11725. (defun org-follow-gnus-link (&optional group article)
  11726. "Follow a Gnus link to GROUP and ARTICLE."
  11727. (require 'gnus)
  11728. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  11729. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  11730. (cond ((and group article)
  11731. (gnus-group-read-group 1 nil group)
  11732. (gnus-summary-goto-article (string-to-number article) nil t))
  11733. (group (gnus-group-jump-to-group group))))
  11734. (defun org-follow-vm-link (&optional folder article readonly)
  11735. "Follow a VM link to FOLDER and ARTICLE."
  11736. (require 'vm)
  11737. (setq article (org-add-angle-brackets article))
  11738. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  11739. ;; ange-ftp or efs or tramp access
  11740. (let ((user (or (match-string 1 folder) (user-login-name)))
  11741. (host (match-string 2 folder))
  11742. (file (match-string 3 folder)))
  11743. (cond
  11744. ((featurep 'tramp)
  11745. ;; use tramp to access the file
  11746. (if (featurep 'xemacs)
  11747. (setq folder (format "[%s@%s]%s" user host file))
  11748. (setq folder (format "/%s@%s:%s" user host file))))
  11749. (t
  11750. ;; use ange-ftp or efs
  11751. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  11752. (setq folder (format "/%s@%s:%s" user host file))))))
  11753. (when folder
  11754. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  11755. (sit-for 0.1)
  11756. (when article
  11757. (vm-select-folder-buffer)
  11758. (widen)
  11759. (let ((case-fold-search t))
  11760. (goto-char (point-min))
  11761. (if (not (re-search-forward
  11762. (concat "^" "message-id: *" (regexp-quote article))))
  11763. (error "Could not find the specified message in this folder"))
  11764. (vm-isearch-update)
  11765. (vm-isearch-narrow)
  11766. (vm-beginning-of-message)
  11767. (vm-summarize)))))
  11768. (defun org-follow-wl-link (folder article)
  11769. "Follow a Wanderlust link to FOLDER and ARTICLE."
  11770. (if (and (string= folder "%")
  11771. article
  11772. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  11773. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  11774. ;; Thus, we recompose folder and article ids.
  11775. (setq folder (format "%s#%s" folder (match-string 1 article))
  11776. article (match-string 3 article)))
  11777. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  11778. (error "No such folder: %s" folder))
  11779. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  11780. (and article
  11781. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  11782. (wl-summary-redisplay)))
  11783. (defun org-follow-rmail-link (folder article)
  11784. "Follow an RMAIL link to FOLDER and ARTICLE."
  11785. (setq article (org-add-angle-brackets article))
  11786. (let (message-number)
  11787. (save-excursion
  11788. (save-window-excursion
  11789. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11790. (setq message-number
  11791. (save-restriction
  11792. (widen)
  11793. (goto-char (point-max))
  11794. (if (re-search-backward
  11795. (concat "^Message-ID:\\s-+" (regexp-quote
  11796. (or article "")))
  11797. nil t)
  11798. (rmail-what-message))))))
  11799. (if message-number
  11800. (progn
  11801. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11802. (rmail-show-message message-number)
  11803. message-number)
  11804. (error "Message not found"))))
  11805. ;;; mh-e integration based on planner-mode
  11806. (defun org-mhe-get-message-real-folder ()
  11807. "Return the name of the current message real folder, so if you use
  11808. sequences, it will now work."
  11809. (save-excursion
  11810. (let* ((folder
  11811. (if (equal major-mode 'mh-folder-mode)
  11812. mh-current-folder
  11813. ;; Refer to the show buffer
  11814. mh-show-folder-buffer))
  11815. (end-index
  11816. (if (boundp 'mh-index-folder)
  11817. (min (length mh-index-folder) (length folder))))
  11818. )
  11819. ;; a simple test on mh-index-data does not work, because
  11820. ;; mh-index-data is always nil in a show buffer.
  11821. (if (and (boundp 'mh-index-folder)
  11822. (string= mh-index-folder (substring folder 0 end-index)))
  11823. (if (equal major-mode 'mh-show-mode)
  11824. (save-window-excursion
  11825. (let (pop-up-frames)
  11826. (when (buffer-live-p (get-buffer folder))
  11827. (progn
  11828. (pop-to-buffer folder)
  11829. (org-mhe-get-message-folder-from-index)
  11830. )
  11831. )))
  11832. (org-mhe-get-message-folder-from-index)
  11833. )
  11834. folder
  11835. )
  11836. )))
  11837. (defun org-mhe-get-message-folder-from-index ()
  11838. "Returns the name of the message folder in a index folder buffer."
  11839. (save-excursion
  11840. (mh-index-previous-folder)
  11841. (re-search-forward "^\\(+.*\\)$" nil t)
  11842. (message "%s" (match-string 1))))
  11843. (defun org-mhe-get-message-folder ()
  11844. "Return the name of the current message folder. Be careful if you
  11845. use sequences."
  11846. (save-excursion
  11847. (if (equal major-mode 'mh-folder-mode)
  11848. mh-current-folder
  11849. ;; Refer to the show buffer
  11850. mh-show-folder-buffer)))
  11851. (defun org-mhe-get-message-num ()
  11852. "Return the number of the current message. Be careful if you
  11853. use sequences."
  11854. (save-excursion
  11855. (if (equal major-mode 'mh-folder-mode)
  11856. (mh-get-msg-num nil)
  11857. ;; Refer to the show buffer
  11858. (mh-show-buffer-message-number))))
  11859. (defun org-mhe-get-header (header)
  11860. "Return a header of the message in folder mode. This will create a
  11861. show buffer for the corresponding message. If you have a more clever
  11862. idea..."
  11863. (let* ((folder (org-mhe-get-message-folder))
  11864. (num (org-mhe-get-message-num))
  11865. (buffer (get-buffer-create (concat "show-" folder)))
  11866. (header-field))
  11867. (with-current-buffer buffer
  11868. (mh-display-msg num folder)
  11869. (if (equal major-mode 'mh-folder-mode)
  11870. (mh-header-display)
  11871. (mh-show-header-display))
  11872. (set-buffer buffer)
  11873. (setq header-field (mh-get-header-field header))
  11874. (if (equal major-mode 'mh-folder-mode)
  11875. (mh-show)
  11876. (mh-show-show))
  11877. header-field)))
  11878. (defun org-follow-mhe-link (folder article)
  11879. "Follow an MHE link to FOLDER and ARTICLE.
  11880. If ARTICLE is nil FOLDER is shown. If the configuration variable
  11881. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  11882. ARTICLE is searched in all folders. Indexed searches (swish++,
  11883. namazu, and others supported by MH-E) will always search in all
  11884. folders."
  11885. (require 'mh-e)
  11886. (require 'mh-search)
  11887. (require 'mh-utils)
  11888. (mh-find-path)
  11889. (if (not article)
  11890. (mh-visit-folder (mh-normalize-folder-name folder))
  11891. (setq article (org-add-angle-brackets article))
  11892. (mh-search-choose)
  11893. (if (equal mh-searcher 'pick)
  11894. (progn
  11895. (mh-search folder (list "--message-id" article))
  11896. (when (and org-mhe-search-all-folders
  11897. (not (org-mhe-get-message-real-folder)))
  11898. (kill-this-buffer)
  11899. (mh-search "+" (list "--message-id" article))))
  11900. (mh-search "+" article))
  11901. (if (org-mhe-get-message-real-folder)
  11902. (mh-show-msg 1)
  11903. (kill-this-buffer)
  11904. (error "Message not found"))))
  11905. ;;; BibTeX links
  11906. ;; Use the custom search meachnism to construct and use search strings for
  11907. ;; file links to BibTeX database entries.
  11908. (defun org-create-file-search-in-bibtex ()
  11909. "Create the search string and description for a BibTeX database entry."
  11910. (when (eq major-mode 'bibtex-mode)
  11911. ;; yes, we want to construct this search string.
  11912. ;; Make a good description for this entry, using names, year and the title
  11913. ;; Put it into the `description' variable which is dynamically scoped.
  11914. (let ((bibtex-autokey-names 1)
  11915. (bibtex-autokey-names-stretch 1)
  11916. (bibtex-autokey-name-case-convert-function 'identity)
  11917. (bibtex-autokey-name-separator " & ")
  11918. (bibtex-autokey-additional-names " et al.")
  11919. (bibtex-autokey-year-length 4)
  11920. (bibtex-autokey-name-year-separator " ")
  11921. (bibtex-autokey-titlewords 3)
  11922. (bibtex-autokey-titleword-separator " ")
  11923. (bibtex-autokey-titleword-case-convert-function 'identity)
  11924. (bibtex-autokey-titleword-length 'infty)
  11925. (bibtex-autokey-year-title-separator ": "))
  11926. (setq description (bibtex-generate-autokey)))
  11927. ;; Now parse the entry, get the key and return it.
  11928. (save-excursion
  11929. (bibtex-beginning-of-entry)
  11930. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  11931. (defun org-execute-file-search-in-bibtex (s)
  11932. "Find the link search string S as a key for a database entry."
  11933. (when (eq major-mode 'bibtex-mode)
  11934. ;; Yes, we want to do the search in this file.
  11935. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  11936. (goto-char (point-min))
  11937. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  11938. (regexp-quote s) "[ \t\n]*,") nil t)
  11939. (goto-char (match-beginning 0)))
  11940. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  11941. ;; Use double prefix to indicate that any web link should be browsed
  11942. (let ((b (current-buffer)) (p (point)))
  11943. ;; Restore the window configuration because we just use the web link
  11944. (set-window-configuration org-window-config-before-follow-link)
  11945. (save-excursion (set-buffer b) (goto-char p)
  11946. (bibtex-url)))
  11947. (recenter 0)) ; Move entry start to beginning of window
  11948. ;; return t to indicate that the search is done.
  11949. t))
  11950. ;; Finally add the functions to the right hooks.
  11951. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  11952. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  11953. ;; end of Bibtex link setup
  11954. ;;; Following file links
  11955. (defun org-open-file (path &optional in-emacs line search)
  11956. "Open the file at PATH.
  11957. First, this expands any special file name abbreviations. Then the
  11958. configuration variable `org-file-apps' is checked if it contains an
  11959. entry for this file type, and if yes, the corresponding command is launched.
  11960. If no application is found, Emacs simply visits the file.
  11961. With optional argument IN-EMACS, Emacs will visit the file.
  11962. Optional LINE specifies a line to go to, optional SEARCH a string to
  11963. search for. If LINE or SEARCH is given, the file will always be
  11964. opened in Emacs.
  11965. If the file does not exist, an error is thrown."
  11966. (setq in-emacs (or in-emacs line search))
  11967. (let* ((file (if (equal path "")
  11968. buffer-file-name
  11969. (substitute-in-file-name (expand-file-name path))))
  11970. (apps (append org-file-apps (org-default-apps)))
  11971. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  11972. (dirp (if remp nil (file-directory-p file)))
  11973. (dfile (downcase file))
  11974. (old-buffer (current-buffer))
  11975. (old-pos (point))
  11976. (old-mode major-mode)
  11977. ext cmd)
  11978. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  11979. (setq ext (match-string 1 dfile))
  11980. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  11981. (setq ext (match-string 1 dfile))))
  11982. (if in-emacs
  11983. (setq cmd 'emacs)
  11984. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  11985. (and dirp (cdr (assoc 'directory apps)))
  11986. (cdr (assoc ext apps))
  11987. (cdr (assoc t apps)))))
  11988. (when (eq cmd 'mailcap)
  11989. (require 'mailcap)
  11990. (mailcap-parse-mailcaps)
  11991. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  11992. (command (mailcap-mime-info mime-type)))
  11993. (if (stringp command)
  11994. (setq cmd command)
  11995. (setq cmd 'emacs))))
  11996. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  11997. (not (file-exists-p file))
  11998. (not org-open-non-existing-files))
  11999. (error "No such file: %s" file))
  12000. (cond
  12001. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12002. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12003. (while (string-match "['\"]%s['\"]" cmd)
  12004. (setq cmd (replace-match "%s" t t cmd)))
  12005. (while (string-match "%s" cmd)
  12006. (setq cmd (replace-match (shell-quote-argument file) t t cmd)))
  12007. (save-window-excursion
  12008. (start-process-shell-command cmd nil cmd)))
  12009. ((or (stringp cmd)
  12010. (eq cmd 'emacs))
  12011. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12012. (widen)
  12013. (if line (goto-line line)
  12014. (if search (org-link-search search))))
  12015. ((consp cmd)
  12016. (eval cmd))
  12017. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12018. (and (org-mode-p) (eq old-mode 'org-mode)
  12019. (or (not (equal old-buffer (current-buffer)))
  12020. (not (equal old-pos (point))))
  12021. (org-mark-ring-push old-pos old-buffer))))
  12022. (defun org-default-apps ()
  12023. "Return the default applications for this operating system."
  12024. (cond
  12025. ((eq system-type 'darwin)
  12026. org-file-apps-defaults-macosx)
  12027. ((eq system-type 'windows-nt)
  12028. org-file-apps-defaults-windowsnt)
  12029. (t org-file-apps-defaults-gnu)))
  12030. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12031. (defun org-file-remote-p (file)
  12032. "Test whether FILE specifies a location on a remote system.
  12033. Return non-nil if the location is indeed remote.
  12034. For example, the filename \"/user@host:/foo\" specifies a location
  12035. on the system \"/user@host:\"."
  12036. (cond ((fboundp 'file-remote-p)
  12037. (file-remote-p file))
  12038. ((fboundp 'tramp-handle-file-remote-p)
  12039. (tramp-handle-file-remote-p file))
  12040. ((and (boundp 'ange-ftp-name-format)
  12041. (string-match (car ange-ftp-name-format) file))
  12042. t)
  12043. (t nil)))
  12044. ;;;; Hooks for remember.el, and refiling
  12045. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12046. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12047. ;;;###autoload
  12048. (defun org-remember-insinuate ()
  12049. "Setup remember.el for use wiht Org-mode."
  12050. (require 'remember)
  12051. (setq remember-annotation-functions '(org-remember-annotation))
  12052. (setq remember-handler-functions '(org-remember-handler))
  12053. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12054. ;;;###autoload
  12055. (defun org-remember-annotation ()
  12056. "Return a link to the current location as an annotation for remember.el.
  12057. If you are using Org-mode files as target for data storage with
  12058. remember.el, then the annotations should include a link compatible with the
  12059. conventions in Org-mode. This function returns such a link."
  12060. (org-store-link nil))
  12061. (defconst org-remember-help
  12062. "Select a destination location for the note.
  12063. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12064. RET on headline -> Store as sublevel entry to current headline
  12065. RET at beg-of-buf -> Append to file as level 2 headline
  12066. <left>/<right> -> before/after current headline, same headings level")
  12067. (defvar org-remember-previous-location nil)
  12068. (defvar org-force-remember-template-char) ;; dynamically scoped
  12069. (defun org-select-remember-template (&optional use-char)
  12070. (when org-remember-templates
  12071. (let* ((templates (mapcar (lambda (x)
  12072. (if (stringp (car x))
  12073. (append (list (nth 1 x) (car x)) (cddr x))
  12074. (append (list (car x) "") (cdr x))))
  12075. org-remember-templates))
  12076. (char (or use-char
  12077. (cond
  12078. ((= (length templates) 1)
  12079. (caar templates))
  12080. ((and (boundp 'org-force-remember-template-char)
  12081. org-force-remember-template-char)
  12082. (if (stringp org-force-remember-template-char)
  12083. (string-to-char org-force-remember-template-char)
  12084. org-force-remember-template-char))
  12085. (t
  12086. (message "Select template: %s"
  12087. (mapconcat
  12088. (lambda (x)
  12089. (cond
  12090. ((not (string-match "\\S-" (nth 1 x)))
  12091. (format "[%c]" (car x)))
  12092. ((equal (downcase (car x))
  12093. (downcase (aref (nth 1 x) 0)))
  12094. (format "[%c]%s" (car x)
  12095. (substring (nth 1 x) 1)))
  12096. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12097. templates " "))
  12098. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12099. (when (equal char0 ?\C-g)
  12100. (jump-to-register remember-register)
  12101. (kill-buffer remember-buffer))
  12102. char0))))))
  12103. (cddr (assoc char templates)))))
  12104. (defvar x-last-selected-text)
  12105. (defvar x-last-selected-text-primary)
  12106. ;;;###autoload
  12107. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12108. "Initialize *remember* buffer with template, invoke `org-mode'.
  12109. This function should be placed into `remember-mode-hook' and in fact requires
  12110. to be run from that hook to function properly."
  12111. (unless (fboundp 'remember-finalize)
  12112. (defalias 'remember-finalize 'remember-buffer))
  12113. (if org-remember-templates
  12114. (let* ((entry (org-select-remember-template use-char))
  12115. (tpl (car entry))
  12116. (plist-p (if org-store-link-plist t nil))
  12117. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12118. (string-match "\\S-" (nth 1 entry)))
  12119. (nth 1 entry)
  12120. org-default-notes-file))
  12121. (headline (nth 2 entry))
  12122. (v-c (or (and (eq window-system 'x)
  12123. (fboundp 'x-cut-buffer-or-selection-value)
  12124. (x-cut-buffer-or-selection-value))
  12125. (org-bound-and-true-p x-last-selected-text)
  12126. (org-bound-and-true-p x-last-selected-text-primary)
  12127. (and (> (length kill-ring) 0) (current-kill 0))))
  12128. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12129. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12130. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12131. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12132. ;; `initial' and `annotation' are bound in `remember'
  12133. (v-i (if (boundp 'initial) initial))
  12134. (v-a (if (and (boundp 'annotation) annotation)
  12135. (if (equal annotation "[[]]") "" annotation)
  12136. ""))
  12137. (v-A (if (and v-a
  12138. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12139. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12140. v-a))
  12141. (v-n user-full-name)
  12142. (org-startup-folded nil)
  12143. org-time-was-given org-end-time-was-given x
  12144. prompt completions char time pos default histvar)
  12145. (setq org-store-link-plist
  12146. (append (list :annotation v-a :initial v-i)
  12147. org-store-link-plist))
  12148. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12149. (erase-buffer)
  12150. (insert (substitute-command-keys
  12151. (format
  12152. "## Filing location: Select interactively, default, or last used:
  12153. ## %s to select file and header location interactively.
  12154. ## %s \"%s\" -> \"* %s\"
  12155. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12156. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12157. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12158. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12159. (abbreviate-file-name (or file org-default-notes-file))
  12160. (or headline "")
  12161. (or (car org-remember-previous-location) "???")
  12162. (or (cdr org-remember-previous-location) "???"))))
  12163. (insert tpl) (goto-char (point-min))
  12164. ;; Simple %-escapes
  12165. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12166. (when (and initial (equal (match-string 0) "%i"))
  12167. (save-match-data
  12168. (let* ((lead (buffer-substring
  12169. (point-at-bol) (match-beginning 0))))
  12170. (setq v-i (mapconcat 'identity
  12171. (org-split-string initial "\n")
  12172. (concat "\n" lead))))))
  12173. (replace-match
  12174. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12175. t t))
  12176. ;; %[] Insert contents of a file.
  12177. (goto-char (point-min))
  12178. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12179. (let ((start (match-beginning 0))
  12180. (end (match-end 0))
  12181. (filename (expand-file-name (match-string 1))))
  12182. (goto-char start)
  12183. (delete-region start end)
  12184. (condition-case error
  12185. (insert-file-contents filename)
  12186. (error (insert (format "%%![Couldn't insert %s: %s]"
  12187. filename error))))))
  12188. ;; %() embedded elisp
  12189. (goto-char (point-min))
  12190. (while (re-search-forward "%\\((.+)\\)" nil t)
  12191. (goto-char (match-beginning 0))
  12192. (let ((template-start (point)))
  12193. (forward-char 1)
  12194. (let ((result
  12195. (condition-case error
  12196. (eval (read (current-buffer)))
  12197. (error (format "%%![Error: %s]" error)))))
  12198. (delete-region template-start (point))
  12199. (insert result))))
  12200. ;; From the property list
  12201. (when plist-p
  12202. (goto-char (point-min))
  12203. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12204. (and (setq x (or (plist-get org-store-link-plist
  12205. (intern (match-string 1))) ""))
  12206. (replace-match x t t))))
  12207. ;; Turn on org-mode in the remember buffer, set local variables
  12208. (org-mode)
  12209. (org-set-local 'org-finish-function 'remember-finalize)
  12210. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12211. (org-set-local 'org-default-notes-file file))
  12212. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12213. (org-set-local 'org-remember-default-headline headline))
  12214. ;; Interactive template entries
  12215. (goto-char (point-min))
  12216. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([guUtT]\\)?" nil t)
  12217. (setq char (if (match-end 3) (match-string 3))
  12218. prompt (if (match-end 2) (match-string 2)))
  12219. (goto-char (match-beginning 0))
  12220. (replace-match "")
  12221. (setq completions nil default nil)
  12222. (when prompt
  12223. (setq completions (org-split-string prompt "|")
  12224. prompt (pop completions)
  12225. default (car completions)
  12226. histvar (intern (concat
  12227. "org-remember-template-prompt-history::"
  12228. (or prompt "")))
  12229. completions (mapcar 'list completions)))
  12230. (cond
  12231. ((member char '("G" "g"))
  12232. (let* ((org-last-tags-completion-table
  12233. (org-global-tags-completion-table
  12234. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12235. (org-add-colon-after-tag-completion t)
  12236. (ins (completing-read
  12237. (if prompt (concat prompt ": ") "Tags: ")
  12238. 'org-tags-completion-function nil nil nil
  12239. 'org-tags-history)))
  12240. (setq ins (mapconcat 'identity
  12241. (org-split-string ins (org-re "[^[:alnum:]]+"))
  12242. ":"))
  12243. (when (string-match "\\S-" ins)
  12244. (or (equal (char-before) ?:) (insert ":"))
  12245. (insert ins)
  12246. (or (equal (char-after) ?:) (insert ":")))))
  12247. (char
  12248. (setq org-time-was-given (equal (upcase char) char))
  12249. (setq time (org-read-date (equal (upcase char) "U") t nil
  12250. prompt))
  12251. (org-insert-time-stamp time org-time-was-given
  12252. (member char '("u" "U"))
  12253. nil nil (list org-end-time-was-given)))
  12254. (t
  12255. (insert (org-completing-read
  12256. (concat (if prompt prompt "Enter string")
  12257. (if default (concat " [" default "]"))
  12258. ": ")
  12259. completions nil nil nil histvar default)))))
  12260. (goto-char (point-min))
  12261. (if (re-search-forward "%\\?" nil t)
  12262. (replace-match "")
  12263. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12264. (org-mode)
  12265. (org-set-local 'org-finish-function 'remember-finalize))
  12266. (when (save-excursion
  12267. (goto-char (point-min))
  12268. (re-search-forward "%!" nil t))
  12269. (replace-match "")
  12270. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12271. (defun org-remember-finish-immediately ()
  12272. "File remember note immediately.
  12273. This should be run in `post-command-hook' and will remove itself
  12274. from that hook."
  12275. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12276. (when org-finish-function
  12277. (funcall org-finish-function)))
  12278. ;;;###autoload
  12279. (defun org-remember (&optional goto org-force-remember-template-char)
  12280. "Call `remember'. If this is already a remember buffer, re-apply template.
  12281. If there is an active region, make sure remember uses it as initial content
  12282. of the remember buffer.
  12283. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12284. anything, just go to the file/headline where the selected templated usually
  12285. stores its notes. With a double prefix arg `C-u C-u', got to the last
  12286. note stored by remember.
  12287. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12288. associated with a template in `org-remember-tempates'."
  12289. (interactive "P")
  12290. (cond
  12291. ((equal goto '(4)) (org-go-to-remember-target))
  12292. ((equal goto '(16)) (org-remember-goto-last-stored))
  12293. (t
  12294. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12295. (progn
  12296. (when (< (length org-remember-templates) 2)
  12297. (error "No other template available"))
  12298. (erase-buffer)
  12299. (let ((annotation (plist-get org-store-link-plist :annotation))
  12300. (initial (plist-get org-store-link-plist :initial)))
  12301. (org-remember-apply-template))
  12302. (message "Press C-c C-c to remember data"))
  12303. (if (org-region-active-p)
  12304. (remember (buffer-substring (point) (mark)))
  12305. (call-interactively 'remember))))))
  12306. (defun org-remember-goto-last-stored ()
  12307. "Go to the location where the last remember note was stored."
  12308. (interactive)
  12309. (bookmark-jump "org-remember-last-stored")
  12310. (message "This is the last note stored by remember"))
  12311. (defun org-go-to-remember-target (&optional template-key)
  12312. "Go to the target location of a remember template.
  12313. The user is queried for the template."
  12314. (interactive)
  12315. (let* ((entry (org-select-remember-template template-key))
  12316. (file (nth 1 entry))
  12317. (heading (nth 2 entry))
  12318. visiting)
  12319. (unless (and file (stringp file) (string-match "\\S-" file))
  12320. (setq file org-default-notes-file))
  12321. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12322. (setq heading org-remember-default-headline))
  12323. (setq visiting (org-find-base-buffer-visiting file))
  12324. (if (not visiting) (find-file-noselect file))
  12325. (switch-to-buffer (or visiting (get-file-buffer file)))
  12326. (widen)
  12327. (goto-char (point-min))
  12328. (if (re-search-forward
  12329. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12330. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12331. nil t)
  12332. (goto-char (match-beginning 0))
  12333. (error "Target headline not found: %s" heading))))
  12334. (defvar org-note-abort nil) ; dynamically scoped
  12335. ;;;###autoload
  12336. (defun org-remember-handler ()
  12337. "Store stuff from remember.el into an org file.
  12338. First prompts for an org file. If the user just presses return, the value
  12339. of `org-default-notes-file' is used.
  12340. Then the command offers the headings tree of the selected file in order to
  12341. file the text at a specific location.
  12342. You can either immediately press RET to get the note appended to the
  12343. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12344. find a better place. Then press RET or <left> or <right> in insert the note.
  12345. Key Cursor position Note gets inserted
  12346. -----------------------------------------------------------------------------
  12347. RET buffer-start as level 1 heading at end of file
  12348. RET on headline as sublevel of the heading at cursor
  12349. RET no heading at cursor position, level taken from context.
  12350. Or use prefix arg to specify level manually.
  12351. <left> on headline as same level, before current heading
  12352. <right> on headline as same level, after current heading
  12353. So the fastest way to store the note is to press RET RET to append it to
  12354. the default file. This way your current train of thought is not
  12355. interrupted, in accordance with the principles of remember.el.
  12356. You can also get the fast execution without prompting by using
  12357. C-u C-c C-c to exit the remember buffer. See also the variable
  12358. `org-remember-store-without-prompt'.
  12359. Before being stored away, the function ensures that the text has a
  12360. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12361. is constructed from the current date and some additional data.
  12362. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12363. also indented so that it starts in the same column as the headline
  12364. \(i.e. after the stars).
  12365. See also the variable `org-reverse-note-order'."
  12366. (goto-char (point-min))
  12367. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12368. (replace-match ""))
  12369. (goto-char (point-max))
  12370. (beginning-of-line 1)
  12371. (while (looking-at "[ \t]*$\\|##.*")
  12372. (delete-region (1- (point)) (point-max))
  12373. (beginning-of-line 1))
  12374. (catch 'quit
  12375. (if org-note-abort (throw 'quit nil))
  12376. (let* ((txt (buffer-substring (point-min) (point-max)))
  12377. (fastp (org-xor (equal current-prefix-arg '(4))
  12378. org-remember-store-without-prompt))
  12379. (file (if fastp org-default-notes-file (org-get-org-file)))
  12380. (heading org-remember-default-headline)
  12381. (visiting (org-find-base-buffer-visiting file))
  12382. (org-startup-folded nil)
  12383. (org-startup-align-all-tables nil)
  12384. (org-goto-start-pos 1)
  12385. spos exitcmd level indent reversed)
  12386. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12387. (setq file (car org-remember-previous-location)
  12388. heading (cdr org-remember-previous-location)))
  12389. (setq current-prefix-arg nil)
  12390. (if (string-match "[ \t\n]+\\'" txt)
  12391. (setq txt (replace-match "" t t txt)))
  12392. ;; Modify text so that it becomes a nice subtree which can be inserted
  12393. ;; into an org tree.
  12394. (let* ((lines (split-string txt "\n"))
  12395. first)
  12396. (setq first (car lines) lines (cdr lines))
  12397. (if (string-match "^\\*+ " first)
  12398. ;; Is already a headline
  12399. (setq indent nil)
  12400. ;; We need to add a headline: Use time and first buffer line
  12401. (setq lines (cons first lines)
  12402. first (concat "* " (current-time-string)
  12403. " (" (remember-buffer-desc) ")")
  12404. indent " "))
  12405. (if (and org-adapt-indentation indent)
  12406. (setq lines (mapcar
  12407. (lambda (x)
  12408. (if (string-match "\\S-" x)
  12409. (concat indent x) x))
  12410. lines)))
  12411. (setq txt (concat first "\n"
  12412. (mapconcat 'identity lines "\n"))))
  12413. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12414. (setq txt (replace-match "\n\n" t t txt))
  12415. (if (string-match "[ \t\n]*\\'" txt)
  12416. (setq txt (replace-match "\n" t t txt))))
  12417. ;; Find the file
  12418. (if (not visiting) (find-file-noselect file))
  12419. (with-current-buffer (or visiting (get-file-buffer file))
  12420. (unless (org-mode-p)
  12421. (error "Target files for remember notes must be in Org-mode"))
  12422. (save-excursion
  12423. (save-restriction
  12424. (widen)
  12425. (and (goto-char (point-min))
  12426. (not (re-search-forward "^\\* " nil t))
  12427. (insert "\n* " (or heading "Notes") "\n"))
  12428. (setq reversed (org-notes-order-reversed-p))
  12429. ;; Find the default location
  12430. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12431. (goto-char (point-min))
  12432. (if (re-search-forward
  12433. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12434. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12435. nil t)
  12436. (setq org-goto-start-pos (match-beginning 0))
  12437. (when fastp
  12438. (goto-char (point-max))
  12439. (unless (bolp) (newline))
  12440. (insert "* " heading "\n")
  12441. (setq org-goto-start-pos (point-at-bol 0)))))
  12442. ;; Ask the User for a location
  12443. (if fastp
  12444. (setq spos org-goto-start-pos
  12445. exitcmd 'return)
  12446. (setq spos (org-get-location (current-buffer) org-remember-help)
  12447. exitcmd (cdr spos)
  12448. spos (car spos)))
  12449. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12450. ; not handle this note
  12451. (goto-char spos)
  12452. (cond ((org-on-heading-p t)
  12453. (org-back-to-heading t)
  12454. (setq level (funcall outline-level))
  12455. (cond
  12456. ((eq exitcmd 'return)
  12457. ;; sublevel of current
  12458. (setq org-remember-previous-location
  12459. (cons (abbreviate-file-name file)
  12460. (org-get-heading 'notags)))
  12461. (if reversed
  12462. (outline-next-heading)
  12463. (org-end-of-subtree)
  12464. (if (not (bolp))
  12465. (if (looking-at "[ \t]*\n")
  12466. (beginning-of-line 2)
  12467. (end-of-line 1)
  12468. (insert "\n"))))
  12469. (bookmark-set "org-remember-last-stored")
  12470. (org-paste-subtree (org-get-legal-level level 1) txt))
  12471. ((eq exitcmd 'left)
  12472. ;; before current
  12473. (bookmark-set "org-remember-last-stored")
  12474. (org-paste-subtree level txt))
  12475. ((eq exitcmd 'right)
  12476. ;; after current
  12477. (org-end-of-subtree t)
  12478. (bookmark-set "org-remember-last-stored")
  12479. (org-paste-subtree level txt))
  12480. (t (error "This should not happen"))))
  12481. ((and (bobp) (not reversed))
  12482. ;; Put it at the end, one level below level 1
  12483. (save-restriction
  12484. (widen)
  12485. (goto-char (point-max))
  12486. (if (not (bolp)) (newline))
  12487. (bookmark-set "org-remember-last-stored")
  12488. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12489. ((and (bobp) reversed)
  12490. ;; Put it at the start, as level 1
  12491. (save-restriction
  12492. (widen)
  12493. (goto-char (point-min))
  12494. (re-search-forward "^\\*+ " nil t)
  12495. (beginning-of-line 1)
  12496. (bookmark-set "org-remember-last-stored")
  12497. (org-paste-subtree 1 txt)))
  12498. (t
  12499. ;; Put it right there, with automatic level determined by
  12500. ;; org-paste-subtree or from prefix arg
  12501. (bookmark-set "org-remember-last-stored")
  12502. (org-paste-subtree
  12503. (if (numberp current-prefix-arg) current-prefix-arg)
  12504. txt)))
  12505. (when remember-save-after-remembering
  12506. (save-buffer)
  12507. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12508. t) ;; return t to indicate that we took care of this note.
  12509. (defun org-get-org-file ()
  12510. "Read a filename, with default directory `org-directory'."
  12511. (let ((default (or org-default-notes-file remember-data-file)))
  12512. (read-file-name (format "File name [%s]: " default)
  12513. (file-name-as-directory org-directory)
  12514. default)))
  12515. (defun org-notes-order-reversed-p ()
  12516. "Check if the current file should receive notes in reversed order."
  12517. (cond
  12518. ((not org-reverse-note-order) nil)
  12519. ((eq t org-reverse-note-order) t)
  12520. ((not (listp org-reverse-note-order)) nil)
  12521. (t (catch 'exit
  12522. (let ((all org-reverse-note-order)
  12523. entry)
  12524. (while (setq entry (pop all))
  12525. (if (string-match (car entry) buffer-file-name)
  12526. (throw 'exit (cdr entry))))
  12527. nil)))))
  12528. ;;; Refiling
  12529. (defvar org-refile-target-table nil
  12530. "The list of refile targets, created by `org-refile'.")
  12531. (defvar org-agenda-new-buffers nil
  12532. "Buffers created to visit agenda files.")
  12533. (defun org-get-refile-targets ()
  12534. "Produce a table with refile targets."
  12535. (let ((entries org-refile-targets)
  12536. org-agenda-new-files targets txt re files f desc descre)
  12537. (while (setq entry (pop entries))
  12538. (setq files (car entry) desc (cdr entry))
  12539. (cond
  12540. ((null files) (setq files (list (current-buffer))))
  12541. ((eq files 'org-agenda-files)
  12542. (setq files (org-agenda-files 'unrestricted)))
  12543. ((and (symbolp files) (fboundp files))
  12544. (setq files (funcall files)))
  12545. ((and (symbolp files) (boundp files))
  12546. (setq files (symbol-value files))))
  12547. (if (stringp files) (setq files (list files)))
  12548. (cond
  12549. ((eq (car desc) :tag)
  12550. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12551. ((eq (car desc) :todo)
  12552. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12553. ((eq (car desc) :regexp)
  12554. (setq descre (cdr desc)))
  12555. ((eq (car desc) :level)
  12556. (setq descre (concat "^\\*\\{" (number-to-string
  12557. (if org-odd-levels-only
  12558. (1- (* 2 (cdr desc)))
  12559. (cdr desc)))
  12560. "\\}[ \t]")))
  12561. ((eq (car desc) :maxlevel)
  12562. (setq descre (concat "^\\*\\{1," (number-to-string
  12563. (if org-odd-levels-only
  12564. (1- (* 2 (cdr desc)))
  12565. (cdr desc)))
  12566. "\\}[ \t]")))
  12567. (t (error "Bad refiling target description %s" desc)))
  12568. (while (setq f (pop files))
  12569. (save-excursion
  12570. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12571. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12572. (save-excursion
  12573. (save-restriction
  12574. (widen)
  12575. (goto-char (point-min))
  12576. (while (re-search-forward descre nil t)
  12577. (goto-char (point-at-bol))
  12578. (when (looking-at org-complex-heading-regexp)
  12579. (setq txt (match-string 4)
  12580. re (concat "^" (regexp-quote
  12581. (buffer-substring (match-beginning 1)
  12582. (match-end 4)))))
  12583. (if (match-end 5) (setq re (concat re "[ \t]+"
  12584. (regexp-quote
  12585. (match-string 5)))))
  12586. (setq re (concat re "[ \t]*$"))
  12587. (when org-refile-use-outline-path
  12588. (setq txt (mapconcat 'identity
  12589. (append
  12590. (if (eq org-refile-use-outline-path 'file)
  12591. (list (file-name-nondirectory
  12592. (buffer-file-name (buffer-base-buffer))))
  12593. (if (eq org-refile-use-outline-path 'full-file-path)
  12594. (list (buffer-file-name (buffer-base-buffer)))))
  12595. (org-get-outline-path)
  12596. (list txt))
  12597. "/")))
  12598. (push (list txt f re (point)) targets))
  12599. (goto-char (point-at-eol))))))))
  12600. (org-release-buffers org-agenda-new-buffers)
  12601. (nreverse targets)))
  12602. (defun org-get-outline-path ()
  12603. (let (rtn)
  12604. (save-excursion
  12605. (while (org-up-heading-safe)
  12606. (when (looking-at org-complex-heading-regexp)
  12607. (push (org-match-string-no-properties 4) rtn)))
  12608. rtn)))
  12609. (defvar org-refile-history nil
  12610. "History for refiling operations.")
  12611. (defun org-refile (&optional reversed-or-update)
  12612. "Move the entry at point to another heading.
  12613. The list of target headings is compiled using the information in
  12614. `org-refile-targets', which see. This list is created upon first use, and
  12615. you can update it by calling this command with a double prefix (`C-u C-u').
  12616. At the target location, the entry is filed as a subitem of the target heading.
  12617. Depending on `org-reverse-note-order', the new subitem will either be the
  12618. first of the last subitem. A single C-u prefix will toggle the value of this
  12619. variable for the duration of the command."
  12620. (interactive "P")
  12621. (if (equal reversed-or-update '(16))
  12622. (progn
  12623. (setq org-refile-target-table (org-get-refile-targets))
  12624. (message "Refile targets updated (%d targets)"
  12625. (length org-refile-target-table)))
  12626. (when (or (not org-refile-target-table)
  12627. (and (= (length org-refile-targets) 1)
  12628. (not (caar org-refile-targets))))
  12629. (setq org-refile-target-table (org-get-refile-targets)))
  12630. (unless org-refile-target-table
  12631. (error "No refile targets"))
  12632. (let* ((cbuf (current-buffer))
  12633. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12634. (fname (and filename (file-truename filename)))
  12635. (tbl (mapcar
  12636. (lambda (x)
  12637. (if (not (equal fname (file-truename (nth 1 x))))
  12638. (cons (concat (car x) " (" (file-name-nondirectory
  12639. (nth 1 x)) ")")
  12640. (cdr x))
  12641. x))
  12642. org-refile-target-table))
  12643. (completion-ignore-case t)
  12644. pos it nbuf file re level reversed)
  12645. (when (setq it (completing-read "Refile to: " tbl
  12646. nil t nil 'org-refile-history))
  12647. (setq it (assoc it tbl)
  12648. file (nth 1 it)
  12649. re (nth 2 it))
  12650. (org-copy-special)
  12651. (save-excursion
  12652. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12653. (find-file-noselect file))))
  12654. (setq reversed (org-notes-order-reversed-p))
  12655. (if (equal reversed-or-update '(16)) (setq reversed (not reversed)))
  12656. (save-excursion
  12657. (save-restriction
  12658. (widen)
  12659. (goto-char (point-min))
  12660. (unless (re-search-forward re nil t)
  12661. (error "Cannot find target location - try again with `C-u' prefix."))
  12662. (goto-char (match-beginning 0))
  12663. (looking-at outline-regexp)
  12664. (setq level (org-get-legal-level (funcall outline-level) 1))
  12665. (goto-char (or (save-excursion
  12666. (if reversed
  12667. (outline-next-heading)
  12668. (outline-get-next-sibling)))
  12669. (point-max)))
  12670. (org-paste-subtree level))))
  12671. (org-cut-special)
  12672. (message "Entry refiled to \"%s\"" (car it))))))
  12673. ;;;; Dynamic blocks
  12674. (defun org-find-dblock (name)
  12675. "Find the first dynamic block with name NAME in the buffer.
  12676. If not found, stay at current position and return nil."
  12677. (let (pos)
  12678. (save-excursion
  12679. (goto-char (point-min))
  12680. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  12681. nil t)
  12682. (match-beginning 0))))
  12683. (if pos (goto-char pos))
  12684. pos))
  12685. (defconst org-dblock-start-re
  12686. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  12687. "Matches the startline of a dynamic block, with parameters.")
  12688. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  12689. "Matches the end of a dyhamic block.")
  12690. (defun org-create-dblock (plist)
  12691. "Create a dynamic block section, with parameters taken from PLIST.
  12692. PLIST must containe a :name entry which is used as name of the block."
  12693. (unless (bolp) (newline))
  12694. (let ((name (plist-get plist :name)))
  12695. (insert "#+BEGIN: " name)
  12696. (while plist
  12697. (if (eq (car plist) :name)
  12698. (setq plist (cddr plist))
  12699. (insert " " (prin1-to-string (pop plist)))))
  12700. (insert "\n\n#+END:\n")
  12701. (beginning-of-line -2)))
  12702. (defun org-prepare-dblock ()
  12703. "Prepare dynamic block for refresh.
  12704. This empties the block, puts the cursor at the insert position and returns
  12705. the property list including an extra property :name with the block name."
  12706. (unless (looking-at org-dblock-start-re)
  12707. (error "Not at a dynamic block"))
  12708. (let* ((begdel (1+ (match-end 0)))
  12709. (name (org-no-properties (match-string 1)))
  12710. (params (append (list :name name)
  12711. (read (concat "(" (match-string 3) ")")))))
  12712. (unless (re-search-forward org-dblock-end-re nil t)
  12713. (error "Dynamic block not terminated"))
  12714. (delete-region begdel (match-beginning 0))
  12715. (goto-char begdel)
  12716. (open-line 1)
  12717. params))
  12718. (defun org-map-dblocks (&optional command)
  12719. "Apply COMMAND to all dynamic blocks in the current buffer.
  12720. If COMMAND is not given, use `org-update-dblock'."
  12721. (let ((cmd (or command 'org-update-dblock))
  12722. pos)
  12723. (save-excursion
  12724. (goto-char (point-min))
  12725. (while (re-search-forward org-dblock-start-re nil t)
  12726. (goto-char (setq pos (match-beginning 0)))
  12727. (condition-case nil
  12728. (funcall cmd)
  12729. (error (message "Error during update of dynamic block")))
  12730. (goto-char pos)
  12731. (unless (re-search-forward org-dblock-end-re nil t)
  12732. (error "Dynamic block not terminated"))))))
  12733. (defun org-dblock-update (&optional arg)
  12734. "User command for updating dynamic blocks.
  12735. Update the dynamic block at point. With prefix ARG, update all dynamic
  12736. blocks in the buffer."
  12737. (interactive "P")
  12738. (if arg
  12739. (org-update-all-dblocks)
  12740. (or (looking-at org-dblock-start-re)
  12741. (org-beginning-of-dblock))
  12742. (org-update-dblock)))
  12743. (defun org-update-dblock ()
  12744. "Update the dynamic block at point
  12745. This means to empty the block, parse for parameters and then call
  12746. the correct writing function."
  12747. (save-window-excursion
  12748. (let* ((pos (point))
  12749. (line (org-current-line))
  12750. (params (org-prepare-dblock))
  12751. (name (plist-get params :name))
  12752. (cmd (intern (concat "org-dblock-write:" name))))
  12753. (message "Updating dynamic block `%s' at line %d..." name line)
  12754. (funcall cmd params)
  12755. (message "Updating dynamic block `%s' at line %d...done" name line)
  12756. (goto-char pos))))
  12757. (defun org-beginning-of-dblock ()
  12758. "Find the beginning of the dynamic block at point.
  12759. Error if there is no scuh block at point."
  12760. (let ((pos (point))
  12761. beg)
  12762. (end-of-line 1)
  12763. (if (and (re-search-backward org-dblock-start-re nil t)
  12764. (setq beg (match-beginning 0))
  12765. (re-search-forward org-dblock-end-re nil t)
  12766. (> (match-end 0) pos))
  12767. (goto-char beg)
  12768. (goto-char pos)
  12769. (error "Not in a dynamic block"))))
  12770. (defun org-update-all-dblocks ()
  12771. "Update all dynamic blocks in the buffer.
  12772. This function can be used in a hook."
  12773. (when (org-mode-p)
  12774. (org-map-dblocks 'org-update-dblock)))
  12775. ;;;; Completion
  12776. (defconst org-additional-option-like-keywords
  12777. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  12778. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  12779. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  12780. (defun org-complete (&optional arg)
  12781. "Perform completion on word at point.
  12782. At the beginning of a headline, this completes TODO keywords as given in
  12783. `org-todo-keywords'.
  12784. If the current word is preceded by a backslash, completes the TeX symbols
  12785. that are supported for HTML support.
  12786. If the current word is preceded by \"#+\", completes special words for
  12787. setting file options.
  12788. In the line after \"#+STARTUP:, complete valid keywords.\"
  12789. At all other locations, this simply calls the value of
  12790. `org-completion-fallback-command'."
  12791. (interactive "P")
  12792. (org-without-partial-completion
  12793. (catch 'exit
  12794. (let* ((end (point))
  12795. (beg1 (save-excursion
  12796. (skip-chars-backward (org-re "[:alnum:]_@"))
  12797. (point)))
  12798. (beg (save-excursion
  12799. (skip-chars-backward "a-zA-Z0-9_:$")
  12800. (point)))
  12801. (confirm (lambda (x) (stringp (car x))))
  12802. (searchhead (equal (char-before beg) ?*))
  12803. (tag (and (equal (char-before beg1) ?:)
  12804. (equal (char-after (point-at-bol)) ?*)))
  12805. (prop (and (equal (char-before beg1) ?:)
  12806. (not (equal (char-after (point-at-bol)) ?*))))
  12807. (texp (equal (char-before beg) ?\\))
  12808. (link (equal (char-before beg) ?\[))
  12809. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  12810. beg)
  12811. "#+"))
  12812. (startup (string-match "^#\\+STARTUP:.*"
  12813. (buffer-substring (point-at-bol) (point))))
  12814. (completion-ignore-case opt)
  12815. (type nil)
  12816. (tbl nil)
  12817. (table (cond
  12818. (opt
  12819. (setq type :opt)
  12820. (append
  12821. (mapcar
  12822. (lambda (x)
  12823. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  12824. (cons (match-string 2 x) (match-string 1 x)))
  12825. (org-split-string (org-get-current-options) "\n"))
  12826. (mapcar 'list org-additional-option-like-keywords)))
  12827. (startup
  12828. (setq type :startup)
  12829. org-startup-options)
  12830. (link (append org-link-abbrev-alist-local
  12831. org-link-abbrev-alist))
  12832. (texp
  12833. (setq type :tex)
  12834. org-html-entities)
  12835. ((string-match "\\`\\*+[ \t]+\\'"
  12836. (buffer-substring (point-at-bol) beg))
  12837. (setq type :todo)
  12838. (mapcar 'list org-todo-keywords-1))
  12839. (searchhead
  12840. (setq type :searchhead)
  12841. (save-excursion
  12842. (goto-char (point-min))
  12843. (while (re-search-forward org-todo-line-regexp nil t)
  12844. (push (list
  12845. (org-make-org-heading-search-string
  12846. (match-string 3) t))
  12847. tbl)))
  12848. tbl)
  12849. (tag (setq type :tag beg beg1)
  12850. (or org-tag-alist (org-get-buffer-tags)))
  12851. (prop (setq type :prop beg beg1)
  12852. (mapcar 'list (org-buffer-property-keys)))
  12853. (t (progn
  12854. (call-interactively org-completion-fallback-command)
  12855. (throw 'exit nil)))))
  12856. (pattern (buffer-substring-no-properties beg end))
  12857. (completion (try-completion pattern table confirm)))
  12858. (cond ((eq completion t)
  12859. (if (not (assoc (upcase pattern) table))
  12860. (message "Already complete")
  12861. (if (equal type :opt)
  12862. (insert (substring (cdr (assoc (upcase pattern) table))
  12863. (length pattern)))
  12864. (if (memq type '(:tag :prop)) (insert ":")))))
  12865. ((null completion)
  12866. (message "Can't find completion for \"%s\"" pattern)
  12867. (ding))
  12868. ((not (string= pattern completion))
  12869. (delete-region beg end)
  12870. (if (string-match " +$" completion)
  12871. (setq completion (replace-match "" t t completion)))
  12872. (insert completion)
  12873. (if (get-buffer-window "*Completions*")
  12874. (delete-window (get-buffer-window "*Completions*")))
  12875. (if (assoc completion table)
  12876. (if (eq type :todo) (insert " ")
  12877. (if (memq type '(:tag :prop)) (insert ":"))))
  12878. (if (and (equal type :opt) (assoc completion table))
  12879. (message "%s" (substitute-command-keys
  12880. "Press \\[org-complete] again to insert example settings"))))
  12881. (t
  12882. (message "Making completion list...")
  12883. (let ((list (sort (all-completions pattern table confirm)
  12884. 'string<)))
  12885. (with-output-to-temp-buffer "*Completions*"
  12886. (condition-case nil
  12887. ;; Protection needed for XEmacs and emacs 21
  12888. (display-completion-list list pattern)
  12889. (error (display-completion-list list)))))
  12890. (message "Making completion list...%s" "done")))))))
  12891. ;;;; TODO, DEADLINE, Comments
  12892. (defun org-toggle-comment ()
  12893. "Change the COMMENT state of an entry."
  12894. (interactive)
  12895. (save-excursion
  12896. (org-back-to-heading)
  12897. (let (case-fold-search)
  12898. (if (looking-at (concat outline-regexp
  12899. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  12900. (replace-match "" t t nil 1)
  12901. (if (looking-at outline-regexp)
  12902. (progn
  12903. (goto-char (match-end 0))
  12904. (insert org-comment-string " ")))))))
  12905. (defvar org-last-todo-state-is-todo nil
  12906. "This is non-nil when the last TODO state change led to a TODO state.
  12907. If the last change removed the TODO tag or switched to DONE, then
  12908. this is nil.")
  12909. (defvar org-setting-tags nil) ; dynamically skiped
  12910. ;; FIXME: better place
  12911. (defun org-property-or-variable-value (var &optional inherit)
  12912. "Check if there is a property fixing the value of VAR.
  12913. If yes, return this value. If not, return the current value of the variable."
  12914. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12915. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12916. (read prop)
  12917. (symbol-value var))))
  12918. (defun org-parse-local-options (string var)
  12919. "Parse STRING for startup setting relevant for variable VAR."
  12920. (let ((rtn (symbol-value var))
  12921. e opts)
  12922. (save-match-data
  12923. (if (or (not string) (not (string-match "\\S-" string)))
  12924. rtn
  12925. (setq opts (delq nil (mapcar (lambda (x)
  12926. (setq e (assoc x org-startup-options))
  12927. (if (eq (nth 1 e) var) e nil))
  12928. (org-split-string string "[ \t]+"))))
  12929. (if (not opts)
  12930. rtn
  12931. (setq rtn nil)
  12932. (while (setq e (pop opts))
  12933. (if (not (nth 3 e))
  12934. (setq rtn (nth 2 e))
  12935. (if (not (listp rtn)) (setq rtn nil))
  12936. (push (nth 2 e) rtn)))
  12937. rtn)))))
  12938. (defvar org-blocker-hook nil
  12939. "Hook for functions that are allowed to block a state change.
  12940. Each function gets as its single argument a property list, see
  12941. `org-trigger-hook' for more information about this list.
  12942. If any of the functions in this hook returns nil, the state change
  12943. is blocked.")
  12944. (defvar org-trigger-hook nil
  12945. "Hook for functions that are triggered by a state change.
  12946. Each function gets as its single argument a property list with at least
  12947. the following elements:
  12948. (:type type-of-change :position pos-at-entry-start
  12949. :from old-state :to new-state)
  12950. Depending on the type, more properties may be present.
  12951. This mechanism is currently implemented for:
  12952. TODO state changes
  12953. ------------------
  12954. :type todo-state-change
  12955. :from previous state (keyword as a string), or nil
  12956. :to new state (keyword as a string), or nil")
  12957. (defun org-todo (&optional arg)
  12958. "Change the TODO state of an item.
  12959. The state of an item is given by a keyword at the start of the heading,
  12960. like
  12961. *** TODO Write paper
  12962. *** DONE Call mom
  12963. The different keywords are specified in the variable `org-todo-keywords'.
  12964. By default the available states are \"TODO\" and \"DONE\".
  12965. So for this example: when the item starts with TODO, it is changed to DONE.
  12966. When it starts with DONE, the DONE is removed. And when neither TODO nor
  12967. DONE are present, add TODO at the beginning of the heading.
  12968. With C-u prefix arg, use completion to determine the new state.
  12969. With numeric prefix arg, switch to that state.
  12970. For calling through lisp, arg is also interpreted in the following way:
  12971. 'none -> empty state
  12972. \"\"(empty string) -> switch to empty state
  12973. 'done -> switch to DONE
  12974. 'nextset -> switch to the next set of keywords
  12975. 'previousset -> switch to the previous set of keywords
  12976. \"WAITING\" -> switch to the specified keyword, but only if it
  12977. really is a member of `org-todo-keywords'."
  12978. (interactive "P")
  12979. (save-excursion
  12980. (catch 'exit
  12981. (org-back-to-heading)
  12982. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  12983. (or (looking-at (concat " +" org-todo-regexp " *"))
  12984. (looking-at " *"))
  12985. (let* ((match-data (match-data))
  12986. (startpos (point-at-bol))
  12987. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  12988. (org-log-done (org-parse-local-options logging 'org-log-done))
  12989. (org-log-repeat (org-parse-local-options logging 'org-log-repeat))
  12990. (this (match-string 1))
  12991. (hl-pos (match-beginning 0))
  12992. (head (org-get-todo-sequence-head this))
  12993. (ass (assoc head org-todo-kwd-alist))
  12994. (interpret (nth 1 ass))
  12995. (done-word (nth 3 ass))
  12996. (final-done-word (nth 4 ass))
  12997. (last-state (or this ""))
  12998. (completion-ignore-case t)
  12999. (member (member this org-todo-keywords-1))
  13000. (tail (cdr member))
  13001. (state (cond
  13002. ((and org-todo-key-trigger
  13003. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13004. (and (not arg) org-use-fast-todo-selection
  13005. (not (eq org-use-fast-todo-selection 'prefix)))))
  13006. ;; Use fast selection
  13007. (org-fast-todo-selection))
  13008. ((and (equal arg '(4))
  13009. (or (not org-use-fast-todo-selection)
  13010. (not org-todo-key-trigger)))
  13011. ;; Read a state with completion
  13012. (completing-read "State: " (mapcar (lambda(x) (list x))
  13013. org-todo-keywords-1)
  13014. nil t))
  13015. ((eq arg 'right)
  13016. (if this
  13017. (if tail (car tail) nil)
  13018. (car org-todo-keywords-1)))
  13019. ((eq arg 'left)
  13020. (if (equal member org-todo-keywords-1)
  13021. nil
  13022. (if this
  13023. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13024. org-todo-keywords-1)
  13025. (org-last org-todo-keywords-1))))
  13026. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13027. (setq arg nil))) ; hack to fall back to cycling
  13028. (arg
  13029. ;; user or caller requests a specific state
  13030. (cond
  13031. ((equal arg "") nil)
  13032. ((eq arg 'none) nil)
  13033. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13034. ((eq arg 'nextset)
  13035. (or (car (cdr (member head org-todo-heads)))
  13036. (car org-todo-heads)))
  13037. ((eq arg 'previousset)
  13038. (let ((org-todo-heads (reverse org-todo-heads)))
  13039. (or (car (cdr (member head org-todo-heads)))
  13040. (car org-todo-heads))))
  13041. ((car (member arg org-todo-keywords-1)))
  13042. ((nth (1- (prefix-numeric-value arg))
  13043. org-todo-keywords-1))))
  13044. ((null member) (or head (car org-todo-keywords-1)))
  13045. ((equal this final-done-word) nil) ;; -> make empty
  13046. ((null tail) nil) ;; -> first entry
  13047. ((eq interpret 'sequence)
  13048. (car tail))
  13049. ((memq interpret '(type priority))
  13050. (if (eq this-command last-command)
  13051. (car tail)
  13052. (if (> (length tail) 0)
  13053. (or done-word (car org-done-keywords))
  13054. nil)))
  13055. (t nil)))
  13056. (next (if state (concat " " state " ") " "))
  13057. (change-plist (list :type 'todo-state-change :from this :to state
  13058. :position startpos))
  13059. dostates)
  13060. (when org-blocker-hook
  13061. (unless (save-excursion
  13062. (save-match-data
  13063. (run-hook-with-args-until-failure
  13064. 'org-blocker-hook change-plist)))
  13065. (if (interactive-p)
  13066. (error "TODO state change from %s to %s blocked" this state)
  13067. ;; fail silently
  13068. (message "TODO state change from %s to %s blocked" this state)
  13069. (throw 'exit nil))))
  13070. (store-match-data match-data)
  13071. (replace-match next t t)
  13072. (unless (pos-visible-in-window-p hl-pos)
  13073. (message "TODO state changed to %s" (org-trim next)))
  13074. (unless head
  13075. (setq head (org-get-todo-sequence-head state)
  13076. ass (assoc head org-todo-kwd-alist)
  13077. interpret (nth 1 ass)
  13078. done-word (nth 3 ass)
  13079. final-done-word (nth 4 ass)))
  13080. (when (memq arg '(nextset previousset))
  13081. (message "Keyword-Set %d/%d: %s"
  13082. (- (length org-todo-sets) -1
  13083. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13084. (length org-todo-sets)
  13085. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13086. (setq org-last-todo-state-is-todo
  13087. (not (member state org-done-keywords)))
  13088. (when (and org-log-done (not (memq arg '(nextset previousset))))
  13089. (setq dostates (and (listp org-log-done) (memq 'state org-log-done)
  13090. (or (not org-todo-log-states)
  13091. (member state org-todo-log-states))))
  13092. (cond
  13093. ((and state (member state org-not-done-keywords)
  13094. (not (member this org-not-done-keywords)))
  13095. ;; This is now a todo state and was not one before
  13096. ;; Remove any CLOSED timestamp, and possibly log the state change
  13097. (org-add-planning-info nil nil 'closed)
  13098. (and dostates (org-add-log-maybe 'state state 'findpos)))
  13099. ((and state dostates)
  13100. ;; This is a non-nil state, and we need to log it
  13101. (org-add-log-maybe 'state state 'findpos))
  13102. ((and (member state org-done-keywords)
  13103. (not (member this org-done-keywords)))
  13104. ;; It is now done, and it was not done before
  13105. (org-add-planning-info 'closed (org-current-time))
  13106. (org-add-log-maybe 'done state 'findpos))))
  13107. ;; Fixup tag positioning
  13108. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13109. (run-hooks 'org-after-todo-state-change-hook)
  13110. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  13111. (if (and arg (not (member state org-done-keywords)))
  13112. (setq head (org-get-todo-sequence-head state)))
  13113. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13114. ;; Fixup cursor location if close to the keyword
  13115. (if (and (outline-on-heading-p)
  13116. (not (bolp))
  13117. (save-excursion (beginning-of-line 1)
  13118. (looking-at org-todo-line-regexp))
  13119. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13120. (progn
  13121. (goto-char (or (match-end 2) (match-end 1)))
  13122. (just-one-space)))
  13123. (when org-trigger-hook
  13124. (save-excursion
  13125. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13126. (defun org-get-todo-sequence-head (kwd)
  13127. "Return the head of the TODO sequence to which KWD belongs.
  13128. If KWD is not set, check if there is a text property remembering the
  13129. right sequence."
  13130. (let (p)
  13131. (cond
  13132. ((not kwd)
  13133. (or (get-text-property (point-at-bol) 'org-todo-head)
  13134. (progn
  13135. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13136. nil (point-at-eol)))
  13137. (get-text-property p 'org-todo-head))))
  13138. ((not (member kwd org-todo-keywords-1))
  13139. (car org-todo-keywords-1))
  13140. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13141. (defun org-fast-todo-selection ()
  13142. "Fast TODO keyword selection with single keys.
  13143. Returns the new TODO keyword, or nil if no state change should occur."
  13144. (let* ((fulltable org-todo-key-alist)
  13145. (done-keywords org-done-keywords) ;; needed for the faces.
  13146. (maxlen (apply 'max (mapcar
  13147. (lambda (x)
  13148. (if (stringp (car x)) (string-width (car x)) 0))
  13149. fulltable)))
  13150. (expert nil)
  13151. (fwidth (+ maxlen 3 1 3))
  13152. (ncol (/ (- (window-width) 4) fwidth))
  13153. tg cnt e c tbl
  13154. groups ingroup)
  13155. (save-window-excursion
  13156. (if expert
  13157. (set-buffer (get-buffer-create " *Org todo*"))
  13158. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13159. (erase-buffer)
  13160. (org-set-local 'org-done-keywords done-keywords)
  13161. (setq tbl fulltable cnt 0)
  13162. (while (setq e (pop tbl))
  13163. (cond
  13164. ((equal e '(:startgroup))
  13165. (push '() groups) (setq ingroup t)
  13166. (when (not (= cnt 0))
  13167. (setq cnt 0)
  13168. (insert "\n"))
  13169. (insert "{ "))
  13170. ((equal e '(:endgroup))
  13171. (setq ingroup nil cnt 0)
  13172. (insert "}\n"))
  13173. (t
  13174. (setq tg (car e) c (cdr e))
  13175. (if ingroup (push tg (car groups)))
  13176. (setq tg (org-add-props tg nil 'face
  13177. (org-get-todo-face tg)))
  13178. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13179. (insert "[" c "] " tg (make-string
  13180. (- fwidth 4 (length tg)) ?\ ))
  13181. (when (= (setq cnt (1+ cnt)) ncol)
  13182. (insert "\n")
  13183. (if ingroup (insert " "))
  13184. (setq cnt 0)))))
  13185. (insert "\n")
  13186. (goto-char (point-min))
  13187. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13188. (fit-window-to-buffer))
  13189. (message "[a-z..]:Set [SPC]:clear")
  13190. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13191. (cond
  13192. ((or (= c ?\C-g)
  13193. (and (= c ?q) (not (rassoc c fulltable))))
  13194. (setq quit-flag t))
  13195. ((= c ?\ ) nil)
  13196. ((setq e (rassoc c fulltable) tg (car e))
  13197. tg)
  13198. (t (setq quit-flag t))))))
  13199. (defun org-get-repeat ()
  13200. "Check if tere is a deadline/schedule with repeater in this entry."
  13201. (save-match-data
  13202. (save-excursion
  13203. (org-back-to-heading t)
  13204. (if (re-search-forward
  13205. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13206. (match-string 1)))))
  13207. (defvar org-last-changed-timestamp)
  13208. (defvar org-log-post-message)
  13209. (defun org-auto-repeat-maybe ()
  13210. "Check if the current headline contains a repeated deadline/schedule.
  13211. If yes, set TODO state back to what it was and change the base date
  13212. of repeating deadline/scheduled time stamps to new date.
  13213. This function should be run in the `org-after-todo-state-change-hook'."
  13214. ;; last-state is dynamically scoped into this function
  13215. (let* ((repeat (org-get-repeat))
  13216. (aa (assoc last-state org-todo-kwd-alist))
  13217. (interpret (nth 1 aa))
  13218. (head (nth 2 aa))
  13219. (done-word (nth 3 aa))
  13220. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13221. (msg "Entry repeats: ")
  13222. (org-log-done)
  13223. re type n what ts)
  13224. (when repeat
  13225. (org-todo (if (eq interpret 'type) last-state head))
  13226. (when (and org-log-repeat
  13227. (not (memq 'org-add-log-note
  13228. (default-value 'post-command-hook))))
  13229. ;; Make sure a note is taken
  13230. (let ((org-log-done '(done)))
  13231. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  13232. 'findpos)))
  13233. (org-back-to-heading t)
  13234. (org-add-planning-info nil nil 'closed)
  13235. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13236. org-deadline-time-regexp "\\)"))
  13237. (while (re-search-forward
  13238. re (save-excursion (outline-next-heading) (point)) t)
  13239. (setq type (if (match-end 1) org-scheduled-string org-deadline-string)
  13240. ts (match-string (if (match-end 2) 2 4)))
  13241. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13242. (setq n (string-to-number (match-string 1 ts))
  13243. what (match-string 2 ts))
  13244. (if (equal what "w") (setq n (* n 7) what "d"))
  13245. (org-timestamp-change n (cdr (assoc what whata))))
  13246. (setq msg (concat msg type org-last-changed-timestamp " ")))
  13247. (setq org-log-post-message msg)
  13248. (message "%s" msg))))
  13249. (defun org-show-todo-tree (arg)
  13250. "Make a compact tree which shows all headlines marked with TODO.
  13251. The tree will show the lines where the regexp matches, and all higher
  13252. headlines above the match.
  13253. With \\[universal-argument] prefix, also show the DONE entries.
  13254. With a numeric prefix N, construct a sparse tree for the Nth element
  13255. of `org-todo-keywords-1'."
  13256. (interactive "P")
  13257. (let ((case-fold-search nil)
  13258. (kwd-re
  13259. (cond ((null arg) org-not-done-regexp)
  13260. ((equal arg '(4))
  13261. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13262. (mapcar 'list org-todo-keywords-1))))
  13263. (concat "\\("
  13264. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13265. "\\)\\>")))
  13266. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13267. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13268. org-todo-keywords-1)))
  13269. (t (error "Invalid prefix argument: %s" arg)))))
  13270. (message "%d TODO entries found"
  13271. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13272. (defun org-deadline (&optional remove)
  13273. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13274. With argument REMOVE, remove any deadline from the item."
  13275. (interactive "P")
  13276. (if remove
  13277. (progn
  13278. (org-add-planning-info nil nil 'deadline)
  13279. (message "Item no longer has a deadline."))
  13280. (org-add-planning-info 'deadline nil 'closed)))
  13281. (defun org-schedule (&optional remove)
  13282. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13283. With argument REMOVE, remove any scheduling date from the item."
  13284. (interactive "P")
  13285. (if remove
  13286. (progn
  13287. (org-add-planning-info nil nil 'scheduled)
  13288. (message "Item is no longer scheduled."))
  13289. (org-add-planning-info 'scheduled nil 'closed)))
  13290. (defun org-add-planning-info (what &optional time &rest remove)
  13291. "Insert new timestamp with keyword in the line directly after the headline.
  13292. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13293. If non is given, the user is prompted for a date.
  13294. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13295. be removed."
  13296. (interactive)
  13297. (let (org-time-was-given org-end-time-was-given)
  13298. (when what (setq time (or time (org-read-date nil 'to-time))))
  13299. (when (and org-insert-labeled-timestamps-at-point
  13300. (member what '(scheduled deadline)))
  13301. (insert
  13302. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13303. (org-insert-time-stamp time org-time-was-given
  13304. nil nil nil (list org-end-time-was-given))
  13305. (setq what nil))
  13306. (save-excursion
  13307. (save-restriction
  13308. (let (col list elt ts buffer-invisibility-spec)
  13309. (org-back-to-heading t)
  13310. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13311. (goto-char (match-end 1))
  13312. (setq col (current-column))
  13313. (goto-char (match-end 0))
  13314. (if (eobp) (insert "\n") (forward-char 1))
  13315. (if (and (not (looking-at outline-regexp))
  13316. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13317. "[^\r\n]*"))
  13318. (not (equal (match-string 1) org-clock-string)))
  13319. (narrow-to-region (match-beginning 0) (match-end 0))
  13320. (insert-before-markers "\n")
  13321. (backward-char 1)
  13322. (narrow-to-region (point) (point))
  13323. (indent-to-column col))
  13324. ;; Check if we have to remove something.
  13325. (setq list (cons what remove))
  13326. (while list
  13327. (setq elt (pop list))
  13328. (goto-char (point-min))
  13329. (when (or (and (eq elt 'scheduled)
  13330. (re-search-forward org-scheduled-time-regexp nil t))
  13331. (and (eq elt 'deadline)
  13332. (re-search-forward org-deadline-time-regexp nil t))
  13333. (and (eq elt 'closed)
  13334. (re-search-forward org-closed-time-regexp nil t)))
  13335. (replace-match "")
  13336. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13337. (if (looking-at " +") (replace-match ""))))
  13338. (goto-char (point-max))
  13339. (when what
  13340. (insert
  13341. (if (not (equal (char-before) ?\ )) " " "")
  13342. (cond ((eq what 'scheduled) org-scheduled-string)
  13343. ((eq what 'deadline) org-deadline-string)
  13344. ((eq what 'closed) org-closed-string))
  13345. " ")
  13346. (setq ts (org-insert-time-stamp
  13347. time
  13348. (or org-time-was-given
  13349. (and (eq what 'closed) org-log-done-with-time))
  13350. (eq what 'closed)
  13351. nil nil (list org-end-time-was-given)))
  13352. (end-of-line 1))
  13353. (goto-char (point-min))
  13354. (widen)
  13355. (if (looking-at "[ \t]+\r?\n")
  13356. (replace-match ""))
  13357. ts)))))
  13358. (defvar org-log-note-marker (make-marker))
  13359. (defvar org-log-note-purpose nil)
  13360. (defvar org-log-note-state nil)
  13361. (defvar org-log-note-window-configuration nil)
  13362. (defvar org-log-note-return-to (make-marker))
  13363. (defvar org-log-post-message nil
  13364. "Message to be displayed after a log note has been stored.
  13365. The auto-repeater uses this.")
  13366. (defun org-add-log-maybe (&optional purpose state findpos)
  13367. "Set up the post command hook to take a note."
  13368. (save-excursion
  13369. (when (and (listp org-log-done)
  13370. (memq purpose org-log-done))
  13371. (when findpos
  13372. (org-back-to-heading t)
  13373. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13374. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13375. "[^\r\n]*\\)?"))
  13376. (goto-char (match-end 0))
  13377. (unless org-log-states-order-reversed
  13378. (and (= (char-after) ?\n) (forward-char 1))
  13379. (org-skip-over-state-notes)
  13380. (skip-chars-backward " \t\n\r")))
  13381. (move-marker org-log-note-marker (point))
  13382. (setq org-log-note-purpose purpose)
  13383. (setq org-log-note-state state)
  13384. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  13385. (defun org-skip-over-state-notes ()
  13386. "Skip past the list of State notes in an entry."
  13387. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13388. (while (looking-at "[ \t]*- State")
  13389. (condition-case nil
  13390. (org-next-item)
  13391. (error (org-end-of-item)))))
  13392. (defun org-add-log-note (&optional purpose)
  13393. "Pop up a window for taking a note, and add this note later at point."
  13394. (remove-hook 'post-command-hook 'org-add-log-note)
  13395. (setq org-log-note-window-configuration (current-window-configuration))
  13396. (delete-other-windows)
  13397. (move-marker org-log-note-return-to (point))
  13398. (switch-to-buffer (marker-buffer org-log-note-marker))
  13399. (goto-char org-log-note-marker)
  13400. (org-switch-to-buffer-other-window "*Org Note*")
  13401. (erase-buffer)
  13402. (let ((org-inhibit-startup t)) (org-mode))
  13403. (insert (format "# Insert note for %s.
  13404. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13405. (cond
  13406. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13407. ((eq org-log-note-purpose 'done) "closed todo item")
  13408. ((eq org-log-note-purpose 'state)
  13409. (format "state change to \"%s\"" org-log-note-state))
  13410. (t (error "This should not happen")))))
  13411. (org-set-local 'org-finish-function 'org-store-log-note))
  13412. (defun org-store-log-note ()
  13413. "Finish taking a log note, and insert it to where it belongs."
  13414. (let ((txt (buffer-string))
  13415. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13416. lines ind)
  13417. (kill-buffer (current-buffer))
  13418. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13419. (setq txt (replace-match "" t t txt)))
  13420. (if (string-match "\\s-+\\'" txt)
  13421. (setq txt (replace-match "" t t txt)))
  13422. (setq lines (org-split-string txt "\n"))
  13423. (when (and note (string-match "\\S-" note))
  13424. (setq note
  13425. (org-replace-escapes
  13426. note
  13427. (list (cons "%u" (user-login-name))
  13428. (cons "%U" user-full-name)
  13429. (cons "%t" (format-time-string
  13430. (org-time-stamp-format 'long 'inactive)
  13431. (current-time)))
  13432. (cons "%s" (if org-log-note-state
  13433. (concat "\"" org-log-note-state "\"")
  13434. "")))))
  13435. (if lines (setq note (concat note " \\\\")))
  13436. (push note lines))
  13437. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13438. (when lines
  13439. (save-excursion
  13440. (set-buffer (marker-buffer org-log-note-marker))
  13441. (save-excursion
  13442. (goto-char org-log-note-marker)
  13443. (move-marker org-log-note-marker nil)
  13444. (end-of-line 1)
  13445. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13446. (indent-relative nil)
  13447. (insert "- " (pop lines))
  13448. (org-indent-line-function)
  13449. (beginning-of-line 1)
  13450. (looking-at "[ \t]*")
  13451. (setq ind (concat (match-string 0) " "))
  13452. (end-of-line 1)
  13453. (while lines (insert "\n" ind (pop lines)))))))
  13454. (set-window-configuration org-log-note-window-configuration)
  13455. (with-current-buffer (marker-buffer org-log-note-return-to)
  13456. (goto-char org-log-note-return-to))
  13457. (move-marker org-log-note-return-to nil)
  13458. (and org-log-post-message (message "%s" org-log-post-message)))
  13459. ;; FIXME: what else would be useful?
  13460. ;; - priority
  13461. ;; - date
  13462. (defun org-sparse-tree (&optional arg)
  13463. "Create a sparse tree, prompt for the details.
  13464. This command can create sparse trees. You first need to select the type
  13465. of match used to create the tree:
  13466. t Show entries with a specific TODO keyword.
  13467. T Show entries selected by a tags match.
  13468. p Enter a property name and its value (both with completion on existing
  13469. names/values) and show entries with that property.
  13470. r Show entries matching a regular expression
  13471. d Show deadlines due within `org-deadline-warning-days'."
  13472. (interactive "P")
  13473. (let (ans kwd value)
  13474. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13475. (setq ans (read-char-exclusive))
  13476. (cond
  13477. ((equal ans ?d)
  13478. (call-interactively 'org-check-deadlines))
  13479. ((equal ans ?b)
  13480. (call-interactively 'org-check-before-date))
  13481. ((equal ans ?t)
  13482. (org-show-todo-tree '(4)))
  13483. ((equal ans ?T)
  13484. (call-interactively 'org-tags-sparse-tree))
  13485. ((member ans '(?p ?P))
  13486. (setq kwd (completing-read "Property: "
  13487. (mapcar 'list (org-buffer-property-keys))))
  13488. (setq value (completing-read "Value: "
  13489. (mapcar 'list (org-property-values kwd))))
  13490. (unless (string-match "\\`{.*}\\'" value)
  13491. (setq value (concat "\"" value "\"")))
  13492. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13493. ((member ans '(?r ?R ?/))
  13494. (call-interactively 'org-occur))
  13495. (t (error "No such sparse tree command \"%c\"" ans)))))
  13496. (defvar org-occur-highlights nil)
  13497. (make-variable-buffer-local 'org-occur-highlights)
  13498. (defun org-occur (regexp &optional keep-previous callback)
  13499. "Make a compact tree which shows all matches of REGEXP.
  13500. The tree will show the lines where the regexp matches, and all higher
  13501. headlines above the match. It will also show the heading after the match,
  13502. to make sure editing the matching entry is easy.
  13503. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13504. call to `org-occur' will be kept, to allow stacking of calls to this
  13505. command.
  13506. If CALLBACK is non-nil, it is a function which is called to confirm
  13507. that the match should indeed be shown."
  13508. (interactive "sRegexp: \nP")
  13509. (or keep-previous (org-remove-occur-highlights nil nil t))
  13510. (let ((cnt 0))
  13511. (save-excursion
  13512. (goto-char (point-min))
  13513. (if (or (not keep-previous) ; do not want to keep
  13514. (not org-occur-highlights)) ; no previous matches
  13515. ;; hide everything
  13516. (org-overview))
  13517. (while (re-search-forward regexp nil t)
  13518. (when (or (not callback)
  13519. (save-match-data (funcall callback)))
  13520. (setq cnt (1+ cnt))
  13521. (when org-highlight-sparse-tree-matches
  13522. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13523. (org-show-context 'occur-tree))))
  13524. (when org-remove-highlights-with-change
  13525. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13526. nil 'local))
  13527. (unless org-sparse-tree-open-archived-trees
  13528. (org-hide-archived-subtrees (point-min) (point-max)))
  13529. (run-hooks 'org-occur-hook)
  13530. (if (interactive-p)
  13531. (message "%d match(es) for regexp %s" cnt regexp))
  13532. cnt))
  13533. (defun org-show-context (&optional key)
  13534. "Make sure point and context and visible.
  13535. How much context is shown depends upon the variables
  13536. `org-show-hierarchy-above', `org-show-following-heading'. and
  13537. `org-show-siblings'."
  13538. (let ((heading-p (org-on-heading-p t))
  13539. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13540. (following-p (org-get-alist-option org-show-following-heading key))
  13541. (entry-p (org-get-alist-option org-show-entry-below key))
  13542. (siblings-p (org-get-alist-option org-show-siblings key)))
  13543. (catch 'exit
  13544. ;; Show heading or entry text
  13545. (if (and heading-p (not entry-p))
  13546. (org-flag-heading nil) ; only show the heading
  13547. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13548. (org-show-hidden-entry))) ; show entire entry
  13549. (when following-p
  13550. ;; Show next sibling, or heading below text
  13551. (save-excursion
  13552. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13553. (org-flag-heading nil))))
  13554. (when siblings-p (org-show-siblings))
  13555. (when hierarchy-p
  13556. ;; show all higher headings, possibly with siblings
  13557. (save-excursion
  13558. (while (and (condition-case nil
  13559. (progn (org-up-heading-all 1) t)
  13560. (error nil))
  13561. (not (bobp)))
  13562. (org-flag-heading nil)
  13563. (when siblings-p (org-show-siblings))))))))
  13564. (defun org-reveal (&optional siblings)
  13565. "Show current entry, hierarchy above it, and the following headline.
  13566. This can be used to show a consistent set of context around locations
  13567. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13568. not t for the search context.
  13569. With optional argument SIBLINGS, on each level of the hierarchy all
  13570. siblings are shown. This repairs the tree structure to what it would
  13571. look like when opened with hierarchical calls to `org-cycle'."
  13572. (interactive "P")
  13573. (let ((org-show-hierarchy-above t)
  13574. (org-show-following-heading t)
  13575. (org-show-siblings (if siblings t org-show-siblings)))
  13576. (org-show-context nil)))
  13577. (defun org-highlight-new-match (beg end)
  13578. "Highlight from BEG to END and mark the highlight is an occur headline."
  13579. (let ((ov (org-make-overlay beg end)))
  13580. (org-overlay-put ov 'face 'secondary-selection)
  13581. (push ov org-occur-highlights)))
  13582. (defun org-remove-occur-highlights (&optional beg end noremove)
  13583. "Remove the occur highlights from the buffer.
  13584. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13585. from the `before-change-functions' in the current buffer."
  13586. (interactive)
  13587. (unless org-inhibit-highlight-removal
  13588. (mapc 'org-delete-overlay org-occur-highlights)
  13589. (setq org-occur-highlights nil)
  13590. (unless noremove
  13591. (remove-hook 'before-change-functions
  13592. 'org-remove-occur-highlights 'local))))
  13593. ;;;; Priorities
  13594. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13595. "Regular expression matching the priority indicator.")
  13596. (defvar org-remove-priority-next-time nil)
  13597. (defun org-priority-up ()
  13598. "Increase the priority of the current item."
  13599. (interactive)
  13600. (org-priority 'up))
  13601. (defun org-priority-down ()
  13602. "Decrease the priority of the current item."
  13603. (interactive)
  13604. (org-priority 'down))
  13605. (defun org-priority (&optional action)
  13606. "Change the priority of an item by ARG.
  13607. ACTION can be `set', `up', `down', or a character."
  13608. (interactive)
  13609. (setq action (or action 'set))
  13610. (let (current new news have remove)
  13611. (save-excursion
  13612. (org-back-to-heading)
  13613. (if (looking-at org-priority-regexp)
  13614. (setq current (string-to-char (match-string 2))
  13615. have t)
  13616. (setq current org-default-priority))
  13617. (cond
  13618. ((or (eq action 'set) (integerp action))
  13619. (if (integerp action)
  13620. (setq new action)
  13621. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13622. (setq new (read-char-exclusive)))
  13623. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13624. (= (upcase org-lowest-priority) org-lowest-priority))
  13625. (setq new (upcase new)))
  13626. (cond ((equal new ?\ ) (setq remove t))
  13627. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  13628. (error "Priority must be between `%c' and `%c'"
  13629. org-highest-priority org-lowest-priority))))
  13630. ((eq action 'up)
  13631. (if (and (not have) (eq last-command this-command))
  13632. (setq new org-lowest-priority)
  13633. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13634. org-default-priority (1- current)))))
  13635. ((eq action 'down)
  13636. (if (and (not have) (eq last-command this-command))
  13637. (setq new org-highest-priority)
  13638. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13639. org-default-priority (1+ current)))))
  13640. (t (error "Invalid action")))
  13641. (if (or (< (upcase new) org-highest-priority)
  13642. (> (upcase new) org-lowest-priority))
  13643. (setq remove t))
  13644. (setq news (format "%c" new))
  13645. (if have
  13646. (if remove
  13647. (replace-match "" t t nil 1)
  13648. (replace-match news t t nil 2))
  13649. (if remove
  13650. (error "No priority cookie found in line")
  13651. (looking-at org-todo-line-regexp)
  13652. (if (match-end 2)
  13653. (progn
  13654. (goto-char (match-end 2))
  13655. (insert " [#" news "]"))
  13656. (goto-char (match-beginning 3))
  13657. (insert "[#" news "] ")))))
  13658. (org-preserve-lc (org-set-tags nil 'align))
  13659. (if remove
  13660. (message "Priority removed")
  13661. (message "Priority of current item set to %s" news))))
  13662. (defun org-get-priority (s)
  13663. "Find priority cookie and return priority."
  13664. (save-match-data
  13665. (if (not (string-match org-priority-regexp s))
  13666. (* 1000 (- org-lowest-priority org-default-priority))
  13667. (* 1000 (- org-lowest-priority
  13668. (string-to-char (match-string 2 s)))))))
  13669. ;;;; Tags
  13670. (defun org-scan-tags (action matcher &optional todo-only)
  13671. "Scan headline tags with inheritance and produce output ACTION.
  13672. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  13673. evaluated, testing if a given set of tags qualifies a headline for
  13674. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  13675. are included in the output."
  13676. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  13677. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  13678. (org-re
  13679. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  13680. (props (list 'face nil
  13681. 'done-face 'org-done
  13682. 'undone-face nil
  13683. 'mouse-face 'highlight
  13684. 'org-not-done-regexp org-not-done-regexp
  13685. 'org-todo-regexp org-todo-regexp
  13686. 'keymap org-agenda-keymap
  13687. 'help-echo
  13688. (format "mouse-2 or RET jump to org file %s"
  13689. (abbreviate-file-name
  13690. (or (buffer-file-name (buffer-base-buffer))
  13691. (buffer-name (buffer-base-buffer)))))))
  13692. (case-fold-search nil)
  13693. lspos
  13694. tags tags-list tags-alist (llast 0) rtn level category i txt
  13695. todo marker entry priority)
  13696. (save-excursion
  13697. (goto-char (point-min))
  13698. (when (eq action 'sparse-tree)
  13699. (org-overview)
  13700. (org-remove-occur-highlights))
  13701. (while (re-search-forward re nil t)
  13702. (catch :skip
  13703. (setq todo (if (match-end 1) (match-string 2))
  13704. tags (if (match-end 4) (match-string 4)))
  13705. (goto-char (setq lspos (1+ (match-beginning 0))))
  13706. (setq level (org-reduced-level (funcall outline-level))
  13707. category (org-get-category))
  13708. (setq i llast llast level)
  13709. ;; remove tag lists from same and sublevels
  13710. (while (>= i level)
  13711. (when (setq entry (assoc i tags-alist))
  13712. (setq tags-alist (delete entry tags-alist)))
  13713. (setq i (1- i)))
  13714. ;; add the nex tags
  13715. (when tags
  13716. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  13717. tags-alist
  13718. (cons (cons level tags) tags-alist)))
  13719. ;; compile tags for current headline
  13720. (setq tags-list
  13721. (if org-use-tag-inheritance
  13722. (apply 'append (mapcar 'cdr tags-alist))
  13723. tags))
  13724. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  13725. (eval matcher)
  13726. (or (not org-agenda-skip-archived-trees)
  13727. (not (member org-archive-tag tags-list))))
  13728. (and (eq action 'agenda) (org-agenda-skip))
  13729. ;; list this headline
  13730. (if (eq action 'sparse-tree)
  13731. (progn
  13732. (and org-highlight-sparse-tree-matches
  13733. (org-get-heading) (match-end 0)
  13734. (org-highlight-new-match
  13735. (match-beginning 0) (match-beginning 1)))
  13736. (org-show-context 'tags-tree))
  13737. (setq txt (org-format-agenda-item
  13738. ""
  13739. (concat
  13740. (if org-tags-match-list-sublevels
  13741. (make-string (1- level) ?.) "")
  13742. (org-get-heading))
  13743. category tags-list)
  13744. priority (org-get-priority txt))
  13745. (goto-char lspos)
  13746. (setq marker (org-agenda-new-marker))
  13747. (org-add-props txt props
  13748. 'org-marker marker 'org-hd-marker marker 'org-category category
  13749. 'priority priority 'type "tagsmatch")
  13750. (push txt rtn))
  13751. ;; if we are to skip sublevels, jump to end of subtree
  13752. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  13753. (when (and (eq action 'sparse-tree)
  13754. (not org-sparse-tree-open-archived-trees))
  13755. (org-hide-archived-subtrees (point-min) (point-max)))
  13756. (nreverse rtn)))
  13757. (defvar todo-only) ;; dynamically scoped
  13758. (defun org-tags-sparse-tree (&optional todo-only match)
  13759. "Create a sparse tree according to tags string MATCH.
  13760. MATCH can contain positive and negative selection of tags, like
  13761. \"+WORK+URGENT-WITHBOSS\".
  13762. If optional argument TODO_ONLY is non-nil, only select lines that are
  13763. also TODO lines."
  13764. (interactive "P")
  13765. (org-prepare-agenda-buffers (list (current-buffer)))
  13766. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13767. (defvar org-cached-props nil)
  13768. (defun org-cached-entry-get (pom property)
  13769. (if (or (eq t org-use-property-inheritance)
  13770. (member property org-use-property-inheritance))
  13771. ;; Caching is not possible, check it directly
  13772. (org-entry-get pom property 'inherit)
  13773. ;; Get all properties, so that we can do complicated checks easily
  13774. (cdr (assoc property (or org-cached-props
  13775. (setq org-cached-props
  13776. (org-entry-properties pom)))))))
  13777. (defun org-global-tags-completion-table (&optional files)
  13778. "Return the list of all tags in all agenda buffer/files."
  13779. (save-excursion
  13780. (org-uniquify
  13781. (apply 'append
  13782. (mapcar
  13783. (lambda (file)
  13784. (set-buffer (find-file-noselect file))
  13785. (org-get-buffer-tags))
  13786. (if (and files (car files))
  13787. files
  13788. (org-agenda-files)))))))
  13789. (defun org-make-tags-matcher (match)
  13790. "Create the TAGS//TODO matcher form for the selection string MATCH."
  13791. ;; todo-only is scoped dynamically into this function, and the function
  13792. ;; may change it it the matcher asksk for it.
  13793. (unless match
  13794. ;; Get a new match request, with completion
  13795. (let ((org-last-tags-completion-table
  13796. (org-global-tags-completion-table)))
  13797. (setq match (completing-read
  13798. "Match: " 'org-tags-completion-function nil nil nil
  13799. 'org-tags-history))))
  13800. ;; Parse the string and create a lisp form
  13801. (let ((match0 match)
  13802. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]+\"\\)\\|[[:alnum:]_@]+\\)"))
  13803. minus tag mm
  13804. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13805. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  13806. (if (string-match "/+" match)
  13807. ;; match contains also a todo-matching request
  13808. (progn
  13809. (setq tagsmatch (substring match 0 (match-beginning 0))
  13810. todomatch (substring match (match-end 0)))
  13811. (if (string-match "^!" todomatch)
  13812. (setq todo-only t todomatch (substring todomatch 1)))
  13813. (if (string-match "^\\s-*$" todomatch)
  13814. (setq todomatch nil)))
  13815. ;; only matching tags
  13816. (setq tagsmatch match todomatch nil))
  13817. ;; Make the tags matcher
  13818. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13819. (setq tagsmatcher t)
  13820. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13821. (while (setq term (pop orterms))
  13822. (while (and (equal (substring term -1) "\\") orterms)
  13823. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13824. (while (string-match re term)
  13825. (setq minus (and (match-end 1)
  13826. (equal (match-string 1 term) "-"))
  13827. tag (match-string 2 term)
  13828. re-p (equal (string-to-char tag) ?{)
  13829. level-p (match-end 3)
  13830. prop-p (match-end 4)
  13831. mm (cond
  13832. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13833. (level-p `(= level ,(string-to-number
  13834. (match-string 3 term))))
  13835. (prop-p
  13836. (setq pn (match-string 4 term)
  13837. pv (match-string 5 term)
  13838. cat-p (equal pn "CATEGORY")
  13839. re-p (equal (string-to-char pv) ?{)
  13840. pv (substring pv 1 -1))
  13841. (if (equal pn "CATEGORY")
  13842. (setq gv '(get-text-property (point) 'org-category))
  13843. (setq gv `(org-cached-entry-get nil ,pn)))
  13844. (if re-p
  13845. `(string-match ,pv (or ,gv ""))
  13846. `(equal ,pv ,gv)))
  13847. (t `(member ,(downcase tag) tags-list)))
  13848. mm (if minus (list 'not mm) mm)
  13849. term (substring term (match-end 0)))
  13850. (push mm tagsmatcher))
  13851. (push (if (> (length tagsmatcher) 1)
  13852. (cons 'and tagsmatcher)
  13853. (car tagsmatcher))
  13854. orlist)
  13855. (setq tagsmatcher nil))
  13856. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13857. (setq tagsmatcher
  13858. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13859. ;; Make the todo matcher
  13860. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13861. (setq todomatcher t)
  13862. (setq orterms (org-split-string todomatch "|") orlist nil)
  13863. (while (setq term (pop orterms))
  13864. (while (string-match re term)
  13865. (setq minus (and (match-end 1)
  13866. (equal (match-string 1 term) "-"))
  13867. kwd (match-string 2 term)
  13868. re-p (equal (string-to-char kwd) ?{)
  13869. term (substring term (match-end 0))
  13870. mm (if re-p
  13871. `(string-match ,(substring kwd 1 -1) todo)
  13872. (list 'equal 'todo kwd))
  13873. mm (if minus (list 'not mm) mm))
  13874. (push mm todomatcher))
  13875. (push (if (> (length todomatcher) 1)
  13876. (cons 'and todomatcher)
  13877. (car todomatcher))
  13878. orlist)
  13879. (setq todomatcher nil))
  13880. (setq todomatcher (if (> (length orlist) 1)
  13881. (cons 'or orlist) (car orlist))))
  13882. ;; Return the string and lisp forms of the matcher
  13883. (setq matcher (if todomatcher
  13884. (list 'and tagsmatcher todomatcher)
  13885. tagsmatcher))
  13886. (cons match0 matcher)))
  13887. (defun org-match-any-p (re list)
  13888. "Does re match any element of list?"
  13889. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13890. (delq nil list))
  13891. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  13892. (defvar org-tags-overlay (org-make-overlay 1 1))
  13893. (org-detach-overlay org-tags-overlay)
  13894. (defun org-align-tags-here (to-col)
  13895. ;; Assumes that this is a headline
  13896. (let ((pos (point)) (col (current-column)) tags)
  13897. (beginning-of-line 1)
  13898. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13899. (< pos (match-beginning 2)))
  13900. (progn
  13901. (setq tags (match-string 2))
  13902. (goto-char (match-beginning 1))
  13903. (insert " ")
  13904. (delete-region (point) (1+ (match-end 0)))
  13905. (backward-char 1)
  13906. (move-to-column
  13907. (max (1+ (current-column))
  13908. (1+ col)
  13909. (if (> to-col 0)
  13910. to-col
  13911. (- (abs to-col) (length tags))))
  13912. t)
  13913. (insert tags)
  13914. (move-to-column (min (current-column) col) t))
  13915. (goto-char pos))))
  13916. (defun org-set-tags (&optional arg just-align)
  13917. "Set the tags for the current headline.
  13918. With prefix ARG, realign all tags in headings in the current buffer."
  13919. (interactive "P")
  13920. (let* ((re (concat "^" outline-regexp))
  13921. (current (org-get-tags-string))
  13922. (col (current-column))
  13923. (org-setting-tags t)
  13924. table current-tags inherited-tags ; computed below when needed
  13925. tags p0 c0 c1 rpl)
  13926. (if arg
  13927. (save-excursion
  13928. (goto-char (point-min))
  13929. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13930. (while (re-search-forward re nil t)
  13931. (org-set-tags nil t)
  13932. (end-of-line 1)))
  13933. (message "All tags realigned to column %d" org-tags-column))
  13934. (if just-align
  13935. (setq tags current)
  13936. ;; Get a new set of tags from the user
  13937. (save-excursion
  13938. (setq table (or org-tag-alist (org-get-buffer-tags))
  13939. org-last-tags-completion-table table
  13940. current-tags (org-split-string current ":")
  13941. inherited-tags (nreverse
  13942. (nthcdr (length current-tags)
  13943. (nreverse (org-get-tags-at))))
  13944. tags
  13945. (if (or (eq t org-use-fast-tag-selection)
  13946. (and org-use-fast-tag-selection
  13947. (delq nil (mapcar 'cdr table))))
  13948. (org-fast-tag-selection
  13949. current-tags inherited-tags table
  13950. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  13951. (let ((org-add-colon-after-tag-completion t))
  13952. (org-trim
  13953. (org-without-partial-completion
  13954. (completing-read "Tags: " 'org-tags-completion-function
  13955. nil nil current 'org-tags-history)))))))
  13956. (while (string-match "[-+&]+" tags)
  13957. ;; No boolean logic, just a list
  13958. (setq tags (replace-match ":" t t tags))))
  13959. (if (string-match "\\`[\t ]*\\'" tags)
  13960. (setq tags "")
  13961. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13962. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13963. ;; Insert new tags at the correct column
  13964. (beginning-of-line 1)
  13965. (cond
  13966. ((and (equal current "") (equal tags "")))
  13967. ((re-search-forward
  13968. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13969. (point-at-eol) t)
  13970. (if (equal tags "")
  13971. (setq rpl "")
  13972. (goto-char (match-beginning 0))
  13973. (setq c0 (current-column) p0 (point)
  13974. c1 (max (1+ c0) (if (> org-tags-column 0)
  13975. org-tags-column
  13976. (- (- org-tags-column) (length tags))))
  13977. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13978. (replace-match rpl t t)
  13979. (and (not (featurep 'xemacs)) c0 (tabify p0 (point)))
  13980. tags)
  13981. (t (error "Tags alignment failed")))
  13982. (move-to-column col)
  13983. (unless just-align
  13984. (run-hooks 'org-after-tags-change-hook)))))
  13985. (defun org-change-tag-in-region (beg end tag off)
  13986. "Add or remove TAG for each entry in the region.
  13987. This works in the agenda, and also in an org-mode buffer."
  13988. (interactive
  13989. (list (region-beginning) (region-end)
  13990. (let ((org-last-tags-completion-table
  13991. (if (org-mode-p)
  13992. (org-get-buffer-tags)
  13993. (org-global-tags-completion-table))))
  13994. (completing-read
  13995. "Tag: " 'org-tags-completion-function nil nil nil
  13996. 'org-tags-history))
  13997. (progn
  13998. (message "[s]et or [r]emove? ")
  13999. (equal (read-char-exclusive) ?r))))
  14000. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14001. (let ((agendap (equal major-mode 'org-agenda-mode))
  14002. l1 l2 m buf pos newhead (cnt 0))
  14003. (goto-char end)
  14004. (setq l2 (1- (org-current-line)))
  14005. (goto-char beg)
  14006. (setq l1 (org-current-line))
  14007. (loop for l from l1 to l2 do
  14008. (goto-line l)
  14009. (setq m (get-text-property (point) 'org-hd-marker))
  14010. (when (or (and (org-mode-p) (org-on-heading-p))
  14011. (and agendap m))
  14012. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14013. pos (if agendap m (point)))
  14014. (with-current-buffer buf
  14015. (save-excursion
  14016. (save-restriction
  14017. (goto-char pos)
  14018. (setq cnt (1+ cnt))
  14019. (org-toggle-tag tag (if off 'off 'on))
  14020. (setq newhead (org-get-heading)))))
  14021. (and agendap (org-agenda-change-all-lines newhead m))))
  14022. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14023. (defun org-tags-completion-function (string predicate &optional flag)
  14024. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14025. (confirm (lambda (x) (stringp (car x)))))
  14026. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14027. (setq s1 (match-string 1 string)
  14028. s2 (match-string 2 string))
  14029. (setq s1 "" s2 string))
  14030. (cond
  14031. ((eq flag nil)
  14032. ;; try completion
  14033. (setq rtn (try-completion s2 ctable confirm))
  14034. (if (stringp rtn)
  14035. (setq rtn
  14036. (concat s1 s2 (substring rtn (length s2))
  14037. (if (and org-add-colon-after-tag-completion
  14038. (assoc rtn ctable))
  14039. ":" ""))))
  14040. rtn)
  14041. ((eq flag t)
  14042. ;; all-completions
  14043. (all-completions s2 ctable confirm)
  14044. )
  14045. ((eq flag 'lambda)
  14046. ;; exact match?
  14047. (assoc s2 ctable)))
  14048. ))
  14049. (defun org-fast-tag-insert (kwd tags face &optional end)
  14050. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14051. (insert (format "%-12s" (concat kwd ":"))
  14052. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14053. (or end "")))
  14054. (defun org-fast-tag-show-exit (flag)
  14055. (save-excursion
  14056. (goto-line 3)
  14057. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14058. (replace-match ""))
  14059. (when flag
  14060. (end-of-line 1)
  14061. (move-to-column (- (window-width) 19) t)
  14062. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14063. (defun org-set-current-tags-overlay (current prefix)
  14064. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14065. (if (featurep 'xemacs)
  14066. (org-overlay-display org-tags-overlay (concat prefix s)
  14067. 'secondary-selection)
  14068. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14069. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14070. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14071. "Fast tag selection with single keys.
  14072. CURRENT is the current list of tags in the headline, INHERITED is the
  14073. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14074. possibly with grouping information. TODO-TABLE is a similar table with
  14075. TODO keywords, should these have keys assigned to them.
  14076. If the keys are nil, a-z are automatically assigned.
  14077. Returns the new tags string, or nil to not change the current settings."
  14078. (let* ((fulltable (append table todo-table))
  14079. (maxlen (apply 'max (mapcar
  14080. (lambda (x)
  14081. (if (stringp (car x)) (string-width (car x)) 0))
  14082. fulltable)))
  14083. (buf (current-buffer))
  14084. (expert (eq org-fast-tag-selection-single-key 'expert))
  14085. (buffer-tags nil)
  14086. (fwidth (+ maxlen 3 1 3))
  14087. (ncol (/ (- (window-width) 4) fwidth))
  14088. (i-face 'org-done)
  14089. (c-face 'org-todo)
  14090. tg cnt e c char c1 c2 ntable tbl rtn
  14091. ov-start ov-end ov-prefix
  14092. (exit-after-next org-fast-tag-selection-single-key)
  14093. (done-keywords org-done-keywords)
  14094. groups ingroup)
  14095. (save-excursion
  14096. (beginning-of-line 1)
  14097. (if (looking-at
  14098. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14099. (setq ov-start (match-beginning 1)
  14100. ov-end (match-end 1)
  14101. ov-prefix "")
  14102. (setq ov-start (1- (point-at-eol))
  14103. ov-end (1+ ov-start))
  14104. (skip-chars-forward "^\n\r")
  14105. (setq ov-prefix
  14106. (concat
  14107. (buffer-substring (1- (point)) (point))
  14108. (if (> (current-column) org-tags-column)
  14109. " "
  14110. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14111. (org-move-overlay org-tags-overlay ov-start ov-end)
  14112. (save-window-excursion
  14113. (if expert
  14114. (set-buffer (get-buffer-create " *Org tags*"))
  14115. (delete-other-windows)
  14116. (split-window-vertically)
  14117. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14118. (erase-buffer)
  14119. (org-set-local 'org-done-keywords done-keywords)
  14120. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14121. (org-fast-tag-insert "Current" current c-face "\n\n")
  14122. (org-fast-tag-show-exit exit-after-next)
  14123. (org-set-current-tags-overlay current ov-prefix)
  14124. (setq tbl fulltable char ?a cnt 0)
  14125. (while (setq e (pop tbl))
  14126. (cond
  14127. ((equal e '(:startgroup))
  14128. (push '() groups) (setq ingroup t)
  14129. (when (not (= cnt 0))
  14130. (setq cnt 0)
  14131. (insert "\n"))
  14132. (insert "{ "))
  14133. ((equal e '(:endgroup))
  14134. (setq ingroup nil cnt 0)
  14135. (insert "}\n"))
  14136. (t
  14137. (setq tg (car e) c2 nil)
  14138. (if (cdr e)
  14139. (setq c (cdr e))
  14140. ;; automatically assign a character.
  14141. (setq c1 (string-to-char
  14142. (downcase (substring
  14143. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14144. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14145. (while (or (rassoc char ntable) (rassoc char table))
  14146. (setq char (1+ char)))
  14147. (setq c2 c1))
  14148. (setq c (or c2 char)))
  14149. (if ingroup (push tg (car groups)))
  14150. (setq tg (org-add-props tg nil 'face
  14151. (cond
  14152. ((not (assoc tg table))
  14153. (org-get-todo-face tg))
  14154. ((member tg current) c-face)
  14155. ((member tg inherited) i-face)
  14156. (t nil))))
  14157. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14158. (insert "[" c "] " tg (make-string
  14159. (- fwidth 4 (length tg)) ?\ ))
  14160. (push (cons tg c) ntable)
  14161. (when (= (setq cnt (1+ cnt)) ncol)
  14162. (insert "\n")
  14163. (if ingroup (insert " "))
  14164. (setq cnt 0)))))
  14165. (setq ntable (nreverse ntable))
  14166. (insert "\n")
  14167. (goto-char (point-min))
  14168. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14169. (fit-window-to-buffer))
  14170. (setq rtn
  14171. (catch 'exit
  14172. (while t
  14173. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14174. (if groups " [!] no groups" " [!]groups")
  14175. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14176. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14177. (cond
  14178. ((= c ?\r) (throw 'exit t))
  14179. ((= c ?!)
  14180. (setq groups (not groups))
  14181. (goto-char (point-min))
  14182. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14183. ((= c ?\C-c)
  14184. (if (not expert)
  14185. (org-fast-tag-show-exit
  14186. (setq exit-after-next (not exit-after-next)))
  14187. (setq expert nil)
  14188. (delete-other-windows)
  14189. (split-window-vertically)
  14190. (org-switch-to-buffer-other-window " *Org tags*")
  14191. (and (fboundp 'fit-window-to-buffer)
  14192. (fit-window-to-buffer))))
  14193. ((or (= c ?\C-g)
  14194. (and (= c ?q) (not (rassoc c ntable))))
  14195. (org-detach-overlay org-tags-overlay)
  14196. (setq quit-flag t))
  14197. ((= c ?\ )
  14198. (setq current nil)
  14199. (if exit-after-next (setq exit-after-next 'now)))
  14200. ((= c ?\t)
  14201. (condition-case nil
  14202. (setq tg (completing-read
  14203. "Tag: "
  14204. (or buffer-tags
  14205. (with-current-buffer buf
  14206. (org-get-buffer-tags)))))
  14207. (quit (setq tg "")))
  14208. (when (string-match "\\S-" tg)
  14209. (add-to-list 'buffer-tags (list tg))
  14210. (if (member tg current)
  14211. (setq current (delete tg current))
  14212. (push tg current)))
  14213. (if exit-after-next (setq exit-after-next 'now)))
  14214. ((setq e (rassoc c todo-table) tg (car e))
  14215. (with-current-buffer buf
  14216. (save-excursion (org-todo tg)))
  14217. (if exit-after-next (setq exit-after-next 'now)))
  14218. ((setq e (rassoc c ntable) tg (car e))
  14219. (if (member tg current)
  14220. (setq current (delete tg current))
  14221. (loop for g in groups do
  14222. (if (member tg g)
  14223. (mapc (lambda (x)
  14224. (setq current (delete x current)))
  14225. g)))
  14226. (push tg current))
  14227. (if exit-after-next (setq exit-after-next 'now))))
  14228. ;; Create a sorted list
  14229. (setq current
  14230. (sort current
  14231. (lambda (a b)
  14232. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14233. (if (eq exit-after-next 'now) (throw 'exit t))
  14234. (goto-char (point-min))
  14235. (beginning-of-line 2)
  14236. (delete-region (point) (point-at-eol))
  14237. (org-fast-tag-insert "Current" current c-face)
  14238. (org-set-current-tags-overlay current ov-prefix)
  14239. (while (re-search-forward
  14240. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14241. (setq tg (match-string 1))
  14242. (add-text-properties
  14243. (match-beginning 1) (match-end 1)
  14244. (list 'face
  14245. (cond
  14246. ((member tg current) c-face)
  14247. ((member tg inherited) i-face)
  14248. (t (get-text-property (match-beginning 1) 'face))))))
  14249. (goto-char (point-min)))))
  14250. (org-detach-overlay org-tags-overlay)
  14251. (if rtn
  14252. (mapconcat 'identity current ":")
  14253. nil))))
  14254. (defun org-get-tags-string ()
  14255. "Get the TAGS string in the current headline."
  14256. (unless (org-on-heading-p t)
  14257. (error "Not on a heading"))
  14258. (save-excursion
  14259. (beginning-of-line 1)
  14260. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14261. (org-match-string-no-properties 1)
  14262. "")))
  14263. (defun org-get-tags ()
  14264. "Get the list of tags specified in the current headline."
  14265. (org-split-string (org-get-tags-string) ":"))
  14266. (defun org-get-buffer-tags ()
  14267. "Get a table of all tags used in the buffer, for completion."
  14268. (let (tags)
  14269. (save-excursion
  14270. (goto-char (point-min))
  14271. (while (re-search-forward
  14272. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14273. (when (equal (char-after (point-at-bol 0)) ?*)
  14274. (mapc (lambda (x) (add-to-list 'tags x))
  14275. (org-split-string (org-match-string-no-properties 1) ":")))))
  14276. (mapcar 'list tags)))
  14277. ;;;; Properties
  14278. ;;; Setting and retrieving properties
  14279. (defconst org-special-properties
  14280. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14281. "TIMESTAMP" "TIMESTAMP_IA")
  14282. "The special properties valid in Org-mode.
  14283. These are properties that are not defined in the property drawer,
  14284. but in some other way.")
  14285. (defconst org-default-properties
  14286. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14287. "LOCATION" "LOGGING" "COLUMNS")
  14288. "Some properties that are used by Org-mode for various purposes.
  14289. Being in this list makes sure that they are offered for completion.")
  14290. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14291. "Regular expression matching the first line of a property drawer.")
  14292. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14293. "Regular expression matching the first line of a property drawer.")
  14294. (defun org-property-action ()
  14295. "Do an action on properties."
  14296. (interactive)
  14297. (let (c)
  14298. (org-at-property-p)
  14299. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14300. (setq c (read-char-exclusive))
  14301. (cond
  14302. ((equal c ?s)
  14303. (call-interactively 'org-set-property))
  14304. ((equal c ?d)
  14305. (call-interactively 'org-delete-property))
  14306. ((equal c ?D)
  14307. (call-interactively 'org-delete-property-globally))
  14308. ((equal c ?c)
  14309. (call-interactively 'org-compute-property-at-point))
  14310. (t (error "No such property action %c" c)))))
  14311. (defun org-at-property-p ()
  14312. "Is the cursor in a property line?"
  14313. ;; FIXME: Does not check if we are actually in the drawer.
  14314. ;; FIXME: also returns true on any drawers.....
  14315. ;; This is used by C-c C-c for property action.
  14316. (save-excursion
  14317. (beginning-of-line 1)
  14318. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14319. (defmacro org-with-point-at (pom &rest body)
  14320. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14321. (declare (indent 1) (debug t))
  14322. `(save-excursion
  14323. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14324. (save-excursion
  14325. (goto-char (or pom (point)))
  14326. ,@body)))
  14327. (defun org-get-property-block (&optional beg end force)
  14328. "Return the (beg . end) range of the body of the property drawer.
  14329. BEG and END can be beginning and end of subtree, if not given
  14330. they will be found.
  14331. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14332. (catch 'exit
  14333. (save-excursion
  14334. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14335. (end (or end (progn (outline-next-heading) (point)))))
  14336. (goto-char beg)
  14337. (if (re-search-forward org-property-start-re end t)
  14338. (setq beg (1+ (match-end 0)))
  14339. (if force
  14340. (save-excursion
  14341. (org-insert-property-drawer)
  14342. (setq end (progn (outline-next-heading) (point))))
  14343. (throw 'exit nil))
  14344. (goto-char beg)
  14345. (if (re-search-forward org-property-start-re end t)
  14346. (setq beg (1+ (match-end 0)))))
  14347. (if (re-search-forward org-property-end-re end t)
  14348. (setq end (match-beginning 0))
  14349. (or force (throw 'exit nil))
  14350. (goto-char beg)
  14351. (setq end beg)
  14352. (org-indent-line-function)
  14353. (insert ":END:\n"))
  14354. (cons beg end)))))
  14355. (defun org-entry-properties (&optional pom which)
  14356. "Get all properties of the entry at point-or-marker POM.
  14357. This includes the TODO keyword, the tags, time strings for deadline,
  14358. scheduled, and clocking, and any additional properties defined in the
  14359. entry. The return value is an alist, keys may occur multiple times
  14360. if the property key was used several times.
  14361. POM may also be nil, in which case the current entry is used.
  14362. If WHICH is nil or `all', get all properties. If WHICH is
  14363. `special' or `standard', only get that subclass."
  14364. (setq which (or which 'all))
  14365. (org-with-point-at pom
  14366. (let ((clockstr (substring org-clock-string 0 -1))
  14367. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14368. beg end range props sum-props key value string)
  14369. (save-excursion
  14370. (when (condition-case nil (org-back-to-heading t) (error nil))
  14371. (setq beg (point))
  14372. (setq sum-props (get-text-property (point) 'org-summaries))
  14373. (outline-next-heading)
  14374. (setq end (point))
  14375. (when (memq which '(all special))
  14376. ;; Get the special properties, like TODO and tags
  14377. (goto-char beg)
  14378. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14379. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14380. (when (looking-at org-priority-regexp)
  14381. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14382. (when (and (setq value (org-get-tags-string))
  14383. (string-match "\\S-" value))
  14384. (push (cons "TAGS" value) props))
  14385. (when (setq value (org-get-tags-at))
  14386. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14387. props))
  14388. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14389. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14390. string (if (equal key clockstr)
  14391. (org-no-properties
  14392. (org-trim
  14393. (buffer-substring
  14394. (match-beginning 3) (goto-char (point-at-eol)))))
  14395. (substring (org-match-string-no-properties 3) 1 -1)))
  14396. (unless key
  14397. (if (= (char-after (match-beginning 3)) ?\[)
  14398. (setq key "TIMESTAMP_IA")
  14399. (setq key "TIMESTAMP")))
  14400. (when (or (equal key clockstr) (not (assoc key props)))
  14401. (push (cons key string) props)))
  14402. )
  14403. (when (memq which '(all standard))
  14404. ;; Get the standard properties, like :PORP: ...
  14405. (setq range (org-get-property-block beg end))
  14406. (when range
  14407. (goto-char (car range))
  14408. (while (re-search-forward
  14409. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14410. (cdr range) t)
  14411. (setq key (org-match-string-no-properties 1)
  14412. value (org-trim (or (org-match-string-no-properties 2) "")))
  14413. (unless (member key excluded)
  14414. (push (cons key (or value "")) props)))))
  14415. (append sum-props (nreverse props)))))))
  14416. (defun org-entry-get (pom property &optional inherit)
  14417. "Get value of PROPERTY for entry at point-or-marker POM.
  14418. If INHERIT is non-nil and the entry does not have the property,
  14419. then also check higher levels of the hierarchy.
  14420. If the property is present but empty, the return value is the empty string.
  14421. If the property is not present at all, nil is returned."
  14422. (org-with-point-at pom
  14423. (if inherit
  14424. (org-entry-get-with-inheritance property)
  14425. (if (member property org-special-properties)
  14426. ;; We need a special property. Use brute force, get all properties.
  14427. (cdr (assoc property (org-entry-properties nil 'special)))
  14428. (let ((range (org-get-property-block)))
  14429. (if (and range
  14430. (goto-char (car range))
  14431. (re-search-forward
  14432. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14433. (cdr range) t))
  14434. ;; Found the property, return it.
  14435. (if (match-end 1)
  14436. (org-match-string-no-properties 1)
  14437. "")))))))
  14438. (defun org-entry-delete (pom property)
  14439. "Delete the property PROPERTY from entry at point-or-marker POM."
  14440. (org-with-point-at pom
  14441. (if (member property org-special-properties)
  14442. nil ; cannot delete these properties.
  14443. (let ((range (org-get-property-block)))
  14444. (if (and range
  14445. (goto-char (car range))
  14446. (re-search-forward
  14447. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14448. (cdr range) t))
  14449. (progn
  14450. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14451. t)
  14452. nil)))))
  14453. ;; Multi-values properties are properties that contain multiple values
  14454. ;; These values are assumed to be single words, separated by whitespace.
  14455. (defun org-entry-add-to-multivalued-property (pom property value)
  14456. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14457. (let* ((old (org-entry-get pom property))
  14458. (values (and old (org-split-string old "[ \t]"))))
  14459. (unless (member value values)
  14460. (setq values (cons value values))
  14461. (org-entry-put pom property
  14462. (mapconcat 'identity values " ")))))
  14463. (defun org-entry-remove-from-multivalued-property (pom property value)
  14464. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14465. (let* ((old (org-entry-get pom property))
  14466. (values (and old (org-split-string old "[ \t]"))))
  14467. (when (member value values)
  14468. (setq values (delete value values))
  14469. (org-entry-put pom property
  14470. (mapconcat 'identity values " ")))))
  14471. (defun org-entry-member-in-multivalued-property (pom property value)
  14472. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14473. (let* ((old (org-entry-get pom property))
  14474. (values (and old (org-split-string old "[ \t]"))))
  14475. (member value values)))
  14476. (defvar org-entry-property-inherited-from (make-marker))
  14477. (defun org-entry-get-with-inheritance (property)
  14478. "Get entry property, and search higher levels if not present."
  14479. (let (tmp)
  14480. (save-excursion
  14481. (save-restriction
  14482. (widen)
  14483. (catch 'ex
  14484. (while t
  14485. (when (setq tmp (org-entry-get nil property))
  14486. (org-back-to-heading t)
  14487. (move-marker org-entry-property-inherited-from (point))
  14488. (throw 'ex tmp))
  14489. (or (org-up-heading-safe) (throw 'ex nil)))))
  14490. (or tmp (cdr (assoc property org-local-properties))
  14491. (cdr (assoc property org-global-properties))))))
  14492. (defun org-entry-put (pom property value)
  14493. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14494. (org-with-point-at pom
  14495. (org-back-to-heading t)
  14496. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14497. range)
  14498. (cond
  14499. ((equal property "TODO")
  14500. (when (and (stringp value) (string-match "\\S-" value)
  14501. (not (member value org-todo-keywords-1)))
  14502. (error "\"%s\" is not a valid TODO state" value))
  14503. (if (or (not value)
  14504. (not (string-match "\\S-" value)))
  14505. (setq value 'none))
  14506. (org-todo value)
  14507. (org-set-tags nil 'align))
  14508. ((equal property "PRIORITY")
  14509. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14510. (string-to-char value) ?\ ))
  14511. (org-set-tags nil 'align))
  14512. ((equal property "SCHEDULED")
  14513. (if (re-search-forward org-scheduled-time-regexp end t)
  14514. (cond
  14515. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14516. ((eq value 'later) (org-timestamp-change 1 'day))
  14517. (t (call-interactively 'org-schedule)))
  14518. (call-interactively 'org-schedule)))
  14519. ((equal property "DEADLINE")
  14520. (if (re-search-forward org-deadline-time-regexp end t)
  14521. (cond
  14522. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14523. ((eq value 'later) (org-timestamp-change 1 'day))
  14524. (t (call-interactively 'org-deadline)))
  14525. (call-interactively 'org-deadline)))
  14526. ((member property org-special-properties)
  14527. (error "The %s property can not yet be set with `org-entry-put'"
  14528. property))
  14529. (t ; a non-special property
  14530. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14531. (setq range (org-get-property-block beg end 'force))
  14532. (goto-char (car range))
  14533. (if (re-search-forward
  14534. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14535. (progn
  14536. (delete-region (match-beginning 1) (match-end 1))
  14537. (goto-char (match-beginning 1)))
  14538. (goto-char (cdr range))
  14539. (insert "\n")
  14540. (backward-char 1)
  14541. (org-indent-line-function)
  14542. (insert ":" property ":"))
  14543. (and value (insert " " value))
  14544. (org-indent-line-function)))))))
  14545. (defun org-buffer-property-keys (&optional include-specials include-defaults)
  14546. "Get all property keys in the current buffer.
  14547. With INCLUDE-SPECIALS, also list the special properties that relect things
  14548. like tags and TODO state.
  14549. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14550. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING."
  14551. (let (rtn range)
  14552. (save-excursion
  14553. (save-restriction
  14554. (widen)
  14555. (goto-char (point-min))
  14556. (while (re-search-forward org-property-start-re nil t)
  14557. (setq range (org-get-property-block))
  14558. (goto-char (car range))
  14559. (while (re-search-forward
  14560. (org-re "^[ \t]*:\\([[:alnum:]_-]+\\):")
  14561. (cdr range) t)
  14562. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14563. (outline-next-heading))))
  14564. (when include-specials
  14565. (setq rtn (append org-special-properties rtn)))
  14566. (when include-defaults
  14567. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14568. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14569. (defun org-property-values (key)
  14570. "Return a list of all values of property KEY."
  14571. (save-excursion
  14572. (save-restriction
  14573. (widen)
  14574. (goto-char (point-min))
  14575. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14576. values)
  14577. (while (re-search-forward re nil t)
  14578. (add-to-list 'values (org-trim (match-string 1))))
  14579. (delete "" values)))))
  14580. (defun org-insert-property-drawer ()
  14581. "Insert a property drawer into the current entry."
  14582. (interactive)
  14583. (org-back-to-heading t)
  14584. (looking-at outline-regexp)
  14585. (let ((indent (- (match-end 0)(match-beginning 0)))
  14586. (beg (point))
  14587. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14588. end hiddenp)
  14589. (outline-next-heading)
  14590. (setq end (point))
  14591. (goto-char beg)
  14592. (while (re-search-forward re end t))
  14593. (setq hiddenp (org-invisible-p))
  14594. (end-of-line 1)
  14595. (and (equal (char-after) ?\n) (forward-char 1))
  14596. (org-skip-over-state-notes)
  14597. (skip-chars-backward " \t\n\r")
  14598. (if (eq (char-before) ?*) (forward-char 1))
  14599. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14600. (beginning-of-line 0)
  14601. (indent-to-column indent)
  14602. (beginning-of-line 2)
  14603. (indent-to-column indent)
  14604. (beginning-of-line 0)
  14605. (if hiddenp
  14606. (save-excursion
  14607. (org-back-to-heading t)
  14608. (hide-entry))
  14609. (org-flag-drawer t))))
  14610. (defun org-set-property (property value)
  14611. "In the current entry, set PROPERTY to VALUE.
  14612. When called interactively, this will prompt for a property name, offering
  14613. completion on existing and default properties. And then it will prompt
  14614. for a value, offering competion either on allowed values (via an inherited
  14615. xxx_ALL property) or on existing values in other instances of this property
  14616. in the current file."
  14617. (interactive
  14618. (let* ((prop (completing-read
  14619. "Property: " (mapcar 'list (org-buffer-property-keys nil t))))
  14620. (cur (org-entry-get nil prop))
  14621. (allowed (org-property-get-allowed-values nil prop 'table))
  14622. (existing (mapcar 'list (org-property-values prop)))
  14623. (val (if allowed
  14624. (completing-read "Value: " allowed nil 'req-match)
  14625. (completing-read
  14626. (concat "Value" (if (and cur (string-match "\\S-" cur))
  14627. (concat "[" cur "]") "")
  14628. ": ")
  14629. existing nil nil "" nil cur))))
  14630. (list prop (if (equal val "") cur val))))
  14631. (unless (equal (org-entry-get nil property) value)
  14632. (org-entry-put nil property value)))
  14633. (defun org-delete-property (property)
  14634. "In the current entry, delete PROPERTY."
  14635. (interactive
  14636. (let* ((prop (completing-read
  14637. "Property: " (org-entry-properties nil 'standard))))
  14638. (list prop)))
  14639. (message "Property %s %s" property
  14640. (if (org-entry-delete nil property)
  14641. "deleted"
  14642. "was not present in the entry")))
  14643. (defun org-delete-property-globally (property)
  14644. "Remove PROPERTY globally, from all entries."
  14645. (interactive
  14646. (let* ((prop (completing-read
  14647. "Globally remove property: "
  14648. (mapcar 'list (org-buffer-property-keys)))))
  14649. (list prop)))
  14650. (save-excursion
  14651. (save-restriction
  14652. (widen)
  14653. (goto-char (point-min))
  14654. (let ((cnt 0))
  14655. (while (re-search-forward
  14656. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  14657. nil t)
  14658. (setq cnt (1+ cnt))
  14659. (replace-match ""))
  14660. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14661. (defvar org-columns-current-fmt-compiled) ; defined below
  14662. (defun org-compute-property-at-point ()
  14663. "Compute the property at point.
  14664. This looks for an enclosing column format, extracts the operator and
  14665. then applies it to the proerty in the column format's scope."
  14666. (interactive)
  14667. (unless (org-at-property-p)
  14668. (error "Not at a property"))
  14669. (let ((prop (org-match-string-no-properties 2)))
  14670. (org-columns-get-format-and-top-level)
  14671. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14672. (error "No operator defined for property %s" prop))
  14673. (org-columns-compute prop)))
  14674. (defun org-property-get-allowed-values (pom property &optional table)
  14675. "Get allowed values for the property PROPERTY.
  14676. When TABLE is non-nil, return an alist that can directly be used for
  14677. completion."
  14678. (let (vals)
  14679. (cond
  14680. ((equal property "TODO")
  14681. (setq vals (org-with-point-at pom
  14682. (append org-todo-keywords-1 '("")))))
  14683. ((equal property "PRIORITY")
  14684. (let ((n org-lowest-priority))
  14685. (while (>= n org-highest-priority)
  14686. (push (char-to-string n) vals)
  14687. (setq n (1- n)))))
  14688. ((member property org-special-properties))
  14689. (t
  14690. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14691. (when (and vals (string-match "\\S-" vals))
  14692. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14693. (setq vals (mapcar (lambda (x)
  14694. (cond ((stringp x) x)
  14695. ((numberp x) (number-to-string x))
  14696. ((symbolp x) (symbol-name x))
  14697. (t "???")))
  14698. vals)))))
  14699. (if table (mapcar 'list vals) vals)))
  14700. (defun org-property-previous-allowed-value (&optional previous)
  14701. "Switch to the next allowed value for this property."
  14702. (interactive)
  14703. (org-property-next-allowed-value t))
  14704. (defun org-property-next-allowed-value (&optional previous)
  14705. "Switch to the next allowed value for this property."
  14706. (interactive)
  14707. (unless (org-at-property-p)
  14708. (error "Not at a property"))
  14709. (let* ((key (match-string 2))
  14710. (value (match-string 3))
  14711. (allowed (or (org-property-get-allowed-values (point) key)
  14712. (and (member value '("[ ]" "[-]" "[X]"))
  14713. '("[ ]" "[X]"))))
  14714. nval)
  14715. (unless allowed
  14716. (error "Allowed values for this property have not been defined"))
  14717. (if previous (setq allowed (reverse allowed)))
  14718. (if (member value allowed)
  14719. (setq nval (car (cdr (member value allowed)))))
  14720. (setq nval (or nval (car allowed)))
  14721. (if (equal nval value)
  14722. (error "Only one allowed value for this property"))
  14723. (org-at-property-p)
  14724. (replace-match (concat " :" key ": " nval) t t)
  14725. (org-indent-line-function)
  14726. (beginning-of-line 1)
  14727. (skip-chars-forward " \t")))
  14728. (defun org-find-entry-with-id (ident)
  14729. "Locate the entry that contains the ID property with exact value IDENT.
  14730. IDENT can be a string, a symbol or a number, this function will search for
  14731. the string representation of it.
  14732. Return the position where this entry starts, or nil if there is no such entry."
  14733. (let ((id (cond
  14734. ((stringp ident) ident)
  14735. ((symbol-name ident) (symbol-name ident))
  14736. ((numberp ident) (number-to-string ident))
  14737. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14738. (case-fold-search nil))
  14739. (save-excursion
  14740. (save-restriction
  14741. (goto-char (point-min))
  14742. (when (re-search-forward
  14743. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14744. nil t)
  14745. (org-back-to-heading)
  14746. (point))))))
  14747. ;;; Column View
  14748. (defvar org-columns-overlays nil
  14749. "Holds the list of current column overlays.")
  14750. (defvar org-columns-current-fmt nil
  14751. "Local variable, holds the currently active column format.")
  14752. (defvar org-columns-current-fmt-compiled nil
  14753. "Local variable, holds the currently active column format.
  14754. This is the compiled version of the format.")
  14755. (defvar org-columns-current-widths nil
  14756. "Loval variable, holds the currently widths of fields.")
  14757. (defvar org-columns-current-maxwidths nil
  14758. "Loval variable, holds the currently active maximum column widths.")
  14759. (defvar org-columns-begin-marker (make-marker)
  14760. "Points to the position where last a column creation command was called.")
  14761. (defvar org-columns-top-level-marker (make-marker)
  14762. "Points to the position where current columns region starts.")
  14763. (defvar org-columns-map (make-sparse-keymap)
  14764. "The keymap valid in column display.")
  14765. (defun org-columns-content ()
  14766. "Switch to contents view while in columns view."
  14767. (interactive)
  14768. (org-overview)
  14769. (org-content))
  14770. (org-defkey org-columns-map "c" 'org-columns-content)
  14771. (org-defkey org-columns-map "o" 'org-overview)
  14772. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  14773. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  14774. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  14775. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  14776. (org-defkey org-columns-map "v" 'org-columns-show-value)
  14777. (org-defkey org-columns-map "q" 'org-columns-quit)
  14778. (org-defkey org-columns-map "r" 'org-columns-redo)
  14779. (org-defkey org-columns-map [left] 'backward-char)
  14780. (org-defkey org-columns-map "\M-b" 'backward-char)
  14781. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  14782. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  14783. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  14784. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  14785. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  14786. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  14787. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  14788. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  14789. (org-defkey org-columns-map "<" 'org-columns-narrow)
  14790. (org-defkey org-columns-map ">" 'org-columns-widen)
  14791. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  14792. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  14793. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  14794. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  14795. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  14796. '("Column"
  14797. ["Edit property" org-columns-edit-value t]
  14798. ["Next allowed value" org-columns-next-allowed-value t]
  14799. ["Previous allowed value" org-columns-previous-allowed-value t]
  14800. ["Show full value" org-columns-show-value t]
  14801. ["Edit allowed values" org-columns-edit-allowed t]
  14802. "--"
  14803. ["Edit column attributes" org-columns-edit-attributes t]
  14804. ["Increase column width" org-columns-widen t]
  14805. ["Decrease column width" org-columns-narrow t]
  14806. "--"
  14807. ["Move column right" org-columns-move-right t]
  14808. ["Move column left" org-columns-move-left t]
  14809. ["Add column" org-columns-new t]
  14810. ["Delete column" org-columns-delete t]
  14811. "--"
  14812. ["CONTENTS" org-columns-content t]
  14813. ["OVERVIEW" org-overview t]
  14814. ["Refresh columns display" org-columns-redo t]
  14815. "--"
  14816. ["Open link" org-columns-open-link t]
  14817. "--"
  14818. ["Quit" org-columns-quit t]))
  14819. (defun org-columns-new-overlay (beg end &optional string face)
  14820. "Create a new column overlay and add it to the list."
  14821. (let ((ov (org-make-overlay beg end)))
  14822. (org-overlay-put ov 'face (or face 'secondary-selection))
  14823. (org-overlay-display ov string face)
  14824. (push ov org-columns-overlays)
  14825. ov))
  14826. (defun org-columns-display-here (&optional props)
  14827. "Overlay the current line with column display."
  14828. (interactive)
  14829. (let* ((fmt org-columns-current-fmt-compiled)
  14830. (beg (point-at-bol))
  14831. (level-face (save-excursion
  14832. (beginning-of-line 1)
  14833. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  14834. (org-get-level-face 2))))
  14835. (color (list :foreground
  14836. (face-attribute (or level-face 'default) :foreground)))
  14837. props pom property ass width f string ov column val modval)
  14838. ;; Check if the entry is in another buffer.
  14839. (unless props
  14840. (if (eq major-mode 'org-agenda-mode)
  14841. (setq pom (or (get-text-property (point) 'org-hd-marker)
  14842. (get-text-property (point) 'org-marker))
  14843. props (if pom (org-entry-properties pom) nil))
  14844. (setq props (org-entry-properties nil))))
  14845. ;; Walk the format
  14846. (while (setq column (pop fmt))
  14847. (setq property (car column)
  14848. ass (if (equal property "ITEM")
  14849. (cons "ITEM"
  14850. (save-match-data
  14851. (org-no-properties
  14852. (org-remove-tabs
  14853. (buffer-substring-no-properties
  14854. (point-at-bol) (point-at-eol))))))
  14855. (assoc property props))
  14856. width (or (cdr (assoc property org-columns-current-maxwidths))
  14857. (nth 2 column)
  14858. (length property))
  14859. f (format "%%-%d.%ds | " width width)
  14860. val (or (cdr ass) "")
  14861. modval (if (equal property "ITEM")
  14862. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  14863. string (format f (or modval val)))
  14864. ;; Create the overlay
  14865. (org-unmodified
  14866. (setq ov (org-columns-new-overlay
  14867. beg (setq beg (1+ beg)) string
  14868. (list color 'org-column)))
  14869. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  14870. (org-overlay-put ov 'keymap org-columns-map)
  14871. (org-overlay-put ov 'org-columns-key property)
  14872. (org-overlay-put ov 'org-columns-value (cdr ass))
  14873. (org-overlay-put ov 'org-columns-value-modified modval)
  14874. (org-overlay-put ov 'org-columns-pom pom)
  14875. (org-overlay-put ov 'org-columns-format f))
  14876. (if (or (not (char-after beg))
  14877. (equal (char-after beg) ?\n))
  14878. (let ((inhibit-read-only t))
  14879. (save-excursion
  14880. (goto-char beg)
  14881. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  14882. ;; Make the rest of the line disappear.
  14883. (org-unmodified
  14884. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  14885. (org-overlay-put ov 'invisible t)
  14886. (org-overlay-put ov 'keymap org-columns-map)
  14887. (org-overlay-put ov 'intangible t)
  14888. (push ov org-columns-overlays)
  14889. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  14890. (org-overlay-put ov 'keymap org-columns-map)
  14891. (push ov org-columns-overlays)
  14892. (let ((inhibit-read-only t))
  14893. (put-text-property (max (point-min) (1- (point-at-bol)))
  14894. (min (point-max) (1+ (point-at-eol)))
  14895. 'read-only "Type `e' to edit property")))))
  14896. (defvar org-previous-header-line-format nil
  14897. "The header line format before column view was turned on.")
  14898. (defvar org-columns-inhibit-recalculation nil
  14899. "Inhibit recomputing of columns on column view startup.")
  14900. (defvar header-line-format)
  14901. (defun org-columns-display-here-title ()
  14902. "Overlay the newline before the current line with the table title."
  14903. (interactive)
  14904. (let ((fmt org-columns-current-fmt-compiled)
  14905. string (title "")
  14906. property width f column str widths)
  14907. (while (setq column (pop fmt))
  14908. (setq property (car column)
  14909. str (or (nth 1 column) property)
  14910. width (or (cdr (assoc property org-columns-current-maxwidths))
  14911. (nth 2 column)
  14912. (length str))
  14913. widths (push width widths)
  14914. f (format "%%-%d.%ds | " width width)
  14915. string (format f str)
  14916. title (concat title string)))
  14917. (setq title (concat
  14918. (org-add-props " " nil 'display '(space :align-to 0))
  14919. (org-add-props title nil 'face '(:weight bold :underline t))))
  14920. (org-set-local 'org-previous-header-line-format header-line-format)
  14921. (org-set-local 'org-columns-current-widths (nreverse widths))
  14922. (setq header-line-format title)))
  14923. (defun org-columns-remove-overlays ()
  14924. "Remove all currently active column overlays."
  14925. (interactive)
  14926. (when (marker-buffer org-columns-begin-marker)
  14927. (with-current-buffer (marker-buffer org-columns-begin-marker)
  14928. (when (local-variable-p 'org-previous-header-line-format)
  14929. (setq header-line-format org-previous-header-line-format)
  14930. (kill-local-variable 'org-previous-header-line-format))
  14931. (move-marker org-columns-begin-marker nil)
  14932. (move-marker org-columns-top-level-marker nil)
  14933. (org-unmodified
  14934. (mapc 'org-delete-overlay org-columns-overlays)
  14935. (setq org-columns-overlays nil)
  14936. (let ((inhibit-read-only t))
  14937. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  14938. (defun org-columns-cleanup-item (item fmt)
  14939. "Remove from ITEM what is a column in the format FMT."
  14940. (if (not org-complex-heading-regexp)
  14941. item
  14942. (when (string-match org-complex-heading-regexp item)
  14943. (concat
  14944. (org-add-props (concat (match-string 1 item) " ") nil
  14945. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  14946. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  14947. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  14948. " " (match-string 4 item)
  14949. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  14950. (defun org-columns-show-value ()
  14951. "Show the full value of the property."
  14952. (interactive)
  14953. (let ((value (get-char-property (point) 'org-columns-value)))
  14954. (message "Value is: %s" (or value ""))))
  14955. (defun org-columns-quit ()
  14956. "Remove the column overlays and in this way exit column editing."
  14957. (interactive)
  14958. (org-unmodified
  14959. (org-columns-remove-overlays)
  14960. (let ((inhibit-read-only t))
  14961. (remove-text-properties (point-min) (point-max) '(read-only t))))
  14962. (when (eq major-mode 'org-agenda-mode)
  14963. (message
  14964. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  14965. (defun org-columns-check-computed ()
  14966. "Check if this column value is computed.
  14967. If yes, throw an error indicating that changing it does not make sense."
  14968. (let ((val (get-char-property (point) 'org-columns-value)))
  14969. (when (and (stringp val)
  14970. (get-char-property 0 'org-computed val))
  14971. (error "This value is computed from the entry's children"))))
  14972. (defun org-columns-todo (&optional arg)
  14973. "Change the TODO state during column view."
  14974. (interactive "P")
  14975. (org-columns-edit-value "TODO"))
  14976. (defun org-columns-set-tags-or-toggle (&optional arg)
  14977. "Toggle checkbox at point, or set tags for current headline."
  14978. (interactive "P")
  14979. (if (string-match "\\`\\[[ xX-]\\]\\'"
  14980. (get-char-property (point) 'org-columns-value))
  14981. (org-columns-next-allowed-value)
  14982. (org-columns-edit-value "TAGS")))
  14983. (defun org-columns-edit-value (&optional key)
  14984. "Edit the value of the property at point in column view.
  14985. Where possible, use the standard interface for changing this line."
  14986. (interactive)
  14987. (org-columns-check-computed)
  14988. (let* ((external-key key)
  14989. (col (current-column))
  14990. (key (or key (get-char-property (point) 'org-columns-key)))
  14991. (value (get-char-property (point) 'org-columns-value))
  14992. (bol (point-at-bol)) (eol (point-at-eol))
  14993. (pom (or (get-text-property bol 'org-hd-marker)
  14994. (point))) ; keep despite of compiler waring
  14995. (line-overlays
  14996. (delq nil (mapcar (lambda (x)
  14997. (and (eq (overlay-buffer x) (current-buffer))
  14998. (>= (overlay-start x) bol)
  14999. (<= (overlay-start x) eol)
  15000. x))
  15001. org-columns-overlays)))
  15002. nval eval allowed)
  15003. (cond
  15004. ((equal key "ITEM")
  15005. (setq eval '(org-with-point-at pom
  15006. (org-edit-headline))))
  15007. ((equal key "TODO")
  15008. (setq eval '(org-with-point-at pom
  15009. (let ((current-prefix-arg
  15010. (if external-key current-prefix-arg '(4))))
  15011. (call-interactively 'org-todo)))))
  15012. ((equal key "PRIORITY")
  15013. (setq eval '(org-with-point-at pom
  15014. (call-interactively 'org-priority))))
  15015. ((equal key "TAGS")
  15016. (setq eval '(org-with-point-at pom
  15017. (let ((org-fast-tag-selection-single-key
  15018. (if (eq org-fast-tag-selection-single-key 'expert)
  15019. t org-fast-tag-selection-single-key)))
  15020. (call-interactively 'org-set-tags)))))
  15021. ((equal key "DEADLINE")
  15022. (setq eval '(org-with-point-at pom
  15023. (call-interactively 'org-deadline))))
  15024. ((equal key "SCHEDULED")
  15025. (setq eval '(org-with-point-at pom
  15026. (call-interactively 'org-schedule))))
  15027. (t
  15028. (setq allowed (org-property-get-allowed-values pom key 'table))
  15029. (if allowed
  15030. (setq nval (completing-read "Value: " allowed nil t))
  15031. (setq nval (read-string "Edit: " value)))
  15032. (setq nval (org-trim nval))
  15033. (when (not (equal nval value))
  15034. (setq eval '(org-entry-put pom key nval)))))
  15035. (when eval
  15036. (let ((inhibit-read-only t))
  15037. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15038. (unwind-protect
  15039. (progn
  15040. (setq org-columns-overlays
  15041. (org-delete-all line-overlays org-columns-overlays))
  15042. (mapc 'org-delete-overlay line-overlays)
  15043. (org-columns-eval eval))
  15044. (org-columns-display-here))))
  15045. (move-to-column col)
  15046. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  15047. (org-columns-update key))))
  15048. (defun org-edit-headline () ; FIXME: this is not columns specific
  15049. "Edit the current headline, the part without TODO keyword, TAGS."
  15050. (org-back-to-heading)
  15051. (when (looking-at org-todo-line-regexp)
  15052. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15053. (txt (match-string 3))
  15054. (post "")
  15055. txt2)
  15056. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15057. (setq post (match-string 0 txt)
  15058. txt (substring txt 0 (match-beginning 0))))
  15059. (setq txt2 (read-string "Edit: " txt))
  15060. (when (not (equal txt txt2))
  15061. (beginning-of-line 1)
  15062. (insert pre txt2 post)
  15063. (delete-region (point) (point-at-eol))
  15064. (org-set-tags nil t)))))
  15065. (defun org-columns-edit-allowed ()
  15066. "Edit the list of allowed values for the current property."
  15067. (interactive)
  15068. (let* ((key (get-char-property (point) 'org-columns-key))
  15069. (key1 (concat key "_ALL"))
  15070. (allowed (org-entry-get (point) key1 t))
  15071. nval)
  15072. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15073. (setq nval (read-string "Allowed: " allowed))
  15074. (org-entry-put
  15075. (cond ((marker-position org-entry-property-inherited-from)
  15076. org-entry-property-inherited-from)
  15077. ((marker-position org-columns-top-level-marker)
  15078. org-columns-top-level-marker))
  15079. key1 nval)))
  15080. (defmacro org-no-warnings (&rest body)
  15081. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15082. (defun org-columns-eval (form)
  15083. (let (hidep)
  15084. (save-excursion
  15085. (beginning-of-line 1)
  15086. ;; `next-line' is needed here, because it skips invisible line.
  15087. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15088. (setq hidep (org-on-heading-p 1)))
  15089. (eval form)
  15090. (and hidep (hide-entry))))
  15091. (defun org-columns-previous-allowed-value ()
  15092. "Switch to the previous allowed value for this column."
  15093. (interactive)
  15094. (org-columns-next-allowed-value t))
  15095. (defun org-columns-next-allowed-value (&optional previous)
  15096. "Switch to the next allowed value for this column."
  15097. (interactive)
  15098. (org-columns-check-computed)
  15099. (let* ((col (current-column))
  15100. (key (get-char-property (point) 'org-columns-key))
  15101. (value (get-char-property (point) 'org-columns-value))
  15102. (bol (point-at-bol)) (eol (point-at-eol))
  15103. (pom (or (get-text-property bol 'org-hd-marker)
  15104. (point))) ; keep despite of compiler waring
  15105. (line-overlays
  15106. (delq nil (mapcar (lambda (x)
  15107. (and (eq (overlay-buffer x) (current-buffer))
  15108. (>= (overlay-start x) bol)
  15109. (<= (overlay-start x) eol)
  15110. x))
  15111. org-columns-overlays)))
  15112. (allowed (or (org-property-get-allowed-values pom key)
  15113. (and (equal
  15114. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15115. 'checkbox) '("[ ]" "[X]"))))
  15116. nval)
  15117. (when (equal key "ITEM")
  15118. (error "Cannot edit item headline from here"))
  15119. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15120. (error "Allowed values for this property have not been defined"))
  15121. (if (member key '("SCHEDULED" "DEADLINE"))
  15122. (setq nval (if previous 'earlier 'later))
  15123. (if previous (setq allowed (reverse allowed)))
  15124. (if (member value allowed)
  15125. (setq nval (car (cdr (member value allowed)))))
  15126. (setq nval (or nval (car allowed)))
  15127. (if (equal nval value)
  15128. (error "Only one allowed value for this property")))
  15129. (let ((inhibit-read-only t))
  15130. (remove-text-properties (1- bol) eol '(read-only t))
  15131. (unwind-protect
  15132. (progn
  15133. (setq org-columns-overlays
  15134. (org-delete-all line-overlays org-columns-overlays))
  15135. (mapc 'org-delete-overlay line-overlays)
  15136. (org-columns-eval '(org-entry-put pom key nval)))
  15137. (org-columns-display-here)))
  15138. (move-to-column col)
  15139. (if (nth 3 (assoc key org-columns-current-fmt-compiled))
  15140. (org-columns-update key))))
  15141. (defun org-verify-version (task)
  15142. (cond
  15143. ((eq task 'columns)
  15144. (if (or (featurep 'xemacs)
  15145. (< emacs-major-version 22))
  15146. (error "Emacs 22 is required for the columns feature")))))
  15147. (defun org-columns-open-link (&optional arg)
  15148. (interactive "P")
  15149. (let ((key (get-char-property (point) 'org-columns-key))
  15150. (value (get-char-property (point) 'org-columns-value)))
  15151. (org-open-link-from-string arg)))
  15152. (defun org-open-link-from-string (s &optional arg)
  15153. "Open a link in the string S, as if it was in Org-mode."
  15154. (interactive)
  15155. (with-temp-buffer
  15156. (let ((org-inhibit-startup t))
  15157. (org-mode)
  15158. (insert s)
  15159. (goto-char (point-min))
  15160. (org-open-at-point arg))))
  15161. (defun org-columns-get-format-and-top-level ()
  15162. (let (fmt)
  15163. (when (condition-case nil (org-back-to-heading) (error nil))
  15164. (move-marker org-entry-property-inherited-from nil)
  15165. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15166. (setq fmt (or fmt org-columns-default-format))
  15167. (org-set-local 'org-columns-current-fmt fmt)
  15168. (org-columns-compile-format fmt)
  15169. (if (marker-position org-entry-property-inherited-from)
  15170. (move-marker org-columns-top-level-marker
  15171. org-entry-property-inherited-from)
  15172. (move-marker org-columns-top-level-marker (point)))
  15173. fmt))
  15174. (defun org-columns ()
  15175. "Turn on column view on an org-mode file."
  15176. (interactive)
  15177. (org-verify-version 'columns)
  15178. (org-columns-remove-overlays)
  15179. (move-marker org-columns-begin-marker (point))
  15180. (let (beg end fmt cache maxwidths)
  15181. (setq fmt (org-columns-get-format-and-top-level))
  15182. (save-excursion
  15183. (goto-char org-columns-top-level-marker)
  15184. (setq beg (point))
  15185. (unless org-columns-inhibit-recalculation
  15186. (org-columns-compute-all))
  15187. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15188. (point-max)))
  15189. (goto-char beg)
  15190. ;; Get and cache the properties
  15191. (while (re-search-forward (concat "^" outline-regexp) end t)
  15192. (push (cons (org-current-line) (org-entry-properties)) cache))
  15193. (when cache
  15194. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15195. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15196. (org-columns-display-here-title)
  15197. (mapc (lambda (x)
  15198. (goto-line (car x))
  15199. (org-columns-display-here (cdr x)))
  15200. cache)))))
  15201. (defun org-columns-new (&optional prop title width op fmt)
  15202. "Insert a new column, to the leeft o the current column."
  15203. (interactive)
  15204. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15205. cell)
  15206. (setq prop (completing-read
  15207. "Property: " (mapcar 'list (org-buffer-property-keys t))
  15208. nil nil prop))
  15209. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15210. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15211. (if (string-match "\\S-" width)
  15212. (setq width (string-to-number width))
  15213. (setq width nil))
  15214. (setq fmt (completing-read "Summary [none]: "
  15215. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox"))
  15216. nil t))
  15217. (if (string-match "\\S-" fmt)
  15218. (setq fmt (intern fmt))
  15219. (setq fmt nil))
  15220. (if (eq fmt 'none) (setq fmt nil))
  15221. (if editp
  15222. (progn
  15223. (setcar editp prop)
  15224. (setcdr editp (list title width nil fmt)))
  15225. (setq cell (nthcdr (1- (current-column))
  15226. org-columns-current-fmt-compiled))
  15227. (setcdr cell (cons (list prop title width nil fmt)
  15228. (cdr cell))))
  15229. (org-columns-store-format)
  15230. (org-columns-redo)))
  15231. (defun org-columns-delete ()
  15232. "Delete the column at point from columns view."
  15233. (interactive)
  15234. (let* ((n (current-column))
  15235. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15236. (when (y-or-n-p
  15237. (format "Are you sure you want to remove column \"%s\"? " title))
  15238. (setq org-columns-current-fmt-compiled
  15239. (delq (nth n org-columns-current-fmt-compiled)
  15240. org-columns-current-fmt-compiled))
  15241. (org-columns-store-format)
  15242. (org-columns-redo)
  15243. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15244. (backward-char 1)))))
  15245. (defun org-columns-edit-attributes ()
  15246. "Edit the attributes of the current column."
  15247. (interactive)
  15248. (let* ((n (current-column))
  15249. (info (nth n org-columns-current-fmt-compiled)))
  15250. (apply 'org-columns-new info)))
  15251. (defun org-columns-widen (arg)
  15252. "Make the column wider by ARG characters."
  15253. (interactive "p")
  15254. (let* ((n (current-column))
  15255. (entry (nth n org-columns-current-fmt-compiled))
  15256. (width (or (nth 2 entry)
  15257. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15258. (setq width (max 1 (+ width arg)))
  15259. (setcar (nthcdr 2 entry) width)
  15260. (org-columns-store-format)
  15261. (org-columns-redo)))
  15262. (defun org-columns-narrow (arg)
  15263. "Make the column nrrower by ARG characters."
  15264. (interactive "p")
  15265. (org-columns-widen (- arg)))
  15266. (defun org-columns-move-right ()
  15267. "Swap this column with the one to the right."
  15268. (interactive)
  15269. (let* ((n (current-column))
  15270. (cell (nthcdr n org-columns-current-fmt-compiled))
  15271. e)
  15272. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15273. (error "Cannot shift this column further to the right"))
  15274. (setq e (car cell))
  15275. (setcar cell (car (cdr cell)))
  15276. (setcdr cell (cons e (cdr (cdr cell))))
  15277. (org-columns-store-format)
  15278. (org-columns-redo)
  15279. (forward-char 1)))
  15280. (defun org-columns-move-left ()
  15281. "Swap this column with the one to the left."
  15282. (interactive)
  15283. (let* ((n (current-column)))
  15284. (when (= n 0)
  15285. (error "Cannot shift this column further to the left"))
  15286. (backward-char 1)
  15287. (org-columns-move-right)
  15288. (backward-char 1)))
  15289. (defun org-columns-store-format ()
  15290. "Store the text version of the current columns format in appropriate place.
  15291. This is either in the COLUMNS property of the node starting the current column
  15292. display, or in the #+COLUMNS line of the current buffer."
  15293. (let (fmt (cnt 0))
  15294. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15295. (org-set-local 'org-columns-current-fmt fmt)
  15296. (if (marker-position org-columns-top-level-marker)
  15297. (save-excursion
  15298. (goto-char org-columns-top-level-marker)
  15299. (if (and (org-at-heading-p)
  15300. (org-entry-get nil "COLUMNS"))
  15301. (org-entry-put nil "COLUMNS" fmt)
  15302. (goto-char (point-min))
  15303. ;; Overwrite all #+COLUMNS lines....
  15304. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15305. (setq cnt (1+ cnt))
  15306. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15307. (unless (> cnt 0)
  15308. (goto-char (point-min))
  15309. (or (org-on-heading-p t) (outline-next-heading))
  15310. (let ((inhibit-read-only t))
  15311. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15312. (org-set-local 'org-columns-default-format fmt))))))
  15313. (defvar org-overriding-columns-format nil
  15314. "When set, overrides any other definition.")
  15315. (defvar org-agenda-view-columns-initially nil
  15316. "When set, switch to columns view immediately after creating the agenda.")
  15317. (defun org-agenda-columns ()
  15318. "Turn on column view in the agenda."
  15319. (interactive)
  15320. (org-verify-version 'columns)
  15321. (org-columns-remove-overlays)
  15322. (move-marker org-columns-begin-marker (point))
  15323. (let (fmt cache maxwidths m)
  15324. (cond
  15325. ((and (local-variable-p 'org-overriding-columns-format)
  15326. org-overriding-columns-format)
  15327. (setq fmt org-overriding-columns-format))
  15328. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15329. (setq fmt (org-entry-get m "COLUMNS" t)))
  15330. ((and (boundp 'org-columns-current-fmt)
  15331. (local-variable-p 'org-columns-current-fmt)
  15332. org-columns-current-fmt)
  15333. (setq fmt org-columns-current-fmt))
  15334. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15335. (setq m (get-text-property m 'org-hd-marker))
  15336. (setq fmt (org-entry-get m "COLUMNS" t))))
  15337. (setq fmt (or fmt org-columns-default-format))
  15338. (org-set-local 'org-columns-current-fmt fmt)
  15339. (org-columns-compile-format fmt)
  15340. (save-excursion
  15341. ;; Get and cache the properties
  15342. (goto-char (point-min))
  15343. (while (not (eobp))
  15344. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15345. (get-text-property (point) 'org-marker)))
  15346. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15347. (beginning-of-line 2))
  15348. (when cache
  15349. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15350. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15351. (org-columns-display-here-title)
  15352. (mapc (lambda (x)
  15353. (goto-line (car x))
  15354. (org-columns-display-here (cdr x)))
  15355. cache)))))
  15356. (defun org-columns-get-autowidth-alist (s cache)
  15357. "Derive the maximum column widths from the format and the cache."
  15358. (let ((start 0) rtn)
  15359. (while (string-match (org-re "%\\([[:alpha:]]\\S-*\\)") s start)
  15360. (push (cons (match-string 1 s) 1) rtn)
  15361. (setq start (match-end 0)))
  15362. (mapc (lambda (x)
  15363. (setcdr x (apply 'max
  15364. (mapcar
  15365. (lambda (y)
  15366. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15367. cache))))
  15368. rtn)
  15369. rtn))
  15370. (defun org-columns-compute-all ()
  15371. "Compute all columns that have operators defined."
  15372. (org-unmodified
  15373. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15374. (let ((columns org-columns-current-fmt-compiled) col)
  15375. (while (setq col (pop columns))
  15376. (when (nth 3 col)
  15377. (save-excursion
  15378. (org-columns-compute (car col)))))))
  15379. (defun org-columns-update (property)
  15380. "Recompute PROPERTY, and update the columns display for it."
  15381. (org-columns-compute property)
  15382. (let (fmt val pos)
  15383. (save-excursion
  15384. (mapc (lambda (ov)
  15385. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15386. (setq pos (org-overlay-start ov))
  15387. (goto-char pos)
  15388. (when (setq val (cdr (assoc property
  15389. (get-text-property
  15390. (point-at-bol) 'org-summaries))))
  15391. (setq fmt (org-overlay-get ov 'org-columns-format))
  15392. (org-overlay-put ov 'org-columns-value val)
  15393. (org-overlay-put ov 'display (format fmt val)))))
  15394. org-columns-overlays))))
  15395. (defun org-columns-compute (property)
  15396. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15397. (interactive)
  15398. (let* ((re (concat "^" outline-regexp))
  15399. (lmax 30) ; Does anyone use deeper levels???
  15400. (lsum (make-vector lmax 0))
  15401. (lflag (make-vector lmax nil))
  15402. (level 0)
  15403. (ass (assoc property org-columns-current-fmt-compiled))
  15404. (format (nth 4 ass))
  15405. (printf (nth 5 ass))
  15406. (beg org-columns-top-level-marker)
  15407. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15408. (save-excursion
  15409. ;; Find the region to compute
  15410. (goto-char beg)
  15411. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15412. (goto-char end)
  15413. ;; Walk the tree from the back and do the computations
  15414. (while (re-search-backward re beg t)
  15415. (setq sumpos (match-beginning 0)
  15416. last-level level
  15417. level (org-outline-level)
  15418. val (org-entry-get nil property)
  15419. valflag (and val (string-match "\\S-" val)))
  15420. (cond
  15421. ((< level last-level)
  15422. ;; put the sum of lower levels here as a property
  15423. (setq sum (aref lsum last-level) ; current sum
  15424. flag (aref lflag last-level) ; any valid entries from children?
  15425. str (org-column-number-to-string sum format printf)
  15426. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15427. useval (if flag str1 (if valflag val ""))
  15428. sum-alist (get-text-property sumpos 'org-summaries))
  15429. (if (assoc property sum-alist)
  15430. (setcdr (assoc property sum-alist) useval)
  15431. (push (cons property useval) sum-alist)
  15432. (org-unmodified
  15433. (add-text-properties sumpos (1+ sumpos)
  15434. (list 'org-summaries sum-alist))))
  15435. (when val
  15436. (org-entry-put nil property (if flag str val)))
  15437. ;; add current to current level accumulator
  15438. (when (or flag valflag)
  15439. (aset lsum level (+ (aref lsum level)
  15440. (if flag sum (org-column-string-to-number
  15441. (if flag str val) format))))
  15442. (aset lflag level t))
  15443. ;; clear accumulators for deeper levels
  15444. (loop for l from (1+ level) to (1- lmax) do
  15445. (aset lsum l 0)
  15446. (aset lflag l nil)))
  15447. ((>= level last-level)
  15448. ;; add what we have here to the accumulator for this level
  15449. (aset lsum level (+ (aref lsum level)
  15450. (org-column-string-to-number (or val "0") format)))
  15451. (and valflag (aset lflag level t)))
  15452. (t (error "This should not happen")))))))
  15453. (defun org-columns-redo ()
  15454. "Construct the column display again."
  15455. (interactive)
  15456. (message "Recomputing columns...")
  15457. (save-excursion
  15458. (if (marker-position org-columns-begin-marker)
  15459. (goto-char org-columns-begin-marker))
  15460. (org-columns-remove-overlays)
  15461. (if (org-mode-p)
  15462. (call-interactively 'org-columns)
  15463. (call-interactively 'org-agenda-columns)))
  15464. (message "Recomputing columns...done"))
  15465. (defun org-columns-not-in-agenda ()
  15466. (if (eq major-mode 'org-agenda-mode)
  15467. (error "This command is only allowed in Org-mode buffers")))
  15468. (defun org-string-to-number (s)
  15469. "Convert string to number, and interpret hh:mm:ss."
  15470. (if (not (string-match ":" s))
  15471. (string-to-number s)
  15472. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15473. (while l
  15474. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15475. sum)))
  15476. (defun org-column-number-to-string (n fmt printf)
  15477. "Convert a computed column number to a string value, according to FMT."
  15478. (cond
  15479. ((eq fmt 'add_times)
  15480. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15481. (format "%d:%02d" h m)))
  15482. ((eq fmt 'checkbox)
  15483. (cond ((= n (floor n)) "[X]")
  15484. ((> n 1.) "[-]")
  15485. (t "[ ]")))
  15486. (printf (format printf n))
  15487. ((eq fmt 'currency)
  15488. (format "%.2f" n))
  15489. (t (number-to-string n))))
  15490. (defun org-column-string-to-number (s fmt)
  15491. "Convert a column value to a number that can be used for column computing."
  15492. (cond
  15493. ((string-match ":" s)
  15494. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15495. (while l
  15496. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15497. sum))
  15498. ((eq fmt 'checkbox)
  15499. (if (equal s "[X]") 1. 0.000001))
  15500. (t (string-to-number s))))
  15501. (defun org-columns-uncompile-format (cfmt)
  15502. "Turn the compiled columns format back into a string representation."
  15503. (let ((rtn "") e s prop title op width fmt printf)
  15504. (while (setq e (pop cfmt))
  15505. (setq prop (car e)
  15506. title (nth 1 e)
  15507. width (nth 2 e)
  15508. op (nth 3 e)
  15509. fmt (nth 4 e)
  15510. printf (nth 5 e))
  15511. (cond
  15512. ((eq fmt 'add_times) (setq op ":"))
  15513. ((eq fmt 'checkbox) (setq op "X"))
  15514. ((eq fmt 'add_numbers) (setq op "+"))
  15515. ((eq fmt 'currency) (setq op "$")))
  15516. (if (and op printf) (setq op (concat op ";" printf)))
  15517. (if (equal title prop) (setq title nil))
  15518. (setq s (concat "%" (if width (number-to-string width))
  15519. prop
  15520. (if title (concat "(" title ")"))
  15521. (if op (concat "{" op "}"))))
  15522. (setq rtn (concat rtn " " s)))
  15523. (org-trim rtn)))
  15524. (defun org-columns-compile-format (fmt)
  15525. "Turn a column format string into an alist of specifications.
  15526. The alist has one entry for each column in the format. The elements of
  15527. that list are:
  15528. property the property
  15529. title the title field for the columns
  15530. width the column width in characters, can be nil for automatic
  15531. operator the operator if any
  15532. format the output format for computed results, derived from operator
  15533. printf a printf format for computed values"
  15534. (let ((start 0) width prop title op f printf)
  15535. (setq org-columns-current-fmt-compiled nil)
  15536. (while (string-match
  15537. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15538. fmt start)
  15539. (setq start (match-end 0)
  15540. width (match-string 1 fmt)
  15541. prop (match-string 2 fmt)
  15542. title (or (match-string 3 fmt) prop)
  15543. op (match-string 4 fmt)
  15544. f nil
  15545. printf nil)
  15546. (if width (setq width (string-to-number width)))
  15547. (when (and op (string-match ";" op))
  15548. (setq printf (substring op (match-end 0))
  15549. op (substring op 0 (match-beginning 0))))
  15550. (cond
  15551. ((equal op "+") (setq f 'add_numbers))
  15552. ((equal op "$") (setq f 'currency))
  15553. ((equal op ":") (setq f 'add_times))
  15554. ((equal op "X") (setq f 'checkbox)))
  15555. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15556. (setq org-columns-current-fmt-compiled
  15557. (nreverse org-columns-current-fmt-compiled))))
  15558. ;;; Dynamic block for Column view
  15559. (defun org-columns-capture-view ()
  15560. "Get the column view of the current buffer and return it as a list.
  15561. The list will contains the title row and all other rows. Each row is
  15562. a list of fields."
  15563. (save-excursion
  15564. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15565. (n (length title)) row tbl)
  15566. (goto-char (point-min))
  15567. (while (re-search-forward "^\\*+ " nil t)
  15568. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15569. (setq row nil)
  15570. (loop for i from 0 to (1- n) do
  15571. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15572. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15573. "")
  15574. row))
  15575. (setq row (nreverse row))
  15576. (push row tbl)))
  15577. (append (list title 'hline) (nreverse tbl)))))
  15578. (defun org-dblock-write:columnview (params)
  15579. "Write the column view table.
  15580. PARAMS is a property list of parameters:
  15581. :width enforce same column widths with <N> specifiers.
  15582. :id the :ID: property of the entry where the columns view
  15583. should be built, as a string. When `local', call locally.
  15584. When `global' call column view with the cursor at the beginning
  15585. of the buffer (usually this means that the whole buffer switches
  15586. to column view).
  15587. :hlines When t, insert a hline before each item. When a number, insert
  15588. a hline before each level <= that number.
  15589. :vlines When t, make each column a colgroup to enforce vertical lines."
  15590. (let ((pos (move-marker (make-marker) (point)))
  15591. (hlines (plist-get params :hlines))
  15592. (vlines (plist-get params :vlines))
  15593. tbl id idpos nfields tmp)
  15594. (save-excursion
  15595. (save-restriction
  15596. (when (setq id (plist-get params :id))
  15597. (cond ((not id) nil)
  15598. ((eq id 'global) (goto-char (point-min)))
  15599. ((eq id 'local) nil)
  15600. ((setq idpos (org-find-entry-with-id id))
  15601. (goto-char idpos))
  15602. (t (error "Cannot find entry with :ID: %s" id))))
  15603. (org-columns)
  15604. (setq tbl (org-columns-capture-view))
  15605. (setq nfields (length (car tbl)))
  15606. (org-columns-quit)))
  15607. (goto-char pos)
  15608. (move-marker pos nil)
  15609. (when tbl
  15610. (when (plist-get params :hlines)
  15611. (setq tmp nil)
  15612. (while tbl
  15613. (if (eq (car tbl) 'hline)
  15614. (push (pop tbl) tmp)
  15615. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  15616. (if (and (not (eq (car tmp) 'hline))
  15617. (or (eq hlines t)
  15618. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  15619. (push 'hline tmp)))
  15620. (push (pop tbl) tmp)))
  15621. (setq tbl (nreverse tmp)))
  15622. (when vlines
  15623. (setq tbl (mapcar (lambda (x)
  15624. (if (eq 'hline x) x (cons "" x)))
  15625. tbl))
  15626. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  15627. (setq pos (point))
  15628. (insert (org-listtable-to-string tbl))
  15629. (when (plist-get params :width)
  15630. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  15631. org-columns-current-widths "|")))
  15632. (goto-char pos)
  15633. (org-table-align))))
  15634. (defun org-listtable-to-string (tbl)
  15635. "Convert a listtable TBL to a string that contains the Org-mode table.
  15636. The table still need to be alligned. The resulting string has no leading
  15637. and tailing newline characters."
  15638. (mapconcat
  15639. (lambda (x)
  15640. (cond
  15641. ((listp x)
  15642. (concat "|" (mapconcat 'identity x "|") "|"))
  15643. ((eq x 'hline) "|-|")
  15644. (t (error "Garbage in listtable: %s" x))))
  15645. tbl "\n"))
  15646. (defun org-insert-columns-dblock ()
  15647. "Create a dynamic block capturing a column view table."
  15648. (interactive)
  15649. (let ((defaults '(:name "columnview" :hlines 1))
  15650. (id (completing-read
  15651. "Capture columns (local, global, entry with :ID: property) [local]: "
  15652. (append '(("global") ("local"))
  15653. (mapcar 'list (org-property-values "ID"))))))
  15654. (if (equal id "") (setq id 'local))
  15655. (if (equal id "global") (setq id 'global))
  15656. (setq defaults (append defaults (list :id id)))
  15657. (org-create-dblock defaults)
  15658. (org-update-dblock)))
  15659. ;;;; Timestamps
  15660. (defvar org-last-changed-timestamp nil)
  15661. (defvar org-time-was-given) ; dynamically scoped parameter
  15662. (defvar org-end-time-was-given) ; dynamically scoped parameter
  15663. (defvar org-ts-what) ; dynamically scoped parameter
  15664. (defun org-time-stamp (arg)
  15665. "Prompt for a date/time and insert a time stamp.
  15666. If the user specifies a time like HH:MM, or if this command is called
  15667. with a prefix argument, the time stamp will contain date and time.
  15668. Otherwise, only the date will be included. All parts of a date not
  15669. specified by the user will be filled in from the current date/time.
  15670. So if you press just return without typing anything, the time stamp
  15671. will represent the current date/time. If there is already a timestamp
  15672. at the cursor, it will be modified."
  15673. (interactive "P")
  15674. (let* ((ts nil)
  15675. (default-time
  15676. ;; Default time is either today, or, when entering a range,
  15677. ;; the range start.
  15678. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  15679. (save-excursion
  15680. (re-search-backward
  15681. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  15682. (- (point) 20) t)))
  15683. (apply 'encode-time (org-parse-time-string (match-string 1)))
  15684. (current-time)))
  15685. (default-input (and ts (org-get-compact-tod ts)))
  15686. org-time-was-given org-end-time-was-given time)
  15687. (cond
  15688. ((and (org-at-timestamp-p)
  15689. (eq last-command 'org-time-stamp)
  15690. (eq this-command 'org-time-stamp))
  15691. (insert "--")
  15692. (setq time (let ((this-command this-command))
  15693. (org-read-date arg 'totime nil nil default-time default-input)))
  15694. (org-insert-time-stamp time (or org-time-was-given arg)))
  15695. ((org-at-timestamp-p)
  15696. (setq time (let ((this-command this-command))
  15697. (org-read-date arg 'totime nil nil default-time default-input)))
  15698. (when (org-at-timestamp-p) ; just to get the match data
  15699. (replace-match "")
  15700. (setq org-last-changed-timestamp
  15701. (org-insert-time-stamp
  15702. time (or org-time-was-given arg)
  15703. nil nil nil (list org-end-time-was-given))))
  15704. (message "Timestamp updated"))
  15705. (t
  15706. (setq time (let ((this-command this-command))
  15707. (org-read-date arg 'totime nil nil default-time default-input)))
  15708. (org-insert-time-stamp time (or org-time-was-given arg)
  15709. nil nil nil (list org-end-time-was-given))))))
  15710. ;; FIXME: can we use this for something else????
  15711. ;; like computing time differences?????
  15712. (defun org-get-compact-tod (s)
  15713. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15714. (let* ((t1 (match-string 1 s))
  15715. (h1 (string-to-number (match-string 2 s)))
  15716. (m1 (string-to-number (match-string 3 s)))
  15717. (t2 (and (match-end 4) (match-string 5 s)))
  15718. (h2 (and t2 (string-to-number (match-string 6 s))))
  15719. (m2 (and t2 (string-to-number (match-string 7 s))))
  15720. dh dm)
  15721. (if (not t2)
  15722. t1
  15723. (setq dh (- h2 h1) dm (- m2 m1))
  15724. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15725. (concat t1 "+" (number-to-string dh)
  15726. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  15727. (defun org-time-stamp-inactive (&optional arg)
  15728. "Insert an inactive time stamp.
  15729. An inactive time stamp is enclosed in square brackets instead of angle
  15730. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15731. does not link to the calendar and cannot be changed with the S-cursor keys.
  15732. So these are more for recording a certain time/date."
  15733. (interactive "P")
  15734. (let (org-time-was-given org-end-time-was-given time)
  15735. (setq time (org-read-date arg 'totime))
  15736. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  15737. nil nil (list org-end-time-was-given))))
  15738. (defvar org-date-ovl (org-make-overlay 1 1))
  15739. (org-overlay-put org-date-ovl 'face 'org-warning)
  15740. (org-detach-overlay org-date-ovl)
  15741. (defvar org-ans1) ; dynamically scoped parameter
  15742. (defvar org-ans2) ; dynamically scoped parameter
  15743. (defvar org-plain-time-of-day-regexp) ; defined below
  15744. (defvar org-read-date-overlay nil)
  15745. (defvar org-dcst nil) ; dynamically scoped
  15746. (defun org-read-date (&optional with-time to-time from-string prompt
  15747. default-time default-input)
  15748. "Read a date, possibly a time, and make things smooth for the user.
  15749. The prompt will suggest to enter an ISO date, but you can also enter anything
  15750. which will at least partially be understood by `parse-time-string'.
  15751. Unrecognized parts of the date will default to the current day, month, year,
  15752. hour and minute. If this command is called to replace a timestamp at point,
  15753. of to enter the second timestamp of a range, the default time is taken from the
  15754. existing stamp. For example,
  15755. 3-2-5 --> 2003-02-05
  15756. feb 15 --> currentyear-02-15
  15757. sep 12 9 --> 2009-09-12
  15758. 12:45 --> today 12:45
  15759. 22 sept 0:34 --> currentyear-09-22 0:34
  15760. 12 --> currentyear-currentmonth-12
  15761. Fri --> nearest Friday (today or later)
  15762. etc.
  15763. Furthermore you can specify a relative date by giving, as the *first* thing
  15764. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  15765. change in days weeks, months, years.
  15766. With a single plus or minus, the date is relative to today. With a double
  15767. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15768. +4d --> four days from today
  15769. +4 --> same as above
  15770. +2w --> two weeks from today
  15771. ++5 --> five days from default date
  15772. The function understands only English month and weekday abbreviations,
  15773. but this can be configured with the variables `parse-time-months' and
  15774. `parse-time-weekdays'.
  15775. While prompting, a calendar is popped up - you can also select the
  15776. date with the mouse (button 1). The calendar shows a period of three
  15777. months. To scroll it to other months, use the keys `>' and `<'.
  15778. If you don't like the calendar, turn it off with
  15779. \(setq org-read-date-popup-calendar nil)
  15780. With optional argument TO-TIME, the date will immediately be converted
  15781. to an internal time.
  15782. With an optional argument WITH-TIME, the prompt will suggest to also
  15783. insert a time. Note that when WITH-TIME is not set, you can still
  15784. enter a time, and this function will inform the calling routine about
  15785. this change. The calling routine may then choose to change the format
  15786. used to insert the time stamp into the buffer to include the time.
  15787. With optional argument FROM-STRING, read from this string instead from
  15788. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15789. the time/date that is used for everything that is not specified by the
  15790. user."
  15791. (require 'parse-time)
  15792. (let* ((org-time-stamp-rounding-minutes
  15793. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  15794. (org-dcst org-display-custom-times)
  15795. (ct (org-current-time))
  15796. (def (or default-time ct))
  15797. (defdecode (decode-time def))
  15798. (dummy (progn
  15799. (when (< (nth 2 defdecode) org-extend-today-until)
  15800. (setcar (nthcdr 2 defdecode) -1)
  15801. (setcar (nthcdr 1 defdecode) 59)
  15802. (setq def (apply 'encode-time defdecode)
  15803. defdecode (decode-time def)))))
  15804. (calendar-move-hook nil)
  15805. (view-diary-entries-initially nil)
  15806. (view-calendar-holidays-initially nil)
  15807. (timestr (format-time-string
  15808. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  15809. (prompt (concat (if prompt (concat prompt " ") "")
  15810. (format "Date+time [%s]: " timestr)))
  15811. ans (org-ans0 "") org-ans1 org-ans2 final)
  15812. (cond
  15813. (from-string (setq ans from-string))
  15814. (org-read-date-popup-calendar
  15815. (save-excursion
  15816. (save-window-excursion
  15817. (calendar)
  15818. (calendar-forward-day (- (time-to-days def)
  15819. (calendar-absolute-from-gregorian
  15820. (calendar-current-date))))
  15821. (org-eval-in-calendar nil t)
  15822. (let* ((old-map (current-local-map))
  15823. (map (copy-keymap calendar-mode-map))
  15824. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  15825. (org-defkey map (kbd "RET") 'org-calendar-select)
  15826. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  15827. 'org-calendar-select-mouse)
  15828. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  15829. 'org-calendar-select-mouse)
  15830. (org-defkey minibuffer-local-map [(meta shift left)]
  15831. (lambda () (interactive)
  15832. (org-eval-in-calendar '(calendar-backward-month 1))))
  15833. (org-defkey minibuffer-local-map [(meta shift right)]
  15834. (lambda () (interactive)
  15835. (org-eval-in-calendar '(calendar-forward-month 1))))
  15836. (org-defkey minibuffer-local-map [(meta shift up)]
  15837. (lambda () (interactive)
  15838. (org-eval-in-calendar '(calendar-backward-year 1))))
  15839. (org-defkey minibuffer-local-map [(meta shift down)]
  15840. (lambda () (interactive)
  15841. (org-eval-in-calendar '(calendar-forward-year 1))))
  15842. (org-defkey minibuffer-local-map [(shift up)]
  15843. (lambda () (interactive)
  15844. (org-eval-in-calendar '(calendar-backward-week 1))))
  15845. (org-defkey minibuffer-local-map [(shift down)]
  15846. (lambda () (interactive)
  15847. (org-eval-in-calendar '(calendar-forward-week 1))))
  15848. (org-defkey minibuffer-local-map [(shift left)]
  15849. (lambda () (interactive)
  15850. (org-eval-in-calendar '(calendar-backward-day 1))))
  15851. (org-defkey minibuffer-local-map [(shift right)]
  15852. (lambda () (interactive)
  15853. (org-eval-in-calendar '(calendar-forward-day 1))))
  15854. (org-defkey minibuffer-local-map ">"
  15855. (lambda () (interactive)
  15856. (org-eval-in-calendar '(scroll-calendar-left 1))))
  15857. (org-defkey minibuffer-local-map "<"
  15858. (lambda () (interactive)
  15859. (org-eval-in-calendar '(scroll-calendar-right 1))))
  15860. (unwind-protect
  15861. (progn
  15862. (use-local-map map)
  15863. (add-hook 'post-command-hook 'org-read-date-display)
  15864. (setq org-ans0 (read-string prompt default-input nil nil))
  15865. ;; org-ans0: from prompt
  15866. ;; org-ans1: from mouse click
  15867. ;; org-ans2: from calendar motion
  15868. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15869. (remove-hook 'post-command-hook 'org-read-date-display)
  15870. (use-local-map old-map)
  15871. (when org-read-date-overlay
  15872. (org-delete-overlay org-read-date-overlay)
  15873. (setq org-read-date-overlay nil)))))))
  15874. (t ; Naked prompt only
  15875. (unwind-protect
  15876. (setq ans (read-string prompt default-input nil timestr))
  15877. (when org-read-date-overlay
  15878. (org-delete-overlay org-read-date-overlay)
  15879. (setq org-read-date-overlay nil)))))
  15880. (setq final (org-read-date-analyze ans def defdecode))
  15881. (if to-time
  15882. (apply 'encode-time final)
  15883. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15884. (format "%04d-%02d-%02d %02d:%02d"
  15885. (nth 5 final) (nth 4 final) (nth 3 final)
  15886. (nth 2 final) (nth 1 final))
  15887. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15888. (defvar def)
  15889. (defvar defdecode)
  15890. (defvar with-time)
  15891. (defun org-read-date-display ()
  15892. "Display the currrent date prompt interpretation in the minibuffer."
  15893. (when org-read-date-display-live
  15894. (when org-read-date-overlay
  15895. (org-delete-overlay org-read-date-overlay))
  15896. (let ((p (point)))
  15897. (end-of-line 1)
  15898. (while (not (equal (buffer-substring
  15899. (max (point-min) (- (point) 4)) (point))
  15900. " "))
  15901. (insert " "))
  15902. (goto-char p))
  15903. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15904. " " (or org-ans1 org-ans2)))
  15905. (org-end-time-was-given nil)
  15906. (f (org-read-date-analyze ans def defdecode))
  15907. (fmts (if org-dcst
  15908. org-time-stamp-custom-formats
  15909. org-time-stamp-formats))
  15910. (fmt (if (or with-time
  15911. (and (boundp 'org-time-was-given) org-time-was-given))
  15912. (cdr fmts)
  15913. (car fmts)))
  15914. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  15915. (when (and org-end-time-was-given
  15916. (string-match org-plain-time-of-day-regexp txt))
  15917. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15918. org-end-time-was-given
  15919. (substring txt (match-end 0)))))
  15920. (setq org-read-date-overlay
  15921. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15922. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  15923. (defun org-read-date-analyze (ans def defdecode)
  15924. "Analyze the combined answer of the date prompt."
  15925. ;; FIXME: cleanup and comment
  15926. (let (delta deltan deltaw deltadef year month day
  15927. hour minute second wday pm h2 m2 tl wday1)
  15928. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  15929. (setq ans (replace-match "" t t ans)
  15930. deltan (car delta)
  15931. deltaw (nth 1 delta)
  15932. deltadef (nth 2 delta)))
  15933. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  15934. (when (string-match
  15935. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15936. (setq year (if (match-end 2)
  15937. (string-to-number (match-string 2 ans))
  15938. (string-to-number (format-time-string "%Y")))
  15939. month (string-to-number (match-string 3 ans))
  15940. day (string-to-number (match-string 4 ans)))
  15941. (if (< year 100) (setq year (+ 2000 year)))
  15942. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15943. t nil ans)))
  15944. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15945. ;; If there is a time with am/pm, and *no* time without it, we convert
  15946. ;; so that matching will be successful.
  15947. (loop for i from 1 to 2 do ; twice, for end time as well
  15948. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15949. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15950. (setq hour (string-to-number (match-string 1 ans))
  15951. minute (if (match-end 3)
  15952. (string-to-number (match-string 3 ans))
  15953. 0)
  15954. pm (equal ?p
  15955. (string-to-char (downcase (match-string 4 ans)))))
  15956. (if (and (= hour 12) (not pm))
  15957. (setq hour 0)
  15958. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15959. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15960. t t ans))))
  15961. ;; Check if a time range is given as a duration
  15962. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15963. (setq hour (string-to-number (match-string 1 ans))
  15964. h2 (+ hour (string-to-number (match-string 3 ans)))
  15965. minute (string-to-number (match-string 2 ans))
  15966. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  15967. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15968. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  15969. ;; Check if there is a time range
  15970. (when (boundp 'org-end-time-was-given)
  15971. (setq org-time-was-given nil)
  15972. (when (and (string-match org-plain-time-of-day-regexp ans)
  15973. (match-end 8))
  15974. (setq org-end-time-was-given (match-string 8 ans))
  15975. (setq ans (concat (substring ans 0 (match-beginning 7))
  15976. (substring ans (match-end 7))))))
  15977. (setq tl (parse-time-string ans)
  15978. day (or (nth 3 tl) (nth 3 defdecode))
  15979. month (or (nth 4 tl)
  15980. (if (and org-read-date-prefer-future
  15981. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  15982. (1+ (nth 4 defdecode))
  15983. (nth 4 defdecode)))
  15984. year (or (nth 5 tl)
  15985. (if (and org-read-date-prefer-future
  15986. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  15987. (1+ (nth 5 defdecode))
  15988. (nth 5 defdecode)))
  15989. hour (or (nth 2 tl) (nth 2 defdecode))
  15990. minute (or (nth 1 tl) (nth 1 defdecode))
  15991. second (or (nth 0 tl) 0)
  15992. wday (nth 6 tl))
  15993. (when deltan
  15994. (unless deltadef
  15995. (let ((now (decode-time (current-time))))
  15996. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15997. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15998. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15999. ((equal deltaw "m") (setq month (+ month deltan)))
  16000. ((equal deltaw "y") (setq year (+ year deltan)))))
  16001. (when (and wday (not (nth 3 tl)))
  16002. ;; Weekday was given, but no day, so pick that day in the week
  16003. ;; on or after the derived date.
  16004. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16005. (unless (equal wday wday1)
  16006. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16007. (if (and (boundp 'org-time-was-given)
  16008. (nth 2 tl))
  16009. (setq org-time-was-given t))
  16010. (if (< year 100) (setq year (+ 2000 year)))
  16011. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16012. (list second minute hour day month year)))
  16013. (defvar parse-time-weekdays)
  16014. (defun org-read-date-get-relative (s today default)
  16015. "Check string S for special relative date string.
  16016. TODAY and DEFAULT are internal times, for today and for a default.
  16017. Return shift list (N what def-flag)
  16018. WHAT is \"d\", \"w\", \"m\", or \"y\" for day. week, month, year.
  16019. N is the number if WHATs to shift
  16020. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16021. the DEFAULT date rather than TODAY."
  16022. (when (string-match
  16023. (concat
  16024. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16025. "\\([0-9]+\\)?"
  16026. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16027. "\\([ \t]\\|$\\)") s)
  16028. (let* ((dir (if (match-end 1)
  16029. (string-to-char (substring (match-string 1 s) -1))
  16030. ?+))
  16031. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16032. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16033. (what (if (match-end 3) (match-string 3 s) "d"))
  16034. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16035. (date (if rel default today))
  16036. (wday (nth 6 (decode-time date)))
  16037. delta)
  16038. (if wday1
  16039. (progn
  16040. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16041. (if (= dir ?-) (setq delta (- delta 7)))
  16042. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16043. (list delta "d" rel))
  16044. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16045. (defun org-eval-in-calendar (form &optional keepdate)
  16046. "Eval FORM in the calendar window and return to current window.
  16047. Also, store the cursor date in variable org-ans2."
  16048. (let ((sw (selected-window)))
  16049. (select-window (get-buffer-window "*Calendar*"))
  16050. (eval form)
  16051. (when (and (not keepdate) (calendar-cursor-to-date))
  16052. (let* ((date (calendar-cursor-to-date))
  16053. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16054. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16055. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16056. (select-window sw)))
  16057. ; ;; Update the prompt to show new default date
  16058. ; (save-excursion
  16059. ; (goto-char (point-min))
  16060. ; (when (and org-ans2
  16061. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16062. ; (get-text-property (match-end 0) 'field))
  16063. ; (let ((inhibit-read-only t))
  16064. ; (replace-match (concat "[" org-ans2 "]") t t)
  16065. ; (add-text-properties (point-min) (1+ (match-end 0))
  16066. ; (text-properties-at (1+ (point-min)))))))))
  16067. (defun org-calendar-select ()
  16068. "Return to `org-read-date' with the date currently selected.
  16069. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16070. (interactive)
  16071. (when (calendar-cursor-to-date)
  16072. (let* ((date (calendar-cursor-to-date))
  16073. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16074. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16075. (if (active-minibuffer-window) (exit-minibuffer))))
  16076. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16077. "Insert a date stamp for the date given by the internal TIME.
  16078. WITH-HM means, use the stamp format that includes the time of the day.
  16079. INACTIVE means use square brackets instead of angular ones, so that the
  16080. stamp will not contribute to the agenda.
  16081. PRE and POST are optional strings to be inserted before and after the
  16082. stamp.
  16083. The command returns the inserted time stamp."
  16084. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16085. stamp)
  16086. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16087. (insert-before-markers (or pre ""))
  16088. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16089. (when (listp extra)
  16090. (setq extra (car extra))
  16091. (if (and (stringp extra)
  16092. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16093. (setq extra (format "-%02d:%02d"
  16094. (string-to-number (match-string 1 extra))
  16095. (string-to-number (match-string 2 extra))))
  16096. (setq extra nil)))
  16097. (when extra
  16098. (backward-char 1)
  16099. (insert-before-markers extra)
  16100. (forward-char 1))
  16101. (insert-before-markers (or post ""))
  16102. stamp))
  16103. (defun org-toggle-time-stamp-overlays ()
  16104. "Toggle the use of custom time stamp formats."
  16105. (interactive)
  16106. (setq org-display-custom-times (not org-display-custom-times))
  16107. (unless org-display-custom-times
  16108. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16109. (while (setq p (next-single-property-change p 'display))
  16110. (if (and (get-text-property p 'display)
  16111. (eq (get-text-property p 'face) 'org-date))
  16112. (remove-text-properties
  16113. p (setq p (next-single-property-change p 'display))
  16114. '(display t))))
  16115. (set-buffer-modified-p bmp)))
  16116. (if (featurep 'xemacs)
  16117. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16118. (org-restart-font-lock)
  16119. (setq org-table-may-need-update t)
  16120. (if org-display-custom-times
  16121. (message "Time stamps are overlayed with custom format")
  16122. (message "Time stamp overlays removed")))
  16123. (defun org-display-custom-time (beg end)
  16124. "Overlay modified time stamp format over timestamp between BED and END."
  16125. (let* ((ts (buffer-substring beg end))
  16126. t1 w1 with-hm tf time str w2 (off 0))
  16127. (save-match-data
  16128. (setq t1 (org-parse-time-string ts t))
  16129. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16130. (setq off (- (match-end 0) (match-beginning 0)))))
  16131. (setq end (- end off))
  16132. (setq w1 (- end beg)
  16133. with-hm (and (nth 1 t1) (nth 2 t1))
  16134. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16135. time (org-fix-decoded-time t1)
  16136. str (org-add-props
  16137. (format-time-string
  16138. (substring tf 1 -1) (apply 'encode-time time))
  16139. nil 'mouse-face 'highlight)
  16140. w2 (length str))
  16141. (if (not (= w2 w1))
  16142. (add-text-properties (1+ beg) (+ 2 beg)
  16143. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16144. (if (featurep 'xemacs)
  16145. (progn
  16146. (put-text-property beg end 'invisible t)
  16147. (put-text-property beg end 'end-glyph (make-glyph str)))
  16148. (put-text-property beg end 'display str))))
  16149. (defun org-translate-time (string)
  16150. "Translate all timestamps in STRING to custom format.
  16151. But do this only if the variable `org-display-custom-times' is set."
  16152. (when org-display-custom-times
  16153. (save-match-data
  16154. (let* ((start 0)
  16155. (re org-ts-regexp-both)
  16156. t1 with-hm inactive tf time str beg end)
  16157. (while (setq start (string-match re string start))
  16158. (setq beg (match-beginning 0)
  16159. end (match-end 0)
  16160. t1 (save-match-data
  16161. (org-parse-time-string (substring string beg end) t))
  16162. with-hm (and (nth 1 t1) (nth 2 t1))
  16163. inactive (equal (substring string beg (1+ beg)) "[")
  16164. tf (funcall (if with-hm 'cdr 'car)
  16165. org-time-stamp-custom-formats)
  16166. time (org-fix-decoded-time t1)
  16167. str (format-time-string
  16168. (concat
  16169. (if inactive "[" "<") (substring tf 1 -1)
  16170. (if inactive "]" ">"))
  16171. (apply 'encode-time time))
  16172. string (replace-match str t t string)
  16173. start (+ start (length str)))))))
  16174. string)
  16175. (defun org-fix-decoded-time (time)
  16176. "Set 0 instead of nil for the first 6 elements of time.
  16177. Don't touch the rest."
  16178. (let ((n 0))
  16179. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16180. (defun org-days-to-time (timestamp-string)
  16181. "Difference between TIMESTAMP-STRING and now in days."
  16182. (- (time-to-days (org-time-string-to-time timestamp-string))
  16183. (time-to-days (current-time))))
  16184. (defun org-deadline-close (timestamp-string &optional ndays)
  16185. "Is the time in TIMESTAMP-STRING close to the current date?"
  16186. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16187. (and (< (org-days-to-time timestamp-string) ndays)
  16188. (not (org-entry-is-done-p))))
  16189. (defun org-get-wdays (ts)
  16190. "Get the deadline lead time appropriate for timestring TS."
  16191. (cond
  16192. ((<= org-deadline-warning-days 0)
  16193. ;; 0 or negative, enforce this value no matter what
  16194. (- org-deadline-warning-days))
  16195. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16196. ;; lead time is specified.
  16197. (floor (* (string-to-number (match-string 1 ts))
  16198. (cdr (assoc (match-string 2 ts)
  16199. '(("d" . 1) ("w" . 7)
  16200. ("m" . 30.4) ("y" . 365.25)))))))
  16201. ;; go for the default.
  16202. (t org-deadline-warning-days)))
  16203. (defun org-calendar-select-mouse (ev)
  16204. "Return to `org-read-date' with the date currently selected.
  16205. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16206. (interactive "e")
  16207. (mouse-set-point ev)
  16208. (when (calendar-cursor-to-date)
  16209. (let* ((date (calendar-cursor-to-date))
  16210. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16211. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16212. (if (active-minibuffer-window) (exit-minibuffer))))
  16213. (defun org-check-deadlines (ndays)
  16214. "Check if there are any deadlines due or past due.
  16215. A deadline is considered due if it happens within `org-deadline-warning-days'
  16216. days from today's date. If the deadline appears in an entry marked DONE,
  16217. it is not shown. The prefix arg NDAYS can be used to test that many
  16218. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16219. (interactive "P")
  16220. (let* ((org-warn-days
  16221. (cond
  16222. ((equal ndays '(4)) 100000)
  16223. (ndays (prefix-numeric-value ndays))
  16224. (t (abs org-deadline-warning-days))))
  16225. (case-fold-search nil)
  16226. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16227. (callback
  16228. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16229. (message "%d deadlines past-due or due within %d days"
  16230. (org-occur regexp nil callback)
  16231. org-warn-days)))
  16232. (defun org-check-before-date (date)
  16233. "Check if there are deadlines or scheduled entries before DATE."
  16234. (interactive (list (org-read-date)))
  16235. (let ((case-fold-search nil)
  16236. (regexp (concat "\\<\\(" org-deadline-string
  16237. "\\|" org-scheduled-string
  16238. "\\) *<\\([^>]+\\)>"))
  16239. (callback
  16240. (lambda () (time-less-p
  16241. (org-time-string-to-time (match-string 2))
  16242. (org-time-string-to-time date)))))
  16243. (message "%d entries before %s"
  16244. (org-occur regexp nil callback) date)))
  16245. (defun org-evaluate-time-range (&optional to-buffer)
  16246. "Evaluate a time range by computing the difference between start and end.
  16247. Normally the result is just printed in the echo area, but with prefix arg
  16248. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16249. If the time range is actually in a table, the result is inserted into the
  16250. next column.
  16251. For time difference computation, a year is assumed to be exactly 365
  16252. days in order to avoid rounding problems."
  16253. (interactive "P")
  16254. (or
  16255. (org-clock-update-time-maybe)
  16256. (save-excursion
  16257. (unless (org-at-date-range-p t)
  16258. (goto-char (point-at-bol))
  16259. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16260. (if (not (org-at-date-range-p t))
  16261. (error "Not at a time-stamp range, and none found in current line")))
  16262. (let* ((ts1 (match-string 1))
  16263. (ts2 (match-string 2))
  16264. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16265. (match-end (match-end 0))
  16266. (time1 (org-time-string-to-time ts1))
  16267. (time2 (org-time-string-to-time ts2))
  16268. (t1 (time-to-seconds time1))
  16269. (t2 (time-to-seconds time2))
  16270. (diff (abs (- t2 t1)))
  16271. (negative (< (- t2 t1) 0))
  16272. ;; (ys (floor (* 365 24 60 60)))
  16273. (ds (* 24 60 60))
  16274. (hs (* 60 60))
  16275. (fy "%dy %dd %02d:%02d")
  16276. (fy1 "%dy %dd")
  16277. (fd "%dd %02d:%02d")
  16278. (fd1 "%dd")
  16279. (fh "%02d:%02d")
  16280. y d h m align)
  16281. (if havetime
  16282. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16283. y 0
  16284. d (floor (/ diff ds)) diff (mod diff ds)
  16285. h (floor (/ diff hs)) diff (mod diff hs)
  16286. m (floor (/ diff 60)))
  16287. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16288. y 0
  16289. d (floor (+ (/ diff ds) 0.5))
  16290. h 0 m 0))
  16291. (if (not to-buffer)
  16292. (message "%s" (org-make-tdiff-string y d h m))
  16293. (if (org-at-table-p)
  16294. (progn
  16295. (goto-char match-end)
  16296. (setq align t)
  16297. (and (looking-at " *|") (goto-char (match-end 0))))
  16298. (goto-char match-end))
  16299. (if (looking-at
  16300. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16301. (replace-match ""))
  16302. (if negative (insert " -"))
  16303. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16304. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16305. (insert " " (format fh h m))))
  16306. (if align (org-table-align))
  16307. (message "Time difference inserted")))))
  16308. (defun org-make-tdiff-string (y d h m)
  16309. (let ((fmt "")
  16310. (l nil))
  16311. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16312. l (push y l)))
  16313. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16314. l (push d l)))
  16315. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16316. l (push h l)))
  16317. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16318. l (push m l)))
  16319. (apply 'format fmt (nreverse l))))
  16320. (defun org-time-string-to-time (s)
  16321. (apply 'encode-time (org-parse-time-string s)))
  16322. (defun org-time-string-to-absolute (s &optional daynr)
  16323. "Convert a time stamp to an absolute day number.
  16324. If there is a specifyer for a cyclic time stamp, get the closest date to
  16325. DAYNR."
  16326. (cond
  16327. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16328. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16329. daynr
  16330. (+ daynr 1000)))
  16331. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16332. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16333. (time-to-days (current-time))) (match-string 0 s)))
  16334. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16335. (defun org-time-from-absolute (d)
  16336. "Return the time corresponding to date D.
  16337. D may be an absolute day number, or a calendar-type list (month day year)."
  16338. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16339. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16340. (defun org-calendar-holiday ()
  16341. "List of holidays, for Diary display in Org-mode."
  16342. (require 'holidays)
  16343. (let ((hl (funcall
  16344. (if (fboundp 'calendar-check-holidays)
  16345. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16346. (if hl (mapconcat 'identity hl "; "))))
  16347. (defun org-diary-sexp-entry (sexp entry date)
  16348. "Process a SEXP diary ENTRY for DATE."
  16349. (require 'diary-lib)
  16350. (let ((result (if calendar-debug-sexp
  16351. (let ((stack-trace-on-error t))
  16352. (eval (car (read-from-string sexp))))
  16353. (condition-case nil
  16354. (eval (car (read-from-string sexp)))
  16355. (error
  16356. (beep)
  16357. (message "Bad sexp at line %d in %s: %s"
  16358. (org-current-line)
  16359. (buffer-file-name) sexp)
  16360. (sleep-for 2))))))
  16361. (cond ((stringp result) result)
  16362. ((and (consp result)
  16363. (stringp (cdr result))) (cdr result))
  16364. (result entry)
  16365. (t nil))))
  16366. (defun org-diary-to-ical-string (frombuf)
  16367. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16368. This uses the icalendar.el library."
  16369. (let* ((tmpdir (if (featurep 'xemacs)
  16370. (temp-directory)
  16371. temporary-file-directory))
  16372. (tmpfile (make-temp-name
  16373. (expand-file-name "orgics" tmpdir)))
  16374. buf rtn b e)
  16375. (save-excursion
  16376. (set-buffer frombuf)
  16377. (icalendar-export-region (point-min) (point-max) tmpfile)
  16378. (setq buf (find-buffer-visiting tmpfile))
  16379. (set-buffer buf)
  16380. (goto-char (point-min))
  16381. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16382. (setq b (match-beginning 0)))
  16383. (goto-char (point-max))
  16384. (if (re-search-backward "^END:VEVENT" nil t)
  16385. (setq e (match-end 0)))
  16386. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16387. (kill-buffer buf)
  16388. (kill-buffer frombuf)
  16389. (delete-file tmpfile)
  16390. rtn))
  16391. (defun org-closest-date (start current change)
  16392. "Find the date closest to CURRENT that is consistent with START and CHANGE."
  16393. ;; Make the proper lists from the dates
  16394. (catch 'exit
  16395. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16396. dn dw sday cday n1 n2
  16397. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16398. (setq start (org-date-to-gregorian start)
  16399. current (org-date-to-gregorian
  16400. (if org-agenda-repeating-timestamp-show-all
  16401. current
  16402. (time-to-days (current-time))))
  16403. sday (calendar-absolute-from-gregorian start)
  16404. cday (calendar-absolute-from-gregorian current))
  16405. (if (<= cday sday) (throw 'exit sday))
  16406. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16407. (setq dn (string-to-number (match-string 1 change))
  16408. dw (cdr (assoc (match-string 2 change) a1)))
  16409. (error "Invalid change specifyer: %s" change))
  16410. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16411. (cond
  16412. ((eq dw 'day)
  16413. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16414. n2 (+ n1 dn)))
  16415. ((eq dw 'year)
  16416. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16417. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16418. (setq date1 (list m d y1)
  16419. n1 (calendar-absolute-from-gregorian date1)
  16420. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16421. n2 (calendar-absolute-from-gregorian date2)))
  16422. ((eq dw 'month)
  16423. ;; approx number of month between the tow dates
  16424. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16425. ;; How often does dn fit in there?
  16426. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16427. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16428. m (+ m nm)
  16429. ny (floor (/ m 12))
  16430. y (+ y ny)
  16431. m (- m (* ny 12)))
  16432. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16433. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16434. (setq m2 (+ m dn) y2 y)
  16435. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16436. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16437. (while (< n2 cday)
  16438. (setq n1 n2 m m2 y y2)
  16439. (setq m2 (+ m dn) y2 y)
  16440. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16441. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16442. (if org-agenda-repeating-timestamp-show-all
  16443. (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)
  16444. (if (= cday n1) n1 n2)))))
  16445. (defun org-date-to-gregorian (date)
  16446. "Turn any specification of DATE into a gregorian date for the calendar."
  16447. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16448. ((and (listp date) (= (length date) 3)) date)
  16449. ((stringp date)
  16450. (setq date (org-parse-time-string date))
  16451. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16452. ((listp date)
  16453. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16454. (defun org-parse-time-string (s &optional nodefault)
  16455. "Parse the standard Org-mode time string.
  16456. This should be a lot faster than the normal `parse-time-string'.
  16457. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16458. hour and minute fields will be nil if not given."
  16459. (if (string-match org-ts-regexp0 s)
  16460. (list 0
  16461. (if (or (match-beginning 8) (not nodefault))
  16462. (string-to-number (or (match-string 8 s) "0")))
  16463. (if (or (match-beginning 7) (not nodefault))
  16464. (string-to-number (or (match-string 7 s) "0")))
  16465. (string-to-number (match-string 4 s))
  16466. (string-to-number (match-string 3 s))
  16467. (string-to-number (match-string 2 s))
  16468. nil nil nil)
  16469. (make-list 9 0)))
  16470. (defun org-timestamp-up (&optional arg)
  16471. "Increase the date item at the cursor by one.
  16472. If the cursor is on the year, change the year. If it is on the month or
  16473. the day, change that.
  16474. With prefix ARG, change by that many units."
  16475. (interactive "p")
  16476. (org-timestamp-change (prefix-numeric-value arg)))
  16477. (defun org-timestamp-down (&optional arg)
  16478. "Decrease the date item at the cursor by one.
  16479. If the cursor is on the year, change the year. If it is on the month or
  16480. the day, change that.
  16481. With prefix ARG, change by that many units."
  16482. (interactive "p")
  16483. (org-timestamp-change (- (prefix-numeric-value arg))))
  16484. (defun org-timestamp-up-day (&optional arg)
  16485. "Increase the date in the time stamp by one day.
  16486. With prefix ARG, change that many days."
  16487. (interactive "p")
  16488. (if (and (not (org-at-timestamp-p t))
  16489. (org-on-heading-p))
  16490. (org-todo 'up)
  16491. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16492. (defun org-timestamp-down-day (&optional arg)
  16493. "Decrease the date in the time stamp by one day.
  16494. With prefix ARG, change that many days."
  16495. (interactive "p")
  16496. (if (and (not (org-at-timestamp-p t))
  16497. (org-on-heading-p))
  16498. (org-todo 'down)
  16499. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16500. (defsubst org-pos-in-match-range (pos n)
  16501. (and (match-beginning n)
  16502. (<= (match-beginning n) pos)
  16503. (>= (match-end n) pos)))
  16504. (defun org-at-timestamp-p (&optional inactive-ok)
  16505. "Determine if the cursor is in or at a timestamp."
  16506. (interactive)
  16507. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16508. (pos (point))
  16509. (ans (or (looking-at tsr)
  16510. (save-excursion
  16511. (skip-chars-backward "^[<\n\r\t")
  16512. (if (> (point) (point-min)) (backward-char 1))
  16513. (and (looking-at tsr)
  16514. (> (- (match-end 0) pos) -1))))))
  16515. (and ans
  16516. (boundp 'org-ts-what)
  16517. (setq org-ts-what
  16518. (cond
  16519. ((= pos (match-beginning 0)) 'bracket)
  16520. ((= pos (1- (match-end 0))) 'bracket)
  16521. ((org-pos-in-match-range pos 2) 'year)
  16522. ((org-pos-in-match-range pos 3) 'month)
  16523. ((org-pos-in-match-range pos 7) 'hour)
  16524. ((org-pos-in-match-range pos 8) 'minute)
  16525. ((or (org-pos-in-match-range pos 4)
  16526. (org-pos-in-match-range pos 5)) 'day)
  16527. ((and (> pos (or (match-end 8) (match-end 5)))
  16528. (< pos (match-end 0)))
  16529. (- pos (or (match-end 8) (match-end 5))))
  16530. (t 'day))))
  16531. ans))
  16532. (defun org-toggle-timestamp-type ()
  16533. ""
  16534. (interactive)
  16535. (when (org-at-timestamp-p t)
  16536. (save-excursion
  16537. (goto-char (match-beginning 0))
  16538. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16539. (goto-char (1- (match-end 0)))
  16540. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16541. (message "Timestamp is now %sactive"
  16542. (if (equal (char-before) ?>) "in" ""))))
  16543. (defun org-timestamp-change (n &optional what)
  16544. "Change the date in the time stamp at point.
  16545. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16546. `year', `minute', `second'. If WHAT is not given, the cursor position
  16547. in the timestamp determines what will be changed."
  16548. (let ((pos (point))
  16549. with-hm inactive
  16550. org-ts-what
  16551. extra
  16552. ts time time0)
  16553. (if (not (org-at-timestamp-p t))
  16554. (error "Not at a timestamp"))
  16555. (if (and (not what) (eq org-ts-what 'bracket))
  16556. (org-toggle-timestamp-type)
  16557. (if (and (not what) (not (eq org-ts-what 'day))
  16558. org-display-custom-times
  16559. (get-text-property (point) 'display)
  16560. (not (get-text-property (1- (point)) 'display)))
  16561. (setq org-ts-what 'day))
  16562. (setq org-ts-what (or what org-ts-what)
  16563. inactive (= (char-after (match-beginning 0)) ?\[)
  16564. ts (match-string 0))
  16565. (replace-match "")
  16566. (if (string-match
  16567. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  16568. ts)
  16569. (setq extra (match-string 1 ts)))
  16570. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16571. (setq with-hm t))
  16572. (setq time0 (org-parse-time-string ts))
  16573. (setq time
  16574. (encode-time (or (car time0) 0)
  16575. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16576. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16577. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16578. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16579. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16580. (nthcdr 6 time0)))
  16581. (when (integerp org-ts-what)
  16582. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  16583. (if (eq what 'calendar)
  16584. (let ((cal-date (org-get-date-from-calendar)))
  16585. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16586. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16587. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16588. (setcar time0 (or (car time0) 0))
  16589. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16590. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16591. (setq time (apply 'encode-time time0))))
  16592. (setq org-last-changed-timestamp
  16593. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16594. (org-clock-update-time-maybe)
  16595. (goto-char pos)
  16596. ;; Try to recenter the calendar window, if any
  16597. (if (and org-calendar-follow-timestamp-change
  16598. (get-buffer-window "*Calendar*" t)
  16599. (memq org-ts-what '(day month year)))
  16600. (org-recenter-calendar (time-to-days time))))))
  16601. ;; FIXME: does not yet work for lead times
  16602. (defun org-modify-ts-extra (s pos n)
  16603. "Change the different parts of the lead-time and repeat fields in timestamp."
  16604. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16605. ng h m new)
  16606. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16607. (cond
  16608. ((or (org-pos-in-match-range pos 2)
  16609. (org-pos-in-match-range pos 3))
  16610. (setq m (string-to-number (match-string 3 s))
  16611. h (string-to-number (match-string 2 s)))
  16612. (if (org-pos-in-match-range pos 2)
  16613. (setq h (+ h n))
  16614. (setq m (+ m n)))
  16615. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16616. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16617. (setq h (min 24 (max 0 h)))
  16618. (setq ng 1 new (format "-%02d:%02d" h m)))
  16619. ((org-pos-in-match-range pos 6)
  16620. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16621. ((org-pos-in-match-range pos 5)
  16622. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  16623. (when ng
  16624. (setq s (concat
  16625. (substring s 0 (match-beginning ng))
  16626. new
  16627. (substring s (match-end ng))))))
  16628. s))
  16629. (defun org-recenter-calendar (date)
  16630. "If the calendar is visible, recenter it to DATE."
  16631. (let* ((win (selected-window))
  16632. (cwin (get-buffer-window "*Calendar*" t))
  16633. (calendar-move-hook nil))
  16634. (when cwin
  16635. (select-window cwin)
  16636. (calendar-goto-date (if (listp date) date
  16637. (calendar-gregorian-from-absolute date)))
  16638. (select-window win))))
  16639. (defun org-goto-calendar (&optional arg)
  16640. "Go to the Emacs calendar at the current date.
  16641. If there is a time stamp in the current line, go to that date.
  16642. A prefix ARG can be used to force the current date."
  16643. (interactive "P")
  16644. (let ((tsr org-ts-regexp) diff
  16645. (calendar-move-hook nil)
  16646. (view-calendar-holidays-initially nil)
  16647. (view-diary-entries-initially nil))
  16648. (if (or (org-at-timestamp-p)
  16649. (save-excursion
  16650. (beginning-of-line 1)
  16651. (looking-at (concat ".*" tsr))))
  16652. (let ((d1 (time-to-days (current-time)))
  16653. (d2 (time-to-days
  16654. (org-time-string-to-time (match-string 1)))))
  16655. (setq diff (- d2 d1))))
  16656. (calendar)
  16657. (calendar-goto-today)
  16658. (if (and diff (not arg)) (calendar-forward-day diff))))
  16659. (defun org-get-date-from-calendar ()
  16660. "Return a list (month day year) of date at point in calendar."
  16661. (with-current-buffer "*Calendar*"
  16662. (save-match-data
  16663. (calendar-cursor-to-date))))
  16664. (defun org-date-from-calendar ()
  16665. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16666. If there is already a time stamp at the cursor position, update it."
  16667. (interactive)
  16668. (if (org-at-timestamp-p t)
  16669. (org-timestamp-change 0 'calendar)
  16670. (let ((cal-date (org-get-date-from-calendar)))
  16671. (org-insert-time-stamp
  16672. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16673. ;; Make appt aware of appointments from the agenda
  16674. ;;;###autoload
  16675. (defun org-agenda-to-appt (&optional filter)
  16676. "Activate appointments found in `org-agenda-files'.
  16677. When prefixed, prompt for a regular expression and use it as a
  16678. filter: only add entries if they match this regular expression.
  16679. FILTER can be a string. In this case, use this string as a
  16680. regular expression to filter results.
  16681. FILTER can also be an alist, with the car of each cell being
  16682. either 'headline or 'category. For example:
  16683. '((headline \"IMPORTANT\")
  16684. (category \"Work\"))
  16685. will only add headlines containing IMPORTANT or headlines
  16686. belonging to the category \"Work\"."
  16687. (interactive "P")
  16688. (require 'calendar)
  16689. (if (equal filter '(4))
  16690. (setq filter (read-from-minibuffer "Regexp filter: ")))
  16691. (let* ((cnt 0) ; count added events
  16692. (org-agenda-new-buffers nil)
  16693. (today (org-date-to-gregorian
  16694. (time-to-days (current-time))))
  16695. (files (org-agenda-files)) entries file)
  16696. ;; Get all entries which may contain an appt
  16697. (while (setq file (pop files))
  16698. (setq entries
  16699. (append entries
  16700. (org-agenda-get-day-entries
  16701. file today
  16702. :timestamp :scheduled :deadline))))
  16703. (setq entries (delq nil entries))
  16704. ;; Map thru entries and find if they pass thru the filter
  16705. (mapc
  16706. (lambda(x)
  16707. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  16708. (cat (get-text-property 1 'org-category x))
  16709. (tod (get-text-property 1 'time-of-day x))
  16710. (ok (or (null filter)
  16711. (and (stringp filter) (string-match filter evt))
  16712. (and (listp filter)
  16713. (or (string-match
  16714. (cadr (assoc 'category filter)) cat)
  16715. (string-match
  16716. (cadr (assoc 'headline filter)) evt))))))
  16717. ;; FIXME: Shall we remove text-properties for the appt text?
  16718. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  16719. (when (and ok tod)
  16720. (setq tod (number-to-string tod)
  16721. tod (when (string-match
  16722. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  16723. (concat (match-string 1 tod) ":"
  16724. (match-string 2 tod))))
  16725. (appt-add tod evt)
  16726. (setq cnt (1+ cnt))))) entries)
  16727. (org-release-buffers org-agenda-new-buffers)
  16728. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" ""))))
  16729. ;;; The clock for measuring work time.
  16730. (defvar org-mode-line-string "")
  16731. (put 'org-mode-line-string 'risky-local-variable t)
  16732. (defvar org-mode-line-timer nil)
  16733. (defvar org-clock-heading "")
  16734. (defvar org-clock-start-time "")
  16735. (defun org-update-mode-line ()
  16736. (let* ((delta (- (time-to-seconds (current-time))
  16737. (time-to-seconds org-clock-start-time)))
  16738. (h (floor delta 3600))
  16739. (m (floor (- delta (* 3600 h)) 60)))
  16740. (setq org-mode-line-string
  16741. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  16742. 'help-echo "Org-mode clock is running"))
  16743. (force-mode-line-update)))
  16744. (defvar org-clock-marker (make-marker)
  16745. "Marker recording the last clock-in.")
  16746. (defvar org-clock-mode-line-entry nil
  16747. "Information for the modeline about the running clock.")
  16748. (defun org-clock-in ()
  16749. "Start the clock on the current item.
  16750. If necessary, clock-out of the currently active clock."
  16751. (interactive)
  16752. (org-clock-out t)
  16753. (let (ts)
  16754. (save-excursion
  16755. (org-back-to-heading t)
  16756. (when (and org-clock-in-switch-to-state
  16757. (not (looking-at (concat outline-regexp "[ \t]*"
  16758. org-clock-in-switch-to-state
  16759. "\\>"))))
  16760. (org-todo org-clock-in-switch-to-state))
  16761. (if (and org-clock-heading-function
  16762. (functionp org-clock-heading-function))
  16763. (setq org-clock-heading (funcall org-clock-heading-function))
  16764. (if (looking-at org-complex-heading-regexp)
  16765. (setq org-clock-heading (match-string 4))
  16766. (setq org-clock-heading "???")))
  16767. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  16768. (org-clock-find-position)
  16769. (insert "\n") (backward-char 1)
  16770. (indent-relative)
  16771. (insert org-clock-string " ")
  16772. (setq org-clock-start-time (current-time))
  16773. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  16774. (move-marker org-clock-marker (point) (buffer-base-buffer))
  16775. (or global-mode-string (setq global-mode-string '("")))
  16776. (or (memq 'org-mode-line-string global-mode-string)
  16777. (setq global-mode-string
  16778. (append global-mode-string '(org-mode-line-string))))
  16779. (org-update-mode-line)
  16780. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  16781. (message "Clock started at %s" ts))))
  16782. (defun org-clock-find-position ()
  16783. "Find the location where the next clock line should be inserted."
  16784. (org-back-to-heading t)
  16785. (catch 'exit
  16786. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  16787. (re (concat "^[ \t]*" org-clock-string))
  16788. (cnt 0)
  16789. first last)
  16790. (goto-char beg)
  16791. (when (eobp) (newline) (setq end (max (point) end)))
  16792. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  16793. ;; we seem to have a CLOCK drawer, so go there.
  16794. (beginning-of-line 2)
  16795. (throw 'exit t))
  16796. ;; Lets count the CLOCK lines
  16797. (goto-char beg)
  16798. (while (re-search-forward re end t)
  16799. (setq first (or first (match-beginning 0))
  16800. last (match-beginning 0)
  16801. cnt (1+ cnt)))
  16802. (when (and (integerp org-clock-into-drawer)
  16803. (>= (1+ cnt) org-clock-into-drawer))
  16804. ;; Wrap current entries into a new drawer
  16805. (goto-char last)
  16806. (beginning-of-line 2)
  16807. (if (org-at-item-p) (org-end-of-item))
  16808. (insert ":END:\n")
  16809. (beginning-of-line 0)
  16810. (org-indent-line-function)
  16811. (goto-char first)
  16812. (insert ":CLOCK:\n")
  16813. (beginning-of-line 0)
  16814. (org-indent-line-function)
  16815. (org-flag-drawer t)
  16816. (beginning-of-line 2)
  16817. (throw 'exit nil))
  16818. (goto-char beg)
  16819. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  16820. (not (equal (match-string 1) org-clock-string)))
  16821. ;; Planning info, skip to after it
  16822. (beginning-of-line 2)
  16823. (or (bolp) (newline)))
  16824. (when (eq t org-clock-into-drawer)
  16825. (insert ":CLOCK:\n:END:\n")
  16826. (beginning-of-line -1)
  16827. (org-indent-line-function)
  16828. (org-flag-drawer t)
  16829. (beginning-of-line 2)
  16830. (org-indent-line-function)))))
  16831. (defun org-clock-out (&optional fail-quietly)
  16832. "Stop the currently running clock.
  16833. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  16834. (interactive)
  16835. (catch 'exit
  16836. (if (not (marker-buffer org-clock-marker))
  16837. (if fail-quietly (throw 'exit t) (error "No active clock")))
  16838. (let (ts te s h m)
  16839. (save-excursion
  16840. (set-buffer (marker-buffer org-clock-marker))
  16841. (goto-char org-clock-marker)
  16842. (beginning-of-line 1)
  16843. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  16844. (equal (match-string 1) org-clock-string))
  16845. (setq ts (match-string 2))
  16846. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  16847. (goto-char (match-end 0))
  16848. (delete-region (point) (point-at-eol))
  16849. (insert "--")
  16850. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  16851. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  16852. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  16853. h (floor (/ s 3600))
  16854. s (- s (* 3600 h))
  16855. m (floor (/ s 60))
  16856. s (- s (* 60 s)))
  16857. (insert " => " (format "%2d:%02d" h m))
  16858. (move-marker org-clock-marker nil)
  16859. (let* ((logging (save-match-data (org-entry-get nil "LOGGING" t)))
  16860. (org-log-done (org-parse-local-options logging 'org-log-done))
  16861. (org-log-repeat (org-parse-local-options logging 'org-log-repeat)))
  16862. (org-add-log-maybe 'clock-out))
  16863. (when org-mode-line-timer
  16864. (cancel-timer org-mode-line-timer)
  16865. (setq org-mode-line-timer nil))
  16866. (setq global-mode-string
  16867. (delq 'org-mode-line-string global-mode-string))
  16868. (force-mode-line-update)
  16869. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  16870. (defun org-clock-cancel ()
  16871. "Cancel the running clock be removing the start timestamp."
  16872. (interactive)
  16873. (if (not (marker-buffer org-clock-marker))
  16874. (error "No active clock"))
  16875. (save-excursion
  16876. (set-buffer (marker-buffer org-clock-marker))
  16877. (goto-char org-clock-marker)
  16878. (delete-region (1- (point-at-bol)) (point-at-eol)))
  16879. (setq global-mode-string
  16880. (delq 'org-mode-line-string global-mode-string))
  16881. (force-mode-line-update)
  16882. (message "Clock canceled"))
  16883. (defun org-clock-goto (&optional delete-windows)
  16884. "Go to the currently clocked-in entry."
  16885. (interactive "P")
  16886. (if (not (marker-buffer org-clock-marker))
  16887. (error "No active clock"))
  16888. (switch-to-buffer-other-window
  16889. (marker-buffer org-clock-marker))
  16890. (if delete-windows (delete-other-windows))
  16891. (goto-char org-clock-marker)
  16892. (org-show-entry)
  16893. (org-back-to-heading)
  16894. (recenter))
  16895. (defvar org-clock-file-total-minutes nil
  16896. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  16897. (make-variable-buffer-local 'org-clock-file-total-minutes)
  16898. (defun org-clock-sum (&optional tstart tend)
  16899. "Sum the times for each subtree.
  16900. Puts the resulting times in minutes as a text property on each headline."
  16901. (interactive)
  16902. (let* ((bmp (buffer-modified-p))
  16903. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  16904. org-clock-string
  16905. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  16906. (lmax 30)
  16907. (ltimes (make-vector lmax 0))
  16908. (t1 0)
  16909. (level 0)
  16910. ts te dt
  16911. time)
  16912. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  16913. (save-excursion
  16914. (goto-char (point-max))
  16915. (while (re-search-backward re nil t)
  16916. (cond
  16917. ((match-end 2)
  16918. ;; Two time stamps
  16919. (setq ts (match-string 2)
  16920. te (match-string 3)
  16921. ts (time-to-seconds
  16922. (apply 'encode-time (org-parse-time-string ts)))
  16923. te (time-to-seconds
  16924. (apply 'encode-time (org-parse-time-string te)))
  16925. ts (if tstart (max ts tstart) ts)
  16926. te (if tend (min te tend) te)
  16927. dt (- te ts)
  16928. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  16929. ((match-end 4)
  16930. ;; A naket time
  16931. (setq t1 (+ t1 (string-to-number (match-string 5))
  16932. (* 60 (string-to-number (match-string 4))))))
  16933. (t ;; A headline
  16934. (setq level (- (match-end 1) (match-beginning 1)))
  16935. (when (or (> t1 0) (> (aref ltimes level) 0))
  16936. (loop for l from 0 to level do
  16937. (aset ltimes l (+ (aref ltimes l) t1)))
  16938. (setq t1 0 time (aref ltimes level))
  16939. (loop for l from level to (1- lmax) do
  16940. (aset ltimes l 0))
  16941. (goto-char (match-beginning 0))
  16942. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  16943. (setq org-clock-file-total-minutes (aref ltimes 0)))
  16944. (set-buffer-modified-p bmp)))
  16945. (defun org-clock-display (&optional total-only)
  16946. "Show subtree times in the entire buffer.
  16947. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  16948. in the echo area."
  16949. (interactive)
  16950. (org-remove-clock-overlays)
  16951. (let (time h m p)
  16952. (org-clock-sum)
  16953. (unless total-only
  16954. (save-excursion
  16955. (goto-char (point-min))
  16956. (while (or (and (equal (setq p (point)) (point-min))
  16957. (get-text-property p :org-clock-minutes))
  16958. (setq p (next-single-property-change
  16959. (point) :org-clock-minutes)))
  16960. (goto-char p)
  16961. (when (setq time (get-text-property p :org-clock-minutes))
  16962. (org-put-clock-overlay time (funcall outline-level))))
  16963. (setq h (/ org-clock-file-total-minutes 60)
  16964. m (- org-clock-file-total-minutes (* 60 h)))
  16965. ;; Arrange to remove the overlays upon next change.
  16966. (when org-remove-highlights-with-change
  16967. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  16968. nil 'local))))
  16969. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  16970. (defvar org-clock-overlays nil)
  16971. (make-variable-buffer-local 'org-clock-overlays)
  16972. (defun org-put-clock-overlay (time &optional level)
  16973. "Put an overlays on the current line, displaying TIME.
  16974. If LEVEL is given, prefix time with a corresponding number of stars.
  16975. This creates a new overlay and stores it in `org-clock-overlays', so that it
  16976. will be easy to remove."
  16977. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  16978. (l (if level (org-get-legal-level level 0) 0))
  16979. (off 0)
  16980. ov tx)
  16981. (move-to-column c)
  16982. (unless (eolp) (skip-chars-backward "^ \t"))
  16983. (skip-chars-backward " \t")
  16984. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  16985. tx (concat (buffer-substring (1- (point)) (point))
  16986. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  16987. (org-add-props (format "%s %2d:%02d%s"
  16988. (make-string l ?*) h m
  16989. (make-string (- 10 l) ?\ ))
  16990. '(face secondary-selection))
  16991. ""))
  16992. (if (not (featurep 'xemacs))
  16993. (org-overlay-put ov 'display tx)
  16994. (org-overlay-put ov 'invisible t)
  16995. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  16996. (push ov org-clock-overlays)))
  16997. (defun org-remove-clock-overlays (&optional beg end noremove)
  16998. "Remove the occur highlights from the buffer.
  16999. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17000. from the `before-change-functions' in the current buffer."
  17001. (interactive)
  17002. (unless org-inhibit-highlight-removal
  17003. (mapc 'org-delete-overlay org-clock-overlays)
  17004. (setq org-clock-overlays nil)
  17005. (unless noremove
  17006. (remove-hook 'before-change-functions
  17007. 'org-remove-clock-overlays 'local))))
  17008. (defun org-clock-out-if-current ()
  17009. "Clock out if the current entry contains the running clock.
  17010. This is used to stop the clock after a TODO entry is marked DONE,
  17011. and is only done if the variable `org-clock-out-when-done' is not nil."
  17012. (when (and org-clock-out-when-done
  17013. (member state org-done-keywords)
  17014. (equal (marker-buffer org-clock-marker) (current-buffer))
  17015. (< (point) org-clock-marker)
  17016. (> (save-excursion (outline-next-heading) (point))
  17017. org-clock-marker))
  17018. ;; Clock out, but don't accept a logging message for this.
  17019. (let ((org-log-done (if (and (listp org-log-done)
  17020. (member 'clock-out org-log-done))
  17021. '(done)
  17022. org-log-done)))
  17023. (org-clock-out))))
  17024. (add-hook 'org-after-todo-state-change-hook
  17025. 'org-clock-out-if-current)
  17026. (defun org-check-running-clock ()
  17027. "Check if the current buffer contains the running clock.
  17028. If yes, offer to stop it and to save the buffer with the changes."
  17029. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17030. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17031. (buffer-name))))
  17032. (org-clock-out)
  17033. (when (y-or-n-p "Save changed buffer?")
  17034. (save-buffer))))
  17035. (defun org-clock-report (&optional arg)
  17036. "Create a table containing a report about clocked time.
  17037. If the cursor is inside an existing clocktable block, then the table
  17038. will be updated. If not, a new clocktable will be inserted.
  17039. When called with a prefix argument, move to the first clock table in the
  17040. buffer and update it."
  17041. (interactive "P")
  17042. (org-remove-clock-overlays)
  17043. (when arg (org-find-dblock "clocktable"))
  17044. (if (org-in-clocktable-p)
  17045. (goto-char (org-in-clocktable-p))
  17046. (org-create-dblock (list :name "clocktable"
  17047. :maxlevel 2 :scope 'file)))
  17048. (org-update-dblock))
  17049. (defun org-in-clocktable-p ()
  17050. "Check if the cursor is in a clocktable."
  17051. (let ((pos (point)) start)
  17052. (save-excursion
  17053. (end-of-line 1)
  17054. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17055. (setq start (match-beginning 0))
  17056. (re-search-forward "^#\\+END:.*" nil t)
  17057. (>= (match-end 0) pos)
  17058. start))))
  17059. (defun org-clock-update-time-maybe ()
  17060. "If this is a CLOCK line, update it and return t.
  17061. Otherwise, return nil."
  17062. (interactive)
  17063. (save-excursion
  17064. (beginning-of-line 1)
  17065. (skip-chars-forward " \t")
  17066. (when (looking-at org-clock-string)
  17067. (let ((re (concat "[ \t]*" org-clock-string
  17068. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17069. "\\([ \t]*=>.*\\)?"))
  17070. ts te h m s)
  17071. (if (not (looking-at re))
  17072. nil
  17073. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17074. (end-of-line 1)
  17075. (setq ts (match-string 1)
  17076. te (match-string 2))
  17077. (setq s (- (time-to-seconds
  17078. (apply 'encode-time (org-parse-time-string te)))
  17079. (time-to-seconds
  17080. (apply 'encode-time (org-parse-time-string ts))))
  17081. h (floor (/ s 3600))
  17082. s (- s (* 3600 h))
  17083. m (floor (/ s 60))
  17084. s (- s (* 60 s)))
  17085. (insert " => " (format "%2d:%02d" h m))
  17086. t)))))
  17087. (defun org-clock-special-range (key &optional time as-strings)
  17088. "Return two times bordering a special time range.
  17089. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17090. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17091. A week starts Monday 0:00 and ends Sunday 24:00.
  17092. The range is determined relative to TIME. TIME defaults to the current time.
  17093. The return value is a cons cell with two internal times like the ones
  17094. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17095. the returned times will be formatted strings."
  17096. (let* ((tm (decode-time (or time (current-time))))
  17097. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17098. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17099. (dow (nth 6 tm))
  17100. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17101. (cond
  17102. ((eq key 'today)
  17103. (setq h 0 m 0 h1 24 m1 0))
  17104. ((eq key 'yesterday)
  17105. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17106. ((eq key 'thisweek)
  17107. (setq diff (if (= dow 0) 6 (1- dow))
  17108. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17109. ((eq key 'lastweek)
  17110. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17111. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17112. ((eq key 'thismonth)
  17113. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17114. ((eq key 'lastmonth)
  17115. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17116. ((eq key 'thisyear)
  17117. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17118. ((eq key 'lastyear)
  17119. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17120. (t (error "No such time block %s" key)))
  17121. (setq ts (encode-time s m h d month y)
  17122. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17123. (or d1 d) (or month1 month) (or y1 y)))
  17124. (setq fm (cdr org-time-stamp-formats))
  17125. (if as-strings
  17126. (cons (format-time-string fm ts) (format-time-string fm te))
  17127. (cons ts te))))
  17128. (defun org-dblock-write:clocktable (params)
  17129. "Write the standard clocktable."
  17130. (let ((hlchars '((1 . "*") (2 . "/")))
  17131. (emph nil)
  17132. (ins (make-marker))
  17133. (total-time nil)
  17134. ipos time h m p level hlc hdl maxlevel
  17135. ts te cc block beg end pos scope tbl tostring multifile)
  17136. (setq scope (plist-get params :scope)
  17137. tostring (plist-get params :tostring)
  17138. multifile (plist-get params :multifile)
  17139. maxlevel (or (plist-get params :maxlevel) 3)
  17140. emph (plist-get params :emphasize)
  17141. ts (plist-get params :tstart)
  17142. te (plist-get params :tend)
  17143. block (plist-get params :block))
  17144. (when block
  17145. (setq cc (org-clock-special-range block nil t)
  17146. ts (car cc) te (cdr cc)))
  17147. (if ts (setq ts (time-to-seconds
  17148. (apply 'encode-time (org-parse-time-string ts)))))
  17149. (if te (setq te (time-to-seconds
  17150. (apply 'encode-time (org-parse-time-string te)))))
  17151. (move-marker ins (point))
  17152. (setq ipos (point))
  17153. ;; Get the right scope
  17154. (setq pos (point))
  17155. (save-restriction
  17156. (cond
  17157. ((not scope))
  17158. ((eq scope 'file) (widen))
  17159. ((eq scope 'subtree) (org-narrow-to-subtree))
  17160. ((eq scope 'tree)
  17161. (while (org-up-heading-safe))
  17162. (org-narrow-to-subtree))
  17163. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17164. (symbol-name scope)))
  17165. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17166. (catch 'exit
  17167. (while (org-up-heading-safe)
  17168. (looking-at outline-regexp)
  17169. (if (<= (org-reduced-level (funcall outline-level)) level)
  17170. (throw 'exit nil))))
  17171. (org-narrow-to-subtree))
  17172. ((or (listp scope) (eq scope 'agenda))
  17173. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17174. (scope 'agenda)
  17175. (p1 (copy-sequence params))
  17176. file)
  17177. (plist-put p1 :tostring t)
  17178. (plist-put p1 :multifile t)
  17179. (plist-put p1 :scope 'file)
  17180. (org-prepare-agenda-buffers files)
  17181. (while (setq file (pop files))
  17182. (with-current-buffer (find-buffer-visiting file)
  17183. (push (org-clocktable-add-file
  17184. file (org-dblock-write:clocktable p1)) tbl)
  17185. (setq total-time (+ (or total-time 0)
  17186. org-clock-file-total-minutes)))))))
  17187. (goto-char pos)
  17188. (unless (eq scope 'agenda)
  17189. (org-clock-sum ts te)
  17190. (goto-char (point-min))
  17191. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17192. (goto-char p)
  17193. (when (setq time (get-text-property p :org-clock-minutes))
  17194. (save-excursion
  17195. (beginning-of-line 1)
  17196. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17197. (setq level (org-reduced-level
  17198. (- (match-end 1) (match-beginning 1))))
  17199. (<= level maxlevel))
  17200. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17201. hdl (match-string 2)
  17202. h (/ time 60)
  17203. m (- time (* 60 h)))
  17204. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17205. (push (concat
  17206. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17207. (make-string (1- level) ?|)
  17208. hlc (format "%d:%02d" h m) hlc
  17209. " |") tbl))))))
  17210. (setq tbl (nreverse tbl))
  17211. (if tostring
  17212. (if tbl (mapconcat 'identity tbl "\n") nil)
  17213. (goto-char ins)
  17214. (insert-before-markers
  17215. "Clock summary at ["
  17216. (substring
  17217. (format-time-string (cdr org-time-stamp-formats))
  17218. 1 -1)
  17219. "]."
  17220. (if block
  17221. (format " Considered range is /%s/." block)
  17222. "")
  17223. "\n\n"
  17224. (if (eq scope 'agenda) "|File" "")
  17225. "|L|Headline|Time|\n")
  17226. (setq total-time (or total-time org-clock-file-total-minutes)
  17227. h (/ total-time 60)
  17228. m (- total-time (* 60 h)))
  17229. (insert-before-markers
  17230. "|-\n|"
  17231. (if (eq scope 'agenda) "|" "")
  17232. "|"
  17233. "*Total time*| "
  17234. (format "*%d:%02d*" h m)
  17235. "|\n|-\n")
  17236. (setq tbl (delq nil tbl))
  17237. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17238. (equal (substring (car tbl) 0 2) "|-"))
  17239. (pop tbl))
  17240. (insert-before-markers (mapconcat
  17241. 'identity (delq nil tbl)
  17242. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17243. (backward-delete-char 1)
  17244. (goto-char ipos)
  17245. (skip-chars-forward "^|")
  17246. (org-table-align)))))
  17247. (defun org-clocktable-add-file (file table)
  17248. (if table
  17249. (let ((lines (org-split-string table "\n"))
  17250. (ff (file-name-nondirectory file)))
  17251. (mapconcat 'identity
  17252. (mapcar (lambda (x)
  17253. (if (string-match org-table-dataline-regexp x)
  17254. (concat "|" ff x)
  17255. x))
  17256. lines)
  17257. "\n"))))
  17258. ;; FIXME: I don't think anybody uses this, ask David
  17259. (defun org-collect-clock-time-entries ()
  17260. "Return an internal list with clocking information.
  17261. This list has one entry for each CLOCK interval.
  17262. FIXME: describe the elements."
  17263. (interactive)
  17264. (let ((re (concat "^[ \t]*" org-clock-string
  17265. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17266. rtn beg end next cont level title total closedp leafp
  17267. clockpos titlepos h m donep)
  17268. (save-excursion
  17269. (org-clock-sum)
  17270. (goto-char (point-min))
  17271. (while (re-search-forward re nil t)
  17272. (setq clockpos (match-beginning 0)
  17273. beg (match-string 1) end (match-string 2)
  17274. cont (match-end 0))
  17275. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17276. end (apply 'encode-time (org-parse-time-string end)))
  17277. (org-back-to-heading t)
  17278. (setq donep (org-entry-is-done-p))
  17279. (setq titlepos (point)
  17280. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17281. h (/ total 60) m (- total (* 60 h))
  17282. total (cons h m))
  17283. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17284. (setq level (- (match-end 1) (match-beginning 1))
  17285. title (org-match-string-no-properties 2))
  17286. (save-excursion (outline-next-heading) (setq next (point)))
  17287. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17288. (goto-char next)
  17289. (setq leafp (and (looking-at "^\\*+ ")
  17290. (<= (- (match-end 0) (point)) level)))
  17291. (push (list beg end clockpos closedp donep
  17292. total title titlepos level leafp)
  17293. rtn)
  17294. (goto-char cont)))
  17295. (nreverse rtn)))
  17296. ;;;; Agenda, and Diary Integration
  17297. ;;; Define the Org-agenda-mode
  17298. (defvar org-agenda-mode-map (make-sparse-keymap)
  17299. "Keymap for `org-agenda-mode'.")
  17300. (defvar org-agenda-menu) ; defined later in this file.
  17301. (defvar org-agenda-follow-mode nil)
  17302. (defvar org-agenda-show-log nil)
  17303. (defvar org-agenda-redo-command nil)
  17304. (defvar org-agenda-mode-hook nil)
  17305. (defvar org-agenda-type nil)
  17306. (defvar org-agenda-force-single-file nil)
  17307. (defun org-agenda-mode ()
  17308. "Mode for time-sorted view on action items in Org-mode files.
  17309. The following commands are available:
  17310. \\{org-agenda-mode-map}"
  17311. (interactive)
  17312. (kill-all-local-variables)
  17313. (setq org-agenda-undo-list nil
  17314. org-agenda-pending-undo-list nil)
  17315. (setq major-mode 'org-agenda-mode)
  17316. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17317. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17318. (setq mode-name "Org-Agenda")
  17319. (use-local-map org-agenda-mode-map)
  17320. (easy-menu-add org-agenda-menu)
  17321. (if org-startup-truncated (setq truncate-lines t))
  17322. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17323. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17324. ;; Make sure properties are removed when copying text
  17325. (when (boundp 'buffer-substring-filters)
  17326. (org-set-local 'buffer-substring-filters
  17327. (cons (lambda (x)
  17328. (set-text-properties 0 (length x) nil x) x)
  17329. buffer-substring-filters)))
  17330. (unless org-agenda-keep-modes
  17331. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17332. org-agenda-show-log nil))
  17333. (easy-menu-change
  17334. '("Agenda") "Agenda Files"
  17335. (append
  17336. (list
  17337. (vector
  17338. (if (get 'org-agenda-files 'org-restrict)
  17339. "Restricted to single file"
  17340. "Edit File List")
  17341. '(org-edit-agenda-file-list)
  17342. (not (get 'org-agenda-files 'org-restrict)))
  17343. "--")
  17344. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17345. (org-agenda-set-mode-name)
  17346. (apply
  17347. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17348. (list 'org-agenda-mode-hook)))
  17349. (substitute-key-definition 'undo 'org-agenda-undo
  17350. org-agenda-mode-map global-map)
  17351. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17352. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17353. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17354. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17355. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17356. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17357. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17358. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17359. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17360. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17361. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17362. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17363. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17364. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17365. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17366. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17367. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17368. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17369. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17370. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17371. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17372. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17373. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17374. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17375. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17376. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17377. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17378. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17379. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17380. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17381. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17382. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17383. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17384. (while l (org-defkey org-agenda-mode-map
  17385. (int-to-string (pop l)) 'digit-argument)))
  17386. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17387. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17388. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17389. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17390. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17391. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17392. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17393. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17394. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17395. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17396. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17397. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17398. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17399. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17400. (org-defkey org-agenda-mode-map "n" 'next-line)
  17401. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17402. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17403. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17404. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17405. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17406. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17407. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17408. (eval-after-load "calendar"
  17409. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17410. 'org-calendar-goto-agenda))
  17411. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17412. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17413. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17414. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17415. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17416. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17417. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17418. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17419. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17420. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17421. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17422. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17423. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17424. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17425. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17426. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17427. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17428. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17429. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17430. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17431. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17432. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17433. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17434. "Local keymap for agenda entries from Org-mode.")
  17435. (org-defkey org-agenda-keymap
  17436. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17437. (org-defkey org-agenda-keymap
  17438. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17439. (when org-agenda-mouse-1-follows-link
  17440. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17441. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17442. '("Agenda"
  17443. ("Agenda Files")
  17444. "--"
  17445. ["Show" org-agenda-show t]
  17446. ["Go To (other window)" org-agenda-goto t]
  17447. ["Go To (this window)" org-agenda-switch-to t]
  17448. ["Follow Mode" org-agenda-follow-mode
  17449. :style toggle :selected org-agenda-follow-mode :active t]
  17450. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17451. "--"
  17452. ["Cycle TODO" org-agenda-todo t]
  17453. ["Archive subtree" org-agenda-archive t]
  17454. ["Delete subtree" org-agenda-kill t]
  17455. "--"
  17456. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17457. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17458. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17459. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17460. "--"
  17461. ("Tags and Properties"
  17462. ["Show all Tags" org-agenda-show-tags t]
  17463. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17464. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17465. "--"
  17466. ["Column View" org-columns t])
  17467. ("Date/Schedule"
  17468. ["Schedule" org-agenda-schedule t]
  17469. ["Set Deadline" org-agenda-deadline t]
  17470. "--"
  17471. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17472. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17473. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17474. ("Clock"
  17475. ["Clock in" org-agenda-clock-in t]
  17476. ["Clock out" org-agenda-clock-out t]
  17477. ["Clock cancel" org-agenda-clock-cancel t]
  17478. ["Goto running clock" org-clock-goto t])
  17479. ("Priority"
  17480. ["Set Priority" org-agenda-priority t]
  17481. ["Increase Priority" org-agenda-priority-up t]
  17482. ["Decrease Priority" org-agenda-priority-down t]
  17483. ["Show Priority" org-agenda-show-priority t])
  17484. ("Calendar/Diary"
  17485. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17486. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17487. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17488. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17489. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17490. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17491. "--"
  17492. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17493. "--"
  17494. ("View"
  17495. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17496. :style radio :selected (equal org-agenda-ndays 1)]
  17497. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17498. :style radio :selected (equal org-agenda-ndays 7)]
  17499. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17500. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17501. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17502. :style radio :selected (member org-agenda-ndays '(365 366))]
  17503. "--"
  17504. ["Show Logbook entries" org-agenda-log-mode
  17505. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17506. ["Include Diary" org-agenda-toggle-diary
  17507. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17508. ["Use Time Grid" org-agenda-toggle-time-grid
  17509. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17510. ["Write view to file" org-write-agenda t]
  17511. ["Rebuild buffer" org-agenda-redo t]
  17512. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  17513. "--"
  17514. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  17515. "--"
  17516. ["Quit" org-agenda-quit t]
  17517. ["Exit and Release Buffers" org-agenda-exit t]
  17518. ))
  17519. ;;; Agenda undo
  17520. (defvar org-agenda-allow-remote-undo t
  17521. "Non-nil means, allow remote undo from the agenda buffer.")
  17522. (defvar org-agenda-undo-list nil
  17523. "List of undoable operations in the agenda since last refresh.")
  17524. (defvar org-agenda-undo-has-started-in nil
  17525. "Buffers that have already seen `undo-start' in the current undo sequence.")
  17526. (defvar org-agenda-pending-undo-list nil
  17527. "In a series of undo commands, this is the list of remaning undo items.")
  17528. (defmacro org-if-unprotected (&rest body)
  17529. "Execute BODY if there is no `org-protected' text property at point."
  17530. (declare (debug t))
  17531. `(unless (get-text-property (point) 'org-protected)
  17532. ,@body))
  17533. (defmacro org-with-remote-undo (_buffer &rest _body)
  17534. "Execute BODY while recording undo information in two buffers."
  17535. (declare (indent 1) (debug t))
  17536. `(let ((_cline (org-current-line))
  17537. (_cmd this-command)
  17538. (_buf1 (current-buffer))
  17539. (_buf2 ,_buffer)
  17540. (_undo1 buffer-undo-list)
  17541. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  17542. _c1 _c2)
  17543. ,@_body
  17544. (when org-agenda-allow-remote-undo
  17545. (setq _c1 (org-verify-change-for-undo
  17546. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  17547. _c2 (org-verify-change-for-undo
  17548. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  17549. (when (or _c1 _c2)
  17550. ;; make sure there are undo boundaries
  17551. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  17552. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  17553. ;; remember which buffer to undo
  17554. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  17555. org-agenda-undo-list)))))
  17556. (defun org-agenda-undo ()
  17557. "Undo a remote editing step in the agenda.
  17558. This undoes changes both in the agenda buffer and in the remote buffer
  17559. that have been changed along."
  17560. (interactive)
  17561. (or org-agenda-allow-remote-undo
  17562. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  17563. (if (not (eq this-command last-command))
  17564. (setq org-agenda-undo-has-started-in nil
  17565. org-agenda-pending-undo-list org-agenda-undo-list))
  17566. (if (not org-agenda-pending-undo-list)
  17567. (error "No further undo information"))
  17568. (let* ((entry (pop org-agenda-pending-undo-list))
  17569. buf line cmd rembuf)
  17570. (setq cmd (pop entry) line (pop entry))
  17571. (setq rembuf (nth 2 entry))
  17572. (org-with-remote-undo rembuf
  17573. (while (bufferp (setq buf (pop entry)))
  17574. (if (pop entry)
  17575. (with-current-buffer buf
  17576. (let ((last-undo-buffer buf)
  17577. (inhibit-read-only t))
  17578. (unless (memq buf org-agenda-undo-has-started-in)
  17579. (push buf org-agenda-undo-has-started-in)
  17580. (make-local-variable 'pending-undo-list)
  17581. (undo-start))
  17582. (while (and pending-undo-list
  17583. (listp pending-undo-list)
  17584. (not (car pending-undo-list)))
  17585. (pop pending-undo-list))
  17586. (undo-more 1))))))
  17587. (goto-line line)
  17588. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  17589. (defun org-verify-change-for-undo (l1 l2)
  17590. "Verify that a real change occurred between the undo lists L1 and L2."
  17591. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  17592. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  17593. (not (eq l1 l2)))
  17594. ;;; Agenda dispatch
  17595. (defvar org-agenda-restrict nil)
  17596. (defvar org-agenda-restrict-begin (make-marker))
  17597. (defvar org-agenda-restrict-end (make-marker))
  17598. (defvar org-agenda-last-dispatch-buffer nil)
  17599. (defvar org-agenda-overriding-restriction nil)
  17600. ;;;###autoload
  17601. (defun org-agenda (arg &optional keys restriction)
  17602. "Dispatch agenda commands to collect entries to the agenda buffer.
  17603. Prompts for a command to execute. Any prefix arg will be passed
  17604. on to the selected command. The default selections are:
  17605. a Call `org-agenda-list' to display the agenda for current day or week.
  17606. t Call `org-todo-list' to display the global todo list.
  17607. T Call `org-todo-list' to display the global todo list, select only
  17608. entries with a specific TODO keyword (the user gets a prompt).
  17609. m Call `org-tags-view' to display headlines with tags matching
  17610. a condition (the user is prompted for the condition).
  17611. M Like `m', but select only TODO entries, no ordinary headlines.
  17612. L Create a timeline for the current buffer.
  17613. e Export views to associated files.
  17614. More commands can be added by configuring the variable
  17615. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  17616. searches can be pre-defined in this way.
  17617. If the current buffer is in Org-mode and visiting a file, you can also
  17618. first press `<' once to indicate that the agenda should be temporarily
  17619. \(until the next use of \\[org-agenda]) restricted to the current file.
  17620. Pressing `<' twice means to restrict to the current subtree or region
  17621. \(if active)."
  17622. (interactive "P")
  17623. (catch 'exit
  17624. (let* ((prefix-descriptions nil)
  17625. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  17626. (org-agenda-custom-commands
  17627. ;; normalize different versions
  17628. (delq nil
  17629. (mapcar
  17630. (lambda (x)
  17631. (cond ((stringp (cdr x))
  17632. (push x prefix-descriptions)
  17633. nil)
  17634. ((stringp (nth 1 x)) x)
  17635. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  17636. (t (cons (car x) (cons "" (cdr x))))))
  17637. org-agenda-custom-commands)))
  17638. (buf (current-buffer))
  17639. (bfn (buffer-file-name (buffer-base-buffer)))
  17640. entry key type match lprops ans)
  17641. ;; Turn off restriction unless there is an overriding one
  17642. (unless org-agenda-overriding-restriction
  17643. (put 'org-agenda-files 'org-restrict nil)
  17644. (setq org-agenda-restrict nil)
  17645. (move-marker org-agenda-restrict-begin nil)
  17646. (move-marker org-agenda-restrict-end nil))
  17647. ;; Delete old local properties
  17648. (put 'org-agenda-redo-command 'org-lprops nil)
  17649. ;; Remember where this call originated
  17650. (setq org-agenda-last-dispatch-buffer (current-buffer))
  17651. (unless keys
  17652. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  17653. keys (car ans)
  17654. restriction (cdr ans)))
  17655. ;; Estabish the restriction, if any
  17656. (when (and (not org-agenda-overriding-restriction) restriction)
  17657. (put 'org-agenda-files 'org-restrict (list bfn))
  17658. (cond
  17659. ((eq restriction 'region)
  17660. (setq org-agenda-restrict t)
  17661. (move-marker org-agenda-restrict-begin (region-beginning))
  17662. (move-marker org-agenda-restrict-end (region-end)))
  17663. ((eq restriction 'subtree)
  17664. (save-excursion
  17665. (setq org-agenda-restrict t)
  17666. (org-back-to-heading t)
  17667. (move-marker org-agenda-restrict-begin (point))
  17668. (move-marker org-agenda-restrict-end
  17669. (progn (org-end-of-subtree t)))))))
  17670. (require 'calendar) ; FIXME: can we avoid this for some commands?
  17671. ;; For example the todo list should not need it (but does...)
  17672. (cond
  17673. ((setq entry (assoc keys org-agenda-custom-commands))
  17674. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  17675. (progn
  17676. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  17677. (put 'org-agenda-redo-command 'org-lprops lprops)
  17678. (cond
  17679. ((eq type 'agenda)
  17680. (org-let lprops '(org-agenda-list current-prefix-arg)))
  17681. ((eq type 'alltodo)
  17682. (org-let lprops '(org-todo-list current-prefix-arg)))
  17683. ((eq type 'stuck)
  17684. (org-let lprops '(org-agenda-list-stuck-projects
  17685. current-prefix-arg)))
  17686. ((eq type 'tags)
  17687. (org-let lprops '(org-tags-view current-prefix-arg match)))
  17688. ((eq type 'tags-todo)
  17689. (org-let lprops '(org-tags-view '(4) match)))
  17690. ((eq type 'todo)
  17691. (org-let lprops '(org-todo-list match)))
  17692. ((eq type 'tags-tree)
  17693. (org-check-for-org-mode)
  17694. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  17695. ((eq type 'todo-tree)
  17696. (org-check-for-org-mode)
  17697. (org-let lprops
  17698. '(org-occur (concat "^" outline-regexp "[ \t]*"
  17699. (regexp-quote match) "\\>"))))
  17700. ((eq type 'occur-tree)
  17701. (org-check-for-org-mode)
  17702. (org-let lprops '(org-occur match)))
  17703. ((functionp type)
  17704. (org-let lprops '(funcall type match)))
  17705. ((fboundp type)
  17706. (org-let lprops '(funcall type match)))
  17707. (t (error "Invalid custom agenda command type %s" type))))
  17708. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  17709. ((equal keys "C")
  17710. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  17711. (customize-variable 'org-agenda-custom-commands))
  17712. ((equal keys "a") (call-interactively 'org-agenda-list))
  17713. ((equal keys "t") (call-interactively 'org-todo-list))
  17714. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  17715. ((equal keys "m") (call-interactively 'org-tags-view))
  17716. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  17717. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  17718. ((equal keys "L")
  17719. (unless (org-mode-p)
  17720. (error "This is not an Org-mode file"))
  17721. (unless restriction
  17722. (put 'org-agenda-files 'org-restrict (list bfn))
  17723. (org-call-with-arg 'org-timeline arg)))
  17724. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  17725. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  17726. ((equal keys "!") (customize-variable 'org-stuck-projects))
  17727. (t (error "Invalid agenda key"))))))
  17728. (defun org-agenda-normalize-custom-commands (cmds)
  17729. (delq nil
  17730. (mapcar
  17731. (lambda (x)
  17732. (cond ((stringp (cdr x)) nil)
  17733. ((stringp (nth 1 x)) x)
  17734. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  17735. (t (cons (car x) (cons "" (cdr x))))))
  17736. cmds)))
  17737. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  17738. "The user interface for selecting an agenda command."
  17739. (catch 'exit
  17740. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  17741. (restrict-ok (and bfn (org-mode-p)))
  17742. (region-p (org-region-active-p))
  17743. (custom org-agenda-custom-commands)
  17744. (selstring "")
  17745. restriction second-time
  17746. c entry key type match prefixes rmheader header-end custom1 desc)
  17747. (save-window-excursion
  17748. (delete-other-windows)
  17749. (org-switch-to-buffer-other-window " *Agenda Commands*")
  17750. (erase-buffer)
  17751. (insert (eval-when-compile
  17752. (let ((header
  17753. "
  17754. Press key for an agenda command: < Buffer,subtree/region restriction
  17755. -------------------------------- > Remove restriction
  17756. a Agenda for current week or day e Export agenda views
  17757. t List of all TODO entries T Entries with special TODO kwd
  17758. m Match a TAGS query M Like m, but only TODO entries
  17759. L Timeline for current buffer # List stuck projects (!=configure)
  17760. / Multi-occur C Configure custom agenda commands
  17761. ")
  17762. (start 0))
  17763. (while (string-match
  17764. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  17765. header start)
  17766. (setq start (match-end 0))
  17767. (add-text-properties (match-beginning 2) (match-end 2)
  17768. '(face bold) header))
  17769. header)))
  17770. (setq header-end (move-marker (make-marker) (point)))
  17771. (while t
  17772. (setq custom1 custom)
  17773. (when (eq rmheader t)
  17774. (goto-line 1)
  17775. (re-search-forward ":" nil t)
  17776. (delete-region (match-end 0) (point-at-eol))
  17777. (forward-char 1)
  17778. (looking-at "-+")
  17779. (delete-region (match-end 0) (point-at-eol))
  17780. (move-marker header-end (match-end 0)))
  17781. (goto-char header-end)
  17782. (delete-region (point) (point-max))
  17783. (while (setq entry (pop custom1))
  17784. (setq key (car entry) desc (nth 1 entry)
  17785. type (nth 2 entry) match (nth 3 entry))
  17786. (if (> (length key) 1)
  17787. (add-to-list 'prefixes (string-to-char key))
  17788. (insert
  17789. (format
  17790. "\n%-4s%-14s: %s"
  17791. (org-add-props (copy-sequence key)
  17792. '(face bold))
  17793. (cond
  17794. ((string-match "\\S-" desc) desc)
  17795. ((eq type 'agenda) "Agenda for current week or day")
  17796. ((eq type 'alltodo) "List of all TODO entries")
  17797. ((eq type 'stuck) "List of stuck projects")
  17798. ((eq type 'todo) "TODO keyword")
  17799. ((eq type 'tags) "Tags query")
  17800. ((eq type 'tags-todo) "Tags (TODO)")
  17801. ((eq type 'tags-tree) "Tags tree")
  17802. ((eq type 'todo-tree) "TODO kwd tree")
  17803. ((eq type 'occur-tree) "Occur tree")
  17804. ((functionp type) (if (symbolp type)
  17805. (symbol-name type)
  17806. "Lambda expression"))
  17807. (t "???"))
  17808. (cond
  17809. ((stringp match)
  17810. (org-add-props match nil 'face 'org-warning))
  17811. (match
  17812. (format "set of %d commands" (length match)))
  17813. (t ""))))))
  17814. (when prefixes
  17815. (mapc (lambda (x)
  17816. (insert
  17817. (format "\n%s %s"
  17818. (org-add-props (char-to-string x)
  17819. nil 'face 'bold)
  17820. (or (cdr (assoc (concat selstring (char-to-string x))
  17821. prefix-descriptions))
  17822. "Prefix key"))))
  17823. prefixes))
  17824. (goto-char (point-min))
  17825. (when (fboundp 'fit-window-to-buffer)
  17826. (if second-time
  17827. (if (not (pos-visible-in-window-p (point-max)))
  17828. (fit-window-to-buffer))
  17829. (setq second-time t)
  17830. (fit-window-to-buffer)))
  17831. (message "Press key for agenda command%s:"
  17832. (if (or restrict-ok org-agenda-overriding-restriction)
  17833. (if org-agenda-overriding-restriction
  17834. " (restriction lock active)"
  17835. (if restriction
  17836. (format " (restricted to %s)" restriction)
  17837. " (unrestricted)"))
  17838. ""))
  17839. (setq c (read-char-exclusive))
  17840. (message "")
  17841. (cond
  17842. ((assoc (char-to-string c) custom)
  17843. (setq selstring (concat selstring (char-to-string c)))
  17844. (throw 'exit (cons selstring restriction)))
  17845. ((memq c prefixes)
  17846. (setq selstring (concat selstring (char-to-string c))
  17847. prefixes nil
  17848. rmheader (or rmheader t)
  17849. custom (delq nil (mapcar
  17850. (lambda (x)
  17851. (if (or (= (length (car x)) 1)
  17852. (/= (string-to-char (car x)) c))
  17853. nil
  17854. (cons (substring (car x) 1) (cdr x))))
  17855. custom))))
  17856. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  17857. (message "Restriction is only possible in Org-mode buffers")
  17858. (ding) (sit-for 1))
  17859. ((eq c ?1)
  17860. (org-agenda-remove-restriction-lock 'noupdate)
  17861. (setq restriction 'buffer))
  17862. ((eq c ?0)
  17863. (org-agenda-remove-restriction-lock 'noupdate)
  17864. (setq restriction (if region-p 'region 'subtree)))
  17865. ((eq c ?<)
  17866. (org-agenda-remove-restriction-lock 'noupdate)
  17867. (setq restriction
  17868. (cond
  17869. ((eq restriction 'buffer)
  17870. (if region-p 'region 'subtree))
  17871. ((memq restriction '(subtree region))
  17872. nil)
  17873. (t 'buffer))))
  17874. ((eq c ?>)
  17875. (org-agenda-remove-restriction-lock 'noupdate)
  17876. (setq restriction nil))
  17877. ((and (equal selstring "") (memq c '(?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  17878. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  17879. ((and (> (length selstring) 0) (eq c ?\d))
  17880. (delete-window)
  17881. (org-agenda-get-restriction-and-command prefix-descriptions))
  17882. ((equal c ?q) (error "Abort"))
  17883. (t (error "Invalid key %c" c))))))))
  17884. (defun org-run-agenda-series (name series)
  17885. (org-prepare-agenda name)
  17886. (let* ((org-agenda-multi t)
  17887. (redo (list 'org-run-agenda-series name (list 'quote series)))
  17888. (cmds (car series))
  17889. (gprops (nth 1 series))
  17890. match ;; The byte compiler incorrectly complains about this. Keep it!
  17891. cmd type lprops)
  17892. (while (setq cmd (pop cmds))
  17893. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  17894. (cond
  17895. ((eq type 'agenda)
  17896. (org-let2 gprops lprops
  17897. '(call-interactively 'org-agenda-list)))
  17898. ((eq type 'alltodo)
  17899. (org-let2 gprops lprops
  17900. '(call-interactively 'org-todo-list)))
  17901. ((eq type 'stuck)
  17902. (org-let2 gprops lprops
  17903. '(call-interactively 'org-agenda-list-stuck-projects)))
  17904. ((eq type 'tags)
  17905. (org-let2 gprops lprops
  17906. '(org-tags-view current-prefix-arg match)))
  17907. ((eq type 'tags-todo)
  17908. (org-let2 gprops lprops
  17909. '(org-tags-view '(4) match)))
  17910. ((eq type 'todo)
  17911. (org-let2 gprops lprops
  17912. '(org-todo-list match)))
  17913. ((fboundp type)
  17914. (org-let2 gprops lprops
  17915. '(funcall type match)))
  17916. (t (error "Invalid type in command series"))))
  17917. (widen)
  17918. (setq org-agenda-redo-command redo)
  17919. (goto-char (point-min)))
  17920. (org-finalize-agenda))
  17921. ;;;###autoload
  17922. (defmacro org-batch-agenda (cmd-key &rest parameters)
  17923. "Run an agenda command in batch mode and send the result to STDOUT.
  17924. If CMD-KEY is a string of length 1, it is used as a key in
  17925. `org-agenda-custom-commands' and triggers this command. If it is a
  17926. longer string is is used as a tags/todo match string.
  17927. Paramters are alternating variable names and values that will be bound
  17928. before running the agenda command."
  17929. (let (pars)
  17930. (while parameters
  17931. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  17932. (if (> (length cmd-key) 2)
  17933. (eval (list 'let (nreverse pars)
  17934. (list 'org-tags-view nil cmd-key)))
  17935. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  17936. (set-buffer org-agenda-buffer-name)
  17937. (princ (org-encode-for-stdout (buffer-string)))))
  17938. (defun org-encode-for-stdout (string)
  17939. (if (fboundp 'encode-coding-string)
  17940. (encode-coding-string string buffer-file-coding-system)
  17941. string))
  17942. (defvar org-agenda-info nil)
  17943. ;;;###autoload
  17944. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  17945. "Run an agenda command in batch mode and send the result to STDOUT.
  17946. If CMD-KEY is a string of length 1, it is used as a key in
  17947. `org-agenda-custom-commands' and triggers this command. If it is a
  17948. longer string is is used as a tags/todo match string.
  17949. Paramters are alternating variable names and values that will be bound
  17950. before running the agenda command.
  17951. The output gives a line for each selected agenda item. Each
  17952. item is a list of comma-separated values, like this:
  17953. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  17954. category The category of the item
  17955. head The headline, without TODO kwd, TAGS and PRIORITY
  17956. type The type of the agenda entry, can be
  17957. todo selected in TODO match
  17958. tagsmatch selected in tags match
  17959. diary imported from diary
  17960. deadline a deadline on given date
  17961. scheduled scheduled on given date
  17962. timestamp entry has timestamp on given date
  17963. closed entry was closed on given date
  17964. upcoming-deadline warning about deadline
  17965. past-scheduled forwarded scheduled item
  17966. block entry has date block including g. date
  17967. todo The todo keyword, if any
  17968. tags All tags including inherited ones, separated by colons
  17969. date The relevant date, like 2007-2-14
  17970. time The time, like 15:00-16:50
  17971. extra Sting with extra planning info
  17972. priority-l The priority letter if any was given
  17973. priority-n The computed numerical priority
  17974. agenda-day The day in the agenda where this is listed"
  17975. (let (pars)
  17976. (while parameters
  17977. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  17978. (push (list 'org-agenda-remove-tags t) pars)
  17979. (if (> (length cmd-key) 2)
  17980. (eval (list 'let (nreverse pars)
  17981. (list 'org-tags-view nil cmd-key)))
  17982. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  17983. (set-buffer org-agenda-buffer-name)
  17984. (let* ((lines (org-split-string (buffer-string) "\n"))
  17985. line)
  17986. (while (setq line (pop lines))
  17987. (catch 'next
  17988. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  17989. (setq org-agenda-info
  17990. (org-fix-agenda-info (text-properties-at 0 line)))
  17991. (princ
  17992. (org-encode-for-stdout
  17993. (mapconcat 'org-agenda-export-csv-mapper
  17994. '(org-category txt type todo tags date time-of-day extra
  17995. priority-letter priority agenda-day)
  17996. ",")))
  17997. (princ "\n"))))))
  17998. (defun org-fix-agenda-info (props)
  17999. "Make sure all properties on an agenda item have a canonical form,
  18000. so the the export commands caneasily use it."
  18001. (let (tmp re)
  18002. (when (setq tmp (plist-get props 'tags))
  18003. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18004. (when (setq tmp (plist-get props 'date))
  18005. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18006. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18007. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18008. (setq tmp (calendar-date-string tmp)))
  18009. (setq props (plist-put props 'date tmp)))
  18010. (when (setq tmp (plist-get props 'day))
  18011. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18012. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18013. (setq tmp (calendar-date-string tmp)))
  18014. (setq props (plist-put props 'day tmp))
  18015. (setq props (plist-put props 'agenda-day tmp)))
  18016. (when (setq tmp (plist-get props 'txt))
  18017. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18018. (plist-put props 'priority-letter (match-string 1 tmp))
  18019. (setq tmp (replace-match "" t t tmp)))
  18020. (when (and (setq re (plist-get props 'org-todo-regexp))
  18021. (setq re (concat "\\`\\.*" re " ?"))
  18022. (string-match re tmp))
  18023. (plist-put props 'todo (match-string 1 tmp))
  18024. (setq tmp (replace-match "" t t tmp)))
  18025. (plist-put props 'txt tmp)))
  18026. props)
  18027. (defun org-agenda-export-csv-mapper (prop)
  18028. (let ((res (plist-get org-agenda-info prop)))
  18029. (setq res
  18030. (cond
  18031. ((not res) "")
  18032. ((stringp res) res)
  18033. (t (prin1-to-string res))))
  18034. (while (string-match "," res)
  18035. (setq res (replace-match ";" t t res)))
  18036. (org-trim res)))
  18037. ;;;###autoload
  18038. (defun org-store-agenda-views (&rest parameters)
  18039. (interactive)
  18040. (eval (list 'org-batch-store-agenda-views)))
  18041. ;; FIXME, why is this a macro?????
  18042. ;;;###autoload
  18043. (defmacro org-batch-store-agenda-views (&rest parameters)
  18044. "Run all custom agenda commands that have a file argument."
  18045. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18046. (pop-up-frames nil)
  18047. (dir default-directory)
  18048. pars cmd thiscmdkey files opts)
  18049. (while parameters
  18050. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18051. (setq pars (reverse pars))
  18052. (save-window-excursion
  18053. (while cmds
  18054. (setq cmd (pop cmds)
  18055. thiscmdkey (car cmd)
  18056. opts (nth 4 cmd)
  18057. files (nth 5 cmd))
  18058. (if (stringp files) (setq files (list files)))
  18059. (when files
  18060. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18061. (list 'org-agenda nil thiscmdkey)))
  18062. (set-buffer org-agenda-buffer-name)
  18063. (while files
  18064. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18065. (list 'org-write-agenda
  18066. (expand-file-name (pop files) dir) t))))
  18067. (and (get-buffer org-agenda-buffer-name)
  18068. (kill-buffer org-agenda-buffer-name)))))))
  18069. (defun org-write-agenda (file &optional nosettings)
  18070. "Write the current buffer (an agenda view) as a file.
  18071. Depending on the extension of the file name, plain text (.txt),
  18072. HTML (.html or .htm) or Postscript (.ps) is produced.
  18073. If NOSETTINGS is given, do not scope the settings of
  18074. `org-agenda-exporter-settings' into the export commands. This is used when
  18075. the settings have already been scoped and we do not wish to overrule other,
  18076. higher priority settings."
  18077. (interactive "FWrite agenda to file: ")
  18078. (if (not (file-writable-p file))
  18079. (error "Cannot write agenda to file %s" file))
  18080. (cond
  18081. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18082. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18083. (org-let (if nosettings nil org-agenda-exporter-settings)
  18084. '(save-excursion
  18085. (save-window-excursion
  18086. (cond
  18087. ((string-match "\\.html?\\'" file)
  18088. (set-buffer (htmlize-buffer (current-buffer)))
  18089. (when (and org-agenda-export-html-style
  18090. (string-match "<style>" org-agenda-export-html-style))
  18091. ;; replace <style> section with org-agenda-export-html-style
  18092. (goto-char (point-min))
  18093. (kill-region (- (search-forward "<style") 6)
  18094. (search-forward "</style>"))
  18095. (insert org-agenda-export-html-style))
  18096. (write-file file)
  18097. (kill-buffer (current-buffer))
  18098. (message "HTML written to %s" file))
  18099. ((string-match "\\.ps\\'" file)
  18100. (ps-print-buffer-with-faces file)
  18101. (message "Postscript written to %s" file))
  18102. (t
  18103. (let ((bs (buffer-string)))
  18104. (find-file file)
  18105. (insert bs)
  18106. (save-buffer 0)
  18107. (kill-buffer (current-buffer))
  18108. (message "Plain text written to %s" file))))))
  18109. (set-buffer org-agenda-buffer-name)))
  18110. (defmacro org-no-read-only (&rest body)
  18111. "Inhibit read-only for BODY."
  18112. `(let ((inhibit-read-only t)) ,@body))
  18113. (defun org-check-for-org-mode ()
  18114. "Make sure current buffer is in org-mode. Error if not."
  18115. (or (org-mode-p)
  18116. (error "Cannot execute org-mode agenda command on buffer in %s."
  18117. major-mode)))
  18118. (defun org-fit-agenda-window ()
  18119. "Fit the window to the buffer size."
  18120. (and (memq org-agenda-window-setup '(reorganize-frame))
  18121. (fboundp 'fit-window-to-buffer)
  18122. (fit-window-to-buffer
  18123. nil
  18124. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18125. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18126. ;;; Agenda file list
  18127. (defun org-agenda-files (&optional unrestricted)
  18128. "Get the list of agenda files.
  18129. Optional UNRESTRICTED means return the full list even if a restriction
  18130. is currently in place."
  18131. (let ((files
  18132. (cond
  18133. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18134. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18135. ((listp org-agenda-files) org-agenda-files)
  18136. (t (error "Invalid value of `org-agenda-files'")))))
  18137. (setq files (apply 'append
  18138. (mapcar (lambda (f)
  18139. (if (file-directory-p f)
  18140. (directory-files f t
  18141. org-agenda-file-regexp)
  18142. (list f)))
  18143. files)))
  18144. (if org-agenda-skip-unavailable-files
  18145. (delq nil
  18146. (mapcar (function
  18147. (lambda (file)
  18148. (and (file-readable-p file) file)))
  18149. files))
  18150. files))) ; `org-check-agenda-file' will remove them from the list
  18151. (defun org-edit-agenda-file-list ()
  18152. "Edit the list of agenda files.
  18153. Depending on setup, this either uses customize to edit the variable
  18154. `org-agenda-files', or it visits the file that is holding the list. In the
  18155. latter case, the buffer is set up in a way that saving it automatically kills
  18156. the buffer and restores the previous window configuration."
  18157. (interactive)
  18158. (if (stringp org-agenda-files)
  18159. (let ((cw (current-window-configuration)))
  18160. (find-file org-agenda-files)
  18161. (org-set-local 'org-window-configuration cw)
  18162. (org-add-hook 'after-save-hook
  18163. (lambda ()
  18164. (set-window-configuration
  18165. (prog1 org-window-configuration
  18166. (kill-buffer (current-buffer))))
  18167. (org-install-agenda-files-menu)
  18168. (message "New agenda file list installed"))
  18169. nil 'local)
  18170. (message "%s" (substitute-command-keys
  18171. "Edit list and finish with \\[save-buffer]")))
  18172. (customize-variable 'org-agenda-files)))
  18173. (defun org-store-new-agenda-file-list (list)
  18174. "Set new value for the agenda file list and save it correcly."
  18175. (if (stringp org-agenda-files)
  18176. (let ((f org-agenda-files) b)
  18177. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18178. (with-temp-file f
  18179. (insert (mapconcat 'identity list "\n") "\n")))
  18180. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18181. (setq org-agenda-files list)
  18182. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18183. (defun org-read-agenda-file-list ()
  18184. "Read the list of agenda files from a file."
  18185. (when (stringp org-agenda-files)
  18186. (with-temp-buffer
  18187. (insert-file-contents org-agenda-files)
  18188. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18189. ;;;###autoload
  18190. (defun org-cycle-agenda-files ()
  18191. "Cycle through the files in `org-agenda-files'.
  18192. If the current buffer visits an agenda file, find the next one in the list.
  18193. If the current buffer does not, find the first agenda file."
  18194. (interactive)
  18195. (let* ((fs (org-agenda-files t))
  18196. (files (append fs (list (car fs))))
  18197. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18198. file)
  18199. (unless files (error "No agenda files"))
  18200. (catch 'exit
  18201. (while (setq file (pop files))
  18202. (if (equal (file-truename file) tcf)
  18203. (when (car files)
  18204. (find-file (car files))
  18205. (throw 'exit t))))
  18206. (find-file (car fs)))
  18207. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18208. (defun org-agenda-file-to-front (&optional to-end)
  18209. "Move/add the current file to the top of the agenda file list.
  18210. If the file is not present in the list, it is added to the front. If it is
  18211. present, it is moved there. With optional argument TO-END, add/move to the
  18212. end of the list."
  18213. (interactive "P")
  18214. (let ((org-agenda-skip-unavailable-files nil)
  18215. (file-alist (mapcar (lambda (x)
  18216. (cons (file-truename x) x))
  18217. (org-agenda-files t)))
  18218. (ctf (file-truename buffer-file-name))
  18219. x had)
  18220. (setq x (assoc ctf file-alist) had x)
  18221. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18222. (if to-end
  18223. (setq file-alist (append (delq x file-alist) (list x)))
  18224. (setq file-alist (cons x (delq x file-alist))))
  18225. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18226. (org-install-agenda-files-menu)
  18227. (message "File %s to %s of agenda file list"
  18228. (if had "moved" "added") (if to-end "end" "front"))))
  18229. (defun org-remove-file (&optional file)
  18230. "Remove current file from the list of files in variable `org-agenda-files'.
  18231. These are the files which are being checked for agenda entries.
  18232. Optional argument FILE means, use this file instead of the current."
  18233. (interactive)
  18234. (let* ((org-agenda-skip-unavailable-files nil)
  18235. (file (or file buffer-file-name))
  18236. (true-file (file-truename file))
  18237. (afile (abbreviate-file-name file))
  18238. (files (delq nil (mapcar
  18239. (lambda (x)
  18240. (if (equal true-file
  18241. (file-truename x))
  18242. nil x))
  18243. (org-agenda-files t)))))
  18244. (if (not (= (length files) (length (org-agenda-files t))))
  18245. (progn
  18246. (org-store-new-agenda-file-list files)
  18247. (org-install-agenda-files-menu)
  18248. (message "Removed file: %s" afile))
  18249. (message "File was not in list: %s (not removed)" afile))))
  18250. (defun org-file-menu-entry (file)
  18251. (vector file (list 'find-file file) t))
  18252. (defun org-check-agenda-file (file)
  18253. "Make sure FILE exists. If not, ask user what to do."
  18254. (when (not (file-exists-p file))
  18255. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18256. (abbreviate-file-name file))
  18257. (let ((r (downcase (read-char-exclusive))))
  18258. (cond
  18259. ((equal r ?r)
  18260. (org-remove-file file)
  18261. (throw 'nextfile t))
  18262. (t (error "Abort"))))))
  18263. ;;; Agenda prepare and finalize
  18264. (defvar org-agenda-multi nil) ; dynammically scoped
  18265. (defvar org-agenda-buffer-name "*Org Agenda*")
  18266. (defvar org-pre-agenda-window-conf nil)
  18267. (defvar org-agenda-name nil)
  18268. (defun org-prepare-agenda (&optional name)
  18269. (setq org-todo-keywords-for-agenda nil)
  18270. (setq org-done-keywords-for-agenda nil)
  18271. (if org-agenda-multi
  18272. (progn
  18273. (setq buffer-read-only nil)
  18274. (goto-char (point-max))
  18275. (unless (or (bobp) org-agenda-compact-blocks)
  18276. (insert "\n" (make-string (window-width) ?=) "\n"))
  18277. (narrow-to-region (point) (point-max)))
  18278. (org-agenda-maybe-reset-markers 'force)
  18279. (org-prepare-agenda-buffers (org-agenda-files))
  18280. (setq org-todo-keywords-for-agenda
  18281. (org-uniquify org-todo-keywords-for-agenda))
  18282. (setq org-done-keywords-for-agenda
  18283. (org-uniquify org-done-keywords-for-agenda))
  18284. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18285. (awin (get-buffer-window abuf)))
  18286. (cond
  18287. ((equal (current-buffer) abuf) nil)
  18288. (awin (select-window awin))
  18289. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18290. ((equal org-agenda-window-setup 'current-window)
  18291. (switch-to-buffer abuf))
  18292. ((equal org-agenda-window-setup 'other-window)
  18293. (org-switch-to-buffer-other-window abuf))
  18294. ((equal org-agenda-window-setup 'other-frame)
  18295. (switch-to-buffer-other-frame abuf))
  18296. ((equal org-agenda-window-setup 'reorganize-frame)
  18297. (delete-other-windows)
  18298. (org-switch-to-buffer-other-window abuf))))
  18299. (setq buffer-read-only nil)
  18300. (erase-buffer)
  18301. (org-agenda-mode)
  18302. (and name (not org-agenda-name)
  18303. (org-set-local 'org-agenda-name name)))
  18304. (setq buffer-read-only nil))
  18305. (defun org-finalize-agenda ()
  18306. "Finishing touch for the agenda buffer, called just before displaying it."
  18307. (unless org-agenda-multi
  18308. (save-excursion
  18309. (let ((inhibit-read-only t))
  18310. (goto-char (point-min))
  18311. (while (org-activate-bracket-links (point-max))
  18312. (add-text-properties (match-beginning 0) (match-end 0)
  18313. '(face org-link)))
  18314. (org-agenda-align-tags)
  18315. (unless org-agenda-with-colors
  18316. (remove-text-properties (point-min) (point-max) '(face nil))))
  18317. (if (and (boundp 'org-overriding-columns-format)
  18318. org-overriding-columns-format)
  18319. (org-set-local 'org-overriding-columns-format
  18320. org-overriding-columns-format))
  18321. (if (and (boundp 'org-agenda-view-columns-initially)
  18322. org-agenda-view-columns-initially)
  18323. (org-agenda-columns))
  18324. (when org-agenda-fontify-priorities
  18325. (org-fontify-priorities))
  18326. (run-hooks 'org-finalize-agenda-hook))))
  18327. (defun org-fontify-priorities ()
  18328. "Make highest priority lines bold, and lowest italic."
  18329. (interactive)
  18330. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18331. (org-delete-overlay o)))
  18332. (org-overlays-in (point-min) (point-max)))
  18333. (save-excursion
  18334. (let ((inhibit-read-only t)
  18335. b e p ov h l)
  18336. (goto-char (point-min))
  18337. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18338. (setq h (or (get-char-property (point) 'org-highest-priority)
  18339. org-highest-priority)
  18340. l (or (get-char-property (point) 'org-lowest-priority)
  18341. org-lowest-priority)
  18342. p (string-to-char (match-string 1))
  18343. b (match-beginning 0) e (point-at-eol)
  18344. ov (org-make-overlay b e))
  18345. (org-overlay-put
  18346. ov 'face
  18347. (cond ((listp org-agenda-fontify-priorities)
  18348. (cdr (assoc p org-agenda-fontify-priorities)))
  18349. ((equal p l) 'italic)
  18350. ((equal p h) 'bold)))
  18351. (org-overlay-put ov 'org-type 'org-priority)))))
  18352. (defun org-prepare-agenda-buffers (files)
  18353. "Create buffers for all agenda files, protect archived trees and comments."
  18354. (interactive)
  18355. (let ((pa '(:org-archived t))
  18356. (pc '(:org-comment t))
  18357. (pall '(:org-archived t :org-comment t))
  18358. (inhibit-read-only t)
  18359. (rea (concat ":" org-archive-tag ":"))
  18360. bmp file re)
  18361. (save-excursion
  18362. (save-restriction
  18363. (while (setq file (pop files))
  18364. (if (bufferp file)
  18365. (set-buffer file)
  18366. (org-check-agenda-file file)
  18367. (set-buffer (org-get-agenda-file-buffer file)))
  18368. (widen)
  18369. (setq bmp (buffer-modified-p))
  18370. (org-refresh-category-properties)
  18371. (setq org-todo-keywords-for-agenda
  18372. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18373. (setq org-done-keywords-for-agenda
  18374. (append org-done-keywords-for-agenda org-done-keywords))
  18375. (save-excursion
  18376. (remove-text-properties (point-min) (point-max) pall)
  18377. (when org-agenda-skip-archived-trees
  18378. (goto-char (point-min))
  18379. (while (re-search-forward rea nil t)
  18380. (if (org-on-heading-p t)
  18381. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18382. (goto-char (point-min))
  18383. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18384. (while (re-search-forward re nil t)
  18385. (add-text-properties
  18386. (match-beginning 0) (org-end-of-subtree t) pc)))
  18387. (set-buffer-modified-p bmp))))))
  18388. (defvar org-agenda-skip-function nil
  18389. "Function to be called at each match during agenda construction.
  18390. If this function returns nil, the current match should not be skipped.
  18391. Otherwise, the function must return a position from where the search
  18392. should be continued.
  18393. This may also be a Lisp form, it will be evaluated.
  18394. Never set this variable using `setq' or so, because then it will apply
  18395. to all future agenda commands. Instead, bind it with `let' to scope
  18396. it dynamically into the agenda-constructing command. A good way to set
  18397. it is through options in org-agenda-custom-commands.")
  18398. (defun org-agenda-skip ()
  18399. "Throw to `:skip' in places that should be skipped.
  18400. Also moves point to the end of the skipped region, so that search can
  18401. continue from there."
  18402. (let ((p (point-at-bol)) to fp)
  18403. (and org-agenda-skip-archived-trees
  18404. (get-text-property p :org-archived)
  18405. (org-end-of-subtree t)
  18406. (throw :skip t))
  18407. (and (get-text-property p :org-comment)
  18408. (org-end-of-subtree t)
  18409. (throw :skip t))
  18410. (if (equal (char-after p) ?#) (throw :skip t))
  18411. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18412. (consp org-agenda-skip-function))
  18413. (setq to (save-excursion
  18414. (save-match-data
  18415. (if fp
  18416. (funcall org-agenda-skip-function)
  18417. (eval org-agenda-skip-function))))))
  18418. (goto-char to)
  18419. (throw :skip t))))
  18420. (defvar org-agenda-markers nil
  18421. "List of all currently active markers created by `org-agenda'.")
  18422. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18423. "Creation time of the last agenda marker.")
  18424. (defun org-agenda-new-marker (&optional pos)
  18425. "Return a new agenda marker.
  18426. Org-mode keeps a list of these markers and resets them when they are
  18427. no longer in use."
  18428. (let ((m (copy-marker (or pos (point)))))
  18429. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18430. (push m org-agenda-markers)
  18431. m))
  18432. (defun org-agenda-maybe-reset-markers (&optional force)
  18433. "Reset markers created by `org-agenda'. But only if they are old enough."
  18434. (if (or (and force (not org-agenda-multi))
  18435. (> (- (time-to-seconds (current-time))
  18436. org-agenda-last-marker-time)
  18437. 5))
  18438. (while org-agenda-markers
  18439. (move-marker (pop org-agenda-markers) nil))))
  18440. (defun org-get-agenda-file-buffer (file)
  18441. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18442. it to the list of buffers which might be released later."
  18443. (let ((buf (org-find-base-buffer-visiting file)))
  18444. (if buf
  18445. buf ; just return it
  18446. ;; Make a new buffer and remember it
  18447. (setq buf (find-file-noselect file))
  18448. (if buf (push buf org-agenda-new-buffers))
  18449. buf)))
  18450. (defun org-release-buffers (blist)
  18451. "Release all buffers in list, asking the user for confirmation when needed.
  18452. When a buffer is unmodified, it is just killed. When modified, it is saved
  18453. \(if the user agrees) and then killed."
  18454. (let (buf file)
  18455. (while (setq buf (pop blist))
  18456. (setq file (buffer-file-name buf))
  18457. (when (and (buffer-modified-p buf)
  18458. file
  18459. (y-or-n-p (format "Save file %s? " file)))
  18460. (with-current-buffer buf (save-buffer)))
  18461. (kill-buffer buf))))
  18462. (defun org-get-category (&optional pos)
  18463. "Get the category applying to position POS."
  18464. (get-text-property (or pos (point)) 'org-category))
  18465. ;;; Agenda timeline
  18466. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18467. (defun org-timeline (&optional include-all)
  18468. "Show a time-sorted view of the entries in the current org file.
  18469. Only entries with a time stamp of today or later will be listed. With
  18470. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18471. under the current date.
  18472. If the buffer contains an active region, only check the region for
  18473. dates."
  18474. (interactive "P")
  18475. (require 'calendar)
  18476. (org-compile-prefix-format 'timeline)
  18477. (org-set-sorting-strategy 'timeline)
  18478. (let* ((dopast t)
  18479. (dotodo include-all)
  18480. (doclosed org-agenda-show-log)
  18481. (entry buffer-file-name)
  18482. (date (calendar-current-date))
  18483. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18484. (end (if (org-region-active-p) (region-end) (point-max)))
  18485. (day-numbers (org-get-all-dates beg end 'no-ranges
  18486. t doclosed ; always include today
  18487. org-timeline-show-empty-dates))
  18488. (org-deadline-warning-days 0)
  18489. (org-agenda-only-exact-dates t)
  18490. (today (time-to-days (current-time)))
  18491. (past t)
  18492. args
  18493. s e rtn d emptyp)
  18494. (setq org-agenda-redo-command
  18495. (list 'progn
  18496. (list 'org-switch-to-buffer-other-window (current-buffer))
  18497. (list 'org-timeline (list 'quote include-all))))
  18498. (if (not dopast)
  18499. ;; Remove past dates from the list of dates.
  18500. (setq day-numbers (delq nil (mapcar (lambda(x)
  18501. (if (>= x today) x nil))
  18502. day-numbers))))
  18503. (org-prepare-agenda (concat "Timeline "
  18504. (file-name-nondirectory buffer-file-name)))
  18505. (if doclosed (push :closed args))
  18506. (push :timestamp args)
  18507. (push :deadline args)
  18508. (push :scheduled args)
  18509. (push :sexp args)
  18510. (if dotodo (push :todo args))
  18511. (while (setq d (pop day-numbers))
  18512. (if (and (listp d) (eq (car d) :omitted))
  18513. (progn
  18514. (setq s (point))
  18515. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  18516. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  18517. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  18518. (if (and (>= d today)
  18519. dopast
  18520. past)
  18521. (progn
  18522. (setq past nil)
  18523. (insert (make-string 79 ?-) "\n")))
  18524. (setq date (calendar-gregorian-from-absolute d))
  18525. (setq s (point))
  18526. (setq rtn (and (not emptyp)
  18527. (apply 'org-agenda-get-day-entries entry
  18528. date args)))
  18529. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  18530. (progn
  18531. (insert
  18532. (if (stringp org-agenda-format-date)
  18533. (format-time-string org-agenda-format-date
  18534. (org-time-from-absolute date))
  18535. (funcall org-agenda-format-date date))
  18536. "\n")
  18537. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18538. (put-text-property s (1- (point)) 'org-date-line t)
  18539. (if (equal d today)
  18540. (put-text-property s (1- (point)) 'org-today t))
  18541. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  18542. (put-text-property s (1- (point)) 'day d)))))
  18543. (goto-char (point-min))
  18544. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  18545. (point-min)))
  18546. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  18547. (org-finalize-agenda)
  18548. (setq buffer-read-only t)))
  18549. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  18550. "Return a list of all relevant day numbers from BEG to END buffer positions.
  18551. If NO-RANGES is non-nil, include only the start and end dates of a range,
  18552. not every single day in the range. If FORCE-TODAY is non-nil, make
  18553. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  18554. inactive time stamps (those in square brackets) are included.
  18555. When EMPTY is non-nil, also include days without any entries."
  18556. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  18557. dates dates1 date day day1 day2 ts1 ts2)
  18558. (if force-today
  18559. (setq dates (list (time-to-days (current-time)))))
  18560. (save-excursion
  18561. (goto-char beg)
  18562. (while (re-search-forward re end t)
  18563. (setq day (time-to-days (org-time-string-to-time
  18564. (substring (match-string 1) 0 10))))
  18565. (or (memq day dates) (push day dates)))
  18566. (unless no-ranges
  18567. (goto-char beg)
  18568. (while (re-search-forward org-tr-regexp end t)
  18569. (setq ts1 (substring (match-string 1) 0 10)
  18570. ts2 (substring (match-string 2) 0 10)
  18571. day1 (time-to-days (org-time-string-to-time ts1))
  18572. day2 (time-to-days (org-time-string-to-time ts2)))
  18573. (while (< (setq day1 (1+ day1)) day2)
  18574. (or (memq day1 dates) (push day1 dates)))))
  18575. (setq dates (sort dates '<))
  18576. (when empty
  18577. (while (setq day (pop dates))
  18578. (setq day2 (car dates))
  18579. (push day dates1)
  18580. (when (and day2 empty)
  18581. (if (or (eq empty t)
  18582. (and (numberp empty) (<= (- day2 day) empty)))
  18583. (while (< (setq day (1+ day)) day2)
  18584. (push (list day) dates1))
  18585. (push (cons :omitted (- day2 day)) dates1))))
  18586. (setq dates (nreverse dates1)))
  18587. dates)))
  18588. ;;; Agenda Daily/Weekly
  18589. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  18590. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  18591. (defvar org-agenda-last-arguments nil
  18592. "The arguments of the previous call to org-agenda")
  18593. (defvar org-starting-day nil) ; local variable in the agenda buffer
  18594. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  18595. (defvar org-include-all-loc nil) ; local variable
  18596. (defvar org-agenda-remove-date nil) ; dynamically scoped
  18597. ;;;###autoload
  18598. (defun org-agenda-list (&optional include-all start-day ndays)
  18599. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  18600. The view will be for the current day or week, but from the overview buffer
  18601. you will be able to go to other days/weeks.
  18602. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  18603. all unfinished TODO items will also be shown, before the agenda.
  18604. This feature is considered obsolete, please use the TODO list or a block
  18605. agenda instead.
  18606. With a numeric prefix argument in an interactive call, the agenda will
  18607. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  18608. the number of days. NDAYS defaults to `org-agenda-ndays'.
  18609. START-DAY defaults to TODAY, or to the most recent match for the weekday
  18610. given in `org-agenda-start-on-weekday'."
  18611. (interactive "P")
  18612. (if (and (integerp include-all) (> include-all 0))
  18613. (setq ndays include-all include-all nil))
  18614. (setq ndays (or ndays org-agenda-ndays)
  18615. start-day (or start-day org-agenda-start-day))
  18616. (if org-agenda-overriding-arguments
  18617. (setq include-all (car org-agenda-overriding-arguments)
  18618. start-day (nth 1 org-agenda-overriding-arguments)
  18619. ndays (nth 2 org-agenda-overriding-arguments)))
  18620. (if (stringp start-day)
  18621. ;; Convert to an absolute day number
  18622. (setq start-day (time-to-days (org-read-date nil t start-day))))
  18623. (setq org-agenda-last-arguments (list include-all start-day ndays))
  18624. (org-compile-prefix-format 'agenda)
  18625. (org-set-sorting-strategy 'agenda)
  18626. (require 'calendar)
  18627. (let* ((org-agenda-start-on-weekday
  18628. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  18629. org-agenda-start-on-weekday nil))
  18630. (thefiles (org-agenda-files))
  18631. (files thefiles)
  18632. (today (time-to-days
  18633. (time-subtract (current-time)
  18634. (list 0 (* 3600 org-extend-today-until) 0))))
  18635. (sd (or start-day today))
  18636. (start (if (or (null org-agenda-start-on-weekday)
  18637. (< org-agenda-ndays 7))
  18638. sd
  18639. (let* ((nt (calendar-day-of-week
  18640. (calendar-gregorian-from-absolute sd)))
  18641. (n1 org-agenda-start-on-weekday)
  18642. (d (- nt n1)))
  18643. (- sd (+ (if (< d 0) 7 0) d)))))
  18644. (day-numbers (list start))
  18645. (day-cnt 0)
  18646. (inhibit-redisplay (not debug-on-error))
  18647. s e rtn rtnall file date d start-pos end-pos todayp nd)
  18648. (setq org-agenda-redo-command
  18649. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  18650. ;; Make the list of days
  18651. (setq ndays (or ndays org-agenda-ndays)
  18652. nd ndays)
  18653. (while (> ndays 1)
  18654. (push (1+ (car day-numbers)) day-numbers)
  18655. (setq ndays (1- ndays)))
  18656. (setq day-numbers (nreverse day-numbers))
  18657. (org-prepare-agenda "Day/Week")
  18658. (org-set-local 'org-starting-day (car day-numbers))
  18659. (org-set-local 'org-include-all-loc include-all)
  18660. (org-set-local 'org-agenda-span
  18661. (org-agenda-ndays-to-span nd))
  18662. (when (and (or include-all org-agenda-include-all-todo)
  18663. (member today day-numbers))
  18664. (setq files thefiles
  18665. rtnall nil)
  18666. (while (setq file (pop files))
  18667. (catch 'nextfile
  18668. (org-check-agenda-file file)
  18669. (setq date (calendar-gregorian-from-absolute today)
  18670. rtn (org-agenda-get-day-entries
  18671. file date :todo))
  18672. (setq rtnall (append rtnall rtn))))
  18673. (when rtnall
  18674. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  18675. (add-text-properties (point-min) (1- (point))
  18676. (list 'face 'org-agenda-structure))
  18677. (insert (org-finalize-agenda-entries rtnall) "\n")))
  18678. (unless org-agenda-compact-blocks
  18679. (setq s (point))
  18680. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  18681. "-agenda:\n")
  18682. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  18683. 'org-date-line t)))
  18684. (while (setq d (pop day-numbers))
  18685. (setq date (calendar-gregorian-from-absolute d)
  18686. s (point))
  18687. (if (or (setq todayp (= d today))
  18688. (and (not start-pos) (= d sd)))
  18689. (setq start-pos (point))
  18690. (if (and start-pos (not end-pos))
  18691. (setq end-pos (point))))
  18692. (setq files thefiles
  18693. rtnall nil)
  18694. (while (setq file (pop files))
  18695. (catch 'nextfile
  18696. (org-check-agenda-file file)
  18697. (if org-agenda-show-log
  18698. (setq rtn (org-agenda-get-day-entries
  18699. file date
  18700. :deadline :scheduled :timestamp :sexp :closed))
  18701. (setq rtn (org-agenda-get-day-entries
  18702. file date
  18703. :deadline :scheduled :sexp :timestamp)))
  18704. (setq rtnall (append rtnall rtn))))
  18705. (if org-agenda-include-diary
  18706. (progn
  18707. (require 'diary-lib)
  18708. (setq rtn (org-get-entries-from-diary date))
  18709. (setq rtnall (append rtnall rtn))))
  18710. (if (or rtnall org-agenda-show-all-dates)
  18711. (progn
  18712. (setq day-cnt (1+ day-cnt))
  18713. (insert
  18714. (if (stringp org-agenda-format-date)
  18715. (format-time-string org-agenda-format-date
  18716. (org-time-from-absolute date))
  18717. (funcall org-agenda-format-date date))
  18718. "\n")
  18719. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18720. (put-text-property s (1- (point)) 'org-date-line t)
  18721. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  18722. (if todayp (put-text-property s (1- (point)) 'org-today t))
  18723. (if rtnall (insert
  18724. (org-finalize-agenda-entries
  18725. (org-agenda-add-time-grid-maybe
  18726. rtnall nd todayp))
  18727. "\n"))
  18728. (put-text-property s (1- (point)) 'day d)
  18729. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  18730. (goto-char (point-min))
  18731. (org-fit-agenda-window)
  18732. (unless (and (pos-visible-in-window-p (point-min))
  18733. (pos-visible-in-window-p (point-max)))
  18734. (goto-char (1- (point-max)))
  18735. (recenter -1)
  18736. (if (not (pos-visible-in-window-p (or start-pos 1)))
  18737. (progn
  18738. (goto-char (or start-pos 1))
  18739. (recenter 1))))
  18740. (goto-char (or start-pos 1))
  18741. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  18742. (org-finalize-agenda)
  18743. (setq buffer-read-only t)
  18744. (message "")))
  18745. (defun org-agenda-ndays-to-span (n)
  18746. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  18747. ;;; Agenda TODO list
  18748. (defvar org-select-this-todo-keyword nil)
  18749. (defvar org-last-arg nil)
  18750. ;;;###autoload
  18751. (defun org-todo-list (arg)
  18752. "Show all TODO entries from all agenda file in a single list.
  18753. The prefix arg can be used to select a specific TODO keyword and limit
  18754. the list to these. When using \\[universal-argument], you will be prompted
  18755. for a keyword. A numeric prefix directly selects the Nth keyword in
  18756. `org-todo-keywords-1'."
  18757. (interactive "P")
  18758. (require 'calendar)
  18759. (org-compile-prefix-format 'todo)
  18760. (org-set-sorting-strategy 'todo)
  18761. (org-prepare-agenda "TODO")
  18762. (let* ((today (time-to-days (current-time)))
  18763. (date (calendar-gregorian-from-absolute today))
  18764. (kwds org-todo-keywords-for-agenda)
  18765. (completion-ignore-case t)
  18766. (org-select-this-todo-keyword
  18767. (if (stringp arg) arg
  18768. (and arg (integerp arg) (> arg 0)
  18769. (nth (1- arg) kwds))))
  18770. rtn rtnall files file pos)
  18771. (when (equal arg '(4))
  18772. (setq org-select-this-todo-keyword
  18773. (completing-read "Keyword (or KWD1|K2D2|...): "
  18774. (mapcar 'list kwds) nil nil)))
  18775. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  18776. (org-set-local 'org-last-arg arg)
  18777. (setq org-agenda-redo-command
  18778. '(org-todo-list (or current-prefix-arg org-last-arg)))
  18779. (setq files (org-agenda-files)
  18780. rtnall nil)
  18781. (while (setq file (pop files))
  18782. (catch 'nextfile
  18783. (org-check-agenda-file file)
  18784. (setq rtn (org-agenda-get-day-entries file date :todo))
  18785. (setq rtnall (append rtnall rtn))))
  18786. (if org-agenda-overriding-header
  18787. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  18788. nil 'face 'org-agenda-structure) "\n")
  18789. (insert "Global list of TODO items of type: ")
  18790. (add-text-properties (point-min) (1- (point))
  18791. (list 'face 'org-agenda-structure))
  18792. (setq pos (point))
  18793. (insert (or org-select-this-todo-keyword "ALL") "\n")
  18794. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  18795. (setq pos (point))
  18796. (unless org-agenda-multi
  18797. (insert "Available with `N r': (0)ALL")
  18798. (let ((n 0) s)
  18799. (mapc (lambda (x)
  18800. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  18801. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  18802. (insert "\n "))
  18803. (insert " " s))
  18804. kwds))
  18805. (insert "\n"))
  18806. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  18807. (when rtnall
  18808. (insert (org-finalize-agenda-entries rtnall) "\n"))
  18809. (goto-char (point-min))
  18810. (org-fit-agenda-window)
  18811. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  18812. (org-finalize-agenda)
  18813. (setq buffer-read-only t)))
  18814. ;;; Agenda tags match
  18815. ;;;###autoload
  18816. (defun org-tags-view (&optional todo-only match)
  18817. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  18818. The prefix arg TODO-ONLY limits the search to TODO entries."
  18819. (interactive "P")
  18820. (org-compile-prefix-format 'tags)
  18821. (org-set-sorting-strategy 'tags)
  18822. (let* ((org-tags-match-list-sublevels
  18823. (if todo-only t org-tags-match-list-sublevels))
  18824. (completion-ignore-case t)
  18825. rtn rtnall files file pos matcher
  18826. buffer)
  18827. (setq matcher (org-make-tags-matcher match)
  18828. match (car matcher) matcher (cdr matcher))
  18829. (org-prepare-agenda (concat "TAGS " match))
  18830. (setq org-agenda-redo-command
  18831. (list 'org-tags-view (list 'quote todo-only)
  18832. (list 'if 'current-prefix-arg nil match)))
  18833. (setq files (org-agenda-files)
  18834. rtnall nil)
  18835. (while (setq file (pop files))
  18836. (catch 'nextfile
  18837. (org-check-agenda-file file)
  18838. (setq buffer (if (file-exists-p file)
  18839. (org-get-agenda-file-buffer file)
  18840. (error "No such file %s" file)))
  18841. (if (not buffer)
  18842. ;; If file does not exist, merror message to agenda
  18843. (setq rtn (list
  18844. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  18845. rtnall (append rtnall rtn))
  18846. (with-current-buffer buffer
  18847. (unless (org-mode-p)
  18848. (error "Agenda file %s is not in `org-mode'" file))
  18849. (save-excursion
  18850. (save-restriction
  18851. (if org-agenda-restrict
  18852. (narrow-to-region org-agenda-restrict-begin
  18853. org-agenda-restrict-end)
  18854. (widen))
  18855. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  18856. (setq rtnall (append rtnall rtn))))))))
  18857. (if org-agenda-overriding-header
  18858. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  18859. nil 'face 'org-agenda-structure) "\n")
  18860. (insert "Headlines with TAGS match: ")
  18861. (add-text-properties (point-min) (1- (point))
  18862. (list 'face 'org-agenda-structure))
  18863. (setq pos (point))
  18864. (insert match "\n")
  18865. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  18866. (setq pos (point))
  18867. (unless org-agenda-multi
  18868. (insert "Press `C-u r' to search again with new search string\n"))
  18869. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  18870. (when rtnall
  18871. (insert (org-finalize-agenda-entries rtnall) "\n"))
  18872. (goto-char (point-min))
  18873. (org-fit-agenda-window)
  18874. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  18875. (org-finalize-agenda)
  18876. (setq buffer-read-only t)))
  18877. ;;; Agenda Finding stuck projects
  18878. (defvar org-agenda-skip-regexp nil
  18879. "Regular expression used in skipping subtrees for the agenda.
  18880. This is basically a temporary global variable that can be set and then
  18881. used by user-defined selections using `org-agenda-skip-function'.")
  18882. (defvar org-agenda-overriding-header nil
  18883. "When this is set during todo and tags searches, will replace header.")
  18884. (defun org-agenda-skip-subtree-when-regexp-matches ()
  18885. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  18886. If yes, it returns the end position of this tree, causing agenda commands
  18887. to skip this subtree. This is a function that can be put into
  18888. `org-agenda-skip-function' for the duration of a command."
  18889. (let ((end (save-excursion (org-end-of-subtree t)))
  18890. skip)
  18891. (save-excursion
  18892. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  18893. (and skip end)))
  18894. (defun org-agenda-skip-entry-if (&rest conditions)
  18895. "Skip entry if any of CONDITIONS is true.
  18896. See `org-agenda-skip-if' for details."
  18897. (org-agenda-skip-if nil conditions))
  18898. (defun org-agenda-skip-subtree-if (&rest conditions)
  18899. "Skip entry if any of CONDITIONS is true.
  18900. See `org-agenda-skip-if' for details."
  18901. (org-agenda-skip-if t conditions))
  18902. (defun org-agenda-skip-if (subtree conditions)
  18903. "Checks current entity for CONDITIONS.
  18904. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  18905. the entry, i.e. the text before the next heading is checked.
  18906. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  18907. from different tests. Valid conditions are:
  18908. scheduled Check if there is a scheduled cookie
  18909. notscheduled Check if there is no scheduled cookie
  18910. deadline Check if there is a deadline
  18911. notdeadline Check if there is no deadline
  18912. regexp Check if regexp matches
  18913. notregexp Check if regexp does not match.
  18914. The regexp is taken from the conditions list, it must come right after
  18915. the `regexp' or `notregexp' element.
  18916. If any of these conditions is met, this function returns the end point of
  18917. the entity, causing the search to continue from there. This is a function
  18918. that can be put into `org-agenda-skip-function' for the duration of a command."
  18919. (let (beg end m)
  18920. (org-back-to-heading t)
  18921. (setq beg (point)
  18922. end (if subtree
  18923. (progn (org-end-of-subtree t) (point))
  18924. (progn (outline-next-heading) (1- (point)))))
  18925. (goto-char beg)
  18926. (and
  18927. (or
  18928. (and (memq 'scheduled conditions)
  18929. (re-search-forward org-scheduled-time-regexp end t))
  18930. (and (memq 'notscheduled conditions)
  18931. (not (re-search-forward org-scheduled-time-regexp end t)))
  18932. (and (memq 'deadline conditions)
  18933. (re-search-forward org-deadline-time-regexp end t))
  18934. (and (memq 'notdeadline conditions)
  18935. (not (re-search-forward org-deadline-time-regexp end t)))
  18936. (and (setq m (memq 'regexp conditions))
  18937. (stringp (nth 1 m))
  18938. (re-search-forward (nth 1 m) end t))
  18939. (and (setq m (memq 'notregexp conditions))
  18940. (stringp (nth 1 m))
  18941. (not (re-search-forward (nth 1 m) end t))))
  18942. end)))
  18943. ;;;###autoload
  18944. (defun org-agenda-list-stuck-projects (&rest ignore)
  18945. "Create agenda view for projects that are stuck.
  18946. Stuck projects are project that have no next actions. For the definitions
  18947. of what a project is and how to check if it stuck, customize the variable
  18948. `org-stuck-projects'.
  18949. MATCH is being ignored."
  18950. (interactive)
  18951. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  18952. ;; FIXME: we could have used org-agenda-skip-if here.
  18953. (org-agenda-overriding-header "List of stuck projects: ")
  18954. (matcher (nth 0 org-stuck-projects))
  18955. (todo (nth 1 org-stuck-projects))
  18956. (todo-wds (if (member "*" todo)
  18957. (progn
  18958. (org-prepare-agenda-buffers (org-agenda-files))
  18959. (org-delete-all
  18960. org-done-keywords-for-agenda
  18961. (copy-sequence org-todo-keywords-for-agenda)))
  18962. todo))
  18963. (todo-re (concat "^\\*+[ \t]+\\("
  18964. (mapconcat 'identity todo-wds "\\|")
  18965. "\\)\\>"))
  18966. (tags (nth 2 org-stuck-projects))
  18967. (tags-re (if (member "*" tags)
  18968. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  18969. (concat "^\\*+ .*:\\("
  18970. (mapconcat 'identity tags "\\|")
  18971. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  18972. (gen-re (nth 3 org-stuck-projects))
  18973. (re-list
  18974. (delq nil
  18975. (list
  18976. (if todo todo-re)
  18977. (if tags tags-re)
  18978. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  18979. gen-re)))))
  18980. (setq org-agenda-skip-regexp
  18981. (if re-list
  18982. (mapconcat 'identity re-list "\\|")
  18983. (error "No information how to identify unstuck projects")))
  18984. (org-tags-view nil matcher)
  18985. (with-current-buffer org-agenda-buffer-name
  18986. (setq org-agenda-redo-command
  18987. '(org-agenda-list-stuck-projects
  18988. (or current-prefix-arg org-last-arg))))))
  18989. ;;; Diary integration
  18990. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  18991. (defun org-get-entries-from-diary (date)
  18992. "Get the (Emacs Calendar) diary entries for DATE."
  18993. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  18994. (diary-display-hook '(fancy-diary-display))
  18995. (pop-up-frames nil)
  18996. (list-diary-entries-hook
  18997. (cons 'org-diary-default-entry list-diary-entries-hook))
  18998. (diary-file-name-prefix-function nil) ; turn this feature off
  18999. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19000. entries
  19001. (org-disable-agenda-to-diary t))
  19002. (save-excursion
  19003. (save-window-excursion
  19004. (funcall (if (fboundp 'diary-list-entries)
  19005. 'diary-list-entries 'list-diary-entries)
  19006. date 1)))
  19007. (if (not (get-buffer fancy-diary-buffer))
  19008. (setq entries nil)
  19009. (with-current-buffer fancy-diary-buffer
  19010. (setq buffer-read-only nil)
  19011. (if (zerop (buffer-size))
  19012. ;; No entries
  19013. (setq entries nil)
  19014. ;; Omit the date and other unnecessary stuff
  19015. (org-agenda-cleanup-fancy-diary)
  19016. ;; Add prefix to each line and extend the text properties
  19017. (if (zerop (buffer-size))
  19018. (setq entries nil)
  19019. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19020. (set-buffer-modified-p nil)
  19021. (kill-buffer fancy-diary-buffer)))
  19022. (when entries
  19023. (setq entries (org-split-string entries "\n"))
  19024. (setq entries
  19025. (mapcar
  19026. (lambda (x)
  19027. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19028. ;; Extend the text properties to the beginning of the line
  19029. (org-add-props x (text-properties-at (1- (length x)) x)
  19030. 'type "diary" 'date date))
  19031. entries)))))
  19032. (defun org-agenda-cleanup-fancy-diary ()
  19033. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19034. This gets rid of the date, the underline under the date, and
  19035. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19036. date. It also removes lines that contain only whitespace."
  19037. (goto-char (point-min))
  19038. (if (looking-at ".*?:[ \t]*")
  19039. (progn
  19040. (replace-match "")
  19041. (re-search-forward "\n=+$" nil t)
  19042. (replace-match "")
  19043. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19044. (re-search-forward "\n=+$" nil t)
  19045. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19046. (goto-char (point-min))
  19047. (while (re-search-forward "^ +\n" nil t)
  19048. (replace-match ""))
  19049. (goto-char (point-min))
  19050. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19051. (replace-match "")))
  19052. ;; Make sure entries from the diary have the right text properties.
  19053. (eval-after-load "diary-lib"
  19054. '(if (boundp 'diary-modify-entry-list-string-function)
  19055. ;; We can rely on the hook, nothing to do
  19056. nil
  19057. ;; Hook not avaiable, must use advice to make this work
  19058. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19059. "Make the position visible."
  19060. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19061. (stringp string)
  19062. buffer-file-name)
  19063. (setq string (org-modify-diary-entry-string string))))))
  19064. (defun org-modify-diary-entry-string (string)
  19065. "Add text properties to string, allowing org-mode to act on it."
  19066. (org-add-props string nil
  19067. 'mouse-face 'highlight
  19068. 'keymap org-agenda-keymap
  19069. 'help-echo (if buffer-file-name
  19070. (format "mouse-2 or RET jump to diary file %s"
  19071. (abbreviate-file-name buffer-file-name))
  19072. "")
  19073. 'org-agenda-diary-link t
  19074. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19075. (defun org-diary-default-entry ()
  19076. "Add a dummy entry to the diary.
  19077. Needed to avoid empty dates which mess up holiday display."
  19078. ;; Catch the error if dealing with the new add-to-diary-alist
  19079. (when org-disable-agenda-to-diary
  19080. (condition-case nil
  19081. (add-to-diary-list original-date "Org-mode dummy" "")
  19082. (error
  19083. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19084. ;;;###autoload
  19085. (defun org-diary (&rest args)
  19086. "Return diary information from org-files.
  19087. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19088. It accesses org files and extracts information from those files to be
  19089. listed in the diary. The function accepts arguments specifying what
  19090. items should be listed. The following arguments are allowed:
  19091. :timestamp List the headlines of items containing a date stamp or
  19092. date range matching the selected date. Deadlines will
  19093. also be listed, on the expiration day.
  19094. :sexp List entries resulting from diary-like sexps.
  19095. :deadline List any deadlines past due, or due within
  19096. `org-deadline-warning-days'. The listing occurs only
  19097. in the diary for *today*, not at any other date. If
  19098. an entry is marked DONE, it is no longer listed.
  19099. :scheduled List all items which are scheduled for the given date.
  19100. The diary for *today* also contains items which were
  19101. scheduled earlier and are not yet marked DONE.
  19102. :todo List all TODO items from the org-file. This may be a
  19103. long list - so this is not turned on by default.
  19104. Like deadlines, these entries only show up in the
  19105. diary for *today*, not at any other date.
  19106. The call in the diary file should look like this:
  19107. &%%(org-diary) ~/path/to/some/orgfile.org
  19108. Use a separate line for each org file to check. Or, if you omit the file name,
  19109. all files listed in `org-agenda-files' will be checked automatically:
  19110. &%%(org-diary)
  19111. If you don't give any arguments (as in the example above), the default
  19112. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19113. So the example above may also be written as
  19114. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19115. The function expects the lisp variables `entry' and `date' to be provided
  19116. by the caller, because this is how the calendar works. Don't use this
  19117. function from a program - use `org-agenda-get-day-entries' instead."
  19118. (org-agenda-maybe-reset-markers)
  19119. (org-compile-prefix-format 'agenda)
  19120. (org-set-sorting-strategy 'agenda)
  19121. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19122. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19123. (list entry)
  19124. (org-agenda-files t)))
  19125. file rtn results)
  19126. (org-prepare-agenda-buffers files)
  19127. ;; If this is called during org-agenda, don't return any entries to
  19128. ;; the calendar. Org Agenda will list these entries itself.
  19129. (if org-disable-agenda-to-diary (setq files nil))
  19130. (while (setq file (pop files))
  19131. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19132. (setq results (append results rtn)))
  19133. (if results
  19134. (concat (org-finalize-agenda-entries results) "\n"))))
  19135. ;;; Agenda entry finders
  19136. (defun org-agenda-get-day-entries (file date &rest args)
  19137. "Does the work for `org-diary' and `org-agenda'.
  19138. FILE is the path to a file to be checked for entries. DATE is date like
  19139. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19140. which kind of entries should be extracted. For details about these, see
  19141. the documentation of `org-diary'."
  19142. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19143. (let* ((org-startup-folded nil)
  19144. (org-startup-align-all-tables nil)
  19145. (buffer (if (file-exists-p file)
  19146. (org-get-agenda-file-buffer file)
  19147. (error "No such file %s" file)))
  19148. arg results rtn)
  19149. (if (not buffer)
  19150. ;; If file does not exist, make sure an error message ends up in diary
  19151. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19152. (with-current-buffer buffer
  19153. (unless (org-mode-p)
  19154. (error "Agenda file %s is not in `org-mode'" file))
  19155. (let ((case-fold-search nil))
  19156. (save-excursion
  19157. (save-restriction
  19158. (if org-agenda-restrict
  19159. (narrow-to-region org-agenda-restrict-begin
  19160. org-agenda-restrict-end)
  19161. (widen))
  19162. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19163. (while (setq arg (pop args))
  19164. (cond
  19165. ((and (eq arg :todo)
  19166. (equal date (calendar-current-date)))
  19167. (setq rtn (org-agenda-get-todos))
  19168. (setq results (append results rtn)))
  19169. ((eq arg :timestamp)
  19170. (setq rtn (org-agenda-get-blocks))
  19171. (setq results (append results rtn))
  19172. (setq rtn (org-agenda-get-timestamps))
  19173. (setq results (append results rtn)))
  19174. ((eq arg :sexp)
  19175. (setq rtn (org-agenda-get-sexps))
  19176. (setq results (append results rtn)))
  19177. ((eq arg :scheduled)
  19178. (setq rtn (org-agenda-get-scheduled))
  19179. (setq results (append results rtn)))
  19180. ((eq arg :closed)
  19181. (setq rtn (org-agenda-get-closed))
  19182. (setq results (append results rtn)))
  19183. ((eq arg :deadline)
  19184. (setq rtn (org-agenda-get-deadlines))
  19185. (setq results (append results rtn))))))))
  19186. results))))
  19187. (defun org-entry-is-todo-p ()
  19188. (member (org-get-todo-state) org-not-done-keywords))
  19189. (defun org-entry-is-done-p ()
  19190. (member (org-get-todo-state) org-done-keywords))
  19191. (defun org-get-todo-state ()
  19192. (save-excursion
  19193. (org-back-to-heading t)
  19194. (and (looking-at org-todo-line-regexp)
  19195. (match-end 2)
  19196. (match-string 2))))
  19197. (defun org-at-date-range-p (&optional inactive-ok)
  19198. "Is the cursor inside a date range?"
  19199. (interactive)
  19200. (save-excursion
  19201. (catch 'exit
  19202. (let ((pos (point)))
  19203. (skip-chars-backward "^[<\r\n")
  19204. (skip-chars-backward "<[")
  19205. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19206. (>= (match-end 0) pos)
  19207. (throw 'exit t))
  19208. (skip-chars-backward "^<[\r\n")
  19209. (skip-chars-backward "<[")
  19210. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19211. (>= (match-end 0) pos)
  19212. (throw 'exit t)))
  19213. nil)))
  19214. (defun org-agenda-get-todos ()
  19215. "Return the TODO information for agenda display."
  19216. (let* ((props (list 'face nil
  19217. 'done-face 'org-done
  19218. 'org-not-done-regexp org-not-done-regexp
  19219. 'org-todo-regexp org-todo-regexp
  19220. 'mouse-face 'highlight
  19221. 'keymap org-agenda-keymap
  19222. 'help-echo
  19223. (format "mouse-2 or RET jump to org file %s"
  19224. (abbreviate-file-name buffer-file-name))))
  19225. ;; FIXME: get rid of the \n at some point but watch out
  19226. (regexp (concat "^\\*+[ \t]+\\("
  19227. (if org-select-this-todo-keyword
  19228. (if (equal org-select-this-todo-keyword "*")
  19229. org-todo-regexp
  19230. (concat "\\<\\("
  19231. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19232. "\\)\\>"))
  19233. org-not-done-regexp)
  19234. "[^\n\r]*\\)"))
  19235. marker priority category tags
  19236. ee txt beg end)
  19237. (goto-char (point-min))
  19238. (while (re-search-forward regexp nil t)
  19239. (catch :skip
  19240. (save-match-data
  19241. (beginning-of-line)
  19242. (setq beg (point) end (progn (outline-next-heading) (point)))
  19243. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19244. (re-search-forward org-ts-regexp end t))
  19245. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19246. (re-search-forward org-scheduled-time-regexp end t))
  19247. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19248. (re-search-forward org-deadline-time-regexp end t)
  19249. (org-deadline-close (match-string 1))))
  19250. (goto-char (1+ beg))
  19251. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19252. (throw :skip nil)))
  19253. (goto-char beg)
  19254. (org-agenda-skip)
  19255. (goto-char (match-beginning 1))
  19256. (setq marker (org-agenda-new-marker (match-beginning 0))
  19257. category (org-get-category)
  19258. tags (org-get-tags-at (point))
  19259. txt (org-format-agenda-item "" (match-string 1) category tags)
  19260. priority (1+ (org-get-priority txt)))
  19261. (org-add-props txt props
  19262. 'org-marker marker 'org-hd-marker marker
  19263. 'priority priority 'org-category category
  19264. 'type "todo")
  19265. (push txt ee)
  19266. (if org-agenda-todo-list-sublevels
  19267. (goto-char (match-end 1))
  19268. (org-end-of-subtree 'invisible))))
  19269. (nreverse ee)))
  19270. (defconst org-agenda-no-heading-message
  19271. "No heading for this item in buffer or region.")
  19272. (defun org-agenda-get-timestamps ()
  19273. "Return the date stamp information for agenda display."
  19274. (let* ((props (list 'face nil
  19275. 'org-not-done-regexp org-not-done-regexp
  19276. 'org-todo-regexp org-todo-regexp
  19277. 'mouse-face 'highlight
  19278. 'keymap org-agenda-keymap
  19279. 'help-echo
  19280. (format "mouse-2 or RET jump to org file %s"
  19281. (abbreviate-file-name buffer-file-name))))
  19282. (d1 (calendar-absolute-from-gregorian date))
  19283. (remove-re
  19284. (concat
  19285. (regexp-quote
  19286. (format-time-string
  19287. "<%Y-%m-%d"
  19288. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19289. ".*?>"))
  19290. (regexp
  19291. (concat
  19292. (regexp-quote
  19293. (substring
  19294. (format-time-string
  19295. (car org-time-stamp-formats)
  19296. (apply 'encode-time ; DATE bound by calendar
  19297. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19298. 0 11))
  19299. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19300. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19301. marker hdmarker deadlinep scheduledp donep tmp priority category
  19302. ee txt timestr tags b0 b3 e3 head)
  19303. (goto-char (point-min))
  19304. (while (re-search-forward regexp nil t)
  19305. (setq b0 (match-beginning 0)
  19306. b3 (match-beginning 3) e3 (match-end 3))
  19307. (catch :skip
  19308. (and (org-at-date-range-p) (throw :skip nil))
  19309. (org-agenda-skip)
  19310. (if (and (match-end 1)
  19311. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19312. (throw :skip nil))
  19313. (if (and e3
  19314. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19315. (throw :skip nil))
  19316. (setq marker (org-agenda-new-marker b0)
  19317. category (org-get-category b0)
  19318. tmp (buffer-substring (max (point-min)
  19319. (- b0 org-ds-keyword-length))
  19320. b0)
  19321. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19322. deadlinep (string-match org-deadline-regexp tmp)
  19323. scheduledp (string-match org-scheduled-regexp tmp)
  19324. donep (org-entry-is-done-p))
  19325. (if (or scheduledp deadlinep) (throw :skip t))
  19326. (if (string-match ">" timestr)
  19327. ;; substring should only run to end of time stamp
  19328. (setq timestr (substring timestr 0 (match-end 0))))
  19329. (save-excursion
  19330. (if (re-search-backward "^\\*+ " nil t)
  19331. (progn
  19332. (goto-char (match-beginning 0))
  19333. (setq hdmarker (org-agenda-new-marker)
  19334. tags (org-get-tags-at))
  19335. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19336. (setq head (match-string 1))
  19337. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19338. (setq txt (org-format-agenda-item
  19339. nil head category tags timestr nil
  19340. remove-re)))
  19341. (setq txt org-agenda-no-heading-message))
  19342. (setq priority (org-get-priority txt))
  19343. (org-add-props txt props
  19344. 'org-marker marker 'org-hd-marker hdmarker)
  19345. (org-add-props txt nil 'priority priority
  19346. 'org-category category 'date date
  19347. 'type "timestamp")
  19348. (push txt ee))
  19349. (outline-next-heading)))
  19350. (nreverse ee)))
  19351. (defun org-agenda-get-sexps ()
  19352. "Return the sexp information for agenda display."
  19353. (require 'diary-lib)
  19354. (let* ((props (list 'face nil
  19355. 'mouse-face 'highlight
  19356. 'keymap org-agenda-keymap
  19357. 'help-echo
  19358. (format "mouse-2 or RET jump to org file %s"
  19359. (abbreviate-file-name buffer-file-name))))
  19360. (regexp "^&?%%(")
  19361. marker category ee txt tags entry result beg b sexp sexp-entry)
  19362. (goto-char (point-min))
  19363. (while (re-search-forward regexp nil t)
  19364. (catch :skip
  19365. (org-agenda-skip)
  19366. (setq beg (match-beginning 0))
  19367. (goto-char (1- (match-end 0)))
  19368. (setq b (point))
  19369. (forward-sexp 1)
  19370. (setq sexp (buffer-substring b (point)))
  19371. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  19372. (org-trim (match-string 1))
  19373. ""))
  19374. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  19375. (when result
  19376. (setq marker (org-agenda-new-marker beg)
  19377. category (org-get-category beg))
  19378. (if (string-match "\\S-" result)
  19379. (setq txt result)
  19380. (setq txt "SEXP entry returned empty string"))
  19381. (setq txt (org-format-agenda-item
  19382. "" txt category tags 'time))
  19383. (org-add-props txt props 'org-marker marker)
  19384. (org-add-props txt nil
  19385. 'org-category category 'date date
  19386. 'type "sexp")
  19387. (push txt ee))))
  19388. (nreverse ee)))
  19389. (defun org-agenda-get-closed ()
  19390. "Return the logged TODO entries for agenda display."
  19391. (let* ((props (list 'mouse-face 'highlight
  19392. 'org-not-done-regexp org-not-done-regexp
  19393. 'org-todo-regexp org-todo-regexp
  19394. 'keymap org-agenda-keymap
  19395. 'help-echo
  19396. (format "mouse-2 or RET jump to org file %s"
  19397. (abbreviate-file-name buffer-file-name))))
  19398. (regexp (concat
  19399. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  19400. (regexp-quote
  19401. (substring
  19402. (format-time-string
  19403. (car org-time-stamp-formats)
  19404. (apply 'encode-time ; DATE bound by calendar
  19405. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19406. 1 11))))
  19407. marker hdmarker priority category tags closedp
  19408. ee txt timestr)
  19409. (goto-char (point-min))
  19410. (while (re-search-forward regexp nil t)
  19411. (catch :skip
  19412. (org-agenda-skip)
  19413. (setq marker (org-agenda-new-marker (match-beginning 0))
  19414. closedp (equal (match-string 1) org-closed-string)
  19415. category (org-get-category (match-beginning 0))
  19416. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  19417. ;; donep (org-entry-is-done-p)
  19418. )
  19419. (if (string-match "\\]" timestr)
  19420. ;; substring should only run to end of time stamp
  19421. (setq timestr (substring timestr 0 (match-end 0))))
  19422. (save-excursion
  19423. (if (re-search-backward "^\\*+ " nil t)
  19424. (progn
  19425. (goto-char (match-beginning 0))
  19426. (setq hdmarker (org-agenda-new-marker)
  19427. tags (org-get-tags-at))
  19428. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19429. (setq txt (org-format-agenda-item
  19430. (if closedp "Closed: " "Clocked: ")
  19431. (match-string 1) category tags timestr)))
  19432. (setq txt org-agenda-no-heading-message))
  19433. (setq priority 100000)
  19434. (org-add-props txt props
  19435. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  19436. 'priority priority 'org-category category
  19437. 'type "closed" 'date date
  19438. 'undone-face 'org-warning 'done-face 'org-done)
  19439. (push txt ee))
  19440. (outline-next-heading)))
  19441. (nreverse ee)))
  19442. (defun org-agenda-get-deadlines ()
  19443. "Return the deadline information for agenda display."
  19444. (let* ((props (list 'mouse-face 'highlight
  19445. 'org-not-done-regexp org-not-done-regexp
  19446. 'org-todo-regexp org-todo-regexp
  19447. 'keymap org-agenda-keymap
  19448. 'help-echo
  19449. (format "mouse-2 or RET jump to org file %s"
  19450. (abbreviate-file-name buffer-file-name))))
  19451. (regexp org-deadline-time-regexp)
  19452. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19453. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19454. d2 diff dfrac wdays pos pos1 category tags
  19455. ee txt head face s upcomingp donep timestr)
  19456. (goto-char (point-min))
  19457. (while (re-search-forward regexp nil t)
  19458. (catch :skip
  19459. (org-agenda-skip)
  19460. (setq s (match-string 1)
  19461. pos (1- (match-beginning 1))
  19462. d2 (org-time-string-to-absolute (match-string 1) d1)
  19463. diff (- d2 d1)
  19464. wdays (org-get-wdays s)
  19465. dfrac (/ (* 1.0 (- wdays diff)) wdays)
  19466. upcomingp (and todayp (> diff 0)))
  19467. ;; When to show a deadline in the calendar:
  19468. ;; If the expiration is within wdays warning time.
  19469. ;; Past-due deadlines are only shown on the current date
  19470. (if (or (and (<= diff wdays)
  19471. (and todayp (not org-agenda-only-exact-dates)))
  19472. (= diff 0))
  19473. (save-excursion
  19474. (setq category (org-get-category))
  19475. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19476. (progn
  19477. (goto-char (match-end 0))
  19478. (setq pos1 (match-beginning 0))
  19479. (setq tags (org-get-tags-at pos1))
  19480. (setq head (buffer-substring-no-properties
  19481. (point)
  19482. (progn (skip-chars-forward "^\r\n")
  19483. (point))))
  19484. (setq donep (string-match org-looking-at-done-regexp head))
  19485. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19486. (setq timestr
  19487. (concat (substring s (match-beginning 1)) " "))
  19488. (setq timestr 'time))
  19489. (if (and donep
  19490. (or org-agenda-skip-deadline-if-done
  19491. (not (= diff 0))))
  19492. (setq txt nil)
  19493. (setq txt (org-format-agenda-item
  19494. (if (= diff 0)
  19495. (car org-agenda-deadline-leaders)
  19496. (format (nth 1 org-agenda-deadline-leaders)
  19497. diff))
  19498. head category tags timestr))))
  19499. (setq txt org-agenda-no-heading-message))
  19500. (when txt
  19501. (setq face (org-agenda-deadline-face dfrac))
  19502. (org-add-props txt props
  19503. 'org-marker (org-agenda-new-marker pos)
  19504. 'org-hd-marker (org-agenda-new-marker pos1)
  19505. 'priority (+ (if upcomingp (floor (* dfrac 10.)) 100)
  19506. (org-get-priority txt))
  19507. 'org-category category
  19508. 'type (if upcomingp "upcoming-deadline" "deadline")
  19509. 'date (if upcomingp date d2)
  19510. 'face (if donep 'org-done face)
  19511. 'undone-face face 'done-face 'org-done)
  19512. (push txt ee))))))
  19513. (nreverse ee)))
  19514. (defun org-agenda-deadline-face (fraction)
  19515. "Return the face to displaying a deadline item.
  19516. FRACTION is what fraction of the head-warning time has passed."
  19517. (let ((faces org-agenda-deadline-faces) f)
  19518. (catch 'exit
  19519. (while (setq f (pop faces))
  19520. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  19521. (defun org-agenda-get-scheduled ()
  19522. "Return the scheduled information for agenda display."
  19523. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  19524. 'org-todo-regexp org-todo-regexp
  19525. 'done-face 'org-done
  19526. 'mouse-face 'highlight
  19527. 'keymap org-agenda-keymap
  19528. 'help-echo
  19529. (format "mouse-2 or RET jump to org file %s"
  19530. (abbreviate-file-name buffer-file-name))))
  19531. (regexp org-scheduled-time-regexp)
  19532. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19533. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19534. d2 diff pos pos1 category tags
  19535. ee txt head pastschedp donep face timestr s)
  19536. (goto-char (point-min))
  19537. (while (re-search-forward regexp nil t)
  19538. (catch :skip
  19539. (org-agenda-skip)
  19540. (setq s (match-string 1)
  19541. pos (1- (match-beginning 1))
  19542. d2 (org-time-string-to-absolute (match-string 1) d1)
  19543. diff (- d2 d1))
  19544. (setq pastschedp (and todayp (< diff 0)))
  19545. ;; When to show a scheduled item in the calendar:
  19546. ;; If it is on or past the date.
  19547. (if (or (and (< diff 0)
  19548. (and todayp (not org-agenda-only-exact-dates)))
  19549. (= diff 0))
  19550. (save-excursion
  19551. (setq category (org-get-category))
  19552. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19553. (progn
  19554. (goto-char (match-end 0))
  19555. (setq pos1 (match-beginning 0))
  19556. (setq tags (org-get-tags-at))
  19557. (setq head (buffer-substring-no-properties
  19558. (point)
  19559. (progn (skip-chars-forward "^\r\n") (point))))
  19560. (setq donep (string-match org-looking-at-done-regexp head))
  19561. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19562. (setq timestr
  19563. (concat (substring s (match-beginning 1)) " "))
  19564. (setq timestr 'time))
  19565. (if (and donep
  19566. (or org-agenda-skip-scheduled-if-done
  19567. (not (= diff 0))))
  19568. (setq txt nil)
  19569. (setq txt (org-format-agenda-item
  19570. (if (= diff 0)
  19571. (car org-agenda-scheduled-leaders)
  19572. (format (nth 1 org-agenda-scheduled-leaders)
  19573. (- 1 diff)))
  19574. head category tags timestr))))
  19575. (setq txt org-agenda-no-heading-message))
  19576. (when txt
  19577. (setq face (if pastschedp
  19578. 'org-scheduled-previously
  19579. 'org-scheduled-today))
  19580. (org-add-props txt props
  19581. 'undone-face face
  19582. 'face (if donep 'org-done face)
  19583. 'org-marker (org-agenda-new-marker pos)
  19584. 'org-hd-marker (org-agenda-new-marker pos1)
  19585. 'type (if pastschedp "past-scheduled" "scheduled")
  19586. 'date (if pastschedp d2 date)
  19587. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  19588. 'org-category category)
  19589. (push txt ee))))))
  19590. (nreverse ee)))
  19591. (defun org-agenda-get-blocks ()
  19592. "Return the date-range information for agenda display."
  19593. (let* ((props (list 'face nil
  19594. 'org-not-done-regexp org-not-done-regexp
  19595. 'org-todo-regexp org-todo-regexp
  19596. 'mouse-face 'highlight
  19597. 'keymap org-agenda-keymap
  19598. 'help-echo
  19599. (format "mouse-2 or RET jump to org file %s"
  19600. (abbreviate-file-name buffer-file-name))))
  19601. (regexp org-tr-regexp)
  19602. (d0 (calendar-absolute-from-gregorian date))
  19603. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  19604. donep head)
  19605. (goto-char (point-min))
  19606. (while (re-search-forward regexp nil t)
  19607. (catch :skip
  19608. (org-agenda-skip)
  19609. (setq pos (point))
  19610. (setq timestr (match-string 0)
  19611. s1 (match-string 1)
  19612. s2 (match-string 2)
  19613. d1 (time-to-days (org-time-string-to-time s1))
  19614. d2 (time-to-days (org-time-string-to-time s2)))
  19615. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  19616. ;; Only allow days between the limits, because the normal
  19617. ;; date stamps will catch the limits.
  19618. (save-excursion
  19619. (setq marker (org-agenda-new-marker (point)))
  19620. (setq category (org-get-category))
  19621. (if (re-search-backward "^\\*+ " nil t)
  19622. (progn
  19623. (goto-char (match-beginning 0))
  19624. (setq hdmarker (org-agenda-new-marker (point)))
  19625. (setq tags (org-get-tags-at))
  19626. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19627. (setq head (match-string 1))
  19628. (and org-agenda-skip-timestamp-if-done
  19629. (org-entry-is-done-p)
  19630. (throw :skip t))
  19631. (setq txt (org-format-agenda-item
  19632. (format (if (= d1 d2) "" "(%d/%d): ")
  19633. (1+ (- d0 d1)) (1+ (- d2 d1)))
  19634. head category tags
  19635. (if (= d0 d1) timestr))))
  19636. (setq txt org-agenda-no-heading-message))
  19637. (org-add-props txt props
  19638. 'org-marker marker 'org-hd-marker hdmarker
  19639. 'type "block" 'date date
  19640. 'priority (org-get-priority txt) 'org-category category)
  19641. (push txt ee)))
  19642. (goto-char pos)))
  19643. ;; Sort the entries by expiration date.
  19644. (nreverse ee)))
  19645. ;;; Agenda presentation and sorting
  19646. (defconst org-plain-time-of-day-regexp
  19647. (concat
  19648. "\\(\\<[012]?[0-9]"
  19649. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19650. "\\(--?"
  19651. "\\(\\<[012]?[0-9]"
  19652. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19653. "\\)?")
  19654. "Regular expression to match a plain time or time range.
  19655. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  19656. groups carry important information:
  19657. 0 the full match
  19658. 1 the first time, range or not
  19659. 8 the second time, if it is a range.")
  19660. (defconst org-plain-time-extension-regexp
  19661. (concat
  19662. "\\(\\<[012]?[0-9]"
  19663. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19664. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  19665. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  19666. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  19667. groups carry important information:
  19668. 0 the full match
  19669. 7 hours of duration
  19670. 9 minutes of duration")
  19671. (defconst org-stamp-time-of-day-regexp
  19672. (concat
  19673. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  19674. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  19675. "\\(--?"
  19676. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  19677. "Regular expression to match a timestamp time or time range.
  19678. After a match, the following groups carry important information:
  19679. 0 the full match
  19680. 1 date plus weekday, for backreferencing to make sure both times on same day
  19681. 2 the first time, range or not
  19682. 4 the second time, if it is a range.")
  19683. (defvar org-prefix-has-time nil
  19684. "A flag, set by `org-compile-prefix-format'.
  19685. The flag is set if the currently compiled format contains a `%t'.")
  19686. (defvar org-prefix-has-tag nil
  19687. "A flag, set by `org-compile-prefix-format'.
  19688. The flag is set if the currently compiled format contains a `%T'.")
  19689. (defun org-format-agenda-item (extra txt &optional category tags dotime
  19690. noprefix remove-re)
  19691. "Format TXT to be inserted into the agenda buffer.
  19692. In particular, it adds the prefix and corresponding text properties. EXTRA
  19693. must be a string and replaces the `%s' specifier in the prefix format.
  19694. CATEGORY (string, symbol or nil) may be used to overrule the default
  19695. category taken from local variable or file name. It will replace the `%c'
  19696. specifier in the format. DOTIME, when non-nil, indicates that a
  19697. time-of-day should be extracted from TXT for sorting of this entry, and for
  19698. the `%t' specifier in the format. When DOTIME is a string, this string is
  19699. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  19700. only the correctly processes TXT should be returned - this is used by
  19701. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  19702. Any match of REMOVE-RE will be removed from TXT."
  19703. (save-match-data
  19704. ;; Diary entries sometimes have extra whitespace at the beginning
  19705. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  19706. (let* ((category (or category
  19707. org-category
  19708. (if buffer-file-name
  19709. (file-name-sans-extension
  19710. (file-name-nondirectory buffer-file-name))
  19711. "")))
  19712. (tag (if tags (nth (1- (length tags)) tags) ""))
  19713. time ; time and tag are needed for the eval of the prefix format
  19714. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  19715. (time-of-day (and dotime (org-get-time-of-day ts)))
  19716. stamp plain s0 s1 s2 rtn srp)
  19717. (when (and dotime time-of-day org-prefix-has-time)
  19718. ;; Extract starting and ending time and move them to prefix
  19719. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  19720. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  19721. (setq s0 (match-string 0 ts)
  19722. srp (and stamp (match-end 3))
  19723. s1 (match-string (if plain 1 2) ts)
  19724. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  19725. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  19726. ;; them, we might want to remove them there to avoid duplication.
  19727. ;; The user can turn this off with a variable.
  19728. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  19729. (string-match (concat (regexp-quote s0) " *") txt)
  19730. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  19731. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  19732. (= (match-beginning 0) 0)
  19733. t))
  19734. (setq txt (replace-match "" nil nil txt))))
  19735. ;; Normalize the time(s) to 24 hour
  19736. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  19737. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  19738. (when (and s1 (not s2) org-agenda-default-appointment-duration
  19739. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  19740. (let ((m (+ (string-to-number (match-string 2 s1))
  19741. (* 60 (string-to-number (match-string 1 s1)))
  19742. org-agenda-default-appointment-duration))
  19743. h)
  19744. (setq h (/ m 60) m (- m (* h 60)))
  19745. (setq s2 (format "%02d:%02d" h m))))
  19746. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  19747. txt)
  19748. ;; Tags are in the string
  19749. (if (or (eq org-agenda-remove-tags t)
  19750. (and org-agenda-remove-tags
  19751. org-prefix-has-tag))
  19752. (setq txt (replace-match "" t t txt))
  19753. (setq txt (replace-match
  19754. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  19755. (match-string 2 txt))
  19756. t t txt))))
  19757. (when remove-re
  19758. (while (string-match remove-re txt)
  19759. (setq txt (replace-match "" t t txt))))
  19760. ;; Create the final string
  19761. (if noprefix
  19762. (setq rtn txt)
  19763. ;; Prepare the variables needed in the eval of the compiled format
  19764. (setq time (cond (s2 (concat s1 "-" s2))
  19765. (s1 (concat s1 "......"))
  19766. (t ""))
  19767. extra (or extra "")
  19768. category (if (symbolp category) (symbol-name category) category))
  19769. ;; Evaluate the compiled format
  19770. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  19771. ;; And finally add the text properties
  19772. (org-add-props rtn nil
  19773. 'org-category (downcase category) 'tags tags
  19774. 'org-highest-priority org-highest-priority
  19775. 'org-lowest-priority org-lowest-priority
  19776. 'prefix-length (- (length rtn) (length txt))
  19777. 'time-of-day time-of-day
  19778. 'txt txt
  19779. 'time time
  19780. 'extra extra
  19781. 'dotime dotime))))
  19782. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  19783. (defvar org-agenda-sorting-strategy-selected nil)
  19784. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  19785. (catch 'exit
  19786. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  19787. ((and todayp (member 'today (car org-agenda-time-grid))))
  19788. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  19789. ((member 'weekly (car org-agenda-time-grid)))
  19790. (t (throw 'exit list)))
  19791. (let* ((have (delq nil (mapcar
  19792. (lambda (x) (get-text-property 1 'time-of-day x))
  19793. list)))
  19794. (string (nth 1 org-agenda-time-grid))
  19795. (gridtimes (nth 2 org-agenda-time-grid))
  19796. (req (car org-agenda-time-grid))
  19797. (remove (member 'remove-match req))
  19798. new time)
  19799. (if (and (member 'require-timed req) (not have))
  19800. ;; don't show empty grid
  19801. (throw 'exit list))
  19802. (while (setq time (pop gridtimes))
  19803. (unless (and remove (member time have))
  19804. (setq time (int-to-string time))
  19805. (push (org-format-agenda-item
  19806. nil string "" nil
  19807. (concat (substring time 0 -2) ":" (substring time -2)))
  19808. new)
  19809. (put-text-property
  19810. 1 (length (car new)) 'face 'org-time-grid (car new))))
  19811. (if (member 'time-up org-agenda-sorting-strategy-selected)
  19812. (append new list)
  19813. (append list new)))))
  19814. (defun org-compile-prefix-format (key)
  19815. "Compile the prefix format into a Lisp form that can be evaluated.
  19816. The resulting form is returned and stored in the variable
  19817. `org-prefix-format-compiled'."
  19818. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  19819. (let ((s (cond
  19820. ((stringp org-agenda-prefix-format)
  19821. org-agenda-prefix-format)
  19822. ((assq key org-agenda-prefix-format)
  19823. (cdr (assq key org-agenda-prefix-format)))
  19824. (t " %-12:c%?-12t% s")))
  19825. (start 0)
  19826. varform vars var e c f opt)
  19827. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  19828. s start)
  19829. (setq var (cdr (assoc (match-string 4 s)
  19830. '(("c" . category) ("t" . time) ("s" . extra)
  19831. ("T" . tag))))
  19832. c (or (match-string 3 s) "")
  19833. opt (match-beginning 1)
  19834. start (1+ (match-beginning 0)))
  19835. (if (equal var 'time) (setq org-prefix-has-time t))
  19836. (if (equal var 'tag) (setq org-prefix-has-tag t))
  19837. (setq f (concat "%" (match-string 2 s) "s"))
  19838. (if opt
  19839. (setq varform
  19840. `(if (equal "" ,var)
  19841. ""
  19842. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  19843. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  19844. (setq s (replace-match "%s" t nil s))
  19845. (push varform vars))
  19846. (setq vars (nreverse vars))
  19847. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  19848. (defun org-set-sorting-strategy (key)
  19849. (if (symbolp (car org-agenda-sorting-strategy))
  19850. ;; the old format
  19851. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  19852. (setq org-agenda-sorting-strategy-selected
  19853. (or (cdr (assq key org-agenda-sorting-strategy))
  19854. (cdr (assq 'agenda org-agenda-sorting-strategy))
  19855. '(time-up category-keep priority-down)))))
  19856. (defun org-get-time-of-day (s &optional string mod24)
  19857. "Check string S for a time of day.
  19858. If found, return it as a military time number between 0 and 2400.
  19859. If not found, return nil.
  19860. The optional STRING argument forces conversion into a 5 character wide string
  19861. HH:MM."
  19862. (save-match-data
  19863. (when
  19864. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  19865. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  19866. (let* ((h (string-to-number (match-string 1 s)))
  19867. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  19868. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  19869. (am-p (equal ampm "am"))
  19870. (h1 (cond ((not ampm) h)
  19871. ((= h 12) (if am-p 0 12))
  19872. (t (+ h (if am-p 0 12)))))
  19873. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  19874. (mod h1 24) h1))
  19875. (t0 (+ (* 100 h2) m))
  19876. (t1 (concat (if (>= h1 24) "+" " ")
  19877. (if (< t0 100) "0" "")
  19878. (if (< t0 10) "0" "")
  19879. (int-to-string t0))))
  19880. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  19881. (defun org-finalize-agenda-entries (list &optional nosort)
  19882. "Sort and concatenate the agenda items."
  19883. (setq list (mapcar 'org-agenda-highlight-todo list))
  19884. (if nosort
  19885. list
  19886. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  19887. (defun org-agenda-highlight-todo (x)
  19888. (let (re pl)
  19889. (if (eq x 'line)
  19890. (save-excursion
  19891. (beginning-of-line 1)
  19892. (setq re (get-text-property (point) 'org-todo-regexp))
  19893. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  19894. (and (looking-at (concat "[ \t]*\\.*" re))
  19895. (add-text-properties (match-beginning 0) (match-end 0)
  19896. (list 'face (org-get-todo-face 0)))))
  19897. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  19898. pl (get-text-property 0 'prefix-length x))
  19899. (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  19900. (add-text-properties
  19901. (or (match-end 1) (match-end 0)) (match-end 0)
  19902. (list 'face (org-get-todo-face (match-string 2 x)))
  19903. x))
  19904. x)))
  19905. (defsubst org-cmp-priority (a b)
  19906. "Compare the priorities of string A and B."
  19907. (let ((pa (or (get-text-property 1 'priority a) 0))
  19908. (pb (or (get-text-property 1 'priority b) 0)))
  19909. (cond ((> pa pb) +1)
  19910. ((< pa pb) -1)
  19911. (t nil))))
  19912. (defsubst org-cmp-category (a b)
  19913. "Compare the string values of categories of strings A and B."
  19914. (let ((ca (or (get-text-property 1 'org-category a) ""))
  19915. (cb (or (get-text-property 1 'org-category b) "")))
  19916. (cond ((string-lessp ca cb) -1)
  19917. ((string-lessp cb ca) +1)
  19918. (t nil))))
  19919. (defsubst org-cmp-tag (a b)
  19920. "Compare the string values of categories of strings A and B."
  19921. (let ((ta (car (last (get-text-property 1 'tags a))))
  19922. (tb (car (last (get-text-property 1 'tags b)))))
  19923. (cond ((not ta) +1)
  19924. ((not tb) -1)
  19925. ((string-lessp ta tb) -1)
  19926. ((string-lessp tb ta) +1)
  19927. (t nil))))
  19928. (defsubst org-cmp-time (a b)
  19929. "Compare the time-of-day values of strings A and B."
  19930. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  19931. (ta (or (get-text-property 1 'time-of-day a) def))
  19932. (tb (or (get-text-property 1 'time-of-day b) def)))
  19933. (cond ((< ta tb) -1)
  19934. ((< tb ta) +1)
  19935. (t nil))))
  19936. (defun org-entries-lessp (a b)
  19937. "Predicate for sorting agenda entries."
  19938. ;; The following variables will be used when the form is evaluated.
  19939. ;; So even though the compiler complains, keep them.
  19940. (let* ((time-up (org-cmp-time a b))
  19941. (time-down (if time-up (- time-up) nil))
  19942. (priority-up (org-cmp-priority a b))
  19943. (priority-down (if priority-up (- priority-up) nil))
  19944. (category-up (org-cmp-category a b))
  19945. (category-down (if category-up (- category-up) nil))
  19946. (category-keep (if category-up +1 nil))
  19947. (tag-up (org-cmp-tag a b))
  19948. (tag-down (if tag-up (- tag-up) nil)))
  19949. (cdr (assoc
  19950. (eval (cons 'or org-agenda-sorting-strategy-selected))
  19951. '((-1 . t) (1 . nil) (nil . nil))))))
  19952. ;;; Agenda restriction lock
  19953. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  19954. "Overlay to mark the headline to which arenda commands are restricted.")
  19955. (org-overlay-put org-agenda-restriction-lock-overlay
  19956. 'face 'org-agenda-restriction-lock)
  19957. (org-overlay-put org-agenda-restriction-lock-overlay
  19958. 'help-echo "Agendas are currently limited to this subtree.")
  19959. (org-detach-overlay org-agenda-restriction-lock-overlay)
  19960. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  19961. "Overlay marking the agenda restriction line in speedbar.")
  19962. (org-overlay-put org-speedbar-restriction-lock-overlay
  19963. 'face 'org-agenda-restriction-lock)
  19964. (org-overlay-put org-speedbar-restriction-lock-overlay
  19965. 'help-echo "Agendas are currently limited to this item.")
  19966. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19967. (defun org-agenda-set-restriction-lock (&optional type)
  19968. "Set restriction lock for agenda, to current subtree or file.
  19969. Restriction will be the file if TYPE is `file', or if type is the
  19970. universal prefix '(4), or if the cursor is before the first headline
  19971. in the file. Otherwise, restriction will be to the current subtree."
  19972. (interactive "P")
  19973. (and (equal type '(4)) (setq type 'file))
  19974. (setq type (cond
  19975. (type type)
  19976. ((org-at-heading-p) 'subtree)
  19977. ((condition-case nil (org-back-to-heading t) (error nil))
  19978. 'subtree)
  19979. (t 'file)))
  19980. (if (eq type 'subtree)
  19981. (progn
  19982. (setq org-agenda-restrict t)
  19983. (setq org-agenda-overriding-restriction 'subtree)
  19984. (put 'org-agenda-files 'org-restrict
  19985. (list (buffer-file-name (buffer-base-buffer))))
  19986. (org-back-to-heading t)
  19987. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  19988. (move-marker org-agenda-restrict-begin (point))
  19989. (move-marker org-agenda-restrict-end
  19990. (save-excursion (org-end-of-subtree t)))
  19991. (message "Locking agenda restriction to subtree"))
  19992. (put 'org-agenda-files 'org-restrict
  19993. (list (buffer-file-name (buffer-base-buffer))))
  19994. (setq org-agenda-restrict nil)
  19995. (setq org-agenda-overriding-restriction 'file)
  19996. (move-marker org-agenda-restrict-begin nil)
  19997. (move-marker org-agenda-restrict-end nil)
  19998. (message "Locking agenda restriction to file"))
  19999. (setq current-prefix-arg nil)
  20000. (org-agenda-maybe-redo))
  20001. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20002. "Remove the agenda restriction lock."
  20003. (interactive "P")
  20004. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20005. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20006. (setq org-agenda-overriding-restriction nil)
  20007. (setq org-agenda-restrict nil)
  20008. (put 'org-agenda-files 'org-restrict nil)
  20009. (move-marker org-agenda-restrict-begin nil)
  20010. (move-marker org-agenda-restrict-end nil)
  20011. (setq current-prefix-arg nil)
  20012. (message "Agenda restriction lock removed")
  20013. (or noupdate (org-agenda-maybe-redo)))
  20014. (defun org-agenda-maybe-redo ()
  20015. "If there is any window showing the agenda view, update it."
  20016. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20017. (w0 (selected-window)))
  20018. (when w
  20019. (select-window w)
  20020. (org-agenda-redo)
  20021. (select-window w0)
  20022. (if org-agenda-overriding-restriction
  20023. (message "Agenda view shifted to new %s restriction"
  20024. org-agenda-overriding-restriction)
  20025. (message "Agenda restriction lock removed")))))
  20026. ;;; Agenda commands
  20027. (defun org-agenda-check-type (error &rest types)
  20028. "Check if agenda buffer is of allowed type.
  20029. If ERROR is non-nil, throw an error, otherwise just return nil."
  20030. (if (memq org-agenda-type types)
  20031. t
  20032. (if error
  20033. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20034. nil)))
  20035. (defun org-agenda-quit ()
  20036. "Exit agenda by removing the window or the buffer."
  20037. (interactive)
  20038. (let ((buf (current-buffer)))
  20039. (if (not (one-window-p)) (delete-window))
  20040. (kill-buffer buf)
  20041. (org-agenda-maybe-reset-markers 'force)
  20042. (org-columns-remove-overlays))
  20043. ;; Maybe restore the pre-agenda window configuration.
  20044. (and org-agenda-restore-windows-after-quit
  20045. (not (eq org-agenda-window-setup 'other-frame))
  20046. org-pre-agenda-window-conf
  20047. (set-window-configuration org-pre-agenda-window-conf)))
  20048. (defun org-agenda-exit ()
  20049. "Exit agenda by removing the window or the buffer.
  20050. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20051. Org-mode buffers visited directly by the user will not be touched."
  20052. (interactive)
  20053. (org-release-buffers org-agenda-new-buffers)
  20054. (setq org-agenda-new-buffers nil)
  20055. (org-agenda-quit))
  20056. (defun org-agenda-execute (arg)
  20057. "Execute another agenda command, keeping same window.\\<global-map>
  20058. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20059. (interactive "P")
  20060. (let ((org-agenda-window-setup 'current-window))
  20061. (org-agenda arg)))
  20062. (defun org-save-all-org-buffers ()
  20063. "Save all Org-mode buffers without user confirmation."
  20064. (interactive)
  20065. (message "Saving all Org-mode buffers...")
  20066. (save-some-buffers t 'org-mode-p)
  20067. (message "Saving all Org-mode buffers... done"))
  20068. (defun org-agenda-redo ()
  20069. "Rebuild Agenda.
  20070. When this is the global TODO list, a prefix argument will be interpreted."
  20071. (interactive)
  20072. (let* ((org-agenda-keep-modes t)
  20073. (line (org-current-line))
  20074. (window-line (- line (org-current-line (window-start))))
  20075. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20076. (message "Rebuilding agenda buffer...")
  20077. (org-let lprops '(eval org-agenda-redo-command))
  20078. (setq org-agenda-undo-list nil
  20079. org-agenda-pending-undo-list nil)
  20080. (message "Rebuilding agenda buffer...done")
  20081. (goto-line line)
  20082. (recenter window-line)))
  20083. (defun org-agenda-goto-date (date)
  20084. "Jump to DATE in agenda."
  20085. (interactive (list (org-read-date)))
  20086. (org-agenda-list nil date))
  20087. (defun org-agenda-goto-today ()
  20088. "Go to today."
  20089. (interactive)
  20090. (org-agenda-check-type t 'timeline 'agenda)
  20091. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20092. (cond
  20093. (tdpos (goto-char tdpos))
  20094. ((eq org-agenda-type 'agenda)
  20095. (let* ((sd (time-to-days
  20096. (time-subtract (current-time)
  20097. (list 0 (* 3600 org-extend-today-until) 0))))
  20098. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20099. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20100. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20101. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20102. (org-agenda-redo)
  20103. (org-agenda-find-same-or-today-or-agenda)))
  20104. (t (error "Cannot find today")))))
  20105. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20106. (goto-char
  20107. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20108. (text-property-any (point-min) (point-max) 'org-today t)
  20109. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20110. (point-min))))
  20111. (defun org-agenda-later (arg)
  20112. "Go forward in time by thee current span.
  20113. With prefix ARG, go forward that many times the current span."
  20114. (interactive "p")
  20115. (org-agenda-check-type t 'agenda)
  20116. (let* ((span org-agenda-span)
  20117. (sd org-starting-day)
  20118. (greg (calendar-gregorian-from-absolute sd))
  20119. (cnt (get-text-property (point) 'org-day-cnt))
  20120. greg2 nd)
  20121. (cond
  20122. ((eq span 'day)
  20123. (setq sd (+ arg sd) nd 1))
  20124. ((eq span 'week)
  20125. (setq sd (+ (* 7 arg) sd) nd 7))
  20126. ((eq span 'month)
  20127. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20128. sd (calendar-absolute-from-gregorian greg2))
  20129. (setcar greg2 (1+ (car greg2)))
  20130. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20131. ((eq span 'year)
  20132. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20133. sd (calendar-absolute-from-gregorian greg2))
  20134. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20135. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20136. (let ((org-agenda-overriding-arguments
  20137. (list (car org-agenda-last-arguments) sd nd t)))
  20138. (org-agenda-redo)
  20139. (org-agenda-find-same-or-today-or-agenda cnt))))
  20140. (defun org-agenda-earlier (arg)
  20141. "Go backward in time by the current span.
  20142. With prefix ARG, go backward that many times the current span."
  20143. (interactive "p")
  20144. (org-agenda-later (- arg)))
  20145. (defun org-agenda-day-view ()
  20146. "Switch to daily view for agenda."
  20147. (interactive)
  20148. (setq org-agenda-ndays 1)
  20149. (org-agenda-change-time-span 'day))
  20150. (defun org-agenda-week-view ()
  20151. "Switch to daily view for agenda."
  20152. (interactive)
  20153. (setq org-agenda-ndays 7)
  20154. (org-agenda-change-time-span 'week))
  20155. (defun org-agenda-month-view ()
  20156. "Switch to daily view for agenda."
  20157. (interactive)
  20158. (org-agenda-change-time-span 'month))
  20159. (defun org-agenda-year-view ()
  20160. "Switch to daily view for agenda."
  20161. (interactive)
  20162. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20163. (org-agenda-change-time-span 'year)
  20164. (error "Abort")))
  20165. (defun org-agenda-change-time-span (span)
  20166. "Change the agenda view to SPAN.
  20167. SPAN may be `day', `week', `month', `year'."
  20168. (org-agenda-check-type t 'agenda)
  20169. (if (equal org-agenda-span span)
  20170. (error "Viewing span is already \"%s\"" span))
  20171. (let* ((sd (or (get-text-property (point) 'day)
  20172. org-starting-day))
  20173. (computed (org-agenda-compute-time-span sd span))
  20174. (org-agenda-overriding-arguments
  20175. (list (car org-agenda-last-arguments)
  20176. (car computed) (cdr computed) t)))
  20177. (org-agenda-redo)
  20178. (org-agenda-find-same-or-today-or-agenda))
  20179. (org-agenda-set-mode-name)
  20180. (message "Switched to %s view" span))
  20181. (defun org-agenda-compute-time-span (sd span)
  20182. "Compute starting date and number of days for agenda.
  20183. SPAN may be `day', `week', `month', `year'. The return value
  20184. is a cons cell with the starting date and the number of days,
  20185. so that the date SD will be in that range."
  20186. (let* ((greg (calendar-gregorian-from-absolute sd))
  20187. nd)
  20188. (cond
  20189. ((eq span 'day)
  20190. (setq nd 1))
  20191. ((eq span 'week)
  20192. (let* ((nt (calendar-day-of-week
  20193. (calendar-gregorian-from-absolute sd)))
  20194. (d (if org-agenda-start-on-weekday
  20195. (- nt org-agenda-start-on-weekday)
  20196. 0)))
  20197. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20198. (setq nd 7)))
  20199. ((eq span 'month)
  20200. (setq sd (calendar-absolute-from-gregorian
  20201. (list (car greg) 1 (nth 2 greg)))
  20202. nd (- (calendar-absolute-from-gregorian
  20203. (list (1+ (car greg)) 1 (nth 2 greg)))
  20204. sd)))
  20205. ((eq span 'year)
  20206. (setq sd (calendar-absolute-from-gregorian
  20207. (list 1 1 (nth 2 greg)))
  20208. nd (- (calendar-absolute-from-gregorian
  20209. (list 1 1 (1+ (nth 2 greg))))
  20210. sd))))
  20211. (cons sd nd)))
  20212. ;; FIXME: does not work if user makes date format that starts with a blank
  20213. (defun org-agenda-next-date-line (&optional arg)
  20214. "Jump to the next line indicating a date in agenda buffer."
  20215. (interactive "p")
  20216. (org-agenda-check-type t 'agenda 'timeline)
  20217. (beginning-of-line 1)
  20218. (if (looking-at "^\\S-") (forward-char 1))
  20219. (if (not (re-search-forward "^\\S-" nil t arg))
  20220. (progn
  20221. (backward-char 1)
  20222. (error "No next date after this line in this buffer")))
  20223. (goto-char (match-beginning 0)))
  20224. (defun org-agenda-previous-date-line (&optional arg)
  20225. "Jump to the previous line indicating a date in agenda buffer."
  20226. (interactive "p")
  20227. (org-agenda-check-type t 'agenda 'timeline)
  20228. (beginning-of-line 1)
  20229. (if (not (re-search-backward "^\\S-" nil t arg))
  20230. (error "No previous date before this line in this buffer")))
  20231. ;; Initialize the highlight
  20232. (defvar org-hl (org-make-overlay 1 1))
  20233. (org-overlay-put org-hl 'face 'highlight)
  20234. (defun org-highlight (begin end &optional buffer)
  20235. "Highlight a region with overlay."
  20236. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20237. org-hl begin end (or buffer (current-buffer))))
  20238. (defun org-unhighlight ()
  20239. "Detach overlay INDEX."
  20240. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20241. ;; FIXME this is currently not used.
  20242. (defun org-highlight-until-next-command (beg end &optional buffer)
  20243. (org-highlight beg end buffer)
  20244. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20245. (defun org-unhighlight-once ()
  20246. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20247. (org-unhighlight))
  20248. (defun org-agenda-follow-mode ()
  20249. "Toggle follow mode in an agenda buffer."
  20250. (interactive)
  20251. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20252. (org-agenda-set-mode-name)
  20253. (message "Follow mode is %s"
  20254. (if org-agenda-follow-mode "on" "off")))
  20255. (defun org-agenda-log-mode ()
  20256. "Toggle log mode in an agenda buffer."
  20257. (interactive)
  20258. (org-agenda-check-type t 'agenda 'timeline)
  20259. (setq org-agenda-show-log (not org-agenda-show-log))
  20260. (org-agenda-set-mode-name)
  20261. (org-agenda-redo)
  20262. (message "Log mode is %s"
  20263. (if org-agenda-show-log "on" "off")))
  20264. (defun org-agenda-toggle-diary ()
  20265. "Toggle diary inclusion in an agenda buffer."
  20266. (interactive)
  20267. (org-agenda-check-type t 'agenda)
  20268. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20269. (org-agenda-redo)
  20270. (org-agenda-set-mode-name)
  20271. (message "Diary inclusion turned %s"
  20272. (if org-agenda-include-diary "on" "off")))
  20273. (defun org-agenda-toggle-time-grid ()
  20274. "Toggle time grid in an agenda buffer."
  20275. (interactive)
  20276. (org-agenda-check-type t 'agenda)
  20277. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20278. (org-agenda-redo)
  20279. (org-agenda-set-mode-name)
  20280. (message "Time-grid turned %s"
  20281. (if org-agenda-use-time-grid "on" "off")))
  20282. (defun org-agenda-set-mode-name ()
  20283. "Set the mode name to indicate all the small mode settings."
  20284. (setq mode-name
  20285. (concat "Org-Agenda"
  20286. (if (equal org-agenda-ndays 1) " Day" "")
  20287. (if (equal org-agenda-ndays 7) " Week" "")
  20288. (if org-agenda-follow-mode " Follow" "")
  20289. (if org-agenda-include-diary " Diary" "")
  20290. (if org-agenda-use-time-grid " Grid" "")
  20291. (if org-agenda-show-log " Log" "")))
  20292. (force-mode-line-update))
  20293. (defun org-agenda-post-command-hook ()
  20294. (and (eolp) (not (bolp)) (backward-char 1))
  20295. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  20296. (if (and org-agenda-follow-mode
  20297. (get-text-property (point) 'org-marker))
  20298. (org-agenda-show)))
  20299. (defun org-agenda-show-priority ()
  20300. "Show the priority of the current item.
  20301. This priority is composed of the main priority given with the [#A] cookies,
  20302. and by additional input from the age of a schedules or deadline entry."
  20303. (interactive)
  20304. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  20305. (message "Priority is %d" (if pri pri -1000))))
  20306. (defun org-agenda-show-tags ()
  20307. "Show the tags applicable to the current item."
  20308. (interactive)
  20309. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  20310. (if tags
  20311. (message "Tags are :%s:"
  20312. (org-no-properties (mapconcat 'identity tags ":")))
  20313. (message "No tags associated with this line"))))
  20314. (defun org-agenda-goto (&optional highlight)
  20315. "Go to the Org-mode file which contains the item at point."
  20316. (interactive)
  20317. (let* ((marker (or (get-text-property (point) 'org-marker)
  20318. (org-agenda-error)))
  20319. (buffer (marker-buffer marker))
  20320. (pos (marker-position marker)))
  20321. (switch-to-buffer-other-window buffer)
  20322. (widen)
  20323. (goto-char pos)
  20324. (when (org-mode-p)
  20325. (org-show-context 'agenda)
  20326. (save-excursion
  20327. (and (outline-next-heading)
  20328. (org-flag-heading nil)))) ; show the next heading
  20329. (run-hooks 'org-agenda-after-show-hook)
  20330. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  20331. (defvar org-agenda-after-show-hook nil
  20332. "Normal hook run after an item has been shown from the agenda.
  20333. Point is in the buffer where the item originated.")
  20334. (defun org-agenda-kill ()
  20335. "Kill the entry or subtree belonging to the current agenda entry."
  20336. (interactive)
  20337. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20338. (let* ((marker (or (get-text-property (point) 'org-marker)
  20339. (org-agenda-error)))
  20340. (buffer (marker-buffer marker))
  20341. (pos (marker-position marker))
  20342. (type (get-text-property (point) 'type))
  20343. dbeg dend (n 0) conf)
  20344. (org-with-remote-undo buffer
  20345. (with-current-buffer buffer
  20346. (save-excursion
  20347. (goto-char pos)
  20348. (if (and (org-mode-p) (not (member type '("sexp"))))
  20349. (setq dbeg (progn (org-back-to-heading t) (point))
  20350. dend (org-end-of-subtree t t))
  20351. (setq dbeg (point-at-bol)
  20352. dend (min (point-max) (1+ (point-at-eol)))))
  20353. (goto-char dbeg)
  20354. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  20355. (setq conf (or (eq t org-agenda-confirm-kill)
  20356. (and (numberp org-agenda-confirm-kill)
  20357. (> n org-agenda-confirm-kill))))
  20358. (and conf
  20359. (not (y-or-n-p
  20360. (format "Delete entry with %d lines in buffer \"%s\"? "
  20361. n (buffer-name buffer))))
  20362. (error "Abort"))
  20363. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  20364. (with-current-buffer buffer (delete-region dbeg dend))
  20365. (message "Agenda item and source killed"))))
  20366. (defun org-agenda-archive ()
  20367. "Kill the entry or subtree belonging to the current agenda entry."
  20368. (interactive)
  20369. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20370. (let* ((marker (or (get-text-property (point) 'org-marker)
  20371. (org-agenda-error)))
  20372. (buffer (marker-buffer marker))
  20373. (pos (marker-position marker)))
  20374. (org-with-remote-undo buffer
  20375. (with-current-buffer buffer
  20376. (if (org-mode-p)
  20377. (save-excursion
  20378. (goto-char pos)
  20379. (org-remove-subtree-entries-from-agenda)
  20380. (org-back-to-heading t)
  20381. (org-archive-subtree))
  20382. (error "Archiving works only in Org-mode files"))))))
  20383. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  20384. "Remove all lines in the agenda that correspond to a given subtree.
  20385. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  20386. If this information is not given, the function uses the tree at point."
  20387. (let ((buf (or buf (current-buffer))) m p)
  20388. (save-excursion
  20389. (unless (and beg end)
  20390. (org-back-to-heading t)
  20391. (setq beg (point))
  20392. (org-end-of-subtree t)
  20393. (setq end (point)))
  20394. (set-buffer (get-buffer org-agenda-buffer-name))
  20395. (save-excursion
  20396. (goto-char (point-max))
  20397. (beginning-of-line 1)
  20398. (while (not (bobp))
  20399. (when (and (setq m (get-text-property (point) 'org-marker))
  20400. (equal buf (marker-buffer m))
  20401. (setq p (marker-position m))
  20402. (>= p beg)
  20403. (<= p end))
  20404. (let ((inhibit-read-only t))
  20405. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  20406. (beginning-of-line 0))))))
  20407. (defun org-agenda-open-link ()
  20408. "Follow the link in the current line, if any."
  20409. (interactive)
  20410. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  20411. (save-excursion
  20412. (save-restriction
  20413. (narrow-to-region (point-at-bol) (point-at-eol))
  20414. (org-open-at-point))))
  20415. (defun org-agenda-copy-local-variable (var)
  20416. "Get a variable from a referenced buffer and install it here."
  20417. (let ((m (get-text-property (point) 'org-marker)))
  20418. (when (and m (buffer-live-p (marker-buffer m)))
  20419. (org-set-local var (with-current-buffer (marker-buffer m)
  20420. (symbol-value var))))))
  20421. (defun org-agenda-switch-to (&optional delete-other-windows)
  20422. "Go to the Org-mode file which contains the item at point."
  20423. (interactive)
  20424. (let* ((marker (or (get-text-property (point) 'org-marker)
  20425. (org-agenda-error)))
  20426. (buffer (marker-buffer marker))
  20427. (pos (marker-position marker)))
  20428. (switch-to-buffer buffer)
  20429. (and delete-other-windows (delete-other-windows))
  20430. (widen)
  20431. (goto-char pos)
  20432. (when (org-mode-p)
  20433. (org-show-context 'agenda)
  20434. (save-excursion
  20435. (and (outline-next-heading)
  20436. (org-flag-heading nil)))))) ; show the next heading
  20437. (defun org-agenda-goto-mouse (ev)
  20438. "Go to the Org-mode file which contains the item at the mouse click."
  20439. (interactive "e")
  20440. (mouse-set-point ev)
  20441. (org-agenda-goto))
  20442. (defun org-agenda-show ()
  20443. "Display the Org-mode file which contains the item at point."
  20444. (interactive)
  20445. (let ((win (selected-window)))
  20446. (org-agenda-goto t)
  20447. (select-window win)))
  20448. (defun org-agenda-recenter (arg)
  20449. "Display the Org-mode file which contains the item at point and recenter."
  20450. (interactive "P")
  20451. (let ((win (selected-window)))
  20452. (org-agenda-goto t)
  20453. (recenter arg)
  20454. (select-window win)))
  20455. (defun org-agenda-show-mouse (ev)
  20456. "Display the Org-mode file which contains the item at the mouse click."
  20457. (interactive "e")
  20458. (mouse-set-point ev)
  20459. (org-agenda-show))
  20460. (defun org-agenda-check-no-diary ()
  20461. "Check if the entry is a diary link and abort if yes."
  20462. (if (get-text-property (point) 'org-agenda-diary-link)
  20463. (org-agenda-error)))
  20464. (defun org-agenda-error ()
  20465. (error "Command not allowed in this line"))
  20466. (defun org-agenda-tree-to-indirect-buffer ()
  20467. "Show the subtree corresponding to the current entry in an indirect buffer.
  20468. This calls the command `org-tree-to-indirect-buffer' from the original
  20469. Org-mode buffer.
  20470. With numerical prefix arg ARG, go up to this level and then take that tree.
  20471. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  20472. dedicated frame)."
  20473. (interactive)
  20474. (org-agenda-check-no-diary)
  20475. (let* ((marker (or (get-text-property (point) 'org-marker)
  20476. (org-agenda-error)))
  20477. (buffer (marker-buffer marker))
  20478. (pos (marker-position marker)))
  20479. (with-current-buffer buffer
  20480. (save-excursion
  20481. (goto-char pos)
  20482. (call-interactively 'org-tree-to-indirect-buffer)))))
  20483. (defvar org-last-heading-marker (make-marker)
  20484. "Marker pointing to the headline that last changed its TODO state
  20485. by a remote command from the agenda.")
  20486. (defun org-agenda-todo-nextset ()
  20487. "Switch TODO entry to next sequence."
  20488. (interactive)
  20489. (org-agenda-todo 'nextset))
  20490. (defun org-agenda-todo-previousset ()
  20491. "Switch TODO entry to previous sequence."
  20492. (interactive)
  20493. (org-agenda-todo 'previousset))
  20494. (defun org-agenda-todo (&optional arg)
  20495. "Cycle TODO state of line at point, also in Org-mode file.
  20496. This changes the line at point, all other lines in the agenda referring to
  20497. the same tree node, and the headline of the tree node in the Org-mode file."
  20498. (interactive "P")
  20499. (org-agenda-check-no-diary)
  20500. (let* ((col (current-column))
  20501. (marker (or (get-text-property (point) 'org-marker)
  20502. (org-agenda-error)))
  20503. (buffer (marker-buffer marker))
  20504. (pos (marker-position marker))
  20505. (hdmarker (get-text-property (point) 'org-hd-marker))
  20506. (inhibit-read-only t)
  20507. newhead)
  20508. (org-with-remote-undo buffer
  20509. (with-current-buffer buffer
  20510. (widen)
  20511. (goto-char pos)
  20512. (org-show-context 'agenda)
  20513. (save-excursion
  20514. (and (outline-next-heading)
  20515. (org-flag-heading nil))) ; show the next heading
  20516. (org-todo arg)
  20517. (and (bolp) (forward-char 1))
  20518. (setq newhead (org-get-heading))
  20519. (save-excursion
  20520. (org-back-to-heading)
  20521. (move-marker org-last-heading-marker (point))))
  20522. (beginning-of-line 1)
  20523. (save-excursion
  20524. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  20525. (move-to-column col))))
  20526. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  20527. "Change all lines in the agenda buffer which match HDMARKER.
  20528. The new content of the line will be NEWHEAD (as modified by
  20529. `org-format-agenda-item'). HDMARKER is checked with
  20530. `equal' against all `org-hd-marker' text properties in the file.
  20531. If FIXFACE is non-nil, the face of each item is modified acording to
  20532. the new TODO state."
  20533. (let* ((inhibit-read-only t)
  20534. props m pl undone-face done-face finish new dotime cat tags)
  20535. (save-excursion
  20536. (goto-char (point-max))
  20537. (beginning-of-line 1)
  20538. (while (not finish)
  20539. (setq finish (bobp))
  20540. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  20541. (equal m hdmarker))
  20542. (setq props (text-properties-at (point))
  20543. dotime (get-text-property (point) 'dotime)
  20544. cat (get-text-property (point) 'org-category)
  20545. tags (get-text-property (point) 'tags)
  20546. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  20547. pl (get-text-property (point) 'prefix-length)
  20548. undone-face (get-text-property (point) 'undone-face)
  20549. done-face (get-text-property (point) 'done-face))
  20550. (move-to-column pl)
  20551. (cond
  20552. ((equal new "")
  20553. (beginning-of-line 1)
  20554. (and (looking-at ".*\n?") (replace-match "")))
  20555. ((looking-at ".*")
  20556. (replace-match new t t)
  20557. (beginning-of-line 1)
  20558. (add-text-properties (point-at-bol) (point-at-eol) props)
  20559. (when fixface
  20560. (add-text-properties
  20561. (point-at-bol) (point-at-eol)
  20562. (list 'face
  20563. (if org-last-todo-state-is-todo
  20564. undone-face done-face))))
  20565. (org-agenda-highlight-todo 'line)
  20566. (beginning-of-line 1))
  20567. (t (error "Line update did not work"))))
  20568. (beginning-of-line 0)))
  20569. (org-finalize-agenda)))
  20570. (defun org-agenda-align-tags (&optional line)
  20571. "Align all tags in agenda items to `org-agenda-tags-column'."
  20572. (let ((inhibit-read-only t) l c)
  20573. (save-excursion
  20574. (goto-char (if line (point-at-bol) (point-min)))
  20575. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20576. (if line (point-at-eol) nil) t)
  20577. (add-text-properties
  20578. (match-beginning 2) (match-end 2)
  20579. (list 'face (list 'org-tag (get-text-property
  20580. (match-beginning 2) 'face))))
  20581. (setq l (- (match-end 2) (match-beginning 2))
  20582. c (if (< org-agenda-tags-column 0)
  20583. (- (abs org-agenda-tags-column) l)
  20584. org-agenda-tags-column))
  20585. (delete-region (match-beginning 1) (match-end 1))
  20586. (goto-char (match-beginning 1))
  20587. (insert (org-add-props
  20588. (make-string (max 1 (- c (current-column))) ?\ )
  20589. (text-properties-at (point))))))))
  20590. (defun org-agenda-priority-up ()
  20591. "Increase the priority of line at point, also in Org-mode file."
  20592. (interactive)
  20593. (org-agenda-priority 'up))
  20594. (defun org-agenda-priority-down ()
  20595. "Decrease the priority of line at point, also in Org-mode file."
  20596. (interactive)
  20597. (org-agenda-priority 'down))
  20598. (defun org-agenda-priority (&optional force-direction)
  20599. "Set the priority of line at point, also in Org-mode file.
  20600. This changes the line at point, all other lines in the agenda referring to
  20601. the same tree node, and the headline of the tree node in the Org-mode file."
  20602. (interactive)
  20603. (org-agenda-check-no-diary)
  20604. (let* ((marker (or (get-text-property (point) 'org-marker)
  20605. (org-agenda-error)))
  20606. (hdmarker (get-text-property (point) 'org-hd-marker))
  20607. (buffer (marker-buffer hdmarker))
  20608. (pos (marker-position hdmarker))
  20609. (inhibit-read-only t)
  20610. newhead)
  20611. (org-with-remote-undo buffer
  20612. (with-current-buffer buffer
  20613. (widen)
  20614. (goto-char pos)
  20615. (org-show-context 'agenda)
  20616. (save-excursion
  20617. (and (outline-next-heading)
  20618. (org-flag-heading nil))) ; show the next heading
  20619. (funcall 'org-priority force-direction)
  20620. (end-of-line 1)
  20621. (setq newhead (org-get-heading)))
  20622. (org-agenda-change-all-lines newhead hdmarker)
  20623. (beginning-of-line 1))))
  20624. (defun org-get-tags-at (&optional pos)
  20625. "Get a list of all headline tags applicable at POS.
  20626. POS defaults to point. If tags are inherited, the list contains
  20627. the targets in the same sequence as the headlines appear, i.e.
  20628. the tags of the current headline come last."
  20629. (interactive)
  20630. (let (tags lastpos)
  20631. (save-excursion
  20632. (save-restriction
  20633. (widen)
  20634. (goto-char (or pos (point)))
  20635. (save-match-data
  20636. (org-back-to-heading t)
  20637. (condition-case nil
  20638. (while (not (equal lastpos (point)))
  20639. (setq lastpos (point))
  20640. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  20641. (setq tags (append (org-split-string
  20642. (org-match-string-no-properties 1) ":")
  20643. tags)))
  20644. (or org-use-tag-inheritance (error ""))
  20645. (org-up-heading-all 1))
  20646. (error nil))))
  20647. tags)))
  20648. ;; FIXME: should fix the tags property of the agenda line.
  20649. (defun org-agenda-set-tags ()
  20650. "Set tags for the current headline."
  20651. (interactive)
  20652. (org-agenda-check-no-diary)
  20653. (if (and (org-region-active-p) (interactive-p))
  20654. (call-interactively 'org-change-tag-in-region)
  20655. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  20656. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  20657. (org-agenda-error)))
  20658. (buffer (marker-buffer hdmarker))
  20659. (pos (marker-position hdmarker))
  20660. (inhibit-read-only t)
  20661. newhead)
  20662. (org-with-remote-undo buffer
  20663. (with-current-buffer buffer
  20664. (widen)
  20665. (goto-char pos)
  20666. (save-excursion
  20667. (org-show-context 'agenda))
  20668. (save-excursion
  20669. (and (outline-next-heading)
  20670. (org-flag-heading nil))) ; show the next heading
  20671. (goto-char pos)
  20672. (call-interactively 'org-set-tags)
  20673. (end-of-line 1)
  20674. (setq newhead (org-get-heading)))
  20675. (org-agenda-change-all-lines newhead hdmarker)
  20676. (beginning-of-line 1)))))
  20677. (defun org-agenda-toggle-archive-tag ()
  20678. "Toggle the archive tag for the current entry."
  20679. (interactive)
  20680. (org-agenda-check-no-diary)
  20681. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  20682. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  20683. (org-agenda-error)))
  20684. (buffer (marker-buffer hdmarker))
  20685. (pos (marker-position hdmarker))
  20686. (inhibit-read-only t)
  20687. newhead)
  20688. (org-with-remote-undo buffer
  20689. (with-current-buffer buffer
  20690. (widen)
  20691. (goto-char pos)
  20692. (org-show-context 'agenda)
  20693. (save-excursion
  20694. (and (outline-next-heading)
  20695. (org-flag-heading nil))) ; show the next heading
  20696. (call-interactively 'org-toggle-archive-tag)
  20697. (end-of-line 1)
  20698. (setq newhead (org-get-heading)))
  20699. (org-agenda-change-all-lines newhead hdmarker)
  20700. (beginning-of-line 1))))
  20701. (defun org-agenda-date-later (arg &optional what)
  20702. "Change the date of this item to one day later."
  20703. (interactive "p")
  20704. (org-agenda-check-type t 'agenda 'timeline)
  20705. (org-agenda-check-no-diary)
  20706. (let* ((marker (or (get-text-property (point) 'org-marker)
  20707. (org-agenda-error)))
  20708. (buffer (marker-buffer marker))
  20709. (pos (marker-position marker)))
  20710. (org-with-remote-undo buffer
  20711. (with-current-buffer buffer
  20712. (widen)
  20713. (goto-char pos)
  20714. (if (not (org-at-timestamp-p))
  20715. (error "Cannot find time stamp"))
  20716. (org-timestamp-change arg (or what 'day)))
  20717. (org-agenda-show-new-time marker org-last-changed-timestamp))
  20718. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  20719. (defun org-agenda-date-earlier (arg &optional what)
  20720. "Change the date of this item to one day earlier."
  20721. (interactive "p")
  20722. (org-agenda-date-later (- arg) what))
  20723. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  20724. "Show new date stamp via text properties."
  20725. ;; We use text properties to make this undoable
  20726. (let ((inhibit-read-only t))
  20727. (setq stamp (concat " " prefix " => " stamp))
  20728. (save-excursion
  20729. (goto-char (point-max))
  20730. (while (not (bobp))
  20731. (when (equal marker (get-text-property (point) 'org-marker))
  20732. (move-to-column (- (window-width) (length stamp)) t)
  20733. (if (featurep 'xemacs)
  20734. ;; Use `duplicable' property to trigger undo recording
  20735. (let ((ex (make-extent nil nil))
  20736. (gl (make-glyph stamp)))
  20737. (set-glyph-face gl 'secondary-selection)
  20738. (set-extent-properties
  20739. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  20740. (insert-extent ex (1- (point)) (point-at-eol)))
  20741. (add-text-properties
  20742. (1- (point)) (point-at-eol)
  20743. (list 'display (org-add-props stamp nil
  20744. 'face 'secondary-selection))))
  20745. (beginning-of-line 1))
  20746. (beginning-of-line 0)))))
  20747. (defun org-agenda-date-prompt (arg)
  20748. "Change the date of this item. Date is prompted for, with default today.
  20749. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  20750. be used to request time specification in the time stamp."
  20751. (interactive "P")
  20752. (org-agenda-check-type t 'agenda 'timeline)
  20753. (org-agenda-check-no-diary)
  20754. (let* ((marker (or (get-text-property (point) 'org-marker)
  20755. (org-agenda-error)))
  20756. (buffer (marker-buffer marker))
  20757. (pos (marker-position marker)))
  20758. (org-with-remote-undo buffer
  20759. (with-current-buffer buffer
  20760. (widen)
  20761. (goto-char pos)
  20762. (if (not (org-at-timestamp-p))
  20763. (error "Cannot find time stamp"))
  20764. (org-time-stamp arg)
  20765. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  20766. (defun org-agenda-schedule (arg)
  20767. "Schedule the item at point."
  20768. (interactive "P")
  20769. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  20770. (org-agenda-check-no-diary)
  20771. (let* ((marker (or (get-text-property (point) 'org-marker)
  20772. (org-agenda-error)))
  20773. (buffer (marker-buffer marker))
  20774. (pos (marker-position marker))
  20775. (org-insert-labeled-timestamps-at-point nil)
  20776. ts)
  20777. (org-with-remote-undo buffer
  20778. (with-current-buffer buffer
  20779. (widen)
  20780. (goto-char pos)
  20781. (setq ts (org-schedule arg)))
  20782. (org-agenda-show-new-time marker ts "S"))
  20783. (message "Item scheduled for %s" ts)))
  20784. (defun org-agenda-deadline (arg)
  20785. "Schedule the item at point."
  20786. (interactive "P")
  20787. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  20788. (org-agenda-check-no-diary)
  20789. (let* ((marker (or (get-text-property (point) 'org-marker)
  20790. (org-agenda-error)))
  20791. (buffer (marker-buffer marker))
  20792. (pos (marker-position marker))
  20793. (org-insert-labeled-timestamps-at-point nil)
  20794. ts)
  20795. (org-with-remote-undo buffer
  20796. (with-current-buffer buffer
  20797. (widen)
  20798. (goto-char pos)
  20799. (setq ts (org-deadline arg)))
  20800. (org-agenda-show-new-time marker ts "S"))
  20801. (message "Deadline for this item set to %s" ts)))
  20802. (defun org-get-heading (&optional no-tags)
  20803. "Return the heading of the current entry, without the stars."
  20804. (save-excursion
  20805. (org-back-to-heading t)
  20806. (if (looking-at
  20807. (if no-tags
  20808. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  20809. "\\*+[ \t]+\\([^\r\n]*\\)"))
  20810. (match-string 1) "")))
  20811. (defun org-agenda-clock-in (&optional arg)
  20812. "Start the clock on the currently selected item."
  20813. (interactive "P")
  20814. (org-agenda-check-no-diary)
  20815. (let* ((marker (or (get-text-property (point) 'org-marker)
  20816. (org-agenda-error)))
  20817. (pos (marker-position marker)))
  20818. (org-with-remote-undo (marker-buffer marker)
  20819. (with-current-buffer (marker-buffer marker)
  20820. (widen)
  20821. (goto-char pos)
  20822. (org-clock-in)))))
  20823. (defun org-agenda-clock-out (&optional arg)
  20824. "Stop the currently running clock."
  20825. (interactive "P")
  20826. (unless (marker-buffer org-clock-marker)
  20827. (error "No running clock"))
  20828. (org-with-remote-undo (marker-buffer org-clock-marker)
  20829. (org-clock-out)))
  20830. (defun org-agenda-clock-cancel (&optional arg)
  20831. "Cancel the currently running clock."
  20832. (interactive "P")
  20833. (unless (marker-buffer org-clock-marker)
  20834. (error "No running clock"))
  20835. (org-with-remote-undo (marker-buffer org-clock-marker)
  20836. (org-clock-cancel)))
  20837. (defun org-agenda-diary-entry ()
  20838. "Make a diary entry, like the `i' command from the calendar.
  20839. All the standard commands work: block, weekly etc."
  20840. (interactive)
  20841. (org-agenda-check-type t 'agenda 'timeline)
  20842. (require 'diary-lib)
  20843. (let* ((char (progn
  20844. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  20845. (read-char-exclusive)))
  20846. (cmd (cdr (assoc char
  20847. '((?d . insert-diary-entry)
  20848. (?w . insert-weekly-diary-entry)
  20849. (?m . insert-monthly-diary-entry)
  20850. (?y . insert-yearly-diary-entry)
  20851. (?a . insert-anniversary-diary-entry)
  20852. (?b . insert-block-diary-entry)
  20853. (?c . insert-cyclic-diary-entry)))))
  20854. (oldf (symbol-function 'calendar-cursor-to-date))
  20855. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  20856. (point (point))
  20857. (mark (or (mark t) (point))))
  20858. (unless cmd
  20859. (error "No command associated with <%c>" char))
  20860. (unless (and (get-text-property point 'day)
  20861. (or (not (equal ?b char))
  20862. (get-text-property mark 'day)))
  20863. (error "Don't know which date to use for diary entry"))
  20864. ;; We implement this by hacking the `calendar-cursor-to-date' function
  20865. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  20866. (let ((calendar-mark-ring
  20867. (list (calendar-gregorian-from-absolute
  20868. (or (get-text-property mark 'day)
  20869. (get-text-property point 'day))))))
  20870. (unwind-protect
  20871. (progn
  20872. (fset 'calendar-cursor-to-date
  20873. (lambda (&optional error)
  20874. (calendar-gregorian-from-absolute
  20875. (get-text-property point 'day))))
  20876. (call-interactively cmd))
  20877. (fset 'calendar-cursor-to-date oldf)))))
  20878. (defun org-agenda-execute-calendar-command (cmd)
  20879. "Execute a calendar command from the agenda, with the date associated to
  20880. the cursor position."
  20881. (org-agenda-check-type t 'agenda 'timeline)
  20882. (require 'diary-lib)
  20883. (unless (get-text-property (point) 'day)
  20884. (error "Don't know which date to use for calendar command"))
  20885. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  20886. (point (point))
  20887. (date (calendar-gregorian-from-absolute
  20888. (get-text-property point 'day)))
  20889. ;; the following 3 vars are needed in the calendar
  20890. (displayed-day (extract-calendar-day date))
  20891. (displayed-month (extract-calendar-month date))
  20892. (displayed-year (extract-calendar-year date)))
  20893. (unwind-protect
  20894. (progn
  20895. (fset 'calendar-cursor-to-date
  20896. (lambda (&optional error)
  20897. (calendar-gregorian-from-absolute
  20898. (get-text-property point 'day))))
  20899. (call-interactively cmd))
  20900. (fset 'calendar-cursor-to-date oldf))))
  20901. (defun org-agenda-phases-of-moon ()
  20902. "Display the phases of the moon for the 3 months around the cursor date."
  20903. (interactive)
  20904. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  20905. (defun org-agenda-holidays ()
  20906. "Display the holidays for the 3 months around the cursor date."
  20907. (interactive)
  20908. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  20909. (defun org-agenda-sunrise-sunset (arg)
  20910. "Display sunrise and sunset for the cursor date.
  20911. Latitude and longitude can be specified with the variables
  20912. `calendar-latitude' and `calendar-longitude'. When called with prefix
  20913. argument, latitude and longitude will be prompted for."
  20914. (interactive "P")
  20915. (let ((calendar-longitude (if arg nil calendar-longitude))
  20916. (calendar-latitude (if arg nil calendar-latitude))
  20917. (calendar-location-name
  20918. (if arg "the given coordinates" calendar-location-name)))
  20919. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  20920. (defun org-agenda-goto-calendar ()
  20921. "Open the Emacs calendar with the date at the cursor."
  20922. (interactive)
  20923. (org-agenda-check-type t 'agenda 'timeline)
  20924. (let* ((day (or (get-text-property (point) 'day)
  20925. (error "Don't know which date to open in calendar")))
  20926. (date (calendar-gregorian-from-absolute day))
  20927. (calendar-move-hook nil)
  20928. (view-calendar-holidays-initially nil)
  20929. (view-diary-entries-initially nil))
  20930. (calendar)
  20931. (calendar-goto-date date)))
  20932. (defun org-calendar-goto-agenda ()
  20933. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  20934. This is a command that has to be installed in `calendar-mode-map'."
  20935. (interactive)
  20936. (org-agenda-list nil (calendar-absolute-from-gregorian
  20937. (calendar-cursor-to-date))
  20938. nil))
  20939. (defun org-agenda-convert-date ()
  20940. (interactive)
  20941. (org-agenda-check-type t 'agenda 'timeline)
  20942. (let ((day (get-text-property (point) 'day))
  20943. date s)
  20944. (unless day
  20945. (error "Don't know which date to convert"))
  20946. (setq date (calendar-gregorian-from-absolute day))
  20947. (setq s (concat
  20948. "Gregorian: " (calendar-date-string date) "\n"
  20949. "ISO: " (calendar-iso-date-string date) "\n"
  20950. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  20951. "Julian: " (calendar-julian-date-string date) "\n"
  20952. "Astron. JD: " (calendar-astro-date-string date)
  20953. " (Julian date number at noon UTC)\n"
  20954. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  20955. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  20956. "French: " (calendar-french-date-string date) "\n"
  20957. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  20958. "Mayan: " (calendar-mayan-date-string date) "\n"
  20959. "Coptic: " (calendar-coptic-date-string date) "\n"
  20960. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  20961. "Persian: " (calendar-persian-date-string date) "\n"
  20962. "Chinese: " (calendar-chinese-date-string date) "\n"))
  20963. (with-output-to-temp-buffer "*Dates*"
  20964. (princ s))
  20965. (if (fboundp 'fit-window-to-buffer)
  20966. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  20967. ;;;; Embedded LaTeX
  20968. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  20969. "Keymap for the minor `org-cdlatex-mode'.")
  20970. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  20971. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  20972. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  20973. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  20974. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  20975. (defvar org-cdlatex-texmathp-advice-is-done nil
  20976. "Flag remembering if we have applied the advice to texmathp already.")
  20977. (define-minor-mode org-cdlatex-mode
  20978. "Toggle the minor `org-cdlatex-mode'.
  20979. This mode supports entering LaTeX environment and math in LaTeX fragments
  20980. in Org-mode.
  20981. \\{org-cdlatex-mode-map}"
  20982. nil " OCDL" nil
  20983. (when org-cdlatex-mode (require 'cdlatex))
  20984. (unless org-cdlatex-texmathp-advice-is-done
  20985. (setq org-cdlatex-texmathp-advice-is-done t)
  20986. (defadvice texmathp (around org-math-always-on activate)
  20987. "Always return t in org-mode buffers.
  20988. This is because we want to insert math symbols without dollars even outside
  20989. the LaTeX math segments. If Orgmode thinks that point is actually inside
  20990. en embedded LaTeX fragement, let texmathp do its job.
  20991. \\[org-cdlatex-mode-map]"
  20992. (interactive)
  20993. (let (p)
  20994. (cond
  20995. ((not (org-mode-p)) ad-do-it)
  20996. ((eq this-command 'cdlatex-math-symbol)
  20997. (setq ad-return-value t
  20998. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  20999. (t
  21000. (let ((p (org-inside-LaTeX-fragment-p)))
  21001. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21002. (setq ad-return-value t
  21003. texmathp-why '("Org-mode embedded math" . 0))
  21004. (if p ad-do-it)))))))))
  21005. (defun turn-on-org-cdlatex ()
  21006. "Unconditionally turn on `org-cdlatex-mode'."
  21007. (org-cdlatex-mode 1))
  21008. (defun org-inside-LaTeX-fragment-p ()
  21009. "Test if point is inside a LaTeX fragment.
  21010. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21011. sequence appearing also before point.
  21012. Even though the matchers for math are configurable, this function assumes
  21013. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21014. delimiters are skipped when they have been removed by customization.
  21015. The return value is nil, or a cons cell with the delimiter and
  21016. and the position of this delimiter.
  21017. This function does a reasonably good job, but can locally be fooled by
  21018. for example currency specifications. For example it will assume being in
  21019. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21020. fragments that are properly closed, but during editing, we have to live
  21021. with the uncertainty caused by missing closing delimiters. This function
  21022. looks only before point, not after."
  21023. (catch 'exit
  21024. (let ((pos (point))
  21025. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21026. (lim (progn
  21027. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21028. (point)))
  21029. dd-on str (start 0) m re)
  21030. (goto-char pos)
  21031. (when dodollar
  21032. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21033. re (nth 1 (assoc "$" org-latex-regexps)))
  21034. (while (string-match re str start)
  21035. (cond
  21036. ((= (match-end 0) (length str))
  21037. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21038. ((= (match-end 0) (- (length str) 5))
  21039. (throw 'exit nil))
  21040. (t (setq start (match-end 0))))))
  21041. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21042. (goto-char pos)
  21043. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21044. (and (match-beginning 2) (throw 'exit nil))
  21045. ;; count $$
  21046. (while (re-search-backward "\\$\\$" lim t)
  21047. (setq dd-on (not dd-on)))
  21048. (goto-char pos)
  21049. (if dd-on (cons "$$" m))))))
  21050. (defun org-try-cdlatex-tab ()
  21051. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21052. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21053. - inside a LaTeX fragment, or
  21054. - after the first word in a line, where an abbreviation expansion could
  21055. insert a LaTeX environment."
  21056. (when org-cdlatex-mode
  21057. (cond
  21058. ((save-excursion
  21059. (skip-chars-backward "a-zA-Z0-9*")
  21060. (skip-chars-backward " \t")
  21061. (bolp))
  21062. (cdlatex-tab) t)
  21063. ((org-inside-LaTeX-fragment-p)
  21064. (cdlatex-tab) t)
  21065. (t nil))))
  21066. (defun org-cdlatex-underscore-caret (&optional arg)
  21067. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21068. Revert to the normal definition outside of these fragments."
  21069. (interactive "P")
  21070. (if (org-inside-LaTeX-fragment-p)
  21071. (call-interactively 'cdlatex-sub-superscript)
  21072. (let (org-cdlatex-mode)
  21073. (call-interactively (key-binding (vector last-input-event))))))
  21074. (defun org-cdlatex-math-modify (&optional arg)
  21075. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21076. Revert to the normal definition outside of these fragments."
  21077. (interactive "P")
  21078. (if (org-inside-LaTeX-fragment-p)
  21079. (call-interactively 'cdlatex-math-modify)
  21080. (let (org-cdlatex-mode)
  21081. (call-interactively (key-binding (vector last-input-event))))))
  21082. (defvar org-latex-fragment-image-overlays nil
  21083. "List of overlays carrying the images of latex fragments.")
  21084. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21085. (defun org-remove-latex-fragment-image-overlays ()
  21086. "Remove all overlays with LaTeX fragment images in current buffer."
  21087. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21088. (setq org-latex-fragment-image-overlays nil))
  21089. (defun org-preview-latex-fragment (&optional subtree)
  21090. "Preview the LaTeX fragment at point, or all locally or globally.
  21091. If the cursor is in a LaTeX fragment, create the image and overlay
  21092. it over the source code. If there is no fragment at point, display
  21093. all fragments in the current text, from one headline to the next. With
  21094. prefix SUBTREE, display all fragments in the current subtree. With a
  21095. double prefix `C-u C-u', or when the cursor is before the first headline,
  21096. display all fragments in the buffer.
  21097. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21098. (interactive "P")
  21099. (org-remove-latex-fragment-image-overlays)
  21100. (save-excursion
  21101. (save-restriction
  21102. (let (beg end at msg)
  21103. (cond
  21104. ((or (equal subtree '(16))
  21105. (not (save-excursion
  21106. (re-search-backward (concat "^" outline-regexp) nil t))))
  21107. (setq beg (point-min) end (point-max)
  21108. msg "Creating images for buffer...%s"))
  21109. ((equal subtree '(4))
  21110. (org-back-to-heading)
  21111. (setq beg (point) end (org-end-of-subtree t)
  21112. msg "Creating images for subtree...%s"))
  21113. (t
  21114. (if (setq at (org-inside-LaTeX-fragment-p))
  21115. (goto-char (max (point-min) (- (cdr at) 2)))
  21116. (org-back-to-heading))
  21117. (setq beg (point) end (progn (outline-next-heading) (point))
  21118. msg (if at "Creating image...%s"
  21119. "Creating images for entry...%s"))))
  21120. (message msg "")
  21121. (narrow-to-region beg end)
  21122. (goto-char beg)
  21123. (org-format-latex
  21124. (concat "ltxpng/" (file-name-sans-extension
  21125. (file-name-nondirectory
  21126. buffer-file-name)))
  21127. default-directory 'overlays msg at 'forbuffer)
  21128. (message msg "done. Use `C-c C-c' to remove images.")))))
  21129. (defvar org-latex-regexps
  21130. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21131. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21132. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21133. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21134. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21135. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21136. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21137. "Regular expressions for matching embedded LaTeX.")
  21138. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21139. "Replace LaTeX fragments with links to an image, and produce images."
  21140. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21141. (let* ((prefixnodir (file-name-nondirectory prefix))
  21142. (absprefix (expand-file-name prefix dir))
  21143. (todir (file-name-directory absprefix))
  21144. (opt org-format-latex-options)
  21145. (matchers (plist-get opt :matchers))
  21146. (re-list org-latex-regexps)
  21147. (cnt 0) txt link beg end re e checkdir
  21148. m n block linkfile movefile ov)
  21149. ;; Check if there are old images files with this prefix, and remove them
  21150. (when (file-directory-p todir)
  21151. (mapc 'delete-file
  21152. (directory-files
  21153. todir 'full
  21154. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21155. ;; Check the different regular expressions
  21156. (while (setq e (pop re-list))
  21157. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21158. block (if (nth 3 e) "\n\n" ""))
  21159. (when (member m matchers)
  21160. (goto-char (point-min))
  21161. (while (re-search-forward re nil t)
  21162. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21163. (setq txt (match-string n)
  21164. beg (match-beginning n) end (match-end n)
  21165. cnt (1+ cnt)
  21166. linkfile (format "%s_%04d.png" prefix cnt)
  21167. movefile (format "%s_%04d.png" absprefix cnt)
  21168. link (concat block "[[file:" linkfile "]]" block))
  21169. (if msg (message msg cnt))
  21170. (goto-char beg)
  21171. (unless checkdir ; make sure the directory exists
  21172. (setq checkdir t)
  21173. (or (file-directory-p todir) (make-directory todir)))
  21174. (org-create-formula-image
  21175. txt movefile opt forbuffer)
  21176. (if overlays
  21177. (progn
  21178. (setq ov (org-make-overlay beg end))
  21179. (if (featurep 'xemacs)
  21180. (progn
  21181. (org-overlay-put ov 'invisible t)
  21182. (org-overlay-put
  21183. ov 'end-glyph
  21184. (make-glyph (vector 'png :file movefile))))
  21185. (org-overlay-put
  21186. ov 'display
  21187. (list 'image :type 'png :file movefile :ascent 'center)))
  21188. (push ov org-latex-fragment-image-overlays)
  21189. (goto-char end))
  21190. (delete-region beg end)
  21191. (insert link))))))))
  21192. ;; This function borrows from Ganesh Swami's latex2png.el
  21193. (defun org-create-formula-image (string tofile options buffer)
  21194. (let* ((tmpdir (if (featurep 'xemacs)
  21195. (temp-directory)
  21196. temporary-file-directory))
  21197. (texfilebase (make-temp-name
  21198. (expand-file-name "orgtex" tmpdir)))
  21199. (texfile (concat texfilebase ".tex"))
  21200. (dvifile (concat texfilebase ".dvi"))
  21201. (pngfile (concat texfilebase ".png"))
  21202. (fnh (face-attribute 'default :height nil))
  21203. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21204. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21205. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21206. "Black"))
  21207. (bg (or (plist-get options (if buffer :background :html-background))
  21208. "Transparent")))
  21209. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21210. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21211. (with-temp-file texfile
  21212. (insert org-format-latex-header
  21213. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21214. (let ((dir default-directory))
  21215. (condition-case nil
  21216. (progn
  21217. (cd tmpdir)
  21218. (call-process "latex" nil nil nil texfile))
  21219. (error nil))
  21220. (cd dir))
  21221. (if (not (file-exists-p dvifile))
  21222. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21223. (call-process "dvipng" nil nil nil
  21224. "-E" "-fg" fg "-bg" bg
  21225. "-D" dpi
  21226. ;;"-x" scale "-y" scale
  21227. "-T" "tight"
  21228. "-o" pngfile
  21229. dvifile)
  21230. (if (not (file-exists-p pngfile))
  21231. (progn (message "Failed to create png file from %s" texfile) nil)
  21232. ;; Use the requested file name and clean up
  21233. (copy-file pngfile tofile 'replace)
  21234. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21235. (delete-file (concat texfilebase e)))
  21236. pngfile))))
  21237. (defun org-dvipng-color (attr)
  21238. "Return an rgb color specification for dvipng."
  21239. (apply 'format "rgb %s %s %s"
  21240. (mapcar 'org-normalize-color
  21241. (color-values (face-attribute 'default attr nil)))))
  21242. (defun org-normalize-color (value)
  21243. "Return string to be used as color value for an RGB component."
  21244. (format "%g" (/ value 65535.0)))
  21245. ;;;; Exporting
  21246. ;;; Variables, constants, and parameter plists
  21247. (defconst org-level-max 20)
  21248. (defvar org-export-html-preamble nil
  21249. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21250. (defvar org-export-html-postamble nil
  21251. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21252. (defvar org-export-html-auto-preamble t
  21253. "Should default preamble be inserted? Set by publishing functions.")
  21254. (defvar org-export-html-auto-postamble t
  21255. "Should default postamble be inserted? Set by publishing functions.")
  21256. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21257. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21258. (defconst org-export-plist-vars
  21259. '((:language . org-export-default-language)
  21260. (:customtime . org-display-custom-times)
  21261. (:headline-levels . org-export-headline-levels)
  21262. (:section-numbers . org-export-with-section-numbers)
  21263. (:table-of-contents . org-export-with-toc)
  21264. (:preserve-breaks . org-export-preserve-breaks)
  21265. (:archived-trees . org-export-with-archived-trees)
  21266. (:emphasize . org-export-with-emphasize)
  21267. (:sub-superscript . org-export-with-sub-superscripts)
  21268. (:special-strings . org-export-with-special-strings)
  21269. (:footnotes . org-export-with-footnotes)
  21270. (:drawers . org-export-with-drawers)
  21271. (:tags . org-export-with-tags)
  21272. (:TeX-macros . org-export-with-TeX-macros)
  21273. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21274. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21275. (:fixed-width . org-export-with-fixed-width)
  21276. (:timestamps . org-export-with-timestamps)
  21277. (:author-info . org-export-author-info)
  21278. (:time-stamp-file . org-export-time-stamp-file)
  21279. (:tables . org-export-with-tables)
  21280. (:table-auto-headline . org-export-highlight-first-table-line)
  21281. (:style . org-export-html-style)
  21282. (:agenda-style . org-agenda-export-html-style)
  21283. (:convert-org-links . org-export-html-link-org-files-as-html)
  21284. (:inline-images . org-export-html-inline-images)
  21285. (:html-extension . org-export-html-extension)
  21286. (:html-table-tag . org-export-html-table-tag)
  21287. (:expand-quoted-html . org-export-html-expand)
  21288. (:timestamp . org-export-html-with-timestamp)
  21289. (:publishing-directory . org-export-publishing-directory)
  21290. (:preamble . org-export-html-preamble)
  21291. (:postamble . org-export-html-postamble)
  21292. (:auto-preamble . org-export-html-auto-preamble)
  21293. (:auto-postamble . org-export-html-auto-postamble)
  21294. (:author . user-full-name)
  21295. (:email . user-mail-address)))
  21296. (defun org-default-export-plist ()
  21297. "Return the property list with default settings for the export variables."
  21298. (let ((l org-export-plist-vars) rtn e)
  21299. (while (setq e (pop l))
  21300. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  21301. rtn))
  21302. (defun org-infile-export-plist ()
  21303. "Return the property list with file-local settings for export."
  21304. (save-excursion
  21305. (save-restriction
  21306. (widen)
  21307. (goto-char 0)
  21308. (let ((re (org-make-options-regexp
  21309. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  21310. p key val text options)
  21311. (while (re-search-forward re nil t)
  21312. (setq key (org-match-string-no-properties 1)
  21313. val (org-match-string-no-properties 2))
  21314. (cond
  21315. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  21316. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  21317. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  21318. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  21319. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  21320. ((string-equal key "TEXT")
  21321. (setq text (if text (concat text "\n" val) val)))
  21322. ((string-equal key "OPTIONS") (setq options val))))
  21323. (setq p (plist-put p :text text))
  21324. (when options
  21325. (let ((op '(("H" . :headline-levels)
  21326. ("num" . :section-numbers)
  21327. ("toc" . :table-of-contents)
  21328. ("\\n" . :preserve-breaks)
  21329. ("@" . :expand-quoted-html)
  21330. (":" . :fixed-width)
  21331. ("|" . :tables)
  21332. ("^" . :sub-superscript)
  21333. ("-" . :special-strings)
  21334. ("f" . :footnotes)
  21335. ("d" . :drawers)
  21336. ("tags" . :tags)
  21337. ("*" . :emphasize)
  21338. ("TeX" . :TeX-macros)
  21339. ("LaTeX" . :LaTeX-fragments)
  21340. ("skip" . :skip-before-1st-heading)
  21341. ("author" . :author-info)
  21342. ("timestamp" . :time-stamp-file)))
  21343. o)
  21344. (while (setq o (pop op))
  21345. (if (string-match (concat (regexp-quote (car o))
  21346. ":\\([^ \t\n\r;,.]*\\)")
  21347. options)
  21348. (setq p (plist-put p (cdr o)
  21349. (car (read-from-string
  21350. (match-string 1 options)))))))))
  21351. p))))
  21352. (defun org-export-directory (type plist)
  21353. (let* ((val (plist-get plist :publishing-directory))
  21354. (dir (if (listp val)
  21355. (or (cdr (assoc type val)) ".")
  21356. val)))
  21357. dir))
  21358. (defun org-skip-comments (lines)
  21359. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  21360. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  21361. (re2 "^\\(\\*+\\)[ \t\n\r]")
  21362. (case-fold-search nil)
  21363. rtn line level)
  21364. (while (setq line (pop lines))
  21365. (cond
  21366. ((and (string-match re1 line)
  21367. (setq level (- (match-end 1) (match-beginning 1))))
  21368. ;; Beginning of a COMMENT subtree. Skip it.
  21369. (while (and (setq line (pop lines))
  21370. (or (not (string-match re2 line))
  21371. (> (- (match-end 1) (match-beginning 1)) level))))
  21372. (setq lines (cons line lines)))
  21373. ((string-match "^#" line)
  21374. ;; an ordinary comment line
  21375. )
  21376. ((and org-export-table-remove-special-lines
  21377. (string-match "^[ \t]*|" line)
  21378. (or (string-match "^[ \t]*| *[!_^] *|" line)
  21379. (and (string-match "| *<[0-9]+> *|" line)
  21380. (not (string-match "| *[^ <|]" line)))))
  21381. ;; a special table line that should be removed
  21382. )
  21383. (t (setq rtn (cons line rtn)))))
  21384. (nreverse rtn)))
  21385. (defun org-export (&optional arg)
  21386. (interactive)
  21387. (let ((help "[t] insert the export option template
  21388. \[v] limit export to visible part of outline tree
  21389. \[a] export as ASCII
  21390. \[h] export as HTML
  21391. \[H] export as HTML to temporary buffer
  21392. \[R] export region as HTML
  21393. \[b] export as HTML and browse immediately
  21394. \[x] export as XOXO
  21395. \[l] export as LaTeX
  21396. \[L] export as LaTeX to temporary buffer
  21397. \[i] export current file as iCalendar file
  21398. \[I] export all agenda files as iCalendar files
  21399. \[c] export agenda files into combined iCalendar file
  21400. \[F] publish current file
  21401. \[P] publish current project
  21402. \[X] publish... (project will be prompted for)
  21403. \[A] publish all projects")
  21404. (cmds
  21405. '((?t . org-insert-export-options-template)
  21406. (?v . org-export-visible)
  21407. (?a . org-export-as-ascii)
  21408. (?h . org-export-as-html)
  21409. (?b . org-export-as-html-and-open)
  21410. (?H . org-export-as-html-to-buffer)
  21411. (?R . org-export-region-as-html)
  21412. (?x . org-export-as-xoxo)
  21413. (?l . org-export-as-latex)
  21414. (?L . org-export-as-latex-to-buffer)
  21415. (?i . org-export-icalendar-this-file)
  21416. (?I . org-export-icalendar-all-agenda-files)
  21417. (?c . org-export-icalendar-combine-agenda-files)
  21418. (?F . org-publish-current-file)
  21419. (?P . org-publish-current-project)
  21420. (?X . org-publish)
  21421. (?A . org-publish-all)))
  21422. r1 r2 ass)
  21423. (save-window-excursion
  21424. (delete-other-windows)
  21425. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  21426. (princ help))
  21427. (message "Select command: ")
  21428. (setq r1 (read-char-exclusive)))
  21429. (setq r2 (if (< r1 27) (+ r1 96) r1))
  21430. (if (setq ass (assq r2 cmds))
  21431. (call-interactively (cdr ass))
  21432. (error "No command associated with key %c" r1))))
  21433. (defconst org-html-entities
  21434. '(("nbsp")
  21435. ("iexcl")
  21436. ("cent")
  21437. ("pound")
  21438. ("curren")
  21439. ("yen")
  21440. ("brvbar")
  21441. ("vert" . "&#124;")
  21442. ("sect")
  21443. ("uml")
  21444. ("copy")
  21445. ("ordf")
  21446. ("laquo")
  21447. ("not")
  21448. ("shy")
  21449. ("reg")
  21450. ("macr")
  21451. ("deg")
  21452. ("plusmn")
  21453. ("sup2")
  21454. ("sup3")
  21455. ("acute")
  21456. ("micro")
  21457. ("para")
  21458. ("middot")
  21459. ("odot"."o")
  21460. ("star"."*")
  21461. ("cedil")
  21462. ("sup1")
  21463. ("ordm")
  21464. ("raquo")
  21465. ("frac14")
  21466. ("frac12")
  21467. ("frac34")
  21468. ("iquest")
  21469. ("Agrave")
  21470. ("Aacute")
  21471. ("Acirc")
  21472. ("Atilde")
  21473. ("Auml")
  21474. ("Aring") ("AA"."&Aring;")
  21475. ("AElig")
  21476. ("Ccedil")
  21477. ("Egrave")
  21478. ("Eacute")
  21479. ("Ecirc")
  21480. ("Euml")
  21481. ("Igrave")
  21482. ("Iacute")
  21483. ("Icirc")
  21484. ("Iuml")
  21485. ("ETH")
  21486. ("Ntilde")
  21487. ("Ograve")
  21488. ("Oacute")
  21489. ("Ocirc")
  21490. ("Otilde")
  21491. ("Ouml")
  21492. ("times")
  21493. ("Oslash")
  21494. ("Ugrave")
  21495. ("Uacute")
  21496. ("Ucirc")
  21497. ("Uuml")
  21498. ("Yacute")
  21499. ("THORN")
  21500. ("szlig")
  21501. ("agrave")
  21502. ("aacute")
  21503. ("acirc")
  21504. ("atilde")
  21505. ("auml")
  21506. ("aring")
  21507. ("aelig")
  21508. ("ccedil")
  21509. ("egrave")
  21510. ("eacute")
  21511. ("ecirc")
  21512. ("euml")
  21513. ("igrave")
  21514. ("iacute")
  21515. ("icirc")
  21516. ("iuml")
  21517. ("eth")
  21518. ("ntilde")
  21519. ("ograve")
  21520. ("oacute")
  21521. ("ocirc")
  21522. ("otilde")
  21523. ("ouml")
  21524. ("divide")
  21525. ("oslash")
  21526. ("ugrave")
  21527. ("uacute")
  21528. ("ucirc")
  21529. ("uuml")
  21530. ("yacute")
  21531. ("thorn")
  21532. ("yuml")
  21533. ("fnof")
  21534. ("Alpha")
  21535. ("Beta")
  21536. ("Gamma")
  21537. ("Delta")
  21538. ("Epsilon")
  21539. ("Zeta")
  21540. ("Eta")
  21541. ("Theta")
  21542. ("Iota")
  21543. ("Kappa")
  21544. ("Lambda")
  21545. ("Mu")
  21546. ("Nu")
  21547. ("Xi")
  21548. ("Omicron")
  21549. ("Pi")
  21550. ("Rho")
  21551. ("Sigma")
  21552. ("Tau")
  21553. ("Upsilon")
  21554. ("Phi")
  21555. ("Chi")
  21556. ("Psi")
  21557. ("Omega")
  21558. ("alpha")
  21559. ("beta")
  21560. ("gamma")
  21561. ("delta")
  21562. ("epsilon")
  21563. ("varepsilon"."&epsilon;")
  21564. ("zeta")
  21565. ("eta")
  21566. ("theta")
  21567. ("iota")
  21568. ("kappa")
  21569. ("lambda")
  21570. ("mu")
  21571. ("nu")
  21572. ("xi")
  21573. ("omicron")
  21574. ("pi")
  21575. ("rho")
  21576. ("sigmaf") ("varsigma"."&sigmaf;")
  21577. ("sigma")
  21578. ("tau")
  21579. ("upsilon")
  21580. ("phi")
  21581. ("chi")
  21582. ("psi")
  21583. ("omega")
  21584. ("thetasym") ("vartheta"."&thetasym;")
  21585. ("upsih")
  21586. ("piv")
  21587. ("bull") ("bullet"."&bull;")
  21588. ("hellip") ("dots"."&hellip;")
  21589. ("prime")
  21590. ("Prime")
  21591. ("oline")
  21592. ("frasl")
  21593. ("weierp")
  21594. ("image")
  21595. ("real")
  21596. ("trade")
  21597. ("alefsym")
  21598. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  21599. ("uarr") ("uparrow"."&uarr;")
  21600. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  21601. ("darr")("downarrow"."&darr;")
  21602. ("harr") ("leftrightarrow"."&harr;")
  21603. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  21604. ("lArr") ("Leftarrow"."&lArr;")
  21605. ("uArr") ("Uparrow"."&uArr;")
  21606. ("rArr") ("Rightarrow"."&rArr;")
  21607. ("dArr") ("Downarrow"."&dArr;")
  21608. ("hArr") ("Leftrightarrow"."&hArr;")
  21609. ("forall")
  21610. ("part") ("partial"."&part;")
  21611. ("exist") ("exists"."&exist;")
  21612. ("empty") ("emptyset"."&empty;")
  21613. ("nabla")
  21614. ("isin") ("in"."&isin;")
  21615. ("notin")
  21616. ("ni")
  21617. ("prod")
  21618. ("sum")
  21619. ("minus")
  21620. ("lowast") ("ast"."&lowast;")
  21621. ("radic")
  21622. ("prop") ("proptp"."&prop;")
  21623. ("infin") ("infty"."&infin;")
  21624. ("ang") ("angle"."&ang;")
  21625. ("and") ("wedge"."&and;")
  21626. ("or") ("vee"."&or;")
  21627. ("cap")
  21628. ("cup")
  21629. ("int")
  21630. ("there4")
  21631. ("sim")
  21632. ("cong") ("simeq"."&cong;")
  21633. ("asymp")("approx"."&asymp;")
  21634. ("ne") ("neq"."&ne;")
  21635. ("equiv")
  21636. ("le")
  21637. ("ge")
  21638. ("sub") ("subset"."&sub;")
  21639. ("sup") ("supset"."&sup;")
  21640. ("nsub")
  21641. ("sube")
  21642. ("supe")
  21643. ("oplus")
  21644. ("otimes")
  21645. ("perp")
  21646. ("sdot") ("cdot"."&sdot;")
  21647. ("lceil")
  21648. ("rceil")
  21649. ("lfloor")
  21650. ("rfloor")
  21651. ("lang")
  21652. ("rang")
  21653. ("loz") ("Diamond"."&loz;")
  21654. ("spades") ("spadesuit"."&spades;")
  21655. ("clubs") ("clubsuit"."&clubs;")
  21656. ("hearts") ("diamondsuit"."&hearts;")
  21657. ("diams") ("diamondsuit"."&diams;")
  21658. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  21659. ("quot")
  21660. ("amp")
  21661. ("lt")
  21662. ("gt")
  21663. ("OElig")
  21664. ("oelig")
  21665. ("Scaron")
  21666. ("scaron")
  21667. ("Yuml")
  21668. ("circ")
  21669. ("tilde")
  21670. ("ensp")
  21671. ("emsp")
  21672. ("thinsp")
  21673. ("zwnj")
  21674. ("zwj")
  21675. ("lrm")
  21676. ("rlm")
  21677. ("ndash")
  21678. ("mdash")
  21679. ("lsquo")
  21680. ("rsquo")
  21681. ("sbquo")
  21682. ("ldquo")
  21683. ("rdquo")
  21684. ("bdquo")
  21685. ("dagger")
  21686. ("Dagger")
  21687. ("permil")
  21688. ("lsaquo")
  21689. ("rsaquo")
  21690. ("euro")
  21691. ("arccos"."arccos")
  21692. ("arcsin"."arcsin")
  21693. ("arctan"."arctan")
  21694. ("arg"."arg")
  21695. ("cos"."cos")
  21696. ("cosh"."cosh")
  21697. ("cot"."cot")
  21698. ("coth"."coth")
  21699. ("csc"."csc")
  21700. ("deg"."deg")
  21701. ("det"."det")
  21702. ("dim"."dim")
  21703. ("exp"."exp")
  21704. ("gcd"."gcd")
  21705. ("hom"."hom")
  21706. ("inf"."inf")
  21707. ("ker"."ker")
  21708. ("lg"."lg")
  21709. ("lim"."lim")
  21710. ("liminf"."liminf")
  21711. ("limsup"."limsup")
  21712. ("ln"."ln")
  21713. ("log"."log")
  21714. ("max"."max")
  21715. ("min"."min")
  21716. ("Pr"."Pr")
  21717. ("sec"."sec")
  21718. ("sin"."sin")
  21719. ("sinh"."sinh")
  21720. ("sup"."sup")
  21721. ("tan"."tan")
  21722. ("tanh"."tanh")
  21723. )
  21724. "Entities for TeX->HTML translation.
  21725. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  21726. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  21727. In that case, \"\\ent\" will be translated to \"&other;\".
  21728. The list contains HTML entities for Latin-1, Greek and other symbols.
  21729. It is supplemented by a number of commonly used TeX macros with appropriate
  21730. translations. There is currently no way for users to extend this.")
  21731. ;;; General functions for all backends
  21732. (defun org-cleaned-string-for-export (string &rest parameters)
  21733. "Cleanup a buffer STRING so that links can be created safely."
  21734. (interactive)
  21735. (let* ((re-radio (and org-target-link-regexp
  21736. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  21737. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  21738. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  21739. (re-archive (concat ":" org-archive-tag ":"))
  21740. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  21741. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  21742. (htmlp (plist-get parameters :for-html))
  21743. (asciip (plist-get parameters :for-ascii))
  21744. (latexp (plist-get parameters :for-LaTeX))
  21745. (commentsp (plist-get parameters :comments))
  21746. (archived-trees (plist-get parameters :archived-trees))
  21747. (inhibit-read-only t)
  21748. (drawers org-drawers)
  21749. (exp-drawers (plist-get parameters :drawers))
  21750. (outline-regexp "\\*+ ")
  21751. a b xx
  21752. rtn p)
  21753. (with-current-buffer (get-buffer-create " org-mode-tmp")
  21754. (erase-buffer)
  21755. (insert string)
  21756. ;; Remove license-to-kill stuff
  21757. (while (setq p (text-property-any (point-min) (point-max)
  21758. :org-license-to-kill t))
  21759. (delete-region p (next-single-property-change p :org-license-to-kill)))
  21760. (let ((org-inhibit-startup t)) (org-mode))
  21761. (untabify (point-min) (point-max))
  21762. ;; Get the correct stuff before the first headline
  21763. (when (plist-get parameters :skip-before-1st-heading)
  21764. (goto-char (point-min))
  21765. (when (re-search-forward "^\\*+[ \t]" nil t)
  21766. (delete-region (point-min) (match-beginning 0))
  21767. (goto-char (point-min))
  21768. (insert "\n")))
  21769. (when (plist-get parameters :add-text)
  21770. (goto-char (point-min))
  21771. (insert (plist-get parameters :add-text) "\n"))
  21772. ;; Get rid of archived trees
  21773. (when (not (eq archived-trees t))
  21774. (goto-char (point-min))
  21775. (while (re-search-forward re-archive nil t)
  21776. (if (not (org-on-heading-p t))
  21777. (org-end-of-subtree t)
  21778. (beginning-of-line 1)
  21779. (setq a (if archived-trees
  21780. (1+ (point-at-eol)) (point))
  21781. b (org-end-of-subtree t))
  21782. (if (> b a) (delete-region a b)))))
  21783. ;; Get rid of drawers
  21784. (unless (eq t exp-drawers)
  21785. (goto-char (point-min))
  21786. (let ((re (concat "^[ \t]*:\\("
  21787. (mapconcat
  21788. 'identity
  21789. (org-delete-all exp-drawers
  21790. (copy-sequence drawers))
  21791. "\\|")
  21792. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  21793. (while (re-search-forward re nil t)
  21794. (replace-match ""))))
  21795. ;; Find targets in comments and move them out of comments,
  21796. ;; but mark them as targets that should be invisible
  21797. (goto-char (point-min))
  21798. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  21799. (replace-match "\\1(INVISIBLE)"))
  21800. ;; Protect backend specific stuff, throw away the others.
  21801. (let ((formatters
  21802. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  21803. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  21804. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  21805. fmt)
  21806. (goto-char (point-min))
  21807. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  21808. (goto-char (match-end 0))
  21809. (while (not (looking-at "#\\+END_EXAMPLE"))
  21810. (insert ": ")
  21811. (beginning-of-line 2)))
  21812. (goto-char (point-min))
  21813. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  21814. (add-text-properties (match-beginning 0) (match-end 0)
  21815. '(org-protected t)))
  21816. (while formatters
  21817. (setq fmt (pop formatters))
  21818. (when (car fmt)
  21819. (goto-char (point-min))
  21820. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  21821. ":[ \t]*\\(.*\\)") nil t)
  21822. (replace-match "\\1" t)
  21823. (add-text-properties
  21824. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  21825. '(org-protected t))))
  21826. (goto-char (point-min))
  21827. (while (re-search-forward
  21828. (concat "^#\\+"
  21829. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  21830. (cadddr fmt) "\\>.*\n?") nil t)
  21831. (if (car fmt)
  21832. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  21833. '(org-protected t))
  21834. (delete-region (match-beginning 0) (match-end 0))))))
  21835. ;; Protect quoted subtrees
  21836. (goto-char (point-min))
  21837. (while (re-search-forward re-quote nil t)
  21838. (goto-char (match-beginning 0))
  21839. (end-of-line 1)
  21840. (add-text-properties (point) (org-end-of-subtree t)
  21841. '(org-protected t)))
  21842. ;; Protect verbatim elements
  21843. (goto-char (point-min))
  21844. (while (re-search-forward org-verbatim-re nil t)
  21845. (add-text-properties (match-beginning 4) (match-end 4)
  21846. '(org-protected t))
  21847. (goto-char (1+ (match-end 4))))
  21848. ;; Remove subtrees that are commented
  21849. (goto-char (point-min))
  21850. (while (re-search-forward re-commented nil t)
  21851. (goto-char (match-beginning 0))
  21852. (delete-region (point) (org-end-of-subtree t)))
  21853. ;; Remove special table lines
  21854. (when org-export-table-remove-special-lines
  21855. (goto-char (point-min))
  21856. (while (re-search-forward "^[ \t]*|" nil t)
  21857. (beginning-of-line 1)
  21858. (if (or (looking-at "[ \t]*| *[!_^] *|")
  21859. (and (looking-at ".*?| *<[0-9]+> *|")
  21860. (not (looking-at ".*?| *[^ <|]"))))
  21861. (delete-region (max (point-min) (1- (point-at-bol)))
  21862. (point-at-eol))
  21863. (end-of-line 1))))
  21864. ;; Specific LaTeX stuff
  21865. (when latexp
  21866. (require 'org-export-latex nil)
  21867. (org-export-latex-cleaned-string))
  21868. (when asciip
  21869. (org-export-ascii-clean-string))
  21870. ;; Specific HTML stuff
  21871. (when htmlp
  21872. ;; Convert LaTeX fragments to images
  21873. (when (plist-get parameters :LaTeX-fragments)
  21874. (org-format-latex
  21875. (concat "ltxpng/" (file-name-sans-extension
  21876. (file-name-nondirectory
  21877. org-current-export-file)))
  21878. org-current-export-dir nil "Creating LaTeX image %s"))
  21879. (message "Exporting..."))
  21880. ;; Remove or replace comments
  21881. (goto-char (point-min))
  21882. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  21883. (if commentsp
  21884. (progn (add-text-properties
  21885. (match-beginning 0) (match-end 0) '(org-protected t))
  21886. (replace-match (format commentsp (match-string 1)) t t))
  21887. (replace-match "")))
  21888. ;; Find matches for radio targets and turn them into internal links
  21889. (goto-char (point-min))
  21890. (when re-radio
  21891. (while (re-search-forward re-radio nil t)
  21892. (org-if-unprotected
  21893. (replace-match "\\1[[\\2]]"))))
  21894. ;; Find all links that contain a newline and put them into a single line
  21895. (goto-char (point-min))
  21896. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  21897. (org-if-unprotected
  21898. (replace-match "\\1 \\3")
  21899. (goto-char (match-beginning 0))))
  21900. ;; Normalize links: Convert angle and plain links into bracket links
  21901. ;; Expand link abbreviations
  21902. (goto-char (point-min))
  21903. (while (re-search-forward re-plain-link nil t)
  21904. (goto-char (1- (match-end 0)))
  21905. (org-if-unprotected
  21906. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  21907. ":" (match-string 3) "]]")))
  21908. ;; added 'org-link face to links
  21909. (put-text-property 0 (length s) 'face 'org-link s)
  21910. (replace-match s t t))))
  21911. (goto-char (point-min))
  21912. (while (re-search-forward re-angle-link nil t)
  21913. (goto-char (1- (match-end 0)))
  21914. (org-if-unprotected
  21915. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  21916. ":" (match-string 3) "]]")))
  21917. (put-text-property 0 (length s) 'face 'org-link s)
  21918. (replace-match s t t))))
  21919. (goto-char (point-min))
  21920. (while (re-search-forward org-bracket-link-regexp nil t)
  21921. (org-if-unprotected
  21922. (let* ((s (concat "[[" (setq xx (save-match-data
  21923. (org-link-expand-abbrev (match-string 1))))
  21924. "]"
  21925. (if (match-end 3)
  21926. (match-string 2)
  21927. (concat "[" xx "]"))
  21928. "]")))
  21929. (put-text-property 0 (length s) 'face 'org-link s)
  21930. (replace-match s t t))))
  21931. ;; Find multiline emphasis and put them into single line
  21932. (when (plist-get parameters :emph-multiline)
  21933. (goto-char (point-min))
  21934. (while (re-search-forward org-emph-re nil t)
  21935. (if (not (= (char-after (match-beginning 3))
  21936. (char-after (match-beginning 4))))
  21937. (org-if-unprotected
  21938. (subst-char-in-region (match-beginning 0) (match-end 0)
  21939. ?\n ?\ t)
  21940. (goto-char (1- (match-end 0))))
  21941. (goto-char (1+ (match-beginning 0))))))
  21942. (setq rtn (buffer-string)))
  21943. (kill-buffer " org-mode-tmp")
  21944. rtn))
  21945. (defun org-export-grab-title-from-buffer ()
  21946. "Get a title for the current document, from looking at the buffer."
  21947. (let ((inhibit-read-only t))
  21948. (save-excursion
  21949. (goto-char (point-min))
  21950. (let ((end (save-excursion (outline-next-heading) (point))))
  21951. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  21952. ;; Mark the line so that it will not be exported as normal text.
  21953. (org-unmodified
  21954. (add-text-properties (match-beginning 0) (match-end 0)
  21955. (list :org-license-to-kill t)))
  21956. ;; Return the title string
  21957. (org-trim (match-string 0)))))))
  21958. (defun org-export-get-title-from-subtree ()
  21959. "Return subtree title and exclude it from export."
  21960. (let (title (m (mark)))
  21961. (save-excursion
  21962. (goto-char (region-beginning))
  21963. (when (and (org-at-heading-p)
  21964. (>= (org-end-of-subtree t t) (region-end)))
  21965. ;; This is a subtree, we take the title from the first heading
  21966. (goto-char (region-beginning))
  21967. (looking-at org-todo-line-regexp)
  21968. (setq title (match-string 3))
  21969. (org-unmodified
  21970. (add-text-properties (point) (1+ (point-at-eol))
  21971. (list :org-license-to-kill t)))))
  21972. title))
  21973. (defun org-solidify-link-text (s &optional alist)
  21974. "Take link text and make a safe target out of it."
  21975. (save-match-data
  21976. (let* ((rtn
  21977. (mapconcat
  21978. 'identity
  21979. (org-split-string s "[ \t\r\n]+") "--"))
  21980. (a (assoc rtn alist)))
  21981. (or (cdr a) rtn))))
  21982. (defun org-get-min-level (lines)
  21983. "Get the minimum level in LINES."
  21984. (let ((re "^\\(\\*+\\) ") l min)
  21985. (catch 'exit
  21986. (while (setq l (pop lines))
  21987. (if (string-match re l)
  21988. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  21989. 1)))
  21990. ;; Variable holding the vector with section numbers
  21991. (defvar org-section-numbers (make-vector org-level-max 0))
  21992. (defun org-init-section-numbers ()
  21993. "Initialize the vector for the section numbers."
  21994. (let* ((level -1)
  21995. (numbers (nreverse (org-split-string "" "\\.")))
  21996. (depth (1- (length org-section-numbers)))
  21997. (i depth) number-string)
  21998. (while (>= i 0)
  21999. (if (> i level)
  22000. (aset org-section-numbers i 0)
  22001. (setq number-string (or (car numbers) "0"))
  22002. (if (string-match "\\`[A-Z]\\'" number-string)
  22003. (aset org-section-numbers i
  22004. (- (string-to-char number-string) ?A -1))
  22005. (aset org-section-numbers i (string-to-number number-string)))
  22006. (pop numbers))
  22007. (setq i (1- i)))))
  22008. (defun org-section-number (&optional level)
  22009. "Return a string with the current section number.
  22010. When LEVEL is non-nil, increase section numbers on that level."
  22011. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22012. (when level
  22013. (when (> level -1)
  22014. (aset org-section-numbers
  22015. level (1+ (aref org-section-numbers level))))
  22016. (setq idx (1+ level))
  22017. (while (<= idx depth)
  22018. (if (not (= idx 1))
  22019. (aset org-section-numbers idx 0))
  22020. (setq idx (1+ idx))))
  22021. (setq idx 0)
  22022. (while (<= idx depth)
  22023. (setq n (aref org-section-numbers idx))
  22024. (setq string (concat string (if (not (string= string "")) "." "")
  22025. (int-to-string n)))
  22026. (setq idx (1+ idx)))
  22027. (save-match-data
  22028. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22029. (setq string (replace-match "" t nil string)))
  22030. (if (string-match "\\(\\.0\\)+\\'" string)
  22031. (setq string (replace-match "" t nil string))))
  22032. string))
  22033. ;;; ASCII export
  22034. (defvar org-last-level nil) ; dynamically scoped variable
  22035. (defvar org-min-level nil) ; dynamically scoped variable
  22036. (defvar org-levels-open nil) ; dynamically scoped parameter
  22037. (defvar org-ascii-current-indentation nil) ; For communication
  22038. (defun org-export-as-ascii (arg)
  22039. "Export the outline as a pretty ASCII file.
  22040. If there is an active region, export only the region.
  22041. The prefix ARG specifies how many levels of the outline should become
  22042. underlined headlines. The default is 3."
  22043. (interactive "P")
  22044. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22045. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22046. (org-infile-export-plist)))
  22047. (region-p (org-region-active-p))
  22048. (subtree-p
  22049. (when region-p
  22050. (save-excursion
  22051. (goto-char (region-beginning))
  22052. (and (org-at-heading-p)
  22053. (>= (org-end-of-subtree t t) (region-end))))))
  22054. (custom-times org-display-custom-times)
  22055. (org-ascii-current-indentation '(0 . 0))
  22056. (level 0) line txt
  22057. (umax nil)
  22058. (umax-toc nil)
  22059. (case-fold-search nil)
  22060. (filename (concat (file-name-as-directory
  22061. (org-export-directory :ascii opt-plist))
  22062. (file-name-sans-extension
  22063. (or (and subtree-p
  22064. (org-entry-get (region-beginning)
  22065. "EXPORT_FILE_NAME" t))
  22066. (file-name-nondirectory buffer-file-name)))
  22067. ".txt"))
  22068. (filename (if (equal (file-truename filename)
  22069. (file-truename buffer-file-name))
  22070. (concat filename ".txt")
  22071. filename))
  22072. (buffer (find-file-noselect filename))
  22073. (org-levels-open (make-vector org-level-max nil))
  22074. (odd org-odd-levels-only)
  22075. (date (plist-get opt-plist :date))
  22076. (author (plist-get opt-plist :author))
  22077. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22078. (plist-get opt-plist :title)
  22079. (and (not
  22080. (plist-get opt-plist :skip-before-1st-heading))
  22081. (org-export-grab-title-from-buffer))
  22082. (file-name-sans-extension
  22083. (file-name-nondirectory buffer-file-name))))
  22084. (email (plist-get opt-plist :email))
  22085. (language (plist-get opt-plist :language))
  22086. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22087. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22088. (todo nil)
  22089. (lang-words nil)
  22090. (region
  22091. (buffer-substring
  22092. (if (org-region-active-p) (region-beginning) (point-min))
  22093. (if (org-region-active-p) (region-end) (point-max))))
  22094. (lines (org-split-string
  22095. (org-cleaned-string-for-export
  22096. region
  22097. :for-ascii t
  22098. :skip-before-1st-heading
  22099. (plist-get opt-plist :skip-before-1st-heading)
  22100. :drawers (plist-get opt-plist :drawers)
  22101. :verbatim-multiline t
  22102. :archived-trees
  22103. (plist-get opt-plist :archived-trees)
  22104. :add-text (plist-get opt-plist :text))
  22105. "\n"))
  22106. thetoc have-headings first-heading-pos
  22107. table-open table-buffer)
  22108. (let ((inhibit-read-only t))
  22109. (org-unmodified
  22110. (remove-text-properties (point-min) (point-max)
  22111. '(:org-license-to-kill t))))
  22112. (setq org-min-level (org-get-min-level lines))
  22113. (setq org-last-level org-min-level)
  22114. (org-init-section-numbers)
  22115. (find-file-noselect filename)
  22116. (setq lang-words (or (assoc language org-export-language-setup)
  22117. (assoc "en" org-export-language-setup)))
  22118. (switch-to-buffer-other-window buffer)
  22119. (erase-buffer)
  22120. (fundamental-mode)
  22121. ;; create local variables for all options, to make sure all called
  22122. ;; functions get the correct information
  22123. (mapc (lambda (x)
  22124. (set (make-local-variable (cdr x))
  22125. (plist-get opt-plist (car x))))
  22126. org-export-plist-vars)
  22127. (org-set-local 'org-odd-levels-only odd)
  22128. (setq umax (if arg (prefix-numeric-value arg)
  22129. org-export-headline-levels))
  22130. (setq umax-toc (if (integerp org-export-with-toc)
  22131. (min org-export-with-toc umax)
  22132. umax))
  22133. ;; File header
  22134. (if title (org-insert-centered title ?=))
  22135. (insert "\n")
  22136. (if (and (or author email)
  22137. org-export-author-info)
  22138. (insert (concat (nth 1 lang-words) ": " (or author "")
  22139. (if email (concat " <" email ">") "")
  22140. "\n")))
  22141. (cond
  22142. ((and date (string-match "%" date))
  22143. (setq date (format-time-string date (current-time))))
  22144. (date)
  22145. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22146. (if (and date org-export-time-stamp-file)
  22147. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22148. (insert "\n\n")
  22149. (if org-export-with-toc
  22150. (progn
  22151. (push (concat (nth 3 lang-words) "\n") thetoc)
  22152. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22153. (mapc '(lambda (line)
  22154. (if (string-match org-todo-line-regexp
  22155. line)
  22156. ;; This is a headline
  22157. (progn
  22158. (setq have-headings t)
  22159. (setq level (- (match-end 1) (match-beginning 1))
  22160. level (org-tr-level level)
  22161. txt (match-string 3 line)
  22162. todo
  22163. (or (and org-export-mark-todo-in-toc
  22164. (match-beginning 2)
  22165. (not (member (match-string 2 line)
  22166. org-done-keywords)))
  22167. ; TODO, not DONE
  22168. (and org-export-mark-todo-in-toc
  22169. (= level umax-toc)
  22170. (org-search-todo-below
  22171. line lines level))))
  22172. (setq txt (org-html-expand-for-ascii txt))
  22173. (while (string-match org-bracket-link-regexp txt)
  22174. (setq txt
  22175. (replace-match
  22176. (match-string (if (match-end 2) 3 1) txt)
  22177. t t txt)))
  22178. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22179. (string-match
  22180. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22181. txt))
  22182. (setq txt (replace-match "" t t txt)))
  22183. (if (string-match quote-re0 txt)
  22184. (setq txt (replace-match "" t t txt)))
  22185. (if org-export-with-section-numbers
  22186. (setq txt (concat (org-section-number level)
  22187. " " txt)))
  22188. (if (<= level umax-toc)
  22189. (progn
  22190. (push
  22191. (concat
  22192. (make-string
  22193. (* (max 0 (- level org-min-level)) 4) ?\ )
  22194. (format (if todo "%s (*)\n" "%s\n") txt))
  22195. thetoc)
  22196. (setq org-last-level level))
  22197. ))))
  22198. lines)
  22199. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22200. (org-init-section-numbers)
  22201. (while (setq line (pop lines))
  22202. ;; Remove the quoted HTML tags.
  22203. (setq line (org-html-expand-for-ascii line))
  22204. ;; Remove targets
  22205. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22206. (setq line (replace-match "" t t line)))
  22207. ;; Replace internal links
  22208. (while (string-match org-bracket-link-regexp line)
  22209. (setq line (replace-match
  22210. (if (match-end 3) "[\\3]" "[\\1]")
  22211. t nil line)))
  22212. (when custom-times
  22213. (setq line (org-translate-time line)))
  22214. (cond
  22215. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22216. ;; a Headline
  22217. (setq first-heading-pos (or first-heading-pos (point)))
  22218. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22219. txt (match-string 2 line))
  22220. (org-ascii-level-start level txt umax lines))
  22221. ((and org-export-with-tables
  22222. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22223. (if (not table-open)
  22224. ;; New table starts
  22225. (setq table-open t table-buffer nil))
  22226. ;; Accumulate lines
  22227. (setq table-buffer (cons line table-buffer))
  22228. (when (or (not lines)
  22229. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22230. (car lines))))
  22231. (setq table-open nil
  22232. table-buffer (nreverse table-buffer))
  22233. (insert (mapconcat
  22234. (lambda (x)
  22235. (org-fix-indentation x org-ascii-current-indentation))
  22236. (org-format-table-ascii table-buffer)
  22237. "\n") "\n")))
  22238. (t
  22239. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22240. (if (and org-export-with-fixed-width
  22241. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22242. (setq line (replace-match "\\1" nil nil line)))
  22243. (insert line "\n"))))
  22244. (normal-mode)
  22245. ;; insert the table of contents
  22246. (when thetoc
  22247. (goto-char (point-min))
  22248. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22249. (progn
  22250. (goto-char (match-beginning 0))
  22251. (replace-match ""))
  22252. (goto-char first-heading-pos))
  22253. (mapc 'insert thetoc)
  22254. (or (looking-at "[ \t]*\n[ \t]*\n")
  22255. (insert "\n\n")))
  22256. ;; Convert whitespace place holders
  22257. (goto-char (point-min))
  22258. (let (beg end)
  22259. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22260. (setq end (next-single-property-change beg 'org-whitespace))
  22261. (goto-char beg)
  22262. (delete-region beg end)
  22263. (insert (make-string (- end beg) ?\ ))))
  22264. (save-buffer)
  22265. ;; remove display and invisible chars
  22266. (let (beg end)
  22267. (goto-char (point-min))
  22268. (while (setq beg (next-single-property-change (point) 'display))
  22269. (setq end (next-single-property-change beg 'display))
  22270. (delete-region beg end)
  22271. (goto-char beg)
  22272. (insert "=>"))
  22273. (goto-char (point-min))
  22274. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22275. (setq end (next-single-property-change beg 'org-cwidth))
  22276. (delete-region beg end)
  22277. (goto-char beg)))
  22278. (goto-char (point-min))))
  22279. (defun org-export-ascii-clean-string ()
  22280. "Do extra work for ASCII export"
  22281. (goto-char (point-min))
  22282. (while (re-search-forward org-verbatim-re nil t)
  22283. (goto-char (match-end 2))
  22284. (backward-delete-char 1) (insert "'")
  22285. (goto-char (match-beginning 2))
  22286. (delete-char 1) (insert "`")
  22287. (goto-char (match-end 2))))
  22288. (defun org-search-todo-below (line lines level)
  22289. "Search the subtree below LINE for any TODO entries."
  22290. (let ((rest (cdr (memq line lines)))
  22291. (re org-todo-line-regexp)
  22292. line lv todo)
  22293. (catch 'exit
  22294. (while (setq line (pop rest))
  22295. (if (string-match re line)
  22296. (progn
  22297. (setq lv (- (match-end 1) (match-beginning 1))
  22298. todo (and (match-beginning 2)
  22299. (not (member (match-string 2 line)
  22300. org-done-keywords))))
  22301. ; TODO, not DONE
  22302. (if (<= lv level) (throw 'exit nil))
  22303. (if todo (throw 'exit t))))))))
  22304. (defun org-html-expand-for-ascii (line)
  22305. "Handle quoted HTML for ASCII export."
  22306. (if org-export-html-expand
  22307. (while (string-match "@<[^<>\n]*>" line)
  22308. ;; We just remove the tags for now.
  22309. (setq line (replace-match "" nil nil line))))
  22310. line)
  22311. (defun org-insert-centered (s &optional underline)
  22312. "Insert the string S centered and underline it with character UNDERLINE."
  22313. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  22314. (insert (make-string ind ?\ ) s "\n")
  22315. (if underline
  22316. (insert (make-string ind ?\ )
  22317. (make-string (string-width s) underline)
  22318. "\n"))))
  22319. (defun org-ascii-level-start (level title umax &optional lines)
  22320. "Insert a new level in ASCII export."
  22321. (let (char (n (- level umax 1)) (ind 0))
  22322. (if (> level umax)
  22323. (progn
  22324. (insert (make-string (* 2 n) ?\ )
  22325. (char-to-string (nth (% n (length org-export-ascii-bullets))
  22326. org-export-ascii-bullets))
  22327. " " title "\n")
  22328. ;; find the indentation of the next non-empty line
  22329. (catch 'stop
  22330. (while lines
  22331. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  22332. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  22333. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  22334. (pop lines)))
  22335. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  22336. (if (or (not (equal (char-before) ?\n))
  22337. (not (equal (char-before (1- (point))) ?\n)))
  22338. (insert "\n"))
  22339. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  22340. (unless org-export-with-tags
  22341. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  22342. (setq title (replace-match "" t t title))))
  22343. (if org-export-with-section-numbers
  22344. (setq title (concat (org-section-number level) " " title)))
  22345. (insert title "\n" (make-string (string-width title) char) "\n")
  22346. (setq org-ascii-current-indentation '(0 . 0)))))
  22347. (defun org-export-visible (type arg)
  22348. "Create a copy of the visible part of the current buffer, and export it.
  22349. The copy is created in a temporary buffer and removed after use.
  22350. TYPE is the final key (as a string) that also select the export command in
  22351. the `C-c C-e' export dispatcher.
  22352. As a special case, if the you type SPC at the prompt, the temporary
  22353. org-mode file will not be removed but presented to you so that you can
  22354. continue to use it. The prefix arg ARG is passed through to the exporting
  22355. command."
  22356. (interactive
  22357. (list (progn
  22358. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  22359. (read-char-exclusive))
  22360. current-prefix-arg))
  22361. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  22362. (error "Invalid export key"))
  22363. (let* ((binding (cdr (assoc type
  22364. '((?a . org-export-as-ascii)
  22365. (?\C-a . org-export-as-ascii)
  22366. (?b . org-export-as-html-and-open)
  22367. (?\C-b . org-export-as-html-and-open)
  22368. (?h . org-export-as-html)
  22369. (?H . org-export-as-html-to-buffer)
  22370. (?R . org-export-region-as-html)
  22371. (?x . org-export-as-xoxo)))))
  22372. (keepp (equal type ?\ ))
  22373. (file buffer-file-name)
  22374. (buffer (get-buffer-create "*Org Export Visible*"))
  22375. s e)
  22376. ;; Need to hack the drawers here.
  22377. (save-excursion
  22378. (goto-char (point-min))
  22379. (while (re-search-forward org-drawer-regexp nil t)
  22380. (goto-char (match-beginning 1))
  22381. (or (org-invisible-p) (org-flag-drawer nil))))
  22382. (with-current-buffer buffer (erase-buffer))
  22383. (save-excursion
  22384. (setq s (goto-char (point-min)))
  22385. (while (not (= (point) (point-max)))
  22386. (goto-char (org-find-invisible))
  22387. (append-to-buffer buffer s (point))
  22388. (setq s (goto-char (org-find-visible))))
  22389. (org-cycle-hide-drawers 'all)
  22390. (goto-char (point-min))
  22391. (unless keepp
  22392. ;; Copy all comment lines to the end, to make sure #+ settings are
  22393. ;; still available for the second export step. Kind of a hack, but
  22394. ;; does do the trick.
  22395. (if (looking-at "#[^\r\n]*")
  22396. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  22397. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  22398. (append-to-buffer buffer (1+ (match-beginning 0))
  22399. (min (point-max) (1+ (match-end 0))))))
  22400. (set-buffer buffer)
  22401. (let ((buffer-file-name file)
  22402. (org-inhibit-startup t))
  22403. (org-mode)
  22404. (show-all)
  22405. (unless keepp (funcall binding arg))))
  22406. (if (not keepp)
  22407. (kill-buffer buffer)
  22408. (switch-to-buffer-other-window buffer)
  22409. (goto-char (point-min)))))
  22410. (defun org-find-visible ()
  22411. (let ((s (point)))
  22412. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22413. (get-char-property s 'invisible)))
  22414. s))
  22415. (defun org-find-invisible ()
  22416. (let ((s (point)))
  22417. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22418. (not (get-char-property s 'invisible))))
  22419. s))
  22420. ;;; HTML export
  22421. (defun org-get-current-options ()
  22422. "Return a string with current options as keyword options.
  22423. Does include HTML export options as well as TODO and CATEGORY stuff."
  22424. (format
  22425. "#+TITLE: %s
  22426. #+AUTHOR: %s
  22427. #+EMAIL: %s
  22428. #+LANGUAGE: %s
  22429. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  22430. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s TeX:%s LaTeX:%s skip:%s d:%s tags:%s
  22431. #+CATEGORY: %s
  22432. #+SEQ_TODO: %s
  22433. #+TYP_TODO: %s
  22434. #+PRIORITIES: %c %c %c
  22435. #+DRAWERS: %s
  22436. #+STARTUP: %s %s %s %s %s
  22437. #+TAGS: %s
  22438. #+ARCHIVE: %s
  22439. #+LINK: %s
  22440. "
  22441. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  22442. org-export-headline-levels
  22443. org-export-with-section-numbers
  22444. org-export-with-toc
  22445. org-export-preserve-breaks
  22446. org-export-html-expand
  22447. org-export-with-fixed-width
  22448. org-export-with-tables
  22449. org-export-with-sub-superscripts
  22450. org-export-with-special-strings
  22451. org-export-with-footnotes
  22452. org-export-with-emphasize
  22453. org-export-with-TeX-macros
  22454. org-export-with-LaTeX-fragments
  22455. org-export-skip-text-before-1st-heading
  22456. org-export-with-drawers
  22457. org-export-with-tags
  22458. (file-name-nondirectory buffer-file-name)
  22459. "TODO FEEDBACK VERIFY DONE"
  22460. "Me Jason Marie DONE"
  22461. org-highest-priority org-lowest-priority org-default-priority
  22462. (mapconcat 'identity org-drawers " ")
  22463. (cdr (assoc org-startup-folded
  22464. '((nil . "showall") (t . "overview") (content . "content"))))
  22465. (if org-odd-levels-only "odd" "oddeven")
  22466. (if org-hide-leading-stars "hidestars" "showstars")
  22467. (if org-startup-align-all-tables "align" "noalign")
  22468. (cond ((eq t org-log-done) "logdone")
  22469. ((not org-log-done) "nologging")
  22470. ((listp org-log-done)
  22471. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  22472. org-log-done " ")))
  22473. (or (mapconcat (lambda (x)
  22474. (cond
  22475. ((equal '(:startgroup) x) "{")
  22476. ((equal '(:endgroup) x) "}")
  22477. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  22478. (t (car x))))
  22479. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  22480. org-archive-location
  22481. "org file:~/org/%s.org"
  22482. ))
  22483. (defun org-insert-export-options-template ()
  22484. "Insert into the buffer a template with information for exporting."
  22485. (interactive)
  22486. (if (not (bolp)) (newline))
  22487. (let ((s (org-get-current-options)))
  22488. (and (string-match "#\\+CATEGORY" s)
  22489. (setq s (substring s 0 (match-beginning 0))))
  22490. (insert s)))
  22491. (defun org-toggle-fixed-width-section (arg)
  22492. "Toggle the fixed-width export.
  22493. If there is no active region, the QUOTE keyword at the current headline is
  22494. inserted or removed. When present, it causes the text between this headline
  22495. and the next to be exported as fixed-width text, and unmodified.
  22496. If there is an active region, this command adds or removes a colon as the
  22497. first character of this line. If the first character of a line is a colon,
  22498. this line is also exported in fixed-width font."
  22499. (interactive "P")
  22500. (let* ((cc 0)
  22501. (regionp (org-region-active-p))
  22502. (beg (if regionp (region-beginning) (point)))
  22503. (end (if regionp (region-end)))
  22504. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  22505. (case-fold-search nil)
  22506. (re "[ \t]*\\(:\\)")
  22507. off)
  22508. (if regionp
  22509. (save-excursion
  22510. (goto-char beg)
  22511. (setq cc (current-column))
  22512. (beginning-of-line 1)
  22513. (setq off (looking-at re))
  22514. (while (> nlines 0)
  22515. (setq nlines (1- nlines))
  22516. (beginning-of-line 1)
  22517. (cond
  22518. (arg
  22519. (move-to-column cc t)
  22520. (insert ":\n")
  22521. (forward-line -1))
  22522. ((and off (looking-at re))
  22523. (replace-match "" t t nil 1))
  22524. ((not off) (move-to-column cc t) (insert ":")))
  22525. (forward-line 1)))
  22526. (save-excursion
  22527. (org-back-to-heading)
  22528. (if (looking-at (concat outline-regexp
  22529. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  22530. (replace-match "" t t nil 1)
  22531. (if (looking-at outline-regexp)
  22532. (progn
  22533. (goto-char (match-end 0))
  22534. (insert org-quote-string " "))))))))
  22535. (defun org-export-as-html-and-open (arg)
  22536. "Export the outline as HTML and immediately open it with a browser.
  22537. If there is an active region, export only the region.
  22538. The prefix ARG specifies how many levels of the outline should become
  22539. headlines. The default is 3. Lower levels will become bulleted lists."
  22540. (interactive "P")
  22541. (org-export-as-html arg 'hidden)
  22542. (org-open-file buffer-file-name))
  22543. (defun org-export-as-html-batch ()
  22544. "Call `org-export-as-html', may be used in batch processing as
  22545. emacs --batch
  22546. --load=$HOME/lib/emacs/org.el
  22547. --eval \"(setq org-export-headline-levels 2)\"
  22548. --visit=MyFile --funcall org-export-as-html-batch"
  22549. (org-export-as-html org-export-headline-levels 'hidden))
  22550. (defun org-export-as-html-to-buffer (arg)
  22551. "Call `org-exort-as-html` with output to a temporary buffer.
  22552. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  22553. (interactive "P")
  22554. (org-export-as-html arg nil nil "*Org HTML Export*")
  22555. (switch-to-buffer-other-window "*Org HTML Export*"))
  22556. (defun org-replace-region-by-html (beg end)
  22557. "Assume the current region has org-mode syntax, and convert it to HTML.
  22558. This can be used in any buffer. For example, you could write an
  22559. itemized list in org-mode syntax in an HTML buffer and then use this
  22560. command to convert it."
  22561. (interactive "r")
  22562. (let (reg html buf pop-up-frames)
  22563. (save-window-excursion
  22564. (if (org-mode-p)
  22565. (setq html (org-export-region-as-html
  22566. beg end t 'string))
  22567. (setq reg (buffer-substring beg end)
  22568. buf (get-buffer-create "*Org tmp*"))
  22569. (with-current-buffer buf
  22570. (erase-buffer)
  22571. (insert reg)
  22572. (org-mode)
  22573. (setq html (org-export-region-as-html
  22574. (point-min) (point-max) t 'string)))
  22575. (kill-buffer buf)))
  22576. (delete-region beg end)
  22577. (insert html)))
  22578. (defun org-export-region-as-html (beg end &optional body-only buffer)
  22579. "Convert region from BEG to END in org-mode buffer to HTML.
  22580. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  22581. contents, and only produce the region of converted text, useful for
  22582. cut-and-paste operations.
  22583. If BUFFER is a buffer or a string, use/create that buffer as a target
  22584. of the converted HTML. If BUFFER is the symbol `string', return the
  22585. produced HTML as a string and leave not buffer behind. For example,
  22586. a Lisp program could call this function in the following way:
  22587. (setq html (org-export-region-as-html beg end t 'string))
  22588. When called interactively, the output buffer is selected, and shown
  22589. in a window. A non-interactive call will only retunr the buffer."
  22590. (interactive "r\nP")
  22591. (when (interactive-p)
  22592. (setq buffer "*Org HTML Export*"))
  22593. (let ((transient-mark-mode t) (zmacs-regions t)
  22594. rtn)
  22595. (goto-char end)
  22596. (set-mark (point)) ;; to activate the region
  22597. (goto-char beg)
  22598. (setq rtn (org-export-as-html
  22599. nil nil nil
  22600. buffer body-only))
  22601. (if (fboundp 'deactivate-mark) (deactivate-mark))
  22602. (if (and (interactive-p) (bufferp rtn))
  22603. (switch-to-buffer-other-window rtn)
  22604. rtn)))
  22605. (defvar html-table-tag nil) ; dynamically scoped into this.
  22606. (defun org-export-as-html (arg &optional hidden ext-plist
  22607. to-buffer body-only)
  22608. "Export the outline as a pretty HTML file.
  22609. If there is an active region, export only the region. The prefix
  22610. ARG specifies how many levels of the outline should become
  22611. headlines. The default is 3. Lower levels will become bulleted
  22612. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  22613. EXT-PLIST is a property list with external parameters overriding
  22614. org-mode's default settings, but still inferior to file-local
  22615. settings. When TO-BUFFER is non-nil, create a buffer with that
  22616. name and export to that buffer. If TO-BUFFER is the symbol `string',
  22617. don't leave any buffer behind but just return the resulting HTML as
  22618. a string. When BODY-ONLY is set, don't produce the file header and footer,
  22619. simply return the content of <body>...</body>, without even
  22620. the body tags themselves."
  22621. (interactive "P")
  22622. ;; Make sure we have a file name when we need it.
  22623. (when (and (not (or to-buffer body-only))
  22624. (not buffer-file-name))
  22625. (if (buffer-base-buffer)
  22626. (org-set-local 'buffer-file-name
  22627. (with-current-buffer (buffer-base-buffer)
  22628. buffer-file-name))
  22629. (error "Need a file name to be able to export.")))
  22630. (message "Exporting...")
  22631. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22632. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  22633. (setq-default org-done-keywords org-done-keywords)
  22634. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  22635. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22636. ext-plist
  22637. (org-infile-export-plist)))
  22638. (style (plist-get opt-plist :style))
  22639. (link-validate (plist-get opt-plist :link-validation-function))
  22640. valid thetoc have-headings first-heading-pos
  22641. (odd org-odd-levels-only)
  22642. (region-p (org-region-active-p))
  22643. (subtree-p
  22644. (when region-p
  22645. (save-excursion
  22646. (goto-char (region-beginning))
  22647. (and (org-at-heading-p)
  22648. (>= (org-end-of-subtree t t) (region-end))))))
  22649. ;; The following two are dynamically scoped into other
  22650. ;; routines below.
  22651. (org-current-export-dir (org-export-directory :html opt-plist))
  22652. (org-current-export-file buffer-file-name)
  22653. (level 0) (line "") (origline "") txt todo
  22654. (umax nil)
  22655. (umax-toc nil)
  22656. (filename (if to-buffer nil
  22657. (expand-file-name
  22658. (concat
  22659. (file-name-sans-extension
  22660. (or (and subtree-p
  22661. (org-entry-get (region-beginning)
  22662. "EXPORT_FILE_NAME" t))
  22663. (file-name-nondirectory buffer-file-name)))
  22664. "." org-export-html-extension)
  22665. (file-name-as-directory
  22666. (org-export-directory :html opt-plist)))))
  22667. (current-dir (if buffer-file-name
  22668. (file-name-directory buffer-file-name)
  22669. default-directory))
  22670. (buffer (if to-buffer
  22671. (cond
  22672. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  22673. (t (get-buffer-create to-buffer)))
  22674. (find-file-noselect filename)))
  22675. (org-levels-open (make-vector org-level-max nil))
  22676. (date (plist-get opt-plist :date))
  22677. (author (plist-get opt-plist :author))
  22678. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22679. (plist-get opt-plist :title)
  22680. (and (not
  22681. (plist-get opt-plist :skip-before-1st-heading))
  22682. (org-export-grab-title-from-buffer))
  22683. (and buffer-file-name
  22684. (file-name-sans-extension
  22685. (file-name-nondirectory buffer-file-name)))
  22686. "UNTITLED"))
  22687. (html-table-tag (plist-get opt-plist :html-table-tag))
  22688. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22689. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  22690. (inquote nil)
  22691. (infixed nil)
  22692. (in-local-list nil)
  22693. (local-list-num nil)
  22694. (local-list-indent nil)
  22695. (llt org-plain-list-ordered-item-terminator)
  22696. (email (plist-get opt-plist :email))
  22697. (language (plist-get opt-plist :language))
  22698. (lang-words nil)
  22699. (target-alist nil) tg
  22700. (head-count 0) cnt
  22701. (start 0)
  22702. (coding-system (and (boundp 'buffer-file-coding-system)
  22703. buffer-file-coding-system))
  22704. (coding-system-for-write (or org-export-html-coding-system
  22705. coding-system))
  22706. (save-buffer-coding-system (or org-export-html-coding-system
  22707. coding-system))
  22708. (charset (and coding-system-for-write
  22709. (fboundp 'coding-system-get)
  22710. (coding-system-get coding-system-for-write
  22711. 'mime-charset)))
  22712. (region
  22713. (buffer-substring
  22714. (if region-p (region-beginning) (point-min))
  22715. (if region-p (region-end) (point-max))))
  22716. (lines
  22717. (org-split-string
  22718. (org-cleaned-string-for-export
  22719. region
  22720. :emph-multiline t
  22721. :for-html t
  22722. :skip-before-1st-heading
  22723. (plist-get opt-plist :skip-before-1st-heading)
  22724. :drawers (plist-get opt-plist :drawers)
  22725. :archived-trees
  22726. (plist-get opt-plist :archived-trees)
  22727. :add-text
  22728. (plist-get opt-plist :text)
  22729. :LaTeX-fragments
  22730. (plist-get opt-plist :LaTeX-fragments))
  22731. "[\r\n]"))
  22732. table-open type
  22733. table-buffer table-orig-buffer
  22734. ind start-is-num starter didclose
  22735. rpl path desc descp desc1 desc2 link
  22736. )
  22737. (let ((inhibit-read-only t))
  22738. (org-unmodified
  22739. (remove-text-properties (point-min) (point-max)
  22740. '(:org-license-to-kill t))))
  22741. (message "Exporting...")
  22742. (setq org-min-level (org-get-min-level lines))
  22743. (setq org-last-level org-min-level)
  22744. (org-init-section-numbers)
  22745. (cond
  22746. ((and date (string-match "%" date))
  22747. (setq date (format-time-string date (current-time))))
  22748. (date)
  22749. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22750. ;; Get the language-dependent settings
  22751. (setq lang-words (or (assoc language org-export-language-setup)
  22752. (assoc "en" org-export-language-setup)))
  22753. ;; Switch to the output buffer
  22754. (set-buffer buffer)
  22755. (erase-buffer)
  22756. (fundamental-mode)
  22757. (and (fboundp 'set-buffer-file-coding-system)
  22758. (set-buffer-file-coding-system coding-system-for-write))
  22759. (let ((case-fold-search nil)
  22760. (org-odd-levels-only odd))
  22761. ;; create local variables for all options, to make sure all called
  22762. ;; functions get the correct information
  22763. (mapc (lambda (x)
  22764. (set (make-local-variable (cdr x))
  22765. (plist-get opt-plist (car x))))
  22766. org-export-plist-vars)
  22767. (setq umax (if arg (prefix-numeric-value arg)
  22768. org-export-headline-levels))
  22769. (setq umax-toc (if (integerp org-export-with-toc)
  22770. (min org-export-with-toc umax)
  22771. umax))
  22772. (unless body-only
  22773. ;; File header
  22774. (insert (format
  22775. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  22776. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  22777. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  22778. lang=\"%s\" xml:lang=\"%s\">
  22779. <head>
  22780. <title>%s</title>
  22781. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  22782. <meta name=\"generator\" content=\"Org-mode\"/>
  22783. <meta name=\"generated\" content=\"%s\"/>
  22784. <meta name=\"author\" content=\"%s\"/>
  22785. %s
  22786. </head><body>
  22787. "
  22788. language language (org-html-expand title)
  22789. (or charset "iso-8859-1") date author style))
  22790. (insert (or (plist-get opt-plist :preamble) ""))
  22791. (when (plist-get opt-plist :auto-preamble)
  22792. (if title (insert (format org-export-html-title-format
  22793. (org-html-expand title))))))
  22794. (if (and org-export-with-toc (not body-only))
  22795. (progn
  22796. (push (format "<h%d>%s</h%d>\n"
  22797. org-export-html-toplevel-hlevel
  22798. (nth 3 lang-words)
  22799. org-export-html-toplevel-hlevel)
  22800. thetoc)
  22801. (push "<ul>\n<li>" thetoc)
  22802. (setq lines
  22803. (mapcar '(lambda (line)
  22804. (if (string-match org-todo-line-regexp line)
  22805. ;; This is a headline
  22806. (progn
  22807. (setq have-headings t)
  22808. (setq level (- (match-end 1) (match-beginning 1))
  22809. level (org-tr-level level)
  22810. txt (save-match-data
  22811. (org-html-expand
  22812. (org-export-cleanup-toc-line
  22813. (match-string 3 line))))
  22814. todo
  22815. (or (and org-export-mark-todo-in-toc
  22816. (match-beginning 2)
  22817. (not (member (match-string 2 line)
  22818. org-done-keywords)))
  22819. ; TODO, not DONE
  22820. (and org-export-mark-todo-in-toc
  22821. (= level umax-toc)
  22822. (org-search-todo-below
  22823. line lines level))))
  22824. (if (string-match
  22825. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  22826. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  22827. (if (string-match quote-re0 txt)
  22828. (setq txt (replace-match "" t t txt)))
  22829. (if org-export-with-section-numbers
  22830. (setq txt (concat (org-section-number level)
  22831. " " txt)))
  22832. (if (<= level (max umax umax-toc))
  22833. (setq head-count (+ head-count 1)))
  22834. (if (<= level umax-toc)
  22835. (progn
  22836. (if (> level org-last-level)
  22837. (progn
  22838. (setq cnt (- level org-last-level))
  22839. (while (>= (setq cnt (1- cnt)) 0)
  22840. (push "\n<ul>\n<li>" thetoc))
  22841. (push "\n" thetoc)))
  22842. (if (< level org-last-level)
  22843. (progn
  22844. (setq cnt (- org-last-level level))
  22845. (while (>= (setq cnt (1- cnt)) 0)
  22846. (push "</li>\n</ul>" thetoc))
  22847. (push "\n" thetoc)))
  22848. ;; Check for targets
  22849. (while (string-match org-target-regexp line)
  22850. (setq tg (match-string 1 line)
  22851. line (replace-match
  22852. (concat "@<span class=\"target\">" tg "@</span> ")
  22853. t t line))
  22854. (push (cons (org-solidify-link-text tg)
  22855. (format "sec-%d" head-count))
  22856. target-alist))
  22857. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  22858. (setq txt (replace-match "" t t txt)))
  22859. (push
  22860. (format
  22861. (if todo
  22862. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  22863. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  22864. head-count txt) thetoc)
  22865. (setq org-last-level level))
  22866. )))
  22867. line)
  22868. lines))
  22869. (while (> org-last-level (1- org-min-level))
  22870. (setq org-last-level (1- org-last-level))
  22871. (push "</li>\n</ul>\n" thetoc))
  22872. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22873. (setq head-count 0)
  22874. (org-init-section-numbers)
  22875. (while (setq line (pop lines) origline line)
  22876. (catch 'nextline
  22877. ;; end of quote section?
  22878. (when (and inquote (string-match "^\\*+ " line))
  22879. (insert "</pre>\n")
  22880. (setq inquote nil))
  22881. ;; inside a quote section?
  22882. (when inquote
  22883. (insert (org-html-protect line) "\n")
  22884. (throw 'nextline nil))
  22885. ;; verbatim lines
  22886. (when (and org-export-with-fixed-width
  22887. (string-match "^[ \t]*:\\(.*\\)" line))
  22888. (when (not infixed)
  22889. (setq infixed t)
  22890. (insert "<pre>\n"))
  22891. (insert (org-html-protect (match-string 1 line)) "\n")
  22892. (when (and lines
  22893. (not (string-match "^[ \t]*\\(:.*\\)"
  22894. (car lines))))
  22895. (setq infixed nil)
  22896. (insert "</pre>\n"))
  22897. (throw 'nextline nil))
  22898. ;; Protected HTML
  22899. (when (get-text-property 0 'org-protected line)
  22900. (let (par)
  22901. (when (re-search-backward
  22902. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  22903. (setq par (match-string 1))
  22904. (replace-match "\\2\n"))
  22905. (insert line "\n")
  22906. (while (and lines
  22907. (or (= (length (car lines)) 0)
  22908. (get-text-property 0 'org-protected (car lines))))
  22909. (insert (pop lines) "\n"))
  22910. (and par (insert "<p>\n")))
  22911. (throw 'nextline nil))
  22912. ;; Horizontal line
  22913. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  22914. (insert "\n<hr/>\n")
  22915. (throw 'nextline nil))
  22916. ;; make targets to anchors
  22917. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  22918. (cond
  22919. ((match-end 2)
  22920. (setq line (replace-match
  22921. (concat "@<a name=\""
  22922. (org-solidify-link-text (match-string 1 line))
  22923. "\">\\nbsp@</a>")
  22924. t t line)))
  22925. ((and org-export-with-toc (equal (string-to-char line) ?*))
  22926. (setq line (replace-match
  22927. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  22928. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  22929. t t line)))
  22930. (t
  22931. (setq line (replace-match
  22932. (concat "@<a name=\""
  22933. (org-solidify-link-text (match-string 1 line))
  22934. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  22935. t t line)))))
  22936. (setq line (org-html-handle-time-stamps line))
  22937. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  22938. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  22939. ;; Also handle sub_superscripts and checkboxes
  22940. (or (string-match org-table-hline-regexp line)
  22941. (setq line (org-html-expand line)))
  22942. ;; Format the links
  22943. (setq start 0)
  22944. (while (string-match org-bracket-link-analytic-regexp line start)
  22945. (setq start (match-beginning 0))
  22946. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  22947. (setq path (match-string 3 line))
  22948. (setq desc1 (if (match-end 5) (match-string 5 line))
  22949. desc2 (if (match-end 2) (concat type ":" path) path)
  22950. descp (and desc1 (not (equal desc1 desc2)))
  22951. desc (or desc1 desc2))
  22952. ;; Make an image out of the description if that is so wanted
  22953. (when (and descp (org-file-image-p desc))
  22954. (save-match-data
  22955. (if (string-match "^file:" desc)
  22956. (setq desc (substring desc (match-end 0)))))
  22957. (setq desc (concat "<img src=\"" desc "\"/>")))
  22958. ;; FIXME: do we need to unescape here somewhere?
  22959. (cond
  22960. ((equal type "internal")
  22961. (setq rpl
  22962. (concat
  22963. "<a href=\"#"
  22964. (org-solidify-link-text
  22965. (save-match-data (org-link-unescape path)) target-alist)
  22966. "\">" desc "</a>")))
  22967. ((member type '("http" "https"))
  22968. ;; standard URL, just check if we need to inline an image
  22969. (if (and (or (eq t org-export-html-inline-images)
  22970. (and org-export-html-inline-images (not descp)))
  22971. (org-file-image-p path))
  22972. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  22973. (setq link (concat type ":" path))
  22974. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  22975. ((member type '("ftp" "mailto" "news"))
  22976. ;; standard URL
  22977. (setq link (concat type ":" path))
  22978. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  22979. ((string= type "file")
  22980. ;; FILE link
  22981. (let* ((filename path)
  22982. (abs-p (file-name-absolute-p filename))
  22983. thefile file-is-image-p search)
  22984. (save-match-data
  22985. (if (string-match "::\\(.*\\)" filename)
  22986. (setq search (match-string 1 filename)
  22987. filename (replace-match "" t nil filename)))
  22988. (setq valid
  22989. (if (functionp link-validate)
  22990. (funcall link-validate filename current-dir)
  22991. t))
  22992. (setq file-is-image-p (org-file-image-p filename))
  22993. (setq thefile (if abs-p (expand-file-name filename) filename))
  22994. (when (and org-export-html-link-org-files-as-html
  22995. (string-match "\\.org$" thefile))
  22996. (setq thefile (concat (substring thefile 0
  22997. (match-beginning 0))
  22998. "." org-export-html-extension))
  22999. (if (and search
  23000. ;; make sure this is can be used as target search
  23001. (not (string-match "^[0-9]*$" search))
  23002. (not (string-match "^\\*" search))
  23003. (not (string-match "^/.*/$" search)))
  23004. (setq thefile (concat thefile "#"
  23005. (org-solidify-link-text
  23006. (org-link-unescape search)))))
  23007. (when (string-match "^file:" desc)
  23008. (setq desc (replace-match "" t t desc))
  23009. (if (string-match "\\.org$" desc)
  23010. (setq desc (replace-match "" t t desc))))))
  23011. (setq rpl (if (and file-is-image-p
  23012. (or (eq t org-export-html-inline-images)
  23013. (and org-export-html-inline-images
  23014. (not descp))))
  23015. (concat "<img src=\"" thefile "\"/>")
  23016. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23017. (if (not valid) (setq rpl desc))))
  23018. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23019. (setq rpl (concat "<i>&lt;" type ":"
  23020. (save-match-data (org-link-unescape path))
  23021. "&gt;</i>"))))
  23022. (setq line (replace-match rpl t t line)
  23023. start (+ start (length rpl))))
  23024. ;; TODO items
  23025. (if (and (string-match org-todo-line-regexp line)
  23026. (match-beginning 2))
  23027. (setq line
  23028. (concat (substring line 0 (match-beginning 2))
  23029. "<span class=\""
  23030. (if (member (match-string 2 line)
  23031. org-done-keywords)
  23032. "done" "todo")
  23033. "\">" (match-string 2 line)
  23034. "</span>" (substring line (match-end 2)))))
  23035. ;; Does this contain a reference to a footnote?
  23036. (when org-export-with-footnotes
  23037. (setq start 0)
  23038. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23039. (if (get-text-property (match-beginning 2) 'org-protected line)
  23040. (setq start (match-end 2))
  23041. (let ((n (match-string 2 line)))
  23042. (setq line
  23043. (replace-match
  23044. (format
  23045. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23046. (match-string 1 line) n n n)
  23047. t t line))))))
  23048. (cond
  23049. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23050. ;; This is a headline
  23051. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23052. txt (match-string 2 line))
  23053. (if (string-match quote-re0 txt)
  23054. (setq txt (replace-match "" t t txt)))
  23055. (if (<= level (max umax umax-toc))
  23056. (setq head-count (+ head-count 1)))
  23057. (when in-local-list
  23058. ;; Close any local lists before inserting a new header line
  23059. (while local-list-num
  23060. (org-close-li)
  23061. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23062. (pop local-list-num))
  23063. (setq local-list-indent nil
  23064. in-local-list nil))
  23065. (setq first-heading-pos (or first-heading-pos (point)))
  23066. (org-html-level-start level txt umax
  23067. (and org-export-with-toc (<= level umax))
  23068. head-count)
  23069. ;; QUOTES
  23070. (when (string-match quote-re line)
  23071. (insert "<pre>")
  23072. (setq inquote t)))
  23073. ((and org-export-with-tables
  23074. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23075. (if (not table-open)
  23076. ;; New table starts
  23077. (setq table-open t table-buffer nil table-orig-buffer nil))
  23078. ;; Accumulate lines
  23079. (setq table-buffer (cons line table-buffer)
  23080. table-orig-buffer (cons origline table-orig-buffer))
  23081. (when (or (not lines)
  23082. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23083. (car lines))))
  23084. (setq table-open nil
  23085. table-buffer (nreverse table-buffer)
  23086. table-orig-buffer (nreverse table-orig-buffer))
  23087. (org-close-par-maybe)
  23088. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23089. (t
  23090. ;; Normal lines
  23091. (when (string-match
  23092. (cond
  23093. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23094. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23095. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23096. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23097. line)
  23098. (setq ind (org-get-string-indentation line)
  23099. start-is-num (match-beginning 4)
  23100. starter (if (match-beginning 2)
  23101. (substring (match-string 2 line) 0 -1))
  23102. line (substring line (match-beginning 5)))
  23103. (unless (string-match "[^ \t]" line)
  23104. ;; empty line. Pretend indentation is large.
  23105. (setq ind (if org-empty-line-terminates-plain-lists
  23106. 0
  23107. (1+ (or (car local-list-indent) 1)))))
  23108. (setq didclose nil)
  23109. (while (and in-local-list
  23110. (or (and (= ind (car local-list-indent))
  23111. (not starter))
  23112. (< ind (car local-list-indent))))
  23113. (setq didclose t)
  23114. (org-close-li)
  23115. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23116. (pop local-list-num) (pop local-list-indent)
  23117. (setq in-local-list local-list-indent))
  23118. (cond
  23119. ((and starter
  23120. (or (not in-local-list)
  23121. (> ind (car local-list-indent))))
  23122. ;; Start new (level of) list
  23123. (org-close-par-maybe)
  23124. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23125. (push start-is-num local-list-num)
  23126. (push ind local-list-indent)
  23127. (setq in-local-list t))
  23128. (starter
  23129. ;; continue current list
  23130. (org-close-li)
  23131. (insert "<li>\n"))
  23132. (didclose
  23133. ;; we did close a list, normal text follows: need <p>
  23134. (org-open-par)))
  23135. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23136. (setq line
  23137. (replace-match
  23138. (if (equal (match-string 1 line) "X")
  23139. "<b>[X]</b>"
  23140. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23141. t t line))))
  23142. ;; Empty lines start a new paragraph. If hand-formatted lists
  23143. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23144. ;; also start a new paragraph.
  23145. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23146. ;; Is this the start of a footnote?
  23147. (when org-export-with-footnotes
  23148. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23149. (org-close-par-maybe)
  23150. (let ((n (match-string 1 line)))
  23151. (setq line (replace-match
  23152. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23153. ;; Check if the line break needs to be conserved
  23154. (cond
  23155. ((string-match "\\\\\\\\[ \t]*$" line)
  23156. (setq line (replace-match "<br/>" t t line)))
  23157. (org-export-preserve-breaks
  23158. (setq line (concat line "<br/>"))))
  23159. (insert line "\n")))))
  23160. ;; Properly close all local lists and other lists
  23161. (when inquote (insert "</pre>\n"))
  23162. (when in-local-list
  23163. ;; Close any local lists before inserting a new header line
  23164. (while local-list-num
  23165. (org-close-li)
  23166. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23167. (pop local-list-num))
  23168. (setq local-list-indent nil
  23169. in-local-list nil))
  23170. (org-html-level-start 1 nil umax
  23171. (and org-export-with-toc (<= level umax))
  23172. head-count)
  23173. (unless body-only
  23174. (when (plist-get opt-plist :auto-postamble)
  23175. (insert "<div id=\"postamble\">")
  23176. (when (and org-export-author-info author)
  23177. (insert "<p class=\"author\"> "
  23178. (nth 1 lang-words) ": " author "\n")
  23179. (when email
  23180. (if (listp (split-string email ",+ *"))
  23181. (mapc (lambda(e)
  23182. (insert "<a href=\"mailto:" e "\">&lt;"
  23183. e "&gt;</a>\n"))
  23184. (split-string email ",+ *"))
  23185. (insert "<a href=\"mailto:" email "\">&lt;"
  23186. email "&gt;</a>\n")))
  23187. (insert "</p>\n"))
  23188. (when (and date org-export-time-stamp-file)
  23189. (insert "<p class=\"date\"> "
  23190. (nth 2 lang-words) ": "
  23191. date "</p>\n"))
  23192. (insert "</div>"))
  23193. (if org-export-html-with-timestamp
  23194. (insert org-export-html-html-helper-timestamp))
  23195. (insert (or (plist-get opt-plist :postamble) ""))
  23196. (insert "</body>\n</html>\n"))
  23197. (normal-mode)
  23198. (if (eq major-mode default-major-mode) (html-mode))
  23199. ;; insert the table of contents
  23200. (goto-char (point-min))
  23201. (when thetoc
  23202. (if (or (re-search-forward
  23203. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23204. (re-search-forward
  23205. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23206. (progn
  23207. (goto-char (match-beginning 0))
  23208. (replace-match ""))
  23209. (goto-char first-heading-pos)
  23210. (when (looking-at "\\s-*</p>")
  23211. (goto-char (match-end 0))
  23212. (insert "\n")))
  23213. (insert "<div id=\"table-of-contents\">\n")
  23214. (mapc 'insert thetoc)
  23215. (insert "</div>\n"))
  23216. ;; remove empty paragraphs and lists
  23217. (goto-char (point-min))
  23218. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23219. (replace-match ""))
  23220. (goto-char (point-min))
  23221. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23222. (replace-match ""))
  23223. ;; Convert whitespace place holders
  23224. (goto-char (point-min))
  23225. (let (beg end n)
  23226. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23227. (setq n (get-text-property beg 'org-whitespace)
  23228. end (next-single-property-change beg 'org-whitespace))
  23229. (goto-char beg)
  23230. (delete-region beg end)
  23231. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23232. (make-string n ?x)))))
  23233. (or to-buffer (save-buffer))
  23234. (goto-char (point-min))
  23235. (message "Exporting... done")
  23236. (if (eq to-buffer 'string)
  23237. (prog1 (buffer-substring (point-min) (point-max))
  23238. (kill-buffer (current-buffer)))
  23239. (current-buffer)))))
  23240. (defvar org-table-colgroup-info nil)
  23241. (defun org-format-table-ascii (lines)
  23242. "Format a table for ascii export."
  23243. (if (stringp lines)
  23244. (setq lines (org-split-string lines "\n")))
  23245. (if (not (string-match "^[ \t]*|" (car lines)))
  23246. ;; Table made by table.el - test for spanning
  23247. lines
  23248. ;; A normal org table
  23249. ;; Get rid of hlines at beginning and end
  23250. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23251. (setq lines (nreverse lines))
  23252. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23253. (setq lines (nreverse lines))
  23254. (when org-export-table-remove-special-lines
  23255. ;; Check if the table has a marking column. If yes remove the
  23256. ;; column and the special lines
  23257. (setq lines (org-table-clean-before-export lines)))
  23258. ;; Get rid of the vertical lines except for grouping
  23259. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23260. rtn line vl1 start)
  23261. (while (setq line (pop lines))
  23262. (if (string-match org-table-hline-regexp line)
  23263. (and (string-match "|\\(.*\\)|" line)
  23264. (setq line (replace-match " \\1" t nil line)))
  23265. (setq start 0 vl1 vl)
  23266. (while (string-match "|" line start)
  23267. (setq start (match-end 0))
  23268. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23269. (push line rtn))
  23270. (nreverse rtn))))
  23271. (defun org-colgroup-info-to-vline-list (info)
  23272. (let (vl new last)
  23273. (while info
  23274. (setq last new new (pop info))
  23275. (if (or (memq last '(:end :startend))
  23276. (memq new '(:start :startend)))
  23277. (push t vl)
  23278. (push nil vl)))
  23279. (setq vl (nreverse vl))
  23280. (and vl (setcar vl nil))
  23281. vl))
  23282. (defun org-format-table-html (lines olines)
  23283. "Find out which HTML converter to use and return the HTML code."
  23284. (if (stringp lines)
  23285. (setq lines (org-split-string lines "\n")))
  23286. (if (string-match "^[ \t]*|" (car lines))
  23287. ;; A normal org table
  23288. (org-format-org-table-html lines)
  23289. ;; Table made by table.el - test for spanning
  23290. (let* ((hlines (delq nil (mapcar
  23291. (lambda (x)
  23292. (if (string-match "^[ \t]*\\+-" x) x
  23293. nil))
  23294. lines)))
  23295. (first (car hlines))
  23296. (ll (and (string-match "\\S-+" first)
  23297. (match-string 0 first)))
  23298. (re (concat "^[ \t]*" (regexp-quote ll)))
  23299. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  23300. hlines))))
  23301. (if (and (not spanning)
  23302. (not org-export-prefer-native-exporter-for-tables))
  23303. ;; We can use my own converter with HTML conversions
  23304. (org-format-table-table-html lines)
  23305. ;; Need to use the code generator in table.el, with the original text.
  23306. (org-format-table-table-html-using-table-generate-source olines)))))
  23307. (defun org-format-org-table-html (lines &optional splice)
  23308. "Format a table into HTML."
  23309. ;; Get rid of hlines at beginning and end
  23310. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23311. (setq lines (nreverse lines))
  23312. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23313. (setq lines (nreverse lines))
  23314. (when org-export-table-remove-special-lines
  23315. ;; Check if the table has a marking column. If yes remove the
  23316. ;; column and the special lines
  23317. (setq lines (org-table-clean-before-export lines)))
  23318. (let ((head (and org-export-highlight-first-table-line
  23319. (delq nil (mapcar
  23320. (lambda (x) (string-match "^[ \t]*|-" x))
  23321. (cdr lines)))))
  23322. (nlines 0) fnum i
  23323. tbopen line fields html gr colgropen)
  23324. (if splice (setq head nil))
  23325. (unless splice (push (if head "<thead>" "<tbody>") html))
  23326. (setq tbopen t)
  23327. (while (setq line (pop lines))
  23328. (catch 'next-line
  23329. (if (string-match "^[ \t]*|-" line)
  23330. (progn
  23331. (unless splice
  23332. (push (if head "</thead>" "</tbody>") html)
  23333. (if lines (push "<tbody>" html) (setq tbopen nil)))
  23334. (setq head nil) ;; head ends here, first time around
  23335. ;; ignore this line
  23336. (throw 'next-line t)))
  23337. ;; Break the line into fields
  23338. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23339. (unless fnum (setq fnum (make-vector (length fields) 0)))
  23340. (setq nlines (1+ nlines) i -1)
  23341. (push (concat "<tr>"
  23342. (mapconcat
  23343. (lambda (x)
  23344. (setq i (1+ i))
  23345. (if (and (< i nlines)
  23346. (string-match org-table-number-regexp x))
  23347. (incf (aref fnum i)))
  23348. (if head
  23349. (concat (car org-export-table-header-tags) x
  23350. (cdr org-export-table-header-tags))
  23351. (concat (car org-export-table-data-tags) x
  23352. (cdr org-export-table-data-tags))))
  23353. fields "")
  23354. "</tr>")
  23355. html)))
  23356. (unless splice (if tbopen (push "</tbody>" html)))
  23357. (unless splice (push "</table>\n" html))
  23358. (setq html (nreverse html))
  23359. (unless splice
  23360. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  23361. (push (mapconcat
  23362. (lambda (x)
  23363. (setq gr (pop org-table-colgroup-info))
  23364. (format "%s<col align=\"%s\"></col>%s"
  23365. (if (memq gr '(:start :startend))
  23366. (prog1
  23367. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  23368. (setq colgropen t))
  23369. "")
  23370. (if (> (/ (float x) nlines) org-table-number-fraction)
  23371. "right" "left")
  23372. (if (memq gr '(:end :startend))
  23373. (progn (setq colgropen nil) "</colgroup>")
  23374. "")))
  23375. fnum "")
  23376. html)
  23377. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  23378. (push html-table-tag html))
  23379. (concat (mapconcat 'identity html "\n") "\n")))
  23380. (defun org-table-clean-before-export (lines)
  23381. "Check if the table has a marking column.
  23382. If yes remove the column and the special lines."
  23383. (setq org-table-colgroup-info nil)
  23384. (if (memq nil
  23385. (mapcar
  23386. (lambda (x) (or (string-match "^[ \t]*|-" x)
  23387. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  23388. lines))
  23389. (progn
  23390. (setq org-table-clean-did-remove-column nil)
  23391. (delq nil
  23392. (mapcar
  23393. (lambda (x)
  23394. (cond
  23395. ((string-match "^[ \t]*| */ *|" x)
  23396. (setq org-table-colgroup-info
  23397. (mapcar (lambda (x)
  23398. (cond ((member x '("<" "&lt;")) :start)
  23399. ((member x '(">" "&gt;")) :end)
  23400. ((member x '("<>" "&lt;&gt;")) :startend)
  23401. (t nil)))
  23402. (org-split-string x "[ \t]*|[ \t]*")))
  23403. nil)
  23404. (t x)))
  23405. lines)))
  23406. (setq org-table-clean-did-remove-column t)
  23407. (delq nil
  23408. (mapcar
  23409. (lambda (x)
  23410. (cond
  23411. ((string-match "^[ \t]*| */ *|" x)
  23412. (setq org-table-colgroup-info
  23413. (mapcar (lambda (x)
  23414. (cond ((member x '("<" "&lt;")) :start)
  23415. ((member x '(">" "&gt;")) :end)
  23416. ((member x '("<>" "&lt;&gt;")) :startend)
  23417. (t nil)))
  23418. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  23419. nil)
  23420. ((string-match "^[ \t]*| *[!_^/] *|" x)
  23421. nil) ; ignore this line
  23422. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  23423. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  23424. ;; remove the first column
  23425. (replace-match "\\1|" t nil x))))
  23426. lines))))
  23427. (defun org-format-table-table-html (lines)
  23428. "Format a table generated by table.el into HTML.
  23429. This conversion does *not* use `table-generate-source' from table.el.
  23430. This has the advantage that Org-mode's HTML conversions can be used.
  23431. But it has the disadvantage, that no cell- or row-spanning is allowed."
  23432. (let (line field-buffer
  23433. (head org-export-highlight-first-table-line)
  23434. fields html empty)
  23435. (setq html (concat html-table-tag "\n"))
  23436. (while (setq line (pop lines))
  23437. (setq empty "&nbsp;")
  23438. (catch 'next-line
  23439. (if (string-match "^[ \t]*\\+-" line)
  23440. (progn
  23441. (if field-buffer
  23442. (progn
  23443. (setq
  23444. html
  23445. (concat
  23446. html
  23447. "<tr>"
  23448. (mapconcat
  23449. (lambda (x)
  23450. (if (equal x "") (setq x empty))
  23451. (if head
  23452. (concat (car org-export-table-header-tags) x
  23453. (cdr org-export-table-header-tags))
  23454. (concat (car org-export-table-data-tags) x
  23455. (cdr org-export-table-data-tags))))
  23456. field-buffer "\n")
  23457. "</tr>\n"))
  23458. (setq head nil)
  23459. (setq field-buffer nil)))
  23460. ;; Ignore this line
  23461. (throw 'next-line t)))
  23462. ;; Break the line into fields and store the fields
  23463. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23464. (if field-buffer
  23465. (setq field-buffer (mapcar
  23466. (lambda (x)
  23467. (concat x "<br/>" (pop fields)))
  23468. field-buffer))
  23469. (setq field-buffer fields))))
  23470. (setq html (concat html "</table>\n"))
  23471. html))
  23472. (defun org-format-table-table-html-using-table-generate-source (lines)
  23473. "Format a table into html, using `table-generate-source' from table.el.
  23474. This has the advantage that cell- or row-spanning is allowed.
  23475. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  23476. (require 'table)
  23477. (with-current-buffer (get-buffer-create " org-tmp1 ")
  23478. (erase-buffer)
  23479. (insert (mapconcat 'identity lines "\n"))
  23480. (goto-char (point-min))
  23481. (if (not (re-search-forward "|[^+]" nil t))
  23482. (error "Error processing table"))
  23483. (table-recognize-table)
  23484. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  23485. (table-generate-source 'html " org-tmp2 ")
  23486. (set-buffer " org-tmp2 ")
  23487. (buffer-substring (point-min) (point-max))))
  23488. (defun org-html-handle-time-stamps (s)
  23489. "Format time stamps in string S, or remove them."
  23490. (catch 'exit
  23491. (let (r b)
  23492. (while (string-match org-maybe-keyword-time-regexp s)
  23493. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  23494. ;; never export CLOCK
  23495. (throw 'exit ""))
  23496. (or b (setq b (substring s 0 (match-beginning 0))))
  23497. (if (not org-export-with-timestamps)
  23498. (setq r (concat r (substring s 0 (match-beginning 0)))
  23499. s (substring s (match-end 0)))
  23500. (setq r (concat
  23501. r (substring s 0 (match-beginning 0))
  23502. (if (match-end 1)
  23503. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  23504. (match-string 1 s)))
  23505. (format " @<span class=\"timestamp\">%s@</span>"
  23506. (substring
  23507. (org-translate-time (match-string 3 s)) 1 -1)))
  23508. s (substring s (match-end 0)))))
  23509. ;; Line break if line started and ended with time stamp stuff
  23510. (if (not r)
  23511. s
  23512. (setq r (concat r s))
  23513. (unless (string-match "\\S-" (concat b s))
  23514. (setq r (concat r "@<br/>")))
  23515. r))))
  23516. (defun org-html-protect (s)
  23517. ;; convert & to &amp;, < to &lt; and > to &gt;
  23518. (let ((start 0))
  23519. (while (string-match "&" s start)
  23520. (setq s (replace-match "&amp;" t t s)
  23521. start (1+ (match-beginning 0))))
  23522. (while (string-match "<" s)
  23523. (setq s (replace-match "&lt;" t t s)))
  23524. (while (string-match ">" s)
  23525. (setq s (replace-match "&gt;" t t s))))
  23526. s)
  23527. (defun org-export-cleanup-toc-line (s)
  23528. "Remove tags and time staps from lines going into the toc."
  23529. (when (memq org-export-with-tags '(not-in-toc nil))
  23530. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  23531. (setq s (replace-match "" t t s))))
  23532. (when org-export-remove-timestamps-from-toc
  23533. (while (string-match org-maybe-keyword-time-regexp s)
  23534. (setq s (replace-match "" t t s))))
  23535. (while (string-match org-bracket-link-regexp s)
  23536. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  23537. t t s)))
  23538. s)
  23539. (defun org-html-expand (string)
  23540. "Prepare STRING for HTML export. Applies all active conversions.
  23541. If there are links in the string, don't modify these."
  23542. (let* ((re (concat org-bracket-link-regexp "\\|"
  23543. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  23544. m s l res)
  23545. (while (setq m (string-match re string))
  23546. (setq s (substring string 0 m)
  23547. l (match-string 0 string)
  23548. string (substring string (match-end 0)))
  23549. (push (org-html-do-expand s) res)
  23550. (push l res))
  23551. (push (org-html-do-expand string) res)
  23552. (apply 'concat (nreverse res))))
  23553. (defun org-html-do-expand (s)
  23554. "Apply all active conversions to translate special ASCII to HTML."
  23555. (setq s (org-html-protect s))
  23556. (if org-export-html-expand
  23557. (let ((start 0))
  23558. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  23559. (setq s (replace-match "<\\1>" t nil s)))))
  23560. (if org-export-with-emphasize
  23561. (setq s (org-export-html-convert-emphasize s)))
  23562. (if org-export-with-special-strings
  23563. (setq s (org-export-html-convert-special-strings s)))
  23564. (if org-export-with-sub-superscripts
  23565. (setq s (org-export-html-convert-sub-super s)))
  23566. (if org-export-with-TeX-macros
  23567. (let ((start 0) wd ass)
  23568. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  23569. (if (get-text-property (match-beginning 0) 'org-protected s)
  23570. (setq start (match-end 0))
  23571. (setq wd (match-string 1 s))
  23572. (if (setq ass (assoc wd org-html-entities))
  23573. (setq s (replace-match (or (cdr ass)
  23574. (concat "&" (car ass) ";"))
  23575. t t s))
  23576. (setq start (+ start (length wd))))))))
  23577. s)
  23578. (defun org-create-multibrace-regexp (left right n)
  23579. "Create a regular expression which will match a balanced sexp.
  23580. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  23581. as single character strings.
  23582. The regexp returned will match the entire expression including the
  23583. delimiters. It will also define a single group which contains the
  23584. match except for the outermost delimiters. The maximum depth of
  23585. stacked delimiters is N. Escaping delimiters is not possible."
  23586. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  23587. (or "\\|")
  23588. (re nothing)
  23589. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  23590. (while (> n 1)
  23591. (setq n (1- n)
  23592. re (concat re or next)
  23593. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  23594. (concat left "\\(" re "\\)" right)))
  23595. (defvar org-match-substring-regexp
  23596. (concat
  23597. "\\([^\\]\\)\\([_^]\\)\\("
  23598. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23599. "\\|"
  23600. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  23601. "\\|"
  23602. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  23603. "The regular expression matching a sub- or superscript.")
  23604. (defvar org-match-substring-with-braces-regexp
  23605. (concat
  23606. "\\([^\\]\\)\\([_^]\\)\\("
  23607. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23608. "\\)")
  23609. "The regular expression matching a sub- or superscript, forcing braces.")
  23610. (defconst org-export-html-special-string-regexps
  23611. '(("\\\\-" . "&shy;")
  23612. ("---\\([^-]\\)" . "&mdash;\\1")
  23613. ("--\\([^-]\\)" . "&ndash;\\1")
  23614. ("\\.\\.\\." . "&hellip;"))
  23615. "Regular expressions for special string conversion.")
  23616. (defun org-export-html-convert-special-strings (string)
  23617. "Convert special characters in STRING to HTML."
  23618. (let ((all org-export-html-special-string-regexps)
  23619. e a re rpl start)
  23620. (while (setq a (pop all))
  23621. (setq re (car a) rpl (cdr a) start 0)
  23622. (while (string-match re string start)
  23623. (if (get-text-property (match-beginning 0) 'org-protected string)
  23624. (setq start (match-end 0))
  23625. (setq string (replace-match rpl t nil string)))))
  23626. string))
  23627. (defun org-export-html-convert-sub-super (string)
  23628. "Convert sub- and superscripts in STRING to HTML."
  23629. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  23630. (while (string-match org-match-substring-regexp string s)
  23631. (cond
  23632. ((and requireb (match-end 8)) (setq s (match-end 2)))
  23633. ((get-text-property (match-beginning 2) 'org-protected string)
  23634. (setq s (match-end 2)))
  23635. (t
  23636. (setq s (match-end 1)
  23637. key (if (string= (match-string 2 string) "_") "sub" "sup")
  23638. c (or (match-string 8 string)
  23639. (match-string 6 string)
  23640. (match-string 5 string))
  23641. string (replace-match
  23642. (concat (match-string 1 string)
  23643. "<" key ">" c "</" key ">")
  23644. t t string)))))
  23645. (while (string-match "\\\\\\([_^]\\)" string)
  23646. (setq string (replace-match (match-string 1 string) t t string)))
  23647. string))
  23648. (defun org-export-html-convert-emphasize (string)
  23649. "Apply emphasis."
  23650. (let ((s 0) rpl)
  23651. (while (string-match org-emph-re string s)
  23652. (if (not (equal
  23653. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  23654. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  23655. (setq s (match-beginning 0)
  23656. rpl
  23657. (concat
  23658. (match-string 1 string)
  23659. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  23660. (match-string 4 string)
  23661. (nth 3 (assoc (match-string 3 string)
  23662. org-emphasis-alist))
  23663. (match-string 5 string))
  23664. string (replace-match rpl t t string)
  23665. s (+ s (- (length rpl) 2)))
  23666. (setq s (1+ s))))
  23667. string))
  23668. (defvar org-par-open nil)
  23669. (defun org-open-par ()
  23670. "Insert <p>, but first close previous paragraph if any."
  23671. (org-close-par-maybe)
  23672. (insert "\n<p>")
  23673. (setq org-par-open t))
  23674. (defun org-close-par-maybe ()
  23675. "Close paragraph if there is one open."
  23676. (when org-par-open
  23677. (insert "</p>")
  23678. (setq org-par-open nil)))
  23679. (defun org-close-li ()
  23680. "Close <li> if necessary."
  23681. (org-close-par-maybe)
  23682. (insert "</li>\n"))
  23683. (defvar body-only) ; dynamically scoped into this.
  23684. (defun org-html-level-start (level title umax with-toc head-count)
  23685. "Insert a new level in HTML export.
  23686. When TITLE is nil, just close all open levels."
  23687. (org-close-par-maybe)
  23688. (let ((l org-level-max))
  23689. (while (>= l level)
  23690. (if (aref org-levels-open (1- l))
  23691. (progn
  23692. (org-html-level-close l umax)
  23693. (aset org-levels-open (1- l) nil)))
  23694. (setq l (1- l)))
  23695. (when title
  23696. ;; If title is nil, this means this function is called to close
  23697. ;; all levels, so the rest is done only if title is given
  23698. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  23699. (setq title (replace-match
  23700. (if org-export-with-tags
  23701. (save-match-data
  23702. (concat
  23703. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  23704. (mapconcat 'identity (org-split-string
  23705. (match-string 1 title) ":")
  23706. "&nbsp;")
  23707. "</span>"))
  23708. "")
  23709. t t title)))
  23710. (if (> level umax)
  23711. (progn
  23712. (if (aref org-levels-open (1- level))
  23713. (progn
  23714. (org-close-li)
  23715. (insert "<li>" title "<br/>\n"))
  23716. (aset org-levels-open (1- level) t)
  23717. (org-close-par-maybe)
  23718. (insert "<ul>\n<li>" title "<br/>\n")))
  23719. (aset org-levels-open (1- level) t)
  23720. (if (and org-export-with-section-numbers (not body-only))
  23721. (setq title (concat (org-section-number level) " " title)))
  23722. (setq level (+ level org-export-html-toplevel-hlevel -1))
  23723. (if with-toc
  23724. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  23725. level level head-count title level))
  23726. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  23727. (org-open-par)))))
  23728. (defun org-html-level-close (level max-outline-level)
  23729. "Terminate one level in HTML export."
  23730. (if (<= level max-outline-level)
  23731. (insert "</div>\n")
  23732. (org-close-li)
  23733. (insert "</ul>\n")))
  23734. ;;; iCalendar export
  23735. ;;;###autoload
  23736. (defun org-export-icalendar-this-file ()
  23737. "Export current file as an iCalendar file.
  23738. The iCalendar file will be located in the same directory as the Org-mode
  23739. file, but with extension `.ics'."
  23740. (interactive)
  23741. (org-export-icalendar nil buffer-file-name))
  23742. ;;;###autoload
  23743. (defun org-export-icalendar-all-agenda-files ()
  23744. "Export all files in `org-agenda-files' to iCalendar .ics files.
  23745. Each iCalendar file will be located in the same directory as the Org-mode
  23746. file, but with extension `.ics'."
  23747. (interactive)
  23748. (apply 'org-export-icalendar nil (org-agenda-files t)))
  23749. ;;;###autoload
  23750. (defun org-export-icalendar-combine-agenda-files ()
  23751. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  23752. The file is stored under the name `org-combined-agenda-icalendar-file'."
  23753. (interactive)
  23754. (apply 'org-export-icalendar t (org-agenda-files t)))
  23755. (defun org-export-icalendar (combine &rest files)
  23756. "Create iCalendar files for all elements of FILES.
  23757. If COMBINE is non-nil, combine all calendar entries into a single large
  23758. file and store it under the name `org-combined-agenda-icalendar-file'."
  23759. (save-excursion
  23760. (org-prepare-agenda-buffers files)
  23761. (let* ((dir (org-export-directory
  23762. :ical (list :publishing-directory
  23763. org-export-publishing-directory)))
  23764. file ical-file ical-buffer category started org-agenda-new-buffers)
  23765. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  23766. (when combine
  23767. (setq ical-file
  23768. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  23769. org-combined-agenda-icalendar-file
  23770. (expand-file-name org-combined-agenda-icalendar-file dir))
  23771. ical-buffer (org-get-agenda-file-buffer ical-file))
  23772. (set-buffer ical-buffer) (erase-buffer))
  23773. (while (setq file (pop files))
  23774. (catch 'nextfile
  23775. (org-check-agenda-file file)
  23776. (set-buffer (org-get-agenda-file-buffer file))
  23777. (unless combine
  23778. (setq ical-file (concat (file-name-as-directory dir)
  23779. (file-name-sans-extension
  23780. (file-name-nondirectory buffer-file-name))
  23781. ".ics"))
  23782. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  23783. (with-current-buffer ical-buffer (erase-buffer)))
  23784. (setq category (or org-category
  23785. (file-name-sans-extension
  23786. (file-name-nondirectory buffer-file-name))))
  23787. (if (symbolp category) (setq category (symbol-name category)))
  23788. (let ((standard-output ical-buffer))
  23789. (if combine
  23790. (and (not started) (setq started t)
  23791. (org-start-icalendar-file org-icalendar-combined-name))
  23792. (org-start-icalendar-file category))
  23793. (org-print-icalendar-entries combine)
  23794. (when (or (and combine (not files)) (not combine))
  23795. (org-finish-icalendar-file)
  23796. (set-buffer ical-buffer)
  23797. (save-buffer)
  23798. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  23799. (org-release-buffers org-agenda-new-buffers))))
  23800. (defvar org-after-save-iCalendar-file-hook nil
  23801. "Hook run after an iCalendar file has been saved.
  23802. The iCalendar buffer is still current when this hook is run.
  23803. A good way to use this is to tell a desktop calenndar application to re-read
  23804. the iCalendar file.")
  23805. (defun org-print-icalendar-entries (&optional combine)
  23806. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  23807. When COMBINE is non nil, add the category to each line."
  23808. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  23809. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  23810. (dts (org-ical-ts-to-string
  23811. (format-time-string (cdr org-time-stamp-formats) (current-time))
  23812. "DTSTART"))
  23813. hd ts ts2 state status (inc t) pos b sexp rrule
  23814. scheduledp deadlinep tmp pri category entry location summary desc
  23815. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  23816. (org-refresh-category-properties)
  23817. (save-excursion
  23818. (goto-char (point-min))
  23819. (while (re-search-forward re1 nil t)
  23820. (catch :skip
  23821. (org-agenda-skip)
  23822. (setq pos (match-beginning 0)
  23823. ts (match-string 0)
  23824. inc t
  23825. hd (org-get-heading)
  23826. summary (org-icalendar-cleanup-string
  23827. (org-entry-get nil "SUMMARY"))
  23828. desc (org-icalendar-cleanup-string
  23829. (or (org-entry-get nil "DESCRIPTION")
  23830. (and org-icalendar-include-body (org-get-entry)))
  23831. t org-icalendar-include-body)
  23832. location (org-icalendar-cleanup-string
  23833. (org-entry-get nil "LOCATION"))
  23834. category (org-get-category))
  23835. (if (looking-at re2)
  23836. (progn
  23837. (goto-char (match-end 0))
  23838. (setq ts2 (match-string 1) inc nil))
  23839. (setq tmp (buffer-substring (max (point-min)
  23840. (- pos org-ds-keyword-length))
  23841. pos)
  23842. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  23843. (progn
  23844. (setq inc nil)
  23845. (replace-match "\\1" t nil ts))
  23846. ts)
  23847. deadlinep (string-match org-deadline-regexp tmp)
  23848. scheduledp (string-match org-scheduled-regexp tmp)
  23849. ;; donep (org-entry-is-done-p)
  23850. ))
  23851. (if (or (string-match org-tr-regexp hd)
  23852. (string-match org-ts-regexp hd))
  23853. (setq hd (replace-match "" t t hd)))
  23854. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  23855. (setq rrule
  23856. (concat "\nRRULE:FREQ="
  23857. (cdr (assoc
  23858. (match-string 2 ts)
  23859. '(("d" . "DAILY")("w" . "WEEKLY")
  23860. ("m" . "MONTHLY")("y" . "YEARLY"))))
  23861. ";INTERVAL=" (match-string 1 ts)))
  23862. (setq rrule ""))
  23863. (setq summary (or summary hd))
  23864. (if (string-match org-bracket-link-regexp summary)
  23865. (setq summary
  23866. (replace-match (if (match-end 3)
  23867. (match-string 3 summary)
  23868. (match-string 1 summary))
  23869. t t summary)))
  23870. (if deadlinep (setq summary (concat "DL: " summary)))
  23871. (if scheduledp (setq summary (concat "S: " summary)))
  23872. (if (string-match "\\`<%%" ts)
  23873. (with-current-buffer sexp-buffer
  23874. (insert (substring ts 1 -1) " " summary "\n"))
  23875. (princ (format "BEGIN:VEVENT
  23876. %s
  23877. %s%s
  23878. SUMMARY:%s%s%s
  23879. CATEGORIES:%s
  23880. END:VEVENT\n"
  23881. (org-ical-ts-to-string ts "DTSTART")
  23882. (org-ical-ts-to-string ts2 "DTEND" inc)
  23883. rrule summary
  23884. (if (and desc (string-match "\\S-" desc))
  23885. (concat "\nDESCRIPTION: " desc) "")
  23886. (if (and location (string-match "\\S-" location))
  23887. (concat "\nLOCATION: " location) "")
  23888. category)))))
  23889. (when (and org-icalendar-include-sexps
  23890. (condition-case nil (require 'icalendar) (error nil))
  23891. (fboundp 'icalendar-export-region))
  23892. ;; Get all the literal sexps
  23893. (goto-char (point-min))
  23894. (while (re-search-forward "^&?%%(" nil t)
  23895. (catch :skip
  23896. (org-agenda-skip)
  23897. (setq b (match-beginning 0))
  23898. (goto-char (1- (match-end 0)))
  23899. (forward-sexp 1)
  23900. (end-of-line 1)
  23901. (setq sexp (buffer-substring b (point)))
  23902. (with-current-buffer sexp-buffer
  23903. (insert sexp "\n"))
  23904. (princ (org-diary-to-ical-string sexp-buffer)))))
  23905. (when org-icalendar-include-todo
  23906. (goto-char (point-min))
  23907. (while (re-search-forward org-todo-line-regexp nil t)
  23908. (catch :skip
  23909. (org-agenda-skip)
  23910. (setq state (match-string 2))
  23911. (setq status (if (member state org-done-keywords)
  23912. "COMPLETED" "NEEDS-ACTION"))
  23913. (when (and state
  23914. (or (not (member state org-done-keywords))
  23915. (eq org-icalendar-include-todo 'all))
  23916. (not (member org-archive-tag (org-get-tags-at)))
  23917. )
  23918. (setq hd (match-string 3)
  23919. summary (org-icalendar-cleanup-string
  23920. (org-entry-get nil "SUMMARY"))
  23921. desc (org-icalendar-cleanup-string
  23922. (or (org-entry-get nil "DESCRIPTION")
  23923. (and org-icalendar-include-body (org-get-entry)))
  23924. t org-icalendar-include-body)
  23925. location (org-icalendar-cleanup-string
  23926. (org-entry-get nil "LOCATION")))
  23927. (if (string-match org-bracket-link-regexp hd)
  23928. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  23929. (match-string 1 hd))
  23930. t t hd)))
  23931. (if (string-match org-priority-regexp hd)
  23932. (setq pri (string-to-char (match-string 2 hd))
  23933. hd (concat (substring hd 0 (match-beginning 1))
  23934. (substring hd (match-end 1))))
  23935. (setq pri org-default-priority))
  23936. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  23937. (- org-lowest-priority org-highest-priority))))))
  23938. (princ (format "BEGIN:VTODO
  23939. %s
  23940. SUMMARY:%s%s%s
  23941. CATEGORIES:%s
  23942. SEQUENCE:1
  23943. PRIORITY:%d
  23944. STATUS:%s
  23945. END:VTODO\n"
  23946. dts
  23947. (or summary hd)
  23948. (if (and location (string-match "\\S-" location))
  23949. (concat "\nLOCATION: " location) "")
  23950. (if (and desc (string-match "\\S-" desc))
  23951. (concat "\nDESCRIPTION: " desc) "")
  23952. category pri status)))))))))
  23953. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  23954. "Take out stuff and quote what needs to be quoted.
  23955. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  23956. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  23957. characters."
  23958. (if (not s)
  23959. nil
  23960. (when is-body
  23961. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  23962. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  23963. (while (string-match re s) (setq s (replace-match "" t t s)))
  23964. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  23965. (let ((start 0))
  23966. (while (string-match "\\([,;\\]\\)" s start)
  23967. (setq start (+ (match-beginning 0) 2)
  23968. s (replace-match "\\\\\\1" nil nil s))))
  23969. (when is-body
  23970. (while (string-match "[ \t]*\n[ \t]*" s)
  23971. (setq s (replace-match "\\n" t t s))))
  23972. (setq s (org-trim s))
  23973. (if is-body
  23974. (if maxlength
  23975. (if (and (numberp maxlength)
  23976. (> (length s) maxlength))
  23977. (setq s (substring s 0 maxlength)))))
  23978. s))
  23979. (defun org-get-entry ()
  23980. "Clean-up description string."
  23981. (save-excursion
  23982. (org-back-to-heading t)
  23983. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  23984. (defun org-start-icalendar-file (name)
  23985. "Start an iCalendar file by inserting the header."
  23986. (let ((user user-full-name)
  23987. (name (or name "unknown"))
  23988. (timezone (cadr (current-time-zone))))
  23989. (princ
  23990. (format "BEGIN:VCALENDAR
  23991. VERSION:2.0
  23992. X-WR-CALNAME:%s
  23993. PRODID:-//%s//Emacs with Org-mode//EN
  23994. X-WR-TIMEZONE:%s
  23995. CALSCALE:GREGORIAN\n" name user timezone))))
  23996. (defun org-finish-icalendar-file ()
  23997. "Finish an iCalendar file by inserting the END statement."
  23998. (princ "END:VCALENDAR\n"))
  23999. (defun org-ical-ts-to-string (s keyword &optional inc)
  24000. "Take a time string S and convert it to iCalendar format.
  24001. KEYWORD is added in front, to make a complete line like DTSTART....
  24002. When INC is non-nil, increase the hour by two (if time string contains
  24003. a time), or the day by one (if it does not contain a time)."
  24004. (let ((t1 (org-parse-time-string s 'nodefault))
  24005. t2 fmt have-time time)
  24006. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24007. (setq t2 t1 have-time t)
  24008. (setq t2 (org-parse-time-string s)))
  24009. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24010. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24011. (when inc
  24012. (if have-time
  24013. (if org-agenda-default-appointment-duration
  24014. (setq mi (+ org-agenda-default-appointment-duration mi))
  24015. (setq h (+ 2 h)))
  24016. (setq d (1+ d))))
  24017. (setq time (encode-time s mi h d m y)))
  24018. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24019. (concat keyword (format-time-string fmt time))))
  24020. ;;; XOXO export
  24021. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24022. (with-current-buffer buffer
  24023. (apply 'insert output)))
  24024. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24025. (defun org-export-as-xoxo (&optional buffer)
  24026. "Export the org buffer as XOXO.
  24027. The XOXO buffer is named *xoxo-<source buffer name>*"
  24028. (interactive (list (current-buffer)))
  24029. ;; A quickie abstraction
  24030. ;; Output everything as XOXO
  24031. (with-current-buffer (get-buffer buffer)
  24032. (let* ((pos (point))
  24033. (opt-plist (org-combine-plists (org-default-export-plist)
  24034. (org-infile-export-plist)))
  24035. (filename (concat (file-name-as-directory
  24036. (org-export-directory :xoxo opt-plist))
  24037. (file-name-sans-extension
  24038. (file-name-nondirectory buffer-file-name))
  24039. ".html"))
  24040. (out (find-file-noselect filename))
  24041. (last-level 1)
  24042. (hanging-li nil))
  24043. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24044. ;; Check the output buffer is empty.
  24045. (with-current-buffer out (erase-buffer))
  24046. ;; Kick off the output
  24047. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24048. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24049. (let* ((hd (match-string-no-properties 1))
  24050. (level (length hd))
  24051. (text (concat
  24052. (match-string-no-properties 2)
  24053. (save-excursion
  24054. (goto-char (match-end 0))
  24055. (let ((str ""))
  24056. (catch 'loop
  24057. (while 't
  24058. (forward-line)
  24059. (if (looking-at "^[ \t]\\(.*\\)")
  24060. (setq str (concat str (match-string-no-properties 1)))
  24061. (throw 'loop str)))))))))
  24062. ;; Handle level rendering
  24063. (cond
  24064. ((> level last-level)
  24065. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24066. ((< level last-level)
  24067. (dotimes (- (- last-level level) 1)
  24068. (if hanging-li
  24069. (org-export-as-xoxo-insert-into out "</li>\n"))
  24070. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24071. (when hanging-li
  24072. (org-export-as-xoxo-insert-into out "</li>\n")
  24073. (setq hanging-li nil)))
  24074. ((equal level last-level)
  24075. (if hanging-li
  24076. (org-export-as-xoxo-insert-into out "</li>\n")))
  24077. )
  24078. (setq last-level level)
  24079. ;; And output the new li
  24080. (setq hanging-li 't)
  24081. (if (equal ?+ (elt text 0))
  24082. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24083. (org-export-as-xoxo-insert-into out "<li>" text))))
  24084. ;; Finally finish off the ol
  24085. (dotimes (- last-level 1)
  24086. (if hanging-li
  24087. (org-export-as-xoxo-insert-into out "</li>\n"))
  24088. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24089. (goto-char pos)
  24090. ;; Finish the buffer off and clean it up.
  24091. (switch-to-buffer-other-window out)
  24092. (indent-region (point-min) (point-max) nil)
  24093. (save-buffer)
  24094. (goto-char (point-min))
  24095. )))
  24096. ;;;; Key bindings
  24097. ;; Make `C-c C-x' a prefix key
  24098. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24099. ;; TAB key with modifiers
  24100. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24101. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24102. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24103. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24104. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24105. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24106. ;; The following line is necessary under Suse GNU/Linux
  24107. (unless (featurep 'xemacs)
  24108. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24109. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24110. (define-key org-mode-map [backtab] 'org-shifttab)
  24111. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24112. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24113. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24114. ;; Cursor keys with modifiers
  24115. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24116. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24117. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24118. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24119. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24120. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24121. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24122. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24123. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24124. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24125. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24126. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24127. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24128. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24129. ;;; Extra keys for tty access.
  24130. ;; We only set them when really needed because otherwise the
  24131. ;; menus don't show the simple keys
  24132. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24133. (not window-system))
  24134. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24135. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24136. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24137. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24138. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24139. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24140. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24141. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24142. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24143. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24144. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24145. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24146. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24147. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24148. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24149. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24150. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24151. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24152. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24153. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24154. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24155. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24156. ;; All the other keys
  24157. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24158. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24159. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24160. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24161. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24162. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24163. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24164. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24165. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24166. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24167. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24168. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24169. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24170. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24171. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24172. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24173. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24174. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24175. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24176. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24177. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24178. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24179. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24180. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24181. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24182. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24183. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24184. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24185. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24186. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24187. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24188. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24189. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24190. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24191. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24192. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24193. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24194. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24195. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24196. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24197. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24198. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24199. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24200. (org-defkey org-mode-map "\C-m" 'org-return)
  24201. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24202. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24203. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24204. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24205. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24206. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24207. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24208. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24209. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  24210. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24211. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24212. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24213. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24214. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24215. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24216. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24217. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24218. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24219. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24220. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24221. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24222. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24223. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24224. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24225. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24226. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24227. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24228. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24229. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24230. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24231. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24232. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24233. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24234. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24235. (when (featurep 'xemacs)
  24236. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24237. (defsubst org-table-p () (org-at-table-p))
  24238. (defun org-self-insert-command (N)
  24239. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24240. If the cursor is in a table looking at whitespace, the whitespace is
  24241. overwritten, and the table is not marked as requiring realignment."
  24242. (interactive "p")
  24243. (if (and (org-table-p)
  24244. (progn
  24245. ;; check if we blank the field, and if that triggers align
  24246. (and org-table-auto-blank-field
  24247. (member last-command
  24248. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24249. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24250. ;; got extra space, this field does not determine column width
  24251. (let (org-table-may-need-update) (org-table-blank-field))
  24252. ;; no extra space, this field may determine column width
  24253. (org-table-blank-field)))
  24254. t)
  24255. (eq N 1)
  24256. (looking-at "[^|\n]* |"))
  24257. (let (org-table-may-need-update)
  24258. (goto-char (1- (match-end 0)))
  24259. (delete-backward-char 1)
  24260. (goto-char (match-beginning 0))
  24261. (self-insert-command N))
  24262. (setq org-table-may-need-update t)
  24263. (self-insert-command N)
  24264. (org-fix-tags-on-the-fly)))
  24265. (defun org-fix-tags-on-the-fly ()
  24266. (when (and (equal (char-after (point-at-bol)) ?*)
  24267. (org-on-heading-p))
  24268. (org-align-tags-here org-tags-column)))
  24269. (defun org-delete-backward-char (N)
  24270. "Like `delete-backward-char', insert whitespace at field end in tables.
  24271. When deleting backwards, in tables this function will insert whitespace in
  24272. front of the next \"|\" separator, to keep the table aligned. The table will
  24273. still be marked for re-alignment if the field did fill the entire column,
  24274. because, in this case the deletion might narrow the column."
  24275. (interactive "p")
  24276. (if (and (org-table-p)
  24277. (eq N 1)
  24278. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24279. (looking-at ".*?|"))
  24280. (let ((pos (point))
  24281. (noalign (looking-at "[^|\n\r]* |"))
  24282. (c org-table-may-need-update))
  24283. (backward-delete-char N)
  24284. (skip-chars-forward "^|")
  24285. (insert " ")
  24286. (goto-char (1- pos))
  24287. ;; noalign: if there were two spaces at the end, this field
  24288. ;; does not determine the width of the column.
  24289. (if noalign (setq org-table-may-need-update c)))
  24290. (backward-delete-char N)
  24291. (org-fix-tags-on-the-fly)))
  24292. (defun org-delete-char (N)
  24293. "Like `delete-char', but insert whitespace at field end in tables.
  24294. When deleting characters, in tables this function will insert whitespace in
  24295. front of the next \"|\" separator, to keep the table aligned. The table will
  24296. still be marked for re-alignment if the field did fill the entire column,
  24297. because, in this case the deletion might narrow the column."
  24298. (interactive "p")
  24299. (if (and (org-table-p)
  24300. (not (bolp))
  24301. (not (= (char-after) ?|))
  24302. (eq N 1))
  24303. (if (looking-at ".*?|")
  24304. (let ((pos (point))
  24305. (noalign (looking-at "[^|\n\r]* |"))
  24306. (c org-table-may-need-update))
  24307. (replace-match (concat
  24308. (substring (match-string 0) 1 -1)
  24309. " |"))
  24310. (goto-char pos)
  24311. ;; noalign: if there were two spaces at the end, this field
  24312. ;; does not determine the width of the column.
  24313. (if noalign (setq org-table-may-need-update c)))
  24314. (delete-char N))
  24315. (delete-char N)
  24316. (org-fix-tags-on-the-fly)))
  24317. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  24318. (put 'org-self-insert-command 'delete-selection t)
  24319. (put 'orgtbl-self-insert-command 'delete-selection t)
  24320. (put 'org-delete-char 'delete-selection 'supersede)
  24321. (put 'org-delete-backward-char 'delete-selection 'supersede)
  24322. ;; Make `flyspell-mode' delay after some commands
  24323. (put 'org-self-insert-command 'flyspell-delayed t)
  24324. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  24325. (put 'org-delete-char 'flyspell-delayed t)
  24326. (put 'org-delete-backward-char 'flyspell-delayed t)
  24327. ;; Make pabbrev-mode expand after org-mode commands
  24328. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  24329. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  24330. ;; How to do this: Measure non-white length of current string
  24331. ;; If equal to column width, we should realign.
  24332. (defun org-remap (map &rest commands)
  24333. "In MAP, remap the functions given in COMMANDS.
  24334. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  24335. (let (new old)
  24336. (while commands
  24337. (setq old (pop commands) new (pop commands))
  24338. (if (fboundp 'command-remapping)
  24339. (org-defkey map (vector 'remap old) new)
  24340. (substitute-key-definition old new map global-map)))))
  24341. (when (eq org-enable-table-editor 'optimized)
  24342. ;; If the user wants maximum table support, we need to hijack
  24343. ;; some standard editing functions
  24344. (org-remap org-mode-map
  24345. 'self-insert-command 'org-self-insert-command
  24346. 'delete-char 'org-delete-char
  24347. 'delete-backward-char 'org-delete-backward-char)
  24348. (org-defkey org-mode-map "|" 'org-force-self-insert))
  24349. (defun org-shiftcursor-error ()
  24350. "Throw an error because Shift-Cursor command was applied in wrong context."
  24351. (error "This command is active in special context like tables, headlines or timestamps"))
  24352. (defun org-shifttab (&optional arg)
  24353. "Global visibility cycling or move to previous table field.
  24354. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  24355. on context.
  24356. See the individual commands for more information."
  24357. (interactive "P")
  24358. (cond
  24359. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  24360. (arg (message "Content view to level: ")
  24361. (org-content (prefix-numeric-value arg))
  24362. (setq org-cycle-global-status 'overview))
  24363. (t (call-interactively 'org-global-cycle))))
  24364. (defun org-shiftmetaleft ()
  24365. "Promote subtree or delete table column.
  24366. Calls `org-promote-subtree', `org-outdent-item',
  24367. or `org-table-delete-column', depending on context.
  24368. See the individual commands for more information."
  24369. (interactive)
  24370. (cond
  24371. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  24372. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  24373. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24374. (t (org-shiftcursor-error))))
  24375. (defun org-shiftmetaright ()
  24376. "Demote subtree or insert table column.
  24377. Calls `org-demote-subtree', `org-indent-item',
  24378. or `org-table-insert-column', depending on context.
  24379. See the individual commands for more information."
  24380. (interactive)
  24381. (cond
  24382. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  24383. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  24384. ((org-at-item-p) (call-interactively 'org-indent-item))
  24385. (t (org-shiftcursor-error))))
  24386. (defun org-shiftmetaup (&optional arg)
  24387. "Move subtree up or kill table row.
  24388. Calls `org-move-subtree-up' or `org-table-kill-row' or
  24389. `org-move-item-up' depending on context. See the individual commands
  24390. for more information."
  24391. (interactive "P")
  24392. (cond
  24393. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  24394. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24395. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24396. (t (org-shiftcursor-error))))
  24397. (defun org-shiftmetadown (&optional arg)
  24398. "Move subtree down or insert table row.
  24399. Calls `org-move-subtree-down' or `org-table-insert-row' or
  24400. `org-move-item-down', depending on context. See the individual
  24401. commands for more information."
  24402. (interactive "P")
  24403. (cond
  24404. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  24405. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24406. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24407. (t (org-shiftcursor-error))))
  24408. (defun org-metaleft (&optional arg)
  24409. "Promote heading or move table column to left.
  24410. Calls `org-do-promote' or `org-table-move-column', depending on context.
  24411. With no specific context, calls the Emacs default `backward-word'.
  24412. See the individual commands for more information."
  24413. (interactive "P")
  24414. (cond
  24415. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  24416. ((or (org-on-heading-p) (org-region-active-p))
  24417. (call-interactively 'org-do-promote))
  24418. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24419. (t (call-interactively 'backward-word))))
  24420. (defun org-metaright (&optional arg)
  24421. "Demote subtree or move table column to right.
  24422. Calls `org-do-demote' or `org-table-move-column', depending on context.
  24423. With no specific context, calls the Emacs default `forward-word'.
  24424. See the individual commands for more information."
  24425. (interactive "P")
  24426. (cond
  24427. ((org-at-table-p) (call-interactively 'org-table-move-column))
  24428. ((or (org-on-heading-p) (org-region-active-p))
  24429. (call-interactively 'org-do-demote))
  24430. ((org-at-item-p) (call-interactively 'org-indent-item))
  24431. (t (call-interactively 'forward-word))))
  24432. (defun org-metaup (&optional arg)
  24433. "Move subtree up or move table row up.
  24434. Calls `org-move-subtree-up' or `org-table-move-row' or
  24435. `org-move-item-up', depending on context. See the individual commands
  24436. for more information."
  24437. (interactive "P")
  24438. (cond
  24439. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  24440. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24441. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24442. (t (transpose-lines 1) (beginning-of-line -1))))
  24443. (defun org-metadown (&optional arg)
  24444. "Move subtree down or move table row down.
  24445. Calls `org-move-subtree-down' or `org-table-move-row' or
  24446. `org-move-item-down', depending on context. See the individual
  24447. commands for more information."
  24448. (interactive "P")
  24449. (cond
  24450. ((org-at-table-p) (call-interactively 'org-table-move-row))
  24451. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24452. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24453. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  24454. (defun org-shiftup (&optional arg)
  24455. "Increase item in timestamp or increase priority of current headline.
  24456. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  24457. depending on context. See the individual commands for more information."
  24458. (interactive "P")
  24459. (cond
  24460. ((org-at-timestamp-p t)
  24461. (call-interactively (if org-edit-timestamp-down-means-later
  24462. 'org-timestamp-down 'org-timestamp-up)))
  24463. ((org-on-heading-p) (call-interactively 'org-priority-up))
  24464. ((org-at-item-p) (call-interactively 'org-previous-item))
  24465. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  24466. (defun org-shiftdown (&optional arg)
  24467. "Decrease item in timestamp or decrease priority of current headline.
  24468. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  24469. depending on context. See the individual commands for more information."
  24470. (interactive "P")
  24471. (cond
  24472. ((org-at-timestamp-p t)
  24473. (call-interactively (if org-edit-timestamp-down-means-later
  24474. 'org-timestamp-up 'org-timestamp-down)))
  24475. ((org-on-heading-p) (call-interactively 'org-priority-down))
  24476. (t (call-interactively 'org-next-item))))
  24477. (defun org-shiftright ()
  24478. "Next TODO keyword or timestamp one day later, depending on context."
  24479. (interactive)
  24480. (cond
  24481. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  24482. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  24483. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  24484. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  24485. (t (org-shiftcursor-error))))
  24486. (defun org-shiftleft ()
  24487. "Previous TODO keyword or timestamp one day earlier, depending on context."
  24488. (interactive)
  24489. (cond
  24490. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  24491. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  24492. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  24493. ((org-at-property-p)
  24494. (call-interactively 'org-property-previous-allowed-value))
  24495. (t (org-shiftcursor-error))))
  24496. (defun org-shiftcontrolright ()
  24497. "Switch to next TODO set."
  24498. (interactive)
  24499. (cond
  24500. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  24501. (t (org-shiftcursor-error))))
  24502. (defun org-shiftcontrolleft ()
  24503. "Switch to previous TODO set."
  24504. (interactive)
  24505. (cond
  24506. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  24507. (t (org-shiftcursor-error))))
  24508. (defun org-ctrl-c-ret ()
  24509. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  24510. (interactive)
  24511. (cond
  24512. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  24513. (t (call-interactively 'org-insert-heading))))
  24514. (defun org-copy-special ()
  24515. "Copy region in table or copy current subtree.
  24516. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  24517. See the individual commands for more information."
  24518. (interactive)
  24519. (call-interactively
  24520. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  24521. (defun org-cut-special ()
  24522. "Cut region in table or cut current subtree.
  24523. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  24524. See the individual commands for more information."
  24525. (interactive)
  24526. (call-interactively
  24527. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  24528. (defun org-paste-special (arg)
  24529. "Paste rectangular region into table, or past subtree relative to level.
  24530. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  24531. See the individual commands for more information."
  24532. (interactive "P")
  24533. (if (org-at-table-p)
  24534. (org-table-paste-rectangle)
  24535. (org-paste-subtree arg)))
  24536. (defun org-ctrl-c-ctrl-c (&optional arg)
  24537. "Set tags in headline, or update according to changed information at point.
  24538. This command does many different things, depending on context:
  24539. - If the cursor is in a headline, prompt for tags and insert them
  24540. into the current line, aligned to `org-tags-column'. When called
  24541. with prefix arg, realign all tags in the current buffer.
  24542. - If the cursor is in one of the special #+KEYWORD lines, this
  24543. triggers scanning the buffer for these lines and updating the
  24544. information.
  24545. - If the cursor is inside a table, realign the table. This command
  24546. works even if the automatic table editor has been turned off.
  24547. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  24548. the entire table.
  24549. - If the cursor is a the beginning of a dynamic block, update it.
  24550. - If the cursor is inside a table created by the table.el package,
  24551. activate that table.
  24552. - If the current buffer is a remember buffer, close note and file it.
  24553. with a prefix argument, file it without further interaction to the default
  24554. location.
  24555. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  24556. links in this buffer.
  24557. - If the cursor is on a numbered item in a plain list, renumber the
  24558. ordered list.
  24559. - If the cursor is on a checkbox, toggle it."
  24560. (interactive "P")
  24561. (let ((org-enable-table-editor t))
  24562. (cond
  24563. ((or org-clock-overlays
  24564. org-occur-highlights
  24565. org-latex-fragment-image-overlays)
  24566. (org-remove-clock-overlays)
  24567. (org-remove-occur-highlights)
  24568. (org-remove-latex-fragment-image-overlays)
  24569. (message "Temporary highlights/overlays removed from current buffer"))
  24570. ((and (local-variable-p 'org-finish-function (current-buffer))
  24571. (fboundp org-finish-function))
  24572. (funcall org-finish-function))
  24573. ((org-at-property-p)
  24574. (call-interactively 'org-property-action))
  24575. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  24576. ((org-on-heading-p) (call-interactively 'org-set-tags))
  24577. ((org-at-table.el-p)
  24578. (require 'table)
  24579. (beginning-of-line 1)
  24580. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  24581. (call-interactively 'table-recognize-table))
  24582. ((org-at-table-p)
  24583. (org-table-maybe-eval-formula)
  24584. (if arg
  24585. (call-interactively 'org-table-recalculate)
  24586. (org-table-maybe-recalculate-line))
  24587. (call-interactively 'org-table-align))
  24588. ((org-at-item-checkbox-p)
  24589. (call-interactively 'org-toggle-checkbox))
  24590. ((org-at-item-p)
  24591. (call-interactively 'org-maybe-renumber-ordered-list))
  24592. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  24593. ;; Dynamic block
  24594. (beginning-of-line 1)
  24595. (org-update-dblock))
  24596. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  24597. (cond
  24598. ((equal (match-string 1) "TBLFM")
  24599. ;; Recalculate the table before this line
  24600. (save-excursion
  24601. (beginning-of-line 1)
  24602. (skip-chars-backward " \r\n\t")
  24603. (if (org-at-table-p)
  24604. (org-call-with-arg 'org-table-recalculate t))))
  24605. (t
  24606. (call-interactively 'org-mode-restart))))
  24607. (t (error "C-c C-c can do nothing useful at this location.")))))
  24608. (defun org-mode-restart ()
  24609. "Restart Org-mode, to scan again for special lines.
  24610. Also updates the keyword regular expressions."
  24611. (interactive)
  24612. (let ((org-inhibit-startup t)) (org-mode))
  24613. (message "Org-mode restarted to refresh keyword and special line setup"))
  24614. (defun org-kill-note-or-show-branches ()
  24615. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  24616. (interactive)
  24617. (if (not org-finish-function)
  24618. (call-interactively 'show-branches)
  24619. (let ((org-note-abort t))
  24620. (funcall org-finish-function))))
  24621. (defun org-return (&optional indent)
  24622. "Goto next table row or insert a newline.
  24623. Calls `org-table-next-row' or `newline', depending on context.
  24624. See the individual commands for more information."
  24625. (interactive)
  24626. (cond
  24627. ((bobp) (if indent (newline-and-indent) (newline)))
  24628. ((org-at-table-p)
  24629. (org-table-justify-field-maybe)
  24630. (call-interactively 'org-table-next-row))
  24631. (t (if indent (newline-and-indent) (newline)))))
  24632. (defun org-return-indent ()
  24633. (interactive)
  24634. "Goto next table row or insert a newline and indent.
  24635. Calls `org-table-next-row' or `newline-and-indent', depending on
  24636. context. See the individual commands for more information."
  24637. (org-return t))
  24638. (defun org-ctrl-c-minus ()
  24639. "Insert separator line in table or modify bullet type in list.
  24640. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  24641. depending on context."
  24642. (interactive)
  24643. (cond
  24644. ((org-at-table-p)
  24645. (call-interactively 'org-table-insert-hline))
  24646. ((org-on-heading-p)
  24647. ;; Convert to item
  24648. (save-excursion
  24649. (beginning-of-line 1)
  24650. (if (looking-at "\\*+ ")
  24651. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  24652. ((org-in-item-p)
  24653. (call-interactively 'org-cycle-list-bullet))
  24654. (t (error "`C-c -' does have no function here."))))
  24655. (defun org-meta-return (&optional arg)
  24656. "Insert a new heading or wrap a region in a table.
  24657. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  24658. See the individual commands for more information."
  24659. (interactive "P")
  24660. (cond
  24661. ((org-at-table-p)
  24662. (call-interactively 'org-table-wrap-region))
  24663. (t (call-interactively 'org-insert-heading))))
  24664. ;;; Menu entries
  24665. ;; Define the Org-mode menus
  24666. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  24667. '("Tbl"
  24668. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  24669. ["Next Field" org-cycle (org-at-table-p)]
  24670. ["Previous Field" org-shifttab (org-at-table-p)]
  24671. ["Next Row" org-return (org-at-table-p)]
  24672. "--"
  24673. ["Blank Field" org-table-blank-field (org-at-table-p)]
  24674. ["Edit Field" org-table-edit-field (org-at-table-p)]
  24675. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  24676. "--"
  24677. ("Column"
  24678. ["Move Column Left" org-metaleft (org-at-table-p)]
  24679. ["Move Column Right" org-metaright (org-at-table-p)]
  24680. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  24681. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  24682. ("Row"
  24683. ["Move Row Up" org-metaup (org-at-table-p)]
  24684. ["Move Row Down" org-metadown (org-at-table-p)]
  24685. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  24686. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  24687. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  24688. "--"
  24689. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  24690. ("Rectangle"
  24691. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  24692. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  24693. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  24694. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  24695. "--"
  24696. ("Calculate"
  24697. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  24698. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  24699. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  24700. "--"
  24701. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  24702. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  24703. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  24704. "--"
  24705. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  24706. "--"
  24707. ["Sum Column/Rectangle" org-table-sum
  24708. (or (org-at-table-p) (org-region-active-p))]
  24709. ["Which Column?" org-table-current-column (org-at-table-p)])
  24710. ["Debug Formulas"
  24711. org-table-toggle-formula-debugger
  24712. :style toggle :selected org-table-formula-debug]
  24713. ["Show Col/Row Numbers"
  24714. org-table-toggle-coordinate-overlays
  24715. :style toggle :selected org-table-overlay-coordinates]
  24716. "--"
  24717. ["Create" org-table-create (and (not (org-at-table-p))
  24718. org-enable-table-editor)]
  24719. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  24720. ["Import from File" org-table-import (not (org-at-table-p))]
  24721. ["Export to File" org-table-export (org-at-table-p)]
  24722. "--"
  24723. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  24724. (easy-menu-define org-org-menu org-mode-map "Org menu"
  24725. '("Org"
  24726. ("Show/Hide"
  24727. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  24728. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  24729. ["Sparse Tree" org-occur t]
  24730. ["Reveal Context" org-reveal t]
  24731. ["Show All" show-all t]
  24732. "--"
  24733. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  24734. "--"
  24735. ["New Heading" org-insert-heading t]
  24736. ("Navigate Headings"
  24737. ["Up" outline-up-heading t]
  24738. ["Next" outline-next-visible-heading t]
  24739. ["Previous" outline-previous-visible-heading t]
  24740. ["Next Same Level" outline-forward-same-level t]
  24741. ["Previous Same Level" outline-backward-same-level t]
  24742. "--"
  24743. ["Jump" org-goto t])
  24744. ("Edit Structure"
  24745. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  24746. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  24747. "--"
  24748. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  24749. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  24750. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  24751. "--"
  24752. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  24753. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  24754. ["Demote Heading" org-metaright (not (org-at-table-p))]
  24755. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  24756. "--"
  24757. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  24758. "--"
  24759. ["Convert to odd levels" org-convert-to-odd-levels t]
  24760. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  24761. ("Editing"
  24762. ["Emphasis..." org-emphasize t])
  24763. ("Archive"
  24764. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  24765. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  24766. ; :active t :keys "C-u C-c C-x C-a"]
  24767. ["Sparse trees open ARCHIVE trees"
  24768. (setq org-sparse-tree-open-archived-trees
  24769. (not org-sparse-tree-open-archived-trees))
  24770. :style toggle :selected org-sparse-tree-open-archived-trees]
  24771. ["Cycling opens ARCHIVE trees"
  24772. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  24773. :style toggle :selected org-cycle-open-archived-trees]
  24774. ["Agenda includes ARCHIVE trees"
  24775. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  24776. :style toggle :selected (not org-agenda-skip-archived-trees)]
  24777. "--"
  24778. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  24779. ; ["Check and Move Children" (org-archive-subtree '(4))
  24780. ; :active t :keys "C-u C-c C-x C-s"]
  24781. )
  24782. "--"
  24783. ("TODO Lists"
  24784. ["TODO/DONE/-" org-todo t]
  24785. ("Select keyword"
  24786. ["Next keyword" org-shiftright (org-on-heading-p)]
  24787. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  24788. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  24789. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  24790. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  24791. ["Show TODO Tree" org-show-todo-tree t]
  24792. ["Global TODO list" org-todo-list t]
  24793. "--"
  24794. ["Set Priority" org-priority t]
  24795. ["Priority Up" org-shiftup t]
  24796. ["Priority Down" org-shiftdown t])
  24797. ("TAGS and Properties"
  24798. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  24799. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  24800. "--"
  24801. ["Set property" 'org-set-property t]
  24802. ["Column view of properties" org-columns t]
  24803. ["Insert Column View DBlock" org-insert-columns-dblock t])
  24804. ("Dates and Scheduling"
  24805. ["Timestamp" org-time-stamp t]
  24806. ["Timestamp (inactive)" org-time-stamp-inactive t]
  24807. ("Change Date"
  24808. ["1 Day Later" org-shiftright t]
  24809. ["1 Day Earlier" org-shiftleft t]
  24810. ["1 ... Later" org-shiftup t]
  24811. ["1 ... Earlier" org-shiftdown t])
  24812. ["Compute Time Range" org-evaluate-time-range t]
  24813. ["Schedule Item" org-schedule t]
  24814. ["Deadline" org-deadline t]
  24815. "--"
  24816. ["Custom time format" org-toggle-time-stamp-overlays
  24817. :style radio :selected org-display-custom-times]
  24818. "--"
  24819. ["Goto Calendar" org-goto-calendar t]
  24820. ["Date from Calendar" org-date-from-calendar t])
  24821. ("Logging work"
  24822. ["Clock in" org-clock-in t]
  24823. ["Clock out" org-clock-out t]
  24824. ["Clock cancel" org-clock-cancel t]
  24825. ["Goto running clock" org-clock-goto t]
  24826. ["Display times" org-clock-display t]
  24827. ["Create clock table" org-clock-report t]
  24828. "--"
  24829. ["Record DONE time"
  24830. (progn (setq org-log-done (not org-log-done))
  24831. (message "Switching to %s will %s record a timestamp"
  24832. (car org-done-keywords)
  24833. (if org-log-done "automatically" "not")))
  24834. :style toggle :selected org-log-done])
  24835. "--"
  24836. ["Agenda Command..." org-agenda t]
  24837. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  24838. ("File List for Agenda")
  24839. ("Special views current file"
  24840. ["TODO Tree" org-show-todo-tree t]
  24841. ["Check Deadlines" org-check-deadlines t]
  24842. ["Timeline" org-timeline t]
  24843. ["Tags Tree" org-tags-sparse-tree t])
  24844. "--"
  24845. ("Hyperlinks"
  24846. ["Store Link (Global)" org-store-link t]
  24847. ["Insert Link" org-insert-link t]
  24848. ["Follow Link" org-open-at-point t]
  24849. "--"
  24850. ["Next link" org-next-link t]
  24851. ["Previous link" org-previous-link t]
  24852. "--"
  24853. ["Descriptive Links"
  24854. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  24855. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  24856. ["Literal Links"
  24857. (progn
  24858. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  24859. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  24860. "--"
  24861. ["Export/Publish..." org-export t]
  24862. ("LaTeX"
  24863. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  24864. :selected org-cdlatex-mode]
  24865. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  24866. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  24867. ["Modify math symbol" org-cdlatex-math-modify
  24868. (org-inside-LaTeX-fragment-p)]
  24869. ["Export LaTeX fragments as images"
  24870. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  24871. :style toggle :selected org-export-with-LaTeX-fragments])
  24872. "--"
  24873. ("Documentation"
  24874. ["Show Version" org-version t]
  24875. ["Info Documentation" org-info t])
  24876. ("Customize"
  24877. ["Browse Org Group" org-customize t]
  24878. "--"
  24879. ["Expand This Menu" org-create-customize-menu
  24880. (fboundp 'customize-menu-create)])
  24881. "--"
  24882. ["Refresh setup" org-mode-restart t]
  24883. ))
  24884. (defun org-info (&optional node)
  24885. "Read documentation for Org-mode in the info system.
  24886. With optional NODE, go directly to that node."
  24887. (interactive)
  24888. (require 'info)
  24889. (Info-goto-node (format "(org)%s" (or node ""))))
  24890. (defun org-install-agenda-files-menu ()
  24891. (let ((bl (buffer-list)))
  24892. (save-excursion
  24893. (while bl
  24894. (set-buffer (pop bl))
  24895. (if (org-mode-p) (setq bl nil)))
  24896. (when (org-mode-p)
  24897. (easy-menu-change
  24898. '("Org") "File List for Agenda"
  24899. (append
  24900. (list
  24901. ["Edit File List" (org-edit-agenda-file-list) t]
  24902. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  24903. ["Remove Current File from List" org-remove-file t]
  24904. ["Cycle through agenda files" org-cycle-agenda-files t]
  24905. ["Occur in all agenda files" org-occur-in-agenda-files t]
  24906. "--")
  24907. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  24908. ;;;; Documentation
  24909. (defun org-customize ()
  24910. "Call the customize function with org as argument."
  24911. (interactive)
  24912. (customize-browse 'org))
  24913. (defun org-create-customize-menu ()
  24914. "Create a full customization menu for Org-mode, insert it into the menu."
  24915. (interactive)
  24916. (if (fboundp 'customize-menu-create)
  24917. (progn
  24918. (easy-menu-change
  24919. '("Org") "Customize"
  24920. `(["Browse Org group" org-customize t]
  24921. "--"
  24922. ,(customize-menu-create 'org)
  24923. ["Set" Custom-set t]
  24924. ["Save" Custom-save t]
  24925. ["Reset to Current" Custom-reset-current t]
  24926. ["Reset to Saved" Custom-reset-saved t]
  24927. ["Reset to Standard Settings" Custom-reset-standard t]))
  24928. (message "\"Org\"-menu now contains full customization menu"))
  24929. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  24930. ;;;; Miscellaneous stuff
  24931. ;;; Generally useful functions
  24932. (defun org-context ()
  24933. "Return a list of contexts of the current cursor position.
  24934. If several contexts apply, all are returned.
  24935. Each context entry is a list with a symbol naming the context, and
  24936. two positions indicating start and end of the context. Possible
  24937. contexts are:
  24938. :headline anywhere in a headline
  24939. :headline-stars on the leading stars in a headline
  24940. :todo-keyword on a TODO keyword (including DONE) in a headline
  24941. :tags on the TAGS in a headline
  24942. :priority on the priority cookie in a headline
  24943. :item on the first line of a plain list item
  24944. :item-bullet on the bullet/number of a plain list item
  24945. :checkbox on the checkbox in a plain list item
  24946. :table in an org-mode table
  24947. :table-special on a special filed in a table
  24948. :table-table in a table.el table
  24949. :link on a hyperlink
  24950. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  24951. :target on a <<target>>
  24952. :radio-target on a <<<radio-target>>>
  24953. :latex-fragment on a LaTeX fragment
  24954. :latex-preview on a LaTeX fragment with overlayed preview image
  24955. This function expects the position to be visible because it uses font-lock
  24956. faces as a help to recognize the following contexts: :table-special, :link,
  24957. and :keyword."
  24958. (let* ((f (get-text-property (point) 'face))
  24959. (faces (if (listp f) f (list f)))
  24960. (p (point)) clist o)
  24961. ;; First the large context
  24962. (cond
  24963. ((org-on-heading-p t)
  24964. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  24965. (when (progn
  24966. (beginning-of-line 1)
  24967. (looking-at org-todo-line-tags-regexp))
  24968. (push (org-point-in-group p 1 :headline-stars) clist)
  24969. (push (org-point-in-group p 2 :todo-keyword) clist)
  24970. (push (org-point-in-group p 4 :tags) clist))
  24971. (goto-char p)
  24972. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  24973. (if (looking-at "\\[#[A-Z0-9]\\]")
  24974. (push (org-point-in-group p 0 :priority) clist)))
  24975. ((org-at-item-p)
  24976. (push (org-point-in-group p 2 :item-bullet) clist)
  24977. (push (list :item (point-at-bol)
  24978. (save-excursion (org-end-of-item) (point)))
  24979. clist)
  24980. (and (org-at-item-checkbox-p)
  24981. (push (org-point-in-group p 0 :checkbox) clist)))
  24982. ((org-at-table-p)
  24983. (push (list :table (org-table-begin) (org-table-end)) clist)
  24984. (if (memq 'org-formula faces)
  24985. (push (list :table-special
  24986. (previous-single-property-change p 'face)
  24987. (next-single-property-change p 'face)) clist)))
  24988. ((org-at-table-p 'any)
  24989. (push (list :table-table) clist)))
  24990. (goto-char p)
  24991. ;; Now the small context
  24992. (cond
  24993. ((org-at-timestamp-p)
  24994. (push (org-point-in-group p 0 :timestamp) clist))
  24995. ((memq 'org-link faces)
  24996. (push (list :link
  24997. (previous-single-property-change p 'face)
  24998. (next-single-property-change p 'face)) clist))
  24999. ((memq 'org-special-keyword faces)
  25000. (push (list :keyword
  25001. (previous-single-property-change p 'face)
  25002. (next-single-property-change p 'face)) clist))
  25003. ((org-on-target-p)
  25004. (push (org-point-in-group p 0 :target) clist)
  25005. (goto-char (1- (match-beginning 0)))
  25006. (if (looking-at org-radio-target-regexp)
  25007. (push (org-point-in-group p 0 :radio-target) clist))
  25008. (goto-char p))
  25009. ((setq o (car (delq nil
  25010. (mapcar
  25011. (lambda (x)
  25012. (if (memq x org-latex-fragment-image-overlays) x))
  25013. (org-overlays-at (point))))))
  25014. (push (list :latex-fragment
  25015. (org-overlay-start o) (org-overlay-end o)) clist)
  25016. (push (list :latex-preview
  25017. (org-overlay-start o) (org-overlay-end o)) clist))
  25018. ((org-inside-LaTeX-fragment-p)
  25019. ;; FIXME: positions wrong.
  25020. (push (list :latex-fragment (point) (point)) clist)))
  25021. (setq clist (nreverse (delq nil clist)))
  25022. clist))
  25023. ;; FIXME: Compare with at-regexp-p Do we need both?
  25024. (defun org-in-regexp (re &optional nlines visually)
  25025. "Check if point is inside a match of regexp.
  25026. Normally only the current line is checked, but you can include NLINES extra
  25027. lines both before and after point into the search.
  25028. If VISUALLY is set, require that the cursor is not after the match but
  25029. really on, so that the block visually is on the match."
  25030. (catch 'exit
  25031. (let ((pos (point))
  25032. (eol (point-at-eol (+ 1 (or nlines 0))))
  25033. (inc (if visually 1 0)))
  25034. (save-excursion
  25035. (beginning-of-line (- 1 (or nlines 0)))
  25036. (while (re-search-forward re eol t)
  25037. (if (and (<= (match-beginning 0) pos)
  25038. (>= (+ inc (match-end 0)) pos))
  25039. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25040. (defun org-at-regexp-p (regexp)
  25041. "Is point inside a match of REGEXP in the current line?"
  25042. (catch 'exit
  25043. (save-excursion
  25044. (let ((pos (point)) (end (point-at-eol)))
  25045. (beginning-of-line 1)
  25046. (while (re-search-forward regexp end t)
  25047. (if (and (<= (match-beginning 0) pos)
  25048. (>= (match-end 0) pos))
  25049. (throw 'exit t)))
  25050. nil))))
  25051. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25052. "Call `multi-occur' with buffers for all agenda files."
  25053. (interactive "sOrg-files matching: \np")
  25054. (let* ((files (org-agenda-files))
  25055. (tnames (mapcar 'file-truename files))
  25056. (extra org-agenda-multi-occur-extra-files)
  25057. f)
  25058. (while (setq f (pop extra))
  25059. (unless (member (file-truename f) tnames)
  25060. (add-to-list 'files f 'append)
  25061. (add-to-list 'tnames (file-truename f) 'append)))
  25062. (multi-occur
  25063. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25064. regexp)))
  25065. (defun org-uniquify (list)
  25066. "Remove duplicate elements from LIST."
  25067. (let (res)
  25068. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25069. res))
  25070. (defun org-delete-all (elts list)
  25071. "Remove all elements in ELTS from LIST."
  25072. (while elts
  25073. (setq list (delete (pop elts) list)))
  25074. list)
  25075. (defun org-back-over-empty-lines ()
  25076. "Move backwards over witespace, to the beginning of the first empty line.
  25077. Returns the number o empty lines passed."
  25078. (let ((pos (point)))
  25079. (skip-chars-backward " \t\n\r")
  25080. (beginning-of-line 2)
  25081. (count-lines (point) pos)))
  25082. (defun org-skip-whitespace ()
  25083. (skip-chars-forward " \t\n\r"))
  25084. (defun org-point-in-group (point group &optional context)
  25085. "Check if POINT is in match-group GROUP.
  25086. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25087. match. If the match group does ot exist or point is not inside it,
  25088. return nil."
  25089. (and (match-beginning group)
  25090. (>= point (match-beginning group))
  25091. (<= point (match-end group))
  25092. (if context
  25093. (list context (match-beginning group) (match-end group))
  25094. t)))
  25095. (defun org-switch-to-buffer-other-window (&rest args)
  25096. "Switch to buffer in a second window on the current frame.
  25097. In particular, do not allow pop-up frames."
  25098. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25099. special-display-function)
  25100. (apply 'switch-to-buffer-other-window args)))
  25101. (defun org-combine-plists (&rest plists)
  25102. "Create a single property list from all plists in PLISTS.
  25103. The process starts by copying the first list, and then setting properties
  25104. from the other lists. Settings in the last list are the most significant
  25105. ones and overrule settings in the other lists."
  25106. (let ((rtn (copy-sequence (pop plists)))
  25107. p v ls)
  25108. (while plists
  25109. (setq ls (pop plists))
  25110. (while ls
  25111. (setq p (pop ls) v (pop ls))
  25112. (setq rtn (plist-put rtn p v))))
  25113. rtn))
  25114. (defun org-move-line-down (arg)
  25115. "Move the current line down. With prefix argument, move it past ARG lines."
  25116. (interactive "p")
  25117. (let ((col (current-column))
  25118. beg end pos)
  25119. (beginning-of-line 1) (setq beg (point))
  25120. (beginning-of-line 2) (setq end (point))
  25121. (beginning-of-line (+ 1 arg))
  25122. (setq pos (move-marker (make-marker) (point)))
  25123. (insert (delete-and-extract-region beg end))
  25124. (goto-char pos)
  25125. (move-to-column col)))
  25126. (defun org-move-line-up (arg)
  25127. "Move the current line up. With prefix argument, move it past ARG lines."
  25128. (interactive "p")
  25129. (let ((col (current-column))
  25130. beg end pos)
  25131. (beginning-of-line 1) (setq beg (point))
  25132. (beginning-of-line 2) (setq end (point))
  25133. (beginning-of-line (- arg))
  25134. (setq pos (move-marker (make-marker) (point)))
  25135. (insert (delete-and-extract-region beg end))
  25136. (goto-char pos)
  25137. (move-to-column col)))
  25138. (defun org-replace-escapes (string table)
  25139. "Replace %-escapes in STRING with values in TABLE.
  25140. TABLE is an association list with keys like \"%a\" and string values.
  25141. The sequences in STRING may contain normal field width and padding information,
  25142. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25143. so values can contain further %-escapes if they are define later in TABLE."
  25144. (let ((case-fold-search nil)
  25145. e re rpl)
  25146. (while (setq e (pop table))
  25147. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25148. (while (string-match re string)
  25149. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25150. (cdr e)))
  25151. (setq string (replace-match rpl t t string))))
  25152. string))
  25153. (defun org-sublist (list start end)
  25154. "Return a section of LIST, from START to END.
  25155. Counting starts at 1."
  25156. (let (rtn (c start))
  25157. (setq list (nthcdr (1- start) list))
  25158. (while (and list (<= c end))
  25159. (push (pop list) rtn)
  25160. (setq c (1+ c)))
  25161. (nreverse rtn)))
  25162. (defun org-find-base-buffer-visiting (file)
  25163. "Like `find-buffer-visiting' but alway return the base buffer and
  25164. not an indirect buffer"
  25165. (let ((buf (find-buffer-visiting file)))
  25166. (if buf
  25167. (or (buffer-base-buffer buf) buf)
  25168. nil)))
  25169. (defun org-image-file-name-regexp ()
  25170. "Return regexp matching the file names of images."
  25171. (if (fboundp 'image-file-name-regexp)
  25172. (image-file-name-regexp)
  25173. (let ((image-file-name-extensions
  25174. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25175. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25176. (concat "\\."
  25177. (regexp-opt (nconc (mapcar 'upcase
  25178. image-file-name-extensions)
  25179. image-file-name-extensions)
  25180. t)
  25181. "\\'"))))
  25182. (defun org-file-image-p (file)
  25183. "Return non-nil if FILE is an image."
  25184. (save-match-data
  25185. (string-match (org-image-file-name-regexp) file)))
  25186. ;;; Paragraph filling stuff.
  25187. ;; We want this to be just right, so use the full arsenal.
  25188. (defun org-indent-line-function ()
  25189. "Indent line like previous, but further if previous was headline or item."
  25190. (interactive)
  25191. (let* ((pos (point))
  25192. (itemp (org-at-item-p))
  25193. column bpos bcol tpos tcol bullet btype bullet-type)
  25194. ;; Find the previous relevant line
  25195. (beginning-of-line 1)
  25196. (cond
  25197. ((looking-at "#") (setq column 0))
  25198. ((looking-at "\\*+ ") (setq column 0))
  25199. (t
  25200. (beginning-of-line 0)
  25201. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25202. (beginning-of-line 0))
  25203. (cond
  25204. ((looking-at "\\*+[ \t]+")
  25205. (goto-char (match-end 0))
  25206. (setq column (current-column)))
  25207. ((org-in-item-p)
  25208. (org-beginning-of-item)
  25209. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25210. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  25211. (setq bpos (match-beginning 1) tpos (match-end 0)
  25212. bcol (progn (goto-char bpos) (current-column))
  25213. tcol (progn (goto-char tpos) (current-column))
  25214. bullet (match-string 1)
  25215. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  25216. (if (not itemp)
  25217. (setq column tcol)
  25218. (goto-char pos)
  25219. (beginning-of-line 1)
  25220. (if (looking-at "\\S-")
  25221. (progn
  25222. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25223. (setq bullet (match-string 1)
  25224. btype (if (string-match "[0-9]" bullet) "n" bullet))
  25225. (setq column (if (equal btype bullet-type) bcol tcol)))
  25226. (setq column (org-get-indentation)))))
  25227. (t (setq column (org-get-indentation))))))
  25228. (goto-char pos)
  25229. (if (<= (current-column) (current-indentation))
  25230. (indent-line-to column)
  25231. (save-excursion (indent-line-to column)))
  25232. (setq column (current-column))
  25233. (beginning-of-line 1)
  25234. (if (looking-at
  25235. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  25236. (replace-match (concat "\\1" (format org-property-format
  25237. (match-string 2) (match-string 3)))
  25238. t nil))
  25239. (move-to-column column)))
  25240. (defun org-set-autofill-regexps ()
  25241. (interactive)
  25242. ;; In the paragraph separator we include headlines, because filling
  25243. ;; text in a line directly attached to a headline would otherwise
  25244. ;; fill the headline as well.
  25245. (org-set-local 'comment-start-skip "^#+[ \t]*")
  25246. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  25247. ;; The paragraph starter includes hand-formatted lists.
  25248. (org-set-local 'paragraph-start
  25249. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  25250. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  25251. ;; But only if the user has not turned off tables or fixed-width regions
  25252. (org-set-local
  25253. 'auto-fill-inhibit-regexp
  25254. (concat "\\*+ \\|#\\+"
  25255. "\\|[ \t]*" org-keyword-time-regexp
  25256. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  25257. (concat
  25258. "\\|[ \t]*["
  25259. (if org-enable-table-editor "|" "")
  25260. (if org-enable-fixed-width-editor ":" "")
  25261. "]"))))
  25262. ;; We use our own fill-paragraph function, to make sure that tables
  25263. ;; and fixed-width regions are not wrapped. That function will pass
  25264. ;; through to `fill-paragraph' when appropriate.
  25265. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  25266. ; Adaptive filling: To get full control, first make sure that
  25267. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  25268. (org-set-local 'adaptive-fill-regexp "\000")
  25269. (org-set-local 'adaptive-fill-function
  25270. 'org-adaptive-fill-function))
  25271. (defun org-fill-paragraph (&optional justify)
  25272. "Re-align a table, pass through to fill-paragraph if no table."
  25273. (let ((table-p (org-at-table-p))
  25274. (table.el-p (org-at-table.el-p)))
  25275. (cond ((and (equal (char-after (point-at-bol)) ?*)
  25276. (save-excursion (goto-char (point-at-bol))
  25277. (looking-at outline-regexp)))
  25278. t) ; skip headlines
  25279. (table.el-p t) ; skip table.el tables
  25280. (table-p (org-table-align) t) ; align org-mode tables
  25281. (t nil)))) ; call paragraph-fill
  25282. ;; For reference, this is the default value of adaptive-fill-regexp
  25283. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  25284. (defun org-adaptive-fill-function ()
  25285. "Return a fill prefix for org-mode files.
  25286. In particular, this makes sure hanging paragraphs for hand-formatted lists
  25287. work correctly."
  25288. (cond ((looking-at "#[ \t]+")
  25289. (match-string 0))
  25290. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  25291. (save-excursion
  25292. (goto-char (match-end 0))
  25293. (make-string (current-column) ?\ )))
  25294. (t nil)))
  25295. ;;;; Functions extending outline functionality
  25296. (defun org-beginning-of-line (&optional arg)
  25297. "Go to the beginning of the current line. If that is invisible, continue
  25298. to a visible line beginning. This makes the function of C-a more intuitive.
  25299. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25300. first attempt, and only move to after the tags when the cursor is already
  25301. beyond the end of the headline."
  25302. (interactive "P")
  25303. (let ((pos (point)))
  25304. (beginning-of-line 1)
  25305. (if (bobp)
  25306. nil
  25307. (backward-char 1)
  25308. (if (org-invisible-p)
  25309. (while (and (not (bobp)) (org-invisible-p))
  25310. (backward-char 1)
  25311. (beginning-of-line 1))
  25312. (forward-char 1)))
  25313. (when org-special-ctrl-a/e
  25314. (cond
  25315. ((and (looking-at org-todo-line-regexp)
  25316. (= (char-after (match-end 1)) ?\ ))
  25317. (goto-char
  25318. (if (eq org-special-ctrl-a/e t)
  25319. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  25320. ((= pos (point)) (match-beginning 3))
  25321. (t (point)))
  25322. (cond ((> pos (point)) (point))
  25323. ((not (eq last-command this-command)) (point))
  25324. (t (match-beginning 3))))))
  25325. ((org-at-item-p)
  25326. (goto-char
  25327. (if (eq org-special-ctrl-a/e t)
  25328. (cond ((> pos (match-end 4)) (match-end 4))
  25329. ((= pos (point)) (match-end 4))
  25330. (t (point)))
  25331. (cond ((> pos (point)) (point))
  25332. ((not (eq last-command this-command)) (point))
  25333. (t (match-end 4))))))))))
  25334. (defun org-end-of-line (&optional arg)
  25335. "Go to the end of the line.
  25336. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25337. first attempt, and only move to after the tags when the cursor is already
  25338. beyond the end of the headline."
  25339. (interactive "P")
  25340. (if (or (not org-special-ctrl-a/e)
  25341. (not (org-on-heading-p)))
  25342. (end-of-line arg)
  25343. (let ((pos (point)))
  25344. (beginning-of-line 1)
  25345. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  25346. (if (eq org-special-ctrl-a/e t)
  25347. (if (or (< pos (match-beginning 1))
  25348. (= pos (match-end 0)))
  25349. (goto-char (match-beginning 1))
  25350. (goto-char (match-end 0)))
  25351. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  25352. (goto-char (match-end 0))
  25353. (goto-char (match-beginning 1))))
  25354. (end-of-line arg)))))
  25355. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  25356. (define-key org-mode-map "\C-e" 'org-end-of-line)
  25357. (defun org-invisible-p ()
  25358. "Check if point is at a character currently not visible."
  25359. ;; Early versions of noutline don't have `outline-invisible-p'.
  25360. (if (fboundp 'outline-invisible-p)
  25361. (outline-invisible-p)
  25362. (get-char-property (point) 'invisible)))
  25363. (defun org-invisible-p2 ()
  25364. "Check if point is at a character currently not visible."
  25365. (save-excursion
  25366. (if (and (eolp) (not (bobp))) (backward-char 1))
  25367. ;; Early versions of noutline don't have `outline-invisible-p'.
  25368. (if (fboundp 'outline-invisible-p)
  25369. (outline-invisible-p)
  25370. (get-char-property (point) 'invisible))))
  25371. (defalias 'org-back-to-heading 'outline-back-to-heading)
  25372. (defalias 'org-on-heading-p 'outline-on-heading-p)
  25373. (defalias 'org-at-heading-p 'outline-on-heading-p)
  25374. (defun org-at-heading-or-item-p ()
  25375. (or (org-on-heading-p) (org-at-item-p)))
  25376. (defun org-on-target-p ()
  25377. (or (org-in-regexp org-radio-target-regexp)
  25378. (org-in-regexp org-target-regexp)))
  25379. (defun org-up-heading-all (arg)
  25380. "Move to the heading line of which the present line is a subheading.
  25381. This function considers both visible and invisible heading lines.
  25382. With argument, move up ARG levels."
  25383. (if (fboundp 'outline-up-heading-all)
  25384. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  25385. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  25386. (defun org-up-heading-safe ()
  25387. "Move to the heading line of which the present line is a subheading.
  25388. This version will not throw an error. It will return the level of the
  25389. headline found, or nil if no higher level is found."
  25390. (let ((pos (point)) start-level level
  25391. (re (concat "^" outline-regexp)))
  25392. (catch 'exit
  25393. (outline-back-to-heading t)
  25394. (setq start-level (funcall outline-level))
  25395. (if (equal start-level 1) (throw 'exit nil))
  25396. (while (re-search-backward re nil t)
  25397. (setq level (funcall outline-level))
  25398. (if (< level start-level) (throw 'exit level)))
  25399. nil)))
  25400. (defun org-first-sibling-p ()
  25401. "Is this heading the first child of its parents?"
  25402. (interactive)
  25403. (let ((re (concat "^" outline-regexp))
  25404. level l)
  25405. (unless (org-at-heading-p t)
  25406. (error "Not at a heading"))
  25407. (setq level (funcall outline-level))
  25408. (save-excursion
  25409. (if (not (re-search-backward re nil t))
  25410. t
  25411. (setq l (funcall outline-level))
  25412. (< l level)))))
  25413. (defun org-goto-sibling (&optional previous)
  25414. "Goto the next sibling, even if it is invisible.
  25415. When PREVIOUS is set, go to the previous sibling instead. Returns t
  25416. when a sibling was found. When none is found, return nil and don't
  25417. move point."
  25418. (let ((fun (if previous 're-search-backward 're-search-forward))
  25419. (pos (point))
  25420. (re (concat "^" outline-regexp))
  25421. level l)
  25422. (when (condition-case nil (org-back-to-heading t) (error nil))
  25423. (setq level (funcall outline-level))
  25424. (catch 'exit
  25425. (or previous (forward-char 1))
  25426. (while (funcall fun re nil t)
  25427. (setq l (funcall outline-level))
  25428. (when (< l level) (goto-char pos) (throw 'exit nil))
  25429. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  25430. (goto-char pos)
  25431. nil))))
  25432. (defun org-show-siblings ()
  25433. "Show all siblings of the current headline."
  25434. (save-excursion
  25435. (while (org-goto-sibling) (org-flag-heading nil)))
  25436. (save-excursion
  25437. (while (org-goto-sibling 'previous)
  25438. (org-flag-heading nil))))
  25439. (defun org-show-hidden-entry ()
  25440. "Show an entry where even the heading is hidden."
  25441. (save-excursion
  25442. (org-show-entry)))
  25443. (defun org-flag-heading (flag &optional entry)
  25444. "Flag the current heading. FLAG non-nil means make invisible.
  25445. When ENTRY is non-nil, show the entire entry."
  25446. (save-excursion
  25447. (org-back-to-heading t)
  25448. ;; Check if we should show the entire entry
  25449. (if entry
  25450. (progn
  25451. (org-show-entry)
  25452. (save-excursion
  25453. (and (outline-next-heading)
  25454. (org-flag-heading nil))))
  25455. (outline-flag-region (max (point-min) (1- (point)))
  25456. (save-excursion (outline-end-of-heading) (point))
  25457. flag))))
  25458. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  25459. ;; This is an exact copy of the original function, but it uses
  25460. ;; `org-back-to-heading', to make it work also in invisible
  25461. ;; trees. And is uses an invisible-OK argument.
  25462. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  25463. (org-back-to-heading invisible-OK)
  25464. (let ((first t)
  25465. (level (funcall outline-level)))
  25466. (while (and (not (eobp))
  25467. (or first (> (funcall outline-level) level)))
  25468. (setq first nil)
  25469. (outline-next-heading))
  25470. (unless to-heading
  25471. (if (memq (preceding-char) '(?\n ?\^M))
  25472. (progn
  25473. ;; Go to end of line before heading
  25474. (forward-char -1)
  25475. (if (memq (preceding-char) '(?\n ?\^M))
  25476. ;; leave blank line before heading
  25477. (forward-char -1))))))
  25478. (point))
  25479. (defun org-show-subtree ()
  25480. "Show everything after this heading at deeper levels."
  25481. (outline-flag-region
  25482. (point)
  25483. (save-excursion
  25484. (outline-end-of-subtree) (outline-next-heading) (point))
  25485. nil))
  25486. (defun org-show-entry ()
  25487. "Show the body directly following this heading.
  25488. Show the heading too, if it is currently invisible."
  25489. (interactive)
  25490. (save-excursion
  25491. (condition-case nil
  25492. (progn
  25493. (org-back-to-heading t)
  25494. (outline-flag-region
  25495. (max (point-min) (1- (point)))
  25496. (save-excursion
  25497. (re-search-forward
  25498. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  25499. (or (match-beginning 1) (point-max)))
  25500. nil))
  25501. (error nil))))
  25502. (defun org-make-options-regexp (kwds)
  25503. "Make a regular expression for keyword lines."
  25504. (concat
  25505. "^"
  25506. "#?[ \t]*\\+\\("
  25507. (mapconcat 'regexp-quote kwds "\\|")
  25508. "\\):[ \t]*"
  25509. "\\(.+\\)"))
  25510. ;; Make isearch reveal the necessary context
  25511. (defun org-isearch-end ()
  25512. "Reveal context after isearch exits."
  25513. (when isearch-success ; only if search was successful
  25514. (if (featurep 'xemacs)
  25515. ;; Under XEmacs, the hook is run in the correct place,
  25516. ;; we directly show the context.
  25517. (org-show-context 'isearch)
  25518. ;; In Emacs the hook runs *before* restoring the overlays.
  25519. ;; So we have to use a one-time post-command-hook to do this.
  25520. ;; (Emacs 22 has a special variable, see function `org-mode')
  25521. (unless (and (boundp 'isearch-mode-end-hook-quit)
  25522. isearch-mode-end-hook-quit)
  25523. ;; Only when the isearch was not quitted.
  25524. (org-add-hook 'post-command-hook 'org-isearch-post-command
  25525. 'append 'local)))))
  25526. (defun org-isearch-post-command ()
  25527. "Remove self from hook, and show context."
  25528. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  25529. (org-show-context 'isearch))
  25530. ;;;; Integration with and fixes for other packages
  25531. ;;; Imenu support
  25532. (defvar org-imenu-markers nil
  25533. "All markers currently used by Imenu.")
  25534. (make-variable-buffer-local 'org-imenu-markers)
  25535. (defun org-imenu-new-marker (&optional pos)
  25536. "Return a new marker for use by Imenu, and remember the marker."
  25537. (let ((m (make-marker)))
  25538. (move-marker m (or pos (point)))
  25539. (push m org-imenu-markers)
  25540. m))
  25541. (defun org-imenu-get-tree ()
  25542. "Produce the index for Imenu."
  25543. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  25544. (setq org-imenu-markers nil)
  25545. (let* ((n org-imenu-depth)
  25546. (re (concat "^" outline-regexp))
  25547. (subs (make-vector (1+ n) nil))
  25548. (last-level 0)
  25549. m tree level head)
  25550. (save-excursion
  25551. (save-restriction
  25552. (widen)
  25553. (goto-char (point-max))
  25554. (while (re-search-backward re nil t)
  25555. (setq level (org-reduced-level (funcall outline-level)))
  25556. (when (<= level n)
  25557. (looking-at org-complex-heading-regexp)
  25558. (setq head (org-match-string-no-properties 4)
  25559. m (org-imenu-new-marker))
  25560. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  25561. (if (>= level last-level)
  25562. (push (cons head m) (aref subs level))
  25563. (push (cons head (aref subs (1+ level))) (aref subs level))
  25564. (loop for i from (1+ level) to n do (aset subs i nil)))
  25565. (setq last-level level)))))
  25566. (aref subs 1)))
  25567. (eval-after-load "imenu"
  25568. '(progn
  25569. (add-hook 'imenu-after-jump-hook
  25570. (lambda () (org-show-context 'org-goto)))))
  25571. ;; Speedbar support
  25572. (defun org-speedbar-set-agenda-restriction ()
  25573. "Restrict future agenda commands to the location at point in speedbar.
  25574. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  25575. (interactive)
  25576. (let (p m tp np dir txt w)
  25577. (cond
  25578. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  25579. 'org-imenu t))
  25580. (setq m (get-text-property p 'org-imenu-marker))
  25581. (save-excursion
  25582. (save-restriction
  25583. (set-buffer (marker-buffer m))
  25584. (goto-char m)
  25585. (org-agenda-set-restriction-lock 'subtree))))
  25586. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  25587. 'speedbar-function 'speedbar-find-file))
  25588. (setq tp (previous-single-property-change
  25589. (1+ p) 'speedbar-function)
  25590. np (next-single-property-change
  25591. tp 'speedbar-function)
  25592. dir (speedbar-line-directory)
  25593. txt (buffer-substring-no-properties (or tp (point-min))
  25594. (or np (point-max))))
  25595. (save-excursion
  25596. (save-restriction
  25597. (set-buffer (find-file-noselect
  25598. (let ((default-directory dir))
  25599. (expand-file-name txt))))
  25600. (unless (org-mode-p)
  25601. (error "Cannot restrict to non-Org-mode file"))
  25602. (org-agenda-set-restriction-lock 'file))))
  25603. (t (error "Don't know how to restrict Org-mode's agenda")))
  25604. (org-move-overlay org-speedbar-restriction-lock-overlay
  25605. (point-at-bol) (point-at-eol))
  25606. (setq current-prefix-arg nil)
  25607. (org-agenda-maybe-redo)))
  25608. (eval-after-load "speedbar"
  25609. '(progn
  25610. (speedbar-add-supported-extension ".org")
  25611. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  25612. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  25613. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  25614. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  25615. (add-hook 'speedbar-visiting-tag-hook
  25616. (lambda () (org-show-context 'org-goto)))))
  25617. ;;; Fixes and Hacks
  25618. ;; Make flyspell not check words in links, to not mess up our keymap
  25619. (defun org-mode-flyspell-verify ()
  25620. "Don't let flyspell put overlays at active buttons."
  25621. (not (get-text-property (point) 'keymap)))
  25622. ;; Make `bookmark-jump' show the jump location if it was hidden.
  25623. (eval-after-load "bookmark"
  25624. '(if (boundp 'bookmark-after-jump-hook)
  25625. ;; We can use the hook
  25626. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  25627. ;; Hook not available, use advice
  25628. (defadvice bookmark-jump (after org-make-visible activate)
  25629. "Make the position visible."
  25630. (org-bookmark-jump-unhide))))
  25631. (defun org-bookmark-jump-unhide ()
  25632. "Unhide the current position, to show the bookmark location."
  25633. (and (org-mode-p)
  25634. (or (org-invisible-p)
  25635. (save-excursion (goto-char (max (point-min) (1- (point))))
  25636. (org-invisible-p)))
  25637. (org-show-context 'bookmark-jump)))
  25638. ;; Fix a bug in htmlize where there are text properties (face nil)
  25639. (eval-after-load "htmlize"
  25640. '(progn
  25641. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  25642. "Make sure there are no nil faces"
  25643. (setq ad-return-value (delq nil ad-return-value)))))
  25644. ;; Make session.el ignore our circular variable
  25645. (eval-after-load "session"
  25646. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  25647. ;;;; Experimental code
  25648. (defun org-closed-in-range ()
  25649. "Sparse tree of items closed in a certain time range.
  25650. Still experimental, may disappear in the future."
  25651. (interactive)
  25652. ;; Get the time interval from the user.
  25653. (let* ((time1 (time-to-seconds
  25654. (org-read-date nil 'to-time nil "Starting date: ")))
  25655. (time2 (time-to-seconds
  25656. (org-read-date nil 'to-time nil "End date:")))
  25657. ;; callback function
  25658. (callback (lambda ()
  25659. (let ((time
  25660. (time-to-seconds
  25661. (apply 'encode-time
  25662. (org-parse-time-string
  25663. (match-string 1))))))
  25664. ;; check if time in interval
  25665. (and (>= time time1) (<= time time2))))))
  25666. ;; make tree, check each match with the callback
  25667. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  25668. ;;;; Finish up
  25669. (provide 'org)
  25670. (run-hooks 'org-load-hook)
  25671. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  25672. ;;; org.el ends here