org.el 1.0 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588
  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.22a+
  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.22a+"
  81. "The version number of the file org.el.")
  82. (defun org-version (&optional here)
  83. "Show the org-mode version in the echo area.
  84. With prefix arg HERE, insert it at point."
  85. (interactive "P")
  86. (let ((version (format "Org-mode version %s" org-version)))
  87. (message version)
  88. (if here
  89. (insert version))))
  90. ;;; Compatibility constants
  91. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  92. (defconst org-format-transports-properties-p
  93. (let ((x "a"))
  94. (add-text-properties 0 1 '(test t) x)
  95. (get-text-property 0 'test (format "%s" x)))
  96. "Does format transport text properties?")
  97. (defmacro org-bound-and-true-p (var)
  98. "Return the value of symbol VAR if it is bound, else nil."
  99. `(and (boundp (quote ,var)) ,var))
  100. (defmacro org-unmodified (&rest body)
  101. "Execute body without changing `buffer-modified-p'."
  102. `(set-buffer-modified-p
  103. (prog1 (buffer-modified-p) ,@body)))
  104. (defmacro org-re (s)
  105. "Replace posix classes in regular expression."
  106. (if (featurep 'xemacs)
  107. (let ((ss s))
  108. (save-match-data
  109. (while (string-match "\\[:alnum:\\]" ss)
  110. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  111. (while (string-match "\\[:alpha:\\]" ss)
  112. (setq ss (replace-match "a-zA-Z" t t ss)))
  113. ss))
  114. s))
  115. (defmacro org-preserve-lc (&rest body)
  116. `(let ((_line (org-current-line))
  117. (_col (current-column)))
  118. (unwind-protect
  119. (progn ,@body)
  120. (goto-line _line)
  121. (move-to-column _col))))
  122. (defmacro org-without-partial-completion (&rest body)
  123. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  124. partial-completion-mode)))
  125. (unwind-protect
  126. (progn
  127. (if pc-mode (partial-completion-mode -1))
  128. ,@body)
  129. (if pc-mode (partial-completion-mode 1)))))
  130. ;;; The custom variables
  131. (defgroup org nil
  132. "Outline-based notes management and organizer."
  133. :tag "Org"
  134. :group 'outlines
  135. :group 'hypermedia
  136. :group 'calendar)
  137. (defcustom org-load-hook '(org-load-default-extensions)
  138. "Hook that is run after org.el has been loaded.
  139. This happens also after `org' has been provided, so
  140. requiring something in this hook that does a (require 'org) is ok."
  141. :group 'org
  142. :type 'hook)
  143. (defcustom org-default-extensions '(org-irc)
  144. "Extensions that should always be loaded together with org.el
  145. If the description starts with <A>, this means the extension
  146. will be autoloaded when needed, to preloading is not necessary."
  147. :group 'org
  148. :type
  149. '(set :greedy t
  150. (const :tag " Mouse support (org-mouse.el)" org-mouse)
  151. (const :tag "<A> Publishing (org-publish.el)" org-publish)
  152. (const :tag "<A> LaTeX export (org-export-latex.el)" org-export-latex)
  153. (const :tag " IRC/ERC links (org-irc.el)" org-irc)
  154. (const :tag " Apple Mail message links under OS X (org-mac-message.el)" org-mac-message)
  155. ))
  156. (defun org-load-default-extensions ()
  157. "Load all extensions that are listed in `org-default-extensions'."
  158. (mapc 'require org-default-extensions))
  159. ;; FIXME: Needs a separate group...
  160. (defcustom org-completion-fallback-command 'hippie-expand
  161. "The expansion command called by \\[org-complete] in normal context.
  162. Normal means, no org-mode-specific context."
  163. :group 'org
  164. :type 'function)
  165. (defgroup org-startup nil
  166. "Options concerning startup of Org-mode."
  167. :tag "Org Startup"
  168. :group 'org)
  169. (defcustom org-startup-folded t
  170. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  171. This can also be configured on a per-file basis by adding one of
  172. the following lines anywhere in the buffer:
  173. #+STARTUP: fold
  174. #+STARTUP: nofold
  175. #+STARTUP: content"
  176. :group 'org-startup
  177. :type '(choice
  178. (const :tag "nofold: show all" nil)
  179. (const :tag "fold: overview" t)
  180. (const :tag "content: all headlines" content)))
  181. (defcustom org-startup-truncated t
  182. "Non-nil means, entering Org-mode will set `truncate-lines'.
  183. This is useful since some lines containing links can be very long and
  184. uninteresting. Also tables look terrible when wrapped."
  185. :group 'org-startup
  186. :type 'boolean)
  187. (defcustom org-startup-align-all-tables nil
  188. "Non-nil means, align all tables when visiting a file.
  189. This is useful when the column width in tables is forced with <N> cookies
  190. in table fields. Such tables will look correct only after the first re-align.
  191. This can also be configured on a per-file basis by adding one of
  192. the following lines anywhere in the buffer:
  193. #+STARTUP: align
  194. #+STARTUP: noalign"
  195. :group 'org-startup
  196. :type 'boolean)
  197. (defcustom org-insert-mode-line-in-empty-file nil
  198. "Non-nil means insert the first line setting Org-mode in empty files.
  199. When the function `org-mode' is called interactively in an empty file, this
  200. normally means that the file name does not automatically trigger Org-mode.
  201. To ensure that the file will always be in Org-mode in the future, a
  202. line enforcing Org-mode will be inserted into the buffer, if this option
  203. has been set."
  204. :group 'org-startup
  205. :type 'boolean)
  206. (defcustom org-replace-disputed-keys nil
  207. "Non-nil means use alternative key bindings for some keys.
  208. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  209. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  210. If you want to use Org-mode together with one of these other modes,
  211. or more generally if you would like to move some Org-mode commands to
  212. other keys, set this variable and configure the keys with the variable
  213. `org-disputed-keys'.
  214. This option is only relevant at load-time of Org-mode, and must be set
  215. *before* org.el is loaded. Changing it requires a restart of Emacs to
  216. become effective."
  217. :group 'org-startup
  218. :type 'boolean)
  219. (if (fboundp 'defvaralias)
  220. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  221. (defcustom org-disputed-keys
  222. '(([(shift up)] . [(meta p)])
  223. ([(shift down)] . [(meta n)])
  224. ([(shift left)] . [(meta -)])
  225. ([(shift right)] . [(meta +)])
  226. ([(control shift right)] . [(meta shift +)])
  227. ([(control shift left)] . [(meta shift -)]))
  228. "Keys for which Org-mode and other modes compete.
  229. This is an alist, cars are the default keys, second element specifies
  230. the alternative to use when `org-replace-disputed-keys' is t.
  231. Keys can be specified in any syntax supported by `define-key'.
  232. The value of this option takes effect only at Org-mode's startup,
  233. therefore you'll have to restart Emacs to apply it after changing."
  234. :group 'org-startup
  235. :type 'alist)
  236. (defun org-key (key)
  237. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  238. Or return the original if not disputed."
  239. (if org-replace-disputed-keys
  240. (let* ((nkey (key-description key))
  241. (x (org-find-if (lambda (x)
  242. (equal (key-description (car x)) nkey))
  243. org-disputed-keys)))
  244. (if x (cdr x) key))
  245. key))
  246. (defun org-find-if (predicate seq)
  247. (catch 'exit
  248. (while seq
  249. (if (funcall predicate (car seq))
  250. (throw 'exit (car seq))
  251. (pop seq)))))
  252. (defun org-defkey (keymap key def)
  253. "Define a key, possibly translated, as returned by `org-key'."
  254. (define-key keymap (org-key key) def))
  255. (defcustom org-ellipsis nil
  256. "The ellipsis to use in the Org-mode outline.
  257. When nil, just use the standard three dots. When a string, use that instead,
  258. When a face, use the standart 3 dots, but with the specified face.
  259. The change affects only Org-mode (which will then use its own display table).
  260. Changing this requires executing `M-x org-mode' in a buffer to become
  261. effective."
  262. :group 'org-startup
  263. :type '(choice (const :tag "Default" nil)
  264. (face :tag "Face" :value org-warning)
  265. (string :tag "String" :value "...#")))
  266. (defvar org-display-table nil
  267. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  268. (defgroup org-keywords nil
  269. "Keywords in Org-mode."
  270. :tag "Org Keywords"
  271. :group 'org)
  272. (defcustom org-deadline-string "DEADLINE:"
  273. "String to mark deadline entries.
  274. A deadline is this string, followed by a time stamp. Should be a word,
  275. terminated by a colon. You can insert a schedule keyword and
  276. a timestamp with \\[org-deadline].
  277. Changes become only effective after restarting Emacs."
  278. :group 'org-keywords
  279. :type 'string)
  280. (defcustom org-scheduled-string "SCHEDULED:"
  281. "String to mark scheduled TODO entries.
  282. A schedule is this string, followed by a time stamp. Should be a word,
  283. terminated by a colon. You can insert a schedule keyword and
  284. a timestamp with \\[org-schedule].
  285. Changes become only effective after restarting Emacs."
  286. :group 'org-keywords
  287. :type 'string)
  288. (defcustom org-closed-string "CLOSED:"
  289. "String used as the prefix for timestamps logging closing a TODO entry."
  290. :group 'org-keywords
  291. :type 'string)
  292. (defcustom org-clock-string "CLOCK:"
  293. "String used as prefix for timestamps clocking work hours on an item."
  294. :group 'org-keywords
  295. :type 'string)
  296. (defcustom org-comment-string "COMMENT"
  297. "Entries starting with this keyword will never be exported.
  298. An entry can be toggled between COMMENT and normal with
  299. \\[org-toggle-comment].
  300. Changes become only effective after restarting Emacs."
  301. :group 'org-keywords
  302. :type 'string)
  303. (defcustom org-quote-string "QUOTE"
  304. "Entries starting with this keyword will be exported in fixed-width font.
  305. Quoting applies only to the text in the entry following the headline, and does
  306. not extend beyond the next headline, even if that is lower level.
  307. An entry can be toggled between QUOTE and normal with
  308. \\[org-toggle-fixed-width-section]."
  309. :group 'org-keywords
  310. :type 'string)
  311. (defconst org-repeat-re
  312. ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  313. ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  314. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
  315. "Regular expression for specifying repeated events.
  316. After a match, group 1 contains the repeat expression.")
  317. (defgroup org-structure nil
  318. "Options concerning the general structure of Org-mode files."
  319. :tag "Org Structure"
  320. :group 'org)
  321. (defgroup org-reveal-location nil
  322. "Options about how to make context of a location visible."
  323. :tag "Org Reveal Location"
  324. :group 'org-structure)
  325. (defconst org-context-choice
  326. '(choice
  327. (const :tag "Always" t)
  328. (const :tag "Never" nil)
  329. (repeat :greedy t :tag "Individual contexts"
  330. (cons
  331. (choice :tag "Context"
  332. (const agenda)
  333. (const org-goto)
  334. (const occur-tree)
  335. (const tags-tree)
  336. (const link-search)
  337. (const mark-goto)
  338. (const bookmark-jump)
  339. (const isearch)
  340. (const default))
  341. (boolean))))
  342. "Contexts for the reveal options.")
  343. (defcustom org-show-hierarchy-above '((default . t))
  344. "Non-nil means, show full hierarchy when revealing a location.
  345. Org-mode often shows locations in an org-mode file which might have
  346. been invisible before. When this is set, the hierarchy of headings
  347. above the exposed location is shown.
  348. Turning this off for example for sparse trees makes them very compact.
  349. Instead of t, this can also be an alist specifying this option for different
  350. contexts. Valid contexts are
  351. agenda when exposing an entry from the agenda
  352. org-goto when using the command `org-goto' on key C-c C-j
  353. occur-tree when using the command `org-occur' on key C-c /
  354. tags-tree when constructing a sparse tree based on tags matches
  355. link-search when exposing search matches associated with a link
  356. mark-goto when exposing the jump goal of a mark
  357. bookmark-jump when exposing a bookmark location
  358. isearch when exiting from an incremental search
  359. default default for all contexts not set explicitly"
  360. :group 'org-reveal-location
  361. :type org-context-choice)
  362. (defcustom org-show-following-heading '((default . nil))
  363. "Non-nil means, show following heading when revealing a location.
  364. Org-mode often shows locations in an org-mode file which might have
  365. been invisible before. When this is set, the heading following the
  366. match is shown.
  367. Turning this off for example for sparse trees makes them very compact,
  368. but makes it harder to edit the location of the match. In such a case,
  369. use the command \\[org-reveal] to show more context.
  370. Instead of t, this can also be an alist specifying this option for different
  371. contexts. See `org-show-hierarchy-above' for valid contexts."
  372. :group 'org-reveal-location
  373. :type org-context-choice)
  374. (defcustom org-show-siblings '((default . nil) (isearch t))
  375. "Non-nil means, show all sibling heading when revealing a location.
  376. Org-mode often shows locations in an org-mode file which might have
  377. been invisible before. When this is set, the sibling of the current entry
  378. heading are all made visible. If `org-show-hierarchy-above' is t,
  379. the same happens on each level of the hierarchy above the current entry.
  380. By default this is on for the isearch context, off for all other contexts.
  381. Turning this off for example for sparse trees makes them very compact,
  382. but makes it harder to edit the location of the match. In such a case,
  383. use the command \\[org-reveal] to show more context.
  384. Instead of t, this can also be an alist specifying this option for different
  385. contexts. See `org-show-hierarchy-above' for valid contexts."
  386. :group 'org-reveal-location
  387. :type org-context-choice)
  388. (defcustom org-show-entry-below '((default . nil))
  389. "Non-nil means, show the entry below a headline when revealing a location.
  390. Org-mode often shows locations in an org-mode file which might have
  391. been invisible before. When this is set, the text below the headline that is
  392. exposed is also shown.
  393. By default this is off for all contexts.
  394. Instead of t, this can also be an alist specifying this option for different
  395. contexts. See `org-show-hierarchy-above' for valid contexts."
  396. :group 'org-reveal-location
  397. :type org-context-choice)
  398. (defgroup org-cycle nil
  399. "Options concerning visibility cycling in Org-mode."
  400. :tag "Org Cycle"
  401. :group 'org-structure)
  402. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  403. "Names of drawers. Drawers are not opened by cycling on the headline above.
  404. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  405. this:
  406. :DRAWERNAME:
  407. .....
  408. :END:
  409. The drawer \"PROPERTIES\" is special for capturing properties through
  410. the property API.
  411. Drawers can be defined on the per-file basis with a line like:
  412. #+DRAWERS: HIDDEN STATE PROPERTIES"
  413. :group 'org-structure
  414. :type '(repeat (string :tag "Drawer Name")))
  415. (defcustom org-cycle-global-at-bob nil
  416. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  417. This makes it possible to do global cycling without having to use S-TAB or
  418. C-u TAB. For this special case to work, the first line of the buffer
  419. must not be a headline - it may be empty ot some other text. When used in
  420. this way, `org-cycle-hook' is disables temporarily, to make sure the
  421. cursor stays at the beginning of the buffer.
  422. When this option is nil, don't do anything special at the beginning
  423. of the buffer."
  424. :group 'org-cycle
  425. :type 'boolean)
  426. (defcustom org-cycle-emulate-tab t
  427. "Where should `org-cycle' emulate TAB.
  428. nil Never
  429. white Only in completely white lines
  430. whitestart Only at the beginning of lines, before the first non-white char
  431. t Everywhere except in headlines
  432. exc-hl-bol Everywhere except at the start of a headline
  433. If TAB is used in a place where it does not emulate TAB, the current subtree
  434. visibility is cycled."
  435. :group 'org-cycle
  436. :type '(choice (const :tag "Never" nil)
  437. (const :tag "Only in completely white lines" white)
  438. (const :tag "Before first char in a line" whitestart)
  439. (const :tag "Everywhere except in headlines" t)
  440. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  441. ))
  442. (defcustom org-cycle-separator-lines 2
  443. "Number of empty lines needed to keep an empty line between collapsed trees.
  444. If you leave an empty line between the end of a subtree and the following
  445. headline, this empty line is hidden when the subtree is folded.
  446. Org-mode will leave (exactly) one empty line visible if the number of
  447. empty lines is equal or larger to the number given in this variable.
  448. So the default 2 means, at least 2 empty lines after the end of a subtree
  449. are needed to produce free space between a collapsed subtree and the
  450. following headline.
  451. Special case: when 0, never leave empty lines in collapsed view."
  452. :group 'org-cycle
  453. :type 'integer)
  454. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  455. org-cycle-hide-drawers
  456. org-cycle-show-empty-lines
  457. org-optimize-window-after-visibility-change)
  458. "Hook that is run after `org-cycle' has changed the buffer visibility.
  459. The function(s) in this hook must accept a single argument which indicates
  460. the new state that was set by the most recent `org-cycle' command. The
  461. argument is a symbol. After a global state change, it can have the values
  462. `overview', `content', or `all'. After a local state change, it can have
  463. the values `folded', `children', or `subtree'."
  464. :group 'org-cycle
  465. :type 'hook)
  466. (defgroup org-edit-structure nil
  467. "Options concerning structure editing in Org-mode."
  468. :tag "Org Edit Structure"
  469. :group 'org-structure)
  470. (defcustom org-odd-levels-only nil
  471. "Non-nil means, skip even levels and only use odd levels for the outline.
  472. This has the effect that two stars are being added/taken away in
  473. promotion/demotion commands. It also influences how levels are
  474. handled by the exporters.
  475. Changing it requires restart of `font-lock-mode' to become effective
  476. for fontification also in regions already fontified.
  477. You may also set this on a per-file basis by adding one of the following
  478. lines to the buffer:
  479. #+STARTUP: odd
  480. #+STARTUP: oddeven"
  481. :group 'org-edit-structure
  482. :group 'org-font-lock
  483. :type 'boolean)
  484. (defcustom org-adapt-indentation t
  485. "Non-nil means, adapt indentation when promoting and demoting.
  486. When this is set and the *entire* text in an entry is indented, the
  487. indentation is increased by one space in a demotion command, and
  488. decreased by one in a promotion command. If any line in the entry
  489. body starts at column 0, indentation is not changed at all."
  490. :group 'org-edit-structure
  491. :type 'boolean)
  492. (defcustom org-special-ctrl-a/e nil
  493. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  494. When t, `C-a' will bring back the cursor to the beginning of the
  495. headline text, i.e. after the stars and after a possible TODO keyword.
  496. In an item, this will be the position after the bullet.
  497. When the cursor is already at that position, another `C-a' will bring
  498. it to the beginning of the line.
  499. `C-e' will jump to the end of the headline, ignoring the presence of tags
  500. in the headline. A second `C-e' will then jump to the true end of the
  501. line, after any tags.
  502. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  503. and only a directly following, identical keypress will bring the cursor
  504. to the special positions."
  505. :group 'org-edit-structure
  506. :type '(choice
  507. (const :tag "off" nil)
  508. (const :tag "after bullet first" t)
  509. (const :tag "border first" reversed)))
  510. (if (fboundp 'defvaralias)
  511. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  512. (defcustom org-special-ctrl-k nil
  513. "Non-nil means `C-k' will behave specially in headlines.
  514. When nil, `C-k' will call the default `kill-line' command.
  515. When t, the following will happen while the cursor is in the headline:
  516. - When the cursor is at the beginning of a headline, kill the entire
  517. line and possible the folded subtree below the line.
  518. - When in the middle of the headline text, kill the headline up to the tags.
  519. - When after the headline text, kill the tags."
  520. :group 'org-edit-structure
  521. :type 'boolean)
  522. (defcustom org-M-RET-may-split-line '((default . t))
  523. "Non-nil means, M-RET will split the line at the cursor position.
  524. When nil, it will go to the end of the line before making a
  525. new line.
  526. You may also set this option in a different way for different
  527. contexts. Valid contexts are:
  528. headline when creating a new headline
  529. item when creating a new item
  530. table in a table field
  531. default the value to be used for all contexts not explicitly
  532. customized"
  533. :group 'org-structure
  534. :group 'org-table
  535. :type '(choice
  536. (const :tag "Always" t)
  537. (const :tag "Never" nil)
  538. (repeat :greedy t :tag "Individual contexts"
  539. (cons
  540. (choice :tag "Context"
  541. (const headline)
  542. (const item)
  543. (const table)
  544. (const default))
  545. (boolean)))))
  546. (defcustom org-blank-before-new-entry '((heading . nil)
  547. (plain-list-item . nil))
  548. "Should `org-insert-heading' leave a blank line before new heading/item?
  549. The value is an alist, with `heading' and `plain-list-item' as car,
  550. and a boolean flag as cdr."
  551. :group 'org-edit-structure
  552. :type '(list
  553. (cons (const heading) (boolean))
  554. (cons (const plain-list-item) (boolean))))
  555. (defcustom org-insert-heading-hook nil
  556. "Hook being run after inserting a new heading."
  557. :group 'org-edit-structure
  558. :type 'hook)
  559. (defcustom org-enable-fixed-width-editor t
  560. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  561. This currently only means, they are never auto-wrapped.
  562. When nil, such lines will be treated like ordinary lines.
  563. See also the QUOTE keyword."
  564. :group 'org-edit-structure
  565. :type 'boolean)
  566. (defcustom org-goto-auto-isearch t
  567. "Non-nil means, typing characters in org-goto starts incremental search."
  568. :group 'org-edit-structure
  569. :type 'boolean)
  570. (defgroup org-sparse-trees nil
  571. "Options concerning sparse trees in Org-mode."
  572. :tag "Org Sparse Trees"
  573. :group 'org-structure)
  574. (defcustom org-highlight-sparse-tree-matches t
  575. "Non-nil means, highlight all matches that define a sparse tree.
  576. The highlights will automatically disappear the next time the buffer is
  577. changed by an edit command."
  578. :group 'org-sparse-trees
  579. :type 'boolean)
  580. (defcustom org-remove-highlights-with-change t
  581. "Non-nil means, any change to the buffer will remove temporary highlights.
  582. Such highlights are created by `org-occur' and `org-clock-display'.
  583. When nil, `C-c C-c needs to be used to get rid of the highlights.
  584. The highlights created by `org-preview-latex-fragment' always need
  585. `C-c C-c' to be removed."
  586. :group 'org-sparse-trees
  587. :group 'org-time
  588. :type 'boolean)
  589. (defcustom org-occur-hook '(org-first-headline-recenter)
  590. "Hook that is run after `org-occur' has constructed a sparse tree.
  591. This can be used to recenter the window to show as much of the structure
  592. as possible."
  593. :group 'org-sparse-trees
  594. :type 'hook)
  595. (defgroup org-plain-lists nil
  596. "Options concerning plain lists in Org-mode."
  597. :tag "Org Plain lists"
  598. :group 'org-structure)
  599. (defcustom org-cycle-include-plain-lists nil
  600. "Non-nil means, include plain lists into visibility cycling.
  601. This means that during cycling, plain list items will *temporarily* be
  602. interpreted as outline headlines with a level given by 1000+i where i is the
  603. indentation of the bullet. In all other operations, plain list items are
  604. not seen as headlines. For example, you cannot assign a TODO keyword to
  605. such an item."
  606. :group 'org-plain-lists
  607. :type 'boolean)
  608. (defcustom org-plain-list-ordered-item-terminator t
  609. "The character that makes a line with leading number an ordered list item.
  610. Valid values are ?. and ?\). To get both terminators, use t. While
  611. ?. may look nicer, it creates the danger that a line with leading
  612. number may be incorrectly interpreted as an item. ?\) therefore is
  613. the safe choice."
  614. :group 'org-plain-lists
  615. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  616. (const :tag "paren like in \"2)\"" ?\))
  617. (const :tab "both" t)))
  618. (defcustom org-auto-renumber-ordered-lists t
  619. "Non-nil means, automatically renumber ordered plain lists.
  620. Renumbering happens when the sequence have been changed with
  621. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  622. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  623. :group 'org-plain-lists
  624. :type 'boolean)
  625. (defcustom org-provide-checkbox-statistics t
  626. "Non-nil means, update checkbox statistics after insert and toggle.
  627. When this is set, checkbox statistics is updated each time you either insert
  628. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  629. with \\[org-ctrl-c-ctrl-c\\]."
  630. :group 'org-plain-lists
  631. :type 'boolean)
  632. (defgroup org-archive nil
  633. "Options concerning archiving in Org-mode."
  634. :tag "Org Archive"
  635. :group 'org-structure)
  636. (defcustom org-archive-tag "ARCHIVE"
  637. "The tag that marks a subtree as archived.
  638. An archived subtree does not open during visibility cycling, and does
  639. not contribute to the agenda listings.
  640. After changing this, font-lock must be restarted in the relevant buffers to
  641. get the proper fontification."
  642. :group 'org-archive
  643. :group 'org-keywords
  644. :type 'string)
  645. (defcustom org-agenda-skip-archived-trees t
  646. "Non-nil means, the agenda will skip any items located in archived trees.
  647. An archived tree is a tree marked with the tag ARCHIVE."
  648. :group 'org-archive
  649. :group 'org-agenda-skip
  650. :type 'boolean)
  651. (defcustom org-cycle-open-archived-trees nil
  652. "Non-nil means, `org-cycle' will open archived trees.
  653. An archived tree is a tree marked with the tag ARCHIVE.
  654. When nil, archived trees will stay folded. You can still open them with
  655. normal outline commands like `show-all', but not with the cycling commands."
  656. :group 'org-archive
  657. :group 'org-cycle
  658. :type 'boolean)
  659. (defcustom org-sparse-tree-open-archived-trees nil
  660. "Non-nil means sparse tree construction shows matches in archived trees.
  661. When nil, matches in these trees are highlighted, but the trees are kept in
  662. collapsed state."
  663. :group 'org-archive
  664. :group 'org-sparse-trees
  665. :type 'boolean)
  666. (defcustom org-archive-location "%s_archive::"
  667. "The location where subtrees should be archived.
  668. This string consists of two parts, separated by a double-colon.
  669. The first part is a file name - when omitted, archiving happens in the same
  670. file. %s will be replaced by the current file name (without directory part).
  671. Archiving to a different file is useful to keep archived entries from
  672. contributing to the Org-mode Agenda.
  673. The part after the double colon is a headline. The archived entries will be
  674. filed under that headline. When omitted, the subtrees are simply filed away
  675. at the end of the file, as top-level entries.
  676. Here are a few examples:
  677. \"%s_archive::\"
  678. If the current file is Projects.org, archive in file
  679. Projects.org_archive, as top-level trees. This is the default.
  680. \"::* Archived Tasks\"
  681. Archive in the current file, under the top-level headline
  682. \"* Archived Tasks\".
  683. \"~/org/archive.org::\"
  684. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  685. \"basement::** Finished Tasks\"
  686. Archive in file ./basement (relative path), as level 3 trees
  687. below the level 2 heading \"** Finished Tasks\".
  688. You may set this option on a per-file basis by adding to the buffer a
  689. line like
  690. #+ARCHIVE: basement::** Finished Tasks"
  691. :group 'org-archive
  692. :type 'string)
  693. (defcustom org-archive-mark-done t
  694. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  695. This can be a string to set the keyword to use. When t, Org-mode will
  696. use the first keyword in its list that means done."
  697. :group 'org-archive
  698. :type '(choice
  699. (const :tag "No" nil)
  700. (const :tag "Yes" t)
  701. (string :tag "Use this keyword")))
  702. (defcustom org-archive-stamp-time t
  703. "Non-nil means, add a time stamp to entries moved to an archive file.
  704. This variable is obsolete and has no effect anymore, instead add ot remove
  705. `time' from the variablle `org-archive-save-context-info'."
  706. :group 'org-archive
  707. :type 'boolean)
  708. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  709. "Parts of context info that should be stored as properties when archiving.
  710. When a subtree is moved to an archive file, it looses information given by
  711. context, like inherited tags, the category, and possibly also the TODO
  712. state (depending on the variable `org-archive-mark-done').
  713. This variable can be a list of any of the following symbols:
  714. time The time of archiving.
  715. file The file where the entry originates.
  716. itags The local tags, in the headline of the subtree.
  717. ltags The tags the subtree inherits from further up the hierarchy.
  718. todo The pre-archive TODO state.
  719. category The category, taken from file name or #+CATEGORY lines.
  720. olpath The outline path to the item. These are all headlines above
  721. the current item, separated by /, like a file path.
  722. For each symbol present in the list, a property will be created in
  723. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  724. information."
  725. :group 'org-archive
  726. :type '(set :greedy t
  727. (const :tag "Time" time)
  728. (const :tag "File" file)
  729. (const :tag "Category" category)
  730. (const :tag "TODO state" todo)
  731. (const :tag "TODO state" priority)
  732. (const :tag "Inherited tags" itags)
  733. (const :tag "Outline path" olpath)
  734. (const :tag "Local tags" ltags)))
  735. (defgroup org-imenu-and-speedbar nil
  736. "Options concerning imenu and speedbar in Org-mode."
  737. :tag "Org Imenu and Speedbar"
  738. :group 'org-structure)
  739. (defcustom org-imenu-depth 2
  740. "The maximum level for Imenu access to Org-mode headlines.
  741. This also applied for speedbar access."
  742. :group 'org-imenu-and-speedbar
  743. :type 'number)
  744. (defgroup org-table nil
  745. "Options concerning tables in Org-mode."
  746. :tag "Org Table"
  747. :group 'org)
  748. (defcustom org-enable-table-editor 'optimized
  749. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  750. When nil, such lines will be treated like ordinary lines.
  751. When equal to the symbol `optimized', the table editor will be optimized to
  752. do the following:
  753. - Automatic overwrite mode in front of whitespace in table fields.
  754. This makes the structure of the table stay in tact as long as the edited
  755. field does not exceed the column width.
  756. - Minimize the number of realigns. Normally, the table is aligned each time
  757. TAB or RET are pressed to move to another field. With optimization this
  758. happens only if changes to a field might have changed the column width.
  759. Optimization requires replacing the functions `self-insert-command',
  760. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  761. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  762. very good at guessing when a re-align will be necessary, but you can always
  763. force one with \\[org-ctrl-c-ctrl-c].
  764. If you would like to use the optimized version in Org-mode, but the
  765. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  766. This variable can be used to turn on and off the table editor during a session,
  767. but in order to toggle optimization, a restart is required.
  768. See also the variable `org-table-auto-blank-field'."
  769. :group 'org-table
  770. :type '(choice
  771. (const :tag "off" nil)
  772. (const :tag "on" t)
  773. (const :tag "on, optimized" optimized)))
  774. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  775. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  776. In the optimized version, the table editor takes over all simple keys that
  777. normally just insert a character. In tables, the characters are inserted
  778. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  779. for empty fields). Outside tables, the correct binding of the keys is
  780. restored.
  781. The default for this option is t if the optimized version is also used in
  782. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  783. this variable requires a restart of Emacs to become effective."
  784. :group 'org-table
  785. :type 'boolean)
  786. (defcustom orgtbl-radio-table-templates
  787. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  788. % END RECEIVE ORGTBL %n
  789. \\begin{comment}
  790. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  791. | | |
  792. \\end{comment}\n")
  793. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  794. @c END RECEIVE ORGTBL %n
  795. @ignore
  796. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  797. | | |
  798. @end ignore\n")
  799. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  800. <!-- END RECEIVE ORGTBL %n -->
  801. <!--
  802. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  803. | | |
  804. -->\n"))
  805. "Templates for radio tables in different major modes.
  806. All occurrences of %n in a template will be replaced with the name of the
  807. table, obtained by prompting the user."
  808. :group 'org-table
  809. :type '(repeat
  810. (list (symbol :tag "Major mode")
  811. (string :tag "Format"))))
  812. (defgroup org-table-settings nil
  813. "Settings for tables in Org-mode."
  814. :tag "Org Table Settings"
  815. :group 'org-table)
  816. (defcustom org-table-default-size "5x2"
  817. "The default size for newly created tables, Columns x Rows."
  818. :group 'org-table-settings
  819. :type 'string)
  820. (defcustom org-table-number-regexp
  821. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  822. "Regular expression for recognizing numbers in table columns.
  823. If a table column contains mostly numbers, it will be aligned to the
  824. right. If not, it will be aligned to the left.
  825. The default value of this option is a regular expression which allows
  826. anything which looks remotely like a number as used in scientific
  827. context. For example, all of the following will be considered a
  828. number:
  829. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  830. Other options offered by the customize interface are more restrictive."
  831. :group 'org-table-settings
  832. :type '(choice
  833. (const :tag "Positive Integers"
  834. "^[0-9]+$")
  835. (const :tag "Integers"
  836. "^[-+]?[0-9]+$")
  837. (const :tag "Floating Point Numbers"
  838. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  839. (const :tag "Floating Point Number or Integer"
  840. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  841. (const :tag "Exponential, Floating point, Integer"
  842. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  843. (const :tag "Very General Number-Like, including hex"
  844. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  845. (string :tag "Regexp:")))
  846. (defcustom org-table-number-fraction 0.5
  847. "Fraction of numbers in a column required to make the column align right.
  848. In a column all non-white fields are considered. If at least this
  849. fraction of fields is matched by `org-table-number-fraction',
  850. alignment to the right border applies."
  851. :group 'org-table-settings
  852. :type 'number)
  853. (defgroup org-table-editing nil
  854. "Behavior of tables during editing in Org-mode."
  855. :tag "Org Table Editing"
  856. :group 'org-table)
  857. (defcustom org-table-automatic-realign t
  858. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  859. When nil, aligning is only done with \\[org-table-align], or after column
  860. removal/insertion."
  861. :group 'org-table-editing
  862. :type 'boolean)
  863. (defcustom org-table-auto-blank-field t
  864. "Non-nil means, automatically blank table field when starting to type into it.
  865. This only happens when typing immediately after a field motion
  866. command (TAB, S-TAB or RET).
  867. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  868. :group 'org-table-editing
  869. :type 'boolean)
  870. (defcustom org-table-tab-jumps-over-hlines t
  871. "Non-nil means, tab in the last column of a table with jump over a hline.
  872. If a horizontal separator line is following the current line,
  873. `org-table-next-field' can either create a new row before that line, or jump
  874. over the line. When this option is nil, a new line will be created before
  875. this line."
  876. :group 'org-table-editing
  877. :type 'boolean)
  878. (defcustom org-table-tab-recognizes-table.el t
  879. "Non-nil means, TAB will automatically notice a table.el table.
  880. When it sees such a table, it moves point into it and - if necessary -
  881. calls `table-recognize-table'."
  882. :group 'org-table-editing
  883. :type 'boolean)
  884. (defgroup org-table-calculation nil
  885. "Options concerning tables in Org-mode."
  886. :tag "Org Table Calculation"
  887. :group 'org-table)
  888. (defcustom org-table-use-standard-references t
  889. "Should org-mode work with table refrences like B3 instead of @3$2?
  890. Possible values are:
  891. nil never use them
  892. from accept as input, do not present for editing
  893. t: accept as input and present for editing"
  894. :group 'org-table-calculation
  895. :type '(choice
  896. (const :tag "Never, don't even check unser input for them" nil)
  897. (const :tag "Always, both as user input, and when editing" t)
  898. (const :tag "Convert user input, don't offer during editing" 'from)))
  899. (defcustom org-table-copy-increment t
  900. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  901. :group 'org-table-calculation
  902. :type 'boolean)
  903. (defcustom org-calc-default-modes
  904. '(calc-internal-prec 12
  905. calc-float-format (float 5)
  906. calc-angle-mode deg
  907. calc-prefer-frac nil
  908. calc-symbolic-mode nil
  909. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  910. calc-display-working-message t
  911. )
  912. "List with Calc mode settings for use in calc-eval for table formulas.
  913. The list must contain alternating symbols (Calc modes variables and values).
  914. Don't remove any of the default settings, just change the values. Org-mode
  915. relies on the variables to be present in the list."
  916. :group 'org-table-calculation
  917. :type 'plist)
  918. (defcustom org-table-formula-evaluate-inline t
  919. "Non-nil means, TAB and RET evaluate a formula in current table field.
  920. If the current field starts with an equal sign, it is assumed to be a formula
  921. which should be evaluated as described in the manual and in the documentation
  922. string of the command `org-table-eval-formula'. This feature requires the
  923. Emacs calc package.
  924. When this variable is nil, formula calculation is only available through
  925. the command \\[org-table-eval-formula]."
  926. :group 'org-table-calculation
  927. :type 'boolean)
  928. (defcustom org-table-formula-use-constants t
  929. "Non-nil means, interpret constants in formulas in tables.
  930. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  931. by the value given in `org-table-formula-constants', or by a value obtained
  932. from the `constants.el' package."
  933. :group 'org-table-calculation
  934. :type 'boolean)
  935. (defcustom org-table-formula-constants nil
  936. "Alist with constant names and values, for use in table formulas.
  937. The car of each element is a name of a constant, without the `$' before it.
  938. The cdr is the value as a string. For example, if you'd like to use the
  939. speed of light in a formula, you would configure
  940. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  941. and then use it in an equation like `$1*$c'.
  942. Constants can also be defined on a per-file basis using a line like
  943. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  944. :group 'org-table-calculation
  945. :type '(repeat
  946. (cons (string :tag "name")
  947. (string :tag "value"))))
  948. (defvar org-table-formula-constants-local nil
  949. "Local version of `org-table-formula-constants'.")
  950. (make-variable-buffer-local 'org-table-formula-constants-local)
  951. (defcustom org-table-allow-automatic-line-recalculation t
  952. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  953. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  954. :group 'org-table-calculation
  955. :type 'boolean)
  956. (defgroup org-link nil
  957. "Options concerning links in Org-mode."
  958. :tag "Org Link"
  959. :group 'org)
  960. (defvar org-link-abbrev-alist-local nil
  961. "Buffer-local version of `org-link-abbrev-alist', which see.
  962. The value of this is taken from the #+LINK lines.")
  963. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  964. (defcustom org-link-abbrev-alist nil
  965. "Alist of link abbreviations.
  966. The car of each element is a string, to be replaced at the start of a link.
  967. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  968. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  969. [[linkkey:tag][description]]
  970. If REPLACE is a string, the tag will simply be appended to create the link.
  971. If the string contains \"%s\", the tag will be inserted there.
  972. REPLACE may also be a function that will be called with the tag as the
  973. only argument to create the link, which should be returned as a string.
  974. See the manual for examples."
  975. :group 'org-link
  976. :type 'alist)
  977. (defcustom org-descriptive-links t
  978. "Non-nil means, hide link part and only show description of bracket links.
  979. Bracket links are like [[link][descritpion]]. This variable sets the initial
  980. state in new org-mode buffers. The setting can then be toggled on a
  981. per-buffer basis from the Org->Hyperlinks menu."
  982. :group 'org-link
  983. :type 'boolean)
  984. (defcustom org-link-file-path-type 'adaptive
  985. "How the path name in file links should be stored.
  986. Valid values are:
  987. relative Relative to the current directory, i.e. the directory of the file
  988. into which the link is being inserted.
  989. absolute Absolute path, if possible with ~ for home directory.
  990. noabbrev Absolute path, no abbreviation of home directory.
  991. adaptive Use relative path for files in the current directory and sub-
  992. directories of it. For other files, use an absolute path."
  993. :group 'org-link
  994. :type '(choice
  995. (const relative)
  996. (const absolute)
  997. (const noabbrev)
  998. (const adaptive)))
  999. (defcustom org-activate-links '(bracket angle plain radio tag date)
  1000. "Types of links that should be activated in Org-mode files.
  1001. This is a list of symbols, each leading to the activation of a certain link
  1002. type. In principle, it does not hurt to turn on most link types - there may
  1003. be a small gain when turning off unused link types. The types are:
  1004. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1005. angular Links in angular brackes that may contain whitespace like
  1006. <bbdb:Carsten Dominik>.
  1007. plain Plain links in normal text, no whitespace, like http://google.com.
  1008. radio Text that is matched by a radio target, see manual for details.
  1009. tag Tag settings in a headline (link to tag search).
  1010. date Time stamps (link to calendar).
  1011. Changing this variable requires a restart of Emacs to become effective."
  1012. :group 'org-link
  1013. :type '(set (const :tag "Double bracket links (new style)" bracket)
  1014. (const :tag "Angular bracket links (old style)" angular)
  1015. (const :tag "plain text links" plain)
  1016. (const :tag "Radio target matches" radio)
  1017. (const :tag "Tags" tag)
  1018. (const :tag "Tags" target)
  1019. (const :tag "Timestamps" date)))
  1020. (defgroup org-link-store nil
  1021. "Options concerning storing links in Org-mode"
  1022. :tag "Org Store Link"
  1023. :group 'org-link)
  1024. (defcustom org-email-link-description-format "Email %c: %.30s"
  1025. "Format of the description part of a link to an email or usenet message.
  1026. The following %-excapes will be replaced by corresponding information:
  1027. %F full \"From\" field
  1028. %f name, taken from \"From\" field, address if no name
  1029. %T full \"To\" field
  1030. %t first name in \"To\" field, address if no name
  1031. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  1032. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1033. %s subject
  1034. %m message-id.
  1035. You may use normal field width specification between the % and the letter.
  1036. This is for example useful to limit the length of the subject.
  1037. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1038. :group 'org-link-store
  1039. :type 'string)
  1040. (defcustom org-from-is-user-regexp
  1041. (let (r1 r2)
  1042. (when (and user-mail-address (not (string= user-mail-address "")))
  1043. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1044. (when (and user-full-name (not (string= user-full-name "")))
  1045. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1046. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1047. "Regexp mached against the \"From:\" header of an email or usenet message.
  1048. It should match if the message is from the user him/herself."
  1049. :group 'org-link-store
  1050. :type 'regexp)
  1051. (defcustom org-context-in-file-links t
  1052. "Non-nil means, file links from `org-store-link' contain context.
  1053. A search string will be added to the file name with :: as separator and
  1054. used to find the context when the link is activated by the command
  1055. `org-open-at-point'.
  1056. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1057. negates this setting for the duration of the command."
  1058. :group 'org-link-store
  1059. :type 'boolean)
  1060. (defcustom org-keep-stored-link-after-insertion nil
  1061. "Non-nil means, keep link in list for entire session.
  1062. The command `org-store-link' adds a link pointing to the current
  1063. location to an internal list. These links accumulate during a session.
  1064. The command `org-insert-link' can be used to insert links into any
  1065. Org-mode file (offering completion for all stored links). When this
  1066. option is nil, every link which has been inserted once using \\[org-insert-link]
  1067. will be removed from the list, to make completing the unused links
  1068. more efficient."
  1069. :group 'org-link-store
  1070. :type 'boolean)
  1071. (defcustom org-usenet-links-prefer-google nil
  1072. "Non-nil means, `org-store-link' will create web links to Google groups.
  1073. When nil, Gnus will be used for such links.
  1074. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1075. negates this setting for the duration of the command."
  1076. :group 'org-link-store
  1077. :type 'boolean)
  1078. (defgroup org-link-follow nil
  1079. "Options concerning following links in Org-mode"
  1080. :tag "Org Follow Link"
  1081. :group 'org-link)
  1082. (defcustom org-tab-follows-link nil
  1083. "Non-nil means, on links TAB will follow the link.
  1084. Needs to be set before org.el is loaded."
  1085. :group 'org-link-follow
  1086. :type 'boolean)
  1087. (defcustom org-return-follows-link nil
  1088. "Non-nil means, on links RET will follow the link.
  1089. Needs to be set before org.el is loaded."
  1090. :group 'org-link-follow
  1091. :type 'boolean)
  1092. (defcustom org-mouse-1-follows-link t
  1093. "Non-nil means, mouse-1 on a link will follow the link.
  1094. A longer mouse click will still set point. Does not wortk on XEmacs.
  1095. Needs to be set before org.el is loaded."
  1096. :group 'org-link-follow
  1097. :type 'boolean)
  1098. (defcustom org-mark-ring-length 4
  1099. "Number of different positions to be recorded in the ring
  1100. Changing this requires a restart of Emacs to work correctly."
  1101. :group 'org-link-follow
  1102. :type 'interger)
  1103. (defcustom org-link-frame-setup
  1104. '((vm . vm-visit-folder-other-frame)
  1105. (gnus . gnus-other-frame)
  1106. (file . find-file-other-window))
  1107. "Setup the frame configuration for following links.
  1108. When following a link with Emacs, it may often be useful to display
  1109. this link in another window or frame. This variable can be used to
  1110. set this up for the different types of links.
  1111. For VM, use any of
  1112. `vm-visit-folder'
  1113. `vm-visit-folder-other-frame'
  1114. For Gnus, use any of
  1115. `gnus'
  1116. `gnus-other-frame'
  1117. For FILE, use any of
  1118. `find-file'
  1119. `find-file-other-window'
  1120. `find-file-other-frame'
  1121. For the calendar, use the variable `calendar-setup'.
  1122. For BBDB, it is currently only possible to display the matches in
  1123. another window."
  1124. :group 'org-link-follow
  1125. :type '(list
  1126. (cons (const vm)
  1127. (choice
  1128. (const vm-visit-folder)
  1129. (const vm-visit-folder-other-window)
  1130. (const vm-visit-folder-other-frame)))
  1131. (cons (const gnus)
  1132. (choice
  1133. (const gnus)
  1134. (const gnus-other-frame)))
  1135. (cons (const file)
  1136. (choice
  1137. (const find-file)
  1138. (const find-file-other-window)
  1139. (const find-file-other-frame)))))
  1140. (defcustom org-display-internal-link-with-indirect-buffer nil
  1141. "Non-nil means, use indirect buffer to display infile links.
  1142. Activating internal links (from one location in a file to another location
  1143. in the same file) normally just jumps to the location. When the link is
  1144. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1145. another window. When this option is set, the other window actually displays
  1146. an indirect buffer clone of the current buffer, to avoid any visibility
  1147. changes to the current buffer."
  1148. :group 'org-link-follow
  1149. :type 'boolean)
  1150. (defcustom org-open-non-existing-files nil
  1151. "Non-nil means, `org-open-file' will open non-existing files.
  1152. When nil, an error will be generated."
  1153. :group 'org-link-follow
  1154. :type 'boolean)
  1155. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1156. "Function and arguments to call for following mailto links.
  1157. This is a list with the first element being a lisp function, and the
  1158. remaining elements being arguments to the function. In string arguments,
  1159. %a will be replaced by the address, and %s will be replaced by the subject
  1160. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1161. :group 'org-link-follow
  1162. :type '(choice
  1163. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1164. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1165. (const :tag "message-mail" (message-mail "%a" "%s"))
  1166. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1167. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1168. "Non-nil means, ask for confirmation before executing shell links.
  1169. Shell links can be dangerous: just think about a link
  1170. [[shell:rm -rf ~/*][Google Search]]
  1171. This link would show up in your Org-mode document as \"Google Search\",
  1172. but really it would remove your entire home directory.
  1173. Therefore we advise against setting this variable to nil.
  1174. Just change it to `y-or-n-p' of you want to confirm with a
  1175. single keystroke rather than having to type \"yes\"."
  1176. :group 'org-link-follow
  1177. :type '(choice
  1178. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1179. (const :tag "with y-or-n (faster)" y-or-n-p)
  1180. (const :tag "no confirmation (dangerous)" nil)))
  1181. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1182. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1183. Elisp links can be dangerous: just think about a link
  1184. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1185. This link would show up in your Org-mode document as \"Google Search\",
  1186. but really it would remove your entire home directory.
  1187. Therefore we advise against setting this variable to nil.
  1188. Just change it to `y-or-n-p' of you want to confirm with a
  1189. single keystroke rather than having to type \"yes\"."
  1190. :group 'org-link-follow
  1191. :type '(choice
  1192. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1193. (const :tag "with y-or-n (faster)" y-or-n-p)
  1194. (const :tag "no confirmation (dangerous)" nil)))
  1195. (defconst org-file-apps-defaults-gnu
  1196. '((remote . emacs)
  1197. (t . mailcap))
  1198. "Default file applications on a UNIX or GNU/Linux system.
  1199. See `org-file-apps'.")
  1200. (defconst org-file-apps-defaults-macosx
  1201. '((remote . emacs)
  1202. (t . "open %s")
  1203. ("ps" . "gv %s")
  1204. ("ps.gz" . "gv %s")
  1205. ("eps" . "gv %s")
  1206. ("eps.gz" . "gv %s")
  1207. ("dvi" . "xdvi %s")
  1208. ("fig" . "xfig %s"))
  1209. "Default file applications on a MacOS X system.
  1210. The system \"open\" is known as a default, but we use X11 applications
  1211. for some files for which the OS does not have a good default.
  1212. See `org-file-apps'.")
  1213. (defconst org-file-apps-defaults-windowsnt
  1214. (list
  1215. '(remote . emacs)
  1216. (cons t
  1217. (list (if (featurep 'xemacs)
  1218. 'mswindows-shell-execute
  1219. 'w32-shell-execute)
  1220. "open" 'file)))
  1221. "Default file applications on a Windows NT system.
  1222. The system \"open\" is used for most files.
  1223. See `org-file-apps'.")
  1224. (defcustom org-file-apps
  1225. '(
  1226. ("txt" . emacs)
  1227. ("tex" . emacs)
  1228. ("ltx" . emacs)
  1229. ("org" . emacs)
  1230. ("el" . emacs)
  1231. ("bib" . emacs)
  1232. )
  1233. "External applications for opening `file:path' items in a document.
  1234. Org-mode uses system defaults for different file types, but
  1235. you can use this variable to set the application for a given file
  1236. extension. The entries in this list are cons cells where the car identifies
  1237. files and the cdr the corresponding command. Possible values for the
  1238. file identifier are
  1239. \"ext\" A string identifying an extension
  1240. `directory' Matches a directory
  1241. `remote' Matches a remote file, accessible through tramp or efs.
  1242. Remote files most likely should be visited through Emacs
  1243. because external applications cannot handle such paths.
  1244. t Default for all remaining files
  1245. Possible values for the command are:
  1246. `emacs' The file will be visited by the current Emacs process.
  1247. `default' Use the default application for this file type.
  1248. string A command to be executed by a shell; %s will be replaced
  1249. by the path to the file.
  1250. sexp A Lisp form which will be evaluated. The file path will
  1251. be available in the Lisp variable `file'.
  1252. For more examples, see the system specific constants
  1253. `org-file-apps-defaults-macosx'
  1254. `org-file-apps-defaults-windowsnt'
  1255. `org-file-apps-defaults-gnu'."
  1256. :group 'org-link-follow
  1257. :type '(repeat
  1258. (cons (choice :value ""
  1259. (string :tag "Extension")
  1260. (const :tag "Default for unrecognized files" t)
  1261. (const :tag "Remote file" remote)
  1262. (const :tag "Links to a directory" directory))
  1263. (choice :value ""
  1264. (const :tag "Visit with Emacs" emacs)
  1265. (const :tag "Use system default" default)
  1266. (string :tag "Command")
  1267. (sexp :tag "Lisp form")))))
  1268. (defcustom org-mhe-search-all-folders nil
  1269. "Non-nil means, that the search for the mh-message will be extended to
  1270. all folders if the message cannot be found in the folder given in the link.
  1271. Searching all folders is very efficient with one of the search engines
  1272. supported by MH-E, but will be slow with pick."
  1273. :group 'org-link-follow
  1274. :type 'boolean)
  1275. (defgroup org-remember nil
  1276. "Options concerning interaction with remember.el."
  1277. :tag "Org Remember"
  1278. :group 'org)
  1279. (defcustom org-directory "~/org"
  1280. "Directory with org files.
  1281. This directory will be used as default to prompt for org files.
  1282. Used by the hooks for remember.el."
  1283. :group 'org-remember
  1284. :type 'directory)
  1285. (defcustom org-default-notes-file "~/.notes"
  1286. "Default target for storing notes.
  1287. Used by the hooks for remember.el. This can be a string, or nil to mean
  1288. the value of `remember-data-file'.
  1289. You can set this on a per-template basis with the variable
  1290. `org-remember-templates'."
  1291. :group 'org-remember
  1292. :type '(choice
  1293. (const :tag "Default from remember-data-file" nil)
  1294. file))
  1295. (defcustom org-remember-store-without-prompt t
  1296. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1297. In this case, you need `C-u C-c C-c' to get the prompts for
  1298. note file and headline.
  1299. When this variable is nil, `C-c C-c' give you the prompts, and
  1300. `C-u C-c C-c' trigger the fasttrack."
  1301. :group 'org-remember
  1302. :type 'boolean)
  1303. (defcustom org-remember-interactive-interface 'refile
  1304. "The interface to be used for interactive filing of remember notes.
  1305. This is only used when the interactive mode for selecting a filing
  1306. location is used (see the variable `org-remember-store-without-prompt').
  1307. Allowed vaues are:
  1308. outline The interface shows an outline of the relevant file
  1309. and the correct heading is found by moving through
  1310. the outline or by searching with incremental search.
  1311. outline-path-completion Headlines in the current buffer are offered via
  1312. completion.
  1313. refile Use the refile interface, and offer headlines,
  1314. possibly from different buffers."
  1315. :group 'org-remember
  1316. :type '(choice
  1317. (const :tag "Refile" refile)
  1318. (const :tag "Outline" outline)
  1319. (const :tag "Outline-path-completion" outline-path-completion)))
  1320. (defcustom org-goto-interface 'outline
  1321. "The default interface to be used for `org-goto'.
  1322. Allowed vaues are:
  1323. outline The interface shows an outline of the relevant file
  1324. and the correct heading is found by moving through
  1325. the outline or by searching with incremental search.
  1326. outline-path-completion Headlines in the current buffer are offered via
  1327. completion."
  1328. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1329. :type '(choice
  1330. (const :tag "Outline" outline)
  1331. (const :tag "Outline-path-completion" outline-path-completion)))
  1332. (defcustom org-remember-default-headline ""
  1333. "The headline that should be the default location in the notes file.
  1334. When filing remember notes, the cursor will start at that position.
  1335. You can set this on a per-template basis with the variable
  1336. `org-remember-templates'."
  1337. :group 'org-remember
  1338. :type 'string)
  1339. (defcustom org-remember-templates nil
  1340. "Templates for the creation of remember buffers.
  1341. When nil, just let remember make the buffer.
  1342. When not nil, this is a list of 5-element lists. In each entry, the first
  1343. element is the name of the template, which should be a single short word.
  1344. The second element is a character, a unique key to select this template.
  1345. The third element is the template. The fourth element is optional and can
  1346. specify a destination file for remember items created with this template.
  1347. The default file is given by `org-default-notes-file'. An optional fifth
  1348. element can specify the headline in that file that should be offered
  1349. first when the user is asked to file the entry. The default headline is
  1350. given in the variable `org-remember-default-headline'.
  1351. The template specifies the structure of the remember buffer. It should have
  1352. a first line starting with a star, to act as the org-mode headline.
  1353. Furthermore, the following %-escapes will be replaced with content:
  1354. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1355. A default value and a completion table ca be specified like this:
  1356. %^{prompt|default|completion2|completion3|...}
  1357. %t time stamp, date only
  1358. %T time stamp with date and time
  1359. %u, %U like the above, but inactive time stamps
  1360. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1361. You may define a prompt like %^{Please specify birthday}t
  1362. %n user name (taken from `user-full-name')
  1363. %a annotation, normally the link created with org-store-link
  1364. %i initial content, the region active. If %i is indented,
  1365. the entire inserted text will be indented as well.
  1366. %c content of the clipboard, or current kill ring head
  1367. %^g prompt for tags, with completion on tags in target file
  1368. %^G prompt for tags, with completion all tags in all agenda files
  1369. %:keyword specific information for certain link types, see below
  1370. %[pathname] insert the contents of the file given by `pathname'
  1371. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1372. %! Store this note immediately after filling the template
  1373. %? After completing the template, position cursor here.
  1374. Apart from these general escapes, you can access information specific to the
  1375. link type that is created. For example, calling `remember' in emails or gnus
  1376. will record the author and the subject of the message, which you can access
  1377. with %:author and %:subject, respectively. Here is a complete list of what
  1378. is recorded for each link type.
  1379. Link type | Available information
  1380. -------------------+------------------------------------------------------
  1381. bbdb | %:type %:name %:company
  1382. vm, wl, mh, rmail | %:type %:subject %:message-id
  1383. | %:from %:fromname %:fromaddress
  1384. | %:to %:toname %:toaddress
  1385. | %:fromto (either \"to NAME\" or \"from NAME\")
  1386. gnus | %:group, for messages also all email fields
  1387. w3, w3m | %:type %:url
  1388. info | %:type %:file %:node
  1389. calendar | %:type %:date"
  1390. :group 'org-remember
  1391. :get (lambda (var) ; Make sure all entries have 5 elements
  1392. (mapcar (lambda (x)
  1393. (if (not (stringp (car x))) (setq x (cons "" x)))
  1394. (cond ((= (length x) 4) (append x '("")))
  1395. ((= (length x) 3) (append x '("" "")))
  1396. (t x)))
  1397. (default-value var)))
  1398. :type '(repeat
  1399. :tag "enabled"
  1400. (list :value ("" ?a "\n" nil nil)
  1401. (string :tag "Name")
  1402. (character :tag "Selection Key")
  1403. (string :tag "Template")
  1404. (choice
  1405. (file :tag "Destination file")
  1406. (const :tag "Prompt for file" nil))
  1407. (choice
  1408. (string :tag "Destination headline")
  1409. (const :tag "Selection interface for heading")))))
  1410. (defcustom org-reverse-note-order nil
  1411. "Non-nil means, store new notes at the beginning of a file or entry.
  1412. When nil, new notes will be filed to the end of a file or entry.
  1413. This can also be a list with cons cells of regular expressions that
  1414. are matched against file names, and values."
  1415. :group 'org-remember
  1416. :type '(choice
  1417. (const :tag "Reverse always" t)
  1418. (const :tag "Reverse never" nil)
  1419. (repeat :tag "By file name regexp"
  1420. (cons regexp boolean))))
  1421. (defcustom org-refile-targets nil
  1422. "Targets for refiling entries with \\[org-refile].
  1423. This is list of cons cells. Each cell contains:
  1424. - a specification of the files to be considered, either a list of files,
  1425. or a symbol whose function or value fields will be used to retrieve
  1426. a file name or a list of file names. Nil means, refile to a different
  1427. heading in the current buffer.
  1428. - A specification of how to find candidate refile targets. This may be
  1429. any of
  1430. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1431. This tag has to be present in all target headlines, inheritance will
  1432. not be considered.
  1433. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1434. todo keyword.
  1435. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1436. headlines that are refiling targets.
  1437. - a cons cell (:level . N). Any headline of level N is considered a target.
  1438. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1439. ;; FIXME: what if there are a var and func with same name???
  1440. :group 'org-remember
  1441. :type '(repeat
  1442. (cons
  1443. (choice :value org-agenda-files
  1444. (const :tag "All agenda files" org-agenda-files)
  1445. (const :tag "Current buffer" nil)
  1446. (function) (variable) (file))
  1447. (choice :tag "Identify target headline by"
  1448. (cons :tag "Specific tag" (const :tag) (string))
  1449. (cons :tag "TODO keyword" (const :todo) (string))
  1450. (cons :tag "Regular expression" (const :regexp) (regexp))
  1451. (cons :tag "Level number" (const :level) (integer))
  1452. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1453. (defcustom org-refile-use-outline-path nil
  1454. "Non-nil means, provide refile targets as paths.
  1455. So a level 3 headline will be available as level1/level2/level3.
  1456. When the value is `file', also include the file name (without directory)
  1457. into the path. When `full-file-path', include the full file path."
  1458. :group 'org-remember
  1459. :type '(choice
  1460. (const :tag "Not" nil)
  1461. (const :tag "Yes" t)
  1462. (const :tag "Start with file name" file)
  1463. (const :tag "Start with full file path" full-file-path)))
  1464. (defgroup org-todo nil
  1465. "Options concerning TODO items in Org-mode."
  1466. :tag "Org TODO"
  1467. :group 'org)
  1468. (defgroup org-progress nil
  1469. "Options concerning Progress logging in Org-mode."
  1470. :tag "Org Progress"
  1471. :group 'org-time)
  1472. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1473. "List of TODO entry keyword sequences and their interpretation.
  1474. \\<org-mode-map>This is a list of sequences.
  1475. Each sequence starts with a symbol, either `sequence' or `type',
  1476. indicating if the keywords should be interpreted as a sequence of
  1477. action steps, or as different types of TODO items. The first
  1478. keywords are states requiring action - these states will select a headline
  1479. for inclusion into the global TODO list Org-mode produces. If one of
  1480. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1481. signify that no further action is necessary. If \"|\" is not found,
  1482. the last keyword is treated as the only DONE state of the sequence.
  1483. The command \\[org-todo] cycles an entry through these states, and one
  1484. additional state where no keyword is present. For details about this
  1485. cycling, see the manual.
  1486. TODO keywords and interpretation can also be set on a per-file basis with
  1487. the special #+SEQ_TODO and #+TYP_TODO lines.
  1488. Each keyword can optionally specify a character for fast state selection
  1489. \(in combination with the variable `org-use-fast-todo-selection')
  1490. and specifiers for state change logging, using the same syntax
  1491. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1492. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1493. indicates to record a time stamp each time this state is selected.
  1494. \"WAIT(w@)\" says that the user should in addition be prompted for a
  1495. note, and \"WAIT(w@/@)\" says that a note should be taken both when
  1496. entering and when leaving this state.
  1497. For backward compatibility, this variable may also be just a list
  1498. of keywords - in this case the interptetation (sequence or type) will be
  1499. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1500. :group 'org-todo
  1501. :group 'org-keywords
  1502. :type '(choice
  1503. (repeat :tag "Old syntax, just keywords"
  1504. (string :tag "Keyword"))
  1505. (repeat :tag "New syntax"
  1506. (cons
  1507. (choice
  1508. :tag "Interpretation"
  1509. (const :tag "Sequence (cycling hits every state)" sequence)
  1510. (const :tag "Type (cycling directly to DONE)" type))
  1511. (repeat
  1512. (string :tag "Keyword"))))))
  1513. (defvar org-todo-keywords-1 nil
  1514. "All TODO and DONE keywords active in a buffer.")
  1515. (make-variable-buffer-local 'org-todo-keywords-1)
  1516. (defvar org-todo-keywords-for-agenda nil)
  1517. (defvar org-done-keywords-for-agenda nil)
  1518. (defvar org-not-done-keywords nil)
  1519. (make-variable-buffer-local 'org-not-done-keywords)
  1520. (defvar org-done-keywords nil)
  1521. (make-variable-buffer-local 'org-done-keywords)
  1522. (defvar org-todo-heads nil)
  1523. (make-variable-buffer-local 'org-todo-heads)
  1524. (defvar org-todo-sets nil)
  1525. (make-variable-buffer-local 'org-todo-sets)
  1526. (defvar org-todo-log-states nil)
  1527. (make-variable-buffer-local 'org-todo-log-states)
  1528. (defvar org-todo-kwd-alist nil)
  1529. (make-variable-buffer-local 'org-todo-kwd-alist)
  1530. (defvar org-todo-key-alist nil)
  1531. (make-variable-buffer-local 'org-todo-key-alist)
  1532. (defvar org-todo-key-trigger nil)
  1533. (make-variable-buffer-local 'org-todo-key-trigger)
  1534. (defcustom org-todo-interpretation 'sequence
  1535. "Controls how TODO keywords are interpreted.
  1536. This variable is in principle obsolete and is only used for
  1537. backward compatibility, if the interpretation of todo keywords is
  1538. not given already in `org-todo-keywords'. See that variable for
  1539. more information."
  1540. :group 'org-todo
  1541. :group 'org-keywords
  1542. :type '(choice (const sequence)
  1543. (const type)))
  1544. (defcustom org-use-fast-todo-selection 'prefix
  1545. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1546. This variable describes if and under what circumstances the cycling
  1547. mechanism for TODO keywords will be replaced by a single-key, direct
  1548. selection scheme.
  1549. When nil, fast selection is never used.
  1550. When the symbol `prefix', it will be used when `org-todo' is called with
  1551. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1552. in an agenda buffer.
  1553. When t, fast selection is used by default. In this case, the prefix
  1554. argument forces cycling instead.
  1555. In all cases, the special interface is only used if access keys have actually
  1556. been assigned by the user, i.e. if keywords in the configuration are followed
  1557. by a letter in parenthesis, like TODO(t)."
  1558. :group 'org-todo
  1559. :type '(choice
  1560. (const :tag "Never" nil)
  1561. (const :tag "By default" t)
  1562. (const :tag "Only with C-u C-c C-t" prefix)))
  1563. (defcustom org-after-todo-state-change-hook nil
  1564. "Hook which is run after the state of a TODO item was changed.
  1565. The new state (a string with a TODO keyword, or nil) is available in the
  1566. Lisp variable `state'."
  1567. :group 'org-todo
  1568. :type 'hook)
  1569. (defcustom org-log-done nil
  1570. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1571. When equal to the list (done), also prompt for a closing note.
  1572. This can also be configured on a per-file basis by adding one of
  1573. the following lines anywhere in the buffer:
  1574. #+STARTUP: logdone
  1575. #+STARTUP: lognotedone
  1576. #+STARTUP: nologdone"
  1577. :group 'org-todo
  1578. :group 'org-progress
  1579. :type '(choice
  1580. (const :tag "No logging" nil)
  1581. (const :tag "Record CLOSED timestamp" time)
  1582. (const :tag "Record CLOSED timestamp with closing note." note)))
  1583. ;; Normalize old uses of org-log-done.
  1584. (cond
  1585. ((eq org-log-done t) (setq org-log-done 'time))
  1586. ((and (listp org-log-done) (memq 'done org-log-done))
  1587. (setq org-log-done 'note)))
  1588. ;; FIXME: document
  1589. (defcustom org-log-note-clock-out nil
  1590. "Non-nil means, recored a note when clocking out of an item.
  1591. This can also be configured on a per-file basis by adding one of
  1592. the following lines anywhere in the buffer:
  1593. #+STARTUP: lognoteclock-out
  1594. #+STARTUP: nolognoteclock-out"
  1595. :group 'org-todo
  1596. :group 'org-progress
  1597. :type 'boolean)
  1598. (defcustom org-log-done-with-time t
  1599. "Non-nil means, the CLOSED time stamp will contain date and time.
  1600. When nil, only the date will be recorded."
  1601. :group 'org-progress
  1602. :type 'boolean)
  1603. (defcustom org-log-note-headings
  1604. '((done . "CLOSING NOTE %t")
  1605. (state . "State %-12s %t")
  1606. (clock-out . ""))
  1607. "Headings for notes added when clocking out or closing TODO items.
  1608. The value is an alist, with the car being a symbol indicating the note
  1609. context, and the cdr is the heading to be used. The heading may also be the
  1610. empty string.
  1611. %t in the heading will be replaced by a time stamp.
  1612. %s will be replaced by the new TODO state, in double quotes.
  1613. %u will be replaced by the user name.
  1614. %U will be replaced by the full user name."
  1615. :group 'org-todo
  1616. :group 'org-progress
  1617. :type '(list :greedy t
  1618. (cons (const :tag "Heading when closing an item" done) string)
  1619. (cons (const :tag
  1620. "Heading when changing todo state (todo sequence only)"
  1621. state) string)
  1622. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1623. (defcustom org-log-states-order-reversed t
  1624. "Non-nil means, the latest state change note will be directly after heading.
  1625. When nil, the notes will be orderer according to time."
  1626. :group 'org-todo
  1627. :group 'org-progress
  1628. :type 'boolean)
  1629. (defcustom org-log-repeat 'time
  1630. "Non-nil means, record moving through the DONE state when triggering repeat.
  1631. An auto-repeating tasks is immediately switched back to TODO when marked
  1632. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1633. the TODO keyword definition, or recording a cloing note by setting
  1634. `org-log-done', there will be no record of the task moving trhough DONE.
  1635. This variable forces taking a note anyway. Possible values are:
  1636. nil Don't force a record
  1637. time Record a time stamp
  1638. note Record a note
  1639. This option can also be set with on a per-file-basis with
  1640. #+STARTUP: logrepeat
  1641. #+STARTUP: lognoterepeat
  1642. #+STARTUP: nologrepeat
  1643. You can have local logging settings for a subtree by setting the LOGGING
  1644. property to one or more of these keywords."
  1645. :group 'org-todo
  1646. :group 'org-progress
  1647. :type '(choice
  1648. (const :tag "Don't force a record" nil)
  1649. (const :tag "Force recording the DONE state" time)
  1650. (const :tag "Force recording a note with the DONE state" note)))
  1651. (defcustom org-clock-into-drawer 2
  1652. "Should clocking info be wrapped into a drawer?
  1653. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1654. If necessary, the drawer will be created.
  1655. When nil, the drawer will not be created, but used when present.
  1656. When an integer and the number of clocking entries in an item
  1657. reaches or exceeds this number, a drawer will be created."
  1658. :group 'org-todo
  1659. :group 'org-progress
  1660. :type '(choice
  1661. (const :tag "Always" t)
  1662. (const :tag "Only when drawer exists" nil)
  1663. (integer :tag "When at least N clock entries")))
  1664. (defcustom org-clock-out-when-done t
  1665. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1666. Nil means, clock will keep running until stopped explicitly with
  1667. `C-c C-x C-o', or until the clock is started in a different item."
  1668. :group 'org-progress
  1669. :type 'boolean)
  1670. (defcustom org-clock-in-switch-to-state nil
  1671. "Set task to a special todo state while clocking it.
  1672. The value should be the state to which the entry should be switched."
  1673. :group 'org-progress
  1674. :group 'org-todo
  1675. :type '(choice
  1676. (const :tag "Don't force a state" nil)
  1677. (string :tag "State")))
  1678. (defgroup org-priorities nil
  1679. "Priorities in Org-mode."
  1680. :tag "Org Priorities"
  1681. :group 'org-todo)
  1682. (defcustom org-highest-priority ?A
  1683. "The highest priority of TODO items. A character like ?A, ?B etc.
  1684. Must have a smaller ASCII number than `org-lowest-priority'."
  1685. :group 'org-priorities
  1686. :type 'character)
  1687. (defcustom org-lowest-priority ?C
  1688. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1689. Must have a larger ASCII number than `org-highest-priority'."
  1690. :group 'org-priorities
  1691. :type 'character)
  1692. (defcustom org-default-priority ?B
  1693. "The default priority of TODO items.
  1694. This is the priority an item get if no explicit priority is given."
  1695. :group 'org-priorities
  1696. :type 'character)
  1697. (defcustom org-priority-start-cycle-with-default t
  1698. "Non-nil means, start with default priority when starting to cycle.
  1699. When this is nil, the first step in the cycle will be (depending on the
  1700. command used) one higher or lower that the default priority."
  1701. :group 'org-priorities
  1702. :type 'boolean)
  1703. (defgroup org-time nil
  1704. "Options concerning time stamps and deadlines in Org-mode."
  1705. :tag "Org Time"
  1706. :group 'org)
  1707. (defcustom org-insert-labeled-timestamps-at-point nil
  1708. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1709. When nil, these labeled time stamps are forces into the second line of an
  1710. entry, just after the headline. When scheduling from the global TODO list,
  1711. the time stamp will always be forced into the second line."
  1712. :group 'org-time
  1713. :type 'boolean)
  1714. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1715. "Formats for `format-time-string' which are used for time stamps.
  1716. It is not recommended to change this constant.")
  1717. (defcustom org-time-stamp-rounding-minutes 0
  1718. "Number of minutes to round time stamps to upon insertion.
  1719. When zero, insert the time unmodified. Useful rounding numbers
  1720. should be factors of 60, so for example 5, 10, 15.
  1721. When this is not zero, you can still force an exact time-stamp by using
  1722. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1723. :group 'org-time
  1724. :type 'integer)
  1725. (defcustom org-display-custom-times nil
  1726. "Non-nil means, overlay custom formats over all time stamps.
  1727. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1728. To turn this on on a per-file basis, insert anywhere in the file:
  1729. #+STARTUP: customtime"
  1730. :group 'org-time
  1731. :set 'set-default
  1732. :type 'sexp)
  1733. (make-variable-buffer-local 'org-display-custom-times)
  1734. (defcustom org-time-stamp-custom-formats
  1735. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1736. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1737. These are overlayed over the default ISO format if the variable
  1738. `org-display-custom-times' is set. Time like %H:%M should be at the
  1739. end of the second format."
  1740. :group 'org-time
  1741. :type 'sexp)
  1742. (defun org-time-stamp-format (&optional long inactive)
  1743. "Get the right format for a time string."
  1744. (let ((f (if long (cdr org-time-stamp-formats)
  1745. (car org-time-stamp-formats))))
  1746. (if inactive
  1747. (concat "[" (substring f 1 -1) "]")
  1748. f)))
  1749. (defcustom org-read-date-prefer-future t
  1750. "Non-nil means, assume future for incomplete date input from user.
  1751. This affects the following situations:
  1752. 1. The user gives a day, but no month.
  1753. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1754. read this as the third of *next* month. However, if you enter \"17\",
  1755. it will be considered as *this* month.
  1756. 2. The user gives a month but not a year.
  1757. For example, if it is april and you enter \"feb 2\", this will be read
  1758. as feb 2, *next* year. \"May 5\", however, will be this year.
  1759. When this option is nil, the current month and year will always be used
  1760. as defaults."
  1761. :group 'org-time
  1762. :type 'boolean)
  1763. (defcustom org-read-date-display-live t
  1764. "Non-nil means, display current interpretation of date prompt live.
  1765. This display will be in an overlay, in the minibuffer."
  1766. :group 'org-time
  1767. :type 'boolean)
  1768. (defcustom org-read-date-popup-calendar t
  1769. "Non-nil means, pop up a calendar when prompting for a date.
  1770. In the calendar, the date can be selected with mouse-1. However, the
  1771. minibuffer will also be active, and you can simply enter the date as well.
  1772. When nil, only the minibuffer will be available."
  1773. :group 'org-time
  1774. :type 'boolean)
  1775. (if (fboundp 'defvaralias)
  1776. (defvaralias 'org-popup-calendar-for-date-prompt
  1777. 'org-read-date-popup-calendar))
  1778. (defcustom org-extend-today-until 0
  1779. "The hour when your day really ends.
  1780. This has influence for the following applications:
  1781. - When switching the agenda to \"today\". It it is still earlier than
  1782. the time given here, the day recognized as TODAY is actually yesterday.
  1783. - When a date is read from the user and it is still before the time given
  1784. here, the current date and time will be assumed to be yesterday, 23:59.
  1785. FIXME:
  1786. IMPORTANT: This is still a very experimental feature, it may disappear
  1787. again or it may be extended to mean more things."
  1788. :group 'org-time
  1789. :type 'number)
  1790. (defcustom org-edit-timestamp-down-means-later nil
  1791. "Non-nil means, S-down will increase the time in a time stamp.
  1792. When nil, S-up will increase."
  1793. :group 'org-time
  1794. :type 'boolean)
  1795. (defcustom org-calendar-follow-timestamp-change t
  1796. "Non-nil means, make the calendar window follow timestamp changes.
  1797. When a timestamp is modified and the calendar window is visible, it will be
  1798. moved to the new date."
  1799. :group 'org-time
  1800. :type 'boolean)
  1801. (defcustom org-clock-heading-function nil
  1802. "When non-nil, should be a function to create `org-clock-heading'.
  1803. This is the string shown in the mode line when a clock is running.
  1804. The function is called with point at the beginning of the headline."
  1805. :group 'org-time ; FIXME: Should we have a separate group????
  1806. :type 'function)
  1807. (defgroup org-tags nil
  1808. "Options concerning tags in Org-mode."
  1809. :tag "Org Tags"
  1810. :group 'org)
  1811. (defcustom org-tag-alist nil
  1812. "List of tags allowed in Org-mode files.
  1813. When this list is nil, Org-mode will base TAG input on what is already in the
  1814. buffer.
  1815. The value of this variable is an alist, the car of each entry must be a
  1816. keyword as a string, the cdr may be a character that is used to select
  1817. that tag through the fast-tag-selection interface.
  1818. See the manual for details."
  1819. :group 'org-tags
  1820. :type '(repeat
  1821. (choice
  1822. (cons (string :tag "Tag name")
  1823. (character :tag "Access char"))
  1824. (const :tag "Start radio group" (:startgroup))
  1825. (const :tag "End radio group" (:endgroup)))))
  1826. (defcustom org-use-fast-tag-selection 'auto
  1827. "Non-nil means, use fast tag selection scheme.
  1828. This is a special interface to select and deselect tags with single keys.
  1829. When nil, fast selection is never used.
  1830. When the symbol `auto', fast selection is used if and only if selection
  1831. characters for tags have been configured, either through the variable
  1832. `org-tag-alist' or through a #+TAGS line in the buffer.
  1833. When t, fast selection is always used and selection keys are assigned
  1834. automatically if necessary."
  1835. :group 'org-tags
  1836. :type '(choice
  1837. (const :tag "Always" t)
  1838. (const :tag "Never" nil)
  1839. (const :tag "When selection characters are configured" 'auto)))
  1840. (defcustom org-fast-tag-selection-single-key nil
  1841. "Non-nil means, fast tag selection exits after first change.
  1842. When nil, you have to press RET to exit it.
  1843. During fast tag selection, you can toggle this flag with `C-c'.
  1844. This variable can also have the value `expert'. In this case, the window
  1845. displaying the tags menu is not even shown, until you press C-c again."
  1846. :group 'org-tags
  1847. :type '(choice
  1848. (const :tag "No" nil)
  1849. (const :tag "Yes" t)
  1850. (const :tag "Expert" expert)))
  1851. (defvar org-fast-tag-selection-include-todo nil
  1852. "Non-nil means, fast tags selection interface will also offer TODO states.
  1853. This is an undocumented feature, you should not rely on it.")
  1854. (defcustom org-tags-column -80
  1855. "The column to which tags should be indented in a headline.
  1856. If this number is positive, it specifies the column. If it is negative,
  1857. it means that the tags should be flushright to that column. For example,
  1858. -80 works well for a normal 80 character screen."
  1859. :group 'org-tags
  1860. :type 'integer)
  1861. (defcustom org-auto-align-tags t
  1862. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1863. These operations change the length of a headline and therefore shift
  1864. the tags around. With this options turned on, after each such operation
  1865. the tags are again aligned to `org-tags-column'."
  1866. :group 'org-tags
  1867. :type 'boolean)
  1868. (defcustom org-use-tag-inheritance t
  1869. "Non-nil means, tags in levels apply also for sublevels.
  1870. When nil, only the tags directly given in a specific line apply there.
  1871. If you turn off this option, you very likely want to turn on the
  1872. companion option `org-tags-match-list-sublevels'."
  1873. :group 'org-tags
  1874. :type 'boolean)
  1875. (defcustom org-tags-match-list-sublevels nil
  1876. "Non-nil means list also sublevels of headlines matching tag search.
  1877. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1878. the sublevels of a headline matching a tag search often also match
  1879. the same search. Listing all of them can create very long lists.
  1880. Setting this variable to nil causes subtrees of a match to be skipped.
  1881. This option is off by default, because inheritance in on. If you turn
  1882. inheritance off, you very likely want to turn this option on.
  1883. As a special case, if the tag search is restricted to TODO items, the
  1884. value of this variable is ignored and sublevels are always checked, to
  1885. make sure all corresponding TODO items find their way into the list."
  1886. :group 'org-tags
  1887. :type 'boolean)
  1888. (defvar org-tags-history nil
  1889. "History of minibuffer reads for tags.")
  1890. (defvar org-last-tags-completion-table nil
  1891. "The last used completion table for tags.")
  1892. (defvar org-after-tags-change-hook nil
  1893. "Hook that is run after the tags in a line have changed.")
  1894. (defgroup org-properties nil
  1895. "Options concerning properties in Org-mode."
  1896. :tag "Org Properties"
  1897. :group 'org)
  1898. (defcustom org-property-format "%-10s %s"
  1899. "How property key/value pairs should be formatted by `indent-line'.
  1900. When `indent-line' hits a property definition, it will format the line
  1901. according to this format, mainly to make sure that the values are
  1902. lined-up with respect to each other."
  1903. :group 'org-properties
  1904. :type 'string)
  1905. (defcustom org-use-property-inheritance nil
  1906. "Non-nil means, properties apply also for sublevels.
  1907. This setting is only relevant during property searches, not when querying
  1908. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1909. you need to call `org-entry-get' with the inheritance flag.
  1910. Turning this on can cause significant overhead when doing a search, so
  1911. this is turned off by default.
  1912. When nil, only the properties directly given in the current entry count.
  1913. The value may also be a list of properties that shouldhave inheritance.
  1914. However, note that some special properties use inheritance under special
  1915. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1916. and the properties ending in \"_ALL\" when they are used as descriptor
  1917. for valid values of a property."
  1918. :group 'org-properties
  1919. :type '(choice
  1920. (const :tag "Not" nil)
  1921. (const :tag "Always" nil)
  1922. (repeat :tag "Specific properties" (string :tag "Property"))))
  1923. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1924. "The default column format, if no other format has been defined.
  1925. This variable can be set on the per-file basis by inserting a line
  1926. #+COLUMNS: %25ITEM ....."
  1927. :group 'org-properties
  1928. :type 'string)
  1929. (defcustom org-global-properties nil
  1930. "List of property/value pairs that can be inherited by any entry.
  1931. You can set buffer-local values for this by adding lines like
  1932. #+PROPERTY: NAME VALUE"
  1933. :group 'org-properties
  1934. :type '(repeat
  1935. (cons (string :tag "Property")
  1936. (string :tag "Value"))))
  1937. (defvar org-local-properties nil
  1938. "List of property/value pairs that can be inherited by any entry.
  1939. Valid for the current buffer.
  1940. This variable is populated from #+PROPERTY lines.")
  1941. (defgroup org-agenda nil
  1942. "Options concerning agenda views in Org-mode."
  1943. :tag "Org Agenda"
  1944. :group 'org)
  1945. (defvar org-category nil
  1946. "Variable used by org files to set a category for agenda display.
  1947. Such files should use a file variable to set it, for example
  1948. # -*- mode: org; org-category: \"ELisp\"
  1949. or contain a special line
  1950. #+CATEGORY: ELisp
  1951. If the file does not specify a category, then file's base name
  1952. is used instead.")
  1953. (make-variable-buffer-local 'org-category)
  1954. (defcustom org-agenda-files nil
  1955. "The files to be used for agenda display.
  1956. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1957. \\[org-remove-file]. You can also use customize to edit the list.
  1958. If an entry is a directory, all files in that directory that are matched by
  1959. `org-agenda-file-regexp' will be part of the file list.
  1960. If the value of the variable is not a list but a single file name, then
  1961. the list of agenda files is actually stored and maintained in that file, one
  1962. agenda file per line."
  1963. :group 'org-agenda
  1964. :type '(choice
  1965. (repeat :tag "List of files and directories" file)
  1966. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1967. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1968. "Regular expression to match files for `org-agenda-files'.
  1969. If any element in the list in that variable contains a directory instead
  1970. of a normal file, all files in that directory that are matched by this
  1971. regular expression will be included."
  1972. :group 'org-agenda
  1973. :type 'regexp)
  1974. (defcustom org-agenda-skip-unavailable-files nil
  1975. "t means to just skip non-reachable files in `org-agenda-files'.
  1976. Nil means to remove them, after a query, from the list."
  1977. :group 'org-agenda
  1978. :type 'boolean)
  1979. (defcustom org-agenda-text-search-extra-files nil
  1980. "List of extra files to be searched by text search commands.
  1981. These files will be search in addition to the agenda files bu the
  1982. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1983. Note that these files will only be searched for text search commands,
  1984. not for the other agenda views like todo lists, tag earches or the weekly
  1985. agenda. This variable is intended to list notes and possibly archive files
  1986. that should also be searched by these two commands."
  1987. :group 'org-agenda
  1988. :type '(repeat file))
  1989. (if (fboundp 'defvaralias)
  1990. (defvaralias 'org-agenda-multi-occur-extra-files
  1991. 'org-agenda-text-search-extra-files))
  1992. (defcustom org-agenda-confirm-kill 1
  1993. "When set, remote killing from the agenda buffer needs confirmation.
  1994. When t, a confirmation is always needed. When a number N, confirmation is
  1995. only needed when the text to be killed contains more than N non-white lines."
  1996. :group 'org-agenda
  1997. :type '(choice
  1998. (const :tag "Never" nil)
  1999. (const :tag "Always" t)
  2000. (number :tag "When more than N lines")))
  2001. (defcustom org-calendar-to-agenda-key [?c]
  2002. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2003. The command `org-calendar-goto-agenda' will be bound to this key. The
  2004. default is the character `c' because then `c' can be used to switch back and
  2005. forth between agenda and calendar."
  2006. :group 'org-agenda
  2007. :type 'sexp)
  2008. (defcustom org-agenda-compact-blocks nil
  2009. "Non-nil means, make the block agenda more compact.
  2010. This is done by leaving out unnecessary lines."
  2011. :group 'org-agenda
  2012. :type nil)
  2013. (defgroup org-agenda-export nil
  2014. "Options concerning exporting agenda views in Org-mode."
  2015. :tag "Org Agenda Export"
  2016. :group 'org-agenda)
  2017. (defcustom org-agenda-with-colors t
  2018. "Non-nil means, use colors in agenda views."
  2019. :group 'org-agenda-export
  2020. :type 'boolean)
  2021. (defcustom org-agenda-exporter-settings nil
  2022. "Alist of variable/value pairs that should be active during agenda export.
  2023. This is a good place to set uptions for ps-print and for htmlize."
  2024. :group 'org-agenda-export
  2025. :type '(repeat
  2026. (list
  2027. (variable)
  2028. (sexp :tag "Value"))))
  2029. (defcustom org-agenda-export-html-style ""
  2030. "The style specification for exported HTML Agenda files.
  2031. If this variable contains a string, it will replace the default <style>
  2032. section as produced by `htmlize'.
  2033. Since there are different ways of setting style information, this variable
  2034. needs to contain the full HTML structure to provide a style, including the
  2035. surrounding HTML tags. The style specifications should include definitions
  2036. the fonts used by the agenda, here is an example:
  2037. <style type=\"text/css\">
  2038. p { font-weight: normal; color: gray; }
  2039. .org-agenda-structure {
  2040. font-size: 110%;
  2041. color: #003399;
  2042. font-weight: 600;
  2043. }
  2044. .org-todo {
  2045. color: #cc6666;Week-agenda:
  2046. font-weight: bold;
  2047. }
  2048. .org-done {
  2049. color: #339933;
  2050. }
  2051. .title { text-align: center; }
  2052. .todo, .deadline { color: red; }
  2053. .done { color: green; }
  2054. </style>
  2055. or, if you want to keep the style in a file,
  2056. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2057. As the value of this option simply gets inserted into the HTML <head> header,
  2058. you can \"misuse\" it to also add other text to the header. However,
  2059. <style>...</style> is required, if not present the variable will be ignored."
  2060. :group 'org-agenda-export
  2061. :group 'org-export-html
  2062. :type 'string)
  2063. (defgroup org-agenda-custom-commands nil
  2064. "Options concerning agenda views in Org-mode."
  2065. :tag "Org Agenda Custom Commands"
  2066. :group 'org-agenda)
  2067. (defcustom org-agenda-custom-commands nil
  2068. "Custom commands for the agenda.
  2069. These commands will be offered on the splash screen displayed by the
  2070. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  2071. (key desc type match options files)
  2072. key The key (one or more characters as a string) to be associated
  2073. with the command.
  2074. desc A description of the commend, when omitted or nil, a default
  2075. description is built using MATCH.
  2076. type The command type, any of the following symbols:
  2077. agenda The daily/weekly agenda.
  2078. todo Entries with a specific TODO keyword, in all agenda files.
  2079. search Entries containing search words entry or headline.
  2080. tags Tags/Property/TODO match in all agenda files.
  2081. tags-todo Tags/P/T match in all agenda files, TODO entries only.
  2082. todo-tree Sparse tree of specific TODO keyword in *current* file.
  2083. tags-tree Sparse tree with all tags matches in *current* file.
  2084. occur-tree Occur sparse tree for *current* file.
  2085. ... A user-defined function.
  2086. match What to search for:
  2087. - a single keyword for TODO keyword searches
  2088. - a tags match expression for tags searches
  2089. - a regular expression for occur searches
  2090. options A list of option settings, similar to that in a let form, so like
  2091. this: ((opt1 val1) (opt2 val2) ...)
  2092. files A list of files file to write the produced agenda buffer to
  2093. with the command `org-store-agenda-views'.
  2094. If a file name ends in \".html\", an HTML version of the buffer
  2095. is written out. If it ends in \".ps\", a postscript version is
  2096. produced. Otherwide, only the plain text is written to the file.
  2097. You can also define a set of commands, to create a composite agenda buffer.
  2098. In this case, an entry looks like this:
  2099. (key desc (cmd1 cmd2 ...) general-options file)
  2100. where
  2101. desc A description string to be displayed in the dispatcher menu.
  2102. cmd An agenda command, similar to the above. However, tree commands
  2103. are no allowed, but instead you can get agenda and global todo list.
  2104. So valid commands for a set are:
  2105. (agenda)
  2106. (alltodo)
  2107. (stuck)
  2108. (todo \"match\" options files)
  2109. (search \"match\" options files)
  2110. (tags \"match\" options files)
  2111. (tags-todo \"match\" options files)
  2112. Each command can carry a list of options, and another set of options can be
  2113. given for the whole set of commands. Individual command options take
  2114. precedence over the general options.
  2115. When using several characters as key to a command, the first characters
  2116. are prefix commands. For the dispatcher to display useful information, you
  2117. should provide a description for the prefix, like
  2118. (setq org-agenda-custom-commands
  2119. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2120. (\"hl\" tags \"+HOME+Lisa\")
  2121. (\"hp\" tags \"+HOME+Peter\")
  2122. (\"hk\" tags \"+HOME+Kim\")))"
  2123. :group 'org-agenda-custom-commands
  2124. :type '(repeat
  2125. (choice :value ("a" "" tags "" nil)
  2126. (list :tag "Single command"
  2127. (string :tag "Access Key(s) ")
  2128. (option (string :tag "Description"))
  2129. (choice
  2130. (const :tag "Agenda" agenda)
  2131. (const :tag "TODO list" alltodo)
  2132. (const :tag "Search words" search)
  2133. (const :tag "Stuck projects" stuck)
  2134. (const :tag "Tags search (all agenda files)" tags)
  2135. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2136. (const :tag "TODO keyword search (all agenda files)" todo)
  2137. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2138. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2139. (const :tag "Occur tree (current buffer)" occur-tree)
  2140. (sexp :tag "Other, user-defined function"))
  2141. (string :tag "Match")
  2142. (repeat :tag "Local options"
  2143. (list (variable :tag "Option") (sexp :tag "Value")))
  2144. (option (repeat :tag "Export" (file :tag "Export to"))))
  2145. (list :tag "Command series, all agenda files"
  2146. (string :tag "Access Key(s)")
  2147. (string :tag "Description ")
  2148. (repeat
  2149. (choice
  2150. (const :tag "Agenda" (agenda))
  2151. (const :tag "TODO list" (alltodo))
  2152. (list :tag "Search words"
  2153. (const :format "" search)
  2154. (string :tag "Match")
  2155. (repeat :tag "Local options"
  2156. (list (variable :tag "Option")
  2157. (sexp :tag "Value"))))
  2158. (const :tag "Stuck projects" (stuck))
  2159. (list :tag "Tags search"
  2160. (const :format "" tags)
  2161. (string :tag "Match")
  2162. (repeat :tag "Local options"
  2163. (list (variable :tag "Option")
  2164. (sexp :tag "Value"))))
  2165. (list :tag "Tags search, TODO entries only"
  2166. (const :format "" tags-todo)
  2167. (string :tag "Match")
  2168. (repeat :tag "Local options"
  2169. (list (variable :tag "Option")
  2170. (sexp :tag "Value"))))
  2171. (list :tag "TODO keyword search"
  2172. (const :format "" todo)
  2173. (string :tag "Match")
  2174. (repeat :tag "Local options"
  2175. (list (variable :tag "Option")
  2176. (sexp :tag "Value"))))
  2177. (list :tag "Other, user-defined function"
  2178. (symbol :tag "function")
  2179. (string :tag "Match")
  2180. (repeat :tag "Local options"
  2181. (list (variable :tag "Option")
  2182. (sexp :tag "Value"))))))
  2183. (repeat :tag "General options"
  2184. (list (variable :tag "Option")
  2185. (sexp :tag "Value")))
  2186. (option (repeat :tag "Export" (file :tag "Export to"))))
  2187. (cons :tag "Prefix key documentation"
  2188. (string :tag "Access Key(s)")
  2189. (string :tag "Description ")))))
  2190. (defcustom org-agenda-query-register ?o
  2191. "The register holding the current query string.
  2192. The prupose of this is that if you construct a query string interactively,
  2193. you can then use it to define a custom command."
  2194. :group 'org-agenda-custom-commands
  2195. :type 'character)
  2196. (defcustom org-stuck-projects
  2197. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2198. "How to identify stuck projects.
  2199. This is a list of four items:
  2200. 1. A tags/todo matcher string that is used to identify a project.
  2201. The entire tree below a headline matched by this is considered one project.
  2202. 2. A list of TODO keywords identifying non-stuck projects.
  2203. If the project subtree contains any headline with one of these todo
  2204. keywords, the project is considered to be not stuck. If you specify
  2205. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2206. 3. A list of tags identifying non-stuck projects.
  2207. If the project subtree contains any headline with one of these tags,
  2208. the project is considered to be not stuck. If you specify \"*\" as
  2209. a tag, any tag will mark the project unstuck.
  2210. 4. An arbitrary regular expression matching non-stuck projects.
  2211. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2212. or `C-c a #' to produce the list."
  2213. :group 'org-agenda-custom-commands
  2214. :type '(list
  2215. (string :tag "Tags/TODO match to identify a project")
  2216. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2217. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2218. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2219. (defgroup org-agenda-skip nil
  2220. "Options concerning skipping parts of agenda files."
  2221. :tag "Org Agenda Skip"
  2222. :group 'org-agenda)
  2223. (defcustom org-agenda-todo-list-sublevels t
  2224. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2225. When nil, the sublevels of a TODO entry are not checked, resulting in
  2226. potentially much shorter TODO lists."
  2227. :group 'org-agenda-skip
  2228. :group 'org-todo
  2229. :type 'boolean)
  2230. (defcustom org-agenda-todo-ignore-with-date nil
  2231. "Non-nil means, don't show entries with a date in the global todo list.
  2232. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2233. but don't want them to show up in the TODO list.
  2234. When this is set, it also covers deadlines and scheduled items, the settings
  2235. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2236. will be ignored."
  2237. :group 'org-agenda-skip
  2238. :group 'org-todo
  2239. :type 'boolean)
  2240. (defcustom org-agenda-todo-ignore-scheduled nil
  2241. "Non-nil means, don't show scheduled entries in the global todo list.
  2242. The idea behind this is that by scheduling it, you have already taken care
  2243. of this item.
  2244. See also `org-agenda-todo-ignore-with-date'."
  2245. :group 'org-agenda-skip
  2246. :group 'org-todo
  2247. :type 'boolean)
  2248. (defcustom org-agenda-todo-ignore-deadlines nil
  2249. "Non-nil means, don't show near deadline entries in the global todo list.
  2250. Near means closer than `org-deadline-warning-days' days.
  2251. The idea behind this is that such items will appear in the agenda anyway.
  2252. See also `org-agenda-todo-ignore-with-date'."
  2253. :group 'org-agenda-skip
  2254. :group 'org-todo
  2255. :type 'boolean)
  2256. (defcustom org-agenda-skip-scheduled-if-done nil
  2257. "Non-nil means don't show scheduled items in agenda when they are done.
  2258. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2259. it applies only to the actual date of the scheduling. Warnings about
  2260. an item with a past scheduling dates are always turned off when the item
  2261. is DONE."
  2262. :group 'org-agenda-skip
  2263. :type 'boolean)
  2264. (defcustom org-agenda-skip-deadline-if-done nil
  2265. "Non-nil means don't show deadines when the corresponding item is done.
  2266. When nil, the deadline is still shown and should give you a happy feeling.
  2267. This is relevant for the daily/weekly agenda. And it applied only to the
  2268. actualy date of the deadline. Warnings about approching and past-due
  2269. deadlines are always turned off when the item is DONE."
  2270. :group 'org-agenda-skip
  2271. :type 'boolean)
  2272. (defcustom org-agenda-skip-timestamp-if-done nil
  2273. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2274. :group 'org-agenda-skip
  2275. :type 'boolean)
  2276. (defcustom org-timeline-show-empty-dates 3
  2277. "Non-nil means, `org-timeline' also shows dates without an entry.
  2278. When nil, only the days which actually have entries are shown.
  2279. When t, all days between the first and the last date are shown.
  2280. When an integer, show also empty dates, but if there is a gap of more than
  2281. N days, just insert a special line indicating the size of the gap."
  2282. :group 'org-agenda-skip
  2283. :type '(choice
  2284. (const :tag "None" nil)
  2285. (const :tag "All" t)
  2286. (number :tag "at most")))
  2287. (defgroup org-agenda-startup nil
  2288. "Options concerning initial settings in the Agenda in Org Mode."
  2289. :tag "Org Agenda Startup"
  2290. :group 'org-agenda)
  2291. (defcustom org-finalize-agenda-hook nil
  2292. "Hook run just before displaying an agenda buffer."
  2293. :group 'org-agenda-startup
  2294. :type 'hook)
  2295. (defcustom org-agenda-mouse-1-follows-link nil
  2296. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2297. A longer mouse click will still set point. Does not wortk on XEmacs.
  2298. Needs to be set before org.el is loaded."
  2299. :group 'org-agenda-startup
  2300. :type 'boolean)
  2301. (defcustom org-agenda-start-with-follow-mode nil
  2302. "The initial value of follow-mode in a newly created agenda window."
  2303. :group 'org-agenda-startup
  2304. :type 'boolean)
  2305. (defgroup org-agenda-windows nil
  2306. "Options concerning the windows used by the Agenda in Org Mode."
  2307. :tag "Org Agenda Windows"
  2308. :group 'org-agenda)
  2309. (defcustom org-agenda-window-setup 'reorganize-frame
  2310. "How the agenda buffer should be displayed.
  2311. Possible values for this option are:
  2312. current-window Show agenda in the current window, keeping all other windows.
  2313. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2314. other-window Use `switch-to-buffer-other-window' to display agenda.
  2315. reorganize-frame Show only two windows on the current frame, the current
  2316. window and the agenda.
  2317. See also the variable `org-agenda-restore-windows-after-quit'."
  2318. :group 'org-agenda-windows
  2319. :type '(choice
  2320. (const current-window)
  2321. (const other-frame)
  2322. (const other-window)
  2323. (const reorganize-frame)))
  2324. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2325. "The min and max height of the agenda window as a fraction of frame height.
  2326. The value of the variable is a cons cell with two numbers between 0 and 1.
  2327. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2328. :group 'org-agenda-windows
  2329. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2330. (defcustom org-agenda-restore-windows-after-quit nil
  2331. "Non-nil means, restore window configuration open exiting agenda.
  2332. Before the window configuration is changed for displaying the agenda,
  2333. the current status is recorded. When the agenda is exited with
  2334. `q' or `x' and this option is set, the old state is restored. If
  2335. `org-agenda-window-setup' is `other-frame', the value of this
  2336. option will be ignored.."
  2337. :group 'org-agenda-windows
  2338. :type 'boolean)
  2339. (defcustom org-indirect-buffer-display 'other-window
  2340. "How should indirect tree buffers be displayed?
  2341. This applies to indirect buffers created with the commands
  2342. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2343. Valid values are:
  2344. current-window Display in the current window
  2345. other-window Just display in another window.
  2346. dedicated-frame Create one new frame, and re-use it each time.
  2347. new-frame Make a new frame each time. Note that in this case
  2348. previously-made indirect buffers are kept, and you need to
  2349. kill these buffers yourself."
  2350. :group 'org-structure
  2351. :group 'org-agenda-windows
  2352. :type '(choice
  2353. (const :tag "In current window" current-window)
  2354. (const :tag "In current frame, other window" other-window)
  2355. (const :tag "Each time a new frame" new-frame)
  2356. (const :tag "One dedicated frame" dedicated-frame)))
  2357. (defgroup org-agenda-daily/weekly nil
  2358. "Options concerning the daily/weekly agenda."
  2359. :tag "Org Agenda Daily/Weekly"
  2360. :group 'org-agenda)
  2361. (defcustom org-agenda-ndays 7
  2362. "Number of days to include in overview display.
  2363. Should be 1 or 7."
  2364. :group 'org-agenda-daily/weekly
  2365. :type 'number)
  2366. (defcustom org-agenda-start-on-weekday 1
  2367. "Non-nil means, start the overview always on the specified weekday.
  2368. 0 denotes Sunday, 1 denotes Monday etc.
  2369. When nil, always start on the current day."
  2370. :group 'org-agenda-daily/weekly
  2371. :type '(choice (const :tag "Today" nil)
  2372. (number :tag "Weekday No.")))
  2373. (defcustom org-agenda-show-all-dates t
  2374. "Non-nil means, `org-agenda' shows every day in the selected range.
  2375. When nil, only the days which actually have entries are shown."
  2376. :group 'org-agenda-daily/weekly
  2377. :type 'boolean)
  2378. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2379. "Format string for displaying dates in the agenda.
  2380. Used by the daily/weekly agenda and by the timeline. This should be
  2381. a format string understood by `format-time-string', or a function returning
  2382. the formatted date as a string. The function must take a single argument,
  2383. a calendar-style date list like (month day year)."
  2384. :group 'org-agenda-daily/weekly
  2385. :type '(choice
  2386. (string :tag "Format string")
  2387. (function :tag "Function")))
  2388. (defun org-agenda-format-date-aligned (date)
  2389. "Format a date string for display in the daily/weekly agenda, or timeline.
  2390. This function makes sure that dates are aligned for easy reading."
  2391. (format "%-9s %2d %s %4d"
  2392. (calendar-day-name date)
  2393. (extract-calendar-day date)
  2394. (calendar-month-name (extract-calendar-month date))
  2395. (extract-calendar-year date)))
  2396. (defcustom org-agenda-include-diary nil
  2397. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2398. :group 'org-agenda-daily/weekly
  2399. :type 'boolean)
  2400. (defcustom org-agenda-include-all-todo nil
  2401. "Set means weekly/daily agenda will always contain all TODO entries.
  2402. The TODO entries will be listed at the top of the agenda, before
  2403. the entries for specific days."
  2404. :group 'org-agenda-daily/weekly
  2405. :type 'boolean)
  2406. (defcustom org-agenda-repeating-timestamp-show-all t
  2407. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2408. When nil, only one occurence is shown, either today or the
  2409. nearest into the future."
  2410. :group 'org-agenda-daily/weekly
  2411. :type 'boolean)
  2412. (defcustom org-deadline-warning-days 14
  2413. "No. of days before expiration during which a deadline becomes active.
  2414. This variable governs the display in sparse trees and in the agenda.
  2415. When 0 or negative, it means use this number (the absolute value of it)
  2416. even if a deadline has a different individual lead time specified."
  2417. :group 'org-time
  2418. :group 'org-agenda-daily/weekly
  2419. :type 'number)
  2420. (defcustom org-scheduled-past-days 10000
  2421. "No. of days to continue listing scheduled items that are not marked DONE.
  2422. When an item is scheduled on a date, it shows up in the agenda on this
  2423. day and will be listed until it is marked done for the number of days
  2424. given here."
  2425. :group 'org-agenda-daily/weekly
  2426. :type 'number)
  2427. (defgroup org-agenda-time-grid nil
  2428. "Options concerning the time grid in the Org-mode Agenda."
  2429. :tag "Org Agenda Time Grid"
  2430. :group 'org-agenda)
  2431. (defcustom org-agenda-use-time-grid t
  2432. "Non-nil means, show a time grid in the agenda schedule.
  2433. A time grid is a set of lines for specific times (like every two hours between
  2434. 8:00 and 20:00). The items scheduled for a day at specific times are
  2435. sorted in between these lines.
  2436. For details about when the grid will be shown, and what it will look like, see
  2437. the variable `org-agenda-time-grid'."
  2438. :group 'org-agenda-time-grid
  2439. :type 'boolean)
  2440. (defcustom org-agenda-time-grid
  2441. '((daily today require-timed)
  2442. "----------------"
  2443. (800 1000 1200 1400 1600 1800 2000))
  2444. "The settings for time grid for agenda display.
  2445. This is a list of three items. The first item is again a list. It contains
  2446. symbols specifying conditions when the grid should be displayed:
  2447. daily if the agenda shows a single day
  2448. weekly if the agenda shows an entire week
  2449. today show grid on current date, independent of daily/weekly display
  2450. require-timed show grid only if at least one item has a time specification
  2451. The second item is a string which will be places behing the grid time.
  2452. The third item is a list of integers, indicating the times that should have
  2453. a grid line."
  2454. :group 'org-agenda-time-grid
  2455. :type
  2456. '(list
  2457. (set :greedy t :tag "Grid Display Options"
  2458. (const :tag "Show grid in single day agenda display" daily)
  2459. (const :tag "Show grid in weekly agenda display" weekly)
  2460. (const :tag "Always show grid for today" today)
  2461. (const :tag "Show grid only if any timed entries are present"
  2462. require-timed)
  2463. (const :tag "Skip grid times already present in an entry"
  2464. remove-match))
  2465. (string :tag "Grid String")
  2466. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2467. (defgroup org-agenda-sorting nil
  2468. "Options concerning sorting in the Org-mode Agenda."
  2469. :tag "Org Agenda Sorting"
  2470. :group 'org-agenda)
  2471. (defconst org-sorting-choice
  2472. '(choice
  2473. (const time-up) (const time-down)
  2474. (const category-keep) (const category-up) (const category-down)
  2475. (const tag-down) (const tag-up)
  2476. (const priority-up) (const priority-down))
  2477. "Sorting choices.")
  2478. (defcustom org-agenda-sorting-strategy
  2479. '((agenda time-up category-keep priority-down)
  2480. (todo category-keep priority-down)
  2481. (tags category-keep priority-down)
  2482. (search category-keep))
  2483. "Sorting structure for the agenda items of a single day.
  2484. This is a list of symbols which will be used in sequence to determine
  2485. if an entry should be listed before another entry. The following
  2486. symbols are recognized:
  2487. time-up Put entries with time-of-day indications first, early first
  2488. time-down Put entries with time-of-day indications first, late first
  2489. category-keep Keep the default order of categories, corresponding to the
  2490. sequence in `org-agenda-files'.
  2491. category-up Sort alphabetically by category, A-Z.
  2492. category-down Sort alphabetically by category, Z-A.
  2493. tag-up Sort alphabetically by last tag, A-Z.
  2494. tag-down Sort alphabetically by last tag, Z-A.
  2495. priority-up Sort numerically by priority, high priority last.
  2496. priority-down Sort numerically by priority, high priority first.
  2497. The different possibilities will be tried in sequence, and testing stops
  2498. if one comparison returns a \"not-equal\". For example, the default
  2499. '(time-up category-keep priority-down)
  2500. means: Pull out all entries having a specified time of day and sort them,
  2501. in order to make a time schedule for the current day the first thing in the
  2502. agenda listing for the day. Of the entries without a time indication, keep
  2503. the grouped in categories, don't sort the categories, but keep them in
  2504. the sequence given in `org-agenda-files'. Within each category sort by
  2505. priority.
  2506. Leaving out `category-keep' would mean that items will be sorted across
  2507. categories by priority.
  2508. Instead of a single list, this can also be a set of list for specific
  2509. contents, with a context symbol in the car of the list, any of
  2510. `agenda', `todo', `tags' for the corresponding agenda views."
  2511. :group 'org-agenda-sorting
  2512. :type `(choice
  2513. (repeat :tag "General" ,org-sorting-choice)
  2514. (list :tag "Individually"
  2515. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2516. (repeat ,org-sorting-choice))
  2517. (cons (const :tag "Strategy for TODO lists" todo)
  2518. (repeat ,org-sorting-choice))
  2519. (cons (const :tag "Strategy for Tags matches" tags)
  2520. (repeat ,org-sorting-choice)))))
  2521. (defcustom org-sort-agenda-notime-is-late t
  2522. "Non-nil means, items without time are considered late.
  2523. This is only relevant for sorting. When t, items which have no explicit
  2524. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2525. do have a time. When nil, the default time is before 0:00. You can use this
  2526. option to decide if the schedule for today should come before or after timeless
  2527. agenda entries."
  2528. :group 'org-agenda-sorting
  2529. :type 'boolean)
  2530. (defgroup org-agenda-line-format nil
  2531. "Options concerning the entry prefix in the Org-mode agenda display."
  2532. :tag "Org Agenda Line Format"
  2533. :group 'org-agenda)
  2534. (defcustom org-agenda-prefix-format
  2535. '((agenda . " %-12:c%?-12t% s")
  2536. (timeline . " % s")
  2537. (todo . " %-12:c")
  2538. (tags . " %-12:c")
  2539. (search . " %-12:c"))
  2540. "Format specifications for the prefix of items in the agenda views.
  2541. An alist with four entries, for the different agenda types. The keys to the
  2542. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2543. are format strings.
  2544. This format works similar to a printf format, with the following meaning:
  2545. %c the category of the item, \"Diary\" for entries from the diary, or
  2546. as given by the CATEGORY keyword or derived from the file name.
  2547. %T the *last* tag of the item. Last because inherited tags come
  2548. first in the list.
  2549. %t the time-of-day specification if one applies to the entry, in the
  2550. format HH:MM
  2551. %s Scheduling/Deadline information, a short string
  2552. All specifiers work basically like the standard `%s' of printf, but may
  2553. contain two additional characters: A question mark just after the `%' and
  2554. a whitespace/punctuation character just before the final letter.
  2555. If the first character after `%' is a question mark, the entire field
  2556. will only be included if the corresponding value applies to the
  2557. current entry. This is useful for fields which should have fixed
  2558. width when present, but zero width when absent. For example,
  2559. \"%?-12t\" will result in a 12 character time field if a time of the
  2560. day is specified, but will completely disappear in entries which do
  2561. not contain a time.
  2562. If there is punctuation or whitespace character just before the final
  2563. format letter, this character will be appended to the field value if
  2564. the value is not empty. For example, the format \"%-12:c\" leads to
  2565. \"Diary: \" if the category is \"Diary\". If the category were be
  2566. empty, no additional colon would be interted.
  2567. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2568. - Indent the line with two space characters
  2569. - Give the category in a 12 chars wide field, padded with whitespace on
  2570. the right (because of `-'). Append a colon if there is a category
  2571. (because of `:').
  2572. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2573. time, don't put in an empty field, just skip it (because of '?').
  2574. - Finally, put the scheduling information and append a whitespace.
  2575. As another example, if you don't want the time-of-day of entries in
  2576. the prefix, you could use:
  2577. (setq org-agenda-prefix-format \" %-11:c% s\")
  2578. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2579. `org-agenda-remove-tags'."
  2580. :type '(choice
  2581. (string :tag "General format")
  2582. (list :greedy t :tag "View dependent"
  2583. (cons (const agenda) (string :tag "Format"))
  2584. (cons (const timeline) (string :tag "Format"))
  2585. (cons (const todo) (string :tag "Format"))
  2586. (cons (const tags) (string :tag "Format"))
  2587. (cons (const search) (string :tag "Format"))))
  2588. :group 'org-agenda-line-format)
  2589. (defvar org-prefix-format-compiled nil
  2590. "The compiled version of the most recently used prefix format.
  2591. See the variable `org-agenda-prefix-format'.")
  2592. (defcustom org-agenda-todo-keyword-format "%-1s"
  2593. "Format for the TODO keyword in agenda lines.
  2594. Set this to something like \"%-12s\" if you want all TODO keywords
  2595. to occupy a fixed space in the agenda display."
  2596. :group 'org-agenda-line-format
  2597. :type 'string)
  2598. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2599. "Text preceeding scheduled items in the agenda view.
  2600. This is a list with two strings. The first applies when the item is
  2601. scheduled on the current day. The second applies when it has been scheduled
  2602. previously, it may contain a %d to capture how many days ago the item was
  2603. scheduled."
  2604. :group 'org-agenda-line-format
  2605. :type '(list
  2606. (string :tag "Scheduled today ")
  2607. (string :tag "Scheduled previously")))
  2608. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2609. "Text preceeding deadline items in the agenda view.
  2610. This is a list with two strings. The first applies when the item has its
  2611. deadline on the current day. The second applies when it is in the past or
  2612. in the future, it may contain %d to capture how many days away the deadline
  2613. is (was)."
  2614. :group 'org-agenda-line-format
  2615. :type '(list
  2616. (string :tag "Deadline today ")
  2617. (string :tag "Deadline relative")))
  2618. (defcustom org-agenda-remove-times-when-in-prefix t
  2619. "Non-nil means, remove duplicate time specifications in agenda items.
  2620. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2621. time-of-day specification in a headline or diary entry is extracted and
  2622. placed into the prefix. If this option is non-nil, the original specification
  2623. \(a timestamp or -range, or just a plain time(range) specification like
  2624. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2625. cluttered.
  2626. The option can be t or nil. It may also be the symbol `beg', indicating
  2627. that the time should only be removed what it is located at the beginning of
  2628. the headline/diary entry."
  2629. :group 'org-agenda-line-format
  2630. :type '(choice
  2631. (const :tag "Always" t)
  2632. (const :tag "Never" nil)
  2633. (const :tag "When at beginning of entry" beg)))
  2634. (defcustom org-agenda-default-appointment-duration nil
  2635. "Default duration for appointments that only have a starting time.
  2636. When nil, no duration is specified in such cases.
  2637. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2638. :group 'org-agenda-line-format
  2639. :type '(choice
  2640. (integer :tag "Minutes")
  2641. (const :tag "No default duration")))
  2642. (defcustom org-agenda-remove-tags nil
  2643. "Non-nil means, remove the tags from the headline copy in the agenda.
  2644. When this is the symbol `prefix', only remove tags when
  2645. `org-agenda-prefix-format' contains a `%T' specifier."
  2646. :group 'org-agenda-line-format
  2647. :type '(choice
  2648. (const :tag "Always" t)
  2649. (const :tag "Never" nil)
  2650. (const :tag "When prefix format contains %T" prefix)))
  2651. (if (fboundp 'defvaralias)
  2652. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2653. 'org-agenda-remove-tags))
  2654. (defcustom org-agenda-tags-column -80
  2655. "Shift tags in agenda items to this column.
  2656. If this number is positive, it specifies the column. If it is negative,
  2657. it means that the tags should be flushright to that column. For example,
  2658. -80 works well for a normal 80 character screen."
  2659. :group 'org-agenda-line-format
  2660. :type 'integer)
  2661. (if (fboundp 'defvaralias)
  2662. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2663. (defcustom org-agenda-fontify-priorities t
  2664. "Non-nil means, highlight low and high priorities in agenda.
  2665. When t, the highest priority entries are bold, lowest priority italic.
  2666. This may also be an association list of priority faces. The face may be
  2667. a names face, or a list like `(:background \"Red\")'."
  2668. :group 'org-agenda-line-format
  2669. :type '(choice
  2670. (const :tag "Never" nil)
  2671. (const :tag "Defaults" t)
  2672. (repeat :tag "Specify"
  2673. (list (character :tag "Priority" :value ?A)
  2674. (sexp :tag "face")))))
  2675. (defgroup org-latex nil
  2676. "Options for embedding LaTeX code into Org-mode"
  2677. :tag "Org LaTeX"
  2678. :group 'org)
  2679. (defcustom org-format-latex-options
  2680. '(:foreground default :background default :scale 1.0
  2681. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2682. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2683. "Options for creating images from LaTeX fragments.
  2684. This is a property list with the following properties:
  2685. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2686. `default' means use the forground of the default face.
  2687. :background the background color, or \"Transparent\".
  2688. `default' means use the background of the default face.
  2689. :scale a scaling factor for the size of the images
  2690. :html-foreground, :html-background, :html-scale
  2691. The same numbers for HTML export.
  2692. :matchers a list indicating which matchers should be used to
  2693. find LaTeX fragments. Valid members of this list are:
  2694. \"begin\" find environments
  2695. \"$\" find math expressions surrounded by $...$
  2696. \"$$\" find math expressions surrounded by $$....$$
  2697. \"\\(\" find math expressions surrounded by \\(...\\)
  2698. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2699. :group 'org-latex
  2700. :type 'plist)
  2701. (defcustom org-format-latex-header "\\documentclass{article}
  2702. \\usepackage{fullpage} % do not remove
  2703. \\usepackage{amssymb}
  2704. \\usepackage[usenames]{color}
  2705. \\usepackage{amsmath}
  2706. \\usepackage{latexsym}
  2707. \\usepackage[mathscr]{eucal}
  2708. \\pagestyle{empty} % do not remove"
  2709. "The document header used for processing LaTeX fragments."
  2710. :group 'org-latex
  2711. :type 'string)
  2712. (defgroup org-export nil
  2713. "Options for exporting org-listings."
  2714. :tag "Org Export"
  2715. :group 'org)
  2716. (defgroup org-export-general nil
  2717. "General options for exporting Org-mode files."
  2718. :tag "Org Export General"
  2719. :group 'org-export)
  2720. ;; FIXME
  2721. (defvar org-export-publishing-directory nil)
  2722. (defcustom org-export-with-special-strings t
  2723. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2724. When this option is turned on, these strings will be exported as:
  2725. Org HTML LaTeX
  2726. -----+----------+--------
  2727. \\- &shy; \\-
  2728. -- &ndash; --
  2729. --- &mdash; ---
  2730. ... &hellip; \ldots
  2731. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2732. :group 'org-export-translation
  2733. :type 'boolean)
  2734. (defcustom org-export-language-setup
  2735. '(("en" "Author" "Date" "Table of Contents")
  2736. ("cs" "Autor" "Datum" "Obsah")
  2737. ("da" "Ophavsmand" "Dato" "Indhold")
  2738. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2739. ("es" "Autor" "Fecha" "\xcdndice")
  2740. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2741. ("it" "Autore" "Data" "Indice")
  2742. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2743. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2744. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2745. "Terms used in export text, translated to different languages.
  2746. Use the variable `org-export-default-language' to set the language,
  2747. or use the +OPTION lines for a per-file setting."
  2748. :group 'org-export-general
  2749. :type '(repeat
  2750. (list
  2751. (string :tag "HTML language tag")
  2752. (string :tag "Author")
  2753. (string :tag "Date")
  2754. (string :tag "Table of Contents"))))
  2755. (defcustom org-export-default-language "en"
  2756. "The default language of HTML export, as a string.
  2757. This should have an association in `org-export-language-setup'."
  2758. :group 'org-export-general
  2759. :type 'string)
  2760. (defcustom org-export-skip-text-before-1st-heading t
  2761. "Non-nil means, skip all text before the first headline when exporting.
  2762. When nil, that text is exported as well."
  2763. :group 'org-export-general
  2764. :type 'boolean)
  2765. (defcustom org-export-headline-levels 3
  2766. "The last level which is still exported as a headline.
  2767. Inferior levels will produce itemize lists when exported.
  2768. Note that a numeric prefix argument to an exporter function overrides
  2769. this setting.
  2770. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2771. :group 'org-export-general
  2772. :type 'number)
  2773. (defcustom org-export-with-section-numbers t
  2774. "Non-nil means, add section numbers to headlines when exporting.
  2775. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2776. :group 'org-export-general
  2777. :type 'boolean)
  2778. (defcustom org-export-with-toc t
  2779. "Non-nil means, create a table of contents in exported files.
  2780. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2781. When an integer, include levels up to N in the toc, this may then be
  2782. different from `org-export-headline-levels', but it will not be allowed
  2783. to be larger than the number of headline levels.
  2784. When nil, no table of contents is made.
  2785. Headlines which contain any TODO items will be marked with \"(*)\" in
  2786. ASCII export, and with red color in HTML output, if the option
  2787. `org-export-mark-todo-in-toc' is set.
  2788. In HTML output, the TOC will be clickable.
  2789. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2790. or \"toc:3\"."
  2791. :group 'org-export-general
  2792. :type '(choice
  2793. (const :tag "No Table of Contents" nil)
  2794. (const :tag "Full Table of Contents" t)
  2795. (integer :tag "TOC to level")))
  2796. (defcustom org-export-mark-todo-in-toc nil
  2797. "Non-nil means, mark TOC lines that contain any open TODO items."
  2798. :group 'org-export-general
  2799. :type 'boolean)
  2800. (defcustom org-export-preserve-breaks nil
  2801. "Non-nil means, preserve all line breaks when exporting.
  2802. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2803. export, line breaks will always be preserved, regardless of this variable.
  2804. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2805. :group 'org-export-general
  2806. :type 'boolean)
  2807. (defcustom org-export-with-archived-trees 'headline
  2808. "Whether subtrees with the ARCHIVE tag should be exported.
  2809. This can have three different values
  2810. nil Do not export, pretend this tree is not present
  2811. t Do export the entire tree
  2812. headline Only export the headline, but skip the tree below it."
  2813. :group 'org-export-general
  2814. :group 'org-archive
  2815. :type '(choice
  2816. (const :tag "not at all" nil)
  2817. (const :tag "headline only" 'headline)
  2818. (const :tag "entirely" t)))
  2819. (defcustom org-export-author-info t
  2820. "Non-nil means, insert author name and email into the exported file.
  2821. This option can also be set with the +OPTIONS line,
  2822. e.g. \"author-info:nil\"."
  2823. :group 'org-export-general
  2824. :type 'boolean)
  2825. (defcustom org-export-time-stamp-file t
  2826. "Non-nil means, insert a time stamp into the exported file.
  2827. The time stamp shows when the file was created.
  2828. This option can also be set with the +OPTIONS line,
  2829. e.g. \"timestamp:nil\"."
  2830. :group 'org-export-general
  2831. :type 'boolean)
  2832. (defcustom org-export-with-timestamps t
  2833. "If nil, do not export time stamps and associated keywords."
  2834. :group 'org-export-general
  2835. :type 'boolean)
  2836. (defcustom org-export-remove-timestamps-from-toc t
  2837. "If nil, remove timestamps from the table of contents entries."
  2838. :group 'org-export-general
  2839. :type 'boolean)
  2840. (defcustom org-export-with-tags 'not-in-toc
  2841. "If nil, do not export tags, just remove them from headlines.
  2842. If this is the symbol `not-in-toc', tags will be removed from table of
  2843. contents entries, but still be shown in the headlines of the document.
  2844. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2845. :group 'org-export-general
  2846. :type '(choice
  2847. (const :tag "Off" nil)
  2848. (const :tag "Not in TOC" not-in-toc)
  2849. (const :tag "On" t)))
  2850. (defcustom org-export-with-drawers nil
  2851. "Non-nil means, export with drawers like the property drawer.
  2852. When t, all drawers are exported. This may also be a list of
  2853. drawer names to export."
  2854. :group 'org-export-general
  2855. :type '(choice
  2856. (const :tag "All drawers" t)
  2857. (const :tag "None" nil)
  2858. (repeat :tag "Selected drawers"
  2859. (string :tag "Drawer name"))))
  2860. (defgroup org-export-translation nil
  2861. "Options for translating special ascii sequences for the export backends."
  2862. :tag "Org Export Translation"
  2863. :group 'org-export)
  2864. (defcustom org-export-with-emphasize t
  2865. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2866. If the export target supports emphasizing text, the word will be
  2867. typeset in bold, italic, or underlined, respectively. Works only for
  2868. single words, but you can say: I *really* *mean* *this*.
  2869. Not all export backends support this.
  2870. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2871. :group 'org-export-translation
  2872. :type 'boolean)
  2873. (defcustom org-export-with-footnotes t
  2874. "If nil, export [1] as a footnote marker.
  2875. Lines starting with [1] will be formatted as footnotes.
  2876. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2877. :group 'org-export-translation
  2878. :type 'boolean)
  2879. (defcustom org-export-with-sub-superscripts t
  2880. "Non-nil means, interpret \"_\" and \"^\" for export.
  2881. When this option is turned on, you can use TeX-like syntax for sub- and
  2882. superscripts. Several characters after \"_\" or \"^\" will be
  2883. considered as a single item - so grouping with {} is normally not
  2884. needed. For example, the following things will be parsed as single
  2885. sub- or superscripts.
  2886. 10^24 or 10^tau several digits will be considered 1 item.
  2887. 10^-12 or 10^-tau a leading sign with digits or a word
  2888. x^2-y^3 will be read as x^2 - y^3, because items are
  2889. terminated by almost any nonword/nondigit char.
  2890. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2891. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2892. sub/superscript. If you set this variable to the symbol `{}',
  2893. the braces are *required* in order to trigger interpretations as
  2894. sub/superscript. This can be helpful in documents that need \"_\"
  2895. frequently in plain text.
  2896. Not all export backends support this, but HTML does.
  2897. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2898. :group 'org-export-translation
  2899. :type '(choice
  2900. (const :tag "Always interpret" t)
  2901. (const :tag "Only with braces" {})
  2902. (const :tag "Never interpret" nil)))
  2903. (defcustom org-export-with-special-strings t
  2904. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2905. When this option is turned on, these strings will be exported as:
  2906. \\- : &shy;
  2907. -- : &ndash;
  2908. --- : &mdash;
  2909. Not all export backends support this, but HTML does.
  2910. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2911. :group 'org-export-translation
  2912. :type 'boolean)
  2913. (defcustom org-export-with-TeX-macros t
  2914. "Non-nil means, interpret simple TeX-like macros when exporting.
  2915. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2916. No only real TeX macros will work here, but the standard HTML entities
  2917. for math can be used as macro names as well. For a list of supported
  2918. names in HTML export, see the constant `org-html-entities'.
  2919. Not all export backends support this.
  2920. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2921. :group 'org-export-translation
  2922. :group 'org-export-latex
  2923. :type 'boolean)
  2924. (defcustom org-export-with-LaTeX-fragments nil
  2925. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2926. When set, the exporter will find LaTeX environments if the \\begin line is
  2927. the first non-white thing on a line. It will also find the math delimiters
  2928. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2929. display math.
  2930. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2931. :group 'org-export-translation
  2932. :group 'org-export-latex
  2933. :type 'boolean)
  2934. (defcustom org-export-with-fixed-width t
  2935. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2936. This can be used to have pre-formatted text, fragments of code etc. For
  2937. example:
  2938. : ;; Some Lisp examples
  2939. : (while (defc cnt)
  2940. : (ding))
  2941. will be looking just like this in also HTML. See also the QUOTE keyword.
  2942. Not all export backends support this.
  2943. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2944. :group 'org-export-translation
  2945. :type 'boolean)
  2946. (defcustom org-match-sexp-depth 3
  2947. "Number of stacked braces for sub/superscript matching.
  2948. This has to be set before loading org.el to be effective."
  2949. :group 'org-export-translation
  2950. :type 'integer)
  2951. (defgroup org-export-tables nil
  2952. "Options for exporting tables in Org-mode."
  2953. :tag "Org Export Tables"
  2954. :group 'org-export)
  2955. (defcustom org-export-with-tables t
  2956. "If non-nil, lines starting with \"|\" define a table.
  2957. For example:
  2958. | Name | Address | Birthday |
  2959. |-------------+----------+-----------|
  2960. | Arthur Dent | England | 29.2.2100 |
  2961. Not all export backends support this.
  2962. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2963. :group 'org-export-tables
  2964. :type 'boolean)
  2965. (defcustom org-export-highlight-first-table-line t
  2966. "Non-nil means, highlight the first table line.
  2967. In HTML export, this means use <th> instead of <td>.
  2968. In tables created with table.el, this applies to the first table line.
  2969. In Org-mode tables, all lines before the first horizontal separator
  2970. line will be formatted with <th> tags."
  2971. :group 'org-export-tables
  2972. :type 'boolean)
  2973. (defcustom org-export-table-remove-special-lines t
  2974. "Remove special lines and marking characters in calculating tables.
  2975. This removes the special marking character column from tables that are set
  2976. up for spreadsheet calculations. It also removes the entire lines
  2977. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2978. the values of constants may be useful to have."
  2979. :group 'org-export-tables
  2980. :type 'boolean)
  2981. (defcustom org-export-prefer-native-exporter-for-tables nil
  2982. "Non-nil means, always export tables created with table.el natively.
  2983. Natively means, use the HTML code generator in table.el.
  2984. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2985. the table does not use row- or column-spanning). This has the
  2986. advantage, that the automatic HTML conversions for math symbols and
  2987. sub/superscripts can be applied. Org-mode's HTML generator is also
  2988. much faster."
  2989. :group 'org-export-tables
  2990. :type 'boolean)
  2991. (defgroup org-export-ascii nil
  2992. "Options specific for ASCII export of Org-mode files."
  2993. :tag "Org Export ASCII"
  2994. :group 'org-export)
  2995. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2996. "Characters for underlining headings in ASCII export.
  2997. In the given sequence, these characters will be used for level 1, 2, ..."
  2998. :group 'org-export-ascii
  2999. :type '(repeat character))
  3000. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  3001. "Bullet characters for headlines converted to lists in ASCII export.
  3002. The first character is used for the first lest level generated in this
  3003. way, and so on. If there are more levels than characters given here,
  3004. the list will be repeated.
  3005. Note that plain lists will keep the same bullets as the have in the
  3006. Org-mode file."
  3007. :group 'org-export-ascii
  3008. :type '(repeat character))
  3009. (defgroup org-export-xml nil
  3010. "Options specific for XML export of Org-mode files."
  3011. :tag "Org Export XML"
  3012. :group 'org-export)
  3013. (defgroup org-export-html nil
  3014. "Options specific for HTML export of Org-mode files."
  3015. :tag "Org Export HTML"
  3016. :group 'org-export)
  3017. (defcustom org-export-html-coding-system nil
  3018. ""
  3019. :group 'org-export-html
  3020. :type 'coding-system)
  3021. (defcustom org-export-html-extension "html"
  3022. "The extension for exported HTML files."
  3023. :group 'org-export-html
  3024. :type 'string)
  3025. (defcustom org-export-html-style
  3026. "<style type=\"text/css\">
  3027. html {
  3028. font-family: Times, serif;
  3029. font-size: 12pt;
  3030. }
  3031. .title { text-align: center; }
  3032. .todo { color: red; }
  3033. .done { color: green; }
  3034. .timestamp { color: grey }
  3035. .timestamp-kwd { color: CadetBlue }
  3036. .tag { background-color:lightblue; font-weight:normal }
  3037. .target { background-color: lavender; }
  3038. pre {
  3039. border: 1pt solid #AEBDCC;
  3040. background-color: #F3F5F7;
  3041. padding: 5pt;
  3042. font-family: courier, monospace;
  3043. }
  3044. table { border-collapse: collapse; }
  3045. td, th {
  3046. vertical-align: top;
  3047. <!--border: 1pt solid #ADB9CC;-->
  3048. }
  3049. </style>"
  3050. "The default style specification for exported HTML files.
  3051. Since there are different ways of setting style information, this variable
  3052. needs to contain the full HTML structure to provide a style, including the
  3053. surrounding HTML tags. The style specifications should include definitions
  3054. for new classes todo, done, title, and deadline. For example, legal values
  3055. would be:
  3056. <style type=\"text/css\">
  3057. p { font-weight: normal; color: gray; }
  3058. h1 { color: black; }
  3059. .title { text-align: center; }
  3060. .todo, .deadline { color: red; }
  3061. .done { color: green; }
  3062. </style>
  3063. or, if you want to keep the style in a file,
  3064. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  3065. As the value of this option simply gets inserted into the HTML <head> header,
  3066. you can \"misuse\" it to add arbitrary text to the header."
  3067. :group 'org-export-html
  3068. :type 'string)
  3069. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  3070. "Format for typesetting the document title in HTML export."
  3071. :group 'org-export-html
  3072. :type 'string)
  3073. (defcustom org-export-html-toplevel-hlevel 2
  3074. "The <H> level for level 1 headings in HTML export."
  3075. :group 'org-export-html
  3076. :type 'string)
  3077. (defcustom org-export-html-link-org-files-as-html t
  3078. "Non-nil means, make file links to `file.org' point to `file.html'.
  3079. When org-mode is exporting an org-mode file to HTML, links to
  3080. non-html files are directly put into a href tag in HTML.
  3081. However, links to other Org-mode files (recognized by the
  3082. extension `.org.) should become links to the corresponding html
  3083. file, assuming that the linked org-mode file will also be
  3084. converted to HTML.
  3085. When nil, the links still point to the plain `.org' file."
  3086. :group 'org-export-html
  3087. :type 'boolean)
  3088. (defcustom org-export-html-inline-images 'maybe
  3089. "Non-nil means, inline images into exported HTML pages.
  3090. This is done using an <img> tag. When nil, an anchor with href is used to
  3091. link to the image. If this option is `maybe', then images in links with
  3092. an empty description will be inlined, while images with a description will
  3093. be linked only."
  3094. :group 'org-export-html
  3095. :type '(choice (const :tag "Never" nil)
  3096. (const :tag "Always" t)
  3097. (const :tag "When there is no description" maybe)))
  3098. ;; FIXME: rename
  3099. (defcustom org-export-html-expand t
  3100. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  3101. When nil, these tags will be exported as plain text and therefore
  3102. not be interpreted by a browser.
  3103. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3104. :group 'org-export-html
  3105. :type 'boolean)
  3106. (defcustom org-export-html-table-tag
  3107. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3108. "The HTML tag that is used to start a table.
  3109. This must be a <table> tag, but you may change the options like
  3110. borders and spacing."
  3111. :group 'org-export-html
  3112. :type 'string)
  3113. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3114. "The opening tag for table header fields.
  3115. This is customizable so that alignment options can be specified."
  3116. :group 'org-export-tables
  3117. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3118. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3119. "The opening tag for table data fields.
  3120. This is customizable so that alignment options can be specified."
  3121. :group 'org-export-tables
  3122. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3123. (defcustom org-export-html-with-timestamp nil
  3124. "If non-nil, write `org-export-html-html-helper-timestamp'
  3125. into the exported HTML text. Otherwise, the buffer will just be saved
  3126. to a file."
  3127. :group 'org-export-html
  3128. :type 'boolean)
  3129. (defcustom org-export-html-html-helper-timestamp
  3130. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3131. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3132. :group 'org-export-html
  3133. :type 'string)
  3134. (defgroup org-export-icalendar nil
  3135. "Options specific for iCalendar export of Org-mode files."
  3136. :tag "Org Export iCalendar"
  3137. :group 'org-export)
  3138. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3139. "The file name for the iCalendar file covering all agenda files.
  3140. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3141. The file name should be absolute, the file will be overwritten without warning."
  3142. :group 'org-export-icalendar
  3143. :type 'file)
  3144. (defcustom org-icalendar-include-todo nil
  3145. "Non-nil means, export to iCalendar files should also cover TODO items."
  3146. :group 'org-export-icalendar
  3147. :type '(choice
  3148. (const :tag "None" nil)
  3149. (const :tag "Unfinished" t)
  3150. (const :tag "All" all)))
  3151. (defcustom org-icalendar-include-sexps t
  3152. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3153. These are entries like in the diary, but directly in an Org-mode file."
  3154. :group 'org-export-icalendar
  3155. :type 'boolean)
  3156. (defcustom org-icalendar-include-body 100
  3157. "Amount of text below headline to be included in iCalendar export.
  3158. This is a number of characters that should maximally be included.
  3159. Properties, scheduling and clocking lines will always be removed.
  3160. The text will be inserted into the DESCRIPTION field."
  3161. :group 'org-export-icalendar
  3162. :type '(choice
  3163. (const :tag "Nothing" nil)
  3164. (const :tag "Everything" t)
  3165. (integer :tag "Max characters")))
  3166. (defcustom org-icalendar-combined-name "OrgMode"
  3167. "Calendar name for the combined iCalendar representing all agenda files."
  3168. :group 'org-export-icalendar
  3169. :type 'string)
  3170. (defgroup org-font-lock nil
  3171. "Font-lock settings for highlighting in Org-mode."
  3172. :tag "Org Font Lock"
  3173. :group 'org)
  3174. (defcustom org-level-color-stars-only nil
  3175. "Non-nil means fontify only the stars in each headline.
  3176. When nil, the entire headline is fontified.
  3177. Changing it requires restart of `font-lock-mode' to become effective
  3178. also in regions already fontified."
  3179. :group 'org-font-lock
  3180. :type 'boolean)
  3181. (defcustom org-hide-leading-stars nil
  3182. "Non-nil means, hide the first N-1 stars in a headline.
  3183. This works by using the face `org-hide' for these stars. This
  3184. face is white for a light background, and black for a dark
  3185. background. You may have to customize the face `org-hide' to
  3186. make this work.
  3187. Changing it requires restart of `font-lock-mode' to become effective
  3188. also in regions already fontified.
  3189. You may also set this on a per-file basis by adding one of the following
  3190. lines to the buffer:
  3191. #+STARTUP: hidestars
  3192. #+STARTUP: showstars"
  3193. :group 'org-font-lock
  3194. :type 'boolean)
  3195. (defcustom org-fontify-done-headline nil
  3196. "Non-nil means, change the face of a headline if it is marked DONE.
  3197. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3198. When this is non-nil, the headline after the keyword is set to the
  3199. `org-headline-done' as an additional indication."
  3200. :group 'org-font-lock
  3201. :type 'boolean)
  3202. (defcustom org-fontify-emphasized-text t
  3203. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3204. Changing this variable requires a restart of Emacs to take effect."
  3205. :group 'org-font-lock
  3206. :type 'boolean)
  3207. (defcustom org-highlight-latex-fragments-and-specials nil
  3208. "Non-nil means, fontify what is treated specially by the exporters."
  3209. :group 'org-font-lock
  3210. :type 'boolean)
  3211. (defcustom org-hide-emphasis-markers nil
  3212. "Non-nil mean font-lock should hide the emphasis marker characters."
  3213. :group 'org-font-lock
  3214. :type 'boolean)
  3215. (defvar org-emph-re nil
  3216. "Regular expression for matching emphasis.")
  3217. (defvar org-verbatim-re nil
  3218. "Regular expression for matching verbatim text.")
  3219. (defvar org-emphasis-regexp-components) ; defined just below
  3220. (defvar org-emphasis-alist) ; defined just below
  3221. (defun org-set-emph-re (var val)
  3222. "Set variable and compute the emphasis regular expression."
  3223. (set var val)
  3224. (when (and (boundp 'org-emphasis-alist)
  3225. (boundp 'org-emphasis-regexp-components)
  3226. org-emphasis-alist org-emphasis-regexp-components)
  3227. (let* ((e org-emphasis-regexp-components)
  3228. (pre (car e))
  3229. (post (nth 1 e))
  3230. (border (nth 2 e))
  3231. (body (nth 3 e))
  3232. (nl (nth 4 e))
  3233. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3234. (body1 (concat body "*?"))
  3235. (markers (mapconcat 'car org-emphasis-alist ""))
  3236. (vmarkers (mapconcat
  3237. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3238. org-emphasis-alist "")))
  3239. ;; make sure special characters appear at the right position in the class
  3240. (if (string-match "\\^" markers)
  3241. (setq markers (concat (replace-match "" t t markers) "^")))
  3242. (if (string-match "-" markers)
  3243. (setq markers (concat (replace-match "" t t markers) "-")))
  3244. (if (string-match "\\^" vmarkers)
  3245. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3246. (if (string-match "-" vmarkers)
  3247. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3248. (if (> nl 0)
  3249. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3250. (int-to-string nl) "\\}")))
  3251. ;; Make the regexp
  3252. (setq org-emph-re
  3253. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3254. "\\("
  3255. "\\([" markers "]\\)"
  3256. "\\("
  3257. "[^" border "]\\|"
  3258. "[^" border (if (and nil stacked) markers) "]"
  3259. body1
  3260. "[^" border (if (and nil stacked) markers) "]"
  3261. "\\)"
  3262. "\\3\\)"
  3263. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3264. (setq org-verbatim-re
  3265. (concat "\\([" pre "]\\|^\\)"
  3266. "\\("
  3267. "\\([" vmarkers "]\\)"
  3268. "\\("
  3269. "[^" border "]\\|"
  3270. "[^" border "]"
  3271. body1
  3272. "[^" border "]"
  3273. "\\)"
  3274. "\\3\\)"
  3275. "\\([" post "]\\|$\\)")))))
  3276. (defcustom org-emphasis-regexp-components
  3277. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3278. "Components used to build the regular expression for emphasis.
  3279. This is a list with 6 entries. Terminology: In an emphasis string
  3280. like \" *strong word* \", we call the initial space PREMATCH, the final
  3281. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3282. and \"trong wor\" is the body. The different components in this variable
  3283. specify what is allowed/forbidden in each part:
  3284. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3285. post Chars allowed as postmatch. End of line will be allowed too.
  3286. border The chars *forbidden* as border characters.
  3287. body-regexp A regexp like \".\" to match a body character. Don't use
  3288. non-shy groups here, and don't allow newline here.
  3289. newline The maximum number of newlines allowed in an emphasis exp.
  3290. Use customize to modify this, or restart Emacs after changing it."
  3291. :group 'org-font-lock
  3292. :set 'org-set-emph-re
  3293. :type '(list
  3294. (sexp :tag "Allowed chars in pre ")
  3295. (sexp :tag "Allowed chars in post ")
  3296. (sexp :tag "Forbidden chars in border ")
  3297. (sexp :tag "Regexp for body ")
  3298. (integer :tag "number of newlines allowed")
  3299. (option (boolean :tag "Stacking (DISABLED) "))))
  3300. (defcustom org-emphasis-alist
  3301. '(("*" bold "<b>" "</b>")
  3302. ("/" italic "<i>" "</i>")
  3303. ("_" underline "<u>" "</u>")
  3304. ("=" org-code "<code>" "</code>" verbatim)
  3305. ("~" org-verbatim "" "" verbatim)
  3306. ("+" (:strike-through t) "<del>" "</del>")
  3307. )
  3308. "Special syntax for emphasized text.
  3309. Text starting and ending with a special character will be emphasized, for
  3310. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3311. characters, the face to be used by font-lock for highlighting in Org-mode
  3312. Emacs buffers, and the HTML tags to be used for this.
  3313. Use customize to modify this, or restart Emacs after changing it."
  3314. :group 'org-font-lock
  3315. :set 'org-set-emph-re
  3316. :type '(repeat
  3317. (list
  3318. (string :tag "Marker character")
  3319. (choice
  3320. (face :tag "Font-lock-face")
  3321. (plist :tag "Face property list"))
  3322. (string :tag "HTML start tag")
  3323. (string :tag "HTML end tag")
  3324. (option (const verbatim)))))
  3325. ;;; The faces
  3326. (defgroup org-faces nil
  3327. "Faces in Org-mode."
  3328. :tag "Org Faces"
  3329. :group 'org-font-lock)
  3330. (defun org-compatible-face (inherits specs)
  3331. "Make a compatible face specification.
  3332. If INHERITS is an existing face and if the Emacs version supports it,
  3333. just inherit the face. If not, use SPECS to define the face.
  3334. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3335. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3336. to the top of the list. The `min-colors' attribute will be removed from
  3337. any other entries, and any resulting duplicates will be removed entirely."
  3338. (cond
  3339. ((and inherits (facep inherits)
  3340. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3341. ;; In Emacs 23, we use inheritance where possible.
  3342. ;; We only do this in Emacs 23, because only there the outline
  3343. ;; faces have been changed to the original org-mode-level-faces.
  3344. (list (list t :inherit inherits)))
  3345. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3346. ;; These do not understand the `min-colors' attribute.
  3347. (let (r e a)
  3348. (while (setq e (pop specs))
  3349. (cond
  3350. ((memq (car e) '(t default)) (push e r))
  3351. ((setq a (member '(min-colors 8) (car e)))
  3352. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3353. (cdr e)))))
  3354. ((setq a (assq 'min-colors (car e)))
  3355. (setq e (cons (delq a (car e)) (cdr e)))
  3356. (or (assoc (car e) r) (push e r)))
  3357. (t (or (assoc (car e) r) (push e r)))))
  3358. (nreverse r)))
  3359. (t specs)))
  3360. (put 'org-compatible-face 'lisp-indent-function 1)
  3361. (defface org-hide
  3362. '((((background light)) (:foreground "white"))
  3363. (((background dark)) (:foreground "black")))
  3364. "Face used to hide leading stars in headlines.
  3365. The forground color of this face should be equal to the background
  3366. color of the frame."
  3367. :group 'org-faces)
  3368. (defface org-level-1 ;; font-lock-function-name-face
  3369. (org-compatible-face 'outline-1
  3370. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3371. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3372. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3373. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3374. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3375. (t (:bold t))))
  3376. "Face used for level 1 headlines."
  3377. :group 'org-faces)
  3378. (defface org-level-2 ;; font-lock-variable-name-face
  3379. (org-compatible-face 'outline-2
  3380. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3381. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3382. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3383. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3384. (t (:bold t))))
  3385. "Face used for level 2 headlines."
  3386. :group 'org-faces)
  3387. (defface org-level-3 ;; font-lock-keyword-face
  3388. (org-compatible-face 'outline-3
  3389. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3390. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3391. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3392. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3393. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3394. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3395. (t (:bold t))))
  3396. "Face used for level 3 headlines."
  3397. :group 'org-faces)
  3398. (defface org-level-4 ;; font-lock-comment-face
  3399. (org-compatible-face 'outline-4
  3400. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3401. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3402. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3403. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3404. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3405. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3406. (t (:bold t))))
  3407. "Face used for level 4 headlines."
  3408. :group 'org-faces)
  3409. (defface org-level-5 ;; font-lock-type-face
  3410. (org-compatible-face 'outline-5
  3411. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3412. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3413. (((class color) (min-colors 8)) (:foreground "green"))))
  3414. "Face used for level 5 headlines."
  3415. :group 'org-faces)
  3416. (defface org-level-6 ;; font-lock-constant-face
  3417. (org-compatible-face 'outline-6
  3418. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3419. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3420. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3421. "Face used for level 6 headlines."
  3422. :group 'org-faces)
  3423. (defface org-level-7 ;; font-lock-builtin-face
  3424. (org-compatible-face 'outline-7
  3425. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3426. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3427. (((class color) (min-colors 8)) (:foreground "blue"))))
  3428. "Face used for level 7 headlines."
  3429. :group 'org-faces)
  3430. (defface org-level-8 ;; font-lock-string-face
  3431. (org-compatible-face 'outline-8
  3432. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3433. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3434. (((class color) (min-colors 8)) (:foreground "green"))))
  3435. "Face used for level 8 headlines."
  3436. :group 'org-faces)
  3437. (defface org-special-keyword ;; font-lock-string-face
  3438. (org-compatible-face nil
  3439. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3440. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3441. (t (:italic t))))
  3442. "Face used for special keywords."
  3443. :group 'org-faces)
  3444. (defface org-drawer ;; font-lock-function-name-face
  3445. (org-compatible-face nil
  3446. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3447. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3448. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3449. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3450. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3451. (t (:bold t))))
  3452. "Face used for drawers."
  3453. :group 'org-faces)
  3454. (defface org-property-value nil
  3455. "Face used for the value of a property."
  3456. :group 'org-faces)
  3457. (defface org-column
  3458. (org-compatible-face nil
  3459. '((((class color) (min-colors 16) (background light))
  3460. (:background "grey90"))
  3461. (((class color) (min-colors 16) (background dark))
  3462. (:background "grey30"))
  3463. (((class color) (min-colors 8))
  3464. (:background "cyan" :foreground "black"))
  3465. (t (:inverse-video t))))
  3466. "Face for column display of entry properties."
  3467. :group 'org-faces)
  3468. (when (fboundp 'set-face-attribute)
  3469. ;; Make sure that a fixed-width face is used when we have a column table.
  3470. (set-face-attribute 'org-column nil
  3471. :height (face-attribute 'default :height)
  3472. :family (face-attribute 'default :family)))
  3473. (defface org-warning
  3474. (org-compatible-face 'font-lock-warning-face
  3475. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3476. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3477. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3478. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3479. (t (:bold t))))
  3480. "Face for deadlines and TODO keywords."
  3481. :group 'org-faces)
  3482. (defface org-archived ; similar to shadow
  3483. (org-compatible-face 'shadow
  3484. '((((class color grayscale) (min-colors 88) (background light))
  3485. (:foreground "grey50"))
  3486. (((class color grayscale) (min-colors 88) (background dark))
  3487. (:foreground "grey70"))
  3488. (((class color) (min-colors 8) (background light))
  3489. (:foreground "green"))
  3490. (((class color) (min-colors 8) (background dark))
  3491. (:foreground "yellow"))))
  3492. "Face for headline with the ARCHIVE tag."
  3493. :group 'org-faces)
  3494. (defface org-link
  3495. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3496. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3497. (t (:underline t)))
  3498. "Face for links."
  3499. :group 'org-faces)
  3500. (defface org-ellipsis
  3501. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3502. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3503. (t (:strike-through t)))
  3504. "Face for the ellipsis in folded text."
  3505. :group 'org-faces)
  3506. (defface org-target
  3507. '((((class color) (background light)) (:underline t))
  3508. (((class color) (background dark)) (:underline t))
  3509. (t (:underline t)))
  3510. "Face for links."
  3511. :group 'org-faces)
  3512. (defface org-date
  3513. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3514. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3515. (t (:underline t)))
  3516. "Face for links."
  3517. :group 'org-faces)
  3518. (defface org-sexp-date
  3519. '((((class color) (background light)) (:foreground "Purple"))
  3520. (((class color) (background dark)) (:foreground "Cyan"))
  3521. (t (:underline t)))
  3522. "Face for links."
  3523. :group 'org-faces)
  3524. (defface org-tag
  3525. '((t (:bold t)))
  3526. "Face for tags."
  3527. :group 'org-faces)
  3528. (defface org-todo ; font-lock-warning-face
  3529. (org-compatible-face nil
  3530. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3531. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3532. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3533. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3534. (t (:inverse-video t :bold t))))
  3535. "Face for TODO keywords."
  3536. :group 'org-faces)
  3537. (defface org-done ;; font-lock-type-face
  3538. (org-compatible-face nil
  3539. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3540. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3541. (((class color) (min-colors 8)) (:foreground "green"))
  3542. (t (:bold t))))
  3543. "Face used for todo keywords that indicate DONE items."
  3544. :group 'org-faces)
  3545. (defface org-headline-done ;; font-lock-string-face
  3546. (org-compatible-face nil
  3547. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3548. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3549. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3550. "Face used to indicate that a headline is DONE.
  3551. This face is only used if `org-fontify-done-headline' is set. If applies
  3552. to the part of the headline after the DONE keyword."
  3553. :group 'org-faces)
  3554. (defcustom org-todo-keyword-faces nil
  3555. "Faces for specific TODO keywords.
  3556. This is a list of cons cells, with TODO keywords in the car
  3557. and faces in the cdr. The face can be a symbol, or a property
  3558. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3559. :group 'org-faces
  3560. :group 'org-todo
  3561. :type '(repeat
  3562. (cons
  3563. (string :tag "keyword")
  3564. (sexp :tag "face"))))
  3565. (defface org-table ;; font-lock-function-name-face
  3566. (org-compatible-face nil
  3567. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3568. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3569. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3570. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3571. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3572. (((class color) (min-colors 8) (background dark)))))
  3573. "Face used for tables."
  3574. :group 'org-faces)
  3575. (defface org-formula
  3576. (org-compatible-face nil
  3577. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3578. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3579. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3580. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3581. (t (:bold t :italic t))))
  3582. "Face for formulas."
  3583. :group 'org-faces)
  3584. (defface org-code
  3585. (org-compatible-face nil
  3586. '((((class color grayscale) (min-colors 88) (background light))
  3587. (:foreground "grey50"))
  3588. (((class color grayscale) (min-colors 88) (background dark))
  3589. (:foreground "grey70"))
  3590. (((class color) (min-colors 8) (background light))
  3591. (:foreground "green"))
  3592. (((class color) (min-colors 8) (background dark))
  3593. (:foreground "yellow"))))
  3594. "Face for fixed-with text like code snippets."
  3595. :group 'org-faces
  3596. :version "22.1")
  3597. (defface org-verbatim
  3598. (org-compatible-face nil
  3599. '((((class color grayscale) (min-colors 88) (background light))
  3600. (:foreground "grey50" :underline t))
  3601. (((class color grayscale) (min-colors 88) (background dark))
  3602. (:foreground "grey70" :underline t))
  3603. (((class color) (min-colors 8) (background light))
  3604. (:foreground "green" :underline t))
  3605. (((class color) (min-colors 8) (background dark))
  3606. (:foreground "yellow" :underline t))))
  3607. "Face for fixed-with text like code snippets."
  3608. :group 'org-faces
  3609. :version "22.1")
  3610. (defface org-agenda-structure ;; font-lock-function-name-face
  3611. (org-compatible-face nil
  3612. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3613. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3614. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3615. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3616. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3617. (t (:bold t))))
  3618. "Face used in agenda for captions and dates."
  3619. :group 'org-faces)
  3620. (defface org-scheduled-today
  3621. (org-compatible-face nil
  3622. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3623. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3624. (((class color) (min-colors 8)) (:foreground "green"))
  3625. (t (:bold t :italic t))))
  3626. "Face for items scheduled for a certain day."
  3627. :group 'org-faces)
  3628. (defface org-scheduled-previously
  3629. (org-compatible-face nil
  3630. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3631. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3632. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3633. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3634. (t (:bold t))))
  3635. "Face for items scheduled previously, and not yet done."
  3636. :group 'org-faces)
  3637. (defface org-upcoming-deadline
  3638. (org-compatible-face nil
  3639. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3640. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3641. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3642. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3643. (t (:bold t))))
  3644. "Face for items scheduled previously, and not yet done."
  3645. :group 'org-faces)
  3646. (defcustom org-agenda-deadline-faces
  3647. '((1.0 . org-warning)
  3648. (0.5 . org-upcoming-deadline)
  3649. (0.0 . default))
  3650. "Faces for showing deadlines in the agenda.
  3651. This is a list of cons cells. The cdr of each cell is a face to be used,
  3652. and it can also just be like '(:foreground \"yellow\").
  3653. Each car is a fraction of the head-warning time that must have passed for
  3654. this the face in the cdr to be used for display. The numbers must be
  3655. given in descending order. The head-warning time is normally taken
  3656. from `org-deadline-warning-days', but can also be specified in the deadline
  3657. timestamp itself, like this:
  3658. DEADLINE: <2007-08-13 Mon -8d>
  3659. You may use d for days, w for weeks, m for months and y for years. Months
  3660. and years will only be treated in an approximate fashion (30.4 days for a
  3661. month and 365.24 days for a year)."
  3662. :group 'org-faces
  3663. :group 'org-agenda-daily/weekly
  3664. :type '(repeat
  3665. (cons
  3666. (number :tag "Fraction of head-warning time passed")
  3667. (sexp :tag "Face"))))
  3668. ;; FIXME: this is not a good face yet.
  3669. (defface org-agenda-restriction-lock
  3670. (org-compatible-face nil
  3671. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3672. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3673. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3674. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3675. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3676. (t (:inverse-video t))))
  3677. "Face for showing the agenda restriction lock."
  3678. :group 'org-faces)
  3679. (defface org-time-grid ;; font-lock-variable-name-face
  3680. (org-compatible-face nil
  3681. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3682. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3683. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3684. "Face used for time grids."
  3685. :group 'org-faces)
  3686. (defconst org-level-faces
  3687. '(org-level-1 org-level-2 org-level-3 org-level-4
  3688. org-level-5 org-level-6 org-level-7 org-level-8
  3689. ))
  3690. (defcustom org-n-level-faces (length org-level-faces)
  3691. "The number of different faces to be used for headlines.
  3692. Org-mode defines 8 different headline faces, so this can be at most 8.
  3693. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3694. :type 'number
  3695. :group 'org-faces)
  3696. ;;; Functions and variables from ther packages
  3697. ;; Declared here to avoid compiler warnings
  3698. (eval-and-compile
  3699. (unless (fboundp 'declare-function)
  3700. (defmacro declare-function (fn file &optional arglist fileonly))))
  3701. ;; XEmacs only
  3702. (defvar outline-mode-menu-heading)
  3703. (defvar outline-mode-menu-show)
  3704. (defvar outline-mode-menu-hide)
  3705. (defvar zmacs-regions) ; XEmacs regions
  3706. ;; Emacs only
  3707. (defvar mark-active)
  3708. ;; Various packages
  3709. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3710. (declare-function add-to-diary-list "diary-lib"
  3711. (date string specifier &optional marker globcolor literal))
  3712. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3713. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3714. (declare-function Info-goto-node "info" (nodename &optional fork))
  3715. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3716. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3717. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3718. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3719. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3720. (declare-function bbdb-record-name "ext:bbdb" (record))
  3721. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3722. (declare-function bibtex-generate-autokey "bibtex" ())
  3723. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3724. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3725. (defvar calc-embedded-close-formula)
  3726. (defvar calc-embedded-open-formula)
  3727. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3728. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3729. (declare-function calendar-check-holidays "holidays" (date))
  3730. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3731. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3732. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3733. (declare-function calendar-forward-day "cal-move" (arg))
  3734. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3735. (declare-function calendar-goto-date "cal-move" (date))
  3736. (declare-function calendar-goto-today "cal-move" ())
  3737. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3738. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3739. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3740. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3741. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3742. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3743. (defvar calendar-mode-map)
  3744. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3745. (declare-function cdlatex-tab "ext:cdlatex" ())
  3746. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3747. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3748. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3749. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3750. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3751. (defvar font-lock-unfontify-region-function)
  3752. (declare-function gnus-article-show-summary "gnus-art" ())
  3753. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3754. (defvar gnus-other-frame-object)
  3755. (defvar gnus-group-name)
  3756. (defvar gnus-article-current)
  3757. (defvar Info-current-file)
  3758. (defvar Info-current-node)
  3759. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3760. (declare-function mh-find-path "mh-utils" ())
  3761. (declare-function mh-get-header-field "mh-utils" (field))
  3762. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3763. (declare-function mh-header-display "mh-show" ())
  3764. (declare-function mh-index-previous-folder "mh-search" ())
  3765. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3766. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3767. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3768. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3769. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3770. (declare-function mh-show-header-display "mh-show" t t)
  3771. (declare-function mh-show-msg "mh-show" (msg))
  3772. (declare-function mh-show-show "mh-show" t t)
  3773. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3774. (defvar mh-progs)
  3775. (defvar mh-current-folder)
  3776. (defvar mh-show-folder-buffer)
  3777. (defvar mh-index-folder)
  3778. (defvar mh-searcher)
  3779. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3780. (declare-function parse-time-string "parse-time" (string))
  3781. (declare-function remember "remember" (&optional initial))
  3782. (declare-function remember-buffer-desc "remember" ())
  3783. (declare-function remember-finalize "remember" ())
  3784. (defvar remember-save-after-remembering)
  3785. (defvar remember-data-file)
  3786. (defvar remember-register)
  3787. (defvar remember-buffer)
  3788. (defvar remember-handler-functions)
  3789. (defvar remember-annotation-functions)
  3790. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3791. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3792. (declare-function rmail-what-message "rmail" ())
  3793. (defvar rmail-current-message)
  3794. (defvar texmathp-why)
  3795. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3796. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3797. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3798. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3799. (declare-function vm-isearch-update "ext:vm-search" ())
  3800. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3801. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3802. (declare-function vm-su-subject "ext:vm-summary" (m))
  3803. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3804. (defvar vm-message-pointer)
  3805. (defvar vm-folder-directory)
  3806. (defvar w3m-current-url)
  3807. (defvar w3m-current-title)
  3808. ;; backward compatibility to old version of wl
  3809. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3810. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3811. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3812. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3813. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3814. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3815. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3816. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3817. (defvar wl-summary-buffer-elmo-folder)
  3818. (defvar wl-summary-buffer-folder-name)
  3819. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3820. (defvar org-latex-regexps)
  3821. (defvar constants-unit-system)
  3822. ;;; Variables for pre-computed regular expressions, all buffer local
  3823. (defvar org-drawer-regexp nil
  3824. "Matches first line of a hidden block.")
  3825. (make-variable-buffer-local 'org-drawer-regexp)
  3826. (defvar org-todo-regexp nil
  3827. "Matches any of the TODO state keywords.")
  3828. (make-variable-buffer-local 'org-todo-regexp)
  3829. (defvar org-not-done-regexp nil
  3830. "Matches any of the TODO state keywords except the last one.")
  3831. (make-variable-buffer-local 'org-not-done-regexp)
  3832. (defvar org-todo-line-regexp nil
  3833. "Matches a headline and puts TODO state into group 2 if present.")
  3834. (make-variable-buffer-local 'org-todo-line-regexp)
  3835. (defvar org-complex-heading-regexp nil
  3836. "Matches a headline and puts everything into groups:
  3837. group 1: the stars
  3838. group 2: The todo keyword, maybe
  3839. group 3: Priority cookie
  3840. group 4: True headline
  3841. group 5: Tags")
  3842. (make-variable-buffer-local 'org-complex-heading-regexp)
  3843. (defvar org-todo-line-tags-regexp nil
  3844. "Matches a headline and puts TODO state into group 2 if present.
  3845. Also put tags into group 4 if tags are present.")
  3846. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3847. (defvar org-nl-done-regexp nil
  3848. "Matches newline followed by a headline with the DONE keyword.")
  3849. (make-variable-buffer-local 'org-nl-done-regexp)
  3850. (defvar org-looking-at-done-regexp nil
  3851. "Matches the DONE keyword a point.")
  3852. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3853. (defvar org-ds-keyword-length 12
  3854. "Maximum length of the Deadline and SCHEDULED keywords.")
  3855. (make-variable-buffer-local 'org-ds-keyword-length)
  3856. (defvar org-deadline-regexp nil
  3857. "Matches the DEADLINE keyword.")
  3858. (make-variable-buffer-local 'org-deadline-regexp)
  3859. (defvar org-deadline-time-regexp nil
  3860. "Matches the DEADLINE keyword together with a time stamp.")
  3861. (make-variable-buffer-local 'org-deadline-time-regexp)
  3862. (defvar org-deadline-line-regexp nil
  3863. "Matches the DEADLINE keyword and the rest of the line.")
  3864. (make-variable-buffer-local 'org-deadline-line-regexp)
  3865. (defvar org-scheduled-regexp nil
  3866. "Matches the SCHEDULED keyword.")
  3867. (make-variable-buffer-local 'org-scheduled-regexp)
  3868. (defvar org-scheduled-time-regexp nil
  3869. "Matches the SCHEDULED keyword together with a time stamp.")
  3870. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3871. (defvar org-closed-time-regexp nil
  3872. "Matches the CLOSED keyword together with a time stamp.")
  3873. (make-variable-buffer-local 'org-closed-time-regexp)
  3874. (defvar org-keyword-time-regexp nil
  3875. "Matches any of the 4 keywords, together with the time stamp.")
  3876. (make-variable-buffer-local 'org-keyword-time-regexp)
  3877. (defvar org-keyword-time-not-clock-regexp nil
  3878. "Matches any of the 3 keywords, together with the time stamp.")
  3879. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3880. (defvar org-maybe-keyword-time-regexp nil
  3881. "Matches a timestamp, possibly preceeded by a keyword.")
  3882. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3883. (defvar org-planning-or-clock-line-re nil
  3884. "Matches a line with planning or clock info.")
  3885. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3886. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3887. rear-nonsticky t mouse-map t fontified t)
  3888. "Properties to remove when a string without properties is wanted.")
  3889. (defsubst org-match-string-no-properties (num &optional string)
  3890. (if (featurep 'xemacs)
  3891. (let ((s (match-string num string)))
  3892. (remove-text-properties 0 (length s) org-rm-props s)
  3893. s)
  3894. (match-string-no-properties num string)))
  3895. (defsubst org-no-properties (s)
  3896. (if (fboundp 'set-text-properties)
  3897. (set-text-properties 0 (length s) nil s)
  3898. (remove-text-properties 0 (length s) org-rm-props s))
  3899. s)
  3900. (defsubst org-get-alist-option (option key)
  3901. (cond ((eq key t) t)
  3902. ((eq option t) t)
  3903. ((assoc key option) (cdr (assoc key option)))
  3904. (t (cdr (assq 'default option)))))
  3905. (defsubst org-inhibit-invisibility ()
  3906. "Modified `buffer-invisibility-spec' for Emacs 21.
  3907. Some ops with invisible text do not work correctly on Emacs 21. For these
  3908. we turn off invisibility temporarily. Use this in a `let' form."
  3909. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3910. (defsubst org-set-local (var value)
  3911. "Make VAR local in current buffer and set it to VALUE."
  3912. (set (make-variable-buffer-local var) value))
  3913. (defsubst org-mode-p ()
  3914. "Check if the current buffer is in Org-mode."
  3915. (eq major-mode 'org-mode))
  3916. (defsubst org-last (list)
  3917. "Return the last element of LIST."
  3918. (car (last list)))
  3919. (defun org-let (list &rest body)
  3920. (eval (cons 'let (cons list body))))
  3921. (put 'org-let 'lisp-indent-function 1)
  3922. (defun org-let2 (list1 list2 &rest body)
  3923. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3924. (put 'org-let2 'lisp-indent-function 2)
  3925. (defconst org-startup-options
  3926. '(("fold" org-startup-folded t)
  3927. ("overview" org-startup-folded t)
  3928. ("nofold" org-startup-folded nil)
  3929. ("showall" org-startup-folded nil)
  3930. ("content" org-startup-folded content)
  3931. ("hidestars" org-hide-leading-stars t)
  3932. ("showstars" org-hide-leading-stars nil)
  3933. ("odd" org-odd-levels-only t)
  3934. ("oddeven" org-odd-levels-only nil)
  3935. ("align" org-startup-align-all-tables t)
  3936. ("noalign" org-startup-align-all-tables nil)
  3937. ("customtime" org-display-custom-times t)
  3938. ("logdone" org-log-done time)
  3939. ("lognotedone" org-log-done note)
  3940. ("nologdone" org-log-done nil)
  3941. ("lognoteclock-out" org-log-note-clock-out t)
  3942. ("nolognoteclock-out" org-log-note-clock-out nil)
  3943. ("logrepeat" org-log-repeat state)
  3944. ("lognoterepeat" org-log-repeat note)
  3945. ("nologrepeat" org-log-repeat nil)
  3946. ("constcgs" constants-unit-system cgs)
  3947. ("constSI" constants-unit-system SI))
  3948. "Variable associated with STARTUP options for org-mode.
  3949. Each element is a list of three items: The startup options as written
  3950. in the #+STARTUP line, the corresponding variable, and the value to
  3951. set this variable to if the option is found. An optional forth element PUSH
  3952. means to push this value onto the list in the variable.")
  3953. (defun org-set-regexps-and-options ()
  3954. "Precompute regular expressions for current buffer."
  3955. (when (org-mode-p)
  3956. (org-set-local 'org-todo-kwd-alist nil)
  3957. (org-set-local 'org-todo-key-alist nil)
  3958. (org-set-local 'org-todo-key-trigger nil)
  3959. (org-set-local 'org-todo-keywords-1 nil)
  3960. (org-set-local 'org-done-keywords nil)
  3961. (org-set-local 'org-todo-heads nil)
  3962. (org-set-local 'org-todo-sets nil)
  3963. (org-set-local 'org-todo-log-states nil)
  3964. (let ((re (org-make-options-regexp
  3965. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3966. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3967. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3968. (splitre "[ \t]+")
  3969. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3970. tail sep kws1 prio props drawers)
  3971. (save-excursion
  3972. (save-restriction
  3973. (widen)
  3974. (goto-char (point-min))
  3975. (while (re-search-forward re nil t)
  3976. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3977. (cond
  3978. ((equal key "CATEGORY")
  3979. (if (string-match "[ \t]+$" value)
  3980. (setq value (replace-match "" t t value)))
  3981. (setq cat value))
  3982. ((member key '("SEQ_TODO" "TODO"))
  3983. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3984. ((equal key "TYP_TODO")
  3985. (push (cons 'type (org-split-string value splitre)) kwds))
  3986. ((equal key "TAGS")
  3987. (setq tags (append tags (org-split-string value splitre))))
  3988. ((equal key "COLUMNS")
  3989. (org-set-local 'org-columns-default-format value))
  3990. ((equal key "LINK")
  3991. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3992. (push (cons (match-string 1 value)
  3993. (org-trim (match-string 2 value)))
  3994. links)))
  3995. ((equal key "PRIORITIES")
  3996. (setq prio (org-split-string value " +")))
  3997. ((equal key "PROPERTY")
  3998. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3999. (push (cons (match-string 1 value) (match-string 2 value))
  4000. props)))
  4001. ((equal key "DRAWERS")
  4002. (setq drawers (org-split-string value splitre)))
  4003. ((equal key "CONSTANTS")
  4004. (setq const (append const (org-split-string value splitre))))
  4005. ((equal key "STARTUP")
  4006. (let ((opts (org-split-string value splitre))
  4007. l var val)
  4008. (while (setq l (pop opts))
  4009. (when (setq l (assoc l org-startup-options))
  4010. (setq var (nth 1 l) val (nth 2 l))
  4011. (if (not (nth 3 l))
  4012. (set (make-local-variable var) val)
  4013. (if (not (listp (symbol-value var)))
  4014. (set (make-local-variable var) nil))
  4015. (set (make-local-variable var) (symbol-value var))
  4016. (add-to-list var val))))))
  4017. ((equal key "ARCHIVE")
  4018. (string-match " *$" value)
  4019. (setq arch (replace-match "" t t value))
  4020. (remove-text-properties 0 (length arch)
  4021. '(face t fontified t) arch)))
  4022. )))
  4023. (when cat
  4024. (org-set-local 'org-category (intern cat))
  4025. (push (cons "CATEGORY" cat) props))
  4026. (when prio
  4027. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4028. (setq prio (mapcar 'string-to-char prio))
  4029. (org-set-local 'org-highest-priority (nth 0 prio))
  4030. (org-set-local 'org-lowest-priority (nth 1 prio))
  4031. (org-set-local 'org-default-priority (nth 2 prio)))
  4032. (and props (org-set-local 'org-local-properties (nreverse props)))
  4033. (and drawers (org-set-local 'org-drawers drawers))
  4034. (and arch (org-set-local 'org-archive-location arch))
  4035. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4036. ;; Process the TODO keywords
  4037. (unless kwds
  4038. ;; Use the global values as if they had been given locally.
  4039. (setq kwds (default-value 'org-todo-keywords))
  4040. (if (stringp (car kwds))
  4041. (setq kwds (list (cons org-todo-interpretation
  4042. (default-value 'org-todo-keywords)))))
  4043. (setq kwds (reverse kwds)))
  4044. (setq kwds (nreverse kwds))
  4045. (let (inter kws kw)
  4046. (while (setq kws (pop kwds))
  4047. (setq inter (pop kws) sep (member "|" kws)
  4048. kws0 (delete "|" (copy-sequence kws))
  4049. kwsa nil
  4050. kws1 (mapcar
  4051. (lambda (x)
  4052. ;; 1 2
  4053. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4054. (progn
  4055. (setq kw (match-string 1 x)
  4056. key (and (match-end 2) (match-string 2 x))
  4057. log (org-extract-log-state-settings x))
  4058. (push (cons kw (and key (string-to-char key))) kwsa)
  4059. (and log (push log org-todo-log-states))
  4060. kw)
  4061. (error "Invalid TODO keyword %s" x)))
  4062. kws0)
  4063. kwsa (if kwsa (append '((:startgroup))
  4064. (nreverse kwsa)
  4065. '((:endgroup))))
  4066. hw (car kws1)
  4067. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4068. tail (list inter hw (car dws) (org-last dws)))
  4069. (add-to-list 'org-todo-heads hw 'append)
  4070. (push kws1 org-todo-sets)
  4071. (setq org-done-keywords (append org-done-keywords dws nil))
  4072. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4073. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4074. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4075. (setq org-todo-sets (nreverse org-todo-sets)
  4076. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4077. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4078. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4079. ;; Process the constants
  4080. (when const
  4081. (let (e cst)
  4082. (while (setq e (pop const))
  4083. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4084. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4085. (setq org-table-formula-constants-local cst)))
  4086. ;; Process the tags.
  4087. (when tags
  4088. (let (e tgs)
  4089. (while (setq e (pop tags))
  4090. (cond
  4091. ((equal e "{") (push '(:startgroup) tgs))
  4092. ((equal e "}") (push '(:endgroup) tgs))
  4093. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  4094. (push (cons (match-string 1 e)
  4095. (string-to-char (match-string 2 e)))
  4096. tgs))
  4097. (t (push (list e) tgs))))
  4098. (org-set-local 'org-tag-alist nil)
  4099. (while (setq e (pop tgs))
  4100. (or (and (stringp (car e))
  4101. (assoc (car e) org-tag-alist))
  4102. (push e org-tag-alist))))))
  4103. ;; Compute the regular expressions and other local variables
  4104. (if (not org-done-keywords)
  4105. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4106. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4107. (length org-scheduled-string)))
  4108. org-drawer-regexp
  4109. (concat "^[ \t]*:\\("
  4110. (mapconcat 'regexp-quote org-drawers "\\|")
  4111. "\\):[ \t]*$")
  4112. org-not-done-keywords
  4113. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4114. org-todo-regexp
  4115. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4116. "\\|") "\\)\\>")
  4117. org-not-done-regexp
  4118. (concat "\\<\\("
  4119. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4120. "\\)\\>")
  4121. org-todo-line-regexp
  4122. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4123. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4124. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4125. org-complex-heading-regexp
  4126. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4127. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4128. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4129. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4130. org-nl-done-regexp
  4131. (concat "\n\\*+[ \t]+"
  4132. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4133. "\\)" "\\>")
  4134. org-todo-line-tags-regexp
  4135. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4136. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4137. (org-re
  4138. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4139. org-looking-at-done-regexp
  4140. (concat "^" "\\(?:"
  4141. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4142. "\\>")
  4143. org-deadline-regexp (concat "\\<" org-deadline-string)
  4144. org-deadline-time-regexp
  4145. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4146. org-deadline-line-regexp
  4147. (concat "\\<\\(" org-deadline-string "\\).*")
  4148. org-scheduled-regexp
  4149. (concat "\\<" org-scheduled-string)
  4150. org-scheduled-time-regexp
  4151. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4152. org-closed-time-regexp
  4153. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4154. org-keyword-time-regexp
  4155. (concat "\\<\\(" org-scheduled-string
  4156. "\\|" org-deadline-string
  4157. "\\|" org-closed-string
  4158. "\\|" org-clock-string "\\)"
  4159. " *[[<]\\([^]>]+\\)[]>]")
  4160. org-keyword-time-not-clock-regexp
  4161. (concat "\\<\\(" org-scheduled-string
  4162. "\\|" org-deadline-string
  4163. "\\|" org-closed-string
  4164. "\\)"
  4165. " *[[<]\\([^]>]+\\)[]>]")
  4166. org-maybe-keyword-time-regexp
  4167. (concat "\\(\\<\\(" org-scheduled-string
  4168. "\\|" org-deadline-string
  4169. "\\|" org-closed-string
  4170. "\\|" org-clock-string "\\)\\)?"
  4171. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4172. org-planning-or-clock-line-re
  4173. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4174. "\\|" org-deadline-string
  4175. "\\|" org-closed-string "\\|" org-clock-string
  4176. "\\)\\>\\)")
  4177. )
  4178. (org-compute-latex-and-specials-regexp)
  4179. (org-set-font-lock-defaults)))
  4180. (defun org-extract-log-state-settings (x)
  4181. "Extract the log state setting from a TODO keyword string.
  4182. This will extract info from a string like \"WAIT(w@/!)\"."
  4183. (let (kw key log1 log2)
  4184. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4185. (setq kw (match-string 1 x)
  4186. key (and (match-end 2) (match-string 2 x))
  4187. log1 (and (match-end 3) (match-string 3 x))
  4188. log2 (and (match-end 4) (match-string 4 x)))
  4189. (and (or log1 log2)
  4190. (list kw
  4191. (and log1 (if (equal log1 "!") 'time 'note))
  4192. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4193. (defun org-remove-keyword-keys (list)
  4194. "Remove a pair of parenthesis at the end of each string in LIST."
  4195. (mapcar (lambda (x)
  4196. (if (string-match "(.*)$" x)
  4197. (substring x 0 (match-beginning 0))
  4198. x))
  4199. list))
  4200. ;; FIXME: this could be done much better, using second characters etc.
  4201. (defun org-assign-fast-keys (alist)
  4202. "Assign fast keys to a keyword-key alist.
  4203. Respect keys that are already there."
  4204. (let (new e k c c1 c2 (char ?a))
  4205. (while (setq e (pop alist))
  4206. (cond
  4207. ((equal e '(:startgroup)) (push e new))
  4208. ((equal e '(:endgroup)) (push e new))
  4209. (t
  4210. (setq k (car e) c2 nil)
  4211. (if (cdr e)
  4212. (setq c (cdr e))
  4213. ;; automatically assign a character.
  4214. (setq c1 (string-to-char
  4215. (downcase (substring
  4216. k (if (= (string-to-char k) ?@) 1 0)))))
  4217. (if (or (rassoc c1 new) (rassoc c1 alist))
  4218. (while (or (rassoc char new) (rassoc char alist))
  4219. (setq char (1+ char)))
  4220. (setq c2 c1))
  4221. (setq c (or c2 char)))
  4222. (push (cons k c) new))))
  4223. (nreverse new)))
  4224. ;;; Some variables ujsed in various places
  4225. (defvar org-window-configuration nil
  4226. "Used in various places to store a window configuration.")
  4227. (defvar org-finish-function nil
  4228. "Function to be called when `C-c C-c' is used.
  4229. This is for getting out of special buffers like remember.")
  4230. ;; FIXME: Occasionally check by commenting these, to make sure
  4231. ;; no other functions uses these, forgetting to let-bind them.
  4232. (defvar entry)
  4233. (defvar state)
  4234. (defvar last-state)
  4235. (defvar date)
  4236. (defvar description)
  4237. ;; Defined somewhere in this file, but used before definition.
  4238. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4239. (defvar org-agenda-buffer-name)
  4240. (defvar org-agenda-undo-list)
  4241. (defvar org-agenda-pending-undo-list)
  4242. (defvar org-agenda-overriding-header)
  4243. (defvar orgtbl-mode)
  4244. (defvar org-html-entities)
  4245. (defvar org-struct-menu)
  4246. (defvar org-org-menu)
  4247. (defvar org-tbl-menu)
  4248. (defvar org-agenda-keymap)
  4249. ;;;; Emacs/XEmacs compatibility
  4250. ;; Overlay compatibility functions
  4251. (defun org-make-overlay (beg end &optional buffer)
  4252. (if (featurep 'xemacs)
  4253. (make-extent beg end buffer)
  4254. (make-overlay beg end buffer)))
  4255. (defun org-delete-overlay (ovl)
  4256. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4257. (defun org-detach-overlay (ovl)
  4258. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4259. (defun org-move-overlay (ovl beg end &optional buffer)
  4260. (if (featurep 'xemacs)
  4261. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4262. (move-overlay ovl beg end buffer)))
  4263. (defun org-overlay-put (ovl prop value)
  4264. (if (featurep 'xemacs)
  4265. (set-extent-property ovl prop value)
  4266. (overlay-put ovl prop value)))
  4267. (defun org-overlay-display (ovl text &optional face evap)
  4268. "Make overlay OVL display TEXT with face FACE."
  4269. (if (featurep 'xemacs)
  4270. (let ((gl (make-glyph text)))
  4271. (and face (set-glyph-face gl face))
  4272. (set-extent-property ovl 'invisible t)
  4273. (set-extent-property ovl 'end-glyph gl))
  4274. (overlay-put ovl 'display text)
  4275. (if face (overlay-put ovl 'face face))
  4276. (if evap (overlay-put ovl 'evaporate t))))
  4277. (defun org-overlay-before-string (ovl text &optional face evap)
  4278. "Make overlay OVL display TEXT with face FACE."
  4279. (if (featurep 'xemacs)
  4280. (let ((gl (make-glyph text)))
  4281. (and face (set-glyph-face gl face))
  4282. (set-extent-property ovl 'begin-glyph gl))
  4283. (if face (org-add-props text nil 'face face))
  4284. (overlay-put ovl 'before-string text)
  4285. (if evap (overlay-put ovl 'evaporate t))))
  4286. (defun org-overlay-get (ovl prop)
  4287. (if (featurep 'xemacs)
  4288. (extent-property ovl prop)
  4289. (overlay-get ovl prop)))
  4290. (defun org-overlays-at (pos)
  4291. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4292. (defun org-overlays-in (&optional start end)
  4293. (if (featurep 'xemacs)
  4294. (extent-list nil start end)
  4295. (overlays-in start end)))
  4296. (defun org-overlay-start (o)
  4297. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4298. (defun org-overlay-end (o)
  4299. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4300. (defun org-find-overlays (prop &optional pos delete)
  4301. "Find all overlays specifying PROP at POS or point.
  4302. If DELETE is non-nil, delete all those overlays."
  4303. (let ((overlays (org-overlays-at (or pos (point))))
  4304. ov found)
  4305. (while (setq ov (pop overlays))
  4306. (if (org-overlay-get ov prop)
  4307. (if delete (org-delete-overlay ov) (push ov found))))
  4308. found))
  4309. ;; Region compatibility
  4310. (defun org-add-hook (hook function &optional append local)
  4311. "Add-hook, compatible with both Emacsen."
  4312. (if (and local (featurep 'xemacs))
  4313. (add-local-hook hook function append)
  4314. (add-hook hook function append local)))
  4315. (defvar org-ignore-region nil
  4316. "To temporarily disable the active region.")
  4317. (defun org-region-active-p ()
  4318. "Is `transient-mark-mode' on and the region active?
  4319. Works on both Emacs and XEmacs."
  4320. (if org-ignore-region
  4321. nil
  4322. (if (featurep 'xemacs)
  4323. (and zmacs-regions (region-active-p))
  4324. (if (fboundp 'use-region-p)
  4325. (use-region-p)
  4326. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4327. ;; Invisibility compatibility
  4328. (defun org-add-to-invisibility-spec (arg)
  4329. "Add elements to `buffer-invisibility-spec'.
  4330. See documentation for `buffer-invisibility-spec' for the kind of elements
  4331. that can be added."
  4332. (cond
  4333. ((fboundp 'add-to-invisibility-spec)
  4334. (add-to-invisibility-spec arg))
  4335. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4336. (setq buffer-invisibility-spec (list arg)))
  4337. (t
  4338. (setq buffer-invisibility-spec
  4339. (cons arg buffer-invisibility-spec)))))
  4340. (defun org-remove-from-invisibility-spec (arg)
  4341. "Remove elements from `buffer-invisibility-spec'."
  4342. (if (fboundp 'remove-from-invisibility-spec)
  4343. (remove-from-invisibility-spec arg)
  4344. (if (consp buffer-invisibility-spec)
  4345. (setq buffer-invisibility-spec
  4346. (delete arg buffer-invisibility-spec)))))
  4347. (defun org-in-invisibility-spec-p (arg)
  4348. "Is ARG a member of `buffer-invisibility-spec'?"
  4349. (if (consp buffer-invisibility-spec)
  4350. (member arg buffer-invisibility-spec)
  4351. nil))
  4352. ;;;; Define the Org-mode
  4353. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4354. (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."))
  4355. ;; We use a before-change function to check if a table might need
  4356. ;; an update.
  4357. (defvar org-table-may-need-update t
  4358. "Indicates that a table might need an update.
  4359. This variable is set by `org-before-change-function'.
  4360. `org-table-align' sets it back to nil.")
  4361. (defvar org-mode-map)
  4362. (defvar org-mode-hook nil)
  4363. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4364. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4365. (defvar org-table-buffer-is-an nil)
  4366. (defconst org-outline-regexp "\\*+ ")
  4367. ;;;###autoload
  4368. (define-derived-mode org-mode outline-mode "Org"
  4369. "Outline-based notes management and organizer, alias
  4370. \"Carsten's outline-mode for keeping track of everything.\"
  4371. Org-mode develops organizational tasks around a NOTES file which
  4372. contains information about projects as plain text. Org-mode is
  4373. implemented on top of outline-mode, which is ideal to keep the content
  4374. of large files well structured. It supports ToDo items, deadlines and
  4375. time stamps, which magically appear in the diary listing of the Emacs
  4376. calendar. Tables are easily created with a built-in table editor.
  4377. Plain text URL-like links connect to websites, emails (VM), Usenet
  4378. messages (Gnus), BBDB entries, and any files related to the project.
  4379. For printing and sharing of notes, an Org-mode file (or a part of it)
  4380. can be exported as a structured ASCII or HTML file.
  4381. The following commands are available:
  4382. \\{org-mode-map}"
  4383. ;; Get rid of Outline menus, they are not needed
  4384. ;; Need to do this here because define-derived-mode sets up
  4385. ;; the keymap so late. Still, it is a waste to call this each time
  4386. ;; we switch another buffer into org-mode.
  4387. (if (featurep 'xemacs)
  4388. (when (boundp 'outline-mode-menu-heading)
  4389. ;; Assume this is Greg's port, it used easymenu
  4390. (easy-menu-remove outline-mode-menu-heading)
  4391. (easy-menu-remove outline-mode-menu-show)
  4392. (easy-menu-remove outline-mode-menu-hide))
  4393. (define-key org-mode-map [menu-bar headings] 'undefined)
  4394. (define-key org-mode-map [menu-bar hide] 'undefined)
  4395. (define-key org-mode-map [menu-bar show] 'undefined))
  4396. (easy-menu-add org-org-menu)
  4397. (easy-menu-add org-tbl-menu)
  4398. (org-install-agenda-files-menu)
  4399. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4400. (org-add-to-invisibility-spec '(org-cwidth))
  4401. (when (featurep 'xemacs)
  4402. (org-set-local 'line-move-ignore-invisible t))
  4403. (org-set-local 'outline-regexp org-outline-regexp)
  4404. (org-set-local 'outline-level 'org-outline-level)
  4405. (when (and org-ellipsis
  4406. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4407. (fboundp 'make-glyph-code))
  4408. (unless org-display-table
  4409. (setq org-display-table (make-display-table)))
  4410. (set-display-table-slot
  4411. org-display-table 4
  4412. (vconcat (mapcar
  4413. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4414. org-ellipsis)))
  4415. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4416. (setq buffer-display-table org-display-table))
  4417. (org-set-regexps-and-options)
  4418. ;; Calc embedded
  4419. (org-set-local 'calc-embedded-open-mode "# ")
  4420. (modify-syntax-entry ?# "<")
  4421. (modify-syntax-entry ?@ "w")
  4422. (if org-startup-truncated (setq truncate-lines t))
  4423. (org-set-local 'font-lock-unfontify-region-function
  4424. 'org-unfontify-region)
  4425. ;; Activate before-change-function
  4426. (org-set-local 'org-table-may-need-update t)
  4427. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4428. 'local)
  4429. ;; Check for running clock before killing a buffer
  4430. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4431. ;; Paragraphs and auto-filling
  4432. (org-set-autofill-regexps)
  4433. (setq indent-line-function 'org-indent-line-function)
  4434. (org-update-radio-target-regexp)
  4435. ;; Comment characters
  4436. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4437. (org-set-local 'comment-padding " ")
  4438. ;; Align options lines
  4439. (org-set-local
  4440. 'align-mode-rules-list
  4441. '((org-in-buffer-settings
  4442. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4443. (modes . '(org-mode)))))
  4444. ;; Imenu
  4445. (org-set-local 'imenu-create-index-function
  4446. 'org-imenu-get-tree)
  4447. ;; Make isearch reveal context
  4448. (if (or (featurep 'xemacs)
  4449. (not (boundp 'outline-isearch-open-invisible-function)))
  4450. ;; Emacs 21 and XEmacs make use of the hook
  4451. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4452. ;; Emacs 22 deals with this through a special variable
  4453. (org-set-local 'outline-isearch-open-invisible-function
  4454. (lambda (&rest ignore) (org-show-context 'isearch))))
  4455. ;; If empty file that did not turn on org-mode automatically, make it to.
  4456. (if (and org-insert-mode-line-in-empty-file
  4457. (interactive-p)
  4458. (= (point-min) (point-max)))
  4459. (insert "# -*- mode: org -*-\n\n"))
  4460. (unless org-inhibit-startup
  4461. (when org-startup-align-all-tables
  4462. (let ((bmp (buffer-modified-p)))
  4463. (org-table-map-tables 'org-table-align)
  4464. (set-buffer-modified-p bmp)))
  4465. (org-cycle-hide-drawers 'all)
  4466. (cond
  4467. ((eq org-startup-folded t)
  4468. (org-cycle '(4)))
  4469. ((eq org-startup-folded 'content)
  4470. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4471. (org-cycle '(4)) (org-cycle '(4)))))))
  4472. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4473. (defsubst org-call-with-arg (command arg)
  4474. "Call COMMAND interactively, but pretend prefix are was ARG."
  4475. (let ((current-prefix-arg arg)) (call-interactively command)))
  4476. (defsubst org-current-line (&optional pos)
  4477. (save-excursion
  4478. (and pos (goto-char pos))
  4479. ;; works also in narrowed buffer, because we start at 1, not point-min
  4480. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4481. (defun org-current-time ()
  4482. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4483. (if (> org-time-stamp-rounding-minutes 0)
  4484. (let ((r org-time-stamp-rounding-minutes)
  4485. (time (decode-time)))
  4486. (apply 'encode-time
  4487. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4488. (nthcdr 2 time))))
  4489. (current-time)))
  4490. (defun org-add-props (string plist &rest props)
  4491. "Add text properties to entire string, from beginning to end.
  4492. PLIST may be a list of properties, PROPS are individual properties and values
  4493. that will be added to PLIST. Returns the string that was modified."
  4494. (add-text-properties
  4495. 0 (length string) (if props (append plist props) plist) string)
  4496. string)
  4497. (put 'org-add-props 'lisp-indent-function 2)
  4498. ;;;; Font-Lock stuff, including the activators
  4499. (defvar org-mouse-map (make-sparse-keymap))
  4500. (org-defkey org-mouse-map
  4501. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4502. (org-defkey org-mouse-map
  4503. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4504. (when org-mouse-1-follows-link
  4505. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4506. (when org-tab-follows-link
  4507. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4508. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4509. (when org-return-follows-link
  4510. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4511. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4512. (require 'font-lock)
  4513. (defconst org-non-link-chars "]\t\n\r<>")
  4514. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4515. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
  4516. (defvar org-link-re-with-space nil
  4517. "Matches a link with spaces, optional angular brackets around it.")
  4518. (defvar org-link-re-with-space2 nil
  4519. "Matches a link with spaces, optional angular brackets around it.")
  4520. (defvar org-angle-link-re nil
  4521. "Matches link with angular brackets, spaces are allowed.")
  4522. (defvar org-plain-link-re nil
  4523. "Matches plain link, without spaces.")
  4524. (defvar org-bracket-link-regexp nil
  4525. "Matches a link in double brackets.")
  4526. (defvar org-bracket-link-analytic-regexp nil
  4527. "Regular expression used to analyze links.
  4528. Here is what the match groups contain after a match:
  4529. 1: http:
  4530. 2: http
  4531. 3: path
  4532. 4: [desc]
  4533. 5: desc")
  4534. (defvar org-any-link-re nil
  4535. "Regular expression matching any link.")
  4536. (defun org-make-link-regexps ()
  4537. "Update the link regular expressions.
  4538. This should be called after the variable `org-link-types' has changed."
  4539. (setq org-link-re-with-space
  4540. (concat
  4541. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4542. "\\([^" org-non-link-chars " ]"
  4543. "[^" org-non-link-chars "]*"
  4544. "[^" org-non-link-chars " ]\\)>?")
  4545. org-link-re-with-space2
  4546. (concat
  4547. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4548. "\\([^" org-non-link-chars " ]"
  4549. "[^]\t\n\r]*"
  4550. "[^" org-non-link-chars " ]\\)>?")
  4551. org-angle-link-re
  4552. (concat
  4553. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4554. "\\([^" org-non-link-chars " ]"
  4555. "[^" org-non-link-chars "]*"
  4556. "\\)>")
  4557. org-plain-link-re
  4558. (concat
  4559. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4560. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4561. org-bracket-link-regexp
  4562. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4563. org-bracket-link-analytic-regexp
  4564. (concat
  4565. "\\[\\["
  4566. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4567. "\\([^]]+\\)"
  4568. "\\]"
  4569. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4570. "\\]")
  4571. org-any-link-re
  4572. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4573. org-angle-link-re "\\)\\|\\("
  4574. org-plain-link-re "\\)")))
  4575. (org-make-link-regexps)
  4576. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4577. "Regular expression for fast time stamp matching.")
  4578. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4579. "Regular expression for fast time stamp matching.")
  4580. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4581. "Regular expression matching time strings for analysis.
  4582. This one does not require the space after the date.")
  4583. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4584. "Regular expression matching time strings for analysis.")
  4585. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4586. "Regular expression matching time stamps, with groups.")
  4587. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4588. "Regular expression matching time stamps (also [..]), with groups.")
  4589. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4590. "Regular expression matching a time stamp range.")
  4591. (defconst org-tr-regexp-both
  4592. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4593. "Regular expression matching a time stamp range.")
  4594. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4595. org-ts-regexp "\\)?")
  4596. "Regular expression matching a time stamp or time stamp range.")
  4597. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4598. org-ts-regexp-both "\\)?")
  4599. "Regular expression matching a time stamp or time stamp range.
  4600. The time stamps may be either active or inactive.")
  4601. (defvar org-emph-face nil)
  4602. (defun org-do-emphasis-faces (limit)
  4603. "Run through the buffer and add overlays to links."
  4604. (let (rtn)
  4605. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4606. (if (not (= (char-after (match-beginning 3))
  4607. (char-after (match-beginning 4))))
  4608. (progn
  4609. (setq rtn t)
  4610. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4611. 'face
  4612. (nth 1 (assoc (match-string 3)
  4613. org-emphasis-alist)))
  4614. (add-text-properties (match-beginning 2) (match-end 2)
  4615. '(font-lock-multiline t))
  4616. (when org-hide-emphasis-markers
  4617. (add-text-properties (match-end 4) (match-beginning 5)
  4618. '(invisible org-link))
  4619. (add-text-properties (match-beginning 3) (match-end 3)
  4620. '(invisible org-link)))))
  4621. (backward-char 1))
  4622. rtn))
  4623. (defun org-emphasize (&optional char)
  4624. "Insert or change an emphasis, i.e. a font like bold or italic.
  4625. If there is an active region, change that region to a new emphasis.
  4626. If there is no region, just insert the marker characters and position
  4627. the cursor between them.
  4628. CHAR should be either the marker character, or the first character of the
  4629. HTML tag associated with that emphasis. If CHAR is a space, the means
  4630. to remove the emphasis of the selected region.
  4631. If char is not given (for example in an interactive call) it
  4632. will be prompted for."
  4633. (interactive)
  4634. (let ((eal org-emphasis-alist) e det
  4635. (erc org-emphasis-regexp-components)
  4636. (prompt "")
  4637. (string "") beg end move tag c s)
  4638. (if (org-region-active-p)
  4639. (setq beg (region-beginning) end (region-end)
  4640. string (buffer-substring beg end))
  4641. (setq move t))
  4642. (while (setq e (pop eal))
  4643. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4644. c (aref tag 0))
  4645. (push (cons c (string-to-char (car e))) det)
  4646. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4647. (substring tag 1)))))
  4648. (unless char
  4649. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4650. (setq char (read-char-exclusive)))
  4651. (setq char (or (cdr (assoc char det)) char))
  4652. (if (equal char ?\ )
  4653. (setq s "" move nil)
  4654. (unless (assoc (char-to-string char) org-emphasis-alist)
  4655. (error "No such emphasis marker: \"%c\"" char))
  4656. (setq s (char-to-string char)))
  4657. (while (and (> (length string) 1)
  4658. (equal (substring string 0 1) (substring string -1))
  4659. (assoc (substring string 0 1) org-emphasis-alist))
  4660. (setq string (substring string 1 -1)))
  4661. (setq string (concat s string s))
  4662. (if beg (delete-region beg end))
  4663. (unless (or (bolp)
  4664. (string-match (concat "[" (nth 0 erc) "\n]")
  4665. (char-to-string (char-before (point)))))
  4666. (insert " "))
  4667. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4668. (char-to-string (char-after (point))))
  4669. (insert " ") (backward-char 1))
  4670. (insert string)
  4671. (and move (backward-char 1))))
  4672. (defconst org-nonsticky-props
  4673. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4674. (defun org-activate-plain-links (limit)
  4675. "Run through the buffer and add overlays to links."
  4676. (catch 'exit
  4677. (let (f)
  4678. (while (re-search-forward org-plain-link-re limit t)
  4679. (setq f (get-text-property (match-beginning 0) 'face))
  4680. (if (or (eq f 'org-tag)
  4681. (and (listp f) (memq 'org-tag f)))
  4682. nil
  4683. (add-text-properties (match-beginning 0) (match-end 0)
  4684. (list 'mouse-face 'highlight
  4685. 'rear-nonsticky org-nonsticky-props
  4686. 'keymap org-mouse-map
  4687. ))
  4688. (throw 'exit t))))))
  4689. (defun org-activate-code (limit)
  4690. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4691. (unless (get-text-property (match-beginning 1) 'face)
  4692. (remove-text-properties (match-beginning 0) (match-end 0)
  4693. '(display t invisible t intangible t))
  4694. t)))
  4695. (defun org-activate-angle-links (limit)
  4696. "Run through the buffer and add overlays to links."
  4697. (if (re-search-forward org-angle-link-re limit t)
  4698. (progn
  4699. (add-text-properties (match-beginning 0) (match-end 0)
  4700. (list 'mouse-face 'highlight
  4701. 'rear-nonsticky org-nonsticky-props
  4702. 'keymap org-mouse-map
  4703. ))
  4704. t)))
  4705. (defmacro org-maybe-intangible (props)
  4706. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4707. In emacs 21, invisible text is not avoided by the command loop, so the
  4708. intangible property is needed to make sure point skips this text.
  4709. In Emacs 22, this is not necessary. The intangible text property has
  4710. led to problems with flyspell. These problems are fixed in flyspell.el,
  4711. but we still avoid setting the property in Emacs 22 and later.
  4712. We use a macro so that the test can happen at compilation time."
  4713. (if (< emacs-major-version 22)
  4714. `(append '(intangible t) ,props)
  4715. props))
  4716. (defun org-activate-bracket-links (limit)
  4717. "Run through the buffer and add overlays to bracketed links."
  4718. (if (re-search-forward org-bracket-link-regexp limit t)
  4719. (let* ((help (concat "LINK: "
  4720. (org-match-string-no-properties 1)))
  4721. ;; FIXME: above we should remove the escapes.
  4722. ;; but that requires another match, protecting match data,
  4723. ;; a lot of overhead for font-lock.
  4724. (ip (org-maybe-intangible
  4725. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4726. 'keymap org-mouse-map 'mouse-face 'highlight
  4727. 'font-lock-multiline t 'help-echo help)))
  4728. (vp (list 'rear-nonsticky org-nonsticky-props
  4729. 'keymap org-mouse-map 'mouse-face 'highlight
  4730. ' font-lock-multiline t 'help-echo help)))
  4731. ;; We need to remove the invisible property here. Table narrowing
  4732. ;; may have made some of this invisible.
  4733. (remove-text-properties (match-beginning 0) (match-end 0)
  4734. '(invisible nil))
  4735. (if (match-end 3)
  4736. (progn
  4737. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4738. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4739. (add-text-properties (match-end 3) (match-end 0) ip))
  4740. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4741. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4742. (add-text-properties (match-end 1) (match-end 0) ip))
  4743. t)))
  4744. (defun org-activate-dates (limit)
  4745. "Run through the buffer and add overlays to dates."
  4746. (if (re-search-forward org-tsr-regexp-both limit t)
  4747. (progn
  4748. (add-text-properties (match-beginning 0) (match-end 0)
  4749. (list 'mouse-face 'highlight
  4750. 'rear-nonsticky org-nonsticky-props
  4751. 'keymap org-mouse-map))
  4752. (when org-display-custom-times
  4753. (if (match-end 3)
  4754. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4755. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4756. t)))
  4757. (defvar org-target-link-regexp nil
  4758. "Regular expression matching radio targets in plain text.")
  4759. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4760. "Regular expression matching a link target.")
  4761. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4762. "Regular expression matching a radio target.")
  4763. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4764. "Regular expression matching any target.")
  4765. (defun org-activate-target-links (limit)
  4766. "Run through the buffer and add overlays to target matches."
  4767. (when org-target-link-regexp
  4768. (let ((case-fold-search t))
  4769. (if (re-search-forward org-target-link-regexp limit t)
  4770. (progn
  4771. (add-text-properties (match-beginning 0) (match-end 0)
  4772. (list 'mouse-face 'highlight
  4773. 'rear-nonsticky org-nonsticky-props
  4774. 'keymap org-mouse-map
  4775. 'help-echo "Radio target link"
  4776. 'org-linked-text t))
  4777. t)))))
  4778. (defun org-update-radio-target-regexp ()
  4779. "Find all radio targets in this file and update the regular expression."
  4780. (interactive)
  4781. (when (memq 'radio org-activate-links)
  4782. (setq org-target-link-regexp
  4783. (org-make-target-link-regexp (org-all-targets 'radio)))
  4784. (org-restart-font-lock)))
  4785. (defun org-hide-wide-columns (limit)
  4786. (let (s e)
  4787. (setq s (text-property-any (point) (or limit (point-max))
  4788. 'org-cwidth t))
  4789. (when s
  4790. (setq e (next-single-property-change s 'org-cwidth))
  4791. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4792. (goto-char e)
  4793. t)))
  4794. (defvar org-latex-and-specials-regexp nil
  4795. "Regular expression for highlighting export special stuff.")
  4796. (defvar org-match-substring-regexp)
  4797. (defvar org-match-substring-with-braces-regexp)
  4798. (defvar org-export-html-special-string-regexps)
  4799. (defun org-compute-latex-and-specials-regexp ()
  4800. "Compute regular expression for stuff treated specially by exporters."
  4801. (if (not org-highlight-latex-fragments-and-specials)
  4802. (org-set-local 'org-latex-and-specials-regexp nil)
  4803. (let*
  4804. ((matchers (plist-get org-format-latex-options :matchers))
  4805. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4806. org-latex-regexps)))
  4807. (options (org-combine-plists (org-default-export-plist)
  4808. (org-infile-export-plist)))
  4809. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4810. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4811. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4812. (org-export-html-expand (plist-get options :expand-quoted-html))
  4813. (org-export-with-special-strings (plist-get options :special-strings))
  4814. (re-sub
  4815. (cond
  4816. ((equal org-export-with-sub-superscripts '{})
  4817. (list org-match-substring-with-braces-regexp))
  4818. (org-export-with-sub-superscripts
  4819. (list org-match-substring-regexp))
  4820. (t nil)))
  4821. (re-latex
  4822. (if org-export-with-LaTeX-fragments
  4823. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4824. (re-macros
  4825. (if org-export-with-TeX-macros
  4826. (list (concat "\\\\"
  4827. (regexp-opt
  4828. (append (mapcar 'car org-html-entities)
  4829. (if (boundp 'org-latex-entities)
  4830. org-latex-entities nil))
  4831. 'words))) ; FIXME
  4832. ))
  4833. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4834. (re-special (if org-export-with-special-strings
  4835. (mapcar (lambda (x) (car x))
  4836. org-export-html-special-string-regexps)))
  4837. (re-rest
  4838. (delq nil
  4839. (list
  4840. (if org-export-html-expand "@<[^>\n]+>")
  4841. ))))
  4842. (org-set-local
  4843. 'org-latex-and-specials-regexp
  4844. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4845. re-rest) "\\|")))))
  4846. (defface org-latex-and-export-specials
  4847. (let ((font (cond ((assq :inherit custom-face-attributes)
  4848. '(:inherit underline))
  4849. (t '(:underline t)))))
  4850. `((((class grayscale) (background light))
  4851. (:foreground "DimGray" ,@font))
  4852. (((class grayscale) (background dark))
  4853. (:foreground "LightGray" ,@font))
  4854. (((class color) (background light))
  4855. (:foreground "SaddleBrown"))
  4856. (((class color) (background dark))
  4857. (:foreground "burlywood"))
  4858. (t (,@font))))
  4859. "Face used to highlight math latex and other special exporter stuff."
  4860. :group 'org-faces)
  4861. (defun org-do-latex-and-special-faces (limit)
  4862. "Run through the buffer and add overlays to links."
  4863. (when org-latex-and-specials-regexp
  4864. (let (rtn d)
  4865. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4866. limit t))
  4867. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4868. 'face))
  4869. '(org-code org-verbatim underline)))
  4870. (progn
  4871. (setq rtn t
  4872. d (cond ((member (char-after (1+ (match-beginning 0)))
  4873. '(?_ ?^)) 1)
  4874. (t 0)))
  4875. (font-lock-prepend-text-property
  4876. (+ d (match-beginning 0)) (match-end 0)
  4877. 'face 'org-latex-and-export-specials)
  4878. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4879. '(font-lock-multiline t)))))
  4880. rtn)))
  4881. (defun org-restart-font-lock ()
  4882. "Restart font-lock-mode, to force refontification."
  4883. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4884. (font-lock-mode -1)
  4885. (font-lock-mode 1)))
  4886. (defun org-all-targets (&optional radio)
  4887. "Return a list of all targets in this file.
  4888. With optional argument RADIO, only find radio targets."
  4889. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4890. rtn)
  4891. (save-excursion
  4892. (goto-char (point-min))
  4893. (while (re-search-forward re nil t)
  4894. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4895. rtn)))
  4896. (defun org-make-target-link-regexp (targets)
  4897. "Make regular expression matching all strings in TARGETS.
  4898. The regular expression finds the targets also if there is a line break
  4899. between words."
  4900. (and targets
  4901. (concat
  4902. "\\<\\("
  4903. (mapconcat
  4904. (lambda (x)
  4905. (while (string-match " +" x)
  4906. (setq x (replace-match "\\s-+" t t x)))
  4907. x)
  4908. targets
  4909. "\\|")
  4910. "\\)\\>")))
  4911. (defun org-activate-tags (limit)
  4912. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4913. (progn
  4914. (add-text-properties (match-beginning 1) (match-end 1)
  4915. (list 'mouse-face 'highlight
  4916. 'rear-nonsticky org-nonsticky-props
  4917. 'keymap org-mouse-map))
  4918. t)))
  4919. (defun org-outline-level ()
  4920. (save-excursion
  4921. (looking-at outline-regexp)
  4922. (if (match-beginning 1)
  4923. (+ (org-get-string-indentation (match-string 1)) 1000)
  4924. (1- (- (match-end 0) (match-beginning 0))))))
  4925. (defvar org-font-lock-keywords nil)
  4926. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4927. "Regular expression matching a property line.")
  4928. (defun org-set-font-lock-defaults ()
  4929. (let* ((em org-fontify-emphasized-text)
  4930. (lk org-activate-links)
  4931. (org-font-lock-extra-keywords
  4932. (list
  4933. ;; Headlines
  4934. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4935. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4936. ;; Table lines
  4937. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4938. (1 'org-table t))
  4939. ;; Table internals
  4940. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4941. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4942. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4943. ;; Drawers
  4944. (list org-drawer-regexp '(0 'org-special-keyword t))
  4945. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4946. ;; Properties
  4947. (list org-property-re
  4948. '(1 'org-special-keyword t)
  4949. '(3 'org-property-value t))
  4950. (if org-format-transports-properties-p
  4951. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4952. ;; Links
  4953. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4954. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4955. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4956. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4957. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4958. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4959. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4960. '(org-hide-wide-columns (0 nil append))
  4961. ;; TODO lines
  4962. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4963. '(1 (org-get-todo-face 1) t))
  4964. ;; DONE
  4965. (if org-fontify-done-headline
  4966. (list (concat "^[*]+ +\\<\\("
  4967. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4968. "\\)\\(.*\\)")
  4969. '(2 'org-headline-done t))
  4970. nil)
  4971. ;; Priorities
  4972. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4973. ;; Special keywords
  4974. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4975. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4976. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4977. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4978. ;; Emphasis
  4979. (if em
  4980. (if (featurep 'xemacs)
  4981. '(org-do-emphasis-faces (0 nil append))
  4982. '(org-do-emphasis-faces)))
  4983. ;; Checkboxes
  4984. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4985. 2 'bold prepend)
  4986. (if org-provide-checkbox-statistics
  4987. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4988. (0 (org-get-checkbox-statistics-face) t)))
  4989. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4990. '(1 'org-archived prepend))
  4991. ;; Specials
  4992. '(org-do-latex-and-special-faces)
  4993. ;; Code
  4994. '(org-activate-code (1 'org-code t))
  4995. ;; COMMENT
  4996. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4997. "\\|" org-quote-string "\\)\\>")
  4998. '(1 'org-special-keyword t))
  4999. '("^#.*" (0 'font-lock-comment-face t))
  5000. )))
  5001. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5002. ;; Now set the full font-lock-keywords
  5003. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5004. (org-set-local 'font-lock-defaults
  5005. '(org-font-lock-keywords t nil nil backward-paragraph))
  5006. (kill-local-variable 'font-lock-keywords) nil))
  5007. (defvar org-m nil)
  5008. (defvar org-l nil)
  5009. (defvar org-f nil)
  5010. (defun org-get-level-face (n)
  5011. "Get the right face for match N in font-lock matching of healdines."
  5012. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5013. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5014. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5015. (cond
  5016. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5017. ((eq n 2) org-f)
  5018. (t (if org-level-color-stars-only nil org-f))))
  5019. (defun org-get-todo-face (kwd)
  5020. "Get the right face for a TODO keyword KWD.
  5021. If KWD is a number, get the corresponding match group."
  5022. (if (numberp kwd) (setq kwd (match-string kwd)))
  5023. (or (cdr (assoc kwd org-todo-keyword-faces))
  5024. (and (member kwd org-done-keywords) 'org-done)
  5025. 'org-todo))
  5026. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5027. "Remove fontification and activation overlays from links."
  5028. (font-lock-default-unfontify-region beg end)
  5029. (let* ((buffer-undo-list t)
  5030. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5031. (inhibit-modification-hooks t)
  5032. deactivate-mark buffer-file-name buffer-file-truename)
  5033. (remove-text-properties beg end
  5034. '(mouse-face t keymap t org-linked-text t
  5035. invisible t intangible t))))
  5036. ;;;; Visibility cycling, including org-goto and indirect buffer
  5037. ;;; Cycling
  5038. (defvar org-cycle-global-status nil)
  5039. (make-variable-buffer-local 'org-cycle-global-status)
  5040. (defvar org-cycle-subtree-status nil)
  5041. (make-variable-buffer-local 'org-cycle-subtree-status)
  5042. ;;;###autoload
  5043. (defun org-cycle (&optional arg)
  5044. "Visibility cycling for Org-mode.
  5045. - When this function is called with a prefix argument, rotate the entire
  5046. buffer through 3 states (global cycling)
  5047. 1. OVERVIEW: Show only top-level headlines.
  5048. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5049. 3. SHOW ALL: Show everything.
  5050. - When point is at the beginning of a headline, rotate the subtree started
  5051. by this line through 3 different states (local cycling)
  5052. 1. FOLDED: Only the main headline is shown.
  5053. 2. CHILDREN: The main headline and the direct children are shown.
  5054. From this state, you can move to one of the children
  5055. and zoom in further.
  5056. 3. SUBTREE: Show the entire subtree, including body text.
  5057. - When there is a numeric prefix, go up to a heading with level ARG, do
  5058. a `show-subtree' and return to the previous cursor position. If ARG
  5059. is negative, go up that many levels.
  5060. - When point is not at the beginning of a headline, execute
  5061. `indent-relative', like TAB normally does. See the option
  5062. `org-cycle-emulate-tab' for details.
  5063. - Special case: if point is at the beginning of the buffer and there is
  5064. no headline in line 1, this function will act as if called with prefix arg.
  5065. But only if also the variable `org-cycle-global-at-bob' is t."
  5066. (interactive "P")
  5067. (let* ((outline-regexp
  5068. (if (and (org-mode-p) org-cycle-include-plain-lists)
  5069. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  5070. outline-regexp))
  5071. (bob-special (and org-cycle-global-at-bob (bobp)
  5072. (not (looking-at outline-regexp))))
  5073. (org-cycle-hook
  5074. (if bob-special
  5075. (delq 'org-optimize-window-after-visibility-change
  5076. (copy-sequence org-cycle-hook))
  5077. org-cycle-hook))
  5078. (pos (point)))
  5079. (if (or bob-special (equal arg '(4)))
  5080. ;; special case: use global cycling
  5081. (setq arg t))
  5082. (cond
  5083. ((org-at-table-p 'any)
  5084. ;; Enter the table or move to the next field in the table
  5085. (or (org-table-recognize-table.el)
  5086. (progn
  5087. (if arg (org-table-edit-field t)
  5088. (org-table-justify-field-maybe)
  5089. (call-interactively 'org-table-next-field)))))
  5090. ((eq arg t) ;; Global cycling
  5091. (cond
  5092. ((and (eq last-command this-command)
  5093. (eq org-cycle-global-status 'overview))
  5094. ;; We just created the overview - now do table of contents
  5095. ;; This can be slow in very large buffers, so indicate action
  5096. (message "CONTENTS...")
  5097. (org-content)
  5098. (message "CONTENTS...done")
  5099. (setq org-cycle-global-status 'contents)
  5100. (run-hook-with-args 'org-cycle-hook 'contents))
  5101. ((and (eq last-command this-command)
  5102. (eq org-cycle-global-status 'contents))
  5103. ;; We just showed the table of contents - now show everything
  5104. (show-all)
  5105. (message "SHOW ALL")
  5106. (setq org-cycle-global-status 'all)
  5107. (run-hook-with-args 'org-cycle-hook 'all))
  5108. (t
  5109. ;; Default action: go to overview
  5110. (org-overview)
  5111. (message "OVERVIEW")
  5112. (setq org-cycle-global-status 'overview)
  5113. (run-hook-with-args 'org-cycle-hook 'overview))))
  5114. ((and org-drawers org-drawer-regexp
  5115. (save-excursion
  5116. (beginning-of-line 1)
  5117. (looking-at org-drawer-regexp)))
  5118. ;; Toggle block visibility
  5119. (org-flag-drawer
  5120. (not (get-char-property (match-end 0) 'invisible))))
  5121. ((integerp arg)
  5122. ;; Show-subtree, ARG levels up from here.
  5123. (save-excursion
  5124. (org-back-to-heading)
  5125. (outline-up-heading (if (< arg 0) (- arg)
  5126. (- (funcall outline-level) arg)))
  5127. (org-show-subtree)))
  5128. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5129. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5130. ;; At a heading: rotate between three different views
  5131. (org-back-to-heading)
  5132. (let ((goal-column 0) eoh eol eos)
  5133. ;; First, some boundaries
  5134. (save-excursion
  5135. (org-back-to-heading)
  5136. (save-excursion
  5137. (beginning-of-line 2)
  5138. (while (and (not (eobp)) ;; this is like `next-line'
  5139. (get-char-property (1- (point)) 'invisible))
  5140. (beginning-of-line 2)) (setq eol (point)))
  5141. (outline-end-of-heading) (setq eoh (point))
  5142. (org-end-of-subtree t)
  5143. (unless (eobp)
  5144. (skip-chars-forward " \t\n")
  5145. (beginning-of-line 1) ; in case this is an item
  5146. )
  5147. (setq eos (1- (point))))
  5148. ;; Find out what to do next and set `this-command'
  5149. (cond
  5150. ((= eos eoh)
  5151. ;; Nothing is hidden behind this heading
  5152. (message "EMPTY ENTRY")
  5153. (setq org-cycle-subtree-status nil)
  5154. (save-excursion
  5155. (goto-char eos)
  5156. (outline-next-heading)
  5157. (if (org-invisible-p) (org-flag-heading nil))))
  5158. ((or (>= eol eos)
  5159. (not (string-match "\\S-" (buffer-substring eol eos))))
  5160. ;; Entire subtree is hidden in one line: open it
  5161. (org-show-entry)
  5162. (show-children)
  5163. (message "CHILDREN")
  5164. (save-excursion
  5165. (goto-char eos)
  5166. (outline-next-heading)
  5167. (if (org-invisible-p) (org-flag-heading nil)))
  5168. (setq org-cycle-subtree-status 'children)
  5169. (run-hook-with-args 'org-cycle-hook 'children))
  5170. ((and (eq last-command this-command)
  5171. (eq org-cycle-subtree-status 'children))
  5172. ;; We just showed the children, now show everything.
  5173. (org-show-subtree)
  5174. (message "SUBTREE")
  5175. (setq org-cycle-subtree-status 'subtree)
  5176. (run-hook-with-args 'org-cycle-hook 'subtree))
  5177. (t
  5178. ;; Default action: hide the subtree.
  5179. (hide-subtree)
  5180. (message "FOLDED")
  5181. (setq org-cycle-subtree-status 'folded)
  5182. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5183. ;; TAB emulation
  5184. (buffer-read-only (org-back-to-heading))
  5185. ((org-try-cdlatex-tab))
  5186. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5187. (or (not (bolp))
  5188. (not (looking-at outline-regexp))))
  5189. (call-interactively (global-key-binding "\t")))
  5190. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5191. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5192. (or (and (eq org-cycle-emulate-tab 'white)
  5193. (= (match-end 0) (point-at-eol)))
  5194. (and (eq org-cycle-emulate-tab 'whitestart)
  5195. (>= (match-end 0) pos))))
  5196. t
  5197. (eq org-cycle-emulate-tab t))
  5198. ; (if (and (looking-at "[ \n\r\t]")
  5199. ; (string-match "^[ \t]*$" (buffer-substring
  5200. ; (point-at-bol) (point))))
  5201. ; (progn
  5202. ; (beginning-of-line 1)
  5203. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5204. (call-interactively (global-key-binding "\t")))
  5205. (t (save-excursion
  5206. (org-back-to-heading)
  5207. (org-cycle))))))
  5208. ;;;###autoload
  5209. (defun org-global-cycle (&optional arg)
  5210. "Cycle the global visibility. For details see `org-cycle'."
  5211. (interactive "P")
  5212. (let ((org-cycle-include-plain-lists
  5213. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5214. (if (integerp arg)
  5215. (progn
  5216. (show-all)
  5217. (hide-sublevels arg)
  5218. (setq org-cycle-global-status 'contents))
  5219. (org-cycle '(4)))))
  5220. (defun org-overview ()
  5221. "Switch to overview mode, shoing only top-level headlines.
  5222. Really, this shows all headlines with level equal or greater than the level
  5223. of the first headline in the buffer. This is important, because if the
  5224. first headline is not level one, then (hide-sublevels 1) gives confusing
  5225. results."
  5226. (interactive)
  5227. (let ((level (save-excursion
  5228. (goto-char (point-min))
  5229. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5230. (progn
  5231. (goto-char (match-beginning 0))
  5232. (funcall outline-level))))))
  5233. (and level (hide-sublevels level))))
  5234. (defun org-content (&optional arg)
  5235. "Show all headlines in the buffer, like a table of contents.
  5236. With numerical argument N, show content up to level N."
  5237. (interactive "P")
  5238. (save-excursion
  5239. ;; Visit all headings and show their offspring
  5240. (and (integerp arg) (org-overview))
  5241. (goto-char (point-max))
  5242. (catch 'exit
  5243. (while (and (progn (condition-case nil
  5244. (outline-previous-visible-heading 1)
  5245. (error (goto-char (point-min))))
  5246. t)
  5247. (looking-at outline-regexp))
  5248. (if (integerp arg)
  5249. (show-children (1- arg))
  5250. (show-branches))
  5251. (if (bobp) (throw 'exit nil))))))
  5252. (defun org-optimize-window-after-visibility-change (state)
  5253. "Adjust the window after a change in outline visibility.
  5254. This function is the default value of the hook `org-cycle-hook'."
  5255. (when (get-buffer-window (current-buffer))
  5256. (cond
  5257. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5258. ; ((eq state 'overview) (org-beginning-of-line))
  5259. ((eq state 'content) nil)
  5260. ((eq state 'all) nil)
  5261. ((eq state 'folded) nil)
  5262. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5263. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5264. (defun org-compact-display-after-subtree-move ()
  5265. (let (beg end)
  5266. (save-excursion
  5267. (if (org-up-heading-safe)
  5268. (progn
  5269. (hide-subtree)
  5270. (show-entry)
  5271. (show-children)
  5272. (org-cycle-show-empty-lines 'children)
  5273. (org-cycle-hide-drawers 'children))
  5274. (org-overview)))))
  5275. (defun org-cycle-show-empty-lines (state)
  5276. "Show empty lines above all visible headlines.
  5277. The region to be covered depends on STATE when called through
  5278. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5279. entire buffer covered. Note that an empty line is only shown if there
  5280. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5281. (when (> org-cycle-separator-lines 0)
  5282. (save-excursion
  5283. (let* ((n org-cycle-separator-lines)
  5284. (re (cond
  5285. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5286. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5287. (t (let ((ns (number-to-string (- n 2))))
  5288. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5289. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5290. beg end)
  5291. (cond
  5292. ((memq state '(overview contents t))
  5293. (setq beg (point-min) end (point-max)))
  5294. ((memq state '(children folded))
  5295. (setq beg (point) end (progn (org-end-of-subtree t t)
  5296. (beginning-of-line 2)
  5297. (point)))))
  5298. (when beg
  5299. (goto-char beg)
  5300. (while (re-search-forward re end t)
  5301. (if (not (get-char-property (match-end 1) 'invisible))
  5302. (outline-flag-region
  5303. (match-beginning 1) (match-end 1) nil)))))))
  5304. ;; Never hide empty lines at the end of the file.
  5305. (save-excursion
  5306. (goto-char (point-max))
  5307. (outline-previous-heading)
  5308. (outline-end-of-heading)
  5309. (if (and (looking-at "[ \t\n]+")
  5310. (= (match-end 0) (point-max)))
  5311. (outline-flag-region (point) (match-end 0) nil))))
  5312. (defun org-subtree-end-visible-p ()
  5313. "Is the end of the current subtree visible?"
  5314. (pos-visible-in-window-p
  5315. (save-excursion (org-end-of-subtree t) (point))))
  5316. (defun org-first-headline-recenter (&optional N)
  5317. "Move cursor to the first headline and recenter the headline.
  5318. Optional argument N means, put the headline into the Nth line of the window."
  5319. (goto-char (point-min))
  5320. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5321. (beginning-of-line)
  5322. (recenter (prefix-numeric-value N))))
  5323. ;;; Org-goto
  5324. (defvar org-goto-window-configuration nil)
  5325. (defvar org-goto-marker nil)
  5326. (defvar org-goto-map
  5327. (let ((map (make-sparse-keymap)))
  5328. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5329. (while (setq cmd (pop cmds))
  5330. (substitute-key-definition cmd cmd map global-map)))
  5331. (suppress-keymap map)
  5332. (org-defkey map "\C-m" 'org-goto-ret)
  5333. (org-defkey map [(return)] 'org-goto-ret)
  5334. (org-defkey map [(left)] 'org-goto-left)
  5335. (org-defkey map [(right)] 'org-goto-right)
  5336. (org-defkey map [(control ?g)] 'org-goto-quit)
  5337. (org-defkey map "\C-i" 'org-cycle)
  5338. (org-defkey map [(tab)] 'org-cycle)
  5339. (org-defkey map [(down)] 'outline-next-visible-heading)
  5340. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5341. (if org-goto-auto-isearch
  5342. (if (fboundp 'define-key-after)
  5343. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5344. nil)
  5345. (org-defkey map "q" 'org-goto-quit)
  5346. (org-defkey map "n" 'outline-next-visible-heading)
  5347. (org-defkey map "p" 'outline-previous-visible-heading)
  5348. (org-defkey map "f" 'outline-forward-same-level)
  5349. (org-defkey map "b" 'outline-backward-same-level)
  5350. (org-defkey map "u" 'outline-up-heading))
  5351. (org-defkey map "/" 'org-occur)
  5352. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5353. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5354. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5355. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5356. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5357. map))
  5358. (defconst org-goto-help
  5359. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5360. RET=jump to location [Q]uit and return to previous location
  5361. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5362. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5363. (defun org-goto (&optional alternative-interface)
  5364. "Look up a different location in the current file, keeping current visibility.
  5365. When you want look-up or go to a different location in a document, the
  5366. fastest way is often to fold the entire buffer and then dive into the tree.
  5367. This method has the disadvantage, that the previous location will be folded,
  5368. which may not be what you want.
  5369. This command works around this by showing a copy of the current buffer
  5370. in an indirect buffer, in overview mode. You can dive into the tree in
  5371. that copy, use org-occur and incremental search to find a location.
  5372. When pressing RET or `Q', the command returns to the original buffer in
  5373. which the visibility is still unchanged. After RET is will also jump to
  5374. the location selected in the indirect buffer and expose the
  5375. the headline hierarchy above."
  5376. (interactive "P")
  5377. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5378. (org-refile-use-outline-path t)
  5379. (interface
  5380. (if (not alternative-interface)
  5381. org-goto-interface
  5382. (if (eq org-goto-interface 'outline)
  5383. 'outline-path-completion
  5384. 'outline)))
  5385. (org-goto-start-pos (point))
  5386. (selected-point
  5387. (if (eq interface 'outline)
  5388. (car (org-get-location (current-buffer) org-goto-help))
  5389. (nth 3 (org-refile-get-location "Goto: ")))))
  5390. (if selected-point
  5391. (progn
  5392. (org-mark-ring-push org-goto-start-pos)
  5393. (goto-char selected-point)
  5394. (if (or (org-invisible-p) (org-invisible-p2))
  5395. (org-show-context 'org-goto)))
  5396. (message "Quit"))))
  5397. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5398. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5399. (defvar org-goto-local-auto-isearch-map) ; defined below
  5400. (defun org-get-location (buf help)
  5401. "Let the user select a location in the Org-mode buffer BUF.
  5402. This function uses a recursive edit. It returns the selected position
  5403. or nil."
  5404. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5405. (isearch-hide-immediately nil)
  5406. (isearch-search-fun-function
  5407. (lambda () 'org-goto-local-search-forward-headings))
  5408. (org-goto-selected-point org-goto-exit-command))
  5409. (save-excursion
  5410. (save-window-excursion
  5411. (delete-other-windows)
  5412. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5413. (switch-to-buffer
  5414. (condition-case nil
  5415. (make-indirect-buffer (current-buffer) "*org-goto*")
  5416. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5417. (with-output-to-temp-buffer "*Help*"
  5418. (princ help))
  5419. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5420. (setq buffer-read-only nil)
  5421. (let ((org-startup-truncated t)
  5422. (org-startup-folded nil)
  5423. (org-startup-align-all-tables nil))
  5424. (org-mode)
  5425. (org-overview))
  5426. (setq buffer-read-only t)
  5427. (if (and (boundp 'org-goto-start-pos)
  5428. (integer-or-marker-p org-goto-start-pos))
  5429. (let ((org-show-hierarchy-above t)
  5430. (org-show-siblings t)
  5431. (org-show-following-heading t))
  5432. (goto-char org-goto-start-pos)
  5433. (and (org-invisible-p) (org-show-context)))
  5434. (goto-char (point-min)))
  5435. (org-beginning-of-line)
  5436. (message "Select location and press RET")
  5437. (use-local-map org-goto-map)
  5438. (recursive-edit)
  5439. ))
  5440. (kill-buffer "*org-goto*")
  5441. (cons org-goto-selected-point org-goto-exit-command)))
  5442. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5443. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5444. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5445. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5446. (defun org-goto-local-search-forward-headings (string bound noerror)
  5447. "Search and make sure that anu matches are in headlines."
  5448. (catch 'return
  5449. (while (search-forward string bound noerror)
  5450. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5451. (and (member :headline context)
  5452. (not (member :tags context))))
  5453. (throw 'return (point))))))
  5454. (defun org-goto-local-auto-isearch ()
  5455. "Start isearch."
  5456. (interactive)
  5457. (goto-char (point-min))
  5458. (let ((keys (this-command-keys)))
  5459. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5460. (isearch-mode t)
  5461. (isearch-process-search-char (string-to-char keys)))))
  5462. (defun org-goto-ret (&optional arg)
  5463. "Finish `org-goto' by going to the new location."
  5464. (interactive "P")
  5465. (setq org-goto-selected-point (point)
  5466. org-goto-exit-command 'return)
  5467. (throw 'exit nil))
  5468. (defun org-goto-left ()
  5469. "Finish `org-goto' by going to the new location."
  5470. (interactive)
  5471. (if (org-on-heading-p)
  5472. (progn
  5473. (beginning-of-line 1)
  5474. (setq org-goto-selected-point (point)
  5475. org-goto-exit-command 'left)
  5476. (throw 'exit nil))
  5477. (error "Not on a heading")))
  5478. (defun org-goto-right ()
  5479. "Finish `org-goto' by going to the new location."
  5480. (interactive)
  5481. (if (org-on-heading-p)
  5482. (progn
  5483. (setq org-goto-selected-point (point)
  5484. org-goto-exit-command 'right)
  5485. (throw 'exit nil))
  5486. (error "Not on a heading")))
  5487. (defun org-goto-quit ()
  5488. "Finish `org-goto' without cursor motion."
  5489. (interactive)
  5490. (setq org-goto-selected-point nil)
  5491. (setq org-goto-exit-command 'quit)
  5492. (throw 'exit nil))
  5493. ;;; Indirect buffer display of subtrees
  5494. (defvar org-indirect-dedicated-frame nil
  5495. "This is the frame being used for indirect tree display.")
  5496. (defvar org-last-indirect-buffer nil)
  5497. (defun org-tree-to-indirect-buffer (&optional arg)
  5498. "Create indirect buffer and narrow it to current subtree.
  5499. With numerical prefix ARG, go up to this level and then take that tree.
  5500. If ARG is negative, go up that many levels.
  5501. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5502. indirect buffer previously made with this command, to avoid proliferation of
  5503. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5504. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5505. is kept so that you can work with several indirect buffers at the same time.
  5506. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5507. requests that a new frame be made for the new buffer, so that the dedicated
  5508. frame is not changed."
  5509. (interactive "P")
  5510. (let ((cbuf (current-buffer))
  5511. (cwin (selected-window))
  5512. (pos (point))
  5513. beg end level heading ibuf)
  5514. (save-excursion
  5515. (org-back-to-heading t)
  5516. (when (numberp arg)
  5517. (setq level (org-outline-level))
  5518. (if (< arg 0) (setq arg (+ level arg)))
  5519. (while (> (setq level (org-outline-level)) arg)
  5520. (outline-up-heading 1 t)))
  5521. (setq beg (point)
  5522. heading (org-get-heading))
  5523. (org-end-of-subtree t) (setq end (point)))
  5524. (if (and (buffer-live-p org-last-indirect-buffer)
  5525. (not (eq org-indirect-buffer-display 'new-frame))
  5526. (not arg))
  5527. (kill-buffer org-last-indirect-buffer))
  5528. (setq ibuf (org-get-indirect-buffer cbuf)
  5529. org-last-indirect-buffer ibuf)
  5530. (cond
  5531. ((or (eq org-indirect-buffer-display 'new-frame)
  5532. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5533. (select-frame (make-frame))
  5534. (delete-other-windows)
  5535. (switch-to-buffer ibuf)
  5536. (org-set-frame-title heading))
  5537. ((eq org-indirect-buffer-display 'dedicated-frame)
  5538. (raise-frame
  5539. (select-frame (or (and org-indirect-dedicated-frame
  5540. (frame-live-p org-indirect-dedicated-frame)
  5541. org-indirect-dedicated-frame)
  5542. (setq org-indirect-dedicated-frame (make-frame)))))
  5543. (delete-other-windows)
  5544. (switch-to-buffer ibuf)
  5545. (org-set-frame-title (concat "Indirect: " heading)))
  5546. ((eq org-indirect-buffer-display 'current-window)
  5547. (switch-to-buffer ibuf))
  5548. ((eq org-indirect-buffer-display 'other-window)
  5549. (pop-to-buffer ibuf))
  5550. (t (error "Invalid value.")))
  5551. (if (featurep 'xemacs)
  5552. (save-excursion (org-mode) (turn-on-font-lock)))
  5553. (narrow-to-region beg end)
  5554. (show-all)
  5555. (goto-char pos)
  5556. (and (window-live-p cwin) (select-window cwin))))
  5557. (defun org-get-indirect-buffer (&optional buffer)
  5558. (setq buffer (or buffer (current-buffer)))
  5559. (let ((n 1) (base (buffer-name buffer)) bname)
  5560. (while (buffer-live-p
  5561. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5562. (setq n (1+ n)))
  5563. (condition-case nil
  5564. (make-indirect-buffer buffer bname 'clone)
  5565. (error (make-indirect-buffer buffer bname)))))
  5566. (defun org-set-frame-title (title)
  5567. "Set the title of the current frame to the string TITLE."
  5568. ;; FIXME: how to name a single frame in XEmacs???
  5569. (unless (featurep 'xemacs)
  5570. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5571. ;;;; Structure editing
  5572. ;;; Inserting headlines
  5573. (defun org-insert-heading (&optional force-heading)
  5574. "Insert a new heading or item with same depth at point.
  5575. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5576. If point is at the beginning of a headline, insert a sibling before the
  5577. current headline. If point is not at the beginning, do not split the line,
  5578. but create the new hedline after the current line."
  5579. (interactive "P")
  5580. (if (= (buffer-size) 0)
  5581. (insert "\n* ")
  5582. (when (or force-heading (not (org-insert-item)))
  5583. (let* ((head (save-excursion
  5584. (condition-case nil
  5585. (progn
  5586. (org-back-to-heading)
  5587. (match-string 0))
  5588. (error "*"))))
  5589. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5590. pos)
  5591. (cond
  5592. ((and (org-on-heading-p) (bolp)
  5593. (or (bobp)
  5594. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5595. ;; insert before the current line
  5596. (open-line (if blank 2 1)))
  5597. ((and (bolp)
  5598. (or (bobp)
  5599. (save-excursion
  5600. (backward-char 1) (not (org-invisible-p)))))
  5601. ;; insert right here
  5602. nil)
  5603. (t
  5604. ; ;; in the middle of the line
  5605. ; (org-show-entry)
  5606. ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
  5607. ; (if (and
  5608. ; (org-on-heading-p)
  5609. ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
  5610. ; ;; protect the tags
  5611. ;; (let ((tags (match-string 2)) pos)
  5612. ; (delete-region (match-beginning 1) (match-end 1))
  5613. ; (setq pos (point-at-bol))
  5614. ; (newline (if blank 2 1))
  5615. ; (save-excursion
  5616. ; (goto-char pos)
  5617. ; (end-of-line 1)
  5618. ; (insert " " tags)
  5619. ; (org-set-tags nil 'align)))
  5620. ; (newline (if blank 2 1)))
  5621. ; (newline (if blank 2 1))))
  5622. ;; in the middle of the line
  5623. (org-show-entry)
  5624. (let ((split
  5625. (org-get-alist-option org-M-RET-may-split-line 'headline))
  5626. tags pos)
  5627. (if (org-on-heading-p)
  5628. (progn
  5629. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5630. (setq tags (and (match-end 2) (match-string 2)))
  5631. (and (match-end 1)
  5632. (delete-region (match-beginning 1) (match-end 1)))
  5633. (setq pos (point-at-bol))
  5634. (or split (end-of-line 1))
  5635. (delete-horizontal-space)
  5636. (newline (if blank 2 1))
  5637. (when tags
  5638. (save-excursion
  5639. (goto-char pos)
  5640. (end-of-line 1)
  5641. (insert " " tags)
  5642. (org-set-tags nil 'align))))
  5643. (or split (end-of-line 1))
  5644. (newline (if blank 2 1))))))
  5645. (insert head) (just-one-space)
  5646. (setq pos (point))
  5647. (end-of-line 1)
  5648. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5649. (run-hooks 'org-insert-heading-hook)))))
  5650. (defun org-insert-heading-after-current ()
  5651. "Insert a new heading with same level as current, after current subtree."
  5652. (interactive)
  5653. (org-back-to-heading)
  5654. (org-insert-heading)
  5655. (org-move-subtree-down)
  5656. (end-of-line 1))
  5657. (defun org-insert-todo-heading (arg)
  5658. "Insert a new heading with the same level and TODO state as current heading.
  5659. If the heading has no TODO state, or if the state is DONE, use the first
  5660. state (TODO by default). Also with prefix arg, force first state."
  5661. (interactive "P")
  5662. (when (not (org-insert-item 'checkbox))
  5663. (org-insert-heading)
  5664. (save-excursion
  5665. (org-back-to-heading)
  5666. (outline-previous-heading)
  5667. (looking-at org-todo-line-regexp))
  5668. (if (or arg
  5669. (not (match-beginning 2))
  5670. (member (match-string 2) org-done-keywords))
  5671. (insert (car org-todo-keywords-1) " ")
  5672. (insert (match-string 2) " "))))
  5673. (defun org-insert-subheading (arg)
  5674. "Insert a new subheading and demote it.
  5675. Works for outline headings and for plain lists alike."
  5676. (interactive "P")
  5677. (org-insert-heading arg)
  5678. (cond
  5679. ((org-on-heading-p) (org-do-demote))
  5680. ((org-at-item-p) (org-indent-item 1))))
  5681. (defun org-insert-todo-subheading (arg)
  5682. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5683. Works for outline headings and for plain lists alike."
  5684. (interactive "P")
  5685. (org-insert-todo-heading arg)
  5686. (cond
  5687. ((org-on-heading-p) (org-do-demote))
  5688. ((org-at-item-p) (org-indent-item 1))))
  5689. ;;; Promotion and Demotion
  5690. (defun org-promote-subtree ()
  5691. "Promote the entire subtree.
  5692. See also `org-promote'."
  5693. (interactive)
  5694. (save-excursion
  5695. (org-map-tree 'org-promote))
  5696. (org-fix-position-after-promote))
  5697. (defun org-demote-subtree ()
  5698. "Demote the entire subtree. See `org-demote'.
  5699. See also `org-promote'."
  5700. (interactive)
  5701. (save-excursion
  5702. (org-map-tree 'org-demote))
  5703. (org-fix-position-after-promote))
  5704. (defun org-do-promote ()
  5705. "Promote the current heading higher up the tree.
  5706. If the region is active in `transient-mark-mode', promote all headings
  5707. in the region."
  5708. (interactive)
  5709. (save-excursion
  5710. (if (org-region-active-p)
  5711. (org-map-region 'org-promote (region-beginning) (region-end))
  5712. (org-promote)))
  5713. (org-fix-position-after-promote))
  5714. (defun org-do-demote ()
  5715. "Demote the current heading lower down the tree.
  5716. If the region is active in `transient-mark-mode', demote all headings
  5717. in the region."
  5718. (interactive)
  5719. (save-excursion
  5720. (if (org-region-active-p)
  5721. (org-map-region 'org-demote (region-beginning) (region-end))
  5722. (org-demote)))
  5723. (org-fix-position-after-promote))
  5724. (defun org-fix-position-after-promote ()
  5725. "Make sure that after pro/demotion cursor position is right."
  5726. (let ((pos (point)))
  5727. (when (save-excursion
  5728. (beginning-of-line 1)
  5729. (looking-at org-todo-line-regexp)
  5730. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5731. (cond ((eobp) (insert " "))
  5732. ((eolp) (insert " "))
  5733. ((equal (char-after) ?\ ) (forward-char 1))))))
  5734. (defun org-reduced-level (l)
  5735. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5736. (defun org-get-legal-level (level &optional change)
  5737. "Rectify a level change under the influence of `org-odd-levels-only'
  5738. LEVEL is a current level, CHANGE is by how much the level should be
  5739. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5740. even level numbers will become the next higher odd number."
  5741. (if org-odd-levels-only
  5742. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5743. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5744. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5745. (max 1 (+ level change))))
  5746. (defun org-promote ()
  5747. "Promote the current heading higher up the tree.
  5748. If the region is active in `transient-mark-mode', promote all headings
  5749. in the region."
  5750. (org-back-to-heading t)
  5751. (let* ((level (save-match-data (funcall outline-level)))
  5752. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5753. (diff (abs (- level (length up-head) -1))))
  5754. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5755. (replace-match up-head nil t)
  5756. ;; Fixup tag positioning
  5757. (and org-auto-align-tags (org-set-tags nil t))
  5758. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5759. (defun org-demote ()
  5760. "Demote the current heading lower down the tree.
  5761. If the region is active in `transient-mark-mode', demote all headings
  5762. in the region."
  5763. (org-back-to-heading t)
  5764. (let* ((level (save-match-data (funcall outline-level)))
  5765. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5766. (diff (abs (- level (length down-head) -1))))
  5767. (replace-match down-head nil t)
  5768. ;; Fixup tag positioning
  5769. (and org-auto-align-tags (org-set-tags nil t))
  5770. (if org-adapt-indentation (org-fixup-indentation diff))))
  5771. (defun org-map-tree (fun)
  5772. "Call FUN for every heading underneath the current one."
  5773. (org-back-to-heading)
  5774. (let ((level (funcall outline-level)))
  5775. (save-excursion
  5776. (funcall fun)
  5777. (while (and (progn
  5778. (outline-next-heading)
  5779. (> (funcall outline-level) level))
  5780. (not (eobp)))
  5781. (funcall fun)))))
  5782. (defun org-map-region (fun beg end)
  5783. "Call FUN for every heading between BEG and END."
  5784. (let ((org-ignore-region t))
  5785. (save-excursion
  5786. (setq end (copy-marker end))
  5787. (goto-char beg)
  5788. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5789. (< (point) end))
  5790. (funcall fun))
  5791. (while (and (progn
  5792. (outline-next-heading)
  5793. (< (point) end))
  5794. (not (eobp)))
  5795. (funcall fun)))))
  5796. (defun org-fixup-indentation (diff)
  5797. "Change the indentation in the current entry by DIFF
  5798. However, if any line in the current entry has no indentation, or if it
  5799. would end up with no indentation after the change, nothing at all is done."
  5800. (save-excursion
  5801. (let ((end (save-excursion (outline-next-heading)
  5802. (point-marker)))
  5803. (prohibit (if (> diff 0)
  5804. "^\\S-"
  5805. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5806. col)
  5807. (unless (save-excursion (end-of-line 1)
  5808. (re-search-forward prohibit end t))
  5809. (while (and (< (point) end)
  5810. (re-search-forward "^[ \t]+" end t))
  5811. (goto-char (match-end 0))
  5812. (setq col (current-column))
  5813. (if (< diff 0) (replace-match ""))
  5814. (indent-to (+ diff col))))
  5815. (move-marker end nil))))
  5816. (defun org-convert-to-odd-levels ()
  5817. "Convert an org-mode file with all levels allowed to one with odd levels.
  5818. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5819. level 5 etc."
  5820. (interactive)
  5821. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5822. (let ((org-odd-levels-only nil) n)
  5823. (save-excursion
  5824. (goto-char (point-min))
  5825. (while (re-search-forward "^\\*\\*+ " nil t)
  5826. (setq n (- (length (match-string 0)) 2))
  5827. (while (>= (setq n (1- n)) 0)
  5828. (org-demote))
  5829. (end-of-line 1))))))
  5830. (defun org-convert-to-oddeven-levels ()
  5831. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5832. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5833. section with an even level, conversion would destroy the structure of the file. An error
  5834. is signaled in this case."
  5835. (interactive)
  5836. (goto-char (point-min))
  5837. ;; First check if there are no even levels
  5838. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5839. (org-show-context t)
  5840. (error "Not all levels are odd in this file. Conversion not possible."))
  5841. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5842. (let ((org-odd-levels-only nil) n)
  5843. (save-excursion
  5844. (goto-char (point-min))
  5845. (while (re-search-forward "^\\*\\*+ " nil t)
  5846. (setq n (/ (1- (length (match-string 0))) 2))
  5847. (while (>= (setq n (1- n)) 0)
  5848. (org-promote))
  5849. (end-of-line 1))))))
  5850. (defun org-tr-level (n)
  5851. "Make N odd if required."
  5852. (if org-odd-levels-only (1+ (/ n 2)) n))
  5853. ;;; Vertical tree motion, cutting and pasting of subtrees
  5854. (defun org-move-subtree-up (&optional arg)
  5855. "Move the current subtree up past ARG headlines of the same level."
  5856. (interactive "p")
  5857. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5858. (defun org-move-subtree-down (&optional arg)
  5859. "Move the current subtree down past ARG headlines of the same level."
  5860. (interactive "p")
  5861. (setq arg (prefix-numeric-value arg))
  5862. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5863. 'outline-get-last-sibling))
  5864. (ins-point (make-marker))
  5865. (cnt (abs arg))
  5866. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5867. ;; Select the tree
  5868. (org-back-to-heading)
  5869. (setq beg0 (point))
  5870. (save-excursion
  5871. (setq ne-beg (org-back-over-empty-lines))
  5872. (setq beg (point)))
  5873. (save-match-data
  5874. (save-excursion (outline-end-of-heading)
  5875. (setq folded (org-invisible-p)))
  5876. (outline-end-of-subtree))
  5877. (outline-next-heading)
  5878. (setq ne-end (org-back-over-empty-lines))
  5879. (setq end (point))
  5880. (goto-char beg0)
  5881. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5882. ;; include less whitespace
  5883. (save-excursion
  5884. (goto-char beg)
  5885. (forward-line (- ne-beg ne-end))
  5886. (setq beg (point))))
  5887. ;; Find insertion point, with error handling
  5888. (while (> cnt 0)
  5889. (or (and (funcall movfunc) (looking-at outline-regexp))
  5890. (progn (goto-char beg0)
  5891. (error "Cannot move past superior level or buffer limit")))
  5892. (setq cnt (1- cnt)))
  5893. (if (> arg 0)
  5894. ;; Moving forward - still need to move over subtree
  5895. (progn (org-end-of-subtree t t)
  5896. (save-excursion
  5897. (org-back-over-empty-lines)
  5898. (or (bolp) (newline)))))
  5899. (setq ne-ins (org-back-over-empty-lines))
  5900. (move-marker ins-point (point))
  5901. (setq txt (buffer-substring beg end))
  5902. (delete-region beg end)
  5903. (outline-flag-region (1- beg) beg nil)
  5904. (outline-flag-region (1- (point)) (point) nil)
  5905. (insert txt)
  5906. (or (bolp) (insert "\n"))
  5907. (setq ins-end (point))
  5908. (goto-char ins-point)
  5909. (org-skip-whitespace)
  5910. (when (and (< arg 0)
  5911. (org-first-sibling-p)
  5912. (> ne-ins ne-beg))
  5913. ;; Move whitespace back to beginning
  5914. (save-excursion
  5915. (goto-char ins-end)
  5916. (let ((kill-whole-line t))
  5917. (kill-line (- ne-ins ne-beg)) (point)))
  5918. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5919. (move-marker ins-point nil)
  5920. (org-compact-display-after-subtree-move)
  5921. (unless folded
  5922. (org-show-entry)
  5923. (show-children)
  5924. (org-cycle-hide-drawers 'children))))
  5925. (defvar org-subtree-clip ""
  5926. "Clipboard for cut and paste of subtrees.
  5927. This is actually only a copy of the kill, because we use the normal kill
  5928. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5929. (defvar org-subtree-clip-folded nil
  5930. "Was the last copied subtree folded?
  5931. This is used to fold the tree back after pasting.")
  5932. (defun org-cut-subtree (&optional n)
  5933. "Cut the current subtree into the clipboard.
  5934. With prefix arg N, cut this many sequential subtrees.
  5935. This is a short-hand for marking the subtree and then cutting it."
  5936. (interactive "p")
  5937. (org-copy-subtree n 'cut))
  5938. (defun org-copy-subtree (&optional n cut)
  5939. "Cut the current subtree into the clipboard.
  5940. With prefix arg N, cut this many sequential subtrees.
  5941. This is a short-hand for marking the subtree and then copying it.
  5942. If CUT is non-nil, actually cut the subtree."
  5943. (interactive "p")
  5944. (let (beg end folded (beg0 (point)))
  5945. (if (interactive-p)
  5946. (org-back-to-heading nil) ; take what looks like a subtree
  5947. (org-back-to-heading t)) ; take what is really there
  5948. (org-back-over-empty-lines)
  5949. (setq beg (point))
  5950. (skip-chars-forward " \t\r\n")
  5951. (save-match-data
  5952. (save-excursion (outline-end-of-heading)
  5953. (setq folded (org-invisible-p)))
  5954. (condition-case nil
  5955. (outline-forward-same-level (1- n))
  5956. (error nil))
  5957. (org-end-of-subtree t t))
  5958. (org-back-over-empty-lines)
  5959. (setq end (point))
  5960. (goto-char beg0)
  5961. (when (> end beg)
  5962. (setq org-subtree-clip-folded folded)
  5963. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5964. (setq org-subtree-clip (current-kill 0))
  5965. (message "%s: Subtree(s) with %d characters"
  5966. (if cut "Cut" "Copied")
  5967. (length org-subtree-clip)))))
  5968. (defun org-paste-subtree (&optional level tree)
  5969. "Paste the clipboard as a subtree, with modification of headline level.
  5970. The entire subtree is promoted or demoted in order to match a new headline
  5971. level. By default, the new level is derived from the visible headings
  5972. before and after the insertion point, and taken to be the inferior headline
  5973. level of the two. So if the previous visible heading is level 3 and the
  5974. next is level 4 (or vice versa), level 4 will be used for insertion.
  5975. This makes sure that the subtree remains an independent subtree and does
  5976. not swallow low level entries.
  5977. You can also force a different level, either by using a numeric prefix
  5978. argument, or by inserting the heading marker by hand. For example, if the
  5979. cursor is after \"*****\", then the tree will be shifted to level 5.
  5980. If you want to insert the tree as is, just use \\[yank].
  5981. If optional TREE is given, use this text instead of the kill ring."
  5982. (interactive "P")
  5983. (unless (org-kill-is-subtree-p tree)
  5984. (error "%s"
  5985. (substitute-command-keys
  5986. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5987. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5988. (^re (concat "^\\(" outline-regexp "\\)"))
  5989. (re (concat "\\(" outline-regexp "\\)"))
  5990. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5991. (old-level (if (string-match ^re txt)
  5992. (- (match-end 0) (match-beginning 0) 1)
  5993. -1))
  5994. (force-level (cond (level (prefix-numeric-value level))
  5995. ((string-match
  5996. ^re_ (buffer-substring (point-at-bol) (point)))
  5997. (- (match-end 1) (match-beginning 1)))
  5998. (t nil)))
  5999. (previous-level (save-excursion
  6000. (condition-case nil
  6001. (progn
  6002. (outline-previous-visible-heading 1)
  6003. (if (looking-at re)
  6004. (- (match-end 0) (match-beginning 0) 1)
  6005. 1))
  6006. (error 1))))
  6007. (next-level (save-excursion
  6008. (condition-case nil
  6009. (progn
  6010. (or (looking-at outline-regexp)
  6011. (outline-next-visible-heading 1))
  6012. (if (looking-at re)
  6013. (- (match-end 0) (match-beginning 0) 1)
  6014. 1))
  6015. (error 1))))
  6016. (new-level (or force-level (max previous-level next-level)))
  6017. (shift (if (or (= old-level -1)
  6018. (= new-level -1)
  6019. (= old-level new-level))
  6020. 0
  6021. (- new-level old-level)))
  6022. (delta (if (> shift 0) -1 1))
  6023. (func (if (> shift 0) 'org-demote 'org-promote))
  6024. (org-odd-levels-only nil)
  6025. beg end)
  6026. ;; Remove the forced level indicator
  6027. (if force-level
  6028. (delete-region (point-at-bol) (point)))
  6029. ;; Paste
  6030. (beginning-of-line 1)
  6031. (org-back-over-empty-lines) ;; FIXME: correct fix????
  6032. (setq beg (point))
  6033. (insert-before-markers txt) ;; FIXME: correct fix????
  6034. (unless (string-match "\n\\'" txt) (insert "\n"))
  6035. (setq end (point))
  6036. (goto-char beg)
  6037. (skip-chars-forward " \t\n\r")
  6038. (setq beg (point))
  6039. ;; Shift if necessary
  6040. (unless (= shift 0)
  6041. (save-restriction
  6042. (narrow-to-region beg end)
  6043. (while (not (= shift 0))
  6044. (org-map-region func (point-min) (point-max))
  6045. (setq shift (+ delta shift)))
  6046. (goto-char (point-min))))
  6047. (when (interactive-p)
  6048. (message "Clipboard pasted as level %d subtree" new-level))
  6049. (if (and kill-ring
  6050. (eq org-subtree-clip (current-kill 0))
  6051. org-subtree-clip-folded)
  6052. ;; The tree was folded before it was killed/copied
  6053. (hide-subtree))))
  6054. (defun org-kill-is-subtree-p (&optional txt)
  6055. "Check if the current kill is an outline subtree, or a set of trees.
  6056. Returns nil if kill does not start with a headline, or if the first
  6057. headline level is not the largest headline level in the tree.
  6058. So this will actually accept several entries of equal levels as well,
  6059. which is OK for `org-paste-subtree'.
  6060. If optional TXT is given, check this string instead of the current kill."
  6061. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6062. (start-level (and kill
  6063. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6064. org-outline-regexp "\\)")
  6065. kill)
  6066. (- (match-end 2) (match-beginning 2) 1)))
  6067. (re (concat "^" org-outline-regexp))
  6068. (start (1+ (match-beginning 2))))
  6069. (if (not start-level)
  6070. (progn
  6071. nil) ;; does not even start with a heading
  6072. (catch 'exit
  6073. (while (setq start (string-match re kill (1+ start)))
  6074. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6075. (throw 'exit nil)))
  6076. t))))
  6077. (defun org-narrow-to-subtree ()
  6078. "Narrow buffer to the current subtree."
  6079. (interactive)
  6080. (save-excursion
  6081. (save-match-data
  6082. (narrow-to-region
  6083. (progn (org-back-to-heading) (point))
  6084. (progn (org-end-of-subtree t t) (point))))))
  6085. ;;; Outline Sorting
  6086. (defun org-sort (with-case)
  6087. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6088. Optional argument WITH-CASE means sort case-sensitively."
  6089. (interactive "P")
  6090. (if (org-at-table-p)
  6091. (org-call-with-arg 'org-table-sort-lines with-case)
  6092. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6093. (defvar org-priority-regexp) ; defined later in the file
  6094. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  6095. "Sort entries on a certain level of an outline tree.
  6096. If there is an active region, the entries in the region are sorted.
  6097. Else, if the cursor is before the first entry, sort the top-level items.
  6098. Else, the children of the entry at point are sorted.
  6099. Sorting can be alphabetically, numerically, and by date/time as given by
  6100. the first time stamp in the entry. The command prompts for the sorting
  6101. type unless it has been given to the function through the SORTING-TYPE
  6102. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  6103. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6104. called with point at the beginning of the record. It must return either
  6105. a string or a number that should serve as the sorting key for that record.
  6106. Comparing entries ignores case by default. However, with an optional argument
  6107. WITH-CASE, the sorting considers case as well."
  6108. (interactive "P")
  6109. (let ((case-func (if with-case 'identity 'downcase))
  6110. start beg end stars re re2
  6111. txt what tmp plain-list-p)
  6112. ;; Find beginning and end of region to sort
  6113. (cond
  6114. ((org-region-active-p)
  6115. ;; we will sort the region
  6116. (setq end (region-end)
  6117. what "region")
  6118. (goto-char (region-beginning))
  6119. (if (not (org-on-heading-p)) (outline-next-heading))
  6120. (setq start (point)))
  6121. ((org-at-item-p)
  6122. ;; we will sort this plain list
  6123. (org-beginning-of-item-list) (setq start (point))
  6124. (org-end-of-item-list) (setq end (point))
  6125. (goto-char start)
  6126. (setq plain-list-p t
  6127. what "plain list"))
  6128. ((or (org-on-heading-p)
  6129. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6130. ;; we will sort the children of the current headline
  6131. (org-back-to-heading)
  6132. (setq start (point)
  6133. end (progn (org-end-of-subtree t t)
  6134. (org-back-over-empty-lines)
  6135. (point))
  6136. what "children")
  6137. (goto-char start)
  6138. (show-subtree)
  6139. (outline-next-heading))
  6140. (t
  6141. ;; we will sort the top-level entries in this file
  6142. (goto-char (point-min))
  6143. (or (org-on-heading-p) (outline-next-heading))
  6144. (setq start (point) end (point-max) what "top-level")
  6145. (goto-char start)
  6146. (show-all)))
  6147. (setq beg (point))
  6148. (if (>= beg end) (error "Nothing to sort"))
  6149. (unless plain-list-p
  6150. (looking-at "\\(\\*+\\)")
  6151. (setq stars (match-string 1)
  6152. re (concat "^" (regexp-quote stars) " +")
  6153. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6154. txt (buffer-substring beg end))
  6155. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6156. (if (and (not (equal stars "*")) (string-match re2 txt))
  6157. (error "Region to sort contains a level above the first entry")))
  6158. (unless sorting-type
  6159. (message
  6160. (if plain-list-p
  6161. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6162. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6163. what)
  6164. (setq sorting-type (read-char-exclusive))
  6165. (and (= (downcase sorting-type) ?f)
  6166. (setq getkey-func
  6167. (completing-read "Sort using function: "
  6168. obarray 'fboundp t nil nil))
  6169. (setq getkey-func (intern getkey-func)))
  6170. (and (= (downcase sorting-type) ?r)
  6171. (setq property
  6172. (completing-read "Property: "
  6173. (mapcar 'list (org-buffer-property-keys t))
  6174. nil t))))
  6175. (message "Sorting entries...")
  6176. (save-restriction
  6177. (narrow-to-region start end)
  6178. (let ((dcst (downcase sorting-type))
  6179. (now (current-time)))
  6180. (sort-subr
  6181. (/= dcst sorting-type)
  6182. ;; This function moves to the beginning character of the "record" to
  6183. ;; be sorted.
  6184. (if plain-list-p
  6185. (lambda nil
  6186. (if (org-at-item-p) t (goto-char (point-max))))
  6187. (lambda nil
  6188. (if (re-search-forward re nil t)
  6189. (goto-char (match-beginning 0))
  6190. (goto-char (point-max)))))
  6191. ;; This function moves to the last character of the "record" being
  6192. ;; sorted.
  6193. (if plain-list-p
  6194. 'org-end-of-item
  6195. (lambda nil
  6196. (save-match-data
  6197. (condition-case nil
  6198. (outline-forward-same-level 1)
  6199. (error
  6200. (goto-char (point-max)))))))
  6201. ;; This function returns the value that gets sorted against.
  6202. (if plain-list-p
  6203. (lambda nil
  6204. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6205. (cond
  6206. ((= dcst ?n)
  6207. (string-to-number (buffer-substring (match-end 0)
  6208. (point-at-eol))))
  6209. ((= dcst ?a)
  6210. (buffer-substring (match-end 0) (point-at-eol)))
  6211. ((= dcst ?t)
  6212. (if (re-search-forward org-ts-regexp
  6213. (point-at-eol) t)
  6214. (org-time-string-to-time (match-string 0))
  6215. now))
  6216. ((= dcst ?f)
  6217. (if getkey-func
  6218. (progn
  6219. (setq tmp (funcall getkey-func))
  6220. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6221. tmp)
  6222. (error "Invalid key function `%s'" getkey-func)))
  6223. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6224. (lambda nil
  6225. (cond
  6226. ((= dcst ?n)
  6227. (if (looking-at outline-regexp)
  6228. (string-to-number (buffer-substring (match-end 0)
  6229. (point-at-eol)))
  6230. nil))
  6231. ((= dcst ?a)
  6232. (funcall case-func (buffer-substring (point-at-bol)
  6233. (point-at-eol))))
  6234. ((= dcst ?t)
  6235. (if (re-search-forward org-ts-regexp
  6236. (save-excursion
  6237. (forward-line 2)
  6238. (point)) t)
  6239. (org-time-string-to-time (match-string 0))
  6240. now))
  6241. ((= dcst ?p)
  6242. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6243. (string-to-char (match-string 2))
  6244. org-default-priority))
  6245. ((= dcst ?r)
  6246. (or (org-entry-get nil property) ""))
  6247. ((= dcst ?f)
  6248. (if getkey-func
  6249. (progn
  6250. (setq tmp (funcall getkey-func))
  6251. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6252. tmp)
  6253. (error "Invalid key function `%s'" getkey-func)))
  6254. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6255. nil
  6256. (cond
  6257. ((= dcst ?a) 'string<)
  6258. ((= dcst ?t) 'time-less-p)
  6259. (t nil)))))
  6260. (message "Sorting entries...done")))
  6261. (defun org-do-sort (table what &optional with-case sorting-type)
  6262. "Sort TABLE of WHAT according to SORTING-TYPE.
  6263. The user will be prompted for the SORTING-TYPE if the call to this
  6264. function does not specify it. WHAT is only for the prompt, to indicate
  6265. what is being sorted. The sorting key will be extracted from
  6266. the car of the elements of the table.
  6267. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6268. (unless sorting-type
  6269. (message
  6270. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6271. what)
  6272. (setq sorting-type (read-char-exclusive)))
  6273. (let ((dcst (downcase sorting-type))
  6274. extractfun comparefun)
  6275. ;; Define the appropriate functions
  6276. (cond
  6277. ((= dcst ?n)
  6278. (setq extractfun 'string-to-number
  6279. comparefun (if (= dcst sorting-type) '< '>)))
  6280. ((= dcst ?a)
  6281. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6282. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6283. comparefun (if (= dcst sorting-type)
  6284. 'string<
  6285. (lambda (a b) (and (not (string< a b))
  6286. (not (string= a b)))))))
  6287. ((= dcst ?t)
  6288. (setq extractfun
  6289. (lambda (x)
  6290. (if (string-match org-ts-regexp x)
  6291. (time-to-seconds
  6292. (org-time-string-to-time (match-string 0 x)))
  6293. 0))
  6294. comparefun (if (= dcst sorting-type) '< '>)))
  6295. (t (error "Invalid sorting type `%c'" sorting-type)))
  6296. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6297. table)
  6298. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6299. ;;;; Plain list items, including checkboxes
  6300. ;;; Plain list items
  6301. (defun org-at-item-p ()
  6302. "Is point in a line starting a hand-formatted item?"
  6303. (let ((llt org-plain-list-ordered-item-terminator))
  6304. (save-excursion
  6305. (goto-char (point-at-bol))
  6306. (looking-at
  6307. (cond
  6308. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6309. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6310. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6311. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6312. (defun org-in-item-p ()
  6313. "It the cursor inside a plain list item.
  6314. Does not have to be the first line."
  6315. (save-excursion
  6316. (condition-case nil
  6317. (progn
  6318. (org-beginning-of-item)
  6319. (org-at-item-p)
  6320. t)
  6321. (error nil))))
  6322. (defun org-insert-item (&optional checkbox)
  6323. "Insert a new item at the current level.
  6324. Return t when things worked, nil when we are not in an item."
  6325. (when (save-excursion
  6326. (condition-case nil
  6327. (progn
  6328. (org-beginning-of-item)
  6329. (org-at-item-p)
  6330. (if (org-invisible-p) (error "Invisible item"))
  6331. t)
  6332. (error nil)))
  6333. (let* ((bul (match-string 0))
  6334. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6335. (match-end 0)))
  6336. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6337. pos)
  6338. (cond
  6339. ((and (org-at-item-p) (<= (point) eow))
  6340. ;; before the bullet
  6341. (beginning-of-line 1)
  6342. (open-line (if blank 2 1)))
  6343. ((<= (point) eow)
  6344. (beginning-of-line 1))
  6345. (t
  6346. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  6347. (end-of-line 1)
  6348. (delete-horizontal-space))
  6349. (newline (if blank 2 1))))
  6350. (insert bul (if checkbox "[ ]" ""))
  6351. (just-one-space)
  6352. (setq pos (point))
  6353. (end-of-line 1)
  6354. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6355. (org-maybe-renumber-ordered-list)
  6356. (and checkbox (org-update-checkbox-count-maybe))
  6357. t))
  6358. ;;; Checkboxes
  6359. (defun org-at-item-checkbox-p ()
  6360. "Is point at a line starting a plain-list item with a checklet?"
  6361. (and (org-at-item-p)
  6362. (save-excursion
  6363. (goto-char (match-end 0))
  6364. (skip-chars-forward " \t")
  6365. (looking-at "\\[[- X]\\]"))))
  6366. (defun org-toggle-checkbox (&optional arg)
  6367. "Toggle the checkbox in the current line."
  6368. (interactive "P")
  6369. (catch 'exit
  6370. (let (beg end status (firstnew 'unknown))
  6371. (cond
  6372. ((org-region-active-p)
  6373. (setq beg (region-beginning) end (region-end)))
  6374. ((org-on-heading-p)
  6375. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6376. ((org-at-item-checkbox-p)
  6377. (let ((pos (point)))
  6378. (replace-match
  6379. (cond (arg "[-]")
  6380. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6381. (t "[ ]"))
  6382. t t)
  6383. (goto-char pos))
  6384. (throw 'exit t))
  6385. (t (error "Not at a checkbox or heading, and no active region")))
  6386. (save-excursion
  6387. (goto-char beg)
  6388. (while (< (point) end)
  6389. (when (org-at-item-checkbox-p)
  6390. (setq status (equal (match-string 0) "[X]"))
  6391. (when (eq firstnew 'unknown)
  6392. (setq firstnew (not status)))
  6393. (replace-match
  6394. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6395. (beginning-of-line 2)))))
  6396. (org-update-checkbox-count-maybe))
  6397. (defun org-update-checkbox-count-maybe ()
  6398. "Update checkbox statistics unless turned off by user."
  6399. (when org-provide-checkbox-statistics
  6400. (org-update-checkbox-count)))
  6401. (defun org-update-checkbox-count (&optional all)
  6402. "Update the checkbox statistics in the current section.
  6403. This will find all statistic cookies like [57%] and [6/12] and update them
  6404. with the current numbers. With optional prefix argument ALL, do this for
  6405. the whole buffer."
  6406. (interactive "P")
  6407. (save-excursion
  6408. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6409. (beg (condition-case nil
  6410. (progn (outline-back-to-heading) (point))
  6411. (error (point-min))))
  6412. (end (move-marker (make-marker)
  6413. (progn (outline-next-heading) (point))))
  6414. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  6415. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6416. (re-find (concat re "\\|" re-box))
  6417. beg-cookie end-cookie is-percent c-on c-off lim
  6418. eline curr-ind next-ind continue-from startsearch
  6419. (cstat 0)
  6420. )
  6421. (when all
  6422. (goto-char (point-min))
  6423. (outline-next-heading)
  6424. (setq beg (point) end (point-max)))
  6425. (goto-char end)
  6426. ;; find each statistic cookie
  6427. (while (re-search-backward re-find beg t)
  6428. (setq beg-cookie (match-beginning 1)
  6429. end-cookie (match-end 1)
  6430. cstat (+ cstat (if end-cookie 1 0))
  6431. startsearch (point-at-eol)
  6432. continue-from (point-at-bol)
  6433. is-percent (match-beginning 2)
  6434. lim (cond
  6435. ((org-on-heading-p) (outline-next-heading) (point))
  6436. ((org-at-item-p) (org-end-of-item) (point))
  6437. (t nil))
  6438. c-on 0
  6439. c-off 0)
  6440. (when lim
  6441. ;; find first checkbox for this cookie and gather
  6442. ;; statistics from all that are at this indentation level
  6443. (goto-char startsearch)
  6444. (if (re-search-forward re-box lim t)
  6445. (progn
  6446. (org-beginning-of-item)
  6447. (setq curr-ind (org-get-indentation))
  6448. (setq next-ind curr-ind)
  6449. (while (= curr-ind next-ind)
  6450. (save-excursion (end-of-line) (setq eline (point)))
  6451. (if (re-search-forward re-box eline t)
  6452. (if (member (match-string 2) '("[ ]" "[-]"))
  6453. (setq c-off (1+ c-off))
  6454. (setq c-on (1+ c-on))
  6455. )
  6456. )
  6457. (org-end-of-item)
  6458. (setq next-ind (org-get-indentation))
  6459. )))
  6460. (goto-char continue-from)
  6461. ;; update cookie
  6462. (when end-cookie
  6463. (delete-region beg-cookie end-cookie)
  6464. (goto-char beg-cookie)
  6465. (insert
  6466. (if is-percent
  6467. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6468. (format "[%d/%d]" c-on (+ c-on c-off)))))
  6469. ;; update items checkbox if it has one
  6470. (when (org-at-item-p)
  6471. (org-beginning-of-item)
  6472. (when (and (> (+ c-on c-off) 0)
  6473. (re-search-forward re-box (point-at-eol) t))
  6474. (setq beg-cookie (match-beginning 2)
  6475. end-cookie (match-end 2))
  6476. (delete-region beg-cookie end-cookie)
  6477. (goto-char beg-cookie)
  6478. (cond ((= c-off 0) (insert "[X]"))
  6479. ((= c-on 0) (insert "[ ]"))
  6480. (t (insert "[-]")))
  6481. )))
  6482. (goto-char continue-from))
  6483. (when (interactive-p)
  6484. (message "Checkbox satistics updated %s (%d places)"
  6485. (if all "in entire file" "in current outline entry") cstat)))))
  6486. (defun org-get-checkbox-statistics-face ()
  6487. "Select the face for checkbox statistics.
  6488. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6489. it will be `org-todo'."
  6490. (if (match-end 1)
  6491. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6492. (if (and (> (match-end 2) (match-beginning 2))
  6493. (equal (match-string 2) (match-string 3)))
  6494. 'org-done
  6495. 'org-todo)))
  6496. (defun org-get-indentation (&optional line)
  6497. "Get the indentation of the current line, interpreting tabs.
  6498. When LINE is given, assume it represents a line and compute its indentation."
  6499. (if line
  6500. (if (string-match "^ *" (org-remove-tabs line))
  6501. (match-end 0))
  6502. (save-excursion
  6503. (beginning-of-line 1)
  6504. (skip-chars-forward " \t")
  6505. (current-column))))
  6506. (defun org-remove-tabs (s &optional width)
  6507. "Replace tabulators in S with spaces.
  6508. Assumes that s is a single line, starting in column 0."
  6509. (setq width (or width tab-width))
  6510. (while (string-match "\t" s)
  6511. (setq s (replace-match
  6512. (make-string
  6513. (- (* width (/ (+ (match-beginning 0) width) width))
  6514. (match-beginning 0)) ?\ )
  6515. t t s)))
  6516. s)
  6517. (defun org-fix-indentation (line ind)
  6518. "Fix indentation in LINE.
  6519. IND is a cons cell with target and minimum indentation.
  6520. If the current indenation in LINE is smaller than the minimum,
  6521. leave it alone. If it is larger than ind, set it to the target."
  6522. (let* ((l (org-remove-tabs line))
  6523. (i (org-get-indentation l))
  6524. (i1 (car ind)) (i2 (cdr ind)))
  6525. (if (>= i i2) (setq l (substring line i2)))
  6526. (if (> i1 0)
  6527. (concat (make-string i1 ?\ ) l)
  6528. l)))
  6529. (defcustom org-empty-line-terminates-plain-lists nil
  6530. "Non-nil means, an empty line ends all plain list levels.
  6531. When nil, empty lines are part of the preceeding item."
  6532. :group 'org-plain-lists
  6533. :type 'boolean)
  6534. (defun org-beginning-of-item ()
  6535. "Go to the beginning of the current hand-formatted item.
  6536. If the cursor is not in an item, throw an error."
  6537. (interactive)
  6538. (let ((pos (point))
  6539. (limit (save-excursion
  6540. (condition-case nil
  6541. (progn
  6542. (org-back-to-heading)
  6543. (beginning-of-line 2) (point))
  6544. (error (point-min)))))
  6545. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6546. ind ind1)
  6547. (if (org-at-item-p)
  6548. (beginning-of-line 1)
  6549. (beginning-of-line 1)
  6550. (skip-chars-forward " \t")
  6551. (setq ind (current-column))
  6552. (if (catch 'exit
  6553. (while t
  6554. (beginning-of-line 0)
  6555. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6556. (if (looking-at "[ \t]*$")
  6557. (setq ind1 ind-empty)
  6558. (skip-chars-forward " \t")
  6559. (setq ind1 (current-column)))
  6560. (if (< ind1 ind)
  6561. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6562. nil
  6563. (goto-char pos)
  6564. (error "Not in an item")))))
  6565. (defun org-end-of-item ()
  6566. "Go to the end of the current hand-formatted item.
  6567. If the cursor is not in an item, throw an error."
  6568. (interactive)
  6569. (let* ((pos (point))
  6570. ind1
  6571. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6572. (limit (save-excursion (outline-next-heading) (point)))
  6573. (ind (save-excursion
  6574. (org-beginning-of-item)
  6575. (skip-chars-forward " \t")
  6576. (current-column)))
  6577. (end (catch 'exit
  6578. (while t
  6579. (beginning-of-line 2)
  6580. (if (eobp) (throw 'exit (point)))
  6581. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6582. (if (looking-at "[ \t]*$")
  6583. (setq ind1 ind-empty)
  6584. (skip-chars-forward " \t")
  6585. (setq ind1 (current-column)))
  6586. (if (<= ind1 ind)
  6587. (throw 'exit (point-at-bol)))))))
  6588. (if end
  6589. (goto-char end)
  6590. (goto-char pos)
  6591. (error "Not in an item"))))
  6592. (defun org-next-item ()
  6593. "Move to the beginning of the next item in the current plain list.
  6594. Error if not at a plain list, or if this is the last item in the list."
  6595. (interactive)
  6596. (let (ind ind1 (pos (point)))
  6597. (org-beginning-of-item)
  6598. (setq ind (org-get-indentation))
  6599. (org-end-of-item)
  6600. (setq ind1 (org-get-indentation))
  6601. (unless (and (org-at-item-p) (= ind ind1))
  6602. (goto-char pos)
  6603. (error "On last item"))))
  6604. (defun org-previous-item ()
  6605. "Move to the beginning of the previous item in the current plain list.
  6606. Error if not at a plain list, or if this is the first item in the list."
  6607. (interactive)
  6608. (let (beg ind ind1 (pos (point)))
  6609. (org-beginning-of-item)
  6610. (setq beg (point))
  6611. (setq ind (org-get-indentation))
  6612. (goto-char beg)
  6613. (catch 'exit
  6614. (while t
  6615. (beginning-of-line 0)
  6616. (if (looking-at "[ \t]*$")
  6617. nil
  6618. (if (<= (setq ind1 (org-get-indentation)) ind)
  6619. (throw 'exit t)))))
  6620. (condition-case nil
  6621. (if (or (not (org-at-item-p))
  6622. (< ind1 (1- ind)))
  6623. (error "")
  6624. (org-beginning-of-item))
  6625. (error (goto-char pos)
  6626. (error "On first item")))))
  6627. (defun org-first-list-item-p ()
  6628. "Is this heading the item in a plain list?"
  6629. (unless (org-at-item-p)
  6630. (error "Not at a plain list item"))
  6631. (org-beginning-of-item)
  6632. (= (point) (save-excursion (org-beginning-of-item-list))))
  6633. (defun org-move-item-down ()
  6634. "Move the plain list item at point down, i.e. swap with following item.
  6635. Subitems (items with larger indentation) are considered part of the item,
  6636. so this really moves item trees."
  6637. (interactive)
  6638. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6639. (org-beginning-of-item)
  6640. (setq beg0 (point))
  6641. (save-excursion
  6642. (setq ne-beg (org-back-over-empty-lines))
  6643. (setq beg (point)))
  6644. (goto-char beg0)
  6645. (setq ind (org-get-indentation))
  6646. (org-end-of-item)
  6647. (setq end0 (point))
  6648. (setq ind1 (org-get-indentation))
  6649. (setq ne-end (org-back-over-empty-lines))
  6650. (setq end (point))
  6651. (goto-char beg0)
  6652. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6653. ;; include less whitespace
  6654. (save-excursion
  6655. (goto-char beg)
  6656. (forward-line (- ne-beg ne-end))
  6657. (setq beg (point))))
  6658. (goto-char end0)
  6659. (if (and (org-at-item-p) (= ind ind1))
  6660. (progn
  6661. (org-end-of-item)
  6662. (org-back-over-empty-lines)
  6663. (setq txt (buffer-substring beg end))
  6664. (save-excursion
  6665. (delete-region beg end))
  6666. (setq pos (point))
  6667. (insert txt)
  6668. (goto-char pos) (org-skip-whitespace)
  6669. (org-maybe-renumber-ordered-list))
  6670. (goto-char pos)
  6671. (error "Cannot move this item further down"))))
  6672. (defun org-move-item-up (arg)
  6673. "Move the plain list item at point up, i.e. swap with previous item.
  6674. Subitems (items with larger indentation) are considered part of the item,
  6675. so this really moves item trees."
  6676. (interactive "p")
  6677. (let (beg beg0 end ind ind1 (pos (point)) txt
  6678. ne-beg ne-ins ins-end)
  6679. (org-beginning-of-item)
  6680. (setq beg0 (point))
  6681. (setq ind (org-get-indentation))
  6682. (save-excursion
  6683. (setq ne-beg (org-back-over-empty-lines))
  6684. (setq beg (point)))
  6685. (goto-char beg0)
  6686. (org-end-of-item)
  6687. (setq end (point))
  6688. (goto-char beg0)
  6689. (catch 'exit
  6690. (while t
  6691. (beginning-of-line 0)
  6692. (if (looking-at "[ \t]*$")
  6693. (if org-empty-line-terminates-plain-lists
  6694. (progn
  6695. (goto-char pos)
  6696. (error "Cannot move this item further up"))
  6697. nil)
  6698. (if (<= (setq ind1 (org-get-indentation)) ind)
  6699. (throw 'exit t)))))
  6700. (condition-case nil
  6701. (org-beginning-of-item)
  6702. (error (goto-char beg)
  6703. (error "Cannot move this item further up")))
  6704. (setq ind1 (org-get-indentation))
  6705. (if (and (org-at-item-p) (= ind ind1))
  6706. (progn
  6707. (setq ne-ins (org-back-over-empty-lines))
  6708. (setq txt (buffer-substring beg end))
  6709. (save-excursion
  6710. (delete-region beg end))
  6711. (setq pos (point))
  6712. (insert txt)
  6713. (setq ins-end (point))
  6714. (goto-char pos) (org-skip-whitespace)
  6715. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6716. ;; Move whitespace back to beginning
  6717. (save-excursion
  6718. (goto-char ins-end)
  6719. (let ((kill-whole-line t))
  6720. (kill-line (- ne-ins ne-beg)) (point)))
  6721. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6722. (org-maybe-renumber-ordered-list))
  6723. (goto-char pos)
  6724. (error "Cannot move this item further up"))))
  6725. (defun org-maybe-renumber-ordered-list ()
  6726. "Renumber the ordered list at point if setup allows it.
  6727. This tests the user option `org-auto-renumber-ordered-lists' before
  6728. doing the renumbering."
  6729. (interactive)
  6730. (when (and org-auto-renumber-ordered-lists
  6731. (org-at-item-p))
  6732. (if (match-beginning 3)
  6733. (org-renumber-ordered-list 1)
  6734. (org-fix-bullet-type))))
  6735. (defun org-maybe-renumber-ordered-list-safe ()
  6736. (condition-case nil
  6737. (save-excursion
  6738. (org-maybe-renumber-ordered-list))
  6739. (error nil)))
  6740. (defun org-cycle-list-bullet (&optional which)
  6741. "Cycle through the different itemize/enumerate bullets.
  6742. This cycle the entire list level through the sequence:
  6743. `-' -> `+' -> `*' -> `1.' -> `1)'
  6744. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6745. 0 meand `-', 1 means `+' etc."
  6746. (interactive "P")
  6747. (org-preserve-lc
  6748. (org-beginning-of-item-list)
  6749. (org-at-item-p)
  6750. (beginning-of-line 1)
  6751. (let ((current (match-string 0))
  6752. (prevp (eq which 'previous))
  6753. new)
  6754. (setq new (cond
  6755. ((and (numberp which)
  6756. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6757. ((string-match "-" current) (if prevp "1)" "+"))
  6758. ((string-match "\\+" current)
  6759. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6760. ((string-match "\\*" current) (if prevp "+" "1."))
  6761. ((string-match "\\." current) (if prevp "*" "1)"))
  6762. ((string-match ")" current) (if prevp "1." "-"))
  6763. (t (error "This should not happen"))))
  6764. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6765. (org-fix-bullet-type)
  6766. (org-maybe-renumber-ordered-list))))
  6767. (defun org-get-string-indentation (s)
  6768. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6769. (let ((n -1) (i 0) (w tab-width) c)
  6770. (catch 'exit
  6771. (while (< (setq n (1+ n)) (length s))
  6772. (setq c (aref s n))
  6773. (cond ((= c ?\ ) (setq i (1+ i)))
  6774. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6775. (t (throw 'exit t)))))
  6776. i))
  6777. (defun org-renumber-ordered-list (arg)
  6778. "Renumber an ordered plain list.
  6779. Cursor needs to be in the first line of an item, the line that starts
  6780. with something like \"1.\" or \"2)\"."
  6781. (interactive "p")
  6782. (unless (and (org-at-item-p)
  6783. (match-beginning 3))
  6784. (error "This is not an ordered list"))
  6785. (let ((line (org-current-line))
  6786. (col (current-column))
  6787. (ind (org-get-string-indentation
  6788. (buffer-substring (point-at-bol) (match-beginning 3))))
  6789. ;; (term (substring (match-string 3) -1))
  6790. ind1 (n (1- arg))
  6791. fmt)
  6792. ;; find where this list begins
  6793. (org-beginning-of-item-list)
  6794. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6795. (setq fmt (concat "%d" (match-string 1)))
  6796. (beginning-of-line 0)
  6797. ;; walk forward and replace these numbers
  6798. (catch 'exit
  6799. (while t
  6800. (catch 'next
  6801. (beginning-of-line 2)
  6802. (if (eobp) (throw 'exit nil))
  6803. (if (looking-at "[ \t]*$") (throw 'next nil))
  6804. (skip-chars-forward " \t") (setq ind1 (current-column))
  6805. (if (> ind1 ind) (throw 'next t))
  6806. (if (< ind1 ind) (throw 'exit t))
  6807. (if (not (org-at-item-p)) (throw 'exit nil))
  6808. (delete-region (match-beginning 2) (match-end 2))
  6809. (goto-char (match-beginning 2))
  6810. (insert (format fmt (setq n (1+ n)))))))
  6811. (goto-line line)
  6812. (move-to-column col)))
  6813. (defun org-fix-bullet-type ()
  6814. "Make sure all items in this list have the same bullet as the firsst item."
  6815. (interactive)
  6816. (unless (org-at-item-p) (error "This is not a list"))
  6817. (let ((line (org-current-line))
  6818. (col (current-column))
  6819. (ind (current-indentation))
  6820. ind1 bullet)
  6821. ;; find where this list begins
  6822. (org-beginning-of-item-list)
  6823. (beginning-of-line 1)
  6824. ;; find out what the bullet type is
  6825. (looking-at "[ \t]*\\(\\S-+\\)")
  6826. (setq bullet (match-string 1))
  6827. ;; walk forward and replace these numbers
  6828. (beginning-of-line 0)
  6829. (catch 'exit
  6830. (while t
  6831. (catch 'next
  6832. (beginning-of-line 2)
  6833. (if (eobp) (throw 'exit nil))
  6834. (if (looking-at "[ \t]*$") (throw 'next nil))
  6835. (skip-chars-forward " \t") (setq ind1 (current-column))
  6836. (if (> ind1 ind) (throw 'next t))
  6837. (if (< ind1 ind) (throw 'exit t))
  6838. (if (not (org-at-item-p)) (throw 'exit nil))
  6839. (skip-chars-forward " \t")
  6840. (looking-at "\\S-+")
  6841. (replace-match bullet))))
  6842. (goto-line line)
  6843. (move-to-column col)
  6844. (if (string-match "[0-9]" bullet)
  6845. (org-renumber-ordered-list 1))))
  6846. (defun org-beginning-of-item-list ()
  6847. "Go to the beginning of the current item list.
  6848. I.e. to the first item in this list."
  6849. (interactive)
  6850. (org-beginning-of-item)
  6851. (let ((pos (point-at-bol))
  6852. (ind (org-get-indentation))
  6853. ind1)
  6854. ;; find where this list begins
  6855. (catch 'exit
  6856. (while t
  6857. (catch 'next
  6858. (beginning-of-line 0)
  6859. (if (looking-at "[ \t]*$")
  6860. (throw (if (bobp) 'exit 'next) t))
  6861. (skip-chars-forward " \t") (setq ind1 (current-column))
  6862. (if (or (< ind1 ind)
  6863. (and (= ind1 ind)
  6864. (not (org-at-item-p)))
  6865. (bobp))
  6866. (throw 'exit t)
  6867. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6868. (goto-char pos)))
  6869. (defun org-end-of-item-list ()
  6870. "Go to the end of the current item list.
  6871. I.e. to the text after the last item."
  6872. (interactive)
  6873. (org-beginning-of-item)
  6874. (let ((pos (point-at-bol))
  6875. (ind (org-get-indentation))
  6876. ind1)
  6877. ;; find where this list begins
  6878. (catch 'exit
  6879. (while t
  6880. (catch 'next
  6881. (beginning-of-line 2)
  6882. (if (looking-at "[ \t]*$")
  6883. (throw (if (eobp) 'exit 'next) t))
  6884. (skip-chars-forward " \t") (setq ind1 (current-column))
  6885. (if (or (< ind1 ind)
  6886. (and (= ind1 ind)
  6887. (not (org-at-item-p)))
  6888. (eobp))
  6889. (progn
  6890. (setq pos (point-at-bol))
  6891. (throw 'exit t))))))
  6892. (goto-char pos)))
  6893. (defvar org-last-indent-begin-marker (make-marker))
  6894. (defvar org-last-indent-end-marker (make-marker))
  6895. (defun org-outdent-item (arg)
  6896. "Outdent a local list item."
  6897. (interactive "p")
  6898. (org-indent-item (- arg)))
  6899. (defun org-indent-item (arg)
  6900. "Indent a local list item."
  6901. (interactive "p")
  6902. (unless (org-at-item-p)
  6903. (error "Not on an item"))
  6904. (save-excursion
  6905. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6906. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6907. (setq beg org-last-indent-begin-marker
  6908. end org-last-indent-end-marker)
  6909. (org-beginning-of-item)
  6910. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6911. (org-end-of-item)
  6912. (setq end (move-marker org-last-indent-end-marker (point))))
  6913. (goto-char beg)
  6914. (setq tmp (org-item-indent-positions)
  6915. ind (car tmp)
  6916. ind-down (nth 2 tmp)
  6917. ind-up (nth 1 tmp)
  6918. delta (if (> arg 0)
  6919. (if ind-down (- ind-down ind) 2)
  6920. (if ind-up (- ind-up ind) -2)))
  6921. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6922. (while (< (point) end)
  6923. (beginning-of-line 1)
  6924. (skip-chars-forward " \t") (setq ind1 (current-column))
  6925. (delete-region (point-at-bol) (point))
  6926. (or (eolp) (indent-to-column (+ ind1 delta)))
  6927. (beginning-of-line 2))))
  6928. (org-fix-bullet-type)
  6929. (org-maybe-renumber-ordered-list-safe)
  6930. (save-excursion
  6931. (beginning-of-line 0)
  6932. (condition-case nil (org-beginning-of-item) (error nil))
  6933. (org-maybe-renumber-ordered-list-safe)))
  6934. (defun org-item-indent-positions ()
  6935. "Return indentation for plain list items.
  6936. This returns a list with three values: The current indentation, the
  6937. parent indentation and the indentation a child should habe.
  6938. Assumes cursor in item line."
  6939. (let* ((bolpos (point-at-bol))
  6940. (ind (org-get-indentation))
  6941. ind-down ind-up pos)
  6942. (save-excursion
  6943. (org-beginning-of-item-list)
  6944. (skip-chars-backward "\n\r \t")
  6945. (when (org-in-item-p)
  6946. (org-beginning-of-item)
  6947. (setq ind-up (org-get-indentation))))
  6948. (setq pos (point))
  6949. (save-excursion
  6950. (cond
  6951. ((and (condition-case nil (progn (org-previous-item) t)
  6952. (error nil))
  6953. (or (forward-char 1) t)
  6954. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6955. (setq ind-down (org-get-indentation)))
  6956. ((and (goto-char pos)
  6957. (org-at-item-p))
  6958. (goto-char (match-end 0))
  6959. (skip-chars-forward " \t")
  6960. (setq ind-down (current-column)))))
  6961. (list ind ind-up ind-down)))
  6962. ;;; The orgstruct minor mode
  6963. ;; Define a minor mode which can be used in other modes in order to
  6964. ;; integrate the org-mode structure editing commands.
  6965. ;; This is really a hack, because the org-mode structure commands use
  6966. ;; keys which normally belong to the major mode. Here is how it
  6967. ;; works: The minor mode defines all the keys necessary to operate the
  6968. ;; structure commands, but wraps the commands into a function which
  6969. ;; tests if the cursor is currently at a headline or a plain list
  6970. ;; item. If that is the case, the structure command is used,
  6971. ;; temporarily setting many Org-mode variables like regular
  6972. ;; expressions for filling etc. However, when any of those keys is
  6973. ;; used at a different location, function uses `key-binding' to look
  6974. ;; up if the key has an associated command in another currently active
  6975. ;; keymap (minor modes, major mode, global), and executes that
  6976. ;; command. There might be problems if any of the keys is otherwise
  6977. ;; used as a prefix key.
  6978. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6979. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6980. ;; addresses this by checking explicitly for both bindings.
  6981. (defvar orgstruct-mode-map (make-sparse-keymap)
  6982. "Keymap for the minor `orgstruct-mode'.")
  6983. (defvar org-local-vars nil
  6984. "List of local variables, for use by `orgstruct-mode'")
  6985. ;;;###autoload
  6986. (define-minor-mode orgstruct-mode
  6987. "Toggle the minor more `orgstruct-mode'.
  6988. This mode is for using Org-mode structure commands in other modes.
  6989. The following key behave as if Org-mode was active, if the cursor
  6990. is on a headline, or on a plain list item (both in the definition
  6991. of Org-mode).
  6992. M-up Move entry/item up
  6993. M-down Move entry/item down
  6994. M-left Promote
  6995. M-right Demote
  6996. M-S-up Move entry/item up
  6997. M-S-down Move entry/item down
  6998. M-S-left Promote subtree
  6999. M-S-right Demote subtree
  7000. M-q Fill paragraph and items like in Org-mode
  7001. C-c ^ Sort entries
  7002. C-c - Cycle list bullet
  7003. TAB Cycle item visibility
  7004. M-RET Insert new heading/item
  7005. S-M-RET Insert new TODO heading / Chekbox item
  7006. C-c C-c Set tags / toggle checkbox"
  7007. nil " OrgStruct" nil
  7008. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7009. ;;;###autoload
  7010. (defun turn-on-orgstruct ()
  7011. "Unconditionally turn on `orgstruct-mode'."
  7012. (orgstruct-mode 1))
  7013. ;;;###autoload
  7014. (defun turn-on-orgstruct++ ()
  7015. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  7016. In addition to setting orgstruct-mode, this also exports all indentation and
  7017. autofilling variables from org-mode into the buffer. Note that turning
  7018. off orgstruct-mode will *not* remove these additional settings."
  7019. (orgstruct-mode 1)
  7020. (let (var val)
  7021. (mapc
  7022. (lambda (x)
  7023. (when (string-match
  7024. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7025. (symbol-name (car x)))
  7026. (setq var (car x) val (nth 1 x))
  7027. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7028. org-local-vars)))
  7029. (defun orgstruct-error ()
  7030. "Error when there is no default binding for a structure key."
  7031. (interactive)
  7032. (error "This key has no function outside structure elements"))
  7033. (defun orgstruct-setup ()
  7034. "Setup orgstruct keymaps."
  7035. (let ((nfunc 0)
  7036. (bindings
  7037. (list
  7038. '([(meta up)] org-metaup)
  7039. '([(meta down)] org-metadown)
  7040. '([(meta left)] org-metaleft)
  7041. '([(meta right)] org-metaright)
  7042. '([(meta shift up)] org-shiftmetaup)
  7043. '([(meta shift down)] org-shiftmetadown)
  7044. '([(meta shift left)] org-shiftmetaleft)
  7045. '([(meta shift right)] org-shiftmetaright)
  7046. '([(shift up)] org-shiftup)
  7047. '([(shift down)] org-shiftdown)
  7048. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7049. '("\M-q" fill-paragraph)
  7050. '("\C-c^" org-sort)
  7051. '("\C-c-" org-cycle-list-bullet)))
  7052. elt key fun cmd)
  7053. (while (setq elt (pop bindings))
  7054. (setq nfunc (1+ nfunc))
  7055. (setq key (org-key (car elt))
  7056. fun (nth 1 elt)
  7057. cmd (orgstruct-make-binding fun nfunc key))
  7058. (org-defkey orgstruct-mode-map key cmd))
  7059. ;; Special treatment needed for TAB and RET
  7060. (org-defkey orgstruct-mode-map [(tab)]
  7061. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7062. (org-defkey orgstruct-mode-map "\C-i"
  7063. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7064. (org-defkey orgstruct-mode-map "\M-\C-m"
  7065. (orgstruct-make-binding 'org-insert-heading 105
  7066. "\M-\C-m" [(meta return)]))
  7067. (org-defkey orgstruct-mode-map [(meta return)]
  7068. (orgstruct-make-binding 'org-insert-heading 106
  7069. [(meta return)] "\M-\C-m"))
  7070. (org-defkey orgstruct-mode-map [(shift meta return)]
  7071. (orgstruct-make-binding 'org-insert-todo-heading 107
  7072. [(meta return)] "\M-\C-m"))
  7073. (unless org-local-vars
  7074. (setq org-local-vars (org-get-local-variables)))
  7075. t))
  7076. (defun orgstruct-make-binding (fun n &rest keys)
  7077. "Create a function for binding in the structure minor mode.
  7078. FUN is the command to call inside a table. N is used to create a unique
  7079. command name. KEYS are keys that should be checked in for a command
  7080. to execute outside of tables."
  7081. (eval
  7082. (list 'defun
  7083. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7084. '(arg)
  7085. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7086. "Outside of structure, run the binding of `"
  7087. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7088. "'.")
  7089. '(interactive "p")
  7090. (list 'if
  7091. '(org-context-p 'headline 'item)
  7092. (list 'org-run-like-in-org-mode (list 'quote fun))
  7093. (list 'let '(orgstruct-mode)
  7094. (list 'call-interactively
  7095. (append '(or)
  7096. (mapcar (lambda (k)
  7097. (list 'key-binding k))
  7098. keys)
  7099. '('orgstruct-error))))))))
  7100. (defun org-context-p (&rest contexts)
  7101. "Check if local context is and of CONTEXTS.
  7102. Possible values in the list of contexts are `table', `headline', and `item'."
  7103. (let ((pos (point)))
  7104. (goto-char (point-at-bol))
  7105. (prog1 (or (and (memq 'table contexts)
  7106. (looking-at "[ \t]*|"))
  7107. (and (memq 'headline contexts)
  7108. (looking-at "\\*+"))
  7109. (and (memq 'item contexts)
  7110. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  7111. (goto-char pos))))
  7112. (defun org-get-local-variables ()
  7113. "Return a list of all local variables in an org-mode buffer."
  7114. (let (varlist)
  7115. (with-current-buffer (get-buffer-create "*Org tmp*")
  7116. (erase-buffer)
  7117. (org-mode)
  7118. (setq varlist (buffer-local-variables)))
  7119. (kill-buffer "*Org tmp*")
  7120. (delq nil
  7121. (mapcar
  7122. (lambda (x)
  7123. (setq x
  7124. (if (symbolp x)
  7125. (list x)
  7126. (list (car x) (list 'quote (cdr x)))))
  7127. (if (string-match
  7128. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7129. (symbol-name (car x)))
  7130. x nil))
  7131. varlist))))
  7132. ;;;###autoload
  7133. (defun org-run-like-in-org-mode (cmd)
  7134. (unless org-local-vars
  7135. (setq org-local-vars (org-get-local-variables)))
  7136. (eval (list 'let org-local-vars
  7137. (list 'call-interactively (list 'quote cmd)))))
  7138. ;;;; Archiving
  7139. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  7140. (defun org-archive-subtree (&optional find-done)
  7141. "Move the current subtree to the archive.
  7142. The archive can be a certain top-level heading in the current file, or in
  7143. a different file. The tree will be moved to that location, the subtree
  7144. heading be marked DONE, and the current time will be added.
  7145. When called with prefix argument FIND-DONE, find whole trees without any
  7146. open TODO items and archive them (after getting confirmation from the user).
  7147. If the cursor is not at a headline when this comand is called, try all level
  7148. 1 trees. If the cursor is on a headline, only try the direct children of
  7149. this heading."
  7150. (interactive "P")
  7151. (if find-done
  7152. (org-archive-all-done)
  7153. ;; Save all relevant TODO keyword-relatex variables
  7154. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  7155. (tr-org-todo-keywords-1 org-todo-keywords-1)
  7156. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  7157. (tr-org-done-keywords org-done-keywords)
  7158. (tr-org-todo-regexp org-todo-regexp)
  7159. (tr-org-todo-line-regexp org-todo-line-regexp)
  7160. (tr-org-odd-levels-only org-odd-levels-only)
  7161. (this-buffer (current-buffer))
  7162. (org-archive-location org-archive-location)
  7163. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  7164. ;; start of variables that will be used for saving context
  7165. ;; The compiler complains about them - keep them anyway!
  7166. (file (abbreviate-file-name (buffer-file-name)))
  7167. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  7168. (time (format-time-string
  7169. (substring (cdr org-time-stamp-formats) 1 -1)
  7170. (current-time)))
  7171. afile heading buffer level newfile-p
  7172. category todo priority
  7173. ;; start of variables that will be used for savind context
  7174. ltags itags prop)
  7175. ;; Try to find a local archive location
  7176. (save-excursion
  7177. (save-restriction
  7178. (widen)
  7179. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  7180. (if (and prop (string-match "\\S-" prop))
  7181. (setq org-archive-location prop)
  7182. (if (or (re-search-backward re nil t)
  7183. (re-search-forward re nil t))
  7184. (setq org-archive-location (match-string 1))))))
  7185. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  7186. (progn
  7187. (setq afile (format (match-string 1 org-archive-location)
  7188. (file-name-nondirectory buffer-file-name))
  7189. heading (match-string 2 org-archive-location)))
  7190. (error "Invalid `org-archive-location'"))
  7191. (if (> (length afile) 0)
  7192. (setq newfile-p (not (file-exists-p afile))
  7193. buffer (find-file-noselect afile))
  7194. (setq buffer (current-buffer)))
  7195. (unless buffer
  7196. (error "Cannot access file \"%s\"" afile))
  7197. (if (and (> (length heading) 0)
  7198. (string-match "^\\*+" heading))
  7199. (setq level (match-end 0))
  7200. (setq heading nil level 0))
  7201. (save-excursion
  7202. (org-back-to-heading t)
  7203. ;; Get context information that will be lost by moving the tree
  7204. (org-refresh-category-properties)
  7205. (setq category (org-get-category)
  7206. todo (and (looking-at org-todo-line-regexp)
  7207. (match-string 2))
  7208. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7209. ltags (org-get-tags)
  7210. itags (org-delete-all ltags (org-get-tags-at)))
  7211. (setq ltags (mapconcat 'identity ltags " ")
  7212. itags (mapconcat 'identity itags " "))
  7213. ;; We first only copy, in case something goes wrong
  7214. ;; we need to protect this-command, to avoid kill-region sets it,
  7215. ;; which would lead to duplication of subtrees
  7216. (let (this-command) (org-copy-subtree))
  7217. (set-buffer buffer)
  7218. ;; Enforce org-mode for the archive buffer
  7219. (if (not (org-mode-p))
  7220. ;; Force the mode for future visits.
  7221. (let ((org-insert-mode-line-in-empty-file t)
  7222. (org-inhibit-startup t))
  7223. (call-interactively 'org-mode)))
  7224. (when newfile-p
  7225. (goto-char (point-max))
  7226. (insert (format "\nArchived entries from file %s\n\n"
  7227. (buffer-file-name this-buffer))))
  7228. ;; Force the TODO keywords of the original buffer
  7229. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7230. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7231. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7232. (org-done-keywords tr-org-done-keywords)
  7233. (org-todo-regexp tr-org-todo-regexp)
  7234. (org-todo-line-regexp tr-org-todo-line-regexp)
  7235. (org-odd-levels-only
  7236. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7237. org-odd-levels-only
  7238. tr-org-odd-levels-only)))
  7239. (goto-char (point-min))
  7240. (if heading
  7241. (progn
  7242. (if (re-search-forward
  7243. (concat "^" (regexp-quote heading)
  7244. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7245. nil t)
  7246. (goto-char (match-end 0))
  7247. ;; Heading not found, just insert it at the end
  7248. (goto-char (point-max))
  7249. (or (bolp) (insert "\n"))
  7250. (insert "\n" heading "\n")
  7251. (end-of-line 0))
  7252. ;; Make the subtree visible
  7253. (show-subtree)
  7254. (org-end-of-subtree t)
  7255. (skip-chars-backward " \t\r\n")
  7256. (and (looking-at "[ \t\r\n]*")
  7257. (replace-match "\n\n")))
  7258. ;; No specific heading, just go to end of file.
  7259. (goto-char (point-max)) (insert "\n"))
  7260. ;; Paste
  7261. (org-paste-subtree (org-get-legal-level level 1))
  7262. ;; Mark the entry as done
  7263. (when (and org-archive-mark-done
  7264. (looking-at org-todo-line-regexp)
  7265. (or (not (match-end 2))
  7266. (not (member (match-string 2) org-done-keywords))))
  7267. (let (org-log-done org-todo-log-states)
  7268. (org-todo
  7269. (car (or (member org-archive-mark-done org-done-keywords)
  7270. org-done-keywords)))))
  7271. ;; Add the context info
  7272. (when org-archive-save-context-info
  7273. (let ((l org-archive-save-context-info) e n v)
  7274. (while (setq e (pop l))
  7275. (when (and (setq v (symbol-value e))
  7276. (stringp v) (string-match "\\S-" v))
  7277. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7278. (org-entry-put (point) n v)))))
  7279. ;; Save the buffer, if it is not the same buffer.
  7280. (if (not (eq this-buffer buffer)) (save-buffer))))
  7281. ;; Here we are back in the original buffer. Everything seems to have
  7282. ;; worked. So now cut the tree and finish up.
  7283. (let (this-command) (org-cut-subtree))
  7284. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7285. (message "Subtree archived %s"
  7286. (if (eq this-buffer buffer)
  7287. (concat "under heading: " heading)
  7288. (concat "in file: " (abbreviate-file-name afile)))))))
  7289. (defun org-refresh-category-properties ()
  7290. "Refresh category text properties in teh buffer."
  7291. (let ((def-cat (cond
  7292. ((null org-category)
  7293. (if buffer-file-name
  7294. (file-name-sans-extension
  7295. (file-name-nondirectory buffer-file-name))
  7296. "???"))
  7297. ((symbolp org-category) (symbol-name org-category))
  7298. (t org-category)))
  7299. beg end cat pos optionp)
  7300. (org-unmodified
  7301. (save-excursion
  7302. (save-restriction
  7303. (widen)
  7304. (goto-char (point-min))
  7305. (put-text-property (point) (point-max) 'org-category def-cat)
  7306. (while (re-search-forward
  7307. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7308. (setq pos (match-end 0)
  7309. optionp (equal (char-after (match-beginning 0)) ?#)
  7310. cat (org-trim (match-string 2)))
  7311. (if optionp
  7312. (setq beg (point-at-bol) end (point-max))
  7313. (org-back-to-heading t)
  7314. (setq beg (point) end (org-end-of-subtree t t)))
  7315. (put-text-property beg end 'org-category cat)
  7316. (goto-char pos)))))))
  7317. (defun org-archive-all-done (&optional tag)
  7318. "Archive sublevels of the current tree without open TODO items.
  7319. If the cursor is not on a headline, try all level 1 trees. If
  7320. it is on a headline, try all direct children.
  7321. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7322. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7323. (rea (concat ".*:" org-archive-tag ":"))
  7324. (begm (make-marker))
  7325. (endm (make-marker))
  7326. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7327. "Move subtree to archive (no open TODO items)? "))
  7328. beg end (cntarch 0))
  7329. (if (org-on-heading-p)
  7330. (progn
  7331. (setq re1 (concat "^" (regexp-quote
  7332. (make-string
  7333. (1+ (- (match-end 0) (match-beginning 0) 1))
  7334. ?*))
  7335. " "))
  7336. (move-marker begm (point))
  7337. (move-marker endm (org-end-of-subtree t)))
  7338. (setq re1 "^* ")
  7339. (move-marker begm (point-min))
  7340. (move-marker endm (point-max)))
  7341. (save-excursion
  7342. (goto-char begm)
  7343. (while (re-search-forward re1 endm t)
  7344. (setq beg (match-beginning 0)
  7345. end (save-excursion (org-end-of-subtree t) (point)))
  7346. (goto-char beg)
  7347. (if (re-search-forward re end t)
  7348. (goto-char end)
  7349. (goto-char beg)
  7350. (if (and (or (not tag) (not (looking-at rea)))
  7351. (y-or-n-p question))
  7352. (progn
  7353. (if tag
  7354. (org-toggle-tag org-archive-tag 'on)
  7355. (org-archive-subtree))
  7356. (setq cntarch (1+ cntarch)))
  7357. (goto-char end)))))
  7358. (message "%d trees archived" cntarch)))
  7359. (defun org-cycle-hide-drawers (state)
  7360. "Re-hide all drawers after a visibility state change."
  7361. (when (and (org-mode-p)
  7362. (not (memq state '(overview folded))))
  7363. (save-excursion
  7364. (let* ((globalp (memq state '(contents all)))
  7365. (beg (if globalp (point-min) (point)))
  7366. (end (if globalp (point-max) (org-end-of-subtree t))))
  7367. (goto-char beg)
  7368. (while (re-search-forward org-drawer-regexp end t)
  7369. (org-flag-drawer t))))))
  7370. (defun org-flag-drawer (flag)
  7371. (save-excursion
  7372. (beginning-of-line 1)
  7373. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7374. (let ((b (match-end 0))
  7375. (outline-regexp org-outline-regexp))
  7376. (if (re-search-forward
  7377. "^[ \t]*:END:"
  7378. (save-excursion (outline-next-heading) (point)) t)
  7379. (outline-flag-region b (point-at-eol) flag)
  7380. (error ":END: line missing"))))))
  7381. (defun org-cycle-hide-archived-subtrees (state)
  7382. "Re-hide all archived subtrees after a visibility state change."
  7383. (when (and (not org-cycle-open-archived-trees)
  7384. (not (memq state '(overview folded))))
  7385. (save-excursion
  7386. (let* ((globalp (memq state '(contents all)))
  7387. (beg (if globalp (point-min) (point)))
  7388. (end (if globalp (point-max) (org-end-of-subtree t))))
  7389. (org-hide-archived-subtrees beg end)
  7390. (goto-char beg)
  7391. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7392. (message "%s" (substitute-command-keys
  7393. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7394. (defun org-force-cycle-archived ()
  7395. "Cycle subtree even if it is archived."
  7396. (interactive)
  7397. (setq this-command 'org-cycle)
  7398. (let ((org-cycle-open-archived-trees t))
  7399. (call-interactively 'org-cycle)))
  7400. (defun org-hide-archived-subtrees (beg end)
  7401. "Re-hide all archived subtrees after a visibility state change."
  7402. (save-excursion
  7403. (let* ((re (concat ":" org-archive-tag ":")))
  7404. (goto-char beg)
  7405. (while (re-search-forward re end t)
  7406. (and (org-on-heading-p) (hide-subtree))
  7407. (org-end-of-subtree t)))))
  7408. (defun org-toggle-tag (tag &optional onoff)
  7409. "Toggle the tag TAG for the current line.
  7410. If ONOFF is `on' or `off', don't toggle but set to this state."
  7411. (unless (org-on-heading-p t) (error "Not on headling"))
  7412. (let (res current)
  7413. (save-excursion
  7414. (beginning-of-line)
  7415. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7416. (point-at-eol) t)
  7417. (progn
  7418. (setq current (match-string 1))
  7419. (replace-match ""))
  7420. (setq current ""))
  7421. (setq current (nreverse (org-split-string current ":")))
  7422. (cond
  7423. ((eq onoff 'on)
  7424. (setq res t)
  7425. (or (member tag current) (push tag current)))
  7426. ((eq onoff 'off)
  7427. (or (not (member tag current)) (setq current (delete tag current))))
  7428. (t (if (member tag current)
  7429. (setq current (delete tag current))
  7430. (setq res t)
  7431. (push tag current))))
  7432. (end-of-line 1)
  7433. (if current
  7434. (progn
  7435. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7436. (org-set-tags nil t))
  7437. (delete-horizontal-space))
  7438. (run-hooks 'org-after-tags-change-hook))
  7439. res))
  7440. (defun org-toggle-archive-tag (&optional arg)
  7441. "Toggle the archive tag for the current headline.
  7442. With prefix ARG, check all children of current headline and offer tagging
  7443. the children that do not contain any open TODO items."
  7444. (interactive "P")
  7445. (if arg
  7446. (org-archive-all-done 'tag)
  7447. (let (set)
  7448. (save-excursion
  7449. (org-back-to-heading t)
  7450. (setq set (org-toggle-tag org-archive-tag))
  7451. (when set (hide-subtree)))
  7452. (and set (beginning-of-line 1))
  7453. (message "Subtree %s" (if set "archived" "unarchived")))))
  7454. ;;;; Tables
  7455. ;;; The table editor
  7456. ;; Watch out: Here we are talking about two different kind of tables.
  7457. ;; Most of the code is for the tables created with the Org-mode table editor.
  7458. ;; Sometimes, we talk about tables created and edited with the table.el
  7459. ;; Emacs package. We call the former org-type tables, and the latter
  7460. ;; table.el-type tables.
  7461. (defun org-before-change-function (beg end)
  7462. "Every change indicates that a table might need an update."
  7463. (setq org-table-may-need-update t))
  7464. (defconst org-table-line-regexp "^[ \t]*|"
  7465. "Detects an org-type table line.")
  7466. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7467. "Detects an org-type table line.")
  7468. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7469. "Detects a table line marked for automatic recalculation.")
  7470. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7471. "Detects a table line marked for automatic recalculation.")
  7472. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7473. "Detects a table line marked for automatic recalculation.")
  7474. (defconst org-table-hline-regexp "^[ \t]*|-"
  7475. "Detects an org-type table hline.")
  7476. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7477. "Detects a table-type table hline.")
  7478. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7479. "Detects an org-type or table-type table.")
  7480. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7481. "Searching from within a table (any type) this finds the first line
  7482. outside the table.")
  7483. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7484. "Searching from within a table (any type) this finds the first line
  7485. outside the table.")
  7486. (defvar org-table-last-highlighted-reference nil)
  7487. (defvar org-table-formula-history nil)
  7488. (defvar org-table-column-names nil
  7489. "Alist with column names, derived from the `!' line.")
  7490. (defvar org-table-column-name-regexp nil
  7491. "Regular expression matching the current column names.")
  7492. (defvar org-table-local-parameters nil
  7493. "Alist with parameter names, derived from the `$' line.")
  7494. (defvar org-table-named-field-locations nil
  7495. "Alist with locations of named fields.")
  7496. (defvar org-table-current-line-types nil
  7497. "Table row types, non-nil only for the duration of a comand.")
  7498. (defvar org-table-current-begin-line nil
  7499. "Table begin line, non-nil only for the duration of a comand.")
  7500. (defvar org-table-current-begin-pos nil
  7501. "Table begin position, non-nil only for the duration of a comand.")
  7502. (defvar org-table-dlines nil
  7503. "Vector of data line line numbers in the current table.")
  7504. (defvar org-table-hlines nil
  7505. "Vector of hline line numbers in the current table.")
  7506. (defconst org-table-range-regexp
  7507. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7508. ;; 1 2 3 4 5
  7509. "Regular expression for matching ranges in formulas.")
  7510. (defconst org-table-range-regexp2
  7511. (concat
  7512. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7513. "\\.\\."
  7514. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7515. "Match a range for reference display.")
  7516. (defconst org-table-translate-regexp
  7517. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7518. "Match a reference that needs translation, for reference display.")
  7519. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7520. (defun org-table-create-with-table.el ()
  7521. "Use the table.el package to insert a new table.
  7522. If there is already a table at point, convert between Org-mode tables
  7523. and table.el tables."
  7524. (interactive)
  7525. (require 'table)
  7526. (cond
  7527. ((org-at-table.el-p)
  7528. (if (y-or-n-p "Convert table to Org-mode table? ")
  7529. (org-table-convert)))
  7530. ((org-at-table-p)
  7531. (if (y-or-n-p "Convert table to table.el table? ")
  7532. (org-table-convert)))
  7533. (t (call-interactively 'table-insert))))
  7534. (defun org-table-create-or-convert-from-region (arg)
  7535. "Convert region to table, or create an empty table.
  7536. If there is an active region, convert it to a table, using the function
  7537. `org-table-convert-region'. See the documentation of that function
  7538. to learn how the prefix argument is interpreted to determine the field
  7539. separator.
  7540. If there is no such region, create an empty table with `org-table-create'."
  7541. (interactive "P")
  7542. (if (org-region-active-p)
  7543. (org-table-convert-region (region-beginning) (region-end) arg)
  7544. (org-table-create arg)))
  7545. (defun org-table-create (&optional size)
  7546. "Query for a size and insert a table skeleton.
  7547. SIZE is a string Columns x Rows like for example \"3x2\"."
  7548. (interactive "P")
  7549. (unless size
  7550. (setq size (read-string
  7551. (concat "Table size Columns x Rows [e.g. "
  7552. org-table-default-size "]: ")
  7553. "" nil org-table-default-size)))
  7554. (let* ((pos (point))
  7555. (indent (make-string (current-column) ?\ ))
  7556. (split (org-split-string size " *x *"))
  7557. (rows (string-to-number (nth 1 split)))
  7558. (columns (string-to-number (car split)))
  7559. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7560. "\n")))
  7561. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7562. (point-at-bol) (point)))
  7563. (beginning-of-line 1)
  7564. (newline))
  7565. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7566. (dotimes (i rows) (insert line))
  7567. (goto-char pos)
  7568. (if (> rows 1)
  7569. ;; Insert a hline after the first row.
  7570. (progn
  7571. (end-of-line 1)
  7572. (insert "\n|-")
  7573. (goto-char pos)))
  7574. (org-table-align)))
  7575. (defun org-table-convert-region (beg0 end0 &optional separator)
  7576. "Convert region to a table.
  7577. The region goes from BEG0 to END0, but these borders will be moved
  7578. slightly, to make sure a beginning of line in the first line is included.
  7579. SEPARATOR specifies the field separator in the lines. It can have the
  7580. following values:
  7581. '(4) Use the comma as a field separator
  7582. '(16) Use a TAB as field separator
  7583. integer When a number, use that many spaces as field separator
  7584. nil When nil, the command tries to be smart and figure out the
  7585. separator in the following way:
  7586. - when each line contains a TAB, assume TAB-separated material
  7587. - when each line contains a comme, assume CSV material
  7588. - else, assume one or more SPACE charcters as separator."
  7589. (interactive "rP")
  7590. (let* ((beg (min beg0 end0))
  7591. (end (max beg0 end0))
  7592. re)
  7593. (goto-char beg)
  7594. (beginning-of-line 1)
  7595. (setq beg (move-marker (make-marker) (point)))
  7596. (goto-char end)
  7597. (if (bolp) (backward-char 1) (end-of-line 1))
  7598. (setq end (move-marker (make-marker) (point)))
  7599. ;; Get the right field separator
  7600. (unless separator
  7601. (goto-char beg)
  7602. (setq separator
  7603. (cond
  7604. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7605. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7606. (t 1))))
  7607. (setq re (cond
  7608. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7609. ((equal separator '(16)) "^\\|\t")
  7610. ((integerp separator)
  7611. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7612. (t (error "This should not happen"))))
  7613. (goto-char beg)
  7614. (while (re-search-forward re end t)
  7615. (replace-match "| " t t))
  7616. (goto-char beg)
  7617. (insert " ")
  7618. (org-table-align)))
  7619. (defun org-table-import (file arg)
  7620. "Import FILE as a table.
  7621. The file is assumed to be tab-separated. Such files can be produced by most
  7622. spreadsheet and database applications. If no tabs (at least one per line)
  7623. are found, lines will be split on whitespace into fields."
  7624. (interactive "f\nP")
  7625. (or (bolp) (newline))
  7626. (let ((beg (point))
  7627. (pm (point-max)))
  7628. (insert-file-contents file)
  7629. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7630. (defun org-table-export ()
  7631. "Export table as a tab-separated file.
  7632. Such a file can be imported into a spreadsheet program like Excel."
  7633. (interactive)
  7634. (let* ((beg (org-table-begin))
  7635. (end (org-table-end))
  7636. (table (buffer-substring beg end))
  7637. (file (read-file-name "Export table to: "))
  7638. buf)
  7639. (unless (or (not (file-exists-p file))
  7640. (y-or-n-p (format "Overwrite file %s? " file)))
  7641. (error "Abort"))
  7642. (with-current-buffer (find-file-noselect file)
  7643. (setq buf (current-buffer))
  7644. (erase-buffer)
  7645. (fundamental-mode)
  7646. (insert table)
  7647. (goto-char (point-min))
  7648. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7649. (replace-match "" t t)
  7650. (end-of-line 1))
  7651. (goto-char (point-min))
  7652. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7653. (replace-match "" t t)
  7654. (goto-char (min (1+ (point)) (point-max))))
  7655. (goto-char (point-min))
  7656. (while (re-search-forward "^-[-+]*$" nil t)
  7657. (replace-match "")
  7658. (if (looking-at "\n")
  7659. (delete-char 1)))
  7660. (goto-char (point-min))
  7661. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7662. (replace-match "\t" t t))
  7663. (save-buffer))
  7664. (kill-buffer buf)))
  7665. (defvar org-table-aligned-begin-marker (make-marker)
  7666. "Marker at the beginning of the table last aligned.
  7667. Used to check if cursor still is in that table, to minimize realignment.")
  7668. (defvar org-table-aligned-end-marker (make-marker)
  7669. "Marker at the end of the table last aligned.
  7670. Used to check if cursor still is in that table, to minimize realignment.")
  7671. (defvar org-table-last-alignment nil
  7672. "List of flags for flushright alignment, from the last re-alignment.
  7673. This is being used to correctly align a single field after TAB or RET.")
  7674. (defvar org-table-last-column-widths nil
  7675. "List of max width of fields in each column.
  7676. This is being used to correctly align a single field after TAB or RET.")
  7677. (defvar org-table-overlay-coordinates nil
  7678. "Overlay coordinates after each align of a table.")
  7679. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7680. (defvar org-last-recalc-line nil)
  7681. (defconst org-narrow-column-arrow "=>"
  7682. "Used as display property in narrowed table columns.")
  7683. (defun org-table-align ()
  7684. "Align the table at point by aligning all vertical bars."
  7685. (interactive)
  7686. (let* (
  7687. ;; Limits of table
  7688. (beg (org-table-begin))
  7689. (end (org-table-end))
  7690. ;; Current cursor position
  7691. (linepos (org-current-line))
  7692. (colpos (org-table-current-column))
  7693. (winstart (window-start))
  7694. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7695. lines (new "") lengths l typenums ty fields maxfields i
  7696. column
  7697. (indent "") cnt frac
  7698. rfmt hfmt
  7699. (spaces '(1 . 1))
  7700. (sp1 (car spaces))
  7701. (sp2 (cdr spaces))
  7702. (rfmt1 (concat
  7703. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7704. (hfmt1 (concat
  7705. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7706. emptystrings links dates emph narrow fmax f1 len c e)
  7707. (untabify beg end)
  7708. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7709. ;; Check if we have links or dates
  7710. (goto-char beg)
  7711. (setq links (re-search-forward org-bracket-link-regexp end t))
  7712. (goto-char beg)
  7713. (setq emph (and org-hide-emphasis-markers
  7714. (re-search-forward org-emph-re end t)))
  7715. (goto-char beg)
  7716. (setq dates (and org-display-custom-times
  7717. (re-search-forward org-ts-regexp-both end t)))
  7718. ;; Make sure the link properties are right
  7719. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7720. ;; Make sure the date properties are right
  7721. (when dates (goto-char beg) (while (org-activate-dates end)))
  7722. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7723. ;; Check if we are narrowing any columns
  7724. (goto-char beg)
  7725. (setq narrow (and org-format-transports-properties-p
  7726. (re-search-forward "<[0-9]+>" end t)))
  7727. ;; Get the rows
  7728. (setq lines (org-split-string
  7729. (buffer-substring beg end) "\n"))
  7730. ;; Store the indentation of the first line
  7731. (if (string-match "^ *" (car lines))
  7732. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7733. ;; Mark the hlines by setting the corresponding element to nil
  7734. ;; At the same time, we remove trailing space.
  7735. (setq lines (mapcar (lambda (l)
  7736. (if (string-match "^ *|-" l)
  7737. nil
  7738. (if (string-match "[ \t]+$" l)
  7739. (substring l 0 (match-beginning 0))
  7740. l)))
  7741. lines))
  7742. ;; Get the data fields by splitting the lines.
  7743. (setq fields (mapcar
  7744. (lambda (l)
  7745. (org-split-string l " *| *"))
  7746. (delq nil (copy-sequence lines))))
  7747. ;; How many fields in the longest line?
  7748. (condition-case nil
  7749. (setq maxfields (apply 'max (mapcar 'length fields)))
  7750. (error
  7751. (kill-region beg end)
  7752. (org-table-create org-table-default-size)
  7753. (error "Empty table - created default table")))
  7754. ;; A list of empty strings to fill any short rows on output
  7755. (setq emptystrings (make-list maxfields ""))
  7756. ;; Check for special formatting.
  7757. (setq i -1)
  7758. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7759. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7760. ;; Check if there is an explicit width specified
  7761. (when narrow
  7762. (setq c column fmax nil)
  7763. (while c
  7764. (setq e (pop c))
  7765. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7766. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7767. ;; Find fields that are wider than fmax, and shorten them
  7768. (when fmax
  7769. (loop for xx in column do
  7770. (when (and (stringp xx)
  7771. (> (org-string-width xx) fmax))
  7772. (org-add-props xx nil
  7773. 'help-echo
  7774. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7775. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7776. (unless (> f1 1)
  7777. (error "Cannot narrow field starting with wide link \"%s\""
  7778. (match-string 0 xx)))
  7779. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7780. (add-text-properties (- f1 2) f1
  7781. (list 'display org-narrow-column-arrow)
  7782. xx)))))
  7783. ;; Get the maximum width for each column
  7784. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7785. ;; Get the fraction of numbers, to decide about alignment of the column
  7786. (setq cnt 0 frac 0.0)
  7787. (loop for x in column do
  7788. (if (equal x "")
  7789. nil
  7790. (setq frac ( / (+ (* frac cnt)
  7791. (if (string-match org-table-number-regexp x) 1 0))
  7792. (setq cnt (1+ cnt))))))
  7793. (push (>= frac org-table-number-fraction) typenums))
  7794. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7795. ;; Store the alignment of this table, for later editing of single fields
  7796. (setq org-table-last-alignment typenums
  7797. org-table-last-column-widths lengths)
  7798. ;; With invisible characters, `format' does not get the field width right
  7799. ;; So we need to make these fields wide by hand.
  7800. (when (or links emph)
  7801. (loop for i from 0 upto (1- maxfields) do
  7802. (setq len (nth i lengths))
  7803. (loop for j from 0 upto (1- (length fields)) do
  7804. (setq c (nthcdr i (car (nthcdr j fields))))
  7805. (if (and (stringp (car c))
  7806. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7807. ; (string-match org-bracket-link-regexp (car c))
  7808. (< (org-string-width (car c)) len))
  7809. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7810. ;; Compute the formats needed for output of the table
  7811. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7812. (while (setq l (pop lengths))
  7813. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7814. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7815. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7816. (setq rfmt (concat rfmt "\n")
  7817. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7818. (setq new (mapconcat
  7819. (lambda (l)
  7820. (if l (apply 'format rfmt
  7821. (append (pop fields) emptystrings))
  7822. hfmt))
  7823. lines ""))
  7824. ;; Replace the old one
  7825. (delete-region beg end)
  7826. (move-marker end nil)
  7827. (move-marker org-table-aligned-begin-marker (point))
  7828. (insert new)
  7829. (move-marker org-table-aligned-end-marker (point))
  7830. (when (and orgtbl-mode (not (org-mode-p)))
  7831. (goto-char org-table-aligned-begin-marker)
  7832. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7833. ;; Try to move to the old location
  7834. (goto-line winstartline)
  7835. (setq winstart (point-at-bol))
  7836. (goto-line linepos)
  7837. (set-window-start (selected-window) winstart 'noforce)
  7838. (org-table-goto-column colpos)
  7839. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7840. (setq org-table-may-need-update nil)
  7841. ))
  7842. (defun org-string-width (s)
  7843. "Compute width of string, ignoring invisible characters.
  7844. This ignores character with invisibility property `org-link', and also
  7845. characters with property `org-cwidth', because these will become invisible
  7846. upon the next fontification round."
  7847. (let (b l)
  7848. (when (or (eq t buffer-invisibility-spec)
  7849. (assq 'org-link buffer-invisibility-spec))
  7850. (while (setq b (text-property-any 0 (length s)
  7851. 'invisible 'org-link s))
  7852. (setq s (concat (substring s 0 b)
  7853. (substring s (or (next-single-property-change
  7854. b 'invisible s) (length s)))))))
  7855. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7856. (setq s (concat (substring s 0 b)
  7857. (substring s (or (next-single-property-change
  7858. b 'org-cwidth s) (length s))))))
  7859. (setq l (string-width s) b -1)
  7860. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7861. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7862. l))
  7863. (defun org-table-begin (&optional table-type)
  7864. "Find the beginning of the table and return its position.
  7865. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7866. (save-excursion
  7867. (if (not (re-search-backward
  7868. (if table-type org-table-any-border-regexp
  7869. org-table-border-regexp)
  7870. nil t))
  7871. (progn (goto-char (point-min)) (point))
  7872. (goto-char (match-beginning 0))
  7873. (beginning-of-line 2)
  7874. (point))))
  7875. (defun org-table-end (&optional table-type)
  7876. "Find the end of the table and return its position.
  7877. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7878. (save-excursion
  7879. (if (not (re-search-forward
  7880. (if table-type org-table-any-border-regexp
  7881. org-table-border-regexp)
  7882. nil t))
  7883. (goto-char (point-max))
  7884. (goto-char (match-beginning 0)))
  7885. (point-marker)))
  7886. (defun org-table-justify-field-maybe (&optional new)
  7887. "Justify the current field, text to left, number to right.
  7888. Optional argument NEW may specify text to replace the current field content."
  7889. (cond
  7890. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7891. ((org-at-table-hline-p))
  7892. ((and (not new)
  7893. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7894. (current-buffer)))
  7895. (< (point) org-table-aligned-begin-marker)
  7896. (>= (point) org-table-aligned-end-marker)))
  7897. ;; This is not the same table, force a full re-align
  7898. (setq org-table-may-need-update t))
  7899. (t ;; realign the current field, based on previous full realign
  7900. (let* ((pos (point)) s
  7901. (col (org-table-current-column))
  7902. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7903. l f n o e)
  7904. (when (> col 0)
  7905. (skip-chars-backward "^|\n")
  7906. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7907. (progn
  7908. (setq s (match-string 1)
  7909. o (match-string 0)
  7910. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7911. e (not (= (match-beginning 2) (match-end 2))))
  7912. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7913. l (if e "|" (setq org-table-may-need-update t) ""))
  7914. n (format f s))
  7915. (if new
  7916. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7917. (setq n (format f new))
  7918. (setq n (concat new "|") org-table-may-need-update t)))
  7919. (or (equal n o)
  7920. (let (org-table-may-need-update)
  7921. (replace-match n t t))))
  7922. (setq org-table-may-need-update t))
  7923. (goto-char pos))))))
  7924. (defun org-table-next-field ()
  7925. "Go to the next field in the current table, creating new lines as needed.
  7926. Before doing so, re-align the table if necessary."
  7927. (interactive)
  7928. (org-table-maybe-eval-formula)
  7929. (org-table-maybe-recalculate-line)
  7930. (if (and org-table-automatic-realign
  7931. org-table-may-need-update)
  7932. (org-table-align))
  7933. (let ((end (org-table-end)))
  7934. (if (org-at-table-hline-p)
  7935. (end-of-line 1))
  7936. (condition-case nil
  7937. (progn
  7938. (re-search-forward "|" end)
  7939. (if (looking-at "[ \t]*$")
  7940. (re-search-forward "|" end))
  7941. (if (and (looking-at "-")
  7942. org-table-tab-jumps-over-hlines
  7943. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7944. (goto-char (match-beginning 1)))
  7945. (if (looking-at "-")
  7946. (progn
  7947. (beginning-of-line 0)
  7948. (org-table-insert-row 'below))
  7949. (if (looking-at " ") (forward-char 1))))
  7950. (error
  7951. (org-table-insert-row 'below)))))
  7952. (defun org-table-previous-field ()
  7953. "Go to the previous field in the table.
  7954. Before doing so, re-align the table if necessary."
  7955. (interactive)
  7956. (org-table-justify-field-maybe)
  7957. (org-table-maybe-recalculate-line)
  7958. (if (and org-table-automatic-realign
  7959. org-table-may-need-update)
  7960. (org-table-align))
  7961. (if (org-at-table-hline-p)
  7962. (end-of-line 1))
  7963. (re-search-backward "|" (org-table-begin))
  7964. (re-search-backward "|" (org-table-begin))
  7965. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7966. (re-search-backward "|" (org-table-begin)))
  7967. (if (looking-at "| ?")
  7968. (goto-char (match-end 0))))
  7969. (defun org-table-next-row ()
  7970. "Go to the next row (same column) in the current table.
  7971. Before doing so, re-align the table if necessary."
  7972. (interactive)
  7973. (org-table-maybe-eval-formula)
  7974. (org-table-maybe-recalculate-line)
  7975. (if (or (looking-at "[ \t]*$")
  7976. (save-excursion (skip-chars-backward " \t") (bolp)))
  7977. (newline)
  7978. (if (and org-table-automatic-realign
  7979. org-table-may-need-update)
  7980. (org-table-align))
  7981. (let ((col (org-table-current-column)))
  7982. (beginning-of-line 2)
  7983. (if (or (not (org-at-table-p))
  7984. (org-at-table-hline-p))
  7985. (progn
  7986. (beginning-of-line 0)
  7987. (org-table-insert-row 'below)))
  7988. (org-table-goto-column col)
  7989. (skip-chars-backward "^|\n\r")
  7990. (if (looking-at " ") (forward-char 1)))))
  7991. (defun org-table-copy-down (n)
  7992. "Copy a field down in the current column.
  7993. If the field at the cursor is empty, copy into it the content of the nearest
  7994. non-empty field above. With argument N, use the Nth non-empty field.
  7995. If the current field is not empty, it is copied down to the next row, and
  7996. the cursor is moved with it. Therefore, repeating this command causes the
  7997. column to be filled row-by-row.
  7998. If the variable `org-table-copy-increment' is non-nil and the field is an
  7999. integer or a timestamp, it will be incremented while copying. In the case of
  8000. a timestamp, if the cursor is on the year, change the year. If it is on the
  8001. month or the day, change that. Point will stay on the current date field
  8002. in order to easily repeat the interval."
  8003. (interactive "p")
  8004. (let* ((colpos (org-table-current-column))
  8005. (col (current-column))
  8006. (field (org-table-get-field))
  8007. (non-empty (string-match "[^ \t]" field))
  8008. (beg (org-table-begin))
  8009. txt)
  8010. (org-table-check-inside-data-field)
  8011. (if non-empty
  8012. (progn
  8013. (setq txt (org-trim field))
  8014. (org-table-next-row)
  8015. (org-table-blank-field))
  8016. (save-excursion
  8017. (setq txt
  8018. (catch 'exit
  8019. (while (progn (beginning-of-line 1)
  8020. (re-search-backward org-table-dataline-regexp
  8021. beg t))
  8022. (org-table-goto-column colpos t)
  8023. (if (and (looking-at
  8024. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  8025. (= (setq n (1- n)) 0))
  8026. (throw 'exit (match-string 1))))))))
  8027. (if txt
  8028. (progn
  8029. (if (and org-table-copy-increment
  8030. (string-match "^[0-9]+$" txt))
  8031. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  8032. (insert txt)
  8033. (move-to-column col)
  8034. (if (and org-table-copy-increment (org-at-timestamp-p t))
  8035. (org-timestamp-up 1)
  8036. (org-table-maybe-recalculate-line))
  8037. (org-table-align)
  8038. (move-to-column col))
  8039. (error "No non-empty field found"))))
  8040. (defun org-table-check-inside-data-field ()
  8041. "Is point inside a table data field?
  8042. I.e. not on a hline or before the first or after the last column?
  8043. This actually throws an error, so it aborts the current command."
  8044. (if (or (not (org-at-table-p))
  8045. (= (org-table-current-column) 0)
  8046. (org-at-table-hline-p)
  8047. (looking-at "[ \t]*$"))
  8048. (error "Not in table data field")))
  8049. (defvar org-table-clip nil
  8050. "Clipboard for table regions.")
  8051. (defun org-table-blank-field ()
  8052. "Blank the current table field or active region."
  8053. (interactive)
  8054. (org-table-check-inside-data-field)
  8055. (if (and (interactive-p) (org-region-active-p))
  8056. (let (org-table-clip)
  8057. (org-table-cut-region (region-beginning) (region-end)))
  8058. (skip-chars-backward "^|")
  8059. (backward-char 1)
  8060. (if (looking-at "|[^|\n]+")
  8061. (let* ((pos (match-beginning 0))
  8062. (match (match-string 0))
  8063. (len (org-string-width match)))
  8064. (replace-match (concat "|" (make-string (1- len) ?\ )))
  8065. (goto-char (+ 2 pos))
  8066. (substring match 1)))))
  8067. (defun org-table-get-field (&optional n replace)
  8068. "Return the value of the field in column N of current row.
  8069. N defaults to current field.
  8070. If REPLACE is a string, replace field with this value. The return value
  8071. is always the old value."
  8072. (and n (org-table-goto-column n))
  8073. (skip-chars-backward "^|\n")
  8074. (backward-char 1)
  8075. (if (looking-at "|[^|\r\n]*")
  8076. (let* ((pos (match-beginning 0))
  8077. (val (buffer-substring (1+ pos) (match-end 0))))
  8078. (if replace
  8079. (replace-match (concat "|" replace) t t))
  8080. (goto-char (min (point-at-eol) (+ 2 pos)))
  8081. val)
  8082. (forward-char 1) ""))
  8083. (defun org-table-field-info (arg)
  8084. "Show info about the current field, and highlight any reference at point."
  8085. (interactive "P")
  8086. (org-table-get-specials)
  8087. (save-excursion
  8088. (let* ((pos (point))
  8089. (col (org-table-current-column))
  8090. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  8091. (name (car (rassoc (list (org-current-line) col)
  8092. org-table-named-field-locations)))
  8093. (eql (org-table-get-stored-formulas))
  8094. (dline (org-table-current-dline))
  8095. (ref (format "@%d$%d" dline col))
  8096. (ref1 (org-table-convert-refs-to-an ref))
  8097. (fequation (or (assoc name eql) (assoc ref eql)))
  8098. (cequation (assoc (int-to-string col) eql))
  8099. (eqn (or fequation cequation)))
  8100. (goto-char pos)
  8101. (condition-case nil
  8102. (org-table-show-reference 'local)
  8103. (error nil))
  8104. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  8105. dline col
  8106. (if cname (concat " or $" cname) "")
  8107. dline col ref1
  8108. (if name (concat " or $" name) "")
  8109. ;; FIXME: formula info not correct if special table line
  8110. (if eqn
  8111. (concat ", formula: "
  8112. (org-table-formula-to-user
  8113. (concat
  8114. (if (string-match "^[$@]"(car eqn)) "" "$")
  8115. (car eqn) "=" (cdr eqn))))
  8116. "")))))
  8117. (defun org-table-current-column ()
  8118. "Find out which column we are in.
  8119. When called interactively, column is also displayed in echo area."
  8120. (interactive)
  8121. (if (interactive-p) (org-table-check-inside-data-field))
  8122. (save-excursion
  8123. (let ((cnt 0) (pos (point)))
  8124. (beginning-of-line 1)
  8125. (while (search-forward "|" pos t)
  8126. (setq cnt (1+ cnt)))
  8127. (if (interactive-p) (message "This is table column %d" cnt))
  8128. cnt)))
  8129. (defun org-table-current-dline ()
  8130. "Find out what table data line we are in.
  8131. Only datalins count for this."
  8132. (interactive)
  8133. (if (interactive-p) (org-table-check-inside-data-field))
  8134. (save-excursion
  8135. (let ((cnt 0) (pos (point)))
  8136. (goto-char (org-table-begin))
  8137. (while (<= (point) pos)
  8138. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  8139. (beginning-of-line 2))
  8140. (if (interactive-p) (message "This is table line %d" cnt))
  8141. cnt)))
  8142. (defun org-table-goto-column (n &optional on-delim force)
  8143. "Move the cursor to the Nth column in the current table line.
  8144. With optional argument ON-DELIM, stop with point before the left delimiter
  8145. of the field.
  8146. If there are less than N fields, just go to after the last delimiter.
  8147. However, when FORCE is non-nil, create new columns if necessary."
  8148. (interactive "p")
  8149. (let ((pos (point-at-eol)))
  8150. (beginning-of-line 1)
  8151. (when (> n 0)
  8152. (while (and (> (setq n (1- n)) -1)
  8153. (or (search-forward "|" pos t)
  8154. (and force
  8155. (progn (end-of-line 1)
  8156. (skip-chars-backward "^|")
  8157. (insert " | "))))))
  8158. ; (backward-char 2) t)))))
  8159. (when (and force (not (looking-at ".*|")))
  8160. (save-excursion (end-of-line 1) (insert " | ")))
  8161. (if on-delim
  8162. (backward-char 1)
  8163. (if (looking-at " ") (forward-char 1))))))
  8164. (defun org-at-table-p (&optional table-type)
  8165. "Return t if the cursor is inside an org-type table.
  8166. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8167. (if org-enable-table-editor
  8168. (save-excursion
  8169. (beginning-of-line 1)
  8170. (looking-at (if table-type org-table-any-line-regexp
  8171. org-table-line-regexp)))
  8172. nil))
  8173. (defun org-at-table.el-p ()
  8174. "Return t if and only if we are at a table.el table."
  8175. (and (org-at-table-p 'any)
  8176. (save-excursion
  8177. (goto-char (org-table-begin 'any))
  8178. (looking-at org-table1-hline-regexp))))
  8179. (defun org-table-recognize-table.el ()
  8180. "If there is a table.el table nearby, recognize it and move into it."
  8181. (if org-table-tab-recognizes-table.el
  8182. (if (org-at-table.el-p)
  8183. (progn
  8184. (beginning-of-line 1)
  8185. (if (looking-at org-table-dataline-regexp)
  8186. nil
  8187. (if (looking-at org-table1-hline-regexp)
  8188. (progn
  8189. (beginning-of-line 2)
  8190. (if (looking-at org-table-any-border-regexp)
  8191. (beginning-of-line -1)))))
  8192. (if (re-search-forward "|" (org-table-end t) t)
  8193. (progn
  8194. (require 'table)
  8195. (if (table--at-cell-p (point))
  8196. t
  8197. (message "recognizing table.el table...")
  8198. (table-recognize-table)
  8199. (message "recognizing table.el table...done")))
  8200. (error "This should not happen..."))
  8201. t)
  8202. nil)
  8203. nil))
  8204. (defun org-at-table-hline-p ()
  8205. "Return t if the cursor is inside a hline in a table."
  8206. (if org-enable-table-editor
  8207. (save-excursion
  8208. (beginning-of-line 1)
  8209. (looking-at org-table-hline-regexp))
  8210. nil))
  8211. (defun org-table-insert-column ()
  8212. "Insert a new column into the table."
  8213. (interactive)
  8214. (if (not (org-at-table-p))
  8215. (error "Not at a table"))
  8216. (org-table-find-dataline)
  8217. (let* ((col (max 1 (org-table-current-column)))
  8218. (beg (org-table-begin))
  8219. (end (org-table-end))
  8220. ;; Current cursor position
  8221. (linepos (org-current-line))
  8222. (colpos col))
  8223. (goto-char beg)
  8224. (while (< (point) end)
  8225. (if (org-at-table-hline-p)
  8226. nil
  8227. (org-table-goto-column col t)
  8228. (insert "| "))
  8229. (beginning-of-line 2))
  8230. (move-marker end nil)
  8231. (goto-line linepos)
  8232. (org-table-goto-column colpos)
  8233. (org-table-align)
  8234. (org-table-fix-formulas "$" nil (1- col) 1)))
  8235. (defun org-table-find-dataline ()
  8236. "Find a dataline in the current table, which is needed for column commands."
  8237. (if (and (org-at-table-p)
  8238. (not (org-at-table-hline-p)))
  8239. t
  8240. (let ((col (current-column))
  8241. (end (org-table-end)))
  8242. (move-to-column col)
  8243. (while (and (< (point) end)
  8244. (or (not (= (current-column) col))
  8245. (org-at-table-hline-p)))
  8246. (beginning-of-line 2)
  8247. (move-to-column col))
  8248. (if (and (org-at-table-p)
  8249. (not (org-at-table-hline-p)))
  8250. t
  8251. (error
  8252. "Please position cursor in a data line for column operations")))))
  8253. (defun org-table-delete-column ()
  8254. "Delete a column from the table."
  8255. (interactive)
  8256. (if (not (org-at-table-p))
  8257. (error "Not at a table"))
  8258. (org-table-find-dataline)
  8259. (org-table-check-inside-data-field)
  8260. (let* ((col (org-table-current-column))
  8261. (beg (org-table-begin))
  8262. (end (org-table-end))
  8263. ;; Current cursor position
  8264. (linepos (org-current-line))
  8265. (colpos col))
  8266. (goto-char beg)
  8267. (while (< (point) end)
  8268. (if (org-at-table-hline-p)
  8269. nil
  8270. (org-table-goto-column col t)
  8271. (and (looking-at "|[^|\n]+|")
  8272. (replace-match "|")))
  8273. (beginning-of-line 2))
  8274. (move-marker end nil)
  8275. (goto-line linepos)
  8276. (org-table-goto-column colpos)
  8277. (org-table-align)
  8278. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8279. col -1 col)))
  8280. (defun org-table-move-column-right ()
  8281. "Move column to the right."
  8282. (interactive)
  8283. (org-table-move-column nil))
  8284. (defun org-table-move-column-left ()
  8285. "Move column to the left."
  8286. (interactive)
  8287. (org-table-move-column 'left))
  8288. (defun org-table-move-column (&optional left)
  8289. "Move the current column to the right. With arg LEFT, move to the left."
  8290. (interactive "P")
  8291. (if (not (org-at-table-p))
  8292. (error "Not at a table"))
  8293. (org-table-find-dataline)
  8294. (org-table-check-inside-data-field)
  8295. (let* ((col (org-table-current-column))
  8296. (col1 (if left (1- col) col))
  8297. (beg (org-table-begin))
  8298. (end (org-table-end))
  8299. ;; Current cursor position
  8300. (linepos (org-current-line))
  8301. (colpos (if left (1- col) (1+ col))))
  8302. (if (and left (= col 1))
  8303. (error "Cannot move column further left"))
  8304. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8305. (error "Cannot move column further right"))
  8306. (goto-char beg)
  8307. (while (< (point) end)
  8308. (if (org-at-table-hline-p)
  8309. nil
  8310. (org-table-goto-column col1 t)
  8311. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8312. (replace-match "|\\2|\\1|")))
  8313. (beginning-of-line 2))
  8314. (move-marker end nil)
  8315. (goto-line linepos)
  8316. (org-table-goto-column colpos)
  8317. (org-table-align)
  8318. (org-table-fix-formulas
  8319. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8320. (cons (number-to-string colpos) (number-to-string col))))))
  8321. (defun org-table-move-row-down ()
  8322. "Move table row down."
  8323. (interactive)
  8324. (org-table-move-row nil))
  8325. (defun org-table-move-row-up ()
  8326. "Move table row up."
  8327. (interactive)
  8328. (org-table-move-row 'up))
  8329. (defun org-table-move-row (&optional up)
  8330. "Move the current table line down. With arg UP, move it up."
  8331. (interactive "P")
  8332. (let* ((col (current-column))
  8333. (pos (point))
  8334. (hline1p (save-excursion (beginning-of-line 1)
  8335. (looking-at org-table-hline-regexp)))
  8336. (dline1 (org-table-current-dline))
  8337. (dline2 (+ dline1 (if up -1 1)))
  8338. (tonew (if up 0 2))
  8339. txt hline2p)
  8340. (beginning-of-line tonew)
  8341. (unless (org-at-table-p)
  8342. (goto-char pos)
  8343. (error "Cannot move row further"))
  8344. (setq hline2p (looking-at org-table-hline-regexp))
  8345. (goto-char pos)
  8346. (beginning-of-line 1)
  8347. (setq pos (point))
  8348. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8349. (delete-region (point) (1+ (point-at-eol)))
  8350. (beginning-of-line tonew)
  8351. (insert txt)
  8352. (beginning-of-line 0)
  8353. (move-to-column col)
  8354. (unless (or hline1p hline2p)
  8355. (org-table-fix-formulas
  8356. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8357. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8358. (defun org-table-insert-row (&optional arg)
  8359. "Insert a new row above the current line into the table.
  8360. With prefix ARG, insert below the current line."
  8361. (interactive "P")
  8362. (if (not (org-at-table-p))
  8363. (error "Not at a table"))
  8364. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8365. (new (org-table-clean-line line)))
  8366. ;; Fix the first field if necessary
  8367. (if (string-match "^[ \t]*| *[#$] *|" line)
  8368. (setq new (replace-match (match-string 0 line) t t new)))
  8369. (beginning-of-line (if arg 2 1))
  8370. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8371. (beginning-of-line 0)
  8372. (re-search-forward "| ?" (point-at-eol) t)
  8373. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8374. (org-table-align))
  8375. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8376. (defun org-table-insert-hline (&optional above)
  8377. "Insert a horizontal-line below the current line into the table.
  8378. With prefix ABOVE, insert above the current line."
  8379. (interactive "P")
  8380. (if (not (org-at-table-p))
  8381. (error "Not at a table"))
  8382. (let ((line (org-table-clean-line
  8383. (buffer-substring (point-at-bol) (point-at-eol))))
  8384. (col (current-column)))
  8385. (while (string-match "|\\( +\\)|" line)
  8386. (setq line (replace-match
  8387. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8388. ?-) "|") t t line)))
  8389. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8390. (beginning-of-line (if above 1 2))
  8391. (insert line "\n")
  8392. (beginning-of-line (if above 1 -1))
  8393. (move-to-column col)
  8394. (and org-table-overlay-coordinates (org-table-align))))
  8395. (defun org-table-hline-and-move (&optional same-column)
  8396. "Insert a hline and move to the row below that line."
  8397. (interactive "P")
  8398. (let ((col (org-table-current-column)))
  8399. (org-table-maybe-eval-formula)
  8400. (org-table-maybe-recalculate-line)
  8401. (org-table-insert-hline)
  8402. (end-of-line 2)
  8403. (if (looking-at "\n[ \t]*|-")
  8404. (progn (insert "\n|") (org-table-align))
  8405. (org-table-next-field))
  8406. (if same-column (org-table-goto-column col))))
  8407. (defun org-table-clean-line (s)
  8408. "Convert a table line S into a string with only \"|\" and space.
  8409. In particular, this does handle wide and invisible characters."
  8410. (if (string-match "^[ \t]*|-" s)
  8411. ;; It's a hline, just map the characters
  8412. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8413. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8414. (setq s (replace-match
  8415. (concat "|" (make-string (org-string-width (match-string 1 s))
  8416. ?\ ) "|")
  8417. t t s)))
  8418. s))
  8419. (defun org-table-kill-row ()
  8420. "Delete the current row or horizontal line from the table."
  8421. (interactive)
  8422. (if (not (org-at-table-p))
  8423. (error "Not at a table"))
  8424. (let ((col (current-column))
  8425. (dline (org-table-current-dline)))
  8426. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8427. (if (not (org-at-table-p)) (beginning-of-line 0))
  8428. (move-to-column col)
  8429. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8430. dline -1 dline)))
  8431. (defun org-table-sort-lines (with-case &optional sorting-type)
  8432. "Sort table lines according to the column at point.
  8433. The position of point indicates the column to be used for
  8434. sorting, and the range of lines is the range between the nearest
  8435. horizontal separator lines, or the entire table of no such lines
  8436. exist. If point is before the first column, you will be prompted
  8437. for the sorting column. If there is an active region, the mark
  8438. specifies the first line and the sorting column, while point
  8439. should be in the last line to be included into the sorting.
  8440. The command then prompts for the sorting type which can be
  8441. alphabetically, numerically, or by time (as given in a time stamp
  8442. in the field). Sorting in reverse order is also possible.
  8443. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8444. If SORTING-TYPE is specified when this function is called from a Lisp
  8445. program, no prompting will take place. SORTING-TYPE must be a character,
  8446. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8447. should be done in reverse order."
  8448. (interactive "P")
  8449. (let* ((thisline (org-current-line))
  8450. (thiscol (org-table-current-column))
  8451. beg end bcol ecol tend tbeg column lns pos)
  8452. (when (equal thiscol 0)
  8453. (if (interactive-p)
  8454. (setq thiscol
  8455. (string-to-number
  8456. (read-string "Use column N for sorting: ")))
  8457. (setq thiscol 1))
  8458. (org-table-goto-column thiscol))
  8459. (org-table-check-inside-data-field)
  8460. (if (org-region-active-p)
  8461. (progn
  8462. (setq beg (region-beginning) end (region-end))
  8463. (goto-char beg)
  8464. (setq column (org-table-current-column)
  8465. beg (point-at-bol))
  8466. (goto-char end)
  8467. (setq end (point-at-bol 2)))
  8468. (setq column (org-table-current-column)
  8469. pos (point)
  8470. tbeg (org-table-begin)
  8471. tend (org-table-end))
  8472. (if (re-search-backward org-table-hline-regexp tbeg t)
  8473. (setq beg (point-at-bol 2))
  8474. (goto-char tbeg)
  8475. (setq beg (point-at-bol 1)))
  8476. (goto-char pos)
  8477. (if (re-search-forward org-table-hline-regexp tend t)
  8478. (setq end (point-at-bol 1))
  8479. (goto-char tend)
  8480. (setq end (point-at-bol))))
  8481. (setq beg (move-marker (make-marker) beg)
  8482. end (move-marker (make-marker) end))
  8483. (untabify beg end)
  8484. (goto-char beg)
  8485. (org-table-goto-column column)
  8486. (skip-chars-backward "^|")
  8487. (setq bcol (current-column))
  8488. (org-table-goto-column (1+ column))
  8489. (skip-chars-backward "^|")
  8490. (setq ecol (1- (current-column)))
  8491. (org-table-goto-column column)
  8492. (setq lns (mapcar (lambda(x) (cons
  8493. (org-sort-remove-invisible
  8494. (nth (1- column)
  8495. (org-split-string x "[ \t]*|[ \t]*")))
  8496. x))
  8497. (org-split-string (buffer-substring beg end) "\n")))
  8498. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8499. (delete-region beg end)
  8500. (move-marker beg nil)
  8501. (move-marker end nil)
  8502. (insert (mapconcat 'cdr lns "\n") "\n")
  8503. (goto-line thisline)
  8504. (org-table-goto-column thiscol)
  8505. (message "%d lines sorted, based on column %d" (length lns) column)))
  8506. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8507. (defun org-sort-remove-invisible (s)
  8508. (remove-text-properties 0 (length s) org-rm-props s)
  8509. (while (string-match org-bracket-link-regexp s)
  8510. (setq s (replace-match (if (match-end 2)
  8511. (match-string 3 s)
  8512. (match-string 1 s)) t t s)))
  8513. s)
  8514. (defun org-table-cut-region (beg end)
  8515. "Copy region in table to the clipboard and blank all relevant fields."
  8516. (interactive "r")
  8517. (org-table-copy-region beg end 'cut))
  8518. (defun org-table-copy-region (beg end &optional cut)
  8519. "Copy rectangular region in table to clipboard.
  8520. A special clipboard is used which can only be accessed
  8521. with `org-table-paste-rectangle'."
  8522. (interactive "rP")
  8523. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8524. region cols
  8525. (rpl (if cut " " nil)))
  8526. (goto-char beg)
  8527. (org-table-check-inside-data-field)
  8528. (setq l01 (org-current-line)
  8529. c01 (org-table-current-column))
  8530. (goto-char end)
  8531. (org-table-check-inside-data-field)
  8532. (setq l02 (org-current-line)
  8533. c02 (org-table-current-column))
  8534. (setq l1 (min l01 l02) l2 (max l01 l02)
  8535. c1 (min c01 c02) c2 (max c01 c02))
  8536. (catch 'exit
  8537. (while t
  8538. (catch 'nextline
  8539. (if (> l1 l2) (throw 'exit t))
  8540. (goto-line l1)
  8541. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8542. (setq cols nil ic1 c1 ic2 c2)
  8543. (while (< ic1 (1+ ic2))
  8544. (push (org-table-get-field ic1 rpl) cols)
  8545. (setq ic1 (1+ ic1)))
  8546. (push (nreverse cols) region)
  8547. (setq l1 (1+ l1)))))
  8548. (setq org-table-clip (nreverse region))
  8549. (if cut (org-table-align))
  8550. org-table-clip))
  8551. (defun org-table-paste-rectangle ()
  8552. "Paste a rectangular region into a table.
  8553. The upper right corner ends up in the current field. All involved fields
  8554. will be overwritten. If the rectangle does not fit into the present table,
  8555. the table is enlarged as needed. The process ignores horizontal separator
  8556. lines."
  8557. (interactive)
  8558. (unless (and org-table-clip (listp org-table-clip))
  8559. (error "First cut/copy a region to paste!"))
  8560. (org-table-check-inside-data-field)
  8561. (let* ((clip org-table-clip)
  8562. (line (org-current-line))
  8563. (col (org-table-current-column))
  8564. (org-enable-table-editor t)
  8565. (org-table-automatic-realign nil)
  8566. c cols field)
  8567. (while (setq cols (pop clip))
  8568. (while (org-at-table-hline-p) (beginning-of-line 2))
  8569. (if (not (org-at-table-p))
  8570. (progn (end-of-line 0) (org-table-next-field)))
  8571. (setq c col)
  8572. (while (setq field (pop cols))
  8573. (org-table-goto-column c nil 'force)
  8574. (org-table-get-field nil field)
  8575. (setq c (1+ c)))
  8576. (beginning-of-line 2))
  8577. (goto-line line)
  8578. (org-table-goto-column col)
  8579. (org-table-align)))
  8580. (defun org-table-convert ()
  8581. "Convert from `org-mode' table to table.el and back.
  8582. Obviously, this only works within limits. When an Org-mode table is
  8583. converted to table.el, all horizontal separator lines get lost, because
  8584. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8585. A table.el table can be converted to an Org-mode table only if it does not
  8586. do row or column spanning. Multiline cells will become multiple cells.
  8587. Beware, Org-mode does not test if the table can be successfully converted - it
  8588. blindly applies a recipe that works for simple tables."
  8589. (interactive)
  8590. (require 'table)
  8591. (if (org-at-table.el-p)
  8592. ;; convert to Org-mode table
  8593. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8594. (end (move-marker (make-marker) (org-table-end t))))
  8595. (table-unrecognize-region beg end)
  8596. (goto-char beg)
  8597. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8598. (replace-match ""))
  8599. (goto-char beg))
  8600. (if (org-at-table-p)
  8601. ;; convert to table.el table
  8602. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8603. (end (move-marker (make-marker) (org-table-end))))
  8604. ;; first, get rid of all horizontal lines
  8605. (goto-char beg)
  8606. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8607. (replace-match ""))
  8608. ;; insert a hline before first
  8609. (goto-char beg)
  8610. (org-table-insert-hline 'above)
  8611. (beginning-of-line -1)
  8612. ;; insert a hline after each line
  8613. (while (progn (beginning-of-line 3) (< (point) end))
  8614. (org-table-insert-hline))
  8615. (goto-char beg)
  8616. (setq end (move-marker end (org-table-end)))
  8617. ;; replace "+" at beginning and ending of hlines
  8618. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8619. (replace-match "\\1+-"))
  8620. (goto-char beg)
  8621. (while (re-search-forward "-|[ \t]*$" end t)
  8622. (replace-match "-+"))
  8623. (goto-char beg)))))
  8624. (defun org-table-wrap-region (arg)
  8625. "Wrap several fields in a column like a paragraph.
  8626. This is useful if you'd like to spread the contents of a field over several
  8627. lines, in order to keep the table compact.
  8628. If there is an active region, and both point and mark are in the same column,
  8629. the text in the column is wrapped to minimum width for the given number of
  8630. lines. Generally, this makes the table more compact. A prefix ARG may be
  8631. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8632. formats the selected text to two lines. If the region was longer than two
  8633. lines, the remaining lines remain empty. A negative prefix argument reduces
  8634. the current number of lines by that amount. The wrapped text is pasted back
  8635. into the table. If you formatted it to more lines than it was before, fields
  8636. further down in the table get overwritten - so you might need to make space in
  8637. the table first.
  8638. If there is no region, the current field is split at the cursor position and
  8639. the text fragment to the right of the cursor is prepended to the field one
  8640. line down.
  8641. If there is no region, but you specify a prefix ARG, the current field gets
  8642. blank, and the content is appended to the field above."
  8643. (interactive "P")
  8644. (org-table-check-inside-data-field)
  8645. (if (org-region-active-p)
  8646. ;; There is a region: fill as a paragraph
  8647. (let* ((beg (region-beginning))
  8648. (cline (save-excursion (goto-char beg) (org-current-line)))
  8649. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8650. nlines)
  8651. (org-table-cut-region (region-beginning) (region-end))
  8652. (if (> (length (car org-table-clip)) 1)
  8653. (error "Region must be limited to single column"))
  8654. (setq nlines (if arg
  8655. (if (< arg 1)
  8656. (+ (length org-table-clip) arg)
  8657. arg)
  8658. (length org-table-clip)))
  8659. (setq org-table-clip
  8660. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8661. nil nlines)))
  8662. (goto-line cline)
  8663. (org-table-goto-column ccol)
  8664. (org-table-paste-rectangle))
  8665. ;; No region, split the current field at point
  8666. (unless (org-get-alist-option org-M-RET-may-split-line 'table)
  8667. (skip-chars-forward "^\r\n|"))
  8668. (if arg
  8669. ;; combine with field above
  8670. (let ((s (org-table-blank-field))
  8671. (col (org-table-current-column)))
  8672. (beginning-of-line 0)
  8673. (while (org-at-table-hline-p) (beginning-of-line 0))
  8674. (org-table-goto-column col)
  8675. (skip-chars-forward "^|")
  8676. (skip-chars-backward " ")
  8677. (insert " " (org-trim s))
  8678. (org-table-align))
  8679. ;; split field
  8680. (if (looking-at "\\([^|]+\\)+|")
  8681. (let ((s (match-string 1)))
  8682. (replace-match " |")
  8683. (goto-char (match-beginning 0))
  8684. (org-table-next-row)
  8685. (insert (org-trim s) " ")
  8686. (org-table-align))
  8687. (org-table-next-row)))))
  8688. (defvar org-field-marker nil)
  8689. (defun org-table-edit-field (arg)
  8690. "Edit table field in a different window.
  8691. This is mainly useful for fields that contain hidden parts.
  8692. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8693. it can be edited in place."
  8694. (interactive "P")
  8695. (if arg
  8696. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8697. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8698. (remove-text-properties b e '(org-cwidth t invisible t
  8699. display t intangible t))
  8700. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8701. (font-lock-fontify-block)))
  8702. (let ((pos (move-marker (make-marker) (point)))
  8703. (field (org-table-get-field))
  8704. (cw (current-window-configuration))
  8705. p)
  8706. (org-switch-to-buffer-other-window "*Org tmp*")
  8707. (erase-buffer)
  8708. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8709. (let ((org-inhibit-startup t)) (org-mode))
  8710. (goto-char (setq p (point-max)))
  8711. (insert (org-trim field))
  8712. (remove-text-properties p (point-max)
  8713. '(invisible t org-cwidth t display t
  8714. intangible t))
  8715. (goto-char p)
  8716. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8717. (org-set-local 'org-window-configuration cw)
  8718. (org-set-local 'org-field-marker pos)
  8719. (message "Edit and finish with C-c C-c"))))
  8720. (defun org-table-finish-edit-field ()
  8721. "Finish editing a table data field.
  8722. Remove all newline characters, insert the result into the table, realign
  8723. the table and kill the editing buffer."
  8724. (let ((pos org-field-marker)
  8725. (cw org-window-configuration)
  8726. (cb (current-buffer))
  8727. text)
  8728. (goto-char (point-min))
  8729. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8730. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8731. (replace-match " "))
  8732. (setq text (org-trim (buffer-string)))
  8733. (set-window-configuration cw)
  8734. (kill-buffer cb)
  8735. (select-window (get-buffer-window (marker-buffer pos)))
  8736. (goto-char pos)
  8737. (move-marker pos nil)
  8738. (org-table-check-inside-data-field)
  8739. (org-table-get-field nil text)
  8740. (org-table-align)
  8741. (message "New field value inserted")))
  8742. (defun org-trim (s)
  8743. "Remove whitespace at beginning and end of string."
  8744. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8745. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8746. s)
  8747. (defun org-wrap (string &optional width lines)
  8748. "Wrap string to either a number of lines, or a width in characters.
  8749. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8750. that costs. If there is a word longer than WIDTH, the text is actually
  8751. wrapped to the length of that word.
  8752. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8753. many lines, whatever width that takes.
  8754. The return value is a list of lines, without newlines at the end."
  8755. (let* ((words (org-split-string string "[ \t\n]+"))
  8756. (maxword (apply 'max (mapcar 'org-string-width words)))
  8757. w ll)
  8758. (cond (width
  8759. (org-do-wrap words (max maxword width)))
  8760. (lines
  8761. (setq w maxword)
  8762. (setq ll (org-do-wrap words maxword))
  8763. (if (<= (length ll) lines)
  8764. ll
  8765. (setq ll words)
  8766. (while (> (length ll) lines)
  8767. (setq w (1+ w))
  8768. (setq ll (org-do-wrap words w)))
  8769. ll))
  8770. (t (error "Cannot wrap this")))))
  8771. (defun org-do-wrap (words width)
  8772. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8773. (let (lines line)
  8774. (while words
  8775. (setq line (pop words))
  8776. (while (and words (< (+ (length line) (length (car words))) width))
  8777. (setq line (concat line " " (pop words))))
  8778. (setq lines (push line lines)))
  8779. (nreverse lines)))
  8780. (defun org-split-string (string &optional separators)
  8781. "Splits STRING into substrings at SEPARATORS.
  8782. No empty strings are returned if there are matches at the beginning
  8783. and end of string."
  8784. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8785. (start 0)
  8786. notfirst
  8787. (list nil))
  8788. (while (and (string-match rexp string
  8789. (if (and notfirst
  8790. (= start (match-beginning 0))
  8791. (< start (length string)))
  8792. (1+ start) start))
  8793. (< (match-beginning 0) (length string)))
  8794. (setq notfirst t)
  8795. (or (eq (match-beginning 0) 0)
  8796. (and (eq (match-beginning 0) (match-end 0))
  8797. (eq (match-beginning 0) start))
  8798. (setq list
  8799. (cons (substring string start (match-beginning 0))
  8800. list)))
  8801. (setq start (match-end 0)))
  8802. (or (eq start (length string))
  8803. (setq list
  8804. (cons (substring string start)
  8805. list)))
  8806. (nreverse list)))
  8807. (defun org-table-map-tables (function)
  8808. "Apply FUNCTION to the start of all tables in the buffer."
  8809. (save-excursion
  8810. (save-restriction
  8811. (widen)
  8812. (goto-char (point-min))
  8813. (while (re-search-forward org-table-any-line-regexp nil t)
  8814. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8815. (beginning-of-line 1)
  8816. (if (looking-at org-table-line-regexp)
  8817. (save-excursion (funcall function)))
  8818. (re-search-forward org-table-any-border-regexp nil 1))))
  8819. (message "Mapping tables: done"))
  8820. (defvar org-timecnt) ; dynamically scoped parameter
  8821. (defun org-table-sum (&optional beg end nlast)
  8822. "Sum numbers in region of current table column.
  8823. The result will be displayed in the echo area, and will be available
  8824. as kill to be inserted with \\[yank].
  8825. If there is an active region, it is interpreted as a rectangle and all
  8826. numbers in that rectangle will be summed. If there is no active
  8827. region and point is located in a table column, sum all numbers in that
  8828. column.
  8829. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8830. numbers are assumed to be times as well (in decimal hours) and the
  8831. numbers are added as such.
  8832. If NLAST is a number, only the NLAST fields will actually be summed."
  8833. (interactive)
  8834. (save-excursion
  8835. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8836. (cond
  8837. ((and beg end)) ; beg and end given explicitly
  8838. ((org-region-active-p)
  8839. (setq beg (region-beginning) end (region-end)))
  8840. (t
  8841. (setq col (org-table-current-column))
  8842. (goto-char (org-table-begin))
  8843. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8844. (error "No table data"))
  8845. (org-table-goto-column col)
  8846. (setq beg (point))
  8847. (goto-char (org-table-end))
  8848. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8849. (error "No table data"))
  8850. (org-table-goto-column col)
  8851. (setq end (point))))
  8852. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8853. (items1 (cond ((not nlast) items)
  8854. ((>= nlast (length items)) items)
  8855. (t (setq items (reverse items))
  8856. (setcdr (nthcdr (1- nlast) items) nil)
  8857. (nreverse items))))
  8858. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8859. items1)))
  8860. (res (apply '+ numbers))
  8861. (sres (if (= org-timecnt 0)
  8862. (format "%g" res)
  8863. (setq diff (* 3600 res)
  8864. h (floor (/ diff 3600)) diff (mod diff 3600)
  8865. m (floor (/ diff 60)) diff (mod diff 60)
  8866. s diff)
  8867. (format "%d:%02d:%02d" h m s))))
  8868. (kill-new sres)
  8869. (if (interactive-p)
  8870. (message "%s"
  8871. (substitute-command-keys
  8872. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8873. (length numbers) sres))))
  8874. sres))))
  8875. (defun org-table-get-number-for-summing (s)
  8876. (let (n)
  8877. (if (string-match "^ *|? *" s)
  8878. (setq s (replace-match "" nil nil s)))
  8879. (if (string-match " *|? *$" s)
  8880. (setq s (replace-match "" nil nil s)))
  8881. (setq n (string-to-number s))
  8882. (cond
  8883. ((and (string-match "0" s)
  8884. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8885. ((string-match "\\`[ \t]+\\'" s) nil)
  8886. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8887. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8888. (m (string-to-number (or (match-string 2 s) "0")))
  8889. (s (string-to-number (or (match-string 4 s) "0"))))
  8890. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8891. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8892. ((equal n 0) nil)
  8893. (t n))))
  8894. (defun org-table-current-field-formula (&optional key noerror)
  8895. "Return the formula active for the current field.
  8896. Assumes that specials are in place.
  8897. If KEY is given, return the key to this formula.
  8898. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8899. (let* ((name (car (rassoc (list (org-current-line)
  8900. (org-table-current-column))
  8901. org-table-named-field-locations)))
  8902. (col (org-table-current-column))
  8903. (scol (int-to-string col))
  8904. (ref (format "@%d$%d" (org-table-current-dline) col))
  8905. (stored-list (org-table-get-stored-formulas noerror))
  8906. (ass (or (assoc name stored-list)
  8907. (assoc ref stored-list)
  8908. (assoc scol stored-list))))
  8909. (if key
  8910. (car ass)
  8911. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8912. (cdr ass))))))
  8913. (defun org-table-get-formula (&optional equation named)
  8914. "Read a formula from the minibuffer, offer stored formula as default.
  8915. When NAMED is non-nil, look for a named equation."
  8916. (let* ((stored-list (org-table-get-stored-formulas))
  8917. (name (car (rassoc (list (org-current-line)
  8918. (org-table-current-column))
  8919. org-table-named-field-locations)))
  8920. (ref (format "@%d$%d" (org-table-current-dline)
  8921. (org-table-current-column)))
  8922. (refass (assoc ref stored-list))
  8923. (scol (if named
  8924. (if name name ref)
  8925. (int-to-string (org-table-current-column))))
  8926. (dummy (and (or name refass) (not named)
  8927. (not (y-or-n-p "Replace field formula with column formula? " ))
  8928. (error "Abort")))
  8929. (name (or name ref))
  8930. (org-table-may-need-update nil)
  8931. (stored (cdr (assoc scol stored-list)))
  8932. (eq (cond
  8933. ((and stored equation (string-match "^ *=? *$" equation))
  8934. stored)
  8935. ((stringp equation)
  8936. equation)
  8937. (t (org-table-formula-from-user
  8938. (read-string
  8939. (org-table-formula-to-user
  8940. (format "%s formula %s%s="
  8941. (if named "Field" "Column")
  8942. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8943. scol))
  8944. (if stored (org-table-formula-to-user stored) "")
  8945. 'org-table-formula-history
  8946. )))))
  8947. mustsave)
  8948. (when (not (string-match "\\S-" eq))
  8949. ;; remove formula
  8950. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8951. (org-table-store-formulas stored-list)
  8952. (error "Formula removed"))
  8953. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8954. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8955. (if (and name (not named))
  8956. ;; We set the column equation, delete the named one.
  8957. (setq stored-list (delq (assoc name stored-list) stored-list)
  8958. mustsave t))
  8959. (if stored
  8960. (setcdr (assoc scol stored-list) eq)
  8961. (setq stored-list (cons (cons scol eq) stored-list)))
  8962. (if (or mustsave (not (equal stored eq)))
  8963. (org-table-store-formulas stored-list))
  8964. eq))
  8965. (defun org-table-store-formulas (alist)
  8966. "Store the list of formulas below the current table."
  8967. (setq alist (sort alist 'org-table-formula-less-p))
  8968. (save-excursion
  8969. (goto-char (org-table-end))
  8970. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8971. (progn
  8972. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8973. (goto-char (match-beginning 2))
  8974. (delete-region (match-beginning 2) (match-end 0)))
  8975. (insert "#+TBLFM:"))
  8976. (insert " "
  8977. (mapconcat (lambda (x)
  8978. (concat
  8979. (if (equal (string-to-char (car x)) ?@) "" "$")
  8980. (car x) "=" (cdr x)))
  8981. alist "::")
  8982. "\n")))
  8983. (defsubst org-table-formula-make-cmp-string (a)
  8984. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8985. (concat
  8986. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8987. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8988. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8989. (defun org-table-formula-less-p (a b)
  8990. "Compare two formulas for sorting."
  8991. (let ((as (org-table-formula-make-cmp-string (car a)))
  8992. (bs (org-table-formula-make-cmp-string (car b))))
  8993. (and as bs (string< as bs))))
  8994. (defun org-table-get-stored-formulas (&optional noerror)
  8995. "Return an alist with the stored formulas directly after current table."
  8996. (interactive)
  8997. (let (scol eq eq-alist strings string seen)
  8998. (save-excursion
  8999. (goto-char (org-table-end))
  9000. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  9001. (setq strings (org-split-string (match-string 2) " *:: *"))
  9002. (while (setq string (pop strings))
  9003. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  9004. (setq scol (if (match-end 2)
  9005. (match-string 2 string)
  9006. (match-string 1 string))
  9007. eq (match-string 3 string)
  9008. eq-alist (cons (cons scol eq) eq-alist))
  9009. (if (member scol seen)
  9010. (if noerror
  9011. (progn
  9012. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  9013. (ding)
  9014. (sit-for 2))
  9015. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  9016. (push scol seen))))))
  9017. (nreverse eq-alist)))
  9018. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  9019. "Modify the equations after the table structure has been edited.
  9020. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  9021. For all numbers larger than LIMIT, shift them by DELTA."
  9022. (save-excursion
  9023. (goto-char (org-table-end))
  9024. (when (looking-at "#\\+TBLFM:")
  9025. (let ((re (concat key "\\([0-9]+\\)"))
  9026. (re2
  9027. (when remove
  9028. (if (equal key "$")
  9029. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  9030. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  9031. s n a)
  9032. (when remove
  9033. (while (re-search-forward re2 (point-at-eol) t)
  9034. (replace-match "")))
  9035. (while (re-search-forward re (point-at-eol) t)
  9036. (setq s (match-string 1) n (string-to-number s))
  9037. (cond
  9038. ((setq a (assoc s replace))
  9039. (replace-match (concat key (cdr a)) t t))
  9040. ((and limit (> n limit))
  9041. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  9042. (defun org-table-get-specials ()
  9043. "Get the column names and local parameters for this table."
  9044. (save-excursion
  9045. (let ((beg (org-table-begin)) (end (org-table-end))
  9046. names name fields fields1 field cnt
  9047. c v l line col types dlines hlines)
  9048. (setq org-table-column-names nil
  9049. org-table-local-parameters nil
  9050. org-table-named-field-locations nil
  9051. org-table-current-begin-line nil
  9052. org-table-current-begin-pos nil
  9053. org-table-current-line-types nil)
  9054. (goto-char beg)
  9055. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  9056. (setq names (org-split-string (match-string 1) " *| *")
  9057. cnt 1)
  9058. (while (setq name (pop names))
  9059. (setq cnt (1+ cnt))
  9060. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  9061. (push (cons name (int-to-string cnt)) org-table-column-names))))
  9062. (setq org-table-column-names (nreverse org-table-column-names))
  9063. (setq org-table-column-name-regexp
  9064. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  9065. (goto-char beg)
  9066. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  9067. (setq fields (org-split-string (match-string 1) " *| *"))
  9068. (while (setq field (pop fields))
  9069. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  9070. (push (cons (match-string 1 field) (match-string 2 field))
  9071. org-table-local-parameters))))
  9072. (goto-char beg)
  9073. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  9074. (setq c (match-string 1)
  9075. fields (org-split-string (match-string 2) " *| *"))
  9076. (save-excursion
  9077. (beginning-of-line (if (equal c "_") 2 0))
  9078. (setq line (org-current-line) col 1)
  9079. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  9080. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  9081. (while (and fields1 (setq field (pop fields)))
  9082. (setq v (pop fields1) col (1+ col))
  9083. (when (and (stringp field) (stringp v)
  9084. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  9085. (push (cons field v) org-table-local-parameters)
  9086. (push (list field line col) org-table-named-field-locations))))
  9087. ;; Analyse the line types
  9088. (goto-char beg)
  9089. (setq org-table-current-begin-line (org-current-line)
  9090. org-table-current-begin-pos (point)
  9091. l org-table-current-begin-line)
  9092. (while (looking-at "[ \t]*|\\(-\\)?")
  9093. (push (if (match-end 1) 'hline 'dline) types)
  9094. (if (match-end 1) (push l hlines) (push l dlines))
  9095. (beginning-of-line 2)
  9096. (setq l (1+ l)))
  9097. (setq org-table-current-line-types (apply 'vector (nreverse types))
  9098. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  9099. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  9100. (defun org-table-maybe-eval-formula ()
  9101. "Check if the current field starts with \"=\" or \":=\".
  9102. If yes, store the formula and apply it."
  9103. ;; We already know we are in a table. Get field will only return a formula
  9104. ;; when appropriate. It might return a separator line, but no problem.
  9105. (when org-table-formula-evaluate-inline
  9106. (let* ((field (org-trim (or (org-table-get-field) "")))
  9107. named eq)
  9108. (when (string-match "^:?=\\(.*\\)" field)
  9109. (setq named (equal (string-to-char field) ?:)
  9110. eq (match-string 1 field))
  9111. (if (or (fboundp 'calc-eval)
  9112. (equal (substring eq 0 (min 2 (length eq))) "'("))
  9113. (org-table-eval-formula (if named '(4) nil)
  9114. (org-table-formula-from-user eq))
  9115. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  9116. (defvar org-recalc-commands nil
  9117. "List of commands triggering the recalculation of a line.
  9118. Will be filled automatically during use.")
  9119. (defvar org-recalc-marks
  9120. '((" " . "Unmarked: no special line, no automatic recalculation")
  9121. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9122. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9123. ("!" . "Column name definition line. Reference in formula as $name.")
  9124. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9125. ("_" . "Names for values in row below this one.")
  9126. ("^" . "Names for values in row above this one.")))
  9127. (defun org-table-rotate-recalc-marks (&optional newchar)
  9128. "Rotate the recalculation mark in the first column.
  9129. If in any row, the first field is not consistent with a mark,
  9130. insert a new column for the markers.
  9131. When there is an active region, change all the lines in the region,
  9132. after prompting for the marking character.
  9133. After each change, a message will be displayed indicating the meaning
  9134. of the new mark."
  9135. (interactive)
  9136. (unless (org-at-table-p) (error "Not at a table"))
  9137. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9138. (beg (org-table-begin))
  9139. (end (org-table-end))
  9140. (l (org-current-line))
  9141. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9142. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9143. (have-col
  9144. (save-excursion
  9145. (goto-char beg)
  9146. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9147. (col (org-table-current-column))
  9148. (forcenew (car (assoc newchar org-recalc-marks)))
  9149. epos new)
  9150. (when l1
  9151. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9152. (setq newchar (char-to-string (read-char-exclusive))
  9153. forcenew (car (assoc newchar org-recalc-marks))))
  9154. (if (and newchar (not forcenew))
  9155. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9156. newchar))
  9157. (if l1 (goto-line l1))
  9158. (save-excursion
  9159. (beginning-of-line 1)
  9160. (unless (looking-at org-table-dataline-regexp)
  9161. (error "Not at a table data line")))
  9162. (unless have-col
  9163. (org-table-goto-column 1)
  9164. (org-table-insert-column)
  9165. (org-table-goto-column (1+ col)))
  9166. (setq epos (point-at-eol))
  9167. (save-excursion
  9168. (beginning-of-line 1)
  9169. (org-table-get-field
  9170. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9171. (concat " "
  9172. (setq new (or forcenew
  9173. (cadr (member (match-string 1) marks))))
  9174. " ")
  9175. " # ")))
  9176. (if (and l1 l2)
  9177. (progn
  9178. (goto-line l1)
  9179. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9180. (and (looking-at org-table-dataline-regexp)
  9181. (org-table-get-field 1 (concat " " new " "))))
  9182. (goto-line l1)))
  9183. (if (not (= epos (point-at-eol))) (org-table-align))
  9184. (goto-line l)
  9185. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  9186. (defun org-table-maybe-recalculate-line ()
  9187. "Recompute the current line if marked for it, and if we haven't just done it."
  9188. (interactive)
  9189. (and org-table-allow-automatic-line-recalculation
  9190. (not (and (memq last-command org-recalc-commands)
  9191. (equal org-last-recalc-line (org-current-line))))
  9192. (save-excursion (beginning-of-line 1)
  9193. (looking-at org-table-auto-recalculate-regexp))
  9194. (org-table-recalculate) t))
  9195. (defvar org-table-formula-debug nil
  9196. "Non-nil means, debug table formulas.
  9197. When nil, simply write \"#ERROR\" in corrupted fields.")
  9198. (make-variable-buffer-local 'org-table-formula-debug)
  9199. (defvar modes)
  9200. (defsubst org-set-calc-mode (var &optional value)
  9201. (if (stringp var)
  9202. (setq var (assoc var '(("D" calc-angle-mode deg)
  9203. ("R" calc-angle-mode rad)
  9204. ("F" calc-prefer-frac t)
  9205. ("S" calc-symbolic-mode t)))
  9206. value (nth 2 var) var (nth 1 var)))
  9207. (if (memq var modes)
  9208. (setcar (cdr (memq var modes)) value)
  9209. (cons var (cons value modes)))
  9210. modes)
  9211. (defun org-table-eval-formula (&optional arg equation
  9212. suppress-align suppress-const
  9213. suppress-store suppress-analysis)
  9214. "Replace the table field value at the cursor by the result of a calculation.
  9215. This function makes use of Dave Gillespie's Calc package, in my view the
  9216. most exciting program ever written for GNU Emacs. So you need to have Calc
  9217. installed in order to use this function.
  9218. In a table, this command replaces the value in the current field with the
  9219. result of a formula. It also installs the formula as the \"current\" column
  9220. formula, by storing it in a special line below the table. When called
  9221. with a `C-u' prefix, the current field must ba a named field, and the
  9222. formula is installed as valid in only this specific field.
  9223. When called with two `C-u' prefixes, insert the active equation
  9224. for the field back into the current field, so that it can be
  9225. edited there. This is useful in order to use \\[org-table-show-reference]
  9226. to check the referenced fields.
  9227. When called, the command first prompts for a formula, which is read in
  9228. the minibuffer. Previously entered formulas are available through the
  9229. history list, and the last used formula is offered as a default.
  9230. These stored formulas are adapted correctly when moving, inserting, or
  9231. deleting columns with the corresponding commands.
  9232. The formula can be any algebraic expression understood by the Calc package.
  9233. For details, see the Org-mode manual.
  9234. This function can also be called from Lisp programs and offers
  9235. additional arguments: EQUATION can be the formula to apply. If this
  9236. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9237. used to speed-up recursive calls by by-passing unnecessary aligns.
  9238. SUPPRESS-CONST suppresses the interpretation of constants in the
  9239. formula, assuming that this has been done already outside the function.
  9240. SUPPRESS-STORE means the formula should not be stored, either because
  9241. it is already stored, or because it is a modified equation that should
  9242. not overwrite the stored one."
  9243. (interactive "P")
  9244. (org-table-check-inside-data-field)
  9245. (or suppress-analysis (org-table-get-specials))
  9246. (if (equal arg '(16))
  9247. (let ((eq (org-table-current-field-formula)))
  9248. (or eq (error "No equation active for current field"))
  9249. (org-table-get-field nil eq)
  9250. (org-table-align)
  9251. (setq org-table-may-need-update t))
  9252. (let* (fields
  9253. (ndown (if (integerp arg) arg 1))
  9254. (org-table-automatic-realign nil)
  9255. (case-fold-search nil)
  9256. (down (> ndown 1))
  9257. (formula (if (and equation suppress-store)
  9258. equation
  9259. (org-table-get-formula equation (equal arg '(4)))))
  9260. (n0 (org-table-current-column))
  9261. (modes (copy-sequence org-calc-default-modes))
  9262. (numbers nil) ; was a variable, now fixed default
  9263. (keep-empty nil)
  9264. n form form0 bw fmt x ev orig c lispp literal)
  9265. ;; Parse the format string. Since we have a lot of modes, this is
  9266. ;; a lot of work. However, I think calc still uses most of the time.
  9267. (if (string-match ";" formula)
  9268. (let ((tmp (org-split-string formula ";")))
  9269. (setq formula (car tmp)
  9270. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9271. (nth 1 tmp)))
  9272. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9273. (setq c (string-to-char (match-string 1 fmt))
  9274. n (string-to-number (match-string 2 fmt)))
  9275. (if (= c ?p)
  9276. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9277. (setq modes (org-set-calc-mode
  9278. 'calc-float-format
  9279. (list (cdr (assoc c '((?n . float) (?f . fix)
  9280. (?s . sci) (?e . eng))))
  9281. n))))
  9282. (setq fmt (replace-match "" t t fmt)))
  9283. (if (string-match "[NT]" fmt)
  9284. (setq numbers (equal (match-string 0 fmt) "N")
  9285. fmt (replace-match "" t t fmt)))
  9286. (if (string-match "L" fmt)
  9287. (setq literal t
  9288. fmt (replace-match "" t t fmt)))
  9289. (if (string-match "E" fmt)
  9290. (setq keep-empty t
  9291. fmt (replace-match "" t t fmt)))
  9292. (while (string-match "[DRFS]" fmt)
  9293. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9294. (setq fmt (replace-match "" t t fmt)))
  9295. (unless (string-match "\\S-" fmt)
  9296. (setq fmt nil))))
  9297. (if (and (not suppress-const) org-table-formula-use-constants)
  9298. (setq formula (org-table-formula-substitute-names formula)))
  9299. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9300. (while (> ndown 0)
  9301. (setq fields (org-split-string
  9302. (org-no-properties
  9303. (buffer-substring (point-at-bol) (point-at-eol)))
  9304. " *| *"))
  9305. (if (eq numbers t)
  9306. (setq fields (mapcar
  9307. (lambda (x) (number-to-string (string-to-number x)))
  9308. fields)))
  9309. (setq ndown (1- ndown))
  9310. (setq form (copy-sequence formula)
  9311. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9312. (if (and lispp literal) (setq lispp 'literal))
  9313. ;; Check for old vertical references
  9314. (setq form (org-rewrite-old-row-references form))
  9315. ;; Insert complex ranges
  9316. (while (string-match org-table-range-regexp form)
  9317. (setq form
  9318. (replace-match
  9319. (save-match-data
  9320. (org-table-make-reference
  9321. (org-table-get-range (match-string 0 form) nil n0)
  9322. keep-empty numbers lispp))
  9323. t t form)))
  9324. ;; Insert simple ranges
  9325. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9326. (setq form
  9327. (replace-match
  9328. (save-match-data
  9329. (org-table-make-reference
  9330. (org-sublist
  9331. fields (string-to-number (match-string 1 form))
  9332. (string-to-number (match-string 2 form)))
  9333. keep-empty numbers lispp))
  9334. t t form)))
  9335. (setq form0 form)
  9336. ;; Insert the references to fields in same row
  9337. (while (string-match "\\$\\([0-9]+\\)" form)
  9338. (setq n (string-to-number (match-string 1 form))
  9339. x (nth (1- (if (= n 0) n0 n)) fields))
  9340. (unless x (error "Invalid field specifier \"%s\""
  9341. (match-string 0 form)))
  9342. (setq form (replace-match
  9343. (save-match-data
  9344. (org-table-make-reference x nil numbers lispp))
  9345. t t form)))
  9346. (if lispp
  9347. (setq ev (condition-case nil
  9348. (eval (eval (read form)))
  9349. (error "#ERROR"))
  9350. ev (if (numberp ev) (number-to-string ev) ev))
  9351. (or (fboundp 'calc-eval)
  9352. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9353. (setq ev (calc-eval (cons form modes)
  9354. (if numbers 'num))))
  9355. (when org-table-formula-debug
  9356. (with-output-to-temp-buffer "*Substitution History*"
  9357. (princ (format "Substitution history of formula
  9358. Orig: %s
  9359. $xyz-> %s
  9360. @r$c-> %s
  9361. $1-> %s\n" orig formula form0 form))
  9362. (if (listp ev)
  9363. (princ (format " %s^\nError: %s"
  9364. (make-string (car ev) ?\-) (nth 1 ev)))
  9365. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9366. ev (or fmt "NONE")
  9367. (if fmt (format fmt (string-to-number ev)) ev)))))
  9368. (setq bw (get-buffer-window "*Substitution History*"))
  9369. (shrink-window-if-larger-than-buffer bw)
  9370. (unless (and (interactive-p) (not ndown))
  9371. (unless (let (inhibit-redisplay)
  9372. (y-or-n-p "Debugging Formula. Continue to next? "))
  9373. (org-table-align)
  9374. (error "Abort"))
  9375. (delete-window bw)
  9376. (message "")))
  9377. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9378. (org-table-justify-field-maybe
  9379. (if fmt (format fmt (string-to-number ev)) ev))
  9380. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9381. (call-interactively 'org-return)
  9382. (setq ndown 0)))
  9383. (and down (org-table-maybe-recalculate-line))
  9384. (or suppress-align (and org-table-may-need-update
  9385. (org-table-align))))))
  9386. (defun org-table-put-field-property (prop value)
  9387. (save-excursion
  9388. (put-text-property (progn (skip-chars-backward "^|") (point))
  9389. (progn (skip-chars-forward "^|") (point))
  9390. prop value)))
  9391. (defun org-table-get-range (desc &optional tbeg col highlight)
  9392. "Get a calc vector from a column, accorting to descriptor DESC.
  9393. Optional arguments TBEG and COL can give the beginning of the table and
  9394. the current column, to avoid unnecessary parsing.
  9395. HIGHLIGHT means, just highlight the range."
  9396. (if (not (equal (string-to-char desc) ?@))
  9397. (setq desc (concat "@" desc)))
  9398. (save-excursion
  9399. (or tbeg (setq tbeg (org-table-begin)))
  9400. (or col (setq col (org-table-current-column)))
  9401. (let ((thisline (org-current-line))
  9402. beg end c1 c2 r1 r2 rangep tmp)
  9403. (unless (string-match org-table-range-regexp desc)
  9404. (error "Invalid table range specifier `%s'" desc))
  9405. (setq rangep (match-end 3)
  9406. r1 (and (match-end 1) (match-string 1 desc))
  9407. r2 (and (match-end 4) (match-string 4 desc))
  9408. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9409. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9410. (and c1 (setq c1 (+ (string-to-number c1)
  9411. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9412. (and c2 (setq c2 (+ (string-to-number c2)
  9413. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9414. (if (equal r1 "") (setq r1 nil))
  9415. (if (equal r2 "") (setq r2 nil))
  9416. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9417. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9418. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9419. (if (not r1) (setq r1 thisline))
  9420. (if (not r2) (setq r2 thisline))
  9421. (if (not c1) (setq c1 col))
  9422. (if (not c2) (setq c2 col))
  9423. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9424. ;; just one field
  9425. (progn
  9426. (goto-line r1)
  9427. (while (not (looking-at org-table-dataline-regexp))
  9428. (beginning-of-line 2))
  9429. (prog1 (org-trim (org-table-get-field c1))
  9430. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9431. ;; A range, return a vector
  9432. ;; First sort the numbers to get a regular ractangle
  9433. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9434. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9435. (goto-line r1)
  9436. (while (not (looking-at org-table-dataline-regexp))
  9437. (beginning-of-line 2))
  9438. (org-table-goto-column c1)
  9439. (setq beg (point))
  9440. (goto-line r2)
  9441. (while (not (looking-at org-table-dataline-regexp))
  9442. (beginning-of-line 0))
  9443. (org-table-goto-column c2)
  9444. (setq end (point))
  9445. (if highlight
  9446. (org-table-highlight-rectangle
  9447. beg (progn (skip-chars-forward "^|\n") (point))))
  9448. ;; return string representation of calc vector
  9449. (mapcar 'org-trim
  9450. (apply 'append (org-table-copy-region beg end)))))))
  9451. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9452. "Analyze descriptor DESC and retrieve the corresponding line number.
  9453. The cursor is currently in line CLINE, the table begins in line BLINE,
  9454. and TABLE is a vector with line types."
  9455. (if (string-match "^[0-9]+$" desc)
  9456. (aref org-table-dlines (string-to-number desc))
  9457. (setq cline (or cline (org-current-line))
  9458. bline (or bline org-table-current-begin-line)
  9459. table (or table org-table-current-line-types))
  9460. (if (or
  9461. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9462. ;; 1 2 3 4 5 6
  9463. (and (not (match-end 3)) (not (match-end 6)))
  9464. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9465. (error "invalid row descriptor `%s'" desc))
  9466. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9467. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9468. (odir (and (match-end 5) (match-string 5 desc)))
  9469. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9470. (i (- cline bline))
  9471. (rel (and (match-end 6)
  9472. (or (and (match-end 1) (not (match-end 3)))
  9473. (match-end 5)))))
  9474. (if (and hn (not hdir))
  9475. (progn
  9476. (setq i 0 hdir "+")
  9477. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9478. (if (and (not hn) on (not odir))
  9479. (error "should never happen");;(aref org-table-dlines on)
  9480. (if (and hn (> hn 0))
  9481. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9482. (if on
  9483. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9484. (+ bline i)))))
  9485. (defun org-find-row-type (table i type backwards relative n)
  9486. (let ((l (length table)))
  9487. (while (> n 0)
  9488. (while (and (setq i (+ i (if backwards -1 1)))
  9489. (>= i 0) (< i l)
  9490. (not (eq (aref table i) type))
  9491. (if (and relative (eq (aref table i) 'hline))
  9492. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9493. t)))
  9494. (setq n (1- n)))
  9495. (if (or (< i 0) (>= i l))
  9496. (error "Row descriptior leads outside table")
  9497. i)))
  9498. (defun org-rewrite-old-row-references (s)
  9499. (if (string-match "&[-+0-9I]" s)
  9500. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9501. s))
  9502. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9503. "Convert list ELEMENTS to something appropriate to insert into formula.
  9504. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9505. NUMBERS indicates that everything should be converted to numbers.
  9506. LISPP means to return something appropriate for a Lisp list."
  9507. (if (stringp elements) ; just a single val
  9508. (if lispp
  9509. (if (eq lispp 'literal)
  9510. elements
  9511. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9512. (if (equal elements "") (setq elements "0"))
  9513. (if numbers (number-to-string (string-to-number elements)) elements))
  9514. (unless keep-empty
  9515. (setq elements
  9516. (delq nil
  9517. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9518. elements))))
  9519. (setq elements (or elements '("0")))
  9520. (if lispp
  9521. (mapconcat
  9522. (lambda (x)
  9523. (if (eq lispp 'literal)
  9524. x
  9525. (prin1-to-string (if numbers (string-to-number x) x))))
  9526. elements " ")
  9527. (concat "[" (mapconcat
  9528. (lambda (x)
  9529. (if numbers (number-to-string (string-to-number x)) x))
  9530. elements
  9531. ",") "]"))))
  9532. (defun org-table-recalculate (&optional all noalign)
  9533. "Recalculate the current table line by applying all stored formulas.
  9534. With prefix arg ALL, do this for all lines in the table."
  9535. (interactive "P")
  9536. (or (memq this-command org-recalc-commands)
  9537. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9538. (unless (org-at-table-p) (error "Not at a table"))
  9539. (if (equal all '(16))
  9540. (org-table-iterate)
  9541. (org-table-get-specials)
  9542. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9543. (lambda (a b) (string< (car a) (car b)))))
  9544. (inhibit-redisplay (not debug-on-error))
  9545. (line-re org-table-dataline-regexp)
  9546. (thisline (org-current-line))
  9547. (thiscol (org-table-current-column))
  9548. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9549. ;; Insert constants in all formulas
  9550. (setq eqlist
  9551. (mapcar (lambda (x)
  9552. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9553. x)
  9554. eqlist))
  9555. ;; Split the equation list
  9556. (while (setq eq (pop eqlist))
  9557. (if (<= (string-to-char (car eq)) ?9)
  9558. (push eq eqlnum)
  9559. (push eq eqlname)))
  9560. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9561. (if all
  9562. (progn
  9563. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9564. (goto-char (setq beg (org-table-begin)))
  9565. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9566. ;; This is a table with marked lines, compute selected lines
  9567. (setq line-re org-table-recalculate-regexp)
  9568. ;; Move forward to the first non-header line
  9569. (if (and (re-search-forward org-table-dataline-regexp end t)
  9570. (re-search-forward org-table-hline-regexp end t)
  9571. (re-search-forward org-table-dataline-regexp end t))
  9572. (setq beg (match-beginning 0))
  9573. nil))) ;; just leave beg where it is
  9574. (setq beg (point-at-bol)
  9575. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9576. (goto-char beg)
  9577. (and all (message "Re-applying formulas to full table..."))
  9578. ;; First find the named fields, and mark them untouchanble
  9579. (remove-text-properties beg end '(org-untouchable t))
  9580. (while (setq eq (pop eqlname))
  9581. (setq name (car eq)
  9582. a (assoc name org-table-named-field-locations))
  9583. (and (not a)
  9584. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9585. (setq a (list name
  9586. (aref org-table-dlines
  9587. (string-to-number (match-string 1 name)))
  9588. (string-to-number (match-string 2 name)))))
  9589. (when (and a (or all (equal (nth 1 a) thisline)))
  9590. (message "Re-applying formula to field: %s" name)
  9591. (goto-line (nth 1 a))
  9592. (org-table-goto-column (nth 2 a))
  9593. (push (append a (list (cdr eq))) eqlname1)
  9594. (org-table-put-field-property :org-untouchable t)))
  9595. ;; Now evauluate the column formulas, but skip fields covered by
  9596. ;; field formulas
  9597. (goto-char beg)
  9598. (while (re-search-forward line-re end t)
  9599. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9600. ;; Unprotected line, recalculate
  9601. (and all (message "Re-applying formulas to full table...(line %d)"
  9602. (setq cnt (1+ cnt))))
  9603. (setq org-last-recalc-line (org-current-line))
  9604. (setq eql eqlnum)
  9605. (while (setq entry (pop eql))
  9606. (goto-line org-last-recalc-line)
  9607. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9608. (unless (get-text-property (point) :org-untouchable)
  9609. (org-table-eval-formula nil (cdr entry)
  9610. 'noalign 'nocst 'nostore 'noanalysis)))))
  9611. ;; Now evaluate the field formulas
  9612. (while (setq eq (pop eqlname1))
  9613. (message "Re-applying formula to field: %s" (car eq))
  9614. (goto-line (nth 1 eq))
  9615. (org-table-goto-column (nth 2 eq))
  9616. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9617. 'nostore 'noanalysis))
  9618. (goto-line thisline)
  9619. (org-table-goto-column thiscol)
  9620. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9621. (or noalign (and org-table-may-need-update (org-table-align))
  9622. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9623. ;; back to initial position
  9624. (message "Re-applying formulas...done")
  9625. (goto-line thisline)
  9626. (org-table-goto-column thiscol)
  9627. (or noalign (and org-table-may-need-update (org-table-align))
  9628. (and all (message "Re-applying formulas...done"))))))
  9629. (defun org-table-iterate (&optional arg)
  9630. "Recalculate the table until it does not change anymore."
  9631. (interactive "P")
  9632. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9633. (i 0)
  9634. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9635. thistbl)
  9636. (catch 'exit
  9637. (while (< i imax)
  9638. (setq i (1+ i))
  9639. (org-table-recalculate 'all)
  9640. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9641. (if (not (string= lasttbl thistbl))
  9642. (setq lasttbl thistbl)
  9643. (if (> i 1)
  9644. (message "Convergence after %d iterations" i)
  9645. (message "Table was already stable"))
  9646. (throw 'exit t)))
  9647. (error "No convergence after %d iterations" i))))
  9648. (defun org-table-formula-substitute-names (f)
  9649. "Replace $const with values in string F."
  9650. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9651. ;; First, check for column names
  9652. (while (setq start (string-match org-table-column-name-regexp f start))
  9653. (setq start (1+ start))
  9654. (setq a (assoc (match-string 1 f) org-table-column-names))
  9655. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9656. ;; Parameters and constants
  9657. (setq start 0)
  9658. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9659. (setq start (1+ start))
  9660. (if (setq a (save-match-data
  9661. (org-table-get-constant (match-string 1 f))))
  9662. (setq f (replace-match
  9663. (concat (if pp "(") a (if pp ")")) t t f))))
  9664. (if org-table-formula-debug
  9665. (put-text-property 0 (length f) :orig-formula f1 f))
  9666. f))
  9667. (defun org-table-get-constant (const)
  9668. "Find the value for a parameter or constant in a formula.
  9669. Parameters get priority."
  9670. (or (cdr (assoc const org-table-local-parameters))
  9671. (cdr (assoc const org-table-formula-constants-local))
  9672. (cdr (assoc const org-table-formula-constants))
  9673. (and (fboundp 'constants-get) (constants-get const))
  9674. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9675. (org-entry-get nil (substring const 5) 'inherit))
  9676. "#UNDEFINED_NAME"))
  9677. (defvar org-table-fedit-map
  9678. (let ((map (make-sparse-keymap)))
  9679. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9680. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9681. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9682. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9683. (org-defkey map "\C-c?" 'org-table-show-reference)
  9684. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9685. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9686. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9687. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9688. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9689. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9690. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9691. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9692. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9693. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9694. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9695. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9696. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9697. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9698. map))
  9699. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9700. '("Edit-Formulas"
  9701. ["Finish and Install" org-table-fedit-finish t]
  9702. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9703. ["Abort" org-table-fedit-abort t]
  9704. "--"
  9705. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9706. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9707. "--"
  9708. "Shift Reference at Point"
  9709. ["Up" org-table-fedit-ref-up t]
  9710. ["Down" org-table-fedit-ref-down t]
  9711. ["Left" org-table-fedit-ref-left t]
  9712. ["Right" org-table-fedit-ref-right t]
  9713. "-"
  9714. "Change Test Row for Column Formulas"
  9715. ["Up" org-table-fedit-line-up t]
  9716. ["Down" org-table-fedit-line-down t]
  9717. "--"
  9718. ["Scroll Table Window" org-table-fedit-scroll t]
  9719. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9720. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9721. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9722. org-table-overlay-coordinates)]
  9723. "--"
  9724. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9725. :style toggle :selected org-table-buffer-is-an]))
  9726. (defvar org-pos)
  9727. (defun org-table-edit-formulas ()
  9728. "Edit the formulas of the current table in a separate buffer."
  9729. (interactive)
  9730. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9731. (beginning-of-line 0))
  9732. (unless (org-at-table-p) (error "Not at a table"))
  9733. (org-table-get-specials)
  9734. (let ((key (org-table-current-field-formula 'key 'noerror))
  9735. (eql (sort (org-table-get-stored-formulas 'noerror)
  9736. 'org-table-formula-less-p))
  9737. (pos (move-marker (make-marker) (point)))
  9738. (startline 1)
  9739. (wc (current-window-configuration))
  9740. (titles '((column . "# Column Formulas\n")
  9741. (field . "# Field Formulas\n")
  9742. (named . "# Named Field Formulas\n")))
  9743. entry s type title)
  9744. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9745. (erase-buffer)
  9746. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9747. (let ((font-lock-global-modes '(not fundamental-mode)))
  9748. (fundamental-mode))
  9749. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9750. (org-set-local 'org-pos pos)
  9751. (org-set-local 'org-window-configuration wc)
  9752. (use-local-map org-table-fedit-map)
  9753. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9754. (easy-menu-add org-table-fedit-menu)
  9755. (setq startline (org-current-line))
  9756. (while (setq entry (pop eql))
  9757. (setq type (cond
  9758. ((equal (string-to-char (car entry)) ?@) 'field)
  9759. ((string-match "^[0-9]" (car entry)) 'column)
  9760. (t 'named)))
  9761. (when (setq title (assq type titles))
  9762. (or (bobp) (insert "\n"))
  9763. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9764. (setq titles (delq title titles)))
  9765. (if (equal key (car entry)) (setq startline (org-current-line)))
  9766. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9767. (car entry) " = " (cdr entry) "\n"))
  9768. (remove-text-properties 0 (length s) '(face nil) s)
  9769. (insert s))
  9770. (if (eq org-table-use-standard-references t)
  9771. (org-table-fedit-toggle-ref-type))
  9772. (goto-line startline)
  9773. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9774. (defun org-table-fedit-post-command ()
  9775. (when (not (memq this-command '(lisp-complete-symbol)))
  9776. (let ((win (selected-window)))
  9777. (save-excursion
  9778. (condition-case nil
  9779. (org-table-show-reference)
  9780. (error nil))
  9781. (select-window win)))))
  9782. (defun org-table-formula-to-user (s)
  9783. "Convert a formula from internal to user representation."
  9784. (if (eq org-table-use-standard-references t)
  9785. (org-table-convert-refs-to-an s)
  9786. s))
  9787. (defun org-table-formula-from-user (s)
  9788. "Convert a formula from user to internal representation."
  9789. (if org-table-use-standard-references
  9790. (org-table-convert-refs-to-rc s)
  9791. s))
  9792. (defun org-table-convert-refs-to-rc (s)
  9793. "Convert spreadsheet references from AB7 to @7$28.
  9794. Works for single references, but also for entire formulas and even the
  9795. full TBLFM line."
  9796. (let ((start 0))
  9797. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9798. (cond
  9799. ((match-end 3)
  9800. ;; format match, just advance
  9801. (setq start (match-end 0)))
  9802. ((and (> (match-beginning 0) 0)
  9803. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9804. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9805. ;; 3.e5 or something like this.
  9806. (setq start (match-end 0)))
  9807. (t
  9808. (setq start (match-beginning 0)
  9809. s (replace-match
  9810. (if (equal (match-string 2 s) "&")
  9811. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9812. (format "@%d$%d"
  9813. (string-to-number (match-string 2 s))
  9814. (org-letters-to-number (match-string 1 s))))
  9815. t t s)))))
  9816. s))
  9817. (defun org-table-convert-refs-to-an (s)
  9818. "Convert spreadsheet references from to @7$28 to AB7.
  9819. Works for single references, but also for entire formulas and even the
  9820. full TBLFM line."
  9821. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9822. (setq s (replace-match
  9823. (format "%s%d"
  9824. (org-number-to-letters
  9825. (string-to-number (match-string 2 s)))
  9826. (string-to-number (match-string 1 s)))
  9827. t t s)))
  9828. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9829. (setq s (replace-match (concat "\\1"
  9830. (org-number-to-letters
  9831. (string-to-number (match-string 2 s))) "&")
  9832. t nil s)))
  9833. s)
  9834. (defun org-letters-to-number (s)
  9835. "Convert a base 26 number represented by letters into an integer.
  9836. For example: AB -> 28."
  9837. (let ((n 0))
  9838. (setq s (upcase s))
  9839. (while (> (length s) 0)
  9840. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9841. s (substring s 1)))
  9842. n))
  9843. (defun org-number-to-letters (n)
  9844. "Convert an integer into a base 26 number represented by letters.
  9845. For example: 28 -> AB."
  9846. (let ((s ""))
  9847. (while (> n 0)
  9848. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9849. n (/ (1- n) 26)))
  9850. s))
  9851. (defun org-table-fedit-convert-buffer (function)
  9852. "Convert all references in this buffer, using FUNTION."
  9853. (let ((line (org-current-line)))
  9854. (goto-char (point-min))
  9855. (while (not (eobp))
  9856. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9857. (delete-region (point) (point-at-eol))
  9858. (or (eobp) (forward-char 1)))
  9859. (goto-line line)))
  9860. (defun org-table-fedit-toggle-ref-type ()
  9861. "Convert all references in the buffer from B3 to @3$2 and back."
  9862. (interactive)
  9863. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9864. (org-table-fedit-convert-buffer
  9865. (if org-table-buffer-is-an
  9866. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9867. (message "Reference type switched to %s"
  9868. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9869. (defun org-table-fedit-ref-up ()
  9870. "Shift the reference at point one row/hline up."
  9871. (interactive)
  9872. (org-table-fedit-shift-reference 'up))
  9873. (defun org-table-fedit-ref-down ()
  9874. "Shift the reference at point one row/hline down."
  9875. (interactive)
  9876. (org-table-fedit-shift-reference 'down))
  9877. (defun org-table-fedit-ref-left ()
  9878. "Shift the reference at point one field to the left."
  9879. (interactive)
  9880. (org-table-fedit-shift-reference 'left))
  9881. (defun org-table-fedit-ref-right ()
  9882. "Shift the reference at point one field to the right."
  9883. (interactive)
  9884. (org-table-fedit-shift-reference 'right))
  9885. (defun org-table-fedit-shift-reference (dir)
  9886. (cond
  9887. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9888. (if (memq dir '(left right))
  9889. (org-rematch-and-replace 1 (eq dir 'left))
  9890. (error "Cannot shift reference in this direction")))
  9891. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9892. ;; A B3-like reference
  9893. (if (memq dir '(up down))
  9894. (org-rematch-and-replace 2 (eq dir 'up))
  9895. (org-rematch-and-replace 1 (eq dir 'left))))
  9896. ((org-at-regexp-p
  9897. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9898. ;; An internal reference
  9899. (if (memq dir '(up down))
  9900. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9901. (org-rematch-and-replace 5 (eq dir 'left))))))
  9902. (defun org-rematch-and-replace (n &optional decr hline)
  9903. "Re-match the group N, and replace it with the shifted refrence."
  9904. (or (match-end n) (error "Cannot shift reference in this direction"))
  9905. (goto-char (match-beginning n))
  9906. (and (looking-at (regexp-quote (match-string n)))
  9907. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9908. t t)))
  9909. (defun org-shift-refpart (ref &optional decr hline)
  9910. "Shift a refrence part REF.
  9911. If DECR is set, decrease the references row/column, else increase.
  9912. If HLINE is set, this may be a hline reference, it certainly is not
  9913. a translation reference."
  9914. (save-match-data
  9915. (let* ((sign (string-match "^[-+]" ref)) n)
  9916. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9917. (cond
  9918. ((and hline (string-match "^I+" ref))
  9919. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9920. (setq n (+ n (if decr -1 1)))
  9921. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9922. (if sign
  9923. (setq sign (if (< n 0) "-" "+") n (abs n))
  9924. (setq n (max 1 n)))
  9925. (concat sign (make-string n ?I)))
  9926. ((string-match "^[0-9]+" ref)
  9927. (setq n (string-to-number (concat sign ref)))
  9928. (setq n (+ n (if decr -1 1)))
  9929. (if sign
  9930. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9931. (number-to-string (max 1 n))))
  9932. ((string-match "^[a-zA-Z]+" ref)
  9933. (org-number-to-letters
  9934. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9935. (t (error "Cannot shift reference"))))))
  9936. (defun org-table-fedit-toggle-coordinates ()
  9937. "Toggle the display of coordinates in the refrenced table."
  9938. (interactive)
  9939. (let ((pos (marker-position org-pos)))
  9940. (with-current-buffer (marker-buffer org-pos)
  9941. (save-excursion
  9942. (goto-char pos)
  9943. (org-table-toggle-coordinate-overlays)))))
  9944. (defun org-table-fedit-finish (&optional arg)
  9945. "Parse the buffer for formula definitions and install them.
  9946. With prefix ARG, apply the new formulas to the table."
  9947. (interactive "P")
  9948. (org-table-remove-rectangle-highlight)
  9949. (if org-table-use-standard-references
  9950. (progn
  9951. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9952. (setq org-table-buffer-is-an nil)))
  9953. (let ((pos org-pos) eql var form)
  9954. (goto-char (point-min))
  9955. (while (re-search-forward
  9956. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9957. nil t)
  9958. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9959. form (match-string 3))
  9960. (setq form (org-trim form))
  9961. (when (not (equal form ""))
  9962. (while (string-match "[ \t]*\n[ \t]*" form)
  9963. (setq form (replace-match " " t t form)))
  9964. (when (assoc var eql)
  9965. (error "Double formulas for %s" var))
  9966. (push (cons var form) eql)))
  9967. (setq org-pos nil)
  9968. (set-window-configuration org-window-configuration)
  9969. (select-window (get-buffer-window (marker-buffer pos)))
  9970. (goto-char pos)
  9971. (unless (org-at-table-p)
  9972. (error "Lost table position - cannot install formulae"))
  9973. (org-table-store-formulas eql)
  9974. (move-marker pos nil)
  9975. (kill-buffer "*Edit Formulas*")
  9976. (if arg
  9977. (org-table-recalculate 'all)
  9978. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9979. (defun org-table-fedit-abort ()
  9980. "Abort editing formulas, without installing the changes."
  9981. (interactive)
  9982. (org-table-remove-rectangle-highlight)
  9983. (let ((pos org-pos))
  9984. (set-window-configuration org-window-configuration)
  9985. (select-window (get-buffer-window (marker-buffer pos)))
  9986. (goto-char pos)
  9987. (move-marker pos nil)
  9988. (message "Formula editing aborted without installing changes")))
  9989. (defun org-table-fedit-lisp-indent ()
  9990. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9991. (interactive)
  9992. (let ((pos (point)) beg end ind)
  9993. (beginning-of-line 1)
  9994. (cond
  9995. ((looking-at "[ \t]")
  9996. (goto-char pos)
  9997. (call-interactively 'lisp-indent-line))
  9998. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9999. ((not (fboundp 'pp-buffer))
  10000. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  10001. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  10002. (goto-char (- (match-end 0) 2))
  10003. (setq beg (point))
  10004. (setq ind (make-string (current-column) ?\ ))
  10005. (condition-case nil (forward-sexp 1)
  10006. (error
  10007. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  10008. (setq end (point))
  10009. (save-restriction
  10010. (narrow-to-region beg end)
  10011. (if (eq last-command this-command)
  10012. (progn
  10013. (goto-char (point-min))
  10014. (setq this-command nil)
  10015. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  10016. (replace-match " ")))
  10017. (pp-buffer)
  10018. (untabify (point-min) (point-max))
  10019. (goto-char (1+ (point-min)))
  10020. (while (re-search-forward "^." nil t)
  10021. (beginning-of-line 1)
  10022. (insert ind))
  10023. (goto-char (point-max))
  10024. (backward-delete-char 1)))
  10025. (goto-char beg))
  10026. (t nil))))
  10027. (defvar org-show-positions nil)
  10028. (defun org-table-show-reference (&optional local)
  10029. "Show the location/value of the $ expression at point."
  10030. (interactive)
  10031. (org-table-remove-rectangle-highlight)
  10032. (catch 'exit
  10033. (let ((pos (if local (point) org-pos))
  10034. (face2 'highlight)
  10035. (org-inhibit-highlight-removal t)
  10036. (win (selected-window))
  10037. (org-show-positions nil)
  10038. var name e what match dest)
  10039. (if local (org-table-get-specials))
  10040. (setq what (cond
  10041. ((or (org-at-regexp-p org-table-range-regexp2)
  10042. (org-at-regexp-p org-table-translate-regexp)
  10043. (org-at-regexp-p org-table-range-regexp))
  10044. (setq match
  10045. (save-match-data
  10046. (org-table-convert-refs-to-rc (match-string 0))))
  10047. 'range)
  10048. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  10049. ((org-at-regexp-p "\\$[0-9]+") 'column)
  10050. ((not local) nil)
  10051. (t (error "No reference at point")))
  10052. match (and what (or match (match-string 0))))
  10053. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  10054. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  10055. 'secondary-selection))
  10056. (org-add-hook 'before-change-functions
  10057. 'org-table-remove-rectangle-highlight)
  10058. (if (eq what 'name) (setq var (substring match 1)))
  10059. (when (eq what 'range)
  10060. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  10061. (setq match (org-table-formula-substitute-names match)))
  10062. (unless local
  10063. (save-excursion
  10064. (end-of-line 1)
  10065. (re-search-backward "^\\S-" nil t)
  10066. (beginning-of-line 1)
  10067. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  10068. (setq dest
  10069. (save-match-data
  10070. (org-table-convert-refs-to-rc (match-string 1))))
  10071. (org-table-add-rectangle-overlay
  10072. (match-beginning 1) (match-end 1) face2))))
  10073. (if (and (markerp pos) (marker-buffer pos))
  10074. (if (get-buffer-window (marker-buffer pos))
  10075. (select-window (get-buffer-window (marker-buffer pos)))
  10076. (org-switch-to-buffer-other-window (get-buffer-window
  10077. (marker-buffer pos)))))
  10078. (goto-char pos)
  10079. (org-table-force-dataline)
  10080. (when dest
  10081. (setq name (substring dest 1))
  10082. (cond
  10083. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  10084. (setq e (assoc name org-table-named-field-locations))
  10085. (goto-line (nth 1 e))
  10086. (org-table-goto-column (nth 2 e)))
  10087. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  10088. (let ((l (string-to-number (match-string 1 dest)))
  10089. (c (string-to-number (match-string 2 dest))))
  10090. (goto-line (aref org-table-dlines l))
  10091. (org-table-goto-column c)))
  10092. (t (org-table-goto-column (string-to-number name))))
  10093. (move-marker pos (point))
  10094. (org-table-highlight-rectangle nil nil face2))
  10095. (cond
  10096. ((equal dest match))
  10097. ((not match))
  10098. ((eq what 'range)
  10099. (condition-case nil
  10100. (save-excursion
  10101. (org-table-get-range match nil nil 'highlight))
  10102. (error nil)))
  10103. ((setq e (assoc var org-table-named-field-locations))
  10104. (goto-line (nth 1 e))
  10105. (org-table-goto-column (nth 2 e))
  10106. (org-table-highlight-rectangle (point) (point))
  10107. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10108. ((setq e (assoc var org-table-column-names))
  10109. (org-table-goto-column (string-to-number (cdr e)))
  10110. (org-table-highlight-rectangle (point) (point))
  10111. (goto-char (org-table-begin))
  10112. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10113. (org-table-end) t)
  10114. (progn
  10115. (goto-char (match-beginning 1))
  10116. (org-table-highlight-rectangle)
  10117. (message "Named column (column %s)" (cdr e)))
  10118. (error "Column name not found")))
  10119. ((eq what 'column)
  10120. ;; column number
  10121. (org-table-goto-column (string-to-number (substring match 1)))
  10122. (org-table-highlight-rectangle (point) (point))
  10123. (message "Column %s" (substring match 1)))
  10124. ((setq e (assoc var org-table-local-parameters))
  10125. (goto-char (org-table-begin))
  10126. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10127. (progn
  10128. (goto-char (match-beginning 1))
  10129. (org-table-highlight-rectangle)
  10130. (message "Local parameter."))
  10131. (error "Parameter not found")))
  10132. (t
  10133. (cond
  10134. ((not var) (error "No reference at point"))
  10135. ((setq e (assoc var org-table-formula-constants-local))
  10136. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  10137. var (cdr e)))
  10138. ((setq e (assoc var org-table-formula-constants))
  10139. (message "Constant: $%s=%s in `org-table-formula-constants'."
  10140. var (cdr e)))
  10141. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10142. (message "Constant: $%s=%s, from `constants.el'%s."
  10143. var e (format " (%s units)" constants-unit-system)))
  10144. (t (error "Undefined name $%s" var)))))
  10145. (goto-char pos)
  10146. (when (and org-show-positions
  10147. (not (memq this-command '(org-table-fedit-scroll
  10148. org-table-fedit-scroll-down))))
  10149. (push pos org-show-positions)
  10150. (push org-table-current-begin-pos org-show-positions)
  10151. (let ((min (apply 'min org-show-positions))
  10152. (max (apply 'max org-show-positions)))
  10153. (goto-char min) (recenter 0)
  10154. (goto-char max)
  10155. (or (pos-visible-in-window-p max) (recenter -1))))
  10156. (select-window win))))
  10157. (defun org-table-force-dataline ()
  10158. "Make sure the cursor is in a dataline in a table."
  10159. (unless (save-excursion
  10160. (beginning-of-line 1)
  10161. (looking-at org-table-dataline-regexp))
  10162. (let* ((re org-table-dataline-regexp)
  10163. (p1 (save-excursion (re-search-forward re nil 'move)))
  10164. (p2 (save-excursion (re-search-backward re nil 'move))))
  10165. (cond ((and p1 p2)
  10166. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  10167. p1 p2)))
  10168. ((or p1 p2) (goto-char (or p1 p2)))
  10169. (t (error "No table dataline around here"))))))
  10170. (defun org-table-fedit-line-up ()
  10171. "Move cursor one line up in the window showing the table."
  10172. (interactive)
  10173. (org-table-fedit-move 'previous-line))
  10174. (defun org-table-fedit-line-down ()
  10175. "Move cursor one line down in the window showing the table."
  10176. (interactive)
  10177. (org-table-fedit-move 'next-line))
  10178. (defun org-table-fedit-move (command)
  10179. "Move the cursor in the window shoinw the table.
  10180. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  10181. (let ((org-table-allow-automatic-line-recalculation nil)
  10182. (pos org-pos) (win (selected-window)) p)
  10183. (select-window (get-buffer-window (marker-buffer org-pos)))
  10184. (setq p (point))
  10185. (call-interactively command)
  10186. (while (and (org-at-table-p)
  10187. (org-at-table-hline-p))
  10188. (call-interactively command))
  10189. (or (org-at-table-p) (goto-char p))
  10190. (move-marker pos (point))
  10191. (select-window win)))
  10192. (defun org-table-fedit-scroll (N)
  10193. (interactive "p")
  10194. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  10195. (scroll-other-window N)))
  10196. (defun org-table-fedit-scroll-down (N)
  10197. (interactive "p")
  10198. (org-table-fedit-scroll (- N)))
  10199. (defvar org-table-rectangle-overlays nil)
  10200. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10201. "Add a new overlay."
  10202. (let ((ov (org-make-overlay beg end)))
  10203. (org-overlay-put ov 'face (or face 'secondary-selection))
  10204. (push ov org-table-rectangle-overlays)))
  10205. (defun org-table-highlight-rectangle (&optional beg end face)
  10206. "Highlight rectangular region in a table."
  10207. (setq beg (or beg (point)) end (or end (point)))
  10208. (let ((b (min beg end))
  10209. (e (max beg end))
  10210. l1 c1 l2 c2 tmp)
  10211. (and (boundp 'org-show-positions)
  10212. (setq org-show-positions (cons b (cons e org-show-positions))))
  10213. (goto-char (min beg end))
  10214. (setq l1 (org-current-line)
  10215. c1 (org-table-current-column))
  10216. (goto-char (max beg end))
  10217. (setq l2 (org-current-line)
  10218. c2 (org-table-current-column))
  10219. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10220. (goto-line l1)
  10221. (beginning-of-line 1)
  10222. (loop for line from l1 to l2 do
  10223. (when (looking-at org-table-dataline-regexp)
  10224. (org-table-goto-column c1)
  10225. (skip-chars-backward "^|\n") (setq beg (point))
  10226. (org-table-goto-column c2)
  10227. (skip-chars-forward "^|\n") (setq end (point))
  10228. (org-table-add-rectangle-overlay beg end face))
  10229. (beginning-of-line 2))
  10230. (goto-char b))
  10231. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10232. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10233. "Remove the rectangle overlays."
  10234. (unless org-inhibit-highlight-removal
  10235. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10236. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10237. (setq org-table-rectangle-overlays nil)))
  10238. (defvar org-table-coordinate-overlays nil
  10239. "Collects the cooordinate grid overlays, so that they can be removed.")
  10240. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10241. (defun org-table-overlay-coordinates ()
  10242. "Add overlays to the table at point, to show row/column coordinates."
  10243. (interactive)
  10244. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10245. (setq org-table-coordinate-overlays nil)
  10246. (save-excursion
  10247. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10248. (goto-char (org-table-begin))
  10249. (while (org-at-table-p)
  10250. (setq eol (point-at-eol))
  10251. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10252. (push ov org-table-coordinate-overlays)
  10253. (setq hline (looking-at org-table-hline-regexp))
  10254. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10255. (format "%4d" (setq id (1+ id)))))
  10256. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10257. (when hline
  10258. (setq ic 0)
  10259. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10260. (setq beg (1+ (match-beginning 0))
  10261. ic (1+ ic)
  10262. s1 (concat "$" (int-to-string ic))
  10263. s2 (org-number-to-letters ic)
  10264. str (if (eq org-table-use-standard-references t) s2 s1))
  10265. (setq ov (org-make-overlay beg (+ beg (length str))))
  10266. (push ov org-table-coordinate-overlays)
  10267. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10268. (beginning-of-line 2)))))
  10269. (defun org-table-toggle-coordinate-overlays ()
  10270. "Toggle the display of Row/Column numbers in tables."
  10271. (interactive)
  10272. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10273. (message "Row/Column number display turned %s"
  10274. (if org-table-overlay-coordinates "on" "off"))
  10275. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10276. (org-table-align))
  10277. (unless org-table-overlay-coordinates
  10278. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10279. (setq org-table-coordinate-overlays nil)))
  10280. (defun org-table-toggle-formula-debugger ()
  10281. "Toggle the formula debugger in tables."
  10282. (interactive)
  10283. (setq org-table-formula-debug (not org-table-formula-debug))
  10284. (message "Formula debugging has been turned %s"
  10285. (if org-table-formula-debug "on" "off")))
  10286. ;;; The orgtbl minor mode
  10287. ;; Define a minor mode which can be used in other modes in order to
  10288. ;; integrate the org-mode table editor.
  10289. ;; This is really a hack, because the org-mode table editor uses several
  10290. ;; keys which normally belong to the major mode, for example the TAB and
  10291. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10292. ;; necessary to operate the table editor, but wraps the commands into a
  10293. ;; function which tests if the cursor is currently inside a table. If that
  10294. ;; is the case, the table editor command is executed. However, when any of
  10295. ;; those keys is used outside a table, the function uses `key-binding' to
  10296. ;; look up if the key has an associated command in another currently active
  10297. ;; keymap (minor modes, major mode, global), and executes that command.
  10298. ;; There might be problems if any of the keys used by the table editor is
  10299. ;; otherwise used as a prefix key.
  10300. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10301. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10302. ;; addresses this by checking explicitly for both bindings.
  10303. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10304. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10305. ;; Some modes bind other commands to simple characters, for example
  10306. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10307. ;; active, this binding is ignored inside tables and replaced with a
  10308. ;; modified self-insert.
  10309. (defvar orgtbl-mode nil
  10310. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10311. table editor in arbitrary modes.")
  10312. (make-variable-buffer-local 'orgtbl-mode)
  10313. (defvar orgtbl-mode-map (make-keymap)
  10314. "Keymap for `orgtbl-mode'.")
  10315. ;;;###autoload
  10316. (defun turn-on-orgtbl ()
  10317. "Unconditionally turn on `orgtbl-mode'."
  10318. (orgtbl-mode 1))
  10319. (defvar org-old-auto-fill-inhibit-regexp nil
  10320. "Local variable used by `orgtbl-mode'")
  10321. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10322. "Matches a line belonging to an orgtbl.")
  10323. (defconst orgtbl-extra-font-lock-keywords
  10324. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10325. 0 (quote 'org-table) 'prepend))
  10326. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10327. ;;;###autoload
  10328. (defun orgtbl-mode (&optional arg)
  10329. "The `org-mode' table editor as a minor mode for use in other modes."
  10330. (interactive)
  10331. (if (org-mode-p)
  10332. ;; Exit without error, in case some hook functions calls this
  10333. ;; by accident in org-mode.
  10334. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10335. (setq orgtbl-mode
  10336. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10337. (if orgtbl-mode
  10338. (progn
  10339. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10340. ;; Make sure we are first in minor-mode-map-alist
  10341. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10342. (and c (setq minor-mode-map-alist
  10343. (cons c (delq c minor-mode-map-alist)))))
  10344. (org-set-local (quote org-table-may-need-update) t)
  10345. (org-add-hook 'before-change-functions 'org-before-change-function
  10346. nil 'local)
  10347. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10348. auto-fill-inhibit-regexp)
  10349. (org-set-local 'auto-fill-inhibit-regexp
  10350. (if auto-fill-inhibit-regexp
  10351. (concat orgtbl-line-start-regexp "\\|"
  10352. auto-fill-inhibit-regexp)
  10353. orgtbl-line-start-regexp))
  10354. (org-add-to-invisibility-spec '(org-cwidth))
  10355. (when (fboundp 'font-lock-add-keywords)
  10356. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10357. (org-restart-font-lock))
  10358. (easy-menu-add orgtbl-mode-menu)
  10359. (run-hooks 'orgtbl-mode-hook))
  10360. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10361. (org-cleanup-narrow-column-properties)
  10362. (org-remove-from-invisibility-spec '(org-cwidth))
  10363. (remove-hook 'before-change-functions 'org-before-change-function t)
  10364. (when (fboundp 'font-lock-remove-keywords)
  10365. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10366. (org-restart-font-lock))
  10367. (easy-menu-remove orgtbl-mode-menu)
  10368. (force-mode-line-update 'all))))
  10369. (defun org-cleanup-narrow-column-properties ()
  10370. "Remove all properties related to narrow-column invisibility."
  10371. (let ((s 1))
  10372. (while (setq s (text-property-any s (point-max)
  10373. 'display org-narrow-column-arrow))
  10374. (remove-text-properties s (1+ s) '(display t)))
  10375. (setq s 1)
  10376. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10377. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10378. (setq s 1)
  10379. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10380. (remove-text-properties s (1+ s) '(invisible t)))))
  10381. ;; Install it as a minor mode.
  10382. (put 'orgtbl-mode :included t)
  10383. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10384. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10385. (defun orgtbl-make-binding (fun n &rest keys)
  10386. "Create a function for binding in the table minor mode.
  10387. FUN is the command to call inside a table. N is used to create a unique
  10388. command name. KEYS are keys that should be checked in for a command
  10389. to execute outside of tables."
  10390. (eval
  10391. (list 'defun
  10392. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10393. '(arg)
  10394. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10395. "Outside of tables, run the binding of `"
  10396. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10397. "'.")
  10398. '(interactive "p")
  10399. (list 'if
  10400. '(org-at-table-p)
  10401. (list 'call-interactively (list 'quote fun))
  10402. (list 'let '(orgtbl-mode)
  10403. (list 'call-interactively
  10404. (append '(or)
  10405. (mapcar (lambda (k)
  10406. (list 'key-binding k))
  10407. keys)
  10408. '('orgtbl-error))))))))
  10409. (defun orgtbl-error ()
  10410. "Error when there is no default binding for a table key."
  10411. (interactive)
  10412. (error "This key has no function outside tables"))
  10413. (defun orgtbl-setup ()
  10414. "Setup orgtbl keymaps."
  10415. (let ((nfunc 0)
  10416. (bindings
  10417. (list
  10418. '([(meta shift left)] org-table-delete-column)
  10419. '([(meta left)] org-table-move-column-left)
  10420. '([(meta right)] org-table-move-column-right)
  10421. '([(meta shift right)] org-table-insert-column)
  10422. '([(meta shift up)] org-table-kill-row)
  10423. '([(meta shift down)] org-table-insert-row)
  10424. '([(meta up)] org-table-move-row-up)
  10425. '([(meta down)] org-table-move-row-down)
  10426. '("\C-c\C-w" org-table-cut-region)
  10427. '("\C-c\M-w" org-table-copy-region)
  10428. '("\C-c\C-y" org-table-paste-rectangle)
  10429. '("\C-c-" org-table-insert-hline)
  10430. '("\C-c}" org-table-toggle-coordinate-overlays)
  10431. '("\C-c{" org-table-toggle-formula-debugger)
  10432. '("\C-m" org-table-next-row)
  10433. '([(shift return)] org-table-copy-down)
  10434. '("\C-c\C-q" org-table-wrap-region)
  10435. '("\C-c?" org-table-field-info)
  10436. '("\C-c " org-table-blank-field)
  10437. '("\C-c+" org-table-sum)
  10438. '("\C-c=" org-table-eval-formula)
  10439. '("\C-c'" org-table-edit-formulas)
  10440. '("\C-c`" org-table-edit-field)
  10441. '("\C-c*" org-table-recalculate)
  10442. '("\C-c|" org-table-create-or-convert-from-region)
  10443. '("\C-c^" org-table-sort-lines)
  10444. '([(control ?#)] org-table-rotate-recalc-marks)))
  10445. elt key fun cmd)
  10446. (while (setq elt (pop bindings))
  10447. (setq nfunc (1+ nfunc))
  10448. (setq key (org-key (car elt))
  10449. fun (nth 1 elt)
  10450. cmd (orgtbl-make-binding fun nfunc key))
  10451. (org-defkey orgtbl-mode-map key cmd))
  10452. ;; Special treatment needed for TAB and RET
  10453. (org-defkey orgtbl-mode-map [(return)]
  10454. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10455. (org-defkey orgtbl-mode-map "\C-m"
  10456. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10457. (org-defkey orgtbl-mode-map [(tab)]
  10458. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10459. (org-defkey orgtbl-mode-map "\C-i"
  10460. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10461. (org-defkey orgtbl-mode-map [(shift tab)]
  10462. (orgtbl-make-binding 'org-table-previous-field 104
  10463. [(shift tab)] [(tab)] "\C-i"))
  10464. (org-defkey orgtbl-mode-map "\M-\C-m"
  10465. (orgtbl-make-binding 'org-table-wrap-region 105
  10466. "\M-\C-m" [(meta return)]))
  10467. (org-defkey orgtbl-mode-map [(meta return)]
  10468. (orgtbl-make-binding 'org-table-wrap-region 106
  10469. [(meta return)] "\M-\C-m"))
  10470. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10471. (when orgtbl-optimized
  10472. ;; If the user wants maximum table support, we need to hijack
  10473. ;; some standard editing functions
  10474. (org-remap orgtbl-mode-map
  10475. 'self-insert-command 'orgtbl-self-insert-command
  10476. 'delete-char 'org-delete-char
  10477. 'delete-backward-char 'org-delete-backward-char)
  10478. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10479. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10480. '("OrgTbl"
  10481. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10482. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10483. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10484. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10485. "--"
  10486. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10487. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10488. ["Copy Field from Above"
  10489. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10490. "--"
  10491. ("Column"
  10492. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10493. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10494. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10495. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10496. ("Row"
  10497. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10498. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10499. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10500. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10501. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10502. "--"
  10503. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10504. ("Rectangle"
  10505. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10506. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10507. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10508. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10509. "--"
  10510. ("Radio tables"
  10511. ["Insert table template" orgtbl-insert-radio-table
  10512. (assq major-mode orgtbl-radio-table-templates)]
  10513. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10514. "--"
  10515. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10516. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10517. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10518. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10519. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10520. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10521. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10522. ["Sum Column/Rectangle" org-table-sum
  10523. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10524. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10525. ["Debug Formulas"
  10526. org-table-toggle-formula-debugger :active (org-at-table-p)
  10527. :keys "C-c {"
  10528. :style toggle :selected org-table-formula-debug]
  10529. ["Show Col/Row Numbers"
  10530. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10531. :keys "C-c }"
  10532. :style toggle :selected org-table-overlay-coordinates]
  10533. ))
  10534. t))
  10535. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10536. "If the cursor is inside a table, realign the table.
  10537. It it is a table to be sent away to a receiver, do it.
  10538. With prefix arg, also recompute table."
  10539. (interactive "P")
  10540. (let ((pos (point)) action)
  10541. (save-excursion
  10542. (beginning-of-line 1)
  10543. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10544. ((looking-at "[ \t]*|") pos)
  10545. ((looking-at "#\\+TBLFM:") 'recalc))))
  10546. (cond
  10547. ((integerp action)
  10548. (goto-char action)
  10549. (org-table-maybe-eval-formula)
  10550. (if arg
  10551. (call-interactively 'org-table-recalculate)
  10552. (org-table-maybe-recalculate-line))
  10553. (call-interactively 'org-table-align)
  10554. (orgtbl-send-table 'maybe))
  10555. ((eq action 'recalc)
  10556. (save-excursion
  10557. (beginning-of-line 1)
  10558. (skip-chars-backward " \r\n\t")
  10559. (if (org-at-table-p)
  10560. (org-call-with-arg 'org-table-recalculate t))))
  10561. (t (let (orgtbl-mode)
  10562. (call-interactively (key-binding "\C-c\C-c")))))))
  10563. (defun orgtbl-tab (arg)
  10564. "Justification and field motion for `orgtbl-mode'."
  10565. (interactive "P")
  10566. (if arg (org-table-edit-field t)
  10567. (org-table-justify-field-maybe)
  10568. (org-table-next-field)))
  10569. (defun orgtbl-ret ()
  10570. "Justification and field motion for `orgtbl-mode'."
  10571. (interactive)
  10572. (org-table-justify-field-maybe)
  10573. (org-table-next-row))
  10574. (defun orgtbl-self-insert-command (N)
  10575. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10576. If the cursor is in a table looking at whitespace, the whitespace is
  10577. overwritten, and the table is not marked as requiring realignment."
  10578. (interactive "p")
  10579. (if (and (org-at-table-p)
  10580. (or
  10581. (and org-table-auto-blank-field
  10582. (member last-command
  10583. '(orgtbl-hijacker-command-100
  10584. orgtbl-hijacker-command-101
  10585. orgtbl-hijacker-command-102
  10586. orgtbl-hijacker-command-103
  10587. orgtbl-hijacker-command-104
  10588. orgtbl-hijacker-command-105))
  10589. (org-table-blank-field))
  10590. t)
  10591. (eq N 1)
  10592. (looking-at "[^|\n]* +|"))
  10593. (let (org-table-may-need-update)
  10594. (goto-char (1- (match-end 0)))
  10595. (delete-backward-char 1)
  10596. (goto-char (match-beginning 0))
  10597. (self-insert-command N))
  10598. (setq org-table-may-need-update t)
  10599. (let (orgtbl-mode)
  10600. (call-interactively (key-binding (vector last-input-event))))))
  10601. (defun org-force-self-insert (N)
  10602. "Needed to enforce self-insert under remapping."
  10603. (interactive "p")
  10604. (self-insert-command N))
  10605. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10606. "Regula expression matching exponentials as produced by calc.")
  10607. (defvar org-table-clean-did-remove-column nil)
  10608. (defun orgtbl-export (table target)
  10609. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10610. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10611. org-table-last-alignment org-table-last-column-widths
  10612. maxcol column)
  10613. (if (not (fboundp func))
  10614. (error "Cannot export orgtbl table to %s" target))
  10615. (setq lines (org-table-clean-before-export lines))
  10616. (setq table
  10617. (mapcar
  10618. (lambda (x)
  10619. (if (string-match org-table-hline-regexp x)
  10620. 'hline
  10621. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10622. lines))
  10623. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10624. table)))
  10625. (loop for i from (1- maxcol) downto 0 do
  10626. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10627. (setq column (delq nil column))
  10628. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10629. (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))
  10630. (funcall func table nil)))
  10631. (defun orgtbl-send-table (&optional maybe)
  10632. "Send a tranformed version of this table to the receiver position.
  10633. With argument MAYBE, fail quietly if no transformation is defined for
  10634. this table."
  10635. (interactive)
  10636. (catch 'exit
  10637. (unless (org-at-table-p) (error "Not at a table"))
  10638. ;; when non-interactive, we assume align has just happened.
  10639. (when (interactive-p) (org-table-align))
  10640. (save-excursion
  10641. (goto-char (org-table-begin))
  10642. (beginning-of-line 0)
  10643. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10644. (if maybe
  10645. (throw 'exit nil)
  10646. (error "Don't know how to transform this table."))))
  10647. (let* ((name (match-string 1))
  10648. beg
  10649. (transform (intern (match-string 2)))
  10650. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10651. (skip (plist-get params :skip))
  10652. (skipcols (plist-get params :skipcols))
  10653. (txt (buffer-substring-no-properties
  10654. (org-table-begin) (org-table-end)))
  10655. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10656. (lines (org-table-clean-before-export lines))
  10657. (i0 (if org-table-clean-did-remove-column 2 1))
  10658. (table (mapcar
  10659. (lambda (x)
  10660. (if (string-match org-table-hline-regexp x)
  10661. 'hline
  10662. (org-remove-by-index
  10663. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10664. skipcols i0)))
  10665. lines))
  10666. (fun (if (= i0 2) 'cdr 'identity))
  10667. (org-table-last-alignment
  10668. (org-remove-by-index (funcall fun org-table-last-alignment)
  10669. skipcols i0))
  10670. (org-table-last-column-widths
  10671. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10672. skipcols i0)))
  10673. (unless (fboundp transform)
  10674. (error "No such transformation function %s" transform))
  10675. (setq txt (funcall transform table params))
  10676. ;; Find the insertion place
  10677. (save-excursion
  10678. (goto-char (point-min))
  10679. (unless (re-search-forward
  10680. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10681. (error "Don't know where to insert translated table"))
  10682. (goto-char (match-beginning 0))
  10683. (beginning-of-line 2)
  10684. (setq beg (point))
  10685. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10686. (error "Cannot find end of insertion region"))
  10687. (beginning-of-line 1)
  10688. (delete-region beg (point))
  10689. (goto-char beg)
  10690. (insert txt "\n"))
  10691. (message "Table converted and installed at receiver location"))))
  10692. (defun org-remove-by-index (list indices &optional i0)
  10693. "Remove the elements in LIST with indices in INDICES.
  10694. First element has index 0, or I0 if given."
  10695. (if (not indices)
  10696. list
  10697. (if (integerp indices) (setq indices (list indices)))
  10698. (setq i0 (1- (or i0 0)))
  10699. (delq :rm (mapcar (lambda (x)
  10700. (setq i0 (1+ i0))
  10701. (if (memq i0 indices) :rm x))
  10702. list))))
  10703. (defun orgtbl-toggle-comment ()
  10704. "Comment or uncomment the orgtbl at point."
  10705. (interactive)
  10706. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10707. (re2 (concat "^" orgtbl-line-start-regexp))
  10708. (commented (save-excursion (beginning-of-line 1)
  10709. (cond ((looking-at re1) t)
  10710. ((looking-at re2) nil)
  10711. (t (error "Not at an org table")))))
  10712. (re (if commented re1 re2))
  10713. beg end)
  10714. (save-excursion
  10715. (beginning-of-line 1)
  10716. (while (looking-at re) (beginning-of-line 0))
  10717. (beginning-of-line 2)
  10718. (setq beg (point))
  10719. (while (looking-at re) (beginning-of-line 2))
  10720. (setq end (point)))
  10721. (comment-region beg end (if commented '(4) nil))))
  10722. (defun orgtbl-insert-radio-table ()
  10723. "Insert a radio table template appropriate for this major mode."
  10724. (interactive)
  10725. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10726. (txt (nth 1 e))
  10727. name pos)
  10728. (unless e (error "No radio table setup defined for %s" major-mode))
  10729. (setq name (read-string "Table name: "))
  10730. (while (string-match "%n" txt)
  10731. (setq txt (replace-match name t t txt)))
  10732. (or (bolp) (insert "\n"))
  10733. (setq pos (point))
  10734. (insert txt)
  10735. (goto-char pos)))
  10736. (defun org-get-param (params header i sym &optional hsym)
  10737. "Get parameter value for symbol SYM.
  10738. If this is a header line, actually get the value for the symbol with an
  10739. additional \"h\" inserted after the colon.
  10740. If the value is a protperty list, get the element for the current column.
  10741. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10742. (let ((val (plist-get params sym)))
  10743. (and hsym header (setq val (or (plist-get params hsym) val)))
  10744. (if (consp val) (plist-get val i) val)))
  10745. (defun orgtbl-to-generic (table params)
  10746. "Convert the orgtbl-mode TABLE to some other format.
  10747. This generic routine can be used for many standard cases.
  10748. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10749. separator line, or a list of fields for that line.
  10750. PARAMS is a property list of parameters that can influence the conversion.
  10751. For the generic converter, some parameters are obligatory: You need to
  10752. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10753. :splice, you must have :tstart and :tend.
  10754. Valid parameters are
  10755. :tstart String to start the table. Ignored when :splice is t.
  10756. :tend String to end the table. Ignored when :splice is t.
  10757. :splice When set to t, return only table body lines, don't wrap
  10758. them into :tstart and :tend. Default is nil.
  10759. :hline String to be inserted on horizontal separation lines.
  10760. May be nil to ignore hlines.
  10761. :lstart String to start a new table line.
  10762. :lend String to end a table line
  10763. :sep Separator between two fields
  10764. :lfmt Format for entire line, with enough %s to capture all fields.
  10765. If this is present, :lstart, :lend, and :sep are ignored.
  10766. :fmt A format to be used to wrap the field, should contain
  10767. %s for the original field value. For example, to wrap
  10768. everything in dollars, you could use :fmt \"$%s$\".
  10769. This may also be a property list with column numbers and
  10770. formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10771. :hlstart :hlend :hlsep :hlfmt :hfmt
  10772. Same as above, specific for the header lines in the table.
  10773. All lines before the first hline are treated as header.
  10774. If any of these is not present, the data line value is used.
  10775. :efmt Use this format to print numbers with exponentials.
  10776. The format should have %s twice for inserting mantissa
  10777. and exponent, for example \"%s\\\\times10^{%s}\". This
  10778. may also be a property list with column numbers and
  10779. formats. :fmt will still be applied after :efmt.
  10780. In addition to this, the parameters :skip and :skipcols are always handled
  10781. directly by `orgtbl-send-table'. See manual."
  10782. (interactive)
  10783. (let* ((p params)
  10784. (splicep (plist-get p :splice))
  10785. (hline (plist-get p :hline))
  10786. rtn line i fm efm lfmt h)
  10787. ;; Do we have a header?
  10788. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10789. (setq h t))
  10790. ;; Put header
  10791. (unless splicep
  10792. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10793. ;; Now loop over all lines
  10794. (while (setq line (pop table))
  10795. (if (eq line 'hline)
  10796. ;; A horizontal separator line
  10797. (progn (if hline (push hline rtn))
  10798. (setq h nil)) ; no longer in header
  10799. ;; A normal line. Convert the fields, push line onto the result list
  10800. (setq i 0)
  10801. (setq line
  10802. (mapcar
  10803. (lambda (f)
  10804. (setq i (1+ i)
  10805. fm (org-get-param p h i :fmt :hfmt)
  10806. efm (org-get-param p h i :efmt))
  10807. (if (and efm (string-match orgtbl-exp-regexp f))
  10808. (setq f (format
  10809. efm (match-string 1 f) (match-string 2 f))))
  10810. (if fm (setq f (format fm f)))
  10811. f)
  10812. line))
  10813. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10814. (push (apply 'format lfmt line) rtn)
  10815. (push (concat
  10816. (org-get-param p h i :lstart :hlstart)
  10817. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10818. (org-get-param p h i :lend :hlend))
  10819. rtn))))
  10820. (unless splicep
  10821. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10822. (mapconcat 'identity (nreverse rtn) "\n")))
  10823. (defun orgtbl-to-latex (table params)
  10824. "Convert the orgtbl-mode TABLE to LaTeX.
  10825. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10826. separator line, or a list of fields for that line.
  10827. PARAMS is a property list of parameters that can influence the conversion.
  10828. Supports all parameters from `orgtbl-to-generic'. Most important for
  10829. LaTeX are:
  10830. :splice When set to t, return only table body lines, don't wrap
  10831. them into a tabular environment. Default is nil.
  10832. :fmt A format to be used to wrap the field, should contain %s for the
  10833. original field value. For example, to wrap everything in dollars,
  10834. use :fmt \"$%s$\". This may also be a property list with column
  10835. numbers and formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10836. :efmt Format for transforming numbers with exponentials. The format
  10837. should have %s twice for inserting mantissa and exponent, for
  10838. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10839. This may also be a property list with column numbers and formats.
  10840. The general parameters :skip and :skipcols have already been applied when
  10841. this function is called."
  10842. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10843. org-table-last-alignment ""))
  10844. (params2
  10845. (list
  10846. :tstart (concat "\\begin{tabular}{" alignment "}")
  10847. :tend "\\end{tabular}"
  10848. :lstart "" :lend " \\\\" :sep " & "
  10849. :efmt "%s\\,(%s)" :hline "\\hline")))
  10850. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10851. (defun orgtbl-to-html (table params)
  10852. "Convert the orgtbl-mode TABLE to LaTeX.
  10853. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10854. separator line, or a list of fields for that line.
  10855. PARAMS is a property list of parameters that can influence the conversion.
  10856. Currently this function recognizes the following parameters:
  10857. :splice When set to t, return only table body lines, don't wrap
  10858. them into a <table> environment. Default is nil.
  10859. The general parameters :skip and :skipcols have already been applied when
  10860. this function is called. The function does *not* use `orgtbl-to-generic',
  10861. so you cannot specify parameters for it."
  10862. (let* ((splicep (plist-get params :splice))
  10863. html)
  10864. ;; Just call the formatter we already have
  10865. ;; We need to make text lines for it, so put the fields back together.
  10866. (setq html (org-format-org-table-html
  10867. (mapcar
  10868. (lambda (x)
  10869. (if (eq x 'hline)
  10870. "|----+----|"
  10871. (concat "| " (mapconcat 'identity x " | ") " |")))
  10872. table)
  10873. splicep))
  10874. (if (string-match "\n+\\'" html)
  10875. (setq html (replace-match "" t t html)))
  10876. html))
  10877. (defun orgtbl-to-texinfo (table params)
  10878. "Convert the orgtbl-mode TABLE to TeXInfo.
  10879. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10880. separator line, or a list of fields for that line.
  10881. PARAMS is a property list of parameters that can influence the conversion.
  10882. Supports all parameters from `orgtbl-to-generic'. Most important for
  10883. TeXInfo are:
  10884. :splice nil/t When set to t, return only table body lines, don't wrap
  10885. them into a multitable environment. Default is nil.
  10886. :fmt fmt A format to be used to wrap the field, should contain
  10887. %s for the original field value. For example, to wrap
  10888. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10889. This may also be a property list with column numbers and
  10890. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10891. :cf \"f1 f2..\" The column fractions for the table. By default these
  10892. are computed automatically from the width of the columns
  10893. under org-mode.
  10894. The general parameters :skip and :skipcols have already been applied when
  10895. this function is called."
  10896. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10897. (colfrac (or (plist-get params :cf)
  10898. (mapconcat
  10899. (lambda (x) (format "%.3f" (/ (float x) total)))
  10900. org-table-last-column-widths " ")))
  10901. (params2
  10902. (list
  10903. :tstart (concat "@multitable @columnfractions " colfrac)
  10904. :tend "@end multitable"
  10905. :lstart "@item " :lend "" :sep " @tab "
  10906. :hlstart "@headitem ")))
  10907. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10908. ;;;; Link Stuff
  10909. ;;; Link abbreviations
  10910. (defun org-link-expand-abbrev (link)
  10911. "Apply replacements as defined in `org-link-abbrev-alist."
  10912. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10913. (let* ((key (match-string 1 link))
  10914. (as (or (assoc key org-link-abbrev-alist-local)
  10915. (assoc key org-link-abbrev-alist)))
  10916. (tag (and (match-end 2) (match-string 3 link)))
  10917. rpl)
  10918. (if (not as)
  10919. link
  10920. (setq rpl (cdr as))
  10921. (cond
  10922. ((symbolp rpl) (funcall rpl tag))
  10923. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10924. (t (concat rpl tag)))))
  10925. link))
  10926. ;;; Storing and inserting links
  10927. (defvar org-insert-link-history nil
  10928. "Minibuffer history for links inserted with `org-insert-link'.")
  10929. (defvar org-stored-links nil
  10930. "Contains the links stored with `org-store-link'.")
  10931. (defvar org-store-link-plist nil
  10932. "Plist with info about the most recently link created with `org-store-link'.")
  10933. (defvar org-link-protocols nil
  10934. "Link protocols added to Org-mode using `org-add-link-type'.")
  10935. (defvar org-store-link-functions nil
  10936. "List of functions that are called to create and store a link.
  10937. Each function will be called in turn until one returns a non-nil
  10938. value. Each function should check if it is responsible for creating
  10939. this link (for example by looking at the major mode).
  10940. If not, it must exit and return nil.
  10941. If yes, it should return a non-nil value after a calling
  10942. `org-store-link-props' with a list of properties and values.
  10943. Special properties are:
  10944. :type The link prefix. like \"http\". This must be given.
  10945. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10946. This is obligatory as well.
  10947. :description Optional default description for the second pair
  10948. of brackets in an Org-mode link. The user can still change
  10949. this when inserting this link into an Org-mode buffer.
  10950. In addition to these, any additional properties can be specified
  10951. and then used in remember templates.")
  10952. (defun org-add-link-type (type &optional follow publish)
  10953. "Add TYPE to the list of `org-link-types'.
  10954. Re-compute all regular expressions depending on `org-link-types'
  10955. FOLLOW and PUBLISH are two functions. Both take the link path as
  10956. an argument.
  10957. FOLLOW should do whatever is necessary to follow the link, for example
  10958. to find a file or display a mail message.
  10959. PUBLISH takes the path and retuns the string that should be used when
  10960. this document is published. FIMXE: This is actually not yet implemented."
  10961. (add-to-list 'org-link-types type t)
  10962. (org-make-link-regexps)
  10963. (add-to-list 'org-link-protocols
  10964. (list type follow publish)))
  10965. (defun org-add-agenda-custom-command (entry)
  10966. "Replace or add a command in `org-agenda-custom-commands'.
  10967. This is mostly for hacking and trying a new command - once the command
  10968. works you probably want to add it to `org-agenda-custom-commands' for good."
  10969. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10970. (if ass
  10971. (setcdr ass (cdr entry))
  10972. (push entry org-agenda-custom-commands))))
  10973. ;;;###autoload
  10974. (defun org-store-link (arg)
  10975. "\\<org-mode-map>Store an org-link to the current location.
  10976. This link is added to `org-stored-links' and can later be inserted
  10977. into an org-buffer with \\[org-insert-link].
  10978. For some link types, a prefix arg is interpreted:
  10979. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10980. For file links, arg negates `org-context-in-file-links'."
  10981. (interactive "P")
  10982. (setq org-store-link-plist nil) ; reset
  10983. (let (link cpltxt desc description search txt)
  10984. (cond
  10985. ((run-hook-with-args-until-success 'org-store-link-functions)
  10986. (setq link (plist-get org-store-link-plist :link)
  10987. desc (or (plist-get org-store-link-plist :description) link)))
  10988. ((eq major-mode 'bbdb-mode)
  10989. (let ((name (bbdb-record-name (bbdb-current-record)))
  10990. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10991. (setq cpltxt (concat "bbdb:" (or name company))
  10992. link (org-make-link cpltxt))
  10993. (org-store-link-props :type "bbdb" :name name :company company)))
  10994. ((eq major-mode 'Info-mode)
  10995. (setq link (org-make-link "info:"
  10996. (file-name-nondirectory Info-current-file)
  10997. ":" Info-current-node))
  10998. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10999. ":" Info-current-node))
  11000. (org-store-link-props :type "info" :file Info-current-file
  11001. :node Info-current-node))
  11002. ((eq major-mode 'calendar-mode)
  11003. (let ((cd (calendar-cursor-to-date)))
  11004. (setq link
  11005. (format-time-string
  11006. (car org-time-stamp-formats)
  11007. (apply 'encode-time
  11008. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  11009. nil nil nil))))
  11010. (org-store-link-props :type "calendar" :date cd)))
  11011. ((or (eq major-mode 'vm-summary-mode)
  11012. (eq major-mode 'vm-presentation-mode))
  11013. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  11014. (vm-follow-summary-cursor)
  11015. (save-excursion
  11016. (vm-select-folder-buffer)
  11017. (let* ((message (car vm-message-pointer))
  11018. (folder buffer-file-name)
  11019. (subject (vm-su-subject message))
  11020. (to (vm-get-header-contents message "To"))
  11021. (from (vm-get-header-contents message "From"))
  11022. (message-id (vm-su-message-id message)))
  11023. (org-store-link-props :type "vm" :from from :to to :subject subject
  11024. :message-id message-id)
  11025. (setq message-id (org-remove-angle-brackets message-id))
  11026. (setq folder (abbreviate-file-name folder))
  11027. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  11028. folder)
  11029. (setq folder (replace-match "" t t folder)))
  11030. (setq cpltxt (org-email-link-description))
  11031. (setq link (org-make-link "vm:" folder "#" message-id)))))
  11032. ((eq major-mode 'wl-summary-mode)
  11033. (let* ((msgnum (wl-summary-message-number))
  11034. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  11035. msgnum 'message-id))
  11036. (wl-message-entity
  11037. (if (fboundp 'elmo-message-entity)
  11038. (elmo-message-entity
  11039. wl-summary-buffer-elmo-folder msgnum)
  11040. (elmo-msgdb-overview-get-entity
  11041. msgnum (wl-summary-buffer-msgdb))))
  11042. (from (wl-summary-line-from))
  11043. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  11044. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  11045. (wl-summary-line-subject))))
  11046. (org-store-link-props :type "wl" :from from :to to
  11047. :subject subject :message-id message-id)
  11048. (setq message-id (org-remove-angle-brackets message-id))
  11049. (setq cpltxt (org-email-link-description))
  11050. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  11051. "#" message-id))))
  11052. ((or (equal major-mode 'mh-folder-mode)
  11053. (equal major-mode 'mh-show-mode))
  11054. (let ((from (org-mhe-get-header "From:"))
  11055. (to (org-mhe-get-header "To:"))
  11056. (message-id (org-mhe-get-header "Message-Id:"))
  11057. (subject (org-mhe-get-header "Subject:")))
  11058. (org-store-link-props :type "mh" :from from :to to
  11059. :subject subject :message-id message-id)
  11060. (setq cpltxt (org-email-link-description))
  11061. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  11062. (org-remove-angle-brackets message-id)))))
  11063. ((or (eq major-mode 'rmail-mode)
  11064. (eq major-mode 'rmail-summary-mode))
  11065. (save-window-excursion
  11066. (save-restriction
  11067. (when (eq major-mode 'rmail-summary-mode)
  11068. (rmail-show-message rmail-current-message))
  11069. (rmail-narrow-to-non-pruned-header)
  11070. (let ((folder buffer-file-name)
  11071. (message-id (mail-fetch-field "message-id"))
  11072. (from (mail-fetch-field "from"))
  11073. (to (mail-fetch-field "to"))
  11074. (subject (mail-fetch-field "subject")))
  11075. (org-store-link-props
  11076. :type "rmail" :from from :to to
  11077. :subject subject :message-id message-id)
  11078. (setq message-id (org-remove-angle-brackets message-id))
  11079. (setq cpltxt (org-email-link-description))
  11080. (setq link (org-make-link "rmail:" folder "#" message-id)))
  11081. (rmail-show-message rmail-current-message))))
  11082. ((eq major-mode 'gnus-group-mode)
  11083. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  11084. (gnus-group-group-name)) ; version
  11085. ((fboundp 'gnus-group-name)
  11086. (gnus-group-name))
  11087. (t "???"))))
  11088. (unless group (error "Not on a group"))
  11089. (org-store-link-props :type "gnus" :group group)
  11090. (setq cpltxt (concat
  11091. (if (org-xor arg org-usenet-links-prefer-google)
  11092. "http://groups.google.com/groups?group="
  11093. "gnus:")
  11094. group)
  11095. link (org-make-link cpltxt))))
  11096. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  11097. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  11098. (let* ((group gnus-newsgroup-name)
  11099. (article (gnus-summary-article-number))
  11100. (header (gnus-summary-article-header article))
  11101. (from (mail-header-from header))
  11102. (message-id (mail-header-id header))
  11103. (date (mail-header-date header))
  11104. (subject (gnus-summary-subject-string)))
  11105. (org-store-link-props :type "gnus" :from from :subject subject
  11106. :message-id message-id :group group)
  11107. (setq cpltxt (org-email-link-description))
  11108. (if (org-xor arg org-usenet-links-prefer-google)
  11109. (setq link
  11110. (concat
  11111. cpltxt "\n "
  11112. (format "http://groups.google.com/groups?as_umsgid=%s"
  11113. (org-fixup-message-id-for-http message-id))))
  11114. (setq link (org-make-link "gnus:" group
  11115. "#" (number-to-string article))))))
  11116. ((eq major-mode 'w3-mode)
  11117. (setq cpltxt (url-view-url t)
  11118. link (org-make-link cpltxt))
  11119. (org-store-link-props :type "w3" :url (url-view-url t)))
  11120. ((eq major-mode 'w3m-mode)
  11121. (setq cpltxt (or w3m-current-title w3m-current-url)
  11122. link (org-make-link w3m-current-url))
  11123. (org-store-link-props :type "w3m" :url (url-view-url t)))
  11124. ((setq search (run-hook-with-args-until-success
  11125. 'org-create-file-search-functions))
  11126. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  11127. "::" search))
  11128. (setq cpltxt (or description link)))
  11129. ((eq major-mode 'image-mode)
  11130. (setq cpltxt (concat "file:"
  11131. (abbreviate-file-name buffer-file-name))
  11132. link (org-make-link cpltxt))
  11133. (org-store-link-props :type "image" :file buffer-file-name))
  11134. ((eq major-mode 'dired-mode)
  11135. ;; link to the file in the current line
  11136. (setq cpltxt (concat "file:"
  11137. (abbreviate-file-name
  11138. (expand-file-name
  11139. (dired-get-filename nil t))))
  11140. link (org-make-link cpltxt)))
  11141. ((and buffer-file-name (org-mode-p))
  11142. ;; Just link to current headline
  11143. (setq cpltxt (concat "file:"
  11144. (abbreviate-file-name buffer-file-name)))
  11145. ;; Add a context search string
  11146. (when (org-xor org-context-in-file-links arg)
  11147. ;; Check if we are on a target
  11148. (if (org-in-regexp "<<\\(.*?\\)>>")
  11149. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11150. (setq txt (cond
  11151. ((org-on-heading-p) nil)
  11152. ((org-region-active-p)
  11153. (buffer-substring (region-beginning) (region-end)))
  11154. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11155. (when (or (null txt) (string-match "\\S-" txt))
  11156. (setq cpltxt
  11157. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11158. desc "NONE"))))
  11159. (if (string-match "::\\'" cpltxt)
  11160. (setq cpltxt (substring cpltxt 0 -2)))
  11161. (setq link (org-make-link cpltxt)))
  11162. ((buffer-file-name (buffer-base-buffer))
  11163. ;; Just link to this file here.
  11164. (setq cpltxt (concat "file:"
  11165. (abbreviate-file-name
  11166. (buffer-file-name (buffer-base-buffer)))))
  11167. ;; Add a context string
  11168. (when (org-xor org-context-in-file-links arg)
  11169. (setq txt (if (org-region-active-p)
  11170. (buffer-substring (region-beginning) (region-end))
  11171. (buffer-substring (point-at-bol) (point-at-eol))))
  11172. ;; Only use search option if there is some text.
  11173. (when (string-match "\\S-" txt)
  11174. (setq cpltxt
  11175. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11176. desc "NONE")))
  11177. (setq link (org-make-link cpltxt)))
  11178. ((interactive-p)
  11179. (error "Cannot link to a buffer which is not visiting a file"))
  11180. (t (setq link nil)))
  11181. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11182. (setq link (or link cpltxt)
  11183. desc (or desc cpltxt))
  11184. (if (equal desc "NONE") (setq desc nil))
  11185. (if (and (interactive-p) link)
  11186. (progn
  11187. (setq org-stored-links
  11188. (cons (list link desc) org-stored-links))
  11189. (message "Stored: %s" (or desc link)))
  11190. (and link (org-make-link-string link desc)))))
  11191. (defun org-store-link-props (&rest plist)
  11192. "Store link properties, extract names and addresses."
  11193. (let (x adr)
  11194. (when (setq x (plist-get plist :from))
  11195. (setq adr (mail-extract-address-components x))
  11196. (plist-put plist :fromname (car adr))
  11197. (plist-put plist :fromaddress (nth 1 adr)))
  11198. (when (setq x (plist-get plist :to))
  11199. (setq adr (mail-extract-address-components x))
  11200. (plist-put plist :toname (car adr))
  11201. (plist-put plist :toaddress (nth 1 adr))))
  11202. (let ((from (plist-get plist :from))
  11203. (to (plist-get plist :to)))
  11204. (when (and from to org-from-is-user-regexp)
  11205. (plist-put plist :fromto
  11206. (if (string-match org-from-is-user-regexp from)
  11207. (concat "to %t")
  11208. (concat "from %f")))))
  11209. (setq org-store-link-plist plist))
  11210. (defun org-email-link-description (&optional fmt)
  11211. "Return the description part of an email link.
  11212. This takes information from `org-store-link-plist' and formats it
  11213. according to FMT (default from `org-email-link-description-format')."
  11214. (setq fmt (or fmt org-email-link-description-format))
  11215. (let* ((p org-store-link-plist)
  11216. (to (plist-get p :toaddress))
  11217. (from (plist-get p :fromaddress))
  11218. (table
  11219. (list
  11220. (cons "%c" (plist-get p :fromto))
  11221. (cons "%F" (plist-get p :from))
  11222. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11223. (cons "%T" (plist-get p :to))
  11224. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11225. (cons "%s" (plist-get p :subject))
  11226. (cons "%m" (plist-get p :message-id)))))
  11227. (when (string-match "%c" fmt)
  11228. ;; Check if the user wrote this message
  11229. (if (and org-from-is-user-regexp from to
  11230. (save-match-data (string-match org-from-is-user-regexp from)))
  11231. (setq fmt (replace-match "to %t" t t fmt))
  11232. (setq fmt (replace-match "from %f" t t fmt))))
  11233. (org-replace-escapes fmt table)))
  11234. (defun org-make-org-heading-search-string (&optional string heading)
  11235. "Make search string for STRING or current headline."
  11236. (interactive)
  11237. (let ((s (or string (org-get-heading))))
  11238. (unless (and string (not heading))
  11239. ;; We are using a headline, clean up garbage in there.
  11240. (if (string-match org-todo-regexp s)
  11241. (setq s (replace-match "" t t s)))
  11242. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11243. (setq s (replace-match "" t t s)))
  11244. (setq s (org-trim s))
  11245. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11246. org-comment-string "\\)") s)
  11247. (setq s (replace-match "" t t s)))
  11248. (while (string-match org-ts-regexp s)
  11249. (setq s (replace-match "" t t s))))
  11250. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11251. (setq s (replace-match " " t t s)))
  11252. (or string (setq s (concat "*" s))) ; Add * for headlines
  11253. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11254. (defun org-make-link (&rest strings)
  11255. "Concatenate STRINGS."
  11256. (apply 'concat strings))
  11257. (defun org-make-link-string (link &optional description)
  11258. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11259. (unless (string-match "\\S-" link)
  11260. (error "Empty link"))
  11261. (when (stringp description)
  11262. ;; Remove brackets from the description, they are fatal.
  11263. (while (string-match "\\[" description)
  11264. (setq description (replace-match "{" t t description)))
  11265. (while (string-match "\\]" description)
  11266. (setq description (replace-match "}" t t description))))
  11267. (when (equal (org-link-escape link) description)
  11268. ;; No description needed, it is identical
  11269. (setq description nil))
  11270. (when (and (not description)
  11271. (not (equal link (org-link-escape link))))
  11272. (setq description link))
  11273. (concat "[[" (org-link-escape link) "]"
  11274. (if description (concat "[" description "]") "")
  11275. "]"))
  11276. (defconst org-link-escape-chars
  11277. '((?\ . "%20")
  11278. (?\[ . "%5B")
  11279. (?\] . "%5D")
  11280. (?\340 . "%E0") ; `a
  11281. (?\342 . "%E2") ; ^a
  11282. (?\347 . "%E7") ; ,c
  11283. (?\350 . "%E8") ; `e
  11284. (?\351 . "%E9") ; 'e
  11285. (?\352 . "%EA") ; ^e
  11286. (?\356 . "%EE") ; ^i
  11287. (?\364 . "%F4") ; ^o
  11288. (?\371 . "%F9") ; `u
  11289. (?\373 . "%FB") ; ^u
  11290. (?\; . "%3B")
  11291. (?? . "%3F")
  11292. (?= . "%3D")
  11293. (?+ . "%2B")
  11294. )
  11295. "Association list of escapes for some characters problematic in links.
  11296. This is the list that is used for internal purposes.")
  11297. (defconst org-link-escape-chars-browser
  11298. '((?\ . "%20")) ; 32 for the SPC char
  11299. "Association list of escapes for some characters problematic in links.
  11300. This is the list that is used before handing over to the browser.")
  11301. (defun org-link-escape (text &optional table)
  11302. "Escape charaters in TEXT that are problematic for links."
  11303. (setq table (or table org-link-escape-chars))
  11304. (when text
  11305. (let ((re (mapconcat (lambda (x) (regexp-quote
  11306. (char-to-string (car x))))
  11307. table "\\|")))
  11308. (while (string-match re text)
  11309. (setq text
  11310. (replace-match
  11311. (cdr (assoc (string-to-char (match-string 0 text))
  11312. table))
  11313. t t text)))
  11314. text)))
  11315. (defun org-link-unescape (text &optional table)
  11316. "Reverse the action of `org-link-escape'."
  11317. (setq table (or table org-link-escape-chars))
  11318. (when text
  11319. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11320. table "\\|")))
  11321. (while (string-match re text)
  11322. (setq text
  11323. (replace-match
  11324. (char-to-string (car (rassoc (match-string 0 text) table)))
  11325. t t text)))
  11326. text)))
  11327. (defun org-xor (a b)
  11328. "Exclusive or."
  11329. (if a (not b) b))
  11330. (defun org-get-header (header)
  11331. "Find a header field in the current buffer."
  11332. (save-excursion
  11333. (goto-char (point-min))
  11334. (let ((case-fold-search t) s)
  11335. (cond
  11336. ((eq header 'from)
  11337. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11338. (setq s (match-string 1)))
  11339. (while (string-match "\"" s)
  11340. (setq s (replace-match "" t t s)))
  11341. (if (string-match "[<(].*" s)
  11342. (setq s (replace-match "" t t s))))
  11343. ((eq header 'message-id)
  11344. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11345. (setq s (match-string 1))))
  11346. ((eq header 'subject)
  11347. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11348. (setq s (match-string 1)))))
  11349. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11350. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11351. s)))
  11352. (defun org-fixup-message-id-for-http (s)
  11353. "Replace special characters in a message id, so it can be used in an http query."
  11354. (while (string-match "<" s)
  11355. (setq s (replace-match "%3C" t t s)))
  11356. (while (string-match ">" s)
  11357. (setq s (replace-match "%3E" t t s)))
  11358. (while (string-match "@" s)
  11359. (setq s (replace-match "%40" t t s)))
  11360. s)
  11361. ;;;###autoload
  11362. (defun org-insert-link-global ()
  11363. "Insert a link like Org-mode does.
  11364. This command can be called in any mode to insert a link in Org-mode syntax."
  11365. (interactive)
  11366. (org-run-like-in-org-mode 'org-insert-link))
  11367. (defun org-insert-link (&optional complete-file)
  11368. "Insert a link. At the prompt, enter the link.
  11369. Completion can be used to select a link previously stored with
  11370. `org-store-link'. When the empty string is entered (i.e. if you just
  11371. press RET at the prompt), the link defaults to the most recently
  11372. stored link. As SPC triggers completion in the minibuffer, you need to
  11373. use M-SPC or C-q SPC to force the insertion of a space character.
  11374. You will also be prompted for a description, and if one is given, it will
  11375. be displayed in the buffer instead of the link.
  11376. If there is already a link at point, this command will allow you to edit link
  11377. and description parts.
  11378. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11379. selected using completion. The path to the file will be relative to
  11380. the current directory if the file is in the current directory or a
  11381. subdirectory. Otherwise, the link will be the absolute path as
  11382. completed in the minibuffer (i.e. normally ~/path/to/file).
  11383. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11384. is in the current directory or below.
  11385. With three \\[universal-argument] prefixes, negate the meaning of
  11386. `org-keep-stored-link-after-insertion'."
  11387. (interactive "P")
  11388. (let* ((wcf (current-window-configuration))
  11389. (region (if (org-region-active-p)
  11390. (buffer-substring (region-beginning) (region-end))))
  11391. (remove (and region (list (region-beginning) (region-end))))
  11392. (desc region)
  11393. tmphist ; byte-compile incorrectly complains about this
  11394. link entry file)
  11395. (cond
  11396. ((org-in-regexp org-bracket-link-regexp 1)
  11397. ;; We do have a link at point, and we are going to edit it.
  11398. (setq remove (list (match-beginning 0) (match-end 0)))
  11399. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11400. (setq link (read-string "Link: "
  11401. (org-link-unescape
  11402. (org-match-string-no-properties 1)))))
  11403. ((or (org-in-regexp org-angle-link-re)
  11404. (org-in-regexp org-plain-link-re))
  11405. ;; Convert to bracket link
  11406. (setq remove (list (match-beginning 0) (match-end 0))
  11407. link (read-string "Link: "
  11408. (org-remove-angle-brackets (match-string 0)))))
  11409. ((equal complete-file '(4))
  11410. ;; Completing read for file names.
  11411. (setq file (read-file-name "File: "))
  11412. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11413. (pwd1 (file-name-as-directory (abbreviate-file-name
  11414. (expand-file-name ".")))))
  11415. (cond
  11416. ((equal complete-file '(16))
  11417. (setq link (org-make-link
  11418. "file:"
  11419. (abbreviate-file-name (expand-file-name file)))))
  11420. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11421. (setq link (org-make-link "file:" (match-string 1 file))))
  11422. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11423. (expand-file-name file))
  11424. (setq link (org-make-link
  11425. "file:" (match-string 1 (expand-file-name file)))))
  11426. (t (setq link (org-make-link "file:" file))))))
  11427. (t
  11428. ;; Read link, with completion for stored links.
  11429. (with-output-to-temp-buffer "*Org Links*"
  11430. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11431. (when org-stored-links
  11432. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11433. (princ (mapconcat
  11434. (lambda (x)
  11435. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11436. (reverse org-stored-links) "\n"))))
  11437. (let ((cw (selected-window)))
  11438. (select-window (get-buffer-window "*Org Links*"))
  11439. (shrink-window-if-larger-than-buffer)
  11440. (setq truncate-lines t)
  11441. (select-window cw))
  11442. ;; Fake a link history, containing the stored links.
  11443. (setq tmphist (append (mapcar 'car org-stored-links)
  11444. org-insert-link-history))
  11445. (unwind-protect
  11446. (setq link (org-completing-read
  11447. "Link: "
  11448. (append
  11449. (mapcar (lambda (x) (list (concat (car x) ":")))
  11450. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11451. (mapcar (lambda (x) (list (concat x ":")))
  11452. org-link-types))
  11453. nil nil nil
  11454. 'tmphist
  11455. (or (car (car org-stored-links)))))
  11456. (set-window-configuration wcf)
  11457. (kill-buffer "*Org Links*"))
  11458. (setq entry (assoc link org-stored-links))
  11459. (or entry (push link org-insert-link-history))
  11460. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11461. (not org-keep-stored-link-after-insertion))
  11462. (setq org-stored-links (delq (assoc link org-stored-links)
  11463. org-stored-links)))
  11464. (setq desc (or desc (nth 1 entry)))))
  11465. (if (string-match org-plain-link-re link)
  11466. ;; URL-like link, normalize the use of angular brackets.
  11467. (setq link (org-make-link (org-remove-angle-brackets link))))
  11468. ;; Check if we are linking to the current file with a search option
  11469. ;; If yes, simplify the link by using only the search option.
  11470. (when (and buffer-file-name
  11471. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11472. (let* ((path (match-string 1 link))
  11473. (case-fold-search nil)
  11474. (search (match-string 2 link)))
  11475. (save-match-data
  11476. (if (equal (file-truename buffer-file-name) (file-truename path))
  11477. ;; We are linking to this same file, with a search option
  11478. (setq link search)))))
  11479. ;; Check if we can/should use a relative path. If yes, simplify the link
  11480. (when (string-match "\\<file:\\(.*\\)" link)
  11481. (let* ((path (match-string 1 link))
  11482. (origpath path)
  11483. (case-fold-search nil))
  11484. (cond
  11485. ((eq org-link-file-path-type 'absolute)
  11486. (setq path (abbreviate-file-name (expand-file-name path))))
  11487. ((eq org-link-file-path-type 'noabbrev)
  11488. (setq path (expand-file-name path)))
  11489. ((eq org-link-file-path-type 'relative)
  11490. (setq path (file-relative-name path)))
  11491. (t
  11492. (save-match-data
  11493. (if (string-match (concat "^" (regexp-quote
  11494. (file-name-as-directory
  11495. (expand-file-name "."))))
  11496. (expand-file-name path))
  11497. ;; We are linking a file with relative path name.
  11498. (setq path (substring (expand-file-name path)
  11499. (match-end 0)))))))
  11500. (setq link (concat "file:" path))
  11501. (if (equal desc origpath)
  11502. (setq desc path))))
  11503. (setq desc (read-string "Description: " desc))
  11504. (unless (string-match "\\S-" desc) (setq desc nil))
  11505. (if remove (apply 'delete-region remove))
  11506. (insert (org-make-link-string link desc))))
  11507. (defun org-completing-read (&rest args)
  11508. (let ((minibuffer-local-completion-map
  11509. (copy-keymap minibuffer-local-completion-map)))
  11510. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11511. (apply 'completing-read args)))
  11512. ;;; Opening/following a link
  11513. (defvar org-link-search-failed nil)
  11514. (defun org-next-link ()
  11515. "Move forward to the next link.
  11516. If the link is in hidden text, expose it."
  11517. (interactive)
  11518. (when (and org-link-search-failed (eq this-command last-command))
  11519. (goto-char (point-min))
  11520. (message "Link search wrapped back to beginning of buffer"))
  11521. (setq org-link-search-failed nil)
  11522. (let* ((pos (point))
  11523. (ct (org-context))
  11524. (a (assoc :link ct)))
  11525. (if a (goto-char (nth 2 a)))
  11526. (if (re-search-forward org-any-link-re nil t)
  11527. (progn
  11528. (goto-char (match-beginning 0))
  11529. (if (org-invisible-p) (org-show-context)))
  11530. (goto-char pos)
  11531. (setq org-link-search-failed t)
  11532. (error "No further link found"))))
  11533. (defun org-previous-link ()
  11534. "Move backward to the previous link.
  11535. If the link is in hidden text, expose it."
  11536. (interactive)
  11537. (when (and org-link-search-failed (eq this-command last-command))
  11538. (goto-char (point-max))
  11539. (message "Link search wrapped back to end of buffer"))
  11540. (setq org-link-search-failed nil)
  11541. (let* ((pos (point))
  11542. (ct (org-context))
  11543. (a (assoc :link ct)))
  11544. (if a (goto-char (nth 1 a)))
  11545. (if (re-search-backward org-any-link-re nil t)
  11546. (progn
  11547. (goto-char (match-beginning 0))
  11548. (if (org-invisible-p) (org-show-context)))
  11549. (goto-char pos)
  11550. (setq org-link-search-failed t)
  11551. (error "No further link found"))))
  11552. (defun org-find-file-at-mouse (ev)
  11553. "Open file link or URL at mouse."
  11554. (interactive "e")
  11555. (mouse-set-point ev)
  11556. (org-open-at-point 'in-emacs))
  11557. (defun org-open-at-mouse (ev)
  11558. "Open file link or URL at mouse."
  11559. (interactive "e")
  11560. (mouse-set-point ev)
  11561. (org-open-at-point))
  11562. (defvar org-window-config-before-follow-link nil
  11563. "The window configuration before following a link.
  11564. This is saved in case the need arises to restore it.")
  11565. (defvar org-open-link-marker (make-marker)
  11566. "Marker pointing to the location where `org-open-at-point; was called.")
  11567. ;;;###autoload
  11568. (defun org-open-at-point-global ()
  11569. "Follow a link like Org-mode does.
  11570. This command can be called in any mode to follow a link that has
  11571. Org-mode syntax."
  11572. (interactive)
  11573. (org-run-like-in-org-mode 'org-open-at-point))
  11574. (defun org-open-at-point (&optional in-emacs)
  11575. "Open link at or after point.
  11576. If there is no link at point, this function will search forward up to
  11577. the end of the current subtree.
  11578. Normally, files will be opened by an appropriate application. If the
  11579. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11580. (interactive "P")
  11581. (catch 'abort
  11582. (move-marker org-open-link-marker (point))
  11583. (setq org-window-config-before-follow-link (current-window-configuration))
  11584. (org-remove-occur-highlights nil nil t)
  11585. (if (org-at-timestamp-p t)
  11586. (org-follow-timestamp-link)
  11587. (let (type path link line search (pos (point)))
  11588. (catch 'match
  11589. (save-excursion
  11590. (skip-chars-forward "^]\n\r")
  11591. (when (org-in-regexp org-bracket-link-regexp)
  11592. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11593. (while (string-match " *\n *" link)
  11594. (setq link (replace-match " " t t link)))
  11595. (setq link (org-link-expand-abbrev link))
  11596. (if (string-match org-link-re-with-space2 link)
  11597. (setq type (match-string 1 link) path (match-string 2 link))
  11598. (setq type "thisfile" path link))
  11599. (throw 'match t)))
  11600. (when (get-text-property (point) 'org-linked-text)
  11601. (setq type "thisfile"
  11602. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11603. (1+ (point)) (point))
  11604. path (buffer-substring
  11605. (previous-single-property-change pos 'org-linked-text)
  11606. (next-single-property-change pos 'org-linked-text)))
  11607. (throw 'match t))
  11608. (save-excursion
  11609. (when (or (org-in-regexp org-angle-link-re)
  11610. (org-in-regexp org-plain-link-re))
  11611. (setq type (match-string 1) path (match-string 2))
  11612. (throw 'match t)))
  11613. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11614. (setq type "tree-match"
  11615. path (match-string 1))
  11616. (throw 'match t))
  11617. (save-excursion
  11618. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11619. (setq type "tags"
  11620. path (match-string 1))
  11621. (while (string-match ":" path)
  11622. (setq path (replace-match "+" t t path)))
  11623. (throw 'match t))))
  11624. (unless path
  11625. (error "No link found"))
  11626. ;; Remove any trailing spaces in path
  11627. (if (string-match " +\\'" path)
  11628. (setq path (replace-match "" t t path)))
  11629. (cond
  11630. ((assoc type org-link-protocols)
  11631. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11632. ((equal type "mailto")
  11633. (let ((cmd (car org-link-mailto-program))
  11634. (args (cdr org-link-mailto-program)) args1
  11635. (address path) (subject "") a)
  11636. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11637. (setq address (match-string 1 path)
  11638. subject (org-link-escape (match-string 2 path))))
  11639. (while args
  11640. (cond
  11641. ((not (stringp (car args))) (push (pop args) args1))
  11642. (t (setq a (pop args))
  11643. (if (string-match "%a" a)
  11644. (setq a (replace-match address t t a)))
  11645. (if (string-match "%s" a)
  11646. (setq a (replace-match subject t t a)))
  11647. (push a args1))))
  11648. (apply cmd (nreverse args1))))
  11649. ((member type '("http" "https" "ftp" "news"))
  11650. (browse-url (concat type ":" (org-link-escape
  11651. path org-link-escape-chars-browser))))
  11652. ((member type '("message"))
  11653. (browse-url (concat type ":" path)))
  11654. ((string= type "tags")
  11655. (org-tags-view in-emacs path))
  11656. ((string= type "thisfile")
  11657. (if in-emacs
  11658. (switch-to-buffer-other-window
  11659. (org-get-buffer-for-internal-link (current-buffer)))
  11660. (org-mark-ring-push))
  11661. (let ((cmd `(org-link-search
  11662. ,path
  11663. ,(cond ((equal in-emacs '(4)) 'occur)
  11664. ((equal in-emacs '(16)) 'org-occur)
  11665. (t nil))
  11666. ,pos)))
  11667. (condition-case nil (eval cmd)
  11668. (error (progn (widen) (eval cmd))))))
  11669. ((string= type "tree-match")
  11670. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11671. ((string= type "file")
  11672. (if (string-match "::\\([0-9]+\\)\\'" path)
  11673. (setq line (string-to-number (match-string 1 path))
  11674. path (substring path 0 (match-beginning 0)))
  11675. (if (string-match "::\\(.+\\)\\'" path)
  11676. (setq search (match-string 1 path)
  11677. path (substring path 0 (match-beginning 0)))))
  11678. (if (string-match "[*?{]" (file-name-nondirectory path))
  11679. (dired path)
  11680. (org-open-file path in-emacs line search)))
  11681. ((string= type "news")
  11682. (org-follow-gnus-link path))
  11683. ((string= type "bbdb")
  11684. (org-follow-bbdb-link path))
  11685. ((string= type "info")
  11686. (org-follow-info-link path))
  11687. ((string= type "gnus")
  11688. (let (group article)
  11689. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11690. (error "Error in Gnus link"))
  11691. (setq group (match-string 1 path)
  11692. article (match-string 3 path))
  11693. (org-follow-gnus-link group article)))
  11694. ((string= type "vm")
  11695. (let (folder article)
  11696. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11697. (error "Error in VM link"))
  11698. (setq folder (match-string 1 path)
  11699. article (match-string 3 path))
  11700. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11701. (org-follow-vm-link folder article in-emacs)))
  11702. ((string= type "wl")
  11703. (let (folder article)
  11704. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11705. (error "Error in Wanderlust link"))
  11706. (setq folder (match-string 1 path)
  11707. article (match-string 3 path))
  11708. (org-follow-wl-link folder article)))
  11709. ((string= type "mhe")
  11710. (let (folder article)
  11711. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11712. (error "Error in MHE link"))
  11713. (setq folder (match-string 1 path)
  11714. article (match-string 3 path))
  11715. (org-follow-mhe-link folder article)))
  11716. ((string= type "rmail")
  11717. (let (folder article)
  11718. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11719. (error "Error in RMAIL link"))
  11720. (setq folder (match-string 1 path)
  11721. article (match-string 3 path))
  11722. (org-follow-rmail-link folder article)))
  11723. ((string= type "shell")
  11724. (let ((cmd path))
  11725. (if (or (not org-confirm-shell-link-function)
  11726. (funcall org-confirm-shell-link-function
  11727. (format "Execute \"%s\" in shell? "
  11728. (org-add-props cmd nil
  11729. 'face 'org-warning))))
  11730. (progn
  11731. (message "Executing %s" cmd)
  11732. (shell-command cmd))
  11733. (error "Abort"))))
  11734. ((string= type "elisp")
  11735. (let ((cmd path))
  11736. (if (or (not org-confirm-elisp-link-function)
  11737. (funcall org-confirm-elisp-link-function
  11738. (format "Execute \"%s\" as elisp? "
  11739. (org-add-props cmd nil
  11740. 'face 'org-warning))))
  11741. (message "%s => %s" cmd (eval (read cmd)))
  11742. (error "Abort"))))
  11743. (t
  11744. (browse-url-at-point)))))
  11745. (move-marker org-open-link-marker nil)))
  11746. ;;; File search
  11747. (defvar org-create-file-search-functions nil
  11748. "List of functions to construct the right search string for a file link.
  11749. These functions are called in turn with point at the location to
  11750. which the link should point.
  11751. A function in the hook should first test if it would like to
  11752. handle this file type, for example by checking the major-mode or
  11753. the file extension. If it decides not to handle this file, it
  11754. should just return nil to give other functions a chance. If it
  11755. does handle the file, it must return the search string to be used
  11756. when following the link. The search string will be part of the
  11757. file link, given after a double colon, and `org-open-at-point'
  11758. will automatically search for it. If special measures must be
  11759. taken to make the search successful, another function should be
  11760. added to the companion hook `org-execute-file-search-functions',
  11761. which see.
  11762. A function in this hook may also use `setq' to set the variable
  11763. `description' to provide a suggestion for the descriptive text to
  11764. be used for this link when it gets inserted into an Org-mode
  11765. buffer with \\[org-insert-link].")
  11766. (defvar org-execute-file-search-functions nil
  11767. "List of functions to execute a file search triggered by a link.
  11768. Functions added to this hook must accept a single argument, the
  11769. search string that was part of the file link, the part after the
  11770. double colon. The function must first check if it would like to
  11771. handle this search, for example by checking the major-mode or the
  11772. file extension. If it decides not to handle this search, it
  11773. should just return nil to give other functions a chance. If it
  11774. does handle the search, it must return a non-nil value to keep
  11775. other functions from trying.
  11776. Each function can access the current prefix argument through the
  11777. variable `current-prefix-argument'. Note that a single prefix is
  11778. used to force opening a link in Emacs, so it may be good to only
  11779. use a numeric or double prefix to guide the search function.
  11780. In case this is needed, a function in this hook can also restore
  11781. the window configuration before `org-open-at-point' was called using:
  11782. (set-window-configuration org-window-config-before-follow-link)")
  11783. (defun org-link-search (s &optional type avoid-pos)
  11784. "Search for a link search option.
  11785. If S is surrounded by forward slashes, it is interpreted as a
  11786. regular expression. In org-mode files, this will create an `org-occur'
  11787. sparse tree. In ordinary files, `occur' will be used to list matches.
  11788. If the current buffer is in `dired-mode', grep will be used to search
  11789. in all files. If AVOID-POS is given, ignore matches near that position."
  11790. (let ((case-fold-search t)
  11791. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11792. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11793. (append '(("") (" ") ("\t") ("\n"))
  11794. org-emphasis-alist)
  11795. "\\|") "\\)"))
  11796. (pos (point))
  11797. (pre "") (post "")
  11798. words re0 re1 re2 re3 re4 re5 re2a reall)
  11799. (cond
  11800. ;; First check if there are any special
  11801. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11802. ;; Now try the builtin stuff
  11803. ((save-excursion
  11804. (goto-char (point-min))
  11805. (and
  11806. (re-search-forward
  11807. (concat "<<" (regexp-quote s0) ">>") nil t)
  11808. (setq pos (match-beginning 0))))
  11809. ;; There is an exact target for this
  11810. (goto-char pos))
  11811. ((string-match "^/\\(.*\\)/$" s)
  11812. ;; A regular expression
  11813. (cond
  11814. ((org-mode-p)
  11815. (org-occur (match-string 1 s)))
  11816. ;;((eq major-mode 'dired-mode)
  11817. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11818. (t (org-do-occur (match-string 1 s)))))
  11819. (t
  11820. ;; A normal search strings
  11821. (when (equal (string-to-char s) ?*)
  11822. ;; Anchor on headlines, post may include tags.
  11823. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11824. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11825. s (substring s 1)))
  11826. (remove-text-properties
  11827. 0 (length s)
  11828. '(face nil mouse-face nil keymap nil fontified nil) s)
  11829. ;; Make a series of regular expressions to find a match
  11830. (setq words (org-split-string s "[ \n\r\t]+")
  11831. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11832. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11833. "\\)" markers)
  11834. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11835. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11836. re1 (concat pre re2 post)
  11837. re3 (concat pre re4 post)
  11838. re5 (concat pre ".*" re4)
  11839. re2 (concat pre re2)
  11840. re2a (concat pre re2a)
  11841. re4 (concat pre re4)
  11842. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11843. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11844. re5 "\\)"
  11845. ))
  11846. (cond
  11847. ((eq type 'org-occur) (org-occur reall))
  11848. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11849. (t (goto-char (point-min))
  11850. (if (or (org-search-not-self 1 re0 nil t)
  11851. (org-search-not-self 1 re1 nil t)
  11852. (org-search-not-self 1 re2 nil t)
  11853. (org-search-not-self 1 re2a nil t)
  11854. (org-search-not-self 1 re3 nil t)
  11855. (org-search-not-self 1 re4 nil t)
  11856. (org-search-not-self 1 re5 nil t)
  11857. )
  11858. (goto-char (match-beginning 1))
  11859. (goto-char pos)
  11860. (error "No match")))))
  11861. (t
  11862. ;; Normal string-search
  11863. (goto-char (point-min))
  11864. (if (search-forward s nil t)
  11865. (goto-char (match-beginning 0))
  11866. (error "No match"))))
  11867. (and (org-mode-p) (org-show-context 'link-search))))
  11868. (defun org-search-not-self (group &rest args)
  11869. "Execute `re-search-forward', but only accept matches that do not
  11870. enclose the position of `org-open-link-marker'."
  11871. (let ((m org-open-link-marker))
  11872. (catch 'exit
  11873. (while (apply 're-search-forward args)
  11874. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11875. (goto-char (match-end group))
  11876. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11877. (> (match-beginning 0) (marker-position m))
  11878. (< (match-end 0) (marker-position m)))
  11879. (save-match-data
  11880. (or (not (org-in-regexp
  11881. org-bracket-link-analytic-regexp 1))
  11882. (not (match-end 4)) ; no description
  11883. (and (<= (match-beginning 4) (point))
  11884. (>= (match-end 4) (point))))))
  11885. (throw 'exit (point))))))))
  11886. (defun org-get-buffer-for-internal-link (buffer)
  11887. "Return a buffer to be used for displaying the link target of internal links."
  11888. (cond
  11889. ((not org-display-internal-link-with-indirect-buffer)
  11890. buffer)
  11891. ((string-match "(Clone)$" (buffer-name buffer))
  11892. (message "Buffer is already a clone, not making another one")
  11893. ;; we also do not modify visibility in this case
  11894. buffer)
  11895. (t ; make a new indirect buffer for displaying the link
  11896. (let* ((bn (buffer-name buffer))
  11897. (ibn (concat bn "(Clone)"))
  11898. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11899. (with-current-buffer ib (org-overview))
  11900. ib))))
  11901. (defun org-do-occur (regexp &optional cleanup)
  11902. "Call the Emacs command `occur'.
  11903. If CLEANUP is non-nil, remove the printout of the regular expression
  11904. in the *Occur* buffer. This is useful if the regex is long and not useful
  11905. to read."
  11906. (occur regexp)
  11907. (when cleanup
  11908. (let ((cwin (selected-window)) win beg end)
  11909. (when (setq win (get-buffer-window "*Occur*"))
  11910. (select-window win))
  11911. (goto-char (point-min))
  11912. (when (re-search-forward "match[a-z]+" nil t)
  11913. (setq beg (match-end 0))
  11914. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11915. (setq end (1- (match-beginning 0)))))
  11916. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11917. (goto-char (point-min))
  11918. (select-window cwin))))
  11919. ;;; The mark ring for links jumps
  11920. (defvar org-mark-ring nil
  11921. "Mark ring for positions before jumps in Org-mode.")
  11922. (defvar org-mark-ring-last-goto nil
  11923. "Last position in the mark ring used to go back.")
  11924. ;; Fill and close the ring
  11925. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11926. (loop for i from 1 to org-mark-ring-length do
  11927. (push (make-marker) org-mark-ring))
  11928. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11929. org-mark-ring)
  11930. (defun org-mark-ring-push (&optional pos buffer)
  11931. "Put the current position or POS into the mark ring and rotate it."
  11932. (interactive)
  11933. (setq pos (or pos (point)))
  11934. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11935. (move-marker (car org-mark-ring)
  11936. (or pos (point))
  11937. (or buffer (current-buffer)))
  11938. (message "%s"
  11939. (substitute-command-keys
  11940. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11941. (defun org-mark-ring-goto (&optional n)
  11942. "Jump to the previous position in the mark ring.
  11943. With prefix arg N, jump back that many stored positions. When
  11944. called several times in succession, walk through the entire ring.
  11945. Org-mode commands jumping to a different position in the current file,
  11946. or to another Org-mode file, automatically push the old position
  11947. onto the ring."
  11948. (interactive "p")
  11949. (let (p m)
  11950. (if (eq last-command this-command)
  11951. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11952. (setq p org-mark-ring))
  11953. (setq org-mark-ring-last-goto p)
  11954. (setq m (car p))
  11955. (switch-to-buffer (marker-buffer m))
  11956. (goto-char m)
  11957. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11958. (defun org-remove-angle-brackets (s)
  11959. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11960. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11961. s)
  11962. (defun org-add-angle-brackets (s)
  11963. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11964. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11965. s)
  11966. ;;; Following specific links
  11967. (defun org-follow-timestamp-link ()
  11968. (cond
  11969. ((org-at-date-range-p t)
  11970. (let ((org-agenda-start-on-weekday)
  11971. (t1 (match-string 1))
  11972. (t2 (match-string 2)))
  11973. (setq t1 (time-to-days (org-time-string-to-time t1))
  11974. t2 (time-to-days (org-time-string-to-time t2)))
  11975. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11976. ((org-at-timestamp-p t)
  11977. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11978. (substring (match-string 1) 0 10)))
  11979. 1))
  11980. (t (error "This should not happen"))))
  11981. (defun org-follow-bbdb-link (name)
  11982. "Follow a BBDB link to NAME."
  11983. (require 'bbdb)
  11984. (let ((inhibit-redisplay (not debug-on-error))
  11985. (bbdb-electric-p nil))
  11986. (catch 'exit
  11987. ;; Exact match on name
  11988. (bbdb-name (concat "\\`" name "\\'") nil)
  11989. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11990. ;; Exact match on name
  11991. (bbdb-company (concat "\\`" name "\\'") nil)
  11992. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11993. ;; Partial match on name
  11994. (bbdb-name name nil)
  11995. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11996. ;; Partial match on company
  11997. (bbdb-company name nil)
  11998. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11999. ;; General match including network address and notes
  12000. (bbdb name nil)
  12001. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  12002. (delete-window (get-buffer-window "*BBDB*"))
  12003. (error "No matching BBDB record")))))
  12004. (defun org-follow-info-link (name)
  12005. "Follow an info file & node link to NAME."
  12006. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  12007. (string-match "\\(.*\\)" name))
  12008. (progn
  12009. (require 'info)
  12010. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  12011. (Info-find-node (match-string 1 name) (match-string 2 name))
  12012. (Info-find-node (match-string 1 name) "Top")))
  12013. (message "Could not open: %s" name)))
  12014. (defun org-follow-gnus-link (&optional group article)
  12015. "Follow a Gnus link to GROUP and ARTICLE."
  12016. (require 'gnus)
  12017. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  12018. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  12019. (cond ((and group article)
  12020. (gnus-group-read-group 1 nil group)
  12021. (gnus-summary-goto-article (string-to-number article) nil t))
  12022. (group (gnus-group-jump-to-group group))))
  12023. (defun org-follow-vm-link (&optional folder article readonly)
  12024. "Follow a VM link to FOLDER and ARTICLE."
  12025. (require 'vm)
  12026. (setq article (org-add-angle-brackets article))
  12027. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  12028. ;; ange-ftp or efs or tramp access
  12029. (let ((user (or (match-string 1 folder) (user-login-name)))
  12030. (host (match-string 2 folder))
  12031. (file (match-string 3 folder)))
  12032. (cond
  12033. ((featurep 'tramp)
  12034. ;; use tramp to access the file
  12035. (if (featurep 'xemacs)
  12036. (setq folder (format "[%s@%s]%s" user host file))
  12037. (setq folder (format "/%s@%s:%s" user host file))))
  12038. (t
  12039. ;; use ange-ftp or efs
  12040. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  12041. (setq folder (format "/%s@%s:%s" user host file))))))
  12042. (when folder
  12043. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  12044. (sit-for 0.1)
  12045. (when article
  12046. (vm-select-folder-buffer)
  12047. (widen)
  12048. (let ((case-fold-search t))
  12049. (goto-char (point-min))
  12050. (if (not (re-search-forward
  12051. (concat "^" "message-id: *" (regexp-quote article))))
  12052. (error "Could not find the specified message in this folder"))
  12053. (vm-isearch-update)
  12054. (vm-isearch-narrow)
  12055. (vm-beginning-of-message)
  12056. (vm-summarize)))))
  12057. (defun org-follow-wl-link (folder article)
  12058. "Follow a Wanderlust link to FOLDER and ARTICLE."
  12059. (if (and (string= folder "%")
  12060. article
  12061. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  12062. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  12063. ;; Thus, we recompose folder and article ids.
  12064. (setq folder (format "%s#%s" folder (match-string 1 article))
  12065. article (match-string 3 article)))
  12066. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  12067. (error "No such folder: %s" folder))
  12068. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  12069. (and article
  12070. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  12071. (wl-summary-redisplay)))
  12072. (defun org-follow-rmail-link (folder article)
  12073. "Follow an RMAIL link to FOLDER and ARTICLE."
  12074. (setq article (org-add-angle-brackets article))
  12075. (let (message-number)
  12076. (save-excursion
  12077. (save-window-excursion
  12078. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12079. (setq message-number
  12080. (save-restriction
  12081. (widen)
  12082. (goto-char (point-max))
  12083. (if (re-search-backward
  12084. (concat "^Message-ID:\\s-+" (regexp-quote
  12085. (or article "")))
  12086. nil t)
  12087. (rmail-what-message))))))
  12088. (if message-number
  12089. (progn
  12090. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12091. (rmail-show-message message-number)
  12092. message-number)
  12093. (error "Message not found"))))
  12094. ;;; mh-e integration based on planner-mode
  12095. (defun org-mhe-get-message-real-folder ()
  12096. "Return the name of the current message real folder, so if you use
  12097. sequences, it will now work."
  12098. (save-excursion
  12099. (let* ((folder
  12100. (if (equal major-mode 'mh-folder-mode)
  12101. mh-current-folder
  12102. ;; Refer to the show buffer
  12103. mh-show-folder-buffer))
  12104. (end-index
  12105. (if (boundp 'mh-index-folder)
  12106. (min (length mh-index-folder) (length folder))))
  12107. )
  12108. ;; a simple test on mh-index-data does not work, because
  12109. ;; mh-index-data is always nil in a show buffer.
  12110. (if (and (boundp 'mh-index-folder)
  12111. (string= mh-index-folder (substring folder 0 end-index)))
  12112. (if (equal major-mode 'mh-show-mode)
  12113. (save-window-excursion
  12114. (let (pop-up-frames)
  12115. (when (buffer-live-p (get-buffer folder))
  12116. (progn
  12117. (pop-to-buffer folder)
  12118. (org-mhe-get-message-folder-from-index)
  12119. )
  12120. )))
  12121. (org-mhe-get-message-folder-from-index)
  12122. )
  12123. folder
  12124. )
  12125. )))
  12126. (defun org-mhe-get-message-folder-from-index ()
  12127. "Returns the name of the message folder in a index folder buffer."
  12128. (save-excursion
  12129. (mh-index-previous-folder)
  12130. (re-search-forward "^\\(+.*\\)$" nil t)
  12131. (message "%s" (match-string 1))))
  12132. (defun org-mhe-get-message-folder ()
  12133. "Return the name of the current message folder. Be careful if you
  12134. use sequences."
  12135. (save-excursion
  12136. (if (equal major-mode 'mh-folder-mode)
  12137. mh-current-folder
  12138. ;; Refer to the show buffer
  12139. mh-show-folder-buffer)))
  12140. (defun org-mhe-get-message-num ()
  12141. "Return the number of the current message. Be careful if you
  12142. use sequences."
  12143. (save-excursion
  12144. (if (equal major-mode 'mh-folder-mode)
  12145. (mh-get-msg-num nil)
  12146. ;; Refer to the show buffer
  12147. (mh-show-buffer-message-number))))
  12148. (defun org-mhe-get-header (header)
  12149. "Return a header of the message in folder mode. This will create a
  12150. show buffer for the corresponding message. If you have a more clever
  12151. idea..."
  12152. (let* ((folder (org-mhe-get-message-folder))
  12153. (num (org-mhe-get-message-num))
  12154. (buffer (get-buffer-create (concat "show-" folder)))
  12155. (header-field))
  12156. (with-current-buffer buffer
  12157. (mh-display-msg num folder)
  12158. (if (equal major-mode 'mh-folder-mode)
  12159. (mh-header-display)
  12160. (mh-show-header-display))
  12161. (set-buffer buffer)
  12162. (setq header-field (mh-get-header-field header))
  12163. (if (equal major-mode 'mh-folder-mode)
  12164. (mh-show)
  12165. (mh-show-show))
  12166. header-field)))
  12167. (defun org-follow-mhe-link (folder article)
  12168. "Follow an MHE link to FOLDER and ARTICLE.
  12169. If ARTICLE is nil FOLDER is shown. If the configuration variable
  12170. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  12171. ARTICLE is searched in all folders. Indexed searches (swish++,
  12172. namazu, and others supported by MH-E) will always search in all
  12173. folders."
  12174. (require 'mh-e)
  12175. (require 'mh-search)
  12176. (require 'mh-utils)
  12177. (mh-find-path)
  12178. (if (not article)
  12179. (mh-visit-folder (mh-normalize-folder-name folder))
  12180. (setq article (org-add-angle-brackets article))
  12181. (mh-search-choose)
  12182. (if (equal mh-searcher 'pick)
  12183. (progn
  12184. (mh-search folder (list "--message-id" article))
  12185. (when (and org-mhe-search-all-folders
  12186. (not (org-mhe-get-message-real-folder)))
  12187. (kill-this-buffer)
  12188. (mh-search "+" (list "--message-id" article))))
  12189. (mh-search "+" article))
  12190. (if (org-mhe-get-message-real-folder)
  12191. (mh-show-msg 1)
  12192. (kill-this-buffer)
  12193. (error "Message not found"))))
  12194. ;;; BibTeX links
  12195. ;; Use the custom search meachnism to construct and use search strings for
  12196. ;; file links to BibTeX database entries.
  12197. (defun org-create-file-search-in-bibtex ()
  12198. "Create the search string and description for a BibTeX database entry."
  12199. (when (eq major-mode 'bibtex-mode)
  12200. ;; yes, we want to construct this search string.
  12201. ;; Make a good description for this entry, using names, year and the title
  12202. ;; Put it into the `description' variable which is dynamically scoped.
  12203. (let ((bibtex-autokey-names 1)
  12204. (bibtex-autokey-names-stretch 1)
  12205. (bibtex-autokey-name-case-convert-function 'identity)
  12206. (bibtex-autokey-name-separator " & ")
  12207. (bibtex-autokey-additional-names " et al.")
  12208. (bibtex-autokey-year-length 4)
  12209. (bibtex-autokey-name-year-separator " ")
  12210. (bibtex-autokey-titlewords 3)
  12211. (bibtex-autokey-titleword-separator " ")
  12212. (bibtex-autokey-titleword-case-convert-function 'identity)
  12213. (bibtex-autokey-titleword-length 'infty)
  12214. (bibtex-autokey-year-title-separator ": "))
  12215. (setq description (bibtex-generate-autokey)))
  12216. ;; Now parse the entry, get the key and return it.
  12217. (save-excursion
  12218. (bibtex-beginning-of-entry)
  12219. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12220. (defun org-execute-file-search-in-bibtex (s)
  12221. "Find the link search string S as a key for a database entry."
  12222. (when (eq major-mode 'bibtex-mode)
  12223. ;; Yes, we want to do the search in this file.
  12224. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12225. (goto-char (point-min))
  12226. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12227. (regexp-quote s) "[ \t\n]*,") nil t)
  12228. (goto-char (match-beginning 0)))
  12229. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12230. ;; Use double prefix to indicate that any web link should be browsed
  12231. (let ((b (current-buffer)) (p (point)))
  12232. ;; Restore the window configuration because we just use the web link
  12233. (set-window-configuration org-window-config-before-follow-link)
  12234. (save-excursion (set-buffer b) (goto-char p)
  12235. (bibtex-url)))
  12236. (recenter 0)) ; Move entry start to beginning of window
  12237. ;; return t to indicate that the search is done.
  12238. t))
  12239. ;; Finally add the functions to the right hooks.
  12240. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12241. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12242. ;; end of Bibtex link setup
  12243. ;;; Following file links
  12244. (defun org-open-file (path &optional in-emacs line search)
  12245. "Open the file at PATH.
  12246. First, this expands any special file name abbreviations. Then the
  12247. configuration variable `org-file-apps' is checked if it contains an
  12248. entry for this file type, and if yes, the corresponding command is launched.
  12249. If no application is found, Emacs simply visits the file.
  12250. With optional argument IN-EMACS, Emacs will visit the file.
  12251. Optional LINE specifies a line to go to, optional SEARCH a string to
  12252. search for. If LINE or SEARCH is given, the file will always be
  12253. opened in Emacs.
  12254. If the file does not exist, an error is thrown."
  12255. (setq in-emacs (or in-emacs line search))
  12256. (let* ((file (if (equal path "")
  12257. buffer-file-name
  12258. (substitute-in-file-name (expand-file-name path))))
  12259. (apps (append org-file-apps (org-default-apps)))
  12260. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12261. (dirp (if remp nil (file-directory-p file)))
  12262. (dfile (downcase file))
  12263. (old-buffer (current-buffer))
  12264. (old-pos (point))
  12265. (old-mode major-mode)
  12266. ext cmd)
  12267. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12268. (setq ext (match-string 1 dfile))
  12269. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12270. (setq ext (match-string 1 dfile))))
  12271. (if in-emacs
  12272. (setq cmd 'emacs)
  12273. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12274. (and dirp (cdr (assoc 'directory apps)))
  12275. (cdr (assoc ext apps))
  12276. (cdr (assoc t apps)))))
  12277. (when (eq cmd 'mailcap)
  12278. (require 'mailcap)
  12279. (mailcap-parse-mailcaps)
  12280. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12281. (command (mailcap-mime-info mime-type)))
  12282. (if (stringp command)
  12283. (setq cmd command)
  12284. (setq cmd 'emacs))))
  12285. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12286. (not (file-exists-p file))
  12287. (not org-open-non-existing-files))
  12288. (error "No such file: %s" file))
  12289. (cond
  12290. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12291. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12292. (while (string-match "['\"]%s['\"]" cmd)
  12293. (setq cmd (replace-match "%s" t t cmd)))
  12294. (while (string-match "%s" cmd)
  12295. (setq cmd (replace-match
  12296. (save-match-data (shell-quote-argument file))
  12297. t t cmd)))
  12298. (save-window-excursion
  12299. (start-process-shell-command cmd nil cmd)))
  12300. ((or (stringp cmd)
  12301. (eq cmd 'emacs))
  12302. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12303. (widen)
  12304. (if line (goto-line line)
  12305. (if search (org-link-search search))))
  12306. ((consp cmd)
  12307. (eval cmd))
  12308. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12309. (and (org-mode-p) (eq old-mode 'org-mode)
  12310. (or (not (equal old-buffer (current-buffer)))
  12311. (not (equal old-pos (point))))
  12312. (org-mark-ring-push old-pos old-buffer))))
  12313. (defun org-default-apps ()
  12314. "Return the default applications for this operating system."
  12315. (cond
  12316. ((eq system-type 'darwin)
  12317. org-file-apps-defaults-macosx)
  12318. ((eq system-type 'windows-nt)
  12319. org-file-apps-defaults-windowsnt)
  12320. (t org-file-apps-defaults-gnu)))
  12321. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12322. (defun org-file-remote-p (file)
  12323. "Test whether FILE specifies a location on a remote system.
  12324. Return non-nil if the location is indeed remote.
  12325. For example, the filename \"/user@host:/foo\" specifies a location
  12326. on the system \"/user@host:\"."
  12327. (cond ((fboundp 'file-remote-p)
  12328. (file-remote-p file))
  12329. ((fboundp 'tramp-handle-file-remote-p)
  12330. (tramp-handle-file-remote-p file))
  12331. ((and (boundp 'ange-ftp-name-format)
  12332. (string-match (car ange-ftp-name-format) file))
  12333. t)
  12334. (t nil)))
  12335. ;;;; Hooks for remember.el, and refiling
  12336. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12337. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12338. ;;;###autoload
  12339. (defun org-remember-insinuate ()
  12340. "Setup remember.el for use wiht Org-mode."
  12341. (require 'remember)
  12342. (setq remember-annotation-functions '(org-remember-annotation))
  12343. (setq remember-handler-functions '(org-remember-handler))
  12344. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12345. ;;;###autoload
  12346. (defun org-remember-annotation ()
  12347. "Return a link to the current location as an annotation for remember.el.
  12348. If you are using Org-mode files as target for data storage with
  12349. remember.el, then the annotations should include a link compatible with the
  12350. conventions in Org-mode. This function returns such a link."
  12351. (org-store-link nil))
  12352. (defconst org-remember-help
  12353. "Select a destination location for the note.
  12354. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12355. RET on headline -> Store as sublevel entry to current headline
  12356. RET at beg-of-buf -> Append to file as level 2 headline
  12357. <left>/<right> -> before/after current headline, same headings level")
  12358. (defvar org-remember-previous-location nil)
  12359. (defvar org-force-remember-template-char) ;; dynamically scoped
  12360. (defun org-select-remember-template (&optional use-char)
  12361. (when org-remember-templates
  12362. (let* ((templates (mapcar (lambda (x)
  12363. (if (stringp (car x))
  12364. (append (list (nth 1 x) (car x)) (cddr x))
  12365. (append (list (car x) "") (cdr x))))
  12366. org-remember-templates))
  12367. (char (or use-char
  12368. (cond
  12369. ((= (length templates) 1)
  12370. (caar templates))
  12371. ((and (boundp 'org-force-remember-template-char)
  12372. org-force-remember-template-char)
  12373. (if (stringp org-force-remember-template-char)
  12374. (string-to-char org-force-remember-template-char)
  12375. org-force-remember-template-char))
  12376. (t
  12377. (message "Select template: %s"
  12378. (mapconcat
  12379. (lambda (x)
  12380. (cond
  12381. ((not (string-match "\\S-" (nth 1 x)))
  12382. (format "[%c]" (car x)))
  12383. ((equal (downcase (car x))
  12384. (downcase (aref (nth 1 x) 0)))
  12385. (format "[%c]%s" (car x)
  12386. (substring (nth 1 x) 1)))
  12387. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12388. templates " "))
  12389. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12390. (when (equal char0 ?\C-g)
  12391. (jump-to-register remember-register)
  12392. (kill-buffer remember-buffer))
  12393. char0))))))
  12394. (cddr (assoc char templates)))))
  12395. (defvar x-last-selected-text)
  12396. (defvar x-last-selected-text-primary)
  12397. ;;;###autoload
  12398. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12399. "Initialize *remember* buffer with template, invoke `org-mode'.
  12400. This function should be placed into `remember-mode-hook' and in fact requires
  12401. to be run from that hook to function properly."
  12402. (if org-remember-templates
  12403. (let* ((entry (org-select-remember-template use-char))
  12404. (tpl (car entry))
  12405. (plist-p (if org-store-link-plist t nil))
  12406. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12407. (string-match "\\S-" (nth 1 entry)))
  12408. (nth 1 entry)
  12409. org-default-notes-file))
  12410. (headline (nth 2 entry))
  12411. (v-c (or (and (eq window-system 'x)
  12412. (fboundp 'x-cut-buffer-or-selection-value)
  12413. (x-cut-buffer-or-selection-value))
  12414. (org-bound-and-true-p x-last-selected-text)
  12415. (org-bound-and-true-p x-last-selected-text-primary)
  12416. (and (> (length kill-ring) 0) (current-kill 0))))
  12417. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12418. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12419. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12420. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12421. ;; `initial' and `annotation' are bound in `remember'
  12422. (v-i (if (boundp 'initial) initial))
  12423. (v-a (if (and (boundp 'annotation) annotation)
  12424. (if (equal annotation "[[]]") "" annotation)
  12425. ""))
  12426. (v-A (if (and v-a
  12427. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12428. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12429. v-a))
  12430. (v-n user-full-name)
  12431. (org-startup-folded nil)
  12432. org-time-was-given org-end-time-was-given x
  12433. prompt completions char time pos default histvar)
  12434. (setq org-store-link-plist
  12435. (append (list :annotation v-a :initial v-i)
  12436. org-store-link-plist))
  12437. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12438. (erase-buffer)
  12439. (insert (substitute-command-keys
  12440. (format
  12441. "## Filing location: Select interactively, default, or last used:
  12442. ## %s to select file and header location interactively.
  12443. ## %s \"%s\" -> \"* %s\"
  12444. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12445. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12446. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12447. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12448. (abbreviate-file-name (or file org-default-notes-file))
  12449. (or headline "")
  12450. (or (car org-remember-previous-location) "???")
  12451. (or (cdr org-remember-previous-location) "???"))))
  12452. (insert tpl) (goto-char (point-min))
  12453. ;; Simple %-escapes
  12454. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12455. (when (and initial (equal (match-string 0) "%i"))
  12456. (save-match-data
  12457. (let* ((lead (buffer-substring
  12458. (point-at-bol) (match-beginning 0))))
  12459. (setq v-i (mapconcat 'identity
  12460. (org-split-string initial "\n")
  12461. (concat "\n" lead))))))
  12462. (replace-match
  12463. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12464. t t))
  12465. ;; %[] Insert contents of a file.
  12466. (goto-char (point-min))
  12467. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12468. (let ((start (match-beginning 0))
  12469. (end (match-end 0))
  12470. (filename (expand-file-name (match-string 1))))
  12471. (goto-char start)
  12472. (delete-region start end)
  12473. (condition-case error
  12474. (insert-file-contents filename)
  12475. (error (insert (format "%%![Couldn't insert %s: %s]"
  12476. filename error))))))
  12477. ;; %() embedded elisp
  12478. (goto-char (point-min))
  12479. (while (re-search-forward "%\\((.+)\\)" nil t)
  12480. (goto-char (match-beginning 0))
  12481. (let ((template-start (point)))
  12482. (forward-char 1)
  12483. (let ((result
  12484. (condition-case error
  12485. (eval (read (current-buffer)))
  12486. (error (format "%%![Error: %s]" error)))))
  12487. (delete-region template-start (point))
  12488. (insert result))))
  12489. ;; From the property list
  12490. (when plist-p
  12491. (goto-char (point-min))
  12492. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12493. (and (setq x (or (plist-get org-store-link-plist
  12494. (intern (match-string 1))) ""))
  12495. (replace-match x t t))))
  12496. ;; Turn on org-mode in the remember buffer, set local variables
  12497. (org-mode)
  12498. (org-set-local 'org-finish-function 'org-remember-finalize)
  12499. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12500. (org-set-local 'org-default-notes-file file))
  12501. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12502. (org-set-local 'org-remember-default-headline headline))
  12503. ;; Interactive template entries
  12504. (goto-char (point-min))
  12505. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12506. (setq char (if (match-end 3) (match-string 3))
  12507. prompt (if (match-end 2) (match-string 2)))
  12508. (goto-char (match-beginning 0))
  12509. (replace-match "")
  12510. (setq completions nil default nil)
  12511. (when prompt
  12512. (setq completions (org-split-string prompt "|")
  12513. prompt (pop completions)
  12514. default (car completions)
  12515. histvar (intern (concat
  12516. "org-remember-template-prompt-history::"
  12517. (or prompt "")))
  12518. completions (mapcar 'list completions)))
  12519. (cond
  12520. ((member char '("G" "g"))
  12521. (let* ((org-last-tags-completion-table
  12522. (org-global-tags-completion-table
  12523. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12524. (org-add-colon-after-tag-completion t)
  12525. (ins (completing-read
  12526. (if prompt (concat prompt ": ") "Tags: ")
  12527. 'org-tags-completion-function nil nil nil
  12528. 'org-tags-history)))
  12529. (setq ins (mapconcat 'identity
  12530. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12531. ":"))
  12532. (when (string-match "\\S-" ins)
  12533. (or (equal (char-before) ?:) (insert ":"))
  12534. (insert ins)
  12535. (or (equal (char-after) ?:) (insert ":")))))
  12536. (char
  12537. (setq org-time-was-given (equal (upcase char) char))
  12538. (setq time (org-read-date (equal (upcase char) "U") t nil
  12539. prompt))
  12540. (org-insert-time-stamp time org-time-was-given
  12541. (member char '("u" "U"))
  12542. nil nil (list org-end-time-was-given)))
  12543. (t
  12544. (insert (org-completing-read
  12545. (concat (if prompt prompt "Enter string")
  12546. (if default (concat " [" default "]"))
  12547. ": ")
  12548. completions nil nil nil histvar default)))))
  12549. (goto-char (point-min))
  12550. (if (re-search-forward "%\\?" nil t)
  12551. (replace-match "")
  12552. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12553. (org-mode)
  12554. (org-set-local 'org-finish-function 'org-remember-finalize))
  12555. (when (save-excursion
  12556. (goto-char (point-min))
  12557. (re-search-forward "%!" nil t))
  12558. (replace-match "")
  12559. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12560. (defun org-remember-finish-immediately ()
  12561. "File remember note immediately.
  12562. This should be run in `post-command-hook' and will remove itself
  12563. from that hook."
  12564. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12565. (when org-finish-function
  12566. (funcall org-finish-function)))
  12567. (defvar org-clock-marker) ; Defined below
  12568. (defun org-remember-finalize ()
  12569. "Finalize the remember process."
  12570. (unless (fboundp 'remember-finalize)
  12571. (defalias 'remember-finalize 'remember-buffer))
  12572. (when (and org-clock-marker
  12573. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12574. ;; FIXME: test this, this is w/o notetaking!
  12575. (let (org-log-note-clock-out) (org-clock-out)))
  12576. (when buffer-file-name
  12577. (save-buffer)
  12578. (setq buffer-file-name nil))
  12579. (remember-finalize))
  12580. ;;;###autoload
  12581. (defun org-remember (&optional goto org-force-remember-template-char)
  12582. "Call `remember'. If this is already a remember buffer, re-apply template.
  12583. If there is an active region, make sure remember uses it as initial content
  12584. of the remember buffer.
  12585. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12586. anything, just go to the file/headline where the selected template usually
  12587. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12588. note stored by remember.
  12589. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12590. associated with a template in `org-remember-templates'."
  12591. (interactive "P")
  12592. (cond
  12593. ((equal goto '(4)) (org-go-to-remember-target))
  12594. ((equal goto '(16)) (org-remember-goto-last-stored))
  12595. (t
  12596. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12597. (progn
  12598. (when (< (length org-remember-templates) 2)
  12599. (error "No other template available"))
  12600. (erase-buffer)
  12601. (let ((annotation (plist-get org-store-link-plist :annotation))
  12602. (initial (plist-get org-store-link-plist :initial)))
  12603. (org-remember-apply-template))
  12604. (message "Press C-c C-c to remember data"))
  12605. (if (org-region-active-p)
  12606. (remember (buffer-substring (point) (mark)))
  12607. (call-interactively 'remember))))))
  12608. (defun org-remember-goto-last-stored ()
  12609. "Go to the location where the last remember note was stored."
  12610. (interactive)
  12611. (bookmark-jump "org-remember-last-stored")
  12612. (message "This is the last note stored by remember"))
  12613. (defun org-go-to-remember-target (&optional template-key)
  12614. "Go to the target location of a remember template.
  12615. The user is queried for the template."
  12616. (interactive)
  12617. (let* ((entry (org-select-remember-template template-key))
  12618. (file (nth 1 entry))
  12619. (heading (nth 2 entry))
  12620. visiting)
  12621. (unless (and file (stringp file) (string-match "\\S-" file))
  12622. (setq file org-default-notes-file))
  12623. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12624. (setq heading org-remember-default-headline))
  12625. (setq visiting (org-find-base-buffer-visiting file))
  12626. (if (not visiting) (find-file-noselect file))
  12627. (switch-to-buffer (or visiting (get-file-buffer file)))
  12628. (widen)
  12629. (goto-char (point-min))
  12630. (if (re-search-forward
  12631. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12632. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12633. nil t)
  12634. (goto-char (match-beginning 0))
  12635. (error "Target headline not found: %s" heading))))
  12636. (defvar org-note-abort nil) ; dynamically scoped
  12637. ;;;###autoload
  12638. (defun org-remember-handler ()
  12639. "Store stuff from remember.el into an org file.
  12640. First prompts for an org file. If the user just presses return, the value
  12641. of `org-default-notes-file' is used.
  12642. Then the command offers the headings tree of the selected file in order to
  12643. file the text at a specific location.
  12644. You can either immediately press RET to get the note appended to the
  12645. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12646. find a better place. Then press RET or <left> or <right> in insert the note.
  12647. Key Cursor position Note gets inserted
  12648. -----------------------------------------------------------------------------
  12649. RET buffer-start as level 1 heading at end of file
  12650. RET on headline as sublevel of the heading at cursor
  12651. RET no heading at cursor position, level taken from context.
  12652. Or use prefix arg to specify level manually.
  12653. <left> on headline as same level, before current heading
  12654. <right> on headline as same level, after current heading
  12655. So the fastest way to store the note is to press RET RET to append it to
  12656. the default file. This way your current train of thought is not
  12657. interrupted, in accordance with the principles of remember.el.
  12658. You can also get the fast execution without prompting by using
  12659. C-u C-c C-c to exit the remember buffer. See also the variable
  12660. `org-remember-store-without-prompt'.
  12661. Before being stored away, the function ensures that the text has a
  12662. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12663. is constructed from the current date and some additional data.
  12664. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12665. also indented so that it starts in the same column as the headline
  12666. \(i.e. after the stars).
  12667. See also the variable `org-reverse-note-order'."
  12668. (goto-char (point-min))
  12669. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12670. (replace-match ""))
  12671. (goto-char (point-max))
  12672. (beginning-of-line 1)
  12673. (while (looking-at "[ \t]*$\\|##.*")
  12674. (delete-region (1- (point)) (point-max))
  12675. (beginning-of-line 1))
  12676. (catch 'quit
  12677. (if org-note-abort (throw 'quit nil))
  12678. (let* ((txt (buffer-substring (point-min) (point-max)))
  12679. (fastp (org-xor (equal current-prefix-arg '(4))
  12680. org-remember-store-without-prompt))
  12681. (file (cond
  12682. (fastp org-default-notes-file)
  12683. ((and (eq org-remember-interactive-interface 'refile)
  12684. org-refile-targets)
  12685. org-default-notes-file)
  12686. ((not (and (equal current-prefix-arg '(16))
  12687. org-remember-previous-location))
  12688. (org-get-org-file))))
  12689. (heading org-remember-default-headline)
  12690. (visiting (and file (org-find-base-buffer-visiting file)))
  12691. (org-startup-folded nil)
  12692. (org-startup-align-all-tables nil)
  12693. (org-goto-start-pos 1)
  12694. spos exitcmd level indent reversed)
  12695. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12696. (setq file (car org-remember-previous-location)
  12697. heading (cdr org-remember-previous-location)
  12698. fastp t))
  12699. (setq current-prefix-arg nil)
  12700. (if (string-match "[ \t\n]+\\'" txt)
  12701. (setq txt (replace-match "" t t txt)))
  12702. ;; Modify text so that it becomes a nice subtree which can be inserted
  12703. ;; into an org tree.
  12704. (let* ((lines (split-string txt "\n"))
  12705. first)
  12706. (setq first (car lines) lines (cdr lines))
  12707. (if (string-match "^\\*+ " first)
  12708. ;; Is already a headline
  12709. (setq indent nil)
  12710. ;; We need to add a headline: Use time and first buffer line
  12711. (setq lines (cons first lines)
  12712. first (concat "* " (current-time-string)
  12713. " (" (remember-buffer-desc) ")")
  12714. indent " "))
  12715. (if (and org-adapt-indentation indent)
  12716. (setq lines (mapcar
  12717. (lambda (x)
  12718. (if (string-match "\\S-" x)
  12719. (concat indent x) x))
  12720. lines)))
  12721. (setq txt (concat first "\n"
  12722. (mapconcat 'identity lines "\n"))))
  12723. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12724. (setq txt (replace-match "\n\n" t t txt))
  12725. (if (string-match "[ \t\n]*\\'" txt)
  12726. (setq txt (replace-match "\n" t t txt))))
  12727. ;; Put the modified text back into the remember buffer, for refile.
  12728. (erase-buffer)
  12729. (insert txt)
  12730. (goto-char (point-min))
  12731. (when (and (eq org-remember-interactive-interface 'refile)
  12732. (not fastp))
  12733. (org-refile nil (or visiting (find-file-noselect file)))
  12734. (throw 'quit t))
  12735. ;; Find the file
  12736. (if (not visiting) (find-file-noselect file))
  12737. (with-current-buffer (or visiting (get-file-buffer file))
  12738. (unless (org-mode-p)
  12739. (error "Target files for remember notes must be in Org-mode"))
  12740. (save-excursion
  12741. (save-restriction
  12742. (widen)
  12743. (and (goto-char (point-min))
  12744. (not (re-search-forward "^\\* " nil t))
  12745. (insert "\n* " (or heading "Notes") "\n"))
  12746. (setq reversed (org-notes-order-reversed-p))
  12747. ;; Find the default location
  12748. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12749. (goto-char (point-min))
  12750. (if (re-search-forward
  12751. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12752. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12753. nil t)
  12754. (setq org-goto-start-pos (match-beginning 0))
  12755. (when fastp
  12756. (goto-char (point-max))
  12757. (unless (bolp) (newline))
  12758. (insert "* " heading "\n")
  12759. (setq org-goto-start-pos (point-at-bol 0)))))
  12760. ;; Ask the User for a location, using the appropriate interface
  12761. (cond
  12762. (fastp (setq spos org-goto-start-pos
  12763. exitcmd 'return))
  12764. ((eq org-remember-interactive-interface 'outline)
  12765. (setq spos (org-get-location (current-buffer)
  12766. org-remember-help)
  12767. exitcmd (cdr spos)
  12768. spos (car spos)))
  12769. ((eq org-remember-interactive-interface 'outline-path-completion)
  12770. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12771. (org-refile-use-outline-path t))
  12772. (setq spos (org-refile-get-location "Heading: ")
  12773. exitcmd 'return
  12774. spos (nth 3 spos))))
  12775. (t (error "this should not hapen")))
  12776. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12777. ; not handle this note
  12778. (goto-char spos)
  12779. (cond ((org-on-heading-p t)
  12780. (org-back-to-heading t)
  12781. (setq level (funcall outline-level))
  12782. (cond
  12783. ((eq exitcmd 'return)
  12784. ;; sublevel of current
  12785. (setq org-remember-previous-location
  12786. (cons (abbreviate-file-name file)
  12787. (org-get-heading 'notags)))
  12788. (if reversed
  12789. (outline-next-heading)
  12790. (org-end-of-subtree t)
  12791. (if (not (bolp))
  12792. (if (looking-at "[ \t]*\n")
  12793. (beginning-of-line 2)
  12794. (end-of-line 1)
  12795. (insert "\n"))))
  12796. (bookmark-set "org-remember-last-stored")
  12797. (org-paste-subtree (org-get-legal-level level 1) txt))
  12798. ((eq exitcmd 'left)
  12799. ;; before current
  12800. (bookmark-set "org-remember-last-stored")
  12801. (org-paste-subtree level txt))
  12802. ((eq exitcmd 'right)
  12803. ;; after current
  12804. (org-end-of-subtree t)
  12805. (bookmark-set "org-remember-last-stored")
  12806. (org-paste-subtree level txt))
  12807. (t (error "This should not happen"))))
  12808. ((and (bobp) (not reversed))
  12809. ;; Put it at the end, one level below level 1
  12810. (save-restriction
  12811. (widen)
  12812. (goto-char (point-max))
  12813. (if (not (bolp)) (newline))
  12814. (bookmark-set "org-remember-last-stored")
  12815. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12816. ((and (bobp) reversed)
  12817. ;; Put it at the start, as level 1
  12818. (save-restriction
  12819. (widen)
  12820. (goto-char (point-min))
  12821. (re-search-forward "^\\*+ " nil t)
  12822. (beginning-of-line 1)
  12823. (bookmark-set "org-remember-last-stored")
  12824. (org-paste-subtree 1 txt)))
  12825. (t
  12826. ;; Put it right there, with automatic level determined by
  12827. ;; org-paste-subtree or from prefix arg
  12828. (bookmark-set "org-remember-last-stored")
  12829. (org-paste-subtree
  12830. (if (numberp current-prefix-arg) current-prefix-arg)
  12831. txt)))
  12832. (when remember-save-after-remembering
  12833. (save-buffer)
  12834. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12835. t) ;; return t to indicate that we took care of this note.
  12836. (defun org-get-org-file ()
  12837. "Read a filename, with default directory `org-directory'."
  12838. (let ((default (or org-default-notes-file remember-data-file)))
  12839. (read-file-name (format "File name [%s]: " default)
  12840. (file-name-as-directory org-directory)
  12841. default)))
  12842. (defun org-notes-order-reversed-p ()
  12843. "Check if the current file should receive notes in reversed order."
  12844. (cond
  12845. ((not org-reverse-note-order) nil)
  12846. ((eq t org-reverse-note-order) t)
  12847. ((not (listp org-reverse-note-order)) nil)
  12848. (t (catch 'exit
  12849. (let ((all org-reverse-note-order)
  12850. entry)
  12851. (while (setq entry (pop all))
  12852. (if (string-match (car entry) buffer-file-name)
  12853. (throw 'exit (cdr entry))))
  12854. nil)))))
  12855. ;;; Refiling
  12856. (defvar org-refile-target-table nil
  12857. "The list of refile targets, created by `org-refile'.")
  12858. (defvar org-agenda-new-buffers nil
  12859. "Buffers created to visit agenda files.")
  12860. (defun org-get-refile-targets (&optional default-buffer)
  12861. "Produce a table with refile targets."
  12862. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12863. targets txt re files f desc descre)
  12864. (with-current-buffer (or default-buffer (current-buffer))
  12865. (while (setq entry (pop entries))
  12866. (setq files (car entry) desc (cdr entry))
  12867. (cond
  12868. ((null files) (setq files (list (current-buffer))))
  12869. ((eq files 'org-agenda-files)
  12870. (setq files (org-agenda-files 'unrestricted)))
  12871. ((and (symbolp files) (fboundp files))
  12872. (setq files (funcall files)))
  12873. ((and (symbolp files) (boundp files))
  12874. (setq files (symbol-value files))))
  12875. (if (stringp files) (setq files (list files)))
  12876. (cond
  12877. ((eq (car desc) :tag)
  12878. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12879. ((eq (car desc) :todo)
  12880. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12881. ((eq (car desc) :regexp)
  12882. (setq descre (cdr desc)))
  12883. ((eq (car desc) :level)
  12884. (setq descre (concat "^\\*\\{" (number-to-string
  12885. (if org-odd-levels-only
  12886. (1- (* 2 (cdr desc)))
  12887. (cdr desc)))
  12888. "\\}[ \t]")))
  12889. ((eq (car desc) :maxlevel)
  12890. (setq descre (concat "^\\*\\{1," (number-to-string
  12891. (if org-odd-levels-only
  12892. (1- (* 2 (cdr desc)))
  12893. (cdr desc)))
  12894. "\\}[ \t]")))
  12895. (t (error "Bad refiling target description %s" desc)))
  12896. (while (setq f (pop files))
  12897. (save-excursion
  12898. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12899. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12900. (save-excursion
  12901. (save-restriction
  12902. (widen)
  12903. (goto-char (point-min))
  12904. (while (re-search-forward descre nil t)
  12905. (goto-char (point-at-bol))
  12906. (when (looking-at org-complex-heading-regexp)
  12907. (setq txt (match-string 4)
  12908. re (concat "^" (regexp-quote
  12909. (buffer-substring (match-beginning 1)
  12910. (match-end 4)))))
  12911. (if (match-end 5) (setq re (concat re "[ \t]+"
  12912. (regexp-quote
  12913. (match-string 5)))))
  12914. (setq re (concat re "[ \t]*$"))
  12915. (when org-refile-use-outline-path
  12916. (setq txt (mapconcat 'identity
  12917. (append
  12918. (if (eq org-refile-use-outline-path 'file)
  12919. (list (file-name-nondirectory
  12920. (buffer-file-name (buffer-base-buffer))))
  12921. (if (eq org-refile-use-outline-path 'full-file-path)
  12922. (list (buffer-file-name (buffer-base-buffer)))))
  12923. (org-get-outline-path)
  12924. (list txt))
  12925. "/")))
  12926. (push (list txt f re (point)) targets))
  12927. (goto-char (point-at-eol))))))))
  12928. (nreverse targets))))
  12929. (defun org-get-outline-path ()
  12930. "Return the outline path to the current entry, as a list."
  12931. (let (rtn)
  12932. (save-excursion
  12933. (while (org-up-heading-safe)
  12934. (when (looking-at org-complex-heading-regexp)
  12935. (push (org-match-string-no-properties 4) rtn)))
  12936. rtn)))
  12937. (defvar org-refile-history nil
  12938. "History for refiling operations.")
  12939. (defun org-refile (&optional goto default-buffer)
  12940. "Move the entry at point to another heading.
  12941. The list of target headings is compiled using the information in
  12942. `org-refile-targets', which see. This list is created upon first use, and
  12943. you can update it by calling this command with a double prefix (`C-u C-u').
  12944. FIXME: Can we find a better way of updating?
  12945. At the target location, the entry is filed as a subitem of the target heading.
  12946. Depending on `org-reverse-note-order', the new subitem will either be the
  12947. first of the last subitem.
  12948. With prefix arg GOTO, the command will only visit the target location,
  12949. not actually move anything.
  12950. With a double prefix `C-c C-c', go to the location where the last refiling
  12951. operation has put the subtree.
  12952. With a double prefix argument, the command can be used to jump to any
  12953. heading in the current buffer."
  12954. (interactive "P")
  12955. (let* ((cbuf (current-buffer))
  12956. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12957. pos it nbuf file re level reversed)
  12958. (if (equal goto '(16))
  12959. (org-refile-goto-last-stored)
  12960. (when (setq it (org-refile-get-location
  12961. (if goto "Goto: " "Refile to: ") default-buffer))
  12962. (setq file (nth 1 it)
  12963. re (nth 2 it)
  12964. pos (nth 3 it))
  12965. (setq nbuf (or (find-buffer-visiting file)
  12966. (find-file-noselect file)))
  12967. (if goto
  12968. (progn
  12969. (switch-to-buffer nbuf)
  12970. (goto-char pos)
  12971. (org-show-context 'org-goto))
  12972. (org-copy-special)
  12973. (save-excursion
  12974. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12975. (find-file-noselect file))))
  12976. (setq reversed (org-notes-order-reversed-p))
  12977. (save-excursion
  12978. (save-restriction
  12979. (widen)
  12980. (goto-char pos)
  12981. (looking-at outline-regexp)
  12982. (setq level (org-get-legal-level (funcall outline-level) 1))
  12983. (goto-char
  12984. (if reversed
  12985. (outline-next-heading)
  12986. (or (save-excursion (outline-get-next-sibling))
  12987. (org-end-of-subtree t t)
  12988. (point-max))))
  12989. (bookmark-set "org-refile-last-stored")
  12990. (org-paste-subtree level))))
  12991. (org-cut-special)
  12992. (message "Entry refiled to \"%s\"" (car it)))))))
  12993. (defun org-refile-goto-last-stored ()
  12994. "Go to the location where the last refile was stored."
  12995. (interactive)
  12996. (bookmark-jump "org-refile-last-stored")
  12997. (message "This is the location of the last refile"))
  12998. (defun org-refile-get-location (&optional prompt default-buffer)
  12999. "Prompt the user for a refile location, using PROMPT."
  13000. (let ((org-refile-targets org-refile-targets)
  13001. (org-refile-use-outline-path org-refile-use-outline-path))
  13002. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  13003. (unless org-refile-target-table
  13004. (error "No refile targets"))
  13005. (let* ((cbuf (current-buffer))
  13006. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  13007. (fname (and filename (file-truename filename)))
  13008. (tbl (mapcar
  13009. (lambda (x)
  13010. (if (not (equal fname (file-truename (nth 1 x))))
  13011. (cons (concat (car x) " (" (file-name-nondirectory
  13012. (nth 1 x)) ")")
  13013. (cdr x))
  13014. x))
  13015. org-refile-target-table))
  13016. (completion-ignore-case t))
  13017. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  13018. tbl)))
  13019. ;;;; Dynamic blocks
  13020. (defun org-find-dblock (name)
  13021. "Find the first dynamic block with name NAME in the buffer.
  13022. If not found, stay at current position and return nil."
  13023. (let (pos)
  13024. (save-excursion
  13025. (goto-char (point-min))
  13026. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  13027. nil t)
  13028. (match-beginning 0))))
  13029. (if pos (goto-char pos))
  13030. pos))
  13031. (defconst org-dblock-start-re
  13032. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  13033. "Matches the startline of a dynamic block, with parameters.")
  13034. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  13035. "Matches the end of a dyhamic block.")
  13036. (defun org-create-dblock (plist)
  13037. "Create a dynamic block section, with parameters taken from PLIST.
  13038. PLIST must containe a :name entry which is used as name of the block."
  13039. (unless (bolp) (newline))
  13040. (let ((name (plist-get plist :name)))
  13041. (insert "#+BEGIN: " name)
  13042. (while plist
  13043. (if (eq (car plist) :name)
  13044. (setq plist (cddr plist))
  13045. (insert " " (prin1-to-string (pop plist)))))
  13046. (insert "\n\n#+END:\n")
  13047. (beginning-of-line -2)))
  13048. (defun org-prepare-dblock ()
  13049. "Prepare dynamic block for refresh.
  13050. This empties the block, puts the cursor at the insert position and returns
  13051. the property list including an extra property :name with the block name."
  13052. (unless (looking-at org-dblock-start-re)
  13053. (error "Not at a dynamic block"))
  13054. (let* ((begdel (1+ (match-end 0)))
  13055. (name (org-no-properties (match-string 1)))
  13056. (params (append (list :name name)
  13057. (read (concat "(" (match-string 3) ")")))))
  13058. (unless (re-search-forward org-dblock-end-re nil t)
  13059. (error "Dynamic block not terminated"))
  13060. (delete-region begdel (match-beginning 0))
  13061. (goto-char begdel)
  13062. (open-line 1)
  13063. params))
  13064. (defun org-map-dblocks (&optional command)
  13065. "Apply COMMAND to all dynamic blocks in the current buffer.
  13066. If COMMAND is not given, use `org-update-dblock'."
  13067. (let ((cmd (or command 'org-update-dblock))
  13068. pos)
  13069. (save-excursion
  13070. (goto-char (point-min))
  13071. (while (re-search-forward org-dblock-start-re nil t)
  13072. (goto-char (setq pos (match-beginning 0)))
  13073. (condition-case nil
  13074. (funcall cmd)
  13075. (error (message "Error during update of dynamic block")))
  13076. (goto-char pos)
  13077. (unless (re-search-forward org-dblock-end-re nil t)
  13078. (error "Dynamic block not terminated"))))))
  13079. (defun org-dblock-update (&optional arg)
  13080. "User command for updating dynamic blocks.
  13081. Update the dynamic block at point. With prefix ARG, update all dynamic
  13082. blocks in the buffer."
  13083. (interactive "P")
  13084. (if arg
  13085. (org-update-all-dblocks)
  13086. (or (looking-at org-dblock-start-re)
  13087. (org-beginning-of-dblock))
  13088. (org-update-dblock)))
  13089. (defun org-update-dblock ()
  13090. "Update the dynamic block at point
  13091. This means to empty the block, parse for parameters and then call
  13092. the correct writing function."
  13093. (save-window-excursion
  13094. (let* ((pos (point))
  13095. (line (org-current-line))
  13096. (params (org-prepare-dblock))
  13097. (name (plist-get params :name))
  13098. (cmd (intern (concat "org-dblock-write:" name))))
  13099. (message "Updating dynamic block `%s' at line %d..." name line)
  13100. (funcall cmd params)
  13101. (message "Updating dynamic block `%s' at line %d...done" name line)
  13102. (goto-char pos))))
  13103. (defun org-beginning-of-dblock ()
  13104. "Find the beginning of the dynamic block at point.
  13105. Error if there is no scuh block at point."
  13106. (let ((pos (point))
  13107. beg)
  13108. (end-of-line 1)
  13109. (if (and (re-search-backward org-dblock-start-re nil t)
  13110. (setq beg (match-beginning 0))
  13111. (re-search-forward org-dblock-end-re nil t)
  13112. (> (match-end 0) pos))
  13113. (goto-char beg)
  13114. (goto-char pos)
  13115. (error "Not in a dynamic block"))))
  13116. (defun org-update-all-dblocks ()
  13117. "Update all dynamic blocks in the buffer.
  13118. This function can be used in a hook."
  13119. (when (org-mode-p)
  13120. (org-map-dblocks 'org-update-dblock)))
  13121. ;;;; Completion
  13122. (defconst org-additional-option-like-keywords
  13123. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  13124. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  13125. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  13126. (defun org-complete (&optional arg)
  13127. "Perform completion on word at point.
  13128. At the beginning of a headline, this completes TODO keywords as given in
  13129. `org-todo-keywords'.
  13130. If the current word is preceded by a backslash, completes the TeX symbols
  13131. that are supported for HTML support.
  13132. If the current word is preceded by \"#+\", completes special words for
  13133. setting file options.
  13134. In the line after \"#+STARTUP:, complete valid keywords.\"
  13135. At all other locations, this simply calls the value of
  13136. `org-completion-fallback-command'."
  13137. (interactive "P")
  13138. (org-without-partial-completion
  13139. (catch 'exit
  13140. (let* ((end (point))
  13141. (beg1 (save-excursion
  13142. (skip-chars-backward (org-re "[:alnum:]_@"))
  13143. (point)))
  13144. (beg (save-excursion
  13145. (skip-chars-backward "a-zA-Z0-9_:$")
  13146. (point)))
  13147. (confirm (lambda (x) (stringp (car x))))
  13148. (searchhead (equal (char-before beg) ?*))
  13149. (tag (and (equal (char-before beg1) ?:)
  13150. (equal (char-after (point-at-bol)) ?*)))
  13151. (prop (and (equal (char-before beg1) ?:)
  13152. (not (equal (char-after (point-at-bol)) ?*))))
  13153. (texp (equal (char-before beg) ?\\))
  13154. (link (equal (char-before beg) ?\[))
  13155. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  13156. beg)
  13157. "#+"))
  13158. (startup (string-match "^#\\+STARTUP:.*"
  13159. (buffer-substring (point-at-bol) (point))))
  13160. (completion-ignore-case opt)
  13161. (type nil)
  13162. (tbl nil)
  13163. (table (cond
  13164. (opt
  13165. (setq type :opt)
  13166. (append
  13167. (mapcar
  13168. (lambda (x)
  13169. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  13170. (cons (match-string 2 x) (match-string 1 x)))
  13171. (org-split-string (org-get-current-options) "\n"))
  13172. (mapcar 'list org-additional-option-like-keywords)))
  13173. (startup
  13174. (setq type :startup)
  13175. org-startup-options)
  13176. (link (append org-link-abbrev-alist-local
  13177. org-link-abbrev-alist))
  13178. (texp
  13179. (setq type :tex)
  13180. org-html-entities)
  13181. ((string-match "\\`\\*+[ \t]+\\'"
  13182. (buffer-substring (point-at-bol) beg))
  13183. (setq type :todo)
  13184. (mapcar 'list org-todo-keywords-1))
  13185. (searchhead
  13186. (setq type :searchhead)
  13187. (save-excursion
  13188. (goto-char (point-min))
  13189. (while (re-search-forward org-todo-line-regexp nil t)
  13190. (push (list
  13191. (org-make-org-heading-search-string
  13192. (match-string 3) t))
  13193. tbl)))
  13194. tbl)
  13195. (tag (setq type :tag beg beg1)
  13196. (or org-tag-alist (org-get-buffer-tags)))
  13197. (prop (setq type :prop beg beg1)
  13198. (mapcar 'list (org-buffer-property-keys nil t t)))
  13199. (t (progn
  13200. (call-interactively org-completion-fallback-command)
  13201. (throw 'exit nil)))))
  13202. (pattern (buffer-substring-no-properties beg end))
  13203. (completion (try-completion pattern table confirm)))
  13204. (cond ((eq completion t)
  13205. (if (not (assoc (upcase pattern) table))
  13206. (message "Already complete")
  13207. (if (equal type :opt)
  13208. (insert (substring (cdr (assoc (upcase pattern) table))
  13209. (length pattern)))
  13210. (if (memq type '(:tag :prop)) (insert ":")))))
  13211. ((null completion)
  13212. (message "Can't find completion for \"%s\"" pattern)
  13213. (ding))
  13214. ((not (string= pattern completion))
  13215. (delete-region beg end)
  13216. (if (string-match " +$" completion)
  13217. (setq completion (replace-match "" t t completion)))
  13218. (insert completion)
  13219. (if (get-buffer-window "*Completions*")
  13220. (delete-window (get-buffer-window "*Completions*")))
  13221. (if (assoc completion table)
  13222. (if (eq type :todo) (insert " ")
  13223. (if (memq type '(:tag :prop)) (insert ":"))))
  13224. (if (and (equal type :opt) (assoc completion table))
  13225. (message "%s" (substitute-command-keys
  13226. "Press \\[org-complete] again to insert example settings"))))
  13227. (t
  13228. (message "Making completion list...")
  13229. (let ((list (sort (all-completions pattern table confirm)
  13230. 'string<)))
  13231. (with-output-to-temp-buffer "*Completions*"
  13232. (condition-case nil
  13233. ;; Protection needed for XEmacs and emacs 21
  13234. (display-completion-list list pattern)
  13235. (error (display-completion-list list)))))
  13236. (message "Making completion list...%s" "done")))))))
  13237. ;;;; TODO, DEADLINE, Comments
  13238. (defun org-toggle-comment ()
  13239. "Change the COMMENT state of an entry."
  13240. (interactive)
  13241. (save-excursion
  13242. (org-back-to-heading)
  13243. (let (case-fold-search)
  13244. (if (looking-at (concat outline-regexp
  13245. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13246. (replace-match "" t t nil 1)
  13247. (if (looking-at outline-regexp)
  13248. (progn
  13249. (goto-char (match-end 0))
  13250. (insert org-comment-string " ")))))))
  13251. (defvar org-last-todo-state-is-todo nil
  13252. "This is non-nil when the last TODO state change led to a TODO state.
  13253. If the last change removed the TODO tag or switched to DONE, then
  13254. this is nil.")
  13255. (defvar org-setting-tags nil) ; dynamically skiped
  13256. ;; FIXME: better place
  13257. (defun org-property-or-variable-value (var &optional inherit)
  13258. "Check if there is a property fixing the value of VAR.
  13259. If yes, return this value. If not, return the current value of the variable."
  13260. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13261. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13262. (read prop)
  13263. (symbol-value var))))
  13264. (defun org-parse-local-options (string var)
  13265. "Parse STRING for startup setting relevant for variable VAR."
  13266. (let ((rtn (symbol-value var))
  13267. e opts)
  13268. (save-match-data
  13269. (if (or (not string) (not (string-match "\\S-" string)))
  13270. rtn
  13271. (setq opts (delq nil (mapcar (lambda (x)
  13272. (setq e (assoc x org-startup-options))
  13273. (if (eq (nth 1 e) var) e nil))
  13274. (org-split-string string "[ \t]+"))))
  13275. (if (not opts)
  13276. rtn
  13277. (setq rtn nil)
  13278. (while (setq e (pop opts))
  13279. (if (not (nth 3 e))
  13280. (setq rtn (nth 2 e))
  13281. (if (not (listp rtn)) (setq rtn nil))
  13282. (push (nth 2 e) rtn)))
  13283. rtn)))))
  13284. (defvar org-blocker-hook nil
  13285. "Hook for functions that are allowed to block a state change.
  13286. Each function gets as its single argument a property list, see
  13287. `org-trigger-hook' for more information about this list.
  13288. If any of the functions in this hook returns nil, the state change
  13289. is blocked.")
  13290. (defvar org-trigger-hook nil
  13291. "Hook for functions that are triggered by a state change.
  13292. Each function gets as its single argument a property list with at least
  13293. the following elements:
  13294. (:type type-of-change :position pos-at-entry-start
  13295. :from old-state :to new-state)
  13296. Depending on the type, more properties may be present.
  13297. This mechanism is currently implemented for:
  13298. TODO state changes
  13299. ------------------
  13300. :type todo-state-change
  13301. :from previous state (keyword as a string), or nil
  13302. :to new state (keyword as a string), or nil")
  13303. (defun org-todo (&optional arg)
  13304. "Change the TODO state of an item.
  13305. The state of an item is given by a keyword at the start of the heading,
  13306. like
  13307. *** TODO Write paper
  13308. *** DONE Call mom
  13309. The different keywords are specified in the variable `org-todo-keywords'.
  13310. By default the available states are \"TODO\" and \"DONE\".
  13311. So for this example: when the item starts with TODO, it is changed to DONE.
  13312. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13313. DONE are present, add TODO at the beginning of the heading.
  13314. With C-u prefix arg, use completion to determine the new state.
  13315. With numeric prefix arg, switch to that state.
  13316. For calling through lisp, arg is also interpreted in the following way:
  13317. 'none -> empty state
  13318. \"\"(empty string) -> switch to empty state
  13319. 'done -> switch to DONE
  13320. 'nextset -> switch to the next set of keywords
  13321. 'previousset -> switch to the previous set of keywords
  13322. \"WAITING\" -> switch to the specified keyword, but only if it
  13323. really is a member of `org-todo-keywords'."
  13324. (interactive "P")
  13325. (save-excursion
  13326. (catch 'exit
  13327. (org-back-to-heading)
  13328. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13329. (or (looking-at (concat " +" org-todo-regexp " *"))
  13330. (looking-at " *"))
  13331. (let* ((match-data (match-data))
  13332. (startpos (point-at-bol))
  13333. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13334. (org-log-done org-log-done)
  13335. (org-log-repeat org-log-repeat)
  13336. (org-todo-log-states org-todo-log-states)
  13337. (this (match-string 1))
  13338. (hl-pos (match-beginning 0))
  13339. (head (org-get-todo-sequence-head this))
  13340. (ass (assoc head org-todo-kwd-alist))
  13341. (interpret (nth 1 ass))
  13342. (done-word (nth 3 ass))
  13343. (final-done-word (nth 4 ass))
  13344. (last-state (or this ""))
  13345. (completion-ignore-case t)
  13346. (member (member this org-todo-keywords-1))
  13347. (tail (cdr member))
  13348. (state (cond
  13349. ((and org-todo-key-trigger
  13350. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13351. (and (not arg) org-use-fast-todo-selection
  13352. (not (eq org-use-fast-todo-selection 'prefix)))))
  13353. ;; Use fast selection
  13354. (org-fast-todo-selection))
  13355. ((and (equal arg '(4))
  13356. (or (not org-use-fast-todo-selection)
  13357. (not org-todo-key-trigger)))
  13358. ;; Read a state with completion
  13359. (completing-read "State: " (mapcar (lambda(x) (list x))
  13360. org-todo-keywords-1)
  13361. nil t))
  13362. ((eq arg 'right)
  13363. (if this
  13364. (if tail (car tail) nil)
  13365. (car org-todo-keywords-1)))
  13366. ((eq arg 'left)
  13367. (if (equal member org-todo-keywords-1)
  13368. nil
  13369. (if this
  13370. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13371. org-todo-keywords-1)
  13372. (org-last org-todo-keywords-1))))
  13373. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13374. (setq arg nil))) ; hack to fall back to cycling
  13375. (arg
  13376. ;; user or caller requests a specific state
  13377. (cond
  13378. ((equal arg "") nil)
  13379. ((eq arg 'none) nil)
  13380. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13381. ((eq arg 'nextset)
  13382. (or (car (cdr (member head org-todo-heads)))
  13383. (car org-todo-heads)))
  13384. ((eq arg 'previousset)
  13385. (let ((org-todo-heads (reverse org-todo-heads)))
  13386. (or (car (cdr (member head org-todo-heads)))
  13387. (car org-todo-heads))))
  13388. ((car (member arg org-todo-keywords-1)))
  13389. ((nth (1- (prefix-numeric-value arg))
  13390. org-todo-keywords-1))))
  13391. ((null member) (or head (car org-todo-keywords-1)))
  13392. ((equal this final-done-word) nil) ;; -> make empty
  13393. ((null tail) nil) ;; -> first entry
  13394. ((eq interpret 'sequence)
  13395. (car tail))
  13396. ((memq interpret '(type priority))
  13397. (if (eq this-command last-command)
  13398. (car tail)
  13399. (if (> (length tail) 0)
  13400. (or done-word (car org-done-keywords))
  13401. nil)))
  13402. (t nil)))
  13403. (next (if state (concat " " state " ") " "))
  13404. (change-plist (list :type 'todo-state-change :from this :to state
  13405. :position startpos))
  13406. dolog now-done-p)
  13407. (when org-blocker-hook
  13408. (unless (save-excursion
  13409. (save-match-data
  13410. (run-hook-with-args-until-failure
  13411. 'org-blocker-hook change-plist)))
  13412. (if (interactive-p)
  13413. (error "TODO state change from %s to %s blocked" this state)
  13414. ;; fail silently
  13415. (message "TODO state change from %s to %s blocked" this state)
  13416. (throw 'exit nil))))
  13417. (store-match-data match-data)
  13418. (replace-match next t t)
  13419. (unless (pos-visible-in-window-p hl-pos)
  13420. (message "TODO state changed to %s" (org-trim next)))
  13421. (unless head
  13422. (setq head (org-get-todo-sequence-head state)
  13423. ass (assoc head org-todo-kwd-alist)
  13424. interpret (nth 1 ass)
  13425. done-word (nth 3 ass)
  13426. final-done-word (nth 4 ass)))
  13427. (when (memq arg '(nextset previousset))
  13428. (message "Keyword-Set %d/%d: %s"
  13429. (- (length org-todo-sets) -1
  13430. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13431. (length org-todo-sets)
  13432. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13433. (setq org-last-todo-state-is-todo
  13434. (not (member state org-done-keywords)))
  13435. (setq now-done-p (and (member state org-done-keywords)
  13436. (not (member this org-done-keywords))))
  13437. (and logging (org-local-logging logging))
  13438. (when (and (or org-todo-log-states org-log-done)
  13439. (not (memq arg '(nextset previousset))))
  13440. ;; we need to look at recording a time and note
  13441. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  13442. (nth 2 (assoc this org-todo-log-states))))
  13443. (when (and state
  13444. (member state org-not-done-keywords)
  13445. (not (member this org-not-done-keywords)))
  13446. ;; This is now a todo state and was not one before
  13447. ;; If there was a CLOSED time stamp, get rid of it.
  13448. (org-add-planning-info nil nil 'closed))
  13449. (when (and now-done-p org-log-done)
  13450. ;; It is now done, and it was not done before
  13451. (org-add-planning-info 'closed (org-current-time))
  13452. (if (and (not dolog) (eq 'note org-log-done))
  13453. (org-add-log-maybe 'done state 'findpos 'note)))
  13454. (when (and state dolog)
  13455. ;; This is a non-nil state, and we need to log it
  13456. (org-add-log-maybe 'state state 'findpos dolog)))
  13457. ;; Fixup tag positioning
  13458. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13459. (run-hooks 'org-after-todo-state-change-hook)
  13460. (if (and arg (not (member state org-done-keywords)))
  13461. (setq head (org-get-todo-sequence-head state)))
  13462. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13463. ;; Do we need to trigger a repeat?
  13464. (when now-done-p (org-auto-repeat-maybe state))
  13465. ;; Fixup cursor location if close to the keyword
  13466. (if (and (outline-on-heading-p)
  13467. (not (bolp))
  13468. (save-excursion (beginning-of-line 1)
  13469. (looking-at org-todo-line-regexp))
  13470. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13471. (progn
  13472. (goto-char (or (match-end 2) (match-end 1)))
  13473. (just-one-space)))
  13474. (when org-trigger-hook
  13475. (save-excursion
  13476. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13477. (defun org-local-logging (value)
  13478. "Get logging settings from a property VALUE."
  13479. (let* (words w a)
  13480. ;; directly set the variables, they are already local.
  13481. (setq org-log-done nil
  13482. org-log-repeat nil
  13483. org-todo-log-states nil)
  13484. (setq words (org-split-string value))
  13485. (while (setq w (pop words))
  13486. (cond
  13487. ((setq a (assoc w org-startup-options))
  13488. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  13489. (set (nth 1 a) (nth 2 a))))
  13490. ((setq a (org-extract-log-state-settings w))
  13491. (and (member (car a) org-todo-keywords-1)
  13492. (push a org-todo-log-states)))))))
  13493. (defun org-get-todo-sequence-head (kwd)
  13494. "Return the head of the TODO sequence to which KWD belongs.
  13495. If KWD is not set, check if there is a text property remembering the
  13496. right sequence."
  13497. (let (p)
  13498. (cond
  13499. ((not kwd)
  13500. (or (get-text-property (point-at-bol) 'org-todo-head)
  13501. (progn
  13502. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13503. nil (point-at-eol)))
  13504. (get-text-property p 'org-todo-head))))
  13505. ((not (member kwd org-todo-keywords-1))
  13506. (car org-todo-keywords-1))
  13507. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13508. (defun org-fast-todo-selection ()
  13509. "Fast TODO keyword selection with single keys.
  13510. Returns the new TODO keyword, or nil if no state change should occur."
  13511. (let* ((fulltable org-todo-key-alist)
  13512. (done-keywords org-done-keywords) ;; needed for the faces.
  13513. (maxlen (apply 'max (mapcar
  13514. (lambda (x)
  13515. (if (stringp (car x)) (string-width (car x)) 0))
  13516. fulltable)))
  13517. (expert nil)
  13518. (fwidth (+ maxlen 3 1 3))
  13519. (ncol (/ (- (window-width) 4) fwidth))
  13520. tg cnt e c tbl
  13521. groups ingroup)
  13522. (save-window-excursion
  13523. (if expert
  13524. (set-buffer (get-buffer-create " *Org todo*"))
  13525. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13526. (erase-buffer)
  13527. (org-set-local 'org-done-keywords done-keywords)
  13528. (setq tbl fulltable cnt 0)
  13529. (while (setq e (pop tbl))
  13530. (cond
  13531. ((equal e '(:startgroup))
  13532. (push '() groups) (setq ingroup t)
  13533. (when (not (= cnt 0))
  13534. (setq cnt 0)
  13535. (insert "\n"))
  13536. (insert "{ "))
  13537. ((equal e '(:endgroup))
  13538. (setq ingroup nil cnt 0)
  13539. (insert "}\n"))
  13540. (t
  13541. (setq tg (car e) c (cdr e))
  13542. (if ingroup (push tg (car groups)))
  13543. (setq tg (org-add-props tg nil 'face
  13544. (org-get-todo-face tg)))
  13545. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13546. (insert "[" c "] " tg (make-string
  13547. (- fwidth 4 (length tg)) ?\ ))
  13548. (when (= (setq cnt (1+ cnt)) ncol)
  13549. (insert "\n")
  13550. (if ingroup (insert " "))
  13551. (setq cnt 0)))))
  13552. (insert "\n")
  13553. (goto-char (point-min))
  13554. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13555. (fit-window-to-buffer))
  13556. (message "[a-z..]:Set [SPC]:clear")
  13557. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13558. (cond
  13559. ((or (= c ?\C-g)
  13560. (and (= c ?q) (not (rassoc c fulltable))))
  13561. (setq quit-flag t))
  13562. ((= c ?\ ) nil)
  13563. ((setq e (rassoc c fulltable) tg (car e))
  13564. tg)
  13565. (t (setq quit-flag t))))))
  13566. (defun org-get-repeat ()
  13567. "Check if tere is a deadline/schedule with repeater in this entry."
  13568. (save-match-data
  13569. (save-excursion
  13570. (org-back-to-heading t)
  13571. (if (re-search-forward
  13572. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13573. (match-string 1)))))
  13574. (defvar org-last-changed-timestamp)
  13575. (defvar org-log-post-message)
  13576. (defvar org-log-note-purpose)
  13577. (defun org-auto-repeat-maybe (done-word)
  13578. "Check if the current headline contains a repeated deadline/schedule.
  13579. If yes, set TODO state back to what it was and change the base date
  13580. of repeating deadline/scheduled time stamps to new date.
  13581. This function is run automatically after each state change to a DONE state."
  13582. ;; last-state is dynamically scoped into this function
  13583. (let* ((repeat (org-get-repeat))
  13584. (aa (assoc last-state org-todo-kwd-alist))
  13585. (interpret (nth 1 aa))
  13586. (head (nth 2 aa))
  13587. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13588. (msg "Entry repeats: ")
  13589. (org-log-done nil)
  13590. (org-todo-log-states nil)
  13591. re type n what ts)
  13592. (when repeat
  13593. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  13594. (org-todo (if (eq interpret 'type) last-state head))
  13595. (when (and org-log-repeat
  13596. (or (not (memq 'org-add-log-note
  13597. (default-value 'post-command-hook)))
  13598. (eq org-log-note-purpose 'done)))
  13599. ;; Make sure a note is taken;
  13600. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  13601. 'findpos org-log-repeat))
  13602. (org-back-to-heading t)
  13603. (org-add-planning-info nil nil 'closed)
  13604. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13605. org-deadline-time-regexp "\\)\\|\\("
  13606. org-ts-regexp "\\)"))
  13607. (while (re-search-forward
  13608. re (save-excursion (outline-next-heading) (point)) t)
  13609. (setq type (if (match-end 1) org-scheduled-string
  13610. (if (match-end 3) org-deadline-string "Plain:"))
  13611. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13612. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13613. (setq n (string-to-number (match-string 1 ts))
  13614. what (match-string 2 ts))
  13615. (if (equal what "w") (setq n (* n 7) what "d"))
  13616. (org-timestamp-change n (cdr (assoc what whata)))
  13617. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13618. (setq org-log-post-message msg)
  13619. (message "%s" msg))))
  13620. (defun org-show-todo-tree (arg)
  13621. "Make a compact tree which shows all headlines marked with TODO.
  13622. The tree will show the lines where the regexp matches, and all higher
  13623. headlines above the match.
  13624. With a \\[universal-argument] prefix, also show the DONE entries.
  13625. With a numeric prefix N, construct a sparse tree for the Nth element
  13626. of `org-todo-keywords-1'."
  13627. (interactive "P")
  13628. (let ((case-fold-search nil)
  13629. (kwd-re
  13630. (cond ((null arg) org-not-done-regexp)
  13631. ((equal arg '(4))
  13632. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13633. (mapcar 'list org-todo-keywords-1))))
  13634. (concat "\\("
  13635. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13636. "\\)\\>")))
  13637. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13638. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13639. org-todo-keywords-1)))
  13640. (t (error "Invalid prefix argument: %s" arg)))))
  13641. (message "%d TODO entries found"
  13642. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13643. (defun org-deadline (&optional remove)
  13644. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13645. With argument REMOVE, remove any deadline from the item."
  13646. (interactive "P")
  13647. (if remove
  13648. (progn
  13649. (org-remove-timestamp-with-keyword org-deadline-string)
  13650. (message "Item no longer has a deadline."))
  13651. (org-add-planning-info 'deadline nil 'closed)))
  13652. (defun org-schedule (&optional remove)
  13653. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13654. With argument REMOVE, remove any scheduling date from the item."
  13655. (interactive "P")
  13656. (if remove
  13657. (progn
  13658. (org-remove-timestamp-with-keyword org-scheduled-string)
  13659. (message "Item is no longer scheduled."))
  13660. (org-add-planning-info 'scheduled nil 'closed)))
  13661. (defun org-remove-timestamp-with-keyword (keyword)
  13662. "Remove all time stamps with KEYWORD in the current entry."
  13663. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13664. beg)
  13665. (save-excursion
  13666. (org-back-to-heading t)
  13667. (setq beg (point))
  13668. (org-end-of-subtree t t)
  13669. (while (re-search-backward re beg t)
  13670. (replace-match "")
  13671. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13672. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13673. (defun org-add-planning-info (what &optional time &rest remove)
  13674. "Insert new timestamp with keyword in the line directly after the headline.
  13675. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13676. If non is given, the user is prompted for a date.
  13677. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13678. be removed."
  13679. (interactive)
  13680. (let (org-time-was-given org-end-time-was-given)
  13681. (when what (setq time (or time (org-read-date nil 'to-time))))
  13682. (when (and org-insert-labeled-timestamps-at-point
  13683. (member what '(scheduled deadline)))
  13684. (insert
  13685. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13686. (org-insert-time-stamp time org-time-was-given
  13687. nil nil nil (list org-end-time-was-given))
  13688. (setq what nil))
  13689. (save-excursion
  13690. (save-restriction
  13691. (let (col list elt ts buffer-invisibility-spec)
  13692. (org-back-to-heading t)
  13693. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13694. (goto-char (match-end 1))
  13695. (setq col (current-column))
  13696. (goto-char (match-end 0))
  13697. (if (eobp) (insert "\n") (forward-char 1))
  13698. (if (and (not (looking-at outline-regexp))
  13699. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13700. "[^\r\n]*"))
  13701. (not (equal (match-string 1) org-clock-string)))
  13702. (narrow-to-region (match-beginning 0) (match-end 0))
  13703. (insert-before-markers "\n")
  13704. (backward-char 1)
  13705. (narrow-to-region (point) (point))
  13706. (indent-to-column col))
  13707. ;; Check if we have to remove something.
  13708. (setq list (cons what remove))
  13709. (while list
  13710. (setq elt (pop list))
  13711. (goto-char (point-min))
  13712. (when (or (and (eq elt 'scheduled)
  13713. (re-search-forward org-scheduled-time-regexp nil t))
  13714. (and (eq elt 'deadline)
  13715. (re-search-forward org-deadline-time-regexp nil t))
  13716. (and (eq elt 'closed)
  13717. (re-search-forward org-closed-time-regexp nil t)))
  13718. (replace-match "")
  13719. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13720. (if (looking-at " +") (replace-match ""))))
  13721. (goto-char (point-max))
  13722. (when what
  13723. (insert
  13724. (if (not (equal (char-before) ?\ )) " " "")
  13725. (cond ((eq what 'scheduled) org-scheduled-string)
  13726. ((eq what 'deadline) org-deadline-string)
  13727. ((eq what 'closed) org-closed-string))
  13728. " ")
  13729. (setq ts (org-insert-time-stamp
  13730. time
  13731. (or org-time-was-given
  13732. (and (eq what 'closed) org-log-done-with-time))
  13733. (eq what 'closed)
  13734. nil nil (list org-end-time-was-given)))
  13735. (end-of-line 1))
  13736. (goto-char (point-min))
  13737. (widen)
  13738. (if (looking-at "[ \t]+\r?\n")
  13739. (replace-match ""))
  13740. ts)))))
  13741. (defvar org-log-note-marker (make-marker))
  13742. (defvar org-log-note-purpose nil)
  13743. (defvar org-log-note-state nil)
  13744. (defvar org-log-note-how nil)
  13745. (defvar org-log-note-window-configuration nil)
  13746. (defvar org-log-note-return-to (make-marker))
  13747. (defvar org-log-post-message nil
  13748. "Message to be displayed after a log note has been stored.
  13749. The auto-repeater uses this.")
  13750. (defun org-add-log-maybe (&optional purpose state findpos how)
  13751. "Set up the post command hook to take a note.
  13752. If this is about to TODO state change, the new state is expected in STATE.
  13753. When FINDPOS is non-nil, find the correct position for the note in
  13754. the current entry. If not, assume that it can be inserted at point."
  13755. (save-excursion
  13756. (when findpos
  13757. (org-back-to-heading t)
  13758. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13759. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13760. "[^\r\n]*\\)?"))
  13761. (goto-char (match-end 0))
  13762. (unless org-log-states-order-reversed
  13763. (and (= (char-after) ?\n) (forward-char 1))
  13764. (org-skip-over-state-notes)
  13765. (skip-chars-backward " \t\n\r")))
  13766. (move-marker org-log-note-marker (point))
  13767. (setq org-log-note-purpose purpose
  13768. org-log-note-state state
  13769. org-log-note-how how)
  13770. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  13771. (defun org-skip-over-state-notes ()
  13772. "Skip past the list of State notes in an entry."
  13773. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13774. (while (looking-at "[ \t]*- State")
  13775. (condition-case nil
  13776. (org-next-item)
  13777. (error (org-end-of-item)))))
  13778. (defun org-add-log-note (&optional purpose)
  13779. "Pop up a window for taking a note, and add this note later at point."
  13780. (remove-hook 'post-command-hook 'org-add-log-note)
  13781. (setq org-log-note-window-configuration (current-window-configuration))
  13782. (delete-other-windows)
  13783. (move-marker org-log-note-return-to (point))
  13784. (switch-to-buffer (marker-buffer org-log-note-marker))
  13785. (goto-char org-log-note-marker)
  13786. (org-switch-to-buffer-other-window "*Org Note*")
  13787. (erase-buffer)
  13788. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  13789. (org-store-log-note)
  13790. (let ((org-inhibit-startup t)) (org-mode))
  13791. (insert (format "# Insert note for %s.
  13792. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13793. (cond
  13794. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13795. ((eq org-log-note-purpose 'done) "closed todo item")
  13796. ((eq org-log-note-purpose 'state)
  13797. (format "state change to \"%s\"" org-log-note-state))
  13798. (t (error "This should not happen")))))
  13799. (org-set-local 'org-finish-function 'org-store-log-note)))
  13800. (defun org-store-log-note ()
  13801. "Finish taking a log note, and insert it to where it belongs."
  13802. (let ((txt (buffer-string))
  13803. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13804. lines ind)
  13805. (kill-buffer (current-buffer))
  13806. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13807. (setq txt (replace-match "" t t txt)))
  13808. (if (string-match "\\s-+\\'" txt)
  13809. (setq txt (replace-match "" t t txt)))
  13810. (setq lines (org-split-string txt "\n"))
  13811. (when (and note (string-match "\\S-" note))
  13812. (setq note
  13813. (org-replace-escapes
  13814. note
  13815. (list (cons "%u" (user-login-name))
  13816. (cons "%U" user-full-name)
  13817. (cons "%t" (format-time-string
  13818. (org-time-stamp-format 'long 'inactive)
  13819. (current-time)))
  13820. (cons "%s" (if org-log-note-state
  13821. (concat "\"" org-log-note-state "\"")
  13822. "")))))
  13823. (if lines (setq note (concat note " \\\\")))
  13824. (push note lines))
  13825. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13826. (when lines
  13827. (save-excursion
  13828. (set-buffer (marker-buffer org-log-note-marker))
  13829. (save-excursion
  13830. (goto-char org-log-note-marker)
  13831. (move-marker org-log-note-marker nil)
  13832. (end-of-line 1)
  13833. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13834. (indent-relative nil)
  13835. (insert "- " (pop lines))
  13836. (org-indent-line-function)
  13837. (beginning-of-line 1)
  13838. (looking-at "[ \t]*")
  13839. (setq ind (concat (match-string 0) " "))
  13840. (end-of-line 1)
  13841. (while lines (insert "\n" ind (pop lines)))))))
  13842. (set-window-configuration org-log-note-window-configuration)
  13843. (with-current-buffer (marker-buffer org-log-note-return-to)
  13844. (goto-char org-log-note-return-to))
  13845. (move-marker org-log-note-return-to nil)
  13846. (and org-log-post-message (message "%s" org-log-post-message)))
  13847. ;; FIXME: what else would be useful?
  13848. ;; - priority
  13849. ;; - date
  13850. (defun org-sparse-tree (&optional arg)
  13851. "Create a sparse tree, prompt for the details.
  13852. This command can create sparse trees. You first need to select the type
  13853. of match used to create the tree:
  13854. t Show entries with a specific TODO keyword.
  13855. T Show entries selected by a tags match.
  13856. p Enter a property name and its value (both with completion on existing
  13857. names/values) and show entries with that property.
  13858. r Show entries matching a regular expression
  13859. d Show deadlines due within `org-deadline-warning-days'."
  13860. (interactive "P")
  13861. (let (ans kwd value)
  13862. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13863. (setq ans (read-char-exclusive))
  13864. (cond
  13865. ((equal ans ?d)
  13866. (call-interactively 'org-check-deadlines))
  13867. ((equal ans ?b)
  13868. (call-interactively 'org-check-before-date))
  13869. ((equal ans ?t)
  13870. (org-show-todo-tree '(4)))
  13871. ((equal ans ?T)
  13872. (call-interactively 'org-tags-sparse-tree))
  13873. ((member ans '(?p ?P))
  13874. (setq kwd (completing-read "Property: "
  13875. (mapcar 'list (org-buffer-property-keys))))
  13876. (setq value (completing-read "Value: "
  13877. (mapcar 'list (org-property-values kwd))))
  13878. (unless (string-match "\\`{.*}\\'" value)
  13879. (setq value (concat "\"" value "\"")))
  13880. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13881. ((member ans '(?r ?R ?/))
  13882. (call-interactively 'org-occur))
  13883. (t (error "No such sparse tree command \"%c\"" ans)))))
  13884. (defvar org-occur-highlights nil)
  13885. (make-variable-buffer-local 'org-occur-highlights)
  13886. (defun org-occur (regexp &optional keep-previous callback)
  13887. "Make a compact tree which shows all matches of REGEXP.
  13888. The tree will show the lines where the regexp matches, and all higher
  13889. headlines above the match. It will also show the heading after the match,
  13890. to make sure editing the matching entry is easy.
  13891. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13892. call to `org-occur' will be kept, to allow stacking of calls to this
  13893. command.
  13894. If CALLBACK is non-nil, it is a function which is called to confirm
  13895. that the match should indeed be shown."
  13896. (interactive "sRegexp: \nP")
  13897. (or keep-previous (org-remove-occur-highlights nil nil t))
  13898. (let ((cnt 0))
  13899. (save-excursion
  13900. (goto-char (point-min))
  13901. (if (or (not keep-previous) ; do not want to keep
  13902. (not org-occur-highlights)) ; no previous matches
  13903. ;; hide everything
  13904. (org-overview))
  13905. (while (re-search-forward regexp nil t)
  13906. (when (or (not callback)
  13907. (save-match-data (funcall callback)))
  13908. (setq cnt (1+ cnt))
  13909. (when org-highlight-sparse-tree-matches
  13910. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13911. (org-show-context 'occur-tree))))
  13912. (when org-remove-highlights-with-change
  13913. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13914. nil 'local))
  13915. (unless org-sparse-tree-open-archived-trees
  13916. (org-hide-archived-subtrees (point-min) (point-max)))
  13917. (run-hooks 'org-occur-hook)
  13918. (if (interactive-p)
  13919. (message "%d match(es) for regexp %s" cnt regexp))
  13920. cnt))
  13921. (defun org-show-context (&optional key)
  13922. "Make sure point and context and visible.
  13923. How much context is shown depends upon the variables
  13924. `org-show-hierarchy-above', `org-show-following-heading'. and
  13925. `org-show-siblings'."
  13926. (let ((heading-p (org-on-heading-p t))
  13927. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13928. (following-p (org-get-alist-option org-show-following-heading key))
  13929. (entry-p (org-get-alist-option org-show-entry-below key))
  13930. (siblings-p (org-get-alist-option org-show-siblings key)))
  13931. (catch 'exit
  13932. ;; Show heading or entry text
  13933. (if (and heading-p (not entry-p))
  13934. (org-flag-heading nil) ; only show the heading
  13935. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13936. (org-show-hidden-entry))) ; show entire entry
  13937. (when following-p
  13938. ;; Show next sibling, or heading below text
  13939. (save-excursion
  13940. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13941. (org-flag-heading nil))))
  13942. (when siblings-p (org-show-siblings))
  13943. (when hierarchy-p
  13944. ;; show all higher headings, possibly with siblings
  13945. (save-excursion
  13946. (while (and (condition-case nil
  13947. (progn (org-up-heading-all 1) t)
  13948. (error nil))
  13949. (not (bobp)))
  13950. (org-flag-heading nil)
  13951. (when siblings-p (org-show-siblings))))))))
  13952. (defun org-reveal (&optional siblings)
  13953. "Show current entry, hierarchy above it, and the following headline.
  13954. This can be used to show a consistent set of context around locations
  13955. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13956. not t for the search context.
  13957. With optional argument SIBLINGS, on each level of the hierarchy all
  13958. siblings are shown. This repairs the tree structure to what it would
  13959. look like when opened with hierarchical calls to `org-cycle'."
  13960. (interactive "P")
  13961. (let ((org-show-hierarchy-above t)
  13962. (org-show-following-heading t)
  13963. (org-show-siblings (if siblings t org-show-siblings)))
  13964. (org-show-context nil)))
  13965. (defun org-highlight-new-match (beg end)
  13966. "Highlight from BEG to END and mark the highlight is an occur headline."
  13967. (let ((ov (org-make-overlay beg end)))
  13968. (org-overlay-put ov 'face 'secondary-selection)
  13969. (push ov org-occur-highlights)))
  13970. (defun org-remove-occur-highlights (&optional beg end noremove)
  13971. "Remove the occur highlights from the buffer.
  13972. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13973. from the `before-change-functions' in the current buffer."
  13974. (interactive)
  13975. (unless org-inhibit-highlight-removal
  13976. (mapc 'org-delete-overlay org-occur-highlights)
  13977. (setq org-occur-highlights nil)
  13978. (unless noremove
  13979. (remove-hook 'before-change-functions
  13980. 'org-remove-occur-highlights 'local))))
  13981. ;;;; Priorities
  13982. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13983. "Regular expression matching the priority indicator.")
  13984. (defvar org-remove-priority-next-time nil)
  13985. (defun org-priority-up ()
  13986. "Increase the priority of the current item."
  13987. (interactive)
  13988. (org-priority 'up))
  13989. (defun org-priority-down ()
  13990. "Decrease the priority of the current item."
  13991. (interactive)
  13992. (org-priority 'down))
  13993. (defun org-priority (&optional action)
  13994. "Change the priority of an item by ARG.
  13995. ACTION can be `set', `up', `down', or a character."
  13996. (interactive)
  13997. (setq action (or action 'set))
  13998. (let (current new news have remove)
  13999. (save-excursion
  14000. (org-back-to-heading)
  14001. (if (looking-at org-priority-regexp)
  14002. (setq current (string-to-char (match-string 2))
  14003. have t)
  14004. (setq current org-default-priority))
  14005. (cond
  14006. ((or (eq action 'set) (integerp action))
  14007. (if (integerp action)
  14008. (setq new action)
  14009. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  14010. (setq new (read-char-exclusive)))
  14011. (if (and (= (upcase org-highest-priority) org-highest-priority)
  14012. (= (upcase org-lowest-priority) org-lowest-priority))
  14013. (setq new (upcase new)))
  14014. (cond ((equal new ?\ ) (setq remove t))
  14015. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  14016. (error "Priority must be between `%c' and `%c'"
  14017. org-highest-priority org-lowest-priority))))
  14018. ((eq action 'up)
  14019. (if (and (not have) (eq last-command this-command))
  14020. (setq new org-lowest-priority)
  14021. (setq new (if (and org-priority-start-cycle-with-default (not have))
  14022. org-default-priority (1- current)))))
  14023. ((eq action 'down)
  14024. (if (and (not have) (eq last-command this-command))
  14025. (setq new org-highest-priority)
  14026. (setq new (if (and org-priority-start-cycle-with-default (not have))
  14027. org-default-priority (1+ current)))))
  14028. (t (error "Invalid action")))
  14029. (if (or (< (upcase new) org-highest-priority)
  14030. (> (upcase new) org-lowest-priority))
  14031. (setq remove t))
  14032. (setq news (format "%c" new))
  14033. (if have
  14034. (if remove
  14035. (replace-match "" t t nil 1)
  14036. (replace-match news t t nil 2))
  14037. (if remove
  14038. (error "No priority cookie found in line")
  14039. (looking-at org-todo-line-regexp)
  14040. (if (match-end 2)
  14041. (progn
  14042. (goto-char (match-end 2))
  14043. (insert " [#" news "]"))
  14044. (goto-char (match-beginning 3))
  14045. (insert "[#" news "] ")))))
  14046. (org-preserve-lc (org-set-tags nil 'align))
  14047. (if remove
  14048. (message "Priority removed")
  14049. (message "Priority of current item set to %s" news))))
  14050. (defun org-get-priority (s)
  14051. "Find priority cookie and return priority."
  14052. (save-match-data
  14053. (if (not (string-match org-priority-regexp s))
  14054. (* 1000 (- org-lowest-priority org-default-priority))
  14055. (* 1000 (- org-lowest-priority
  14056. (string-to-char (match-string 2 s)))))))
  14057. ;;;; Tags
  14058. (defun org-scan-tags (action matcher &optional todo-only)
  14059. "Scan headline tags with inheritance and produce output ACTION.
  14060. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  14061. evaluated, testing if a given set of tags qualifies a headline for
  14062. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  14063. are included in the output."
  14064. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  14065. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  14066. (org-re
  14067. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  14068. (props (list 'face nil
  14069. 'done-face 'org-done
  14070. 'undone-face nil
  14071. 'mouse-face 'highlight
  14072. 'org-not-done-regexp org-not-done-regexp
  14073. 'org-todo-regexp org-todo-regexp
  14074. 'keymap org-agenda-keymap
  14075. 'help-echo
  14076. (format "mouse-2 or RET jump to org file %s"
  14077. (abbreviate-file-name
  14078. (or (buffer-file-name (buffer-base-buffer))
  14079. (buffer-name (buffer-base-buffer)))))))
  14080. (case-fold-search nil)
  14081. lspos
  14082. tags tags-list tags-alist (llast 0) rtn level category i txt
  14083. todo marker entry priority)
  14084. (save-excursion
  14085. (goto-char (point-min))
  14086. (when (eq action 'sparse-tree)
  14087. (org-overview)
  14088. (org-remove-occur-highlights))
  14089. (while (re-search-forward re nil t)
  14090. (catch :skip
  14091. (setq todo (if (match-end 1) (match-string 2))
  14092. tags (if (match-end 4) (match-string 4)))
  14093. (goto-char (setq lspos (1+ (match-beginning 0))))
  14094. (setq level (org-reduced-level (funcall outline-level))
  14095. category (org-get-category))
  14096. (setq i llast llast level)
  14097. ;; remove tag lists from same and sublevels
  14098. (while (>= i level)
  14099. (when (setq entry (assoc i tags-alist))
  14100. (setq tags-alist (delete entry tags-alist)))
  14101. (setq i (1- i)))
  14102. ;; add the nex tags
  14103. (when tags
  14104. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  14105. tags-alist
  14106. (cons (cons level tags) tags-alist)))
  14107. ;; compile tags for current headline
  14108. (setq tags-list
  14109. (if org-use-tag-inheritance
  14110. (apply 'append (mapcar 'cdr tags-alist))
  14111. tags))
  14112. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  14113. (eval matcher)
  14114. (or (not org-agenda-skip-archived-trees)
  14115. (not (member org-archive-tag tags-list))))
  14116. (and (eq action 'agenda) (org-agenda-skip))
  14117. ;; list this headline
  14118. (if (eq action 'sparse-tree)
  14119. (progn
  14120. (and org-highlight-sparse-tree-matches
  14121. (org-get-heading) (match-end 0)
  14122. (org-highlight-new-match
  14123. (match-beginning 0) (match-beginning 1)))
  14124. (org-show-context 'tags-tree))
  14125. (setq txt (org-format-agenda-item
  14126. ""
  14127. (concat
  14128. (if org-tags-match-list-sublevels
  14129. (make-string (1- level) ?.) "")
  14130. (org-get-heading))
  14131. category tags-list)
  14132. priority (org-get-priority txt))
  14133. (goto-char lspos)
  14134. (setq marker (org-agenda-new-marker))
  14135. (org-add-props txt props
  14136. 'org-marker marker 'org-hd-marker marker 'org-category category
  14137. 'priority priority 'type "tagsmatch")
  14138. (push txt rtn))
  14139. ;; if we are to skip sublevels, jump to end of subtree
  14140. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  14141. (when (and (eq action 'sparse-tree)
  14142. (not org-sparse-tree-open-archived-trees))
  14143. (org-hide-archived-subtrees (point-min) (point-max)))
  14144. (nreverse rtn)))
  14145. (defvar todo-only) ;; dynamically scoped
  14146. (defun org-tags-sparse-tree (&optional todo-only match)
  14147. "Create a sparse tree according to tags string MATCH.
  14148. MATCH can contain positive and negative selection of tags, like
  14149. \"+WORK+URGENT-WITHBOSS\".
  14150. If optional argument TODO_ONLY is non-nil, only select lines that are
  14151. also TODO lines."
  14152. (interactive "P")
  14153. (org-prepare-agenda-buffers (list (current-buffer)))
  14154. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  14155. (defvar org-cached-props nil)
  14156. (defun org-cached-entry-get (pom property)
  14157. (if (or (eq t org-use-property-inheritance)
  14158. (member property org-use-property-inheritance))
  14159. ;; Caching is not possible, check it directly
  14160. (org-entry-get pom property 'inherit)
  14161. ;; Get all properties, so that we can do complicated checks easily
  14162. (cdr (assoc property (or org-cached-props
  14163. (setq org-cached-props
  14164. (org-entry-properties pom)))))))
  14165. (defun org-global-tags-completion-table (&optional files)
  14166. "Return the list of all tags in all agenda buffer/files."
  14167. (save-excursion
  14168. (org-uniquify
  14169. (delq nil
  14170. (apply 'append
  14171. (mapcar
  14172. (lambda (file)
  14173. (set-buffer (find-file-noselect file))
  14174. (append (org-get-buffer-tags)
  14175. (mapcar (lambda (x) (if (stringp (car-safe x))
  14176. (list (car-safe x)) nil))
  14177. org-tag-alist)))
  14178. (if (and files (car files))
  14179. files
  14180. (org-agenda-files))))))))
  14181. (defun org-make-tags-matcher (match)
  14182. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14183. ;; todo-only is scoped dynamically into this function, and the function
  14184. ;; may change it it the matcher asksk for it.
  14185. (unless match
  14186. ;; Get a new match request, with completion
  14187. (let ((org-last-tags-completion-table
  14188. (org-global-tags-completion-table)))
  14189. (setq match (completing-read
  14190. "Match: " 'org-tags-completion-function nil nil nil
  14191. 'org-tags-history))))
  14192. ;; Parse the string and create a lisp form
  14193. (let ((match0 match)
  14194. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  14195. minus tag mm
  14196. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14197. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14198. (if (string-match "/+" match)
  14199. ;; match contains also a todo-matching request
  14200. (progn
  14201. (setq tagsmatch (substring match 0 (match-beginning 0))
  14202. todomatch (substring match (match-end 0)))
  14203. (if (string-match "^!" todomatch)
  14204. (setq todo-only t todomatch (substring todomatch 1)))
  14205. (if (string-match "^\\s-*$" todomatch)
  14206. (setq todomatch nil)))
  14207. ;; only matching tags
  14208. (setq tagsmatch match todomatch nil))
  14209. ;; Make the tags matcher
  14210. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14211. (setq tagsmatcher t)
  14212. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14213. (while (setq term (pop orterms))
  14214. (while (and (equal (substring term -1) "\\") orterms)
  14215. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14216. (while (string-match re term)
  14217. (setq minus (and (match-end 1)
  14218. (equal (match-string 1 term) "-"))
  14219. tag (match-string 2 term)
  14220. re-p (equal (string-to-char tag) ?{)
  14221. level-p (match-end 3)
  14222. prop-p (match-end 4)
  14223. mm (cond
  14224. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14225. (level-p `(= level ,(string-to-number
  14226. (match-string 3 term))))
  14227. (prop-p
  14228. (setq pn (match-string 4 term)
  14229. pv (match-string 5 term)
  14230. cat-p (equal pn "CATEGORY")
  14231. re-p (equal (string-to-char pv) ?{)
  14232. pv (substring pv 1 -1))
  14233. (if (equal pn "CATEGORY")
  14234. (setq gv '(get-text-property (point) 'org-category))
  14235. (setq gv `(org-cached-entry-get nil ,pn)))
  14236. (if re-p
  14237. `(string-match ,pv (or ,gv ""))
  14238. `(equal ,pv (or ,gv ""))))
  14239. (t `(member ,(downcase tag) tags-list)))
  14240. mm (if minus (list 'not mm) mm)
  14241. term (substring term (match-end 0)))
  14242. (push mm tagsmatcher))
  14243. (push (if (> (length tagsmatcher) 1)
  14244. (cons 'and tagsmatcher)
  14245. (car tagsmatcher))
  14246. orlist)
  14247. (setq tagsmatcher nil))
  14248. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14249. (setq tagsmatcher
  14250. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14251. ;; Make the todo matcher
  14252. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14253. (setq todomatcher t)
  14254. (setq orterms (org-split-string todomatch "|") orlist nil)
  14255. (while (setq term (pop orterms))
  14256. (while (string-match re term)
  14257. (setq minus (and (match-end 1)
  14258. (equal (match-string 1 term) "-"))
  14259. kwd (match-string 2 term)
  14260. re-p (equal (string-to-char kwd) ?{)
  14261. term (substring term (match-end 0))
  14262. mm (if re-p
  14263. `(string-match ,(substring kwd 1 -1) todo)
  14264. (list 'equal 'todo kwd))
  14265. mm (if minus (list 'not mm) mm))
  14266. (push mm todomatcher))
  14267. (push (if (> (length todomatcher) 1)
  14268. (cons 'and todomatcher)
  14269. (car todomatcher))
  14270. orlist)
  14271. (setq todomatcher nil))
  14272. (setq todomatcher (if (> (length orlist) 1)
  14273. (cons 'or orlist) (car orlist))))
  14274. ;; Return the string and lisp forms of the matcher
  14275. (setq matcher (if todomatcher
  14276. (list 'and tagsmatcher todomatcher)
  14277. tagsmatcher))
  14278. (cons match0 matcher)))
  14279. (defun org-match-any-p (re list)
  14280. "Does re match any element of list?"
  14281. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14282. (delq nil list))
  14283. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14284. (defvar org-tags-overlay (org-make-overlay 1 1))
  14285. (org-detach-overlay org-tags-overlay)
  14286. (defun org-align-tags-here (to-col)
  14287. ;; Assumes that this is a headline
  14288. (let ((pos (point)) (col (current-column)) tags)
  14289. (beginning-of-line 1)
  14290. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14291. (< pos (match-beginning 2)))
  14292. (progn
  14293. (setq tags (match-string 2))
  14294. (goto-char (match-beginning 1))
  14295. (insert " ")
  14296. (delete-region (point) (1+ (match-end 0)))
  14297. (backward-char 1)
  14298. (move-to-column
  14299. (max (1+ (current-column))
  14300. (1+ col)
  14301. (if (> to-col 0)
  14302. to-col
  14303. (- (abs to-col) (length tags))))
  14304. t)
  14305. (insert tags)
  14306. (move-to-column (min (current-column) col) t))
  14307. (goto-char pos))))
  14308. (defun org-set-tags (&optional arg just-align)
  14309. "Set the tags for the current headline.
  14310. With prefix ARG, realign all tags in headings in the current buffer."
  14311. (interactive "P")
  14312. (let* ((re (concat "^" outline-regexp))
  14313. (current (org-get-tags-string))
  14314. (col (current-column))
  14315. (org-setting-tags t)
  14316. table current-tags inherited-tags ; computed below when needed
  14317. tags p0 c0 c1 rpl)
  14318. (if arg
  14319. (save-excursion
  14320. (goto-char (point-min))
  14321. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14322. (while (re-search-forward re nil t)
  14323. (org-set-tags nil t)
  14324. (end-of-line 1)))
  14325. (message "All tags realigned to column %d" org-tags-column))
  14326. (if just-align
  14327. (setq tags current)
  14328. ;; Get a new set of tags from the user
  14329. (save-excursion
  14330. (setq table (or org-tag-alist (org-get-buffer-tags))
  14331. org-last-tags-completion-table table
  14332. current-tags (org-split-string current ":")
  14333. inherited-tags (nreverse
  14334. (nthcdr (length current-tags)
  14335. (nreverse (org-get-tags-at))))
  14336. tags
  14337. (if (or (eq t org-use-fast-tag-selection)
  14338. (and org-use-fast-tag-selection
  14339. (delq nil (mapcar 'cdr table))))
  14340. (org-fast-tag-selection
  14341. current-tags inherited-tags table
  14342. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14343. (let ((org-add-colon-after-tag-completion t))
  14344. (org-trim
  14345. (org-without-partial-completion
  14346. (completing-read "Tags: " 'org-tags-completion-function
  14347. nil nil current 'org-tags-history)))))))
  14348. (while (string-match "[-+&]+" tags)
  14349. ;; No boolean logic, just a list
  14350. (setq tags (replace-match ":" t t tags))))
  14351. (if (string-match "\\`[\t ]*\\'" tags)
  14352. (setq tags "")
  14353. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14354. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14355. ;; Insert new tags at the correct column
  14356. (beginning-of-line 1)
  14357. (cond
  14358. ((and (equal current "") (equal tags "")))
  14359. ((re-search-forward
  14360. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14361. (point-at-eol) t)
  14362. (if (equal tags "")
  14363. (setq rpl "")
  14364. (goto-char (match-beginning 0))
  14365. (setq c0 (current-column) p0 (point)
  14366. c1 (max (1+ c0) (if (> org-tags-column 0)
  14367. org-tags-column
  14368. (- (- org-tags-column) (length tags))))
  14369. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14370. (replace-match rpl t t)
  14371. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14372. tags)
  14373. (t (error "Tags alignment failed")))
  14374. (move-to-column col)
  14375. (unless just-align
  14376. (run-hooks 'org-after-tags-change-hook)))))
  14377. (defun org-change-tag-in-region (beg end tag off)
  14378. "Add or remove TAG for each entry in the region.
  14379. This works in the agenda, and also in an org-mode buffer."
  14380. (interactive
  14381. (list (region-beginning) (region-end)
  14382. (let ((org-last-tags-completion-table
  14383. (if (org-mode-p)
  14384. (org-get-buffer-tags)
  14385. (org-global-tags-completion-table))))
  14386. (completing-read
  14387. "Tag: " 'org-tags-completion-function nil nil nil
  14388. 'org-tags-history))
  14389. (progn
  14390. (message "[s]et or [r]emove? ")
  14391. (equal (read-char-exclusive) ?r))))
  14392. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14393. (let ((agendap (equal major-mode 'org-agenda-mode))
  14394. l1 l2 m buf pos newhead (cnt 0))
  14395. (goto-char end)
  14396. (setq l2 (1- (org-current-line)))
  14397. (goto-char beg)
  14398. (setq l1 (org-current-line))
  14399. (loop for l from l1 to l2 do
  14400. (goto-line l)
  14401. (setq m (get-text-property (point) 'org-hd-marker))
  14402. (when (or (and (org-mode-p) (org-on-heading-p))
  14403. (and agendap m))
  14404. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14405. pos (if agendap m (point)))
  14406. (with-current-buffer buf
  14407. (save-excursion
  14408. (save-restriction
  14409. (goto-char pos)
  14410. (setq cnt (1+ cnt))
  14411. (org-toggle-tag tag (if off 'off 'on))
  14412. (setq newhead (org-get-heading)))))
  14413. (and agendap (org-agenda-change-all-lines newhead m))))
  14414. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14415. (defun org-tags-completion-function (string predicate &optional flag)
  14416. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14417. (confirm (lambda (x) (stringp (car x)))))
  14418. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14419. (setq s1 (match-string 1 string)
  14420. s2 (match-string 2 string))
  14421. (setq s1 "" s2 string))
  14422. (cond
  14423. ((eq flag nil)
  14424. ;; try completion
  14425. (setq rtn (try-completion s2 ctable confirm))
  14426. (if (stringp rtn)
  14427. (setq rtn
  14428. (concat s1 s2 (substring rtn (length s2))
  14429. (if (and org-add-colon-after-tag-completion
  14430. (assoc rtn ctable))
  14431. ":" ""))))
  14432. rtn)
  14433. ((eq flag t)
  14434. ;; all-completions
  14435. (all-completions s2 ctable confirm)
  14436. )
  14437. ((eq flag 'lambda)
  14438. ;; exact match?
  14439. (assoc s2 ctable)))
  14440. ))
  14441. (defun org-fast-tag-insert (kwd tags face &optional end)
  14442. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14443. (insert (format "%-12s" (concat kwd ":"))
  14444. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14445. (or end "")))
  14446. (defun org-fast-tag-show-exit (flag)
  14447. (save-excursion
  14448. (goto-line 3)
  14449. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14450. (replace-match ""))
  14451. (when flag
  14452. (end-of-line 1)
  14453. (move-to-column (- (window-width) 19) t)
  14454. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14455. (defun org-set-current-tags-overlay (current prefix)
  14456. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14457. (if (featurep 'xemacs)
  14458. (org-overlay-display org-tags-overlay (concat prefix s)
  14459. 'secondary-selection)
  14460. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14461. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14462. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14463. "Fast tag selection with single keys.
  14464. CURRENT is the current list of tags in the headline, INHERITED is the
  14465. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14466. possibly with grouping information. TODO-TABLE is a similar table with
  14467. TODO keywords, should these have keys assigned to them.
  14468. If the keys are nil, a-z are automatically assigned.
  14469. Returns the new tags string, or nil to not change the current settings."
  14470. (let* ((fulltable (append table todo-table))
  14471. (maxlen (apply 'max (mapcar
  14472. (lambda (x)
  14473. (if (stringp (car x)) (string-width (car x)) 0))
  14474. fulltable)))
  14475. (buf (current-buffer))
  14476. (expert (eq org-fast-tag-selection-single-key 'expert))
  14477. (buffer-tags nil)
  14478. (fwidth (+ maxlen 3 1 3))
  14479. (ncol (/ (- (window-width) 4) fwidth))
  14480. (i-face 'org-done)
  14481. (c-face 'org-todo)
  14482. tg cnt e c char c1 c2 ntable tbl rtn
  14483. ov-start ov-end ov-prefix
  14484. (exit-after-next org-fast-tag-selection-single-key)
  14485. (done-keywords org-done-keywords)
  14486. groups ingroup)
  14487. (save-excursion
  14488. (beginning-of-line 1)
  14489. (if (looking-at
  14490. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14491. (setq ov-start (match-beginning 1)
  14492. ov-end (match-end 1)
  14493. ov-prefix "")
  14494. (setq ov-start (1- (point-at-eol))
  14495. ov-end (1+ ov-start))
  14496. (skip-chars-forward "^\n\r")
  14497. (setq ov-prefix
  14498. (concat
  14499. (buffer-substring (1- (point)) (point))
  14500. (if (> (current-column) org-tags-column)
  14501. " "
  14502. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14503. (org-move-overlay org-tags-overlay ov-start ov-end)
  14504. (save-window-excursion
  14505. (if expert
  14506. (set-buffer (get-buffer-create " *Org tags*"))
  14507. (delete-other-windows)
  14508. (split-window-vertically)
  14509. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14510. (erase-buffer)
  14511. (org-set-local 'org-done-keywords done-keywords)
  14512. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14513. (org-fast-tag-insert "Current" current c-face "\n\n")
  14514. (org-fast-tag-show-exit exit-after-next)
  14515. (org-set-current-tags-overlay current ov-prefix)
  14516. (setq tbl fulltable char ?a cnt 0)
  14517. (while (setq e (pop tbl))
  14518. (cond
  14519. ((equal e '(:startgroup))
  14520. (push '() groups) (setq ingroup t)
  14521. (when (not (= cnt 0))
  14522. (setq cnt 0)
  14523. (insert "\n"))
  14524. (insert "{ "))
  14525. ((equal e '(:endgroup))
  14526. (setq ingroup nil cnt 0)
  14527. (insert "}\n"))
  14528. (t
  14529. (setq tg (car e) c2 nil)
  14530. (if (cdr e)
  14531. (setq c (cdr e))
  14532. ;; automatically assign a character.
  14533. (setq c1 (string-to-char
  14534. (downcase (substring
  14535. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14536. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14537. (while (or (rassoc char ntable) (rassoc char table))
  14538. (setq char (1+ char)))
  14539. (setq c2 c1))
  14540. (setq c (or c2 char)))
  14541. (if ingroup (push tg (car groups)))
  14542. (setq tg (org-add-props tg nil 'face
  14543. (cond
  14544. ((not (assoc tg table))
  14545. (org-get-todo-face tg))
  14546. ((member tg current) c-face)
  14547. ((member tg inherited) i-face)
  14548. (t nil))))
  14549. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14550. (insert "[" c "] " tg (make-string
  14551. (- fwidth 4 (length tg)) ?\ ))
  14552. (push (cons tg c) ntable)
  14553. (when (= (setq cnt (1+ cnt)) ncol)
  14554. (insert "\n")
  14555. (if ingroup (insert " "))
  14556. (setq cnt 0)))))
  14557. (setq ntable (nreverse ntable))
  14558. (insert "\n")
  14559. (goto-char (point-min))
  14560. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14561. (fit-window-to-buffer))
  14562. (setq rtn
  14563. (catch 'exit
  14564. (while t
  14565. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14566. (if groups " [!] no groups" " [!]groups")
  14567. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14568. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14569. (cond
  14570. ((= c ?\r) (throw 'exit t))
  14571. ((= c ?!)
  14572. (setq groups (not groups))
  14573. (goto-char (point-min))
  14574. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14575. ((= c ?\C-c)
  14576. (if (not expert)
  14577. (org-fast-tag-show-exit
  14578. (setq exit-after-next (not exit-after-next)))
  14579. (setq expert nil)
  14580. (delete-other-windows)
  14581. (split-window-vertically)
  14582. (org-switch-to-buffer-other-window " *Org tags*")
  14583. (and (fboundp 'fit-window-to-buffer)
  14584. (fit-window-to-buffer))))
  14585. ((or (= c ?\C-g)
  14586. (and (= c ?q) (not (rassoc c ntable))))
  14587. (org-detach-overlay org-tags-overlay)
  14588. (setq quit-flag t))
  14589. ((= c ?\ )
  14590. (setq current nil)
  14591. (if exit-after-next (setq exit-after-next 'now)))
  14592. ((= c ?\t)
  14593. (condition-case nil
  14594. (setq tg (completing-read
  14595. "Tag: "
  14596. (or buffer-tags
  14597. (with-current-buffer buf
  14598. (org-get-buffer-tags)))))
  14599. (quit (setq tg "")))
  14600. (when (string-match "\\S-" tg)
  14601. (add-to-list 'buffer-tags (list tg))
  14602. (if (member tg current)
  14603. (setq current (delete tg current))
  14604. (push tg current)))
  14605. (if exit-after-next (setq exit-after-next 'now)))
  14606. ((setq e (rassoc c todo-table) tg (car e))
  14607. (with-current-buffer buf
  14608. (save-excursion (org-todo tg)))
  14609. (if exit-after-next (setq exit-after-next 'now)))
  14610. ((setq e (rassoc c ntable) tg (car e))
  14611. (if (member tg current)
  14612. (setq current (delete tg current))
  14613. (loop for g in groups do
  14614. (if (member tg g)
  14615. (mapc (lambda (x)
  14616. (setq current (delete x current)))
  14617. g)))
  14618. (push tg current))
  14619. (if exit-after-next (setq exit-after-next 'now))))
  14620. ;; Create a sorted list
  14621. (setq current
  14622. (sort current
  14623. (lambda (a b)
  14624. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14625. (if (eq exit-after-next 'now) (throw 'exit t))
  14626. (goto-char (point-min))
  14627. (beginning-of-line 2)
  14628. (delete-region (point) (point-at-eol))
  14629. (org-fast-tag-insert "Current" current c-face)
  14630. (org-set-current-tags-overlay current ov-prefix)
  14631. (while (re-search-forward
  14632. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14633. (setq tg (match-string 1))
  14634. (add-text-properties
  14635. (match-beginning 1) (match-end 1)
  14636. (list 'face
  14637. (cond
  14638. ((member tg current) c-face)
  14639. ((member tg inherited) i-face)
  14640. (t (get-text-property (match-beginning 1) 'face))))))
  14641. (goto-char (point-min)))))
  14642. (org-detach-overlay org-tags-overlay)
  14643. (if rtn
  14644. (mapconcat 'identity current ":")
  14645. nil))))
  14646. (defun org-get-tags-string ()
  14647. "Get the TAGS string in the current headline."
  14648. (unless (org-on-heading-p t)
  14649. (error "Not on a heading"))
  14650. (save-excursion
  14651. (beginning-of-line 1)
  14652. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14653. (org-match-string-no-properties 1)
  14654. "")))
  14655. (defun org-get-tags ()
  14656. "Get the list of tags specified in the current headline."
  14657. (org-split-string (org-get-tags-string) ":"))
  14658. (defun org-get-buffer-tags ()
  14659. "Get a table of all tags used in the buffer, for completion."
  14660. (let (tags)
  14661. (save-excursion
  14662. (goto-char (point-min))
  14663. (while (re-search-forward
  14664. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14665. (when (equal (char-after (point-at-bol 0)) ?*)
  14666. (mapc (lambda (x) (add-to-list 'tags x))
  14667. (org-split-string (org-match-string-no-properties 1) ":")))))
  14668. (mapcar 'list tags)))
  14669. ;;;; Properties
  14670. ;;; Setting and retrieving properties
  14671. (defconst org-special-properties
  14672. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14673. "TIMESTAMP" "TIMESTAMP_IA")
  14674. "The special properties valid in Org-mode.
  14675. These are properties that are not defined in the property drawer,
  14676. but in some other way.")
  14677. (defconst org-default-properties
  14678. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14679. "LOCATION" "LOGGING" "COLUMNS")
  14680. "Some properties that are used by Org-mode for various purposes.
  14681. Being in this list makes sure that they are offered for completion.")
  14682. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14683. "Regular expression matching the first line of a property drawer.")
  14684. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14685. "Regular expression matching the first line of a property drawer.")
  14686. (defun org-property-action ()
  14687. "Do an action on properties."
  14688. (interactive)
  14689. (let (c)
  14690. (org-at-property-p)
  14691. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14692. (setq c (read-char-exclusive))
  14693. (cond
  14694. ((equal c ?s)
  14695. (call-interactively 'org-set-property))
  14696. ((equal c ?d)
  14697. (call-interactively 'org-delete-property))
  14698. ((equal c ?D)
  14699. (call-interactively 'org-delete-property-globally))
  14700. ((equal c ?c)
  14701. (call-interactively 'org-compute-property-at-point))
  14702. (t (error "No such property action %c" c)))))
  14703. (defun org-at-property-p ()
  14704. "Is the cursor in a property line?"
  14705. ;; FIXME: Does not check if we are actually in the drawer.
  14706. ;; FIXME: also returns true on any drawers.....
  14707. ;; This is used by C-c C-c for property action.
  14708. (save-excursion
  14709. (beginning-of-line 1)
  14710. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14711. (defmacro org-with-point-at (pom &rest body)
  14712. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14713. (declare (indent 1) (debug t))
  14714. `(save-excursion
  14715. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14716. (save-excursion
  14717. (goto-char (or pom (point)))
  14718. ,@body)))
  14719. (defun org-get-property-block (&optional beg end force)
  14720. "Return the (beg . end) range of the body of the property drawer.
  14721. BEG and END can be beginning and end of subtree, if not given
  14722. they will be found.
  14723. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14724. (catch 'exit
  14725. (save-excursion
  14726. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14727. (end (or end (progn (outline-next-heading) (point)))))
  14728. (goto-char beg)
  14729. (if (re-search-forward org-property-start-re end t)
  14730. (setq beg (1+ (match-end 0)))
  14731. (if force
  14732. (save-excursion
  14733. (org-insert-property-drawer)
  14734. (setq end (progn (outline-next-heading) (point))))
  14735. (throw 'exit nil))
  14736. (goto-char beg)
  14737. (if (re-search-forward org-property-start-re end t)
  14738. (setq beg (1+ (match-end 0)))))
  14739. (if (re-search-forward org-property-end-re end t)
  14740. (setq end (match-beginning 0))
  14741. (or force (throw 'exit nil))
  14742. (goto-char beg)
  14743. (setq end beg)
  14744. (org-indent-line-function)
  14745. (insert ":END:\n"))
  14746. (cons beg end)))))
  14747. (defun org-entry-properties (&optional pom which)
  14748. "Get all properties of the entry at point-or-marker POM.
  14749. This includes the TODO keyword, the tags, time strings for deadline,
  14750. scheduled, and clocking, and any additional properties defined in the
  14751. entry. The return value is an alist, keys may occur multiple times
  14752. if the property key was used several times.
  14753. POM may also be nil, in which case the current entry is used.
  14754. If WHICH is nil or `all', get all properties. If WHICH is
  14755. `special' or `standard', only get that subclass."
  14756. (setq which (or which 'all))
  14757. (org-with-point-at pom
  14758. (let ((clockstr (substring org-clock-string 0 -1))
  14759. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14760. beg end range props sum-props key value string clocksum)
  14761. (save-excursion
  14762. (when (condition-case nil (org-back-to-heading t) (error nil))
  14763. (setq beg (point))
  14764. (setq sum-props (get-text-property (point) 'org-summaries))
  14765. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14766. (outline-next-heading)
  14767. (setq end (point))
  14768. (when (memq which '(all special))
  14769. ;; Get the special properties, like TODO and tags
  14770. (goto-char beg)
  14771. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14772. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14773. (when (looking-at org-priority-regexp)
  14774. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14775. (when (and (setq value (org-get-tags-string))
  14776. (string-match "\\S-" value))
  14777. (push (cons "TAGS" value) props))
  14778. (when (setq value (org-get-tags-at))
  14779. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14780. props))
  14781. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14782. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14783. string (if (equal key clockstr)
  14784. (org-no-properties
  14785. (org-trim
  14786. (buffer-substring
  14787. (match-beginning 3) (goto-char (point-at-eol)))))
  14788. (substring (org-match-string-no-properties 3) 1 -1)))
  14789. (unless key
  14790. (if (= (char-after (match-beginning 3)) ?\[)
  14791. (setq key "TIMESTAMP_IA")
  14792. (setq key "TIMESTAMP")))
  14793. (when (or (equal key clockstr) (not (assoc key props)))
  14794. (push (cons key string) props)))
  14795. )
  14796. (when (memq which '(all standard))
  14797. ;; Get the standard properties, like :PORP: ...
  14798. (setq range (org-get-property-block beg end))
  14799. (when range
  14800. (goto-char (car range))
  14801. (while (re-search-forward
  14802. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14803. (cdr range) t)
  14804. (setq key (org-match-string-no-properties 1)
  14805. value (org-trim (or (org-match-string-no-properties 2) "")))
  14806. (unless (member key excluded)
  14807. (push (cons key (or value "")) props)))))
  14808. (if clocksum
  14809. (push (cons "CLOCKSUM"
  14810. (org-column-number-to-string (/ (float clocksum) 60.)
  14811. 'add_times))
  14812. props))
  14813. (append sum-props (nreverse props)))))))
  14814. (defun org-entry-get (pom property &optional inherit)
  14815. "Get value of PROPERTY for entry at point-or-marker POM.
  14816. If INHERIT is non-nil and the entry does not have the property,
  14817. then also check higher levels of the hierarchy.
  14818. If the property is present but empty, the return value is the empty string.
  14819. If the property is not present at all, nil is returned."
  14820. (org-with-point-at pom
  14821. (if inherit
  14822. (org-entry-get-with-inheritance property)
  14823. (if (member property org-special-properties)
  14824. ;; We need a special property. Use brute force, get all properties.
  14825. (cdr (assoc property (org-entry-properties nil 'special)))
  14826. (let ((range (org-get-property-block)))
  14827. (if (and range
  14828. (goto-char (car range))
  14829. (re-search-forward
  14830. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14831. (cdr range) t))
  14832. ;; Found the property, return it.
  14833. (if (match-end 1)
  14834. (org-match-string-no-properties 1)
  14835. "")))))))
  14836. (defun org-entry-delete (pom property)
  14837. "Delete the property PROPERTY from entry at point-or-marker POM."
  14838. (org-with-point-at pom
  14839. (if (member property org-special-properties)
  14840. nil ; cannot delete these properties.
  14841. (let ((range (org-get-property-block)))
  14842. (if (and range
  14843. (goto-char (car range))
  14844. (re-search-forward
  14845. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14846. (cdr range) t))
  14847. (progn
  14848. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14849. t)
  14850. nil)))))
  14851. ;; Multi-values properties are properties that contain multiple values
  14852. ;; These values are assumed to be single words, separated by whitespace.
  14853. (defun org-entry-add-to-multivalued-property (pom property value)
  14854. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14855. (let* ((old (org-entry-get pom property))
  14856. (values (and old (org-split-string old "[ \t]"))))
  14857. (unless (member value values)
  14858. (setq values (cons value values))
  14859. (org-entry-put pom property
  14860. (mapconcat 'identity values " ")))))
  14861. (defun org-entry-remove-from-multivalued-property (pom property value)
  14862. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14863. (let* ((old (org-entry-get pom property))
  14864. (values (and old (org-split-string old "[ \t]"))))
  14865. (when (member value values)
  14866. (setq values (delete value values))
  14867. (org-entry-put pom property
  14868. (mapconcat 'identity values " ")))))
  14869. (defun org-entry-member-in-multivalued-property (pom property value)
  14870. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14871. (let* ((old (org-entry-get pom property))
  14872. (values (and old (org-split-string old "[ \t]"))))
  14873. (member value values)))
  14874. (defvar org-entry-property-inherited-from (make-marker))
  14875. (defun org-entry-get-with-inheritance (property)
  14876. "Get entry property, and search higher levels if not present."
  14877. (let (tmp)
  14878. (save-excursion
  14879. (save-restriction
  14880. (widen)
  14881. (catch 'ex
  14882. (while t
  14883. (when (setq tmp (org-entry-get nil property))
  14884. (org-back-to-heading t)
  14885. (move-marker org-entry-property-inherited-from (point))
  14886. (throw 'ex tmp))
  14887. (or (org-up-heading-safe) (throw 'ex nil)))))
  14888. (or tmp (cdr (assoc property org-local-properties))
  14889. (cdr (assoc property org-global-properties))))))
  14890. (defun org-entry-put (pom property value)
  14891. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14892. (org-with-point-at pom
  14893. (org-back-to-heading t)
  14894. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14895. range)
  14896. (cond
  14897. ((equal property "TODO")
  14898. (when (and (stringp value) (string-match "\\S-" value)
  14899. (not (member value org-todo-keywords-1)))
  14900. (error "\"%s\" is not a valid TODO state" value))
  14901. (if (or (not value)
  14902. (not (string-match "\\S-" value)))
  14903. (setq value 'none))
  14904. (org-todo value)
  14905. (org-set-tags nil 'align))
  14906. ((equal property "PRIORITY")
  14907. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14908. (string-to-char value) ?\ ))
  14909. (org-set-tags nil 'align))
  14910. ((equal property "SCHEDULED")
  14911. (if (re-search-forward org-scheduled-time-regexp end t)
  14912. (cond
  14913. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14914. ((eq value 'later) (org-timestamp-change 1 'day))
  14915. (t (call-interactively 'org-schedule)))
  14916. (call-interactively 'org-schedule)))
  14917. ((equal property "DEADLINE")
  14918. (if (re-search-forward org-deadline-time-regexp end t)
  14919. (cond
  14920. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14921. ((eq value 'later) (org-timestamp-change 1 'day))
  14922. (t (call-interactively 'org-deadline)))
  14923. (call-interactively 'org-deadline)))
  14924. ((member property org-special-properties)
  14925. (error "The %s property can not yet be set with `org-entry-put'"
  14926. property))
  14927. (t ; a non-special property
  14928. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14929. (setq range (org-get-property-block beg end 'force))
  14930. (goto-char (car range))
  14931. (if (re-search-forward
  14932. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14933. (progn
  14934. (delete-region (match-beginning 1) (match-end 1))
  14935. (goto-char (match-beginning 1)))
  14936. (goto-char (cdr range))
  14937. (insert "\n")
  14938. (backward-char 1)
  14939. (org-indent-line-function)
  14940. (insert ":" property ":"))
  14941. (and value (insert " " value))
  14942. (org-indent-line-function)))))))
  14943. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14944. "Get all property keys in the current buffer.
  14945. With INCLUDE-SPECIALS, also list the special properties that relect things
  14946. like tags and TODO state.
  14947. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14948. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  14949. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14950. formats in the current buffer."
  14951. (let (rtn range cfmt cols s p)
  14952. (save-excursion
  14953. (save-restriction
  14954. (widen)
  14955. (goto-char (point-min))
  14956. (while (re-search-forward org-property-start-re nil t)
  14957. (setq range (org-get-property-block))
  14958. (goto-char (car range))
  14959. (while (re-search-forward
  14960. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14961. (cdr range) t)
  14962. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14963. (outline-next-heading))))
  14964. (when include-specials
  14965. (setq rtn (append org-special-properties rtn)))
  14966. (when include-defaults
  14967. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14968. (when include-columns
  14969. (save-excursion
  14970. (save-restriction
  14971. (widen)
  14972. (goto-char (point-min))
  14973. (while (re-search-forward
  14974. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14975. nil t)
  14976. (setq cfmt (match-string 2) s 0)
  14977. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14978. cfmt s)
  14979. (setq s (match-end 0)
  14980. p (match-string 1 cfmt))
  14981. (unless (or (equal p "ITEM")
  14982. (member p org-special-properties))
  14983. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14984. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14985. (defun org-property-values (key)
  14986. "Return a list of all values of property KEY."
  14987. (save-excursion
  14988. (save-restriction
  14989. (widen)
  14990. (goto-char (point-min))
  14991. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14992. values)
  14993. (while (re-search-forward re nil t)
  14994. (add-to-list 'values (org-trim (match-string 1))))
  14995. (delete "" values)))))
  14996. (defun org-insert-property-drawer ()
  14997. "Insert a property drawer into the current entry."
  14998. (interactive)
  14999. (org-back-to-heading t)
  15000. (looking-at outline-regexp)
  15001. (let ((indent (- (match-end 0)(match-beginning 0)))
  15002. (beg (point))
  15003. (re (concat "^[ \t]*" org-keyword-time-regexp))
  15004. end hiddenp)
  15005. (outline-next-heading)
  15006. (setq end (point))
  15007. (goto-char beg)
  15008. (while (re-search-forward re end t))
  15009. (setq hiddenp (org-invisible-p))
  15010. (end-of-line 1)
  15011. (and (equal (char-after) ?\n) (forward-char 1))
  15012. (org-skip-over-state-notes)
  15013. (skip-chars-backward " \t\n\r")
  15014. (if (eq (char-before) ?*) (forward-char 1))
  15015. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  15016. (beginning-of-line 0)
  15017. (indent-to-column indent)
  15018. (beginning-of-line 2)
  15019. (indent-to-column indent)
  15020. (beginning-of-line 0)
  15021. (if hiddenp
  15022. (save-excursion
  15023. (org-back-to-heading t)
  15024. (hide-entry))
  15025. (org-flag-drawer t))))
  15026. (defun org-set-property (property value)
  15027. "In the current entry, set PROPERTY to VALUE.
  15028. When called interactively, this will prompt for a property name, offering
  15029. completion on existing and default properties. And then it will prompt
  15030. for a value, offering competion either on allowed values (via an inherited
  15031. xxx_ALL property) or on existing values in other instances of this property
  15032. in the current file."
  15033. (interactive
  15034. (let* ((prop (completing-read
  15035. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  15036. (cur (org-entry-get nil prop))
  15037. (allowed (org-property-get-allowed-values nil prop 'table))
  15038. (existing (mapcar 'list (org-property-values prop)))
  15039. (val (if allowed
  15040. (completing-read "Value: " allowed nil 'req-match)
  15041. (completing-read
  15042. (concat "Value" (if (and cur (string-match "\\S-" cur))
  15043. (concat "[" cur "]") "")
  15044. ": ")
  15045. existing nil nil "" nil cur))))
  15046. (list prop (if (equal val "") cur val))))
  15047. (unless (equal (org-entry-get nil property) value)
  15048. (org-entry-put nil property value)))
  15049. (defun org-delete-property (property)
  15050. "In the current entry, delete PROPERTY."
  15051. (interactive
  15052. (let* ((prop (completing-read
  15053. "Property: " (org-entry-properties nil 'standard))))
  15054. (list prop)))
  15055. (message "Property %s %s" property
  15056. (if (org-entry-delete nil property)
  15057. "deleted"
  15058. "was not present in the entry")))
  15059. (defun org-delete-property-globally (property)
  15060. "Remove PROPERTY globally, from all entries."
  15061. (interactive
  15062. (let* ((prop (completing-read
  15063. "Globally remove property: "
  15064. (mapcar 'list (org-buffer-property-keys)))))
  15065. (list prop)))
  15066. (save-excursion
  15067. (save-restriction
  15068. (widen)
  15069. (goto-char (point-min))
  15070. (let ((cnt 0))
  15071. (while (re-search-forward
  15072. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  15073. nil t)
  15074. (setq cnt (1+ cnt))
  15075. (replace-match ""))
  15076. (message "Property \"%s\" removed from %d entries" property cnt)))))
  15077. (defvar org-columns-current-fmt-compiled) ; defined below
  15078. (defun org-compute-property-at-point ()
  15079. "Compute the property at point.
  15080. This looks for an enclosing column format, extracts the operator and
  15081. then applies it to the proerty in the column format's scope."
  15082. (interactive)
  15083. (unless (org-at-property-p)
  15084. (error "Not at a property"))
  15085. (let ((prop (org-match-string-no-properties 2)))
  15086. (org-columns-get-format-and-top-level)
  15087. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15088. (error "No operator defined for property %s" prop))
  15089. (org-columns-compute prop)))
  15090. (defun org-property-get-allowed-values (pom property &optional table)
  15091. "Get allowed values for the property PROPERTY.
  15092. When TABLE is non-nil, return an alist that can directly be used for
  15093. completion."
  15094. (let (vals)
  15095. (cond
  15096. ((equal property "TODO")
  15097. (setq vals (org-with-point-at pom
  15098. (append org-todo-keywords-1 '("")))))
  15099. ((equal property "PRIORITY")
  15100. (let ((n org-lowest-priority))
  15101. (while (>= n org-highest-priority)
  15102. (push (char-to-string n) vals)
  15103. (setq n (1- n)))))
  15104. ((member property org-special-properties))
  15105. (t
  15106. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15107. (when (and vals (string-match "\\S-" vals))
  15108. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15109. (setq vals (mapcar (lambda (x)
  15110. (cond ((stringp x) x)
  15111. ((numberp x) (number-to-string x))
  15112. ((symbolp x) (symbol-name x))
  15113. (t "???")))
  15114. vals)))))
  15115. (if table (mapcar 'list vals) vals)))
  15116. (defun org-property-previous-allowed-value (&optional previous)
  15117. "Switch to the next allowed value for this property."
  15118. (interactive)
  15119. (org-property-next-allowed-value t))
  15120. (defun org-property-next-allowed-value (&optional previous)
  15121. "Switch to the next allowed value for this property."
  15122. (interactive)
  15123. (unless (org-at-property-p)
  15124. (error "Not at a property"))
  15125. (let* ((key (match-string 2))
  15126. (value (match-string 3))
  15127. (allowed (or (org-property-get-allowed-values (point) key)
  15128. (and (member value '("[ ]" "[-]" "[X]"))
  15129. '("[ ]" "[X]"))))
  15130. nval)
  15131. (unless allowed
  15132. (error "Allowed values for this property have not been defined"))
  15133. (if previous (setq allowed (reverse allowed)))
  15134. (if (member value allowed)
  15135. (setq nval (car (cdr (member value allowed)))))
  15136. (setq nval (or nval (car allowed)))
  15137. (if (equal nval value)
  15138. (error "Only one allowed value for this property"))
  15139. (org-at-property-p)
  15140. (replace-match (concat " :" key ": " nval) t t)
  15141. (org-indent-line-function)
  15142. (beginning-of-line 1)
  15143. (skip-chars-forward " \t")))
  15144. (defun org-find-entry-with-id (ident)
  15145. "Locate the entry that contains the ID property with exact value IDENT.
  15146. IDENT can be a string, a symbol or a number, this function will search for
  15147. the string representation of it.
  15148. Return the position where this entry starts, or nil if there is no such entry."
  15149. (let ((id (cond
  15150. ((stringp ident) ident)
  15151. ((symbol-name ident) (symbol-name ident))
  15152. ((numberp ident) (number-to-string ident))
  15153. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15154. (case-fold-search nil))
  15155. (save-excursion
  15156. (save-restriction
  15157. (widen)
  15158. (goto-char (point-min))
  15159. (when (re-search-forward
  15160. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15161. nil t)
  15162. (org-back-to-heading)
  15163. (point))))))
  15164. ;;; Column View
  15165. (defvar org-columns-overlays nil
  15166. "Holds the list of current column overlays.")
  15167. (defvar org-columns-current-fmt nil
  15168. "Local variable, holds the currently active column format.")
  15169. (defvar org-columns-current-fmt-compiled nil
  15170. "Local variable, holds the currently active column format.
  15171. This is the compiled version of the format.")
  15172. (defvar org-columns-current-widths nil
  15173. "Loval variable, holds the currently widths of fields.")
  15174. (defvar org-columns-current-maxwidths nil
  15175. "Loval variable, holds the currently active maximum column widths.")
  15176. (defvar org-columns-begin-marker (make-marker)
  15177. "Points to the position where last a column creation command was called.")
  15178. (defvar org-columns-top-level-marker (make-marker)
  15179. "Points to the position where current columns region starts.")
  15180. (defvar org-columns-map (make-sparse-keymap)
  15181. "The keymap valid in column display.")
  15182. (defun org-columns-content ()
  15183. "Switch to contents view while in columns view."
  15184. (interactive)
  15185. (org-overview)
  15186. (org-content))
  15187. (org-defkey org-columns-map "c" 'org-columns-content)
  15188. (org-defkey org-columns-map "o" 'org-overview)
  15189. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  15190. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  15191. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  15192. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15193. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15194. (org-defkey org-columns-map "q" 'org-columns-quit)
  15195. (org-defkey org-columns-map "r" 'org-columns-redo)
  15196. (org-defkey org-columns-map "g" 'org-columns-redo)
  15197. (org-defkey org-columns-map [left] 'backward-char)
  15198. (org-defkey org-columns-map "\M-b" 'backward-char)
  15199. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15200. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15201. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15202. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15203. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15204. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15205. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15206. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15207. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15208. (org-defkey org-columns-map ">" 'org-columns-widen)
  15209. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15210. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15211. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15212. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15213. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15214. '("Column"
  15215. ["Edit property" org-columns-edit-value t]
  15216. ["Next allowed value" org-columns-next-allowed-value t]
  15217. ["Previous allowed value" org-columns-previous-allowed-value t]
  15218. ["Show full value" org-columns-show-value t]
  15219. ["Edit allowed values" org-columns-edit-allowed t]
  15220. "--"
  15221. ["Edit column attributes" org-columns-edit-attributes t]
  15222. ["Increase column width" org-columns-widen t]
  15223. ["Decrease column width" org-columns-narrow t]
  15224. "--"
  15225. ["Move column right" org-columns-move-right t]
  15226. ["Move column left" org-columns-move-left t]
  15227. ["Add column" org-columns-new t]
  15228. ["Delete column" org-columns-delete t]
  15229. "--"
  15230. ["CONTENTS" org-columns-content t]
  15231. ["OVERVIEW" org-overview t]
  15232. ["Refresh columns display" org-columns-redo t]
  15233. "--"
  15234. ["Open link" org-columns-open-link t]
  15235. "--"
  15236. ["Quit" org-columns-quit t]))
  15237. (defun org-columns-new-overlay (beg end &optional string face)
  15238. "Create a new column overlay and add it to the list."
  15239. (let ((ov (org-make-overlay beg end)))
  15240. (org-overlay-put ov 'face (or face 'secondary-selection))
  15241. (org-overlay-display ov string face)
  15242. (push ov org-columns-overlays)
  15243. ov))
  15244. (defun org-columns-display-here (&optional props)
  15245. "Overlay the current line with column display."
  15246. (interactive)
  15247. (let* ((fmt org-columns-current-fmt-compiled)
  15248. (beg (point-at-bol))
  15249. (level-face (save-excursion
  15250. (beginning-of-line 1)
  15251. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15252. (org-get-level-face 2))))
  15253. (color (list :foreground
  15254. (face-attribute (or level-face 'default) :foreground)))
  15255. props pom property ass width f string ov column val modval)
  15256. ;; Check if the entry is in another buffer.
  15257. (unless props
  15258. (if (eq major-mode 'org-agenda-mode)
  15259. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15260. (get-text-property (point) 'org-marker))
  15261. props (if pom (org-entry-properties pom) nil))
  15262. (setq props (org-entry-properties nil))))
  15263. ;; Walk the format
  15264. (while (setq column (pop fmt))
  15265. (setq property (car column)
  15266. ass (if (equal property "ITEM")
  15267. (cons "ITEM"
  15268. (save-match-data
  15269. (org-no-properties
  15270. (org-remove-tabs
  15271. (buffer-substring-no-properties
  15272. (point-at-bol) (point-at-eol))))))
  15273. (assoc property props))
  15274. width (or (cdr (assoc property org-columns-current-maxwidths))
  15275. (nth 2 column)
  15276. (length property))
  15277. f (format "%%-%d.%ds | " width width)
  15278. val (or (cdr ass) "")
  15279. modval (if (equal property "ITEM")
  15280. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15281. string (format f (or modval val)))
  15282. ;; Create the overlay
  15283. (org-unmodified
  15284. (setq ov (org-columns-new-overlay
  15285. beg (setq beg (1+ beg)) string
  15286. (list color 'org-column)))
  15287. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15288. (org-overlay-put ov 'keymap org-columns-map)
  15289. (org-overlay-put ov 'org-columns-key property)
  15290. (org-overlay-put ov 'org-columns-value (cdr ass))
  15291. (org-overlay-put ov 'org-columns-value-modified modval)
  15292. (org-overlay-put ov 'org-columns-pom pom)
  15293. (org-overlay-put ov 'org-columns-format f))
  15294. (if (or (not (char-after beg))
  15295. (equal (char-after beg) ?\n))
  15296. (let ((inhibit-read-only t))
  15297. (save-excursion
  15298. (goto-char beg)
  15299. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15300. ;; Make the rest of the line disappear.
  15301. (org-unmodified
  15302. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15303. (org-overlay-put ov 'invisible t)
  15304. (org-overlay-put ov 'keymap org-columns-map)
  15305. (org-overlay-put ov 'intangible t)
  15306. (push ov org-columns-overlays)
  15307. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15308. (org-overlay-put ov 'keymap org-columns-map)
  15309. (push ov org-columns-overlays)
  15310. (let ((inhibit-read-only t))
  15311. (put-text-property (max (point-min) (1- (point-at-bol)))
  15312. (min (point-max) (1+ (point-at-eol)))
  15313. 'read-only "Type `e' to edit property")))))
  15314. (defvar org-previous-header-line-format nil
  15315. "The header line format before column view was turned on.")
  15316. (defvar org-columns-inhibit-recalculation nil
  15317. "Inhibit recomputing of columns on column view startup.")
  15318. (defvar header-line-format)
  15319. (defun org-columns-display-here-title ()
  15320. "Overlay the newline before the current line with the table title."
  15321. (interactive)
  15322. (let ((fmt org-columns-current-fmt-compiled)
  15323. string (title "")
  15324. property width f column str widths)
  15325. (while (setq column (pop fmt))
  15326. (setq property (car column)
  15327. str (or (nth 1 column) property)
  15328. width (or (cdr (assoc property org-columns-current-maxwidths))
  15329. (nth 2 column)
  15330. (length str))
  15331. widths (push width widths)
  15332. f (format "%%-%d.%ds | " width width)
  15333. string (format f str)
  15334. title (concat title string)))
  15335. (setq title (concat
  15336. (org-add-props " " nil 'display '(space :align-to 0))
  15337. (org-add-props title nil 'face '(:weight bold :underline t))))
  15338. (org-set-local 'org-previous-header-line-format header-line-format)
  15339. (org-set-local 'org-columns-current-widths (nreverse widths))
  15340. (setq header-line-format title)))
  15341. (defun org-columns-remove-overlays ()
  15342. "Remove all currently active column overlays."
  15343. (interactive)
  15344. (when (marker-buffer org-columns-begin-marker)
  15345. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15346. (when (local-variable-p 'org-previous-header-line-format)
  15347. (setq header-line-format org-previous-header-line-format)
  15348. (kill-local-variable 'org-previous-header-line-format))
  15349. (move-marker org-columns-begin-marker nil)
  15350. (move-marker org-columns-top-level-marker nil)
  15351. (org-unmodified
  15352. (mapc 'org-delete-overlay org-columns-overlays)
  15353. (setq org-columns-overlays nil)
  15354. (let ((inhibit-read-only t))
  15355. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15356. (defun org-columns-cleanup-item (item fmt)
  15357. "Remove from ITEM what is a column in the format FMT."
  15358. (if (not org-complex-heading-regexp)
  15359. item
  15360. (when (string-match org-complex-heading-regexp item)
  15361. (concat
  15362. (org-add-props (concat (match-string 1 item) " ") nil
  15363. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15364. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15365. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15366. " " (match-string 4 item)
  15367. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15368. (defun org-columns-show-value ()
  15369. "Show the full value of the property."
  15370. (interactive)
  15371. (let ((value (get-char-property (point) 'org-columns-value)))
  15372. (message "Value is: %s" (or value ""))))
  15373. (defun org-columns-quit ()
  15374. "Remove the column overlays and in this way exit column editing."
  15375. (interactive)
  15376. (org-unmodified
  15377. (org-columns-remove-overlays)
  15378. (let ((inhibit-read-only t))
  15379. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15380. (when (eq major-mode 'org-agenda-mode)
  15381. (message
  15382. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15383. (defun org-columns-check-computed ()
  15384. "Check if this column value is computed.
  15385. If yes, throw an error indicating that changing it does not make sense."
  15386. (let ((val (get-char-property (point) 'org-columns-value)))
  15387. (when (and (stringp val)
  15388. (get-char-property 0 'org-computed val))
  15389. (error "This value is computed from the entry's children"))))
  15390. (defun org-columns-todo (&optional arg)
  15391. "Change the TODO state during column view."
  15392. (interactive "P")
  15393. (org-columns-edit-value "TODO"))
  15394. (defun org-columns-set-tags-or-toggle (&optional arg)
  15395. "Toggle checkbox at point, or set tags for current headline."
  15396. (interactive "P")
  15397. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15398. (get-char-property (point) 'org-columns-value))
  15399. (org-columns-next-allowed-value)
  15400. (org-columns-edit-value "TAGS")))
  15401. (defun org-columns-edit-value (&optional key)
  15402. "Edit the value of the property at point in column view.
  15403. Where possible, use the standard interface for changing this line."
  15404. (interactive)
  15405. (org-columns-check-computed)
  15406. (let* ((external-key key)
  15407. (col (current-column))
  15408. (key (or key (get-char-property (point) 'org-columns-key)))
  15409. (value (get-char-property (point) 'org-columns-value))
  15410. (bol (point-at-bol)) (eol (point-at-eol))
  15411. (pom (or (get-text-property bol 'org-hd-marker)
  15412. (point))) ; keep despite of compiler waring
  15413. (line-overlays
  15414. (delq nil (mapcar (lambda (x)
  15415. (and (eq (overlay-buffer x) (current-buffer))
  15416. (>= (overlay-start x) bol)
  15417. (<= (overlay-start x) eol)
  15418. x))
  15419. org-columns-overlays)))
  15420. nval eval allowed)
  15421. (cond
  15422. ((equal key "CLOCKSUM")
  15423. (error "This special column cannot be edited"))
  15424. ((equal key "ITEM")
  15425. (setq eval '(org-with-point-at pom
  15426. (org-edit-headline))))
  15427. ((equal key "TODO")
  15428. (setq eval '(org-with-point-at pom
  15429. (let ((current-prefix-arg
  15430. (if external-key current-prefix-arg '(4))))
  15431. (call-interactively 'org-todo)))))
  15432. ((equal key "PRIORITY")
  15433. (setq eval '(org-with-point-at pom
  15434. (call-interactively 'org-priority))))
  15435. ((equal key "TAGS")
  15436. (setq eval '(org-with-point-at pom
  15437. (let ((org-fast-tag-selection-single-key
  15438. (if (eq org-fast-tag-selection-single-key 'expert)
  15439. t org-fast-tag-selection-single-key)))
  15440. (call-interactively 'org-set-tags)))))
  15441. ((equal key "DEADLINE")
  15442. (setq eval '(org-with-point-at pom
  15443. (call-interactively 'org-deadline))))
  15444. ((equal key "SCHEDULED")
  15445. (setq eval '(org-with-point-at pom
  15446. (call-interactively 'org-schedule))))
  15447. (t
  15448. (setq allowed (org-property-get-allowed-values pom key 'table))
  15449. (if allowed
  15450. (setq nval (completing-read "Value: " allowed nil t))
  15451. (setq nval (read-string "Edit: " value)))
  15452. (setq nval (org-trim nval))
  15453. (when (not (equal nval value))
  15454. (setq eval '(org-entry-put pom key nval)))))
  15455. (when eval
  15456. (let ((inhibit-read-only t))
  15457. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15458. (unwind-protect
  15459. (progn
  15460. (setq org-columns-overlays
  15461. (org-delete-all line-overlays org-columns-overlays))
  15462. (mapc 'org-delete-overlay line-overlays)
  15463. (org-columns-eval eval))
  15464. (org-columns-display-here))))
  15465. (move-to-column col)
  15466. (if (and (org-mode-p)
  15467. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15468. (org-columns-update key))))
  15469. (defun org-edit-headline () ; FIXME: this is not columns specific
  15470. "Edit the current headline, the part without TODO keyword, TAGS."
  15471. (org-back-to-heading)
  15472. (when (looking-at org-todo-line-regexp)
  15473. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15474. (txt (match-string 3))
  15475. (post "")
  15476. txt2)
  15477. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15478. (setq post (match-string 0 txt)
  15479. txt (substring txt 0 (match-beginning 0))))
  15480. (setq txt2 (read-string "Edit: " txt))
  15481. (when (not (equal txt txt2))
  15482. (beginning-of-line 1)
  15483. (insert pre txt2 post)
  15484. (delete-region (point) (point-at-eol))
  15485. (org-set-tags nil t)))))
  15486. (defun org-columns-edit-allowed ()
  15487. "Edit the list of allowed values for the current property."
  15488. (interactive)
  15489. (let* ((key (get-char-property (point) 'org-columns-key))
  15490. (key1 (concat key "_ALL"))
  15491. (allowed (org-entry-get (point) key1 t))
  15492. nval)
  15493. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15494. (setq nval (read-string "Allowed: " allowed))
  15495. (org-entry-put
  15496. (cond ((marker-position org-entry-property-inherited-from)
  15497. org-entry-property-inherited-from)
  15498. ((marker-position org-columns-top-level-marker)
  15499. org-columns-top-level-marker))
  15500. key1 nval)))
  15501. (defmacro org-no-warnings (&rest body)
  15502. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15503. (defun org-columns-eval (form)
  15504. (let (hidep)
  15505. (save-excursion
  15506. (beginning-of-line 1)
  15507. ;; `next-line' is needed here, because it skips invisible line.
  15508. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15509. (setq hidep (org-on-heading-p 1)))
  15510. (eval form)
  15511. (and hidep (hide-entry))))
  15512. (defun org-columns-previous-allowed-value ()
  15513. "Switch to the previous allowed value for this column."
  15514. (interactive)
  15515. (org-columns-next-allowed-value t))
  15516. (defun org-columns-next-allowed-value (&optional previous)
  15517. "Switch to the next allowed value for this column."
  15518. (interactive)
  15519. (org-columns-check-computed)
  15520. (let* ((col (current-column))
  15521. (key (get-char-property (point) 'org-columns-key))
  15522. (value (get-char-property (point) 'org-columns-value))
  15523. (bol (point-at-bol)) (eol (point-at-eol))
  15524. (pom (or (get-text-property bol 'org-hd-marker)
  15525. (point))) ; keep despite of compiler waring
  15526. (line-overlays
  15527. (delq nil (mapcar (lambda (x)
  15528. (and (eq (overlay-buffer x) (current-buffer))
  15529. (>= (overlay-start x) bol)
  15530. (<= (overlay-start x) eol)
  15531. x))
  15532. org-columns-overlays)))
  15533. (allowed (or (org-property-get-allowed-values pom key)
  15534. (and (memq
  15535. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15536. '(checkbox checkbox-n-of-m checkbox-percent))
  15537. '("[ ]" "[X]"))))
  15538. nval)
  15539. (when (equal key "ITEM")
  15540. (error "Cannot edit item headline from here"))
  15541. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15542. (error "Allowed values for this property have not been defined"))
  15543. (if (member key '("SCHEDULED" "DEADLINE"))
  15544. (setq nval (if previous 'earlier 'later))
  15545. (if previous (setq allowed (reverse allowed)))
  15546. (if (member value allowed)
  15547. (setq nval (car (cdr (member value allowed)))))
  15548. (setq nval (or nval (car allowed)))
  15549. (if (equal nval value)
  15550. (error "Only one allowed value for this property")))
  15551. (let ((inhibit-read-only t))
  15552. (remove-text-properties (1- bol) eol '(read-only t))
  15553. (unwind-protect
  15554. (progn
  15555. (setq org-columns-overlays
  15556. (org-delete-all line-overlays org-columns-overlays))
  15557. (mapc 'org-delete-overlay line-overlays)
  15558. (org-columns-eval '(org-entry-put pom key nval)))
  15559. (org-columns-display-here)))
  15560. (move-to-column col)
  15561. (if (and (org-mode-p)
  15562. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15563. (org-columns-update key))))
  15564. (defun org-verify-version (task)
  15565. (cond
  15566. ((eq task 'columns)
  15567. (if (or (featurep 'xemacs)
  15568. (< emacs-major-version 22))
  15569. (error "Emacs 22 is required for the columns feature")))))
  15570. (defun org-columns-open-link (&optional arg)
  15571. (interactive "P")
  15572. (let ((value (get-char-property (point) 'org-columns-value)))
  15573. (org-open-link-from-string value arg)))
  15574. (defun org-open-link-from-string (s &optional arg)
  15575. "Open a link in the string S, as if it was in Org-mode."
  15576. (interactive)
  15577. (with-temp-buffer
  15578. (let ((org-inhibit-startup t))
  15579. (org-mode)
  15580. (insert s)
  15581. (goto-char (point-min))
  15582. (org-open-at-point arg))))
  15583. (defun org-columns-get-format-and-top-level ()
  15584. (let (fmt)
  15585. (when (condition-case nil (org-back-to-heading) (error nil))
  15586. (move-marker org-entry-property-inherited-from nil)
  15587. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15588. (setq fmt (or fmt org-columns-default-format))
  15589. (org-set-local 'org-columns-current-fmt fmt)
  15590. (org-columns-compile-format fmt)
  15591. (if (marker-position org-entry-property-inherited-from)
  15592. (move-marker org-columns-top-level-marker
  15593. org-entry-property-inherited-from)
  15594. (move-marker org-columns-top-level-marker (point)))
  15595. fmt))
  15596. (defun org-columns ()
  15597. "Turn on column view on an org-mode file."
  15598. (interactive)
  15599. (org-verify-version 'columns)
  15600. (org-columns-remove-overlays)
  15601. (move-marker org-columns-begin-marker (point))
  15602. (let (beg end fmt cache maxwidths)
  15603. (setq fmt (org-columns-get-format-and-top-level))
  15604. (save-excursion
  15605. (goto-char org-columns-top-level-marker)
  15606. (setq beg (point))
  15607. (unless org-columns-inhibit-recalculation
  15608. (org-columns-compute-all))
  15609. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15610. (point-max)))
  15611. ;; Get and cache the properties
  15612. (goto-char beg)
  15613. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15614. (save-excursion
  15615. (save-restriction
  15616. (narrow-to-region beg end)
  15617. (org-clock-sum))))
  15618. (while (re-search-forward (concat "^" outline-regexp) end t)
  15619. (push (cons (org-current-line) (org-entry-properties)) cache))
  15620. (when cache
  15621. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15622. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15623. (org-columns-display-here-title)
  15624. (mapc (lambda (x)
  15625. (goto-line (car x))
  15626. (org-columns-display-here (cdr x)))
  15627. cache)))))
  15628. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15629. "Insert a new column, to the leeft o the current column."
  15630. (interactive)
  15631. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15632. cell)
  15633. (setq prop (completing-read
  15634. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  15635. nil nil prop))
  15636. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15637. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15638. (if (string-match "\\S-" width)
  15639. (setq width (string-to-number width))
  15640. (setq width nil))
  15641. (setq fmt (completing-read "Summary [none]: "
  15642. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  15643. nil t))
  15644. (if (string-match "\\S-" fmt)
  15645. (setq fmt (intern fmt))
  15646. (setq fmt nil))
  15647. (if (eq fmt 'none) (setq fmt nil))
  15648. (if editp
  15649. (progn
  15650. (setcar editp prop)
  15651. (setcdr editp (list title width nil fmt)))
  15652. (setq cell (nthcdr (1- (current-column))
  15653. org-columns-current-fmt-compiled))
  15654. (setcdr cell (cons (list prop title width nil fmt)
  15655. (cdr cell))))
  15656. (org-columns-store-format)
  15657. (org-columns-redo)))
  15658. (defun org-columns-delete ()
  15659. "Delete the column at point from columns view."
  15660. (interactive)
  15661. (let* ((n (current-column))
  15662. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15663. (when (y-or-n-p
  15664. (format "Are you sure you want to remove column \"%s\"? " title))
  15665. (setq org-columns-current-fmt-compiled
  15666. (delq (nth n org-columns-current-fmt-compiled)
  15667. org-columns-current-fmt-compiled))
  15668. (org-columns-store-format)
  15669. (org-columns-redo)
  15670. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15671. (backward-char 1)))))
  15672. (defun org-columns-edit-attributes ()
  15673. "Edit the attributes of the current column."
  15674. (interactive)
  15675. (let* ((n (current-column))
  15676. (info (nth n org-columns-current-fmt-compiled)))
  15677. (apply 'org-columns-new info)))
  15678. (defun org-columns-widen (arg)
  15679. "Make the column wider by ARG characters."
  15680. (interactive "p")
  15681. (let* ((n (current-column))
  15682. (entry (nth n org-columns-current-fmt-compiled))
  15683. (width (or (nth 2 entry)
  15684. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15685. (setq width (max 1 (+ width arg)))
  15686. (setcar (nthcdr 2 entry) width)
  15687. (org-columns-store-format)
  15688. (org-columns-redo)))
  15689. (defun org-columns-narrow (arg)
  15690. "Make the column nrrower by ARG characters."
  15691. (interactive "p")
  15692. (org-columns-widen (- arg)))
  15693. (defun org-columns-move-right ()
  15694. "Swap this column with the one to the right."
  15695. (interactive)
  15696. (let* ((n (current-column))
  15697. (cell (nthcdr n org-columns-current-fmt-compiled))
  15698. e)
  15699. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15700. (error "Cannot shift this column further to the right"))
  15701. (setq e (car cell))
  15702. (setcar cell (car (cdr cell)))
  15703. (setcdr cell (cons e (cdr (cdr cell))))
  15704. (org-columns-store-format)
  15705. (org-columns-redo)
  15706. (forward-char 1)))
  15707. (defun org-columns-move-left ()
  15708. "Swap this column with the one to the left."
  15709. (interactive)
  15710. (let* ((n (current-column)))
  15711. (when (= n 0)
  15712. (error "Cannot shift this column further to the left"))
  15713. (backward-char 1)
  15714. (org-columns-move-right)
  15715. (backward-char 1)))
  15716. (defun org-columns-store-format ()
  15717. "Store the text version of the current columns format in appropriate place.
  15718. This is either in the COLUMNS property of the node starting the current column
  15719. display, or in the #+COLUMNS line of the current buffer."
  15720. (let (fmt (cnt 0))
  15721. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15722. (org-set-local 'org-columns-current-fmt fmt)
  15723. (if (marker-position org-columns-top-level-marker)
  15724. (save-excursion
  15725. (goto-char org-columns-top-level-marker)
  15726. (if (and (org-at-heading-p)
  15727. (org-entry-get nil "COLUMNS"))
  15728. (org-entry-put nil "COLUMNS" fmt)
  15729. (goto-char (point-min))
  15730. ;; Overwrite all #+COLUMNS lines....
  15731. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15732. (setq cnt (1+ cnt))
  15733. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15734. (unless (> cnt 0)
  15735. (goto-char (point-min))
  15736. (or (org-on-heading-p t) (outline-next-heading))
  15737. (let ((inhibit-read-only t))
  15738. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15739. (org-set-local 'org-columns-default-format fmt))))))
  15740. (defvar org-overriding-columns-format nil
  15741. "When set, overrides any other definition.")
  15742. (defvar org-agenda-view-columns-initially nil
  15743. "When set, switch to columns view immediately after creating the agenda.")
  15744. (defun org-agenda-columns ()
  15745. "Turn on column view in the agenda."
  15746. (interactive)
  15747. (org-verify-version 'columns)
  15748. (org-columns-remove-overlays)
  15749. (move-marker org-columns-begin-marker (point))
  15750. (let (fmt cache maxwidths m)
  15751. (cond
  15752. ((and (local-variable-p 'org-overriding-columns-format)
  15753. org-overriding-columns-format)
  15754. (setq fmt org-overriding-columns-format))
  15755. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15756. (setq fmt (org-entry-get m "COLUMNS" t)))
  15757. ((and (boundp 'org-columns-current-fmt)
  15758. (local-variable-p 'org-columns-current-fmt)
  15759. org-columns-current-fmt)
  15760. (setq fmt org-columns-current-fmt))
  15761. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15762. (setq m (get-text-property m 'org-hd-marker))
  15763. (setq fmt (org-entry-get m "COLUMNS" t))))
  15764. (setq fmt (or fmt org-columns-default-format))
  15765. (org-set-local 'org-columns-current-fmt fmt)
  15766. (org-columns-compile-format fmt)
  15767. (save-excursion
  15768. ;; Get and cache the properties
  15769. (goto-char (point-min))
  15770. (while (not (eobp))
  15771. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15772. (get-text-property (point) 'org-marker)))
  15773. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15774. (beginning-of-line 2))
  15775. (when cache
  15776. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15777. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15778. (org-columns-display-here-title)
  15779. (mapc (lambda (x)
  15780. (goto-line (car x))
  15781. (org-columns-display-here (cdr x)))
  15782. cache)))))
  15783. (defun org-columns-get-autowidth-alist (s cache)
  15784. "Derive the maximum column widths from the format and the cache."
  15785. (let ((start 0) rtn)
  15786. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  15787. (push (cons (match-string 1 s) 1) rtn)
  15788. (setq start (match-end 0)))
  15789. (mapc (lambda (x)
  15790. (setcdr x (apply 'max
  15791. (mapcar
  15792. (lambda (y)
  15793. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15794. cache))))
  15795. rtn)
  15796. rtn))
  15797. (defun org-columns-compute-all ()
  15798. "Compute all columns that have operators defined."
  15799. (org-unmodified
  15800. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15801. (let ((columns org-columns-current-fmt-compiled) col)
  15802. (while (setq col (pop columns))
  15803. (when (nth 3 col)
  15804. (save-excursion
  15805. (org-columns-compute (car col)))))))
  15806. (defun org-columns-update (property)
  15807. "Recompute PROPERTY, and update the columns display for it."
  15808. (org-columns-compute property)
  15809. (let (fmt val pos)
  15810. (save-excursion
  15811. (mapc (lambda (ov)
  15812. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15813. (setq pos (org-overlay-start ov))
  15814. (goto-char pos)
  15815. (when (setq val (cdr (assoc property
  15816. (get-text-property
  15817. (point-at-bol) 'org-summaries))))
  15818. (setq fmt (org-overlay-get ov 'org-columns-format))
  15819. (org-overlay-put ov 'org-columns-value val)
  15820. (org-overlay-put ov 'display (format fmt val)))))
  15821. org-columns-overlays))))
  15822. (defun org-columns-compute (property)
  15823. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15824. (interactive)
  15825. (let* ((re (concat "^" outline-regexp))
  15826. (lmax 30) ; Does anyone use deeper levels???
  15827. (lsum (make-vector lmax 0))
  15828. (lflag (make-vector lmax nil))
  15829. (level 0)
  15830. (ass (assoc property org-columns-current-fmt-compiled))
  15831. (format (nth 4 ass))
  15832. (printf (nth 5 ass))
  15833. (beg org-columns-top-level-marker)
  15834. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15835. (save-excursion
  15836. ;; Find the region to compute
  15837. (goto-char beg)
  15838. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15839. (goto-char end)
  15840. ;; Walk the tree from the back and do the computations
  15841. (while (re-search-backward re beg t)
  15842. (setq sumpos (match-beginning 0)
  15843. last-level level
  15844. level (org-outline-level)
  15845. val (org-entry-get nil property)
  15846. valflag (and val (string-match "\\S-" val)))
  15847. (cond
  15848. ((< level last-level)
  15849. ;; put the sum of lower levels here as a property
  15850. (setq sum (aref lsum last-level) ; current sum
  15851. flag (aref lflag last-level) ; any valid entries from children?
  15852. str (org-column-number-to-string sum format printf)
  15853. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15854. useval (if flag str1 (if valflag val ""))
  15855. sum-alist (get-text-property sumpos 'org-summaries))
  15856. (if (assoc property sum-alist)
  15857. (setcdr (assoc property sum-alist) useval)
  15858. (push (cons property useval) sum-alist)
  15859. (org-unmodified
  15860. (add-text-properties sumpos (1+ sumpos)
  15861. (list 'org-summaries sum-alist))))
  15862. (when val
  15863. (org-entry-put nil property (if flag str val)))
  15864. ;; add current to current level accumulator
  15865. (when (or flag valflag)
  15866. (aset lsum level (+ (aref lsum level)
  15867. (if flag sum (org-column-string-to-number
  15868. (if flag str val) format))))
  15869. (aset lflag level t))
  15870. ;; clear accumulators for deeper levels
  15871. (loop for l from (1+ level) to (1- lmax) do
  15872. (aset lsum l 0)
  15873. (aset lflag l nil)))
  15874. ((>= level last-level)
  15875. ;; add what we have here to the accumulator for this level
  15876. (aset lsum level (+ (aref lsum level)
  15877. (org-column-string-to-number (or val "0") format)))
  15878. (and valflag (aset lflag level t)))
  15879. (t (error "This should not happen")))))))
  15880. (defun org-columns-redo ()
  15881. "Construct the column display again."
  15882. (interactive)
  15883. (message "Recomputing columns...")
  15884. (save-excursion
  15885. (if (marker-position org-columns-begin-marker)
  15886. (goto-char org-columns-begin-marker))
  15887. (org-columns-remove-overlays)
  15888. (if (org-mode-p)
  15889. (call-interactively 'org-columns)
  15890. (call-interactively 'org-agenda-columns)))
  15891. (message "Recomputing columns...done"))
  15892. (defun org-columns-not-in-agenda ()
  15893. (if (eq major-mode 'org-agenda-mode)
  15894. (error "This command is only allowed in Org-mode buffers")))
  15895. (defun org-string-to-number (s)
  15896. "Convert string to number, and interpret hh:mm:ss."
  15897. (if (not (string-match ":" s))
  15898. (string-to-number s)
  15899. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15900. (while l
  15901. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15902. sum)))
  15903. (defun org-column-number-to-string (n fmt &optional printf)
  15904. "Convert a computed column number to a string value, according to FMT."
  15905. (cond
  15906. ((eq fmt 'add_times)
  15907. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15908. (format "%d:%02d" h m)))
  15909. ((eq fmt 'checkbox)
  15910. (cond ((= n (floor n)) "[X]")
  15911. ((> n 1.) "[-]")
  15912. (t "[ ]")))
  15913. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  15914. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  15915. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  15916. (printf (format printf n))
  15917. ((eq fmt 'currency)
  15918. (format "%.2f" n))
  15919. (t (number-to-string n))))
  15920. (defun org-nofm-to-completion (n m &optional percent)
  15921. (if (not percent)
  15922. (format "[%d/%d]" n m)
  15923. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  15924. (defun org-column-string-to-number (s fmt)
  15925. "Convert a column value to a number that can be used for column computing."
  15926. (cond
  15927. ((string-match ":" s)
  15928. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15929. (while l
  15930. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15931. sum))
  15932. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  15933. (if (equal s "[X]") 1. 0.000001))
  15934. (t (string-to-number s))))
  15935. (defun org-columns-uncompile-format (cfmt)
  15936. "Turn the compiled columns format back into a string representation."
  15937. (let ((rtn "") e s prop title op width fmt printf)
  15938. (while (setq e (pop cfmt))
  15939. (setq prop (car e)
  15940. title (nth 1 e)
  15941. width (nth 2 e)
  15942. op (nth 3 e)
  15943. fmt (nth 4 e)
  15944. printf (nth 5 e))
  15945. (cond
  15946. ((eq fmt 'add_times) (setq op ":"))
  15947. ((eq fmt 'checkbox) (setq op "X"))
  15948. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  15949. ((eq fmt 'checkbox-percent) (setq op "X%"))
  15950. ((eq fmt 'add_numbers) (setq op "+"))
  15951. ((eq fmt 'currency) (setq op "$")))
  15952. (if (and op printf) (setq op (concat op ";" printf)))
  15953. (if (equal title prop) (setq title nil))
  15954. (setq s (concat "%" (if width (number-to-string width))
  15955. prop
  15956. (if title (concat "(" title ")"))
  15957. (if op (concat "{" op "}"))))
  15958. (setq rtn (concat rtn " " s)))
  15959. (org-trim rtn)))
  15960. (defun org-columns-compile-format (fmt)
  15961. "Turn a column format string into an alist of specifications.
  15962. The alist has one entry for each column in the format. The elements of
  15963. that list are:
  15964. property the property
  15965. title the title field for the columns
  15966. width the column width in characters, can be nil for automatic
  15967. operator the operator if any
  15968. format the output format for computed results, derived from operator
  15969. printf a printf format for computed values"
  15970. (let ((start 0) width prop title op f printf)
  15971. (setq org-columns-current-fmt-compiled nil)
  15972. (while (string-match
  15973. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15974. fmt start)
  15975. (setq start (match-end 0)
  15976. width (match-string 1 fmt)
  15977. prop (match-string 2 fmt)
  15978. title (or (match-string 3 fmt) prop)
  15979. op (match-string 4 fmt)
  15980. f nil
  15981. printf nil)
  15982. (if width (setq width (string-to-number width)))
  15983. (when (and op (string-match ";" op))
  15984. (setq printf (substring op (match-end 0))
  15985. op (substring op 0 (match-beginning 0))))
  15986. (cond
  15987. ((equal op "+") (setq f 'add_numbers))
  15988. ((equal op "$") (setq f 'currency))
  15989. ((equal op ":") (setq f 'add_times))
  15990. ((equal op "X") (setq f 'checkbox))
  15991. ((equal op "X/") (setq f 'checkbox-n-of-m))
  15992. ((equal op "X%") (setq f 'checkbox-percent))
  15993. )
  15994. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15995. (setq org-columns-current-fmt-compiled
  15996. (nreverse org-columns-current-fmt-compiled))))
  15997. ;;; Dynamic block for Column view
  15998. (defun org-columns-capture-view ()
  15999. "Get the column view of the current buffer and return it as a list.
  16000. The list will contains the title row and all other rows. Each row is
  16001. a list of fields."
  16002. (save-excursion
  16003. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  16004. (n (length title)) row tbl)
  16005. (goto-char (point-min))
  16006. (while (re-search-forward "^\\*+ " nil t)
  16007. (when (get-char-property (match-beginning 0) 'org-columns-key)
  16008. (setq row nil)
  16009. (loop for i from 0 to (1- n) do
  16010. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  16011. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  16012. "")
  16013. row))
  16014. (setq row (nreverse row))
  16015. (push row tbl)))
  16016. (append (list title 'hline) (nreverse tbl)))))
  16017. (defun org-dblock-write:columnview (params)
  16018. "Write the column view table.
  16019. PARAMS is a property list of parameters:
  16020. :width enforce same column widths with <N> specifiers.
  16021. :id the :ID: property of the entry where the columns view
  16022. should be built, as a string. When `local', call locally.
  16023. When `global' call column view with the cursor at the beginning
  16024. of the buffer (usually this means that the whole buffer switches
  16025. to column view).
  16026. :hlines When t, insert a hline before each item. When a number, insert
  16027. a hline before each level <= that number.
  16028. :vlines When t, make each column a colgroup to enforce vertical lines."
  16029. (let ((pos (move-marker (make-marker) (point)))
  16030. (hlines (plist-get params :hlines))
  16031. (vlines (plist-get params :vlines))
  16032. tbl id idpos nfields tmp)
  16033. (save-excursion
  16034. (save-restriction
  16035. (when (setq id (plist-get params :id))
  16036. (cond ((not id) nil)
  16037. ((eq id 'global) (goto-char (point-min)))
  16038. ((eq id 'local) nil)
  16039. ((setq idpos (org-find-entry-with-id id))
  16040. (goto-char idpos))
  16041. (t (error "Cannot find entry with :ID: %s" id))))
  16042. (org-columns)
  16043. (setq tbl (org-columns-capture-view))
  16044. (setq nfields (length (car tbl)))
  16045. (org-columns-quit)))
  16046. (goto-char pos)
  16047. (move-marker pos nil)
  16048. (when tbl
  16049. (when (plist-get params :hlines)
  16050. (setq tmp nil)
  16051. (while tbl
  16052. (if (eq (car tbl) 'hline)
  16053. (push (pop tbl) tmp)
  16054. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  16055. (if (and (not (eq (car tmp) 'hline))
  16056. (or (eq hlines t)
  16057. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  16058. (push 'hline tmp)))
  16059. (push (pop tbl) tmp)))
  16060. (setq tbl (nreverse tmp)))
  16061. (when vlines
  16062. (setq tbl (mapcar (lambda (x)
  16063. (if (eq 'hline x) x (cons "" x)))
  16064. tbl))
  16065. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  16066. (setq pos (point))
  16067. (insert (org-listtable-to-string tbl))
  16068. (when (plist-get params :width)
  16069. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  16070. org-columns-current-widths "|")))
  16071. (goto-char pos)
  16072. (org-table-align))))
  16073. (defun org-listtable-to-string (tbl)
  16074. "Convert a listtable TBL to a string that contains the Org-mode table.
  16075. The table still need to be alligned. The resulting string has no leading
  16076. and tailing newline characters."
  16077. (mapconcat
  16078. (lambda (x)
  16079. (cond
  16080. ((listp x)
  16081. (concat "|" (mapconcat 'identity x "|") "|"))
  16082. ((eq x 'hline) "|-|")
  16083. (t (error "Garbage in listtable: %s" x))))
  16084. tbl "\n"))
  16085. (defun org-insert-columns-dblock ()
  16086. "Create a dynamic block capturing a column view table."
  16087. (interactive)
  16088. (let ((defaults '(:name "columnview" :hlines 1))
  16089. (id (completing-read
  16090. "Capture columns (local, global, entry with :ID: property) [local]: "
  16091. (append '(("global") ("local"))
  16092. (mapcar 'list (org-property-values "ID"))))))
  16093. (if (equal id "") (setq id 'local))
  16094. (if (equal id "global") (setq id 'global))
  16095. (setq defaults (append defaults (list :id id)))
  16096. (org-create-dblock defaults)
  16097. (org-update-dblock)))
  16098. ;;;; Timestamps
  16099. (defvar org-last-changed-timestamp nil)
  16100. (defvar org-time-was-given) ; dynamically scoped parameter
  16101. (defvar org-end-time-was-given) ; dynamically scoped parameter
  16102. (defvar org-ts-what) ; dynamically scoped parameter
  16103. (defun org-time-stamp (arg)
  16104. "Prompt for a date/time and insert a time stamp.
  16105. If the user specifies a time like HH:MM, or if this command is called
  16106. with a prefix argument, the time stamp will contain date and time.
  16107. Otherwise, only the date will be included. All parts of a date not
  16108. specified by the user will be filled in from the current date/time.
  16109. So if you press just return without typing anything, the time stamp
  16110. will represent the current date/time. If there is already a timestamp
  16111. at the cursor, it will be modified."
  16112. (interactive "P")
  16113. (let* ((ts nil)
  16114. (default-time
  16115. ;; Default time is either today, or, when entering a range,
  16116. ;; the range start.
  16117. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  16118. (save-excursion
  16119. (re-search-backward
  16120. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  16121. (- (point) 20) t)))
  16122. (apply 'encode-time (org-parse-time-string (match-string 1)))
  16123. (current-time)))
  16124. (default-input (and ts (org-get-compact-tod ts)))
  16125. org-time-was-given org-end-time-was-given time)
  16126. (cond
  16127. ((and (org-at-timestamp-p)
  16128. (eq last-command 'org-time-stamp)
  16129. (eq this-command 'org-time-stamp))
  16130. (insert "--")
  16131. (setq time (let ((this-command this-command))
  16132. (org-read-date arg 'totime nil nil default-time default-input)))
  16133. (org-insert-time-stamp time (or org-time-was-given arg)))
  16134. ((org-at-timestamp-p)
  16135. (setq time (let ((this-command this-command))
  16136. (org-read-date arg 'totime nil nil default-time default-input)))
  16137. (when (org-at-timestamp-p) ; just to get the match data
  16138. (replace-match "")
  16139. (setq org-last-changed-timestamp
  16140. (org-insert-time-stamp
  16141. time (or org-time-was-given arg)
  16142. nil nil nil (list org-end-time-was-given))))
  16143. (message "Timestamp updated"))
  16144. (t
  16145. (setq time (let ((this-command this-command))
  16146. (org-read-date arg 'totime nil nil default-time default-input)))
  16147. (org-insert-time-stamp time (or org-time-was-given arg)
  16148. nil nil nil (list org-end-time-was-given))))))
  16149. ;; FIXME: can we use this for something else????
  16150. ;; like computing time differences?????
  16151. (defun org-get-compact-tod (s)
  16152. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  16153. (let* ((t1 (match-string 1 s))
  16154. (h1 (string-to-number (match-string 2 s)))
  16155. (m1 (string-to-number (match-string 3 s)))
  16156. (t2 (and (match-end 4) (match-string 5 s)))
  16157. (h2 (and t2 (string-to-number (match-string 6 s))))
  16158. (m2 (and t2 (string-to-number (match-string 7 s))))
  16159. dh dm)
  16160. (if (not t2)
  16161. t1
  16162. (setq dh (- h2 h1) dm (- m2 m1))
  16163. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  16164. (concat t1 "+" (number-to-string dh)
  16165. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  16166. (defun org-time-stamp-inactive (&optional arg)
  16167. "Insert an inactive time stamp.
  16168. An inactive time stamp is enclosed in square brackets instead of angle
  16169. brackets. It is inactive in the sense that it does not trigger agenda entries,
  16170. does not link to the calendar and cannot be changed with the S-cursor keys.
  16171. So these are more for recording a certain time/date."
  16172. (interactive "P")
  16173. (let (org-time-was-given org-end-time-was-given time)
  16174. (setq time (org-read-date arg 'totime))
  16175. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  16176. nil nil (list org-end-time-was-given))))
  16177. (defvar org-date-ovl (org-make-overlay 1 1))
  16178. (org-overlay-put org-date-ovl 'face 'org-warning)
  16179. (org-detach-overlay org-date-ovl)
  16180. (defvar org-ans1) ; dynamically scoped parameter
  16181. (defvar org-ans2) ; dynamically scoped parameter
  16182. (defvar org-plain-time-of-day-regexp) ; defined below
  16183. (defvar org-read-date-overlay nil)
  16184. (defvar org-dcst nil) ; dynamically scoped
  16185. (defun org-read-date (&optional with-time to-time from-string prompt
  16186. default-time default-input)
  16187. "Read a date, possibly a time, and make things smooth for the user.
  16188. The prompt will suggest to enter an ISO date, but you can also enter anything
  16189. which will at least partially be understood by `parse-time-string'.
  16190. Unrecognized parts of the date will default to the current day, month, year,
  16191. hour and minute. If this command is called to replace a timestamp at point,
  16192. of to enter the second timestamp of a range, the default time is taken from the
  16193. existing stamp. For example,
  16194. 3-2-5 --> 2003-02-05
  16195. feb 15 --> currentyear-02-15
  16196. sep 12 9 --> 2009-09-12
  16197. 12:45 --> today 12:45
  16198. 22 sept 0:34 --> currentyear-09-22 0:34
  16199. 12 --> currentyear-currentmonth-12
  16200. Fri --> nearest Friday (today or later)
  16201. etc.
  16202. Furthermore you can specify a relative date by giving, as the *first* thing
  16203. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16204. change in days weeks, months, years.
  16205. With a single plus or minus, the date is relative to today. With a double
  16206. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16207. +4d --> four days from today
  16208. +4 --> same as above
  16209. +2w --> two weeks from today
  16210. ++5 --> five days from default date
  16211. The function understands only English month and weekday abbreviations,
  16212. but this can be configured with the variables `parse-time-months' and
  16213. `parse-time-weekdays'.
  16214. While prompting, a calendar is popped up - you can also select the
  16215. date with the mouse (button 1). The calendar shows a period of three
  16216. months. To scroll it to other months, use the keys `>' and `<'.
  16217. If you don't like the calendar, turn it off with
  16218. \(setq org-read-date-popup-calendar nil)
  16219. With optional argument TO-TIME, the date will immediately be converted
  16220. to an internal time.
  16221. With an optional argument WITH-TIME, the prompt will suggest to also
  16222. insert a time. Note that when WITH-TIME is not set, you can still
  16223. enter a time, and this function will inform the calling routine about
  16224. this change. The calling routine may then choose to change the format
  16225. used to insert the time stamp into the buffer to include the time.
  16226. With optional argument FROM-STRING, read from this string instead from
  16227. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16228. the time/date that is used for everything that is not specified by the
  16229. user."
  16230. (require 'parse-time)
  16231. (let* ((org-time-stamp-rounding-minutes
  16232. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  16233. (org-dcst org-display-custom-times)
  16234. (ct (org-current-time))
  16235. (def (or default-time ct))
  16236. (defdecode (decode-time def))
  16237. (dummy (progn
  16238. (when (< (nth 2 defdecode) org-extend-today-until)
  16239. (setcar (nthcdr 2 defdecode) -1)
  16240. (setcar (nthcdr 1 defdecode) 59)
  16241. (setq def (apply 'encode-time defdecode)
  16242. defdecode (decode-time def)))))
  16243. (calendar-move-hook nil)
  16244. (view-diary-entries-initially nil)
  16245. (view-calendar-holidays-initially nil)
  16246. (timestr (format-time-string
  16247. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16248. (prompt (concat (if prompt (concat prompt " ") "")
  16249. (format "Date+time [%s]: " timestr)))
  16250. ans (org-ans0 "") org-ans1 org-ans2 final)
  16251. (cond
  16252. (from-string (setq ans from-string))
  16253. (org-read-date-popup-calendar
  16254. (save-excursion
  16255. (save-window-excursion
  16256. (calendar)
  16257. (calendar-forward-day (- (time-to-days def)
  16258. (calendar-absolute-from-gregorian
  16259. (calendar-current-date))))
  16260. (org-eval-in-calendar nil t)
  16261. (let* ((old-map (current-local-map))
  16262. (map (copy-keymap calendar-mode-map))
  16263. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16264. (org-defkey map (kbd "RET") 'org-calendar-select)
  16265. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16266. 'org-calendar-select-mouse)
  16267. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16268. 'org-calendar-select-mouse)
  16269. (org-defkey minibuffer-local-map [(meta shift left)]
  16270. (lambda () (interactive)
  16271. (org-eval-in-calendar '(calendar-backward-month 1))))
  16272. (org-defkey minibuffer-local-map [(meta shift right)]
  16273. (lambda () (interactive)
  16274. (org-eval-in-calendar '(calendar-forward-month 1))))
  16275. (org-defkey minibuffer-local-map [(meta shift up)]
  16276. (lambda () (interactive)
  16277. (org-eval-in-calendar '(calendar-backward-year 1))))
  16278. (org-defkey minibuffer-local-map [(meta shift down)]
  16279. (lambda () (interactive)
  16280. (org-eval-in-calendar '(calendar-forward-year 1))))
  16281. (org-defkey minibuffer-local-map [(shift up)]
  16282. (lambda () (interactive)
  16283. (org-eval-in-calendar '(calendar-backward-week 1))))
  16284. (org-defkey minibuffer-local-map [(shift down)]
  16285. (lambda () (interactive)
  16286. (org-eval-in-calendar '(calendar-forward-week 1))))
  16287. (org-defkey minibuffer-local-map [(shift left)]
  16288. (lambda () (interactive)
  16289. (org-eval-in-calendar '(calendar-backward-day 1))))
  16290. (org-defkey minibuffer-local-map [(shift right)]
  16291. (lambda () (interactive)
  16292. (org-eval-in-calendar '(calendar-forward-day 1))))
  16293. (org-defkey minibuffer-local-map ">"
  16294. (lambda () (interactive)
  16295. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16296. (org-defkey minibuffer-local-map "<"
  16297. (lambda () (interactive)
  16298. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16299. (unwind-protect
  16300. (progn
  16301. (use-local-map map)
  16302. (add-hook 'post-command-hook 'org-read-date-display)
  16303. (setq org-ans0 (read-string prompt default-input nil nil))
  16304. ;; org-ans0: from prompt
  16305. ;; org-ans1: from mouse click
  16306. ;; org-ans2: from calendar motion
  16307. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16308. (remove-hook 'post-command-hook 'org-read-date-display)
  16309. (use-local-map old-map)
  16310. (when org-read-date-overlay
  16311. (org-delete-overlay org-read-date-overlay)
  16312. (setq org-read-date-overlay nil)))))))
  16313. (t ; Naked prompt only
  16314. (unwind-protect
  16315. (setq ans (read-string prompt default-input nil timestr))
  16316. (when org-read-date-overlay
  16317. (org-delete-overlay org-read-date-overlay)
  16318. (setq org-read-date-overlay nil)))))
  16319. (setq final (org-read-date-analyze ans def defdecode))
  16320. (if to-time
  16321. (apply 'encode-time final)
  16322. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16323. (format "%04d-%02d-%02d %02d:%02d"
  16324. (nth 5 final) (nth 4 final) (nth 3 final)
  16325. (nth 2 final) (nth 1 final))
  16326. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16327. (defvar def)
  16328. (defvar defdecode)
  16329. (defvar with-time)
  16330. (defun org-read-date-display ()
  16331. "Display the currrent date prompt interpretation in the minibuffer."
  16332. (when org-read-date-display-live
  16333. (when org-read-date-overlay
  16334. (org-delete-overlay org-read-date-overlay))
  16335. (let ((p (point)))
  16336. (end-of-line 1)
  16337. (while (not (equal (buffer-substring
  16338. (max (point-min) (- (point) 4)) (point))
  16339. " "))
  16340. (insert " "))
  16341. (goto-char p))
  16342. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16343. " " (or org-ans1 org-ans2)))
  16344. (org-end-time-was-given nil)
  16345. (f (org-read-date-analyze ans def defdecode))
  16346. (fmts (if org-dcst
  16347. org-time-stamp-custom-formats
  16348. org-time-stamp-formats))
  16349. (fmt (if (or with-time
  16350. (and (boundp 'org-time-was-given) org-time-was-given))
  16351. (cdr fmts)
  16352. (car fmts)))
  16353. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16354. (when (and org-end-time-was-given
  16355. (string-match org-plain-time-of-day-regexp txt))
  16356. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16357. org-end-time-was-given
  16358. (substring txt (match-end 0)))))
  16359. (setq org-read-date-overlay
  16360. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16361. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16362. (defun org-read-date-analyze (ans def defdecode)
  16363. "Analyze the combined answer of the date prompt."
  16364. ;; FIXME: cleanup and comment
  16365. (let (delta deltan deltaw deltadef year month day
  16366. hour minute second wday pm h2 m2 tl wday1)
  16367. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16368. (setq ans (replace-match "" t t ans)
  16369. deltan (car delta)
  16370. deltaw (nth 1 delta)
  16371. deltadef (nth 2 delta)))
  16372. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16373. (when (string-match
  16374. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16375. (setq year (if (match-end 2)
  16376. (string-to-number (match-string 2 ans))
  16377. (string-to-number (format-time-string "%Y")))
  16378. month (string-to-number (match-string 3 ans))
  16379. day (string-to-number (match-string 4 ans)))
  16380. (if (< year 100) (setq year (+ 2000 year)))
  16381. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16382. t nil ans)))
  16383. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16384. ;; If there is a time with am/pm, and *no* time without it, we convert
  16385. ;; so that matching will be successful.
  16386. (loop for i from 1 to 2 do ; twice, for end time as well
  16387. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16388. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16389. (setq hour (string-to-number (match-string 1 ans))
  16390. minute (if (match-end 3)
  16391. (string-to-number (match-string 3 ans))
  16392. 0)
  16393. pm (equal ?p
  16394. (string-to-char (downcase (match-string 4 ans)))))
  16395. (if (and (= hour 12) (not pm))
  16396. (setq hour 0)
  16397. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16398. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16399. t t ans))))
  16400. ;; Check if a time range is given as a duration
  16401. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16402. (setq hour (string-to-number (match-string 1 ans))
  16403. h2 (+ hour (string-to-number (match-string 3 ans)))
  16404. minute (string-to-number (match-string 2 ans))
  16405. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16406. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16407. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16408. ;; Check if there is a time range
  16409. (when (boundp 'org-end-time-was-given)
  16410. (setq org-time-was-given nil)
  16411. (when (and (string-match org-plain-time-of-day-regexp ans)
  16412. (match-end 8))
  16413. (setq org-end-time-was-given (match-string 8 ans))
  16414. (setq ans (concat (substring ans 0 (match-beginning 7))
  16415. (substring ans (match-end 7))))))
  16416. (setq tl (parse-time-string ans)
  16417. day (or (nth 3 tl) (nth 3 defdecode))
  16418. month (or (nth 4 tl)
  16419. (if (and org-read-date-prefer-future
  16420. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16421. (1+ (nth 4 defdecode))
  16422. (nth 4 defdecode)))
  16423. year (or (nth 5 tl)
  16424. (if (and org-read-date-prefer-future
  16425. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16426. (1+ (nth 5 defdecode))
  16427. (nth 5 defdecode)))
  16428. hour (or (nth 2 tl) (nth 2 defdecode))
  16429. minute (or (nth 1 tl) (nth 1 defdecode))
  16430. second (or (nth 0 tl) 0)
  16431. wday (nth 6 tl))
  16432. (when deltan
  16433. (unless deltadef
  16434. (let ((now (decode-time (current-time))))
  16435. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16436. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16437. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16438. ((equal deltaw "m") (setq month (+ month deltan)))
  16439. ((equal deltaw "y") (setq year (+ year deltan)))))
  16440. (when (and wday (not (nth 3 tl)))
  16441. ;; Weekday was given, but no day, so pick that day in the week
  16442. ;; on or after the derived date.
  16443. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16444. (unless (equal wday wday1)
  16445. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16446. (if (and (boundp 'org-time-was-given)
  16447. (nth 2 tl))
  16448. (setq org-time-was-given t))
  16449. (if (< year 100) (setq year (+ 2000 year)))
  16450. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16451. (list second minute hour day month year)))
  16452. (defvar parse-time-weekdays)
  16453. (defun org-read-date-get-relative (s today default)
  16454. "Check string S for special relative date string.
  16455. TODAY and DEFAULT are internal times, for today and for a default.
  16456. Return shift list (N what def-flag)
  16457. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16458. N is the number of WHATs to shift.
  16459. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16460. the DEFAULT date rather than TODAY."
  16461. (when (string-match
  16462. (concat
  16463. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16464. "\\([0-9]+\\)?"
  16465. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16466. "\\([ \t]\\|$\\)") s)
  16467. (let* ((dir (if (match-end 1)
  16468. (string-to-char (substring (match-string 1 s) -1))
  16469. ?+))
  16470. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16471. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16472. (what (if (match-end 3) (match-string 3 s) "d"))
  16473. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16474. (date (if rel default today))
  16475. (wday (nth 6 (decode-time date)))
  16476. delta)
  16477. (if wday1
  16478. (progn
  16479. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16480. (if (= dir ?-) (setq delta (- delta 7)))
  16481. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16482. (list delta "d" rel))
  16483. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16484. (defun org-eval-in-calendar (form &optional keepdate)
  16485. "Eval FORM in the calendar window and return to current window.
  16486. Also, store the cursor date in variable org-ans2."
  16487. (let ((sw (selected-window)))
  16488. (select-window (get-buffer-window "*Calendar*"))
  16489. (eval form)
  16490. (when (and (not keepdate) (calendar-cursor-to-date))
  16491. (let* ((date (calendar-cursor-to-date))
  16492. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16493. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16494. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16495. (select-window sw)))
  16496. ; ;; Update the prompt to show new default date
  16497. ; (save-excursion
  16498. ; (goto-char (point-min))
  16499. ; (when (and org-ans2
  16500. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16501. ; (get-text-property (match-end 0) 'field))
  16502. ; (let ((inhibit-read-only t))
  16503. ; (replace-match (concat "[" org-ans2 "]") t t)
  16504. ; (add-text-properties (point-min) (1+ (match-end 0))
  16505. ; (text-properties-at (1+ (point-min)))))))))
  16506. (defun org-calendar-select ()
  16507. "Return to `org-read-date' with the date currently selected.
  16508. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16509. (interactive)
  16510. (when (calendar-cursor-to-date)
  16511. (let* ((date (calendar-cursor-to-date))
  16512. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16513. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16514. (if (active-minibuffer-window) (exit-minibuffer))))
  16515. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16516. "Insert a date stamp for the date given by the internal TIME.
  16517. WITH-HM means, use the stamp format that includes the time of the day.
  16518. INACTIVE means use square brackets instead of angular ones, so that the
  16519. stamp will not contribute to the agenda.
  16520. PRE and POST are optional strings to be inserted before and after the
  16521. stamp.
  16522. The command returns the inserted time stamp."
  16523. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16524. stamp)
  16525. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16526. (insert-before-markers (or pre ""))
  16527. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16528. (when (listp extra)
  16529. (setq extra (car extra))
  16530. (if (and (stringp extra)
  16531. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16532. (setq extra (format "-%02d:%02d"
  16533. (string-to-number (match-string 1 extra))
  16534. (string-to-number (match-string 2 extra))))
  16535. (setq extra nil)))
  16536. (when extra
  16537. (backward-char 1)
  16538. (insert-before-markers extra)
  16539. (forward-char 1))
  16540. (insert-before-markers (or post ""))
  16541. stamp))
  16542. (defun org-toggle-time-stamp-overlays ()
  16543. "Toggle the use of custom time stamp formats."
  16544. (interactive)
  16545. (setq org-display-custom-times (not org-display-custom-times))
  16546. (unless org-display-custom-times
  16547. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16548. (while (setq p (next-single-property-change p 'display))
  16549. (if (and (get-text-property p 'display)
  16550. (eq (get-text-property p 'face) 'org-date))
  16551. (remove-text-properties
  16552. p (setq p (next-single-property-change p 'display))
  16553. '(display t))))
  16554. (set-buffer-modified-p bmp)))
  16555. (if (featurep 'xemacs)
  16556. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16557. (org-restart-font-lock)
  16558. (setq org-table-may-need-update t)
  16559. (if org-display-custom-times
  16560. (message "Time stamps are overlayed with custom format")
  16561. (message "Time stamp overlays removed")))
  16562. (defun org-display-custom-time (beg end)
  16563. "Overlay modified time stamp format over timestamp between BED and END."
  16564. (let* ((ts (buffer-substring beg end))
  16565. t1 w1 with-hm tf time str w2 (off 0))
  16566. (save-match-data
  16567. (setq t1 (org-parse-time-string ts t))
  16568. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16569. (setq off (- (match-end 0) (match-beginning 0)))))
  16570. (setq end (- end off))
  16571. (setq w1 (- end beg)
  16572. with-hm (and (nth 1 t1) (nth 2 t1))
  16573. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16574. time (org-fix-decoded-time t1)
  16575. str (org-add-props
  16576. (format-time-string
  16577. (substring tf 1 -1) (apply 'encode-time time))
  16578. nil 'mouse-face 'highlight)
  16579. w2 (length str))
  16580. (if (not (= w2 w1))
  16581. (add-text-properties (1+ beg) (+ 2 beg)
  16582. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16583. (if (featurep 'xemacs)
  16584. (progn
  16585. (put-text-property beg end 'invisible t)
  16586. (put-text-property beg end 'end-glyph (make-glyph str)))
  16587. (put-text-property beg end 'display str))))
  16588. (defun org-translate-time (string)
  16589. "Translate all timestamps in STRING to custom format.
  16590. But do this only if the variable `org-display-custom-times' is set."
  16591. (when org-display-custom-times
  16592. (save-match-data
  16593. (let* ((start 0)
  16594. (re org-ts-regexp-both)
  16595. t1 with-hm inactive tf time str beg end)
  16596. (while (setq start (string-match re string start))
  16597. (setq beg (match-beginning 0)
  16598. end (match-end 0)
  16599. t1 (save-match-data
  16600. (org-parse-time-string (substring string beg end) t))
  16601. with-hm (and (nth 1 t1) (nth 2 t1))
  16602. inactive (equal (substring string beg (1+ beg)) "[")
  16603. tf (funcall (if with-hm 'cdr 'car)
  16604. org-time-stamp-custom-formats)
  16605. time (org-fix-decoded-time t1)
  16606. str (format-time-string
  16607. (concat
  16608. (if inactive "[" "<") (substring tf 1 -1)
  16609. (if inactive "]" ">"))
  16610. (apply 'encode-time time))
  16611. string (replace-match str t t string)
  16612. start (+ start (length str)))))))
  16613. string)
  16614. (defun org-fix-decoded-time (time)
  16615. "Set 0 instead of nil for the first 6 elements of time.
  16616. Don't touch the rest."
  16617. (let ((n 0))
  16618. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16619. (defun org-days-to-time (timestamp-string)
  16620. "Difference between TIMESTAMP-STRING and now in days."
  16621. (- (time-to-days (org-time-string-to-time timestamp-string))
  16622. (time-to-days (current-time))))
  16623. (defun org-deadline-close (timestamp-string &optional ndays)
  16624. "Is the time in TIMESTAMP-STRING close to the current date?"
  16625. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16626. (and (< (org-days-to-time timestamp-string) ndays)
  16627. (not (org-entry-is-done-p))))
  16628. (defun org-get-wdays (ts)
  16629. "Get the deadline lead time appropriate for timestring TS."
  16630. (cond
  16631. ((<= org-deadline-warning-days 0)
  16632. ;; 0 or negative, enforce this value no matter what
  16633. (- org-deadline-warning-days))
  16634. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16635. ;; lead time is specified.
  16636. (floor (* (string-to-number (match-string 1 ts))
  16637. (cdr (assoc (match-string 2 ts)
  16638. '(("d" . 1) ("w" . 7)
  16639. ("m" . 30.4) ("y" . 365.25)))))))
  16640. ;; go for the default.
  16641. (t org-deadline-warning-days)))
  16642. (defun org-calendar-select-mouse (ev)
  16643. "Return to `org-read-date' with the date currently selected.
  16644. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16645. (interactive "e")
  16646. (mouse-set-point ev)
  16647. (when (calendar-cursor-to-date)
  16648. (let* ((date (calendar-cursor-to-date))
  16649. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16650. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16651. (if (active-minibuffer-window) (exit-minibuffer))))
  16652. (defun org-check-deadlines (ndays)
  16653. "Check if there are any deadlines due or past due.
  16654. A deadline is considered due if it happens within `org-deadline-warning-days'
  16655. days from today's date. If the deadline appears in an entry marked DONE,
  16656. it is not shown. The prefix arg NDAYS can be used to test that many
  16657. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16658. (interactive "P")
  16659. (let* ((org-warn-days
  16660. (cond
  16661. ((equal ndays '(4)) 100000)
  16662. (ndays (prefix-numeric-value ndays))
  16663. (t (abs org-deadline-warning-days))))
  16664. (case-fold-search nil)
  16665. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16666. (callback
  16667. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16668. (message "%d deadlines past-due or due within %d days"
  16669. (org-occur regexp nil callback)
  16670. org-warn-days)))
  16671. (defun org-check-before-date (date)
  16672. "Check if there are deadlines or scheduled entries before DATE."
  16673. (interactive (list (org-read-date)))
  16674. (let ((case-fold-search nil)
  16675. (regexp (concat "\\<\\(" org-deadline-string
  16676. "\\|" org-scheduled-string
  16677. "\\) *<\\([^>]+\\)>"))
  16678. (callback
  16679. (lambda () (time-less-p
  16680. (org-time-string-to-time (match-string 2))
  16681. (org-time-string-to-time date)))))
  16682. (message "%d entries before %s"
  16683. (org-occur regexp nil callback) date)))
  16684. (defun org-evaluate-time-range (&optional to-buffer)
  16685. "Evaluate a time range by computing the difference between start and end.
  16686. Normally the result is just printed in the echo area, but with prefix arg
  16687. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16688. If the time range is actually in a table, the result is inserted into the
  16689. next column.
  16690. For time difference computation, a year is assumed to be exactly 365
  16691. days in order to avoid rounding problems."
  16692. (interactive "P")
  16693. (or
  16694. (org-clock-update-time-maybe)
  16695. (save-excursion
  16696. (unless (org-at-date-range-p t)
  16697. (goto-char (point-at-bol))
  16698. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16699. (if (not (org-at-date-range-p t))
  16700. (error "Not at a time-stamp range, and none found in current line")))
  16701. (let* ((ts1 (match-string 1))
  16702. (ts2 (match-string 2))
  16703. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16704. (match-end (match-end 0))
  16705. (time1 (org-time-string-to-time ts1))
  16706. (time2 (org-time-string-to-time ts2))
  16707. (t1 (time-to-seconds time1))
  16708. (t2 (time-to-seconds time2))
  16709. (diff (abs (- t2 t1)))
  16710. (negative (< (- t2 t1) 0))
  16711. ;; (ys (floor (* 365 24 60 60)))
  16712. (ds (* 24 60 60))
  16713. (hs (* 60 60))
  16714. (fy "%dy %dd %02d:%02d")
  16715. (fy1 "%dy %dd")
  16716. (fd "%dd %02d:%02d")
  16717. (fd1 "%dd")
  16718. (fh "%02d:%02d")
  16719. y d h m align)
  16720. (if havetime
  16721. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16722. y 0
  16723. d (floor (/ diff ds)) diff (mod diff ds)
  16724. h (floor (/ diff hs)) diff (mod diff hs)
  16725. m (floor (/ diff 60)))
  16726. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16727. y 0
  16728. d (floor (+ (/ diff ds) 0.5))
  16729. h 0 m 0))
  16730. (if (not to-buffer)
  16731. (message "%s" (org-make-tdiff-string y d h m))
  16732. (if (org-at-table-p)
  16733. (progn
  16734. (goto-char match-end)
  16735. (setq align t)
  16736. (and (looking-at " *|") (goto-char (match-end 0))))
  16737. (goto-char match-end))
  16738. (if (looking-at
  16739. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16740. (replace-match ""))
  16741. (if negative (insert " -"))
  16742. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16743. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16744. (insert " " (format fh h m))))
  16745. (if align (org-table-align))
  16746. (message "Time difference inserted")))))
  16747. (defun org-make-tdiff-string (y d h m)
  16748. (let ((fmt "")
  16749. (l nil))
  16750. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16751. l (push y l)))
  16752. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16753. l (push d l)))
  16754. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16755. l (push h l)))
  16756. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16757. l (push m l)))
  16758. (apply 'format fmt (nreverse l))))
  16759. (defun org-time-string-to-time (s)
  16760. (apply 'encode-time (org-parse-time-string s)))
  16761. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16762. "Convert a time stamp to an absolute day number.
  16763. If there is a specifyer for a cyclic time stamp, get the closest date to
  16764. DAYNR."
  16765. (cond
  16766. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16767. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16768. daynr
  16769. (+ daynr 1000)))
  16770. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16771. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16772. (time-to-days (current-time))) (match-string 0 s)
  16773. prefer))
  16774. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16775. (defun org-time-from-absolute (d)
  16776. "Return the time corresponding to date D.
  16777. D may be an absolute day number, or a calendar-type list (month day year)."
  16778. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16779. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16780. (defun org-calendar-holiday ()
  16781. "List of holidays, for Diary display in Org-mode."
  16782. (require 'holidays)
  16783. (let ((hl (funcall
  16784. (if (fboundp 'calendar-check-holidays)
  16785. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16786. (if hl (mapconcat 'identity hl "; "))))
  16787. (defun org-diary-sexp-entry (sexp entry date)
  16788. "Process a SEXP diary ENTRY for DATE."
  16789. (require 'diary-lib)
  16790. (let ((result (if calendar-debug-sexp
  16791. (let ((stack-trace-on-error t))
  16792. (eval (car (read-from-string sexp))))
  16793. (condition-case nil
  16794. (eval (car (read-from-string sexp)))
  16795. (error
  16796. (beep)
  16797. (message "Bad sexp at line %d in %s: %s"
  16798. (org-current-line)
  16799. (buffer-file-name) sexp)
  16800. (sleep-for 2))))))
  16801. (cond ((stringp result) result)
  16802. ((and (consp result)
  16803. (stringp (cdr result))) (cdr result))
  16804. (result entry)
  16805. (t nil))))
  16806. (defun org-diary-to-ical-string (frombuf)
  16807. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16808. This uses the icalendar.el library."
  16809. (let* ((tmpdir (if (featurep 'xemacs)
  16810. (temp-directory)
  16811. temporary-file-directory))
  16812. (tmpfile (make-temp-name
  16813. (expand-file-name "orgics" tmpdir)))
  16814. buf rtn b e)
  16815. (save-excursion
  16816. (set-buffer frombuf)
  16817. (icalendar-export-region (point-min) (point-max) tmpfile)
  16818. (setq buf (find-buffer-visiting tmpfile))
  16819. (set-buffer buf)
  16820. (goto-char (point-min))
  16821. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16822. (setq b (match-beginning 0)))
  16823. (goto-char (point-max))
  16824. (if (re-search-backward "^END:VEVENT" nil t)
  16825. (setq e (match-end 0)))
  16826. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16827. (kill-buffer buf)
  16828. (kill-buffer frombuf)
  16829. (delete-file tmpfile)
  16830. rtn))
  16831. (defun org-closest-date (start current change prefer)
  16832. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16833. When PREFER is `past' return a date that is either CURRENT or past.
  16834. When PREFER is `future', return a date that is either CURRENT or future."
  16835. ;; Make the proper lists from the dates
  16836. (catch 'exit
  16837. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16838. dn dw sday cday n1 n2
  16839. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16840. (setq start (org-date-to-gregorian start)
  16841. current (org-date-to-gregorian
  16842. (if org-agenda-repeating-timestamp-show-all
  16843. current
  16844. (time-to-days (current-time))))
  16845. sday (calendar-absolute-from-gregorian start)
  16846. cday (calendar-absolute-from-gregorian current))
  16847. (if (<= cday sday) (throw 'exit sday))
  16848. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16849. (setq dn (string-to-number (match-string 1 change))
  16850. dw (cdr (assoc (match-string 2 change) a1)))
  16851. (error "Invalid change specifyer: %s" change))
  16852. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16853. (cond
  16854. ((eq dw 'day)
  16855. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16856. n2 (+ n1 dn)))
  16857. ((eq dw 'year)
  16858. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16859. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16860. (setq date1 (list m d y1)
  16861. n1 (calendar-absolute-from-gregorian date1)
  16862. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16863. n2 (calendar-absolute-from-gregorian date2)))
  16864. ((eq dw 'month)
  16865. ;; approx number of month between the tow dates
  16866. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16867. ;; How often does dn fit in there?
  16868. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16869. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16870. m (+ m nm)
  16871. ny (floor (/ m 12))
  16872. y (+ y ny)
  16873. m (- m (* ny 12)))
  16874. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16875. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16876. (setq m2 (+ m dn) y2 y)
  16877. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16878. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16879. (while (< n2 cday)
  16880. (setq n1 n2 m m2 y y2)
  16881. (setq m2 (+ m dn) y2 y)
  16882. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16883. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16884. (if org-agenda-repeating-timestamp-show-all
  16885. (cond
  16886. ((eq prefer 'past) n1)
  16887. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16888. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16889. (cond
  16890. ((eq prefer 'past) n1)
  16891. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16892. (t (if (= cday n1) n1 n2)))))))
  16893. (defun org-date-to-gregorian (date)
  16894. "Turn any specification of DATE into a gregorian date for the calendar."
  16895. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16896. ((and (listp date) (= (length date) 3)) date)
  16897. ((stringp date)
  16898. (setq date (org-parse-time-string date))
  16899. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16900. ((listp date)
  16901. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16902. (defun org-parse-time-string (s &optional nodefault)
  16903. "Parse the standard Org-mode time string.
  16904. This should be a lot faster than the normal `parse-time-string'.
  16905. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16906. hour and minute fields will be nil if not given."
  16907. (if (string-match org-ts-regexp0 s)
  16908. (list 0
  16909. (if (or (match-beginning 8) (not nodefault))
  16910. (string-to-number (or (match-string 8 s) "0")))
  16911. (if (or (match-beginning 7) (not nodefault))
  16912. (string-to-number (or (match-string 7 s) "0")))
  16913. (string-to-number (match-string 4 s))
  16914. (string-to-number (match-string 3 s))
  16915. (string-to-number (match-string 2 s))
  16916. nil nil nil)
  16917. (make-list 9 0)))
  16918. (defun org-timestamp-up (&optional arg)
  16919. "Increase the date item at the cursor by one.
  16920. If the cursor is on the year, change the year. If it is on the month or
  16921. the day, change that.
  16922. With prefix ARG, change by that many units."
  16923. (interactive "p")
  16924. (org-timestamp-change (prefix-numeric-value arg)))
  16925. (defun org-timestamp-down (&optional arg)
  16926. "Decrease the date item at the cursor by one.
  16927. If the cursor is on the year, change the year. If it is on the month or
  16928. the day, change that.
  16929. With prefix ARG, change by that many units."
  16930. (interactive "p")
  16931. (org-timestamp-change (- (prefix-numeric-value arg))))
  16932. (defun org-timestamp-up-day (&optional arg)
  16933. "Increase the date in the time stamp by one day.
  16934. With prefix ARG, change that many days."
  16935. (interactive "p")
  16936. (if (and (not (org-at-timestamp-p t))
  16937. (org-on-heading-p))
  16938. (org-todo 'up)
  16939. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16940. (defun org-timestamp-down-day (&optional arg)
  16941. "Decrease the date in the time stamp by one day.
  16942. With prefix ARG, change that many days."
  16943. (interactive "p")
  16944. (if (and (not (org-at-timestamp-p t))
  16945. (org-on-heading-p))
  16946. (org-todo 'down)
  16947. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16948. (defsubst org-pos-in-match-range (pos n)
  16949. (and (match-beginning n)
  16950. (<= (match-beginning n) pos)
  16951. (>= (match-end n) pos)))
  16952. (defun org-at-timestamp-p (&optional inactive-ok)
  16953. "Determine if the cursor is in or at a timestamp."
  16954. (interactive)
  16955. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16956. (pos (point))
  16957. (ans (or (looking-at tsr)
  16958. (save-excursion
  16959. (skip-chars-backward "^[<\n\r\t")
  16960. (if (> (point) (point-min)) (backward-char 1))
  16961. (and (looking-at tsr)
  16962. (> (- (match-end 0) pos) -1))))))
  16963. (and ans
  16964. (boundp 'org-ts-what)
  16965. (setq org-ts-what
  16966. (cond
  16967. ((= pos (match-beginning 0)) 'bracket)
  16968. ((= pos (1- (match-end 0))) 'bracket)
  16969. ((org-pos-in-match-range pos 2) 'year)
  16970. ((org-pos-in-match-range pos 3) 'month)
  16971. ((org-pos-in-match-range pos 7) 'hour)
  16972. ((org-pos-in-match-range pos 8) 'minute)
  16973. ((or (org-pos-in-match-range pos 4)
  16974. (org-pos-in-match-range pos 5)) 'day)
  16975. ((and (> pos (or (match-end 8) (match-end 5)))
  16976. (< pos (match-end 0)))
  16977. (- pos (or (match-end 8) (match-end 5))))
  16978. (t 'day))))
  16979. ans))
  16980. (defun org-toggle-timestamp-type ()
  16981. ""
  16982. (interactive)
  16983. (when (org-at-timestamp-p t)
  16984. (save-excursion
  16985. (goto-char (match-beginning 0))
  16986. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16987. (goto-char (1- (match-end 0)))
  16988. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16989. (message "Timestamp is now %sactive"
  16990. (if (equal (char-before) ?>) "in" ""))))
  16991. (defun org-timestamp-change (n &optional what)
  16992. "Change the date in the time stamp at point.
  16993. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16994. `year', `minute', `second'. If WHAT is not given, the cursor position
  16995. in the timestamp determines what will be changed."
  16996. (let ((pos (point))
  16997. with-hm inactive
  16998. org-ts-what
  16999. extra
  17000. ts time time0)
  17001. (if (not (org-at-timestamp-p t))
  17002. (error "Not at a timestamp"))
  17003. (if (and (not what) (eq org-ts-what 'bracket))
  17004. (org-toggle-timestamp-type)
  17005. (if (and (not what) (not (eq org-ts-what 'day))
  17006. org-display-custom-times
  17007. (get-text-property (point) 'display)
  17008. (not (get-text-property (1- (point)) 'display)))
  17009. (setq org-ts-what 'day))
  17010. (setq org-ts-what (or what org-ts-what)
  17011. inactive (= (char-after (match-beginning 0)) ?\[)
  17012. ts (match-string 0))
  17013. (replace-match "")
  17014. (if (string-match
  17015. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  17016. ts)
  17017. (setq extra (match-string 1 ts)))
  17018. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  17019. (setq with-hm t))
  17020. (setq time0 (org-parse-time-string ts))
  17021. (setq time
  17022. (encode-time (or (car time0) 0)
  17023. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  17024. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  17025. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  17026. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  17027. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  17028. (nthcdr 6 time0)))
  17029. (when (integerp org-ts-what)
  17030. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  17031. (if (eq what 'calendar)
  17032. (let ((cal-date (org-get-date-from-calendar)))
  17033. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  17034. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  17035. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  17036. (setcar time0 (or (car time0) 0))
  17037. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  17038. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  17039. (setq time (apply 'encode-time time0))))
  17040. (setq org-last-changed-timestamp
  17041. (org-insert-time-stamp time with-hm inactive nil nil extra))
  17042. (org-clock-update-time-maybe)
  17043. (goto-char pos)
  17044. ;; Try to recenter the calendar window, if any
  17045. (if (and org-calendar-follow-timestamp-change
  17046. (get-buffer-window "*Calendar*" t)
  17047. (memq org-ts-what '(day month year)))
  17048. (org-recenter-calendar (time-to-days time))))))
  17049. ;; FIXME: does not yet work for lead times
  17050. (defun org-modify-ts-extra (s pos n)
  17051. "Change the different parts of the lead-time and repeat fields in timestamp."
  17052. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  17053. ng h m new)
  17054. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  17055. (cond
  17056. ((or (org-pos-in-match-range pos 2)
  17057. (org-pos-in-match-range pos 3))
  17058. (setq m (string-to-number (match-string 3 s))
  17059. h (string-to-number (match-string 2 s)))
  17060. (if (org-pos-in-match-range pos 2)
  17061. (setq h (+ h n))
  17062. (setq m (+ m n)))
  17063. (if (< m 0) (setq m (+ m 60) h (1- h)))
  17064. (if (> m 59) (setq m (- m 60) h (1+ h)))
  17065. (setq h (min 24 (max 0 h)))
  17066. (setq ng 1 new (format "-%02d:%02d" h m)))
  17067. ((org-pos-in-match-range pos 6)
  17068. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  17069. ((org-pos-in-match-range pos 5)
  17070. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  17071. (when ng
  17072. (setq s (concat
  17073. (substring s 0 (match-beginning ng))
  17074. new
  17075. (substring s (match-end ng))))))
  17076. s))
  17077. (defun org-recenter-calendar (date)
  17078. "If the calendar is visible, recenter it to DATE."
  17079. (let* ((win (selected-window))
  17080. (cwin (get-buffer-window "*Calendar*" t))
  17081. (calendar-move-hook nil))
  17082. (when cwin
  17083. (select-window cwin)
  17084. (calendar-goto-date (if (listp date) date
  17085. (calendar-gregorian-from-absolute date)))
  17086. (select-window win))))
  17087. (defun org-goto-calendar (&optional arg)
  17088. "Go to the Emacs calendar at the current date.
  17089. If there is a time stamp in the current line, go to that date.
  17090. A prefix ARG can be used to force the current date."
  17091. (interactive "P")
  17092. (let ((tsr org-ts-regexp) diff
  17093. (calendar-move-hook nil)
  17094. (view-calendar-holidays-initially nil)
  17095. (view-diary-entries-initially nil))
  17096. (if (or (org-at-timestamp-p)
  17097. (save-excursion
  17098. (beginning-of-line 1)
  17099. (looking-at (concat ".*" tsr))))
  17100. (let ((d1 (time-to-days (current-time)))
  17101. (d2 (time-to-days
  17102. (org-time-string-to-time (match-string 1)))))
  17103. (setq diff (- d2 d1))))
  17104. (calendar)
  17105. (calendar-goto-today)
  17106. (if (and diff (not arg)) (calendar-forward-day diff))))
  17107. (defun org-get-date-from-calendar ()
  17108. "Return a list (month day year) of date at point in calendar."
  17109. (with-current-buffer "*Calendar*"
  17110. (save-match-data
  17111. (calendar-cursor-to-date))))
  17112. (defun org-date-from-calendar ()
  17113. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  17114. If there is already a time stamp at the cursor position, update it."
  17115. (interactive)
  17116. (if (org-at-timestamp-p t)
  17117. (org-timestamp-change 0 'calendar)
  17118. (let ((cal-date (org-get-date-from-calendar)))
  17119. (org-insert-time-stamp
  17120. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  17121. (defvar appt-time-msg-list)
  17122. ;;;###autoload
  17123. (defun org-agenda-to-appt (&optional refresh filter)
  17124. "Activate appointments found in `org-agenda-files'.
  17125. With a \\[universal-argument] prefix, refresh the list of
  17126. appointements.
  17127. If FILTER is t, interactively prompt the user for a regular
  17128. expression, and filter out entries that don't match it.
  17129. If FILTER is a string, use this string as a regular expression
  17130. for filtering entries out.
  17131. FILTER can also be an alist with the car of each cell being
  17132. either 'headline or 'category. For example:
  17133. '((headline \"IMPORTANT\")
  17134. (category \"Work\"))
  17135. will only add headlines containing IMPORTANT or headlines
  17136. belonging to the \"Work\" category."
  17137. (interactive "P")
  17138. (require 'calendar)
  17139. (if refresh (setq appt-time-msg-list nil))
  17140. (if (eq filter t)
  17141. (setq filter (read-from-minibuffer "Regexp filter: ")))
  17142. (let* ((cnt 0) ; count added events
  17143. (org-agenda-new-buffers nil)
  17144. (org-deadline-warning-days 0)
  17145. (today (org-date-to-gregorian
  17146. (time-to-days (current-time))))
  17147. (files (org-agenda-files)) entries file)
  17148. ;; Get all entries which may contain an appt
  17149. (while (setq file (pop files))
  17150. (setq entries
  17151. (append entries
  17152. (org-agenda-get-day-entries
  17153. file today :timestamp :scheduled :deadline))))
  17154. (setq entries (delq nil entries))
  17155. ;; Map thru entries and find if we should filter them out
  17156. (mapc
  17157. (lambda(x)
  17158. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  17159. (cat (get-text-property 1 'org-category x))
  17160. (tod (get-text-property 1 'time-of-day x))
  17161. (ok (or (null filter)
  17162. (and (stringp filter) (string-match filter evt))
  17163. (and (listp filter)
  17164. (or (string-match
  17165. (cadr (assoc 'category filter)) cat)
  17166. (string-match
  17167. (cadr (assoc 'headline filter)) evt))))))
  17168. ;; FIXME: Shall we remove text-properties for the appt text?
  17169. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  17170. (when (and ok tod)
  17171. (setq tod (number-to-string tod)
  17172. tod (when (string-match
  17173. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  17174. (concat (match-string 1 tod) ":"
  17175. (match-string 2 tod))))
  17176. (appt-add tod evt)
  17177. (setq cnt (1+ cnt))))) entries)
  17178. (org-release-buffers org-agenda-new-buffers)
  17179. (if (eq cnt 0)
  17180. (message "No event to add")
  17181. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
  17182. ;;; The clock for measuring work time.
  17183. (defvar org-mode-line-string "")
  17184. (put 'org-mode-line-string 'risky-local-variable t)
  17185. (defvar org-mode-line-timer nil)
  17186. (defvar org-clock-heading "")
  17187. (defvar org-clock-start-time "")
  17188. (defun org-update-mode-line ()
  17189. (let* ((delta (- (time-to-seconds (current-time))
  17190. (time-to-seconds org-clock-start-time)))
  17191. (h (floor delta 3600))
  17192. (m (floor (- delta (* 3600 h)) 60)))
  17193. (setq org-mode-line-string
  17194. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  17195. 'help-echo "Org-mode clock is running"))
  17196. (force-mode-line-update)))
  17197. (defvar org-clock-marker (make-marker)
  17198. "Marker recording the last clock-in.")
  17199. (defvar org-clock-mode-line-entry nil
  17200. "Information for the modeline about the running clock.")
  17201. (defun org-clock-in ()
  17202. "Start the clock on the current item.
  17203. If necessary, clock-out of the currently active clock."
  17204. (interactive)
  17205. (org-clock-out t)
  17206. (let (ts)
  17207. (save-excursion
  17208. (org-back-to-heading t)
  17209. (when (and org-clock-in-switch-to-state
  17210. (not (looking-at (concat outline-regexp "[ \t]*"
  17211. org-clock-in-switch-to-state
  17212. "\\>"))))
  17213. (org-todo org-clock-in-switch-to-state))
  17214. (if (and org-clock-heading-function
  17215. (functionp org-clock-heading-function))
  17216. (setq org-clock-heading (funcall org-clock-heading-function))
  17217. (if (looking-at org-complex-heading-regexp)
  17218. (setq org-clock-heading (match-string 4))
  17219. (setq org-clock-heading "???")))
  17220. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17221. (org-clock-find-position)
  17222. (insert "\n") (backward-char 1)
  17223. (indent-relative)
  17224. (insert org-clock-string " ")
  17225. (setq org-clock-start-time (current-time))
  17226. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17227. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17228. (or global-mode-string (setq global-mode-string '("")))
  17229. (or (memq 'org-mode-line-string global-mode-string)
  17230. (setq global-mode-string
  17231. (append global-mode-string '(org-mode-line-string))))
  17232. (org-update-mode-line)
  17233. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17234. (message "Clock started at %s" ts))))
  17235. (defun org-clock-find-position ()
  17236. "Find the location where the next clock line should be inserted."
  17237. (org-back-to-heading t)
  17238. (catch 'exit
  17239. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  17240. (re (concat "^[ \t]*" org-clock-string))
  17241. (cnt 0)
  17242. first last)
  17243. (goto-char beg)
  17244. (when (eobp) (newline) (setq end (max (point) end)))
  17245. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17246. ;; we seem to have a CLOCK drawer, so go there.
  17247. (beginning-of-line 2)
  17248. (throw 'exit t))
  17249. ;; Lets count the CLOCK lines
  17250. (goto-char beg)
  17251. (while (re-search-forward re end t)
  17252. (setq first (or first (match-beginning 0))
  17253. last (match-beginning 0)
  17254. cnt (1+ cnt)))
  17255. (when (and (integerp org-clock-into-drawer)
  17256. (>= (1+ cnt) org-clock-into-drawer))
  17257. ;; Wrap current entries into a new drawer
  17258. (goto-char last)
  17259. (beginning-of-line 2)
  17260. (if (org-at-item-p) (org-end-of-item))
  17261. (insert ":END:\n")
  17262. (beginning-of-line 0)
  17263. (org-indent-line-function)
  17264. (goto-char first)
  17265. (insert ":CLOCK:\n")
  17266. (beginning-of-line 0)
  17267. (org-indent-line-function)
  17268. (org-flag-drawer t)
  17269. (beginning-of-line 2)
  17270. (throw 'exit nil))
  17271. (goto-char beg)
  17272. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17273. (not (equal (match-string 1) org-clock-string)))
  17274. ;; Planning info, skip to after it
  17275. (beginning-of-line 2)
  17276. (or (bolp) (newline)))
  17277. (when (eq t org-clock-into-drawer)
  17278. (insert ":CLOCK:\n:END:\n")
  17279. (beginning-of-line -1)
  17280. (org-indent-line-function)
  17281. (org-flag-drawer t)
  17282. (beginning-of-line 2)
  17283. (org-indent-line-function)))))
  17284. (defun org-clock-out (&optional fail-quietly)
  17285. "Stop the currently running clock.
  17286. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17287. (interactive)
  17288. (catch 'exit
  17289. (if (not (marker-buffer org-clock-marker))
  17290. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17291. (let (ts te s h m)
  17292. (save-excursion
  17293. (set-buffer (marker-buffer org-clock-marker))
  17294. (goto-char org-clock-marker)
  17295. (beginning-of-line 1)
  17296. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17297. (equal (match-string 1) org-clock-string))
  17298. (setq ts (match-string 2))
  17299. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17300. (goto-char (match-end 0))
  17301. (delete-region (point) (point-at-eol))
  17302. (insert "--")
  17303. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17304. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17305. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17306. h (floor (/ s 3600))
  17307. s (- s (* 3600 h))
  17308. m (floor (/ s 60))
  17309. s (- s (* 60 s)))
  17310. (insert " => " (format "%2d:%02d" h m))
  17311. (move-marker org-clock-marker nil)
  17312. (when org-log-note-clock-out
  17313. (org-add-log-maybe 'clock-out))
  17314. (when org-mode-line-timer
  17315. (cancel-timer org-mode-line-timer)
  17316. (setq org-mode-line-timer nil))
  17317. (setq global-mode-string
  17318. (delq 'org-mode-line-string global-mode-string))
  17319. (force-mode-line-update)
  17320. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17321. (defun org-clock-cancel ()
  17322. "Cancel the running clock be removing the start timestamp."
  17323. (interactive)
  17324. (if (not (marker-buffer org-clock-marker))
  17325. (error "No active clock"))
  17326. (save-excursion
  17327. (set-buffer (marker-buffer org-clock-marker))
  17328. (goto-char org-clock-marker)
  17329. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17330. (setq global-mode-string
  17331. (delq 'org-mode-line-string global-mode-string))
  17332. (force-mode-line-update)
  17333. (message "Clock canceled"))
  17334. (defun org-clock-goto (&optional delete-windows)
  17335. "Go to the currently clocked-in entry."
  17336. (interactive "P")
  17337. (if (not (marker-buffer org-clock-marker))
  17338. (error "No active clock"))
  17339. (switch-to-buffer-other-window
  17340. (marker-buffer org-clock-marker))
  17341. (if delete-windows (delete-other-windows))
  17342. (goto-char org-clock-marker)
  17343. (org-show-entry)
  17344. (org-back-to-heading)
  17345. (recenter))
  17346. (defvar org-clock-file-total-minutes nil
  17347. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17348. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17349. (defun org-clock-sum (&optional tstart tend)
  17350. "Sum the times for each subtree.
  17351. Puts the resulting times in minutes as a text property on each headline."
  17352. (interactive)
  17353. (let* ((bmp (buffer-modified-p))
  17354. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17355. org-clock-string
  17356. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17357. (lmax 30)
  17358. (ltimes (make-vector lmax 0))
  17359. (t1 0)
  17360. (level 0)
  17361. ts te dt
  17362. time)
  17363. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17364. (save-excursion
  17365. (goto-char (point-max))
  17366. (while (re-search-backward re nil t)
  17367. (cond
  17368. ((match-end 2)
  17369. ;; Two time stamps
  17370. (setq ts (match-string 2)
  17371. te (match-string 3)
  17372. ts (time-to-seconds
  17373. (apply 'encode-time (org-parse-time-string ts)))
  17374. te (time-to-seconds
  17375. (apply 'encode-time (org-parse-time-string te)))
  17376. ts (if tstart (max ts tstart) ts)
  17377. te (if tend (min te tend) te)
  17378. dt (- te ts)
  17379. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17380. ((match-end 4)
  17381. ;; A naket time
  17382. (setq t1 (+ t1 (string-to-number (match-string 5))
  17383. (* 60 (string-to-number (match-string 4))))))
  17384. (t ;; A headline
  17385. (setq level (- (match-end 1) (match-beginning 1)))
  17386. (when (or (> t1 0) (> (aref ltimes level) 0))
  17387. (loop for l from 0 to level do
  17388. (aset ltimes l (+ (aref ltimes l) t1)))
  17389. (setq t1 0 time (aref ltimes level))
  17390. (loop for l from level to (1- lmax) do
  17391. (aset ltimes l 0))
  17392. (goto-char (match-beginning 0))
  17393. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17394. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17395. (set-buffer-modified-p bmp)))
  17396. (defun org-clock-display (&optional total-only)
  17397. "Show subtree times in the entire buffer.
  17398. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17399. in the echo area."
  17400. (interactive)
  17401. (org-remove-clock-overlays)
  17402. (let (time h m p)
  17403. (org-clock-sum)
  17404. (unless total-only
  17405. (save-excursion
  17406. (goto-char (point-min))
  17407. (while (or (and (equal (setq p (point)) (point-min))
  17408. (get-text-property p :org-clock-minutes))
  17409. (setq p (next-single-property-change
  17410. (point) :org-clock-minutes)))
  17411. (goto-char p)
  17412. (when (setq time (get-text-property p :org-clock-minutes))
  17413. (org-put-clock-overlay time (funcall outline-level))))
  17414. (setq h (/ org-clock-file-total-minutes 60)
  17415. m (- org-clock-file-total-minutes (* 60 h)))
  17416. ;; Arrange to remove the overlays upon next change.
  17417. (when org-remove-highlights-with-change
  17418. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17419. nil 'local))))
  17420. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17421. (defvar org-clock-overlays nil)
  17422. (make-variable-buffer-local 'org-clock-overlays)
  17423. (defun org-put-clock-overlay (time &optional level)
  17424. "Put an overlays on the current line, displaying TIME.
  17425. If LEVEL is given, prefix time with a corresponding number of stars.
  17426. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17427. will be easy to remove."
  17428. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17429. (l (if level (org-get-legal-level level 0) 0))
  17430. (off 0)
  17431. ov tx)
  17432. (move-to-column c)
  17433. (unless (eolp) (skip-chars-backward "^ \t"))
  17434. (skip-chars-backward " \t")
  17435. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17436. tx (concat (buffer-substring (1- (point)) (point))
  17437. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17438. (org-add-props (format "%s %2d:%02d%s"
  17439. (make-string l ?*) h m
  17440. (make-string (- 16 l) ?\ ))
  17441. '(face secondary-selection))
  17442. ""))
  17443. (if (not (featurep 'xemacs))
  17444. (org-overlay-put ov 'display tx)
  17445. (org-overlay-put ov 'invisible t)
  17446. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17447. (push ov org-clock-overlays)))
  17448. (defun org-remove-clock-overlays (&optional beg end noremove)
  17449. "Remove the occur highlights from the buffer.
  17450. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17451. from the `before-change-functions' in the current buffer."
  17452. (interactive)
  17453. (unless org-inhibit-highlight-removal
  17454. (mapc 'org-delete-overlay org-clock-overlays)
  17455. (setq org-clock-overlays nil)
  17456. (unless noremove
  17457. (remove-hook 'before-change-functions
  17458. 'org-remove-clock-overlays 'local))))
  17459. (defun org-clock-out-if-current ()
  17460. "Clock out if the current entry contains the running clock.
  17461. This is used to stop the clock after a TODO entry is marked DONE,
  17462. and is only done if the variable `org-clock-out-when-done' is not nil."
  17463. (when (and org-clock-out-when-done
  17464. (member state org-done-keywords)
  17465. (equal (marker-buffer org-clock-marker) (current-buffer))
  17466. (< (point) org-clock-marker)
  17467. (> (save-excursion (outline-next-heading) (point))
  17468. org-clock-marker))
  17469. ;; Clock out, but don't accept a logging message for this.
  17470. (let ((org-log-note-clock-out nil))
  17471. (org-clock-out))))
  17472. (add-hook 'org-after-todo-state-change-hook
  17473. 'org-clock-out-if-current)
  17474. (defun org-check-running-clock ()
  17475. "Check if the current buffer contains the running clock.
  17476. If yes, offer to stop it and to save the buffer with the changes."
  17477. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17478. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17479. (buffer-name))))
  17480. (org-clock-out)
  17481. (when (y-or-n-p "Save changed buffer?")
  17482. (save-buffer))))
  17483. (defun org-clock-report (&optional arg)
  17484. "Create a table containing a report about clocked time.
  17485. If the cursor is inside an existing clocktable block, then the table
  17486. will be updated. If not, a new clocktable will be inserted.
  17487. When called with a prefix argument, move to the first clock table in the
  17488. buffer and update it."
  17489. (interactive "P")
  17490. (org-remove-clock-overlays)
  17491. (when arg
  17492. (org-find-dblock "clocktable")
  17493. (org-show-entry))
  17494. (if (org-in-clocktable-p)
  17495. (goto-char (org-in-clocktable-p))
  17496. (org-create-dblock (list :name "clocktable"
  17497. :maxlevel 2 :scope 'file)))
  17498. (org-update-dblock))
  17499. (defun org-in-clocktable-p ()
  17500. "Check if the cursor is in a clocktable."
  17501. (let ((pos (point)) start)
  17502. (save-excursion
  17503. (end-of-line 1)
  17504. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17505. (setq start (match-beginning 0))
  17506. (re-search-forward "^#\\+END:.*" nil t)
  17507. (>= (match-end 0) pos)
  17508. start))))
  17509. (defun org-clock-update-time-maybe ()
  17510. "If this is a CLOCK line, update it and return t.
  17511. Otherwise, return nil."
  17512. (interactive)
  17513. (save-excursion
  17514. (beginning-of-line 1)
  17515. (skip-chars-forward " \t")
  17516. (when (looking-at org-clock-string)
  17517. (let ((re (concat "[ \t]*" org-clock-string
  17518. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17519. "\\([ \t]*=>.*\\)?"))
  17520. ts te h m s)
  17521. (if (not (looking-at re))
  17522. nil
  17523. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17524. (end-of-line 1)
  17525. (setq ts (match-string 1)
  17526. te (match-string 2))
  17527. (setq s (- (time-to-seconds
  17528. (apply 'encode-time (org-parse-time-string te)))
  17529. (time-to-seconds
  17530. (apply 'encode-time (org-parse-time-string ts))))
  17531. h (floor (/ s 3600))
  17532. s (- s (* 3600 h))
  17533. m (floor (/ s 60))
  17534. s (- s (* 60 s)))
  17535. (insert " => " (format "%2d:%02d" h m))
  17536. t)))))
  17537. (defun org-clock-special-range (key &optional time as-strings)
  17538. "Return two times bordering a special time range.
  17539. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17540. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17541. A week starts Monday 0:00 and ends Sunday 24:00.
  17542. The range is determined relative to TIME. TIME defaults to the current time.
  17543. The return value is a cons cell with two internal times like the ones
  17544. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17545. the returned times will be formatted strings."
  17546. (let* ((tm (decode-time (or time (current-time))))
  17547. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17548. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17549. (dow (nth 6 tm))
  17550. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17551. (cond
  17552. ((eq key 'today)
  17553. (setq h 0 m 0 h1 24 m1 0))
  17554. ((eq key 'yesterday)
  17555. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17556. ((eq key 'thisweek)
  17557. (setq diff (if (= dow 0) 6 (1- dow))
  17558. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17559. ((eq key 'lastweek)
  17560. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17561. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17562. ((eq key 'thismonth)
  17563. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17564. ((eq key 'lastmonth)
  17565. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17566. ((eq key 'thisyear)
  17567. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17568. ((eq key 'lastyear)
  17569. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17570. (t (error "No such time block %s" key)))
  17571. (setq ts (encode-time s m h d month y)
  17572. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17573. (or d1 d) (or month1 month) (or y1 y)))
  17574. (setq fm (cdr org-time-stamp-formats))
  17575. (if as-strings
  17576. (cons (format-time-string fm ts) (format-time-string fm te))
  17577. (cons ts te))))
  17578. (defun org-dblock-write:clocktable (params)
  17579. "Write the standard clocktable."
  17580. (catch 'exit
  17581. (let* ((hlchars '((1 . "*") (2 . "/")))
  17582. (ins (make-marker))
  17583. (total-time nil)
  17584. (scope (plist-get params :scope))
  17585. (tostring (plist-get params :tostring))
  17586. (multifile (plist-get params :multifile))
  17587. (header (plist-get params :header))
  17588. (maxlevel (or (plist-get params :maxlevel) 3))
  17589. (step (plist-get params :step))
  17590. (emph (plist-get params :emphasize))
  17591. (ts (plist-get params :tstart))
  17592. (te (plist-get params :tend))
  17593. (block (plist-get params :block))
  17594. ipos time h m p level hlc hdl
  17595. cc beg end pos tbl)
  17596. (when step
  17597. (org-clocktable-steps params)
  17598. (throw 'exit nil))
  17599. (when block
  17600. (setq cc (org-clock-special-range block nil t)
  17601. ts (car cc) te (cdr cc)))
  17602. (if ts (setq ts (time-to-seconds
  17603. (apply 'encode-time (org-parse-time-string ts)))))
  17604. (if te (setq te (time-to-seconds
  17605. (apply 'encode-time (org-parse-time-string te)))))
  17606. (move-marker ins (point))
  17607. (setq ipos (point))
  17608. ;; Get the right scope
  17609. (setq pos (point))
  17610. (save-restriction
  17611. (cond
  17612. ((not scope))
  17613. ((eq scope 'file) (widen))
  17614. ((eq scope 'subtree) (org-narrow-to-subtree))
  17615. ((eq scope 'tree)
  17616. (while (org-up-heading-safe))
  17617. (org-narrow-to-subtree))
  17618. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17619. (symbol-name scope)))
  17620. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17621. (catch 'exit
  17622. (while (org-up-heading-safe)
  17623. (looking-at outline-regexp)
  17624. (if (<= (org-reduced-level (funcall outline-level)) level)
  17625. (throw 'exit nil))))
  17626. (org-narrow-to-subtree))
  17627. ((or (listp scope) (eq scope 'agenda))
  17628. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17629. (scope 'agenda)
  17630. (p1 (copy-sequence params))
  17631. file)
  17632. (plist-put p1 :tostring t)
  17633. (plist-put p1 :multifile t)
  17634. (plist-put p1 :scope 'file)
  17635. (org-prepare-agenda-buffers files)
  17636. (while (setq file (pop files))
  17637. (with-current-buffer (find-buffer-visiting file)
  17638. (push (org-clocktable-add-file
  17639. file (org-dblock-write:clocktable p1)) tbl)
  17640. (setq total-time (+ (or total-time 0)
  17641. org-clock-file-total-minutes)))))))
  17642. (goto-char pos)
  17643. (unless (eq scope 'agenda)
  17644. (org-clock-sum ts te)
  17645. (goto-char (point-min))
  17646. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17647. (goto-char p)
  17648. (when (setq time (get-text-property p :org-clock-minutes))
  17649. (save-excursion
  17650. (beginning-of-line 1)
  17651. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17652. (setq level (org-reduced-level
  17653. (- (match-end 1) (match-beginning 1))))
  17654. (<= level maxlevel))
  17655. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17656. hdl (match-string 2)
  17657. h (/ time 60)
  17658. m (- time (* 60 h)))
  17659. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17660. (push (concat
  17661. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17662. (make-string (1- level) ?|)
  17663. hlc (format "%d:%02d" h m) hlc
  17664. " |") tbl))))))
  17665. (setq tbl (nreverse tbl))
  17666. (if tostring
  17667. (if tbl (mapconcat 'identity tbl "\n") nil)
  17668. (goto-char ins)
  17669. (insert-before-markers
  17670. (or header
  17671. (concat
  17672. "Clock summary at ["
  17673. (substring
  17674. (format-time-string (cdr org-time-stamp-formats))
  17675. 1 -1)
  17676. "]."
  17677. (if block
  17678. (format " Considered range is /%s/." block)
  17679. "")
  17680. "\n\n"))
  17681. (if (eq scope 'agenda) "|File" "")
  17682. "|L|Headline|Time|\n")
  17683. (setq total-time (or total-time org-clock-file-total-minutes)
  17684. h (/ total-time 60)
  17685. m (- total-time (* 60 h)))
  17686. (insert-before-markers
  17687. "|-\n|"
  17688. (if (eq scope 'agenda) "|" "")
  17689. "|"
  17690. "*Total time*| "
  17691. (format "*%d:%02d*" h m)
  17692. "|\n|-\n")
  17693. (setq tbl (delq nil tbl))
  17694. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17695. (equal (substring (car tbl) 0 2) "|-"))
  17696. (pop tbl))
  17697. (insert-before-markers (mapconcat
  17698. 'identity (delq nil tbl)
  17699. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17700. (backward-delete-char 1)
  17701. (goto-char ipos)
  17702. (skip-chars-forward "^|")
  17703. (org-table-align))))))
  17704. (defun org-clocktable-steps (params)
  17705. (let* ((p1 (copy-sequence params))
  17706. (ts (plist-get p1 :tstart))
  17707. (te (plist-get p1 :tend))
  17708. (step0 (plist-get p1 :step))
  17709. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17710. (block (plist-get p1 :block))
  17711. cc)
  17712. (when block
  17713. (setq cc (org-clock-special-range block nil t)
  17714. ts (car cc) te (cdr cc)))
  17715. (if ts (setq ts (time-to-seconds
  17716. (apply 'encode-time (org-parse-time-string ts)))))
  17717. (if te (setq te (time-to-seconds
  17718. (apply 'encode-time (org-parse-time-string te)))))
  17719. (plist-put p1 :header "")
  17720. (plist-put p1 :step nil)
  17721. (plist-put p1 :block nil)
  17722. (while (< ts te)
  17723. (or (bolp) (insert "\n"))
  17724. (plist-put p1 :tstart (format-time-string
  17725. (car org-time-stamp-formats)
  17726. (seconds-to-time ts)))
  17727. (plist-put p1 :tend (format-time-string
  17728. (car org-time-stamp-formats)
  17729. (seconds-to-time (setq ts (+ ts step)))))
  17730. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17731. (plist-get p1 :tstart) "\n")
  17732. (org-dblock-write:clocktable p1)
  17733. (re-search-forward "#\\+END:")
  17734. (end-of-line 0))))
  17735. (defun org-clocktable-add-file (file table)
  17736. (if table
  17737. (let ((lines (org-split-string table "\n"))
  17738. (ff (file-name-nondirectory file)))
  17739. (mapconcat 'identity
  17740. (mapcar (lambda (x)
  17741. (if (string-match org-table-dataline-regexp x)
  17742. (concat "|" ff x)
  17743. x))
  17744. lines)
  17745. "\n"))))
  17746. ;; FIXME: I don't think anybody uses this, ask David
  17747. (defun org-collect-clock-time-entries ()
  17748. "Return an internal list with clocking information.
  17749. This list has one entry for each CLOCK interval.
  17750. FIXME: describe the elements."
  17751. (interactive)
  17752. (let ((re (concat "^[ \t]*" org-clock-string
  17753. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17754. rtn beg end next cont level title total closedp leafp
  17755. clockpos titlepos h m donep)
  17756. (save-excursion
  17757. (org-clock-sum)
  17758. (goto-char (point-min))
  17759. (while (re-search-forward re nil t)
  17760. (setq clockpos (match-beginning 0)
  17761. beg (match-string 1) end (match-string 2)
  17762. cont (match-end 0))
  17763. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17764. end (apply 'encode-time (org-parse-time-string end)))
  17765. (org-back-to-heading t)
  17766. (setq donep (org-entry-is-done-p))
  17767. (setq titlepos (point)
  17768. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17769. h (/ total 60) m (- total (* 60 h))
  17770. total (cons h m))
  17771. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17772. (setq level (- (match-end 1) (match-beginning 1))
  17773. title (org-match-string-no-properties 2))
  17774. (save-excursion (outline-next-heading) (setq next (point)))
  17775. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17776. (goto-char next)
  17777. (setq leafp (and (looking-at "^\\*+ ")
  17778. (<= (- (match-end 0) (point)) level)))
  17779. (push (list beg end clockpos closedp donep
  17780. total title titlepos level leafp)
  17781. rtn)
  17782. (goto-char cont)))
  17783. (nreverse rtn)))
  17784. ;;;; Agenda, and Diary Integration
  17785. ;;; Define the Org-agenda-mode
  17786. (defvar org-agenda-mode-map (make-sparse-keymap)
  17787. "Keymap for `org-agenda-mode'.")
  17788. (defvar org-agenda-menu) ; defined later in this file.
  17789. (defvar org-agenda-follow-mode nil)
  17790. (defvar org-agenda-show-log nil)
  17791. (defvar org-agenda-redo-command nil)
  17792. (defvar org-agenda-query-string nil)
  17793. (defvar org-agenda-mode-hook nil)
  17794. (defvar org-agenda-type nil)
  17795. (defvar org-agenda-force-single-file nil)
  17796. (defun org-agenda-mode ()
  17797. "Mode for time-sorted view on action items in Org-mode files.
  17798. The following commands are available:
  17799. \\{org-agenda-mode-map}"
  17800. (interactive)
  17801. (kill-all-local-variables)
  17802. (setq org-agenda-undo-list nil
  17803. org-agenda-pending-undo-list nil)
  17804. (setq major-mode 'org-agenda-mode)
  17805. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17806. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17807. (setq mode-name "Org-Agenda")
  17808. (use-local-map org-agenda-mode-map)
  17809. (easy-menu-add org-agenda-menu)
  17810. (if org-startup-truncated (setq truncate-lines t))
  17811. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17812. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17813. ;; Make sure properties are removed when copying text
  17814. (when (boundp 'buffer-substring-filters)
  17815. (org-set-local 'buffer-substring-filters
  17816. (cons (lambda (x)
  17817. (set-text-properties 0 (length x) nil x) x)
  17818. buffer-substring-filters)))
  17819. (unless org-agenda-keep-modes
  17820. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17821. org-agenda-show-log nil))
  17822. (easy-menu-change
  17823. '("Agenda") "Agenda Files"
  17824. (append
  17825. (list
  17826. (vector
  17827. (if (get 'org-agenda-files 'org-restrict)
  17828. "Restricted to single file"
  17829. "Edit File List")
  17830. '(org-edit-agenda-file-list)
  17831. (not (get 'org-agenda-files 'org-restrict)))
  17832. "--")
  17833. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17834. (org-agenda-set-mode-name)
  17835. (apply
  17836. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17837. (list 'org-agenda-mode-hook)))
  17838. (substitute-key-definition 'undo 'org-agenda-undo
  17839. org-agenda-mode-map global-map)
  17840. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17841. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17842. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17843. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17844. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17845. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17846. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17847. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17848. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17849. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17850. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17851. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17852. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17853. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17854. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17855. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17856. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17857. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17858. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17859. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17860. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17861. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17862. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17863. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17864. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17865. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17866. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17867. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17868. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17869. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17870. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17871. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17872. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17873. (while l (org-defkey org-agenda-mode-map
  17874. (int-to-string (pop l)) 'digit-argument)))
  17875. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17876. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17877. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17878. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17879. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17880. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17881. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17882. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17883. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17884. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17885. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17886. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17887. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17888. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17889. (org-defkey org-agenda-mode-map "n" 'next-line)
  17890. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17891. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17892. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17893. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17894. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17895. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17896. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17897. (eval-after-load "calendar"
  17898. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17899. 'org-calendar-goto-agenda))
  17900. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17901. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17902. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17903. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17904. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17905. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17906. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17907. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17908. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17909. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17910. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17911. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17912. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17913. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17914. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17915. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17916. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17917. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17918. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17919. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17920. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17921. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17922. (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
  17923. (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
  17924. (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
  17925. (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
  17926. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17927. "Local keymap for agenda entries from Org-mode.")
  17928. (org-defkey org-agenda-keymap
  17929. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17930. (org-defkey org-agenda-keymap
  17931. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17932. (when org-agenda-mouse-1-follows-link
  17933. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17934. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17935. '("Agenda"
  17936. ("Agenda Files")
  17937. "--"
  17938. ["Show" org-agenda-show t]
  17939. ["Go To (other window)" org-agenda-goto t]
  17940. ["Go To (this window)" org-agenda-switch-to t]
  17941. ["Follow Mode" org-agenda-follow-mode
  17942. :style toggle :selected org-agenda-follow-mode :active t]
  17943. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17944. "--"
  17945. ["Cycle TODO" org-agenda-todo t]
  17946. ["Archive subtree" org-agenda-archive t]
  17947. ["Delete subtree" org-agenda-kill t]
  17948. "--"
  17949. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17950. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17951. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17952. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17953. "--"
  17954. ("Tags and Properties"
  17955. ["Show all Tags" org-agenda-show-tags t]
  17956. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17957. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17958. "--"
  17959. ["Column View" org-columns t])
  17960. ("Date/Schedule"
  17961. ["Schedule" org-agenda-schedule t]
  17962. ["Set Deadline" org-agenda-deadline t]
  17963. "--"
  17964. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17965. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17966. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17967. ("Clock"
  17968. ["Clock in" org-agenda-clock-in t]
  17969. ["Clock out" org-agenda-clock-out t]
  17970. ["Clock cancel" org-agenda-clock-cancel t]
  17971. ["Goto running clock" org-clock-goto t])
  17972. ("Priority"
  17973. ["Set Priority" org-agenda-priority t]
  17974. ["Increase Priority" org-agenda-priority-up t]
  17975. ["Decrease Priority" org-agenda-priority-down t]
  17976. ["Show Priority" org-agenda-show-priority t])
  17977. ("Calendar/Diary"
  17978. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17979. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17980. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17981. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17982. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17983. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17984. "--"
  17985. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17986. "--"
  17987. ("View"
  17988. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17989. :style radio :selected (equal org-agenda-ndays 1)]
  17990. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17991. :style radio :selected (equal org-agenda-ndays 7)]
  17992. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17993. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17994. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17995. :style radio :selected (member org-agenda-ndays '(365 366))]
  17996. "--"
  17997. ["Show Logbook entries" org-agenda-log-mode
  17998. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17999. ["Include Diary" org-agenda-toggle-diary
  18000. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  18001. ["Use Time Grid" org-agenda-toggle-time-grid
  18002. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  18003. ["Write view to file" org-write-agenda t]
  18004. ["Rebuild buffer" org-agenda-redo t]
  18005. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  18006. "--"
  18007. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  18008. "--"
  18009. ["Quit" org-agenda-quit t]
  18010. ["Exit and Release Buffers" org-agenda-exit t]
  18011. ))
  18012. ;;; Agenda undo
  18013. (defvar org-agenda-allow-remote-undo t
  18014. "Non-nil means, allow remote undo from the agenda buffer.")
  18015. (defvar org-agenda-undo-list nil
  18016. "List of undoable operations in the agenda since last refresh.")
  18017. (defvar org-agenda-undo-has-started-in nil
  18018. "Buffers that have already seen `undo-start' in the current undo sequence.")
  18019. (defvar org-agenda-pending-undo-list nil
  18020. "In a series of undo commands, this is the list of remaning undo items.")
  18021. (defmacro org-if-unprotected (&rest body)
  18022. "Execute BODY if there is no `org-protected' text property at point."
  18023. (declare (debug t))
  18024. `(unless (get-text-property (point) 'org-protected)
  18025. ,@body))
  18026. (defmacro org-with-remote-undo (_buffer &rest _body)
  18027. "Execute BODY while recording undo information in two buffers."
  18028. (declare (indent 1) (debug t))
  18029. `(let ((_cline (org-current-line))
  18030. (_cmd this-command)
  18031. (_buf1 (current-buffer))
  18032. (_buf2 ,_buffer)
  18033. (_undo1 buffer-undo-list)
  18034. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  18035. _c1 _c2)
  18036. ,@_body
  18037. (when org-agenda-allow-remote-undo
  18038. (setq _c1 (org-verify-change-for-undo
  18039. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  18040. _c2 (org-verify-change-for-undo
  18041. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  18042. (when (or _c1 _c2)
  18043. ;; make sure there are undo boundaries
  18044. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  18045. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  18046. ;; remember which buffer to undo
  18047. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  18048. org-agenda-undo-list)))))
  18049. (defun org-agenda-undo ()
  18050. "Undo a remote editing step in the agenda.
  18051. This undoes changes both in the agenda buffer and in the remote buffer
  18052. that have been changed along."
  18053. (interactive)
  18054. (or org-agenda-allow-remote-undo
  18055. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  18056. (if (not (eq this-command last-command))
  18057. (setq org-agenda-undo-has-started-in nil
  18058. org-agenda-pending-undo-list org-agenda-undo-list))
  18059. (if (not org-agenda-pending-undo-list)
  18060. (error "No further undo information"))
  18061. (let* ((entry (pop org-agenda-pending-undo-list))
  18062. buf line cmd rembuf)
  18063. (setq cmd (pop entry) line (pop entry))
  18064. (setq rembuf (nth 2 entry))
  18065. (org-with-remote-undo rembuf
  18066. (while (bufferp (setq buf (pop entry)))
  18067. (if (pop entry)
  18068. (with-current-buffer buf
  18069. (let ((last-undo-buffer buf)
  18070. (inhibit-read-only t))
  18071. (unless (memq buf org-agenda-undo-has-started-in)
  18072. (push buf org-agenda-undo-has-started-in)
  18073. (make-local-variable 'pending-undo-list)
  18074. (undo-start))
  18075. (while (and pending-undo-list
  18076. (listp pending-undo-list)
  18077. (not (car pending-undo-list)))
  18078. (pop pending-undo-list))
  18079. (undo-more 1))))))
  18080. (goto-line line)
  18081. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  18082. (defun org-verify-change-for-undo (l1 l2)
  18083. "Verify that a real change occurred between the undo lists L1 and L2."
  18084. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  18085. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  18086. (not (eq l1 l2)))
  18087. ;;; Agenda dispatch
  18088. (defvar org-agenda-restrict nil)
  18089. (defvar org-agenda-restrict-begin (make-marker))
  18090. (defvar org-agenda-restrict-end (make-marker))
  18091. (defvar org-agenda-last-dispatch-buffer nil)
  18092. (defvar org-agenda-overriding-restriction nil)
  18093. ;;;###autoload
  18094. (defun org-agenda (arg &optional keys restriction)
  18095. "Dispatch agenda commands to collect entries to the agenda buffer.
  18096. Prompts for a command to execute. Any prefix arg will be passed
  18097. on to the selected command. The default selections are:
  18098. a Call `org-agenda-list' to display the agenda for current day or week.
  18099. t Call `org-todo-list' to display the global todo list.
  18100. T Call `org-todo-list' to display the global todo list, select only
  18101. entries with a specific TODO keyword (the user gets a prompt).
  18102. m Call `org-tags-view' to display headlines with tags matching
  18103. a condition (the user is prompted for the condition).
  18104. M Like `m', but select only TODO entries, no ordinary headlines.
  18105. L Create a timeline for the current buffer.
  18106. e Export views to associated files.
  18107. More commands can be added by configuring the variable
  18108. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  18109. searches can be pre-defined in this way.
  18110. If the current buffer is in Org-mode and visiting a file, you can also
  18111. first press `<' once to indicate that the agenda should be temporarily
  18112. \(until the next use of \\[org-agenda]) restricted to the current file.
  18113. Pressing `<' twice means to restrict to the current subtree or region
  18114. \(if active)."
  18115. (interactive "P")
  18116. (catch 'exit
  18117. (let* ((prefix-descriptions nil)
  18118. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  18119. (org-agenda-custom-commands
  18120. ;; normalize different versions
  18121. (delq nil
  18122. (mapcar
  18123. (lambda (x)
  18124. (cond ((stringp (cdr x))
  18125. (push x prefix-descriptions)
  18126. nil)
  18127. ((stringp (nth 1 x)) x)
  18128. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18129. (t (cons (car x) (cons "" (cdr x))))))
  18130. org-agenda-custom-commands)))
  18131. (buf (current-buffer))
  18132. (bfn (buffer-file-name (buffer-base-buffer)))
  18133. entry key type match lprops ans)
  18134. ;; Turn off restriction unless there is an overriding one
  18135. (unless org-agenda-overriding-restriction
  18136. (put 'org-agenda-files 'org-restrict nil)
  18137. (setq org-agenda-restrict nil)
  18138. (move-marker org-agenda-restrict-begin nil)
  18139. (move-marker org-agenda-restrict-end nil))
  18140. ;; Delete old local properties
  18141. (put 'org-agenda-redo-command 'org-lprops nil)
  18142. ;; Remember where this call originated
  18143. (setq org-agenda-last-dispatch-buffer (current-buffer))
  18144. (unless keys
  18145. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  18146. keys (car ans)
  18147. restriction (cdr ans)))
  18148. ;; Estabish the restriction, if any
  18149. (when (and (not org-agenda-overriding-restriction) restriction)
  18150. (put 'org-agenda-files 'org-restrict (list bfn))
  18151. (cond
  18152. ((eq restriction 'region)
  18153. (setq org-agenda-restrict t)
  18154. (move-marker org-agenda-restrict-begin (region-beginning))
  18155. (move-marker org-agenda-restrict-end (region-end)))
  18156. ((eq restriction 'subtree)
  18157. (save-excursion
  18158. (setq org-agenda-restrict t)
  18159. (org-back-to-heading t)
  18160. (move-marker org-agenda-restrict-begin (point))
  18161. (move-marker org-agenda-restrict-end
  18162. (progn (org-end-of-subtree t)))))))
  18163. (require 'calendar) ; FIXME: can we avoid this for some commands?
  18164. ;; For example the todo list should not need it (but does...)
  18165. (cond
  18166. ((setq entry (assoc keys org-agenda-custom-commands))
  18167. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  18168. (progn
  18169. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  18170. (put 'org-agenda-redo-command 'org-lprops lprops)
  18171. (cond
  18172. ((eq type 'agenda)
  18173. (org-let lprops '(org-agenda-list current-prefix-arg)))
  18174. ((eq type 'alltodo)
  18175. (org-let lprops '(org-todo-list current-prefix-arg)))
  18176. ((eq type 'search)
  18177. (org-let lprops '(org-search-view current-prefix-arg match)))
  18178. ((eq type 'stuck)
  18179. (org-let lprops '(org-agenda-list-stuck-projects
  18180. current-prefix-arg)))
  18181. ((eq type 'tags)
  18182. (org-let lprops '(org-tags-view current-prefix-arg match)))
  18183. ((eq type 'tags-todo)
  18184. (org-let lprops '(org-tags-view '(4) match)))
  18185. ((eq type 'todo)
  18186. (org-let lprops '(org-todo-list match)))
  18187. ((eq type 'tags-tree)
  18188. (org-check-for-org-mode)
  18189. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  18190. ((eq type 'todo-tree)
  18191. (org-check-for-org-mode)
  18192. (org-let lprops
  18193. '(org-occur (concat "^" outline-regexp "[ \t]*"
  18194. (regexp-quote match) "\\>"))))
  18195. ((eq type 'occur-tree)
  18196. (org-check-for-org-mode)
  18197. (org-let lprops '(org-occur match)))
  18198. ((functionp type)
  18199. (org-let lprops '(funcall type match)))
  18200. ((fboundp type)
  18201. (org-let lprops '(funcall type match)))
  18202. (t (error "Invalid custom agenda command type %s" type))))
  18203. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  18204. ((equal keys "C")
  18205. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  18206. (customize-variable 'org-agenda-custom-commands))
  18207. ((equal keys "a") (call-interactively 'org-agenda-list))
  18208. ((equal keys "s") (call-interactively 'org-search-view))
  18209. ((equal keys "t") (call-interactively 'org-todo-list))
  18210. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  18211. ((equal keys "m") (call-interactively 'org-tags-view))
  18212. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18213. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18214. ((equal keys "L")
  18215. (unless (org-mode-p)
  18216. (error "This is not an Org-mode file"))
  18217. (unless restriction
  18218. (put 'org-agenda-files 'org-restrict (list bfn))
  18219. (org-call-with-arg 'org-timeline arg)))
  18220. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18221. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18222. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18223. (t (error "Invalid agenda key"))))))
  18224. (defun org-agenda-normalize-custom-commands (cmds)
  18225. (delq nil
  18226. (mapcar
  18227. (lambda (x)
  18228. (cond ((stringp (cdr x)) nil)
  18229. ((stringp (nth 1 x)) x)
  18230. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18231. (t (cons (car x) (cons "" (cdr x))))))
  18232. cmds)))
  18233. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18234. "The user interface for selecting an agenda command."
  18235. (catch 'exit
  18236. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18237. (restrict-ok (and bfn (org-mode-p)))
  18238. (region-p (org-region-active-p))
  18239. (custom org-agenda-custom-commands)
  18240. (selstring "")
  18241. restriction second-time
  18242. c entry key type match prefixes rmheader header-end custom1 desc)
  18243. (save-window-excursion
  18244. (delete-other-windows)
  18245. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18246. (erase-buffer)
  18247. (insert (eval-when-compile
  18248. (let ((header
  18249. "
  18250. Press key for an agenda command: < Buffer,subtree/region restriction
  18251. -------------------------------- > Remove restriction
  18252. a Agenda for current week or day e Export agenda views
  18253. t List of all TODO entries T Entries with special TODO kwd
  18254. m Match a TAGS query M Like m, but only TODO entries
  18255. L Timeline for current buffer # List stuck projects (!=configure)
  18256. s Search for keywords C Configure custom agenda commands
  18257. / Multi-occur
  18258. ")
  18259. (start 0))
  18260. (while (string-match
  18261. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18262. header start)
  18263. (setq start (match-end 0))
  18264. (add-text-properties (match-beginning 2) (match-end 2)
  18265. '(face bold) header))
  18266. header)))
  18267. (setq header-end (move-marker (make-marker) (point)))
  18268. (while t
  18269. (setq custom1 custom)
  18270. (when (eq rmheader t)
  18271. (goto-line 1)
  18272. (re-search-forward ":" nil t)
  18273. (delete-region (match-end 0) (point-at-eol))
  18274. (forward-char 1)
  18275. (looking-at "-+")
  18276. (delete-region (match-end 0) (point-at-eol))
  18277. (move-marker header-end (match-end 0)))
  18278. (goto-char header-end)
  18279. (delete-region (point) (point-max))
  18280. (while (setq entry (pop custom1))
  18281. (setq key (car entry) desc (nth 1 entry)
  18282. type (nth 2 entry) match (nth 3 entry))
  18283. (if (> (length key) 1)
  18284. (add-to-list 'prefixes (string-to-char key))
  18285. (insert
  18286. (format
  18287. "\n%-4s%-14s: %s"
  18288. (org-add-props (copy-sequence key)
  18289. '(face bold))
  18290. (cond
  18291. ((string-match "\\S-" desc) desc)
  18292. ((eq type 'agenda) "Agenda for current week or day")
  18293. ((eq type 'alltodo) "List of all TODO entries")
  18294. ((eq type 'search) "Word search")
  18295. ((eq type 'stuck) "List of stuck projects")
  18296. ((eq type 'todo) "TODO keyword")
  18297. ((eq type 'tags) "Tags query")
  18298. ((eq type 'tags-todo) "Tags (TODO)")
  18299. ((eq type 'tags-tree) "Tags tree")
  18300. ((eq type 'todo-tree) "TODO kwd tree")
  18301. ((eq type 'occur-tree) "Occur tree")
  18302. ((functionp type) (if (symbolp type)
  18303. (symbol-name type)
  18304. "Lambda expression"))
  18305. (t "???"))
  18306. (cond
  18307. ((stringp match)
  18308. (org-add-props match nil 'face 'org-warning))
  18309. (match
  18310. (format "set of %d commands" (length match)))
  18311. (t ""))))))
  18312. (when prefixes
  18313. (mapc (lambda (x)
  18314. (insert
  18315. (format "\n%s %s"
  18316. (org-add-props (char-to-string x)
  18317. nil 'face 'bold)
  18318. (or (cdr (assoc (concat selstring (char-to-string x))
  18319. prefix-descriptions))
  18320. "Prefix key"))))
  18321. prefixes))
  18322. (goto-char (point-min))
  18323. (when (fboundp 'fit-window-to-buffer)
  18324. (if second-time
  18325. (if (not (pos-visible-in-window-p (point-max)))
  18326. (fit-window-to-buffer))
  18327. (setq second-time t)
  18328. (fit-window-to-buffer)))
  18329. (message "Press key for agenda command%s:"
  18330. (if (or restrict-ok org-agenda-overriding-restriction)
  18331. (if org-agenda-overriding-restriction
  18332. " (restriction lock active)"
  18333. (if restriction
  18334. (format " (restricted to %s)" restriction)
  18335. " (unrestricted)"))
  18336. ""))
  18337. (setq c (read-char-exclusive))
  18338. (message "")
  18339. (cond
  18340. ((assoc (char-to-string c) custom)
  18341. (setq selstring (concat selstring (char-to-string c)))
  18342. (throw 'exit (cons selstring restriction)))
  18343. ((memq c prefixes)
  18344. (setq selstring (concat selstring (char-to-string c))
  18345. prefixes nil
  18346. rmheader (or rmheader t)
  18347. custom (delq nil (mapcar
  18348. (lambda (x)
  18349. (if (or (= (length (car x)) 1)
  18350. (/= (string-to-char (car x)) c))
  18351. nil
  18352. (cons (substring (car x) 1) (cdr x))))
  18353. custom))))
  18354. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18355. (message "Restriction is only possible in Org-mode buffers")
  18356. (ding) (sit-for 1))
  18357. ((eq c ?1)
  18358. (org-agenda-remove-restriction-lock 'noupdate)
  18359. (setq restriction 'buffer))
  18360. ((eq c ?0)
  18361. (org-agenda-remove-restriction-lock 'noupdate)
  18362. (setq restriction (if region-p 'region 'subtree)))
  18363. ((eq c ?<)
  18364. (org-agenda-remove-restriction-lock 'noupdate)
  18365. (setq restriction
  18366. (cond
  18367. ((eq restriction 'buffer)
  18368. (if region-p 'region 'subtree))
  18369. ((memq restriction '(subtree region))
  18370. nil)
  18371. (t 'buffer))))
  18372. ((eq c ?>)
  18373. (org-agenda-remove-restriction-lock 'noupdate)
  18374. (setq restriction nil))
  18375. ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18376. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18377. ((and (> (length selstring) 0) (eq c ?\d))
  18378. (delete-window)
  18379. (org-agenda-get-restriction-and-command prefix-descriptions))
  18380. ((equal c ?q) (error "Abort"))
  18381. (t (error "Invalid key %c" c))))))))
  18382. (defun org-run-agenda-series (name series)
  18383. (org-prepare-agenda name)
  18384. (let* ((org-agenda-multi t)
  18385. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18386. (cmds (car series))
  18387. (gprops (nth 1 series))
  18388. match ;; The byte compiler incorrectly complains about this. Keep it!
  18389. cmd type lprops)
  18390. (while (setq cmd (pop cmds))
  18391. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18392. (cond
  18393. ((eq type 'agenda)
  18394. (org-let2 gprops lprops
  18395. '(call-interactively 'org-agenda-list)))
  18396. ((eq type 'alltodo)
  18397. (org-let2 gprops lprops
  18398. '(call-interactively 'org-todo-list)))
  18399. ((eq type 'search)
  18400. (org-let2 gprops lprops
  18401. '(org-search-view current-prefix-arg match)))
  18402. ((eq type 'stuck)
  18403. (org-let2 gprops lprops
  18404. '(call-interactively 'org-agenda-list-stuck-projects)))
  18405. ((eq type 'tags)
  18406. (org-let2 gprops lprops
  18407. '(org-tags-view current-prefix-arg match)))
  18408. ((eq type 'tags-todo)
  18409. (org-let2 gprops lprops
  18410. '(org-tags-view '(4) match)))
  18411. ((eq type 'todo)
  18412. (org-let2 gprops lprops
  18413. '(org-todo-list match)))
  18414. ((fboundp type)
  18415. (org-let2 gprops lprops
  18416. '(funcall type match)))
  18417. (t (error "Invalid type in command series"))))
  18418. (widen)
  18419. (setq org-agenda-redo-command redo)
  18420. (goto-char (point-min)))
  18421. (org-finalize-agenda))
  18422. ;;;###autoload
  18423. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18424. "Run an agenda command in batch mode and send the result to STDOUT.
  18425. If CMD-KEY is a string of length 1, it is used as a key in
  18426. `org-agenda-custom-commands' and triggers this command. If it is a
  18427. longer string it is used as a tags/todo match string.
  18428. Paramters are alternating variable names and values that will be bound
  18429. before running the agenda command."
  18430. (let (pars)
  18431. (while parameters
  18432. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18433. (if (> (length cmd-key) 2)
  18434. (eval (list 'let (nreverse pars)
  18435. (list 'org-tags-view nil cmd-key)))
  18436. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18437. (set-buffer org-agenda-buffer-name)
  18438. (princ (org-encode-for-stdout (buffer-string)))))
  18439. (defun org-encode-for-stdout (string)
  18440. (if (fboundp 'encode-coding-string)
  18441. (encode-coding-string string buffer-file-coding-system)
  18442. string))
  18443. (defvar org-agenda-info nil)
  18444. ;;;###autoload
  18445. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18446. "Run an agenda command in batch mode and send the result to STDOUT.
  18447. If CMD-KEY is a string of length 1, it is used as a key in
  18448. `org-agenda-custom-commands' and triggers this command. If it is a
  18449. longer string it is used as a tags/todo match string.
  18450. Paramters are alternating variable names and values that will be bound
  18451. before running the agenda command.
  18452. The output gives a line for each selected agenda item. Each
  18453. item is a list of comma-separated values, like this:
  18454. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18455. category The category of the item
  18456. head The headline, without TODO kwd, TAGS and PRIORITY
  18457. type The type of the agenda entry, can be
  18458. todo selected in TODO match
  18459. tagsmatch selected in tags match
  18460. diary imported from diary
  18461. deadline a deadline on given date
  18462. scheduled scheduled on given date
  18463. timestamp entry has timestamp on given date
  18464. closed entry was closed on given date
  18465. upcoming-deadline warning about deadline
  18466. past-scheduled forwarded scheduled item
  18467. block entry has date block including g. date
  18468. todo The todo keyword, if any
  18469. tags All tags including inherited ones, separated by colons
  18470. date The relevant date, like 2007-2-14
  18471. time The time, like 15:00-16:50
  18472. extra Sting with extra planning info
  18473. priority-l The priority letter if any was given
  18474. priority-n The computed numerical priority
  18475. agenda-day The day in the agenda where this is listed"
  18476. (let (pars)
  18477. (while parameters
  18478. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18479. (push (list 'org-agenda-remove-tags t) pars)
  18480. (if (> (length cmd-key) 2)
  18481. (eval (list 'let (nreverse pars)
  18482. (list 'org-tags-view nil cmd-key)))
  18483. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18484. (set-buffer org-agenda-buffer-name)
  18485. (let* ((lines (org-split-string (buffer-string) "\n"))
  18486. line)
  18487. (while (setq line (pop lines))
  18488. (catch 'next
  18489. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18490. (setq org-agenda-info
  18491. (org-fix-agenda-info (text-properties-at 0 line)))
  18492. (princ
  18493. (org-encode-for-stdout
  18494. (mapconcat 'org-agenda-export-csv-mapper
  18495. '(org-category txt type todo tags date time-of-day extra
  18496. priority-letter priority agenda-day)
  18497. ",")))
  18498. (princ "\n"))))))
  18499. (defun org-fix-agenda-info (props)
  18500. "Make sure all properties on an agenda item have a canonical form,
  18501. so the export commands can easily use it."
  18502. (let (tmp re)
  18503. (when (setq tmp (plist-get props 'tags))
  18504. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18505. (when (setq tmp (plist-get props 'date))
  18506. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18507. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18508. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18509. (setq tmp (calendar-date-string tmp)))
  18510. (setq props (plist-put props 'date tmp)))
  18511. (when (setq tmp (plist-get props 'day))
  18512. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18513. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18514. (setq tmp (calendar-date-string tmp)))
  18515. (setq props (plist-put props 'day tmp))
  18516. (setq props (plist-put props 'agenda-day tmp)))
  18517. (when (setq tmp (plist-get props 'txt))
  18518. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18519. (plist-put props 'priority-letter (match-string 1 tmp))
  18520. (setq tmp (replace-match "" t t tmp)))
  18521. (when (and (setq re (plist-get props 'org-todo-regexp))
  18522. (setq re (concat "\\`\\.*" re " ?"))
  18523. (string-match re tmp))
  18524. (plist-put props 'todo (match-string 1 tmp))
  18525. (setq tmp (replace-match "" t t tmp)))
  18526. (plist-put props 'txt tmp)))
  18527. props)
  18528. (defun org-agenda-export-csv-mapper (prop)
  18529. (let ((res (plist-get org-agenda-info prop)))
  18530. (setq res
  18531. (cond
  18532. ((not res) "")
  18533. ((stringp res) res)
  18534. (t (prin1-to-string res))))
  18535. (while (string-match "," res)
  18536. (setq res (replace-match ";" t t res)))
  18537. (org-trim res)))
  18538. ;;;###autoload
  18539. (defun org-store-agenda-views (&rest parameters)
  18540. (interactive)
  18541. (eval (list 'org-batch-store-agenda-views)))
  18542. ;; FIXME, why is this a macro?????
  18543. ;;;###autoload
  18544. (defmacro org-batch-store-agenda-views (&rest parameters)
  18545. "Run all custom agenda commands that have a file argument."
  18546. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18547. (pop-up-frames nil)
  18548. (dir default-directory)
  18549. pars cmd thiscmdkey files opts)
  18550. (while parameters
  18551. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18552. (setq pars (reverse pars))
  18553. (save-window-excursion
  18554. (while cmds
  18555. (setq cmd (pop cmds)
  18556. thiscmdkey (car cmd)
  18557. opts (nth 4 cmd)
  18558. files (nth 5 cmd))
  18559. (if (stringp files) (setq files (list files)))
  18560. (when files
  18561. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18562. (list 'org-agenda nil thiscmdkey)))
  18563. (set-buffer org-agenda-buffer-name)
  18564. (while files
  18565. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18566. (list 'org-write-agenda
  18567. (expand-file-name (pop files) dir) t))))
  18568. (and (get-buffer org-agenda-buffer-name)
  18569. (kill-buffer org-agenda-buffer-name)))))))
  18570. (defun org-write-agenda (file &optional nosettings)
  18571. "Write the current buffer (an agenda view) as a file.
  18572. Depending on the extension of the file name, plain text (.txt),
  18573. HTML (.html or .htm) or Postscript (.ps) is produced.
  18574. If NOSETTINGS is given, do not scope the settings of
  18575. `org-agenda-exporter-settings' into the export commands. This is used when
  18576. the settings have already been scoped and we do not wish to overrule other,
  18577. higher priority settings."
  18578. (interactive "FWrite agenda to file: ")
  18579. (if (not (file-writable-p file))
  18580. (error "Cannot write agenda to file %s" file))
  18581. (cond
  18582. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18583. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18584. (org-let (if nosettings nil org-agenda-exporter-settings)
  18585. '(save-excursion
  18586. (save-window-excursion
  18587. (cond
  18588. ((string-match "\\.html?\\'" file)
  18589. (set-buffer (htmlize-buffer (current-buffer)))
  18590. (when (and org-agenda-export-html-style
  18591. (string-match "<style>" org-agenda-export-html-style))
  18592. ;; replace <style> section with org-agenda-export-html-style
  18593. (goto-char (point-min))
  18594. (kill-region (- (search-forward "<style") 6)
  18595. (search-forward "</style>"))
  18596. (insert org-agenda-export-html-style))
  18597. (write-file file)
  18598. (kill-buffer (current-buffer))
  18599. (message "HTML written to %s" file))
  18600. ((string-match "\\.ps\\'" file)
  18601. (ps-print-buffer-with-faces file)
  18602. (message "Postscript written to %s" file))
  18603. (t
  18604. (let ((bs (buffer-string)))
  18605. (find-file file)
  18606. (insert bs)
  18607. (save-buffer 0)
  18608. (kill-buffer (current-buffer))
  18609. (message "Plain text written to %s" file))))))
  18610. (set-buffer org-agenda-buffer-name)))
  18611. (defmacro org-no-read-only (&rest body)
  18612. "Inhibit read-only for BODY."
  18613. `(let ((inhibit-read-only t)) ,@body))
  18614. (defun org-check-for-org-mode ()
  18615. "Make sure current buffer is in org-mode. Error if not."
  18616. (or (org-mode-p)
  18617. (error "Cannot execute org-mode agenda command on buffer in %s."
  18618. major-mode)))
  18619. (defun org-fit-agenda-window ()
  18620. "Fit the window to the buffer size."
  18621. (and (memq org-agenda-window-setup '(reorganize-frame))
  18622. (fboundp 'fit-window-to-buffer)
  18623. (fit-window-to-buffer
  18624. nil
  18625. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18626. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18627. ;;; Agenda file list
  18628. (defun org-agenda-files (&optional unrestricted)
  18629. "Get the list of agenda files.
  18630. Optional UNRESTRICTED means return the full list even if a restriction
  18631. is currently in place."
  18632. (let ((files
  18633. (cond
  18634. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18635. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18636. ((listp org-agenda-files) org-agenda-files)
  18637. (t (error "Invalid value of `org-agenda-files'")))))
  18638. (setq files (apply 'append
  18639. (mapcar (lambda (f)
  18640. (if (file-directory-p f)
  18641. (directory-files f t
  18642. org-agenda-file-regexp)
  18643. (list f)))
  18644. files)))
  18645. (if org-agenda-skip-unavailable-files
  18646. (delq nil
  18647. (mapcar (function
  18648. (lambda (file)
  18649. (and (file-readable-p file) file)))
  18650. files))
  18651. files))) ; `org-check-agenda-file' will remove them from the list
  18652. (defun org-edit-agenda-file-list ()
  18653. "Edit the list of agenda files.
  18654. Depending on setup, this either uses customize to edit the variable
  18655. `org-agenda-files', or it visits the file that is holding the list. In the
  18656. latter case, the buffer is set up in a way that saving it automatically kills
  18657. the buffer and restores the previous window configuration."
  18658. (interactive)
  18659. (if (stringp org-agenda-files)
  18660. (let ((cw (current-window-configuration)))
  18661. (find-file org-agenda-files)
  18662. (org-set-local 'org-window-configuration cw)
  18663. (org-add-hook 'after-save-hook
  18664. (lambda ()
  18665. (set-window-configuration
  18666. (prog1 org-window-configuration
  18667. (kill-buffer (current-buffer))))
  18668. (org-install-agenda-files-menu)
  18669. (message "New agenda file list installed"))
  18670. nil 'local)
  18671. (message "%s" (substitute-command-keys
  18672. "Edit list and finish with \\[save-buffer]")))
  18673. (customize-variable 'org-agenda-files)))
  18674. (defun org-store-new-agenda-file-list (list)
  18675. "Set new value for the agenda file list and save it correcly."
  18676. (if (stringp org-agenda-files)
  18677. (let ((f org-agenda-files) b)
  18678. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18679. (with-temp-file f
  18680. (insert (mapconcat 'identity list "\n") "\n")))
  18681. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18682. (setq org-agenda-files list)
  18683. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18684. (defun org-read-agenda-file-list ()
  18685. "Read the list of agenda files from a file."
  18686. (when (stringp org-agenda-files)
  18687. (with-temp-buffer
  18688. (insert-file-contents org-agenda-files)
  18689. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18690. ;;;###autoload
  18691. (defun org-cycle-agenda-files ()
  18692. "Cycle through the files in `org-agenda-files'.
  18693. If the current buffer visits an agenda file, find the next one in the list.
  18694. If the current buffer does not, find the first agenda file."
  18695. (interactive)
  18696. (let* ((fs (org-agenda-files t))
  18697. (files (append fs (list (car fs))))
  18698. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18699. file)
  18700. (unless files (error "No agenda files"))
  18701. (catch 'exit
  18702. (while (setq file (pop files))
  18703. (if (equal (file-truename file) tcf)
  18704. (when (car files)
  18705. (find-file (car files))
  18706. (throw 'exit t))))
  18707. (find-file (car fs)))
  18708. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18709. (defun org-agenda-file-to-front (&optional to-end)
  18710. "Move/add the current file to the top of the agenda file list.
  18711. If the file is not present in the list, it is added to the front. If it is
  18712. present, it is moved there. With optional argument TO-END, add/move to the
  18713. end of the list."
  18714. (interactive "P")
  18715. (let ((org-agenda-skip-unavailable-files nil)
  18716. (file-alist (mapcar (lambda (x)
  18717. (cons (file-truename x) x))
  18718. (org-agenda-files t)))
  18719. (ctf (file-truename buffer-file-name))
  18720. x had)
  18721. (setq x (assoc ctf file-alist) had x)
  18722. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18723. (if to-end
  18724. (setq file-alist (append (delq x file-alist) (list x)))
  18725. (setq file-alist (cons x (delq x file-alist))))
  18726. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18727. (org-install-agenda-files-menu)
  18728. (message "File %s to %s of agenda file list"
  18729. (if had "moved" "added") (if to-end "end" "front"))))
  18730. (defun org-remove-file (&optional file)
  18731. "Remove current file from the list of files in variable `org-agenda-files'.
  18732. These are the files which are being checked for agenda entries.
  18733. Optional argument FILE means, use this file instead of the current."
  18734. (interactive)
  18735. (let* ((org-agenda-skip-unavailable-files nil)
  18736. (file (or file buffer-file-name))
  18737. (true-file (file-truename file))
  18738. (afile (abbreviate-file-name file))
  18739. (files (delq nil (mapcar
  18740. (lambda (x)
  18741. (if (equal true-file
  18742. (file-truename x))
  18743. nil x))
  18744. (org-agenda-files t)))))
  18745. (if (not (= (length files) (length (org-agenda-files t))))
  18746. (progn
  18747. (org-store-new-agenda-file-list files)
  18748. (org-install-agenda-files-menu)
  18749. (message "Removed file: %s" afile))
  18750. (message "File was not in list: %s (not removed)" afile))))
  18751. (defun org-file-menu-entry (file)
  18752. (vector file (list 'find-file file) t))
  18753. (defun org-check-agenda-file (file)
  18754. "Make sure FILE exists. If not, ask user what to do."
  18755. (when (not (file-exists-p file))
  18756. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18757. (abbreviate-file-name file))
  18758. (let ((r (downcase (read-char-exclusive))))
  18759. (cond
  18760. ((equal r ?r)
  18761. (org-remove-file file)
  18762. (throw 'nextfile t))
  18763. (t (error "Abort"))))))
  18764. ;;; Agenda prepare and finalize
  18765. (defvar org-agenda-multi nil) ; dynammically scoped
  18766. (defvar org-agenda-buffer-name "*Org Agenda*")
  18767. (defvar org-pre-agenda-window-conf nil)
  18768. (defvar org-agenda-name nil)
  18769. (defun org-prepare-agenda (&optional name)
  18770. (setq org-todo-keywords-for-agenda nil)
  18771. (setq org-done-keywords-for-agenda nil)
  18772. (if org-agenda-multi
  18773. (progn
  18774. (setq buffer-read-only nil)
  18775. (goto-char (point-max))
  18776. (unless (or (bobp) org-agenda-compact-blocks)
  18777. (insert "\n" (make-string (window-width) ?=) "\n"))
  18778. (narrow-to-region (point) (point-max)))
  18779. (org-agenda-reset-markers)
  18780. (org-prepare-agenda-buffers (org-agenda-files))
  18781. (setq org-todo-keywords-for-agenda
  18782. (org-uniquify org-todo-keywords-for-agenda))
  18783. (setq org-done-keywords-for-agenda
  18784. (org-uniquify org-done-keywords-for-agenda))
  18785. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18786. (awin (get-buffer-window abuf)))
  18787. (cond
  18788. ((equal (current-buffer) abuf) nil)
  18789. (awin (select-window awin))
  18790. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18791. ((equal org-agenda-window-setup 'current-window)
  18792. (switch-to-buffer abuf))
  18793. ((equal org-agenda-window-setup 'other-window)
  18794. (org-switch-to-buffer-other-window abuf))
  18795. ((equal org-agenda-window-setup 'other-frame)
  18796. (switch-to-buffer-other-frame abuf))
  18797. ((equal org-agenda-window-setup 'reorganize-frame)
  18798. (delete-other-windows)
  18799. (org-switch-to-buffer-other-window abuf))))
  18800. (setq buffer-read-only nil)
  18801. (erase-buffer)
  18802. (org-agenda-mode)
  18803. (and name (not org-agenda-name)
  18804. (org-set-local 'org-agenda-name name)))
  18805. (setq buffer-read-only nil))
  18806. (defun org-finalize-agenda ()
  18807. "Finishing touch for the agenda buffer, called just before displaying it."
  18808. (unless org-agenda-multi
  18809. (save-excursion
  18810. (let ((inhibit-read-only t))
  18811. (goto-char (point-min))
  18812. (while (org-activate-bracket-links (point-max))
  18813. (add-text-properties (match-beginning 0) (match-end 0)
  18814. '(face org-link)))
  18815. (org-agenda-align-tags)
  18816. (unless org-agenda-with-colors
  18817. (remove-text-properties (point-min) (point-max) '(face nil))))
  18818. (if (and (boundp 'org-overriding-columns-format)
  18819. org-overriding-columns-format)
  18820. (org-set-local 'org-overriding-columns-format
  18821. org-overriding-columns-format))
  18822. (if (and (boundp 'org-agenda-view-columns-initially)
  18823. org-agenda-view-columns-initially)
  18824. (org-agenda-columns))
  18825. (when org-agenda-fontify-priorities
  18826. (org-fontify-priorities))
  18827. (run-hooks 'org-finalize-agenda-hook)
  18828. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18829. )))
  18830. (defun org-fontify-priorities ()
  18831. "Make highest priority lines bold, and lowest italic."
  18832. (interactive)
  18833. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18834. (org-delete-overlay o)))
  18835. (org-overlays-in (point-min) (point-max)))
  18836. (save-excursion
  18837. (let ((inhibit-read-only t)
  18838. b e p ov h l)
  18839. (goto-char (point-min))
  18840. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18841. (setq h (or (get-char-property (point) 'org-highest-priority)
  18842. org-highest-priority)
  18843. l (or (get-char-property (point) 'org-lowest-priority)
  18844. org-lowest-priority)
  18845. p (string-to-char (match-string 1))
  18846. b (match-beginning 0) e (point-at-eol)
  18847. ov (org-make-overlay b e))
  18848. (org-overlay-put
  18849. ov 'face
  18850. (cond ((listp org-agenda-fontify-priorities)
  18851. (cdr (assoc p org-agenda-fontify-priorities)))
  18852. ((equal p l) 'italic)
  18853. ((equal p h) 'bold)))
  18854. (org-overlay-put ov 'org-type 'org-priority)))))
  18855. (defun org-prepare-agenda-buffers (files)
  18856. "Create buffers for all agenda files, protect archived trees and comments."
  18857. (interactive)
  18858. (let ((pa '(:org-archived t))
  18859. (pc '(:org-comment t))
  18860. (pall '(:org-archived t :org-comment t))
  18861. (inhibit-read-only t)
  18862. (rea (concat ":" org-archive-tag ":"))
  18863. bmp file re)
  18864. (save-excursion
  18865. (save-restriction
  18866. (while (setq file (pop files))
  18867. (if (bufferp file)
  18868. (set-buffer file)
  18869. (org-check-agenda-file file)
  18870. (set-buffer (org-get-agenda-file-buffer file)))
  18871. (widen)
  18872. (setq bmp (buffer-modified-p))
  18873. (org-refresh-category-properties)
  18874. (setq org-todo-keywords-for-agenda
  18875. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18876. (setq org-done-keywords-for-agenda
  18877. (append org-done-keywords-for-agenda org-done-keywords))
  18878. (save-excursion
  18879. (remove-text-properties (point-min) (point-max) pall)
  18880. (when org-agenda-skip-archived-trees
  18881. (goto-char (point-min))
  18882. (while (re-search-forward rea nil t)
  18883. (if (org-on-heading-p t)
  18884. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18885. (goto-char (point-min))
  18886. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18887. (while (re-search-forward re nil t)
  18888. (add-text-properties
  18889. (match-beginning 0) (org-end-of-subtree t) pc)))
  18890. (set-buffer-modified-p bmp))))))
  18891. (defvar org-agenda-skip-function nil
  18892. "Function to be called at each match during agenda construction.
  18893. If this function returns nil, the current match should not be skipped.
  18894. Otherwise, the function must return a position from where the search
  18895. should be continued.
  18896. This may also be a Lisp form, it will be evaluated.
  18897. Never set this variable using `setq' or so, because then it will apply
  18898. to all future agenda commands. Instead, bind it with `let' to scope
  18899. it dynamically into the agenda-constructing command. A good way to set
  18900. it is through options in org-agenda-custom-commands.")
  18901. (defun org-agenda-skip ()
  18902. "Throw to `:skip' in places that should be skipped.
  18903. Also moves point to the end of the skipped region, so that search can
  18904. continue from there."
  18905. (let ((p (point-at-bol)) to fp)
  18906. (and org-agenda-skip-archived-trees
  18907. (get-text-property p :org-archived)
  18908. (org-end-of-subtree t)
  18909. (throw :skip t))
  18910. (and (get-text-property p :org-comment)
  18911. (org-end-of-subtree t)
  18912. (throw :skip t))
  18913. (if (equal (char-after p) ?#) (throw :skip t))
  18914. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18915. (consp org-agenda-skip-function))
  18916. (setq to (save-excursion
  18917. (save-match-data
  18918. (if fp
  18919. (funcall org-agenda-skip-function)
  18920. (eval org-agenda-skip-function))))))
  18921. (goto-char to)
  18922. (throw :skip t))))
  18923. (defvar org-agenda-markers nil
  18924. "List of all currently active markers created by `org-agenda'.")
  18925. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18926. "Creation time of the last agenda marker.")
  18927. (defun org-agenda-new-marker (&optional pos)
  18928. "Return a new agenda marker.
  18929. Org-mode keeps a list of these markers and resets them when they are
  18930. no longer in use."
  18931. (let ((m (copy-marker (or pos (point)))))
  18932. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18933. (push m org-agenda-markers)
  18934. m))
  18935. (defun org-agenda-reset-markers ()
  18936. "Reset markers created by `org-agenda'."
  18937. (while org-agenda-markers
  18938. (move-marker (pop org-agenda-markers) nil)))
  18939. (defun org-get-agenda-file-buffer (file)
  18940. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18941. it to the list of buffers which might be released later."
  18942. (let ((buf (org-find-base-buffer-visiting file)))
  18943. (if buf
  18944. buf ; just return it
  18945. ;; Make a new buffer and remember it
  18946. (setq buf (find-file-noselect file))
  18947. (if buf (push buf org-agenda-new-buffers))
  18948. buf)))
  18949. (defun org-release-buffers (blist)
  18950. "Release all buffers in list, asking the user for confirmation when needed.
  18951. When a buffer is unmodified, it is just killed. When modified, it is saved
  18952. \(if the user agrees) and then killed."
  18953. (let (buf file)
  18954. (while (setq buf (pop blist))
  18955. (setq file (buffer-file-name buf))
  18956. (when (and (buffer-modified-p buf)
  18957. file
  18958. (y-or-n-p (format "Save file %s? " file)))
  18959. (with-current-buffer buf (save-buffer)))
  18960. (kill-buffer buf))))
  18961. (defun org-get-category (&optional pos)
  18962. "Get the category applying to position POS."
  18963. (get-text-property (or pos (point)) 'org-category))
  18964. ;;; Agenda timeline
  18965. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18966. (defun org-timeline (&optional include-all)
  18967. "Show a time-sorted view of the entries in the current org file.
  18968. Only entries with a time stamp of today or later will be listed. With
  18969. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18970. under the current date.
  18971. If the buffer contains an active region, only check the region for
  18972. dates."
  18973. (interactive "P")
  18974. (require 'calendar)
  18975. (org-compile-prefix-format 'timeline)
  18976. (org-set-sorting-strategy 'timeline)
  18977. (let* ((dopast t)
  18978. (dotodo include-all)
  18979. (doclosed org-agenda-show-log)
  18980. (entry buffer-file-name)
  18981. (date (calendar-current-date))
  18982. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18983. (end (if (org-region-active-p) (region-end) (point-max)))
  18984. (day-numbers (org-get-all-dates beg end 'no-ranges
  18985. t doclosed ; always include today
  18986. org-timeline-show-empty-dates))
  18987. (org-deadline-warning-days 0)
  18988. (org-agenda-only-exact-dates t)
  18989. (today (time-to-days (current-time)))
  18990. (past t)
  18991. args
  18992. s e rtn d emptyp)
  18993. (setq org-agenda-redo-command
  18994. (list 'progn
  18995. (list 'org-switch-to-buffer-other-window (current-buffer))
  18996. (list 'org-timeline (list 'quote include-all))))
  18997. (if (not dopast)
  18998. ;; Remove past dates from the list of dates.
  18999. (setq day-numbers (delq nil (mapcar (lambda(x)
  19000. (if (>= x today) x nil))
  19001. day-numbers))))
  19002. (org-prepare-agenda (concat "Timeline "
  19003. (file-name-nondirectory buffer-file-name)))
  19004. (if doclosed (push :closed args))
  19005. (push :timestamp args)
  19006. (push :deadline args)
  19007. (push :scheduled args)
  19008. (push :sexp args)
  19009. (if dotodo (push :todo args))
  19010. (while (setq d (pop day-numbers))
  19011. (if (and (listp d) (eq (car d) :omitted))
  19012. (progn
  19013. (setq s (point))
  19014. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  19015. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  19016. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  19017. (if (and (>= d today)
  19018. dopast
  19019. past)
  19020. (progn
  19021. (setq past nil)
  19022. (insert (make-string 79 ?-) "\n")))
  19023. (setq date (calendar-gregorian-from-absolute d))
  19024. (setq s (point))
  19025. (setq rtn (and (not emptyp)
  19026. (apply 'org-agenda-get-day-entries entry
  19027. date args)))
  19028. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  19029. (progn
  19030. (insert
  19031. (if (stringp org-agenda-format-date)
  19032. (format-time-string org-agenda-format-date
  19033. (org-time-from-absolute date))
  19034. (funcall org-agenda-format-date date))
  19035. "\n")
  19036. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19037. (put-text-property s (1- (point)) 'org-date-line t)
  19038. (if (equal d today)
  19039. (put-text-property s (1- (point)) 'org-today t))
  19040. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  19041. (put-text-property s (1- (point)) 'day d)))))
  19042. (goto-char (point-min))
  19043. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  19044. (point-min)))
  19045. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  19046. (org-finalize-agenda)
  19047. (setq buffer-read-only t)))
  19048. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  19049. "Return a list of all relevant day numbers from BEG to END buffer positions.
  19050. If NO-RANGES is non-nil, include only the start and end dates of a range,
  19051. not every single day in the range. If FORCE-TODAY is non-nil, make
  19052. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  19053. inactive time stamps (those in square brackets) are included.
  19054. When EMPTY is non-nil, also include days without any entries."
  19055. (let ((re (concat
  19056. (if pre-re pre-re "")
  19057. (if inactive org-ts-regexp-both org-ts-regexp)))
  19058. dates dates1 date day day1 day2 ts1 ts2)
  19059. (if force-today
  19060. (setq dates (list (time-to-days (current-time)))))
  19061. (save-excursion
  19062. (goto-char beg)
  19063. (while (re-search-forward re end t)
  19064. (setq day (time-to-days (org-time-string-to-time
  19065. (substring (match-string 1) 0 10))))
  19066. (or (memq day dates) (push day dates)))
  19067. (unless no-ranges
  19068. (goto-char beg)
  19069. (while (re-search-forward org-tr-regexp end t)
  19070. (setq ts1 (substring (match-string 1) 0 10)
  19071. ts2 (substring (match-string 2) 0 10)
  19072. day1 (time-to-days (org-time-string-to-time ts1))
  19073. day2 (time-to-days (org-time-string-to-time ts2)))
  19074. (while (< (setq day1 (1+ day1)) day2)
  19075. (or (memq day1 dates) (push day1 dates)))))
  19076. (setq dates (sort dates '<))
  19077. (when empty
  19078. (while (setq day (pop dates))
  19079. (setq day2 (car dates))
  19080. (push day dates1)
  19081. (when (and day2 empty)
  19082. (if (or (eq empty t)
  19083. (and (numberp empty) (<= (- day2 day) empty)))
  19084. (while (< (setq day (1+ day)) day2)
  19085. (push (list day) dates1))
  19086. (push (cons :omitted (- day2 day)) dates1))))
  19087. (setq dates (nreverse dates1)))
  19088. dates)))
  19089. ;;; Agenda Daily/Weekly
  19090. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  19091. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  19092. (defvar org-agenda-last-arguments nil
  19093. "The arguments of the previous call to org-agenda")
  19094. (defvar org-starting-day nil) ; local variable in the agenda buffer
  19095. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  19096. (defvar org-include-all-loc nil) ; local variable
  19097. (defvar org-agenda-remove-date nil) ; dynamically scoped
  19098. ;;;###autoload
  19099. (defun org-agenda-list (&optional include-all start-day ndays)
  19100. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  19101. The view will be for the current day or week, but from the overview buffer
  19102. you will be able to go to other days/weeks.
  19103. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  19104. all unfinished TODO items will also be shown, before the agenda.
  19105. This feature is considered obsolete, please use the TODO list or a block
  19106. agenda instead.
  19107. With a numeric prefix argument in an interactive call, the agenda will
  19108. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  19109. the number of days. NDAYS defaults to `org-agenda-ndays'.
  19110. START-DAY defaults to TODAY, or to the most recent match for the weekday
  19111. given in `org-agenda-start-on-weekday'."
  19112. (interactive "P")
  19113. (if (and (integerp include-all) (> include-all 0))
  19114. (setq ndays include-all include-all nil))
  19115. (setq ndays (or ndays org-agenda-ndays)
  19116. start-day (or start-day org-agenda-start-day))
  19117. (if org-agenda-overriding-arguments
  19118. (setq include-all (car org-agenda-overriding-arguments)
  19119. start-day (nth 1 org-agenda-overriding-arguments)
  19120. ndays (nth 2 org-agenda-overriding-arguments)))
  19121. (if (stringp start-day)
  19122. ;; Convert to an absolute day number
  19123. (setq start-day (time-to-days (org-read-date nil t start-day))))
  19124. (setq org-agenda-last-arguments (list include-all start-day ndays))
  19125. (org-compile-prefix-format 'agenda)
  19126. (org-set-sorting-strategy 'agenda)
  19127. (require 'calendar)
  19128. (let* ((org-agenda-start-on-weekday
  19129. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  19130. org-agenda-start-on-weekday nil))
  19131. (thefiles (org-agenda-files))
  19132. (files thefiles)
  19133. (today (time-to-days
  19134. (time-subtract (current-time)
  19135. (list 0 (* 3600 org-extend-today-until) 0))))
  19136. (sd (or start-day today))
  19137. (start (if (or (null org-agenda-start-on-weekday)
  19138. (< org-agenda-ndays 7))
  19139. sd
  19140. (let* ((nt (calendar-day-of-week
  19141. (calendar-gregorian-from-absolute sd)))
  19142. (n1 org-agenda-start-on-weekday)
  19143. (d (- nt n1)))
  19144. (- sd (+ (if (< d 0) 7 0) d)))))
  19145. (day-numbers (list start))
  19146. (day-cnt 0)
  19147. (inhibit-redisplay (not debug-on-error))
  19148. s e rtn rtnall file date d start-pos end-pos todayp nd)
  19149. (setq org-agenda-redo-command
  19150. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  19151. ;; Make the list of days
  19152. (setq ndays (or ndays org-agenda-ndays)
  19153. nd ndays)
  19154. (while (> ndays 1)
  19155. (push (1+ (car day-numbers)) day-numbers)
  19156. (setq ndays (1- ndays)))
  19157. (setq day-numbers (nreverse day-numbers))
  19158. (org-prepare-agenda "Day/Week")
  19159. (org-set-local 'org-starting-day (car day-numbers))
  19160. (org-set-local 'org-include-all-loc include-all)
  19161. (org-set-local 'org-agenda-span
  19162. (org-agenda-ndays-to-span nd))
  19163. (when (and (or include-all org-agenda-include-all-todo)
  19164. (member today day-numbers))
  19165. (setq files thefiles
  19166. rtnall nil)
  19167. (while (setq file (pop files))
  19168. (catch 'nextfile
  19169. (org-check-agenda-file file)
  19170. (setq date (calendar-gregorian-from-absolute today)
  19171. rtn (org-agenda-get-day-entries
  19172. file date :todo))
  19173. (setq rtnall (append rtnall rtn))))
  19174. (when rtnall
  19175. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  19176. (add-text-properties (point-min) (1- (point))
  19177. (list 'face 'org-agenda-structure))
  19178. (insert (org-finalize-agenda-entries rtnall) "\n")))
  19179. (unless org-agenda-compact-blocks
  19180. (setq s (point))
  19181. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  19182. "-agenda:\n")
  19183. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  19184. 'org-date-line t)))
  19185. (while (setq d (pop day-numbers))
  19186. (setq date (calendar-gregorian-from-absolute d)
  19187. s (point))
  19188. (if (or (setq todayp (= d today))
  19189. (and (not start-pos) (= d sd)))
  19190. (setq start-pos (point))
  19191. (if (and start-pos (not end-pos))
  19192. (setq end-pos (point))))
  19193. (setq files thefiles
  19194. rtnall nil)
  19195. (while (setq file (pop files))
  19196. (catch 'nextfile
  19197. (org-check-agenda-file file)
  19198. (if org-agenda-show-log
  19199. (setq rtn (org-agenda-get-day-entries
  19200. file date
  19201. :deadline :scheduled :timestamp :sexp :closed))
  19202. (setq rtn (org-agenda-get-day-entries
  19203. file date
  19204. :deadline :scheduled :sexp :timestamp)))
  19205. (setq rtnall (append rtnall rtn))))
  19206. (if org-agenda-include-diary
  19207. (progn
  19208. (require 'diary-lib)
  19209. (setq rtn (org-get-entries-from-diary date))
  19210. (setq rtnall (append rtnall rtn))))
  19211. (if (or rtnall org-agenda-show-all-dates)
  19212. (progn
  19213. (setq day-cnt (1+ day-cnt))
  19214. (insert
  19215. (if (stringp org-agenda-format-date)
  19216. (format-time-string org-agenda-format-date
  19217. (org-time-from-absolute date))
  19218. (funcall org-agenda-format-date date))
  19219. "\n")
  19220. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19221. (put-text-property s (1- (point)) 'org-date-line t)
  19222. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19223. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19224. (if rtnall (insert
  19225. (org-finalize-agenda-entries
  19226. (org-agenda-add-time-grid-maybe
  19227. rtnall nd todayp))
  19228. "\n"))
  19229. (put-text-property s (1- (point)) 'day d)
  19230. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19231. (goto-char (point-min))
  19232. (org-fit-agenda-window)
  19233. (unless (and (pos-visible-in-window-p (point-min))
  19234. (pos-visible-in-window-p (point-max)))
  19235. (goto-char (1- (point-max)))
  19236. (recenter -1)
  19237. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19238. (progn
  19239. (goto-char (or start-pos 1))
  19240. (recenter 1))))
  19241. (goto-char (or start-pos 1))
  19242. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19243. (org-finalize-agenda)
  19244. (setq buffer-read-only t)
  19245. (message "")))
  19246. (defun org-agenda-ndays-to-span (n)
  19247. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19248. ;;; Agenda word search
  19249. (defvar org-agenda-search-history nil)
  19250. ;;;###autoload
  19251. (defun org-search-view (&optional arg string)
  19252. "Show all entries that contain words or regular expressions.
  19253. If the first character of the search string is an asterisks,
  19254. search only the headlines.
  19255. The search string is broken into \"words\" by splitting at whitespace.
  19256. The individual words are then interpreted as a boolean expression with
  19257. logical AND. Words prefixed with a minus must not occur in the entry.
  19258. Words without a prefix or prefixed with a plus must occur in the entry.
  19259. Matching is case-insensitive and the words are enclosed by word delimiters.
  19260. Words enclosed by curly braces are interpreted as regular expressions
  19261. that must or must not match in the entry.
  19262. This command searches the agenda files, and in addition the files listed
  19263. in `org-agenda-text-search-extra-files'."
  19264. (interactive "P")
  19265. (org-compile-prefix-format 'search)
  19266. (org-set-sorting-strategy 'search)
  19267. (org-prepare-agenda "SEARCH")
  19268. (let* ((props (list 'face nil
  19269. 'done-face 'org-done
  19270. 'org-not-done-regexp org-not-done-regexp
  19271. 'org-todo-regexp org-todo-regexp
  19272. 'mouse-face 'highlight
  19273. 'keymap org-agenda-keymap
  19274. 'help-echo (format "mouse-2 or RET jump to location")))
  19275. regexp rtn rtnall files file pos
  19276. marker priority category tags c neg re
  19277. ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
  19278. (unless (and (not arg)
  19279. (stringp string)
  19280. (string-match "\\S-" string))
  19281. (setq string (read-string "[+-]Word/{Regexp} ...: "
  19282. (cond
  19283. ((integerp arg) (cons string arg))
  19284. (arg string))
  19285. 'org-agenda-search-history)))
  19286. (setq org-agenda-redo-command
  19287. (list 'org-search-view 'current-prefix-arg string))
  19288. (setq org-agenda-query-string string)
  19289. (if (equal (string-to-char string) ?*)
  19290. (setq hdl-only t
  19291. words (substring string 1))
  19292. (setq words string))
  19293. (setq words (org-split-string words))
  19294. (mapc (lambda (w)
  19295. (setq c (string-to-char w))
  19296. (if (equal c ?-)
  19297. (setq neg t w (substring w 1))
  19298. (if (equal c ?+)
  19299. (setq neg nil w (substring w 1))
  19300. (setq neg nil)))
  19301. (if (string-match "\\`{.*}\\'" w)
  19302. (setq re (substring w 1 -1))
  19303. (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
  19304. (if neg (push re regexps-) (push re regexps+)))
  19305. words)
  19306. (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
  19307. (if (not regexps+)
  19308. (setq regexp (concat "^" org-outline-regexp))
  19309. (setq regexp (pop regexps+))
  19310. (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
  19311. regexp))))
  19312. (setq files (append (org-agenda-files) org-agenda-text-search-extra-files)
  19313. rtnall nil)
  19314. (while (setq file (pop files))
  19315. (setq ee nil)
  19316. (catch 'nextfile
  19317. (org-check-agenda-file file)
  19318. (setq buffer (if (file-exists-p file)
  19319. (org-get-agenda-file-buffer file)
  19320. (error "No such file %s" file)))
  19321. (if (not buffer)
  19322. ;; If file does not exist, make sure an error message is sent
  19323. (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
  19324. file))))
  19325. (with-current-buffer buffer
  19326. (unless (org-mode-p)
  19327. (error "Agenda file %s is not in `org-mode'" file))
  19328. (let ((case-fold-search t))
  19329. (save-excursion
  19330. (save-restriction
  19331. (if org-agenda-restrict
  19332. (narrow-to-region org-agenda-restrict-begin
  19333. org-agenda-restrict-end)
  19334. (widen))
  19335. (goto-char (point-min))
  19336. (unless (or (org-on-heading-p)
  19337. (outline-next-heading))
  19338. (throw 'nextfile t))
  19339. (goto-char (max (point-min) (1- (point))))
  19340. (while (re-search-forward regexp nil t)
  19341. (org-back-to-heading t)
  19342. (skip-chars-forward "* ")
  19343. (setq beg (point-at-bol)
  19344. beg1 (point)
  19345. end (progn (outline-next-heading) (point)))
  19346. (catch :skip
  19347. (goto-char beg)
  19348. (org-agenda-skip)
  19349. (setq str (buffer-substring-no-properties
  19350. (point-at-bol)
  19351. (if hdl-only (point-at-eol) end)))
  19352. (mapc (lambda (wr) (when (string-match wr str)
  19353. (goto-char (1- end))
  19354. (throw :skip t)))
  19355. regexps-)
  19356. (mapc (lambda (wr) (unless (string-match wr str)
  19357. (goto-char (1- end))
  19358. (throw :skip t)))
  19359. regexps+)
  19360. (goto-char beg)
  19361. (setq marker (org-agenda-new-marker (point))
  19362. category (org-get-category)
  19363. tags (org-get-tags-at (point))
  19364. txt (org-format-agenda-item
  19365. ""
  19366. (buffer-substring-no-properties
  19367. beg1 (point-at-eol))
  19368. category tags))
  19369. (org-add-props txt props
  19370. 'org-marker marker 'org-hd-marker marker
  19371. 'priority 1000 'org-category category
  19372. 'type "search")
  19373. (push txt ee)
  19374. (goto-char (1- end)))))))))
  19375. (setq rtn (nreverse ee))
  19376. (setq rtnall (append rtnall rtn)))
  19377. (if org-agenda-overriding-header
  19378. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19379. nil 'face 'org-agenda-structure) "\n")
  19380. (insert "Search words: ")
  19381. (add-text-properties (point-min) (1- (point))
  19382. (list 'face 'org-agenda-structure))
  19383. (setq pos (point))
  19384. (insert string "\n")
  19385. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19386. (setq pos (point))
  19387. (unless org-agenda-multi
  19388. (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
  19389. (add-text-properties pos (1- (point))
  19390. (list 'face 'org-agenda-structure))))
  19391. (when rtnall
  19392. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19393. (goto-char (point-min))
  19394. (org-fit-agenda-window)
  19395. (add-text-properties (point-min) (point-max) '(org-agenda-type search))
  19396. (org-finalize-agenda)
  19397. (setq buffer-read-only t)))
  19398. ;;; Agenda TODO list
  19399. (defvar org-select-this-todo-keyword nil)
  19400. (defvar org-last-arg nil)
  19401. ;;;###autoload
  19402. (defun org-todo-list (arg)
  19403. "Show all TODO entries from all agenda file in a single list.
  19404. The prefix arg can be used to select a specific TODO keyword and limit
  19405. the list to these. When using \\[universal-argument], you will be prompted
  19406. for a keyword. A numeric prefix directly selects the Nth keyword in
  19407. `org-todo-keywords-1'."
  19408. (interactive "P")
  19409. (require 'calendar)
  19410. (org-compile-prefix-format 'todo)
  19411. (org-set-sorting-strategy 'todo)
  19412. (org-prepare-agenda "TODO")
  19413. (let* ((today (time-to-days (current-time)))
  19414. (date (calendar-gregorian-from-absolute today))
  19415. (kwds org-todo-keywords-for-agenda)
  19416. (completion-ignore-case t)
  19417. (org-select-this-todo-keyword
  19418. (if (stringp arg) arg
  19419. (and arg (integerp arg) (> arg 0)
  19420. (nth (1- arg) kwds))))
  19421. rtn rtnall files file pos)
  19422. (when (equal arg '(4))
  19423. (setq org-select-this-todo-keyword
  19424. (completing-read "Keyword (or KWD1|K2D2|...): "
  19425. (mapcar 'list kwds) nil nil)))
  19426. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19427. (org-set-local 'org-last-arg arg)
  19428. (setq org-agenda-redo-command
  19429. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19430. (setq files (org-agenda-files)
  19431. rtnall nil)
  19432. (while (setq file (pop files))
  19433. (catch 'nextfile
  19434. (org-check-agenda-file file)
  19435. (setq rtn (org-agenda-get-day-entries file date :todo))
  19436. (setq rtnall (append rtnall rtn))))
  19437. (if org-agenda-overriding-header
  19438. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19439. nil 'face 'org-agenda-structure) "\n")
  19440. (insert "Global list of TODO items of type: ")
  19441. (add-text-properties (point-min) (1- (point))
  19442. (list 'face 'org-agenda-structure))
  19443. (setq pos (point))
  19444. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19445. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19446. (setq pos (point))
  19447. (unless org-agenda-multi
  19448. (insert "Available with `N r': (0)ALL")
  19449. (let ((n 0) s)
  19450. (mapc (lambda (x)
  19451. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19452. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19453. (insert "\n "))
  19454. (insert " " s))
  19455. kwds))
  19456. (insert "\n"))
  19457. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19458. (when rtnall
  19459. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19460. (goto-char (point-min))
  19461. (org-fit-agenda-window)
  19462. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19463. (org-finalize-agenda)
  19464. (setq buffer-read-only t)))
  19465. ;;; Agenda tags match
  19466. ;;;###autoload
  19467. (defun org-tags-view (&optional todo-only match)
  19468. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19469. The prefix arg TODO-ONLY limits the search to TODO entries."
  19470. (interactive "P")
  19471. (org-compile-prefix-format 'tags)
  19472. (org-set-sorting-strategy 'tags)
  19473. (let* ((org-tags-match-list-sublevels
  19474. (if todo-only t org-tags-match-list-sublevels))
  19475. (completion-ignore-case t)
  19476. rtn rtnall files file pos matcher
  19477. buffer)
  19478. (setq matcher (org-make-tags-matcher match)
  19479. match (car matcher) matcher (cdr matcher))
  19480. (org-prepare-agenda (concat "TAGS " match))
  19481. (setq org-agenda-redo-command
  19482. (list 'org-tags-view (list 'quote todo-only)
  19483. (list 'if 'current-prefix-arg nil match)))
  19484. (setq files (org-agenda-files)
  19485. rtnall nil)
  19486. (while (setq file (pop files))
  19487. (catch 'nextfile
  19488. (org-check-agenda-file file)
  19489. (setq buffer (if (file-exists-p file)
  19490. (org-get-agenda-file-buffer file)
  19491. (error "No such file %s" file)))
  19492. (if (not buffer)
  19493. ;; If file does not exist, merror message to agenda
  19494. (setq rtn (list
  19495. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19496. rtnall (append rtnall rtn))
  19497. (with-current-buffer buffer
  19498. (unless (org-mode-p)
  19499. (error "Agenda file %s is not in `org-mode'" file))
  19500. (save-excursion
  19501. (save-restriction
  19502. (if org-agenda-restrict
  19503. (narrow-to-region org-agenda-restrict-begin
  19504. org-agenda-restrict-end)
  19505. (widen))
  19506. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19507. (setq rtnall (append rtnall rtn))))))))
  19508. (if org-agenda-overriding-header
  19509. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19510. nil 'face 'org-agenda-structure) "\n")
  19511. (insert "Headlines with TAGS match: ")
  19512. (add-text-properties (point-min) (1- (point))
  19513. (list 'face 'org-agenda-structure))
  19514. (setq pos (point))
  19515. (insert match "\n")
  19516. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19517. (setq pos (point))
  19518. (unless org-agenda-multi
  19519. (insert "Press `C-u r' to search again with new search string\n"))
  19520. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19521. (when rtnall
  19522. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19523. (goto-char (point-min))
  19524. (org-fit-agenda-window)
  19525. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19526. (org-finalize-agenda)
  19527. (setq buffer-read-only t)))
  19528. ;;; Agenda Finding stuck projects
  19529. (defvar org-agenda-skip-regexp nil
  19530. "Regular expression used in skipping subtrees for the agenda.
  19531. This is basically a temporary global variable that can be set and then
  19532. used by user-defined selections using `org-agenda-skip-function'.")
  19533. (defvar org-agenda-overriding-header nil
  19534. "When this is set during todo and tags searches, will replace header.")
  19535. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19536. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19537. If yes, it returns the end position of this tree, causing agenda commands
  19538. to skip this subtree. This is a function that can be put into
  19539. `org-agenda-skip-function' for the duration of a command."
  19540. (let ((end (save-excursion (org-end-of-subtree t)))
  19541. skip)
  19542. (save-excursion
  19543. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19544. (and skip end)))
  19545. (defun org-agenda-skip-entry-if (&rest conditions)
  19546. "Skip entry if any of CONDITIONS is true.
  19547. See `org-agenda-skip-if' for details."
  19548. (org-agenda-skip-if nil conditions))
  19549. (defun org-agenda-skip-subtree-if (&rest conditions)
  19550. "Skip entry if any of CONDITIONS is true.
  19551. See `org-agenda-skip-if' for details."
  19552. (org-agenda-skip-if t conditions))
  19553. (defun org-agenda-skip-if (subtree conditions)
  19554. "Checks current entity for CONDITIONS.
  19555. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19556. the entry, i.e. the text before the next heading is checked.
  19557. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19558. from different tests. Valid conditions are:
  19559. scheduled Check if there is a scheduled cookie
  19560. notscheduled Check if there is no scheduled cookie
  19561. deadline Check if there is a deadline
  19562. notdeadline Check if there is no deadline
  19563. regexp Check if regexp matches
  19564. notregexp Check if regexp does not match.
  19565. The regexp is taken from the conditions list, it must come right after
  19566. the `regexp' or `notregexp' element.
  19567. If any of these conditions is met, this function returns the end point of
  19568. the entity, causing the search to continue from there. This is a function
  19569. that can be put into `org-agenda-skip-function' for the duration of a command."
  19570. (let (beg end m)
  19571. (org-back-to-heading t)
  19572. (setq beg (point)
  19573. end (if subtree
  19574. (progn (org-end-of-subtree t) (point))
  19575. (progn (outline-next-heading) (1- (point)))))
  19576. (goto-char beg)
  19577. (and
  19578. (or
  19579. (and (memq 'scheduled conditions)
  19580. (re-search-forward org-scheduled-time-regexp end t))
  19581. (and (memq 'notscheduled conditions)
  19582. (not (re-search-forward org-scheduled-time-regexp end t)))
  19583. (and (memq 'deadline conditions)
  19584. (re-search-forward org-deadline-time-regexp end t))
  19585. (and (memq 'notdeadline conditions)
  19586. (not (re-search-forward org-deadline-time-regexp end t)))
  19587. (and (setq m (memq 'regexp conditions))
  19588. (stringp (nth 1 m))
  19589. (re-search-forward (nth 1 m) end t))
  19590. (and (setq m (memq 'notregexp conditions))
  19591. (stringp (nth 1 m))
  19592. (not (re-search-forward (nth 1 m) end t))))
  19593. end)))
  19594. ;;;###autoload
  19595. (defun org-agenda-list-stuck-projects (&rest ignore)
  19596. "Create agenda view for projects that are stuck.
  19597. Stuck projects are project that have no next actions. For the definitions
  19598. of what a project is and how to check if it stuck, customize the variable
  19599. `org-stuck-projects'.
  19600. MATCH is being ignored."
  19601. (interactive)
  19602. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19603. ;; FIXME: we could have used org-agenda-skip-if here.
  19604. (org-agenda-overriding-header "List of stuck projects: ")
  19605. (matcher (nth 0 org-stuck-projects))
  19606. (todo (nth 1 org-stuck-projects))
  19607. (todo-wds (if (member "*" todo)
  19608. (progn
  19609. (org-prepare-agenda-buffers (org-agenda-files))
  19610. (org-delete-all
  19611. org-done-keywords-for-agenda
  19612. (copy-sequence org-todo-keywords-for-agenda)))
  19613. todo))
  19614. (todo-re (concat "^\\*+[ \t]+\\("
  19615. (mapconcat 'identity todo-wds "\\|")
  19616. "\\)\\>"))
  19617. (tags (nth 2 org-stuck-projects))
  19618. (tags-re (if (member "*" tags)
  19619. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19620. (concat "^\\*+ .*:\\("
  19621. (mapconcat 'identity tags "\\|")
  19622. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19623. (gen-re (nth 3 org-stuck-projects))
  19624. (re-list
  19625. (delq nil
  19626. (list
  19627. (if todo todo-re)
  19628. (if tags tags-re)
  19629. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19630. gen-re)))))
  19631. (setq org-agenda-skip-regexp
  19632. (if re-list
  19633. (mapconcat 'identity re-list "\\|")
  19634. (error "No information how to identify unstuck projects")))
  19635. (org-tags-view nil matcher)
  19636. (with-current-buffer org-agenda-buffer-name
  19637. (setq org-agenda-redo-command
  19638. '(org-agenda-list-stuck-projects
  19639. (or current-prefix-arg org-last-arg))))))
  19640. ;;; Diary integration
  19641. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19642. (defun org-get-entries-from-diary (date)
  19643. "Get the (Emacs Calendar) diary entries for DATE."
  19644. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19645. (diary-display-hook '(fancy-diary-display))
  19646. (pop-up-frames nil)
  19647. (list-diary-entries-hook
  19648. (cons 'org-diary-default-entry list-diary-entries-hook))
  19649. (diary-file-name-prefix-function nil) ; turn this feature off
  19650. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19651. entries
  19652. (org-disable-agenda-to-diary t))
  19653. (save-excursion
  19654. (save-window-excursion
  19655. (funcall (if (fboundp 'diary-list-entries)
  19656. 'diary-list-entries 'list-diary-entries)
  19657. date 1)))
  19658. (if (not (get-buffer fancy-diary-buffer))
  19659. (setq entries nil)
  19660. (with-current-buffer fancy-diary-buffer
  19661. (setq buffer-read-only nil)
  19662. (if (zerop (buffer-size))
  19663. ;; No entries
  19664. (setq entries nil)
  19665. ;; Omit the date and other unnecessary stuff
  19666. (org-agenda-cleanup-fancy-diary)
  19667. ;; Add prefix to each line and extend the text properties
  19668. (if (zerop (buffer-size))
  19669. (setq entries nil)
  19670. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19671. (set-buffer-modified-p nil)
  19672. (kill-buffer fancy-diary-buffer)))
  19673. (when entries
  19674. (setq entries (org-split-string entries "\n"))
  19675. (setq entries
  19676. (mapcar
  19677. (lambda (x)
  19678. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19679. ;; Extend the text properties to the beginning of the line
  19680. (org-add-props x (text-properties-at (1- (length x)) x)
  19681. 'type "diary" 'date date))
  19682. entries)))))
  19683. (defun org-agenda-cleanup-fancy-diary ()
  19684. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19685. This gets rid of the date, the underline under the date, and
  19686. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19687. date. It also removes lines that contain only whitespace."
  19688. (goto-char (point-min))
  19689. (if (looking-at ".*?:[ \t]*")
  19690. (progn
  19691. (replace-match "")
  19692. (re-search-forward "\n=+$" nil t)
  19693. (replace-match "")
  19694. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19695. (re-search-forward "\n=+$" nil t)
  19696. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19697. (goto-char (point-min))
  19698. (while (re-search-forward "^ +\n" nil t)
  19699. (replace-match ""))
  19700. (goto-char (point-min))
  19701. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19702. (replace-match "")))
  19703. ;; Make sure entries from the diary have the right text properties.
  19704. (eval-after-load "diary-lib"
  19705. '(if (boundp 'diary-modify-entry-list-string-function)
  19706. ;; We can rely on the hook, nothing to do
  19707. nil
  19708. ;; Hook not avaiable, must use advice to make this work
  19709. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19710. "Make the position visible."
  19711. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19712. (stringp string)
  19713. buffer-file-name)
  19714. (setq string (org-modify-diary-entry-string string))))))
  19715. (defun org-modify-diary-entry-string (string)
  19716. "Add text properties to string, allowing org-mode to act on it."
  19717. (org-add-props string nil
  19718. 'mouse-face 'highlight
  19719. 'keymap org-agenda-keymap
  19720. 'help-echo (if buffer-file-name
  19721. (format "mouse-2 or RET jump to diary file %s"
  19722. (abbreviate-file-name buffer-file-name))
  19723. "")
  19724. 'org-agenda-diary-link t
  19725. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19726. (defun org-diary-default-entry ()
  19727. "Add a dummy entry to the diary.
  19728. Needed to avoid empty dates which mess up holiday display."
  19729. ;; Catch the error if dealing with the new add-to-diary-alist
  19730. (when org-disable-agenda-to-diary
  19731. (condition-case nil
  19732. (add-to-diary-list original-date "Org-mode dummy" "")
  19733. (error
  19734. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19735. ;;;###autoload
  19736. (defun org-diary (&rest args)
  19737. "Return diary information from org-files.
  19738. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19739. It accesses org files and extracts information from those files to be
  19740. listed in the diary. The function accepts arguments specifying what
  19741. items should be listed. The following arguments are allowed:
  19742. :timestamp List the headlines of items containing a date stamp or
  19743. date range matching the selected date. Deadlines will
  19744. also be listed, on the expiration day.
  19745. :sexp List entries resulting from diary-like sexps.
  19746. :deadline List any deadlines past due, or due within
  19747. `org-deadline-warning-days'. The listing occurs only
  19748. in the diary for *today*, not at any other date. If
  19749. an entry is marked DONE, it is no longer listed.
  19750. :scheduled List all items which are scheduled for the given date.
  19751. The diary for *today* also contains items which were
  19752. scheduled earlier and are not yet marked DONE.
  19753. :todo List all TODO items from the org-file. This may be a
  19754. long list - so this is not turned on by default.
  19755. Like deadlines, these entries only show up in the
  19756. diary for *today*, not at any other date.
  19757. The call in the diary file should look like this:
  19758. &%%(org-diary) ~/path/to/some/orgfile.org
  19759. Use a separate line for each org file to check. Or, if you omit the file name,
  19760. all files listed in `org-agenda-files' will be checked automatically:
  19761. &%%(org-diary)
  19762. If you don't give any arguments (as in the example above), the default
  19763. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19764. So the example above may also be written as
  19765. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19766. The function expects the lisp variables `entry' and `date' to be provided
  19767. by the caller, because this is how the calendar works. Don't use this
  19768. function from a program - use `org-agenda-get-day-entries' instead."
  19769. (when (> (- (time-to-seconds (current-time))
  19770. org-agenda-last-marker-time)
  19771. 5)
  19772. (org-agenda-reset-markers))
  19773. (org-compile-prefix-format 'agenda)
  19774. (org-set-sorting-strategy 'agenda)
  19775. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19776. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19777. (list entry)
  19778. (org-agenda-files t)))
  19779. file rtn results)
  19780. (org-prepare-agenda-buffers files)
  19781. ;; If this is called during org-agenda, don't return any entries to
  19782. ;; the calendar. Org Agenda will list these entries itself.
  19783. (if org-disable-agenda-to-diary (setq files nil))
  19784. (while (setq file (pop files))
  19785. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19786. (setq results (append results rtn)))
  19787. (if results
  19788. (concat (org-finalize-agenda-entries results) "\n"))))
  19789. ;;; Agenda entry finders
  19790. (defun org-agenda-get-day-entries (file date &rest args)
  19791. "Does the work for `org-diary' and `org-agenda'.
  19792. FILE is the path to a file to be checked for entries. DATE is date like
  19793. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19794. which kind of entries should be extracted. For details about these, see
  19795. the documentation of `org-diary'."
  19796. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19797. (let* ((org-startup-folded nil)
  19798. (org-startup-align-all-tables nil)
  19799. (buffer (if (file-exists-p file)
  19800. (org-get-agenda-file-buffer file)
  19801. (error "No such file %s" file)))
  19802. arg results rtn)
  19803. (if (not buffer)
  19804. ;; If file does not exist, make sure an error message ends up in diary
  19805. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19806. (with-current-buffer buffer
  19807. (unless (org-mode-p)
  19808. (error "Agenda file %s is not in `org-mode'" file))
  19809. (let ((case-fold-search nil))
  19810. (save-excursion
  19811. (save-restriction
  19812. (if org-agenda-restrict
  19813. (narrow-to-region org-agenda-restrict-begin
  19814. org-agenda-restrict-end)
  19815. (widen))
  19816. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19817. (while (setq arg (pop args))
  19818. (cond
  19819. ((and (eq arg :todo)
  19820. (equal date (calendar-current-date)))
  19821. (setq rtn (org-agenda-get-todos))
  19822. (setq results (append results rtn)))
  19823. ((eq arg :timestamp)
  19824. (setq rtn (org-agenda-get-blocks))
  19825. (setq results (append results rtn))
  19826. (setq rtn (org-agenda-get-timestamps))
  19827. (setq results (append results rtn)))
  19828. ((eq arg :sexp)
  19829. (setq rtn (org-agenda-get-sexps))
  19830. (setq results (append results rtn)))
  19831. ((eq arg :scheduled)
  19832. (setq rtn (org-agenda-get-scheduled))
  19833. (setq results (append results rtn)))
  19834. ((eq arg :closed)
  19835. (setq rtn (org-agenda-get-closed))
  19836. (setq results (append results rtn)))
  19837. ((eq arg :deadline)
  19838. (setq rtn (org-agenda-get-deadlines))
  19839. (setq results (append results rtn))))))))
  19840. results))))
  19841. (defun org-entry-is-todo-p ()
  19842. (member (org-get-todo-state) org-not-done-keywords))
  19843. (defun org-entry-is-done-p ()
  19844. (member (org-get-todo-state) org-done-keywords))
  19845. (defun org-get-todo-state ()
  19846. (save-excursion
  19847. (org-back-to-heading t)
  19848. (and (looking-at org-todo-line-regexp)
  19849. (match-end 2)
  19850. (match-string 2))))
  19851. (defun org-at-date-range-p (&optional inactive-ok)
  19852. "Is the cursor inside a date range?"
  19853. (interactive)
  19854. (save-excursion
  19855. (catch 'exit
  19856. (let ((pos (point)))
  19857. (skip-chars-backward "^[<\r\n")
  19858. (skip-chars-backward "<[")
  19859. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19860. (>= (match-end 0) pos)
  19861. (throw 'exit t))
  19862. (skip-chars-backward "^<[\r\n")
  19863. (skip-chars-backward "<[")
  19864. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19865. (>= (match-end 0) pos)
  19866. (throw 'exit t)))
  19867. nil)))
  19868. (defun org-agenda-get-todos ()
  19869. "Return the TODO information for agenda display."
  19870. (let* ((props (list 'face nil
  19871. 'done-face 'org-done
  19872. 'org-not-done-regexp org-not-done-regexp
  19873. 'org-todo-regexp org-todo-regexp
  19874. 'mouse-face 'highlight
  19875. 'keymap org-agenda-keymap
  19876. 'help-echo
  19877. (format "mouse-2 or RET jump to org file %s"
  19878. (abbreviate-file-name buffer-file-name))))
  19879. ;; FIXME: get rid of the \n at some point but watch out
  19880. (regexp (concat "^\\*+[ \t]+\\("
  19881. (if org-select-this-todo-keyword
  19882. (if (equal org-select-this-todo-keyword "*")
  19883. org-todo-regexp
  19884. (concat "\\<\\("
  19885. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19886. "\\)\\>"))
  19887. org-not-done-regexp)
  19888. "[^\n\r]*\\)"))
  19889. marker priority category tags
  19890. ee txt beg end)
  19891. (goto-char (point-min))
  19892. (while (re-search-forward regexp nil t)
  19893. (catch :skip
  19894. (save-match-data
  19895. (beginning-of-line)
  19896. (setq beg (point) end (progn (outline-next-heading) (point)))
  19897. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19898. (re-search-forward org-ts-regexp end t))
  19899. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19900. (re-search-forward org-scheduled-time-regexp end t))
  19901. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19902. (re-search-forward org-deadline-time-regexp end t)
  19903. (org-deadline-close (match-string 1))))
  19904. (goto-char (1+ beg))
  19905. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19906. (throw :skip nil)))
  19907. (goto-char beg)
  19908. (org-agenda-skip)
  19909. (goto-char (match-beginning 1))
  19910. (setq marker (org-agenda-new-marker (match-beginning 0))
  19911. category (org-get-category)
  19912. tags (org-get-tags-at (point))
  19913. txt (org-format-agenda-item "" (match-string 1) category tags)
  19914. priority (1+ (org-get-priority txt)))
  19915. (org-add-props txt props
  19916. 'org-marker marker 'org-hd-marker marker
  19917. 'priority priority 'org-category category
  19918. 'type "todo")
  19919. (push txt ee)
  19920. (if org-agenda-todo-list-sublevels
  19921. (goto-char (match-end 1))
  19922. (org-end-of-subtree 'invisible))))
  19923. (nreverse ee)))
  19924. (defconst org-agenda-no-heading-message
  19925. "No heading for this item in buffer or region.")
  19926. (defun org-agenda-get-timestamps ()
  19927. "Return the date stamp information for agenda display."
  19928. (let* ((props (list 'face nil
  19929. 'org-not-done-regexp org-not-done-regexp
  19930. 'org-todo-regexp org-todo-regexp
  19931. 'mouse-face 'highlight
  19932. 'keymap org-agenda-keymap
  19933. 'help-echo
  19934. (format "mouse-2 or RET jump to org file %s"
  19935. (abbreviate-file-name buffer-file-name))))
  19936. (d1 (calendar-absolute-from-gregorian date))
  19937. (remove-re
  19938. (concat
  19939. (regexp-quote
  19940. (format-time-string
  19941. "<%Y-%m-%d"
  19942. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19943. ".*?>"))
  19944. (regexp
  19945. (concat
  19946. (regexp-quote
  19947. (substring
  19948. (format-time-string
  19949. (car org-time-stamp-formats)
  19950. (apply 'encode-time ; DATE bound by calendar
  19951. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19952. 0 11))
  19953. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19954. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19955. marker hdmarker deadlinep scheduledp donep tmp priority category
  19956. ee txt timestr tags b0 b3 e3 head)
  19957. (goto-char (point-min))
  19958. (while (re-search-forward regexp nil t)
  19959. (setq b0 (match-beginning 0)
  19960. b3 (match-beginning 3) e3 (match-end 3))
  19961. (catch :skip
  19962. (and (org-at-date-range-p) (throw :skip nil))
  19963. (org-agenda-skip)
  19964. (if (and (match-end 1)
  19965. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19966. (throw :skip nil))
  19967. (if (and e3
  19968. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19969. (throw :skip nil))
  19970. (setq marker (org-agenda-new-marker b0)
  19971. category (org-get-category b0)
  19972. tmp (buffer-substring (max (point-min)
  19973. (- b0 org-ds-keyword-length))
  19974. b0)
  19975. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19976. deadlinep (string-match org-deadline-regexp tmp)
  19977. scheduledp (string-match org-scheduled-regexp tmp)
  19978. donep (org-entry-is-done-p))
  19979. (if (or scheduledp deadlinep) (throw :skip t))
  19980. (if (string-match ">" timestr)
  19981. ;; substring should only run to end of time stamp
  19982. (setq timestr (substring timestr 0 (match-end 0))))
  19983. (save-excursion
  19984. (if (re-search-backward "^\\*+ " nil t)
  19985. (progn
  19986. (goto-char (match-beginning 0))
  19987. (setq hdmarker (org-agenda-new-marker)
  19988. tags (org-get-tags-at))
  19989. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19990. (setq head (match-string 1))
  19991. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19992. (setq txt (org-format-agenda-item
  19993. nil head category tags timestr nil
  19994. remove-re)))
  19995. (setq txt org-agenda-no-heading-message))
  19996. (setq priority (org-get-priority txt))
  19997. (org-add-props txt props
  19998. 'org-marker marker 'org-hd-marker hdmarker)
  19999. (org-add-props txt nil 'priority priority
  20000. 'org-category category 'date date
  20001. 'type "timestamp")
  20002. (push txt ee))
  20003. (outline-next-heading)))
  20004. (nreverse ee)))
  20005. (defun org-agenda-get-sexps ()
  20006. "Return the sexp information for agenda display."
  20007. (require 'diary-lib)
  20008. (let* ((props (list 'face nil
  20009. 'mouse-face 'highlight
  20010. 'keymap org-agenda-keymap
  20011. 'help-echo
  20012. (format "mouse-2 or RET jump to org file %s"
  20013. (abbreviate-file-name buffer-file-name))))
  20014. (regexp "^&?%%(")
  20015. marker category ee txt tags entry result beg b sexp sexp-entry)
  20016. (goto-char (point-min))
  20017. (while (re-search-forward regexp nil t)
  20018. (catch :skip
  20019. (org-agenda-skip)
  20020. (setq beg (match-beginning 0))
  20021. (goto-char (1- (match-end 0)))
  20022. (setq b (point))
  20023. (forward-sexp 1)
  20024. (setq sexp (buffer-substring b (point)))
  20025. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  20026. (org-trim (match-string 1))
  20027. ""))
  20028. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  20029. (when result
  20030. (setq marker (org-agenda-new-marker beg)
  20031. category (org-get-category beg))
  20032. (if (string-match "\\S-" result)
  20033. (setq txt result)
  20034. (setq txt "SEXP entry returned empty string"))
  20035. (setq txt (org-format-agenda-item
  20036. "" txt category tags 'time))
  20037. (org-add-props txt props 'org-marker marker)
  20038. (org-add-props txt nil
  20039. 'org-category category 'date date
  20040. 'type "sexp")
  20041. (push txt ee))))
  20042. (nreverse ee)))
  20043. (defun org-agenda-get-closed ()
  20044. "Return the logged TODO entries for agenda display."
  20045. (let* ((props (list 'mouse-face 'highlight
  20046. 'org-not-done-regexp org-not-done-regexp
  20047. 'org-todo-regexp org-todo-regexp
  20048. 'keymap org-agenda-keymap
  20049. 'help-echo
  20050. (format "mouse-2 or RET jump to org file %s"
  20051. (abbreviate-file-name buffer-file-name))))
  20052. (regexp (concat
  20053. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  20054. (regexp-quote
  20055. (substring
  20056. (format-time-string
  20057. (car org-time-stamp-formats)
  20058. (apply 'encode-time ; DATE bound by calendar
  20059. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  20060. 1 11))))
  20061. marker hdmarker priority category tags closedp
  20062. ee txt timestr)
  20063. (goto-char (point-min))
  20064. (while (re-search-forward regexp nil t)
  20065. (catch :skip
  20066. (org-agenda-skip)
  20067. (setq marker (org-agenda-new-marker (match-beginning 0))
  20068. closedp (equal (match-string 1) org-closed-string)
  20069. category (org-get-category (match-beginning 0))
  20070. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  20071. ;; donep (org-entry-is-done-p)
  20072. )
  20073. (if (string-match "\\]" timestr)
  20074. ;; substring should only run to end of time stamp
  20075. (setq timestr (substring timestr 0 (match-end 0))))
  20076. (save-excursion
  20077. (if (re-search-backward "^\\*+ " nil t)
  20078. (progn
  20079. (goto-char (match-beginning 0))
  20080. (setq hdmarker (org-agenda-new-marker)
  20081. tags (org-get-tags-at))
  20082. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20083. (setq txt (org-format-agenda-item
  20084. (if closedp "Closed: " "Clocked: ")
  20085. (match-string 1) category tags timestr)))
  20086. (setq txt org-agenda-no-heading-message))
  20087. (setq priority 100000)
  20088. (org-add-props txt props
  20089. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  20090. 'priority priority 'org-category category
  20091. 'type "closed" 'date date
  20092. 'undone-face 'org-warning 'done-face 'org-done)
  20093. (push txt ee))
  20094. (goto-char (point-at-eol))))
  20095. (nreverse ee)))
  20096. (defun org-agenda-get-deadlines ()
  20097. "Return the deadline information for agenda display."
  20098. (let* ((props (list 'mouse-face 'highlight
  20099. 'org-not-done-regexp org-not-done-regexp
  20100. 'org-todo-regexp org-todo-regexp
  20101. 'keymap org-agenda-keymap
  20102. 'help-echo
  20103. (format "mouse-2 or RET jump to org file %s"
  20104. (abbreviate-file-name buffer-file-name))))
  20105. (regexp org-deadline-time-regexp)
  20106. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20107. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20108. d2 diff dfrac wdays pos pos1 category tags
  20109. ee txt head face s upcomingp donep timestr)
  20110. (goto-char (point-min))
  20111. (while (re-search-forward regexp nil t)
  20112. (catch :skip
  20113. (org-agenda-skip)
  20114. (setq s (match-string 1)
  20115. pos (1- (match-beginning 1))
  20116. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20117. diff (- d2 d1)
  20118. wdays (org-get-wdays s)
  20119. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  20120. upcomingp (and todayp (> diff 0)))
  20121. ;; When to show a deadline in the calendar:
  20122. ;; If the expiration is within wdays warning time.
  20123. ;; Past-due deadlines are only shown on the current date
  20124. (if (or (and (<= diff wdays)
  20125. (and todayp (not org-agenda-only-exact-dates)))
  20126. (= diff 0))
  20127. (save-excursion
  20128. (setq category (org-get-category))
  20129. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20130. (progn
  20131. (goto-char (match-end 0))
  20132. (setq pos1 (match-beginning 0))
  20133. (setq tags (org-get-tags-at pos1))
  20134. (setq head (buffer-substring-no-properties
  20135. (point)
  20136. (progn (skip-chars-forward "^\r\n")
  20137. (point))))
  20138. (setq donep (string-match org-looking-at-done-regexp head))
  20139. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20140. (setq timestr
  20141. (concat (substring s (match-beginning 1)) " "))
  20142. (setq timestr 'time))
  20143. (if (and donep
  20144. (or org-agenda-skip-deadline-if-done
  20145. (not (= diff 0))))
  20146. (setq txt nil)
  20147. (setq txt (org-format-agenda-item
  20148. (if (= diff 0)
  20149. (car org-agenda-deadline-leaders)
  20150. (format (nth 1 org-agenda-deadline-leaders)
  20151. diff))
  20152. head category tags timestr))))
  20153. (setq txt org-agenda-no-heading-message))
  20154. (when txt
  20155. (setq face (org-agenda-deadline-face dfrac))
  20156. (org-add-props txt props
  20157. 'org-marker (org-agenda-new-marker pos)
  20158. 'org-hd-marker (org-agenda-new-marker pos1)
  20159. 'priority (+ (- diff)
  20160. (org-get-priority txt))
  20161. 'org-category category
  20162. 'type (if upcomingp "upcoming-deadline" "deadline")
  20163. 'date (if upcomingp date d2)
  20164. 'face (if donep 'org-done face)
  20165. 'undone-face face 'done-face 'org-done)
  20166. (push txt ee))))))
  20167. (nreverse ee)))
  20168. (defun org-agenda-deadline-face (fraction)
  20169. "Return the face to displaying a deadline item.
  20170. FRACTION is what fraction of the head-warning time has passed."
  20171. (let ((faces org-agenda-deadline-faces) f)
  20172. (catch 'exit
  20173. (while (setq f (pop faces))
  20174. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  20175. (defun org-agenda-get-scheduled ()
  20176. "Return the scheduled information for agenda display."
  20177. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  20178. 'org-todo-regexp org-todo-regexp
  20179. 'done-face 'org-done
  20180. 'mouse-face 'highlight
  20181. 'keymap org-agenda-keymap
  20182. 'help-echo
  20183. (format "mouse-2 or RET jump to org file %s"
  20184. (abbreviate-file-name buffer-file-name))))
  20185. (regexp org-scheduled-time-regexp)
  20186. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20187. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20188. d2 diff pos pos1 category tags
  20189. ee txt head pastschedp donep face timestr s)
  20190. (goto-char (point-min))
  20191. (while (re-search-forward regexp nil t)
  20192. (catch :skip
  20193. (org-agenda-skip)
  20194. (setq s (match-string 1)
  20195. pos (1- (match-beginning 1))
  20196. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20197. ;;; is this right?
  20198. ;;; do we need to do this for deadleine too????
  20199. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  20200. diff (- d2 d1))
  20201. (setq pastschedp (and todayp (< diff 0)))
  20202. ;; When to show a scheduled item in the calendar:
  20203. ;; If it is on or past the date.
  20204. (if (or (and (< diff 0)
  20205. (and todayp (not org-agenda-only-exact-dates)))
  20206. (= diff 0))
  20207. (save-excursion
  20208. (setq category (org-get-category))
  20209. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20210. (progn
  20211. (goto-char (match-end 0))
  20212. (setq pos1 (match-beginning 0))
  20213. (setq tags (org-get-tags-at))
  20214. (setq head (buffer-substring-no-properties
  20215. (point)
  20216. (progn (skip-chars-forward "^\r\n") (point))))
  20217. (setq donep (string-match org-looking-at-done-regexp head))
  20218. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20219. (setq timestr
  20220. (concat (substring s (match-beginning 1)) " "))
  20221. (setq timestr 'time))
  20222. (if (and donep
  20223. (or org-agenda-skip-scheduled-if-done
  20224. (not (= diff 0))))
  20225. (setq txt nil)
  20226. (setq txt (org-format-agenda-item
  20227. (if (= diff 0)
  20228. (car org-agenda-scheduled-leaders)
  20229. (format (nth 1 org-agenda-scheduled-leaders)
  20230. (- 1 diff)))
  20231. head category tags timestr))))
  20232. (setq txt org-agenda-no-heading-message))
  20233. (when txt
  20234. (setq face (if pastschedp
  20235. 'org-scheduled-previously
  20236. 'org-scheduled-today))
  20237. (org-add-props txt props
  20238. 'undone-face face
  20239. 'face (if donep 'org-done face)
  20240. 'org-marker (org-agenda-new-marker pos)
  20241. 'org-hd-marker (org-agenda-new-marker pos1)
  20242. 'type (if pastschedp "past-scheduled" "scheduled")
  20243. 'date (if pastschedp d2 date)
  20244. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  20245. 'org-category category)
  20246. (push txt ee))))))
  20247. (nreverse ee)))
  20248. (defun org-agenda-get-blocks ()
  20249. "Return the date-range information for agenda display."
  20250. (let* ((props (list 'face nil
  20251. 'org-not-done-regexp org-not-done-regexp
  20252. 'org-todo-regexp org-todo-regexp
  20253. 'mouse-face 'highlight
  20254. 'keymap org-agenda-keymap
  20255. 'help-echo
  20256. (format "mouse-2 or RET jump to org file %s"
  20257. (abbreviate-file-name buffer-file-name))))
  20258. (regexp org-tr-regexp)
  20259. (d0 (calendar-absolute-from-gregorian date))
  20260. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  20261. donep head)
  20262. (goto-char (point-min))
  20263. (while (re-search-forward regexp nil t)
  20264. (catch :skip
  20265. (org-agenda-skip)
  20266. (setq pos (point))
  20267. (setq timestr (match-string 0)
  20268. s1 (match-string 1)
  20269. s2 (match-string 2)
  20270. d1 (time-to-days (org-time-string-to-time s1))
  20271. d2 (time-to-days (org-time-string-to-time s2)))
  20272. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  20273. ;; Only allow days between the limits, because the normal
  20274. ;; date stamps will catch the limits.
  20275. (save-excursion
  20276. (setq marker (org-agenda-new-marker (point)))
  20277. (setq category (org-get-category))
  20278. (if (re-search-backward "^\\*+ " nil t)
  20279. (progn
  20280. (goto-char (match-beginning 0))
  20281. (setq hdmarker (org-agenda-new-marker (point)))
  20282. (setq tags (org-get-tags-at))
  20283. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20284. (setq head (match-string 1))
  20285. (and org-agenda-skip-timestamp-if-done
  20286. (org-entry-is-done-p)
  20287. (throw :skip t))
  20288. (setq txt (org-format-agenda-item
  20289. (format (if (= d1 d2) "" "(%d/%d): ")
  20290. (1+ (- d0 d1)) (1+ (- d2 d1)))
  20291. head category tags
  20292. (if (= d0 d1) timestr))))
  20293. (setq txt org-agenda-no-heading-message))
  20294. (org-add-props txt props
  20295. 'org-marker marker 'org-hd-marker hdmarker
  20296. 'type "block" 'date date
  20297. 'priority (org-get-priority txt) 'org-category category)
  20298. (push txt ee)))
  20299. (goto-char pos)))
  20300. ;; Sort the entries by expiration date.
  20301. (nreverse ee)))
  20302. ;;; Agenda presentation and sorting
  20303. (defconst org-plain-time-of-day-regexp
  20304. (concat
  20305. "\\(\\<[012]?[0-9]"
  20306. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20307. "\\(--?"
  20308. "\\(\\<[012]?[0-9]"
  20309. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20310. "\\)?")
  20311. "Regular expression to match a plain time or time range.
  20312. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20313. groups carry important information:
  20314. 0 the full match
  20315. 1 the first time, range or not
  20316. 8 the second time, if it is a range.")
  20317. (defconst org-plain-time-extension-regexp
  20318. (concat
  20319. "\\(\\<[012]?[0-9]"
  20320. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20321. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  20322. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  20323. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20324. groups carry important information:
  20325. 0 the full match
  20326. 7 hours of duration
  20327. 9 minutes of duration")
  20328. (defconst org-stamp-time-of-day-regexp
  20329. (concat
  20330. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  20331. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  20332. "\\(--?"
  20333. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  20334. "Regular expression to match a timestamp time or time range.
  20335. After a match, the following groups carry important information:
  20336. 0 the full match
  20337. 1 date plus weekday, for backreferencing to make sure both times on same day
  20338. 2 the first time, range or not
  20339. 4 the second time, if it is a range.")
  20340. (defvar org-prefix-has-time nil
  20341. "A flag, set by `org-compile-prefix-format'.
  20342. The flag is set if the currently compiled format contains a `%t'.")
  20343. (defvar org-prefix-has-tag nil
  20344. "A flag, set by `org-compile-prefix-format'.
  20345. The flag is set if the currently compiled format contains a `%T'.")
  20346. (defun org-format-agenda-item (extra txt &optional category tags dotime
  20347. noprefix remove-re)
  20348. "Format TXT to be inserted into the agenda buffer.
  20349. In particular, it adds the prefix and corresponding text properties. EXTRA
  20350. must be a string and replaces the `%s' specifier in the prefix format.
  20351. CATEGORY (string, symbol or nil) may be used to overrule the default
  20352. category taken from local variable or file name. It will replace the `%c'
  20353. specifier in the format. DOTIME, when non-nil, indicates that a
  20354. time-of-day should be extracted from TXT for sorting of this entry, and for
  20355. the `%t' specifier in the format. When DOTIME is a string, this string is
  20356. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  20357. only the correctly processes TXT should be returned - this is used by
  20358. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  20359. Any match of REMOVE-RE will be removed from TXT."
  20360. (save-match-data
  20361. ;; Diary entries sometimes have extra whitespace at the beginning
  20362. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  20363. (let* ((category (or category
  20364. org-category
  20365. (if buffer-file-name
  20366. (file-name-sans-extension
  20367. (file-name-nondirectory buffer-file-name))
  20368. "")))
  20369. (tag (if tags (nth (1- (length tags)) tags) ""))
  20370. time ; time and tag are needed for the eval of the prefix format
  20371. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20372. (time-of-day (and dotime (org-get-time-of-day ts)))
  20373. stamp plain s0 s1 s2 rtn srp)
  20374. (when (and dotime time-of-day org-prefix-has-time)
  20375. ;; Extract starting and ending time and move them to prefix
  20376. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20377. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20378. (setq s0 (match-string 0 ts)
  20379. srp (and stamp (match-end 3))
  20380. s1 (match-string (if plain 1 2) ts)
  20381. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20382. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20383. ;; them, we might want to remove them there to avoid duplication.
  20384. ;; The user can turn this off with a variable.
  20385. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20386. (string-match (concat (regexp-quote s0) " *") txt)
  20387. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20388. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20389. (= (match-beginning 0) 0)
  20390. t))
  20391. (setq txt (replace-match "" nil nil txt))))
  20392. ;; Normalize the time(s) to 24 hour
  20393. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20394. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20395. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20396. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20397. (let ((m (+ (string-to-number (match-string 2 s1))
  20398. (* 60 (string-to-number (match-string 1 s1)))
  20399. org-agenda-default-appointment-duration))
  20400. h)
  20401. (setq h (/ m 60) m (- m (* h 60)))
  20402. (setq s2 (format "%02d:%02d" h m))))
  20403. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20404. txt)
  20405. ;; Tags are in the string
  20406. (if (or (eq org-agenda-remove-tags t)
  20407. (and org-agenda-remove-tags
  20408. org-prefix-has-tag))
  20409. (setq txt (replace-match "" t t txt))
  20410. (setq txt (replace-match
  20411. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20412. (match-string 2 txt))
  20413. t t txt))))
  20414. (when remove-re
  20415. (while (string-match remove-re txt)
  20416. (setq txt (replace-match "" t t txt))))
  20417. ;; Create the final string
  20418. (if noprefix
  20419. (setq rtn txt)
  20420. ;; Prepare the variables needed in the eval of the compiled format
  20421. (setq time (cond (s2 (concat s1 "-" s2))
  20422. (s1 (concat s1 "......"))
  20423. (t ""))
  20424. extra (or extra "")
  20425. category (if (symbolp category) (symbol-name category) category))
  20426. ;; Evaluate the compiled format
  20427. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20428. ;; And finally add the text properties
  20429. (org-add-props rtn nil
  20430. 'org-category (downcase category) 'tags tags
  20431. 'org-highest-priority org-highest-priority
  20432. 'org-lowest-priority org-lowest-priority
  20433. 'prefix-length (- (length rtn) (length txt))
  20434. 'time-of-day time-of-day
  20435. 'txt txt
  20436. 'time time
  20437. 'extra extra
  20438. 'dotime dotime))))
  20439. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20440. (defvar org-agenda-sorting-strategy-selected nil)
  20441. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20442. (catch 'exit
  20443. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20444. ((and todayp (member 'today (car org-agenda-time-grid))))
  20445. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20446. ((member 'weekly (car org-agenda-time-grid)))
  20447. (t (throw 'exit list)))
  20448. (let* ((have (delq nil (mapcar
  20449. (lambda (x) (get-text-property 1 'time-of-day x))
  20450. list)))
  20451. (string (nth 1 org-agenda-time-grid))
  20452. (gridtimes (nth 2 org-agenda-time-grid))
  20453. (req (car org-agenda-time-grid))
  20454. (remove (member 'remove-match req))
  20455. new time)
  20456. (if (and (member 'require-timed req) (not have))
  20457. ;; don't show empty grid
  20458. (throw 'exit list))
  20459. (while (setq time (pop gridtimes))
  20460. (unless (and remove (member time have))
  20461. (setq time (int-to-string time))
  20462. (push (org-format-agenda-item
  20463. nil string "" nil
  20464. (concat (substring time 0 -2) ":" (substring time -2)))
  20465. new)
  20466. (put-text-property
  20467. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20468. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20469. (append new list)
  20470. (append list new)))))
  20471. (defun org-compile-prefix-format (key)
  20472. "Compile the prefix format into a Lisp form that can be evaluated.
  20473. The resulting form is returned and stored in the variable
  20474. `org-prefix-format-compiled'."
  20475. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20476. (let ((s (cond
  20477. ((stringp org-agenda-prefix-format)
  20478. org-agenda-prefix-format)
  20479. ((assq key org-agenda-prefix-format)
  20480. (cdr (assq key org-agenda-prefix-format)))
  20481. (t " %-12:c%?-12t% s")))
  20482. (start 0)
  20483. varform vars var e c f opt)
  20484. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20485. s start)
  20486. (setq var (cdr (assoc (match-string 4 s)
  20487. '(("c" . category) ("t" . time) ("s" . extra)
  20488. ("T" . tag))))
  20489. c (or (match-string 3 s) "")
  20490. opt (match-beginning 1)
  20491. start (1+ (match-beginning 0)))
  20492. (if (equal var 'time) (setq org-prefix-has-time t))
  20493. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20494. (setq f (concat "%" (match-string 2 s) "s"))
  20495. (if opt
  20496. (setq varform
  20497. `(if (equal "" ,var)
  20498. ""
  20499. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20500. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20501. (setq s (replace-match "%s" t nil s))
  20502. (push varform vars))
  20503. (setq vars (nreverse vars))
  20504. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20505. (defun org-set-sorting-strategy (key)
  20506. (if (symbolp (car org-agenda-sorting-strategy))
  20507. ;; the old format
  20508. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20509. (setq org-agenda-sorting-strategy-selected
  20510. (or (cdr (assq key org-agenda-sorting-strategy))
  20511. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20512. '(time-up category-keep priority-down)))))
  20513. (defun org-get-time-of-day (s &optional string mod24)
  20514. "Check string S for a time of day.
  20515. If found, return it as a military time number between 0 and 2400.
  20516. If not found, return nil.
  20517. The optional STRING argument forces conversion into a 5 character wide string
  20518. HH:MM."
  20519. (save-match-data
  20520. (when
  20521. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20522. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20523. (let* ((h (string-to-number (match-string 1 s)))
  20524. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20525. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20526. (am-p (equal ampm "am"))
  20527. (h1 (cond ((not ampm) h)
  20528. ((= h 12) (if am-p 0 12))
  20529. (t (+ h (if am-p 0 12)))))
  20530. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20531. (mod h1 24) h1))
  20532. (t0 (+ (* 100 h2) m))
  20533. (t1 (concat (if (>= h1 24) "+" " ")
  20534. (if (< t0 100) "0" "")
  20535. (if (< t0 10) "0" "")
  20536. (int-to-string t0))))
  20537. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20538. (defun org-finalize-agenda-entries (list &optional nosort)
  20539. "Sort and concatenate the agenda items."
  20540. (setq list (mapcar 'org-agenda-highlight-todo list))
  20541. (if nosort
  20542. list
  20543. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20544. (defun org-agenda-highlight-todo (x)
  20545. (let (re pl)
  20546. (if (eq x 'line)
  20547. (save-excursion
  20548. (beginning-of-line 1)
  20549. (setq re (get-text-property (point) 'org-todo-regexp))
  20550. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20551. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20552. (add-text-properties (match-beginning 0) (match-end 0)
  20553. (list 'face (org-get-todo-face 0)))
  20554. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20555. (delete-region (match-beginning 1) (1- (match-end 0)))
  20556. (goto-char (match-beginning 1))
  20557. (insert (format org-agenda-todo-keyword-format s)))))
  20558. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20559. pl (get-text-property 0 'prefix-length x))
  20560. (when (and re
  20561. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20562. x (or pl 0)) pl))
  20563. (add-text-properties
  20564. (or (match-end 1) (match-end 0)) (match-end 0)
  20565. (list 'face (org-get-todo-face (match-string 2 x)))
  20566. x)
  20567. (setq x (concat (substring x 0 (match-end 1))
  20568. (format org-agenda-todo-keyword-format
  20569. (match-string 2 x))
  20570. " "
  20571. (substring x (match-end 3)))))
  20572. x)))
  20573. (defsubst org-cmp-priority (a b)
  20574. "Compare the priorities of string A and B."
  20575. (let ((pa (or (get-text-property 1 'priority a) 0))
  20576. (pb (or (get-text-property 1 'priority b) 0)))
  20577. (cond ((> pa pb) +1)
  20578. ((< pa pb) -1)
  20579. (t nil))))
  20580. (defsubst org-cmp-category (a b)
  20581. "Compare the string values of categories of strings A and B."
  20582. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20583. (cb (or (get-text-property 1 'org-category b) "")))
  20584. (cond ((string-lessp ca cb) -1)
  20585. ((string-lessp cb ca) +1)
  20586. (t nil))))
  20587. (defsubst org-cmp-tag (a b)
  20588. "Compare the string values of categories of strings A and B."
  20589. (let ((ta (car (last (get-text-property 1 'tags a))))
  20590. (tb (car (last (get-text-property 1 'tags b)))))
  20591. (cond ((not ta) +1)
  20592. ((not tb) -1)
  20593. ((string-lessp ta tb) -1)
  20594. ((string-lessp tb ta) +1)
  20595. (t nil))))
  20596. (defsubst org-cmp-time (a b)
  20597. "Compare the time-of-day values of strings A and B."
  20598. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20599. (ta (or (get-text-property 1 'time-of-day a) def))
  20600. (tb (or (get-text-property 1 'time-of-day b) def)))
  20601. (cond ((< ta tb) -1)
  20602. ((< tb ta) +1)
  20603. (t nil))))
  20604. (defun org-entries-lessp (a b)
  20605. "Predicate for sorting agenda entries."
  20606. ;; The following variables will be used when the form is evaluated.
  20607. ;; So even though the compiler complains, keep them.
  20608. (let* ((time-up (org-cmp-time a b))
  20609. (time-down (if time-up (- time-up) nil))
  20610. (priority-up (org-cmp-priority a b))
  20611. (priority-down (if priority-up (- priority-up) nil))
  20612. (category-up (org-cmp-category a b))
  20613. (category-down (if category-up (- category-up) nil))
  20614. (category-keep (if category-up +1 nil))
  20615. (tag-up (org-cmp-tag a b))
  20616. (tag-down (if tag-up (- tag-up) nil)))
  20617. (cdr (assoc
  20618. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20619. '((-1 . t) (1 . nil) (nil . nil))))))
  20620. ;;; Agenda restriction lock
  20621. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20622. "Overlay to mark the headline to which arenda commands are restricted.")
  20623. (org-overlay-put org-agenda-restriction-lock-overlay
  20624. 'face 'org-agenda-restriction-lock)
  20625. (org-overlay-put org-agenda-restriction-lock-overlay
  20626. 'help-echo "Agendas are currently limited to this subtree.")
  20627. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20628. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20629. "Overlay marking the agenda restriction line in speedbar.")
  20630. (org-overlay-put org-speedbar-restriction-lock-overlay
  20631. 'face 'org-agenda-restriction-lock)
  20632. (org-overlay-put org-speedbar-restriction-lock-overlay
  20633. 'help-echo "Agendas are currently limited to this item.")
  20634. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20635. (defun org-agenda-set-restriction-lock (&optional type)
  20636. "Set restriction lock for agenda, to current subtree or file.
  20637. Restriction will be the file if TYPE is `file', or if type is the
  20638. universal prefix '(4), or if the cursor is before the first headline
  20639. in the file. Otherwise, restriction will be to the current subtree."
  20640. (interactive "P")
  20641. (and (equal type '(4)) (setq type 'file))
  20642. (setq type (cond
  20643. (type type)
  20644. ((org-at-heading-p) 'subtree)
  20645. ((condition-case nil (org-back-to-heading t) (error nil))
  20646. 'subtree)
  20647. (t 'file)))
  20648. (if (eq type 'subtree)
  20649. (progn
  20650. (setq org-agenda-restrict t)
  20651. (setq org-agenda-overriding-restriction 'subtree)
  20652. (put 'org-agenda-files 'org-restrict
  20653. (list (buffer-file-name (buffer-base-buffer))))
  20654. (org-back-to-heading t)
  20655. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20656. (move-marker org-agenda-restrict-begin (point))
  20657. (move-marker org-agenda-restrict-end
  20658. (save-excursion (org-end-of-subtree t)))
  20659. (message "Locking agenda restriction to subtree"))
  20660. (put 'org-agenda-files 'org-restrict
  20661. (list (buffer-file-name (buffer-base-buffer))))
  20662. (setq org-agenda-restrict nil)
  20663. (setq org-agenda-overriding-restriction 'file)
  20664. (move-marker org-agenda-restrict-begin nil)
  20665. (move-marker org-agenda-restrict-end nil)
  20666. (message "Locking agenda restriction to file"))
  20667. (setq current-prefix-arg nil)
  20668. (org-agenda-maybe-redo))
  20669. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20670. "Remove the agenda restriction lock."
  20671. (interactive "P")
  20672. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20673. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20674. (setq org-agenda-overriding-restriction nil)
  20675. (setq org-agenda-restrict nil)
  20676. (put 'org-agenda-files 'org-restrict nil)
  20677. (move-marker org-agenda-restrict-begin nil)
  20678. (move-marker org-agenda-restrict-end nil)
  20679. (setq current-prefix-arg nil)
  20680. (message "Agenda restriction lock removed")
  20681. (or noupdate (org-agenda-maybe-redo)))
  20682. (defun org-agenda-maybe-redo ()
  20683. "If there is any window showing the agenda view, update it."
  20684. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20685. (w0 (selected-window)))
  20686. (when w
  20687. (select-window w)
  20688. (org-agenda-redo)
  20689. (select-window w0)
  20690. (if org-agenda-overriding-restriction
  20691. (message "Agenda view shifted to new %s restriction"
  20692. org-agenda-overriding-restriction)
  20693. (message "Agenda restriction lock removed")))))
  20694. ;;; Agenda commands
  20695. (defun org-agenda-check-type (error &rest types)
  20696. "Check if agenda buffer is of allowed type.
  20697. If ERROR is non-nil, throw an error, otherwise just return nil."
  20698. (if (memq org-agenda-type types)
  20699. t
  20700. (if error
  20701. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20702. nil)))
  20703. (defun org-agenda-quit ()
  20704. "Exit agenda by removing the window or the buffer."
  20705. (interactive)
  20706. (let ((buf (current-buffer)))
  20707. (if (not (one-window-p)) (delete-window))
  20708. (kill-buffer buf)
  20709. (org-agenda-reset-markers)
  20710. (org-columns-remove-overlays))
  20711. ;; Maybe restore the pre-agenda window configuration.
  20712. (and org-agenda-restore-windows-after-quit
  20713. (not (eq org-agenda-window-setup 'other-frame))
  20714. org-pre-agenda-window-conf
  20715. (set-window-configuration org-pre-agenda-window-conf)))
  20716. (defun org-agenda-exit ()
  20717. "Exit agenda by removing the window or the buffer.
  20718. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20719. Org-mode buffers visited directly by the user will not be touched."
  20720. (interactive)
  20721. (org-release-buffers org-agenda-new-buffers)
  20722. (setq org-agenda-new-buffers nil)
  20723. (org-agenda-quit))
  20724. (defun org-agenda-execute (arg)
  20725. "Execute another agenda command, keeping same window.\\<global-map>
  20726. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20727. (interactive "P")
  20728. (let ((org-agenda-window-setup 'current-window))
  20729. (org-agenda arg)))
  20730. (defun org-save-all-org-buffers ()
  20731. "Save all Org-mode buffers without user confirmation."
  20732. (interactive)
  20733. (message "Saving all Org-mode buffers...")
  20734. (save-some-buffers t 'org-mode-p)
  20735. (message "Saving all Org-mode buffers... done"))
  20736. (defun org-agenda-redo ()
  20737. "Rebuild Agenda.
  20738. When this is the global TODO list, a prefix argument will be interpreted."
  20739. (interactive)
  20740. (let* ((org-agenda-keep-modes t)
  20741. (line (org-current-line))
  20742. (window-line (- line (org-current-line (window-start))))
  20743. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20744. (message "Rebuilding agenda buffer...")
  20745. (org-let lprops '(eval org-agenda-redo-command))
  20746. (setq org-agenda-undo-list nil
  20747. org-agenda-pending-undo-list nil)
  20748. (message "Rebuilding agenda buffer...done")
  20749. (goto-line line)
  20750. (recenter window-line)))
  20751. (defun org-agenda-manipulate-query-add ()
  20752. "Manipulate the query by adding a search term with positive selection.
  20753. Positive selection means, the term must be matched for selection of an entry."
  20754. (interactive)
  20755. (org-agenda-manipulate-query ?\[))
  20756. (defun org-agenda-manipulate-query-subtract ()
  20757. "Manipulate the query by adding a search term with negative selection.
  20758. Negative selection means, term must not be matched for selection of an entry."
  20759. (interactive)
  20760. (org-agenda-manipulate-query ?\]))
  20761. (defun org-agenda-manipulate-query-add-re ()
  20762. "Manipulate the query by adding a search regexp with positive selection.
  20763. Positive selection means, the regexp must match for selection of an entry."
  20764. (interactive)
  20765. (org-agenda-manipulate-query ?\{))
  20766. (defun org-agenda-manipulate-query-subtract-re ()
  20767. "Manipulate the query by adding a search regexp with negative selection.
  20768. Negative selection means, regexp must not match for selection of an entry."
  20769. (interactive)
  20770. (org-agenda-manipulate-query ?\}))
  20771. (defun org-agenda-manipulate-query (char)
  20772. (cond
  20773. ((eq org-agenda-type 'search)
  20774. (org-add-to-string
  20775. 'org-agenda-query-string
  20776. (cdr (assoc char '((?\[ . " +") (?\] . " -")
  20777. (?\{ . " +{}") (?\} . " -{}")))))
  20778. (setq org-agenda-redo-command
  20779. (list 'org-search-view
  20780. (+ (length org-agenda-query-string)
  20781. (if (member char '(?\{ ?\})) 0 1))
  20782. org-agenda-query-string))
  20783. (set-register org-agenda-query-register org-agenda-query-string)
  20784. (org-agenda-redo))
  20785. (t (error "Canot manipulate query for %s-type agenda buffers"
  20786. org-agenda-type))))
  20787. (defun org-add-to-string (var string)
  20788. (set var (concat (symbol-value var) string)))
  20789. (defun org-agenda-goto-date (date)
  20790. "Jump to DATE in agenda."
  20791. (interactive (list (org-read-date)))
  20792. (org-agenda-list nil date))
  20793. (defun org-agenda-goto-today ()
  20794. "Go to today."
  20795. (interactive)
  20796. (org-agenda-check-type t 'timeline 'agenda)
  20797. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20798. (cond
  20799. (tdpos (goto-char tdpos))
  20800. ((eq org-agenda-type 'agenda)
  20801. (let* ((sd (time-to-days
  20802. (time-subtract (current-time)
  20803. (list 0 (* 3600 org-extend-today-until) 0))))
  20804. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20805. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20806. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20807. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20808. (org-agenda-redo)
  20809. (org-agenda-find-same-or-today-or-agenda)))
  20810. (t (error "Cannot find today")))))
  20811. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20812. (goto-char
  20813. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20814. (text-property-any (point-min) (point-max) 'org-today t)
  20815. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20816. (point-min))))
  20817. (defun org-agenda-later (arg)
  20818. "Go forward in time by thee current span.
  20819. With prefix ARG, go forward that many times the current span."
  20820. (interactive "p")
  20821. (org-agenda-check-type t 'agenda)
  20822. (let* ((span org-agenda-span)
  20823. (sd org-starting-day)
  20824. (greg (calendar-gregorian-from-absolute sd))
  20825. (cnt (get-text-property (point) 'org-day-cnt))
  20826. greg2 nd)
  20827. (cond
  20828. ((eq span 'day)
  20829. (setq sd (+ arg sd) nd 1))
  20830. ((eq span 'week)
  20831. (setq sd (+ (* 7 arg) sd) nd 7))
  20832. ((eq span 'month)
  20833. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20834. sd (calendar-absolute-from-gregorian greg2))
  20835. (setcar greg2 (1+ (car greg2)))
  20836. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20837. ((eq span 'year)
  20838. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20839. sd (calendar-absolute-from-gregorian greg2))
  20840. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20841. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20842. (let ((org-agenda-overriding-arguments
  20843. (list (car org-agenda-last-arguments) sd nd t)))
  20844. (org-agenda-redo)
  20845. (org-agenda-find-same-or-today-or-agenda cnt))))
  20846. (defun org-agenda-earlier (arg)
  20847. "Go backward in time by the current span.
  20848. With prefix ARG, go backward that many times the current span."
  20849. (interactive "p")
  20850. (org-agenda-later (- arg)))
  20851. (defun org-agenda-day-view ()
  20852. "Switch to daily view for agenda."
  20853. (interactive)
  20854. (setq org-agenda-ndays 1)
  20855. (org-agenda-change-time-span 'day))
  20856. (defun org-agenda-week-view ()
  20857. "Switch to daily view for agenda."
  20858. (interactive)
  20859. (setq org-agenda-ndays 7)
  20860. (org-agenda-change-time-span 'week))
  20861. (defun org-agenda-month-view ()
  20862. "Switch to daily view for agenda."
  20863. (interactive)
  20864. (org-agenda-change-time-span 'month))
  20865. (defun org-agenda-year-view ()
  20866. "Switch to daily view for agenda."
  20867. (interactive)
  20868. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20869. (org-agenda-change-time-span 'year)
  20870. (error "Abort")))
  20871. (defun org-agenda-change-time-span (span)
  20872. "Change the agenda view to SPAN.
  20873. SPAN may be `day', `week', `month', `year'."
  20874. (org-agenda-check-type t 'agenda)
  20875. (if (equal org-agenda-span span)
  20876. (error "Viewing span is already \"%s\"" span))
  20877. (let* ((sd (or (get-text-property (point) 'day)
  20878. org-starting-day))
  20879. (computed (org-agenda-compute-time-span sd span))
  20880. (org-agenda-overriding-arguments
  20881. (list (car org-agenda-last-arguments)
  20882. (car computed) (cdr computed) t)))
  20883. (org-agenda-redo)
  20884. (org-agenda-find-same-or-today-or-agenda))
  20885. (org-agenda-set-mode-name)
  20886. (message "Switched to %s view" span))
  20887. (defun org-agenda-compute-time-span (sd span)
  20888. "Compute starting date and number of days for agenda.
  20889. SPAN may be `day', `week', `month', `year'. The return value
  20890. is a cons cell with the starting date and the number of days,
  20891. so that the date SD will be in that range."
  20892. (let* ((greg (calendar-gregorian-from-absolute sd))
  20893. nd)
  20894. (cond
  20895. ((eq span 'day)
  20896. (setq nd 1))
  20897. ((eq span 'week)
  20898. (let* ((nt (calendar-day-of-week
  20899. (calendar-gregorian-from-absolute sd)))
  20900. (d (if org-agenda-start-on-weekday
  20901. (- nt org-agenda-start-on-weekday)
  20902. 0)))
  20903. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20904. (setq nd 7)))
  20905. ((eq span 'month)
  20906. (setq sd (calendar-absolute-from-gregorian
  20907. (list (car greg) 1 (nth 2 greg)))
  20908. nd (- (calendar-absolute-from-gregorian
  20909. (list (1+ (car greg)) 1 (nth 2 greg)))
  20910. sd)))
  20911. ((eq span 'year)
  20912. (setq sd (calendar-absolute-from-gregorian
  20913. (list 1 1 (nth 2 greg)))
  20914. nd (- (calendar-absolute-from-gregorian
  20915. (list 1 1 (1+ (nth 2 greg))))
  20916. sd))))
  20917. (cons sd nd)))
  20918. ;; FIXME: does not work if user makes date format that starts with a blank
  20919. (defun org-agenda-next-date-line (&optional arg)
  20920. "Jump to the next line indicating a date in agenda buffer."
  20921. (interactive "p")
  20922. (org-agenda-check-type t 'agenda 'timeline)
  20923. (beginning-of-line 1)
  20924. (if (looking-at "^\\S-") (forward-char 1))
  20925. (if (not (re-search-forward "^\\S-" nil t arg))
  20926. (progn
  20927. (backward-char 1)
  20928. (error "No next date after this line in this buffer")))
  20929. (goto-char (match-beginning 0)))
  20930. (defun org-agenda-previous-date-line (&optional arg)
  20931. "Jump to the previous line indicating a date in agenda buffer."
  20932. (interactive "p")
  20933. (org-agenda-check-type t 'agenda 'timeline)
  20934. (beginning-of-line 1)
  20935. (if (not (re-search-backward "^\\S-" nil t arg))
  20936. (error "No previous date before this line in this buffer")))
  20937. ;; Initialize the highlight
  20938. (defvar org-hl (org-make-overlay 1 1))
  20939. (org-overlay-put org-hl 'face 'highlight)
  20940. (defun org-highlight (begin end &optional buffer)
  20941. "Highlight a region with overlay."
  20942. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20943. org-hl begin end (or buffer (current-buffer))))
  20944. (defun org-unhighlight ()
  20945. "Detach overlay INDEX."
  20946. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20947. ;; FIXME this is currently not used.
  20948. (defun org-highlight-until-next-command (beg end &optional buffer)
  20949. (org-highlight beg end buffer)
  20950. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20951. (defun org-unhighlight-once ()
  20952. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20953. (org-unhighlight))
  20954. (defun org-agenda-follow-mode ()
  20955. "Toggle follow mode in an agenda buffer."
  20956. (interactive)
  20957. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20958. (org-agenda-set-mode-name)
  20959. (message "Follow mode is %s"
  20960. (if org-agenda-follow-mode "on" "off")))
  20961. (defun org-agenda-log-mode ()
  20962. "Toggle log mode in an agenda buffer."
  20963. (interactive)
  20964. (org-agenda-check-type t 'agenda 'timeline)
  20965. (setq org-agenda-show-log (not org-agenda-show-log))
  20966. (org-agenda-set-mode-name)
  20967. (org-agenda-redo)
  20968. (message "Log mode is %s"
  20969. (if org-agenda-show-log "on" "off")))
  20970. (defun org-agenda-toggle-diary ()
  20971. "Toggle diary inclusion in an agenda buffer."
  20972. (interactive)
  20973. (org-agenda-check-type t 'agenda)
  20974. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20975. (org-agenda-redo)
  20976. (org-agenda-set-mode-name)
  20977. (message "Diary inclusion turned %s"
  20978. (if org-agenda-include-diary "on" "off")))
  20979. (defun org-agenda-toggle-time-grid ()
  20980. "Toggle time grid in an agenda buffer."
  20981. (interactive)
  20982. (org-agenda-check-type t 'agenda)
  20983. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20984. (org-agenda-redo)
  20985. (org-agenda-set-mode-name)
  20986. (message "Time-grid turned %s"
  20987. (if org-agenda-use-time-grid "on" "off")))
  20988. (defun org-agenda-set-mode-name ()
  20989. "Set the mode name to indicate all the small mode settings."
  20990. (setq mode-name
  20991. (concat "Org-Agenda"
  20992. (if (equal org-agenda-ndays 1) " Day" "")
  20993. (if (equal org-agenda-ndays 7) " Week" "")
  20994. (if org-agenda-follow-mode " Follow" "")
  20995. (if org-agenda-include-diary " Diary" "")
  20996. (if org-agenda-use-time-grid " Grid" "")
  20997. (if org-agenda-show-log " Log" "")))
  20998. (force-mode-line-update))
  20999. (defun org-agenda-post-command-hook ()
  21000. (and (eolp) (not (bolp)) (backward-char 1))
  21001. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  21002. (if (and org-agenda-follow-mode
  21003. (get-text-property (point) 'org-marker))
  21004. (org-agenda-show)))
  21005. (defun org-agenda-show-priority ()
  21006. "Show the priority of the current item.
  21007. This priority is composed of the main priority given with the [#A] cookies,
  21008. and by additional input from the age of a schedules or deadline entry."
  21009. (interactive)
  21010. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  21011. (message "Priority is %d" (if pri pri -1000))))
  21012. (defun org-agenda-show-tags ()
  21013. "Show the tags applicable to the current item."
  21014. (interactive)
  21015. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  21016. (if tags
  21017. (message "Tags are :%s:"
  21018. (org-no-properties (mapconcat 'identity tags ":")))
  21019. (message "No tags associated with this line"))))
  21020. (defun org-agenda-goto (&optional highlight)
  21021. "Go to the Org-mode file which contains the item at point."
  21022. (interactive)
  21023. (let* ((marker (or (get-text-property (point) 'org-marker)
  21024. (org-agenda-error)))
  21025. (buffer (marker-buffer marker))
  21026. (pos (marker-position marker)))
  21027. (switch-to-buffer-other-window buffer)
  21028. (widen)
  21029. (goto-char pos)
  21030. (when (org-mode-p)
  21031. (org-show-context 'agenda)
  21032. (save-excursion
  21033. (and (outline-next-heading)
  21034. (org-flag-heading nil)))) ; show the next heading
  21035. (recenter (/ (window-height) 2))
  21036. (run-hooks 'org-agenda-after-show-hook)
  21037. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  21038. (defvar org-agenda-after-show-hook nil
  21039. "Normal hook run after an item has been shown from the agenda.
  21040. Point is in the buffer where the item originated.")
  21041. (defun org-agenda-kill ()
  21042. "Kill the entry or subtree belonging to the current agenda entry."
  21043. (interactive)
  21044. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  21045. (let* ((marker (or (get-text-property (point) 'org-marker)
  21046. (org-agenda-error)))
  21047. (buffer (marker-buffer marker))
  21048. (pos (marker-position marker))
  21049. (type (get-text-property (point) 'type))
  21050. dbeg dend (n 0) conf)
  21051. (org-with-remote-undo buffer
  21052. (with-current-buffer buffer
  21053. (save-excursion
  21054. (goto-char pos)
  21055. (if (and (org-mode-p) (not (member type '("sexp"))))
  21056. (setq dbeg (progn (org-back-to-heading t) (point))
  21057. dend (org-end-of-subtree t t))
  21058. (setq dbeg (point-at-bol)
  21059. dend (min (point-max) (1+ (point-at-eol)))))
  21060. (goto-char dbeg)
  21061. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  21062. (setq conf (or (eq t org-agenda-confirm-kill)
  21063. (and (numberp org-agenda-confirm-kill)
  21064. (> n org-agenda-confirm-kill))))
  21065. (and conf
  21066. (not (y-or-n-p
  21067. (format "Delete entry with %d lines in buffer \"%s\"? "
  21068. n (buffer-name buffer))))
  21069. (error "Abort"))
  21070. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  21071. (with-current-buffer buffer (delete-region dbeg dend))
  21072. (message "Agenda item and source killed"))))
  21073. (defun org-agenda-archive ()
  21074. "Kill the entry or subtree belonging to the current agenda entry."
  21075. (interactive)
  21076. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  21077. (let* ((marker (or (get-text-property (point) 'org-marker)
  21078. (org-agenda-error)))
  21079. (buffer (marker-buffer marker))
  21080. (pos (marker-position marker)))
  21081. (org-with-remote-undo buffer
  21082. (with-current-buffer buffer
  21083. (if (org-mode-p)
  21084. (save-excursion
  21085. (goto-char pos)
  21086. (org-remove-subtree-entries-from-agenda)
  21087. (org-back-to-heading t)
  21088. (org-archive-subtree))
  21089. (error "Archiving works only in Org-mode files"))))))
  21090. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  21091. "Remove all lines in the agenda that correspond to a given subtree.
  21092. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  21093. If this information is not given, the function uses the tree at point."
  21094. (let ((buf (or buf (current-buffer))) m p)
  21095. (save-excursion
  21096. (unless (and beg end)
  21097. (org-back-to-heading t)
  21098. (setq beg (point))
  21099. (org-end-of-subtree t)
  21100. (setq end (point)))
  21101. (set-buffer (get-buffer org-agenda-buffer-name))
  21102. (save-excursion
  21103. (goto-char (point-max))
  21104. (beginning-of-line 1)
  21105. (while (not (bobp))
  21106. (when (and (setq m (get-text-property (point) 'org-marker))
  21107. (equal buf (marker-buffer m))
  21108. (setq p (marker-position m))
  21109. (>= p beg)
  21110. (<= p end))
  21111. (let ((inhibit-read-only t))
  21112. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  21113. (beginning-of-line 0))))))
  21114. (defun org-agenda-open-link ()
  21115. "Follow the link in the current line, if any."
  21116. (interactive)
  21117. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  21118. (save-excursion
  21119. (save-restriction
  21120. (narrow-to-region (point-at-bol) (point-at-eol))
  21121. (org-open-at-point))))
  21122. (defun org-agenda-copy-local-variable (var)
  21123. "Get a variable from a referenced buffer and install it here."
  21124. (let ((m (get-text-property (point) 'org-marker)))
  21125. (when (and m (buffer-live-p (marker-buffer m)))
  21126. (org-set-local var (with-current-buffer (marker-buffer m)
  21127. (symbol-value var))))))
  21128. (defun org-agenda-switch-to (&optional delete-other-windows)
  21129. "Go to the Org-mode file which contains the item at point."
  21130. (interactive)
  21131. (let* ((marker (or (get-text-property (point) 'org-marker)
  21132. (org-agenda-error)))
  21133. (buffer (marker-buffer marker))
  21134. (pos (marker-position marker)))
  21135. (switch-to-buffer buffer)
  21136. (and delete-other-windows (delete-other-windows))
  21137. (widen)
  21138. (goto-char pos)
  21139. (when (org-mode-p)
  21140. (org-show-context 'agenda)
  21141. (save-excursion
  21142. (and (outline-next-heading)
  21143. (org-flag-heading nil)))))) ; show the next heading
  21144. (defun org-agenda-goto-mouse (ev)
  21145. "Go to the Org-mode file which contains the item at the mouse click."
  21146. (interactive "e")
  21147. (mouse-set-point ev)
  21148. (org-agenda-goto))
  21149. (defun org-agenda-show ()
  21150. "Display the Org-mode file which contains the item at point."
  21151. (interactive)
  21152. (let ((win (selected-window)))
  21153. (org-agenda-goto t)
  21154. (select-window win)))
  21155. (defun org-agenda-recenter (arg)
  21156. "Display the Org-mode file which contains the item at point and recenter."
  21157. (interactive "P")
  21158. (let ((win (selected-window)))
  21159. (org-agenda-goto t)
  21160. (recenter arg)
  21161. (select-window win)))
  21162. (defun org-agenda-show-mouse (ev)
  21163. "Display the Org-mode file which contains the item at the mouse click."
  21164. (interactive "e")
  21165. (mouse-set-point ev)
  21166. (org-agenda-show))
  21167. (defun org-agenda-check-no-diary ()
  21168. "Check if the entry is a diary link and abort if yes."
  21169. (if (get-text-property (point) 'org-agenda-diary-link)
  21170. (org-agenda-error)))
  21171. (defun org-agenda-error ()
  21172. (error "Command not allowed in this line"))
  21173. (defun org-agenda-tree-to-indirect-buffer ()
  21174. "Show the subtree corresponding to the current entry in an indirect buffer.
  21175. This calls the command `org-tree-to-indirect-buffer' from the original
  21176. Org-mode buffer.
  21177. With numerical prefix arg ARG, go up to this level and then take that tree.
  21178. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  21179. dedicated frame)."
  21180. (interactive)
  21181. (org-agenda-check-no-diary)
  21182. (let* ((marker (or (get-text-property (point) 'org-marker)
  21183. (org-agenda-error)))
  21184. (buffer (marker-buffer marker))
  21185. (pos (marker-position marker)))
  21186. (with-current-buffer buffer
  21187. (save-excursion
  21188. (goto-char pos)
  21189. (call-interactively 'org-tree-to-indirect-buffer)))))
  21190. (defvar org-last-heading-marker (make-marker)
  21191. "Marker pointing to the headline that last changed its TODO state
  21192. by a remote command from the agenda.")
  21193. (defun org-agenda-todo-nextset ()
  21194. "Switch TODO entry to next sequence."
  21195. (interactive)
  21196. (org-agenda-todo 'nextset))
  21197. (defun org-agenda-todo-previousset ()
  21198. "Switch TODO entry to previous sequence."
  21199. (interactive)
  21200. (org-agenda-todo 'previousset))
  21201. (defun org-agenda-todo (&optional arg)
  21202. "Cycle TODO state of line at point, also in Org-mode file.
  21203. This changes the line at point, all other lines in the agenda referring to
  21204. the same tree node, and the headline of the tree node in the Org-mode file."
  21205. (interactive "P")
  21206. (org-agenda-check-no-diary)
  21207. (let* ((col (current-column))
  21208. (marker (or (get-text-property (point) 'org-marker)
  21209. (org-agenda-error)))
  21210. (buffer (marker-buffer marker))
  21211. (pos (marker-position marker))
  21212. (hdmarker (get-text-property (point) 'org-hd-marker))
  21213. (inhibit-read-only t)
  21214. newhead)
  21215. (org-with-remote-undo buffer
  21216. (with-current-buffer buffer
  21217. (widen)
  21218. (goto-char pos)
  21219. (org-show-context 'agenda)
  21220. (save-excursion
  21221. (and (outline-next-heading)
  21222. (org-flag-heading nil))) ; show the next heading
  21223. (org-todo arg)
  21224. (and (bolp) (forward-char 1))
  21225. (setq newhead (org-get-heading))
  21226. (save-excursion
  21227. (org-back-to-heading)
  21228. (move-marker org-last-heading-marker (point))))
  21229. (beginning-of-line 1)
  21230. (save-excursion
  21231. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  21232. (move-to-column col))))
  21233. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  21234. "Change all lines in the agenda buffer which match HDMARKER.
  21235. The new content of the line will be NEWHEAD (as modified by
  21236. `org-format-agenda-item'). HDMARKER is checked with
  21237. `equal' against all `org-hd-marker' text properties in the file.
  21238. If FIXFACE is non-nil, the face of each item is modified acording to
  21239. the new TODO state."
  21240. (let* ((inhibit-read-only t)
  21241. props m pl undone-face done-face finish new dotime cat tags)
  21242. (save-excursion
  21243. (goto-char (point-max))
  21244. (beginning-of-line 1)
  21245. (while (not finish)
  21246. (setq finish (bobp))
  21247. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  21248. (equal m hdmarker))
  21249. (setq props (text-properties-at (point))
  21250. dotime (get-text-property (point) 'dotime)
  21251. cat (get-text-property (point) 'org-category)
  21252. tags (get-text-property (point) 'tags)
  21253. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  21254. pl (get-text-property (point) 'prefix-length)
  21255. undone-face (get-text-property (point) 'undone-face)
  21256. done-face (get-text-property (point) 'done-face))
  21257. (move-to-column pl)
  21258. (cond
  21259. ((equal new "")
  21260. (beginning-of-line 1)
  21261. (and (looking-at ".*\n?") (replace-match "")))
  21262. ((looking-at ".*")
  21263. (replace-match new t t)
  21264. (beginning-of-line 1)
  21265. (add-text-properties (point-at-bol) (point-at-eol) props)
  21266. (when fixface
  21267. (add-text-properties
  21268. (point-at-bol) (point-at-eol)
  21269. (list 'face
  21270. (if org-last-todo-state-is-todo
  21271. undone-face done-face))))
  21272. (org-agenda-highlight-todo 'line)
  21273. (beginning-of-line 1))
  21274. (t (error "Line update did not work"))))
  21275. (beginning-of-line 0)))
  21276. (org-finalize-agenda)))
  21277. (defun org-agenda-align-tags (&optional line)
  21278. "Align all tags in agenda items to `org-agenda-tags-column'."
  21279. (let ((inhibit-read-only t) l c)
  21280. (save-excursion
  21281. (goto-char (if line (point-at-bol) (point-min)))
  21282. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  21283. (if line (point-at-eol) nil) t)
  21284. (add-text-properties
  21285. (match-beginning 2) (match-end 2)
  21286. (list 'face (list 'org-tag (get-text-property
  21287. (match-beginning 2) 'face))))
  21288. (setq l (- (match-end 2) (match-beginning 2))
  21289. c (if (< org-agenda-tags-column 0)
  21290. (- (abs org-agenda-tags-column) l)
  21291. org-agenda-tags-column))
  21292. (delete-region (match-beginning 1) (match-end 1))
  21293. (goto-char (match-beginning 1))
  21294. (insert (org-add-props
  21295. (make-string (max 1 (- c (current-column))) ?\ )
  21296. (text-properties-at (point))))))))
  21297. (defun org-agenda-priority-up ()
  21298. "Increase the priority of line at point, also in Org-mode file."
  21299. (interactive)
  21300. (org-agenda-priority 'up))
  21301. (defun org-agenda-priority-down ()
  21302. "Decrease the priority of line at point, also in Org-mode file."
  21303. (interactive)
  21304. (org-agenda-priority 'down))
  21305. (defun org-agenda-priority (&optional force-direction)
  21306. "Set the priority of line at point, also in Org-mode file.
  21307. This changes the line at point, all other lines in the agenda referring to
  21308. the same tree node, and the headline of the tree node in the Org-mode file."
  21309. (interactive)
  21310. (org-agenda-check-no-diary)
  21311. (let* ((marker (or (get-text-property (point) 'org-marker)
  21312. (org-agenda-error)))
  21313. (hdmarker (get-text-property (point) 'org-hd-marker))
  21314. (buffer (marker-buffer hdmarker))
  21315. (pos (marker-position hdmarker))
  21316. (inhibit-read-only t)
  21317. newhead)
  21318. (org-with-remote-undo buffer
  21319. (with-current-buffer buffer
  21320. (widen)
  21321. (goto-char pos)
  21322. (org-show-context 'agenda)
  21323. (save-excursion
  21324. (and (outline-next-heading)
  21325. (org-flag-heading nil))) ; show the next heading
  21326. (funcall 'org-priority force-direction)
  21327. (end-of-line 1)
  21328. (setq newhead (org-get-heading)))
  21329. (org-agenda-change-all-lines newhead hdmarker)
  21330. (beginning-of-line 1))))
  21331. (defun org-get-tags-at (&optional pos)
  21332. "Get a list of all headline tags applicable at POS.
  21333. POS defaults to point. If tags are inherited, the list contains
  21334. the targets in the same sequence as the headlines appear, i.e.
  21335. the tags of the current headline come last."
  21336. (interactive)
  21337. (let (tags lastpos)
  21338. (save-excursion
  21339. (save-restriction
  21340. (widen)
  21341. (goto-char (or pos (point)))
  21342. (save-match-data
  21343. (org-back-to-heading t)
  21344. (condition-case nil
  21345. (while (not (equal lastpos (point)))
  21346. (setq lastpos (point))
  21347. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  21348. (setq tags (append (org-split-string
  21349. (org-match-string-no-properties 1) ":")
  21350. tags)))
  21351. (or org-use-tag-inheritance (error ""))
  21352. (org-up-heading-all 1))
  21353. (error nil))))
  21354. tags)))
  21355. ;; FIXME: should fix the tags property of the agenda line.
  21356. (defun org-agenda-set-tags ()
  21357. "Set tags for the current headline."
  21358. (interactive)
  21359. (org-agenda-check-no-diary)
  21360. (if (and (org-region-active-p) (interactive-p))
  21361. (call-interactively 'org-change-tag-in-region)
  21362. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21363. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21364. (org-agenda-error)))
  21365. (buffer (marker-buffer hdmarker))
  21366. (pos (marker-position hdmarker))
  21367. (inhibit-read-only t)
  21368. newhead)
  21369. (org-with-remote-undo buffer
  21370. (with-current-buffer buffer
  21371. (widen)
  21372. (goto-char pos)
  21373. (save-excursion
  21374. (org-show-context 'agenda))
  21375. (save-excursion
  21376. (and (outline-next-heading)
  21377. (org-flag-heading nil))) ; show the next heading
  21378. (goto-char pos)
  21379. (call-interactively 'org-set-tags)
  21380. (end-of-line 1)
  21381. (setq newhead (org-get-heading)))
  21382. (org-agenda-change-all-lines newhead hdmarker)
  21383. (beginning-of-line 1)))))
  21384. (defun org-agenda-toggle-archive-tag ()
  21385. "Toggle the archive tag for the current entry."
  21386. (interactive)
  21387. (org-agenda-check-no-diary)
  21388. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21389. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21390. (org-agenda-error)))
  21391. (buffer (marker-buffer hdmarker))
  21392. (pos (marker-position hdmarker))
  21393. (inhibit-read-only t)
  21394. newhead)
  21395. (org-with-remote-undo buffer
  21396. (with-current-buffer buffer
  21397. (widen)
  21398. (goto-char pos)
  21399. (org-show-context 'agenda)
  21400. (save-excursion
  21401. (and (outline-next-heading)
  21402. (org-flag-heading nil))) ; show the next heading
  21403. (call-interactively 'org-toggle-archive-tag)
  21404. (end-of-line 1)
  21405. (setq newhead (org-get-heading)))
  21406. (org-agenda-change-all-lines newhead hdmarker)
  21407. (beginning-of-line 1))))
  21408. (defun org-agenda-date-later (arg &optional what)
  21409. "Change the date of this item to one day later."
  21410. (interactive "p")
  21411. (org-agenda-check-type t 'agenda 'timeline)
  21412. (org-agenda-check-no-diary)
  21413. (let* ((marker (or (get-text-property (point) 'org-marker)
  21414. (org-agenda-error)))
  21415. (buffer (marker-buffer marker))
  21416. (pos (marker-position marker)))
  21417. (org-with-remote-undo buffer
  21418. (with-current-buffer buffer
  21419. (widen)
  21420. (goto-char pos)
  21421. (if (not (org-at-timestamp-p))
  21422. (error "Cannot find time stamp"))
  21423. (org-timestamp-change arg (or what 'day)))
  21424. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21425. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21426. (defun org-agenda-date-earlier (arg &optional what)
  21427. "Change the date of this item to one day earlier."
  21428. (interactive "p")
  21429. (org-agenda-date-later (- arg) what))
  21430. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21431. "Show new date stamp via text properties."
  21432. ;; We use text properties to make this undoable
  21433. (let ((inhibit-read-only t))
  21434. (setq stamp (concat " " prefix " => " stamp))
  21435. (save-excursion
  21436. (goto-char (point-max))
  21437. (while (not (bobp))
  21438. (when (equal marker (get-text-property (point) 'org-marker))
  21439. (move-to-column (- (window-width) (length stamp)) t)
  21440. (if (featurep 'xemacs)
  21441. ;; Use `duplicable' property to trigger undo recording
  21442. (let ((ex (make-extent nil nil))
  21443. (gl (make-glyph stamp)))
  21444. (set-glyph-face gl 'secondary-selection)
  21445. (set-extent-properties
  21446. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21447. (insert-extent ex (1- (point)) (point-at-eol)))
  21448. (add-text-properties
  21449. (1- (point)) (point-at-eol)
  21450. (list 'display (org-add-props stamp nil
  21451. 'face 'secondary-selection))))
  21452. (beginning-of-line 1))
  21453. (beginning-of-line 0)))))
  21454. (defun org-agenda-date-prompt (arg)
  21455. "Change the date of this item. Date is prompted for, with default today.
  21456. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21457. be used to request time specification in the time stamp."
  21458. (interactive "P")
  21459. (org-agenda-check-type t 'agenda 'timeline)
  21460. (org-agenda-check-no-diary)
  21461. (let* ((marker (or (get-text-property (point) 'org-marker)
  21462. (org-agenda-error)))
  21463. (buffer (marker-buffer marker))
  21464. (pos (marker-position marker)))
  21465. (org-with-remote-undo buffer
  21466. (with-current-buffer buffer
  21467. (widen)
  21468. (goto-char pos)
  21469. (if (not (org-at-timestamp-p))
  21470. (error "Cannot find time stamp"))
  21471. (org-time-stamp arg)
  21472. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21473. (defun org-agenda-schedule (arg)
  21474. "Schedule the item at point."
  21475. (interactive "P")
  21476. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21477. (org-agenda-check-no-diary)
  21478. (let* ((marker (or (get-text-property (point) 'org-marker)
  21479. (org-agenda-error)))
  21480. (buffer (marker-buffer marker))
  21481. (pos (marker-position marker))
  21482. (org-insert-labeled-timestamps-at-point nil)
  21483. ts)
  21484. (message "%s" (marker-insertion-type marker)) (sit-for 3)
  21485. (set-marker-insertion-type marker t)
  21486. (org-with-remote-undo buffer
  21487. (with-current-buffer buffer
  21488. (widen)
  21489. (goto-char pos)
  21490. (setq ts (org-schedule arg)))
  21491. (org-agenda-show-new-time marker ts "S"))
  21492. (message "Item scheduled for %s" ts)))
  21493. (defun org-agenda-deadline (arg)
  21494. "Schedule the item at point."
  21495. (interactive "P")
  21496. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21497. (org-agenda-check-no-diary)
  21498. (let* ((marker (or (get-text-property (point) 'org-marker)
  21499. (org-agenda-error)))
  21500. (buffer (marker-buffer marker))
  21501. (pos (marker-position marker))
  21502. (org-insert-labeled-timestamps-at-point nil)
  21503. ts)
  21504. (org-with-remote-undo buffer
  21505. (with-current-buffer buffer
  21506. (widen)
  21507. (goto-char pos)
  21508. (setq ts (org-deadline arg)))
  21509. (org-agenda-show-new-time marker ts "S"))
  21510. (message "Deadline for this item set to %s" ts)))
  21511. (defun org-get-heading (&optional no-tags)
  21512. "Return the heading of the current entry, without the stars."
  21513. (save-excursion
  21514. (org-back-to-heading t)
  21515. (if (looking-at
  21516. (if no-tags
  21517. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21518. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21519. (match-string 1) "")))
  21520. (defun org-agenda-clock-in (&optional arg)
  21521. "Start the clock on the currently selected item."
  21522. (interactive "P")
  21523. (org-agenda-check-no-diary)
  21524. (let* ((marker (or (get-text-property (point) 'org-marker)
  21525. (org-agenda-error)))
  21526. (pos (marker-position marker)))
  21527. (org-with-remote-undo (marker-buffer marker)
  21528. (with-current-buffer (marker-buffer marker)
  21529. (widen)
  21530. (goto-char pos)
  21531. (org-clock-in)))))
  21532. (defun org-agenda-clock-out (&optional arg)
  21533. "Stop the currently running clock."
  21534. (interactive "P")
  21535. (unless (marker-buffer org-clock-marker)
  21536. (error "No running clock"))
  21537. (org-with-remote-undo (marker-buffer org-clock-marker)
  21538. (org-clock-out)))
  21539. (defun org-agenda-clock-cancel (&optional arg)
  21540. "Cancel the currently running clock."
  21541. (interactive "P")
  21542. (unless (marker-buffer org-clock-marker)
  21543. (error "No running clock"))
  21544. (org-with-remote-undo (marker-buffer org-clock-marker)
  21545. (org-clock-cancel)))
  21546. (defun org-agenda-diary-entry ()
  21547. "Make a diary entry, like the `i' command from the calendar.
  21548. All the standard commands work: block, weekly etc."
  21549. (interactive)
  21550. (org-agenda-check-type t 'agenda 'timeline)
  21551. (require 'diary-lib)
  21552. (let* ((char (progn
  21553. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21554. (read-char-exclusive)))
  21555. (cmd (cdr (assoc char
  21556. '((?d . insert-diary-entry)
  21557. (?w . insert-weekly-diary-entry)
  21558. (?m . insert-monthly-diary-entry)
  21559. (?y . insert-yearly-diary-entry)
  21560. (?a . insert-anniversary-diary-entry)
  21561. (?b . insert-block-diary-entry)
  21562. (?c . insert-cyclic-diary-entry)))))
  21563. (oldf (symbol-function 'calendar-cursor-to-date))
  21564. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21565. (point (point))
  21566. (mark (or (mark t) (point))))
  21567. (unless cmd
  21568. (error "No command associated with <%c>" char))
  21569. (unless (and (get-text-property point 'day)
  21570. (or (not (equal ?b char))
  21571. (get-text-property mark 'day)))
  21572. (error "Don't know which date to use for diary entry"))
  21573. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21574. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21575. (let ((calendar-mark-ring
  21576. (list (calendar-gregorian-from-absolute
  21577. (or (get-text-property mark 'day)
  21578. (get-text-property point 'day))))))
  21579. (unwind-protect
  21580. (progn
  21581. (fset 'calendar-cursor-to-date
  21582. (lambda (&optional error)
  21583. (calendar-gregorian-from-absolute
  21584. (get-text-property point 'day))))
  21585. (call-interactively cmd))
  21586. (fset 'calendar-cursor-to-date oldf)))))
  21587. (defun org-agenda-execute-calendar-command (cmd)
  21588. "Execute a calendar command from the agenda, with the date associated to
  21589. the cursor position."
  21590. (org-agenda-check-type t 'agenda 'timeline)
  21591. (require 'diary-lib)
  21592. (unless (get-text-property (point) 'day)
  21593. (error "Don't know which date to use for calendar command"))
  21594. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21595. (point (point))
  21596. (date (calendar-gregorian-from-absolute
  21597. (get-text-property point 'day)))
  21598. ;; the following 3 vars are needed in the calendar
  21599. (displayed-day (extract-calendar-day date))
  21600. (displayed-month (extract-calendar-month date))
  21601. (displayed-year (extract-calendar-year date)))
  21602. (unwind-protect
  21603. (progn
  21604. (fset 'calendar-cursor-to-date
  21605. (lambda (&optional error)
  21606. (calendar-gregorian-from-absolute
  21607. (get-text-property point 'day))))
  21608. (call-interactively cmd))
  21609. (fset 'calendar-cursor-to-date oldf))))
  21610. (defun org-agenda-phases-of-moon ()
  21611. "Display the phases of the moon for the 3 months around the cursor date."
  21612. (interactive)
  21613. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21614. (defun org-agenda-holidays ()
  21615. "Display the holidays for the 3 months around the cursor date."
  21616. (interactive)
  21617. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21618. (defun org-agenda-sunrise-sunset (arg)
  21619. "Display sunrise and sunset for the cursor date.
  21620. Latitude and longitude can be specified with the variables
  21621. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21622. argument, latitude and longitude will be prompted for."
  21623. (interactive "P")
  21624. (let ((calendar-longitude (if arg nil calendar-longitude))
  21625. (calendar-latitude (if arg nil calendar-latitude))
  21626. (calendar-location-name
  21627. (if arg "the given coordinates" calendar-location-name)))
  21628. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21629. (defun org-agenda-goto-calendar ()
  21630. "Open the Emacs calendar with the date at the cursor."
  21631. (interactive)
  21632. (org-agenda-check-type t 'agenda 'timeline)
  21633. (let* ((day (or (get-text-property (point) 'day)
  21634. (error "Don't know which date to open in calendar")))
  21635. (date (calendar-gregorian-from-absolute day))
  21636. (calendar-move-hook nil)
  21637. (view-calendar-holidays-initially nil)
  21638. (view-diary-entries-initially nil))
  21639. (calendar)
  21640. (calendar-goto-date date)))
  21641. (defun org-calendar-goto-agenda ()
  21642. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21643. This is a command that has to be installed in `calendar-mode-map'."
  21644. (interactive)
  21645. (org-agenda-list nil (calendar-absolute-from-gregorian
  21646. (calendar-cursor-to-date))
  21647. nil))
  21648. (defun org-agenda-convert-date ()
  21649. (interactive)
  21650. (org-agenda-check-type t 'agenda 'timeline)
  21651. (let ((day (get-text-property (point) 'day))
  21652. date s)
  21653. (unless day
  21654. (error "Don't know which date to convert"))
  21655. (setq date (calendar-gregorian-from-absolute day))
  21656. (setq s (concat
  21657. "Gregorian: " (calendar-date-string date) "\n"
  21658. "ISO: " (calendar-iso-date-string date) "\n"
  21659. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21660. "Julian: " (calendar-julian-date-string date) "\n"
  21661. "Astron. JD: " (calendar-astro-date-string date)
  21662. " (Julian date number at noon UTC)\n"
  21663. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21664. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21665. "French: " (calendar-french-date-string date) "\n"
  21666. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21667. "Mayan: " (calendar-mayan-date-string date) "\n"
  21668. "Coptic: " (calendar-coptic-date-string date) "\n"
  21669. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21670. "Persian: " (calendar-persian-date-string date) "\n"
  21671. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21672. (with-output-to-temp-buffer "*Dates*"
  21673. (princ s))
  21674. (if (fboundp 'fit-window-to-buffer)
  21675. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21676. ;;;; Embedded LaTeX
  21677. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21678. "Keymap for the minor `org-cdlatex-mode'.")
  21679. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21680. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21681. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21682. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21683. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21684. (defvar org-cdlatex-texmathp-advice-is-done nil
  21685. "Flag remembering if we have applied the advice to texmathp already.")
  21686. (define-minor-mode org-cdlatex-mode
  21687. "Toggle the minor `org-cdlatex-mode'.
  21688. This mode supports entering LaTeX environment and math in LaTeX fragments
  21689. in Org-mode.
  21690. \\{org-cdlatex-mode-map}"
  21691. nil " OCDL" nil
  21692. (when org-cdlatex-mode (require 'cdlatex))
  21693. (unless org-cdlatex-texmathp-advice-is-done
  21694. (setq org-cdlatex-texmathp-advice-is-done t)
  21695. (defadvice texmathp (around org-math-always-on activate)
  21696. "Always return t in org-mode buffers.
  21697. This is because we want to insert math symbols without dollars even outside
  21698. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21699. en embedded LaTeX fragement, let texmathp do its job.
  21700. \\[org-cdlatex-mode-map]"
  21701. (interactive)
  21702. (let (p)
  21703. (cond
  21704. ((not (org-mode-p)) ad-do-it)
  21705. ((eq this-command 'cdlatex-math-symbol)
  21706. (setq ad-return-value t
  21707. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21708. (t
  21709. (let ((p (org-inside-LaTeX-fragment-p)))
  21710. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21711. (setq ad-return-value t
  21712. texmathp-why '("Org-mode embedded math" . 0))
  21713. (if p ad-do-it)))))))))
  21714. (defun turn-on-org-cdlatex ()
  21715. "Unconditionally turn on `org-cdlatex-mode'."
  21716. (org-cdlatex-mode 1))
  21717. (defun org-inside-LaTeX-fragment-p ()
  21718. "Test if point is inside a LaTeX fragment.
  21719. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21720. sequence appearing also before point.
  21721. Even though the matchers for math are configurable, this function assumes
  21722. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21723. delimiters are skipped when they have been removed by customization.
  21724. The return value is nil, or a cons cell with the delimiter and
  21725. and the position of this delimiter.
  21726. This function does a reasonably good job, but can locally be fooled by
  21727. for example currency specifications. For example it will assume being in
  21728. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21729. fragments that are properly closed, but during editing, we have to live
  21730. with the uncertainty caused by missing closing delimiters. This function
  21731. looks only before point, not after."
  21732. (catch 'exit
  21733. (let ((pos (point))
  21734. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21735. (lim (progn
  21736. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21737. (point)))
  21738. dd-on str (start 0) m re)
  21739. (goto-char pos)
  21740. (when dodollar
  21741. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21742. re (nth 1 (assoc "$" org-latex-regexps)))
  21743. (while (string-match re str start)
  21744. (cond
  21745. ((= (match-end 0) (length str))
  21746. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21747. ((= (match-end 0) (- (length str) 5))
  21748. (throw 'exit nil))
  21749. (t (setq start (match-end 0))))))
  21750. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21751. (goto-char pos)
  21752. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21753. (and (match-beginning 2) (throw 'exit nil))
  21754. ;; count $$
  21755. (while (re-search-backward "\\$\\$" lim t)
  21756. (setq dd-on (not dd-on)))
  21757. (goto-char pos)
  21758. (if dd-on (cons "$$" m))))))
  21759. (defun org-try-cdlatex-tab ()
  21760. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21761. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21762. - inside a LaTeX fragment, or
  21763. - after the first word in a line, where an abbreviation expansion could
  21764. insert a LaTeX environment."
  21765. (when org-cdlatex-mode
  21766. (cond
  21767. ((save-excursion
  21768. (skip-chars-backward "a-zA-Z0-9*")
  21769. (skip-chars-backward " \t")
  21770. (bolp))
  21771. (cdlatex-tab) t)
  21772. ((org-inside-LaTeX-fragment-p)
  21773. (cdlatex-tab) t)
  21774. (t nil))))
  21775. (defun org-cdlatex-underscore-caret (&optional arg)
  21776. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21777. Revert to the normal definition outside of these fragments."
  21778. (interactive "P")
  21779. (if (org-inside-LaTeX-fragment-p)
  21780. (call-interactively 'cdlatex-sub-superscript)
  21781. (let (org-cdlatex-mode)
  21782. (call-interactively (key-binding (vector last-input-event))))))
  21783. (defun org-cdlatex-math-modify (&optional arg)
  21784. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21785. Revert to the normal definition outside of these fragments."
  21786. (interactive "P")
  21787. (if (org-inside-LaTeX-fragment-p)
  21788. (call-interactively 'cdlatex-math-modify)
  21789. (let (org-cdlatex-mode)
  21790. (call-interactively (key-binding (vector last-input-event))))))
  21791. (defvar org-latex-fragment-image-overlays nil
  21792. "List of overlays carrying the images of latex fragments.")
  21793. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21794. (defun org-remove-latex-fragment-image-overlays ()
  21795. "Remove all overlays with LaTeX fragment images in current buffer."
  21796. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21797. (setq org-latex-fragment-image-overlays nil))
  21798. (defun org-preview-latex-fragment (&optional subtree)
  21799. "Preview the LaTeX fragment at point, or all locally or globally.
  21800. If the cursor is in a LaTeX fragment, create the image and overlay
  21801. it over the source code. If there is no fragment at point, display
  21802. all fragments in the current text, from one headline to the next. With
  21803. prefix SUBTREE, display all fragments in the current subtree. With a
  21804. double prefix `C-u C-u', or when the cursor is before the first headline,
  21805. display all fragments in the buffer.
  21806. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21807. (interactive "P")
  21808. (org-remove-latex-fragment-image-overlays)
  21809. (save-excursion
  21810. (save-restriction
  21811. (let (beg end at msg)
  21812. (cond
  21813. ((or (equal subtree '(16))
  21814. (not (save-excursion
  21815. (re-search-backward (concat "^" outline-regexp) nil t))))
  21816. (setq beg (point-min) end (point-max)
  21817. msg "Creating images for buffer...%s"))
  21818. ((equal subtree '(4))
  21819. (org-back-to-heading)
  21820. (setq beg (point) end (org-end-of-subtree t)
  21821. msg "Creating images for subtree...%s"))
  21822. (t
  21823. (if (setq at (org-inside-LaTeX-fragment-p))
  21824. (goto-char (max (point-min) (- (cdr at) 2)))
  21825. (org-back-to-heading))
  21826. (setq beg (point) end (progn (outline-next-heading) (point))
  21827. msg (if at "Creating image...%s"
  21828. "Creating images for entry...%s"))))
  21829. (message msg "")
  21830. (narrow-to-region beg end)
  21831. (goto-char beg)
  21832. (org-format-latex
  21833. (concat "ltxpng/" (file-name-sans-extension
  21834. (file-name-nondirectory
  21835. buffer-file-name)))
  21836. default-directory 'overlays msg at 'forbuffer)
  21837. (message msg "done. Use `C-c C-c' to remove images.")))))
  21838. (defvar org-latex-regexps
  21839. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21840. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21841. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21842. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21843. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21844. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21845. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21846. "Regular expressions for matching embedded LaTeX.")
  21847. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21848. "Replace LaTeX fragments with links to an image, and produce images."
  21849. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21850. (let* ((prefixnodir (file-name-nondirectory prefix))
  21851. (absprefix (expand-file-name prefix dir))
  21852. (todir (file-name-directory absprefix))
  21853. (opt org-format-latex-options)
  21854. (matchers (plist-get opt :matchers))
  21855. (re-list org-latex-regexps)
  21856. (cnt 0) txt link beg end re e checkdir
  21857. m n block linkfile movefile ov)
  21858. ;; Check if there are old images files with this prefix, and remove them
  21859. (when (file-directory-p todir)
  21860. (mapc 'delete-file
  21861. (directory-files
  21862. todir 'full
  21863. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21864. ;; Check the different regular expressions
  21865. (while (setq e (pop re-list))
  21866. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21867. block (if (nth 3 e) "\n\n" ""))
  21868. (when (member m matchers)
  21869. (goto-char (point-min))
  21870. (while (re-search-forward re nil t)
  21871. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21872. (setq txt (match-string n)
  21873. beg (match-beginning n) end (match-end n)
  21874. cnt (1+ cnt)
  21875. linkfile (format "%s_%04d.png" prefix cnt)
  21876. movefile (format "%s_%04d.png" absprefix cnt)
  21877. link (concat block "[[file:" linkfile "]]" block))
  21878. (if msg (message msg cnt))
  21879. (goto-char beg)
  21880. (unless checkdir ; make sure the directory exists
  21881. (setq checkdir t)
  21882. (or (file-directory-p todir) (make-directory todir)))
  21883. (org-create-formula-image
  21884. txt movefile opt forbuffer)
  21885. (if overlays
  21886. (progn
  21887. (setq ov (org-make-overlay beg end))
  21888. (if (featurep 'xemacs)
  21889. (progn
  21890. (org-overlay-put ov 'invisible t)
  21891. (org-overlay-put
  21892. ov 'end-glyph
  21893. (make-glyph (vector 'png :file movefile))))
  21894. (org-overlay-put
  21895. ov 'display
  21896. (list 'image :type 'png :file movefile :ascent 'center)))
  21897. (push ov org-latex-fragment-image-overlays)
  21898. (goto-char end))
  21899. (delete-region beg end)
  21900. (insert link))))))))
  21901. ;; This function borrows from Ganesh Swami's latex2png.el
  21902. (defun org-create-formula-image (string tofile options buffer)
  21903. (let* ((tmpdir (if (featurep 'xemacs)
  21904. (temp-directory)
  21905. temporary-file-directory))
  21906. (texfilebase (make-temp-name
  21907. (expand-file-name "orgtex" tmpdir)))
  21908. (texfile (concat texfilebase ".tex"))
  21909. (dvifile (concat texfilebase ".dvi"))
  21910. (pngfile (concat texfilebase ".png"))
  21911. (fnh (face-attribute 'default :height nil))
  21912. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21913. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21914. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21915. "Black"))
  21916. (bg (or (plist-get options (if buffer :background :html-background))
  21917. "Transparent")))
  21918. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21919. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21920. (with-temp-file texfile
  21921. (insert org-format-latex-header
  21922. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21923. (let ((dir default-directory))
  21924. (condition-case nil
  21925. (progn
  21926. (cd tmpdir)
  21927. (call-process "latex" nil nil nil texfile))
  21928. (error nil))
  21929. (cd dir))
  21930. (if (not (file-exists-p dvifile))
  21931. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21932. (call-process "dvipng" nil nil nil
  21933. "-E" "-fg" fg "-bg" bg
  21934. "-D" dpi
  21935. ;;"-x" scale "-y" scale
  21936. "-T" "tight"
  21937. "-o" pngfile
  21938. dvifile)
  21939. (if (not (file-exists-p pngfile))
  21940. (progn (message "Failed to create png file from %s" texfile) nil)
  21941. ;; Use the requested file name and clean up
  21942. (copy-file pngfile tofile 'replace)
  21943. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21944. (delete-file (concat texfilebase e)))
  21945. pngfile))))
  21946. (defun org-dvipng-color (attr)
  21947. "Return an rgb color specification for dvipng."
  21948. (apply 'format "rgb %s %s %s"
  21949. (mapcar 'org-normalize-color
  21950. (color-values (face-attribute 'default attr nil)))))
  21951. (defun org-normalize-color (value)
  21952. "Return string to be used as color value for an RGB component."
  21953. (format "%g" (/ value 65535.0)))
  21954. ;;;; Exporting
  21955. ;;; Variables, constants, and parameter plists
  21956. (defconst org-level-max 20)
  21957. (defvar org-export-html-preamble nil
  21958. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21959. (defvar org-export-html-postamble nil
  21960. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21961. (defvar org-export-html-auto-preamble t
  21962. "Should default preamble be inserted? Set by publishing functions.")
  21963. (defvar org-export-html-auto-postamble t
  21964. "Should default postamble be inserted? Set by publishing functions.")
  21965. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21966. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21967. (defconst org-export-plist-vars
  21968. '((:language . org-export-default-language)
  21969. (:customtime . org-display-custom-times)
  21970. (:headline-levels . org-export-headline-levels)
  21971. (:section-numbers . org-export-with-section-numbers)
  21972. (:table-of-contents . org-export-with-toc)
  21973. (:preserve-breaks . org-export-preserve-breaks)
  21974. (:archived-trees . org-export-with-archived-trees)
  21975. (:emphasize . org-export-with-emphasize)
  21976. (:sub-superscript . org-export-with-sub-superscripts)
  21977. (:special-strings . org-export-with-special-strings)
  21978. (:footnotes . org-export-with-footnotes)
  21979. (:drawers . org-export-with-drawers)
  21980. (:tags . org-export-with-tags)
  21981. (:TeX-macros . org-export-with-TeX-macros)
  21982. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21983. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21984. (:fixed-width . org-export-with-fixed-width)
  21985. (:timestamps . org-export-with-timestamps)
  21986. (:author-info . org-export-author-info)
  21987. (:time-stamp-file . org-export-time-stamp-file)
  21988. (:tables . org-export-with-tables)
  21989. (:table-auto-headline . org-export-highlight-first-table-line)
  21990. (:style . org-export-html-style)
  21991. (:agenda-style . org-agenda-export-html-style)
  21992. (:convert-org-links . org-export-html-link-org-files-as-html)
  21993. (:inline-images . org-export-html-inline-images)
  21994. (:html-extension . org-export-html-extension)
  21995. (:html-table-tag . org-export-html-table-tag)
  21996. (:expand-quoted-html . org-export-html-expand)
  21997. (:timestamp . org-export-html-with-timestamp)
  21998. (:publishing-directory . org-export-publishing-directory)
  21999. (:preamble . org-export-html-preamble)
  22000. (:postamble . org-export-html-postamble)
  22001. (:auto-preamble . org-export-html-auto-preamble)
  22002. (:auto-postamble . org-export-html-auto-postamble)
  22003. (:author . user-full-name)
  22004. (:email . user-mail-address)))
  22005. (defun org-default-export-plist ()
  22006. "Return the property list with default settings for the export variables."
  22007. (let ((l org-export-plist-vars) rtn e)
  22008. (while (setq e (pop l))
  22009. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  22010. rtn))
  22011. (defun org-infile-export-plist ()
  22012. "Return the property list with file-local settings for export."
  22013. (save-excursion
  22014. (save-restriction
  22015. (widen)
  22016. (goto-char 0)
  22017. (let ((re (org-make-options-regexp
  22018. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  22019. p key val text options)
  22020. (while (re-search-forward re nil t)
  22021. (setq key (org-match-string-no-properties 1)
  22022. val (org-match-string-no-properties 2))
  22023. (cond
  22024. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  22025. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  22026. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  22027. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  22028. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  22029. ((string-equal key "TEXT")
  22030. (setq text (if text (concat text "\n" val) val)))
  22031. ((string-equal key "OPTIONS") (setq options val))))
  22032. (setq p (plist-put p :text text))
  22033. (when options
  22034. (let ((op '(("H" . :headline-levels)
  22035. ("num" . :section-numbers)
  22036. ("toc" . :table-of-contents)
  22037. ("\\n" . :preserve-breaks)
  22038. ("@" . :expand-quoted-html)
  22039. (":" . :fixed-width)
  22040. ("|" . :tables)
  22041. ("^" . :sub-superscript)
  22042. ("-" . :special-strings)
  22043. ("f" . :footnotes)
  22044. ("d" . :drawers)
  22045. ("tags" . :tags)
  22046. ("*" . :emphasize)
  22047. ("TeX" . :TeX-macros)
  22048. ("LaTeX" . :LaTeX-fragments)
  22049. ("skip" . :skip-before-1st-heading)
  22050. ("author" . :author-info)
  22051. ("timestamp" . :time-stamp-file)))
  22052. o)
  22053. (while (setq o (pop op))
  22054. (if (string-match (concat (regexp-quote (car o))
  22055. ":\\([^ \t\n\r;,.]*\\)")
  22056. options)
  22057. (setq p (plist-put p (cdr o)
  22058. (car (read-from-string
  22059. (match-string 1 options)))))))))
  22060. p))))
  22061. (defun org-export-directory (type plist)
  22062. (let* ((val (plist-get plist :publishing-directory))
  22063. (dir (if (listp val)
  22064. (or (cdr (assoc type val)) ".")
  22065. val)))
  22066. dir))
  22067. (defun org-skip-comments (lines)
  22068. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  22069. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  22070. (re2 "^\\(\\*+\\)[ \t\n\r]")
  22071. (case-fold-search nil)
  22072. rtn line level)
  22073. (while (setq line (pop lines))
  22074. (cond
  22075. ((and (string-match re1 line)
  22076. (setq level (- (match-end 1) (match-beginning 1))))
  22077. ;; Beginning of a COMMENT subtree. Skip it.
  22078. (while (and (setq line (pop lines))
  22079. (or (not (string-match re2 line))
  22080. (> (- (match-end 1) (match-beginning 1)) level))))
  22081. (setq lines (cons line lines)))
  22082. ((string-match "^#" line)
  22083. ;; an ordinary comment line
  22084. )
  22085. ((and org-export-table-remove-special-lines
  22086. (string-match "^[ \t]*|" line)
  22087. (or (string-match "^[ \t]*| *[!_^] *|" line)
  22088. (and (string-match "| *<[0-9]+> *|" line)
  22089. (not (string-match "| *[^ <|]" line)))))
  22090. ;; a special table line that should be removed
  22091. )
  22092. (t (setq rtn (cons line rtn)))))
  22093. (nreverse rtn)))
  22094. (defun org-export (&optional arg)
  22095. (interactive)
  22096. (let ((help "[t] insert the export option template
  22097. \[v] limit export to visible part of outline tree
  22098. \[a] export as ASCII
  22099. \[h] export as HTML
  22100. \[H] export as HTML to temporary buffer
  22101. \[R] export region as HTML
  22102. \[b] export as HTML and browse immediately
  22103. \[x] export as XOXO
  22104. \[l] export as LaTeX
  22105. \[L] export as LaTeX to temporary buffer
  22106. \[i] export current file as iCalendar file
  22107. \[I] export all agenda files as iCalendar files
  22108. \[c] export agenda files into combined iCalendar file
  22109. \[F] publish current file
  22110. \[P] publish current project
  22111. \[X] publish... (project will be prompted for)
  22112. \[A] publish all projects")
  22113. (cmds
  22114. '((?t . org-insert-export-options-template)
  22115. (?v . org-export-visible)
  22116. (?a . org-export-as-ascii)
  22117. (?h . org-export-as-html)
  22118. (?b . org-export-as-html-and-open)
  22119. (?H . org-export-as-html-to-buffer)
  22120. (?R . org-export-region-as-html)
  22121. (?x . org-export-as-xoxo)
  22122. (?l . org-export-as-latex)
  22123. (?L . org-export-as-latex-to-buffer)
  22124. (?i . org-export-icalendar-this-file)
  22125. (?I . org-export-icalendar-all-agenda-files)
  22126. (?c . org-export-icalendar-combine-agenda-files)
  22127. (?F . org-publish-current-file)
  22128. (?P . org-publish-current-project)
  22129. (?X . org-publish)
  22130. (?A . org-publish-all)))
  22131. r1 r2 ass)
  22132. (save-window-excursion
  22133. (delete-other-windows)
  22134. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  22135. (princ help))
  22136. (message "Select command: ")
  22137. (setq r1 (read-char-exclusive)))
  22138. (setq r2 (if (< r1 27) (+ r1 96) r1))
  22139. (if (setq ass (assq r2 cmds))
  22140. (call-interactively (cdr ass))
  22141. (error "No command associated with key %c" r1))))
  22142. (defconst org-html-entities
  22143. '(("nbsp")
  22144. ("iexcl")
  22145. ("cent")
  22146. ("pound")
  22147. ("curren")
  22148. ("yen")
  22149. ("brvbar")
  22150. ("vert" . "&#124;")
  22151. ("sect")
  22152. ("uml")
  22153. ("copy")
  22154. ("ordf")
  22155. ("laquo")
  22156. ("not")
  22157. ("shy")
  22158. ("reg")
  22159. ("macr")
  22160. ("deg")
  22161. ("plusmn")
  22162. ("sup2")
  22163. ("sup3")
  22164. ("acute")
  22165. ("micro")
  22166. ("para")
  22167. ("middot")
  22168. ("odot"."o")
  22169. ("star"."*")
  22170. ("cedil")
  22171. ("sup1")
  22172. ("ordm")
  22173. ("raquo")
  22174. ("frac14")
  22175. ("frac12")
  22176. ("frac34")
  22177. ("iquest")
  22178. ("Agrave")
  22179. ("Aacute")
  22180. ("Acirc")
  22181. ("Atilde")
  22182. ("Auml")
  22183. ("Aring") ("AA"."&Aring;")
  22184. ("AElig")
  22185. ("Ccedil")
  22186. ("Egrave")
  22187. ("Eacute")
  22188. ("Ecirc")
  22189. ("Euml")
  22190. ("Igrave")
  22191. ("Iacute")
  22192. ("Icirc")
  22193. ("Iuml")
  22194. ("ETH")
  22195. ("Ntilde")
  22196. ("Ograve")
  22197. ("Oacute")
  22198. ("Ocirc")
  22199. ("Otilde")
  22200. ("Ouml")
  22201. ("times")
  22202. ("Oslash")
  22203. ("Ugrave")
  22204. ("Uacute")
  22205. ("Ucirc")
  22206. ("Uuml")
  22207. ("Yacute")
  22208. ("THORN")
  22209. ("szlig")
  22210. ("agrave")
  22211. ("aacute")
  22212. ("acirc")
  22213. ("atilde")
  22214. ("auml")
  22215. ("aring")
  22216. ("aelig")
  22217. ("ccedil")
  22218. ("egrave")
  22219. ("eacute")
  22220. ("ecirc")
  22221. ("euml")
  22222. ("igrave")
  22223. ("iacute")
  22224. ("icirc")
  22225. ("iuml")
  22226. ("eth")
  22227. ("ntilde")
  22228. ("ograve")
  22229. ("oacute")
  22230. ("ocirc")
  22231. ("otilde")
  22232. ("ouml")
  22233. ("divide")
  22234. ("oslash")
  22235. ("ugrave")
  22236. ("uacute")
  22237. ("ucirc")
  22238. ("uuml")
  22239. ("yacute")
  22240. ("thorn")
  22241. ("yuml")
  22242. ("fnof")
  22243. ("Alpha")
  22244. ("Beta")
  22245. ("Gamma")
  22246. ("Delta")
  22247. ("Epsilon")
  22248. ("Zeta")
  22249. ("Eta")
  22250. ("Theta")
  22251. ("Iota")
  22252. ("Kappa")
  22253. ("Lambda")
  22254. ("Mu")
  22255. ("Nu")
  22256. ("Xi")
  22257. ("Omicron")
  22258. ("Pi")
  22259. ("Rho")
  22260. ("Sigma")
  22261. ("Tau")
  22262. ("Upsilon")
  22263. ("Phi")
  22264. ("Chi")
  22265. ("Psi")
  22266. ("Omega")
  22267. ("alpha")
  22268. ("beta")
  22269. ("gamma")
  22270. ("delta")
  22271. ("epsilon")
  22272. ("varepsilon"."&epsilon;")
  22273. ("zeta")
  22274. ("eta")
  22275. ("theta")
  22276. ("iota")
  22277. ("kappa")
  22278. ("lambda")
  22279. ("mu")
  22280. ("nu")
  22281. ("xi")
  22282. ("omicron")
  22283. ("pi")
  22284. ("rho")
  22285. ("sigmaf") ("varsigma"."&sigmaf;")
  22286. ("sigma")
  22287. ("tau")
  22288. ("upsilon")
  22289. ("phi")
  22290. ("chi")
  22291. ("psi")
  22292. ("omega")
  22293. ("thetasym") ("vartheta"."&thetasym;")
  22294. ("upsih")
  22295. ("piv")
  22296. ("bull") ("bullet"."&bull;")
  22297. ("hellip") ("dots"."&hellip;")
  22298. ("prime")
  22299. ("Prime")
  22300. ("oline")
  22301. ("frasl")
  22302. ("weierp")
  22303. ("image")
  22304. ("real")
  22305. ("trade")
  22306. ("alefsym")
  22307. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  22308. ("uarr") ("uparrow"."&uarr;")
  22309. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  22310. ("darr")("downarrow"."&darr;")
  22311. ("harr") ("leftrightarrow"."&harr;")
  22312. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  22313. ("lArr") ("Leftarrow"."&lArr;")
  22314. ("uArr") ("Uparrow"."&uArr;")
  22315. ("rArr") ("Rightarrow"."&rArr;")
  22316. ("dArr") ("Downarrow"."&dArr;")
  22317. ("hArr") ("Leftrightarrow"."&hArr;")
  22318. ("forall")
  22319. ("part") ("partial"."&part;")
  22320. ("exist") ("exists"."&exist;")
  22321. ("empty") ("emptyset"."&empty;")
  22322. ("nabla")
  22323. ("isin") ("in"."&isin;")
  22324. ("notin")
  22325. ("ni")
  22326. ("prod")
  22327. ("sum")
  22328. ("minus")
  22329. ("lowast") ("ast"."&lowast;")
  22330. ("radic")
  22331. ("prop") ("proptp"."&prop;")
  22332. ("infin") ("infty"."&infin;")
  22333. ("ang") ("angle"."&ang;")
  22334. ("and") ("wedge"."&and;")
  22335. ("or") ("vee"."&or;")
  22336. ("cap")
  22337. ("cup")
  22338. ("int")
  22339. ("there4")
  22340. ("sim")
  22341. ("cong") ("simeq"."&cong;")
  22342. ("asymp")("approx"."&asymp;")
  22343. ("ne") ("neq"."&ne;")
  22344. ("equiv")
  22345. ("le")
  22346. ("ge")
  22347. ("sub") ("subset"."&sub;")
  22348. ("sup") ("supset"."&sup;")
  22349. ("nsub")
  22350. ("sube")
  22351. ("supe")
  22352. ("oplus")
  22353. ("otimes")
  22354. ("perp")
  22355. ("sdot") ("cdot"."&sdot;")
  22356. ("lceil")
  22357. ("rceil")
  22358. ("lfloor")
  22359. ("rfloor")
  22360. ("lang")
  22361. ("rang")
  22362. ("loz") ("Diamond"."&loz;")
  22363. ("spades") ("spadesuit"."&spades;")
  22364. ("clubs") ("clubsuit"."&clubs;")
  22365. ("hearts") ("diamondsuit"."&hearts;")
  22366. ("diams") ("diamondsuit"."&diams;")
  22367. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  22368. ("quot")
  22369. ("amp")
  22370. ("lt")
  22371. ("gt")
  22372. ("OElig")
  22373. ("oelig")
  22374. ("Scaron")
  22375. ("scaron")
  22376. ("Yuml")
  22377. ("circ")
  22378. ("tilde")
  22379. ("ensp")
  22380. ("emsp")
  22381. ("thinsp")
  22382. ("zwnj")
  22383. ("zwj")
  22384. ("lrm")
  22385. ("rlm")
  22386. ("ndash")
  22387. ("mdash")
  22388. ("lsquo")
  22389. ("rsquo")
  22390. ("sbquo")
  22391. ("ldquo")
  22392. ("rdquo")
  22393. ("bdquo")
  22394. ("dagger")
  22395. ("Dagger")
  22396. ("permil")
  22397. ("lsaquo")
  22398. ("rsaquo")
  22399. ("euro")
  22400. ("arccos"."arccos")
  22401. ("arcsin"."arcsin")
  22402. ("arctan"."arctan")
  22403. ("arg"."arg")
  22404. ("cos"."cos")
  22405. ("cosh"."cosh")
  22406. ("cot"."cot")
  22407. ("coth"."coth")
  22408. ("csc"."csc")
  22409. ("deg"."deg")
  22410. ("det"."det")
  22411. ("dim"."dim")
  22412. ("exp"."exp")
  22413. ("gcd"."gcd")
  22414. ("hom"."hom")
  22415. ("inf"."inf")
  22416. ("ker"."ker")
  22417. ("lg"."lg")
  22418. ("lim"."lim")
  22419. ("liminf"."liminf")
  22420. ("limsup"."limsup")
  22421. ("ln"."ln")
  22422. ("log"."log")
  22423. ("max"."max")
  22424. ("min"."min")
  22425. ("Pr"."Pr")
  22426. ("sec"."sec")
  22427. ("sin"."sin")
  22428. ("sinh"."sinh")
  22429. ("sup"."sup")
  22430. ("tan"."tan")
  22431. ("tanh"."tanh")
  22432. )
  22433. "Entities for TeX->HTML translation.
  22434. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22435. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22436. In that case, \"\\ent\" will be translated to \"&other;\".
  22437. The list contains HTML entities for Latin-1, Greek and other symbols.
  22438. It is supplemented by a number of commonly used TeX macros with appropriate
  22439. translations. There is currently no way for users to extend this.")
  22440. ;;; General functions for all backends
  22441. (defun org-cleaned-string-for-export (string &rest parameters)
  22442. "Cleanup a buffer STRING so that links can be created safely."
  22443. (interactive)
  22444. (let* ((re-radio (and org-target-link-regexp
  22445. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22446. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22447. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22448. (re-archive (concat ":" org-archive-tag ":"))
  22449. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22450. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22451. (htmlp (plist-get parameters :for-html))
  22452. (asciip (plist-get parameters :for-ascii))
  22453. (latexp (plist-get parameters :for-LaTeX))
  22454. (commentsp (plist-get parameters :comments))
  22455. (archived-trees (plist-get parameters :archived-trees))
  22456. (inhibit-read-only t)
  22457. (drawers org-drawers)
  22458. (exp-drawers (plist-get parameters :drawers))
  22459. (outline-regexp "\\*+ ")
  22460. a b xx
  22461. rtn p)
  22462. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22463. (erase-buffer)
  22464. (insert string)
  22465. ;; Remove license-to-kill stuff
  22466. (while (setq p (text-property-any (point-min) (point-max)
  22467. :org-license-to-kill t))
  22468. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22469. (let ((org-inhibit-startup t)) (org-mode))
  22470. (untabify (point-min) (point-max))
  22471. ;; Get rid of drawers
  22472. (unless (eq t exp-drawers)
  22473. (goto-char (point-min))
  22474. (let ((re (concat "^[ \t]*:\\("
  22475. (mapconcat
  22476. 'identity
  22477. (org-delete-all exp-drawers
  22478. (copy-sequence drawers))
  22479. "\\|")
  22480. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22481. (while (re-search-forward re nil t)
  22482. (replace-match ""))))
  22483. ;; Get the correct stuff before the first headline
  22484. (when (plist-get parameters :skip-before-1st-heading)
  22485. (goto-char (point-min))
  22486. (when (re-search-forward "^\\*+[ \t]" nil t)
  22487. (delete-region (point-min) (match-beginning 0))
  22488. (goto-char (point-min))
  22489. (insert "\n")))
  22490. (when (plist-get parameters :add-text)
  22491. (goto-char (point-min))
  22492. (insert (plist-get parameters :add-text) "\n"))
  22493. ;; Get rid of archived trees
  22494. (when (not (eq archived-trees t))
  22495. (goto-char (point-min))
  22496. (while (re-search-forward re-archive nil t)
  22497. (if (not (org-on-heading-p t))
  22498. (org-end-of-subtree t)
  22499. (beginning-of-line 1)
  22500. (setq a (if archived-trees
  22501. (1+ (point-at-eol)) (point))
  22502. b (org-end-of-subtree t))
  22503. (if (> b a) (delete-region a b)))))
  22504. ;; Find targets in comments and move them out of comments,
  22505. ;; but mark them as targets that should be invisible
  22506. (goto-char (point-min))
  22507. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22508. (replace-match "\\1(INVISIBLE)"))
  22509. ;; Protect backend specific stuff, throw away the others.
  22510. (let ((formatters
  22511. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22512. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22513. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22514. fmt)
  22515. (goto-char (point-min))
  22516. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22517. (goto-char (match-end 0))
  22518. (while (not (looking-at "#\\+END_EXAMPLE"))
  22519. (insert ": ")
  22520. (beginning-of-line 2)))
  22521. (goto-char (point-min))
  22522. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22523. (add-text-properties (match-beginning 0) (match-end 0)
  22524. '(org-protected t)))
  22525. (while formatters
  22526. (setq fmt (pop formatters))
  22527. (when (car fmt)
  22528. (goto-char (point-min))
  22529. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22530. ":[ \t]*\\(.*\\)") nil t)
  22531. (replace-match "\\1" t)
  22532. (add-text-properties
  22533. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22534. '(org-protected t))))
  22535. (goto-char (point-min))
  22536. (while (re-search-forward
  22537. (concat "^#\\+"
  22538. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22539. (cadddr fmt) "\\>.*\n?") nil t)
  22540. (if (car fmt)
  22541. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22542. '(org-protected t))
  22543. (delete-region (match-beginning 0) (match-end 0))))))
  22544. ;; Protect quoted subtrees
  22545. (goto-char (point-min))
  22546. (while (re-search-forward re-quote nil t)
  22547. (goto-char (match-beginning 0))
  22548. (end-of-line 1)
  22549. (add-text-properties (point) (org-end-of-subtree t)
  22550. '(org-protected t)))
  22551. ;; Protect verbatim elements
  22552. (goto-char (point-min))
  22553. (while (re-search-forward org-verbatim-re nil t)
  22554. (add-text-properties (match-beginning 4) (match-end 4)
  22555. '(org-protected t))
  22556. (goto-char (1+ (match-end 4))))
  22557. ;; Remove subtrees that are commented
  22558. (goto-char (point-min))
  22559. (while (re-search-forward re-commented nil t)
  22560. (goto-char (match-beginning 0))
  22561. (delete-region (point) (org-end-of-subtree t)))
  22562. ;; Remove special table lines
  22563. (when org-export-table-remove-special-lines
  22564. (goto-char (point-min))
  22565. (while (re-search-forward "^[ \t]*|" nil t)
  22566. (beginning-of-line 1)
  22567. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22568. (and (looking-at ".*?| *<[0-9]+> *|")
  22569. (not (looking-at ".*?| *[^ <|]"))))
  22570. (delete-region (max (point-min) (1- (point-at-bol)))
  22571. (point-at-eol))
  22572. (end-of-line 1))))
  22573. ;; Specific LaTeX stuff
  22574. (when latexp
  22575. (require 'org-export-latex nil)
  22576. (org-export-latex-cleaned-string))
  22577. (when asciip
  22578. (org-export-ascii-clean-string))
  22579. ;; Specific HTML stuff
  22580. (when htmlp
  22581. ;; Convert LaTeX fragments to images
  22582. (when (plist-get parameters :LaTeX-fragments)
  22583. (org-format-latex
  22584. (concat "ltxpng/" (file-name-sans-extension
  22585. (file-name-nondirectory
  22586. org-current-export-file)))
  22587. org-current-export-dir nil "Creating LaTeX image %s"))
  22588. (message "Exporting..."))
  22589. ;; Remove or replace comments
  22590. (goto-char (point-min))
  22591. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22592. (if commentsp
  22593. (progn (add-text-properties
  22594. (match-beginning 0) (match-end 0) '(org-protected t))
  22595. (replace-match (format commentsp (match-string 1)) t t))
  22596. (replace-match "")))
  22597. ;; Find matches for radio targets and turn them into internal links
  22598. (goto-char (point-min))
  22599. (when re-radio
  22600. (while (re-search-forward re-radio nil t)
  22601. (org-if-unprotected
  22602. (replace-match "\\1[[\\2]]"))))
  22603. ;; Find all links that contain a newline and put them into a single line
  22604. (goto-char (point-min))
  22605. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22606. (org-if-unprotected
  22607. (replace-match "\\1 \\3")
  22608. (goto-char (match-beginning 0))))
  22609. ;; Normalize links: Convert angle and plain links into bracket links
  22610. ;; Expand link abbreviations
  22611. (goto-char (point-min))
  22612. (while (re-search-forward re-plain-link nil t)
  22613. (goto-char (1- (match-end 0)))
  22614. (org-if-unprotected
  22615. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22616. ":" (match-string 3) "]]")))
  22617. ;; added 'org-link face to links
  22618. (put-text-property 0 (length s) 'face 'org-link s)
  22619. (replace-match s t t))))
  22620. (goto-char (point-min))
  22621. (while (re-search-forward re-angle-link nil t)
  22622. (goto-char (1- (match-end 0)))
  22623. (org-if-unprotected
  22624. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22625. ":" (match-string 3) "]]")))
  22626. (put-text-property 0 (length s) 'face 'org-link s)
  22627. (replace-match s t t))))
  22628. (goto-char (point-min))
  22629. (while (re-search-forward org-bracket-link-regexp nil t)
  22630. (org-if-unprotected
  22631. (let* ((s (concat "[[" (setq xx (save-match-data
  22632. (org-link-expand-abbrev (match-string 1))))
  22633. "]"
  22634. (if (match-end 3)
  22635. (match-string 2)
  22636. (concat "[" xx "]"))
  22637. "]")))
  22638. (put-text-property 0 (length s) 'face 'org-link s)
  22639. (replace-match s t t))))
  22640. ;; Find multiline emphasis and put them into single line
  22641. (when (plist-get parameters :emph-multiline)
  22642. (goto-char (point-min))
  22643. (while (re-search-forward org-emph-re nil t)
  22644. (if (not (= (char-after (match-beginning 3))
  22645. (char-after (match-beginning 4))))
  22646. (org-if-unprotected
  22647. (subst-char-in-region (match-beginning 0) (match-end 0)
  22648. ?\n ?\ t)
  22649. (goto-char (1- (match-end 0))))
  22650. (goto-char (1+ (match-beginning 0))))))
  22651. (setq rtn (buffer-string)))
  22652. (kill-buffer " org-mode-tmp")
  22653. rtn))
  22654. (defun org-export-grab-title-from-buffer ()
  22655. "Get a title for the current document, from looking at the buffer."
  22656. (let ((inhibit-read-only t))
  22657. (save-excursion
  22658. (goto-char (point-min))
  22659. (let ((end (save-excursion (outline-next-heading) (point))))
  22660. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22661. ;; Mark the line so that it will not be exported as normal text.
  22662. (org-unmodified
  22663. (add-text-properties (match-beginning 0) (match-end 0)
  22664. (list :org-license-to-kill t)))
  22665. ;; Return the title string
  22666. (org-trim (match-string 0)))))))
  22667. (defun org-export-get-title-from-subtree ()
  22668. "Return subtree title and exclude it from export."
  22669. (let (title (m (mark)))
  22670. (save-excursion
  22671. (goto-char (region-beginning))
  22672. (when (and (org-at-heading-p)
  22673. (>= (org-end-of-subtree t t) (region-end)))
  22674. ;; This is a subtree, we take the title from the first heading
  22675. (goto-char (region-beginning))
  22676. (looking-at org-todo-line-regexp)
  22677. (setq title (match-string 3))
  22678. (org-unmodified
  22679. (add-text-properties (point) (1+ (point-at-eol))
  22680. (list :org-license-to-kill t)))))
  22681. title))
  22682. (defun org-solidify-link-text (s &optional alist)
  22683. "Take link text and make a safe target out of it."
  22684. (save-match-data
  22685. (let* ((rtn
  22686. (mapconcat
  22687. 'identity
  22688. (org-split-string s "[ \t\r\n]+") "--"))
  22689. (a (assoc rtn alist)))
  22690. (or (cdr a) rtn))))
  22691. (defun org-get-min-level (lines)
  22692. "Get the minimum level in LINES."
  22693. (let ((re "^\\(\\*+\\) ") l min)
  22694. (catch 'exit
  22695. (while (setq l (pop lines))
  22696. (if (string-match re l)
  22697. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22698. 1)))
  22699. ;; Variable holding the vector with section numbers
  22700. (defvar org-section-numbers (make-vector org-level-max 0))
  22701. (defun org-init-section-numbers ()
  22702. "Initialize the vector for the section numbers."
  22703. (let* ((level -1)
  22704. (numbers (nreverse (org-split-string "" "\\.")))
  22705. (depth (1- (length org-section-numbers)))
  22706. (i depth) number-string)
  22707. (while (>= i 0)
  22708. (if (> i level)
  22709. (aset org-section-numbers i 0)
  22710. (setq number-string (or (car numbers) "0"))
  22711. (if (string-match "\\`[A-Z]\\'" number-string)
  22712. (aset org-section-numbers i
  22713. (- (string-to-char number-string) ?A -1))
  22714. (aset org-section-numbers i (string-to-number number-string)))
  22715. (pop numbers))
  22716. (setq i (1- i)))))
  22717. (defun org-section-number (&optional level)
  22718. "Return a string with the current section number.
  22719. When LEVEL is non-nil, increase section numbers on that level."
  22720. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22721. (when level
  22722. (when (> level -1)
  22723. (aset org-section-numbers
  22724. level (1+ (aref org-section-numbers level))))
  22725. (setq idx (1+ level))
  22726. (while (<= idx depth)
  22727. (if (not (= idx 1))
  22728. (aset org-section-numbers idx 0))
  22729. (setq idx (1+ idx))))
  22730. (setq idx 0)
  22731. (while (<= idx depth)
  22732. (setq n (aref org-section-numbers idx))
  22733. (setq string (concat string (if (not (string= string "")) "." "")
  22734. (int-to-string n)))
  22735. (setq idx (1+ idx)))
  22736. (save-match-data
  22737. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22738. (setq string (replace-match "" t nil string)))
  22739. (if (string-match "\\(\\.0\\)+\\'" string)
  22740. (setq string (replace-match "" t nil string))))
  22741. string))
  22742. ;;; ASCII export
  22743. (defvar org-last-level nil) ; dynamically scoped variable
  22744. (defvar org-min-level nil) ; dynamically scoped variable
  22745. (defvar org-levels-open nil) ; dynamically scoped parameter
  22746. (defvar org-ascii-current-indentation nil) ; For communication
  22747. (defun org-export-as-ascii (arg)
  22748. "Export the outline as a pretty ASCII file.
  22749. If there is an active region, export only the region.
  22750. The prefix ARG specifies how many levels of the outline should become
  22751. underlined headlines. The default is 3."
  22752. (interactive "P")
  22753. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22754. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22755. (org-infile-export-plist)))
  22756. (region-p (org-region-active-p))
  22757. (subtree-p
  22758. (when region-p
  22759. (save-excursion
  22760. (goto-char (region-beginning))
  22761. (and (org-at-heading-p)
  22762. (>= (org-end-of-subtree t t) (region-end))))))
  22763. (custom-times org-display-custom-times)
  22764. (org-ascii-current-indentation '(0 . 0))
  22765. (level 0) line txt
  22766. (umax nil)
  22767. (umax-toc nil)
  22768. (case-fold-search nil)
  22769. (filename (concat (file-name-as-directory
  22770. (org-export-directory :ascii opt-plist))
  22771. (file-name-sans-extension
  22772. (or (and subtree-p
  22773. (org-entry-get (region-beginning)
  22774. "EXPORT_FILE_NAME" t))
  22775. (file-name-nondirectory buffer-file-name)))
  22776. ".txt"))
  22777. (filename (if (equal (file-truename filename)
  22778. (file-truename buffer-file-name))
  22779. (concat filename ".txt")
  22780. filename))
  22781. (buffer (find-file-noselect filename))
  22782. (org-levels-open (make-vector org-level-max nil))
  22783. (odd org-odd-levels-only)
  22784. (date (plist-get opt-plist :date))
  22785. (author (plist-get opt-plist :author))
  22786. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22787. (plist-get opt-plist :title)
  22788. (and (not
  22789. (plist-get opt-plist :skip-before-1st-heading))
  22790. (org-export-grab-title-from-buffer))
  22791. (file-name-sans-extension
  22792. (file-name-nondirectory buffer-file-name))))
  22793. (email (plist-get opt-plist :email))
  22794. (language (plist-get opt-plist :language))
  22795. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22796. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22797. (todo nil)
  22798. (lang-words nil)
  22799. (region
  22800. (buffer-substring
  22801. (if (org-region-active-p) (region-beginning) (point-min))
  22802. (if (org-region-active-p) (region-end) (point-max))))
  22803. (lines (org-split-string
  22804. (org-cleaned-string-for-export
  22805. region
  22806. :for-ascii t
  22807. :skip-before-1st-heading
  22808. (plist-get opt-plist :skip-before-1st-heading)
  22809. :drawers (plist-get opt-plist :drawers)
  22810. :verbatim-multiline t
  22811. :archived-trees
  22812. (plist-get opt-plist :archived-trees)
  22813. :add-text (plist-get opt-plist :text))
  22814. "\n"))
  22815. thetoc have-headings first-heading-pos
  22816. table-open table-buffer)
  22817. (let ((inhibit-read-only t))
  22818. (org-unmodified
  22819. (remove-text-properties (point-min) (point-max)
  22820. '(:org-license-to-kill t))))
  22821. (setq org-min-level (org-get-min-level lines))
  22822. (setq org-last-level org-min-level)
  22823. (org-init-section-numbers)
  22824. (find-file-noselect filename)
  22825. (setq lang-words (or (assoc language org-export-language-setup)
  22826. (assoc "en" org-export-language-setup)))
  22827. (switch-to-buffer-other-window buffer)
  22828. (erase-buffer)
  22829. (fundamental-mode)
  22830. ;; create local variables for all options, to make sure all called
  22831. ;; functions get the correct information
  22832. (mapc (lambda (x)
  22833. (set (make-local-variable (cdr x))
  22834. (plist-get opt-plist (car x))))
  22835. org-export-plist-vars)
  22836. (org-set-local 'org-odd-levels-only odd)
  22837. (setq umax (if arg (prefix-numeric-value arg)
  22838. org-export-headline-levels))
  22839. (setq umax-toc (if (integerp org-export-with-toc)
  22840. (min org-export-with-toc umax)
  22841. umax))
  22842. ;; File header
  22843. (if title (org-insert-centered title ?=))
  22844. (insert "\n")
  22845. (if (and (or author email)
  22846. org-export-author-info)
  22847. (insert (concat (nth 1 lang-words) ": " (or author "")
  22848. (if email (concat " <" email ">") "")
  22849. "\n")))
  22850. (cond
  22851. ((and date (string-match "%" date))
  22852. (setq date (format-time-string date (current-time))))
  22853. (date)
  22854. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22855. (if (and date org-export-time-stamp-file)
  22856. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22857. (insert "\n\n")
  22858. (if org-export-with-toc
  22859. (progn
  22860. (push (concat (nth 3 lang-words) "\n") thetoc)
  22861. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22862. (mapc '(lambda (line)
  22863. (if (string-match org-todo-line-regexp
  22864. line)
  22865. ;; This is a headline
  22866. (progn
  22867. (setq have-headings t)
  22868. (setq level (- (match-end 1) (match-beginning 1))
  22869. level (org-tr-level level)
  22870. txt (match-string 3 line)
  22871. todo
  22872. (or (and org-export-mark-todo-in-toc
  22873. (match-beginning 2)
  22874. (not (member (match-string 2 line)
  22875. org-done-keywords)))
  22876. ; TODO, not DONE
  22877. (and org-export-mark-todo-in-toc
  22878. (= level umax-toc)
  22879. (org-search-todo-below
  22880. line lines level))))
  22881. (setq txt (org-html-expand-for-ascii txt))
  22882. (while (string-match org-bracket-link-regexp txt)
  22883. (setq txt
  22884. (replace-match
  22885. (match-string (if (match-end 2) 3 1) txt)
  22886. t t txt)))
  22887. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22888. (string-match
  22889. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22890. txt))
  22891. (setq txt (replace-match "" t t txt)))
  22892. (if (string-match quote-re0 txt)
  22893. (setq txt (replace-match "" t t txt)))
  22894. (if org-export-with-section-numbers
  22895. (setq txt (concat (org-section-number level)
  22896. " " txt)))
  22897. (if (<= level umax-toc)
  22898. (progn
  22899. (push
  22900. (concat
  22901. (make-string
  22902. (* (max 0 (- level org-min-level)) 4) ?\ )
  22903. (format (if todo "%s (*)\n" "%s\n") txt))
  22904. thetoc)
  22905. (setq org-last-level level))
  22906. ))))
  22907. lines)
  22908. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22909. (org-init-section-numbers)
  22910. (while (setq line (pop lines))
  22911. ;; Remove the quoted HTML tags.
  22912. (setq line (org-html-expand-for-ascii line))
  22913. ;; Remove targets
  22914. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22915. (setq line (replace-match "" t t line)))
  22916. ;; Replace internal links
  22917. (while (string-match org-bracket-link-regexp line)
  22918. (setq line (replace-match
  22919. (if (match-end 3) "[\\3]" "[\\1]")
  22920. t nil line)))
  22921. (when custom-times
  22922. (setq line (org-translate-time line)))
  22923. (cond
  22924. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22925. ;; a Headline
  22926. (setq first-heading-pos (or first-heading-pos (point)))
  22927. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22928. txt (match-string 2 line))
  22929. (org-ascii-level-start level txt umax lines))
  22930. ((and org-export-with-tables
  22931. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22932. (if (not table-open)
  22933. ;; New table starts
  22934. (setq table-open t table-buffer nil))
  22935. ;; Accumulate lines
  22936. (setq table-buffer (cons line table-buffer))
  22937. (when (or (not lines)
  22938. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22939. (car lines))))
  22940. (setq table-open nil
  22941. table-buffer (nreverse table-buffer))
  22942. (insert (mapconcat
  22943. (lambda (x)
  22944. (org-fix-indentation x org-ascii-current-indentation))
  22945. (org-format-table-ascii table-buffer)
  22946. "\n") "\n")))
  22947. (t
  22948. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22949. (if (and org-export-with-fixed-width
  22950. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22951. (setq line (replace-match "\\1" nil nil line)))
  22952. (insert line "\n"))))
  22953. (normal-mode)
  22954. ;; insert the table of contents
  22955. (when thetoc
  22956. (goto-char (point-min))
  22957. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22958. (progn
  22959. (goto-char (match-beginning 0))
  22960. (replace-match ""))
  22961. (goto-char first-heading-pos))
  22962. (mapc 'insert thetoc)
  22963. (or (looking-at "[ \t]*\n[ \t]*\n")
  22964. (insert "\n\n")))
  22965. ;; Convert whitespace place holders
  22966. (goto-char (point-min))
  22967. (let (beg end)
  22968. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22969. (setq end (next-single-property-change beg 'org-whitespace))
  22970. (goto-char beg)
  22971. (delete-region beg end)
  22972. (insert (make-string (- end beg) ?\ ))))
  22973. (save-buffer)
  22974. ;; remove display and invisible chars
  22975. (let (beg end)
  22976. (goto-char (point-min))
  22977. (while (setq beg (next-single-property-change (point) 'display))
  22978. (setq end (next-single-property-change beg 'display))
  22979. (delete-region beg end)
  22980. (goto-char beg)
  22981. (insert "=>"))
  22982. (goto-char (point-min))
  22983. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22984. (setq end (next-single-property-change beg 'org-cwidth))
  22985. (delete-region beg end)
  22986. (goto-char beg)))
  22987. (goto-char (point-min))))
  22988. (defun org-export-ascii-clean-string ()
  22989. "Do extra work for ASCII export"
  22990. (goto-char (point-min))
  22991. (while (re-search-forward org-verbatim-re nil t)
  22992. (goto-char (match-end 2))
  22993. (backward-delete-char 1) (insert "'")
  22994. (goto-char (match-beginning 2))
  22995. (delete-char 1) (insert "`")
  22996. (goto-char (match-end 2))))
  22997. (defun org-search-todo-below (line lines level)
  22998. "Search the subtree below LINE for any TODO entries."
  22999. (let ((rest (cdr (memq line lines)))
  23000. (re org-todo-line-regexp)
  23001. line lv todo)
  23002. (catch 'exit
  23003. (while (setq line (pop rest))
  23004. (if (string-match re line)
  23005. (progn
  23006. (setq lv (- (match-end 1) (match-beginning 1))
  23007. todo (and (match-beginning 2)
  23008. (not (member (match-string 2 line)
  23009. org-done-keywords))))
  23010. ; TODO, not DONE
  23011. (if (<= lv level) (throw 'exit nil))
  23012. (if todo (throw 'exit t))))))))
  23013. (defun org-html-expand-for-ascii (line)
  23014. "Handle quoted HTML for ASCII export."
  23015. (if org-export-html-expand
  23016. (while (string-match "@<[^<>\n]*>" line)
  23017. ;; We just remove the tags for now.
  23018. (setq line (replace-match "" nil nil line))))
  23019. line)
  23020. (defun org-insert-centered (s &optional underline)
  23021. "Insert the string S centered and underline it with character UNDERLINE."
  23022. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  23023. (insert (make-string ind ?\ ) s "\n")
  23024. (if underline
  23025. (insert (make-string ind ?\ )
  23026. (make-string (string-width s) underline)
  23027. "\n"))))
  23028. (defun org-ascii-level-start (level title umax &optional lines)
  23029. "Insert a new level in ASCII export."
  23030. (let (char (n (- level umax 1)) (ind 0))
  23031. (if (> level umax)
  23032. (progn
  23033. (insert (make-string (* 2 n) ?\ )
  23034. (char-to-string (nth (% n (length org-export-ascii-bullets))
  23035. org-export-ascii-bullets))
  23036. " " title "\n")
  23037. ;; find the indentation of the next non-empty line
  23038. (catch 'stop
  23039. (while lines
  23040. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  23041. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  23042. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  23043. (pop lines)))
  23044. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  23045. (if (or (not (equal (char-before) ?\n))
  23046. (not (equal (char-before (1- (point))) ?\n)))
  23047. (insert "\n"))
  23048. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  23049. (unless org-export-with-tags
  23050. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  23051. (setq title (replace-match "" t t title))))
  23052. (if org-export-with-section-numbers
  23053. (setq title (concat (org-section-number level) " " title)))
  23054. (insert title "\n" (make-string (string-width title) char) "\n")
  23055. (setq org-ascii-current-indentation '(0 . 0)))))
  23056. (defun org-export-visible (type arg)
  23057. "Create a copy of the visible part of the current buffer, and export it.
  23058. The copy is created in a temporary buffer and removed after use.
  23059. TYPE is the final key (as a string) that also select the export command in
  23060. the `C-c C-e' export dispatcher.
  23061. As a special case, if the you type SPC at the prompt, the temporary
  23062. org-mode file will not be removed but presented to you so that you can
  23063. continue to use it. The prefix arg ARG is passed through to the exporting
  23064. command."
  23065. (interactive
  23066. (list (progn
  23067. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  23068. (read-char-exclusive))
  23069. current-prefix-arg))
  23070. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  23071. (error "Invalid export key"))
  23072. (let* ((binding (cdr (assoc type
  23073. '((?a . org-export-as-ascii)
  23074. (?\C-a . org-export-as-ascii)
  23075. (?b . org-export-as-html-and-open)
  23076. (?\C-b . org-export-as-html-and-open)
  23077. (?h . org-export-as-html)
  23078. (?H . org-export-as-html-to-buffer)
  23079. (?R . org-export-region-as-html)
  23080. (?x . org-export-as-xoxo)))))
  23081. (keepp (equal type ?\ ))
  23082. (file buffer-file-name)
  23083. (buffer (get-buffer-create "*Org Export Visible*"))
  23084. s e)
  23085. ;; Need to hack the drawers here.
  23086. (save-excursion
  23087. (goto-char (point-min))
  23088. (while (re-search-forward org-drawer-regexp nil t)
  23089. (goto-char (match-beginning 1))
  23090. (or (org-invisible-p) (org-flag-drawer nil))))
  23091. (with-current-buffer buffer (erase-buffer))
  23092. (save-excursion
  23093. (setq s (goto-char (point-min)))
  23094. (while (not (= (point) (point-max)))
  23095. (goto-char (org-find-invisible))
  23096. (append-to-buffer buffer s (point))
  23097. (setq s (goto-char (org-find-visible))))
  23098. (org-cycle-hide-drawers 'all)
  23099. (goto-char (point-min))
  23100. (unless keepp
  23101. ;; Copy all comment lines to the end, to make sure #+ settings are
  23102. ;; still available for the second export step. Kind of a hack, but
  23103. ;; does do the trick.
  23104. (if (looking-at "#[^\r\n]*")
  23105. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  23106. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  23107. (append-to-buffer buffer (1+ (match-beginning 0))
  23108. (min (point-max) (1+ (match-end 0))))))
  23109. (set-buffer buffer)
  23110. (let ((buffer-file-name file)
  23111. (org-inhibit-startup t))
  23112. (org-mode)
  23113. (show-all)
  23114. (unless keepp (funcall binding arg))))
  23115. (if (not keepp)
  23116. (kill-buffer buffer)
  23117. (switch-to-buffer-other-window buffer)
  23118. (goto-char (point-min)))))
  23119. (defun org-find-visible ()
  23120. (let ((s (point)))
  23121. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23122. (get-char-property s 'invisible)))
  23123. s))
  23124. (defun org-find-invisible ()
  23125. (let ((s (point)))
  23126. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23127. (not (get-char-property s 'invisible))))
  23128. s))
  23129. ;;; HTML export
  23130. (defun org-get-current-options ()
  23131. "Return a string with current options as keyword options.
  23132. Does include HTML export options as well as TODO and CATEGORY stuff."
  23133. (format
  23134. "#+TITLE: %s
  23135. #+AUTHOR: %s
  23136. #+EMAIL: %s
  23137. #+LANGUAGE: %s
  23138. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  23139. #+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
  23140. #+CATEGORY: %s
  23141. #+SEQ_TODO: %s
  23142. #+TYP_TODO: %s
  23143. #+PRIORITIES: %c %c %c
  23144. #+DRAWERS: %s
  23145. #+STARTUP: %s %s %s %s %s
  23146. #+TAGS: %s
  23147. #+ARCHIVE: %s
  23148. #+LINK: %s
  23149. "
  23150. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  23151. org-export-headline-levels
  23152. org-export-with-section-numbers
  23153. org-export-with-toc
  23154. org-export-preserve-breaks
  23155. org-export-html-expand
  23156. org-export-with-fixed-width
  23157. org-export-with-tables
  23158. org-export-with-sub-superscripts
  23159. org-export-with-special-strings
  23160. org-export-with-footnotes
  23161. org-export-with-emphasize
  23162. org-export-with-TeX-macros
  23163. org-export-with-LaTeX-fragments
  23164. org-export-skip-text-before-1st-heading
  23165. org-export-with-drawers
  23166. org-export-with-tags
  23167. (file-name-nondirectory buffer-file-name)
  23168. "TODO FEEDBACK VERIFY DONE"
  23169. "Me Jason Marie DONE"
  23170. org-highest-priority org-lowest-priority org-default-priority
  23171. (mapconcat 'identity org-drawers " ")
  23172. (cdr (assoc org-startup-folded
  23173. '((nil . "showall") (t . "overview") (content . "content"))))
  23174. (if org-odd-levels-only "odd" "oddeven")
  23175. (if org-hide-leading-stars "hidestars" "showstars")
  23176. (if org-startup-align-all-tables "align" "noalign")
  23177. (cond ((eq org-log-done t) "logdone")
  23178. ((equal org-log-done 'note) "lognotedone")
  23179. ((not org-log-done) "nologdone"))
  23180. (or (mapconcat (lambda (x)
  23181. (cond
  23182. ((equal '(:startgroup) x) "{")
  23183. ((equal '(:endgroup) x) "}")
  23184. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  23185. (t (car x))))
  23186. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  23187. org-archive-location
  23188. "org file:~/org/%s.org"
  23189. ))
  23190. (defun org-insert-export-options-template ()
  23191. "Insert into the buffer a template with information for exporting."
  23192. (interactive)
  23193. (if (not (bolp)) (newline))
  23194. (let ((s (org-get-current-options)))
  23195. (and (string-match "#\\+CATEGORY" s)
  23196. (setq s (substring s 0 (match-beginning 0))))
  23197. (insert s)))
  23198. (defun org-toggle-fixed-width-section (arg)
  23199. "Toggle the fixed-width export.
  23200. If there is no active region, the QUOTE keyword at the current headline is
  23201. inserted or removed. When present, it causes the text between this headline
  23202. and the next to be exported as fixed-width text, and unmodified.
  23203. If there is an active region, this command adds or removes a colon as the
  23204. first character of this line. If the first character of a line is a colon,
  23205. this line is also exported in fixed-width font."
  23206. (interactive "P")
  23207. (let* ((cc 0)
  23208. (regionp (org-region-active-p))
  23209. (beg (if regionp (region-beginning) (point)))
  23210. (end (if regionp (region-end)))
  23211. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  23212. (case-fold-search nil)
  23213. (re "[ \t]*\\(:\\)")
  23214. off)
  23215. (if regionp
  23216. (save-excursion
  23217. (goto-char beg)
  23218. (setq cc (current-column))
  23219. (beginning-of-line 1)
  23220. (setq off (looking-at re))
  23221. (while (> nlines 0)
  23222. (setq nlines (1- nlines))
  23223. (beginning-of-line 1)
  23224. (cond
  23225. (arg
  23226. (move-to-column cc t)
  23227. (insert ":\n")
  23228. (forward-line -1))
  23229. ((and off (looking-at re))
  23230. (replace-match "" t t nil 1))
  23231. ((not off) (move-to-column cc t) (insert ":")))
  23232. (forward-line 1)))
  23233. (save-excursion
  23234. (org-back-to-heading)
  23235. (if (looking-at (concat outline-regexp
  23236. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  23237. (replace-match "" t t nil 1)
  23238. (if (looking-at outline-regexp)
  23239. (progn
  23240. (goto-char (match-end 0))
  23241. (insert org-quote-string " "))))))))
  23242. (defun org-export-as-html-and-open (arg)
  23243. "Export the outline as HTML and immediately open it with a browser.
  23244. If there is an active region, export only the region.
  23245. The prefix ARG specifies how many levels of the outline should become
  23246. headlines. The default is 3. Lower levels will become bulleted lists."
  23247. (interactive "P")
  23248. (org-export-as-html arg 'hidden)
  23249. (org-open-file buffer-file-name))
  23250. (defun org-export-as-html-batch ()
  23251. "Call `org-export-as-html', may be used in batch processing as
  23252. emacs --batch
  23253. --load=$HOME/lib/emacs/org.el
  23254. --eval \"(setq org-export-headline-levels 2)\"
  23255. --visit=MyFile --funcall org-export-as-html-batch"
  23256. (org-export-as-html org-export-headline-levels 'hidden))
  23257. (defun org-export-as-html-to-buffer (arg)
  23258. "Call `org-exort-as-html` with output to a temporary buffer.
  23259. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  23260. (interactive "P")
  23261. (org-export-as-html arg nil nil "*Org HTML Export*")
  23262. (switch-to-buffer-other-window "*Org HTML Export*"))
  23263. (defun org-replace-region-by-html (beg end)
  23264. "Assume the current region has org-mode syntax, and convert it to HTML.
  23265. This can be used in any buffer. For example, you could write an
  23266. itemized list in org-mode syntax in an HTML buffer and then use this
  23267. command to convert it."
  23268. (interactive "r")
  23269. (let (reg html buf pop-up-frames)
  23270. (save-window-excursion
  23271. (if (org-mode-p)
  23272. (setq html (org-export-region-as-html
  23273. beg end t 'string))
  23274. (setq reg (buffer-substring beg end)
  23275. buf (get-buffer-create "*Org tmp*"))
  23276. (with-current-buffer buf
  23277. (erase-buffer)
  23278. (insert reg)
  23279. (org-mode)
  23280. (setq html (org-export-region-as-html
  23281. (point-min) (point-max) t 'string)))
  23282. (kill-buffer buf)))
  23283. (delete-region beg end)
  23284. (insert html)))
  23285. (defun org-export-region-as-html (beg end &optional body-only buffer)
  23286. "Convert region from BEG to END in org-mode buffer to HTML.
  23287. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  23288. contents, and only produce the region of converted text, useful for
  23289. cut-and-paste operations.
  23290. If BUFFER is a buffer or a string, use/create that buffer as a target
  23291. of the converted HTML. If BUFFER is the symbol `string', return the
  23292. produced HTML as a string and leave not buffer behind. For example,
  23293. a Lisp program could call this function in the following way:
  23294. (setq html (org-export-region-as-html beg end t 'string))
  23295. When called interactively, the output buffer is selected, and shown
  23296. in a window. A non-interactive call will only retunr the buffer."
  23297. (interactive "r\nP")
  23298. (when (interactive-p)
  23299. (setq buffer "*Org HTML Export*"))
  23300. (let ((transient-mark-mode t) (zmacs-regions t)
  23301. rtn)
  23302. (goto-char end)
  23303. (set-mark (point)) ;; to activate the region
  23304. (goto-char beg)
  23305. (setq rtn (org-export-as-html
  23306. nil nil nil
  23307. buffer body-only))
  23308. (if (fboundp 'deactivate-mark) (deactivate-mark))
  23309. (if (and (interactive-p) (bufferp rtn))
  23310. (switch-to-buffer-other-window rtn)
  23311. rtn)))
  23312. (defvar html-table-tag nil) ; dynamically scoped into this.
  23313. (defun org-export-as-html (arg &optional hidden ext-plist
  23314. to-buffer body-only)
  23315. "Export the outline as a pretty HTML file.
  23316. If there is an active region, export only the region. The prefix
  23317. ARG specifies how many levels of the outline should become
  23318. headlines. The default is 3. Lower levels will become bulleted
  23319. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  23320. EXT-PLIST is a property list with external parameters overriding
  23321. org-mode's default settings, but still inferior to file-local
  23322. settings. When TO-BUFFER is non-nil, create a buffer with that
  23323. name and export to that buffer. If TO-BUFFER is the symbol `string',
  23324. don't leave any buffer behind but just return the resulting HTML as
  23325. a string. When BODY-ONLY is set, don't produce the file header and footer,
  23326. simply return the content of <body>...</body>, without even
  23327. the body tags themselves."
  23328. (interactive "P")
  23329. ;; Make sure we have a file name when we need it.
  23330. (when (and (not (or to-buffer body-only))
  23331. (not buffer-file-name))
  23332. (if (buffer-base-buffer)
  23333. (org-set-local 'buffer-file-name
  23334. (with-current-buffer (buffer-base-buffer)
  23335. buffer-file-name))
  23336. (error "Need a file name to be able to export.")))
  23337. (message "Exporting...")
  23338. (setq-default org-todo-line-regexp org-todo-line-regexp)
  23339. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  23340. (setq-default org-done-keywords org-done-keywords)
  23341. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  23342. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23343. ext-plist
  23344. (org-infile-export-plist)))
  23345. (style (plist-get opt-plist :style))
  23346. (html-extension (plist-get opt-plist :html-extension))
  23347. (link-validate (plist-get opt-plist :link-validation-function))
  23348. valid thetoc have-headings first-heading-pos
  23349. (odd org-odd-levels-only)
  23350. (region-p (org-region-active-p))
  23351. (subtree-p
  23352. (when region-p
  23353. (save-excursion
  23354. (goto-char (region-beginning))
  23355. (and (org-at-heading-p)
  23356. (>= (org-end-of-subtree t t) (region-end))))))
  23357. ;; The following two are dynamically scoped into other
  23358. ;; routines below.
  23359. (org-current-export-dir (org-export-directory :html opt-plist))
  23360. (org-current-export-file buffer-file-name)
  23361. (level 0) (line "") (origline "") txt todo
  23362. (umax nil)
  23363. (umax-toc nil)
  23364. (filename (if to-buffer nil
  23365. (expand-file-name
  23366. (concat
  23367. (file-name-sans-extension
  23368. (or (and subtree-p
  23369. (org-entry-get (region-beginning)
  23370. "EXPORT_FILE_NAME" t))
  23371. (file-name-nondirectory buffer-file-name)))
  23372. "." html-extension)
  23373. (file-name-as-directory
  23374. (org-export-directory :html opt-plist)))))
  23375. (current-dir (if buffer-file-name
  23376. (file-name-directory buffer-file-name)
  23377. default-directory))
  23378. (buffer (if to-buffer
  23379. (cond
  23380. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  23381. (t (get-buffer-create to-buffer)))
  23382. (find-file-noselect filename)))
  23383. (org-levels-open (make-vector org-level-max nil))
  23384. (date (plist-get opt-plist :date))
  23385. (author (plist-get opt-plist :author))
  23386. (title (or (and subtree-p (org-export-get-title-from-subtree))
  23387. (plist-get opt-plist :title)
  23388. (and (not
  23389. (plist-get opt-plist :skip-before-1st-heading))
  23390. (org-export-grab-title-from-buffer))
  23391. (and buffer-file-name
  23392. (file-name-sans-extension
  23393. (file-name-nondirectory buffer-file-name)))
  23394. "UNTITLED"))
  23395. (html-table-tag (plist-get opt-plist :html-table-tag))
  23396. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  23397. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  23398. (inquote nil)
  23399. (infixed nil)
  23400. (in-local-list nil)
  23401. (local-list-num nil)
  23402. (local-list-indent nil)
  23403. (llt org-plain-list-ordered-item-terminator)
  23404. (email (plist-get opt-plist :email))
  23405. (language (plist-get opt-plist :language))
  23406. (lang-words nil)
  23407. (target-alist nil) tg
  23408. (head-count 0) cnt
  23409. (start 0)
  23410. (coding-system (and (boundp 'buffer-file-coding-system)
  23411. buffer-file-coding-system))
  23412. (coding-system-for-write (or org-export-html-coding-system
  23413. coding-system))
  23414. (save-buffer-coding-system (or org-export-html-coding-system
  23415. coding-system))
  23416. (charset (and coding-system-for-write
  23417. (fboundp 'coding-system-get)
  23418. (coding-system-get coding-system-for-write
  23419. 'mime-charset)))
  23420. (region
  23421. (buffer-substring
  23422. (if region-p (region-beginning) (point-min))
  23423. (if region-p (region-end) (point-max))))
  23424. (lines
  23425. (org-split-string
  23426. (org-cleaned-string-for-export
  23427. region
  23428. :emph-multiline t
  23429. :for-html t
  23430. :skip-before-1st-heading
  23431. (plist-get opt-plist :skip-before-1st-heading)
  23432. :drawers (plist-get opt-plist :drawers)
  23433. :archived-trees
  23434. (plist-get opt-plist :archived-trees)
  23435. :add-text
  23436. (plist-get opt-plist :text)
  23437. :LaTeX-fragments
  23438. (plist-get opt-plist :LaTeX-fragments))
  23439. "[\r\n]"))
  23440. table-open type
  23441. table-buffer table-orig-buffer
  23442. ind start-is-num starter didclose
  23443. rpl path desc descp desc1 desc2 link
  23444. )
  23445. (let ((inhibit-read-only t))
  23446. (org-unmodified
  23447. (remove-text-properties (point-min) (point-max)
  23448. '(:org-license-to-kill t))))
  23449. (message "Exporting...")
  23450. (setq org-min-level (org-get-min-level lines))
  23451. (setq org-last-level org-min-level)
  23452. (org-init-section-numbers)
  23453. (cond
  23454. ((and date (string-match "%" date))
  23455. (setq date (format-time-string date (current-time))))
  23456. (date)
  23457. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23458. ;; Get the language-dependent settings
  23459. (setq lang-words (or (assoc language org-export-language-setup)
  23460. (assoc "en" org-export-language-setup)))
  23461. ;; Switch to the output buffer
  23462. (set-buffer buffer)
  23463. (let ((inhibit-read-only t)) (erase-buffer))
  23464. (fundamental-mode)
  23465. (and (fboundp 'set-buffer-file-coding-system)
  23466. (set-buffer-file-coding-system coding-system-for-write))
  23467. (let ((case-fold-search nil)
  23468. (org-odd-levels-only odd))
  23469. ;; create local variables for all options, to make sure all called
  23470. ;; functions get the correct information
  23471. (mapc (lambda (x)
  23472. (set (make-local-variable (cdr x))
  23473. (plist-get opt-plist (car x))))
  23474. org-export-plist-vars)
  23475. (setq umax (if arg (prefix-numeric-value arg)
  23476. org-export-headline-levels))
  23477. (setq umax-toc (if (integerp org-export-with-toc)
  23478. (min org-export-with-toc umax)
  23479. umax))
  23480. (unless body-only
  23481. ;; File header
  23482. (insert (format
  23483. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23484. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23485. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23486. lang=\"%s\" xml:lang=\"%s\">
  23487. <head>
  23488. <title>%s</title>
  23489. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23490. <meta name=\"generator\" content=\"Org-mode\"/>
  23491. <meta name=\"generated\" content=\"%s\"/>
  23492. <meta name=\"author\" content=\"%s\"/>
  23493. %s
  23494. </head><body>
  23495. "
  23496. language language (org-html-expand title)
  23497. (or charset "iso-8859-1") date author style))
  23498. (insert (or (plist-get opt-plist :preamble) ""))
  23499. (when (plist-get opt-plist :auto-preamble)
  23500. (if title (insert (format org-export-html-title-format
  23501. (org-html-expand title))))))
  23502. (if (and org-export-with-toc (not body-only))
  23503. (progn
  23504. (push (format "<h%d>%s</h%d>\n"
  23505. org-export-html-toplevel-hlevel
  23506. (nth 3 lang-words)
  23507. org-export-html-toplevel-hlevel)
  23508. thetoc)
  23509. (push "<ul>\n<li>" thetoc)
  23510. (setq lines
  23511. (mapcar '(lambda (line)
  23512. (if (string-match org-todo-line-regexp line)
  23513. ;; This is a headline
  23514. (progn
  23515. (setq have-headings t)
  23516. (setq level (- (match-end 1) (match-beginning 1))
  23517. level (org-tr-level level)
  23518. txt (save-match-data
  23519. (org-html-expand
  23520. (org-export-cleanup-toc-line
  23521. (match-string 3 line))))
  23522. todo
  23523. (or (and org-export-mark-todo-in-toc
  23524. (match-beginning 2)
  23525. (not (member (match-string 2 line)
  23526. org-done-keywords)))
  23527. ; TODO, not DONE
  23528. (and org-export-mark-todo-in-toc
  23529. (= level umax-toc)
  23530. (org-search-todo-below
  23531. line lines level))))
  23532. (if (string-match
  23533. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23534. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23535. (if (string-match quote-re0 txt)
  23536. (setq txt (replace-match "" t t txt)))
  23537. (if org-export-with-section-numbers
  23538. (setq txt (concat (org-section-number level)
  23539. " " txt)))
  23540. (if (<= level (max umax umax-toc))
  23541. (setq head-count (+ head-count 1)))
  23542. (if (<= level umax-toc)
  23543. (progn
  23544. (if (> level org-last-level)
  23545. (progn
  23546. (setq cnt (- level org-last-level))
  23547. (while (>= (setq cnt (1- cnt)) 0)
  23548. (push "\n<ul>\n<li>" thetoc))
  23549. (push "\n" thetoc)))
  23550. (if (< level org-last-level)
  23551. (progn
  23552. (setq cnt (- org-last-level level))
  23553. (while (>= (setq cnt (1- cnt)) 0)
  23554. (push "</li>\n</ul>" thetoc))
  23555. (push "\n" thetoc)))
  23556. ;; Check for targets
  23557. (while (string-match org-target-regexp line)
  23558. (setq tg (match-string 1 line)
  23559. line (replace-match
  23560. (concat "@<span class=\"target\">" tg "@</span> ")
  23561. t t line))
  23562. (push (cons (org-solidify-link-text tg)
  23563. (format "sec-%d" head-count))
  23564. target-alist))
  23565. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23566. (setq txt (replace-match "" t t txt)))
  23567. (push
  23568. (format
  23569. (if todo
  23570. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23571. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23572. head-count txt) thetoc)
  23573. (setq org-last-level level))
  23574. )))
  23575. line)
  23576. lines))
  23577. (while (> org-last-level (1- org-min-level))
  23578. (setq org-last-level (1- org-last-level))
  23579. (push "</li>\n</ul>\n" thetoc))
  23580. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23581. (setq head-count 0)
  23582. (org-init-section-numbers)
  23583. (while (setq line (pop lines) origline line)
  23584. (catch 'nextline
  23585. ;; end of quote section?
  23586. (when (and inquote (string-match "^\\*+ " line))
  23587. (insert "</pre>\n")
  23588. (setq inquote nil))
  23589. ;; inside a quote section?
  23590. (when inquote
  23591. (insert (org-html-protect line) "\n")
  23592. (throw 'nextline nil))
  23593. ;; verbatim lines
  23594. (when (and org-export-with-fixed-width
  23595. (string-match "^[ \t]*:\\(.*\\)" line))
  23596. (when (not infixed)
  23597. (setq infixed t)
  23598. (insert "<pre>\n"))
  23599. (insert (org-html-protect (match-string 1 line)) "\n")
  23600. (when (and lines
  23601. (not (string-match "^[ \t]*\\(:.*\\)"
  23602. (car lines))))
  23603. (setq infixed nil)
  23604. (insert "</pre>\n"))
  23605. (throw 'nextline nil))
  23606. ;; Protected HTML
  23607. (when (get-text-property 0 'org-protected line)
  23608. (let (par)
  23609. (when (re-search-backward
  23610. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23611. (setq par (match-string 1))
  23612. (replace-match "\\2\n"))
  23613. (insert line "\n")
  23614. (while (and lines
  23615. (or (= (length (car lines)) 0)
  23616. (get-text-property 0 'org-protected (car lines))))
  23617. (insert (pop lines) "\n"))
  23618. (and par (insert "<p>\n")))
  23619. (throw 'nextline nil))
  23620. ;; Horizontal line
  23621. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23622. (insert "\n<hr/>\n")
  23623. (throw 'nextline nil))
  23624. ;; make targets to anchors
  23625. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23626. (cond
  23627. ((match-end 2)
  23628. (setq line (replace-match
  23629. (concat "@<a name=\""
  23630. (org-solidify-link-text (match-string 1 line))
  23631. "\">\\nbsp@</a>")
  23632. t t line)))
  23633. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23634. (setq line (replace-match
  23635. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23636. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23637. t t line)))
  23638. (t
  23639. (setq line (replace-match
  23640. (concat "@<a name=\""
  23641. (org-solidify-link-text (match-string 1 line))
  23642. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23643. t t line)))))
  23644. (setq line (org-html-handle-time-stamps line))
  23645. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23646. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23647. ;; Also handle sub_superscripts and checkboxes
  23648. (or (string-match org-table-hline-regexp line)
  23649. (setq line (org-html-expand line)))
  23650. ;; Format the links
  23651. (setq start 0)
  23652. (while (string-match org-bracket-link-analytic-regexp line start)
  23653. (setq start (match-beginning 0))
  23654. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23655. (setq path (match-string 3 line))
  23656. (setq desc1 (if (match-end 5) (match-string 5 line))
  23657. desc2 (if (match-end 2) (concat type ":" path) path)
  23658. descp (and desc1 (not (equal desc1 desc2)))
  23659. desc (or desc1 desc2))
  23660. ;; Make an image out of the description if that is so wanted
  23661. (when (and descp (org-file-image-p desc))
  23662. (save-match-data
  23663. (if (string-match "^file:" desc)
  23664. (setq desc (substring desc (match-end 0)))))
  23665. (setq desc (concat "<img src=\"" desc "\"/>")))
  23666. ;; FIXME: do we need to unescape here somewhere?
  23667. (cond
  23668. ((equal type "internal")
  23669. (setq rpl
  23670. (concat
  23671. "<a href=\"#"
  23672. (org-solidify-link-text
  23673. (save-match-data (org-link-unescape path)) target-alist)
  23674. "\">" desc "</a>")))
  23675. ((member type '("http" "https"))
  23676. ;; standard URL, just check if we need to inline an image
  23677. (if (and (or (eq t org-export-html-inline-images)
  23678. (and org-export-html-inline-images (not descp)))
  23679. (org-file-image-p path))
  23680. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23681. (setq link (concat type ":" path))
  23682. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23683. ((member type '("ftp" "mailto" "news"))
  23684. ;; standard URL
  23685. (setq link (concat type ":" path))
  23686. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23687. ((string= type "file")
  23688. ;; FILE link
  23689. (let* ((filename path)
  23690. (abs-p (file-name-absolute-p filename))
  23691. thefile file-is-image-p search)
  23692. (save-match-data
  23693. (if (string-match "::\\(.*\\)" filename)
  23694. (setq search (match-string 1 filename)
  23695. filename (replace-match "" t nil filename)))
  23696. (setq valid
  23697. (if (functionp link-validate)
  23698. (funcall link-validate filename current-dir)
  23699. t))
  23700. (setq file-is-image-p (org-file-image-p filename))
  23701. (setq thefile (if abs-p (expand-file-name filename) filename))
  23702. (when (and org-export-html-link-org-files-as-html
  23703. (string-match "\\.org$" thefile))
  23704. (setq thefile (concat (substring thefile 0
  23705. (match-beginning 0))
  23706. "." html-extension))
  23707. (if (and search
  23708. ;; make sure this is can be used as target search
  23709. (not (string-match "^[0-9]*$" search))
  23710. (not (string-match "^\\*" search))
  23711. (not (string-match "^/.*/$" search)))
  23712. (setq thefile (concat thefile "#"
  23713. (org-solidify-link-text
  23714. (org-link-unescape search)))))
  23715. (when (string-match "^file:" desc)
  23716. (setq desc (replace-match "" t t desc))
  23717. (if (string-match "\\.org$" desc)
  23718. (setq desc (replace-match "" t t desc))))))
  23719. (setq rpl (if (and file-is-image-p
  23720. (or (eq t org-export-html-inline-images)
  23721. (and org-export-html-inline-images
  23722. (not descp))))
  23723. (concat "<img src=\"" thefile "\"/>")
  23724. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23725. (if (not valid) (setq rpl desc))))
  23726. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23727. (setq rpl (concat "<i>&lt;" type ":"
  23728. (save-match-data (org-link-unescape path))
  23729. "&gt;</i>"))))
  23730. (setq line (replace-match rpl t t line)
  23731. start (+ start (length rpl))))
  23732. ;; TODO items
  23733. (if (and (string-match org-todo-line-regexp line)
  23734. (match-beginning 2))
  23735. (setq line
  23736. (concat (substring line 0 (match-beginning 2))
  23737. "<span class=\""
  23738. (if (member (match-string 2 line)
  23739. org-done-keywords)
  23740. "done" "todo")
  23741. "\">" (match-string 2 line)
  23742. "</span>" (substring line (match-end 2)))))
  23743. ;; Does this contain a reference to a footnote?
  23744. (when org-export-with-footnotes
  23745. (setq start 0)
  23746. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23747. (if (get-text-property (match-beginning 2) 'org-protected line)
  23748. (setq start (match-end 2))
  23749. (let ((n (match-string 2 line)))
  23750. (setq line
  23751. (replace-match
  23752. (format
  23753. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23754. (match-string 1 line) n n n)
  23755. t t line))))))
  23756. (cond
  23757. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23758. ;; This is a headline
  23759. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23760. txt (match-string 2 line))
  23761. (if (string-match quote-re0 txt)
  23762. (setq txt (replace-match "" t t txt)))
  23763. (if (<= level (max umax umax-toc))
  23764. (setq head-count (+ head-count 1)))
  23765. (when in-local-list
  23766. ;; Close any local lists before inserting a new header line
  23767. (while local-list-num
  23768. (org-close-li)
  23769. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23770. (pop local-list-num))
  23771. (setq local-list-indent nil
  23772. in-local-list nil))
  23773. (setq first-heading-pos (or first-heading-pos (point)))
  23774. (org-html-level-start level txt umax
  23775. (and org-export-with-toc (<= level umax))
  23776. head-count)
  23777. ;; QUOTES
  23778. (when (string-match quote-re line)
  23779. (insert "<pre>")
  23780. (setq inquote t)))
  23781. ((and org-export-with-tables
  23782. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23783. (if (not table-open)
  23784. ;; New table starts
  23785. (setq table-open t table-buffer nil table-orig-buffer nil))
  23786. ;; Accumulate lines
  23787. (setq table-buffer (cons line table-buffer)
  23788. table-orig-buffer (cons origline table-orig-buffer))
  23789. (when (or (not lines)
  23790. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23791. (car lines))))
  23792. (setq table-open nil
  23793. table-buffer (nreverse table-buffer)
  23794. table-orig-buffer (nreverse table-orig-buffer))
  23795. (org-close-par-maybe)
  23796. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23797. (t
  23798. ;; Normal lines
  23799. (when (string-match
  23800. (cond
  23801. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23802. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23803. ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23804. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23805. line)
  23806. (setq ind (org-get-string-indentation line)
  23807. start-is-num (match-beginning 4)
  23808. starter (if (match-beginning 2)
  23809. (substring (match-string 2 line) 0 -1))
  23810. line (substring line (match-beginning 5)))
  23811. (unless (string-match "[^ \t]" line)
  23812. ;; empty line. Pretend indentation is large.
  23813. (setq ind (if org-empty-line-terminates-plain-lists
  23814. 0
  23815. (1+ (or (car local-list-indent) 1)))))
  23816. (setq didclose nil)
  23817. (while (and in-local-list
  23818. (or (and (= ind (car local-list-indent))
  23819. (not starter))
  23820. (< ind (car local-list-indent))))
  23821. (setq didclose t)
  23822. (org-close-li)
  23823. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23824. (pop local-list-num) (pop local-list-indent)
  23825. (setq in-local-list local-list-indent))
  23826. (cond
  23827. ((and starter
  23828. (or (not in-local-list)
  23829. (> ind (car local-list-indent))))
  23830. ;; Start new (level of) list
  23831. (org-close-par-maybe)
  23832. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23833. (push start-is-num local-list-num)
  23834. (push ind local-list-indent)
  23835. (setq in-local-list t))
  23836. (starter
  23837. ;; continue current list
  23838. (org-close-li)
  23839. (insert "<li>\n"))
  23840. (didclose
  23841. ;; we did close a list, normal text follows: need <p>
  23842. (org-open-par)))
  23843. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23844. (setq line
  23845. (replace-match
  23846. (if (equal (match-string 1 line) "X")
  23847. "<b>[X]</b>"
  23848. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23849. t t line))))
  23850. ;; Empty lines start a new paragraph. If hand-formatted lists
  23851. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23852. ;; also start a new paragraph.
  23853. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23854. ;; Is this the start of a footnote?
  23855. (when org-export-with-footnotes
  23856. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23857. (org-close-par-maybe)
  23858. (let ((n (match-string 1 line)))
  23859. (setq line (replace-match
  23860. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23861. ;; Check if the line break needs to be conserved
  23862. (cond
  23863. ((string-match "\\\\\\\\[ \t]*$" line)
  23864. (setq line (replace-match "<br/>" t t line)))
  23865. (org-export-preserve-breaks
  23866. (setq line (concat line "<br/>"))))
  23867. (insert line "\n")))))
  23868. ;; Properly close all local lists and other lists
  23869. (when inquote (insert "</pre>\n"))
  23870. (when in-local-list
  23871. ;; Close any local lists before inserting a new header line
  23872. (while local-list-num
  23873. (org-close-li)
  23874. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23875. (pop local-list-num))
  23876. (setq local-list-indent nil
  23877. in-local-list nil))
  23878. (org-html-level-start 1 nil umax
  23879. (and org-export-with-toc (<= level umax))
  23880. head-count)
  23881. (unless body-only
  23882. (when (plist-get opt-plist :auto-postamble)
  23883. (insert "<div id=\"postamble\">")
  23884. (when (and org-export-author-info author)
  23885. (insert "<p class=\"author\"> "
  23886. (nth 1 lang-words) ": " author "\n")
  23887. (when email
  23888. (if (listp (split-string email ",+ *"))
  23889. (mapc (lambda(e)
  23890. (insert "<a href=\"mailto:" e "\">&lt;"
  23891. e "&gt;</a>\n"))
  23892. (split-string email ",+ *"))
  23893. (insert "<a href=\"mailto:" email "\">&lt;"
  23894. email "&gt;</a>\n")))
  23895. (insert "</p>\n"))
  23896. (when (and date org-export-time-stamp-file)
  23897. (insert "<p class=\"date\"> "
  23898. (nth 2 lang-words) ": "
  23899. date "</p>\n"))
  23900. (insert "</div>"))
  23901. (if org-export-html-with-timestamp
  23902. (insert org-export-html-html-helper-timestamp))
  23903. (insert (or (plist-get opt-plist :postamble) ""))
  23904. (insert "</body>\n</html>\n"))
  23905. (normal-mode)
  23906. (if (eq major-mode default-major-mode) (html-mode))
  23907. ;; insert the table of contents
  23908. (goto-char (point-min))
  23909. (when thetoc
  23910. (if (or (re-search-forward
  23911. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23912. (re-search-forward
  23913. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23914. (progn
  23915. (goto-char (match-beginning 0))
  23916. (replace-match ""))
  23917. (goto-char first-heading-pos)
  23918. (when (looking-at "\\s-*</p>")
  23919. (goto-char (match-end 0))
  23920. (insert "\n")))
  23921. (insert "<div id=\"table-of-contents\">\n")
  23922. (mapc 'insert thetoc)
  23923. (insert "</div>\n"))
  23924. ;; remove empty paragraphs and lists
  23925. (goto-char (point-min))
  23926. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23927. (replace-match ""))
  23928. (goto-char (point-min))
  23929. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23930. (replace-match ""))
  23931. (goto-char (point-min))
  23932. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  23933. (replace-match ""))
  23934. ;; Convert whitespace place holders
  23935. (goto-char (point-min))
  23936. (let (beg end n)
  23937. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23938. (setq n (get-text-property beg 'org-whitespace)
  23939. end (next-single-property-change beg 'org-whitespace))
  23940. (goto-char beg)
  23941. (delete-region beg end)
  23942. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23943. (make-string n ?x)))))
  23944. (or to-buffer (save-buffer))
  23945. (goto-char (point-min))
  23946. (message "Exporting... done")
  23947. (if (eq to-buffer 'string)
  23948. (prog1 (buffer-substring (point-min) (point-max))
  23949. (kill-buffer (current-buffer)))
  23950. (current-buffer)))))
  23951. (defvar org-table-colgroup-info nil)
  23952. (defun org-format-table-ascii (lines)
  23953. "Format a table for ascii export."
  23954. (if (stringp lines)
  23955. (setq lines (org-split-string lines "\n")))
  23956. (if (not (string-match "^[ \t]*|" (car lines)))
  23957. ;; Table made by table.el - test for spanning
  23958. lines
  23959. ;; A normal org table
  23960. ;; Get rid of hlines at beginning and end
  23961. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23962. (setq lines (nreverse lines))
  23963. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23964. (setq lines (nreverse lines))
  23965. (when org-export-table-remove-special-lines
  23966. ;; Check if the table has a marking column. If yes remove the
  23967. ;; column and the special lines
  23968. (setq lines (org-table-clean-before-export lines)))
  23969. ;; Get rid of the vertical lines except for grouping
  23970. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23971. rtn line vl1 start)
  23972. (while (setq line (pop lines))
  23973. (if (string-match org-table-hline-regexp line)
  23974. (and (string-match "|\\(.*\\)|" line)
  23975. (setq line (replace-match " \\1" t nil line)))
  23976. (setq start 0 vl1 vl)
  23977. (while (string-match "|" line start)
  23978. (setq start (match-end 0))
  23979. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23980. (push line rtn))
  23981. (nreverse rtn))))
  23982. (defun org-colgroup-info-to-vline-list (info)
  23983. (let (vl new last)
  23984. (while info
  23985. (setq last new new (pop info))
  23986. (if (or (memq last '(:end :startend))
  23987. (memq new '(:start :startend)))
  23988. (push t vl)
  23989. (push nil vl)))
  23990. (setq vl (nreverse vl))
  23991. (and vl (setcar vl nil))
  23992. vl))
  23993. (defun org-format-table-html (lines olines)
  23994. "Find out which HTML converter to use and return the HTML code."
  23995. (if (stringp lines)
  23996. (setq lines (org-split-string lines "\n")))
  23997. (if (string-match "^[ \t]*|" (car lines))
  23998. ;; A normal org table
  23999. (org-format-org-table-html lines)
  24000. ;; Table made by table.el - test for spanning
  24001. (let* ((hlines (delq nil (mapcar
  24002. (lambda (x)
  24003. (if (string-match "^[ \t]*\\+-" x) x
  24004. nil))
  24005. lines)))
  24006. (first (car hlines))
  24007. (ll (and (string-match "\\S-+" first)
  24008. (match-string 0 first)))
  24009. (re (concat "^[ \t]*" (regexp-quote ll)))
  24010. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  24011. hlines))))
  24012. (if (and (not spanning)
  24013. (not org-export-prefer-native-exporter-for-tables))
  24014. ;; We can use my own converter with HTML conversions
  24015. (org-format-table-table-html lines)
  24016. ;; Need to use the code generator in table.el, with the original text.
  24017. (org-format-table-table-html-using-table-generate-source olines)))))
  24018. (defun org-format-org-table-html (lines &optional splice)
  24019. "Format a table into HTML."
  24020. ;; Get rid of hlines at beginning and end
  24021. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24022. (setq lines (nreverse lines))
  24023. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24024. (setq lines (nreverse lines))
  24025. (when org-export-table-remove-special-lines
  24026. ;; Check if the table has a marking column. If yes remove the
  24027. ;; column and the special lines
  24028. (setq lines (org-table-clean-before-export lines)))
  24029. (let ((head (and org-export-highlight-first-table-line
  24030. (delq nil (mapcar
  24031. (lambda (x) (string-match "^[ \t]*|-" x))
  24032. (cdr lines)))))
  24033. (nlines 0) fnum i
  24034. tbopen line fields html gr colgropen)
  24035. (if splice (setq head nil))
  24036. (unless splice (push (if head "<thead>" "<tbody>") html))
  24037. (setq tbopen t)
  24038. (while (setq line (pop lines))
  24039. (catch 'next-line
  24040. (if (string-match "^[ \t]*|-" line)
  24041. (progn
  24042. (unless splice
  24043. (push (if head "</thead>" "</tbody>") html)
  24044. (if lines (push "<tbody>" html) (setq tbopen nil)))
  24045. (setq head nil) ;; head ends here, first time around
  24046. ;; ignore this line
  24047. (throw 'next-line t)))
  24048. ;; Break the line into fields
  24049. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24050. (unless fnum (setq fnum (make-vector (length fields) 0)))
  24051. (setq nlines (1+ nlines) i -1)
  24052. (push (concat "<tr>"
  24053. (mapconcat
  24054. (lambda (x)
  24055. (setq i (1+ i))
  24056. (if (and (< i nlines)
  24057. (string-match org-table-number-regexp x))
  24058. (incf (aref fnum i)))
  24059. (if head
  24060. (concat (car org-export-table-header-tags) x
  24061. (cdr org-export-table-header-tags))
  24062. (concat (car org-export-table-data-tags) x
  24063. (cdr org-export-table-data-tags))))
  24064. fields "")
  24065. "</tr>")
  24066. html)))
  24067. (unless splice (if tbopen (push "</tbody>" html)))
  24068. (unless splice (push "</table>\n" html))
  24069. (setq html (nreverse html))
  24070. (unless splice
  24071. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  24072. (push (mapconcat
  24073. (lambda (x)
  24074. (setq gr (pop org-table-colgroup-info))
  24075. (format "%s<col align=\"%s\"></col>%s"
  24076. (if (memq gr '(:start :startend))
  24077. (prog1
  24078. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  24079. (setq colgropen t))
  24080. "")
  24081. (if (> (/ (float x) nlines) org-table-number-fraction)
  24082. "right" "left")
  24083. (if (memq gr '(:end :startend))
  24084. (progn (setq colgropen nil) "</colgroup>")
  24085. "")))
  24086. fnum "")
  24087. html)
  24088. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  24089. (push html-table-tag html))
  24090. (concat (mapconcat 'identity html "\n") "\n")))
  24091. (defun org-table-clean-before-export (lines)
  24092. "Check if the table has a marking column.
  24093. If yes remove the column and the special lines."
  24094. (setq org-table-colgroup-info nil)
  24095. (if (memq nil
  24096. (mapcar
  24097. (lambda (x) (or (string-match "^[ \t]*|-" x)
  24098. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  24099. lines))
  24100. (progn
  24101. (setq org-table-clean-did-remove-column nil)
  24102. (delq nil
  24103. (mapcar
  24104. (lambda (x)
  24105. (cond
  24106. ((string-match "^[ \t]*| */ *|" x)
  24107. (setq org-table-colgroup-info
  24108. (mapcar (lambda (x)
  24109. (cond ((member x '("<" "&lt;")) :start)
  24110. ((member x '(">" "&gt;")) :end)
  24111. ((member x '("<>" "&lt;&gt;")) :startend)
  24112. (t nil)))
  24113. (org-split-string x "[ \t]*|[ \t]*")))
  24114. nil)
  24115. (t x)))
  24116. lines)))
  24117. (setq org-table-clean-did-remove-column t)
  24118. (delq nil
  24119. (mapcar
  24120. (lambda (x)
  24121. (cond
  24122. ((string-match "^[ \t]*| */ *|" x)
  24123. (setq org-table-colgroup-info
  24124. (mapcar (lambda (x)
  24125. (cond ((member x '("<" "&lt;")) :start)
  24126. ((member x '(">" "&gt;")) :end)
  24127. ((member x '("<>" "&lt;&gt;")) :startend)
  24128. (t nil)))
  24129. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  24130. nil)
  24131. ((string-match "^[ \t]*| *[!_^/] *|" x)
  24132. nil) ; ignore this line
  24133. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  24134. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  24135. ;; remove the first column
  24136. (replace-match "\\1|" t nil x))))
  24137. lines))))
  24138. (defun org-format-table-table-html (lines)
  24139. "Format a table generated by table.el into HTML.
  24140. This conversion does *not* use `table-generate-source' from table.el.
  24141. This has the advantage that Org-mode's HTML conversions can be used.
  24142. But it has the disadvantage, that no cell- or row-spanning is allowed."
  24143. (let (line field-buffer
  24144. (head org-export-highlight-first-table-line)
  24145. fields html empty)
  24146. (setq html (concat html-table-tag "\n"))
  24147. (while (setq line (pop lines))
  24148. (setq empty "&nbsp;")
  24149. (catch 'next-line
  24150. (if (string-match "^[ \t]*\\+-" line)
  24151. (progn
  24152. (if field-buffer
  24153. (progn
  24154. (setq
  24155. html
  24156. (concat
  24157. html
  24158. "<tr>"
  24159. (mapconcat
  24160. (lambda (x)
  24161. (if (equal x "") (setq x empty))
  24162. (if head
  24163. (concat (car org-export-table-header-tags) x
  24164. (cdr org-export-table-header-tags))
  24165. (concat (car org-export-table-data-tags) x
  24166. (cdr org-export-table-data-tags))))
  24167. field-buffer "\n")
  24168. "</tr>\n"))
  24169. (setq head nil)
  24170. (setq field-buffer nil)))
  24171. ;; Ignore this line
  24172. (throw 'next-line t)))
  24173. ;; Break the line into fields and store the fields
  24174. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24175. (if field-buffer
  24176. (setq field-buffer (mapcar
  24177. (lambda (x)
  24178. (concat x "<br/>" (pop fields)))
  24179. field-buffer))
  24180. (setq field-buffer fields))))
  24181. (setq html (concat html "</table>\n"))
  24182. html))
  24183. (defun org-format-table-table-html-using-table-generate-source (lines)
  24184. "Format a table into html, using `table-generate-source' from table.el.
  24185. This has the advantage that cell- or row-spanning is allowed.
  24186. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  24187. (require 'table)
  24188. (with-current-buffer (get-buffer-create " org-tmp1 ")
  24189. (erase-buffer)
  24190. (insert (mapconcat 'identity lines "\n"))
  24191. (goto-char (point-min))
  24192. (if (not (re-search-forward "|[^+]" nil t))
  24193. (error "Error processing table"))
  24194. (table-recognize-table)
  24195. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  24196. (table-generate-source 'html " org-tmp2 ")
  24197. (set-buffer " org-tmp2 ")
  24198. (buffer-substring (point-min) (point-max))))
  24199. (defun org-html-handle-time-stamps (s)
  24200. "Format time stamps in string S, or remove them."
  24201. (catch 'exit
  24202. (let (r b)
  24203. (while (string-match org-maybe-keyword-time-regexp s)
  24204. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  24205. ;; never export CLOCK
  24206. (throw 'exit ""))
  24207. (or b (setq b (substring s 0 (match-beginning 0))))
  24208. (if (not org-export-with-timestamps)
  24209. (setq r (concat r (substring s 0 (match-beginning 0)))
  24210. s (substring s (match-end 0)))
  24211. (setq r (concat
  24212. r (substring s 0 (match-beginning 0))
  24213. (if (match-end 1)
  24214. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  24215. (match-string 1 s)))
  24216. (format " @<span class=\"timestamp\">%s@</span>"
  24217. (substring
  24218. (org-translate-time (match-string 3 s)) 1 -1)))
  24219. s (substring s (match-end 0)))))
  24220. ;; Line break if line started and ended with time stamp stuff
  24221. (if (not r)
  24222. s
  24223. (setq r (concat r s))
  24224. (unless (string-match "\\S-" (concat b s))
  24225. (setq r (concat r "@<br/>")))
  24226. r))))
  24227. (defun org-html-protect (s)
  24228. ;; convert & to &amp;, < to &lt; and > to &gt;
  24229. (let ((start 0))
  24230. (while (string-match "&" s start)
  24231. (setq s (replace-match "&amp;" t t s)
  24232. start (1+ (match-beginning 0))))
  24233. (while (string-match "<" s)
  24234. (setq s (replace-match "&lt;" t t s)))
  24235. (while (string-match ">" s)
  24236. (setq s (replace-match "&gt;" t t s))))
  24237. s)
  24238. (defun org-export-cleanup-toc-line (s)
  24239. "Remove tags and time staps from lines going into the toc."
  24240. (when (memq org-export-with-tags '(not-in-toc nil))
  24241. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  24242. (setq s (replace-match "" t t s))))
  24243. (when org-export-remove-timestamps-from-toc
  24244. (while (string-match org-maybe-keyword-time-regexp s)
  24245. (setq s (replace-match "" t t s))))
  24246. (while (string-match org-bracket-link-regexp s)
  24247. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  24248. t t s)))
  24249. s)
  24250. (defun org-html-expand (string)
  24251. "Prepare STRING for HTML export. Applies all active conversions.
  24252. If there are links in the string, don't modify these."
  24253. (let* ((re (concat org-bracket-link-regexp "\\|"
  24254. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  24255. m s l res)
  24256. (while (setq m (string-match re string))
  24257. (setq s (substring string 0 m)
  24258. l (match-string 0 string)
  24259. string (substring string (match-end 0)))
  24260. (push (org-html-do-expand s) res)
  24261. (push l res))
  24262. (push (org-html-do-expand string) res)
  24263. (apply 'concat (nreverse res))))
  24264. (defun org-html-do-expand (s)
  24265. "Apply all active conversions to translate special ASCII to HTML."
  24266. (setq s (org-html-protect s))
  24267. (if org-export-html-expand
  24268. (let ((start 0))
  24269. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  24270. (setq s (replace-match "<\\1>" t nil s)))))
  24271. (if org-export-with-emphasize
  24272. (setq s (org-export-html-convert-emphasize s)))
  24273. (if org-export-with-special-strings
  24274. (setq s (org-export-html-convert-special-strings s)))
  24275. (if org-export-with-sub-superscripts
  24276. (setq s (org-export-html-convert-sub-super s)))
  24277. (if org-export-with-TeX-macros
  24278. (let ((start 0) wd ass)
  24279. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  24280. (if (get-text-property (match-beginning 0) 'org-protected s)
  24281. (setq start (match-end 0))
  24282. (setq wd (match-string 1 s))
  24283. (if (setq ass (assoc wd org-html-entities))
  24284. (setq s (replace-match (or (cdr ass)
  24285. (concat "&" (car ass) ";"))
  24286. t t s))
  24287. (setq start (+ start (length wd))))))))
  24288. s)
  24289. (defun org-create-multibrace-regexp (left right n)
  24290. "Create a regular expression which will match a balanced sexp.
  24291. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  24292. as single character strings.
  24293. The regexp returned will match the entire expression including the
  24294. delimiters. It will also define a single group which contains the
  24295. match except for the outermost delimiters. The maximum depth of
  24296. stacked delimiters is N. Escaping delimiters is not possible."
  24297. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  24298. (or "\\|")
  24299. (re nothing)
  24300. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  24301. (while (> n 1)
  24302. (setq n (1- n)
  24303. re (concat re or next)
  24304. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  24305. (concat left "\\(" re "\\)" right)))
  24306. (defvar org-match-substring-regexp
  24307. (concat
  24308. "\\([^\\]\\)\\([_^]\\)\\("
  24309. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24310. "\\|"
  24311. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  24312. "\\|"
  24313. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  24314. "The regular expression matching a sub- or superscript.")
  24315. (defvar org-match-substring-with-braces-regexp
  24316. (concat
  24317. "\\([^\\]\\)\\([_^]\\)\\("
  24318. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24319. "\\)")
  24320. "The regular expression matching a sub- or superscript, forcing braces.")
  24321. (defconst org-export-html-special-string-regexps
  24322. '(("\\\\-" . "&shy;")
  24323. ("---\\([^-]\\)" . "&mdash;\\1")
  24324. ("--\\([^-]\\)" . "&ndash;\\1")
  24325. ("\\.\\.\\." . "&hellip;"))
  24326. "Regular expressions for special string conversion.")
  24327. (defun org-export-html-convert-special-strings (string)
  24328. "Convert special characters in STRING to HTML."
  24329. (let ((all org-export-html-special-string-regexps)
  24330. e a re rpl start)
  24331. (while (setq a (pop all))
  24332. (setq re (car a) rpl (cdr a) start 0)
  24333. (while (string-match re string start)
  24334. (if (get-text-property (match-beginning 0) 'org-protected string)
  24335. (setq start (match-end 0))
  24336. (setq string (replace-match rpl t nil string)))))
  24337. string))
  24338. (defun org-export-html-convert-sub-super (string)
  24339. "Convert sub- and superscripts in STRING to HTML."
  24340. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  24341. (while (string-match org-match-substring-regexp string s)
  24342. (cond
  24343. ((and requireb (match-end 8)) (setq s (match-end 2)))
  24344. ((get-text-property (match-beginning 2) 'org-protected string)
  24345. (setq s (match-end 2)))
  24346. (t
  24347. (setq s (match-end 1)
  24348. key (if (string= (match-string 2 string) "_") "sub" "sup")
  24349. c (or (match-string 8 string)
  24350. (match-string 6 string)
  24351. (match-string 5 string))
  24352. string (replace-match
  24353. (concat (match-string 1 string)
  24354. "<" key ">" c "</" key ">")
  24355. t t string)))))
  24356. (while (string-match "\\\\\\([_^]\\)" string)
  24357. (setq string (replace-match (match-string 1 string) t t string)))
  24358. string))
  24359. (defun org-export-html-convert-emphasize (string)
  24360. "Apply emphasis."
  24361. (let ((s 0) rpl)
  24362. (while (string-match org-emph-re string s)
  24363. (if (not (equal
  24364. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  24365. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  24366. (setq s (match-beginning 0)
  24367. rpl
  24368. (concat
  24369. (match-string 1 string)
  24370. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  24371. (match-string 4 string)
  24372. (nth 3 (assoc (match-string 3 string)
  24373. org-emphasis-alist))
  24374. (match-string 5 string))
  24375. string (replace-match rpl t t string)
  24376. s (+ s (- (length rpl) 2)))
  24377. (setq s (1+ s))))
  24378. string))
  24379. (defvar org-par-open nil)
  24380. (defun org-open-par ()
  24381. "Insert <p>, but first close previous paragraph if any."
  24382. (org-close-par-maybe)
  24383. (insert "\n<p>")
  24384. (setq org-par-open t))
  24385. (defun org-close-par-maybe ()
  24386. "Close paragraph if there is one open."
  24387. (when org-par-open
  24388. (insert "</p>")
  24389. (setq org-par-open nil)))
  24390. (defun org-close-li ()
  24391. "Close <li> if necessary."
  24392. (org-close-par-maybe)
  24393. (insert "</li>\n"))
  24394. (defvar body-only) ; dynamically scoped into this.
  24395. (defun org-html-level-start (level title umax with-toc head-count)
  24396. "Insert a new level in HTML export.
  24397. When TITLE is nil, just close all open levels."
  24398. (org-close-par-maybe)
  24399. (let ((l org-level-max))
  24400. (while (>= l level)
  24401. (if (aref org-levels-open (1- l))
  24402. (progn
  24403. (org-html-level-close l umax)
  24404. (aset org-levels-open (1- l) nil)))
  24405. (setq l (1- l)))
  24406. (when title
  24407. ;; If title is nil, this means this function is called to close
  24408. ;; all levels, so the rest is done only if title is given
  24409. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24410. (setq title (replace-match
  24411. (if org-export-with-tags
  24412. (save-match-data
  24413. (concat
  24414. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24415. (mapconcat 'identity (org-split-string
  24416. (match-string 1 title) ":")
  24417. "&nbsp;")
  24418. "</span>"))
  24419. "")
  24420. t t title)))
  24421. (if (> level umax)
  24422. (progn
  24423. (if (aref org-levels-open (1- level))
  24424. (progn
  24425. (org-close-li)
  24426. (insert "<li>" title "<br/>\n"))
  24427. (aset org-levels-open (1- level) t)
  24428. (org-close-par-maybe)
  24429. (insert "<ul>\n<li>" title "<br/>\n")))
  24430. (aset org-levels-open (1- level) t)
  24431. (if (and org-export-with-section-numbers (not body-only))
  24432. (setq title (concat (org-section-number level) " " title)))
  24433. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24434. (if with-toc
  24435. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  24436. level level head-count title level))
  24437. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  24438. (org-open-par)))))
  24439. (defun org-html-level-close (level max-outline-level)
  24440. "Terminate one level in HTML export."
  24441. (if (<= level max-outline-level)
  24442. (insert "</div>\n")
  24443. (org-close-li)
  24444. (insert "</ul>\n")))
  24445. ;;; iCalendar export
  24446. ;;;###autoload
  24447. (defun org-export-icalendar-this-file ()
  24448. "Export current file as an iCalendar file.
  24449. The iCalendar file will be located in the same directory as the Org-mode
  24450. file, but with extension `.ics'."
  24451. (interactive)
  24452. (org-export-icalendar nil buffer-file-name))
  24453. ;;;###autoload
  24454. (defun org-export-icalendar-all-agenda-files ()
  24455. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24456. Each iCalendar file will be located in the same directory as the Org-mode
  24457. file, but with extension `.ics'."
  24458. (interactive)
  24459. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24460. ;;;###autoload
  24461. (defun org-export-icalendar-combine-agenda-files ()
  24462. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24463. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24464. (interactive)
  24465. (apply 'org-export-icalendar t (org-agenda-files t)))
  24466. (defun org-export-icalendar (combine &rest files)
  24467. "Create iCalendar files for all elements of FILES.
  24468. If COMBINE is non-nil, combine all calendar entries into a single large
  24469. file and store it under the name `org-combined-agenda-icalendar-file'."
  24470. (save-excursion
  24471. (org-prepare-agenda-buffers files)
  24472. (let* ((dir (org-export-directory
  24473. :ical (list :publishing-directory
  24474. org-export-publishing-directory)))
  24475. file ical-file ical-buffer category started org-agenda-new-buffers)
  24476. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24477. (when combine
  24478. (setq ical-file
  24479. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24480. org-combined-agenda-icalendar-file
  24481. (expand-file-name org-combined-agenda-icalendar-file dir))
  24482. ical-buffer (org-get-agenda-file-buffer ical-file))
  24483. (set-buffer ical-buffer) (erase-buffer))
  24484. (while (setq file (pop files))
  24485. (catch 'nextfile
  24486. (org-check-agenda-file file)
  24487. (set-buffer (org-get-agenda-file-buffer file))
  24488. (unless combine
  24489. (setq ical-file (concat (file-name-as-directory dir)
  24490. (file-name-sans-extension
  24491. (file-name-nondirectory buffer-file-name))
  24492. ".ics"))
  24493. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24494. (with-current-buffer ical-buffer (erase-buffer)))
  24495. (setq category (or org-category
  24496. (file-name-sans-extension
  24497. (file-name-nondirectory buffer-file-name))))
  24498. (if (symbolp category) (setq category (symbol-name category)))
  24499. (let ((standard-output ical-buffer))
  24500. (if combine
  24501. (and (not started) (setq started t)
  24502. (org-start-icalendar-file org-icalendar-combined-name))
  24503. (org-start-icalendar-file category))
  24504. (org-print-icalendar-entries combine)
  24505. (when (or (and combine (not files)) (not combine))
  24506. (org-finish-icalendar-file)
  24507. (set-buffer ical-buffer)
  24508. (save-buffer)
  24509. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24510. (org-release-buffers org-agenda-new-buffers))))
  24511. (defvar org-after-save-iCalendar-file-hook nil
  24512. "Hook run after an iCalendar file has been saved.
  24513. The iCalendar buffer is still current when this hook is run.
  24514. A good way to use this is to tell a desktop calenndar application to re-read
  24515. the iCalendar file.")
  24516. (defun org-print-icalendar-entries (&optional combine)
  24517. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24518. When COMBINE is non nil, add the category to each line."
  24519. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24520. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24521. (dts (org-ical-ts-to-string
  24522. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24523. "DTSTART"))
  24524. hd ts ts2 state status (inc t) pos b sexp rrule
  24525. scheduledp deadlinep tmp pri category entry location summary desc
  24526. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24527. (org-refresh-category-properties)
  24528. (save-excursion
  24529. (goto-char (point-min))
  24530. (while (re-search-forward re1 nil t)
  24531. (catch :skip
  24532. (org-agenda-skip)
  24533. (setq pos (match-beginning 0)
  24534. ts (match-string 0)
  24535. inc t
  24536. hd (org-get-heading)
  24537. summary (org-icalendar-cleanup-string
  24538. (org-entry-get nil "SUMMARY"))
  24539. desc (org-icalendar-cleanup-string
  24540. (or (org-entry-get nil "DESCRIPTION")
  24541. (and org-icalendar-include-body (org-get-entry)))
  24542. t org-icalendar-include-body)
  24543. location (org-icalendar-cleanup-string
  24544. (org-entry-get nil "LOCATION"))
  24545. category (org-get-category))
  24546. (if (looking-at re2)
  24547. (progn
  24548. (goto-char (match-end 0))
  24549. (setq ts2 (match-string 1) inc nil))
  24550. (setq tmp (buffer-substring (max (point-min)
  24551. (- pos org-ds-keyword-length))
  24552. pos)
  24553. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24554. (progn
  24555. (setq inc nil)
  24556. (replace-match "\\1" t nil ts))
  24557. ts)
  24558. deadlinep (string-match org-deadline-regexp tmp)
  24559. scheduledp (string-match org-scheduled-regexp tmp)
  24560. ;; donep (org-entry-is-done-p)
  24561. ))
  24562. (if (or (string-match org-tr-regexp hd)
  24563. (string-match org-ts-regexp hd))
  24564. (setq hd (replace-match "" t t hd)))
  24565. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24566. (setq rrule
  24567. (concat "\nRRULE:FREQ="
  24568. (cdr (assoc
  24569. (match-string 2 ts)
  24570. '(("d" . "DAILY")("w" . "WEEKLY")
  24571. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24572. ";INTERVAL=" (match-string 1 ts)))
  24573. (setq rrule ""))
  24574. (setq summary (or summary hd))
  24575. (if (string-match org-bracket-link-regexp summary)
  24576. (setq summary
  24577. (replace-match (if (match-end 3)
  24578. (match-string 3 summary)
  24579. (match-string 1 summary))
  24580. t t summary)))
  24581. (if deadlinep (setq summary (concat "DL: " summary)))
  24582. (if scheduledp (setq summary (concat "S: " summary)))
  24583. (if (string-match "\\`<%%" ts)
  24584. (with-current-buffer sexp-buffer
  24585. (insert (substring ts 1 -1) " " summary "\n"))
  24586. (princ (format "BEGIN:VEVENT
  24587. %s
  24588. %s%s
  24589. SUMMARY:%s%s%s
  24590. CATEGORIES:%s
  24591. END:VEVENT\n"
  24592. (org-ical-ts-to-string ts "DTSTART")
  24593. (org-ical-ts-to-string ts2 "DTEND" inc)
  24594. rrule summary
  24595. (if (and desc (string-match "\\S-" desc))
  24596. (concat "\nDESCRIPTION: " desc) "")
  24597. (if (and location (string-match "\\S-" location))
  24598. (concat "\nLOCATION: " location) "")
  24599. category)))))
  24600. (when (and org-icalendar-include-sexps
  24601. (condition-case nil (require 'icalendar) (error nil))
  24602. (fboundp 'icalendar-export-region))
  24603. ;; Get all the literal sexps
  24604. (goto-char (point-min))
  24605. (while (re-search-forward "^&?%%(" nil t)
  24606. (catch :skip
  24607. (org-agenda-skip)
  24608. (setq b (match-beginning 0))
  24609. (goto-char (1- (match-end 0)))
  24610. (forward-sexp 1)
  24611. (end-of-line 1)
  24612. (setq sexp (buffer-substring b (point)))
  24613. (with-current-buffer sexp-buffer
  24614. (insert sexp "\n"))
  24615. (princ (org-diary-to-ical-string sexp-buffer)))))
  24616. (when org-icalendar-include-todo
  24617. (goto-char (point-min))
  24618. (while (re-search-forward org-todo-line-regexp nil t)
  24619. (catch :skip
  24620. (org-agenda-skip)
  24621. (setq state (match-string 2))
  24622. (setq status (if (member state org-done-keywords)
  24623. "COMPLETED" "NEEDS-ACTION"))
  24624. (when (and state
  24625. (or (not (member state org-done-keywords))
  24626. (eq org-icalendar-include-todo 'all))
  24627. (not (member org-archive-tag (org-get-tags-at)))
  24628. )
  24629. (setq hd (match-string 3)
  24630. summary (org-icalendar-cleanup-string
  24631. (org-entry-get nil "SUMMARY"))
  24632. desc (org-icalendar-cleanup-string
  24633. (or (org-entry-get nil "DESCRIPTION")
  24634. (and org-icalendar-include-body (org-get-entry)))
  24635. t org-icalendar-include-body)
  24636. location (org-icalendar-cleanup-string
  24637. (org-entry-get nil "LOCATION")))
  24638. (if (string-match org-bracket-link-regexp hd)
  24639. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24640. (match-string 1 hd))
  24641. t t hd)))
  24642. (if (string-match org-priority-regexp hd)
  24643. (setq pri (string-to-char (match-string 2 hd))
  24644. hd (concat (substring hd 0 (match-beginning 1))
  24645. (substring hd (match-end 1))))
  24646. (setq pri org-default-priority))
  24647. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24648. (- org-lowest-priority org-highest-priority))))))
  24649. (princ (format "BEGIN:VTODO
  24650. %s
  24651. SUMMARY:%s%s%s
  24652. CATEGORIES:%s
  24653. SEQUENCE:1
  24654. PRIORITY:%d
  24655. STATUS:%s
  24656. END:VTODO\n"
  24657. dts
  24658. (or summary hd)
  24659. (if (and location (string-match "\\S-" location))
  24660. (concat "\nLOCATION: " location) "")
  24661. (if (and desc (string-match "\\S-" desc))
  24662. (concat "\nDESCRIPTION: " desc) "")
  24663. category pri status)))))))))
  24664. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24665. "Take out stuff and quote what needs to be quoted.
  24666. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24667. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24668. characters."
  24669. (if (not s)
  24670. nil
  24671. (when is-body
  24672. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24673. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24674. (while (string-match re s) (setq s (replace-match "" t t s)))
  24675. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24676. (let ((start 0))
  24677. (while (string-match "\\([,;\\]\\)" s start)
  24678. (setq start (+ (match-beginning 0) 2)
  24679. s (replace-match "\\\\\\1" nil nil s))))
  24680. (when is-body
  24681. (while (string-match "[ \t]*\n[ \t]*" s)
  24682. (setq s (replace-match "\\n" t t s))))
  24683. (setq s (org-trim s))
  24684. (if is-body
  24685. (if maxlength
  24686. (if (and (numberp maxlength)
  24687. (> (length s) maxlength))
  24688. (setq s (substring s 0 maxlength)))))
  24689. s))
  24690. (defun org-get-entry ()
  24691. "Clean-up description string."
  24692. (save-excursion
  24693. (org-back-to-heading t)
  24694. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24695. (defun org-start-icalendar-file (name)
  24696. "Start an iCalendar file by inserting the header."
  24697. (let ((user user-full-name)
  24698. (name (or name "unknown"))
  24699. (timezone (cadr (current-time-zone))))
  24700. (princ
  24701. (format "BEGIN:VCALENDAR
  24702. VERSION:2.0
  24703. X-WR-CALNAME:%s
  24704. PRODID:-//%s//Emacs with Org-mode//EN
  24705. X-WR-TIMEZONE:%s
  24706. CALSCALE:GREGORIAN\n" name user timezone))))
  24707. (defun org-finish-icalendar-file ()
  24708. "Finish an iCalendar file by inserting the END statement."
  24709. (princ "END:VCALENDAR\n"))
  24710. (defun org-ical-ts-to-string (s keyword &optional inc)
  24711. "Take a time string S and convert it to iCalendar format.
  24712. KEYWORD is added in front, to make a complete line like DTSTART....
  24713. When INC is non-nil, increase the hour by two (if time string contains
  24714. a time), or the day by one (if it does not contain a time)."
  24715. (let ((t1 (org-parse-time-string s 'nodefault))
  24716. t2 fmt have-time time)
  24717. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24718. (setq t2 t1 have-time t)
  24719. (setq t2 (org-parse-time-string s)))
  24720. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24721. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24722. (when inc
  24723. (if have-time
  24724. (if org-agenda-default-appointment-duration
  24725. (setq mi (+ org-agenda-default-appointment-duration mi))
  24726. (setq h (+ 2 h)))
  24727. (setq d (1+ d))))
  24728. (setq time (encode-time s mi h d m y)))
  24729. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24730. (concat keyword (format-time-string fmt time))))
  24731. ;;; XOXO export
  24732. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24733. (with-current-buffer buffer
  24734. (apply 'insert output)))
  24735. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24736. (defun org-export-as-xoxo (&optional buffer)
  24737. "Export the org buffer as XOXO.
  24738. The XOXO buffer is named *xoxo-<source buffer name>*"
  24739. (interactive (list (current-buffer)))
  24740. ;; A quickie abstraction
  24741. ;; Output everything as XOXO
  24742. (with-current-buffer (get-buffer buffer)
  24743. (let* ((pos (point))
  24744. (opt-plist (org-combine-plists (org-default-export-plist)
  24745. (org-infile-export-plist)))
  24746. (filename (concat (file-name-as-directory
  24747. (org-export-directory :xoxo opt-plist))
  24748. (file-name-sans-extension
  24749. (file-name-nondirectory buffer-file-name))
  24750. ".html"))
  24751. (out (find-file-noselect filename))
  24752. (last-level 1)
  24753. (hanging-li nil))
  24754. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24755. ;; Check the output buffer is empty.
  24756. (with-current-buffer out (erase-buffer))
  24757. ;; Kick off the output
  24758. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24759. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24760. (let* ((hd (match-string-no-properties 1))
  24761. (level (length hd))
  24762. (text (concat
  24763. (match-string-no-properties 2)
  24764. (save-excursion
  24765. (goto-char (match-end 0))
  24766. (let ((str ""))
  24767. (catch 'loop
  24768. (while 't
  24769. (forward-line)
  24770. (if (looking-at "^[ \t]\\(.*\\)")
  24771. (setq str (concat str (match-string-no-properties 1)))
  24772. (throw 'loop str)))))))))
  24773. ;; Handle level rendering
  24774. (cond
  24775. ((> level last-level)
  24776. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24777. ((< level last-level)
  24778. (dotimes (- (- last-level level) 1)
  24779. (if hanging-li
  24780. (org-export-as-xoxo-insert-into out "</li>\n"))
  24781. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24782. (when hanging-li
  24783. (org-export-as-xoxo-insert-into out "</li>\n")
  24784. (setq hanging-li nil)))
  24785. ((equal level last-level)
  24786. (if hanging-li
  24787. (org-export-as-xoxo-insert-into out "</li>\n")))
  24788. )
  24789. (setq last-level level)
  24790. ;; And output the new li
  24791. (setq hanging-li 't)
  24792. (if (equal ?+ (elt text 0))
  24793. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24794. (org-export-as-xoxo-insert-into out "<li>" text))))
  24795. ;; Finally finish off the ol
  24796. (dotimes (- last-level 1)
  24797. (if hanging-li
  24798. (org-export-as-xoxo-insert-into out "</li>\n"))
  24799. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24800. (goto-char pos)
  24801. ;; Finish the buffer off and clean it up.
  24802. (switch-to-buffer-other-window out)
  24803. (indent-region (point-min) (point-max) nil)
  24804. (save-buffer)
  24805. (goto-char (point-min))
  24806. )))
  24807. ;;;; Key bindings
  24808. ;; Make `C-c C-x' a prefix key
  24809. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24810. ;; TAB key with modifiers
  24811. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24812. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24813. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24814. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24815. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24816. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24817. ;; The following line is necessary under Suse GNU/Linux
  24818. (unless (featurep 'xemacs)
  24819. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24820. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24821. (define-key org-mode-map [backtab] 'org-shifttab)
  24822. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24823. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24824. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24825. ;; Cursor keys with modifiers
  24826. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24827. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24828. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24829. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24830. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24831. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24832. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24833. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24834. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24835. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24836. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24837. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24838. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24839. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24840. ;;; Extra keys for tty access.
  24841. ;; We only set them when really needed because otherwise the
  24842. ;; menus don't show the simple keys
  24843. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24844. (not window-system))
  24845. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24846. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24847. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24848. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24849. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24850. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24851. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24852. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24853. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24854. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24855. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24856. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24857. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24858. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24859. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24860. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24861. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24862. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24863. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24864. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24865. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24866. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24867. ;; All the other keys
  24868. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24869. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24870. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24871. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24872. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24873. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24874. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24875. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24876. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24877. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24878. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24879. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24880. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24881. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24882. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24883. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24884. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24885. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24886. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24887. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24888. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24889. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24890. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24891. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24892. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24893. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24894. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24895. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24896. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24897. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24898. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24899. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24900. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24901. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24902. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24903. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24904. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24905. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24906. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24907. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24908. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24909. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24910. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24911. (org-defkey org-mode-map "\C-m" 'org-return)
  24912. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24913. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24914. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24915. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24916. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24917. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24918. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24919. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24920. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  24921. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24922. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24923. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24924. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24925. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24926. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24927. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24928. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24929. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24930. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24931. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24932. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24933. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24934. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24935. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24936. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24937. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24938. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24939. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24940. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24941. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24942. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24943. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24944. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24945. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24946. (when (featurep 'xemacs)
  24947. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24948. (defsubst org-table-p () (org-at-table-p))
  24949. (defun org-self-insert-command (N)
  24950. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24951. If the cursor is in a table looking at whitespace, the whitespace is
  24952. overwritten, and the table is not marked as requiring realignment."
  24953. (interactive "p")
  24954. (if (and (org-table-p)
  24955. (progn
  24956. ;; check if we blank the field, and if that triggers align
  24957. (and org-table-auto-blank-field
  24958. (member last-command
  24959. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24960. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24961. ;; got extra space, this field does not determine column width
  24962. (let (org-table-may-need-update) (org-table-blank-field))
  24963. ;; no extra space, this field may determine column width
  24964. (org-table-blank-field)))
  24965. t)
  24966. (eq N 1)
  24967. (looking-at "[^|\n]* |"))
  24968. (let (org-table-may-need-update)
  24969. (goto-char (1- (match-end 0)))
  24970. (delete-backward-char 1)
  24971. (goto-char (match-beginning 0))
  24972. (self-insert-command N))
  24973. (setq org-table-may-need-update t)
  24974. (self-insert-command N)
  24975. (org-fix-tags-on-the-fly)))
  24976. (defun org-fix-tags-on-the-fly ()
  24977. (when (and (equal (char-after (point-at-bol)) ?*)
  24978. (org-on-heading-p))
  24979. (org-align-tags-here org-tags-column)))
  24980. (defun org-delete-backward-char (N)
  24981. "Like `delete-backward-char', insert whitespace at field end in tables.
  24982. When deleting backwards, in tables this function will insert whitespace in
  24983. front of the next \"|\" separator, to keep the table aligned. The table will
  24984. still be marked for re-alignment if the field did fill the entire column,
  24985. because, in this case the deletion might narrow the column."
  24986. (interactive "p")
  24987. (if (and (org-table-p)
  24988. (eq N 1)
  24989. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24990. (looking-at ".*?|"))
  24991. (let ((pos (point))
  24992. (noalign (looking-at "[^|\n\r]* |"))
  24993. (c org-table-may-need-update))
  24994. (backward-delete-char N)
  24995. (skip-chars-forward "^|")
  24996. (insert " ")
  24997. (goto-char (1- pos))
  24998. ;; noalign: if there were two spaces at the end, this field
  24999. ;; does not determine the width of the column.
  25000. (if noalign (setq org-table-may-need-update c)))
  25001. (backward-delete-char N)
  25002. (org-fix-tags-on-the-fly)))
  25003. (defun org-delete-char (N)
  25004. "Like `delete-char', but insert whitespace at field end in tables.
  25005. When deleting characters, in tables this function will insert whitespace in
  25006. front of the next \"|\" separator, to keep the table aligned. The table will
  25007. still be marked for re-alignment if the field did fill the entire column,
  25008. because, in this case the deletion might narrow the column."
  25009. (interactive "p")
  25010. (if (and (org-table-p)
  25011. (not (bolp))
  25012. (not (= (char-after) ?|))
  25013. (eq N 1))
  25014. (if (looking-at ".*?|")
  25015. (let ((pos (point))
  25016. (noalign (looking-at "[^|\n\r]* |"))
  25017. (c org-table-may-need-update))
  25018. (replace-match (concat
  25019. (substring (match-string 0) 1 -1)
  25020. " |"))
  25021. (goto-char pos)
  25022. ;; noalign: if there were two spaces at the end, this field
  25023. ;; does not determine the width of the column.
  25024. (if noalign (setq org-table-may-need-update c)))
  25025. (delete-char N))
  25026. (delete-char N)
  25027. (org-fix-tags-on-the-fly)))
  25028. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  25029. (put 'org-self-insert-command 'delete-selection t)
  25030. (put 'orgtbl-self-insert-command 'delete-selection t)
  25031. (put 'org-delete-char 'delete-selection 'supersede)
  25032. (put 'org-delete-backward-char 'delete-selection 'supersede)
  25033. ;; Make `flyspell-mode' delay after some commands
  25034. (put 'org-self-insert-command 'flyspell-delayed t)
  25035. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  25036. (put 'org-delete-char 'flyspell-delayed t)
  25037. (put 'org-delete-backward-char 'flyspell-delayed t)
  25038. ;; Make pabbrev-mode expand after org-mode commands
  25039. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  25040. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  25041. ;; How to do this: Measure non-white length of current string
  25042. ;; If equal to column width, we should realign.
  25043. (defun org-remap (map &rest commands)
  25044. "In MAP, remap the functions given in COMMANDS.
  25045. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  25046. (let (new old)
  25047. (while commands
  25048. (setq old (pop commands) new (pop commands))
  25049. (if (fboundp 'command-remapping)
  25050. (org-defkey map (vector 'remap old) new)
  25051. (substitute-key-definition old new map global-map)))))
  25052. (when (eq org-enable-table-editor 'optimized)
  25053. ;; If the user wants maximum table support, we need to hijack
  25054. ;; some standard editing functions
  25055. (org-remap org-mode-map
  25056. 'self-insert-command 'org-self-insert-command
  25057. 'delete-char 'org-delete-char
  25058. 'delete-backward-char 'org-delete-backward-char)
  25059. (org-defkey org-mode-map "|" 'org-force-self-insert))
  25060. (defun org-shiftcursor-error ()
  25061. "Throw an error because Shift-Cursor command was applied in wrong context."
  25062. (error "This command is active in special context like tables, headlines or timestamps"))
  25063. (defun org-shifttab (&optional arg)
  25064. "Global visibility cycling or move to previous table field.
  25065. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  25066. on context.
  25067. See the individual commands for more information."
  25068. (interactive "P")
  25069. (cond
  25070. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  25071. (arg (message "Content view to level: ")
  25072. (org-content (prefix-numeric-value arg))
  25073. (setq org-cycle-global-status 'overview))
  25074. (t (call-interactively 'org-global-cycle))))
  25075. (defun org-shiftmetaleft ()
  25076. "Promote subtree or delete table column.
  25077. Calls `org-promote-subtree', `org-outdent-item',
  25078. or `org-table-delete-column', depending on context.
  25079. See the individual commands for more information."
  25080. (interactive)
  25081. (cond
  25082. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  25083. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  25084. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25085. (t (org-shiftcursor-error))))
  25086. (defun org-shiftmetaright ()
  25087. "Demote subtree or insert table column.
  25088. Calls `org-demote-subtree', `org-indent-item',
  25089. or `org-table-insert-column', depending on context.
  25090. See the individual commands for more information."
  25091. (interactive)
  25092. (cond
  25093. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  25094. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  25095. ((org-at-item-p) (call-interactively 'org-indent-item))
  25096. (t (org-shiftcursor-error))))
  25097. (defun org-shiftmetaup (&optional arg)
  25098. "Move subtree up or kill table row.
  25099. Calls `org-move-subtree-up' or `org-table-kill-row' or
  25100. `org-move-item-up' depending on context. See the individual commands
  25101. for more information."
  25102. (interactive "P")
  25103. (cond
  25104. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  25105. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25106. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25107. (t (org-shiftcursor-error))))
  25108. (defun org-shiftmetadown (&optional arg)
  25109. "Move subtree down or insert table row.
  25110. Calls `org-move-subtree-down' or `org-table-insert-row' or
  25111. `org-move-item-down', depending on context. See the individual
  25112. commands for more information."
  25113. (interactive "P")
  25114. (cond
  25115. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  25116. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25117. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25118. (t (org-shiftcursor-error))))
  25119. (defun org-metaleft (&optional arg)
  25120. "Promote heading or move table column to left.
  25121. Calls `org-do-promote' or `org-table-move-column', depending on context.
  25122. With no specific context, calls the Emacs default `backward-word'.
  25123. See the individual commands for more information."
  25124. (interactive "P")
  25125. (cond
  25126. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  25127. ((or (org-on-heading-p) (org-region-active-p))
  25128. (call-interactively 'org-do-promote))
  25129. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25130. (t (call-interactively 'backward-word))))
  25131. (defun org-metaright (&optional arg)
  25132. "Demote subtree or move table column to right.
  25133. Calls `org-do-demote' or `org-table-move-column', depending on context.
  25134. With no specific context, calls the Emacs default `forward-word'.
  25135. See the individual commands for more information."
  25136. (interactive "P")
  25137. (cond
  25138. ((org-at-table-p) (call-interactively 'org-table-move-column))
  25139. ((or (org-on-heading-p) (org-region-active-p))
  25140. (call-interactively 'org-do-demote))
  25141. ((org-at-item-p) (call-interactively 'org-indent-item))
  25142. (t (call-interactively 'forward-word))))
  25143. (defun org-metaup (&optional arg)
  25144. "Move subtree up or move table row up.
  25145. Calls `org-move-subtree-up' or `org-table-move-row' or
  25146. `org-move-item-up', depending on context. See the individual commands
  25147. for more information."
  25148. (interactive "P")
  25149. (cond
  25150. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  25151. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25152. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25153. (t (transpose-lines 1) (beginning-of-line -1))))
  25154. (defun org-metadown (&optional arg)
  25155. "Move subtree down or move table row down.
  25156. Calls `org-move-subtree-down' or `org-table-move-row' or
  25157. `org-move-item-down', depending on context. See the individual
  25158. commands for more information."
  25159. (interactive "P")
  25160. (cond
  25161. ((org-at-table-p) (call-interactively 'org-table-move-row))
  25162. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25163. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25164. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  25165. (defun org-shiftup (&optional arg)
  25166. "Increase item in timestamp or increase priority of current headline.
  25167. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  25168. depending on context. See the individual commands for more information."
  25169. (interactive "P")
  25170. (cond
  25171. ((org-at-timestamp-p t)
  25172. (call-interactively (if org-edit-timestamp-down-means-later
  25173. 'org-timestamp-down 'org-timestamp-up)))
  25174. ((org-on-heading-p) (call-interactively 'org-priority-up))
  25175. ((org-at-item-p) (call-interactively 'org-previous-item))
  25176. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  25177. (defun org-shiftdown (&optional arg)
  25178. "Decrease item in timestamp or decrease priority of current headline.
  25179. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  25180. depending on context. See the individual commands for more information."
  25181. (interactive "P")
  25182. (cond
  25183. ((org-at-timestamp-p t)
  25184. (call-interactively (if org-edit-timestamp-down-means-later
  25185. 'org-timestamp-up 'org-timestamp-down)))
  25186. ((org-on-heading-p) (call-interactively 'org-priority-down))
  25187. (t (call-interactively 'org-next-item))))
  25188. (defun org-shiftright ()
  25189. "Next TODO keyword or timestamp one day later, depending on context."
  25190. (interactive)
  25191. (cond
  25192. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  25193. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  25194. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  25195. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  25196. (t (org-shiftcursor-error))))
  25197. (defun org-shiftleft ()
  25198. "Previous TODO keyword or timestamp one day earlier, depending on context."
  25199. (interactive)
  25200. (cond
  25201. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  25202. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  25203. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  25204. ((org-at-property-p)
  25205. (call-interactively 'org-property-previous-allowed-value))
  25206. (t (org-shiftcursor-error))))
  25207. (defun org-shiftcontrolright ()
  25208. "Switch to next TODO set."
  25209. (interactive)
  25210. (cond
  25211. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  25212. (t (org-shiftcursor-error))))
  25213. (defun org-shiftcontrolleft ()
  25214. "Switch to previous TODO set."
  25215. (interactive)
  25216. (cond
  25217. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  25218. (t (org-shiftcursor-error))))
  25219. (defun org-ctrl-c-ret ()
  25220. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  25221. (interactive)
  25222. (cond
  25223. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  25224. (t (call-interactively 'org-insert-heading))))
  25225. (defun org-copy-special ()
  25226. "Copy region in table or copy current subtree.
  25227. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  25228. See the individual commands for more information."
  25229. (interactive)
  25230. (call-interactively
  25231. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  25232. (defun org-cut-special ()
  25233. "Cut region in table or cut current subtree.
  25234. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  25235. See the individual commands for more information."
  25236. (interactive)
  25237. (call-interactively
  25238. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  25239. (defun org-paste-special (arg)
  25240. "Paste rectangular region into table, or past subtree relative to level.
  25241. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  25242. See the individual commands for more information."
  25243. (interactive "P")
  25244. (if (org-at-table-p)
  25245. (org-table-paste-rectangle)
  25246. (org-paste-subtree arg)))
  25247. (defun org-ctrl-c-ctrl-c (&optional arg)
  25248. "Set tags in headline, or update according to changed information at point.
  25249. This command does many different things, depending on context:
  25250. - If the cursor is in a headline, prompt for tags and insert them
  25251. into the current line, aligned to `org-tags-column'. When called
  25252. with prefix arg, realign all tags in the current buffer.
  25253. - If the cursor is in one of the special #+KEYWORD lines, this
  25254. triggers scanning the buffer for these lines and updating the
  25255. information.
  25256. - If the cursor is inside a table, realign the table. This command
  25257. works even if the automatic table editor has been turned off.
  25258. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  25259. the entire table.
  25260. - If the cursor is a the beginning of a dynamic block, update it.
  25261. - If the cursor is inside a table created by the table.el package,
  25262. activate that table.
  25263. - If the current buffer is a remember buffer, close note and file it.
  25264. with a prefix argument, file it without further interaction to the default
  25265. location.
  25266. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  25267. links in this buffer.
  25268. - If the cursor is on a numbered item in a plain list, renumber the
  25269. ordered list.
  25270. - If the cursor is on a checkbox, toggle it."
  25271. (interactive "P")
  25272. (let ((org-enable-table-editor t))
  25273. (cond
  25274. ((or org-clock-overlays
  25275. org-occur-highlights
  25276. org-latex-fragment-image-overlays)
  25277. (org-remove-clock-overlays)
  25278. (org-remove-occur-highlights)
  25279. (org-remove-latex-fragment-image-overlays)
  25280. (message "Temporary highlights/overlays removed from current buffer"))
  25281. ((and (local-variable-p 'org-finish-function (current-buffer))
  25282. (fboundp org-finish-function))
  25283. (funcall org-finish-function))
  25284. ((org-at-property-p)
  25285. (call-interactively 'org-property-action))
  25286. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  25287. ((org-on-heading-p) (call-interactively 'org-set-tags))
  25288. ((org-at-table.el-p)
  25289. (require 'table)
  25290. (beginning-of-line 1)
  25291. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  25292. (call-interactively 'table-recognize-table))
  25293. ((org-at-table-p)
  25294. (org-table-maybe-eval-formula)
  25295. (if arg
  25296. (call-interactively 'org-table-recalculate)
  25297. (org-table-maybe-recalculate-line))
  25298. (call-interactively 'org-table-align))
  25299. ((org-at-item-checkbox-p)
  25300. (call-interactively 'org-toggle-checkbox))
  25301. ((org-at-item-p)
  25302. (call-interactively 'org-maybe-renumber-ordered-list))
  25303. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  25304. ;; Dynamic block
  25305. (beginning-of-line 1)
  25306. (org-update-dblock))
  25307. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  25308. (cond
  25309. ((equal (match-string 1) "TBLFM")
  25310. ;; Recalculate the table before this line
  25311. (save-excursion
  25312. (beginning-of-line 1)
  25313. (skip-chars-backward " \r\n\t")
  25314. (if (org-at-table-p)
  25315. (org-call-with-arg 'org-table-recalculate t))))
  25316. (t
  25317. (call-interactively 'org-mode-restart))))
  25318. (t (error "C-c C-c can do nothing useful at this location.")))))
  25319. (defun org-mode-restart ()
  25320. "Restart Org-mode, to scan again for special lines.
  25321. Also updates the keyword regular expressions."
  25322. (interactive)
  25323. (let ((org-inhibit-startup t)) (org-mode))
  25324. (message "Org-mode restarted to refresh keyword and special line setup"))
  25325. (defun org-kill-note-or-show-branches ()
  25326. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  25327. (interactive)
  25328. (if (not org-finish-function)
  25329. (call-interactively 'show-branches)
  25330. (let ((org-note-abort t))
  25331. (funcall org-finish-function))))
  25332. (defun org-return (&optional indent)
  25333. "Goto next table row or insert a newline.
  25334. Calls `org-table-next-row' or `newline', depending on context.
  25335. See the individual commands for more information."
  25336. (interactive)
  25337. (cond
  25338. ((bobp) (if indent (newline-and-indent) (newline)))
  25339. ((and (org-at-heading-p)
  25340. (looking-at
  25341. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  25342. (org-show-entry)
  25343. (end-of-line 1)
  25344. (newline))
  25345. ((org-at-table-p)
  25346. (org-table-justify-field-maybe)
  25347. (call-interactively 'org-table-next-row))
  25348. (t (if indent (newline-and-indent) (newline)))))
  25349. (defun org-return-indent ()
  25350. (interactive)
  25351. "Goto next table row or insert a newline and indent.
  25352. Calls `org-table-next-row' or `newline-and-indent', depending on
  25353. context. See the individual commands for more information."
  25354. (org-return t))
  25355. (defun org-ctrl-c-minus ()
  25356. "Insert separator line in table or modify bullet type in list.
  25357. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  25358. depending on context."
  25359. (interactive)
  25360. (cond
  25361. ((org-at-table-p)
  25362. (call-interactively 'org-table-insert-hline))
  25363. ((org-on-heading-p)
  25364. ;; Convert to item
  25365. (save-excursion
  25366. (beginning-of-line 1)
  25367. (if (looking-at "\\*+ ")
  25368. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  25369. ((org-in-item-p)
  25370. (call-interactively 'org-cycle-list-bullet))
  25371. (t (error "`C-c -' does have no function here."))))
  25372. (defun org-meta-return (&optional arg)
  25373. "Insert a new heading or wrap a region in a table.
  25374. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  25375. See the individual commands for more information."
  25376. (interactive "P")
  25377. (cond
  25378. ((org-at-table-p)
  25379. (call-interactively 'org-table-wrap-region))
  25380. (t (call-interactively 'org-insert-heading))))
  25381. ;;; Menu entries
  25382. ;; Define the Org-mode menus
  25383. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  25384. '("Tbl"
  25385. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  25386. ["Next Field" org-cycle (org-at-table-p)]
  25387. ["Previous Field" org-shifttab (org-at-table-p)]
  25388. ["Next Row" org-return (org-at-table-p)]
  25389. "--"
  25390. ["Blank Field" org-table-blank-field (org-at-table-p)]
  25391. ["Edit Field" org-table-edit-field (org-at-table-p)]
  25392. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  25393. "--"
  25394. ("Column"
  25395. ["Move Column Left" org-metaleft (org-at-table-p)]
  25396. ["Move Column Right" org-metaright (org-at-table-p)]
  25397. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  25398. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  25399. ("Row"
  25400. ["Move Row Up" org-metaup (org-at-table-p)]
  25401. ["Move Row Down" org-metadown (org-at-table-p)]
  25402. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25403. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25404. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25405. "--"
  25406. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25407. ("Rectangle"
  25408. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25409. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25410. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25411. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25412. "--"
  25413. ("Calculate"
  25414. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25415. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25416. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25417. "--"
  25418. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25419. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25420. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25421. "--"
  25422. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25423. "--"
  25424. ["Sum Column/Rectangle" org-table-sum
  25425. (or (org-at-table-p) (org-region-active-p))]
  25426. ["Which Column?" org-table-current-column (org-at-table-p)])
  25427. ["Debug Formulas"
  25428. org-table-toggle-formula-debugger
  25429. :style toggle :selected org-table-formula-debug]
  25430. ["Show Col/Row Numbers"
  25431. org-table-toggle-coordinate-overlays
  25432. :style toggle :selected org-table-overlay-coordinates]
  25433. "--"
  25434. ["Create" org-table-create (and (not (org-at-table-p))
  25435. org-enable-table-editor)]
  25436. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25437. ["Import from File" org-table-import (not (org-at-table-p))]
  25438. ["Export to File" org-table-export (org-at-table-p)]
  25439. "--"
  25440. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25441. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25442. '("Org"
  25443. ("Show/Hide"
  25444. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25445. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25446. ["Sparse Tree" org-occur t]
  25447. ["Reveal Context" org-reveal t]
  25448. ["Show All" show-all t]
  25449. "--"
  25450. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25451. "--"
  25452. ["New Heading" org-insert-heading t]
  25453. ("Navigate Headings"
  25454. ["Up" outline-up-heading t]
  25455. ["Next" outline-next-visible-heading t]
  25456. ["Previous" outline-previous-visible-heading t]
  25457. ["Next Same Level" outline-forward-same-level t]
  25458. ["Previous Same Level" outline-backward-same-level t]
  25459. "--"
  25460. ["Jump" org-goto t])
  25461. ("Edit Structure"
  25462. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25463. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25464. "--"
  25465. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25466. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25467. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25468. "--"
  25469. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25470. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25471. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25472. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25473. "--"
  25474. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25475. "--"
  25476. ["Convert to odd levels" org-convert-to-odd-levels t]
  25477. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25478. ("Editing"
  25479. ["Emphasis..." org-emphasize t])
  25480. ("Archive"
  25481. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25482. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25483. ; :active t :keys "C-u C-c C-x C-a"]
  25484. ["Sparse trees open ARCHIVE trees"
  25485. (setq org-sparse-tree-open-archived-trees
  25486. (not org-sparse-tree-open-archived-trees))
  25487. :style toggle :selected org-sparse-tree-open-archived-trees]
  25488. ["Cycling opens ARCHIVE trees"
  25489. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25490. :style toggle :selected org-cycle-open-archived-trees]
  25491. ["Agenda includes ARCHIVE trees"
  25492. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25493. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25494. "--"
  25495. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25496. ; ["Check and Move Children" (org-archive-subtree '(4))
  25497. ; :active t :keys "C-u C-c C-x C-s"]
  25498. )
  25499. "--"
  25500. ("TODO Lists"
  25501. ["TODO/DONE/-" org-todo t]
  25502. ("Select keyword"
  25503. ["Next keyword" org-shiftright (org-on-heading-p)]
  25504. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25505. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25506. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25507. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25508. ["Show TODO Tree" org-show-todo-tree t]
  25509. ["Global TODO list" org-todo-list t]
  25510. "--"
  25511. ["Set Priority" org-priority t]
  25512. ["Priority Up" org-shiftup t]
  25513. ["Priority Down" org-shiftdown t])
  25514. ("TAGS and Properties"
  25515. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25516. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25517. "--"
  25518. ["Set property" 'org-set-property t]
  25519. ["Column view of properties" org-columns t]
  25520. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25521. ("Dates and Scheduling"
  25522. ["Timestamp" org-time-stamp t]
  25523. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25524. ("Change Date"
  25525. ["1 Day Later" org-shiftright t]
  25526. ["1 Day Earlier" org-shiftleft t]
  25527. ["1 ... Later" org-shiftup t]
  25528. ["1 ... Earlier" org-shiftdown t])
  25529. ["Compute Time Range" org-evaluate-time-range t]
  25530. ["Schedule Item" org-schedule t]
  25531. ["Deadline" org-deadline t]
  25532. "--"
  25533. ["Custom time format" org-toggle-time-stamp-overlays
  25534. :style radio :selected org-display-custom-times]
  25535. "--"
  25536. ["Goto Calendar" org-goto-calendar t]
  25537. ["Date from Calendar" org-date-from-calendar t])
  25538. ("Logging work"
  25539. ["Clock in" org-clock-in t]
  25540. ["Clock out" org-clock-out t]
  25541. ["Clock cancel" org-clock-cancel t]
  25542. ["Goto running clock" org-clock-goto t]
  25543. ["Display times" org-clock-display t]
  25544. ["Create clock table" org-clock-report t]
  25545. "--"
  25546. ["Record DONE time"
  25547. (progn (setq org-log-done (not org-log-done))
  25548. (message "Switching to %s will %s record a timestamp"
  25549. (car org-done-keywords)
  25550. (if org-log-done "automatically" "not")))
  25551. :style toggle :selected org-log-done])
  25552. "--"
  25553. ["Agenda Command..." org-agenda t]
  25554. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25555. ("File List for Agenda")
  25556. ("Special views current file"
  25557. ["TODO Tree" org-show-todo-tree t]
  25558. ["Check Deadlines" org-check-deadlines t]
  25559. ["Timeline" org-timeline t]
  25560. ["Tags Tree" org-tags-sparse-tree t])
  25561. "--"
  25562. ("Hyperlinks"
  25563. ["Store Link (Global)" org-store-link t]
  25564. ["Insert Link" org-insert-link t]
  25565. ["Follow Link" org-open-at-point t]
  25566. "--"
  25567. ["Next link" org-next-link t]
  25568. ["Previous link" org-previous-link t]
  25569. "--"
  25570. ["Descriptive Links"
  25571. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25572. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25573. ["Literal Links"
  25574. (progn
  25575. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25576. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25577. "--"
  25578. ["Export/Publish..." org-export t]
  25579. ("LaTeX"
  25580. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25581. :selected org-cdlatex-mode]
  25582. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25583. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25584. ["Modify math symbol" org-cdlatex-math-modify
  25585. (org-inside-LaTeX-fragment-p)]
  25586. ["Export LaTeX fragments as images"
  25587. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25588. :style toggle :selected org-export-with-LaTeX-fragments])
  25589. "--"
  25590. ("Documentation"
  25591. ["Show Version" org-version t]
  25592. ["Info Documentation" org-info t])
  25593. ("Customize"
  25594. ["Browse Org Group" org-customize t]
  25595. "--"
  25596. ["Expand This Menu" org-create-customize-menu
  25597. (fboundp 'customize-menu-create)])
  25598. "--"
  25599. ["Refresh setup" org-mode-restart t]
  25600. ))
  25601. (defun org-info (&optional node)
  25602. "Read documentation for Org-mode in the info system.
  25603. With optional NODE, go directly to that node."
  25604. (interactive)
  25605. (require 'info)
  25606. (Info-goto-node (format "(org)%s" (or node ""))))
  25607. (defun org-install-agenda-files-menu ()
  25608. (let ((bl (buffer-list)))
  25609. (save-excursion
  25610. (while bl
  25611. (set-buffer (pop bl))
  25612. (if (org-mode-p) (setq bl nil)))
  25613. (when (org-mode-p)
  25614. (easy-menu-change
  25615. '("Org") "File List for Agenda"
  25616. (append
  25617. (list
  25618. ["Edit File List" (org-edit-agenda-file-list) t]
  25619. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25620. ["Remove Current File from List" org-remove-file t]
  25621. ["Cycle through agenda files" org-cycle-agenda-files t]
  25622. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25623. "--")
  25624. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25625. ;;;; Documentation
  25626. (defun org-customize ()
  25627. "Call the customize function with org as argument."
  25628. (interactive)
  25629. (customize-browse 'org))
  25630. (defun org-create-customize-menu ()
  25631. "Create a full customization menu for Org-mode, insert it into the menu."
  25632. (interactive)
  25633. (if (fboundp 'customize-menu-create)
  25634. (progn
  25635. (easy-menu-change
  25636. '("Org") "Customize"
  25637. `(["Browse Org group" org-customize t]
  25638. "--"
  25639. ,(customize-menu-create 'org)
  25640. ["Set" Custom-set t]
  25641. ["Save" Custom-save t]
  25642. ["Reset to Current" Custom-reset-current t]
  25643. ["Reset to Saved" Custom-reset-saved t]
  25644. ["Reset to Standard Settings" Custom-reset-standard t]))
  25645. (message "\"Org\"-menu now contains full customization menu"))
  25646. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25647. ;;;; Miscellaneous stuff
  25648. ;;; Generally useful functions
  25649. (defun org-context ()
  25650. "Return a list of contexts of the current cursor position.
  25651. If several contexts apply, all are returned.
  25652. Each context entry is a list with a symbol naming the context, and
  25653. two positions indicating start and end of the context. Possible
  25654. contexts are:
  25655. :headline anywhere in a headline
  25656. :headline-stars on the leading stars in a headline
  25657. :todo-keyword on a TODO keyword (including DONE) in a headline
  25658. :tags on the TAGS in a headline
  25659. :priority on the priority cookie in a headline
  25660. :item on the first line of a plain list item
  25661. :item-bullet on the bullet/number of a plain list item
  25662. :checkbox on the checkbox in a plain list item
  25663. :table in an org-mode table
  25664. :table-special on a special filed in a table
  25665. :table-table in a table.el table
  25666. :link on a hyperlink
  25667. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25668. :target on a <<target>>
  25669. :radio-target on a <<<radio-target>>>
  25670. :latex-fragment on a LaTeX fragment
  25671. :latex-preview on a LaTeX fragment with overlayed preview image
  25672. This function expects the position to be visible because it uses font-lock
  25673. faces as a help to recognize the following contexts: :table-special, :link,
  25674. and :keyword."
  25675. (let* ((f (get-text-property (point) 'face))
  25676. (faces (if (listp f) f (list f)))
  25677. (p (point)) clist o)
  25678. ;; First the large context
  25679. (cond
  25680. ((org-on-heading-p t)
  25681. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25682. (when (progn
  25683. (beginning-of-line 1)
  25684. (looking-at org-todo-line-tags-regexp))
  25685. (push (org-point-in-group p 1 :headline-stars) clist)
  25686. (push (org-point-in-group p 2 :todo-keyword) clist)
  25687. (push (org-point-in-group p 4 :tags) clist))
  25688. (goto-char p)
  25689. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25690. (if (looking-at "\\[#[A-Z0-9]\\]")
  25691. (push (org-point-in-group p 0 :priority) clist)))
  25692. ((org-at-item-p)
  25693. (push (org-point-in-group p 2 :item-bullet) clist)
  25694. (push (list :item (point-at-bol)
  25695. (save-excursion (org-end-of-item) (point)))
  25696. clist)
  25697. (and (org-at-item-checkbox-p)
  25698. (push (org-point-in-group p 0 :checkbox) clist)))
  25699. ((org-at-table-p)
  25700. (push (list :table (org-table-begin) (org-table-end)) clist)
  25701. (if (memq 'org-formula faces)
  25702. (push (list :table-special
  25703. (previous-single-property-change p 'face)
  25704. (next-single-property-change p 'face)) clist)))
  25705. ((org-at-table-p 'any)
  25706. (push (list :table-table) clist)))
  25707. (goto-char p)
  25708. ;; Now the small context
  25709. (cond
  25710. ((org-at-timestamp-p)
  25711. (push (org-point-in-group p 0 :timestamp) clist))
  25712. ((memq 'org-link faces)
  25713. (push (list :link
  25714. (previous-single-property-change p 'face)
  25715. (next-single-property-change p 'face)) clist))
  25716. ((memq 'org-special-keyword faces)
  25717. (push (list :keyword
  25718. (previous-single-property-change p 'face)
  25719. (next-single-property-change p 'face)) clist))
  25720. ((org-on-target-p)
  25721. (push (org-point-in-group p 0 :target) clist)
  25722. (goto-char (1- (match-beginning 0)))
  25723. (if (looking-at org-radio-target-regexp)
  25724. (push (org-point-in-group p 0 :radio-target) clist))
  25725. (goto-char p))
  25726. ((setq o (car (delq nil
  25727. (mapcar
  25728. (lambda (x)
  25729. (if (memq x org-latex-fragment-image-overlays) x))
  25730. (org-overlays-at (point))))))
  25731. (push (list :latex-fragment
  25732. (org-overlay-start o) (org-overlay-end o)) clist)
  25733. (push (list :latex-preview
  25734. (org-overlay-start o) (org-overlay-end o)) clist))
  25735. ((org-inside-LaTeX-fragment-p)
  25736. ;; FIXME: positions wrong.
  25737. (push (list :latex-fragment (point) (point)) clist)))
  25738. (setq clist (nreverse (delq nil clist)))
  25739. clist))
  25740. ;; FIXME: Compare with at-regexp-p Do we need both?
  25741. (defun org-in-regexp (re &optional nlines visually)
  25742. "Check if point is inside a match of regexp.
  25743. Normally only the current line is checked, but you can include NLINES extra
  25744. lines both before and after point into the search.
  25745. If VISUALLY is set, require that the cursor is not after the match but
  25746. really on, so that the block visually is on the match."
  25747. (catch 'exit
  25748. (let ((pos (point))
  25749. (eol (point-at-eol (+ 1 (or nlines 0))))
  25750. (inc (if visually 1 0)))
  25751. (save-excursion
  25752. (beginning-of-line (- 1 (or nlines 0)))
  25753. (while (re-search-forward re eol t)
  25754. (if (and (<= (match-beginning 0) pos)
  25755. (>= (+ inc (match-end 0)) pos))
  25756. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25757. (defun org-at-regexp-p (regexp)
  25758. "Is point inside a match of REGEXP in the current line?"
  25759. (catch 'exit
  25760. (save-excursion
  25761. (let ((pos (point)) (end (point-at-eol)))
  25762. (beginning-of-line 1)
  25763. (while (re-search-forward regexp end t)
  25764. (if (and (<= (match-beginning 0) pos)
  25765. (>= (match-end 0) pos))
  25766. (throw 'exit t)))
  25767. nil))))
  25768. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25769. "Call `multi-occur' with buffers for all agenda files."
  25770. (interactive "sOrg-files matching: \np")
  25771. (let* ((files (org-agenda-files))
  25772. (tnames (mapcar 'file-truename files))
  25773. (extra org-agenda-text-search-extra-files)
  25774. f)
  25775. (while (setq f (pop extra))
  25776. (unless (member (file-truename f) tnames)
  25777. (add-to-list 'files f 'append)
  25778. (add-to-list 'tnames (file-truename f) 'append)))
  25779. (multi-occur
  25780. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25781. regexp)))
  25782. (if (boundp 'occur-mode-find-occurrence-hook)
  25783. ;; Emacs 23
  25784. (add-hook 'occur-mode-find-occurrence-hook
  25785. (lambda ()
  25786. (when (org-mode-p)
  25787. (org-reveal))))
  25788. ;; Emacs 22
  25789. (defadvice occur-mode-goto-occurrence
  25790. (after org-occur-reveal activate)
  25791. (and (org-mode-p) (org-reveal)))
  25792. (defadvice occur-mode-goto-occurrence-other-window
  25793. (after org-occur-reveal activate)
  25794. (and (org-mode-p) (org-reveal)))
  25795. (defadvice occur-mode-display-occurrence
  25796. (after org-occur-reveal activate)
  25797. (when (org-mode-p)
  25798. (let ((pos (occur-mode-find-occurrence)))
  25799. (with-current-buffer (marker-buffer pos)
  25800. (save-excursion
  25801. (goto-char pos)
  25802. (org-reveal)))))))
  25803. (defun org-uniquify (list)
  25804. "Remove duplicate elements from LIST."
  25805. (let (res)
  25806. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25807. res))
  25808. (defun org-delete-all (elts list)
  25809. "Remove all elements in ELTS from LIST."
  25810. (while elts
  25811. (setq list (delete (pop elts) list)))
  25812. list)
  25813. (defun org-back-over-empty-lines ()
  25814. "Move backwards over witespace, to the beginning of the first empty line.
  25815. Returns the number o empty lines passed."
  25816. (let ((pos (point)))
  25817. (skip-chars-backward " \t\n\r")
  25818. (beginning-of-line 2)
  25819. (goto-char (min (point) pos))
  25820. (count-lines (point) pos)))
  25821. (defun org-skip-whitespace ()
  25822. (skip-chars-forward " \t\n\r"))
  25823. (defun org-point-in-group (point group &optional context)
  25824. "Check if POINT is in match-group GROUP.
  25825. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25826. match. If the match group does ot exist or point is not inside it,
  25827. return nil."
  25828. (and (match-beginning group)
  25829. (>= point (match-beginning group))
  25830. (<= point (match-end group))
  25831. (if context
  25832. (list context (match-beginning group) (match-end group))
  25833. t)))
  25834. (defun org-switch-to-buffer-other-window (&rest args)
  25835. "Switch to buffer in a second window on the current frame.
  25836. In particular, do not allow pop-up frames."
  25837. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25838. special-display-function)
  25839. (apply 'switch-to-buffer-other-window args)))
  25840. (defun org-combine-plists (&rest plists)
  25841. "Create a single property list from all plists in PLISTS.
  25842. The process starts by copying the first list, and then setting properties
  25843. from the other lists. Settings in the last list are the most significant
  25844. ones and overrule settings in the other lists."
  25845. (let ((rtn (copy-sequence (pop plists)))
  25846. p v ls)
  25847. (while plists
  25848. (setq ls (pop plists))
  25849. (while ls
  25850. (setq p (pop ls) v (pop ls))
  25851. (setq rtn (plist-put rtn p v))))
  25852. rtn))
  25853. (defun org-move-line-down (arg)
  25854. "Move the current line down. With prefix argument, move it past ARG lines."
  25855. (interactive "p")
  25856. (let ((col (current-column))
  25857. beg end pos)
  25858. (beginning-of-line 1) (setq beg (point))
  25859. (beginning-of-line 2) (setq end (point))
  25860. (beginning-of-line (+ 1 arg))
  25861. (setq pos (move-marker (make-marker) (point)))
  25862. (insert (delete-and-extract-region beg end))
  25863. (goto-char pos)
  25864. (move-to-column col)))
  25865. (defun org-move-line-up (arg)
  25866. "Move the current line up. With prefix argument, move it past ARG lines."
  25867. (interactive "p")
  25868. (let ((col (current-column))
  25869. beg end pos)
  25870. (beginning-of-line 1) (setq beg (point))
  25871. (beginning-of-line 2) (setq end (point))
  25872. (beginning-of-line (- arg))
  25873. (setq pos (move-marker (make-marker) (point)))
  25874. (insert (delete-and-extract-region beg end))
  25875. (goto-char pos)
  25876. (move-to-column col)))
  25877. (defun org-replace-escapes (string table)
  25878. "Replace %-escapes in STRING with values in TABLE.
  25879. TABLE is an association list with keys like \"%a\" and string values.
  25880. The sequences in STRING may contain normal field width and padding information,
  25881. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25882. so values can contain further %-escapes if they are define later in TABLE."
  25883. (let ((case-fold-search nil)
  25884. e re rpl)
  25885. (while (setq e (pop table))
  25886. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25887. (while (string-match re string)
  25888. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25889. (cdr e)))
  25890. (setq string (replace-match rpl t t string))))
  25891. string))
  25892. (defun org-sublist (list start end)
  25893. "Return a section of LIST, from START to END.
  25894. Counting starts at 1."
  25895. (let (rtn (c start))
  25896. (setq list (nthcdr (1- start) list))
  25897. (while (and list (<= c end))
  25898. (push (pop list) rtn)
  25899. (setq c (1+ c)))
  25900. (nreverse rtn)))
  25901. (defun org-find-base-buffer-visiting (file)
  25902. "Like `find-buffer-visiting' but alway return the base buffer and
  25903. not an indirect buffer"
  25904. (let ((buf (find-buffer-visiting file)))
  25905. (if buf
  25906. (or (buffer-base-buffer buf) buf)
  25907. nil)))
  25908. (defun org-image-file-name-regexp ()
  25909. "Return regexp matching the file names of images."
  25910. (if (fboundp 'image-file-name-regexp)
  25911. (image-file-name-regexp)
  25912. (let ((image-file-name-extensions
  25913. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25914. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25915. (concat "\\."
  25916. (regexp-opt (nconc (mapcar 'upcase
  25917. image-file-name-extensions)
  25918. image-file-name-extensions)
  25919. t)
  25920. "\\'"))))
  25921. (defun org-file-image-p (file)
  25922. "Return non-nil if FILE is an image."
  25923. (save-match-data
  25924. (string-match (org-image-file-name-regexp) file)))
  25925. ;;; Paragraph filling stuff.
  25926. ;; We want this to be just right, so use the full arsenal.
  25927. (defun org-indent-line-function ()
  25928. "Indent line like previous, but further if previous was headline or item."
  25929. (interactive)
  25930. (let* ((pos (point))
  25931. (itemp (org-at-item-p))
  25932. column bpos bcol tpos tcol bullet btype bullet-type)
  25933. ;; Find the previous relevant line
  25934. (beginning-of-line 1)
  25935. (cond
  25936. ((looking-at "#") (setq column 0))
  25937. ((looking-at "\\*+ ") (setq column 0))
  25938. (t
  25939. (beginning-of-line 0)
  25940. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25941. (beginning-of-line 0))
  25942. (cond
  25943. ((looking-at "\\*+[ \t]+")
  25944. (goto-char (match-end 0))
  25945. (setq column (current-column)))
  25946. ((org-in-item-p)
  25947. (org-beginning-of-item)
  25948. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25949. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  25950. (setq bpos (match-beginning 1) tpos (match-end 0)
  25951. bcol (progn (goto-char bpos) (current-column))
  25952. tcol (progn (goto-char tpos) (current-column))
  25953. bullet (match-string 1)
  25954. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  25955. (if (not itemp)
  25956. (setq column tcol)
  25957. (goto-char pos)
  25958. (beginning-of-line 1)
  25959. (if (looking-at "\\S-")
  25960. (progn
  25961. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25962. (setq bullet (match-string 1)
  25963. btype (if (string-match "[0-9]" bullet) "n" bullet))
  25964. (setq column (if (equal btype bullet-type) bcol tcol)))
  25965. (setq column (org-get-indentation)))))
  25966. (t (setq column (org-get-indentation))))))
  25967. (goto-char pos)
  25968. (if (<= (current-column) (current-indentation))
  25969. (indent-line-to column)
  25970. (save-excursion (indent-line-to column)))
  25971. (setq column (current-column))
  25972. (beginning-of-line 1)
  25973. (if (looking-at
  25974. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  25975. (replace-match (concat "\\1" (format org-property-format
  25976. (match-string 2) (match-string 3)))
  25977. t nil))
  25978. (move-to-column column)))
  25979. (defun org-set-autofill-regexps ()
  25980. (interactive)
  25981. ;; In the paragraph separator we include headlines, because filling
  25982. ;; text in a line directly attached to a headline would otherwise
  25983. ;; fill the headline as well.
  25984. (org-set-local 'comment-start-skip "^#+[ \t]*")
  25985. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  25986. ;; The paragraph starter includes hand-formatted lists.
  25987. (org-set-local 'paragraph-start
  25988. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  25989. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  25990. ;; But only if the user has not turned off tables or fixed-width regions
  25991. (org-set-local
  25992. 'auto-fill-inhibit-regexp
  25993. (concat "\\*+ \\|#\\+"
  25994. "\\|[ \t]*" org-keyword-time-regexp
  25995. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  25996. (concat
  25997. "\\|[ \t]*["
  25998. (if org-enable-table-editor "|" "")
  25999. (if org-enable-fixed-width-editor ":" "")
  26000. "]"))))
  26001. ;; We use our own fill-paragraph function, to make sure that tables
  26002. ;; and fixed-width regions are not wrapped. That function will pass
  26003. ;; through to `fill-paragraph' when appropriate.
  26004. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  26005. ; Adaptive filling: To get full control, first make sure that
  26006. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  26007. (org-set-local 'adaptive-fill-regexp "\000")
  26008. (org-set-local 'adaptive-fill-function
  26009. 'org-adaptive-fill-function)
  26010. (org-set-local
  26011. 'align-mode-rules-list
  26012. '((org-in-buffer-settings
  26013. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  26014. (modes . '(org-mode))))))
  26015. (defun org-fill-paragraph (&optional justify)
  26016. "Re-align a table, pass through to fill-paragraph if no table."
  26017. (let ((table-p (org-at-table-p))
  26018. (table.el-p (org-at-table.el-p)))
  26019. (cond ((and (equal (char-after (point-at-bol)) ?*)
  26020. (save-excursion (goto-char (point-at-bol))
  26021. (looking-at outline-regexp)))
  26022. t) ; skip headlines
  26023. (table.el-p t) ; skip table.el tables
  26024. (table-p (org-table-align) t) ; align org-mode tables
  26025. (t nil)))) ; call paragraph-fill
  26026. ;; For reference, this is the default value of adaptive-fill-regexp
  26027. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  26028. (defun org-adaptive-fill-function ()
  26029. "Return a fill prefix for org-mode files.
  26030. In particular, this makes sure hanging paragraphs for hand-formatted lists
  26031. work correctly."
  26032. (cond ((looking-at "#[ \t]+")
  26033. (match-string 0))
  26034. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  26035. (save-excursion
  26036. (goto-char (match-end 0))
  26037. (make-string (current-column) ?\ )))
  26038. (t nil)))
  26039. ;;;; Functions extending outline functionality
  26040. (defun org-beginning-of-line (&optional arg)
  26041. "Go to the beginning of the current line. If that is invisible, continue
  26042. to a visible line beginning. This makes the function of C-a more intuitive.
  26043. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  26044. first attempt, and only move to after the tags when the cursor is already
  26045. beyond the end of the headline."
  26046. (interactive "P")
  26047. (let ((pos (point)))
  26048. (beginning-of-line 1)
  26049. (if (bobp)
  26050. nil
  26051. (backward-char 1)
  26052. (if (org-invisible-p)
  26053. (while (and (not (bobp)) (org-invisible-p))
  26054. (backward-char 1)
  26055. (beginning-of-line 1))
  26056. (forward-char 1)))
  26057. (when org-special-ctrl-a/e
  26058. (cond
  26059. ((and (looking-at org-todo-line-regexp)
  26060. (= (char-after (match-end 1)) ?\ ))
  26061. (goto-char
  26062. (if (eq org-special-ctrl-a/e t)
  26063. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  26064. ((= pos (point)) (match-beginning 3))
  26065. (t (point)))
  26066. (cond ((> pos (point)) (point))
  26067. ((not (eq last-command this-command)) (point))
  26068. (t (match-beginning 3))))))
  26069. ((org-at-item-p)
  26070. (goto-char
  26071. (if (eq org-special-ctrl-a/e t)
  26072. (cond ((> pos (match-end 4)) (match-end 4))
  26073. ((= pos (point)) (match-end 4))
  26074. (t (point)))
  26075. (cond ((> pos (point)) (point))
  26076. ((not (eq last-command this-command)) (point))
  26077. (t (match-end 4))))))))))
  26078. (defun org-end-of-line (&optional arg)
  26079. "Go to the end of the line.
  26080. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  26081. first attempt, and only move to after the tags when the cursor is already
  26082. beyond the end of the headline."
  26083. (interactive "P")
  26084. (if (or (not org-special-ctrl-a/e)
  26085. (not (org-on-heading-p)))
  26086. (end-of-line arg)
  26087. (let ((pos (point)))
  26088. (beginning-of-line 1)
  26089. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  26090. (if (eq org-special-ctrl-a/e t)
  26091. (if (or (< pos (match-beginning 1))
  26092. (= pos (match-end 0)))
  26093. (goto-char (match-beginning 1))
  26094. (goto-char (match-end 0)))
  26095. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  26096. (goto-char (match-end 0))
  26097. (goto-char (match-beginning 1))))
  26098. (end-of-line arg)))))
  26099. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  26100. (define-key org-mode-map "\C-e" 'org-end-of-line)
  26101. (defun org-kill-line (&optional arg)
  26102. "Kill line, to tags or end of line."
  26103. (interactive "P")
  26104. (cond
  26105. ((or (not org-special-ctrl-k)
  26106. (bolp)
  26107. (not (org-on-heading-p)))
  26108. (call-interactively 'kill-line))
  26109. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  26110. (kill-region (point) (match-beginning 1))
  26111. (org-set-tags nil t))
  26112. (t (kill-region (point) (point-at-eol)))))
  26113. (define-key org-mode-map "\C-k" 'org-kill-line)
  26114. (defun org-invisible-p ()
  26115. "Check if point is at a character currently not visible."
  26116. ;; Early versions of noutline don't have `outline-invisible-p'.
  26117. (if (fboundp 'outline-invisible-p)
  26118. (outline-invisible-p)
  26119. (get-char-property (point) 'invisible)))
  26120. (defun org-invisible-p2 ()
  26121. "Check if point is at a character currently not visible."
  26122. (save-excursion
  26123. (if (and (eolp) (not (bobp))) (backward-char 1))
  26124. ;; Early versions of noutline don't have `outline-invisible-p'.
  26125. (if (fboundp 'outline-invisible-p)
  26126. (outline-invisible-p)
  26127. (get-char-property (point) 'invisible))))
  26128. (defalias 'org-back-to-heading 'outline-back-to-heading)
  26129. (defalias 'org-on-heading-p 'outline-on-heading-p)
  26130. (defalias 'org-at-heading-p 'outline-on-heading-p)
  26131. (defun org-at-heading-or-item-p ()
  26132. (or (org-on-heading-p) (org-at-item-p)))
  26133. (defun org-on-target-p ()
  26134. (or (org-in-regexp org-radio-target-regexp)
  26135. (org-in-regexp org-target-regexp)))
  26136. (defun org-up-heading-all (arg)
  26137. "Move to the heading line of which the present line is a subheading.
  26138. This function considers both visible and invisible heading lines.
  26139. With argument, move up ARG levels."
  26140. (if (fboundp 'outline-up-heading-all)
  26141. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  26142. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  26143. (defun org-up-heading-safe ()
  26144. "Move to the heading line of which the present line is a subheading.
  26145. This version will not throw an error. It will return the level of the
  26146. headline found, or nil if no higher level is found."
  26147. (let ((pos (point)) start-level level
  26148. (re (concat "^" outline-regexp)))
  26149. (catch 'exit
  26150. (outline-back-to-heading t)
  26151. (setq start-level (funcall outline-level))
  26152. (if (equal start-level 1) (throw 'exit nil))
  26153. (while (re-search-backward re nil t)
  26154. (setq level (funcall outline-level))
  26155. (if (< level start-level) (throw 'exit level)))
  26156. nil)))
  26157. (defun org-first-sibling-p ()
  26158. "Is this heading the first child of its parents?"
  26159. (interactive)
  26160. (let ((re (concat "^" outline-regexp))
  26161. level l)
  26162. (unless (org-at-heading-p t)
  26163. (error "Not at a heading"))
  26164. (setq level (funcall outline-level))
  26165. (save-excursion
  26166. (if (not (re-search-backward re nil t))
  26167. t
  26168. (setq l (funcall outline-level))
  26169. (< l level)))))
  26170. (defun org-goto-sibling (&optional previous)
  26171. "Goto the next sibling, even if it is invisible.
  26172. When PREVIOUS is set, go to the previous sibling instead. Returns t
  26173. when a sibling was found. When none is found, return nil and don't
  26174. move point."
  26175. (let ((fun (if previous 're-search-backward 're-search-forward))
  26176. (pos (point))
  26177. (re (concat "^" outline-regexp))
  26178. level l)
  26179. (when (condition-case nil (org-back-to-heading t) (error nil))
  26180. (setq level (funcall outline-level))
  26181. (catch 'exit
  26182. (or previous (forward-char 1))
  26183. (while (funcall fun re nil t)
  26184. (setq l (funcall outline-level))
  26185. (when (< l level) (goto-char pos) (throw 'exit nil))
  26186. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  26187. (goto-char pos)
  26188. nil))))
  26189. (defun org-show-siblings ()
  26190. "Show all siblings of the current headline."
  26191. (save-excursion
  26192. (while (org-goto-sibling) (org-flag-heading nil)))
  26193. (save-excursion
  26194. (while (org-goto-sibling 'previous)
  26195. (org-flag-heading nil))))
  26196. (defun org-show-hidden-entry ()
  26197. "Show an entry where even the heading is hidden."
  26198. (save-excursion
  26199. (org-show-entry)))
  26200. (defun org-flag-heading (flag &optional entry)
  26201. "Flag the current heading. FLAG non-nil means make invisible.
  26202. When ENTRY is non-nil, show the entire entry."
  26203. (save-excursion
  26204. (org-back-to-heading t)
  26205. ;; Check if we should show the entire entry
  26206. (if entry
  26207. (progn
  26208. (org-show-entry)
  26209. (save-excursion
  26210. (and (outline-next-heading)
  26211. (org-flag-heading nil))))
  26212. (outline-flag-region (max (point-min) (1- (point)))
  26213. (save-excursion (outline-end-of-heading) (point))
  26214. flag))))
  26215. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  26216. ;; This is an exact copy of the original function, but it uses
  26217. ;; `org-back-to-heading', to make it work also in invisible
  26218. ;; trees. And is uses an invisible-OK argument.
  26219. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  26220. (org-back-to-heading invisible-OK)
  26221. (let ((first t)
  26222. (level (funcall outline-level)))
  26223. (while (and (not (eobp))
  26224. (or first (> (funcall outline-level) level)))
  26225. (setq first nil)
  26226. (outline-next-heading))
  26227. (unless to-heading
  26228. (if (memq (preceding-char) '(?\n ?\^M))
  26229. (progn
  26230. ;; Go to end of line before heading
  26231. (forward-char -1)
  26232. (if (memq (preceding-char) '(?\n ?\^M))
  26233. ;; leave blank line before heading
  26234. (forward-char -1))))))
  26235. (point))
  26236. (defun org-show-subtree ()
  26237. "Show everything after this heading at deeper levels."
  26238. (outline-flag-region
  26239. (point)
  26240. (save-excursion
  26241. (outline-end-of-subtree) (outline-next-heading) (point))
  26242. nil))
  26243. (defun org-show-entry ()
  26244. "Show the body directly following this heading.
  26245. Show the heading too, if it is currently invisible."
  26246. (interactive)
  26247. (save-excursion
  26248. (condition-case nil
  26249. (progn
  26250. (org-back-to-heading t)
  26251. (outline-flag-region
  26252. (max (point-min) (1- (point)))
  26253. (save-excursion
  26254. (re-search-forward
  26255. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  26256. (or (match-beginning 1) (point-max)))
  26257. nil))
  26258. (error nil))))
  26259. (defun org-make-options-regexp (kwds)
  26260. "Make a regular expression for keyword lines."
  26261. (concat
  26262. "^"
  26263. "#?[ \t]*\\+\\("
  26264. (mapconcat 'regexp-quote kwds "\\|")
  26265. "\\):[ \t]*"
  26266. "\\(.+\\)"))
  26267. ;; Make isearch reveal the necessary context
  26268. (defun org-isearch-end ()
  26269. "Reveal context after isearch exits."
  26270. (when isearch-success ; only if search was successful
  26271. (if (featurep 'xemacs)
  26272. ;; Under XEmacs, the hook is run in the correct place,
  26273. ;; we directly show the context.
  26274. (org-show-context 'isearch)
  26275. ;; In Emacs the hook runs *before* restoring the overlays.
  26276. ;; So we have to use a one-time post-command-hook to do this.
  26277. ;; (Emacs 22 has a special variable, see function `org-mode')
  26278. (unless (and (boundp 'isearch-mode-end-hook-quit)
  26279. isearch-mode-end-hook-quit)
  26280. ;; Only when the isearch was not quitted.
  26281. (org-add-hook 'post-command-hook 'org-isearch-post-command
  26282. 'append 'local)))))
  26283. (defun org-isearch-post-command ()
  26284. "Remove self from hook, and show context."
  26285. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  26286. (org-show-context 'isearch))
  26287. ;;;; Integration with and fixes for other packages
  26288. ;;; Imenu support
  26289. (defvar org-imenu-markers nil
  26290. "All markers currently used by Imenu.")
  26291. (make-variable-buffer-local 'org-imenu-markers)
  26292. (defun org-imenu-new-marker (&optional pos)
  26293. "Return a new marker for use by Imenu, and remember the marker."
  26294. (let ((m (make-marker)))
  26295. (move-marker m (or pos (point)))
  26296. (push m org-imenu-markers)
  26297. m))
  26298. (defun org-imenu-get-tree ()
  26299. "Produce the index for Imenu."
  26300. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  26301. (setq org-imenu-markers nil)
  26302. (let* ((n org-imenu-depth)
  26303. (re (concat "^" outline-regexp))
  26304. (subs (make-vector (1+ n) nil))
  26305. (last-level 0)
  26306. m tree level head)
  26307. (save-excursion
  26308. (save-restriction
  26309. (widen)
  26310. (goto-char (point-max))
  26311. (while (re-search-backward re nil t)
  26312. (setq level (org-reduced-level (funcall outline-level)))
  26313. (when (<= level n)
  26314. (looking-at org-complex-heading-regexp)
  26315. (setq head (org-match-string-no-properties 4)
  26316. m (org-imenu-new-marker))
  26317. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  26318. (if (>= level last-level)
  26319. (push (cons head m) (aref subs level))
  26320. (push (cons head (aref subs (1+ level))) (aref subs level))
  26321. (loop for i from (1+ level) to n do (aset subs i nil)))
  26322. (setq last-level level)))))
  26323. (aref subs 1)))
  26324. (eval-after-load "imenu"
  26325. '(progn
  26326. (add-hook 'imenu-after-jump-hook
  26327. (lambda () (org-show-context 'org-goto)))))
  26328. ;; Speedbar support
  26329. (defun org-speedbar-set-agenda-restriction ()
  26330. "Restrict future agenda commands to the location at point in speedbar.
  26331. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  26332. (interactive)
  26333. (let (p m tp np dir txt w)
  26334. (cond
  26335. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26336. 'org-imenu t))
  26337. (setq m (get-text-property p 'org-imenu-marker))
  26338. (save-excursion
  26339. (save-restriction
  26340. (set-buffer (marker-buffer m))
  26341. (goto-char m)
  26342. (org-agenda-set-restriction-lock 'subtree))))
  26343. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26344. 'speedbar-function 'speedbar-find-file))
  26345. (setq tp (previous-single-property-change
  26346. (1+ p) 'speedbar-function)
  26347. np (next-single-property-change
  26348. tp 'speedbar-function)
  26349. dir (speedbar-line-directory)
  26350. txt (buffer-substring-no-properties (or tp (point-min))
  26351. (or np (point-max))))
  26352. (save-excursion
  26353. (save-restriction
  26354. (set-buffer (find-file-noselect
  26355. (let ((default-directory dir))
  26356. (expand-file-name txt))))
  26357. (unless (org-mode-p)
  26358. (error "Cannot restrict to non-Org-mode file"))
  26359. (org-agenda-set-restriction-lock 'file))))
  26360. (t (error "Don't know how to restrict Org-mode's agenda")))
  26361. (org-move-overlay org-speedbar-restriction-lock-overlay
  26362. (point-at-bol) (point-at-eol))
  26363. (setq current-prefix-arg nil)
  26364. (org-agenda-maybe-redo)))
  26365. (eval-after-load "speedbar"
  26366. '(progn
  26367. (speedbar-add-supported-extension ".org")
  26368. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  26369. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  26370. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  26371. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  26372. (add-hook 'speedbar-visiting-tag-hook
  26373. (lambda () (org-show-context 'org-goto)))))
  26374. ;;; Fixes and Hacks
  26375. ;; Make flyspell not check words in links, to not mess up our keymap
  26376. (defun org-mode-flyspell-verify ()
  26377. "Don't let flyspell put overlays at active buttons."
  26378. (not (get-text-property (point) 'keymap)))
  26379. ;; Make `bookmark-jump' show the jump location if it was hidden.
  26380. (eval-after-load "bookmark"
  26381. '(if (boundp 'bookmark-after-jump-hook)
  26382. ;; We can use the hook
  26383. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  26384. ;; Hook not available, use advice
  26385. (defadvice bookmark-jump (after org-make-visible activate)
  26386. "Make the position visible."
  26387. (org-bookmark-jump-unhide))))
  26388. (defun org-bookmark-jump-unhide ()
  26389. "Unhide the current position, to show the bookmark location."
  26390. (and (org-mode-p)
  26391. (or (org-invisible-p)
  26392. (save-excursion (goto-char (max (point-min) (1- (point))))
  26393. (org-invisible-p)))
  26394. (org-show-context 'bookmark-jump)))
  26395. ;; Fix a bug in htmlize where there are text properties (face nil)
  26396. (eval-after-load "htmlize"
  26397. '(progn
  26398. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  26399. "Make sure there are no nil faces"
  26400. (setq ad-return-value (delq nil ad-return-value)))))
  26401. ;; Make session.el ignore our circular variable
  26402. (eval-after-load "session"
  26403. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26404. ;;;; Experimental code
  26405. (defun org-closed-in-range ()
  26406. "Sparse tree of items closed in a certain time range.
  26407. Still experimental, may disappear in the future."
  26408. (interactive)
  26409. ;; Get the time interval from the user.
  26410. (let* ((time1 (time-to-seconds
  26411. (org-read-date nil 'to-time nil "Starting date: ")))
  26412. (time2 (time-to-seconds
  26413. (org-read-date nil 'to-time nil "End date:")))
  26414. ;; callback function
  26415. (callback (lambda ()
  26416. (let ((time
  26417. (time-to-seconds
  26418. (apply 'encode-time
  26419. (org-parse-time-string
  26420. (match-string 1))))))
  26421. ;; check if time in interval
  26422. (and (>= time time1) (<= time time2))))))
  26423. ;; make tree, check each match with the callback
  26424. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26425. ;;;; Finish up
  26426. (provide 'org)
  26427. (run-hooks 'org-load-hook)
  26428. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26429. ;;; org.el ends here