org.el 1.0 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585
  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. (defun org-load-default-extensions ()
  156. "Load all extensions that are listed in `org-default-extensions'."
  157. (mapc 'require org-default-extensions))
  158. ;; FIXME: Needs a separate group...
  159. (defcustom org-completion-fallback-command 'hippie-expand
  160. "The expansion command called by \\[org-complete] in normal context.
  161. Normal means, no org-mode-specific context."
  162. :group 'org
  163. :type 'function)
  164. (defgroup org-startup nil
  165. "Options concerning startup of Org-mode."
  166. :tag "Org Startup"
  167. :group 'org)
  168. (defcustom org-startup-folded t
  169. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  170. This can also be configured on a per-file basis by adding one of
  171. the following lines anywhere in the buffer:
  172. #+STARTUP: fold
  173. #+STARTUP: nofold
  174. #+STARTUP: content"
  175. :group 'org-startup
  176. :type '(choice
  177. (const :tag "nofold: show all" nil)
  178. (const :tag "fold: overview" t)
  179. (const :tag "content: all headlines" content)))
  180. (defcustom org-startup-truncated t
  181. "Non-nil means, entering Org-mode will set `truncate-lines'.
  182. This is useful since some lines containing links can be very long and
  183. uninteresting. Also tables look terrible when wrapped."
  184. :group 'org-startup
  185. :type 'boolean)
  186. (defcustom org-startup-align-all-tables nil
  187. "Non-nil means, align all tables when visiting a file.
  188. This is useful when the column width in tables is forced with <N> cookies
  189. in table fields. Such tables will look correct only after the first re-align.
  190. This can also be configured on a per-file basis by adding one of
  191. the following lines anywhere in the buffer:
  192. #+STARTUP: align
  193. #+STARTUP: noalign"
  194. :group 'org-startup
  195. :type 'boolean)
  196. (defcustom org-insert-mode-line-in-empty-file nil
  197. "Non-nil means insert the first line setting Org-mode in empty files.
  198. When the function `org-mode' is called interactively in an empty file, this
  199. normally means that the file name does not automatically trigger Org-mode.
  200. To ensure that the file will always be in Org-mode in the future, a
  201. line enforcing Org-mode will be inserted into the buffer, if this option
  202. has been set."
  203. :group 'org-startup
  204. :type 'boolean)
  205. (defcustom org-replace-disputed-keys nil
  206. "Non-nil means use alternative key bindings for some keys.
  207. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  208. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  209. If you want to use Org-mode together with one of these other modes,
  210. or more generally if you would like to move some Org-mode commands to
  211. other keys, set this variable and configure the keys with the variable
  212. `org-disputed-keys'.
  213. This option is only relevant at load-time of Org-mode, and must be set
  214. *before* org.el is loaded. Changing it requires a restart of Emacs to
  215. become effective."
  216. :group 'org-startup
  217. :type 'boolean)
  218. (if (fboundp 'defvaralias)
  219. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  220. (defcustom org-disputed-keys
  221. '(([(shift up)] . [(meta p)])
  222. ([(shift down)] . [(meta n)])
  223. ([(shift left)] . [(meta -)])
  224. ([(shift right)] . [(meta +)])
  225. ([(control shift right)] . [(meta shift +)])
  226. ([(control shift left)] . [(meta shift -)]))
  227. "Keys for which Org-mode and other modes compete.
  228. This is an alist, cars are the default keys, second element specifies
  229. the alternative to use when `org-replace-disputed-keys' is t.
  230. Keys can be specified in any syntax supported by `define-key'.
  231. The value of this option takes effect only at Org-mode's startup,
  232. therefore you'll have to restart Emacs to apply it after changing."
  233. :group 'org-startup
  234. :type 'alist)
  235. (defun org-key (key)
  236. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  237. Or return the original if not disputed."
  238. (if org-replace-disputed-keys
  239. (let* ((nkey (key-description key))
  240. (x (org-find-if (lambda (x)
  241. (equal (key-description (car x)) nkey))
  242. org-disputed-keys)))
  243. (if x (cdr x) key))
  244. key))
  245. (defun org-find-if (predicate seq)
  246. (catch 'exit
  247. (while seq
  248. (if (funcall predicate (car seq))
  249. (throw 'exit (car seq))
  250. (pop seq)))))
  251. (defun org-defkey (keymap key def)
  252. "Define a key, possibly translated, as returned by `org-key'."
  253. (define-key keymap (org-key key) def))
  254. (defcustom org-ellipsis nil
  255. "The ellipsis to use in the Org-mode outline.
  256. When nil, just use the standard three dots. When a string, use that instead,
  257. When a face, use the standart 3 dots, but with the specified face.
  258. The change affects only Org-mode (which will then use its own display table).
  259. Changing this requires executing `M-x org-mode' in a buffer to become
  260. effective."
  261. :group 'org-startup
  262. :type '(choice (const :tag "Default" nil)
  263. (face :tag "Face" :value org-warning)
  264. (string :tag "String" :value "...#")))
  265. (defvar org-display-table nil
  266. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  267. (defgroup org-keywords nil
  268. "Keywords in Org-mode."
  269. :tag "Org Keywords"
  270. :group 'org)
  271. (defcustom org-deadline-string "DEADLINE:"
  272. "String to mark deadline entries.
  273. A deadline is this string, followed by a time stamp. Should be a word,
  274. terminated by a colon. You can insert a schedule keyword and
  275. a timestamp with \\[org-deadline].
  276. Changes become only effective after restarting Emacs."
  277. :group 'org-keywords
  278. :type 'string)
  279. (defcustom org-scheduled-string "SCHEDULED:"
  280. "String to mark scheduled TODO entries.
  281. A schedule is this string, followed by a time stamp. Should be a word,
  282. terminated by a colon. You can insert a schedule keyword and
  283. a timestamp with \\[org-schedule].
  284. Changes become only effective after restarting Emacs."
  285. :group 'org-keywords
  286. :type 'string)
  287. (defcustom org-closed-string "CLOSED:"
  288. "String used as the prefix for timestamps logging closing a TODO entry."
  289. :group 'org-keywords
  290. :type 'string)
  291. (defcustom org-clock-string "CLOCK:"
  292. "String used as prefix for timestamps clocking work hours on an item."
  293. :group 'org-keywords
  294. :type 'string)
  295. (defcustom org-comment-string "COMMENT"
  296. "Entries starting with this keyword will never be exported.
  297. An entry can be toggled between COMMENT and normal with
  298. \\[org-toggle-comment].
  299. Changes become only effective after restarting Emacs."
  300. :group 'org-keywords
  301. :type 'string)
  302. (defcustom org-quote-string "QUOTE"
  303. "Entries starting with this keyword will be exported in fixed-width font.
  304. Quoting applies only to the text in the entry following the headline, and does
  305. not extend beyond the next headline, even if that is lower level.
  306. An entry can be toggled between QUOTE and normal with
  307. \\[org-toggle-fixed-width-section]."
  308. :group 'org-keywords
  309. :type 'string)
  310. (defconst org-repeat-re
  311. ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  312. ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  313. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
  314. "Regular expression for specifying repeated events.
  315. After a match, group 1 contains the repeat expression.")
  316. (defgroup org-structure nil
  317. "Options concerning the general structure of Org-mode files."
  318. :tag "Org Structure"
  319. :group 'org)
  320. (defgroup org-reveal-location nil
  321. "Options about how to make context of a location visible."
  322. :tag "Org Reveal Location"
  323. :group 'org-structure)
  324. (defconst org-context-choice
  325. '(choice
  326. (const :tag "Always" t)
  327. (const :tag "Never" nil)
  328. (repeat :greedy t :tag "Individual contexts"
  329. (cons
  330. (choice :tag "Context"
  331. (const agenda)
  332. (const org-goto)
  333. (const occur-tree)
  334. (const tags-tree)
  335. (const link-search)
  336. (const mark-goto)
  337. (const bookmark-jump)
  338. (const isearch)
  339. (const default))
  340. (boolean))))
  341. "Contexts for the reveal options.")
  342. (defcustom org-show-hierarchy-above '((default . t))
  343. "Non-nil means, show full hierarchy when revealing a location.
  344. Org-mode often shows locations in an org-mode file which might have
  345. been invisible before. When this is set, the hierarchy of headings
  346. above the exposed location is shown.
  347. Turning this off for example for sparse trees makes them very compact.
  348. Instead of t, this can also be an alist specifying this option for different
  349. contexts. Valid contexts are
  350. agenda when exposing an entry from the agenda
  351. org-goto when using the command `org-goto' on key C-c C-j
  352. occur-tree when using the command `org-occur' on key C-c /
  353. tags-tree when constructing a sparse tree based on tags matches
  354. link-search when exposing search matches associated with a link
  355. mark-goto when exposing the jump goal of a mark
  356. bookmark-jump when exposing a bookmark location
  357. isearch when exiting from an incremental search
  358. default default for all contexts not set explicitly"
  359. :group 'org-reveal-location
  360. :type org-context-choice)
  361. (defcustom org-show-following-heading '((default . nil))
  362. "Non-nil means, show following heading when revealing a location.
  363. Org-mode often shows locations in an org-mode file which might have
  364. been invisible before. When this is set, the heading following the
  365. match is shown.
  366. Turning this off for example for sparse trees makes them very compact,
  367. but makes it harder to edit the location of the match. In such a case,
  368. use the command \\[org-reveal] to show more context.
  369. Instead of t, this can also be an alist specifying this option for different
  370. contexts. See `org-show-hierarchy-above' for valid contexts."
  371. :group 'org-reveal-location
  372. :type org-context-choice)
  373. (defcustom org-show-siblings '((default . nil) (isearch t))
  374. "Non-nil means, show all sibling heading when revealing a location.
  375. Org-mode often shows locations in an org-mode file which might have
  376. been invisible before. When this is set, the sibling of the current entry
  377. heading are all made visible. If `org-show-hierarchy-above' is t,
  378. the same happens on each level of the hierarchy above the current entry.
  379. By default this is on for the isearch context, off for all other contexts.
  380. Turning this off for example for sparse trees makes them very compact,
  381. but makes it harder to edit the location of the match. In such a case,
  382. use the command \\[org-reveal] to show more context.
  383. Instead of t, this can also be an alist specifying this option for different
  384. contexts. See `org-show-hierarchy-above' for valid contexts."
  385. :group 'org-reveal-location
  386. :type org-context-choice)
  387. (defcustom org-show-entry-below '((default . nil))
  388. "Non-nil means, show the entry below a headline when revealing a location.
  389. Org-mode often shows locations in an org-mode file which might have
  390. been invisible before. When this is set, the text below the headline that is
  391. exposed is also shown.
  392. By default this is off for all contexts.
  393. Instead of t, this can also be an alist specifying this option for different
  394. contexts. See `org-show-hierarchy-above' for valid contexts."
  395. :group 'org-reveal-location
  396. :type org-context-choice)
  397. (defgroup org-cycle nil
  398. "Options concerning visibility cycling in Org-mode."
  399. :tag "Org Cycle"
  400. :group 'org-structure)
  401. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  402. "Names of drawers. Drawers are not opened by cycling on the headline above.
  403. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  404. this:
  405. :DRAWERNAME:
  406. .....
  407. :END:
  408. The drawer \"PROPERTIES\" is special for capturing properties through
  409. the property API.
  410. Drawers can be defined on the per-file basis with a line like:
  411. #+DRAWERS: HIDDEN STATE PROPERTIES"
  412. :group 'org-structure
  413. :type '(repeat (string :tag "Drawer Name")))
  414. (defcustom org-cycle-global-at-bob nil
  415. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  416. This makes it possible to do global cycling without having to use S-TAB or
  417. C-u TAB. For this special case to work, the first line of the buffer
  418. must not be a headline - it may be empty ot some other text. When used in
  419. this way, `org-cycle-hook' is disables temporarily, to make sure the
  420. cursor stays at the beginning of the buffer.
  421. When this option is nil, don't do anything special at the beginning
  422. of the buffer."
  423. :group 'org-cycle
  424. :type 'boolean)
  425. (defcustom org-cycle-emulate-tab t
  426. "Where should `org-cycle' emulate TAB.
  427. nil Never
  428. white Only in completely white lines
  429. whitestart Only at the beginning of lines, before the first non-white char
  430. t Everywhere except in headlines
  431. exc-hl-bol Everywhere except at the start of a headline
  432. If TAB is used in a place where it does not emulate TAB, the current subtree
  433. visibility is cycled."
  434. :group 'org-cycle
  435. :type '(choice (const :tag "Never" nil)
  436. (const :tag "Only in completely white lines" white)
  437. (const :tag "Before first char in a line" whitestart)
  438. (const :tag "Everywhere except in headlines" t)
  439. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  440. ))
  441. (defcustom org-cycle-separator-lines 2
  442. "Number of empty lines needed to keep an empty line between collapsed trees.
  443. If you leave an empty line between the end of a subtree and the following
  444. headline, this empty line is hidden when the subtree is folded.
  445. Org-mode will leave (exactly) one empty line visible if the number of
  446. empty lines is equal or larger to the number given in this variable.
  447. So the default 2 means, at least 2 empty lines after the end of a subtree
  448. are needed to produce free space between a collapsed subtree and the
  449. following headline.
  450. Special case: when 0, never leave empty lines in collapsed view."
  451. :group 'org-cycle
  452. :type 'integer)
  453. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  454. org-cycle-hide-drawers
  455. org-cycle-show-empty-lines
  456. org-optimize-window-after-visibility-change)
  457. "Hook that is run after `org-cycle' has changed the buffer visibility.
  458. The function(s) in this hook must accept a single argument which indicates
  459. the new state that was set by the most recent `org-cycle' command. The
  460. argument is a symbol. After a global state change, it can have the values
  461. `overview', `content', or `all'. After a local state change, it can have
  462. the values `folded', `children', or `subtree'."
  463. :group 'org-cycle
  464. :type 'hook)
  465. (defgroup org-edit-structure nil
  466. "Options concerning structure editing in Org-mode."
  467. :tag "Org Edit Structure"
  468. :group 'org-structure)
  469. (defcustom org-odd-levels-only nil
  470. "Non-nil means, skip even levels and only use odd levels for the outline.
  471. This has the effect that two stars are being added/taken away in
  472. promotion/demotion commands. It also influences how levels are
  473. handled by the exporters.
  474. Changing it requires restart of `font-lock-mode' to become effective
  475. for fontification also in regions already fontified.
  476. You may also set this on a per-file basis by adding one of the following
  477. lines to the buffer:
  478. #+STARTUP: odd
  479. #+STARTUP: oddeven"
  480. :group 'org-edit-structure
  481. :group 'org-font-lock
  482. :type 'boolean)
  483. (defcustom org-adapt-indentation t
  484. "Non-nil means, adapt indentation when promoting and demoting.
  485. When this is set and the *entire* text in an entry is indented, the
  486. indentation is increased by one space in a demotion command, and
  487. decreased by one in a promotion command. If any line in the entry
  488. body starts at column 0, indentation is not changed at all."
  489. :group 'org-edit-structure
  490. :type 'boolean)
  491. (defcustom org-special-ctrl-a/e nil
  492. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  493. When t, `C-a' will bring back the cursor to the beginning of the
  494. headline text, i.e. after the stars and after a possible TODO keyword.
  495. In an item, this will be the position after the bullet.
  496. When the cursor is already at that position, another `C-a' will bring
  497. it to the beginning of the line.
  498. `C-e' will jump to the end of the headline, ignoring the presence of tags
  499. in the headline. A second `C-e' will then jump to the true end of the
  500. line, after any tags.
  501. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  502. and only a directly following, identical keypress will bring the cursor
  503. to the special positions."
  504. :group 'org-edit-structure
  505. :type '(choice
  506. (const :tag "off" nil)
  507. (const :tag "after bullet first" t)
  508. (const :tag "border first" reversed)))
  509. (if (fboundp 'defvaralias)
  510. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  511. (defcustom org-special-ctrl-k nil
  512. "Non-nil means `C-k' will behave specially in headlines.
  513. When nil, `C-k' will call the default `kill-line' command.
  514. When t, the following will happen while the cursor is in the headline:
  515. - When the cursor is at the beginning of a headline, kill the entire
  516. line and possible the folded subtree below the line.
  517. - When in the middle of the headline text, kill the headline up to the tags.
  518. - When after the headline text, kill the tags."
  519. :group 'org-edit-structure
  520. :type 'boolean)
  521. (defcustom org-M-RET-may-split-line '((default . t))
  522. "Non-nil means, M-RET will split the line at the cursor position.
  523. When nil, it will go to the end of the line before making a
  524. new line.
  525. You may also set this option in a different way for different
  526. contexts. Valid contexts are:
  527. headline when creating a new headline
  528. item when creating a new item
  529. table in a table field
  530. default the value to be used for all contexts not explicitly
  531. customized"
  532. :group 'org-structure
  533. :group 'org-table
  534. :type '(choice
  535. (const :tag "Always" t)
  536. (const :tag "Never" nil)
  537. (repeat :greedy t :tag "Individual contexts"
  538. (cons
  539. (choice :tag "Context"
  540. (const headline)
  541. (const item)
  542. (const table)
  543. (const default))
  544. (boolean)))))
  545. (defcustom org-blank-before-new-entry '((heading . nil)
  546. (plain-list-item . nil))
  547. "Should `org-insert-heading' leave a blank line before new heading/item?
  548. The value is an alist, with `heading' and `plain-list-item' as car,
  549. and a boolean flag as cdr."
  550. :group 'org-edit-structure
  551. :type '(list
  552. (cons (const heading) (boolean))
  553. (cons (const plain-list-item) (boolean))))
  554. (defcustom org-insert-heading-hook nil
  555. "Hook being run after inserting a new heading."
  556. :group 'org-edit-structure
  557. :type 'hook)
  558. (defcustom org-enable-fixed-width-editor t
  559. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  560. This currently only means, they are never auto-wrapped.
  561. When nil, such lines will be treated like ordinary lines.
  562. See also the QUOTE keyword."
  563. :group 'org-edit-structure
  564. :type 'boolean)
  565. (defcustom org-goto-auto-isearch t
  566. "Non-nil means, typing characters in org-goto starts incremental search."
  567. :group 'org-edit-structure
  568. :type 'boolean)
  569. (defgroup org-sparse-trees nil
  570. "Options concerning sparse trees in Org-mode."
  571. :tag "Org Sparse Trees"
  572. :group 'org-structure)
  573. (defcustom org-highlight-sparse-tree-matches t
  574. "Non-nil means, highlight all matches that define a sparse tree.
  575. The highlights will automatically disappear the next time the buffer is
  576. changed by an edit command."
  577. :group 'org-sparse-trees
  578. :type 'boolean)
  579. (defcustom org-remove-highlights-with-change t
  580. "Non-nil means, any change to the buffer will remove temporary highlights.
  581. Such highlights are created by `org-occur' and `org-clock-display'.
  582. When nil, `C-c C-c needs to be used to get rid of the highlights.
  583. The highlights created by `org-preview-latex-fragment' always need
  584. `C-c C-c' to be removed."
  585. :group 'org-sparse-trees
  586. :group 'org-time
  587. :type 'boolean)
  588. (defcustom org-occur-hook '(org-first-headline-recenter)
  589. "Hook that is run after `org-occur' has constructed a sparse tree.
  590. This can be used to recenter the window to show as much of the structure
  591. as possible."
  592. :group 'org-sparse-trees
  593. :type 'hook)
  594. (defgroup org-plain-lists nil
  595. "Options concerning plain lists in Org-mode."
  596. :tag "Org Plain lists"
  597. :group 'org-structure)
  598. (defcustom org-cycle-include-plain-lists nil
  599. "Non-nil means, include plain lists into visibility cycling.
  600. This means that during cycling, plain list items will *temporarily* be
  601. interpreted as outline headlines with a level given by 1000+i where i is the
  602. indentation of the bullet. In all other operations, plain list items are
  603. not seen as headlines. For example, you cannot assign a TODO keyword to
  604. such an item."
  605. :group 'org-plain-lists
  606. :type 'boolean)
  607. (defcustom org-plain-list-ordered-item-terminator t
  608. "The character that makes a line with leading number an ordered list item.
  609. Valid values are ?. and ?\). To get both terminators, use t. While
  610. ?. may look nicer, it creates the danger that a line with leading
  611. number may be incorrectly interpreted as an item. ?\) therefore is
  612. the safe choice."
  613. :group 'org-plain-lists
  614. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  615. (const :tag "paren like in \"2)\"" ?\))
  616. (const :tab "both" t)))
  617. (defcustom org-auto-renumber-ordered-lists t
  618. "Non-nil means, automatically renumber ordered plain lists.
  619. Renumbering happens when the sequence have been changed with
  620. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  621. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  622. :group 'org-plain-lists
  623. :type 'boolean)
  624. (defcustom org-provide-checkbox-statistics t
  625. "Non-nil means, update checkbox statistics after insert and toggle.
  626. When this is set, checkbox statistics is updated each time you either insert
  627. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  628. with \\[org-ctrl-c-ctrl-c\\]."
  629. :group 'org-plain-lists
  630. :type 'boolean)
  631. (defgroup org-archive nil
  632. "Options concerning archiving in Org-mode."
  633. :tag "Org Archive"
  634. :group 'org-structure)
  635. (defcustom org-archive-tag "ARCHIVE"
  636. "The tag that marks a subtree as archived.
  637. An archived subtree does not open during visibility cycling, and does
  638. not contribute to the agenda listings.
  639. After changing this, font-lock must be restarted in the relevant buffers to
  640. get the proper fontification."
  641. :group 'org-archive
  642. :group 'org-keywords
  643. :type 'string)
  644. (defcustom org-agenda-skip-archived-trees t
  645. "Non-nil means, the agenda will skip any items located in archived trees.
  646. An archived tree is a tree marked with the tag ARCHIVE."
  647. :group 'org-archive
  648. :group 'org-agenda-skip
  649. :type 'boolean)
  650. (defcustom org-cycle-open-archived-trees nil
  651. "Non-nil means, `org-cycle' will open archived trees.
  652. An archived tree is a tree marked with the tag ARCHIVE.
  653. When nil, archived trees will stay folded. You can still open them with
  654. normal outline commands like `show-all', but not with the cycling commands."
  655. :group 'org-archive
  656. :group 'org-cycle
  657. :type 'boolean)
  658. (defcustom org-sparse-tree-open-archived-trees nil
  659. "Non-nil means sparse tree construction shows matches in archived trees.
  660. When nil, matches in these trees are highlighted, but the trees are kept in
  661. collapsed state."
  662. :group 'org-archive
  663. :group 'org-sparse-trees
  664. :type 'boolean)
  665. (defcustom org-archive-location "%s_archive::"
  666. "The location where subtrees should be archived.
  667. This string consists of two parts, separated by a double-colon.
  668. The first part is a file name - when omitted, archiving happens in the same
  669. file. %s will be replaced by the current file name (without directory part).
  670. Archiving to a different file is useful to keep archived entries from
  671. contributing to the Org-mode Agenda.
  672. The part after the double colon is a headline. The archived entries will be
  673. filed under that headline. When omitted, the subtrees are simply filed away
  674. at the end of the file, as top-level entries.
  675. Here are a few examples:
  676. \"%s_archive::\"
  677. If the current file is Projects.org, archive in file
  678. Projects.org_archive, as top-level trees. This is the default.
  679. \"::* Archived Tasks\"
  680. Archive in the current file, under the top-level headline
  681. \"* Archived Tasks\".
  682. \"~/org/archive.org::\"
  683. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  684. \"basement::** Finished Tasks\"
  685. Archive in file ./basement (relative path), as level 3 trees
  686. below the level 2 heading \"** Finished Tasks\".
  687. You may set this option on a per-file basis by adding to the buffer a
  688. line like
  689. #+ARCHIVE: basement::** Finished Tasks"
  690. :group 'org-archive
  691. :type 'string)
  692. (defcustom org-archive-mark-done t
  693. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  694. This can be a string to set the keyword to use. When t, Org-mode will
  695. use the first keyword in its list that means done."
  696. :group 'org-archive
  697. :type '(choice
  698. (const :tag "No" nil)
  699. (const :tag "Yes" t)
  700. (string :tag "Use this keyword")))
  701. (defcustom org-archive-stamp-time t
  702. "Non-nil means, add a time stamp to entries moved to an archive file.
  703. This variable is obsolete and has no effect anymore, instead add ot remove
  704. `time' from the variablle `org-archive-save-context-info'."
  705. :group 'org-archive
  706. :type 'boolean)
  707. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  708. "Parts of context info that should be stored as properties when archiving.
  709. When a subtree is moved to an archive file, it looses information given by
  710. context, like inherited tags, the category, and possibly also the TODO
  711. state (depending on the variable `org-archive-mark-done').
  712. This variable can be a list of any of the following symbols:
  713. time The time of archiving.
  714. file The file where the entry originates.
  715. itags The local tags, in the headline of the subtree.
  716. ltags The tags the subtree inherits from further up the hierarchy.
  717. todo The pre-archive TODO state.
  718. category The category, taken from file name or #+CATEGORY lines.
  719. olpath The outline path to the item. These are all headlines above
  720. the current item, separated by /, like a file path.
  721. For each symbol present in the list, a property will be created in
  722. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  723. information."
  724. :group 'org-archive
  725. :type '(set :greedy t
  726. (const :tag "Time" time)
  727. (const :tag "File" file)
  728. (const :tag "Category" category)
  729. (const :tag "TODO state" todo)
  730. (const :tag "TODO state" priority)
  731. (const :tag "Inherited tags" itags)
  732. (const :tag "Outline path" olpath)
  733. (const :tag "Local tags" ltags)))
  734. (defgroup org-imenu-and-speedbar nil
  735. "Options concerning imenu and speedbar in Org-mode."
  736. :tag "Org Imenu and Speedbar"
  737. :group 'org-structure)
  738. (defcustom org-imenu-depth 2
  739. "The maximum level for Imenu access to Org-mode headlines.
  740. This also applied for speedbar access."
  741. :group 'org-imenu-and-speedbar
  742. :type 'number)
  743. (defgroup org-table nil
  744. "Options concerning tables in Org-mode."
  745. :tag "Org Table"
  746. :group 'org)
  747. (defcustom org-enable-table-editor 'optimized
  748. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  749. When nil, such lines will be treated like ordinary lines.
  750. When equal to the symbol `optimized', the table editor will be optimized to
  751. do the following:
  752. - Automatic overwrite mode in front of whitespace in table fields.
  753. This makes the structure of the table stay in tact as long as the edited
  754. field does not exceed the column width.
  755. - Minimize the number of realigns. Normally, the table is aligned each time
  756. TAB or RET are pressed to move to another field. With optimization this
  757. happens only if changes to a field might have changed the column width.
  758. Optimization requires replacing the functions `self-insert-command',
  759. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  760. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  761. very good at guessing when a re-align will be necessary, but you can always
  762. force one with \\[org-ctrl-c-ctrl-c].
  763. If you would like to use the optimized version in Org-mode, but the
  764. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  765. This variable can be used to turn on and off the table editor during a session,
  766. but in order to toggle optimization, a restart is required.
  767. See also the variable `org-table-auto-blank-field'."
  768. :group 'org-table
  769. :type '(choice
  770. (const :tag "off" nil)
  771. (const :tag "on" t)
  772. (const :tag "on, optimized" optimized)))
  773. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  774. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  775. In the optimized version, the table editor takes over all simple keys that
  776. normally just insert a character. In tables, the characters are inserted
  777. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  778. for empty fields). Outside tables, the correct binding of the keys is
  779. restored.
  780. The default for this option is t if the optimized version is also used in
  781. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  782. this variable requires a restart of Emacs to become effective."
  783. :group 'org-table
  784. :type 'boolean)
  785. (defcustom orgtbl-radio-table-templates
  786. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  787. % END RECEIVE ORGTBL %n
  788. \\begin{comment}
  789. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  790. | | |
  791. \\end{comment}\n")
  792. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  793. @c END RECEIVE ORGTBL %n
  794. @ignore
  795. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  796. | | |
  797. @end ignore\n")
  798. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  799. <!-- END RECEIVE ORGTBL %n -->
  800. <!--
  801. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  802. | | |
  803. -->\n"))
  804. "Templates for radio tables in different major modes.
  805. All occurrences of %n in a template will be replaced with the name of the
  806. table, obtained by prompting the user."
  807. :group 'org-table
  808. :type '(repeat
  809. (list (symbol :tag "Major mode")
  810. (string :tag "Format"))))
  811. (defgroup org-table-settings nil
  812. "Settings for tables in Org-mode."
  813. :tag "Org Table Settings"
  814. :group 'org-table)
  815. (defcustom org-table-default-size "5x2"
  816. "The default size for newly created tables, Columns x Rows."
  817. :group 'org-table-settings
  818. :type 'string)
  819. (defcustom org-table-number-regexp
  820. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  821. "Regular expression for recognizing numbers in table columns.
  822. If a table column contains mostly numbers, it will be aligned to the
  823. right. If not, it will be aligned to the left.
  824. The default value of this option is a regular expression which allows
  825. anything which looks remotely like a number as used in scientific
  826. context. For example, all of the following will be considered a
  827. number:
  828. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  829. Other options offered by the customize interface are more restrictive."
  830. :group 'org-table-settings
  831. :type '(choice
  832. (const :tag "Positive Integers"
  833. "^[0-9]+$")
  834. (const :tag "Integers"
  835. "^[-+]?[0-9]+$")
  836. (const :tag "Floating Point Numbers"
  837. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  838. (const :tag "Floating Point Number or Integer"
  839. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  840. (const :tag "Exponential, Floating point, Integer"
  841. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  842. (const :tag "Very General Number-Like, including hex"
  843. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  844. (string :tag "Regexp:")))
  845. (defcustom org-table-number-fraction 0.5
  846. "Fraction of numbers in a column required to make the column align right.
  847. In a column all non-white fields are considered. If at least this
  848. fraction of fields is matched by `org-table-number-fraction',
  849. alignment to the right border applies."
  850. :group 'org-table-settings
  851. :type 'number)
  852. (defgroup org-table-editing nil
  853. "Behavior of tables during editing in Org-mode."
  854. :tag "Org Table Editing"
  855. :group 'org-table)
  856. (defcustom org-table-automatic-realign t
  857. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  858. When nil, aligning is only done with \\[org-table-align], or after column
  859. removal/insertion."
  860. :group 'org-table-editing
  861. :type 'boolean)
  862. (defcustom org-table-auto-blank-field t
  863. "Non-nil means, automatically blank table field when starting to type into it.
  864. This only happens when typing immediately after a field motion
  865. command (TAB, S-TAB or RET).
  866. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  867. :group 'org-table-editing
  868. :type 'boolean)
  869. (defcustom org-table-tab-jumps-over-hlines t
  870. "Non-nil means, tab in the last column of a table with jump over a hline.
  871. If a horizontal separator line is following the current line,
  872. `org-table-next-field' can either create a new row before that line, or jump
  873. over the line. When this option is nil, a new line will be created before
  874. this line."
  875. :group 'org-table-editing
  876. :type 'boolean)
  877. (defcustom org-table-tab-recognizes-table.el t
  878. "Non-nil means, TAB will automatically notice a table.el table.
  879. When it sees such a table, it moves point into it and - if necessary -
  880. calls `table-recognize-table'."
  881. :group 'org-table-editing
  882. :type 'boolean)
  883. (defgroup org-table-calculation nil
  884. "Options concerning tables in Org-mode."
  885. :tag "Org Table Calculation"
  886. :group 'org-table)
  887. (defcustom org-table-use-standard-references t
  888. "Should org-mode work with table refrences like B3 instead of @3$2?
  889. Possible values are:
  890. nil never use them
  891. from accept as input, do not present for editing
  892. t: accept as input and present for editing"
  893. :group 'org-table-calculation
  894. :type '(choice
  895. (const :tag "Never, don't even check unser input for them" nil)
  896. (const :tag "Always, both as user input, and when editing" t)
  897. (const :tag "Convert user input, don't offer during editing" 'from)))
  898. (defcustom org-table-copy-increment t
  899. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  900. :group 'org-table-calculation
  901. :type 'boolean)
  902. (defcustom org-calc-default-modes
  903. '(calc-internal-prec 12
  904. calc-float-format (float 5)
  905. calc-angle-mode deg
  906. calc-prefer-frac nil
  907. calc-symbolic-mode nil
  908. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  909. calc-display-working-message t
  910. )
  911. "List with Calc mode settings for use in calc-eval for table formulas.
  912. The list must contain alternating symbols (Calc modes variables and values).
  913. Don't remove any of the default settings, just change the values. Org-mode
  914. relies on the variables to be present in the list."
  915. :group 'org-table-calculation
  916. :type 'plist)
  917. (defcustom org-table-formula-evaluate-inline t
  918. "Non-nil means, TAB and RET evaluate a formula in current table field.
  919. If the current field starts with an equal sign, it is assumed to be a formula
  920. which should be evaluated as described in the manual and in the documentation
  921. string of the command `org-table-eval-formula'. This feature requires the
  922. Emacs calc package.
  923. When this variable is nil, formula calculation is only available through
  924. the command \\[org-table-eval-formula]."
  925. :group 'org-table-calculation
  926. :type 'boolean)
  927. (defcustom org-table-formula-use-constants t
  928. "Non-nil means, interpret constants in formulas in tables.
  929. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  930. by the value given in `org-table-formula-constants', or by a value obtained
  931. from the `constants.el' package."
  932. :group 'org-table-calculation
  933. :type 'boolean)
  934. (defcustom org-table-formula-constants nil
  935. "Alist with constant names and values, for use in table formulas.
  936. The car of each element is a name of a constant, without the `$' before it.
  937. The cdr is the value as a string. For example, if you'd like to use the
  938. speed of light in a formula, you would configure
  939. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  940. and then use it in an equation like `$1*$c'.
  941. Constants can also be defined on a per-file basis using a line like
  942. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  943. :group 'org-table-calculation
  944. :type '(repeat
  945. (cons (string :tag "name")
  946. (string :tag "value"))))
  947. (defvar org-table-formula-constants-local nil
  948. "Local version of `org-table-formula-constants'.")
  949. (make-variable-buffer-local 'org-table-formula-constants-local)
  950. (defcustom org-table-allow-automatic-line-recalculation t
  951. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  952. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  953. :group 'org-table-calculation
  954. :type 'boolean)
  955. (defgroup org-link nil
  956. "Options concerning links in Org-mode."
  957. :tag "Org Link"
  958. :group 'org)
  959. (defvar org-link-abbrev-alist-local nil
  960. "Buffer-local version of `org-link-abbrev-alist', which see.
  961. The value of this is taken from the #+LINK lines.")
  962. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  963. (defcustom org-link-abbrev-alist nil
  964. "Alist of link abbreviations.
  965. The car of each element is a string, to be replaced at the start of a link.
  966. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  967. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  968. [[linkkey:tag][description]]
  969. If REPLACE is a string, the tag will simply be appended to create the link.
  970. If the string contains \"%s\", the tag will be inserted there.
  971. REPLACE may also be a function that will be called with the tag as the
  972. only argument to create the link, which should be returned as a string.
  973. See the manual for examples."
  974. :group 'org-link
  975. :type 'alist)
  976. (defcustom org-descriptive-links t
  977. "Non-nil means, hide link part and only show description of bracket links.
  978. Bracket links are like [[link][descritpion]]. This variable sets the initial
  979. state in new org-mode buffers. The setting can then be toggled on a
  980. per-buffer basis from the Org->Hyperlinks menu."
  981. :group 'org-link
  982. :type 'boolean)
  983. (defcustom org-link-file-path-type 'adaptive
  984. "How the path name in file links should be stored.
  985. Valid values are:
  986. relative Relative to the current directory, i.e. the directory of the file
  987. into which the link is being inserted.
  988. absolute Absolute path, if possible with ~ for home directory.
  989. noabbrev Absolute path, no abbreviation of home directory.
  990. adaptive Use relative path for files in the current directory and sub-
  991. directories of it. For other files, use an absolute path."
  992. :group 'org-link
  993. :type '(choice
  994. (const relative)
  995. (const absolute)
  996. (const noabbrev)
  997. (const adaptive)))
  998. (defcustom org-activate-links '(bracket angle plain radio tag date)
  999. "Types of links that should be activated in Org-mode files.
  1000. This is a list of symbols, each leading to the activation of a certain link
  1001. type. In principle, it does not hurt to turn on most link types - there may
  1002. be a small gain when turning off unused link types. The types are:
  1003. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1004. angular Links in angular brackes that may contain whitespace like
  1005. <bbdb:Carsten Dominik>.
  1006. plain Plain links in normal text, no whitespace, like http://google.com.
  1007. radio Text that is matched by a radio target, see manual for details.
  1008. tag Tag settings in a headline (link to tag search).
  1009. date Time stamps (link to calendar).
  1010. Changing this variable requires a restart of Emacs to become effective."
  1011. :group 'org-link
  1012. :type '(set (const :tag "Double bracket links (new style)" bracket)
  1013. (const :tag "Angular bracket links (old style)" angular)
  1014. (const :tag "Plain text links" plain)
  1015. (const :tag "Radio target matches" radio)
  1016. (const :tag "Tags" tag)
  1017. (const :tag "Timestamps" date)))
  1018. (defgroup org-link-store nil
  1019. "Options concerning storing links in Org-mode"
  1020. :tag "Org Store Link"
  1021. :group 'org-link)
  1022. (defcustom org-email-link-description-format "Email %c: %.30s"
  1023. "Format of the description part of a link to an email or usenet message.
  1024. The following %-excapes will be replaced by corresponding information:
  1025. %F full \"From\" field
  1026. %f name, taken from \"From\" field, address if no name
  1027. %T full \"To\" field
  1028. %t first name in \"To\" field, address if no name
  1029. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  1030. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1031. %s subject
  1032. %m message-id.
  1033. You may use normal field width specification between the % and the letter.
  1034. This is for example useful to limit the length of the subject.
  1035. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1036. :group 'org-link-store
  1037. :type 'string)
  1038. (defcustom org-from-is-user-regexp
  1039. (let (r1 r2)
  1040. (when (and user-mail-address (not (string= user-mail-address "")))
  1041. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1042. (when (and user-full-name (not (string= user-full-name "")))
  1043. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1044. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1045. "Regexp mached against the \"From:\" header of an email or usenet message.
  1046. It should match if the message is from the user him/herself."
  1047. :group 'org-link-store
  1048. :type 'regexp)
  1049. (defcustom org-context-in-file-links t
  1050. "Non-nil means, file links from `org-store-link' contain context.
  1051. A search string will be added to the file name with :: as separator and
  1052. used to find the context when the link is activated by the command
  1053. `org-open-at-point'.
  1054. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1055. negates this setting for the duration of the command."
  1056. :group 'org-link-store
  1057. :type 'boolean)
  1058. (defcustom org-keep-stored-link-after-insertion nil
  1059. "Non-nil means, keep link in list for entire session.
  1060. The command `org-store-link' adds a link pointing to the current
  1061. location to an internal list. These links accumulate during a session.
  1062. The command `org-insert-link' can be used to insert links into any
  1063. Org-mode file (offering completion for all stored links). When this
  1064. option is nil, every link which has been inserted once using \\[org-insert-link]
  1065. will be removed from the list, to make completing the unused links
  1066. more efficient."
  1067. :group 'org-link-store
  1068. :type 'boolean)
  1069. (defcustom org-usenet-links-prefer-google nil
  1070. "Non-nil means, `org-store-link' will create web links to Google groups.
  1071. When nil, Gnus will be used for such links.
  1072. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1073. negates this setting for the duration of the command."
  1074. :group 'org-link-store
  1075. :type 'boolean)
  1076. (defgroup org-link-follow nil
  1077. "Options concerning following links in Org-mode"
  1078. :tag "Org Follow Link"
  1079. :group 'org-link)
  1080. (defcustom org-tab-follows-link nil
  1081. "Non-nil means, on links TAB will follow the link.
  1082. Needs to be set before org.el is loaded."
  1083. :group 'org-link-follow
  1084. :type 'boolean)
  1085. (defcustom org-return-follows-link nil
  1086. "Non-nil means, on links RET will follow the link.
  1087. Needs to be set before org.el is loaded."
  1088. :group 'org-link-follow
  1089. :type 'boolean)
  1090. (defcustom org-mouse-1-follows-link mouse-1-click-follows-link
  1091. "Non-nil means, mouse-1 on a link will follow the link.
  1092. A longer mouse click will still set point. Does not work on XEmacs.
  1093. Needs to be set before org.el is loaded."
  1094. :group 'org-link-follow
  1095. :type 'boolean)
  1096. (defcustom org-mark-ring-length 4
  1097. "Number of different positions to be recorded in the ring
  1098. Changing this requires a restart of Emacs to work correctly."
  1099. :group 'org-link-follow
  1100. :type 'interger)
  1101. (defcustom org-link-frame-setup
  1102. '((vm . vm-visit-folder-other-frame)
  1103. (gnus . gnus-other-frame)
  1104. (file . find-file-other-window))
  1105. "Setup the frame configuration for following links.
  1106. When following a link with Emacs, it may often be useful to display
  1107. this link in another window or frame. This variable can be used to
  1108. set this up for the different types of links.
  1109. For VM, use any of
  1110. `vm-visit-folder'
  1111. `vm-visit-folder-other-frame'
  1112. For Gnus, use any of
  1113. `gnus'
  1114. `gnus-other-frame'
  1115. For FILE, use any of
  1116. `find-file'
  1117. `find-file-other-window'
  1118. `find-file-other-frame'
  1119. For the calendar, use the variable `calendar-setup'.
  1120. For BBDB, it is currently only possible to display the matches in
  1121. another window."
  1122. :group 'org-link-follow
  1123. :type '(list
  1124. (cons (const vm)
  1125. (choice
  1126. (const vm-visit-folder)
  1127. (const vm-visit-folder-other-window)
  1128. (const vm-visit-folder-other-frame)))
  1129. (cons (const gnus)
  1130. (choice
  1131. (const gnus)
  1132. (const gnus-other-frame)))
  1133. (cons (const file)
  1134. (choice
  1135. (const find-file)
  1136. (const find-file-other-window)
  1137. (const find-file-other-frame)))))
  1138. (defcustom org-display-internal-link-with-indirect-buffer nil
  1139. "Non-nil means, use indirect buffer to display infile links.
  1140. Activating internal links (from one location in a file to another location
  1141. in the same file) normally just jumps to the location. When the link is
  1142. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1143. another window. When this option is set, the other window actually displays
  1144. an indirect buffer clone of the current buffer, to avoid any visibility
  1145. changes to the current buffer."
  1146. :group 'org-link-follow
  1147. :type 'boolean)
  1148. (defcustom org-open-non-existing-files nil
  1149. "Non-nil means, `org-open-file' will open non-existing files.
  1150. When nil, an error will be generated."
  1151. :group 'org-link-follow
  1152. :type 'boolean)
  1153. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1154. "Function and arguments to call for following mailto links.
  1155. This is a list with the first element being a lisp function, and the
  1156. remaining elements being arguments to the function. In string arguments,
  1157. %a will be replaced by the address, and %s will be replaced by the subject
  1158. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1159. :group 'org-link-follow
  1160. :type '(choice
  1161. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1162. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1163. (const :tag "message-mail" (message-mail "%a" "%s"))
  1164. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1165. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1166. "Non-nil means, ask for confirmation before executing shell links.
  1167. Shell links can be dangerous: just think about a link
  1168. [[shell:rm -rf ~/*][Google Search]]
  1169. This link would show up in your Org-mode document as \"Google Search\",
  1170. but really it would remove your entire home directory.
  1171. Therefore we advise against setting this variable to nil.
  1172. Just change it to `y-or-n-p' of you want to confirm with a
  1173. single keystroke rather than having to type \"yes\"."
  1174. :group 'org-link-follow
  1175. :type '(choice
  1176. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1177. (const :tag "with y-or-n (faster)" y-or-n-p)
  1178. (const :tag "no confirmation (dangerous)" nil)))
  1179. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1180. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1181. Elisp links can be dangerous: just think about a link
  1182. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1183. This link would show up in your Org-mode document as \"Google Search\",
  1184. but really it would remove your entire home directory.
  1185. Therefore we advise against setting this variable to nil.
  1186. Just change it to `y-or-n-p' of you want to confirm with a
  1187. single keystroke rather than having to type \"yes\"."
  1188. :group 'org-link-follow
  1189. :type '(choice
  1190. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1191. (const :tag "with y-or-n (faster)" y-or-n-p)
  1192. (const :tag "no confirmation (dangerous)" nil)))
  1193. (defconst org-file-apps-defaults-gnu
  1194. '((remote . emacs)
  1195. (t . mailcap))
  1196. "Default file applications on a UNIX or GNU/Linux system.
  1197. See `org-file-apps'.")
  1198. (defconst org-file-apps-defaults-macosx
  1199. '((remote . emacs)
  1200. (t . "open %s")
  1201. ("ps" . "gv %s")
  1202. ("ps.gz" . "gv %s")
  1203. ("eps" . "gv %s")
  1204. ("eps.gz" . "gv %s")
  1205. ("dvi" . "xdvi %s")
  1206. ("fig" . "xfig %s"))
  1207. "Default file applications on a MacOS X system.
  1208. The system \"open\" is known as a default, but we use X11 applications
  1209. for some files for which the OS does not have a good default.
  1210. See `org-file-apps'.")
  1211. (defconst org-file-apps-defaults-windowsnt
  1212. (list
  1213. '(remote . emacs)
  1214. (cons t
  1215. (list (if (featurep 'xemacs)
  1216. 'mswindows-shell-execute
  1217. 'w32-shell-execute)
  1218. "open" 'file)))
  1219. "Default file applications on a Windows NT system.
  1220. The system \"open\" is used for most files.
  1221. See `org-file-apps'.")
  1222. (defcustom org-file-apps
  1223. '(
  1224. ("txt" . emacs)
  1225. ("tex" . emacs)
  1226. ("ltx" . emacs)
  1227. ("org" . emacs)
  1228. ("el" . emacs)
  1229. ("bib" . emacs)
  1230. )
  1231. "External applications for opening `file:path' items in a document.
  1232. Org-mode uses system defaults for different file types, but
  1233. you can use this variable to set the application for a given file
  1234. extension. The entries in this list are cons cells where the car identifies
  1235. files and the cdr the corresponding command. Possible values for the
  1236. file identifier are
  1237. \"ext\" A string identifying an extension
  1238. `directory' Matches a directory
  1239. `remote' Matches a remote file, accessible through tramp or efs.
  1240. Remote files most likely should be visited through Emacs
  1241. because external applications cannot handle such paths.
  1242. t Default for all remaining files
  1243. Possible values for the command are:
  1244. `emacs' The file will be visited by the current Emacs process.
  1245. `default' Use the default application for this file type.
  1246. string A command to be executed by a shell; %s will be replaced
  1247. by the path to the file.
  1248. sexp A Lisp form which will be evaluated. The file path will
  1249. be available in the Lisp variable `file'.
  1250. For more examples, see the system specific constants
  1251. `org-file-apps-defaults-macosx'
  1252. `org-file-apps-defaults-windowsnt'
  1253. `org-file-apps-defaults-gnu'."
  1254. :group 'org-link-follow
  1255. :type '(repeat
  1256. (cons (choice :value ""
  1257. (string :tag "Extension")
  1258. (const :tag "Default for unrecognized files" t)
  1259. (const :tag "Remote file" remote)
  1260. (const :tag "Links to a directory" directory))
  1261. (choice :value ""
  1262. (const :tag "Visit with Emacs" emacs)
  1263. (const :tag "Use system default" default)
  1264. (string :tag "Command")
  1265. (sexp :tag "Lisp form")))))
  1266. (defcustom org-mhe-search-all-folders nil
  1267. "Non-nil means, that the search for the mh-message will be extended to
  1268. all folders if the message cannot be found in the folder given in the link.
  1269. Searching all folders is very efficient with one of the search engines
  1270. supported by MH-E, but will be slow with pick."
  1271. :group 'org-link-follow
  1272. :type 'boolean)
  1273. (defgroup org-remember nil
  1274. "Options concerning interaction with remember.el."
  1275. :tag "Org Remember"
  1276. :group 'org)
  1277. (defcustom org-directory "~/org"
  1278. "Directory with org files.
  1279. This directory will be used as default to prompt for org files.
  1280. Used by the hooks for remember.el."
  1281. :group 'org-remember
  1282. :type 'directory)
  1283. (defcustom org-default-notes-file "~/.notes"
  1284. "Default target for storing notes.
  1285. Used by the hooks for remember.el. This can be a string, or nil to mean
  1286. the value of `remember-data-file'.
  1287. You can set this on a per-template basis with the variable
  1288. `org-remember-templates'."
  1289. :group 'org-remember
  1290. :type '(choice
  1291. (const :tag "Default from remember-data-file" nil)
  1292. file))
  1293. (defcustom org-remember-store-without-prompt t
  1294. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1295. In this case, you need `C-u C-c C-c' to get the prompts for
  1296. note file and headline.
  1297. When this variable is nil, `C-c C-c' give you the prompts, and
  1298. `C-u C-c C-c' trigger the fasttrack."
  1299. :group 'org-remember
  1300. :type 'boolean)
  1301. (defcustom org-remember-interactive-interface 'refile
  1302. "The interface to be used for interactive filing of remember notes.
  1303. This is only used when the interactive mode for selecting a filing
  1304. location is used (see the variable `org-remember-store-without-prompt').
  1305. Allowed vaues are:
  1306. outline The interface shows an outline of the relevant file
  1307. and the correct heading is found by moving through
  1308. the outline or by searching with incremental search.
  1309. outline-path-completion Headlines in the current buffer are offered via
  1310. completion.
  1311. refile Use the refile interface, and offer headlines,
  1312. possibly from different buffers."
  1313. :group 'org-remember
  1314. :type '(choice
  1315. (const :tag "Refile" refile)
  1316. (const :tag "Outline" outline)
  1317. (const :tag "Outline-path-completion" outline-path-completion)))
  1318. (defcustom org-goto-interface 'outline
  1319. "The default interface to be used for `org-goto'.
  1320. Allowed vaues are:
  1321. outline The interface shows an outline of the relevant file
  1322. and the correct heading is found by moving through
  1323. the outline or by searching with incremental search.
  1324. outline-path-completion Headlines in the current buffer are offered via
  1325. completion."
  1326. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1327. :type '(choice
  1328. (const :tag "Outline" outline)
  1329. (const :tag "Outline-path-completion" outline-path-completion)))
  1330. (defcustom org-remember-default-headline ""
  1331. "The headline that should be the default location in the notes file.
  1332. When filing remember notes, the cursor will start at that position.
  1333. You can set this on a per-template basis with the variable
  1334. `org-remember-templates'."
  1335. :group 'org-remember
  1336. :type 'string)
  1337. (defcustom org-remember-templates nil
  1338. "Templates for the creation of remember buffers.
  1339. When nil, just let remember make the buffer.
  1340. When not nil, this is a list of 5-element lists. In each entry, the first
  1341. element is the name of the template, which should be a single short word.
  1342. The second element is a character, a unique key to select this template.
  1343. The third element is the template. The fourth element is optional and can
  1344. specify a destination file for remember items created with this template.
  1345. The default file is given by `org-default-notes-file'. An optional fifth
  1346. element can specify the headline in that file that should be offered
  1347. first when the user is asked to file the entry. The default headline is
  1348. given in the variable `org-remember-default-headline'.
  1349. The template specifies the structure of the remember buffer. It should have
  1350. a first line starting with a star, to act as the org-mode headline.
  1351. Furthermore, the following %-escapes will be replaced with content:
  1352. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1353. A default value and a completion table ca be specified like this:
  1354. %^{prompt|default|completion2|completion3|...}
  1355. %t time stamp, date only
  1356. %T time stamp with date and time
  1357. %u, %U like the above, but inactive time stamps
  1358. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1359. You may define a prompt like %^{Please specify birthday}t
  1360. %n user name (taken from `user-full-name')
  1361. %a annotation, normally the link created with org-store-link
  1362. %i initial content, the region active. If %i is indented,
  1363. the entire inserted text will be indented as well.
  1364. %c content of the clipboard, or current kill ring head
  1365. %^g prompt for tags, with completion on tags in target file
  1366. %^G prompt for tags, with completion all tags in all agenda files
  1367. %:keyword specific information for certain link types, see below
  1368. %[pathname] insert the contents of the file given by `pathname'
  1369. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1370. %! Store this note immediately after filling the template
  1371. %? After completing the template, position cursor here.
  1372. Apart from these general escapes, you can access information specific to the
  1373. link type that is created. For example, calling `remember' in emails or gnus
  1374. will record the author and the subject of the message, which you can access
  1375. with %:author and %:subject, respectively. Here is a complete list of what
  1376. is recorded for each link type.
  1377. Link type | Available information
  1378. -------------------+------------------------------------------------------
  1379. bbdb | %:type %:name %:company
  1380. vm, wl, mh, rmail | %:type %:subject %:message-id
  1381. | %:from %:fromname %:fromaddress
  1382. | %:to %:toname %:toaddress
  1383. | %:fromto (either \"to NAME\" or \"from NAME\")
  1384. gnus | %:group, for messages also all email fields
  1385. w3, w3m | %:type %:url
  1386. info | %:type %:file %:node
  1387. calendar | %:type %:date"
  1388. :group 'org-remember
  1389. :get (lambda (var) ; Make sure all entries have 5 elements
  1390. (mapcar (lambda (x)
  1391. (if (not (stringp (car x))) (setq x (cons "" x)))
  1392. (cond ((= (length x) 4) (append x '("")))
  1393. ((= (length x) 3) (append x '("" "")))
  1394. (t x)))
  1395. (default-value var)))
  1396. :type '(repeat
  1397. :tag "enabled"
  1398. (list :value ("" ?a "\n" nil nil)
  1399. (string :tag "Name")
  1400. (character :tag "Selection Key")
  1401. (string :tag "Template")
  1402. (choice
  1403. (file :tag "Destination file")
  1404. (const :tag "Prompt for file" nil))
  1405. (choice
  1406. (string :tag "Destination headline")
  1407. (const :tag "Selection interface for heading")))))
  1408. (defcustom org-reverse-note-order nil
  1409. "Non-nil means, store new notes at the beginning of a file or entry.
  1410. When nil, new notes will be filed to the end of a file or entry.
  1411. This can also be a list with cons cells of regular expressions that
  1412. are matched against file names, and values."
  1413. :group 'org-remember
  1414. :type '(choice
  1415. (const :tag "Reverse always" t)
  1416. (const :tag "Reverse never" nil)
  1417. (repeat :tag "By file name regexp"
  1418. (cons regexp boolean))))
  1419. (defcustom org-refile-targets nil
  1420. "Targets for refiling entries with \\[org-refile].
  1421. This is list of cons cells. Each cell contains:
  1422. - a specification of the files to be considered, either a list of files,
  1423. or a symbol whose function or value fields will be used to retrieve
  1424. a file name or a list of file names. Nil means, refile to a different
  1425. heading in the current buffer.
  1426. - A specification of how to find candidate refile targets. This may be
  1427. any of
  1428. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1429. This tag has to be present in all target headlines, inheritance will
  1430. not be considered.
  1431. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1432. todo keyword.
  1433. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1434. headlines that are refiling targets.
  1435. - a cons cell (:level . N). Any headline of level N is considered a target.
  1436. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1437. ;; FIXME: what if there are a var and func with same name???
  1438. :group 'org-remember
  1439. :type '(repeat
  1440. (cons
  1441. (choice :value org-agenda-files
  1442. (const :tag "All agenda files" org-agenda-files)
  1443. (const :tag "Current buffer" nil)
  1444. (function) (variable) (file))
  1445. (choice :tag "Identify target headline by"
  1446. (cons :tag "Specific tag" (const :tag) (string))
  1447. (cons :tag "TODO keyword" (const :todo) (string))
  1448. (cons :tag "Regular expression" (const :regexp) (regexp))
  1449. (cons :tag "Level number" (const :level) (integer))
  1450. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1451. (defcustom org-refile-use-outline-path nil
  1452. "Non-nil means, provide refile targets as paths.
  1453. So a level 3 headline will be available as level1/level2/level3.
  1454. When the value is `file', also include the file name (without directory)
  1455. into the path. When `full-file-path', include the full file path."
  1456. :group 'org-remember
  1457. :type '(choice
  1458. (const :tag "Not" nil)
  1459. (const :tag "Yes" t)
  1460. (const :tag "Start with file name" file)
  1461. (const :tag "Start with full file path" full-file-path)))
  1462. (defgroup org-todo nil
  1463. "Options concerning TODO items in Org-mode."
  1464. :tag "Org TODO"
  1465. :group 'org)
  1466. (defgroup org-progress nil
  1467. "Options concerning Progress logging in Org-mode."
  1468. :tag "Org Progress"
  1469. :group 'org-time)
  1470. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1471. "List of TODO entry keyword sequences and their interpretation.
  1472. \\<org-mode-map>This is a list of sequences.
  1473. Each sequence starts with a symbol, either `sequence' or `type',
  1474. indicating if the keywords should be interpreted as a sequence of
  1475. action steps, or as different types of TODO items. The first
  1476. keywords are states requiring action - these states will select a headline
  1477. for inclusion into the global TODO list Org-mode produces. If one of
  1478. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1479. signify that no further action is necessary. If \"|\" is not found,
  1480. the last keyword is treated as the only DONE state of the sequence.
  1481. The command \\[org-todo] cycles an entry through these states, and one
  1482. additional state where no keyword is present. For details about this
  1483. cycling, see the manual.
  1484. TODO keywords and interpretation can also be set on a per-file basis with
  1485. the special #+SEQ_TODO and #+TYP_TODO lines.
  1486. Each keyword can optionally specify a character for fast state selection
  1487. \(in combination with the variable `org-use-fast-todo-selection')
  1488. and specifiers for state change logging, using the same syntax
  1489. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1490. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1491. indicates to record a time stamp each time this state is selected.
  1492. \"WAIT(w@)\" says that the user should in addition be prompted for a
  1493. note, and \"WAIT(w@/@)\" says that a note should be taken both when
  1494. entering and when leaving this state.
  1495. For backward compatibility, this variable may also be just a list
  1496. of keywords - in this case the interptetation (sequence or type) will be
  1497. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1498. :group 'org-todo
  1499. :group 'org-keywords
  1500. :type '(choice
  1501. (repeat :tag "Old syntax, just keywords"
  1502. (string :tag "Keyword"))
  1503. (repeat :tag "New syntax"
  1504. (cons
  1505. (choice
  1506. :tag "Interpretation"
  1507. (const :tag "Sequence (cycling hits every state)" sequence)
  1508. (const :tag "Type (cycling directly to DONE)" type))
  1509. (repeat
  1510. (string :tag "Keyword"))))))
  1511. (defvar org-todo-keywords-1 nil
  1512. "All TODO and DONE keywords active in a buffer.")
  1513. (make-variable-buffer-local 'org-todo-keywords-1)
  1514. (defvar org-todo-keywords-for-agenda nil)
  1515. (defvar org-done-keywords-for-agenda nil)
  1516. (defvar org-not-done-keywords nil)
  1517. (make-variable-buffer-local 'org-not-done-keywords)
  1518. (defvar org-done-keywords nil)
  1519. (make-variable-buffer-local 'org-done-keywords)
  1520. (defvar org-todo-heads nil)
  1521. (make-variable-buffer-local 'org-todo-heads)
  1522. (defvar org-todo-sets nil)
  1523. (make-variable-buffer-local 'org-todo-sets)
  1524. (defvar org-todo-log-states nil)
  1525. (make-variable-buffer-local 'org-todo-log-states)
  1526. (defvar org-todo-kwd-alist nil)
  1527. (make-variable-buffer-local 'org-todo-kwd-alist)
  1528. (defvar org-todo-key-alist nil)
  1529. (make-variable-buffer-local 'org-todo-key-alist)
  1530. (defvar org-todo-key-trigger nil)
  1531. (make-variable-buffer-local 'org-todo-key-trigger)
  1532. (defcustom org-todo-interpretation 'sequence
  1533. "Controls how TODO keywords are interpreted.
  1534. This variable is in principle obsolete and is only used for
  1535. backward compatibility, if the interpretation of todo keywords is
  1536. not given already in `org-todo-keywords'. See that variable for
  1537. more information."
  1538. :group 'org-todo
  1539. :group 'org-keywords
  1540. :type '(choice (const sequence)
  1541. (const type)))
  1542. (defcustom org-use-fast-todo-selection 'prefix
  1543. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1544. This variable describes if and under what circumstances the cycling
  1545. mechanism for TODO keywords will be replaced by a single-key, direct
  1546. selection scheme.
  1547. When nil, fast selection is never used.
  1548. When the symbol `prefix', it will be used when `org-todo' is called with
  1549. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1550. in an agenda buffer.
  1551. When t, fast selection is used by default. In this case, the prefix
  1552. argument forces cycling instead.
  1553. In all cases, the special interface is only used if access keys have actually
  1554. been assigned by the user, i.e. if keywords in the configuration are followed
  1555. by a letter in parenthesis, like TODO(t)."
  1556. :group 'org-todo
  1557. :type '(choice
  1558. (const :tag "Never" nil)
  1559. (const :tag "By default" t)
  1560. (const :tag "Only with C-u C-c C-t" prefix)))
  1561. (defcustom org-after-todo-state-change-hook nil
  1562. "Hook which is run after the state of a TODO item was changed.
  1563. The new state (a string with a TODO keyword, or nil) is available in the
  1564. Lisp variable `state'."
  1565. :group 'org-todo
  1566. :type 'hook)
  1567. (defcustom org-log-done nil
  1568. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1569. When equal to the list (done), also prompt for a closing note.
  1570. This can also be configured on a per-file basis by adding one of
  1571. the following lines anywhere in the buffer:
  1572. #+STARTUP: logdone
  1573. #+STARTUP: lognotedone
  1574. #+STARTUP: nologdone"
  1575. :group 'org-todo
  1576. :group 'org-progress
  1577. :type '(choice
  1578. (const :tag "No logging" nil)
  1579. (const :tag "Record CLOSED timestamp" time)
  1580. (const :tag "Record CLOSED timestamp with closing note." note)))
  1581. ;; Normalize old uses of org-log-done.
  1582. (cond
  1583. ((eq org-log-done t) (setq org-log-done 'time))
  1584. ((and (listp org-log-done) (memq 'done org-log-done))
  1585. (setq org-log-done 'note)))
  1586. ;; FIXME: document
  1587. (defcustom org-log-note-clock-out nil
  1588. "Non-nil means, recored a note when clocking out of an item.
  1589. This can also be configured on a per-file basis by adding one of
  1590. the following lines anywhere in the buffer:
  1591. #+STARTUP: lognoteclock-out
  1592. #+STARTUP: nolognoteclock-out"
  1593. :group 'org-todo
  1594. :group 'org-progress
  1595. :type 'boolean)
  1596. (defcustom org-log-done-with-time t
  1597. "Non-nil means, the CLOSED time stamp will contain date and time.
  1598. When nil, only the date will be recorded."
  1599. :group 'org-progress
  1600. :type 'boolean)
  1601. (defcustom org-log-note-headings
  1602. '((done . "CLOSING NOTE %t")
  1603. (state . "State %-12s %t")
  1604. (clock-out . ""))
  1605. "Headings for notes added when clocking out or closing TODO items.
  1606. The value is an alist, with the car being a symbol indicating the note
  1607. context, and the cdr is the heading to be used. The heading may also be the
  1608. empty string.
  1609. %t in the heading will be replaced by a time stamp.
  1610. %s will be replaced by the new TODO state, in double quotes.
  1611. %u will be replaced by the user name.
  1612. %U will be replaced by the full user name."
  1613. :group 'org-todo
  1614. :group 'org-progress
  1615. :type '(list :greedy t
  1616. (cons (const :tag "Heading when closing an item" done) string)
  1617. (cons (const :tag
  1618. "Heading when changing todo state (todo sequence only)"
  1619. state) string)
  1620. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1621. (defcustom org-log-states-order-reversed t
  1622. "Non-nil means, the latest state change note will be directly after heading.
  1623. When nil, the notes will be orderer according to time."
  1624. :group 'org-todo
  1625. :group 'org-progress
  1626. :type 'boolean)
  1627. (defcustom org-log-repeat 'time
  1628. "Non-nil means, record moving through the DONE state when triggering repeat.
  1629. An auto-repeating tasks is immediately switched back to TODO when marked
  1630. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1631. the TODO keyword definition, or recording a cloing note by setting
  1632. `org-log-done', there will be no record of the task moving trhough DONE.
  1633. This variable forces taking a note anyway. Possible values are:
  1634. nil Don't force a record
  1635. time Record a time stamp
  1636. note Record a note
  1637. This option can also be set with on a per-file-basis with
  1638. #+STARTUP: logrepeat
  1639. #+STARTUP: lognoterepeat
  1640. #+STARTUP: nologrepeat
  1641. You can have local logging settings for a subtree by setting the LOGGING
  1642. property to one or more of these keywords."
  1643. :group 'org-todo
  1644. :group 'org-progress
  1645. :type '(choice
  1646. (const :tag "Don't force a record" nil)
  1647. (const :tag "Force recording the DONE state" time)
  1648. (const :tag "Force recording a note with the DONE state" note)))
  1649. (defcustom org-clock-into-drawer 2
  1650. "Should clocking info be wrapped into a drawer?
  1651. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1652. If necessary, the drawer will be created.
  1653. When nil, the drawer will not be created, but used when present.
  1654. When an integer and the number of clocking entries in an item
  1655. reaches or exceeds this number, a drawer will be created."
  1656. :group 'org-todo
  1657. :group 'org-progress
  1658. :type '(choice
  1659. (const :tag "Always" t)
  1660. (const :tag "Only when drawer exists" nil)
  1661. (integer :tag "When at least N clock entries")))
  1662. (defcustom org-clock-out-when-done t
  1663. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1664. Nil means, clock will keep running until stopped explicitly with
  1665. `C-c C-x C-o', or until the clock is started in a different item."
  1666. :group 'org-progress
  1667. :type 'boolean)
  1668. (defcustom org-clock-in-switch-to-state nil
  1669. "Set task to a special todo state while clocking it.
  1670. The value should be the state to which the entry should be switched."
  1671. :group 'org-progress
  1672. :group 'org-todo
  1673. :type '(choice
  1674. (const :tag "Don't force a state" nil)
  1675. (string :tag "State")))
  1676. (defgroup org-priorities nil
  1677. "Priorities in Org-mode."
  1678. :tag "Org Priorities"
  1679. :group 'org-todo)
  1680. (defcustom org-highest-priority ?A
  1681. "The highest priority of TODO items. A character like ?A, ?B etc.
  1682. Must have a smaller ASCII number than `org-lowest-priority'."
  1683. :group 'org-priorities
  1684. :type 'character)
  1685. (defcustom org-lowest-priority ?C
  1686. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1687. Must have a larger ASCII number than `org-highest-priority'."
  1688. :group 'org-priorities
  1689. :type 'character)
  1690. (defcustom org-default-priority ?B
  1691. "The default priority of TODO items.
  1692. This is the priority an item get if no explicit priority is given."
  1693. :group 'org-priorities
  1694. :type 'character)
  1695. (defcustom org-priority-start-cycle-with-default t
  1696. "Non-nil means, start with default priority when starting to cycle.
  1697. When this is nil, the first step in the cycle will be (depending on the
  1698. command used) one higher or lower that the default priority."
  1699. :group 'org-priorities
  1700. :type 'boolean)
  1701. (defgroup org-time nil
  1702. "Options concerning time stamps and deadlines in Org-mode."
  1703. :tag "Org Time"
  1704. :group 'org)
  1705. (defcustom org-insert-labeled-timestamps-at-point nil
  1706. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1707. When nil, these labeled time stamps are forces into the second line of an
  1708. entry, just after the headline. When scheduling from the global TODO list,
  1709. the time stamp will always be forced into the second line."
  1710. :group 'org-time
  1711. :type 'boolean)
  1712. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1713. "Formats for `format-time-string' which are used for time stamps.
  1714. It is not recommended to change this constant.")
  1715. (defcustom org-time-stamp-rounding-minutes 0
  1716. "Number of minutes to round time stamps to upon insertion.
  1717. When zero, insert the time unmodified. Useful rounding numbers
  1718. should be factors of 60, so for example 5, 10, 15.
  1719. When this is not zero, you can still force an exact time-stamp by using
  1720. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1721. :group 'org-time
  1722. :type 'integer)
  1723. (defcustom org-display-custom-times nil
  1724. "Non-nil means, overlay custom formats over all time stamps.
  1725. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1726. To turn this on on a per-file basis, insert anywhere in the file:
  1727. #+STARTUP: customtime"
  1728. :group 'org-time
  1729. :set 'set-default
  1730. :type 'sexp)
  1731. (make-variable-buffer-local 'org-display-custom-times)
  1732. (defcustom org-time-stamp-custom-formats
  1733. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1734. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1735. These are overlayed over the default ISO format if the variable
  1736. `org-display-custom-times' is set. Time like %H:%M should be at the
  1737. end of the second format."
  1738. :group 'org-time
  1739. :type 'sexp)
  1740. (defun org-time-stamp-format (&optional long inactive)
  1741. "Get the right format for a time string."
  1742. (let ((f (if long (cdr org-time-stamp-formats)
  1743. (car org-time-stamp-formats))))
  1744. (if inactive
  1745. (concat "[" (substring f 1 -1) "]")
  1746. f)))
  1747. (defcustom org-read-date-prefer-future t
  1748. "Non-nil means, assume future for incomplete date input from user.
  1749. This affects the following situations:
  1750. 1. The user gives a day, but no month.
  1751. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1752. read this as the third of *next* month. However, if you enter \"17\",
  1753. it will be considered as *this* month.
  1754. 2. The user gives a month but not a year.
  1755. For example, if it is april and you enter \"feb 2\", this will be read
  1756. as feb 2, *next* year. \"May 5\", however, will be this year.
  1757. When this option is nil, the current month and year will always be used
  1758. as defaults."
  1759. :group 'org-time
  1760. :type 'boolean)
  1761. (defcustom org-read-date-display-live t
  1762. "Non-nil means, display current interpretation of date prompt live.
  1763. This display will be in an overlay, in the minibuffer."
  1764. :group 'org-time
  1765. :type 'boolean)
  1766. (defcustom org-read-date-popup-calendar t
  1767. "Non-nil means, pop up a calendar when prompting for a date.
  1768. In the calendar, the date can be selected with mouse-1. However, the
  1769. minibuffer will also be active, and you can simply enter the date as well.
  1770. When nil, only the minibuffer will be available."
  1771. :group 'org-time
  1772. :type 'boolean)
  1773. (if (fboundp 'defvaralias)
  1774. (defvaralias 'org-popup-calendar-for-date-prompt
  1775. 'org-read-date-popup-calendar))
  1776. (defcustom org-extend-today-until 0
  1777. "The hour when your day really ends.
  1778. This has influence for the following applications:
  1779. - When switching the agenda to \"today\". It it is still earlier than
  1780. the time given here, the day recognized as TODAY is actually yesterday.
  1781. - When a date is read from the user and it is still before the time given
  1782. here, the current date and time will be assumed to be yesterday, 23:59.
  1783. FIXME:
  1784. IMPORTANT: This is still a very experimental feature, it may disappear
  1785. again or it may be extended to mean more things."
  1786. :group 'org-time
  1787. :type 'number)
  1788. (defcustom org-edit-timestamp-down-means-later nil
  1789. "Non-nil means, S-down will increase the time in a time stamp.
  1790. When nil, S-up will increase."
  1791. :group 'org-time
  1792. :type 'boolean)
  1793. (defcustom org-calendar-follow-timestamp-change t
  1794. "Non-nil means, make the calendar window follow timestamp changes.
  1795. When a timestamp is modified and the calendar window is visible, it will be
  1796. moved to the new date."
  1797. :group 'org-time
  1798. :type 'boolean)
  1799. (defcustom org-clock-heading-function nil
  1800. "When non-nil, should be a function to create `org-clock-heading'.
  1801. This is the string shown in the mode line when a clock is running.
  1802. The function is called with point at the beginning of the headline."
  1803. :group 'org-time ; FIXME: Should we have a separate group????
  1804. :type 'function)
  1805. (defgroup org-tags nil
  1806. "Options concerning tags in Org-mode."
  1807. :tag "Org Tags"
  1808. :group 'org)
  1809. (defcustom org-tag-alist nil
  1810. "List of tags allowed in Org-mode files.
  1811. When this list is nil, Org-mode will base TAG input on what is already in the
  1812. buffer.
  1813. The value of this variable is an alist, the car of each entry must be a
  1814. keyword as a string, the cdr may be a character that is used to select
  1815. that tag through the fast-tag-selection interface.
  1816. See the manual for details."
  1817. :group 'org-tags
  1818. :type '(repeat
  1819. (choice
  1820. (cons (string :tag "Tag name")
  1821. (character :tag "Access char"))
  1822. (const :tag "Start radio group" (:startgroup))
  1823. (const :tag "End radio group" (:endgroup)))))
  1824. (defcustom org-use-fast-tag-selection 'auto
  1825. "Non-nil means, use fast tag selection scheme.
  1826. This is a special interface to select and deselect tags with single keys.
  1827. When nil, fast selection is never used.
  1828. When the symbol `auto', fast selection is used if and only if selection
  1829. characters for tags have been configured, either through the variable
  1830. `org-tag-alist' or through a #+TAGS line in the buffer.
  1831. When t, fast selection is always used and selection keys are assigned
  1832. automatically if necessary."
  1833. :group 'org-tags
  1834. :type '(choice
  1835. (const :tag "Always" t)
  1836. (const :tag "Never" nil)
  1837. (const :tag "When selection characters are configured" 'auto)))
  1838. (defcustom org-fast-tag-selection-single-key nil
  1839. "Non-nil means, fast tag selection exits after first change.
  1840. When nil, you have to press RET to exit it.
  1841. During fast tag selection, you can toggle this flag with `C-c'.
  1842. This variable can also have the value `expert'. In this case, the window
  1843. displaying the tags menu is not even shown, until you press C-c again."
  1844. :group 'org-tags
  1845. :type '(choice
  1846. (const :tag "No" nil)
  1847. (const :tag "Yes" t)
  1848. (const :tag "Expert" expert)))
  1849. (defvar org-fast-tag-selection-include-todo nil
  1850. "Non-nil means, fast tags selection interface will also offer TODO states.
  1851. This is an undocumented feature, you should not rely on it.")
  1852. (defcustom org-tags-column -80
  1853. "The column to which tags should be indented in a headline.
  1854. If this number is positive, it specifies the column. If it is negative,
  1855. it means that the tags should be flushright to that column. For example,
  1856. -80 works well for a normal 80 character screen."
  1857. :group 'org-tags
  1858. :type 'integer)
  1859. (defcustom org-auto-align-tags t
  1860. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1861. These operations change the length of a headline and therefore shift
  1862. the tags around. With this options turned on, after each such operation
  1863. the tags are again aligned to `org-tags-column'."
  1864. :group 'org-tags
  1865. :type 'boolean)
  1866. (defcustom org-use-tag-inheritance t
  1867. "Non-nil means, tags in levels apply also for sublevels.
  1868. When nil, only the tags directly given in a specific line apply there.
  1869. If you turn off this option, you very likely want to turn on the
  1870. companion option `org-tags-match-list-sublevels'."
  1871. :group 'org-tags
  1872. :type 'boolean)
  1873. (defcustom org-tags-match-list-sublevels nil
  1874. "Non-nil means list also sublevels of headlines matching tag search.
  1875. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1876. the sublevels of a headline matching a tag search often also match
  1877. the same search. Listing all of them can create very long lists.
  1878. Setting this variable to nil causes subtrees of a match to be skipped.
  1879. This option is off by default, because inheritance in on. If you turn
  1880. inheritance off, you very likely want to turn this option on.
  1881. As a special case, if the tag search is restricted to TODO items, the
  1882. value of this variable is ignored and sublevels are always checked, to
  1883. make sure all corresponding TODO items find their way into the list."
  1884. :group 'org-tags
  1885. :type 'boolean)
  1886. (defvar org-tags-history nil
  1887. "History of minibuffer reads for tags.")
  1888. (defvar org-last-tags-completion-table nil
  1889. "The last used completion table for tags.")
  1890. (defvar org-after-tags-change-hook nil
  1891. "Hook that is run after the tags in a line have changed.")
  1892. (defgroup org-properties nil
  1893. "Options concerning properties in Org-mode."
  1894. :tag "Org Properties"
  1895. :group 'org)
  1896. (defcustom org-property-format "%-10s %s"
  1897. "How property key/value pairs should be formatted by `indent-line'.
  1898. When `indent-line' hits a property definition, it will format the line
  1899. according to this format, mainly to make sure that the values are
  1900. lined-up with respect to each other."
  1901. :group 'org-properties
  1902. :type 'string)
  1903. (defcustom org-use-property-inheritance nil
  1904. "Non-nil means, properties apply also for sublevels.
  1905. This setting is only relevant during property searches, not when querying
  1906. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1907. you need to call `org-entry-get' with the inheritance flag.
  1908. Turning this on can cause significant overhead when doing a search, so
  1909. this is turned off by default.
  1910. When nil, only the properties directly given in the current entry count.
  1911. The value may also be a list of properties that shouldhave inheritance.
  1912. However, note that some special properties use inheritance under special
  1913. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1914. and the properties ending in \"_ALL\" when they are used as descriptor
  1915. for valid values of a property."
  1916. :group 'org-properties
  1917. :type '(choice
  1918. (const :tag "Not" nil)
  1919. (const :tag "Always" nil)
  1920. (repeat :tag "Specific properties" (string :tag "Property"))))
  1921. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1922. "The default column format, if no other format has been defined.
  1923. This variable can be set on the per-file basis by inserting a line
  1924. #+COLUMNS: %25ITEM ....."
  1925. :group 'org-properties
  1926. :type 'string)
  1927. (defcustom org-global-properties nil
  1928. "List of property/value pairs that can be inherited by any entry.
  1929. You can set buffer-local values for this by adding lines like
  1930. #+PROPERTY: NAME VALUE"
  1931. :group 'org-properties
  1932. :type '(repeat
  1933. (cons (string :tag "Property")
  1934. (string :tag "Value"))))
  1935. (defvar org-local-properties nil
  1936. "List of property/value pairs that can be inherited by any entry.
  1937. Valid for the current buffer.
  1938. This variable is populated from #+PROPERTY lines.")
  1939. (defgroup org-agenda nil
  1940. "Options concerning agenda views in Org-mode."
  1941. :tag "Org Agenda"
  1942. :group 'org)
  1943. (defvar org-category nil
  1944. "Variable used by org files to set a category for agenda display.
  1945. Such files should use a file variable to set it, for example
  1946. # -*- mode: org; org-category: \"ELisp\"
  1947. or contain a special line
  1948. #+CATEGORY: ELisp
  1949. If the file does not specify a category, then file's base name
  1950. is used instead.")
  1951. (make-variable-buffer-local 'org-category)
  1952. (defcustom org-agenda-files nil
  1953. "The files to be used for agenda display.
  1954. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1955. \\[org-remove-file]. You can also use customize to edit the list.
  1956. If an entry is a directory, all files in that directory that are matched by
  1957. `org-agenda-file-regexp' will be part of the file list.
  1958. If the value of the variable is not a list but a single file name, then
  1959. the list of agenda files is actually stored and maintained in that file, one
  1960. agenda file per line."
  1961. :group 'org-agenda
  1962. :type '(choice
  1963. (repeat :tag "List of files and directories" file)
  1964. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1965. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1966. "Regular expression to match files for `org-agenda-files'.
  1967. If any element in the list in that variable contains a directory instead
  1968. of a normal file, all files in that directory that are matched by this
  1969. regular expression will be included."
  1970. :group 'org-agenda
  1971. :type 'regexp)
  1972. (defcustom org-agenda-skip-unavailable-files nil
  1973. "t means to just skip non-reachable files in `org-agenda-files'.
  1974. Nil means to remove them, after a query, from the list."
  1975. :group 'org-agenda
  1976. :type 'boolean)
  1977. (defcustom org-agenda-text-search-extra-files nil
  1978. "List of extra files to be searched by text search commands.
  1979. These files will be search in addition to the agenda files bu the
  1980. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1981. Note that these files will only be searched for text search commands,
  1982. not for the other agenda views like todo lists, tag earches or the weekly
  1983. agenda. This variable is intended to list notes and possibly archive files
  1984. that should also be searched by these two commands."
  1985. :group 'org-agenda
  1986. :type '(repeat file))
  1987. (if (fboundp 'defvaralias)
  1988. (defvaralias 'org-agenda-multi-occur-extra-files
  1989. 'org-agenda-text-search-extra-files))
  1990. (defcustom org-agenda-confirm-kill 1
  1991. "When set, remote killing from the agenda buffer needs confirmation.
  1992. When t, a confirmation is always needed. When a number N, confirmation is
  1993. only needed when the text to be killed contains more than N non-white lines."
  1994. :group 'org-agenda
  1995. :type '(choice
  1996. (const :tag "Never" nil)
  1997. (const :tag "Always" t)
  1998. (number :tag "When more than N lines")))
  1999. (defcustom org-calendar-to-agenda-key [?c]
  2000. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2001. The command `org-calendar-goto-agenda' will be bound to this key. The
  2002. default is the character `c' because then `c' can be used to switch back and
  2003. forth between agenda and calendar."
  2004. :group 'org-agenda
  2005. :type 'sexp)
  2006. (defcustom org-agenda-compact-blocks nil
  2007. "Non-nil means, make the block agenda more compact.
  2008. This is done by leaving out unnecessary lines."
  2009. :group 'org-agenda
  2010. :type nil)
  2011. (defgroup org-agenda-export nil
  2012. "Options concerning exporting agenda views in Org-mode."
  2013. :tag "Org Agenda Export"
  2014. :group 'org-agenda)
  2015. (defcustom org-agenda-with-colors t
  2016. "Non-nil means, use colors in agenda views."
  2017. :group 'org-agenda-export
  2018. :type 'boolean)
  2019. (defcustom org-agenda-exporter-settings nil
  2020. "Alist of variable/value pairs that should be active during agenda export.
  2021. This is a good place to set uptions for ps-print and for htmlize."
  2022. :group 'org-agenda-export
  2023. :type '(repeat
  2024. (list
  2025. (variable)
  2026. (sexp :tag "Value"))))
  2027. (defcustom org-agenda-export-html-style ""
  2028. "The style specification for exported HTML Agenda files.
  2029. If this variable contains a string, it will replace the default <style>
  2030. section as produced by `htmlize'.
  2031. Since there are different ways of setting style information, this variable
  2032. needs to contain the full HTML structure to provide a style, including the
  2033. surrounding HTML tags. The style specifications should include definitions
  2034. the fonts used by the agenda, here is an example:
  2035. <style type=\"text/css\">
  2036. p { font-weight: normal; color: gray; }
  2037. .org-agenda-structure {
  2038. font-size: 110%;
  2039. color: #003399;
  2040. font-weight: 600;
  2041. }
  2042. .org-todo {
  2043. color: #cc6666;Week-agenda:
  2044. font-weight: bold;
  2045. }
  2046. .org-done {
  2047. color: #339933;
  2048. }
  2049. .title { text-align: center; }
  2050. .todo, .deadline { color: red; }
  2051. .done { color: green; }
  2052. </style>
  2053. or, if you want to keep the style in a file,
  2054. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2055. As the value of this option simply gets inserted into the HTML <head> header,
  2056. you can \"misuse\" it to also add other text to the header. However,
  2057. <style>...</style> is required, if not present the variable will be ignored."
  2058. :group 'org-agenda-export
  2059. :group 'org-export-html
  2060. :type 'string)
  2061. (defgroup org-agenda-custom-commands nil
  2062. "Options concerning agenda views in Org-mode."
  2063. :tag "Org Agenda Custom Commands"
  2064. :group 'org-agenda)
  2065. (defcustom org-agenda-custom-commands nil
  2066. "Custom commands for the agenda.
  2067. These commands will be offered on the splash screen displayed by the
  2068. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  2069. (key desc type match options files)
  2070. key The key (one or more characters as a string) to be associated
  2071. with the command.
  2072. desc A description of the commend, when omitted or nil, a default
  2073. description is built using MATCH.
  2074. type The command type, any of the following symbols:
  2075. agenda The daily/weekly agenda.
  2076. todo Entries with a specific TODO keyword, in all agenda files.
  2077. search Entries containing search words entry or headline.
  2078. tags Tags/Property/TODO match in all agenda files.
  2079. tags-todo Tags/P/T match in all agenda files, TODO entries only.
  2080. todo-tree Sparse tree of specific TODO keyword in *current* file.
  2081. tags-tree Sparse tree with all tags matches in *current* file.
  2082. occur-tree Occur sparse tree for *current* file.
  2083. ... A user-defined function.
  2084. match What to search for:
  2085. - a single keyword for TODO keyword searches
  2086. - a tags match expression for tags searches
  2087. - a regular expression for occur searches
  2088. options A list of option settings, similar to that in a let form, so like
  2089. this: ((opt1 val1) (opt2 val2) ...)
  2090. files A list of files file to write the produced agenda buffer to
  2091. with the command `org-store-agenda-views'.
  2092. If a file name ends in \".html\", an HTML version of the buffer
  2093. is written out. If it ends in \".ps\", a postscript version is
  2094. produced. Otherwide, only the plain text is written to the file.
  2095. You can also define a set of commands, to create a composite agenda buffer.
  2096. In this case, an entry looks like this:
  2097. (key desc (cmd1 cmd2 ...) general-options file)
  2098. where
  2099. desc A description string to be displayed in the dispatcher menu.
  2100. cmd An agenda command, similar to the above. However, tree commands
  2101. are no allowed, but instead you can get agenda and global todo list.
  2102. So valid commands for a set are:
  2103. (agenda)
  2104. (alltodo)
  2105. (stuck)
  2106. (todo \"match\" options files)
  2107. (search \"match\" options files)
  2108. (tags \"match\" options files)
  2109. (tags-todo \"match\" options files)
  2110. Each command can carry a list of options, and another set of options can be
  2111. given for the whole set of commands. Individual command options take
  2112. precedence over the general options.
  2113. When using several characters as key to a command, the first characters
  2114. are prefix commands. For the dispatcher to display useful information, you
  2115. should provide a description for the prefix, like
  2116. (setq org-agenda-custom-commands
  2117. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2118. (\"hl\" tags \"+HOME+Lisa\")
  2119. (\"hp\" tags \"+HOME+Peter\")
  2120. (\"hk\" tags \"+HOME+Kim\")))"
  2121. :group 'org-agenda-custom-commands
  2122. :type '(repeat
  2123. (choice :value ("a" "" tags "" nil)
  2124. (list :tag "Single command"
  2125. (string :tag "Access Key(s) ")
  2126. (option (string :tag "Description"))
  2127. (choice
  2128. (const :tag "Agenda" agenda)
  2129. (const :tag "TODO list" alltodo)
  2130. (const :tag "Search words" search)
  2131. (const :tag "Stuck projects" stuck)
  2132. (const :tag "Tags search (all agenda files)" tags)
  2133. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2134. (const :tag "TODO keyword search (all agenda files)" todo)
  2135. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2136. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2137. (const :tag "Occur tree (current buffer)" occur-tree)
  2138. (sexp :tag "Other, user-defined function"))
  2139. (string :tag "Match")
  2140. (repeat :tag "Local options"
  2141. (list (variable :tag "Option") (sexp :tag "Value")))
  2142. (option (repeat :tag "Export" (file :tag "Export to"))))
  2143. (list :tag "Command series, all agenda files"
  2144. (string :tag "Access Key(s)")
  2145. (string :tag "Description ")
  2146. (repeat
  2147. (choice
  2148. (const :tag "Agenda" (agenda))
  2149. (const :tag "TODO list" (alltodo))
  2150. (list :tag "Search words"
  2151. (const :format "" search)
  2152. (string :tag "Match")
  2153. (repeat :tag "Local options"
  2154. (list (variable :tag "Option")
  2155. (sexp :tag "Value"))))
  2156. (const :tag "Stuck projects" (stuck))
  2157. (list :tag "Tags search"
  2158. (const :format "" tags)
  2159. (string :tag "Match")
  2160. (repeat :tag "Local options"
  2161. (list (variable :tag "Option")
  2162. (sexp :tag "Value"))))
  2163. (list :tag "Tags search, TODO entries only"
  2164. (const :format "" tags-todo)
  2165. (string :tag "Match")
  2166. (repeat :tag "Local options"
  2167. (list (variable :tag "Option")
  2168. (sexp :tag "Value"))))
  2169. (list :tag "TODO keyword search"
  2170. (const :format "" todo)
  2171. (string :tag "Match")
  2172. (repeat :tag "Local options"
  2173. (list (variable :tag "Option")
  2174. (sexp :tag "Value"))))
  2175. (list :tag "Other, user-defined function"
  2176. (symbol :tag "function")
  2177. (string :tag "Match")
  2178. (repeat :tag "Local options"
  2179. (list (variable :tag "Option")
  2180. (sexp :tag "Value"))))))
  2181. (repeat :tag "General options"
  2182. (list (variable :tag "Option")
  2183. (sexp :tag "Value")))
  2184. (option (repeat :tag "Export" (file :tag "Export to"))))
  2185. (cons :tag "Prefix key documentation"
  2186. (string :tag "Access Key(s)")
  2187. (string :tag "Description ")))))
  2188. (defcustom org-agenda-query-register ?o
  2189. "The register holding the current query string.
  2190. The prupose of this is that if you construct a query string interactively,
  2191. you can then use it to define a custom command."
  2192. :group 'org-agenda-custom-commands
  2193. :type 'character)
  2194. (defcustom org-stuck-projects
  2195. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2196. "How to identify stuck projects.
  2197. This is a list of four items:
  2198. 1. A tags/todo matcher string that is used to identify a project.
  2199. The entire tree below a headline matched by this is considered one project.
  2200. 2. A list of TODO keywords identifying non-stuck projects.
  2201. If the project subtree contains any headline with one of these todo
  2202. keywords, the project is considered to be not stuck. If you specify
  2203. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2204. 3. A list of tags identifying non-stuck projects.
  2205. If the project subtree contains any headline with one of these tags,
  2206. the project is considered to be not stuck. If you specify \"*\" as
  2207. a tag, any tag will mark the project unstuck.
  2208. 4. An arbitrary regular expression matching non-stuck projects.
  2209. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2210. or `C-c a #' to produce the list."
  2211. :group 'org-agenda-custom-commands
  2212. :type '(list
  2213. (string :tag "Tags/TODO match to identify a project")
  2214. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2215. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2216. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2217. (defgroup org-agenda-skip nil
  2218. "Options concerning skipping parts of agenda files."
  2219. :tag "Org Agenda Skip"
  2220. :group 'org-agenda)
  2221. (defcustom org-agenda-todo-list-sublevels t
  2222. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2223. When nil, the sublevels of a TODO entry are not checked, resulting in
  2224. potentially much shorter TODO lists."
  2225. :group 'org-agenda-skip
  2226. :group 'org-todo
  2227. :type 'boolean)
  2228. (defcustom org-agenda-todo-ignore-with-date nil
  2229. "Non-nil means, don't show entries with a date in the global todo list.
  2230. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2231. but don't want them to show up in the TODO list.
  2232. When this is set, it also covers deadlines and scheduled items, the settings
  2233. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2234. will be ignored."
  2235. :group 'org-agenda-skip
  2236. :group 'org-todo
  2237. :type 'boolean)
  2238. (defcustom org-agenda-todo-ignore-scheduled nil
  2239. "Non-nil means, don't show scheduled entries in the global todo list.
  2240. The idea behind this is that by scheduling it, you have already taken care
  2241. of this item.
  2242. See also `org-agenda-todo-ignore-with-date'."
  2243. :group 'org-agenda-skip
  2244. :group 'org-todo
  2245. :type 'boolean)
  2246. (defcustom org-agenda-todo-ignore-deadlines nil
  2247. "Non-nil means, don't show near deadline entries in the global todo list.
  2248. Near means closer than `org-deadline-warning-days' days.
  2249. The idea behind this is that such items will appear in the agenda anyway.
  2250. See also `org-agenda-todo-ignore-with-date'."
  2251. :group 'org-agenda-skip
  2252. :group 'org-todo
  2253. :type 'boolean)
  2254. (defcustom org-agenda-skip-scheduled-if-done nil
  2255. "Non-nil means don't show scheduled items in agenda when they are done.
  2256. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2257. it applies only to the actual date of the scheduling. Warnings about
  2258. an item with a past scheduling dates are always turned off when the item
  2259. is DONE."
  2260. :group 'org-agenda-skip
  2261. :type 'boolean)
  2262. (defcustom org-agenda-skip-deadline-if-done nil
  2263. "Non-nil means don't show deadines when the corresponding item is done.
  2264. When nil, the deadline is still shown and should give you a happy feeling.
  2265. This is relevant for the daily/weekly agenda. And it applied only to the
  2266. actualy date of the deadline. Warnings about approching and past-due
  2267. deadlines are always turned off when the item is DONE."
  2268. :group 'org-agenda-skip
  2269. :type 'boolean)
  2270. (defcustom org-agenda-skip-timestamp-if-done nil
  2271. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2272. :group 'org-agenda-skip
  2273. :type 'boolean)
  2274. (defcustom org-timeline-show-empty-dates 3
  2275. "Non-nil means, `org-timeline' also shows dates without an entry.
  2276. When nil, only the days which actually have entries are shown.
  2277. When t, all days between the first and the last date are shown.
  2278. When an integer, show also empty dates, but if there is a gap of more than
  2279. N days, just insert a special line indicating the size of the gap."
  2280. :group 'org-agenda-skip
  2281. :type '(choice
  2282. (const :tag "None" nil)
  2283. (const :tag "All" t)
  2284. (number :tag "at most")))
  2285. (defgroup org-agenda-startup nil
  2286. "Options concerning initial settings in the Agenda in Org Mode."
  2287. :tag "Org Agenda Startup"
  2288. :group 'org-agenda)
  2289. (defcustom org-finalize-agenda-hook nil
  2290. "Hook run just before displaying an agenda buffer."
  2291. :group 'org-agenda-startup
  2292. :type 'hook)
  2293. (defcustom org-agenda-mouse-1-follows-link nil
  2294. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2295. A longer mouse click will still set point. Does not work on XEmacs.
  2296. Needs to be set before org.el is loaded."
  2297. :group 'org-agenda-startup
  2298. :type 'boolean)
  2299. (defcustom org-agenda-start-with-follow-mode nil
  2300. "The initial value of follow-mode in a newly created agenda window."
  2301. :group 'org-agenda-startup
  2302. :type 'boolean)
  2303. (defgroup org-agenda-windows nil
  2304. "Options concerning the windows used by the Agenda in Org Mode."
  2305. :tag "Org Agenda Windows"
  2306. :group 'org-agenda)
  2307. (defcustom org-agenda-window-setup 'reorganize-frame
  2308. "How the agenda buffer should be displayed.
  2309. Possible values for this option are:
  2310. current-window Show agenda in the current window, keeping all other windows.
  2311. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2312. other-window Use `switch-to-buffer-other-window' to display agenda.
  2313. reorganize-frame Show only two windows on the current frame, the current
  2314. window and the agenda.
  2315. See also the variable `org-agenda-restore-windows-after-quit'."
  2316. :group 'org-agenda-windows
  2317. :type '(choice
  2318. (const current-window)
  2319. (const other-frame)
  2320. (const other-window)
  2321. (const reorganize-frame)))
  2322. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2323. "The min and max height of the agenda window as a fraction of frame height.
  2324. The value of the variable is a cons cell with two numbers between 0 and 1.
  2325. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2326. :group 'org-agenda-windows
  2327. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2328. (defcustom org-agenda-restore-windows-after-quit nil
  2329. "Non-nil means, restore window configuration open exiting agenda.
  2330. Before the window configuration is changed for displaying the agenda,
  2331. the current status is recorded. When the agenda is exited with
  2332. `q' or `x' and this option is set, the old state is restored. If
  2333. `org-agenda-window-setup' is `other-frame', the value of this
  2334. option will be ignored.."
  2335. :group 'org-agenda-windows
  2336. :type 'boolean)
  2337. (defcustom org-indirect-buffer-display 'other-window
  2338. "How should indirect tree buffers be displayed?
  2339. This applies to indirect buffers created with the commands
  2340. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2341. Valid values are:
  2342. current-window Display in the current window
  2343. other-window Just display in another window.
  2344. dedicated-frame Create one new frame, and re-use it each time.
  2345. new-frame Make a new frame each time. Note that in this case
  2346. previously-made indirect buffers are kept, and you need to
  2347. kill these buffers yourself."
  2348. :group 'org-structure
  2349. :group 'org-agenda-windows
  2350. :type '(choice
  2351. (const :tag "In current window" current-window)
  2352. (const :tag "In current frame, other window" other-window)
  2353. (const :tag "Each time a new frame" new-frame)
  2354. (const :tag "One dedicated frame" dedicated-frame)))
  2355. (defgroup org-agenda-daily/weekly nil
  2356. "Options concerning the daily/weekly agenda."
  2357. :tag "Org Agenda Daily/Weekly"
  2358. :group 'org-agenda)
  2359. (defcustom org-agenda-ndays 7
  2360. "Number of days to include in overview display.
  2361. Should be 1 or 7."
  2362. :group 'org-agenda-daily/weekly
  2363. :type 'number)
  2364. (defcustom org-agenda-start-on-weekday 1
  2365. "Non-nil means, start the overview always on the specified weekday.
  2366. 0 denotes Sunday, 1 denotes Monday etc.
  2367. When nil, always start on the current day."
  2368. :group 'org-agenda-daily/weekly
  2369. :type '(choice (const :tag "Today" nil)
  2370. (number :tag "Weekday No.")))
  2371. (defcustom org-agenda-show-all-dates t
  2372. "Non-nil means, `org-agenda' shows every day in the selected range.
  2373. When nil, only the days which actually have entries are shown."
  2374. :group 'org-agenda-daily/weekly
  2375. :type 'boolean)
  2376. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2377. "Format string for displaying dates in the agenda.
  2378. Used by the daily/weekly agenda and by the timeline. This should be
  2379. a format string understood by `format-time-string', or a function returning
  2380. the formatted date as a string. The function must take a single argument,
  2381. a calendar-style date list like (month day year)."
  2382. :group 'org-agenda-daily/weekly
  2383. :type '(choice
  2384. (string :tag "Format string")
  2385. (function :tag "Function")))
  2386. (defun org-agenda-format-date-aligned (date)
  2387. "Format a date string for display in the daily/weekly agenda, or timeline.
  2388. This function makes sure that dates are aligned for easy reading."
  2389. (format "%-9s %2d %s %4d"
  2390. (calendar-day-name date)
  2391. (extract-calendar-day date)
  2392. (calendar-month-name (extract-calendar-month date))
  2393. (extract-calendar-year date)))
  2394. (defcustom org-agenda-include-diary nil
  2395. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2396. :group 'org-agenda-daily/weekly
  2397. :type 'boolean)
  2398. (defcustom org-agenda-include-all-todo nil
  2399. "Set means weekly/daily agenda will always contain all TODO entries.
  2400. The TODO entries will be listed at the top of the agenda, before
  2401. the entries for specific days."
  2402. :group 'org-agenda-daily/weekly
  2403. :type 'boolean)
  2404. (defcustom org-agenda-repeating-timestamp-show-all t
  2405. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2406. When nil, only one occurence is shown, either today or the
  2407. nearest into the future."
  2408. :group 'org-agenda-daily/weekly
  2409. :type 'boolean)
  2410. (defcustom org-deadline-warning-days 14
  2411. "No. of days before expiration during which a deadline becomes active.
  2412. This variable governs the display in sparse trees and in the agenda.
  2413. When 0 or negative, it means use this number (the absolute value of it)
  2414. even if a deadline has a different individual lead time specified."
  2415. :group 'org-time
  2416. :group 'org-agenda-daily/weekly
  2417. :type 'number)
  2418. (defcustom org-scheduled-past-days 10000
  2419. "No. of days to continue listing scheduled items that are not marked DONE.
  2420. When an item is scheduled on a date, it shows up in the agenda on this
  2421. day and will be listed until it is marked done for the number of days
  2422. given here."
  2423. :group 'org-agenda-daily/weekly
  2424. :type 'number)
  2425. (defgroup org-agenda-time-grid nil
  2426. "Options concerning the time grid in the Org-mode Agenda."
  2427. :tag "Org Agenda Time Grid"
  2428. :group 'org-agenda)
  2429. (defcustom org-agenda-use-time-grid t
  2430. "Non-nil means, show a time grid in the agenda schedule.
  2431. A time grid is a set of lines for specific times (like every two hours between
  2432. 8:00 and 20:00). The items scheduled for a day at specific times are
  2433. sorted in between these lines.
  2434. For details about when the grid will be shown, and what it will look like, see
  2435. the variable `org-agenda-time-grid'."
  2436. :group 'org-agenda-time-grid
  2437. :type 'boolean)
  2438. (defcustom org-agenda-time-grid
  2439. '((daily today require-timed)
  2440. "----------------"
  2441. (800 1000 1200 1400 1600 1800 2000))
  2442. "The settings for time grid for agenda display.
  2443. This is a list of three items. The first item is again a list. It contains
  2444. symbols specifying conditions when the grid should be displayed:
  2445. daily if the agenda shows a single day
  2446. weekly if the agenda shows an entire week
  2447. today show grid on current date, independent of daily/weekly display
  2448. require-timed show grid only if at least one item has a time specification
  2449. The second item is a string which will be places behing the grid time.
  2450. The third item is a list of integers, indicating the times that should have
  2451. a grid line."
  2452. :group 'org-agenda-time-grid
  2453. :type
  2454. '(list
  2455. (set :greedy t :tag "Grid Display Options"
  2456. (const :tag "Show grid in single day agenda display" daily)
  2457. (const :tag "Show grid in weekly agenda display" weekly)
  2458. (const :tag "Always show grid for today" today)
  2459. (const :tag "Show grid only if any timed entries are present"
  2460. require-timed)
  2461. (const :tag "Skip grid times already present in an entry"
  2462. remove-match))
  2463. (string :tag "Grid String")
  2464. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2465. (defgroup org-agenda-sorting nil
  2466. "Options concerning sorting in the Org-mode Agenda."
  2467. :tag "Org Agenda Sorting"
  2468. :group 'org-agenda)
  2469. (defconst org-sorting-choice
  2470. '(choice
  2471. (const time-up) (const time-down)
  2472. (const category-keep) (const category-up) (const category-down)
  2473. (const tag-down) (const tag-up)
  2474. (const priority-up) (const priority-down))
  2475. "Sorting choices.")
  2476. (defcustom org-agenda-sorting-strategy
  2477. '((agenda time-up category-keep priority-down)
  2478. (todo category-keep priority-down)
  2479. (tags category-keep priority-down)
  2480. (search category-keep))
  2481. "Sorting structure for the agenda items of a single day.
  2482. This is a list of symbols which will be used in sequence to determine
  2483. if an entry should be listed before another entry. The following
  2484. symbols are recognized:
  2485. time-up Put entries with time-of-day indications first, early first
  2486. time-down Put entries with time-of-day indications first, late first
  2487. category-keep Keep the default order of categories, corresponding to the
  2488. sequence in `org-agenda-files'.
  2489. category-up Sort alphabetically by category, A-Z.
  2490. category-down Sort alphabetically by category, Z-A.
  2491. tag-up Sort alphabetically by last tag, A-Z.
  2492. tag-down Sort alphabetically by last tag, Z-A.
  2493. priority-up Sort numerically by priority, high priority last.
  2494. priority-down Sort numerically by priority, high priority first.
  2495. The different possibilities will be tried in sequence, and testing stops
  2496. if one comparison returns a \"not-equal\". For example, the default
  2497. '(time-up category-keep priority-down)
  2498. means: Pull out all entries having a specified time of day and sort them,
  2499. in order to make a time schedule for the current day the first thing in the
  2500. agenda listing for the day. Of the entries without a time indication, keep
  2501. the grouped in categories, don't sort the categories, but keep them in
  2502. the sequence given in `org-agenda-files'. Within each category sort by
  2503. priority.
  2504. Leaving out `category-keep' would mean that items will be sorted across
  2505. categories by priority.
  2506. Instead of a single list, this can also be a set of list for specific
  2507. contents, with a context symbol in the car of the list, any of
  2508. `agenda', `todo', `tags' for the corresponding agenda views."
  2509. :group 'org-agenda-sorting
  2510. :type `(choice
  2511. (repeat :tag "General" ,org-sorting-choice)
  2512. (list :tag "Individually"
  2513. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2514. (repeat ,org-sorting-choice))
  2515. (cons (const :tag "Strategy for TODO lists" todo)
  2516. (repeat ,org-sorting-choice))
  2517. (cons (const :tag "Strategy for Tags matches" tags)
  2518. (repeat ,org-sorting-choice)))))
  2519. (defcustom org-sort-agenda-notime-is-late t
  2520. "Non-nil means, items without time are considered late.
  2521. This is only relevant for sorting. When t, items which have no explicit
  2522. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2523. do have a time. When nil, the default time is before 0:00. You can use this
  2524. option to decide if the schedule for today should come before or after timeless
  2525. agenda entries."
  2526. :group 'org-agenda-sorting
  2527. :type 'boolean)
  2528. (defgroup org-agenda-line-format nil
  2529. "Options concerning the entry prefix in the Org-mode agenda display."
  2530. :tag "Org Agenda Line Format"
  2531. :group 'org-agenda)
  2532. (defcustom org-agenda-prefix-format
  2533. '((agenda . " %-12:c%?-12t% s")
  2534. (timeline . " % s")
  2535. (todo . " %-12:c")
  2536. (tags . " %-12:c")
  2537. (search . " %-12:c"))
  2538. "Format specifications for the prefix of items in the agenda views.
  2539. An alist with four entries, for the different agenda types. The keys to the
  2540. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2541. are format strings.
  2542. This format works similar to a printf format, with the following meaning:
  2543. %c the category of the item, \"Diary\" for entries from the diary, or
  2544. as given by the CATEGORY keyword or derived from the file name.
  2545. %T the *last* tag of the item. Last because inherited tags come
  2546. first in the list.
  2547. %t the time-of-day specification if one applies to the entry, in the
  2548. format HH:MM
  2549. %s Scheduling/Deadline information, a short string
  2550. All specifiers work basically like the standard `%s' of printf, but may
  2551. contain two additional characters: A question mark just after the `%' and
  2552. a whitespace/punctuation character just before the final letter.
  2553. If the first character after `%' is a question mark, the entire field
  2554. will only be included if the corresponding value applies to the
  2555. current entry. This is useful for fields which should have fixed
  2556. width when present, but zero width when absent. For example,
  2557. \"%?-12t\" will result in a 12 character time field if a time of the
  2558. day is specified, but will completely disappear in entries which do
  2559. not contain a time.
  2560. If there is punctuation or whitespace character just before the final
  2561. format letter, this character will be appended to the field value if
  2562. the value is not empty. For example, the format \"%-12:c\" leads to
  2563. \"Diary: \" if the category is \"Diary\". If the category were be
  2564. empty, no additional colon would be interted.
  2565. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2566. - Indent the line with two space characters
  2567. - Give the category in a 12 chars wide field, padded with whitespace on
  2568. the right (because of `-'). Append a colon if there is a category
  2569. (because of `:').
  2570. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2571. time, don't put in an empty field, just skip it (because of '?').
  2572. - Finally, put the scheduling information and append a whitespace.
  2573. As another example, if you don't want the time-of-day of entries in
  2574. the prefix, you could use:
  2575. (setq org-agenda-prefix-format \" %-11:c% s\")
  2576. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2577. `org-agenda-remove-tags'."
  2578. :type '(choice
  2579. (string :tag "General format")
  2580. (list :greedy t :tag "View dependent"
  2581. (cons (const agenda) (string :tag "Format"))
  2582. (cons (const timeline) (string :tag "Format"))
  2583. (cons (const todo) (string :tag "Format"))
  2584. (cons (const tags) (string :tag "Format"))
  2585. (cons (const search) (string :tag "Format"))))
  2586. :group 'org-agenda-line-format)
  2587. (defvar org-prefix-format-compiled nil
  2588. "The compiled version of the most recently used prefix format.
  2589. See the variable `org-agenda-prefix-format'.")
  2590. (defcustom org-agenda-todo-keyword-format "%-1s"
  2591. "Format for the TODO keyword in agenda lines.
  2592. Set this to something like \"%-12s\" if you want all TODO keywords
  2593. to occupy a fixed space in the agenda display."
  2594. :group 'org-agenda-line-format
  2595. :type 'string)
  2596. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2597. "Text preceeding scheduled items in the agenda view.
  2598. This is a list with two strings. The first applies when the item is
  2599. scheduled on the current day. The second applies when it has been scheduled
  2600. previously, it may contain a %d to capture how many days ago the item was
  2601. scheduled."
  2602. :group 'org-agenda-line-format
  2603. :type '(list
  2604. (string :tag "Scheduled today ")
  2605. (string :tag "Scheduled previously")))
  2606. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2607. "Text preceeding deadline items in the agenda view.
  2608. This is a list with two strings. The first applies when the item has its
  2609. deadline on the current day. The second applies when it is in the past or
  2610. in the future, it may contain %d to capture how many days away the deadline
  2611. is (was)."
  2612. :group 'org-agenda-line-format
  2613. :type '(list
  2614. (string :tag "Deadline today ")
  2615. (string :tag "Deadline relative")))
  2616. (defcustom org-agenda-remove-times-when-in-prefix t
  2617. "Non-nil means, remove duplicate time specifications in agenda items.
  2618. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2619. time-of-day specification in a headline or diary entry is extracted and
  2620. placed into the prefix. If this option is non-nil, the original specification
  2621. \(a timestamp or -range, or just a plain time(range) specification like
  2622. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2623. cluttered.
  2624. The option can be t or nil. It may also be the symbol `beg', indicating
  2625. that the time should only be removed what it is located at the beginning of
  2626. the headline/diary entry."
  2627. :group 'org-agenda-line-format
  2628. :type '(choice
  2629. (const :tag "Always" t)
  2630. (const :tag "Never" nil)
  2631. (const :tag "When at beginning of entry" beg)))
  2632. (defcustom org-agenda-default-appointment-duration nil
  2633. "Default duration for appointments that only have a starting time.
  2634. When nil, no duration is specified in such cases.
  2635. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2636. :group 'org-agenda-line-format
  2637. :type '(choice
  2638. (integer :tag "Minutes")
  2639. (const :tag "No default duration")))
  2640. (defcustom org-agenda-remove-tags nil
  2641. "Non-nil means, remove the tags from the headline copy in the agenda.
  2642. When this is the symbol `prefix', only remove tags when
  2643. `org-agenda-prefix-format' contains a `%T' specifier."
  2644. :group 'org-agenda-line-format
  2645. :type '(choice
  2646. (const :tag "Always" t)
  2647. (const :tag "Never" nil)
  2648. (const :tag "When prefix format contains %T" prefix)))
  2649. (if (fboundp 'defvaralias)
  2650. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2651. 'org-agenda-remove-tags))
  2652. (defcustom org-agenda-tags-column -80
  2653. "Shift tags in agenda items to this column.
  2654. If this number is positive, it specifies the column. If it is negative,
  2655. it means that the tags should be flushright to that column. For example,
  2656. -80 works well for a normal 80 character screen."
  2657. :group 'org-agenda-line-format
  2658. :type 'integer)
  2659. (if (fboundp 'defvaralias)
  2660. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2661. (defcustom org-agenda-fontify-priorities t
  2662. "Non-nil means, highlight low and high priorities in agenda.
  2663. When t, the highest priority entries are bold, lowest priority italic.
  2664. This may also be an association list of priority faces. The face may be
  2665. a names face, or a list like `(:background \"Red\")'."
  2666. :group 'org-agenda-line-format
  2667. :type '(choice
  2668. (const :tag "Never" nil)
  2669. (const :tag "Defaults" t)
  2670. (repeat :tag "Specify"
  2671. (list (character :tag "Priority" :value ?A)
  2672. (sexp :tag "face")))))
  2673. (defgroup org-latex nil
  2674. "Options for embedding LaTeX code into Org-mode"
  2675. :tag "Org LaTeX"
  2676. :group 'org)
  2677. (defcustom org-format-latex-options
  2678. '(:foreground default :background default :scale 1.0
  2679. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2680. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2681. "Options for creating images from LaTeX fragments.
  2682. This is a property list with the following properties:
  2683. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2684. `default' means use the forground of the default face.
  2685. :background the background color, or \"Transparent\".
  2686. `default' means use the background of the default face.
  2687. :scale a scaling factor for the size of the images
  2688. :html-foreground, :html-background, :html-scale
  2689. The same numbers for HTML export.
  2690. :matchers a list indicating which matchers should be used to
  2691. find LaTeX fragments. Valid members of this list are:
  2692. \"begin\" find environments
  2693. \"$\" find math expressions surrounded by $...$
  2694. \"$$\" find math expressions surrounded by $$....$$
  2695. \"\\(\" find math expressions surrounded by \\(...\\)
  2696. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2697. :group 'org-latex
  2698. :type 'plist)
  2699. (defcustom org-format-latex-header "\\documentclass{article}
  2700. \\usepackage{fullpage} % do not remove
  2701. \\usepackage{amssymb}
  2702. \\usepackage[usenames]{color}
  2703. \\usepackage{amsmath}
  2704. \\usepackage{latexsym}
  2705. \\usepackage[mathscr]{eucal}
  2706. \\pagestyle{empty} % do not remove"
  2707. "The document header used for processing LaTeX fragments."
  2708. :group 'org-latex
  2709. :type 'string)
  2710. (defgroup org-export nil
  2711. "Options for exporting org-listings."
  2712. :tag "Org Export"
  2713. :group 'org)
  2714. (defgroup org-export-general nil
  2715. "General options for exporting Org-mode files."
  2716. :tag "Org Export General"
  2717. :group 'org-export)
  2718. ;; FIXME
  2719. (defvar org-export-publishing-directory nil)
  2720. (defcustom org-export-with-special-strings t
  2721. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2722. When this option is turned on, these strings will be exported as:
  2723. Org HTML LaTeX
  2724. -----+----------+--------
  2725. \\- &shy; \\-
  2726. -- &ndash; --
  2727. --- &mdash; ---
  2728. ... &hellip; \ldots
  2729. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2730. :group 'org-export-translation
  2731. :type 'boolean)
  2732. (defcustom org-export-language-setup
  2733. '(("en" "Author" "Date" "Table of Contents")
  2734. ("cs" "Autor" "Datum" "Obsah")
  2735. ("da" "Ophavsmand" "Dato" "Indhold")
  2736. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2737. ("es" "Autor" "Fecha" "\xcdndice")
  2738. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2739. ("it" "Autore" "Data" "Indice")
  2740. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2741. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2742. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2743. "Terms used in export text, translated to different languages.
  2744. Use the variable `org-export-default-language' to set the language,
  2745. or use the +OPTION lines for a per-file setting."
  2746. :group 'org-export-general
  2747. :type '(repeat
  2748. (list
  2749. (string :tag "HTML language tag")
  2750. (string :tag "Author")
  2751. (string :tag "Date")
  2752. (string :tag "Table of Contents"))))
  2753. (defcustom org-export-default-language "en"
  2754. "The default language of HTML export, as a string.
  2755. This should have an association in `org-export-language-setup'."
  2756. :group 'org-export-general
  2757. :type 'string)
  2758. (defcustom org-export-skip-text-before-1st-heading t
  2759. "Non-nil means, skip all text before the first headline when exporting.
  2760. When nil, that text is exported as well."
  2761. :group 'org-export-general
  2762. :type 'boolean)
  2763. (defcustom org-export-headline-levels 3
  2764. "The last level which is still exported as a headline.
  2765. Inferior levels will produce itemize lists when exported.
  2766. Note that a numeric prefix argument to an exporter function overrides
  2767. this setting.
  2768. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2769. :group 'org-export-general
  2770. :type 'number)
  2771. (defcustom org-export-with-section-numbers t
  2772. "Non-nil means, add section numbers to headlines when exporting.
  2773. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2774. :group 'org-export-general
  2775. :type 'boolean)
  2776. (defcustom org-export-with-toc t
  2777. "Non-nil means, create a table of contents in exported files.
  2778. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2779. When an integer, include levels up to N in the toc, this may then be
  2780. different from `org-export-headline-levels', but it will not be allowed
  2781. to be larger than the number of headline levels.
  2782. When nil, no table of contents is made.
  2783. Headlines which contain any TODO items will be marked with \"(*)\" in
  2784. ASCII export, and with red color in HTML output, if the option
  2785. `org-export-mark-todo-in-toc' is set.
  2786. In HTML output, the TOC will be clickable.
  2787. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2788. or \"toc:3\"."
  2789. :group 'org-export-general
  2790. :type '(choice
  2791. (const :tag "No Table of Contents" nil)
  2792. (const :tag "Full Table of Contents" t)
  2793. (integer :tag "TOC to level")))
  2794. (defcustom org-export-mark-todo-in-toc nil
  2795. "Non-nil means, mark TOC lines that contain any open TODO items."
  2796. :group 'org-export-general
  2797. :type 'boolean)
  2798. (defcustom org-export-preserve-breaks nil
  2799. "Non-nil means, preserve all line breaks when exporting.
  2800. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2801. export, line breaks will always be preserved, regardless of this variable.
  2802. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2803. :group 'org-export-general
  2804. :type 'boolean)
  2805. (defcustom org-export-with-archived-trees 'headline
  2806. "Whether subtrees with the ARCHIVE tag should be exported.
  2807. This can have three different values
  2808. nil Do not export, pretend this tree is not present
  2809. t Do export the entire tree
  2810. headline Only export the headline, but skip the tree below it."
  2811. :group 'org-export-general
  2812. :group 'org-archive
  2813. :type '(choice
  2814. (const :tag "not at all" nil)
  2815. (const :tag "headline only" 'headline)
  2816. (const :tag "entirely" t)))
  2817. (defcustom org-export-author-info t
  2818. "Non-nil means, insert author name and email into the exported file.
  2819. This option can also be set with the +OPTIONS line,
  2820. e.g. \"author-info:nil\"."
  2821. :group 'org-export-general
  2822. :type 'boolean)
  2823. (defcustom org-export-time-stamp-file t
  2824. "Non-nil means, insert a time stamp into the exported file.
  2825. The time stamp shows when the file was created.
  2826. This option can also be set with the +OPTIONS line,
  2827. e.g. \"timestamp:nil\"."
  2828. :group 'org-export-general
  2829. :type 'boolean)
  2830. (defcustom org-export-with-timestamps t
  2831. "If nil, do not export time stamps and associated keywords."
  2832. :group 'org-export-general
  2833. :type 'boolean)
  2834. (defcustom org-export-remove-timestamps-from-toc t
  2835. "If nil, remove timestamps from the table of contents entries."
  2836. :group 'org-export-general
  2837. :type 'boolean)
  2838. (defcustom org-export-with-tags 'not-in-toc
  2839. "If nil, do not export tags, just remove them from headlines.
  2840. If this is the symbol `not-in-toc', tags will be removed from table of
  2841. contents entries, but still be shown in the headlines of the document.
  2842. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2843. :group 'org-export-general
  2844. :type '(choice
  2845. (const :tag "Off" nil)
  2846. (const :tag "Not in TOC" not-in-toc)
  2847. (const :tag "On" t)))
  2848. (defcustom org-export-with-drawers nil
  2849. "Non-nil means, export with drawers like the property drawer.
  2850. When t, all drawers are exported. This may also be a list of
  2851. drawer names to export."
  2852. :group 'org-export-general
  2853. :type '(choice
  2854. (const :tag "All drawers" t)
  2855. (const :tag "None" nil)
  2856. (repeat :tag "Selected drawers"
  2857. (string :tag "Drawer name"))))
  2858. (defgroup org-export-translation nil
  2859. "Options for translating special ascii sequences for the export backends."
  2860. :tag "Org Export Translation"
  2861. :group 'org-export)
  2862. (defcustom org-export-with-emphasize t
  2863. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2864. If the export target supports emphasizing text, the word will be
  2865. typeset in bold, italic, or underlined, respectively. Works only for
  2866. single words, but you can say: I *really* *mean* *this*.
  2867. Not all export backends support this.
  2868. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2869. :group 'org-export-translation
  2870. :type 'boolean)
  2871. (defcustom org-export-with-footnotes t
  2872. "If nil, export [1] as a footnote marker.
  2873. Lines starting with [1] will be formatted as footnotes.
  2874. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2875. :group 'org-export-translation
  2876. :type 'boolean)
  2877. (defcustom org-export-with-sub-superscripts t
  2878. "Non-nil means, interpret \"_\" and \"^\" for export.
  2879. When this option is turned on, you can use TeX-like syntax for sub- and
  2880. superscripts. Several characters after \"_\" or \"^\" will be
  2881. considered as a single item - so grouping with {} is normally not
  2882. needed. For example, the following things will be parsed as single
  2883. sub- or superscripts.
  2884. 10^24 or 10^tau several digits will be considered 1 item.
  2885. 10^-12 or 10^-tau a leading sign with digits or a word
  2886. x^2-y^3 will be read as x^2 - y^3, because items are
  2887. terminated by almost any nonword/nondigit char.
  2888. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2889. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2890. sub/superscript. If you set this variable to the symbol `{}',
  2891. the braces are *required* in order to trigger interpretations as
  2892. sub/superscript. This can be helpful in documents that need \"_\"
  2893. frequently in plain text.
  2894. Not all export backends support this, but HTML does.
  2895. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2896. :group 'org-export-translation
  2897. :type '(choice
  2898. (const :tag "Always interpret" t)
  2899. (const :tag "Only with braces" {})
  2900. (const :tag "Never interpret" nil)))
  2901. (defcustom org-export-with-special-strings t
  2902. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2903. When this option is turned on, these strings will be exported as:
  2904. \\- : &shy;
  2905. -- : &ndash;
  2906. --- : &mdash;
  2907. Not all export backends support this, but HTML does.
  2908. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2909. :group 'org-export-translation
  2910. :type 'boolean)
  2911. (defcustom org-export-with-TeX-macros t
  2912. "Non-nil means, interpret simple TeX-like macros when exporting.
  2913. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2914. No only real TeX macros will work here, but the standard HTML entities
  2915. for math can be used as macro names as well. For a list of supported
  2916. names in HTML export, see the constant `org-html-entities'.
  2917. Not all export backends support this.
  2918. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2919. :group 'org-export-translation
  2920. :group 'org-export-latex
  2921. :type 'boolean)
  2922. (defcustom org-export-with-LaTeX-fragments nil
  2923. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2924. When set, the exporter will find LaTeX environments if the \\begin line is
  2925. the first non-white thing on a line. It will also find the math delimiters
  2926. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2927. display math.
  2928. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2929. :group 'org-export-translation
  2930. :group 'org-export-latex
  2931. :type 'boolean)
  2932. (defcustom org-export-with-fixed-width t
  2933. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2934. This can be used to have pre-formatted text, fragments of code etc. For
  2935. example:
  2936. : ;; Some Lisp examples
  2937. : (while (defc cnt)
  2938. : (ding))
  2939. will be looking just like this in also HTML. See also the QUOTE keyword.
  2940. Not all export backends support this.
  2941. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2942. :group 'org-export-translation
  2943. :type 'boolean)
  2944. (defcustom org-match-sexp-depth 3
  2945. "Number of stacked braces for sub/superscript matching.
  2946. This has to be set before loading org.el to be effective."
  2947. :group 'org-export-translation
  2948. :type 'integer)
  2949. (defgroup org-export-tables nil
  2950. "Options for exporting tables in Org-mode."
  2951. :tag "Org Export Tables"
  2952. :group 'org-export)
  2953. (defcustom org-export-with-tables t
  2954. "If non-nil, lines starting with \"|\" define a table.
  2955. For example:
  2956. | Name | Address | Birthday |
  2957. |-------------+----------+-----------|
  2958. | Arthur Dent | England | 29.2.2100 |
  2959. Not all export backends support this.
  2960. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2961. :group 'org-export-tables
  2962. :type 'boolean)
  2963. (defcustom org-export-highlight-first-table-line t
  2964. "Non-nil means, highlight the first table line.
  2965. In HTML export, this means use <th> instead of <td>.
  2966. In tables created with table.el, this applies to the first table line.
  2967. In Org-mode tables, all lines before the first horizontal separator
  2968. line will be formatted with <th> tags."
  2969. :group 'org-export-tables
  2970. :type 'boolean)
  2971. (defcustom org-export-table-remove-special-lines t
  2972. "Remove special lines and marking characters in calculating tables.
  2973. This removes the special marking character column from tables that are set
  2974. up for spreadsheet calculations. It also removes the entire lines
  2975. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2976. the values of constants may be useful to have."
  2977. :group 'org-export-tables
  2978. :type 'boolean)
  2979. (defcustom org-export-prefer-native-exporter-for-tables nil
  2980. "Non-nil means, always export tables created with table.el natively.
  2981. Natively means, use the HTML code generator in table.el.
  2982. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2983. the table does not use row- or column-spanning). This has the
  2984. advantage, that the automatic HTML conversions for math symbols and
  2985. sub/superscripts can be applied. Org-mode's HTML generator is also
  2986. much faster."
  2987. :group 'org-export-tables
  2988. :type 'boolean)
  2989. (defgroup org-export-ascii nil
  2990. "Options specific for ASCII export of Org-mode files."
  2991. :tag "Org Export ASCII"
  2992. :group 'org-export)
  2993. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2994. "Characters for underlining headings in ASCII export.
  2995. In the given sequence, these characters will be used for level 1, 2, ..."
  2996. :group 'org-export-ascii
  2997. :type '(repeat character))
  2998. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2999. "Bullet characters for headlines converted to lists in ASCII export.
  3000. The first character is used for the first lest level generated in this
  3001. way, and so on. If there are more levels than characters given here,
  3002. the list will be repeated.
  3003. Note that plain lists will keep the same bullets as the have in the
  3004. Org-mode file."
  3005. :group 'org-export-ascii
  3006. :type '(repeat character))
  3007. (defgroup org-export-xml nil
  3008. "Options specific for XML export of Org-mode files."
  3009. :tag "Org Export XML"
  3010. :group 'org-export)
  3011. (defgroup org-export-html nil
  3012. "Options specific for HTML export of Org-mode files."
  3013. :tag "Org Export HTML"
  3014. :group 'org-export)
  3015. (defcustom org-export-html-coding-system nil
  3016. ""
  3017. :group 'org-export-html
  3018. :type 'coding-system)
  3019. (defcustom org-export-html-extension "html"
  3020. "The extension for exported HTML files."
  3021. :group 'org-export-html
  3022. :type 'string)
  3023. (defcustom org-export-html-style
  3024. "<style type=\"text/css\">
  3025. html {
  3026. font-family: Times, serif;
  3027. font-size: 12pt;
  3028. }
  3029. .title { text-align: center; }
  3030. .todo { color: red; }
  3031. .done { color: green; }
  3032. .timestamp { color: grey }
  3033. .timestamp-kwd { color: CadetBlue }
  3034. .tag { background-color:lightblue; font-weight:normal }
  3035. .target { background-color: lavender; }
  3036. pre {
  3037. border: 1pt solid #AEBDCC;
  3038. background-color: #F3F5F7;
  3039. padding: 5pt;
  3040. font-family: courier, monospace;
  3041. }
  3042. table { border-collapse: collapse; }
  3043. td, th {
  3044. vertical-align: top;
  3045. <!--border: 1pt solid #ADB9CC;-->
  3046. }
  3047. </style>"
  3048. "The default style specification for exported HTML files.
  3049. Since there are different ways of setting style information, this variable
  3050. needs to contain the full HTML structure to provide a style, including the
  3051. surrounding HTML tags. The style specifications should include definitions
  3052. for new classes todo, done, title, and deadline. For example, legal values
  3053. would be:
  3054. <style type=\"text/css\">
  3055. p { font-weight: normal; color: gray; }
  3056. h1 { color: black; }
  3057. .title { text-align: center; }
  3058. .todo, .deadline { color: red; }
  3059. .done { color: green; }
  3060. </style>
  3061. or, if you want to keep the style in a file,
  3062. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  3063. As the value of this option simply gets inserted into the HTML <head> header,
  3064. you can \"misuse\" it to add arbitrary text to the header."
  3065. :group 'org-export-html
  3066. :type 'string)
  3067. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  3068. "Format for typesetting the document title in HTML export."
  3069. :group 'org-export-html
  3070. :type 'string)
  3071. (defcustom org-export-html-toplevel-hlevel 2
  3072. "The <H> level for level 1 headings in HTML export."
  3073. :group 'org-export-html
  3074. :type 'string)
  3075. (defcustom org-export-html-link-org-files-as-html t
  3076. "Non-nil means, make file links to `file.org' point to `file.html'.
  3077. When org-mode is exporting an org-mode file to HTML, links to
  3078. non-html files are directly put into a href tag in HTML.
  3079. However, links to other Org-mode files (recognized by the
  3080. extension `.org.) should become links to the corresponding html
  3081. file, assuming that the linked org-mode file will also be
  3082. converted to HTML.
  3083. When nil, the links still point to the plain `.org' file."
  3084. :group 'org-export-html
  3085. :type 'boolean)
  3086. (defcustom org-export-html-inline-images 'maybe
  3087. "Non-nil means, inline images into exported HTML pages.
  3088. This is done using an <img> tag. When nil, an anchor with href is used to
  3089. link to the image. If this option is `maybe', then images in links with
  3090. an empty description will be inlined, while images with a description will
  3091. be linked only."
  3092. :group 'org-export-html
  3093. :type '(choice (const :tag "Never" nil)
  3094. (const :tag "Always" t)
  3095. (const :tag "When there is no description" maybe)))
  3096. ;; FIXME: rename
  3097. (defcustom org-export-html-expand t
  3098. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  3099. When nil, these tags will be exported as plain text and therefore
  3100. not be interpreted by a browser.
  3101. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3102. :group 'org-export-html
  3103. :type 'boolean)
  3104. (defcustom org-export-html-table-tag
  3105. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3106. "The HTML tag that is used to start a table.
  3107. This must be a <table> tag, but you may change the options like
  3108. borders and spacing."
  3109. :group 'org-export-html
  3110. :type 'string)
  3111. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3112. "The opening tag for table header fields.
  3113. This is customizable so that alignment options can be specified."
  3114. :group 'org-export-tables
  3115. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3116. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3117. "The opening tag for table data fields.
  3118. This is customizable so that alignment options can be specified."
  3119. :group 'org-export-tables
  3120. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3121. (defcustom org-export-html-with-timestamp nil
  3122. "If non-nil, write `org-export-html-html-helper-timestamp'
  3123. into the exported HTML text. Otherwise, the buffer will just be saved
  3124. to a file."
  3125. :group 'org-export-html
  3126. :type 'boolean)
  3127. (defcustom org-export-html-html-helper-timestamp
  3128. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3129. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3130. :group 'org-export-html
  3131. :type 'string)
  3132. (defgroup org-export-icalendar nil
  3133. "Options specific for iCalendar export of Org-mode files."
  3134. :tag "Org Export iCalendar"
  3135. :group 'org-export)
  3136. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3137. "The file name for the iCalendar file covering all agenda files.
  3138. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3139. The file name should be absolute, the file will be overwritten without warning."
  3140. :group 'org-export-icalendar
  3141. :type 'file)
  3142. (defcustom org-icalendar-include-todo nil
  3143. "Non-nil means, export to iCalendar files should also cover TODO items."
  3144. :group 'org-export-icalendar
  3145. :type '(choice
  3146. (const :tag "None" nil)
  3147. (const :tag "Unfinished" t)
  3148. (const :tag "All" all)))
  3149. (defcustom org-icalendar-include-sexps t
  3150. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3151. These are entries like in the diary, but directly in an Org-mode file."
  3152. :group 'org-export-icalendar
  3153. :type 'boolean)
  3154. (defcustom org-icalendar-include-body 100
  3155. "Amount of text below headline to be included in iCalendar export.
  3156. This is a number of characters that should maximally be included.
  3157. Properties, scheduling and clocking lines will always be removed.
  3158. The text will be inserted into the DESCRIPTION field."
  3159. :group 'org-export-icalendar
  3160. :type '(choice
  3161. (const :tag "Nothing" nil)
  3162. (const :tag "Everything" t)
  3163. (integer :tag "Max characters")))
  3164. (defcustom org-icalendar-combined-name "OrgMode"
  3165. "Calendar name for the combined iCalendar representing all agenda files."
  3166. :group 'org-export-icalendar
  3167. :type 'string)
  3168. (defgroup org-font-lock nil
  3169. "Font-lock settings for highlighting in Org-mode."
  3170. :tag "Org Font Lock"
  3171. :group 'org)
  3172. (defcustom org-level-color-stars-only nil
  3173. "Non-nil means fontify only the stars in each headline.
  3174. When nil, the entire headline is fontified.
  3175. Changing it requires restart of `font-lock-mode' to become effective
  3176. also in regions already fontified."
  3177. :group 'org-font-lock
  3178. :type 'boolean)
  3179. (defcustom org-hide-leading-stars nil
  3180. "Non-nil means, hide the first N-1 stars in a headline.
  3181. This works by using the face `org-hide' for these stars. This
  3182. face is white for a light background, and black for a dark
  3183. background. You may have to customize the face `org-hide' to
  3184. make this work.
  3185. Changing it requires restart of `font-lock-mode' to become effective
  3186. also in regions already fontified.
  3187. You may also set this on a per-file basis by adding one of the following
  3188. lines to the buffer:
  3189. #+STARTUP: hidestars
  3190. #+STARTUP: showstars"
  3191. :group 'org-font-lock
  3192. :type 'boolean)
  3193. (defcustom org-fontify-done-headline nil
  3194. "Non-nil means, change the face of a headline if it is marked DONE.
  3195. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3196. When this is non-nil, the headline after the keyword is set to the
  3197. `org-headline-done' as an additional indication."
  3198. :group 'org-font-lock
  3199. :type 'boolean)
  3200. (defcustom org-fontify-emphasized-text t
  3201. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3202. Changing this variable requires a restart of Emacs to take effect."
  3203. :group 'org-font-lock
  3204. :type 'boolean)
  3205. (defcustom org-highlight-latex-fragments-and-specials nil
  3206. "Non-nil means, fontify what is treated specially by the exporters."
  3207. :group 'org-font-lock
  3208. :type 'boolean)
  3209. (defcustom org-hide-emphasis-markers nil
  3210. "Non-nil mean font-lock should hide the emphasis marker characters."
  3211. :group 'org-font-lock
  3212. :type 'boolean)
  3213. (defvar org-emph-re nil
  3214. "Regular expression for matching emphasis.")
  3215. (defvar org-verbatim-re nil
  3216. "Regular expression for matching verbatim text.")
  3217. (defvar org-emphasis-regexp-components) ; defined just below
  3218. (defvar org-emphasis-alist) ; defined just below
  3219. (defun org-set-emph-re (var val)
  3220. "Set variable and compute the emphasis regular expression."
  3221. (set var val)
  3222. (when (and (boundp 'org-emphasis-alist)
  3223. (boundp 'org-emphasis-regexp-components)
  3224. org-emphasis-alist org-emphasis-regexp-components)
  3225. (let* ((e org-emphasis-regexp-components)
  3226. (pre (car e))
  3227. (post (nth 1 e))
  3228. (border (nth 2 e))
  3229. (body (nth 3 e))
  3230. (nl (nth 4 e))
  3231. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3232. (body1 (concat body "*?"))
  3233. (markers (mapconcat 'car org-emphasis-alist ""))
  3234. (vmarkers (mapconcat
  3235. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3236. org-emphasis-alist "")))
  3237. ;; make sure special characters appear at the right position in the class
  3238. (if (string-match "\\^" markers)
  3239. (setq markers (concat (replace-match "" t t markers) "^")))
  3240. (if (string-match "-" markers)
  3241. (setq markers (concat (replace-match "" t t markers) "-")))
  3242. (if (string-match "\\^" vmarkers)
  3243. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3244. (if (string-match "-" vmarkers)
  3245. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3246. (if (> nl 0)
  3247. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3248. (int-to-string nl) "\\}")))
  3249. ;; Make the regexp
  3250. (setq org-emph-re
  3251. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3252. "\\("
  3253. "\\([" markers "]\\)"
  3254. "\\("
  3255. "[^" border "]\\|"
  3256. "[^" border (if (and nil stacked) markers) "]"
  3257. body1
  3258. "[^" border (if (and nil stacked) markers) "]"
  3259. "\\)"
  3260. "\\3\\)"
  3261. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3262. (setq org-verbatim-re
  3263. (concat "\\([" pre "]\\|^\\)"
  3264. "\\("
  3265. "\\([" vmarkers "]\\)"
  3266. "\\("
  3267. "[^" border "]\\|"
  3268. "[^" border "]"
  3269. body1
  3270. "[^" border "]"
  3271. "\\)"
  3272. "\\3\\)"
  3273. "\\([" post "]\\|$\\)")))))
  3274. (defcustom org-emphasis-regexp-components
  3275. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3276. "Components used to build the regular expression for emphasis.
  3277. This is a list with 6 entries. Terminology: In an emphasis string
  3278. like \" *strong word* \", we call the initial space PREMATCH, the final
  3279. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3280. and \"trong wor\" is the body. The different components in this variable
  3281. specify what is allowed/forbidden in each part:
  3282. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3283. post Chars allowed as postmatch. End of line will be allowed too.
  3284. border The chars *forbidden* as border characters.
  3285. body-regexp A regexp like \".\" to match a body character. Don't use
  3286. non-shy groups here, and don't allow newline here.
  3287. newline The maximum number of newlines allowed in an emphasis exp.
  3288. Use customize to modify this, or restart Emacs after changing it."
  3289. :group 'org-font-lock
  3290. :set 'org-set-emph-re
  3291. :type '(list
  3292. (sexp :tag "Allowed chars in pre ")
  3293. (sexp :tag "Allowed chars in post ")
  3294. (sexp :tag "Forbidden chars in border ")
  3295. (sexp :tag "Regexp for body ")
  3296. (integer :tag "number of newlines allowed")
  3297. (option (boolean :tag "Stacking (DISABLED) "))))
  3298. (defcustom org-emphasis-alist
  3299. '(("*" bold "<b>" "</b>")
  3300. ("/" italic "<i>" "</i>")
  3301. ("_" underline "<u>" "</u>")
  3302. ("=" org-code "<code>" "</code>" verbatim)
  3303. ("~" org-verbatim "" "" verbatim)
  3304. ("+" (:strike-through t) "<del>" "</del>")
  3305. )
  3306. "Special syntax for emphasized text.
  3307. Text starting and ending with a special character will be emphasized, for
  3308. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3309. characters, the face to be used by font-lock for highlighting in Org-mode
  3310. Emacs buffers, and the HTML tags to be used for this.
  3311. Use customize to modify this, or restart Emacs after changing it."
  3312. :group 'org-font-lock
  3313. :set 'org-set-emph-re
  3314. :type '(repeat
  3315. (list
  3316. (string :tag "Marker character")
  3317. (choice
  3318. (face :tag "Font-lock-face")
  3319. (plist :tag "Face property list"))
  3320. (string :tag "HTML start tag")
  3321. (string :tag "HTML end tag")
  3322. (option (const verbatim)))))
  3323. ;;; The faces
  3324. (defgroup org-faces nil
  3325. "Faces in Org-mode."
  3326. :tag "Org Faces"
  3327. :group 'org-font-lock)
  3328. (defun org-compatible-face (inherits specs)
  3329. "Make a compatible face specification.
  3330. If INHERITS is an existing face and if the Emacs version supports it,
  3331. just inherit the face. If not, use SPECS to define the face.
  3332. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3333. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3334. to the top of the list. The `min-colors' attribute will be removed from
  3335. any other entries, and any resulting duplicates will be removed entirely."
  3336. (cond
  3337. ((and inherits (facep inherits)
  3338. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3339. ;; In Emacs 23, we use inheritance where possible.
  3340. ;; We only do this in Emacs 23, because only there the outline
  3341. ;; faces have been changed to the original org-mode-level-faces.
  3342. (list (list t :inherit inherits)))
  3343. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3344. ;; These do not understand the `min-colors' attribute.
  3345. (let (r e a)
  3346. (while (setq e (pop specs))
  3347. (cond
  3348. ((memq (car e) '(t default)) (push e r))
  3349. ((setq a (member '(min-colors 8) (car e)))
  3350. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3351. (cdr e)))))
  3352. ((setq a (assq 'min-colors (car e)))
  3353. (setq e (cons (delq a (car e)) (cdr e)))
  3354. (or (assoc (car e) r) (push e r)))
  3355. (t (or (assoc (car e) r) (push e r)))))
  3356. (nreverse r)))
  3357. (t specs)))
  3358. (put 'org-compatible-face 'lisp-indent-function 1)
  3359. (defface org-hide
  3360. '((((background light)) (:foreground "white"))
  3361. (((background dark)) (:foreground "black")))
  3362. "Face used to hide leading stars in headlines.
  3363. The forground color of this face should be equal to the background
  3364. color of the frame."
  3365. :group 'org-faces)
  3366. (defface org-level-1 ;; font-lock-function-name-face
  3367. (org-compatible-face 'outline-1
  3368. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3369. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3370. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3371. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3372. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3373. (t (:bold t))))
  3374. "Face used for level 1 headlines."
  3375. :group 'org-faces)
  3376. (defface org-level-2 ;; font-lock-variable-name-face
  3377. (org-compatible-face 'outline-2
  3378. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3379. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3380. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3381. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3382. (t (:bold t))))
  3383. "Face used for level 2 headlines."
  3384. :group 'org-faces)
  3385. (defface org-level-3 ;; font-lock-keyword-face
  3386. (org-compatible-face 'outline-3
  3387. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3388. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3389. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3390. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3391. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3392. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3393. (t (:bold t))))
  3394. "Face used for level 3 headlines."
  3395. :group 'org-faces)
  3396. (defface org-level-4 ;; font-lock-comment-face
  3397. (org-compatible-face 'outline-4
  3398. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3399. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3400. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3401. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3402. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3403. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3404. (t (:bold t))))
  3405. "Face used for level 4 headlines."
  3406. :group 'org-faces)
  3407. (defface org-level-5 ;; font-lock-type-face
  3408. (org-compatible-face 'outline-5
  3409. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3410. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3411. (((class color) (min-colors 8)) (:foreground "green"))))
  3412. "Face used for level 5 headlines."
  3413. :group 'org-faces)
  3414. (defface org-level-6 ;; font-lock-constant-face
  3415. (org-compatible-face 'outline-6
  3416. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3417. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3418. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3419. "Face used for level 6 headlines."
  3420. :group 'org-faces)
  3421. (defface org-level-7 ;; font-lock-builtin-face
  3422. (org-compatible-face 'outline-7
  3423. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3424. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3425. (((class color) (min-colors 8)) (:foreground "blue"))))
  3426. "Face used for level 7 headlines."
  3427. :group 'org-faces)
  3428. (defface org-level-8 ;; font-lock-string-face
  3429. (org-compatible-face 'outline-8
  3430. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3431. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3432. (((class color) (min-colors 8)) (:foreground "green"))))
  3433. "Face used for level 8 headlines."
  3434. :group 'org-faces)
  3435. (defface org-special-keyword ;; font-lock-string-face
  3436. (org-compatible-face nil
  3437. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3438. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3439. (t (:italic t))))
  3440. "Face used for special keywords."
  3441. :group 'org-faces)
  3442. (defface org-drawer ;; font-lock-function-name-face
  3443. (org-compatible-face nil
  3444. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3445. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3446. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3447. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3448. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3449. (t (:bold t))))
  3450. "Face used for drawers."
  3451. :group 'org-faces)
  3452. (defface org-property-value nil
  3453. "Face used for the value of a property."
  3454. :group 'org-faces)
  3455. (defface org-column
  3456. (org-compatible-face nil
  3457. '((((class color) (min-colors 16) (background light))
  3458. (:background "grey90"))
  3459. (((class color) (min-colors 16) (background dark))
  3460. (:background "grey30"))
  3461. (((class color) (min-colors 8))
  3462. (:background "cyan" :foreground "black"))
  3463. (t (:inverse-video t))))
  3464. "Face for column display of entry properties."
  3465. :group 'org-faces)
  3466. (when (fboundp 'set-face-attribute)
  3467. ;; Make sure that a fixed-width face is used when we have a column table.
  3468. (set-face-attribute 'org-column nil
  3469. :height (face-attribute 'default :height)
  3470. :family (face-attribute 'default :family)))
  3471. (defface org-warning
  3472. (org-compatible-face 'font-lock-warning-face
  3473. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3474. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3475. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3476. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3477. (t (:bold t))))
  3478. "Face for deadlines and TODO keywords."
  3479. :group 'org-faces)
  3480. (defface org-archived ; similar to shadow
  3481. (org-compatible-face 'shadow
  3482. '((((class color grayscale) (min-colors 88) (background light))
  3483. (:foreground "grey50"))
  3484. (((class color grayscale) (min-colors 88) (background dark))
  3485. (:foreground "grey70"))
  3486. (((class color) (min-colors 8) (background light))
  3487. (:foreground "green"))
  3488. (((class color) (min-colors 8) (background dark))
  3489. (:foreground "yellow"))))
  3490. "Face for headline with the ARCHIVE tag."
  3491. :group 'org-faces)
  3492. (defface org-link
  3493. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3494. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3495. (t (:underline t)))
  3496. "Face for links."
  3497. :group 'org-faces)
  3498. (defface org-ellipsis
  3499. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3500. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3501. (t (:strike-through t)))
  3502. "Face for the ellipsis in folded text."
  3503. :group 'org-faces)
  3504. (defface org-target
  3505. '((((class color) (background light)) (:underline t))
  3506. (((class color) (background dark)) (:underline t))
  3507. (t (:underline t)))
  3508. "Face for links."
  3509. :group 'org-faces)
  3510. (defface org-date
  3511. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3512. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3513. (t (:underline t)))
  3514. "Face for links."
  3515. :group 'org-faces)
  3516. (defface org-sexp-date
  3517. '((((class color) (background light)) (:foreground "Purple"))
  3518. (((class color) (background dark)) (:foreground "Cyan"))
  3519. (t (:underline t)))
  3520. "Face for links."
  3521. :group 'org-faces)
  3522. (defface org-tag
  3523. '((t (:bold t)))
  3524. "Face for tags."
  3525. :group 'org-faces)
  3526. (defface org-todo ; font-lock-warning-face
  3527. (org-compatible-face nil
  3528. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3529. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3530. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3531. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3532. (t (:inverse-video t :bold t))))
  3533. "Face for TODO keywords."
  3534. :group 'org-faces)
  3535. (defface org-done ;; font-lock-type-face
  3536. (org-compatible-face nil
  3537. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3538. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3539. (((class color) (min-colors 8)) (:foreground "green"))
  3540. (t (:bold t))))
  3541. "Face used for todo keywords that indicate DONE items."
  3542. :group 'org-faces)
  3543. (defface org-headline-done ;; font-lock-string-face
  3544. (org-compatible-face nil
  3545. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3546. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3547. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3548. "Face used to indicate that a headline is DONE.
  3549. This face is only used if `org-fontify-done-headline' is set. If applies
  3550. to the part of the headline after the DONE keyword."
  3551. :group 'org-faces)
  3552. (defcustom org-todo-keyword-faces nil
  3553. "Faces for specific TODO keywords.
  3554. This is a list of cons cells, with TODO keywords in the car
  3555. and faces in the cdr. The face can be a symbol, or a property
  3556. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3557. :group 'org-faces
  3558. :group 'org-todo
  3559. :type '(repeat
  3560. (cons
  3561. (string :tag "keyword")
  3562. (sexp :tag "face"))))
  3563. (defface org-table ;; font-lock-function-name-face
  3564. (org-compatible-face nil
  3565. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3566. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3567. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3568. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3569. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3570. (((class color) (min-colors 8) (background dark)))))
  3571. "Face used for tables."
  3572. :group 'org-faces)
  3573. (defface org-formula
  3574. (org-compatible-face nil
  3575. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3576. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3577. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3578. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3579. (t (:bold t :italic t))))
  3580. "Face for formulas."
  3581. :group 'org-faces)
  3582. (defface org-code
  3583. (org-compatible-face nil
  3584. '((((class color grayscale) (min-colors 88) (background light))
  3585. (:foreground "grey50"))
  3586. (((class color grayscale) (min-colors 88) (background dark))
  3587. (:foreground "grey70"))
  3588. (((class color) (min-colors 8) (background light))
  3589. (:foreground "green"))
  3590. (((class color) (min-colors 8) (background dark))
  3591. (:foreground "yellow"))))
  3592. "Face for fixed-with text like code snippets."
  3593. :group 'org-faces
  3594. :version "22.1")
  3595. (defface org-verbatim
  3596. (org-compatible-face nil
  3597. '((((class color grayscale) (min-colors 88) (background light))
  3598. (:foreground "grey50" :underline t))
  3599. (((class color grayscale) (min-colors 88) (background dark))
  3600. (:foreground "grey70" :underline t))
  3601. (((class color) (min-colors 8) (background light))
  3602. (:foreground "green" :underline t))
  3603. (((class color) (min-colors 8) (background dark))
  3604. (:foreground "yellow" :underline t))))
  3605. "Face for fixed-with text like code snippets."
  3606. :group 'org-faces
  3607. :version "22.1")
  3608. (defface org-agenda-structure ;; font-lock-function-name-face
  3609. (org-compatible-face nil
  3610. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3611. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3612. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3613. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3614. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3615. (t (:bold t))))
  3616. "Face used in agenda for captions and dates."
  3617. :group 'org-faces)
  3618. (defface org-scheduled-today
  3619. (org-compatible-face nil
  3620. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3621. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3622. (((class color) (min-colors 8)) (:foreground "green"))
  3623. (t (:bold t :italic t))))
  3624. "Face for items scheduled for a certain day."
  3625. :group 'org-faces)
  3626. (defface org-scheduled-previously
  3627. (org-compatible-face nil
  3628. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3629. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3630. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3631. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3632. (t (:bold t))))
  3633. "Face for items scheduled previously, and not yet done."
  3634. :group 'org-faces)
  3635. (defface org-upcoming-deadline
  3636. (org-compatible-face nil
  3637. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3638. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3639. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3640. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3641. (t (:bold t))))
  3642. "Face for items scheduled previously, and not yet done."
  3643. :group 'org-faces)
  3644. (defcustom org-agenda-deadline-faces
  3645. '((1.0 . org-warning)
  3646. (0.5 . org-upcoming-deadline)
  3647. (0.0 . default))
  3648. "Faces for showing deadlines in the agenda.
  3649. This is a list of cons cells. The cdr of each cell is a face to be used,
  3650. and it can also just be like '(:foreground \"yellow\").
  3651. Each car is a fraction of the head-warning time that must have passed for
  3652. this the face in the cdr to be used for display. The numbers must be
  3653. given in descending order. The head-warning time is normally taken
  3654. from `org-deadline-warning-days', but can also be specified in the deadline
  3655. timestamp itself, like this:
  3656. DEADLINE: <2007-08-13 Mon -8d>
  3657. You may use d for days, w for weeks, m for months and y for years. Months
  3658. and years will only be treated in an approximate fashion (30.4 days for a
  3659. month and 365.24 days for a year)."
  3660. :group 'org-faces
  3661. :group 'org-agenda-daily/weekly
  3662. :type '(repeat
  3663. (cons
  3664. (number :tag "Fraction of head-warning time passed")
  3665. (sexp :tag "Face"))))
  3666. ;; FIXME: this is not a good face yet.
  3667. (defface org-agenda-restriction-lock
  3668. (org-compatible-face nil
  3669. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3670. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3671. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3672. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3673. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3674. (t (:inverse-video t))))
  3675. "Face for showing the agenda restriction lock."
  3676. :group 'org-faces)
  3677. (defface org-time-grid ;; font-lock-variable-name-face
  3678. (org-compatible-face nil
  3679. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3680. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3681. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3682. "Face used for time grids."
  3683. :group 'org-faces)
  3684. (defconst org-level-faces
  3685. '(org-level-1 org-level-2 org-level-3 org-level-4
  3686. org-level-5 org-level-6 org-level-7 org-level-8
  3687. ))
  3688. (defcustom org-n-level-faces (length org-level-faces)
  3689. "The number of different faces to be used for headlines.
  3690. Org-mode defines 8 different headline faces, so this can be at most 8.
  3691. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3692. :type 'number
  3693. :group 'org-faces)
  3694. ;;; Functions and variables from ther packages
  3695. ;; Declared here to avoid compiler warnings
  3696. (eval-and-compile
  3697. (unless (fboundp 'declare-function)
  3698. (defmacro declare-function (fn file &optional arglist fileonly))))
  3699. ;; XEmacs only
  3700. (defvar outline-mode-menu-heading)
  3701. (defvar outline-mode-menu-show)
  3702. (defvar outline-mode-menu-hide)
  3703. (defvar zmacs-regions) ; XEmacs regions
  3704. ;; Emacs only
  3705. (defvar mark-active)
  3706. ;; Various packages
  3707. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3708. (declare-function add-to-diary-list "diary-lib"
  3709. (date string specifier &optional marker globcolor literal))
  3710. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3711. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3712. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3713. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3714. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3715. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3716. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3717. (declare-function bbdb-record-name "ext:bbdb" (record))
  3718. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3719. (declare-function bibtex-generate-autokey "bibtex" ())
  3720. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3721. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3722. (defvar calc-embedded-close-formula)
  3723. (defvar calc-embedded-open-formula)
  3724. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3725. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3726. (declare-function calendar-check-holidays "holidays" (date))
  3727. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3728. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3729. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3730. (declare-function calendar-forward-day "cal-move" (arg))
  3731. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3732. (declare-function calendar-goto-date "cal-move" (date))
  3733. (declare-function calendar-goto-today "cal-move" ())
  3734. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3735. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3736. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3737. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3738. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3739. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3740. (defvar calendar-mode-map)
  3741. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3742. (declare-function cdlatex-tab "ext:cdlatex" ())
  3743. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3744. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3745. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3746. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3747. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3748. (defvar font-lock-unfontify-region-function)
  3749. (declare-function gnus-article-show-summary "gnus-art" ())
  3750. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3751. (defvar gnus-other-frame-object)
  3752. (defvar gnus-group-name)
  3753. (defvar gnus-article-current)
  3754. (defvar Info-current-file)
  3755. (defvar Info-current-node)
  3756. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3757. (declare-function mh-find-path "mh-utils" ())
  3758. (declare-function mh-get-header-field "mh-utils" (field))
  3759. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3760. (declare-function mh-header-display "mh-show" ())
  3761. (declare-function mh-index-previous-folder "mh-search" ())
  3762. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3763. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3764. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3765. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3766. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3767. (declare-function mh-show-header-display "mh-show" t t)
  3768. (declare-function mh-show-msg "mh-show" (msg))
  3769. (declare-function mh-show-show "mh-show" t t)
  3770. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3771. (defvar mh-progs)
  3772. (defvar mh-current-folder)
  3773. (defvar mh-show-folder-buffer)
  3774. (defvar mh-index-folder)
  3775. (defvar mh-searcher)
  3776. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3777. (declare-function parse-time-string "parse-time" (string))
  3778. (declare-function remember "remember" (&optional initial))
  3779. (declare-function remember-buffer-desc "remember" ())
  3780. (declare-function remember-finalize "remember" ())
  3781. (defvar remember-save-after-remembering)
  3782. (defvar remember-data-file)
  3783. (defvar remember-register)
  3784. (defvar remember-buffer)
  3785. (defvar remember-handler-functions)
  3786. (defvar remember-annotation-functions)
  3787. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3788. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3789. (declare-function rmail-what-message "rmail" ())
  3790. (defvar rmail-current-message)
  3791. (defvar texmathp-why)
  3792. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3793. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3794. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3795. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3796. (declare-function vm-isearch-update "ext:vm-search" ())
  3797. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3798. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3799. (declare-function vm-su-subject "ext:vm-summary" (m))
  3800. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3801. (defvar vm-message-pointer)
  3802. (defvar vm-folder-directory)
  3803. (defvar w3m-current-url)
  3804. (defvar w3m-current-title)
  3805. ;; backward compatibility to old version of wl
  3806. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3807. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3808. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3809. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3810. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3811. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3812. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3813. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3814. (defvar wl-summary-buffer-elmo-folder)
  3815. (defvar wl-summary-buffer-folder-name)
  3816. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3817. (defvar org-latex-regexps)
  3818. (defvar constants-unit-system)
  3819. ;;; Variables for pre-computed regular expressions, all buffer local
  3820. (defvar org-drawer-regexp nil
  3821. "Matches first line of a hidden block.")
  3822. (make-variable-buffer-local 'org-drawer-regexp)
  3823. (defvar org-todo-regexp nil
  3824. "Matches any of the TODO state keywords.")
  3825. (make-variable-buffer-local 'org-todo-regexp)
  3826. (defvar org-not-done-regexp nil
  3827. "Matches any of the TODO state keywords except the last one.")
  3828. (make-variable-buffer-local 'org-not-done-regexp)
  3829. (defvar org-todo-line-regexp nil
  3830. "Matches a headline and puts TODO state into group 2 if present.")
  3831. (make-variable-buffer-local 'org-todo-line-regexp)
  3832. (defvar org-complex-heading-regexp nil
  3833. "Matches a headline and puts everything into groups:
  3834. group 1: the stars
  3835. group 2: The todo keyword, maybe
  3836. group 3: Priority cookie
  3837. group 4: True headline
  3838. group 5: Tags")
  3839. (make-variable-buffer-local 'org-complex-heading-regexp)
  3840. (defvar org-todo-line-tags-regexp nil
  3841. "Matches a headline and puts TODO state into group 2 if present.
  3842. Also put tags into group 4 if tags are present.")
  3843. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3844. (defvar org-nl-done-regexp nil
  3845. "Matches newline followed by a headline with the DONE keyword.")
  3846. (make-variable-buffer-local 'org-nl-done-regexp)
  3847. (defvar org-looking-at-done-regexp nil
  3848. "Matches the DONE keyword a point.")
  3849. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3850. (defvar org-ds-keyword-length 12
  3851. "Maximum length of the Deadline and SCHEDULED keywords.")
  3852. (make-variable-buffer-local 'org-ds-keyword-length)
  3853. (defvar org-deadline-regexp nil
  3854. "Matches the DEADLINE keyword.")
  3855. (make-variable-buffer-local 'org-deadline-regexp)
  3856. (defvar org-deadline-time-regexp nil
  3857. "Matches the DEADLINE keyword together with a time stamp.")
  3858. (make-variable-buffer-local 'org-deadline-time-regexp)
  3859. (defvar org-deadline-line-regexp nil
  3860. "Matches the DEADLINE keyword and the rest of the line.")
  3861. (make-variable-buffer-local 'org-deadline-line-regexp)
  3862. (defvar org-scheduled-regexp nil
  3863. "Matches the SCHEDULED keyword.")
  3864. (make-variable-buffer-local 'org-scheduled-regexp)
  3865. (defvar org-scheduled-time-regexp nil
  3866. "Matches the SCHEDULED keyword together with a time stamp.")
  3867. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3868. (defvar org-closed-time-regexp nil
  3869. "Matches the CLOSED keyword together with a time stamp.")
  3870. (make-variable-buffer-local 'org-closed-time-regexp)
  3871. (defvar org-keyword-time-regexp nil
  3872. "Matches any of the 4 keywords, together with the time stamp.")
  3873. (make-variable-buffer-local 'org-keyword-time-regexp)
  3874. (defvar org-keyword-time-not-clock-regexp nil
  3875. "Matches any of the 3 keywords, together with the time stamp.")
  3876. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3877. (defvar org-maybe-keyword-time-regexp nil
  3878. "Matches a timestamp, possibly preceeded by a keyword.")
  3879. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3880. (defvar org-planning-or-clock-line-re nil
  3881. "Matches a line with planning or clock info.")
  3882. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3883. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3884. rear-nonsticky t mouse-map t fontified t)
  3885. "Properties to remove when a string without properties is wanted.")
  3886. (defsubst org-match-string-no-properties (num &optional string)
  3887. (if (featurep 'xemacs)
  3888. (let ((s (match-string num string)))
  3889. (remove-text-properties 0 (length s) org-rm-props s)
  3890. s)
  3891. (match-string-no-properties num string)))
  3892. (defsubst org-no-properties (s)
  3893. (if (fboundp 'set-text-properties)
  3894. (set-text-properties 0 (length s) nil s)
  3895. (remove-text-properties 0 (length s) org-rm-props s))
  3896. s)
  3897. (defsubst org-get-alist-option (option key)
  3898. (cond ((eq key t) t)
  3899. ((eq option t) t)
  3900. ((assoc key option) (cdr (assoc key option)))
  3901. (t (cdr (assq 'default option)))))
  3902. (defsubst org-inhibit-invisibility ()
  3903. "Modified `buffer-invisibility-spec' for Emacs 21.
  3904. Some ops with invisible text do not work correctly on Emacs 21. For these
  3905. we turn off invisibility temporarily. Use this in a `let' form."
  3906. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3907. (defsubst org-set-local (var value)
  3908. "Make VAR local in current buffer and set it to VALUE."
  3909. (set (make-variable-buffer-local var) value))
  3910. (defsubst org-mode-p ()
  3911. "Check if the current buffer is in Org-mode."
  3912. (eq major-mode 'org-mode))
  3913. (defsubst org-last (list)
  3914. "Return the last element of LIST."
  3915. (car (last list)))
  3916. (defun org-let (list &rest body)
  3917. (eval (cons 'let (cons list body))))
  3918. (put 'org-let 'lisp-indent-function 1)
  3919. (defun org-let2 (list1 list2 &rest body)
  3920. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3921. (put 'org-let2 'lisp-indent-function 2)
  3922. (defconst org-startup-options
  3923. '(("fold" org-startup-folded t)
  3924. ("overview" org-startup-folded t)
  3925. ("nofold" org-startup-folded nil)
  3926. ("showall" org-startup-folded nil)
  3927. ("content" org-startup-folded content)
  3928. ("hidestars" org-hide-leading-stars t)
  3929. ("showstars" org-hide-leading-stars nil)
  3930. ("odd" org-odd-levels-only t)
  3931. ("oddeven" org-odd-levels-only nil)
  3932. ("align" org-startup-align-all-tables t)
  3933. ("noalign" org-startup-align-all-tables nil)
  3934. ("customtime" org-display-custom-times t)
  3935. ("logdone" org-log-done time)
  3936. ("lognotedone" org-log-done note)
  3937. ("nologdone" org-log-done nil)
  3938. ("lognoteclock-out" org-log-note-clock-out t)
  3939. ("nolognoteclock-out" org-log-note-clock-out nil)
  3940. ("logrepeat" org-log-repeat state)
  3941. ("lognoterepeat" org-log-repeat note)
  3942. ("nologrepeat" org-log-repeat nil)
  3943. ("constcgs" constants-unit-system cgs)
  3944. ("constSI" constants-unit-system SI))
  3945. "Variable associated with STARTUP options for org-mode.
  3946. Each element is a list of three items: The startup options as written
  3947. in the #+STARTUP line, the corresponding variable, and the value to
  3948. set this variable to if the option is found. An optional forth element PUSH
  3949. means to push this value onto the list in the variable.")
  3950. (defun org-set-regexps-and-options ()
  3951. "Precompute regular expressions for current buffer."
  3952. (when (org-mode-p)
  3953. (org-set-local 'org-todo-kwd-alist nil)
  3954. (org-set-local 'org-todo-key-alist nil)
  3955. (org-set-local 'org-todo-key-trigger nil)
  3956. (org-set-local 'org-todo-keywords-1 nil)
  3957. (org-set-local 'org-done-keywords nil)
  3958. (org-set-local 'org-todo-heads nil)
  3959. (org-set-local 'org-todo-sets nil)
  3960. (org-set-local 'org-todo-log-states nil)
  3961. (let ((re (org-make-options-regexp
  3962. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3963. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3964. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3965. (splitre "[ \t]+")
  3966. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3967. tail sep kws1 prio props drawers)
  3968. (save-excursion
  3969. (save-restriction
  3970. (widen)
  3971. (goto-char (point-min))
  3972. (while (re-search-forward re nil t)
  3973. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3974. (cond
  3975. ((equal key "CATEGORY")
  3976. (if (string-match "[ \t]+$" value)
  3977. (setq value (replace-match "" t t value)))
  3978. (setq cat value))
  3979. ((member key '("SEQ_TODO" "TODO"))
  3980. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3981. ((equal key "TYP_TODO")
  3982. (push (cons 'type (org-split-string value splitre)) kwds))
  3983. ((equal key "TAGS")
  3984. (setq tags (append tags (org-split-string value splitre))))
  3985. ((equal key "COLUMNS")
  3986. (org-set-local 'org-columns-default-format value))
  3987. ((equal key "LINK")
  3988. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3989. (push (cons (match-string 1 value)
  3990. (org-trim (match-string 2 value)))
  3991. links)))
  3992. ((equal key "PRIORITIES")
  3993. (setq prio (org-split-string value " +")))
  3994. ((equal key "PROPERTY")
  3995. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3996. (push (cons (match-string 1 value) (match-string 2 value))
  3997. props)))
  3998. ((equal key "DRAWERS")
  3999. (setq drawers (org-split-string value splitre)))
  4000. ((equal key "CONSTANTS")
  4001. (setq const (append const (org-split-string value splitre))))
  4002. ((equal key "STARTUP")
  4003. (let ((opts (org-split-string value splitre))
  4004. l var val)
  4005. (while (setq l (pop opts))
  4006. (when (setq l (assoc l org-startup-options))
  4007. (setq var (nth 1 l) val (nth 2 l))
  4008. (if (not (nth 3 l))
  4009. (set (make-local-variable var) val)
  4010. (if (not (listp (symbol-value var)))
  4011. (set (make-local-variable var) nil))
  4012. (set (make-local-variable var) (symbol-value var))
  4013. (add-to-list var val))))))
  4014. ((equal key "ARCHIVE")
  4015. (string-match " *$" value)
  4016. (setq arch (replace-match "" t t value))
  4017. (remove-text-properties 0 (length arch)
  4018. '(face t fontified t) arch)))
  4019. )))
  4020. (when cat
  4021. (org-set-local 'org-category (intern cat))
  4022. (push (cons "CATEGORY" cat) props))
  4023. (when prio
  4024. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4025. (setq prio (mapcar 'string-to-char prio))
  4026. (org-set-local 'org-highest-priority (nth 0 prio))
  4027. (org-set-local 'org-lowest-priority (nth 1 prio))
  4028. (org-set-local 'org-default-priority (nth 2 prio)))
  4029. (and props (org-set-local 'org-local-properties (nreverse props)))
  4030. (and drawers (org-set-local 'org-drawers drawers))
  4031. (and arch (org-set-local 'org-archive-location arch))
  4032. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4033. ;; Process the TODO keywords
  4034. (unless kwds
  4035. ;; Use the global values as if they had been given locally.
  4036. (setq kwds (default-value 'org-todo-keywords))
  4037. (if (stringp (car kwds))
  4038. (setq kwds (list (cons org-todo-interpretation
  4039. (default-value 'org-todo-keywords)))))
  4040. (setq kwds (reverse kwds)))
  4041. (setq kwds (nreverse kwds))
  4042. (let (inter kws kw)
  4043. (while (setq kws (pop kwds))
  4044. (setq inter (pop kws) sep (member "|" kws)
  4045. kws0 (delete "|" (copy-sequence kws))
  4046. kwsa nil
  4047. kws1 (mapcar
  4048. (lambda (x)
  4049. ;; 1 2
  4050. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4051. (progn
  4052. (setq kw (match-string 1 x)
  4053. key (and (match-end 2) (match-string 2 x))
  4054. log (org-extract-log-state-settings x))
  4055. (push (cons kw (and key (string-to-char key))) kwsa)
  4056. (and log (push log org-todo-log-states))
  4057. kw)
  4058. (error "Invalid TODO keyword %s" x)))
  4059. kws0)
  4060. kwsa (if kwsa (append '((:startgroup))
  4061. (nreverse kwsa)
  4062. '((:endgroup))))
  4063. hw (car kws1)
  4064. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4065. tail (list inter hw (car dws) (org-last dws)))
  4066. (add-to-list 'org-todo-heads hw 'append)
  4067. (push kws1 org-todo-sets)
  4068. (setq org-done-keywords (append org-done-keywords dws nil))
  4069. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4070. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4071. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4072. (setq org-todo-sets (nreverse org-todo-sets)
  4073. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4074. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4075. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4076. ;; Process the constants
  4077. (when const
  4078. (let (e cst)
  4079. (while (setq e (pop const))
  4080. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4081. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4082. (setq org-table-formula-constants-local cst)))
  4083. ;; Process the tags.
  4084. (when tags
  4085. (let (e tgs)
  4086. (while (setq e (pop tags))
  4087. (cond
  4088. ((equal e "{") (push '(:startgroup) tgs))
  4089. ((equal e "}") (push '(:endgroup) tgs))
  4090. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  4091. (push (cons (match-string 1 e)
  4092. (string-to-char (match-string 2 e)))
  4093. tgs))
  4094. (t (push (list e) tgs))))
  4095. (org-set-local 'org-tag-alist nil)
  4096. (while (setq e (pop tgs))
  4097. (or (and (stringp (car e))
  4098. (assoc (car e) org-tag-alist))
  4099. (push e org-tag-alist))))))
  4100. ;; Compute the regular expressions and other local variables
  4101. (if (not org-done-keywords)
  4102. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4103. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4104. (length org-scheduled-string)))
  4105. org-drawer-regexp
  4106. (concat "^[ \t]*:\\("
  4107. (mapconcat 'regexp-quote org-drawers "\\|")
  4108. "\\):[ \t]*$")
  4109. org-not-done-keywords
  4110. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4111. org-todo-regexp
  4112. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4113. "\\|") "\\)\\>")
  4114. org-not-done-regexp
  4115. (concat "\\<\\("
  4116. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4117. "\\)\\>")
  4118. org-todo-line-regexp
  4119. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4120. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4121. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4122. org-complex-heading-regexp
  4123. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4124. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4125. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4126. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4127. org-nl-done-regexp
  4128. (concat "\n\\*+[ \t]+"
  4129. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4130. "\\)" "\\>")
  4131. org-todo-line-tags-regexp
  4132. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4133. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4134. (org-re
  4135. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4136. org-looking-at-done-regexp
  4137. (concat "^" "\\(?:"
  4138. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4139. "\\>")
  4140. org-deadline-regexp (concat "\\<" org-deadline-string)
  4141. org-deadline-time-regexp
  4142. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4143. org-deadline-line-regexp
  4144. (concat "\\<\\(" org-deadline-string "\\).*")
  4145. org-scheduled-regexp
  4146. (concat "\\<" org-scheduled-string)
  4147. org-scheduled-time-regexp
  4148. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4149. org-closed-time-regexp
  4150. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4151. org-keyword-time-regexp
  4152. (concat "\\<\\(" org-scheduled-string
  4153. "\\|" org-deadline-string
  4154. "\\|" org-closed-string
  4155. "\\|" org-clock-string "\\)"
  4156. " *[[<]\\([^]>]+\\)[]>]")
  4157. org-keyword-time-not-clock-regexp
  4158. (concat "\\<\\(" org-scheduled-string
  4159. "\\|" org-deadline-string
  4160. "\\|" org-closed-string
  4161. "\\)"
  4162. " *[[<]\\([^]>]+\\)[]>]")
  4163. org-maybe-keyword-time-regexp
  4164. (concat "\\(\\<\\(" org-scheduled-string
  4165. "\\|" org-deadline-string
  4166. "\\|" org-closed-string
  4167. "\\|" org-clock-string "\\)\\)?"
  4168. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4169. org-planning-or-clock-line-re
  4170. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4171. "\\|" org-deadline-string
  4172. "\\|" org-closed-string "\\|" org-clock-string
  4173. "\\)\\>\\)")
  4174. )
  4175. (org-compute-latex-and-specials-regexp)
  4176. (org-set-font-lock-defaults)))
  4177. (defun org-extract-log-state-settings (x)
  4178. "Extract the log state setting from a TODO keyword string.
  4179. This will extract info from a string like \"WAIT(w@/!)\"."
  4180. (let (kw key log1 log2)
  4181. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4182. (setq kw (match-string 1 x)
  4183. key (and (match-end 2) (match-string 2 x))
  4184. log1 (and (match-end 3) (match-string 3 x))
  4185. log2 (and (match-end 4) (match-string 4 x)))
  4186. (and (or log1 log2)
  4187. (list kw
  4188. (and log1 (if (equal log1 "!") 'time 'note))
  4189. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4190. (defun org-remove-keyword-keys (list)
  4191. "Remove a pair of parenthesis at the end of each string in LIST."
  4192. (mapcar (lambda (x)
  4193. (if (string-match "(.*)$" x)
  4194. (substring x 0 (match-beginning 0))
  4195. x))
  4196. list))
  4197. ;; FIXME: this could be done much better, using second characters etc.
  4198. (defun org-assign-fast-keys (alist)
  4199. "Assign fast keys to a keyword-key alist.
  4200. Respect keys that are already there."
  4201. (let (new e k c c1 c2 (char ?a))
  4202. (while (setq e (pop alist))
  4203. (cond
  4204. ((equal e '(:startgroup)) (push e new))
  4205. ((equal e '(:endgroup)) (push e new))
  4206. (t
  4207. (setq k (car e) c2 nil)
  4208. (if (cdr e)
  4209. (setq c (cdr e))
  4210. ;; automatically assign a character.
  4211. (setq c1 (string-to-char
  4212. (downcase (substring
  4213. k (if (= (string-to-char k) ?@) 1 0)))))
  4214. (if (or (rassoc c1 new) (rassoc c1 alist))
  4215. (while (or (rassoc char new) (rassoc char alist))
  4216. (setq char (1+ char)))
  4217. (setq c2 c1))
  4218. (setq c (or c2 char)))
  4219. (push (cons k c) new))))
  4220. (nreverse new)))
  4221. ;;; Some variables ujsed in various places
  4222. (defvar org-window-configuration nil
  4223. "Used in various places to store a window configuration.")
  4224. (defvar org-finish-function nil
  4225. "Function to be called when `C-c C-c' is used.
  4226. This is for getting out of special buffers like remember.")
  4227. ;; FIXME: Occasionally check by commenting these, to make sure
  4228. ;; no other functions uses these, forgetting to let-bind them.
  4229. (defvar entry)
  4230. (defvar state)
  4231. (defvar last-state)
  4232. (defvar date)
  4233. (defvar description)
  4234. ;; Defined somewhere in this file, but used before definition.
  4235. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4236. (defvar org-agenda-buffer-name)
  4237. (defvar org-agenda-undo-list)
  4238. (defvar org-agenda-pending-undo-list)
  4239. (defvar org-agenda-overriding-header)
  4240. (defvar orgtbl-mode)
  4241. (defvar org-html-entities)
  4242. (defvar org-struct-menu)
  4243. (defvar org-org-menu)
  4244. (defvar org-tbl-menu)
  4245. (defvar org-agenda-keymap)
  4246. ;;;; Emacs/XEmacs compatibility
  4247. ;; Overlay compatibility functions
  4248. (defun org-make-overlay (beg end &optional buffer)
  4249. (if (featurep 'xemacs)
  4250. (make-extent beg end buffer)
  4251. (make-overlay beg end buffer)))
  4252. (defun org-delete-overlay (ovl)
  4253. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4254. (defun org-detach-overlay (ovl)
  4255. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4256. (defun org-move-overlay (ovl beg end &optional buffer)
  4257. (if (featurep 'xemacs)
  4258. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4259. (move-overlay ovl beg end buffer)))
  4260. (defun org-overlay-put (ovl prop value)
  4261. (if (featurep 'xemacs)
  4262. (set-extent-property ovl prop value)
  4263. (overlay-put ovl prop value)))
  4264. (defun org-overlay-display (ovl text &optional face evap)
  4265. "Make overlay OVL display TEXT with face FACE."
  4266. (if (featurep 'xemacs)
  4267. (let ((gl (make-glyph text)))
  4268. (and face (set-glyph-face gl face))
  4269. (set-extent-property ovl 'invisible t)
  4270. (set-extent-property ovl 'end-glyph gl))
  4271. (overlay-put ovl 'display text)
  4272. (if face (overlay-put ovl 'face face))
  4273. (if evap (overlay-put ovl 'evaporate t))))
  4274. (defun org-overlay-before-string (ovl text &optional face evap)
  4275. "Make overlay OVL display TEXT with face FACE."
  4276. (if (featurep 'xemacs)
  4277. (let ((gl (make-glyph text)))
  4278. (and face (set-glyph-face gl face))
  4279. (set-extent-property ovl 'begin-glyph gl))
  4280. (if face (org-add-props text nil 'face face))
  4281. (overlay-put ovl 'before-string text)
  4282. (if evap (overlay-put ovl 'evaporate t))))
  4283. (defun org-overlay-get (ovl prop)
  4284. (if (featurep 'xemacs)
  4285. (extent-property ovl prop)
  4286. (overlay-get ovl prop)))
  4287. (defun org-overlays-at (pos)
  4288. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4289. (defun org-overlays-in (&optional start end)
  4290. (if (featurep 'xemacs)
  4291. (extent-list nil start end)
  4292. (overlays-in start end)))
  4293. (defun org-overlay-start (o)
  4294. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4295. (defun org-overlay-end (o)
  4296. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4297. (defun org-find-overlays (prop &optional pos delete)
  4298. "Find all overlays specifying PROP at POS or point.
  4299. If DELETE is non-nil, delete all those overlays."
  4300. (let ((overlays (org-overlays-at (or pos (point))))
  4301. ov found)
  4302. (while (setq ov (pop overlays))
  4303. (if (org-overlay-get ov prop)
  4304. (if delete (org-delete-overlay ov) (push ov found))))
  4305. found))
  4306. ;; Region compatibility
  4307. (defun org-add-hook (hook function &optional append local)
  4308. "Add-hook, compatible with both Emacsen."
  4309. (if (and local (featurep 'xemacs))
  4310. (add-local-hook hook function append)
  4311. (add-hook hook function append local)))
  4312. (defvar org-ignore-region nil
  4313. "To temporarily disable the active region.")
  4314. (defun org-region-active-p ()
  4315. "Is `transient-mark-mode' on and the region active?
  4316. Works on both Emacs and XEmacs."
  4317. (if org-ignore-region
  4318. nil
  4319. (if (featurep 'xemacs)
  4320. (and zmacs-regions (region-active-p))
  4321. (if (fboundp 'use-region-p)
  4322. (use-region-p)
  4323. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4324. ;; Invisibility compatibility
  4325. (defun org-add-to-invisibility-spec (arg)
  4326. "Add elements to `buffer-invisibility-spec'.
  4327. See documentation for `buffer-invisibility-spec' for the kind of elements
  4328. that can be added."
  4329. (cond
  4330. ((fboundp 'add-to-invisibility-spec)
  4331. (add-to-invisibility-spec arg))
  4332. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4333. (setq buffer-invisibility-spec (list arg)))
  4334. (t
  4335. (setq buffer-invisibility-spec
  4336. (cons arg buffer-invisibility-spec)))))
  4337. (defun org-remove-from-invisibility-spec (arg)
  4338. "Remove elements from `buffer-invisibility-spec'."
  4339. (if (fboundp 'remove-from-invisibility-spec)
  4340. (remove-from-invisibility-spec arg)
  4341. (if (consp buffer-invisibility-spec)
  4342. (setq buffer-invisibility-spec
  4343. (delete arg buffer-invisibility-spec)))))
  4344. (defun org-in-invisibility-spec-p (arg)
  4345. "Is ARG a member of `buffer-invisibility-spec'?"
  4346. (if (consp buffer-invisibility-spec)
  4347. (member arg buffer-invisibility-spec)
  4348. nil))
  4349. ;;;; Define the Org-mode
  4350. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4351. (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."))
  4352. ;; We use a before-change function to check if a table might need
  4353. ;; an update.
  4354. (defvar org-table-may-need-update t
  4355. "Indicates that a table might need an update.
  4356. This variable is set by `org-before-change-function'.
  4357. `org-table-align' sets it back to nil.")
  4358. (defvar org-mode-map)
  4359. (defvar org-mode-hook nil)
  4360. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4361. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4362. (defvar org-table-buffer-is-an nil)
  4363. (defconst org-outline-regexp "\\*+ ")
  4364. ;;;###autoload
  4365. (define-derived-mode org-mode outline-mode "Org"
  4366. "Outline-based notes management and organizer, alias
  4367. \"Carsten's outline-mode for keeping track of everything.\"
  4368. Org-mode develops organizational tasks around a NOTES file which
  4369. contains information about projects as plain text. Org-mode is
  4370. implemented on top of outline-mode, which is ideal to keep the content
  4371. of large files well structured. It supports ToDo items, deadlines and
  4372. time stamps, which magically appear in the diary listing of the Emacs
  4373. calendar. Tables are easily created with a built-in table editor.
  4374. Plain text URL-like links connect to websites, emails (VM), Usenet
  4375. messages (Gnus), BBDB entries, and any files related to the project.
  4376. For printing and sharing of notes, an Org-mode file (or a part of it)
  4377. can be exported as a structured ASCII or HTML file.
  4378. The following commands are available:
  4379. \\{org-mode-map}"
  4380. ;; Get rid of Outline menus, they are not needed
  4381. ;; Need to do this here because define-derived-mode sets up
  4382. ;; the keymap so late. Still, it is a waste to call this each time
  4383. ;; we switch another buffer into org-mode.
  4384. (if (featurep 'xemacs)
  4385. (when (boundp 'outline-mode-menu-heading)
  4386. ;; Assume this is Greg's port, it used easymenu
  4387. (easy-menu-remove outline-mode-menu-heading)
  4388. (easy-menu-remove outline-mode-menu-show)
  4389. (easy-menu-remove outline-mode-menu-hide))
  4390. (define-key org-mode-map [menu-bar headings] 'undefined)
  4391. (define-key org-mode-map [menu-bar hide] 'undefined)
  4392. (define-key org-mode-map [menu-bar show] 'undefined))
  4393. (easy-menu-add org-org-menu)
  4394. (easy-menu-add org-tbl-menu)
  4395. (org-install-agenda-files-menu)
  4396. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4397. (org-add-to-invisibility-spec '(org-cwidth))
  4398. (when (featurep 'xemacs)
  4399. (org-set-local 'line-move-ignore-invisible t))
  4400. (org-set-local 'outline-regexp org-outline-regexp)
  4401. (org-set-local 'outline-level 'org-outline-level)
  4402. (when (and org-ellipsis
  4403. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4404. (fboundp 'make-glyph-code))
  4405. (unless org-display-table
  4406. (setq org-display-table (make-display-table)))
  4407. (set-display-table-slot
  4408. org-display-table 4
  4409. (vconcat (mapcar
  4410. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4411. org-ellipsis)))
  4412. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4413. (setq buffer-display-table org-display-table))
  4414. (org-set-regexps-and-options)
  4415. ;; Calc embedded
  4416. (org-set-local 'calc-embedded-open-mode "# ")
  4417. (modify-syntax-entry ?# "<")
  4418. (modify-syntax-entry ?@ "w")
  4419. (if org-startup-truncated (setq truncate-lines t))
  4420. (org-set-local 'font-lock-unfontify-region-function
  4421. 'org-unfontify-region)
  4422. ;; Activate before-change-function
  4423. (org-set-local 'org-table-may-need-update t)
  4424. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4425. 'local)
  4426. ;; Check for running clock before killing a buffer
  4427. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4428. ;; Paragraphs and auto-filling
  4429. (org-set-autofill-regexps)
  4430. (setq indent-line-function 'org-indent-line-function)
  4431. (org-update-radio-target-regexp)
  4432. ;; Comment characters
  4433. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4434. (org-set-local 'comment-padding " ")
  4435. ;; Align options lines
  4436. (org-set-local
  4437. 'align-mode-rules-list
  4438. '((org-in-buffer-settings
  4439. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4440. (modes . '(org-mode)))))
  4441. ;; Imenu
  4442. (org-set-local 'imenu-create-index-function
  4443. 'org-imenu-get-tree)
  4444. ;; Make isearch reveal context
  4445. (if (or (featurep 'xemacs)
  4446. (not (boundp 'outline-isearch-open-invisible-function)))
  4447. ;; Emacs 21 and XEmacs make use of the hook
  4448. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4449. ;; Emacs 22 deals with this through a special variable
  4450. (org-set-local 'outline-isearch-open-invisible-function
  4451. (lambda (&rest ignore) (org-show-context 'isearch))))
  4452. ;; If empty file that did not turn on org-mode automatically, make it to.
  4453. (if (and org-insert-mode-line-in-empty-file
  4454. (interactive-p)
  4455. (= (point-min) (point-max)))
  4456. (insert "# -*- mode: org -*-\n\n"))
  4457. (unless org-inhibit-startup
  4458. (when org-startup-align-all-tables
  4459. (let ((bmp (buffer-modified-p)))
  4460. (org-table-map-tables 'org-table-align)
  4461. (set-buffer-modified-p bmp)))
  4462. (org-cycle-hide-drawers 'all)
  4463. (cond
  4464. ((eq org-startup-folded t)
  4465. (org-cycle '(4)))
  4466. ((eq org-startup-folded 'content)
  4467. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4468. (org-cycle '(4)) (org-cycle '(4)))))))
  4469. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4470. (defsubst org-call-with-arg (command arg)
  4471. "Call COMMAND interactively, but pretend prefix are was ARG."
  4472. (let ((current-prefix-arg arg)) (call-interactively command)))
  4473. (defsubst org-current-line (&optional pos)
  4474. (save-excursion
  4475. (and pos (goto-char pos))
  4476. ;; works also in narrowed buffer, because we start at 1, not point-min
  4477. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4478. (defun org-current-time ()
  4479. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4480. (if (> org-time-stamp-rounding-minutes 0)
  4481. (let ((r org-time-stamp-rounding-minutes)
  4482. (time (decode-time)))
  4483. (apply 'encode-time
  4484. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4485. (nthcdr 2 time))))
  4486. (current-time)))
  4487. (defun org-add-props (string plist &rest props)
  4488. "Add text properties to entire string, from beginning to end.
  4489. PLIST may be a list of properties, PROPS are individual properties and values
  4490. that will be added to PLIST. Returns the string that was modified."
  4491. (add-text-properties
  4492. 0 (length string) (if props (append plist props) plist) string)
  4493. string)
  4494. (put 'org-add-props 'lisp-indent-function 2)
  4495. ;;;; Font-Lock stuff, including the activators
  4496. (defvar org-mouse-map (make-sparse-keymap))
  4497. (org-defkey org-mouse-map
  4498. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4499. (org-defkey org-mouse-map
  4500. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4501. (when org-mouse-1-follows-link
  4502. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4503. (when org-tab-follows-link
  4504. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4505. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4506. (when org-return-follows-link
  4507. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4508. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4509. (require 'font-lock)
  4510. (defconst org-non-link-chars "]\t\n\r<>")
  4511. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4512. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
  4513. (defvar org-link-re-with-space nil
  4514. "Matches a link with spaces, optional angular brackets around it.")
  4515. (defvar org-link-re-with-space2 nil
  4516. "Matches a link with spaces, optional angular brackets around it.")
  4517. (defvar org-angle-link-re nil
  4518. "Matches link with angular brackets, spaces are allowed.")
  4519. (defvar org-plain-link-re nil
  4520. "Matches plain link, without spaces.")
  4521. (defvar org-bracket-link-regexp nil
  4522. "Matches a link in double brackets.")
  4523. (defvar org-bracket-link-analytic-regexp nil
  4524. "Regular expression used to analyze links.
  4525. Here is what the match groups contain after a match:
  4526. 1: http:
  4527. 2: http
  4528. 3: path
  4529. 4: [desc]
  4530. 5: desc")
  4531. (defvar org-any-link-re nil
  4532. "Regular expression matching any link.")
  4533. (defun org-make-link-regexps ()
  4534. "Update the link regular expressions.
  4535. This should be called after the variable `org-link-types' has changed."
  4536. (setq org-link-re-with-space
  4537. (concat
  4538. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4539. "\\([^" org-non-link-chars " ]"
  4540. "[^" org-non-link-chars "]*"
  4541. "[^" org-non-link-chars " ]\\)>?")
  4542. org-link-re-with-space2
  4543. (concat
  4544. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4545. "\\([^" org-non-link-chars " ]"
  4546. "[^]\t\n\r]*"
  4547. "[^" org-non-link-chars " ]\\)>?")
  4548. org-angle-link-re
  4549. (concat
  4550. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4551. "\\([^" org-non-link-chars " ]"
  4552. "[^" org-non-link-chars "]*"
  4553. "\\)>")
  4554. org-plain-link-re
  4555. (concat
  4556. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4557. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4558. org-bracket-link-regexp
  4559. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4560. org-bracket-link-analytic-regexp
  4561. (concat
  4562. "\\[\\["
  4563. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4564. "\\([^]]+\\)"
  4565. "\\]"
  4566. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4567. "\\]")
  4568. org-any-link-re
  4569. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4570. org-angle-link-re "\\)\\|\\("
  4571. org-plain-link-re "\\)")))
  4572. (org-make-link-regexps)
  4573. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4574. "Regular expression for fast time stamp matching.")
  4575. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4576. "Regular expression for fast time stamp matching.")
  4577. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4578. "Regular expression matching time strings for analysis.
  4579. This one does not require the space after the date.")
  4580. (defconst org-ts-regexp1 "\\(\\([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. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4583. "Regular expression matching time stamps, with groups.")
  4584. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4585. "Regular expression matching time stamps (also [..]), with groups.")
  4586. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4587. "Regular expression matching a time stamp range.")
  4588. (defconst org-tr-regexp-both
  4589. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4590. "Regular expression matching a time stamp range.")
  4591. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4592. org-ts-regexp "\\)?")
  4593. "Regular expression matching a time stamp or time stamp range.")
  4594. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4595. org-ts-regexp-both "\\)?")
  4596. "Regular expression matching a time stamp or time stamp range.
  4597. The time stamps may be either active or inactive.")
  4598. (defvar org-emph-face nil)
  4599. (defun org-do-emphasis-faces (limit)
  4600. "Run through the buffer and add overlays to links."
  4601. (let (rtn)
  4602. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4603. (if (not (= (char-after (match-beginning 3))
  4604. (char-after (match-beginning 4))))
  4605. (progn
  4606. (setq rtn t)
  4607. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4608. 'face
  4609. (nth 1 (assoc (match-string 3)
  4610. org-emphasis-alist)))
  4611. (add-text-properties (match-beginning 2) (match-end 2)
  4612. '(font-lock-multiline t))
  4613. (when org-hide-emphasis-markers
  4614. (add-text-properties (match-end 4) (match-beginning 5)
  4615. '(invisible org-link))
  4616. (add-text-properties (match-beginning 3) (match-end 3)
  4617. '(invisible org-link)))))
  4618. (backward-char 1))
  4619. rtn))
  4620. (defun org-emphasize (&optional char)
  4621. "Insert or change an emphasis, i.e. a font like bold or italic.
  4622. If there is an active region, change that region to a new emphasis.
  4623. If there is no region, just insert the marker characters and position
  4624. the cursor between them.
  4625. CHAR should be either the marker character, or the first character of the
  4626. HTML tag associated with that emphasis. If CHAR is a space, the means
  4627. to remove the emphasis of the selected region.
  4628. If char is not given (for example in an interactive call) it
  4629. will be prompted for."
  4630. (interactive)
  4631. (let ((eal org-emphasis-alist) e det
  4632. (erc org-emphasis-regexp-components)
  4633. (prompt "")
  4634. (string "") beg end move tag c s)
  4635. (if (org-region-active-p)
  4636. (setq beg (region-beginning) end (region-end)
  4637. string (buffer-substring beg end))
  4638. (setq move t))
  4639. (while (setq e (pop eal))
  4640. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4641. c (aref tag 0))
  4642. (push (cons c (string-to-char (car e))) det)
  4643. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4644. (substring tag 1)))))
  4645. (unless char
  4646. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4647. (setq char (read-char-exclusive)))
  4648. (setq char (or (cdr (assoc char det)) char))
  4649. (if (equal char ?\ )
  4650. (setq s "" move nil)
  4651. (unless (assoc (char-to-string char) org-emphasis-alist)
  4652. (error "No such emphasis marker: \"%c\"" char))
  4653. (setq s (char-to-string char)))
  4654. (while (and (> (length string) 1)
  4655. (equal (substring string 0 1) (substring string -1))
  4656. (assoc (substring string 0 1) org-emphasis-alist))
  4657. (setq string (substring string 1 -1)))
  4658. (setq string (concat s string s))
  4659. (if beg (delete-region beg end))
  4660. (unless (or (bolp)
  4661. (string-match (concat "[" (nth 0 erc) "\n]")
  4662. (char-to-string (char-before (point)))))
  4663. (insert " "))
  4664. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4665. (char-to-string (char-after (point))))
  4666. (insert " ") (backward-char 1))
  4667. (insert string)
  4668. (and move (backward-char 1))))
  4669. (defconst org-nonsticky-props
  4670. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4671. (defun org-activate-plain-links (limit)
  4672. "Run through the buffer and add overlays to links."
  4673. (catch 'exit
  4674. (let (f)
  4675. (while (re-search-forward org-plain-link-re limit t)
  4676. (setq f (get-text-property (match-beginning 0) 'face))
  4677. (if (or (eq f 'org-tag)
  4678. (and (listp f) (memq 'org-tag f)))
  4679. nil
  4680. (add-text-properties (match-beginning 0) (match-end 0)
  4681. (list 'mouse-face 'highlight
  4682. 'rear-nonsticky org-nonsticky-props
  4683. 'keymap org-mouse-map
  4684. ))
  4685. (throw 'exit t))))))
  4686. (defun org-activate-code (limit)
  4687. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4688. (unless (get-text-property (match-beginning 1) 'face)
  4689. (remove-text-properties (match-beginning 0) (match-end 0)
  4690. '(display t invisible t intangible t))
  4691. t)))
  4692. (defun org-activate-angle-links (limit)
  4693. "Run through the buffer and add overlays to links."
  4694. (if (re-search-forward org-angle-link-re limit t)
  4695. (progn
  4696. (add-text-properties (match-beginning 0) (match-end 0)
  4697. (list 'mouse-face 'highlight
  4698. 'rear-nonsticky org-nonsticky-props
  4699. 'keymap org-mouse-map
  4700. ))
  4701. t)))
  4702. (defmacro org-maybe-intangible (props)
  4703. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4704. In emacs 21, invisible text is not avoided by the command loop, so the
  4705. intangible property is needed to make sure point skips this text.
  4706. In Emacs 22, this is not necessary. The intangible text property has
  4707. led to problems with flyspell. These problems are fixed in flyspell.el,
  4708. but we still avoid setting the property in Emacs 22 and later.
  4709. We use a macro so that the test can happen at compilation time."
  4710. (if (< emacs-major-version 22)
  4711. `(append '(intangible t) ,props)
  4712. props))
  4713. (defun org-activate-bracket-links (limit)
  4714. "Run through the buffer and add overlays to bracketed links."
  4715. (if (re-search-forward org-bracket-link-regexp limit t)
  4716. (let* ((help (concat "LINK: "
  4717. (org-match-string-no-properties 1)))
  4718. ;; FIXME: above we should remove the escapes.
  4719. ;; but that requires another match, protecting match data,
  4720. ;; a lot of overhead for font-lock.
  4721. (ip (org-maybe-intangible
  4722. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4723. 'keymap org-mouse-map 'mouse-face 'highlight
  4724. 'font-lock-multiline t 'help-echo help)))
  4725. (vp (list 'rear-nonsticky org-nonsticky-props
  4726. 'keymap org-mouse-map 'mouse-face 'highlight
  4727. ' font-lock-multiline t 'help-echo help)))
  4728. ;; We need to remove the invisible property here. Table narrowing
  4729. ;; may have made some of this invisible.
  4730. (remove-text-properties (match-beginning 0) (match-end 0)
  4731. '(invisible nil))
  4732. (if (match-end 3)
  4733. (progn
  4734. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4735. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4736. (add-text-properties (match-end 3) (match-end 0) ip))
  4737. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4738. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4739. (add-text-properties (match-end 1) (match-end 0) ip))
  4740. t)))
  4741. (defun org-activate-dates (limit)
  4742. "Run through the buffer and add overlays to dates."
  4743. (if (re-search-forward org-tsr-regexp-both limit t)
  4744. (progn
  4745. (add-text-properties (match-beginning 0) (match-end 0)
  4746. (list 'mouse-face 'highlight
  4747. 'rear-nonsticky org-nonsticky-props
  4748. 'keymap org-mouse-map))
  4749. (when org-display-custom-times
  4750. (if (match-end 3)
  4751. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4752. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4753. t)))
  4754. (defvar org-target-link-regexp nil
  4755. "Regular expression matching radio targets in plain text.")
  4756. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4757. "Regular expression matching a link target.")
  4758. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4759. "Regular expression matching a radio target.")
  4760. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4761. "Regular expression matching any target.")
  4762. (defun org-activate-target-links (limit)
  4763. "Run through the buffer and add overlays to target matches."
  4764. (when org-target-link-regexp
  4765. (let ((case-fold-search t))
  4766. (if (re-search-forward org-target-link-regexp limit t)
  4767. (progn
  4768. (add-text-properties (match-beginning 0) (match-end 0)
  4769. (list 'mouse-face 'highlight
  4770. 'rear-nonsticky org-nonsticky-props
  4771. 'keymap org-mouse-map
  4772. 'help-echo "Radio target link"
  4773. 'org-linked-text t))
  4774. t)))))
  4775. (defun org-update-radio-target-regexp ()
  4776. "Find all radio targets in this file and update the regular expression."
  4777. (interactive)
  4778. (when (memq 'radio org-activate-links)
  4779. (setq org-target-link-regexp
  4780. (org-make-target-link-regexp (org-all-targets 'radio)))
  4781. (org-restart-font-lock)))
  4782. (defun org-hide-wide-columns (limit)
  4783. (let (s e)
  4784. (setq s (text-property-any (point) (or limit (point-max))
  4785. 'org-cwidth t))
  4786. (when s
  4787. (setq e (next-single-property-change s 'org-cwidth))
  4788. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4789. (goto-char e)
  4790. t)))
  4791. (defvar org-latex-and-specials-regexp nil
  4792. "Regular expression for highlighting export special stuff.")
  4793. (defvar org-match-substring-regexp)
  4794. (defvar org-match-substring-with-braces-regexp)
  4795. (defvar org-export-html-special-string-regexps)
  4796. (defun org-compute-latex-and-specials-regexp ()
  4797. "Compute regular expression for stuff treated specially by exporters."
  4798. (if (not org-highlight-latex-fragments-and-specials)
  4799. (org-set-local 'org-latex-and-specials-regexp nil)
  4800. (let*
  4801. ((matchers (plist-get org-format-latex-options :matchers))
  4802. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4803. org-latex-regexps)))
  4804. (options (org-combine-plists (org-default-export-plist)
  4805. (org-infile-export-plist)))
  4806. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4807. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4808. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4809. (org-export-html-expand (plist-get options :expand-quoted-html))
  4810. (org-export-with-special-strings (plist-get options :special-strings))
  4811. (re-sub
  4812. (cond
  4813. ((equal org-export-with-sub-superscripts '{})
  4814. (list org-match-substring-with-braces-regexp))
  4815. (org-export-with-sub-superscripts
  4816. (list org-match-substring-regexp))
  4817. (t nil)))
  4818. (re-latex
  4819. (if org-export-with-LaTeX-fragments
  4820. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4821. (re-macros
  4822. (if org-export-with-TeX-macros
  4823. (list (concat "\\\\"
  4824. (regexp-opt
  4825. (append (mapcar 'car org-html-entities)
  4826. (if (boundp 'org-latex-entities)
  4827. org-latex-entities nil))
  4828. 'words))) ; FIXME
  4829. ))
  4830. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4831. (re-special (if org-export-with-special-strings
  4832. (mapcar (lambda (x) (car x))
  4833. org-export-html-special-string-regexps)))
  4834. (re-rest
  4835. (delq nil
  4836. (list
  4837. (if org-export-html-expand "@<[^>\n]+>")
  4838. ))))
  4839. (org-set-local
  4840. 'org-latex-and-specials-regexp
  4841. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4842. re-rest) "\\|")))))
  4843. (defface org-latex-and-export-specials
  4844. (let ((font (cond ((assq :inherit custom-face-attributes)
  4845. '(:inherit underline))
  4846. (t '(:underline t)))))
  4847. `((((class grayscale) (background light))
  4848. (:foreground "DimGray" ,@font))
  4849. (((class grayscale) (background dark))
  4850. (:foreground "LightGray" ,@font))
  4851. (((class color) (background light))
  4852. (:foreground "SaddleBrown"))
  4853. (((class color) (background dark))
  4854. (:foreground "burlywood"))
  4855. (t (,@font))))
  4856. "Face used to highlight math latex and other special exporter stuff."
  4857. :group 'org-faces)
  4858. (defun org-do-latex-and-special-faces (limit)
  4859. "Run through the buffer and add overlays to links."
  4860. (when org-latex-and-specials-regexp
  4861. (let (rtn d)
  4862. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4863. limit t))
  4864. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4865. 'face))
  4866. '(org-code org-verbatim underline)))
  4867. (progn
  4868. (setq rtn t
  4869. d (cond ((member (char-after (1+ (match-beginning 0)))
  4870. '(?_ ?^)) 1)
  4871. (t 0)))
  4872. (font-lock-prepend-text-property
  4873. (+ d (match-beginning 0)) (match-end 0)
  4874. 'face 'org-latex-and-export-specials)
  4875. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4876. '(font-lock-multiline t)))))
  4877. rtn)))
  4878. (defun org-restart-font-lock ()
  4879. "Restart font-lock-mode, to force refontification."
  4880. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4881. (font-lock-mode -1)
  4882. (font-lock-mode 1)))
  4883. (defun org-all-targets (&optional radio)
  4884. "Return a list of all targets in this file.
  4885. With optional argument RADIO, only find radio targets."
  4886. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4887. rtn)
  4888. (save-excursion
  4889. (goto-char (point-min))
  4890. (while (re-search-forward re nil t)
  4891. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4892. rtn)))
  4893. (defun org-make-target-link-regexp (targets)
  4894. "Make regular expression matching all strings in TARGETS.
  4895. The regular expression finds the targets also if there is a line break
  4896. between words."
  4897. (and targets
  4898. (concat
  4899. "\\<\\("
  4900. (mapconcat
  4901. (lambda (x)
  4902. (while (string-match " +" x)
  4903. (setq x (replace-match "\\s-+" t t x)))
  4904. x)
  4905. targets
  4906. "\\|")
  4907. "\\)\\>")))
  4908. (defun org-activate-tags (limit)
  4909. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4910. (progn
  4911. (add-text-properties (match-beginning 1) (match-end 1)
  4912. (list 'mouse-face 'highlight
  4913. 'rear-nonsticky org-nonsticky-props
  4914. 'keymap org-mouse-map))
  4915. t)))
  4916. (defun org-outline-level ()
  4917. (save-excursion
  4918. (looking-at outline-regexp)
  4919. (if (match-beginning 1)
  4920. (+ (org-get-string-indentation (match-string 1)) 1000)
  4921. (1- (- (match-end 0) (match-beginning 0))))))
  4922. (defvar org-font-lock-keywords nil)
  4923. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4924. "Regular expression matching a property line.")
  4925. (defun org-set-font-lock-defaults ()
  4926. (let* ((em org-fontify-emphasized-text)
  4927. (lk org-activate-links)
  4928. (org-font-lock-extra-keywords
  4929. (list
  4930. ;; Headlines
  4931. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4932. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4933. ;; Table lines
  4934. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4935. (1 'org-table t))
  4936. ;; Table internals
  4937. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4938. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4939. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4940. ;; Drawers
  4941. (list org-drawer-regexp '(0 'org-special-keyword t))
  4942. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4943. ;; Properties
  4944. (list org-property-re
  4945. '(1 'org-special-keyword t)
  4946. '(3 'org-property-value t))
  4947. (if org-format-transports-properties-p
  4948. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4949. ;; Links
  4950. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4951. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4952. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4953. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4954. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4955. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4956. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4957. '(org-hide-wide-columns (0 nil append))
  4958. ;; TODO lines
  4959. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4960. '(1 (org-get-todo-face 1) t))
  4961. ;; DONE
  4962. (if org-fontify-done-headline
  4963. (list (concat "^[*]+ +\\<\\("
  4964. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4965. "\\)\\(.*\\)")
  4966. '(2 'org-headline-done t))
  4967. nil)
  4968. ;; Priorities
  4969. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4970. ;; Special keywords
  4971. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4972. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4973. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4974. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4975. ;; Emphasis
  4976. (if em
  4977. (if (featurep 'xemacs)
  4978. '(org-do-emphasis-faces (0 nil append))
  4979. '(org-do-emphasis-faces)))
  4980. ;; Checkboxes
  4981. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4982. 2 'bold prepend)
  4983. (if org-provide-checkbox-statistics
  4984. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4985. (0 (org-get-checkbox-statistics-face) t)))
  4986. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4987. '(1 'org-archived prepend))
  4988. ;; Specials
  4989. '(org-do-latex-and-special-faces)
  4990. ;; Code
  4991. '(org-activate-code (1 'org-code t))
  4992. ;; COMMENT
  4993. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4994. "\\|" org-quote-string "\\)\\>")
  4995. '(1 'org-special-keyword t))
  4996. '("^#.*" (0 'font-lock-comment-face t))
  4997. )))
  4998. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4999. ;; Now set the full font-lock-keywords
  5000. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5001. (org-set-local 'font-lock-defaults
  5002. '(org-font-lock-keywords t nil nil backward-paragraph))
  5003. (kill-local-variable 'font-lock-keywords) nil))
  5004. (defvar org-m nil)
  5005. (defvar org-l nil)
  5006. (defvar org-f nil)
  5007. (defun org-get-level-face (n)
  5008. "Get the right face for match N in font-lock matching of healdines."
  5009. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5010. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5011. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5012. (cond
  5013. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5014. ((eq n 2) org-f)
  5015. (t (if org-level-color-stars-only nil org-f))))
  5016. (defun org-get-todo-face (kwd)
  5017. "Get the right face for a TODO keyword KWD.
  5018. If KWD is a number, get the corresponding match group."
  5019. (if (numberp kwd) (setq kwd (match-string kwd)))
  5020. (or (cdr (assoc kwd org-todo-keyword-faces))
  5021. (and (member kwd org-done-keywords) 'org-done)
  5022. 'org-todo))
  5023. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5024. "Remove fontification and activation overlays from links."
  5025. (font-lock-default-unfontify-region beg end)
  5026. (let* ((buffer-undo-list t)
  5027. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5028. (inhibit-modification-hooks t)
  5029. deactivate-mark buffer-file-name buffer-file-truename)
  5030. (remove-text-properties beg end
  5031. '(mouse-face t keymap t org-linked-text t
  5032. invisible t intangible t))))
  5033. ;;;; Visibility cycling, including org-goto and indirect buffer
  5034. ;;; Cycling
  5035. (defvar org-cycle-global-status nil)
  5036. (make-variable-buffer-local 'org-cycle-global-status)
  5037. (defvar org-cycle-subtree-status nil)
  5038. (make-variable-buffer-local 'org-cycle-subtree-status)
  5039. ;;;###autoload
  5040. (defun org-cycle (&optional arg)
  5041. "Visibility cycling for Org-mode.
  5042. - When this function is called with a prefix argument, rotate the entire
  5043. buffer through 3 states (global cycling)
  5044. 1. OVERVIEW: Show only top-level headlines.
  5045. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5046. 3. SHOW ALL: Show everything.
  5047. - When point is at the beginning of a headline, rotate the subtree started
  5048. by this line through 3 different states (local cycling)
  5049. 1. FOLDED: Only the main headline is shown.
  5050. 2. CHILDREN: The main headline and the direct children are shown.
  5051. From this state, you can move to one of the children
  5052. and zoom in further.
  5053. 3. SUBTREE: Show the entire subtree, including body text.
  5054. - When there is a numeric prefix, go up to a heading with level ARG, do
  5055. a `show-subtree' and return to the previous cursor position. If ARG
  5056. is negative, go up that many levels.
  5057. - When point is not at the beginning of a headline, execute
  5058. `indent-relative', like TAB normally does. See the option
  5059. `org-cycle-emulate-tab' for details.
  5060. - Special case: if point is at the beginning of the buffer and there is
  5061. no headline in line 1, this function will act as if called with prefix arg.
  5062. But only if also the variable `org-cycle-global-at-bob' is t."
  5063. (interactive "P")
  5064. (let* ((outline-regexp
  5065. (if (and (org-mode-p) org-cycle-include-plain-lists)
  5066. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  5067. outline-regexp))
  5068. (bob-special (and org-cycle-global-at-bob (bobp)
  5069. (not (looking-at outline-regexp))))
  5070. (org-cycle-hook
  5071. (if bob-special
  5072. (delq 'org-optimize-window-after-visibility-change
  5073. (copy-sequence org-cycle-hook))
  5074. org-cycle-hook))
  5075. (pos (point)))
  5076. (if (or bob-special (equal arg '(4)))
  5077. ;; special case: use global cycling
  5078. (setq arg t))
  5079. (cond
  5080. ((org-at-table-p 'any)
  5081. ;; Enter the table or move to the next field in the table
  5082. (or (org-table-recognize-table.el)
  5083. (progn
  5084. (if arg (org-table-edit-field t)
  5085. (org-table-justify-field-maybe)
  5086. (call-interactively 'org-table-next-field)))))
  5087. ((eq arg t) ;; Global cycling
  5088. (cond
  5089. ((and (eq last-command this-command)
  5090. (eq org-cycle-global-status 'overview))
  5091. ;; We just created the overview - now do table of contents
  5092. ;; This can be slow in very large buffers, so indicate action
  5093. (message "CONTENTS...")
  5094. (org-content)
  5095. (message "CONTENTS...done")
  5096. (setq org-cycle-global-status 'contents)
  5097. (run-hook-with-args 'org-cycle-hook 'contents))
  5098. ((and (eq last-command this-command)
  5099. (eq org-cycle-global-status 'contents))
  5100. ;; We just showed the table of contents - now show everything
  5101. (show-all)
  5102. (message "SHOW ALL")
  5103. (setq org-cycle-global-status 'all)
  5104. (run-hook-with-args 'org-cycle-hook 'all))
  5105. (t
  5106. ;; Default action: go to overview
  5107. (org-overview)
  5108. (message "OVERVIEW")
  5109. (setq org-cycle-global-status 'overview)
  5110. (run-hook-with-args 'org-cycle-hook 'overview))))
  5111. ((and org-drawers org-drawer-regexp
  5112. (save-excursion
  5113. (beginning-of-line 1)
  5114. (looking-at org-drawer-regexp)))
  5115. ;; Toggle block visibility
  5116. (org-flag-drawer
  5117. (not (get-char-property (match-end 0) 'invisible))))
  5118. ((integerp arg)
  5119. ;; Show-subtree, ARG levels up from here.
  5120. (save-excursion
  5121. (org-back-to-heading)
  5122. (outline-up-heading (if (< arg 0) (- arg)
  5123. (- (funcall outline-level) arg)))
  5124. (org-show-subtree)))
  5125. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5126. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5127. ;; At a heading: rotate between three different views
  5128. (org-back-to-heading)
  5129. (let ((goal-column 0) eoh eol eos)
  5130. ;; First, some boundaries
  5131. (save-excursion
  5132. (org-back-to-heading)
  5133. (save-excursion
  5134. (beginning-of-line 2)
  5135. (while (and (not (eobp)) ;; this is like `next-line'
  5136. (get-char-property (1- (point)) 'invisible))
  5137. (beginning-of-line 2)) (setq eol (point)))
  5138. (outline-end-of-heading) (setq eoh (point))
  5139. (org-end-of-subtree t)
  5140. (unless (eobp)
  5141. (skip-chars-forward " \t\n")
  5142. (beginning-of-line 1) ; in case this is an item
  5143. )
  5144. (setq eos (1- (point))))
  5145. ;; Find out what to do next and set `this-command'
  5146. (cond
  5147. ((= eos eoh)
  5148. ;; Nothing is hidden behind this heading
  5149. (message "EMPTY ENTRY")
  5150. (setq org-cycle-subtree-status nil)
  5151. (save-excursion
  5152. (goto-char eos)
  5153. (outline-next-heading)
  5154. (if (org-invisible-p) (org-flag-heading nil))))
  5155. ((or (>= eol eos)
  5156. (not (string-match "\\S-" (buffer-substring eol eos))))
  5157. ;; Entire subtree is hidden in one line: open it
  5158. (org-show-entry)
  5159. (show-children)
  5160. (message "CHILDREN")
  5161. (save-excursion
  5162. (goto-char eos)
  5163. (outline-next-heading)
  5164. (if (org-invisible-p) (org-flag-heading nil)))
  5165. (setq org-cycle-subtree-status 'children)
  5166. (run-hook-with-args 'org-cycle-hook 'children))
  5167. ((and (eq last-command this-command)
  5168. (eq org-cycle-subtree-status 'children))
  5169. ;; We just showed the children, now show everything.
  5170. (org-show-subtree)
  5171. (message "SUBTREE")
  5172. (setq org-cycle-subtree-status 'subtree)
  5173. (run-hook-with-args 'org-cycle-hook 'subtree))
  5174. (t
  5175. ;; Default action: hide the subtree.
  5176. (hide-subtree)
  5177. (message "FOLDED")
  5178. (setq org-cycle-subtree-status 'folded)
  5179. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5180. ;; TAB emulation
  5181. (buffer-read-only (org-back-to-heading))
  5182. ((org-try-cdlatex-tab))
  5183. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5184. (or (not (bolp))
  5185. (not (looking-at outline-regexp))))
  5186. (call-interactively (global-key-binding "\t")))
  5187. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5188. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5189. (or (and (eq org-cycle-emulate-tab 'white)
  5190. (= (match-end 0) (point-at-eol)))
  5191. (and (eq org-cycle-emulate-tab 'whitestart)
  5192. (>= (match-end 0) pos))))
  5193. t
  5194. (eq org-cycle-emulate-tab t))
  5195. ; (if (and (looking-at "[ \n\r\t]")
  5196. ; (string-match "^[ \t]*$" (buffer-substring
  5197. ; (point-at-bol) (point))))
  5198. ; (progn
  5199. ; (beginning-of-line 1)
  5200. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5201. (call-interactively (global-key-binding "\t")))
  5202. (t (save-excursion
  5203. (org-back-to-heading)
  5204. (org-cycle))))))
  5205. ;;;###autoload
  5206. (defun org-global-cycle (&optional arg)
  5207. "Cycle the global visibility. For details see `org-cycle'."
  5208. (interactive "P")
  5209. (let ((org-cycle-include-plain-lists
  5210. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5211. (if (integerp arg)
  5212. (progn
  5213. (show-all)
  5214. (hide-sublevels arg)
  5215. (setq org-cycle-global-status 'contents))
  5216. (org-cycle '(4)))))
  5217. (defun org-overview ()
  5218. "Switch to overview mode, shoing only top-level headlines.
  5219. Really, this shows all headlines with level equal or greater than the level
  5220. of the first headline in the buffer. This is important, because if the
  5221. first headline is not level one, then (hide-sublevels 1) gives confusing
  5222. results."
  5223. (interactive)
  5224. (let ((level (save-excursion
  5225. (goto-char (point-min))
  5226. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5227. (progn
  5228. (goto-char (match-beginning 0))
  5229. (funcall outline-level))))))
  5230. (and level (hide-sublevels level))))
  5231. (defun org-content (&optional arg)
  5232. "Show all headlines in the buffer, like a table of contents.
  5233. With numerical argument N, show content up to level N."
  5234. (interactive "P")
  5235. (save-excursion
  5236. ;; Visit all headings and show their offspring
  5237. (and (integerp arg) (org-overview))
  5238. (goto-char (point-max))
  5239. (catch 'exit
  5240. (while (and (progn (condition-case nil
  5241. (outline-previous-visible-heading 1)
  5242. (error (goto-char (point-min))))
  5243. t)
  5244. (looking-at outline-regexp))
  5245. (if (integerp arg)
  5246. (show-children (1- arg))
  5247. (show-branches))
  5248. (if (bobp) (throw 'exit nil))))))
  5249. (defun org-optimize-window-after-visibility-change (state)
  5250. "Adjust the window after a change in outline visibility.
  5251. This function is the default value of the hook `org-cycle-hook'."
  5252. (when (get-buffer-window (current-buffer))
  5253. (cond
  5254. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5255. ; ((eq state 'overview) (org-beginning-of-line))
  5256. ((eq state 'content) nil)
  5257. ((eq state 'all) nil)
  5258. ((eq state 'folded) nil)
  5259. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5260. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5261. (defun org-compact-display-after-subtree-move ()
  5262. (let (beg end)
  5263. (save-excursion
  5264. (if (org-up-heading-safe)
  5265. (progn
  5266. (hide-subtree)
  5267. (show-entry)
  5268. (show-children)
  5269. (org-cycle-show-empty-lines 'children)
  5270. (org-cycle-hide-drawers 'children))
  5271. (org-overview)))))
  5272. (defun org-cycle-show-empty-lines (state)
  5273. "Show empty lines above all visible headlines.
  5274. The region to be covered depends on STATE when called through
  5275. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5276. entire buffer covered. Note that an empty line is only shown if there
  5277. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5278. (when (> org-cycle-separator-lines 0)
  5279. (save-excursion
  5280. (let* ((n org-cycle-separator-lines)
  5281. (re (cond
  5282. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5283. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5284. (t (let ((ns (number-to-string (- n 2))))
  5285. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5286. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5287. beg end)
  5288. (cond
  5289. ((memq state '(overview contents t))
  5290. (setq beg (point-min) end (point-max)))
  5291. ((memq state '(children folded))
  5292. (setq beg (point) end (progn (org-end-of-subtree t t)
  5293. (beginning-of-line 2)
  5294. (point)))))
  5295. (when beg
  5296. (goto-char beg)
  5297. (while (re-search-forward re end t)
  5298. (if (not (get-char-property (match-end 1) 'invisible))
  5299. (outline-flag-region
  5300. (match-beginning 1) (match-end 1) nil)))))))
  5301. ;; Never hide empty lines at the end of the file.
  5302. (save-excursion
  5303. (goto-char (point-max))
  5304. (outline-previous-heading)
  5305. (outline-end-of-heading)
  5306. (if (and (looking-at "[ \t\n]+")
  5307. (= (match-end 0) (point-max)))
  5308. (outline-flag-region (point) (match-end 0) nil))))
  5309. (defun org-subtree-end-visible-p ()
  5310. "Is the end of the current subtree visible?"
  5311. (pos-visible-in-window-p
  5312. (save-excursion (org-end-of-subtree t) (point))))
  5313. (defun org-first-headline-recenter (&optional N)
  5314. "Move cursor to the first headline and recenter the headline.
  5315. Optional argument N means, put the headline into the Nth line of the window."
  5316. (goto-char (point-min))
  5317. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5318. (beginning-of-line)
  5319. (recenter (prefix-numeric-value N))))
  5320. ;;; Org-goto
  5321. (defvar org-goto-window-configuration nil)
  5322. (defvar org-goto-marker nil)
  5323. (defvar org-goto-map
  5324. (let ((map (make-sparse-keymap)))
  5325. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5326. (while (setq cmd (pop cmds))
  5327. (substitute-key-definition cmd cmd map global-map)))
  5328. (suppress-keymap map)
  5329. (org-defkey map "\C-m" 'org-goto-ret)
  5330. (org-defkey map [(return)] 'org-goto-ret)
  5331. (org-defkey map [(left)] 'org-goto-left)
  5332. (org-defkey map [(right)] 'org-goto-right)
  5333. (org-defkey map [(control ?g)] 'org-goto-quit)
  5334. (org-defkey map "\C-i" 'org-cycle)
  5335. (org-defkey map [(tab)] 'org-cycle)
  5336. (org-defkey map [(down)] 'outline-next-visible-heading)
  5337. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5338. (if org-goto-auto-isearch
  5339. (if (fboundp 'define-key-after)
  5340. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5341. nil)
  5342. (org-defkey map "q" 'org-goto-quit)
  5343. (org-defkey map "n" 'outline-next-visible-heading)
  5344. (org-defkey map "p" 'outline-previous-visible-heading)
  5345. (org-defkey map "f" 'outline-forward-same-level)
  5346. (org-defkey map "b" 'outline-backward-same-level)
  5347. (org-defkey map "u" 'outline-up-heading))
  5348. (org-defkey map "/" 'org-occur)
  5349. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5350. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5351. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5352. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5353. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5354. map))
  5355. (defconst org-goto-help
  5356. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5357. RET=jump to location [Q]uit and return to previous location
  5358. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5359. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5360. (defun org-goto (&optional alternative-interface)
  5361. "Look up a different location in the current file, keeping current visibility.
  5362. When you want look-up or go to a different location in a document, the
  5363. fastest way is often to fold the entire buffer and then dive into the tree.
  5364. This method has the disadvantage, that the previous location will be folded,
  5365. which may not be what you want.
  5366. This command works around this by showing a copy of the current buffer
  5367. in an indirect buffer, in overview mode. You can dive into the tree in
  5368. that copy, use org-occur and incremental search to find a location.
  5369. When pressing RET or `Q', the command returns to the original buffer in
  5370. which the visibility is still unchanged. After RET is will also jump to
  5371. the location selected in the indirect buffer and expose the
  5372. the headline hierarchy above."
  5373. (interactive "P")
  5374. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5375. (org-refile-use-outline-path t)
  5376. (interface
  5377. (if (not alternative-interface)
  5378. org-goto-interface
  5379. (if (eq org-goto-interface 'outline)
  5380. 'outline-path-completion
  5381. 'outline)))
  5382. (org-goto-start-pos (point))
  5383. (selected-point
  5384. (if (eq interface 'outline)
  5385. (car (org-get-location (current-buffer) org-goto-help))
  5386. (nth 3 (org-refile-get-location "Goto: ")))))
  5387. (if selected-point
  5388. (progn
  5389. (org-mark-ring-push org-goto-start-pos)
  5390. (goto-char selected-point)
  5391. (if (or (org-invisible-p) (org-invisible-p2))
  5392. (org-show-context 'org-goto)))
  5393. (message "Quit"))))
  5394. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5395. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5396. (defvar org-goto-local-auto-isearch-map) ; defined below
  5397. (defun org-get-location (buf help)
  5398. "Let the user select a location in the Org-mode buffer BUF.
  5399. This function uses a recursive edit. It returns the selected position
  5400. or nil."
  5401. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5402. (isearch-hide-immediately nil)
  5403. (isearch-search-fun-function
  5404. (lambda () 'org-goto-local-search-forward-headings))
  5405. (org-goto-selected-point org-goto-exit-command))
  5406. (save-excursion
  5407. (save-window-excursion
  5408. (delete-other-windows)
  5409. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5410. (switch-to-buffer
  5411. (condition-case nil
  5412. (make-indirect-buffer (current-buffer) "*org-goto*")
  5413. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5414. (with-output-to-temp-buffer "*Help*"
  5415. (princ help))
  5416. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5417. (setq buffer-read-only nil)
  5418. (let ((org-startup-truncated t)
  5419. (org-startup-folded nil)
  5420. (org-startup-align-all-tables nil))
  5421. (org-mode)
  5422. (org-overview))
  5423. (setq buffer-read-only t)
  5424. (if (and (boundp 'org-goto-start-pos)
  5425. (integer-or-marker-p org-goto-start-pos))
  5426. (let ((org-show-hierarchy-above t)
  5427. (org-show-siblings t)
  5428. (org-show-following-heading t))
  5429. (goto-char org-goto-start-pos)
  5430. (and (org-invisible-p) (org-show-context)))
  5431. (goto-char (point-min)))
  5432. (org-beginning-of-line)
  5433. (message "Select location and press RET")
  5434. (use-local-map org-goto-map)
  5435. (recursive-edit)
  5436. ))
  5437. (kill-buffer "*org-goto*")
  5438. (cons org-goto-selected-point org-goto-exit-command)))
  5439. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5440. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5441. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5442. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5443. (defun org-goto-local-search-forward-headings (string bound noerror)
  5444. "Search and make sure that anu matches are in headlines."
  5445. (catch 'return
  5446. (while (search-forward string bound noerror)
  5447. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5448. (and (member :headline context)
  5449. (not (member :tags context))))
  5450. (throw 'return (point))))))
  5451. (defun org-goto-local-auto-isearch ()
  5452. "Start isearch."
  5453. (interactive)
  5454. (goto-char (point-min))
  5455. (let ((keys (this-command-keys)))
  5456. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5457. (isearch-mode t)
  5458. (isearch-process-search-char (string-to-char keys)))))
  5459. (defun org-goto-ret (&optional arg)
  5460. "Finish `org-goto' by going to the new location."
  5461. (interactive "P")
  5462. (setq org-goto-selected-point (point)
  5463. org-goto-exit-command 'return)
  5464. (throw 'exit nil))
  5465. (defun org-goto-left ()
  5466. "Finish `org-goto' by going to the new location."
  5467. (interactive)
  5468. (if (org-on-heading-p)
  5469. (progn
  5470. (beginning-of-line 1)
  5471. (setq org-goto-selected-point (point)
  5472. org-goto-exit-command 'left)
  5473. (throw 'exit nil))
  5474. (error "Not on a heading")))
  5475. (defun org-goto-right ()
  5476. "Finish `org-goto' by going to the new location."
  5477. (interactive)
  5478. (if (org-on-heading-p)
  5479. (progn
  5480. (setq org-goto-selected-point (point)
  5481. org-goto-exit-command 'right)
  5482. (throw 'exit nil))
  5483. (error "Not on a heading")))
  5484. (defun org-goto-quit ()
  5485. "Finish `org-goto' without cursor motion."
  5486. (interactive)
  5487. (setq org-goto-selected-point nil)
  5488. (setq org-goto-exit-command 'quit)
  5489. (throw 'exit nil))
  5490. ;;; Indirect buffer display of subtrees
  5491. (defvar org-indirect-dedicated-frame nil
  5492. "This is the frame being used for indirect tree display.")
  5493. (defvar org-last-indirect-buffer nil)
  5494. (defun org-tree-to-indirect-buffer (&optional arg)
  5495. "Create indirect buffer and narrow it to current subtree.
  5496. With numerical prefix ARG, go up to this level and then take that tree.
  5497. If ARG is negative, go up that many levels.
  5498. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5499. indirect buffer previously made with this command, to avoid proliferation of
  5500. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5501. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5502. is kept so that you can work with several indirect buffers at the same time.
  5503. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5504. requests that a new frame be made for the new buffer, so that the dedicated
  5505. frame is not changed."
  5506. (interactive "P")
  5507. (let ((cbuf (current-buffer))
  5508. (cwin (selected-window))
  5509. (pos (point))
  5510. beg end level heading ibuf)
  5511. (save-excursion
  5512. (org-back-to-heading t)
  5513. (when (numberp arg)
  5514. (setq level (org-outline-level))
  5515. (if (< arg 0) (setq arg (+ level arg)))
  5516. (while (> (setq level (org-outline-level)) arg)
  5517. (outline-up-heading 1 t)))
  5518. (setq beg (point)
  5519. heading (org-get-heading))
  5520. (org-end-of-subtree t) (setq end (point)))
  5521. (if (and (buffer-live-p org-last-indirect-buffer)
  5522. (not (eq org-indirect-buffer-display 'new-frame))
  5523. (not arg))
  5524. (kill-buffer org-last-indirect-buffer))
  5525. (setq ibuf (org-get-indirect-buffer cbuf)
  5526. org-last-indirect-buffer ibuf)
  5527. (cond
  5528. ((or (eq org-indirect-buffer-display 'new-frame)
  5529. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5530. (select-frame (make-frame))
  5531. (delete-other-windows)
  5532. (switch-to-buffer ibuf)
  5533. (org-set-frame-title heading))
  5534. ((eq org-indirect-buffer-display 'dedicated-frame)
  5535. (raise-frame
  5536. (select-frame (or (and org-indirect-dedicated-frame
  5537. (frame-live-p org-indirect-dedicated-frame)
  5538. org-indirect-dedicated-frame)
  5539. (setq org-indirect-dedicated-frame (make-frame)))))
  5540. (delete-other-windows)
  5541. (switch-to-buffer ibuf)
  5542. (org-set-frame-title (concat "Indirect: " heading)))
  5543. ((eq org-indirect-buffer-display 'current-window)
  5544. (switch-to-buffer ibuf))
  5545. ((eq org-indirect-buffer-display 'other-window)
  5546. (pop-to-buffer ibuf))
  5547. (t (error "Invalid value.")))
  5548. (if (featurep 'xemacs)
  5549. (save-excursion (org-mode) (turn-on-font-lock)))
  5550. (narrow-to-region beg end)
  5551. (show-all)
  5552. (goto-char pos)
  5553. (and (window-live-p cwin) (select-window cwin))))
  5554. (defun org-get-indirect-buffer (&optional buffer)
  5555. (setq buffer (or buffer (current-buffer)))
  5556. (let ((n 1) (base (buffer-name buffer)) bname)
  5557. (while (buffer-live-p
  5558. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5559. (setq n (1+ n)))
  5560. (condition-case nil
  5561. (make-indirect-buffer buffer bname 'clone)
  5562. (error (make-indirect-buffer buffer bname)))))
  5563. (defun org-set-frame-title (title)
  5564. "Set the title of the current frame to the string TITLE."
  5565. ;; FIXME: how to name a single frame in XEmacs???
  5566. (unless (featurep 'xemacs)
  5567. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5568. ;;;; Structure editing
  5569. ;;; Inserting headlines
  5570. (defun org-insert-heading (&optional force-heading)
  5571. "Insert a new heading or item with same depth at point.
  5572. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5573. If point is at the beginning of a headline, insert a sibling before the
  5574. current headline. If point is not at the beginning, do not split the line,
  5575. but create the new hedline after the current line."
  5576. (interactive "P")
  5577. (if (= (buffer-size) 0)
  5578. (insert "\n* ")
  5579. (when (or force-heading (not (org-insert-item)))
  5580. (let* ((head (save-excursion
  5581. (condition-case nil
  5582. (progn
  5583. (org-back-to-heading)
  5584. (match-string 0))
  5585. (error "*"))))
  5586. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5587. pos)
  5588. (cond
  5589. ((and (org-on-heading-p) (bolp)
  5590. (or (bobp)
  5591. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5592. ;; insert before the current line
  5593. (open-line (if blank 2 1)))
  5594. ((and (bolp)
  5595. (or (bobp)
  5596. (save-excursion
  5597. (backward-char 1) (not (org-invisible-p)))))
  5598. ;; insert right here
  5599. nil)
  5600. (t
  5601. ; ;; in the middle of the line
  5602. ; (org-show-entry)
  5603. ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
  5604. ; (if (and
  5605. ; (org-on-heading-p)
  5606. ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
  5607. ; ;; protect the tags
  5608. ;; (let ((tags (match-string 2)) pos)
  5609. ; (delete-region (match-beginning 1) (match-end 1))
  5610. ; (setq pos (point-at-bol))
  5611. ; (newline (if blank 2 1))
  5612. ; (save-excursion
  5613. ; (goto-char pos)
  5614. ; (end-of-line 1)
  5615. ; (insert " " tags)
  5616. ; (org-set-tags nil 'align)))
  5617. ; (newline (if blank 2 1)))
  5618. ; (newline (if blank 2 1))))
  5619. ;; in the middle of the line
  5620. (org-show-entry)
  5621. (let ((split
  5622. (org-get-alist-option org-M-RET-may-split-line 'headline))
  5623. tags pos)
  5624. (if (org-on-heading-p)
  5625. (progn
  5626. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5627. (setq tags (and (match-end 2) (match-string 2)))
  5628. (and (match-end 1)
  5629. (delete-region (match-beginning 1) (match-end 1)))
  5630. (setq pos (point-at-bol))
  5631. (or split (end-of-line 1))
  5632. (delete-horizontal-space)
  5633. (newline (if blank 2 1))
  5634. (when tags
  5635. (save-excursion
  5636. (goto-char pos)
  5637. (end-of-line 1)
  5638. (insert " " tags)
  5639. (org-set-tags nil 'align))))
  5640. (or split (end-of-line 1))
  5641. (newline (if blank 2 1))))))
  5642. (insert head) (just-one-space)
  5643. (setq pos (point))
  5644. (end-of-line 1)
  5645. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5646. (run-hooks 'org-insert-heading-hook)))))
  5647. (defun org-insert-heading-after-current ()
  5648. "Insert a new heading with same level as current, after current subtree."
  5649. (interactive)
  5650. (org-back-to-heading)
  5651. (org-insert-heading)
  5652. (org-move-subtree-down)
  5653. (end-of-line 1))
  5654. (defun org-insert-todo-heading (arg)
  5655. "Insert a new heading with the same level and TODO state as current heading.
  5656. If the heading has no TODO state, or if the state is DONE, use the first
  5657. state (TODO by default). Also with prefix arg, force first state."
  5658. (interactive "P")
  5659. (when (not (org-insert-item 'checkbox))
  5660. (org-insert-heading)
  5661. (save-excursion
  5662. (org-back-to-heading)
  5663. (outline-previous-heading)
  5664. (looking-at org-todo-line-regexp))
  5665. (if (or arg
  5666. (not (match-beginning 2))
  5667. (member (match-string 2) org-done-keywords))
  5668. (insert (car org-todo-keywords-1) " ")
  5669. (insert (match-string 2) " "))))
  5670. (defun org-insert-subheading (arg)
  5671. "Insert a new subheading and demote it.
  5672. Works for outline headings and for plain lists alike."
  5673. (interactive "P")
  5674. (org-insert-heading arg)
  5675. (cond
  5676. ((org-on-heading-p) (org-do-demote))
  5677. ((org-at-item-p) (org-indent-item 1))))
  5678. (defun org-insert-todo-subheading (arg)
  5679. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5680. Works for outline headings and for plain lists alike."
  5681. (interactive "P")
  5682. (org-insert-todo-heading arg)
  5683. (cond
  5684. ((org-on-heading-p) (org-do-demote))
  5685. ((org-at-item-p) (org-indent-item 1))))
  5686. ;;; Promotion and Demotion
  5687. (defun org-promote-subtree ()
  5688. "Promote the entire subtree.
  5689. See also `org-promote'."
  5690. (interactive)
  5691. (save-excursion
  5692. (org-map-tree 'org-promote))
  5693. (org-fix-position-after-promote))
  5694. (defun org-demote-subtree ()
  5695. "Demote the entire subtree. See `org-demote'.
  5696. See also `org-promote'."
  5697. (interactive)
  5698. (save-excursion
  5699. (org-map-tree 'org-demote))
  5700. (org-fix-position-after-promote))
  5701. (defun org-do-promote ()
  5702. "Promote the current heading higher up the tree.
  5703. If the region is active in `transient-mark-mode', promote all headings
  5704. in the region."
  5705. (interactive)
  5706. (save-excursion
  5707. (if (org-region-active-p)
  5708. (org-map-region 'org-promote (region-beginning) (region-end))
  5709. (org-promote)))
  5710. (org-fix-position-after-promote))
  5711. (defun org-do-demote ()
  5712. "Demote the current heading lower down the tree.
  5713. If the region is active in `transient-mark-mode', demote all headings
  5714. in the region."
  5715. (interactive)
  5716. (save-excursion
  5717. (if (org-region-active-p)
  5718. (org-map-region 'org-demote (region-beginning) (region-end))
  5719. (org-demote)))
  5720. (org-fix-position-after-promote))
  5721. (defun org-fix-position-after-promote ()
  5722. "Make sure that after pro/demotion cursor position is right."
  5723. (let ((pos (point)))
  5724. (when (save-excursion
  5725. (beginning-of-line 1)
  5726. (looking-at org-todo-line-regexp)
  5727. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5728. (cond ((eobp) (insert " "))
  5729. ((eolp) (insert " "))
  5730. ((equal (char-after) ?\ ) (forward-char 1))))))
  5731. (defun org-reduced-level (l)
  5732. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5733. (defun org-get-legal-level (level &optional change)
  5734. "Rectify a level change under the influence of `org-odd-levels-only'
  5735. LEVEL is a current level, CHANGE is by how much the level should be
  5736. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5737. even level numbers will become the next higher odd number."
  5738. (if org-odd-levels-only
  5739. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5740. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5741. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5742. (max 1 (+ level change))))
  5743. (defun org-promote ()
  5744. "Promote the current heading higher up the tree.
  5745. If the region is active in `transient-mark-mode', promote all headings
  5746. in the region."
  5747. (org-back-to-heading t)
  5748. (let* ((level (save-match-data (funcall outline-level)))
  5749. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5750. (diff (abs (- level (length up-head) -1))))
  5751. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5752. (replace-match up-head nil t)
  5753. ;; Fixup tag positioning
  5754. (and org-auto-align-tags (org-set-tags nil t))
  5755. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5756. (defun org-demote ()
  5757. "Demote the current heading lower down the tree.
  5758. If the region is active in `transient-mark-mode', demote all headings
  5759. in the region."
  5760. (org-back-to-heading t)
  5761. (let* ((level (save-match-data (funcall outline-level)))
  5762. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5763. (diff (abs (- level (length down-head) -1))))
  5764. (replace-match down-head nil t)
  5765. ;; Fixup tag positioning
  5766. (and org-auto-align-tags (org-set-tags nil t))
  5767. (if org-adapt-indentation (org-fixup-indentation diff))))
  5768. (defun org-map-tree (fun)
  5769. "Call FUN for every heading underneath the current one."
  5770. (org-back-to-heading)
  5771. (let ((level (funcall outline-level)))
  5772. (save-excursion
  5773. (funcall fun)
  5774. (while (and (progn
  5775. (outline-next-heading)
  5776. (> (funcall outline-level) level))
  5777. (not (eobp)))
  5778. (funcall fun)))))
  5779. (defun org-map-region (fun beg end)
  5780. "Call FUN for every heading between BEG and END."
  5781. (let ((org-ignore-region t))
  5782. (save-excursion
  5783. (setq end (copy-marker end))
  5784. (goto-char beg)
  5785. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5786. (< (point) end))
  5787. (funcall fun))
  5788. (while (and (progn
  5789. (outline-next-heading)
  5790. (< (point) end))
  5791. (not (eobp)))
  5792. (funcall fun)))))
  5793. (defun org-fixup-indentation (diff)
  5794. "Change the indentation in the current entry by DIFF
  5795. However, if any line in the current entry has no indentation, or if it
  5796. would end up with no indentation after the change, nothing at all is done."
  5797. (save-excursion
  5798. (let ((end (save-excursion (outline-next-heading)
  5799. (point-marker)))
  5800. (prohibit (if (> diff 0)
  5801. "^\\S-"
  5802. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5803. col)
  5804. (unless (save-excursion (end-of-line 1)
  5805. (re-search-forward prohibit end t))
  5806. (while (and (< (point) end)
  5807. (re-search-forward "^[ \t]+" end t))
  5808. (goto-char (match-end 0))
  5809. (setq col (current-column))
  5810. (if (< diff 0) (replace-match ""))
  5811. (indent-to (+ diff col))))
  5812. (move-marker end nil))))
  5813. (defun org-convert-to-odd-levels ()
  5814. "Convert an org-mode file with all levels allowed to one with odd levels.
  5815. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5816. level 5 etc."
  5817. (interactive)
  5818. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5819. (let ((org-odd-levels-only nil) n)
  5820. (save-excursion
  5821. (goto-char (point-min))
  5822. (while (re-search-forward "^\\*\\*+ " nil t)
  5823. (setq n (- (length (match-string 0)) 2))
  5824. (while (>= (setq n (1- n)) 0)
  5825. (org-demote))
  5826. (end-of-line 1))))))
  5827. (defun org-convert-to-oddeven-levels ()
  5828. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5829. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5830. section with an even level, conversion would destroy the structure of the file. An error
  5831. is signaled in this case."
  5832. (interactive)
  5833. (goto-char (point-min))
  5834. ;; First check if there are no even levels
  5835. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5836. (org-show-context t)
  5837. (error "Not all levels are odd in this file. Conversion not possible."))
  5838. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5839. (let ((org-odd-levels-only nil) n)
  5840. (save-excursion
  5841. (goto-char (point-min))
  5842. (while (re-search-forward "^\\*\\*+ " nil t)
  5843. (setq n (/ (1- (length (match-string 0))) 2))
  5844. (while (>= (setq n (1- n)) 0)
  5845. (org-promote))
  5846. (end-of-line 1))))))
  5847. (defun org-tr-level (n)
  5848. "Make N odd if required."
  5849. (if org-odd-levels-only (1+ (/ n 2)) n))
  5850. ;;; Vertical tree motion, cutting and pasting of subtrees
  5851. (defun org-move-subtree-up (&optional arg)
  5852. "Move the current subtree up past ARG headlines of the same level."
  5853. (interactive "p")
  5854. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5855. (defun org-move-subtree-down (&optional arg)
  5856. "Move the current subtree down past ARG headlines of the same level."
  5857. (interactive "p")
  5858. (setq arg (prefix-numeric-value arg))
  5859. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5860. 'outline-get-last-sibling))
  5861. (ins-point (make-marker))
  5862. (cnt (abs arg))
  5863. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5864. ;; Select the tree
  5865. (org-back-to-heading)
  5866. (setq beg0 (point))
  5867. (save-excursion
  5868. (setq ne-beg (org-back-over-empty-lines))
  5869. (setq beg (point)))
  5870. (save-match-data
  5871. (save-excursion (outline-end-of-heading)
  5872. (setq folded (org-invisible-p)))
  5873. (outline-end-of-subtree))
  5874. (outline-next-heading)
  5875. (setq ne-end (org-back-over-empty-lines))
  5876. (setq end (point))
  5877. (goto-char beg0)
  5878. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5879. ;; include less whitespace
  5880. (save-excursion
  5881. (goto-char beg)
  5882. (forward-line (- ne-beg ne-end))
  5883. (setq beg (point))))
  5884. ;; Find insertion point, with error handling
  5885. (while (> cnt 0)
  5886. (or (and (funcall movfunc) (looking-at outline-regexp))
  5887. (progn (goto-char beg0)
  5888. (error "Cannot move past superior level or buffer limit")))
  5889. (setq cnt (1- cnt)))
  5890. (if (> arg 0)
  5891. ;; Moving forward - still need to move over subtree
  5892. (progn (org-end-of-subtree t t)
  5893. (save-excursion
  5894. (org-back-over-empty-lines)
  5895. (or (bolp) (newline)))))
  5896. (setq ne-ins (org-back-over-empty-lines))
  5897. (move-marker ins-point (point))
  5898. (setq txt (buffer-substring beg end))
  5899. (delete-region beg end)
  5900. (outline-flag-region (1- beg) beg nil)
  5901. (outline-flag-region (1- (point)) (point) nil)
  5902. (insert txt)
  5903. (or (bolp) (insert "\n"))
  5904. (setq ins-end (point))
  5905. (goto-char ins-point)
  5906. (org-skip-whitespace)
  5907. (when (and (< arg 0)
  5908. (org-first-sibling-p)
  5909. (> ne-ins ne-beg))
  5910. ;; Move whitespace back to beginning
  5911. (save-excursion
  5912. (goto-char ins-end)
  5913. (let ((kill-whole-line t))
  5914. (kill-line (- ne-ins ne-beg)) (point)))
  5915. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5916. (move-marker ins-point nil)
  5917. (org-compact-display-after-subtree-move)
  5918. (unless folded
  5919. (org-show-entry)
  5920. (show-children)
  5921. (org-cycle-hide-drawers 'children))))
  5922. (defvar org-subtree-clip ""
  5923. "Clipboard for cut and paste of subtrees.
  5924. This is actually only a copy of the kill, because we use the normal kill
  5925. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5926. (defvar org-subtree-clip-folded nil
  5927. "Was the last copied subtree folded?
  5928. This is used to fold the tree back after pasting.")
  5929. (defun org-cut-subtree (&optional n)
  5930. "Cut the current subtree into the clipboard.
  5931. With prefix arg N, cut this many sequential subtrees.
  5932. This is a short-hand for marking the subtree and then cutting it."
  5933. (interactive "p")
  5934. (org-copy-subtree n 'cut))
  5935. (defun org-copy-subtree (&optional n cut)
  5936. "Cut the current subtree into the clipboard.
  5937. With prefix arg N, cut this many sequential subtrees.
  5938. This is a short-hand for marking the subtree and then copying it.
  5939. If CUT is non-nil, actually cut the subtree."
  5940. (interactive "p")
  5941. (let (beg end folded (beg0 (point)))
  5942. (if (interactive-p)
  5943. (org-back-to-heading nil) ; take what looks like a subtree
  5944. (org-back-to-heading t)) ; take what is really there
  5945. (org-back-over-empty-lines)
  5946. (setq beg (point))
  5947. (skip-chars-forward " \t\r\n")
  5948. (save-match-data
  5949. (save-excursion (outline-end-of-heading)
  5950. (setq folded (org-invisible-p)))
  5951. (condition-case nil
  5952. (outline-forward-same-level (1- n))
  5953. (error nil))
  5954. (org-end-of-subtree t t))
  5955. (org-back-over-empty-lines)
  5956. (setq end (point))
  5957. (goto-char beg0)
  5958. (when (> end beg)
  5959. (setq org-subtree-clip-folded folded)
  5960. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5961. (setq org-subtree-clip (current-kill 0))
  5962. (message "%s: Subtree(s) with %d characters"
  5963. (if cut "Cut" "Copied")
  5964. (length org-subtree-clip)))))
  5965. (defun org-paste-subtree (&optional level tree)
  5966. "Paste the clipboard as a subtree, with modification of headline level.
  5967. The entire subtree is promoted or demoted in order to match a new headline
  5968. level. By default, the new level is derived from the visible headings
  5969. before and after the insertion point, and taken to be the inferior headline
  5970. level of the two. So if the previous visible heading is level 3 and the
  5971. next is level 4 (or vice versa), level 4 will be used for insertion.
  5972. This makes sure that the subtree remains an independent subtree and does
  5973. not swallow low level entries.
  5974. You can also force a different level, either by using a numeric prefix
  5975. argument, or by inserting the heading marker by hand. For example, if the
  5976. cursor is after \"*****\", then the tree will be shifted to level 5.
  5977. If you want to insert the tree as is, just use \\[yank].
  5978. If optional TREE is given, use this text instead of the kill ring."
  5979. (interactive "P")
  5980. (unless (org-kill-is-subtree-p tree)
  5981. (error "%s"
  5982. (substitute-command-keys
  5983. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5984. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5985. (^re (concat "^\\(" outline-regexp "\\)"))
  5986. (re (concat "\\(" outline-regexp "\\)"))
  5987. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5988. (old-level (if (string-match ^re txt)
  5989. (- (match-end 0) (match-beginning 0) 1)
  5990. -1))
  5991. (force-level (cond (level (prefix-numeric-value level))
  5992. ((string-match
  5993. ^re_ (buffer-substring (point-at-bol) (point)))
  5994. (- (match-end 1) (match-beginning 1)))
  5995. (t nil)))
  5996. (previous-level (save-excursion
  5997. (condition-case nil
  5998. (progn
  5999. (outline-previous-visible-heading 1)
  6000. (if (looking-at re)
  6001. (- (match-end 0) (match-beginning 0) 1)
  6002. 1))
  6003. (error 1))))
  6004. (next-level (save-excursion
  6005. (condition-case nil
  6006. (progn
  6007. (or (looking-at outline-regexp)
  6008. (outline-next-visible-heading 1))
  6009. (if (looking-at re)
  6010. (- (match-end 0) (match-beginning 0) 1)
  6011. 1))
  6012. (error 1))))
  6013. (new-level (or force-level (max previous-level next-level)))
  6014. (shift (if (or (= old-level -1)
  6015. (= new-level -1)
  6016. (= old-level new-level))
  6017. 0
  6018. (- new-level old-level)))
  6019. (delta (if (> shift 0) -1 1))
  6020. (func (if (> shift 0) 'org-demote 'org-promote))
  6021. (org-odd-levels-only nil)
  6022. beg end)
  6023. ;; Remove the forced level indicator
  6024. (if force-level
  6025. (delete-region (point-at-bol) (point)))
  6026. ;; Paste
  6027. (beginning-of-line 1)
  6028. (org-back-over-empty-lines) ;; FIXME: correct fix????
  6029. (setq beg (point))
  6030. (insert-before-markers txt) ;; FIXME: correct fix????
  6031. (unless (string-match "\n\\'" txt) (insert "\n"))
  6032. (setq end (point))
  6033. (goto-char beg)
  6034. (skip-chars-forward " \t\n\r")
  6035. (setq beg (point))
  6036. ;; Shift if necessary
  6037. (unless (= shift 0)
  6038. (save-restriction
  6039. (narrow-to-region beg end)
  6040. (while (not (= shift 0))
  6041. (org-map-region func (point-min) (point-max))
  6042. (setq shift (+ delta shift)))
  6043. (goto-char (point-min))))
  6044. (when (interactive-p)
  6045. (message "Clipboard pasted as level %d subtree" new-level))
  6046. (if (and kill-ring
  6047. (eq org-subtree-clip (current-kill 0))
  6048. org-subtree-clip-folded)
  6049. ;; The tree was folded before it was killed/copied
  6050. (hide-subtree))))
  6051. (defun org-kill-is-subtree-p (&optional txt)
  6052. "Check if the current kill is an outline subtree, or a set of trees.
  6053. Returns nil if kill does not start with a headline, or if the first
  6054. headline level is not the largest headline level in the tree.
  6055. So this will actually accept several entries of equal levels as well,
  6056. which is OK for `org-paste-subtree'.
  6057. If optional TXT is given, check this string instead of the current kill."
  6058. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6059. (start-level (and kill
  6060. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6061. org-outline-regexp "\\)")
  6062. kill)
  6063. (- (match-end 2) (match-beginning 2) 1)))
  6064. (re (concat "^" org-outline-regexp))
  6065. (start (1+ (match-beginning 2))))
  6066. (if (not start-level)
  6067. (progn
  6068. nil) ;; does not even start with a heading
  6069. (catch 'exit
  6070. (while (setq start (string-match re kill (1+ start)))
  6071. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6072. (throw 'exit nil)))
  6073. t))))
  6074. (defun org-narrow-to-subtree ()
  6075. "Narrow buffer to the current subtree."
  6076. (interactive)
  6077. (save-excursion
  6078. (save-match-data
  6079. (narrow-to-region
  6080. (progn (org-back-to-heading) (point))
  6081. (progn (org-end-of-subtree t t) (point))))))
  6082. ;;; Outline Sorting
  6083. (defun org-sort (with-case)
  6084. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6085. Optional argument WITH-CASE means sort case-sensitively."
  6086. (interactive "P")
  6087. (if (org-at-table-p)
  6088. (org-call-with-arg 'org-table-sort-lines with-case)
  6089. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6090. (defvar org-priority-regexp) ; defined later in the file
  6091. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  6092. "Sort entries on a certain level of an outline tree.
  6093. If there is an active region, the entries in the region are sorted.
  6094. Else, if the cursor is before the first entry, sort the top-level items.
  6095. Else, the children of the entry at point are sorted.
  6096. Sorting can be alphabetically, numerically, and by date/time as given by
  6097. the first time stamp in the entry. The command prompts for the sorting
  6098. type unless it has been given to the function through the SORTING-TYPE
  6099. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  6100. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6101. called with point at the beginning of the record. It must return either
  6102. a string or a number that should serve as the sorting key for that record.
  6103. Comparing entries ignores case by default. However, with an optional argument
  6104. WITH-CASE, the sorting considers case as well."
  6105. (interactive "P")
  6106. (let ((case-func (if with-case 'identity 'downcase))
  6107. start beg end stars re re2
  6108. txt what tmp plain-list-p)
  6109. ;; Find beginning and end of region to sort
  6110. (cond
  6111. ((org-region-active-p)
  6112. ;; we will sort the region
  6113. (setq end (region-end)
  6114. what "region")
  6115. (goto-char (region-beginning))
  6116. (if (not (org-on-heading-p)) (outline-next-heading))
  6117. (setq start (point)))
  6118. ((org-at-item-p)
  6119. ;; we will sort this plain list
  6120. (org-beginning-of-item-list) (setq start (point))
  6121. (org-end-of-item-list) (setq end (point))
  6122. (goto-char start)
  6123. (setq plain-list-p t
  6124. what "plain list"))
  6125. ((or (org-on-heading-p)
  6126. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6127. ;; we will sort the children of the current headline
  6128. (org-back-to-heading)
  6129. (setq start (point)
  6130. end (progn (org-end-of-subtree t t)
  6131. (org-back-over-empty-lines)
  6132. (point))
  6133. what "children")
  6134. (goto-char start)
  6135. (show-subtree)
  6136. (outline-next-heading))
  6137. (t
  6138. ;; we will sort the top-level entries in this file
  6139. (goto-char (point-min))
  6140. (or (org-on-heading-p) (outline-next-heading))
  6141. (setq start (point) end (point-max) what "top-level")
  6142. (goto-char start)
  6143. (show-all)))
  6144. (setq beg (point))
  6145. (if (>= beg end) (error "Nothing to sort"))
  6146. (unless plain-list-p
  6147. (looking-at "\\(\\*+\\)")
  6148. (setq stars (match-string 1)
  6149. re (concat "^" (regexp-quote stars) " +")
  6150. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6151. txt (buffer-substring beg end))
  6152. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6153. (if (and (not (equal stars "*")) (string-match re2 txt))
  6154. (error "Region to sort contains a level above the first entry")))
  6155. (unless sorting-type
  6156. (message
  6157. (if plain-list-p
  6158. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6159. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6160. what)
  6161. (setq sorting-type (read-char-exclusive))
  6162. (and (= (downcase sorting-type) ?f)
  6163. (setq getkey-func
  6164. (completing-read "Sort using function: "
  6165. obarray 'fboundp t nil nil))
  6166. (setq getkey-func (intern getkey-func)))
  6167. (and (= (downcase sorting-type) ?r)
  6168. (setq property
  6169. (completing-read "Property: "
  6170. (mapcar 'list (org-buffer-property-keys t))
  6171. nil t))))
  6172. (message "Sorting entries...")
  6173. (save-restriction
  6174. (narrow-to-region start end)
  6175. (let ((dcst (downcase sorting-type))
  6176. (now (current-time)))
  6177. (sort-subr
  6178. (/= dcst sorting-type)
  6179. ;; This function moves to the beginning character of the "record" to
  6180. ;; be sorted.
  6181. (if plain-list-p
  6182. (lambda nil
  6183. (if (org-at-item-p) t (goto-char (point-max))))
  6184. (lambda nil
  6185. (if (re-search-forward re nil t)
  6186. (goto-char (match-beginning 0))
  6187. (goto-char (point-max)))))
  6188. ;; This function moves to the last character of the "record" being
  6189. ;; sorted.
  6190. (if plain-list-p
  6191. 'org-end-of-item
  6192. (lambda nil
  6193. (save-match-data
  6194. (condition-case nil
  6195. (outline-forward-same-level 1)
  6196. (error
  6197. (goto-char (point-max)))))))
  6198. ;; This function returns the value that gets sorted against.
  6199. (if plain-list-p
  6200. (lambda nil
  6201. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6202. (cond
  6203. ((= dcst ?n)
  6204. (string-to-number (buffer-substring (match-end 0)
  6205. (point-at-eol))))
  6206. ((= dcst ?a)
  6207. (buffer-substring (match-end 0) (point-at-eol)))
  6208. ((= dcst ?t)
  6209. (if (re-search-forward org-ts-regexp
  6210. (point-at-eol) t)
  6211. (org-time-string-to-time (match-string 0))
  6212. now))
  6213. ((= dcst ?f)
  6214. (if getkey-func
  6215. (progn
  6216. (setq tmp (funcall getkey-func))
  6217. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6218. tmp)
  6219. (error "Invalid key function `%s'" getkey-func)))
  6220. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6221. (lambda nil
  6222. (cond
  6223. ((= dcst ?n)
  6224. (if (looking-at outline-regexp)
  6225. (string-to-number (buffer-substring (match-end 0)
  6226. (point-at-eol)))
  6227. nil))
  6228. ((= dcst ?a)
  6229. (funcall case-func (buffer-substring (point-at-bol)
  6230. (point-at-eol))))
  6231. ((= dcst ?t)
  6232. (if (re-search-forward org-ts-regexp
  6233. (save-excursion
  6234. (forward-line 2)
  6235. (point)) t)
  6236. (org-time-string-to-time (match-string 0))
  6237. now))
  6238. ((= dcst ?p)
  6239. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6240. (string-to-char (match-string 2))
  6241. org-default-priority))
  6242. ((= dcst ?r)
  6243. (or (org-entry-get nil property) ""))
  6244. ((= dcst ?f)
  6245. (if getkey-func
  6246. (progn
  6247. (setq tmp (funcall getkey-func))
  6248. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6249. tmp)
  6250. (error "Invalid key function `%s'" getkey-func)))
  6251. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6252. nil
  6253. (cond
  6254. ((= dcst ?a) 'string<)
  6255. ((= dcst ?t) 'time-less-p)
  6256. (t nil)))))
  6257. (message "Sorting entries...done")))
  6258. (defun org-do-sort (table what &optional with-case sorting-type)
  6259. "Sort TABLE of WHAT according to SORTING-TYPE.
  6260. The user will be prompted for the SORTING-TYPE if the call to this
  6261. function does not specify it. WHAT is only for the prompt, to indicate
  6262. what is being sorted. The sorting key will be extracted from
  6263. the car of the elements of the table.
  6264. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6265. (unless sorting-type
  6266. (message
  6267. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6268. what)
  6269. (setq sorting-type (read-char-exclusive)))
  6270. (let ((dcst (downcase sorting-type))
  6271. extractfun comparefun)
  6272. ;; Define the appropriate functions
  6273. (cond
  6274. ((= dcst ?n)
  6275. (setq extractfun 'string-to-number
  6276. comparefun (if (= dcst sorting-type) '< '>)))
  6277. ((= dcst ?a)
  6278. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6279. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6280. comparefun (if (= dcst sorting-type)
  6281. 'string<
  6282. (lambda (a b) (and (not (string< a b))
  6283. (not (string= a b)))))))
  6284. ((= dcst ?t)
  6285. (setq extractfun
  6286. (lambda (x)
  6287. (if (string-match org-ts-regexp x)
  6288. (time-to-seconds
  6289. (org-time-string-to-time (match-string 0 x)))
  6290. 0))
  6291. comparefun (if (= dcst sorting-type) '< '>)))
  6292. (t (error "Invalid sorting type `%c'" sorting-type)))
  6293. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6294. table)
  6295. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6296. ;;;; Plain list items, including checkboxes
  6297. ;;; Plain list items
  6298. (defun org-at-item-p ()
  6299. "Is point in a line starting a hand-formatted item?"
  6300. (let ((llt org-plain-list-ordered-item-terminator))
  6301. (save-excursion
  6302. (goto-char (point-at-bol))
  6303. (looking-at
  6304. (cond
  6305. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6306. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6307. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6308. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6309. (defun org-in-item-p ()
  6310. "It the cursor inside a plain list item.
  6311. Does not have to be the first line."
  6312. (save-excursion
  6313. (condition-case nil
  6314. (progn
  6315. (org-beginning-of-item)
  6316. (org-at-item-p)
  6317. t)
  6318. (error nil))))
  6319. (defun org-insert-item (&optional checkbox)
  6320. "Insert a new item at the current level.
  6321. Return t when things worked, nil when we are not in an item."
  6322. (when (save-excursion
  6323. (condition-case nil
  6324. (progn
  6325. (org-beginning-of-item)
  6326. (org-at-item-p)
  6327. (if (org-invisible-p) (error "Invisible item"))
  6328. t)
  6329. (error nil)))
  6330. (let* ((bul (match-string 0))
  6331. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6332. (match-end 0)))
  6333. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6334. pos)
  6335. (cond
  6336. ((and (org-at-item-p) (<= (point) eow))
  6337. ;; before the bullet
  6338. (beginning-of-line 1)
  6339. (open-line (if blank 2 1)))
  6340. ((<= (point) eow)
  6341. (beginning-of-line 1))
  6342. (t
  6343. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  6344. (end-of-line 1)
  6345. (delete-horizontal-space))
  6346. (newline (if blank 2 1))))
  6347. (insert bul (if checkbox "[ ]" ""))
  6348. (just-one-space)
  6349. (setq pos (point))
  6350. (end-of-line 1)
  6351. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6352. (org-maybe-renumber-ordered-list)
  6353. (and checkbox (org-update-checkbox-count-maybe))
  6354. t))
  6355. ;;; Checkboxes
  6356. (defun org-at-item-checkbox-p ()
  6357. "Is point at a line starting a plain-list item with a checklet?"
  6358. (and (org-at-item-p)
  6359. (save-excursion
  6360. (goto-char (match-end 0))
  6361. (skip-chars-forward " \t")
  6362. (looking-at "\\[[- X]\\]"))))
  6363. (defun org-toggle-checkbox (&optional arg)
  6364. "Toggle the checkbox in the current line."
  6365. (interactive "P")
  6366. (catch 'exit
  6367. (let (beg end status (firstnew 'unknown))
  6368. (cond
  6369. ((org-region-active-p)
  6370. (setq beg (region-beginning) end (region-end)))
  6371. ((org-on-heading-p)
  6372. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6373. ((org-at-item-checkbox-p)
  6374. (let ((pos (point)))
  6375. (replace-match
  6376. (cond (arg "[-]")
  6377. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6378. (t "[ ]"))
  6379. t t)
  6380. (goto-char pos))
  6381. (throw 'exit t))
  6382. (t (error "Not at a checkbox or heading, and no active region")))
  6383. (save-excursion
  6384. (goto-char beg)
  6385. (while (< (point) end)
  6386. (when (org-at-item-checkbox-p)
  6387. (setq status (equal (match-string 0) "[X]"))
  6388. (when (eq firstnew 'unknown)
  6389. (setq firstnew (not status)))
  6390. (replace-match
  6391. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6392. (beginning-of-line 2)))))
  6393. (org-update-checkbox-count-maybe))
  6394. (defun org-update-checkbox-count-maybe ()
  6395. "Update checkbox statistics unless turned off by user."
  6396. (when org-provide-checkbox-statistics
  6397. (org-update-checkbox-count)))
  6398. (defun org-update-checkbox-count (&optional all)
  6399. "Update the checkbox statistics in the current section.
  6400. This will find all statistic cookies like [57%] and [6/12] and update them
  6401. with the current numbers. With optional prefix argument ALL, do this for
  6402. the whole buffer."
  6403. (interactive "P")
  6404. (save-excursion
  6405. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6406. (beg (condition-case nil
  6407. (progn (outline-back-to-heading) (point))
  6408. (error (point-min))))
  6409. (end (move-marker (make-marker)
  6410. (progn (outline-next-heading) (point))))
  6411. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  6412. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6413. (re-find (concat re "\\|" re-box))
  6414. beg-cookie end-cookie is-percent c-on c-off lim
  6415. eline curr-ind next-ind continue-from startsearch
  6416. (cstat 0)
  6417. )
  6418. (when all
  6419. (goto-char (point-min))
  6420. (outline-next-heading)
  6421. (setq beg (point) end (point-max)))
  6422. (goto-char end)
  6423. ;; find each statistic cookie
  6424. (while (re-search-backward re-find beg t)
  6425. (setq beg-cookie (match-beginning 1)
  6426. end-cookie (match-end 1)
  6427. cstat (+ cstat (if end-cookie 1 0))
  6428. startsearch (point-at-eol)
  6429. continue-from (point-at-bol)
  6430. is-percent (match-beginning 2)
  6431. lim (cond
  6432. ((org-on-heading-p) (outline-next-heading) (point))
  6433. ((org-at-item-p) (org-end-of-item) (point))
  6434. (t nil))
  6435. c-on 0
  6436. c-off 0)
  6437. (when lim
  6438. ;; find first checkbox for this cookie and gather
  6439. ;; statistics from all that are at this indentation level
  6440. (goto-char startsearch)
  6441. (if (re-search-forward re-box lim t)
  6442. (progn
  6443. (org-beginning-of-item)
  6444. (setq curr-ind (org-get-indentation))
  6445. (setq next-ind curr-ind)
  6446. (while (= curr-ind next-ind)
  6447. (save-excursion (end-of-line) (setq eline (point)))
  6448. (if (re-search-forward re-box eline t)
  6449. (if (member (match-string 2) '("[ ]" "[-]"))
  6450. (setq c-off (1+ c-off))
  6451. (setq c-on (1+ c-on))
  6452. )
  6453. )
  6454. (org-end-of-item)
  6455. (setq next-ind (org-get-indentation))
  6456. )))
  6457. (goto-char continue-from)
  6458. ;; update cookie
  6459. (when end-cookie
  6460. (delete-region beg-cookie end-cookie)
  6461. (goto-char beg-cookie)
  6462. (insert
  6463. (if is-percent
  6464. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6465. (format "[%d/%d]" c-on (+ c-on c-off)))))
  6466. ;; update items checkbox if it has one
  6467. (when (org-at-item-p)
  6468. (org-beginning-of-item)
  6469. (when (and (> (+ c-on c-off) 0)
  6470. (re-search-forward re-box (point-at-eol) t))
  6471. (setq beg-cookie (match-beginning 2)
  6472. end-cookie (match-end 2))
  6473. (delete-region beg-cookie end-cookie)
  6474. (goto-char beg-cookie)
  6475. (cond ((= c-off 0) (insert "[X]"))
  6476. ((= c-on 0) (insert "[ ]"))
  6477. (t (insert "[-]")))
  6478. )))
  6479. (goto-char continue-from))
  6480. (when (interactive-p)
  6481. (message "Checkbox satistics updated %s (%d places)"
  6482. (if all "in entire file" "in current outline entry") cstat)))))
  6483. (defun org-get-checkbox-statistics-face ()
  6484. "Select the face for checkbox statistics.
  6485. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6486. it will be `org-todo'."
  6487. (if (match-end 1)
  6488. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6489. (if (and (> (match-end 2) (match-beginning 2))
  6490. (equal (match-string 2) (match-string 3)))
  6491. 'org-done
  6492. 'org-todo)))
  6493. (defun org-get-indentation (&optional line)
  6494. "Get the indentation of the current line, interpreting tabs.
  6495. When LINE is given, assume it represents a line and compute its indentation."
  6496. (if line
  6497. (if (string-match "^ *" (org-remove-tabs line))
  6498. (match-end 0))
  6499. (save-excursion
  6500. (beginning-of-line 1)
  6501. (skip-chars-forward " \t")
  6502. (current-column))))
  6503. (defun org-remove-tabs (s &optional width)
  6504. "Replace tabulators in S with spaces.
  6505. Assumes that s is a single line, starting in column 0."
  6506. (setq width (or width tab-width))
  6507. (while (string-match "\t" s)
  6508. (setq s (replace-match
  6509. (make-string
  6510. (- (* width (/ (+ (match-beginning 0) width) width))
  6511. (match-beginning 0)) ?\ )
  6512. t t s)))
  6513. s)
  6514. (defun org-fix-indentation (line ind)
  6515. "Fix indentation in LINE.
  6516. IND is a cons cell with target and minimum indentation.
  6517. If the current indenation in LINE is smaller than the minimum,
  6518. leave it alone. If it is larger than ind, set it to the target."
  6519. (let* ((l (org-remove-tabs line))
  6520. (i (org-get-indentation l))
  6521. (i1 (car ind)) (i2 (cdr ind)))
  6522. (if (>= i i2) (setq l (substring line i2)))
  6523. (if (> i1 0)
  6524. (concat (make-string i1 ?\ ) l)
  6525. l)))
  6526. (defcustom org-empty-line-terminates-plain-lists nil
  6527. "Non-nil means, an empty line ends all plain list levels.
  6528. When nil, empty lines are part of the preceeding item."
  6529. :group 'org-plain-lists
  6530. :type 'boolean)
  6531. (defun org-beginning-of-item ()
  6532. "Go to the beginning of the current hand-formatted item.
  6533. If the cursor is not in an item, throw an error."
  6534. (interactive)
  6535. (let ((pos (point))
  6536. (limit (save-excursion
  6537. (condition-case nil
  6538. (progn
  6539. (org-back-to-heading)
  6540. (beginning-of-line 2) (point))
  6541. (error (point-min)))))
  6542. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6543. ind ind1)
  6544. (if (org-at-item-p)
  6545. (beginning-of-line 1)
  6546. (beginning-of-line 1)
  6547. (skip-chars-forward " \t")
  6548. (setq ind (current-column))
  6549. (if (catch 'exit
  6550. (while t
  6551. (beginning-of-line 0)
  6552. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6553. (if (looking-at "[ \t]*$")
  6554. (setq ind1 ind-empty)
  6555. (skip-chars-forward " \t")
  6556. (setq ind1 (current-column)))
  6557. (if (< ind1 ind)
  6558. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6559. nil
  6560. (goto-char pos)
  6561. (error "Not in an item")))))
  6562. (defun org-end-of-item ()
  6563. "Go to the end of the current hand-formatted item.
  6564. If the cursor is not in an item, throw an error."
  6565. (interactive)
  6566. (let* ((pos (point))
  6567. ind1
  6568. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6569. (limit (save-excursion (outline-next-heading) (point)))
  6570. (ind (save-excursion
  6571. (org-beginning-of-item)
  6572. (skip-chars-forward " \t")
  6573. (current-column)))
  6574. (end (catch 'exit
  6575. (while t
  6576. (beginning-of-line 2)
  6577. (if (eobp) (throw 'exit (point)))
  6578. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6579. (if (looking-at "[ \t]*$")
  6580. (setq ind1 ind-empty)
  6581. (skip-chars-forward " \t")
  6582. (setq ind1 (current-column)))
  6583. (if (<= ind1 ind)
  6584. (throw 'exit (point-at-bol)))))))
  6585. (if end
  6586. (goto-char end)
  6587. (goto-char pos)
  6588. (error "Not in an item"))))
  6589. (defun org-next-item ()
  6590. "Move to the beginning of the next item in the current plain list.
  6591. Error if not at a plain list, or if this is the last item in the list."
  6592. (interactive)
  6593. (let (ind ind1 (pos (point)))
  6594. (org-beginning-of-item)
  6595. (setq ind (org-get-indentation))
  6596. (org-end-of-item)
  6597. (setq ind1 (org-get-indentation))
  6598. (unless (and (org-at-item-p) (= ind ind1))
  6599. (goto-char pos)
  6600. (error "On last item"))))
  6601. (defun org-previous-item ()
  6602. "Move to the beginning of the previous item in the current plain list.
  6603. Error if not at a plain list, or if this is the first item in the list."
  6604. (interactive)
  6605. (let (beg ind ind1 (pos (point)))
  6606. (org-beginning-of-item)
  6607. (setq beg (point))
  6608. (setq ind (org-get-indentation))
  6609. (goto-char beg)
  6610. (catch 'exit
  6611. (while t
  6612. (beginning-of-line 0)
  6613. (if (looking-at "[ \t]*$")
  6614. nil
  6615. (if (<= (setq ind1 (org-get-indentation)) ind)
  6616. (throw 'exit t)))))
  6617. (condition-case nil
  6618. (if (or (not (org-at-item-p))
  6619. (< ind1 (1- ind)))
  6620. (error "")
  6621. (org-beginning-of-item))
  6622. (error (goto-char pos)
  6623. (error "On first item")))))
  6624. (defun org-first-list-item-p ()
  6625. "Is this heading the item in a plain list?"
  6626. (unless (org-at-item-p)
  6627. (error "Not at a plain list item"))
  6628. (org-beginning-of-item)
  6629. (= (point) (save-excursion (org-beginning-of-item-list))))
  6630. (defun org-move-item-down ()
  6631. "Move the plain list item at point down, i.e. swap with following item.
  6632. Subitems (items with larger indentation) are considered part of the item,
  6633. so this really moves item trees."
  6634. (interactive)
  6635. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6636. (org-beginning-of-item)
  6637. (setq beg0 (point))
  6638. (save-excursion
  6639. (setq ne-beg (org-back-over-empty-lines))
  6640. (setq beg (point)))
  6641. (goto-char beg0)
  6642. (setq ind (org-get-indentation))
  6643. (org-end-of-item)
  6644. (setq end0 (point))
  6645. (setq ind1 (org-get-indentation))
  6646. (setq ne-end (org-back-over-empty-lines))
  6647. (setq end (point))
  6648. (goto-char beg0)
  6649. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6650. ;; include less whitespace
  6651. (save-excursion
  6652. (goto-char beg)
  6653. (forward-line (- ne-beg ne-end))
  6654. (setq beg (point))))
  6655. (goto-char end0)
  6656. (if (and (org-at-item-p) (= ind ind1))
  6657. (progn
  6658. (org-end-of-item)
  6659. (org-back-over-empty-lines)
  6660. (setq txt (buffer-substring beg end))
  6661. (save-excursion
  6662. (delete-region beg end))
  6663. (setq pos (point))
  6664. (insert txt)
  6665. (goto-char pos) (org-skip-whitespace)
  6666. (org-maybe-renumber-ordered-list))
  6667. (goto-char pos)
  6668. (error "Cannot move this item further down"))))
  6669. (defun org-move-item-up (arg)
  6670. "Move the plain list item at point up, i.e. swap with previous item.
  6671. Subitems (items with larger indentation) are considered part of the item,
  6672. so this really moves item trees."
  6673. (interactive "p")
  6674. (let (beg beg0 end ind ind1 (pos (point)) txt
  6675. ne-beg ne-ins ins-end)
  6676. (org-beginning-of-item)
  6677. (setq beg0 (point))
  6678. (setq ind (org-get-indentation))
  6679. (save-excursion
  6680. (setq ne-beg (org-back-over-empty-lines))
  6681. (setq beg (point)))
  6682. (goto-char beg0)
  6683. (org-end-of-item)
  6684. (setq end (point))
  6685. (goto-char beg0)
  6686. (catch 'exit
  6687. (while t
  6688. (beginning-of-line 0)
  6689. (if (looking-at "[ \t]*$")
  6690. (if org-empty-line-terminates-plain-lists
  6691. (progn
  6692. (goto-char pos)
  6693. (error "Cannot move this item further up"))
  6694. nil)
  6695. (if (<= (setq ind1 (org-get-indentation)) ind)
  6696. (throw 'exit t)))))
  6697. (condition-case nil
  6698. (org-beginning-of-item)
  6699. (error (goto-char beg)
  6700. (error "Cannot move this item further up")))
  6701. (setq ind1 (org-get-indentation))
  6702. (if (and (org-at-item-p) (= ind ind1))
  6703. (progn
  6704. (setq ne-ins (org-back-over-empty-lines))
  6705. (setq txt (buffer-substring beg end))
  6706. (save-excursion
  6707. (delete-region beg end))
  6708. (setq pos (point))
  6709. (insert txt)
  6710. (setq ins-end (point))
  6711. (goto-char pos) (org-skip-whitespace)
  6712. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6713. ;; Move whitespace back to beginning
  6714. (save-excursion
  6715. (goto-char ins-end)
  6716. (let ((kill-whole-line t))
  6717. (kill-line (- ne-ins ne-beg)) (point)))
  6718. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6719. (org-maybe-renumber-ordered-list))
  6720. (goto-char pos)
  6721. (error "Cannot move this item further up"))))
  6722. (defun org-maybe-renumber-ordered-list ()
  6723. "Renumber the ordered list at point if setup allows it.
  6724. This tests the user option `org-auto-renumber-ordered-lists' before
  6725. doing the renumbering."
  6726. (interactive)
  6727. (when (and org-auto-renumber-ordered-lists
  6728. (org-at-item-p))
  6729. (if (match-beginning 3)
  6730. (org-renumber-ordered-list 1)
  6731. (org-fix-bullet-type))))
  6732. (defun org-maybe-renumber-ordered-list-safe ()
  6733. (condition-case nil
  6734. (save-excursion
  6735. (org-maybe-renumber-ordered-list))
  6736. (error nil)))
  6737. (defun org-cycle-list-bullet (&optional which)
  6738. "Cycle through the different itemize/enumerate bullets.
  6739. This cycle the entire list level through the sequence:
  6740. `-' -> `+' -> `*' -> `1.' -> `1)'
  6741. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6742. 0 meand `-', 1 means `+' etc."
  6743. (interactive "P")
  6744. (org-preserve-lc
  6745. (org-beginning-of-item-list)
  6746. (org-at-item-p)
  6747. (beginning-of-line 1)
  6748. (let ((current (match-string 0))
  6749. (prevp (eq which 'previous))
  6750. new)
  6751. (setq new (cond
  6752. ((and (numberp which)
  6753. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6754. ((string-match "-" current) (if prevp "1)" "+"))
  6755. ((string-match "\\+" current)
  6756. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6757. ((string-match "\\*" current) (if prevp "+" "1."))
  6758. ((string-match "\\." current) (if prevp "*" "1)"))
  6759. ((string-match ")" current) (if prevp "1." "-"))
  6760. (t (error "This should not happen"))))
  6761. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6762. (org-fix-bullet-type)
  6763. (org-maybe-renumber-ordered-list))))
  6764. (defun org-get-string-indentation (s)
  6765. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6766. (let ((n -1) (i 0) (w tab-width) c)
  6767. (catch 'exit
  6768. (while (< (setq n (1+ n)) (length s))
  6769. (setq c (aref s n))
  6770. (cond ((= c ?\ ) (setq i (1+ i)))
  6771. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6772. (t (throw 'exit t)))))
  6773. i))
  6774. (defun org-renumber-ordered-list (arg)
  6775. "Renumber an ordered plain list.
  6776. Cursor needs to be in the first line of an item, the line that starts
  6777. with something like \"1.\" or \"2)\"."
  6778. (interactive "p")
  6779. (unless (and (org-at-item-p)
  6780. (match-beginning 3))
  6781. (error "This is not an ordered list"))
  6782. (let ((line (org-current-line))
  6783. (col (current-column))
  6784. (ind (org-get-string-indentation
  6785. (buffer-substring (point-at-bol) (match-beginning 3))))
  6786. ;; (term (substring (match-string 3) -1))
  6787. ind1 (n (1- arg))
  6788. fmt)
  6789. ;; find where this list begins
  6790. (org-beginning-of-item-list)
  6791. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6792. (setq fmt (concat "%d" (match-string 1)))
  6793. (beginning-of-line 0)
  6794. ;; walk forward and replace these numbers
  6795. (catch 'exit
  6796. (while t
  6797. (catch 'next
  6798. (beginning-of-line 2)
  6799. (if (eobp) (throw 'exit nil))
  6800. (if (looking-at "[ \t]*$") (throw 'next nil))
  6801. (skip-chars-forward " \t") (setq ind1 (current-column))
  6802. (if (> ind1 ind) (throw 'next t))
  6803. (if (< ind1 ind) (throw 'exit t))
  6804. (if (not (org-at-item-p)) (throw 'exit nil))
  6805. (delete-region (match-beginning 2) (match-end 2))
  6806. (goto-char (match-beginning 2))
  6807. (insert (format fmt (setq n (1+ n)))))))
  6808. (goto-line line)
  6809. (move-to-column col)))
  6810. (defun org-fix-bullet-type ()
  6811. "Make sure all items in this list have the same bullet as the firsst item."
  6812. (interactive)
  6813. (unless (org-at-item-p) (error "This is not a list"))
  6814. (let ((line (org-current-line))
  6815. (col (current-column))
  6816. (ind (current-indentation))
  6817. ind1 bullet)
  6818. ;; find where this list begins
  6819. (org-beginning-of-item-list)
  6820. (beginning-of-line 1)
  6821. ;; find out what the bullet type is
  6822. (looking-at "[ \t]*\\(\\S-+\\)")
  6823. (setq bullet (match-string 1))
  6824. ;; walk forward and replace these numbers
  6825. (beginning-of-line 0)
  6826. (catch 'exit
  6827. (while t
  6828. (catch 'next
  6829. (beginning-of-line 2)
  6830. (if (eobp) (throw 'exit nil))
  6831. (if (looking-at "[ \t]*$") (throw 'next nil))
  6832. (skip-chars-forward " \t") (setq ind1 (current-column))
  6833. (if (> ind1 ind) (throw 'next t))
  6834. (if (< ind1 ind) (throw 'exit t))
  6835. (if (not (org-at-item-p)) (throw 'exit nil))
  6836. (skip-chars-forward " \t")
  6837. (looking-at "\\S-+")
  6838. (replace-match bullet))))
  6839. (goto-line line)
  6840. (move-to-column col)
  6841. (if (string-match "[0-9]" bullet)
  6842. (org-renumber-ordered-list 1))))
  6843. (defun org-beginning-of-item-list ()
  6844. "Go to the beginning of the current item list.
  6845. I.e. to the first item in this list."
  6846. (interactive)
  6847. (org-beginning-of-item)
  6848. (let ((pos (point-at-bol))
  6849. (ind (org-get-indentation))
  6850. ind1)
  6851. ;; find where this list begins
  6852. (catch 'exit
  6853. (while t
  6854. (catch 'next
  6855. (beginning-of-line 0)
  6856. (if (looking-at "[ \t]*$")
  6857. (throw (if (bobp) 'exit 'next) t))
  6858. (skip-chars-forward " \t") (setq ind1 (current-column))
  6859. (if (or (< ind1 ind)
  6860. (and (= ind1 ind)
  6861. (not (org-at-item-p)))
  6862. (bobp))
  6863. (throw 'exit t)
  6864. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6865. (goto-char pos)))
  6866. (defun org-end-of-item-list ()
  6867. "Go to the end of the current item list.
  6868. I.e. to the text after the last item."
  6869. (interactive)
  6870. (org-beginning-of-item)
  6871. (let ((pos (point-at-bol))
  6872. (ind (org-get-indentation))
  6873. ind1)
  6874. ;; find where this list begins
  6875. (catch 'exit
  6876. (while t
  6877. (catch 'next
  6878. (beginning-of-line 2)
  6879. (if (looking-at "[ \t]*$")
  6880. (throw (if (eobp) 'exit 'next) t))
  6881. (skip-chars-forward " \t") (setq ind1 (current-column))
  6882. (if (or (< ind1 ind)
  6883. (and (= ind1 ind)
  6884. (not (org-at-item-p)))
  6885. (eobp))
  6886. (progn
  6887. (setq pos (point-at-bol))
  6888. (throw 'exit t))))))
  6889. (goto-char pos)))
  6890. (defvar org-last-indent-begin-marker (make-marker))
  6891. (defvar org-last-indent-end-marker (make-marker))
  6892. (defun org-outdent-item (arg)
  6893. "Outdent a local list item."
  6894. (interactive "p")
  6895. (org-indent-item (- arg)))
  6896. (defun org-indent-item (arg)
  6897. "Indent a local list item."
  6898. (interactive "p")
  6899. (unless (org-at-item-p)
  6900. (error "Not on an item"))
  6901. (save-excursion
  6902. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6903. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6904. (setq beg org-last-indent-begin-marker
  6905. end org-last-indent-end-marker)
  6906. (org-beginning-of-item)
  6907. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6908. (org-end-of-item)
  6909. (setq end (move-marker org-last-indent-end-marker (point))))
  6910. (goto-char beg)
  6911. (setq tmp (org-item-indent-positions)
  6912. ind (car tmp)
  6913. ind-down (nth 2 tmp)
  6914. ind-up (nth 1 tmp)
  6915. delta (if (> arg 0)
  6916. (if ind-down (- ind-down ind) 2)
  6917. (if ind-up (- ind-up ind) -2)))
  6918. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6919. (while (< (point) end)
  6920. (beginning-of-line 1)
  6921. (skip-chars-forward " \t") (setq ind1 (current-column))
  6922. (delete-region (point-at-bol) (point))
  6923. (or (eolp) (indent-to-column (+ ind1 delta)))
  6924. (beginning-of-line 2))))
  6925. (org-fix-bullet-type)
  6926. (org-maybe-renumber-ordered-list-safe)
  6927. (save-excursion
  6928. (beginning-of-line 0)
  6929. (condition-case nil (org-beginning-of-item) (error nil))
  6930. (org-maybe-renumber-ordered-list-safe)))
  6931. (defun org-item-indent-positions ()
  6932. "Return indentation for plain list items.
  6933. This returns a list with three values: The current indentation, the
  6934. parent indentation and the indentation a child should habe.
  6935. Assumes cursor in item line."
  6936. (let* ((bolpos (point-at-bol))
  6937. (ind (org-get-indentation))
  6938. ind-down ind-up pos)
  6939. (save-excursion
  6940. (org-beginning-of-item-list)
  6941. (skip-chars-backward "\n\r \t")
  6942. (when (org-in-item-p)
  6943. (org-beginning-of-item)
  6944. (setq ind-up (org-get-indentation))))
  6945. (setq pos (point))
  6946. (save-excursion
  6947. (cond
  6948. ((and (condition-case nil (progn (org-previous-item) t)
  6949. (error nil))
  6950. (or (forward-char 1) t)
  6951. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6952. (setq ind-down (org-get-indentation)))
  6953. ((and (goto-char pos)
  6954. (org-at-item-p))
  6955. (goto-char (match-end 0))
  6956. (skip-chars-forward " \t")
  6957. (setq ind-down (current-column)))))
  6958. (list ind ind-up ind-down)))
  6959. ;;; The orgstruct minor mode
  6960. ;; Define a minor mode which can be used in other modes in order to
  6961. ;; integrate the org-mode structure editing commands.
  6962. ;; This is really a hack, because the org-mode structure commands use
  6963. ;; keys which normally belong to the major mode. Here is how it
  6964. ;; works: The minor mode defines all the keys necessary to operate the
  6965. ;; structure commands, but wraps the commands into a function which
  6966. ;; tests if the cursor is currently at a headline or a plain list
  6967. ;; item. If that is the case, the structure command is used,
  6968. ;; temporarily setting many Org-mode variables like regular
  6969. ;; expressions for filling etc. However, when any of those keys is
  6970. ;; used at a different location, function uses `key-binding' to look
  6971. ;; up if the key has an associated command in another currently active
  6972. ;; keymap (minor modes, major mode, global), and executes that
  6973. ;; command. There might be problems if any of the keys is otherwise
  6974. ;; used as a prefix key.
  6975. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6976. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6977. ;; addresses this by checking explicitly for both bindings.
  6978. (defvar orgstruct-mode-map (make-sparse-keymap)
  6979. "Keymap for the minor `orgstruct-mode'.")
  6980. (defvar org-local-vars nil
  6981. "List of local variables, for use by `orgstruct-mode'")
  6982. ;;;###autoload
  6983. (define-minor-mode orgstruct-mode
  6984. "Toggle the minor more `orgstruct-mode'.
  6985. This mode is for using Org-mode structure commands in other modes.
  6986. The following key behave as if Org-mode was active, if the cursor
  6987. is on a headline, or on a plain list item (both in the definition
  6988. of Org-mode).
  6989. M-up Move entry/item up
  6990. M-down Move entry/item down
  6991. M-left Promote
  6992. M-right Demote
  6993. M-S-up Move entry/item up
  6994. M-S-down Move entry/item down
  6995. M-S-left Promote subtree
  6996. M-S-right Demote subtree
  6997. M-q Fill paragraph and items like in Org-mode
  6998. C-c ^ Sort entries
  6999. C-c - Cycle list bullet
  7000. TAB Cycle item visibility
  7001. M-RET Insert new heading/item
  7002. S-M-RET Insert new TODO heading / Chekbox item
  7003. C-c C-c Set tags / toggle checkbox"
  7004. nil " OrgStruct" nil
  7005. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7006. ;;;###autoload
  7007. (defun turn-on-orgstruct ()
  7008. "Unconditionally turn on `orgstruct-mode'."
  7009. (orgstruct-mode 1))
  7010. ;;;###autoload
  7011. (defun turn-on-orgstruct++ ()
  7012. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  7013. In addition to setting orgstruct-mode, this also exports all indentation and
  7014. autofilling variables from org-mode into the buffer. Note that turning
  7015. off orgstruct-mode will *not* remove these additional settings."
  7016. (orgstruct-mode 1)
  7017. (let (var val)
  7018. (mapc
  7019. (lambda (x)
  7020. (when (string-match
  7021. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7022. (symbol-name (car x)))
  7023. (setq var (car x) val (nth 1 x))
  7024. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7025. org-local-vars)))
  7026. (defun orgstruct-error ()
  7027. "Error when there is no default binding for a structure key."
  7028. (interactive)
  7029. (error "This key has no function outside structure elements"))
  7030. (defun orgstruct-setup ()
  7031. "Setup orgstruct keymaps."
  7032. (let ((nfunc 0)
  7033. (bindings
  7034. (list
  7035. '([(meta up)] org-metaup)
  7036. '([(meta down)] org-metadown)
  7037. '([(meta left)] org-metaleft)
  7038. '([(meta right)] org-metaright)
  7039. '([(meta shift up)] org-shiftmetaup)
  7040. '([(meta shift down)] org-shiftmetadown)
  7041. '([(meta shift left)] org-shiftmetaleft)
  7042. '([(meta shift right)] org-shiftmetaright)
  7043. '([(shift up)] org-shiftup)
  7044. '([(shift down)] org-shiftdown)
  7045. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7046. '("\M-q" fill-paragraph)
  7047. '("\C-c^" org-sort)
  7048. '("\C-c-" org-cycle-list-bullet)))
  7049. elt key fun cmd)
  7050. (while (setq elt (pop bindings))
  7051. (setq nfunc (1+ nfunc))
  7052. (setq key (org-key (car elt))
  7053. fun (nth 1 elt)
  7054. cmd (orgstruct-make-binding fun nfunc key))
  7055. (org-defkey orgstruct-mode-map key cmd))
  7056. ;; Special treatment needed for TAB and RET
  7057. (org-defkey orgstruct-mode-map [(tab)]
  7058. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7059. (org-defkey orgstruct-mode-map "\C-i"
  7060. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7061. (org-defkey orgstruct-mode-map "\M-\C-m"
  7062. (orgstruct-make-binding 'org-insert-heading 105
  7063. "\M-\C-m" [(meta return)]))
  7064. (org-defkey orgstruct-mode-map [(meta return)]
  7065. (orgstruct-make-binding 'org-insert-heading 106
  7066. [(meta return)] "\M-\C-m"))
  7067. (org-defkey orgstruct-mode-map [(shift meta return)]
  7068. (orgstruct-make-binding 'org-insert-todo-heading 107
  7069. [(meta return)] "\M-\C-m"))
  7070. (unless org-local-vars
  7071. (setq org-local-vars (org-get-local-variables)))
  7072. t))
  7073. (defun orgstruct-make-binding (fun n &rest keys)
  7074. "Create a function for binding in the structure minor mode.
  7075. FUN is the command to call inside a table. N is used to create a unique
  7076. command name. KEYS are keys that should be checked in for a command
  7077. to execute outside of tables."
  7078. (eval
  7079. (list 'defun
  7080. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7081. '(arg)
  7082. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7083. "Outside of structure, run the binding of `"
  7084. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7085. "'.")
  7086. '(interactive "p")
  7087. (list 'if
  7088. '(org-context-p 'headline 'item)
  7089. (list 'org-run-like-in-org-mode (list 'quote fun))
  7090. (list 'let '(orgstruct-mode)
  7091. (list 'call-interactively
  7092. (append '(or)
  7093. (mapcar (lambda (k)
  7094. (list 'key-binding k))
  7095. keys)
  7096. '('orgstruct-error))))))))
  7097. (defun org-context-p (&rest contexts)
  7098. "Check if local context is and of CONTEXTS.
  7099. Possible values in the list of contexts are `table', `headline', and `item'."
  7100. (let ((pos (point)))
  7101. (goto-char (point-at-bol))
  7102. (prog1 (or (and (memq 'table contexts)
  7103. (looking-at "[ \t]*|"))
  7104. (and (memq 'headline contexts)
  7105. (looking-at "\\*+"))
  7106. (and (memq 'item contexts)
  7107. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  7108. (goto-char pos))))
  7109. (defun org-get-local-variables ()
  7110. "Return a list of all local variables in an org-mode buffer."
  7111. (let (varlist)
  7112. (with-current-buffer (get-buffer-create "*Org tmp*")
  7113. (erase-buffer)
  7114. (org-mode)
  7115. (setq varlist (buffer-local-variables)))
  7116. (kill-buffer "*Org tmp*")
  7117. (delq nil
  7118. (mapcar
  7119. (lambda (x)
  7120. (setq x
  7121. (if (symbolp x)
  7122. (list x)
  7123. (list (car x) (list 'quote (cdr x)))))
  7124. (if (string-match
  7125. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7126. (symbol-name (car x)))
  7127. x nil))
  7128. varlist))))
  7129. ;;;###autoload
  7130. (defun org-run-like-in-org-mode (cmd)
  7131. (unless org-local-vars
  7132. (setq org-local-vars (org-get-local-variables)))
  7133. (eval (list 'let org-local-vars
  7134. (list 'call-interactively (list 'quote cmd)))))
  7135. ;;;; Archiving
  7136. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  7137. (defun org-archive-subtree (&optional find-done)
  7138. "Move the current subtree to the archive.
  7139. The archive can be a certain top-level heading in the current file, or in
  7140. a different file. The tree will be moved to that location, the subtree
  7141. heading be marked DONE, and the current time will be added.
  7142. When called with prefix argument FIND-DONE, find whole trees without any
  7143. open TODO items and archive them (after getting confirmation from the user).
  7144. If the cursor is not at a headline when this comand is called, try all level
  7145. 1 trees. If the cursor is on a headline, only try the direct children of
  7146. this heading."
  7147. (interactive "P")
  7148. (if find-done
  7149. (org-archive-all-done)
  7150. ;; Save all relevant TODO keyword-relatex variables
  7151. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  7152. (tr-org-todo-keywords-1 org-todo-keywords-1)
  7153. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  7154. (tr-org-done-keywords org-done-keywords)
  7155. (tr-org-todo-regexp org-todo-regexp)
  7156. (tr-org-todo-line-regexp org-todo-line-regexp)
  7157. (tr-org-odd-levels-only org-odd-levels-only)
  7158. (this-buffer (current-buffer))
  7159. (org-archive-location org-archive-location)
  7160. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  7161. ;; start of variables that will be used for saving context
  7162. ;; The compiler complains about them - keep them anyway!
  7163. (file (abbreviate-file-name (buffer-file-name)))
  7164. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  7165. (time (format-time-string
  7166. (substring (cdr org-time-stamp-formats) 1 -1)
  7167. (current-time)))
  7168. afile heading buffer level newfile-p
  7169. category todo priority
  7170. ;; start of variables that will be used for savind context
  7171. ltags itags prop)
  7172. ;; Try to find a local archive location
  7173. (save-excursion
  7174. (save-restriction
  7175. (widen)
  7176. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  7177. (if (and prop (string-match "\\S-" prop))
  7178. (setq org-archive-location prop)
  7179. (if (or (re-search-backward re nil t)
  7180. (re-search-forward re nil t))
  7181. (setq org-archive-location (match-string 1))))))
  7182. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  7183. (progn
  7184. (setq afile (format (match-string 1 org-archive-location)
  7185. (file-name-nondirectory buffer-file-name))
  7186. heading (match-string 2 org-archive-location)))
  7187. (error "Invalid `org-archive-location'"))
  7188. (if (> (length afile) 0)
  7189. (setq newfile-p (not (file-exists-p afile))
  7190. buffer (find-file-noselect afile))
  7191. (setq buffer (current-buffer)))
  7192. (unless buffer
  7193. (error "Cannot access file \"%s\"" afile))
  7194. (if (and (> (length heading) 0)
  7195. (string-match "^\\*+" heading))
  7196. (setq level (match-end 0))
  7197. (setq heading nil level 0))
  7198. (save-excursion
  7199. (org-back-to-heading t)
  7200. ;; Get context information that will be lost by moving the tree
  7201. (org-refresh-category-properties)
  7202. (setq category (org-get-category)
  7203. todo (and (looking-at org-todo-line-regexp)
  7204. (match-string 2))
  7205. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7206. ltags (org-get-tags)
  7207. itags (org-delete-all ltags (org-get-tags-at)))
  7208. (setq ltags (mapconcat 'identity ltags " ")
  7209. itags (mapconcat 'identity itags " "))
  7210. ;; We first only copy, in case something goes wrong
  7211. ;; we need to protect this-command, to avoid kill-region sets it,
  7212. ;; which would lead to duplication of subtrees
  7213. (let (this-command) (org-copy-subtree))
  7214. (set-buffer buffer)
  7215. ;; Enforce org-mode for the archive buffer
  7216. (if (not (org-mode-p))
  7217. ;; Force the mode for future visits.
  7218. (let ((org-insert-mode-line-in-empty-file t)
  7219. (org-inhibit-startup t))
  7220. (call-interactively 'org-mode)))
  7221. (when newfile-p
  7222. (goto-char (point-max))
  7223. (insert (format "\nArchived entries from file %s\n\n"
  7224. (buffer-file-name this-buffer))))
  7225. ;; Force the TODO keywords of the original buffer
  7226. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7227. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7228. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7229. (org-done-keywords tr-org-done-keywords)
  7230. (org-todo-regexp tr-org-todo-regexp)
  7231. (org-todo-line-regexp tr-org-todo-line-regexp)
  7232. (org-odd-levels-only
  7233. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7234. org-odd-levels-only
  7235. tr-org-odd-levels-only)))
  7236. (goto-char (point-min))
  7237. (if heading
  7238. (progn
  7239. (if (re-search-forward
  7240. (concat "^" (regexp-quote heading)
  7241. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7242. nil t)
  7243. (goto-char (match-end 0))
  7244. ;; Heading not found, just insert it at the end
  7245. (goto-char (point-max))
  7246. (or (bolp) (insert "\n"))
  7247. (insert "\n" heading "\n")
  7248. (end-of-line 0))
  7249. ;; Make the subtree visible
  7250. (show-subtree)
  7251. (org-end-of-subtree t)
  7252. (skip-chars-backward " \t\r\n")
  7253. (and (looking-at "[ \t\r\n]*")
  7254. (replace-match "\n\n")))
  7255. ;; No specific heading, just go to end of file.
  7256. (goto-char (point-max)) (insert "\n"))
  7257. ;; Paste
  7258. (org-paste-subtree (org-get-legal-level level 1))
  7259. ;; Mark the entry as done
  7260. (when (and org-archive-mark-done
  7261. (looking-at org-todo-line-regexp)
  7262. (or (not (match-end 2))
  7263. (not (member (match-string 2) org-done-keywords))))
  7264. (let (org-log-done org-todo-log-states)
  7265. (org-todo
  7266. (car (or (member org-archive-mark-done org-done-keywords)
  7267. org-done-keywords)))))
  7268. ;; Add the context info
  7269. (when org-archive-save-context-info
  7270. (let ((l org-archive-save-context-info) e n v)
  7271. (while (setq e (pop l))
  7272. (when (and (setq v (symbol-value e))
  7273. (stringp v) (string-match "\\S-" v))
  7274. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7275. (org-entry-put (point) n v)))))
  7276. ;; Save the buffer, if it is not the same buffer.
  7277. (if (not (eq this-buffer buffer)) (save-buffer))))
  7278. ;; Here we are back in the original buffer. Everything seems to have
  7279. ;; worked. So now cut the tree and finish up.
  7280. (let (this-command) (org-cut-subtree))
  7281. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7282. (message "Subtree archived %s"
  7283. (if (eq this-buffer buffer)
  7284. (concat "under heading: " heading)
  7285. (concat "in file: " (abbreviate-file-name afile)))))))
  7286. (defun org-refresh-category-properties ()
  7287. "Refresh category text properties in teh buffer."
  7288. (let ((def-cat (cond
  7289. ((null org-category)
  7290. (if buffer-file-name
  7291. (file-name-sans-extension
  7292. (file-name-nondirectory buffer-file-name))
  7293. "???"))
  7294. ((symbolp org-category) (symbol-name org-category))
  7295. (t org-category)))
  7296. beg end cat pos optionp)
  7297. (org-unmodified
  7298. (save-excursion
  7299. (save-restriction
  7300. (widen)
  7301. (goto-char (point-min))
  7302. (put-text-property (point) (point-max) 'org-category def-cat)
  7303. (while (re-search-forward
  7304. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7305. (setq pos (match-end 0)
  7306. optionp (equal (char-after (match-beginning 0)) ?#)
  7307. cat (org-trim (match-string 2)))
  7308. (if optionp
  7309. (setq beg (point-at-bol) end (point-max))
  7310. (org-back-to-heading t)
  7311. (setq beg (point) end (org-end-of-subtree t t)))
  7312. (put-text-property beg end 'org-category cat)
  7313. (goto-char pos)))))))
  7314. (defun org-archive-all-done (&optional tag)
  7315. "Archive sublevels of the current tree without open TODO items.
  7316. If the cursor is not on a headline, try all level 1 trees. If
  7317. it is on a headline, try all direct children.
  7318. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7319. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7320. (rea (concat ".*:" org-archive-tag ":"))
  7321. (begm (make-marker))
  7322. (endm (make-marker))
  7323. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7324. "Move subtree to archive (no open TODO items)? "))
  7325. beg end (cntarch 0))
  7326. (if (org-on-heading-p)
  7327. (progn
  7328. (setq re1 (concat "^" (regexp-quote
  7329. (make-string
  7330. (1+ (- (match-end 0) (match-beginning 0) 1))
  7331. ?*))
  7332. " "))
  7333. (move-marker begm (point))
  7334. (move-marker endm (org-end-of-subtree t)))
  7335. (setq re1 "^* ")
  7336. (move-marker begm (point-min))
  7337. (move-marker endm (point-max)))
  7338. (save-excursion
  7339. (goto-char begm)
  7340. (while (re-search-forward re1 endm t)
  7341. (setq beg (match-beginning 0)
  7342. end (save-excursion (org-end-of-subtree t) (point)))
  7343. (goto-char beg)
  7344. (if (re-search-forward re end t)
  7345. (goto-char end)
  7346. (goto-char beg)
  7347. (if (and (or (not tag) (not (looking-at rea)))
  7348. (y-or-n-p question))
  7349. (progn
  7350. (if tag
  7351. (org-toggle-tag org-archive-tag 'on)
  7352. (org-archive-subtree))
  7353. (setq cntarch (1+ cntarch)))
  7354. (goto-char end)))))
  7355. (message "%d trees archived" cntarch)))
  7356. (defun org-cycle-hide-drawers (state)
  7357. "Re-hide all drawers after a visibility state change."
  7358. (when (and (org-mode-p)
  7359. (not (memq state '(overview folded))))
  7360. (save-excursion
  7361. (let* ((globalp (memq state '(contents all)))
  7362. (beg (if globalp (point-min) (point)))
  7363. (end (if globalp (point-max) (org-end-of-subtree t))))
  7364. (goto-char beg)
  7365. (while (re-search-forward org-drawer-regexp end t)
  7366. (org-flag-drawer t))))))
  7367. (defun org-flag-drawer (flag)
  7368. (save-excursion
  7369. (beginning-of-line 1)
  7370. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7371. (let ((b (match-end 0))
  7372. (outline-regexp org-outline-regexp))
  7373. (if (re-search-forward
  7374. "^[ \t]*:END:"
  7375. (save-excursion (outline-next-heading) (point)) t)
  7376. (outline-flag-region b (point-at-eol) flag)
  7377. (error ":END: line missing"))))))
  7378. (defun org-cycle-hide-archived-subtrees (state)
  7379. "Re-hide all archived subtrees after a visibility state change."
  7380. (when (and (not org-cycle-open-archived-trees)
  7381. (not (memq state '(overview folded))))
  7382. (save-excursion
  7383. (let* ((globalp (memq state '(contents all)))
  7384. (beg (if globalp (point-min) (point)))
  7385. (end (if globalp (point-max) (org-end-of-subtree t))))
  7386. (org-hide-archived-subtrees beg end)
  7387. (goto-char beg)
  7388. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7389. (message "%s" (substitute-command-keys
  7390. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7391. (defun org-force-cycle-archived ()
  7392. "Cycle subtree even if it is archived."
  7393. (interactive)
  7394. (setq this-command 'org-cycle)
  7395. (let ((org-cycle-open-archived-trees t))
  7396. (call-interactively 'org-cycle)))
  7397. (defun org-hide-archived-subtrees (beg end)
  7398. "Re-hide all archived subtrees after a visibility state change."
  7399. (save-excursion
  7400. (let* ((re (concat ":" org-archive-tag ":")))
  7401. (goto-char beg)
  7402. (while (re-search-forward re end t)
  7403. (and (org-on-heading-p) (hide-subtree))
  7404. (org-end-of-subtree t)))))
  7405. (defun org-toggle-tag (tag &optional onoff)
  7406. "Toggle the tag TAG for the current line.
  7407. If ONOFF is `on' or `off', don't toggle but set to this state."
  7408. (unless (org-on-heading-p t) (error "Not on headling"))
  7409. (let (res current)
  7410. (save-excursion
  7411. (beginning-of-line)
  7412. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7413. (point-at-eol) t)
  7414. (progn
  7415. (setq current (match-string 1))
  7416. (replace-match ""))
  7417. (setq current ""))
  7418. (setq current (nreverse (org-split-string current ":")))
  7419. (cond
  7420. ((eq onoff 'on)
  7421. (setq res t)
  7422. (or (member tag current) (push tag current)))
  7423. ((eq onoff 'off)
  7424. (or (not (member tag current)) (setq current (delete tag current))))
  7425. (t (if (member tag current)
  7426. (setq current (delete tag current))
  7427. (setq res t)
  7428. (push tag current))))
  7429. (end-of-line 1)
  7430. (if current
  7431. (progn
  7432. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7433. (org-set-tags nil t))
  7434. (delete-horizontal-space))
  7435. (run-hooks 'org-after-tags-change-hook))
  7436. res))
  7437. (defun org-toggle-archive-tag (&optional arg)
  7438. "Toggle the archive tag for the current headline.
  7439. With prefix ARG, check all children of current headline and offer tagging
  7440. the children that do not contain any open TODO items."
  7441. (interactive "P")
  7442. (if arg
  7443. (org-archive-all-done 'tag)
  7444. (let (set)
  7445. (save-excursion
  7446. (org-back-to-heading t)
  7447. (setq set (org-toggle-tag org-archive-tag))
  7448. (when set (hide-subtree)))
  7449. (and set (beginning-of-line 1))
  7450. (message "Subtree %s" (if set "archived" "unarchived")))))
  7451. ;;;; Tables
  7452. ;;; The table editor
  7453. ;; Watch out: Here we are talking about two different kind of tables.
  7454. ;; Most of the code is for the tables created with the Org-mode table editor.
  7455. ;; Sometimes, we talk about tables created and edited with the table.el
  7456. ;; Emacs package. We call the former org-type tables, and the latter
  7457. ;; table.el-type tables.
  7458. (defun org-before-change-function (beg end)
  7459. "Every change indicates that a table might need an update."
  7460. (setq org-table-may-need-update t))
  7461. (defconst org-table-line-regexp "^[ \t]*|"
  7462. "Detects an org-type table line.")
  7463. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7464. "Detects an org-type table line.")
  7465. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7466. "Detects a table line marked for automatic recalculation.")
  7467. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7468. "Detects a table line marked for automatic recalculation.")
  7469. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7470. "Detects a table line marked for automatic recalculation.")
  7471. (defconst org-table-hline-regexp "^[ \t]*|-"
  7472. "Detects an org-type table hline.")
  7473. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7474. "Detects a table-type table hline.")
  7475. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7476. "Detects an org-type or table-type table.")
  7477. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7478. "Searching from within a table (any type) this finds the first line
  7479. outside the table.")
  7480. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7481. "Searching from within a table (any type) this finds the first line
  7482. outside the table.")
  7483. (defvar org-table-last-highlighted-reference nil)
  7484. (defvar org-table-formula-history nil)
  7485. (defvar org-table-column-names nil
  7486. "Alist with column names, derived from the `!' line.")
  7487. (defvar org-table-column-name-regexp nil
  7488. "Regular expression matching the current column names.")
  7489. (defvar org-table-local-parameters nil
  7490. "Alist with parameter names, derived from the `$' line.")
  7491. (defvar org-table-named-field-locations nil
  7492. "Alist with locations of named fields.")
  7493. (defvar org-table-current-line-types nil
  7494. "Table row types, non-nil only for the duration of a comand.")
  7495. (defvar org-table-current-begin-line nil
  7496. "Table begin line, non-nil only for the duration of a comand.")
  7497. (defvar org-table-current-begin-pos nil
  7498. "Table begin position, non-nil only for the duration of a comand.")
  7499. (defvar org-table-dlines nil
  7500. "Vector of data line line numbers in the current table.")
  7501. (defvar org-table-hlines nil
  7502. "Vector of hline line numbers in the current table.")
  7503. (defconst org-table-range-regexp
  7504. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7505. ;; 1 2 3 4 5
  7506. "Regular expression for matching ranges in formulas.")
  7507. (defconst org-table-range-regexp2
  7508. (concat
  7509. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7510. "\\.\\."
  7511. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7512. "Match a range for reference display.")
  7513. (defconst org-table-translate-regexp
  7514. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7515. "Match a reference that needs translation, for reference display.")
  7516. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7517. (defun org-table-create-with-table.el ()
  7518. "Use the table.el package to insert a new table.
  7519. If there is already a table at point, convert between Org-mode tables
  7520. and table.el tables."
  7521. (interactive)
  7522. (require 'table)
  7523. (cond
  7524. ((org-at-table.el-p)
  7525. (if (y-or-n-p "Convert table to Org-mode table? ")
  7526. (org-table-convert)))
  7527. ((org-at-table-p)
  7528. (if (y-or-n-p "Convert table to table.el table? ")
  7529. (org-table-convert)))
  7530. (t (call-interactively 'table-insert))))
  7531. (defun org-table-create-or-convert-from-region (arg)
  7532. "Convert region to table, or create an empty table.
  7533. If there is an active region, convert it to a table, using the function
  7534. `org-table-convert-region'. See the documentation of that function
  7535. to learn how the prefix argument is interpreted to determine the field
  7536. separator.
  7537. If there is no such region, create an empty table with `org-table-create'."
  7538. (interactive "P")
  7539. (if (org-region-active-p)
  7540. (org-table-convert-region (region-beginning) (region-end) arg)
  7541. (org-table-create arg)))
  7542. (defun org-table-create (&optional size)
  7543. "Query for a size and insert a table skeleton.
  7544. SIZE is a string Columns x Rows like for example \"3x2\"."
  7545. (interactive "P")
  7546. (unless size
  7547. (setq size (read-string
  7548. (concat "Table size Columns x Rows [e.g. "
  7549. org-table-default-size "]: ")
  7550. "" nil org-table-default-size)))
  7551. (let* ((pos (point))
  7552. (indent (make-string (current-column) ?\ ))
  7553. (split (org-split-string size " *x *"))
  7554. (rows (string-to-number (nth 1 split)))
  7555. (columns (string-to-number (car split)))
  7556. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7557. "\n")))
  7558. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7559. (point-at-bol) (point)))
  7560. (beginning-of-line 1)
  7561. (newline))
  7562. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7563. (dotimes (i rows) (insert line))
  7564. (goto-char pos)
  7565. (if (> rows 1)
  7566. ;; Insert a hline after the first row.
  7567. (progn
  7568. (end-of-line 1)
  7569. (insert "\n|-")
  7570. (goto-char pos)))
  7571. (org-table-align)))
  7572. (defun org-table-convert-region (beg0 end0 &optional separator)
  7573. "Convert region to a table.
  7574. The region goes from BEG0 to END0, but these borders will be moved
  7575. slightly, to make sure a beginning of line in the first line is included.
  7576. SEPARATOR specifies the field separator in the lines. It can have the
  7577. following values:
  7578. '(4) Use the comma as a field separator
  7579. '(16) Use a TAB as field separator
  7580. integer When a number, use that many spaces as field separator
  7581. nil When nil, the command tries to be smart and figure out the
  7582. separator in the following way:
  7583. - when each line contains a TAB, assume TAB-separated material
  7584. - when each line contains a comme, assume CSV material
  7585. - else, assume one or more SPACE charcters as separator."
  7586. (interactive "rP")
  7587. (let* ((beg (min beg0 end0))
  7588. (end (max beg0 end0))
  7589. re)
  7590. (goto-char beg)
  7591. (beginning-of-line 1)
  7592. (setq beg (move-marker (make-marker) (point)))
  7593. (goto-char end)
  7594. (if (bolp) (backward-char 1) (end-of-line 1))
  7595. (setq end (move-marker (make-marker) (point)))
  7596. ;; Get the right field separator
  7597. (unless separator
  7598. (goto-char beg)
  7599. (setq separator
  7600. (cond
  7601. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7602. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7603. (t 1))))
  7604. (setq re (cond
  7605. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7606. ((equal separator '(16)) "^\\|\t")
  7607. ((integerp separator)
  7608. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7609. (t (error "This should not happen"))))
  7610. (goto-char beg)
  7611. (while (re-search-forward re end t)
  7612. (replace-match "| " t t))
  7613. (goto-char beg)
  7614. (insert " ")
  7615. (org-table-align)))
  7616. (defun org-table-import (file arg)
  7617. "Import FILE as a table.
  7618. The file is assumed to be tab-separated. Such files can be produced by most
  7619. spreadsheet and database applications. If no tabs (at least one per line)
  7620. are found, lines will be split on whitespace into fields."
  7621. (interactive "f\nP")
  7622. (or (bolp) (newline))
  7623. (let ((beg (point))
  7624. (pm (point-max)))
  7625. (insert-file-contents file)
  7626. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7627. (defun org-table-export ()
  7628. "Export table as a tab-separated file.
  7629. Such a file can be imported into a spreadsheet program like Excel."
  7630. (interactive)
  7631. (let* ((beg (org-table-begin))
  7632. (end (org-table-end))
  7633. (table (buffer-substring beg end))
  7634. (file (read-file-name "Export table to: "))
  7635. buf)
  7636. (unless (or (not (file-exists-p file))
  7637. (y-or-n-p (format "Overwrite file %s? " file)))
  7638. (error "Abort"))
  7639. (with-current-buffer (find-file-noselect file)
  7640. (setq buf (current-buffer))
  7641. (erase-buffer)
  7642. (fundamental-mode)
  7643. (insert table)
  7644. (goto-char (point-min))
  7645. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7646. (replace-match "" t t)
  7647. (end-of-line 1))
  7648. (goto-char (point-min))
  7649. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7650. (replace-match "" t t)
  7651. (goto-char (min (1+ (point)) (point-max))))
  7652. (goto-char (point-min))
  7653. (while (re-search-forward "^-[-+]*$" nil t)
  7654. (replace-match "")
  7655. (if (looking-at "\n")
  7656. (delete-char 1)))
  7657. (goto-char (point-min))
  7658. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7659. (replace-match "\t" t t))
  7660. (save-buffer))
  7661. (kill-buffer buf)))
  7662. (defvar org-table-aligned-begin-marker (make-marker)
  7663. "Marker at the beginning of the table last aligned.
  7664. Used to check if cursor still is in that table, to minimize realignment.")
  7665. (defvar org-table-aligned-end-marker (make-marker)
  7666. "Marker at the end of the table last aligned.
  7667. Used to check if cursor still is in that table, to minimize realignment.")
  7668. (defvar org-table-last-alignment nil
  7669. "List of flags for flushright alignment, from the last re-alignment.
  7670. This is being used to correctly align a single field after TAB or RET.")
  7671. (defvar org-table-last-column-widths nil
  7672. "List of max width of fields in each column.
  7673. This is being used to correctly align a single field after TAB or RET.")
  7674. (defvar org-table-overlay-coordinates nil
  7675. "Overlay coordinates after each align of a table.")
  7676. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7677. (defvar org-last-recalc-line nil)
  7678. (defconst org-narrow-column-arrow "=>"
  7679. "Used as display property in narrowed table columns.")
  7680. (defun org-table-align ()
  7681. "Align the table at point by aligning all vertical bars."
  7682. (interactive)
  7683. (let* (
  7684. ;; Limits of table
  7685. (beg (org-table-begin))
  7686. (end (org-table-end))
  7687. ;; Current cursor position
  7688. (linepos (org-current-line))
  7689. (colpos (org-table-current-column))
  7690. (winstart (window-start))
  7691. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7692. lines (new "") lengths l typenums ty fields maxfields i
  7693. column
  7694. (indent "") cnt frac
  7695. rfmt hfmt
  7696. (spaces '(1 . 1))
  7697. (sp1 (car spaces))
  7698. (sp2 (cdr spaces))
  7699. (rfmt1 (concat
  7700. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7701. (hfmt1 (concat
  7702. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7703. emptystrings links dates emph narrow fmax f1 len c e)
  7704. (untabify beg end)
  7705. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7706. ;; Check if we have links or dates
  7707. (goto-char beg)
  7708. (setq links (re-search-forward org-bracket-link-regexp end t))
  7709. (goto-char beg)
  7710. (setq emph (and org-hide-emphasis-markers
  7711. (re-search-forward org-emph-re end t)))
  7712. (goto-char beg)
  7713. (setq dates (and org-display-custom-times
  7714. (re-search-forward org-ts-regexp-both end t)))
  7715. ;; Make sure the link properties are right
  7716. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7717. ;; Make sure the date properties are right
  7718. (when dates (goto-char beg) (while (org-activate-dates end)))
  7719. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7720. ;; Check if we are narrowing any columns
  7721. (goto-char beg)
  7722. (setq narrow (and org-format-transports-properties-p
  7723. (re-search-forward "<[0-9]+>" end t)))
  7724. ;; Get the rows
  7725. (setq lines (org-split-string
  7726. (buffer-substring beg end) "\n"))
  7727. ;; Store the indentation of the first line
  7728. (if (string-match "^ *" (car lines))
  7729. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7730. ;; Mark the hlines by setting the corresponding element to nil
  7731. ;; At the same time, we remove trailing space.
  7732. (setq lines (mapcar (lambda (l)
  7733. (if (string-match "^ *|-" l)
  7734. nil
  7735. (if (string-match "[ \t]+$" l)
  7736. (substring l 0 (match-beginning 0))
  7737. l)))
  7738. lines))
  7739. ;; Get the data fields by splitting the lines.
  7740. (setq fields (mapcar
  7741. (lambda (l)
  7742. (org-split-string l " *| *"))
  7743. (delq nil (copy-sequence lines))))
  7744. ;; How many fields in the longest line?
  7745. (condition-case nil
  7746. (setq maxfields (apply 'max (mapcar 'length fields)))
  7747. (error
  7748. (kill-region beg end)
  7749. (org-table-create org-table-default-size)
  7750. (error "Empty table - created default table")))
  7751. ;; A list of empty strings to fill any short rows on output
  7752. (setq emptystrings (make-list maxfields ""))
  7753. ;; Check for special formatting.
  7754. (setq i -1)
  7755. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7756. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7757. ;; Check if there is an explicit width specified
  7758. (when narrow
  7759. (setq c column fmax nil)
  7760. (while c
  7761. (setq e (pop c))
  7762. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7763. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7764. ;; Find fields that are wider than fmax, and shorten them
  7765. (when fmax
  7766. (loop for xx in column do
  7767. (when (and (stringp xx)
  7768. (> (org-string-width xx) fmax))
  7769. (org-add-props xx nil
  7770. 'help-echo
  7771. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7772. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7773. (unless (> f1 1)
  7774. (error "Cannot narrow field starting with wide link \"%s\""
  7775. (match-string 0 xx)))
  7776. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7777. (add-text-properties (- f1 2) f1
  7778. (list 'display org-narrow-column-arrow)
  7779. xx)))))
  7780. ;; Get the maximum width for each column
  7781. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7782. ;; Get the fraction of numbers, to decide about alignment of the column
  7783. (setq cnt 0 frac 0.0)
  7784. (loop for x in column do
  7785. (if (equal x "")
  7786. nil
  7787. (setq frac ( / (+ (* frac cnt)
  7788. (if (string-match org-table-number-regexp x) 1 0))
  7789. (setq cnt (1+ cnt))))))
  7790. (push (>= frac org-table-number-fraction) typenums))
  7791. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7792. ;; Store the alignment of this table, for later editing of single fields
  7793. (setq org-table-last-alignment typenums
  7794. org-table-last-column-widths lengths)
  7795. ;; With invisible characters, `format' does not get the field width right
  7796. ;; So we need to make these fields wide by hand.
  7797. (when (or links emph)
  7798. (loop for i from 0 upto (1- maxfields) do
  7799. (setq len (nth i lengths))
  7800. (loop for j from 0 upto (1- (length fields)) do
  7801. (setq c (nthcdr i (car (nthcdr j fields))))
  7802. (if (and (stringp (car c))
  7803. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7804. ; (string-match org-bracket-link-regexp (car c))
  7805. (< (org-string-width (car c)) len))
  7806. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7807. ;; Compute the formats needed for output of the table
  7808. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7809. (while (setq l (pop lengths))
  7810. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7811. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7812. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7813. (setq rfmt (concat rfmt "\n")
  7814. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7815. (setq new (mapconcat
  7816. (lambda (l)
  7817. (if l (apply 'format rfmt
  7818. (append (pop fields) emptystrings))
  7819. hfmt))
  7820. lines ""))
  7821. ;; Replace the old one
  7822. (delete-region beg end)
  7823. (move-marker end nil)
  7824. (move-marker org-table-aligned-begin-marker (point))
  7825. (insert new)
  7826. (move-marker org-table-aligned-end-marker (point))
  7827. (when (and orgtbl-mode (not (org-mode-p)))
  7828. (goto-char org-table-aligned-begin-marker)
  7829. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7830. ;; Try to move to the old location
  7831. (goto-line winstartline)
  7832. (setq winstart (point-at-bol))
  7833. (goto-line linepos)
  7834. (set-window-start (selected-window) winstart 'noforce)
  7835. (org-table-goto-column colpos)
  7836. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7837. (setq org-table-may-need-update nil)
  7838. ))
  7839. (defun org-string-width (s)
  7840. "Compute width of string, ignoring invisible characters.
  7841. This ignores character with invisibility property `org-link', and also
  7842. characters with property `org-cwidth', because these will become invisible
  7843. upon the next fontification round."
  7844. (let (b l)
  7845. (when (or (eq t buffer-invisibility-spec)
  7846. (assq 'org-link buffer-invisibility-spec))
  7847. (while (setq b (text-property-any 0 (length s)
  7848. 'invisible 'org-link s))
  7849. (setq s (concat (substring s 0 b)
  7850. (substring s (or (next-single-property-change
  7851. b 'invisible s) (length s)))))))
  7852. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7853. (setq s (concat (substring s 0 b)
  7854. (substring s (or (next-single-property-change
  7855. b 'org-cwidth s) (length s))))))
  7856. (setq l (string-width s) b -1)
  7857. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7858. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7859. l))
  7860. (defun org-table-begin (&optional table-type)
  7861. "Find the beginning of the table and return its position.
  7862. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7863. (save-excursion
  7864. (if (not (re-search-backward
  7865. (if table-type org-table-any-border-regexp
  7866. org-table-border-regexp)
  7867. nil t))
  7868. (progn (goto-char (point-min)) (point))
  7869. (goto-char (match-beginning 0))
  7870. (beginning-of-line 2)
  7871. (point))))
  7872. (defun org-table-end (&optional table-type)
  7873. "Find the end of the table and return its position.
  7874. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7875. (save-excursion
  7876. (if (not (re-search-forward
  7877. (if table-type org-table-any-border-regexp
  7878. org-table-border-regexp)
  7879. nil t))
  7880. (goto-char (point-max))
  7881. (goto-char (match-beginning 0)))
  7882. (point-marker)))
  7883. (defun org-table-justify-field-maybe (&optional new)
  7884. "Justify the current field, text to left, number to right.
  7885. Optional argument NEW may specify text to replace the current field content."
  7886. (cond
  7887. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7888. ((org-at-table-hline-p))
  7889. ((and (not new)
  7890. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7891. (current-buffer)))
  7892. (< (point) org-table-aligned-begin-marker)
  7893. (>= (point) org-table-aligned-end-marker)))
  7894. ;; This is not the same table, force a full re-align
  7895. (setq org-table-may-need-update t))
  7896. (t ;; realign the current field, based on previous full realign
  7897. (let* ((pos (point)) s
  7898. (col (org-table-current-column))
  7899. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7900. l f n o e)
  7901. (when (> col 0)
  7902. (skip-chars-backward "^|\n")
  7903. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7904. (progn
  7905. (setq s (match-string 1)
  7906. o (match-string 0)
  7907. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7908. e (not (= (match-beginning 2) (match-end 2))))
  7909. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7910. l (if e "|" (setq org-table-may-need-update t) ""))
  7911. n (format f s))
  7912. (if new
  7913. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7914. (setq n (format f new))
  7915. (setq n (concat new "|") org-table-may-need-update t)))
  7916. (or (equal n o)
  7917. (let (org-table-may-need-update)
  7918. (replace-match n t t))))
  7919. (setq org-table-may-need-update t))
  7920. (goto-char pos))))))
  7921. (defun org-table-next-field ()
  7922. "Go to the next field in the current table, creating new lines as needed.
  7923. Before doing so, re-align the table if necessary."
  7924. (interactive)
  7925. (org-table-maybe-eval-formula)
  7926. (org-table-maybe-recalculate-line)
  7927. (if (and org-table-automatic-realign
  7928. org-table-may-need-update)
  7929. (org-table-align))
  7930. (let ((end (org-table-end)))
  7931. (if (org-at-table-hline-p)
  7932. (end-of-line 1))
  7933. (condition-case nil
  7934. (progn
  7935. (re-search-forward "|" end)
  7936. (if (looking-at "[ \t]*$")
  7937. (re-search-forward "|" end))
  7938. (if (and (looking-at "-")
  7939. org-table-tab-jumps-over-hlines
  7940. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7941. (goto-char (match-beginning 1)))
  7942. (if (looking-at "-")
  7943. (progn
  7944. (beginning-of-line 0)
  7945. (org-table-insert-row 'below))
  7946. (if (looking-at " ") (forward-char 1))))
  7947. (error
  7948. (org-table-insert-row 'below)))))
  7949. (defun org-table-previous-field ()
  7950. "Go to the previous field in the table.
  7951. Before doing so, re-align the table if necessary."
  7952. (interactive)
  7953. (org-table-justify-field-maybe)
  7954. (org-table-maybe-recalculate-line)
  7955. (if (and org-table-automatic-realign
  7956. org-table-may-need-update)
  7957. (org-table-align))
  7958. (if (org-at-table-hline-p)
  7959. (end-of-line 1))
  7960. (re-search-backward "|" (org-table-begin))
  7961. (re-search-backward "|" (org-table-begin))
  7962. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7963. (re-search-backward "|" (org-table-begin)))
  7964. (if (looking-at "| ?")
  7965. (goto-char (match-end 0))))
  7966. (defun org-table-next-row ()
  7967. "Go to the next row (same column) in the current table.
  7968. Before doing so, re-align the table if necessary."
  7969. (interactive)
  7970. (org-table-maybe-eval-formula)
  7971. (org-table-maybe-recalculate-line)
  7972. (if (or (looking-at "[ \t]*$")
  7973. (save-excursion (skip-chars-backward " \t") (bolp)))
  7974. (newline)
  7975. (if (and org-table-automatic-realign
  7976. org-table-may-need-update)
  7977. (org-table-align))
  7978. (let ((col (org-table-current-column)))
  7979. (beginning-of-line 2)
  7980. (if (or (not (org-at-table-p))
  7981. (org-at-table-hline-p))
  7982. (progn
  7983. (beginning-of-line 0)
  7984. (org-table-insert-row 'below)))
  7985. (org-table-goto-column col)
  7986. (skip-chars-backward "^|\n\r")
  7987. (if (looking-at " ") (forward-char 1)))))
  7988. (defun org-table-copy-down (n)
  7989. "Copy a field down in the current column.
  7990. If the field at the cursor is empty, copy into it the content of the nearest
  7991. non-empty field above. With argument N, use the Nth non-empty field.
  7992. If the current field is not empty, it is copied down to the next row, and
  7993. the cursor is moved with it. Therefore, repeating this command causes the
  7994. column to be filled row-by-row.
  7995. If the variable `org-table-copy-increment' is non-nil and the field is an
  7996. integer or a timestamp, it will be incremented while copying. In the case of
  7997. a timestamp, if the cursor is on the year, change the year. If it is on the
  7998. month or the day, change that. Point will stay on the current date field
  7999. in order to easily repeat the interval."
  8000. (interactive "p")
  8001. (let* ((colpos (org-table-current-column))
  8002. (col (current-column))
  8003. (field (org-table-get-field))
  8004. (non-empty (string-match "[^ \t]" field))
  8005. (beg (org-table-begin))
  8006. txt)
  8007. (org-table-check-inside-data-field)
  8008. (if non-empty
  8009. (progn
  8010. (setq txt (org-trim field))
  8011. (org-table-next-row)
  8012. (org-table-blank-field))
  8013. (save-excursion
  8014. (setq txt
  8015. (catch 'exit
  8016. (while (progn (beginning-of-line 1)
  8017. (re-search-backward org-table-dataline-regexp
  8018. beg t))
  8019. (org-table-goto-column colpos t)
  8020. (if (and (looking-at
  8021. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  8022. (= (setq n (1- n)) 0))
  8023. (throw 'exit (match-string 1))))))))
  8024. (if txt
  8025. (progn
  8026. (if (and org-table-copy-increment
  8027. (string-match "^[0-9]+$" txt))
  8028. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  8029. (insert txt)
  8030. (move-to-column col)
  8031. (if (and org-table-copy-increment (org-at-timestamp-p t))
  8032. (org-timestamp-up 1)
  8033. (org-table-maybe-recalculate-line))
  8034. (org-table-align)
  8035. (move-to-column col))
  8036. (error "No non-empty field found"))))
  8037. (defun org-table-check-inside-data-field ()
  8038. "Is point inside a table data field?
  8039. I.e. not on a hline or before the first or after the last column?
  8040. This actually throws an error, so it aborts the current command."
  8041. (if (or (not (org-at-table-p))
  8042. (= (org-table-current-column) 0)
  8043. (org-at-table-hline-p)
  8044. (looking-at "[ \t]*$"))
  8045. (error "Not in table data field")))
  8046. (defvar org-table-clip nil
  8047. "Clipboard for table regions.")
  8048. (defun org-table-blank-field ()
  8049. "Blank the current table field or active region."
  8050. (interactive)
  8051. (org-table-check-inside-data-field)
  8052. (if (and (interactive-p) (org-region-active-p))
  8053. (let (org-table-clip)
  8054. (org-table-cut-region (region-beginning) (region-end)))
  8055. (skip-chars-backward "^|")
  8056. (backward-char 1)
  8057. (if (looking-at "|[^|\n]+")
  8058. (let* ((pos (match-beginning 0))
  8059. (match (match-string 0))
  8060. (len (org-string-width match)))
  8061. (replace-match (concat "|" (make-string (1- len) ?\ )))
  8062. (goto-char (+ 2 pos))
  8063. (substring match 1)))))
  8064. (defun org-table-get-field (&optional n replace)
  8065. "Return the value of the field in column N of current row.
  8066. N defaults to current field.
  8067. If REPLACE is a string, replace field with this value. The return value
  8068. is always the old value."
  8069. (and n (org-table-goto-column n))
  8070. (skip-chars-backward "^|\n")
  8071. (backward-char 1)
  8072. (if (looking-at "|[^|\r\n]*")
  8073. (let* ((pos (match-beginning 0))
  8074. (val (buffer-substring (1+ pos) (match-end 0))))
  8075. (if replace
  8076. (replace-match (concat "|" replace) t t))
  8077. (goto-char (min (point-at-eol) (+ 2 pos)))
  8078. val)
  8079. (forward-char 1) ""))
  8080. (defun org-table-field-info (arg)
  8081. "Show info about the current field, and highlight any reference at point."
  8082. (interactive "P")
  8083. (org-table-get-specials)
  8084. (save-excursion
  8085. (let* ((pos (point))
  8086. (col (org-table-current-column))
  8087. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  8088. (name (car (rassoc (list (org-current-line) col)
  8089. org-table-named-field-locations)))
  8090. (eql (org-table-get-stored-formulas))
  8091. (dline (org-table-current-dline))
  8092. (ref (format "@%d$%d" dline col))
  8093. (ref1 (org-table-convert-refs-to-an ref))
  8094. (fequation (or (assoc name eql) (assoc ref eql)))
  8095. (cequation (assoc (int-to-string col) eql))
  8096. (eqn (or fequation cequation)))
  8097. (goto-char pos)
  8098. (condition-case nil
  8099. (org-table-show-reference 'local)
  8100. (error nil))
  8101. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  8102. dline col
  8103. (if cname (concat " or $" cname) "")
  8104. dline col ref1
  8105. (if name (concat " or $" name) "")
  8106. ;; FIXME: formula info not correct if special table line
  8107. (if eqn
  8108. (concat ", formula: "
  8109. (org-table-formula-to-user
  8110. (concat
  8111. (if (string-match "^[$@]"(car eqn)) "" "$")
  8112. (car eqn) "=" (cdr eqn))))
  8113. "")))))
  8114. (defun org-table-current-column ()
  8115. "Find out which column we are in.
  8116. When called interactively, column is also displayed in echo area."
  8117. (interactive)
  8118. (if (interactive-p) (org-table-check-inside-data-field))
  8119. (save-excursion
  8120. (let ((cnt 0) (pos (point)))
  8121. (beginning-of-line 1)
  8122. (while (search-forward "|" pos t)
  8123. (setq cnt (1+ cnt)))
  8124. (if (interactive-p) (message "This is table column %d" cnt))
  8125. cnt)))
  8126. (defun org-table-current-dline ()
  8127. "Find out what table data line we are in.
  8128. Only datalins count for this."
  8129. (interactive)
  8130. (if (interactive-p) (org-table-check-inside-data-field))
  8131. (save-excursion
  8132. (let ((cnt 0) (pos (point)))
  8133. (goto-char (org-table-begin))
  8134. (while (<= (point) pos)
  8135. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  8136. (beginning-of-line 2))
  8137. (if (interactive-p) (message "This is table line %d" cnt))
  8138. cnt)))
  8139. (defun org-table-goto-column (n &optional on-delim force)
  8140. "Move the cursor to the Nth column in the current table line.
  8141. With optional argument ON-DELIM, stop with point before the left delimiter
  8142. of the field.
  8143. If there are less than N fields, just go to after the last delimiter.
  8144. However, when FORCE is non-nil, create new columns if necessary."
  8145. (interactive "p")
  8146. (let ((pos (point-at-eol)))
  8147. (beginning-of-line 1)
  8148. (when (> n 0)
  8149. (while (and (> (setq n (1- n)) -1)
  8150. (or (search-forward "|" pos t)
  8151. (and force
  8152. (progn (end-of-line 1)
  8153. (skip-chars-backward "^|")
  8154. (insert " | "))))))
  8155. ; (backward-char 2) t)))))
  8156. (when (and force (not (looking-at ".*|")))
  8157. (save-excursion (end-of-line 1) (insert " | ")))
  8158. (if on-delim
  8159. (backward-char 1)
  8160. (if (looking-at " ") (forward-char 1))))))
  8161. (defun org-at-table-p (&optional table-type)
  8162. "Return t if the cursor is inside an org-type table.
  8163. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8164. (if org-enable-table-editor
  8165. (save-excursion
  8166. (beginning-of-line 1)
  8167. (looking-at (if table-type org-table-any-line-regexp
  8168. org-table-line-regexp)))
  8169. nil))
  8170. (defun org-at-table.el-p ()
  8171. "Return t if and only if we are at a table.el table."
  8172. (and (org-at-table-p 'any)
  8173. (save-excursion
  8174. (goto-char (org-table-begin 'any))
  8175. (looking-at org-table1-hline-regexp))))
  8176. (defun org-table-recognize-table.el ()
  8177. "If there is a table.el table nearby, recognize it and move into it."
  8178. (if org-table-tab-recognizes-table.el
  8179. (if (org-at-table.el-p)
  8180. (progn
  8181. (beginning-of-line 1)
  8182. (if (looking-at org-table-dataline-regexp)
  8183. nil
  8184. (if (looking-at org-table1-hline-regexp)
  8185. (progn
  8186. (beginning-of-line 2)
  8187. (if (looking-at org-table-any-border-regexp)
  8188. (beginning-of-line -1)))))
  8189. (if (re-search-forward "|" (org-table-end t) t)
  8190. (progn
  8191. (require 'table)
  8192. (if (table--at-cell-p (point))
  8193. t
  8194. (message "recognizing table.el table...")
  8195. (table-recognize-table)
  8196. (message "recognizing table.el table...done")))
  8197. (error "This should not happen..."))
  8198. t)
  8199. nil)
  8200. nil))
  8201. (defun org-at-table-hline-p ()
  8202. "Return t if the cursor is inside a hline in a table."
  8203. (if org-enable-table-editor
  8204. (save-excursion
  8205. (beginning-of-line 1)
  8206. (looking-at org-table-hline-regexp))
  8207. nil))
  8208. (defun org-table-insert-column ()
  8209. "Insert a new column into the table."
  8210. (interactive)
  8211. (if (not (org-at-table-p))
  8212. (error "Not at a table"))
  8213. (org-table-find-dataline)
  8214. (let* ((col (max 1 (org-table-current-column)))
  8215. (beg (org-table-begin))
  8216. (end (org-table-end))
  8217. ;; Current cursor position
  8218. (linepos (org-current-line))
  8219. (colpos col))
  8220. (goto-char beg)
  8221. (while (< (point) end)
  8222. (if (org-at-table-hline-p)
  8223. nil
  8224. (org-table-goto-column col t)
  8225. (insert "| "))
  8226. (beginning-of-line 2))
  8227. (move-marker end nil)
  8228. (goto-line linepos)
  8229. (org-table-goto-column colpos)
  8230. (org-table-align)
  8231. (org-table-fix-formulas "$" nil (1- col) 1)))
  8232. (defun org-table-find-dataline ()
  8233. "Find a dataline in the current table, which is needed for column commands."
  8234. (if (and (org-at-table-p)
  8235. (not (org-at-table-hline-p)))
  8236. t
  8237. (let ((col (current-column))
  8238. (end (org-table-end)))
  8239. (move-to-column col)
  8240. (while (and (< (point) end)
  8241. (or (not (= (current-column) col))
  8242. (org-at-table-hline-p)))
  8243. (beginning-of-line 2)
  8244. (move-to-column col))
  8245. (if (and (org-at-table-p)
  8246. (not (org-at-table-hline-p)))
  8247. t
  8248. (error
  8249. "Please position cursor in a data line for column operations")))))
  8250. (defun org-table-delete-column ()
  8251. "Delete a column from the table."
  8252. (interactive)
  8253. (if (not (org-at-table-p))
  8254. (error "Not at a table"))
  8255. (org-table-find-dataline)
  8256. (org-table-check-inside-data-field)
  8257. (let* ((col (org-table-current-column))
  8258. (beg (org-table-begin))
  8259. (end (org-table-end))
  8260. ;; Current cursor position
  8261. (linepos (org-current-line))
  8262. (colpos col))
  8263. (goto-char beg)
  8264. (while (< (point) end)
  8265. (if (org-at-table-hline-p)
  8266. nil
  8267. (org-table-goto-column col t)
  8268. (and (looking-at "|[^|\n]+|")
  8269. (replace-match "|")))
  8270. (beginning-of-line 2))
  8271. (move-marker end nil)
  8272. (goto-line linepos)
  8273. (org-table-goto-column colpos)
  8274. (org-table-align)
  8275. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8276. col -1 col)))
  8277. (defun org-table-move-column-right ()
  8278. "Move column to the right."
  8279. (interactive)
  8280. (org-table-move-column nil))
  8281. (defun org-table-move-column-left ()
  8282. "Move column to the left."
  8283. (interactive)
  8284. (org-table-move-column 'left))
  8285. (defun org-table-move-column (&optional left)
  8286. "Move the current column to the right. With arg LEFT, move to the left."
  8287. (interactive "P")
  8288. (if (not (org-at-table-p))
  8289. (error "Not at a table"))
  8290. (org-table-find-dataline)
  8291. (org-table-check-inside-data-field)
  8292. (let* ((col (org-table-current-column))
  8293. (col1 (if left (1- col) col))
  8294. (beg (org-table-begin))
  8295. (end (org-table-end))
  8296. ;; Current cursor position
  8297. (linepos (org-current-line))
  8298. (colpos (if left (1- col) (1+ col))))
  8299. (if (and left (= col 1))
  8300. (error "Cannot move column further left"))
  8301. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8302. (error "Cannot move column further right"))
  8303. (goto-char beg)
  8304. (while (< (point) end)
  8305. (if (org-at-table-hline-p)
  8306. nil
  8307. (org-table-goto-column col1 t)
  8308. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8309. (replace-match "|\\2|\\1|")))
  8310. (beginning-of-line 2))
  8311. (move-marker end nil)
  8312. (goto-line linepos)
  8313. (org-table-goto-column colpos)
  8314. (org-table-align)
  8315. (org-table-fix-formulas
  8316. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8317. (cons (number-to-string colpos) (number-to-string col))))))
  8318. (defun org-table-move-row-down ()
  8319. "Move table row down."
  8320. (interactive)
  8321. (org-table-move-row nil))
  8322. (defun org-table-move-row-up ()
  8323. "Move table row up."
  8324. (interactive)
  8325. (org-table-move-row 'up))
  8326. (defun org-table-move-row (&optional up)
  8327. "Move the current table line down. With arg UP, move it up."
  8328. (interactive "P")
  8329. (let* ((col (current-column))
  8330. (pos (point))
  8331. (hline1p (save-excursion (beginning-of-line 1)
  8332. (looking-at org-table-hline-regexp)))
  8333. (dline1 (org-table-current-dline))
  8334. (dline2 (+ dline1 (if up -1 1)))
  8335. (tonew (if up 0 2))
  8336. txt hline2p)
  8337. (beginning-of-line tonew)
  8338. (unless (org-at-table-p)
  8339. (goto-char pos)
  8340. (error "Cannot move row further"))
  8341. (setq hline2p (looking-at org-table-hline-regexp))
  8342. (goto-char pos)
  8343. (beginning-of-line 1)
  8344. (setq pos (point))
  8345. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8346. (delete-region (point) (1+ (point-at-eol)))
  8347. (beginning-of-line tonew)
  8348. (insert txt)
  8349. (beginning-of-line 0)
  8350. (move-to-column col)
  8351. (unless (or hline1p hline2p)
  8352. (org-table-fix-formulas
  8353. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8354. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8355. (defun org-table-insert-row (&optional arg)
  8356. "Insert a new row above the current line into the table.
  8357. With prefix ARG, insert below the current line."
  8358. (interactive "P")
  8359. (if (not (org-at-table-p))
  8360. (error "Not at a table"))
  8361. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8362. (new (org-table-clean-line line)))
  8363. ;; Fix the first field if necessary
  8364. (if (string-match "^[ \t]*| *[#$] *|" line)
  8365. (setq new (replace-match (match-string 0 line) t t new)))
  8366. (beginning-of-line (if arg 2 1))
  8367. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8368. (beginning-of-line 0)
  8369. (re-search-forward "| ?" (point-at-eol) t)
  8370. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8371. (org-table-align))
  8372. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8373. (defun org-table-insert-hline (&optional above)
  8374. "Insert a horizontal-line below the current line into the table.
  8375. With prefix ABOVE, insert above the current line."
  8376. (interactive "P")
  8377. (if (not (org-at-table-p))
  8378. (error "Not at a table"))
  8379. (let ((line (org-table-clean-line
  8380. (buffer-substring (point-at-bol) (point-at-eol))))
  8381. (col (current-column)))
  8382. (while (string-match "|\\( +\\)|" line)
  8383. (setq line (replace-match
  8384. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8385. ?-) "|") t t line)))
  8386. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8387. (beginning-of-line (if above 1 2))
  8388. (insert line "\n")
  8389. (beginning-of-line (if above 1 -1))
  8390. (move-to-column col)
  8391. (and org-table-overlay-coordinates (org-table-align))))
  8392. (defun org-table-hline-and-move (&optional same-column)
  8393. "Insert a hline and move to the row below that line."
  8394. (interactive "P")
  8395. (let ((col (org-table-current-column)))
  8396. (org-table-maybe-eval-formula)
  8397. (org-table-maybe-recalculate-line)
  8398. (org-table-insert-hline)
  8399. (end-of-line 2)
  8400. (if (looking-at "\n[ \t]*|-")
  8401. (progn (insert "\n|") (org-table-align))
  8402. (org-table-next-field))
  8403. (if same-column (org-table-goto-column col))))
  8404. (defun org-table-clean-line (s)
  8405. "Convert a table line S into a string with only \"|\" and space.
  8406. In particular, this does handle wide and invisible characters."
  8407. (if (string-match "^[ \t]*|-" s)
  8408. ;; It's a hline, just map the characters
  8409. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8410. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8411. (setq s (replace-match
  8412. (concat "|" (make-string (org-string-width (match-string 1 s))
  8413. ?\ ) "|")
  8414. t t s)))
  8415. s))
  8416. (defun org-table-kill-row ()
  8417. "Delete the current row or horizontal line from the table."
  8418. (interactive)
  8419. (if (not (org-at-table-p))
  8420. (error "Not at a table"))
  8421. (let ((col (current-column))
  8422. (dline (org-table-current-dline)))
  8423. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8424. (if (not (org-at-table-p)) (beginning-of-line 0))
  8425. (move-to-column col)
  8426. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8427. dline -1 dline)))
  8428. (defun org-table-sort-lines (with-case &optional sorting-type)
  8429. "Sort table lines according to the column at point.
  8430. The position of point indicates the column to be used for
  8431. sorting, and the range of lines is the range between the nearest
  8432. horizontal separator lines, or the entire table of no such lines
  8433. exist. If point is before the first column, you will be prompted
  8434. for the sorting column. If there is an active region, the mark
  8435. specifies the first line and the sorting column, while point
  8436. should be in the last line to be included into the sorting.
  8437. The command then prompts for the sorting type which can be
  8438. alphabetically, numerically, or by time (as given in a time stamp
  8439. in the field). Sorting in reverse order is also possible.
  8440. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8441. If SORTING-TYPE is specified when this function is called from a Lisp
  8442. program, no prompting will take place. SORTING-TYPE must be a character,
  8443. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8444. should be done in reverse order."
  8445. (interactive "P")
  8446. (let* ((thisline (org-current-line))
  8447. (thiscol (org-table-current-column))
  8448. beg end bcol ecol tend tbeg column lns pos)
  8449. (when (equal thiscol 0)
  8450. (if (interactive-p)
  8451. (setq thiscol
  8452. (string-to-number
  8453. (read-string "Use column N for sorting: ")))
  8454. (setq thiscol 1))
  8455. (org-table-goto-column thiscol))
  8456. (org-table-check-inside-data-field)
  8457. (if (org-region-active-p)
  8458. (progn
  8459. (setq beg (region-beginning) end (region-end))
  8460. (goto-char beg)
  8461. (setq column (org-table-current-column)
  8462. beg (point-at-bol))
  8463. (goto-char end)
  8464. (setq end (point-at-bol 2)))
  8465. (setq column (org-table-current-column)
  8466. pos (point)
  8467. tbeg (org-table-begin)
  8468. tend (org-table-end))
  8469. (if (re-search-backward org-table-hline-regexp tbeg t)
  8470. (setq beg (point-at-bol 2))
  8471. (goto-char tbeg)
  8472. (setq beg (point-at-bol 1)))
  8473. (goto-char pos)
  8474. (if (re-search-forward org-table-hline-regexp tend t)
  8475. (setq end (point-at-bol 1))
  8476. (goto-char tend)
  8477. (setq end (point-at-bol))))
  8478. (setq beg (move-marker (make-marker) beg)
  8479. end (move-marker (make-marker) end))
  8480. (untabify beg end)
  8481. (goto-char beg)
  8482. (org-table-goto-column column)
  8483. (skip-chars-backward "^|")
  8484. (setq bcol (current-column))
  8485. (org-table-goto-column (1+ column))
  8486. (skip-chars-backward "^|")
  8487. (setq ecol (1- (current-column)))
  8488. (org-table-goto-column column)
  8489. (setq lns (mapcar (lambda(x) (cons
  8490. (org-sort-remove-invisible
  8491. (nth (1- column)
  8492. (org-split-string x "[ \t]*|[ \t]*")))
  8493. x))
  8494. (org-split-string (buffer-substring beg end) "\n")))
  8495. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8496. (delete-region beg end)
  8497. (move-marker beg nil)
  8498. (move-marker end nil)
  8499. (insert (mapconcat 'cdr lns "\n") "\n")
  8500. (goto-line thisline)
  8501. (org-table-goto-column thiscol)
  8502. (message "%d lines sorted, based on column %d" (length lns) column)))
  8503. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8504. (defun org-sort-remove-invisible (s)
  8505. (remove-text-properties 0 (length s) org-rm-props s)
  8506. (while (string-match org-bracket-link-regexp s)
  8507. (setq s (replace-match (if (match-end 2)
  8508. (match-string 3 s)
  8509. (match-string 1 s)) t t s)))
  8510. s)
  8511. (defun org-table-cut-region (beg end)
  8512. "Copy region in table to the clipboard and blank all relevant fields."
  8513. (interactive "r")
  8514. (org-table-copy-region beg end 'cut))
  8515. (defun org-table-copy-region (beg end &optional cut)
  8516. "Copy rectangular region in table to clipboard.
  8517. A special clipboard is used which can only be accessed
  8518. with `org-table-paste-rectangle'."
  8519. (interactive "rP")
  8520. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8521. region cols
  8522. (rpl (if cut " " nil)))
  8523. (goto-char beg)
  8524. (org-table-check-inside-data-field)
  8525. (setq l01 (org-current-line)
  8526. c01 (org-table-current-column))
  8527. (goto-char end)
  8528. (org-table-check-inside-data-field)
  8529. (setq l02 (org-current-line)
  8530. c02 (org-table-current-column))
  8531. (setq l1 (min l01 l02) l2 (max l01 l02)
  8532. c1 (min c01 c02) c2 (max c01 c02))
  8533. (catch 'exit
  8534. (while t
  8535. (catch 'nextline
  8536. (if (> l1 l2) (throw 'exit t))
  8537. (goto-line l1)
  8538. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8539. (setq cols nil ic1 c1 ic2 c2)
  8540. (while (< ic1 (1+ ic2))
  8541. (push (org-table-get-field ic1 rpl) cols)
  8542. (setq ic1 (1+ ic1)))
  8543. (push (nreverse cols) region)
  8544. (setq l1 (1+ l1)))))
  8545. (setq org-table-clip (nreverse region))
  8546. (if cut (org-table-align))
  8547. org-table-clip))
  8548. (defun org-table-paste-rectangle ()
  8549. "Paste a rectangular region into a table.
  8550. The upper right corner ends up in the current field. All involved fields
  8551. will be overwritten. If the rectangle does not fit into the present table,
  8552. the table is enlarged as needed. The process ignores horizontal separator
  8553. lines."
  8554. (interactive)
  8555. (unless (and org-table-clip (listp org-table-clip))
  8556. (error "First cut/copy a region to paste!"))
  8557. (org-table-check-inside-data-field)
  8558. (let* ((clip org-table-clip)
  8559. (line (org-current-line))
  8560. (col (org-table-current-column))
  8561. (org-enable-table-editor t)
  8562. (org-table-automatic-realign nil)
  8563. c cols field)
  8564. (while (setq cols (pop clip))
  8565. (while (org-at-table-hline-p) (beginning-of-line 2))
  8566. (if (not (org-at-table-p))
  8567. (progn (end-of-line 0) (org-table-next-field)))
  8568. (setq c col)
  8569. (while (setq field (pop cols))
  8570. (org-table-goto-column c nil 'force)
  8571. (org-table-get-field nil field)
  8572. (setq c (1+ c)))
  8573. (beginning-of-line 2))
  8574. (goto-line line)
  8575. (org-table-goto-column col)
  8576. (org-table-align)))
  8577. (defun org-table-convert ()
  8578. "Convert from `org-mode' table to table.el and back.
  8579. Obviously, this only works within limits. When an Org-mode table is
  8580. converted to table.el, all horizontal separator lines get lost, because
  8581. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8582. A table.el table can be converted to an Org-mode table only if it does not
  8583. do row or column spanning. Multiline cells will become multiple cells.
  8584. Beware, Org-mode does not test if the table can be successfully converted - it
  8585. blindly applies a recipe that works for simple tables."
  8586. (interactive)
  8587. (require 'table)
  8588. (if (org-at-table.el-p)
  8589. ;; convert to Org-mode table
  8590. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8591. (end (move-marker (make-marker) (org-table-end t))))
  8592. (table-unrecognize-region beg end)
  8593. (goto-char beg)
  8594. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8595. (replace-match ""))
  8596. (goto-char beg))
  8597. (if (org-at-table-p)
  8598. ;; convert to table.el table
  8599. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8600. (end (move-marker (make-marker) (org-table-end))))
  8601. ;; first, get rid of all horizontal lines
  8602. (goto-char beg)
  8603. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8604. (replace-match ""))
  8605. ;; insert a hline before first
  8606. (goto-char beg)
  8607. (org-table-insert-hline 'above)
  8608. (beginning-of-line -1)
  8609. ;; insert a hline after each line
  8610. (while (progn (beginning-of-line 3) (< (point) end))
  8611. (org-table-insert-hline))
  8612. (goto-char beg)
  8613. (setq end (move-marker end (org-table-end)))
  8614. ;; replace "+" at beginning and ending of hlines
  8615. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8616. (replace-match "\\1+-"))
  8617. (goto-char beg)
  8618. (while (re-search-forward "-|[ \t]*$" end t)
  8619. (replace-match "-+"))
  8620. (goto-char beg)))))
  8621. (defun org-table-wrap-region (arg)
  8622. "Wrap several fields in a column like a paragraph.
  8623. This is useful if you'd like to spread the contents of a field over several
  8624. lines, in order to keep the table compact.
  8625. If there is an active region, and both point and mark are in the same column,
  8626. the text in the column is wrapped to minimum width for the given number of
  8627. lines. Generally, this makes the table more compact. A prefix ARG may be
  8628. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8629. formats the selected text to two lines. If the region was longer than two
  8630. lines, the remaining lines remain empty. A negative prefix argument reduces
  8631. the current number of lines by that amount. The wrapped text is pasted back
  8632. into the table. If you formatted it to more lines than it was before, fields
  8633. further down in the table get overwritten - so you might need to make space in
  8634. the table first.
  8635. If there is no region, the current field is split at the cursor position and
  8636. the text fragment to the right of the cursor is prepended to the field one
  8637. line down.
  8638. If there is no region, but you specify a prefix ARG, the current field gets
  8639. blank, and the content is appended to the field above."
  8640. (interactive "P")
  8641. (org-table-check-inside-data-field)
  8642. (if (org-region-active-p)
  8643. ;; There is a region: fill as a paragraph
  8644. (let* ((beg (region-beginning))
  8645. (cline (save-excursion (goto-char beg) (org-current-line)))
  8646. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8647. nlines)
  8648. (org-table-cut-region (region-beginning) (region-end))
  8649. (if (> (length (car org-table-clip)) 1)
  8650. (error "Region must be limited to single column"))
  8651. (setq nlines (if arg
  8652. (if (< arg 1)
  8653. (+ (length org-table-clip) arg)
  8654. arg)
  8655. (length org-table-clip)))
  8656. (setq org-table-clip
  8657. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8658. nil nlines)))
  8659. (goto-line cline)
  8660. (org-table-goto-column ccol)
  8661. (org-table-paste-rectangle))
  8662. ;; No region, split the current field at point
  8663. (unless (org-get-alist-option org-M-RET-may-split-line 'table)
  8664. (skip-chars-forward "^\r\n|"))
  8665. (if arg
  8666. ;; combine with field above
  8667. (let ((s (org-table-blank-field))
  8668. (col (org-table-current-column)))
  8669. (beginning-of-line 0)
  8670. (while (org-at-table-hline-p) (beginning-of-line 0))
  8671. (org-table-goto-column col)
  8672. (skip-chars-forward "^|")
  8673. (skip-chars-backward " ")
  8674. (insert " " (org-trim s))
  8675. (org-table-align))
  8676. ;; split field
  8677. (if (looking-at "\\([^|]+\\)+|")
  8678. (let ((s (match-string 1)))
  8679. (replace-match " |")
  8680. (goto-char (match-beginning 0))
  8681. (org-table-next-row)
  8682. (insert (org-trim s) " ")
  8683. (org-table-align))
  8684. (org-table-next-row)))))
  8685. (defvar org-field-marker nil)
  8686. (defun org-table-edit-field (arg)
  8687. "Edit table field in a different window.
  8688. This is mainly useful for fields that contain hidden parts.
  8689. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8690. it can be edited in place."
  8691. (interactive "P")
  8692. (if arg
  8693. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8694. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8695. (remove-text-properties b e '(org-cwidth t invisible t
  8696. display t intangible t))
  8697. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8698. (font-lock-fontify-block)))
  8699. (let ((pos (move-marker (make-marker) (point)))
  8700. (field (org-table-get-field))
  8701. (cw (current-window-configuration))
  8702. p)
  8703. (org-switch-to-buffer-other-window "*Org tmp*")
  8704. (erase-buffer)
  8705. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8706. (let ((org-inhibit-startup t)) (org-mode))
  8707. (goto-char (setq p (point-max)))
  8708. (insert (org-trim field))
  8709. (remove-text-properties p (point-max)
  8710. '(invisible t org-cwidth t display t
  8711. intangible t))
  8712. (goto-char p)
  8713. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8714. (org-set-local 'org-window-configuration cw)
  8715. (org-set-local 'org-field-marker pos)
  8716. (message "Edit and finish with C-c C-c"))))
  8717. (defun org-table-finish-edit-field ()
  8718. "Finish editing a table data field.
  8719. Remove all newline characters, insert the result into the table, realign
  8720. the table and kill the editing buffer."
  8721. (let ((pos org-field-marker)
  8722. (cw org-window-configuration)
  8723. (cb (current-buffer))
  8724. text)
  8725. (goto-char (point-min))
  8726. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8727. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8728. (replace-match " "))
  8729. (setq text (org-trim (buffer-string)))
  8730. (set-window-configuration cw)
  8731. (kill-buffer cb)
  8732. (select-window (get-buffer-window (marker-buffer pos)))
  8733. (goto-char pos)
  8734. (move-marker pos nil)
  8735. (org-table-check-inside-data-field)
  8736. (org-table-get-field nil text)
  8737. (org-table-align)
  8738. (message "New field value inserted")))
  8739. (defun org-trim (s)
  8740. "Remove whitespace at beginning and end of string."
  8741. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8742. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8743. s)
  8744. (defun org-wrap (string &optional width lines)
  8745. "Wrap string to either a number of lines, or a width in characters.
  8746. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8747. that costs. If there is a word longer than WIDTH, the text is actually
  8748. wrapped to the length of that word.
  8749. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8750. many lines, whatever width that takes.
  8751. The return value is a list of lines, without newlines at the end."
  8752. (let* ((words (org-split-string string "[ \t\n]+"))
  8753. (maxword (apply 'max (mapcar 'org-string-width words)))
  8754. w ll)
  8755. (cond (width
  8756. (org-do-wrap words (max maxword width)))
  8757. (lines
  8758. (setq w maxword)
  8759. (setq ll (org-do-wrap words maxword))
  8760. (if (<= (length ll) lines)
  8761. ll
  8762. (setq ll words)
  8763. (while (> (length ll) lines)
  8764. (setq w (1+ w))
  8765. (setq ll (org-do-wrap words w)))
  8766. ll))
  8767. (t (error "Cannot wrap this")))))
  8768. (defun org-do-wrap (words width)
  8769. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8770. (let (lines line)
  8771. (while words
  8772. (setq line (pop words))
  8773. (while (and words (< (+ (length line) (length (car words))) width))
  8774. (setq line (concat line " " (pop words))))
  8775. (setq lines (push line lines)))
  8776. (nreverse lines)))
  8777. (defun org-split-string (string &optional separators)
  8778. "Splits STRING into substrings at SEPARATORS.
  8779. No empty strings are returned if there are matches at the beginning
  8780. and end of string."
  8781. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8782. (start 0)
  8783. notfirst
  8784. (list nil))
  8785. (while (and (string-match rexp string
  8786. (if (and notfirst
  8787. (= start (match-beginning 0))
  8788. (< start (length string)))
  8789. (1+ start) start))
  8790. (< (match-beginning 0) (length string)))
  8791. (setq notfirst t)
  8792. (or (eq (match-beginning 0) 0)
  8793. (and (eq (match-beginning 0) (match-end 0))
  8794. (eq (match-beginning 0) start))
  8795. (setq list
  8796. (cons (substring string start (match-beginning 0))
  8797. list)))
  8798. (setq start (match-end 0)))
  8799. (or (eq start (length string))
  8800. (setq list
  8801. (cons (substring string start)
  8802. list)))
  8803. (nreverse list)))
  8804. (defun org-table-map-tables (function)
  8805. "Apply FUNCTION to the start of all tables in the buffer."
  8806. (save-excursion
  8807. (save-restriction
  8808. (widen)
  8809. (goto-char (point-min))
  8810. (while (re-search-forward org-table-any-line-regexp nil t)
  8811. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8812. (beginning-of-line 1)
  8813. (if (looking-at org-table-line-regexp)
  8814. (save-excursion (funcall function)))
  8815. (re-search-forward org-table-any-border-regexp nil 1))))
  8816. (message "Mapping tables: done"))
  8817. (defvar org-timecnt) ; dynamically scoped parameter
  8818. (defun org-table-sum (&optional beg end nlast)
  8819. "Sum numbers in region of current table column.
  8820. The result will be displayed in the echo area, and will be available
  8821. as kill to be inserted with \\[yank].
  8822. If there is an active region, it is interpreted as a rectangle and all
  8823. numbers in that rectangle will be summed. If there is no active
  8824. region and point is located in a table column, sum all numbers in that
  8825. column.
  8826. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8827. numbers are assumed to be times as well (in decimal hours) and the
  8828. numbers are added as such.
  8829. If NLAST is a number, only the NLAST fields will actually be summed."
  8830. (interactive)
  8831. (save-excursion
  8832. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8833. (cond
  8834. ((and beg end)) ; beg and end given explicitly
  8835. ((org-region-active-p)
  8836. (setq beg (region-beginning) end (region-end)))
  8837. (t
  8838. (setq col (org-table-current-column))
  8839. (goto-char (org-table-begin))
  8840. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8841. (error "No table data"))
  8842. (org-table-goto-column col)
  8843. (setq beg (point))
  8844. (goto-char (org-table-end))
  8845. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8846. (error "No table data"))
  8847. (org-table-goto-column col)
  8848. (setq end (point))))
  8849. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8850. (items1 (cond ((not nlast) items)
  8851. ((>= nlast (length items)) items)
  8852. (t (setq items (reverse items))
  8853. (setcdr (nthcdr (1- nlast) items) nil)
  8854. (nreverse items))))
  8855. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8856. items1)))
  8857. (res (apply '+ numbers))
  8858. (sres (if (= org-timecnt 0)
  8859. (format "%g" res)
  8860. (setq diff (* 3600 res)
  8861. h (floor (/ diff 3600)) diff (mod diff 3600)
  8862. m (floor (/ diff 60)) diff (mod diff 60)
  8863. s diff)
  8864. (format "%d:%02d:%02d" h m s))))
  8865. (kill-new sres)
  8866. (if (interactive-p)
  8867. (message "%s"
  8868. (substitute-command-keys
  8869. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8870. (length numbers) sres))))
  8871. sres))))
  8872. (defun org-table-get-number-for-summing (s)
  8873. (let (n)
  8874. (if (string-match "^ *|? *" s)
  8875. (setq s (replace-match "" nil nil s)))
  8876. (if (string-match " *|? *$" s)
  8877. (setq s (replace-match "" nil nil s)))
  8878. (setq n (string-to-number s))
  8879. (cond
  8880. ((and (string-match "0" s)
  8881. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8882. ((string-match "\\`[ \t]+\\'" s) nil)
  8883. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8884. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8885. (m (string-to-number (or (match-string 2 s) "0")))
  8886. (s (string-to-number (or (match-string 4 s) "0"))))
  8887. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8888. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8889. ((equal n 0) nil)
  8890. (t n))))
  8891. (defun org-table-current-field-formula (&optional key noerror)
  8892. "Return the formula active for the current field.
  8893. Assumes that specials are in place.
  8894. If KEY is given, return the key to this formula.
  8895. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8896. (let* ((name (car (rassoc (list (org-current-line)
  8897. (org-table-current-column))
  8898. org-table-named-field-locations)))
  8899. (col (org-table-current-column))
  8900. (scol (int-to-string col))
  8901. (ref (format "@%d$%d" (org-table-current-dline) col))
  8902. (stored-list (org-table-get-stored-formulas noerror))
  8903. (ass (or (assoc name stored-list)
  8904. (assoc ref stored-list)
  8905. (assoc scol stored-list))))
  8906. (if key
  8907. (car ass)
  8908. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8909. (cdr ass))))))
  8910. (defun org-table-get-formula (&optional equation named)
  8911. "Read a formula from the minibuffer, offer stored formula as default.
  8912. When NAMED is non-nil, look for a named equation."
  8913. (let* ((stored-list (org-table-get-stored-formulas))
  8914. (name (car (rassoc (list (org-current-line)
  8915. (org-table-current-column))
  8916. org-table-named-field-locations)))
  8917. (ref (format "@%d$%d" (org-table-current-dline)
  8918. (org-table-current-column)))
  8919. (refass (assoc ref stored-list))
  8920. (scol (if named
  8921. (if name name ref)
  8922. (int-to-string (org-table-current-column))))
  8923. (dummy (and (or name refass) (not named)
  8924. (not (y-or-n-p "Replace field formula with column formula? " ))
  8925. (error "Abort")))
  8926. (name (or name ref))
  8927. (org-table-may-need-update nil)
  8928. (stored (cdr (assoc scol stored-list)))
  8929. (eq (cond
  8930. ((and stored equation (string-match "^ *=? *$" equation))
  8931. stored)
  8932. ((stringp equation)
  8933. equation)
  8934. (t (org-table-formula-from-user
  8935. (read-string
  8936. (org-table-formula-to-user
  8937. (format "%s formula %s%s="
  8938. (if named "Field" "Column")
  8939. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8940. scol))
  8941. (if stored (org-table-formula-to-user stored) "")
  8942. 'org-table-formula-history
  8943. )))))
  8944. mustsave)
  8945. (when (not (string-match "\\S-" eq))
  8946. ;; remove formula
  8947. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8948. (org-table-store-formulas stored-list)
  8949. (error "Formula removed"))
  8950. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8951. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8952. (if (and name (not named))
  8953. ;; We set the column equation, delete the named one.
  8954. (setq stored-list (delq (assoc name stored-list) stored-list)
  8955. mustsave t))
  8956. (if stored
  8957. (setcdr (assoc scol stored-list) eq)
  8958. (setq stored-list (cons (cons scol eq) stored-list)))
  8959. (if (or mustsave (not (equal stored eq)))
  8960. (org-table-store-formulas stored-list))
  8961. eq))
  8962. (defun org-table-store-formulas (alist)
  8963. "Store the list of formulas below the current table."
  8964. (setq alist (sort alist 'org-table-formula-less-p))
  8965. (save-excursion
  8966. (goto-char (org-table-end))
  8967. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8968. (progn
  8969. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8970. (goto-char (match-beginning 2))
  8971. (delete-region (match-beginning 2) (match-end 0)))
  8972. (insert "#+TBLFM:"))
  8973. (insert " "
  8974. (mapconcat (lambda (x)
  8975. (concat
  8976. (if (equal (string-to-char (car x)) ?@) "" "$")
  8977. (car x) "=" (cdr x)))
  8978. alist "::")
  8979. "\n")))
  8980. (defsubst org-table-formula-make-cmp-string (a)
  8981. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8982. (concat
  8983. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8984. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8985. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8986. (defun org-table-formula-less-p (a b)
  8987. "Compare two formulas for sorting."
  8988. (let ((as (org-table-formula-make-cmp-string (car a)))
  8989. (bs (org-table-formula-make-cmp-string (car b))))
  8990. (and as bs (string< as bs))))
  8991. (defun org-table-get-stored-formulas (&optional noerror)
  8992. "Return an alist with the stored formulas directly after current table."
  8993. (interactive)
  8994. (let (scol eq eq-alist strings string seen)
  8995. (save-excursion
  8996. (goto-char (org-table-end))
  8997. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8998. (setq strings (org-split-string (match-string 2) " *:: *"))
  8999. (while (setq string (pop strings))
  9000. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  9001. (setq scol (if (match-end 2)
  9002. (match-string 2 string)
  9003. (match-string 1 string))
  9004. eq (match-string 3 string)
  9005. eq-alist (cons (cons scol eq) eq-alist))
  9006. (if (member scol seen)
  9007. (if noerror
  9008. (progn
  9009. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  9010. (ding)
  9011. (sit-for 2))
  9012. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  9013. (push scol seen))))))
  9014. (nreverse eq-alist)))
  9015. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  9016. "Modify the equations after the table structure has been edited.
  9017. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  9018. For all numbers larger than LIMIT, shift them by DELTA."
  9019. (save-excursion
  9020. (goto-char (org-table-end))
  9021. (when (looking-at "#\\+TBLFM:")
  9022. (let ((re (concat key "\\([0-9]+\\)"))
  9023. (re2
  9024. (when remove
  9025. (if (equal key "$")
  9026. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  9027. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  9028. s n a)
  9029. (when remove
  9030. (while (re-search-forward re2 (point-at-eol) t)
  9031. (replace-match "")))
  9032. (while (re-search-forward re (point-at-eol) t)
  9033. (setq s (match-string 1) n (string-to-number s))
  9034. (cond
  9035. ((setq a (assoc s replace))
  9036. (replace-match (concat key (cdr a)) t t))
  9037. ((and limit (> n limit))
  9038. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  9039. (defun org-table-get-specials ()
  9040. "Get the column names and local parameters for this table."
  9041. (save-excursion
  9042. (let ((beg (org-table-begin)) (end (org-table-end))
  9043. names name fields fields1 field cnt
  9044. c v l line col types dlines hlines)
  9045. (setq org-table-column-names nil
  9046. org-table-local-parameters nil
  9047. org-table-named-field-locations nil
  9048. org-table-current-begin-line nil
  9049. org-table-current-begin-pos nil
  9050. org-table-current-line-types nil)
  9051. (goto-char beg)
  9052. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  9053. (setq names (org-split-string (match-string 1) " *| *")
  9054. cnt 1)
  9055. (while (setq name (pop names))
  9056. (setq cnt (1+ cnt))
  9057. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  9058. (push (cons name (int-to-string cnt)) org-table-column-names))))
  9059. (setq org-table-column-names (nreverse org-table-column-names))
  9060. (setq org-table-column-name-regexp
  9061. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  9062. (goto-char beg)
  9063. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  9064. (setq fields (org-split-string (match-string 1) " *| *"))
  9065. (while (setq field (pop fields))
  9066. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  9067. (push (cons (match-string 1 field) (match-string 2 field))
  9068. org-table-local-parameters))))
  9069. (goto-char beg)
  9070. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  9071. (setq c (match-string 1)
  9072. fields (org-split-string (match-string 2) " *| *"))
  9073. (save-excursion
  9074. (beginning-of-line (if (equal c "_") 2 0))
  9075. (setq line (org-current-line) col 1)
  9076. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  9077. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  9078. (while (and fields1 (setq field (pop fields)))
  9079. (setq v (pop fields1) col (1+ col))
  9080. (when (and (stringp field) (stringp v)
  9081. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  9082. (push (cons field v) org-table-local-parameters)
  9083. (push (list field line col) org-table-named-field-locations))))
  9084. ;; Analyse the line types
  9085. (goto-char beg)
  9086. (setq org-table-current-begin-line (org-current-line)
  9087. org-table-current-begin-pos (point)
  9088. l org-table-current-begin-line)
  9089. (while (looking-at "[ \t]*|\\(-\\)?")
  9090. (push (if (match-end 1) 'hline 'dline) types)
  9091. (if (match-end 1) (push l hlines) (push l dlines))
  9092. (beginning-of-line 2)
  9093. (setq l (1+ l)))
  9094. (setq org-table-current-line-types (apply 'vector (nreverse types))
  9095. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  9096. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  9097. (defun org-table-maybe-eval-formula ()
  9098. "Check if the current field starts with \"=\" or \":=\".
  9099. If yes, store the formula and apply it."
  9100. ;; We already know we are in a table. Get field will only return a formula
  9101. ;; when appropriate. It might return a separator line, but no problem.
  9102. (when org-table-formula-evaluate-inline
  9103. (let* ((field (org-trim (or (org-table-get-field) "")))
  9104. named eq)
  9105. (when (string-match "^:?=\\(.*\\)" field)
  9106. (setq named (equal (string-to-char field) ?:)
  9107. eq (match-string 1 field))
  9108. (if (or (fboundp 'calc-eval)
  9109. (equal (substring eq 0 (min 2 (length eq))) "'("))
  9110. (org-table-eval-formula (if named '(4) nil)
  9111. (org-table-formula-from-user eq))
  9112. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  9113. (defvar org-recalc-commands nil
  9114. "List of commands triggering the recalculation of a line.
  9115. Will be filled automatically during use.")
  9116. (defvar org-recalc-marks
  9117. '((" " . "Unmarked: no special line, no automatic recalculation")
  9118. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9119. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9120. ("!" . "Column name definition line. Reference in formula as $name.")
  9121. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9122. ("_" . "Names for values in row below this one.")
  9123. ("^" . "Names for values in row above this one.")))
  9124. (defun org-table-rotate-recalc-marks (&optional newchar)
  9125. "Rotate the recalculation mark in the first column.
  9126. If in any row, the first field is not consistent with a mark,
  9127. insert a new column for the markers.
  9128. When there is an active region, change all the lines in the region,
  9129. after prompting for the marking character.
  9130. After each change, a message will be displayed indicating the meaning
  9131. of the new mark."
  9132. (interactive)
  9133. (unless (org-at-table-p) (error "Not at a table"))
  9134. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9135. (beg (org-table-begin))
  9136. (end (org-table-end))
  9137. (l (org-current-line))
  9138. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9139. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9140. (have-col
  9141. (save-excursion
  9142. (goto-char beg)
  9143. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9144. (col (org-table-current-column))
  9145. (forcenew (car (assoc newchar org-recalc-marks)))
  9146. epos new)
  9147. (when l1
  9148. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9149. (setq newchar (char-to-string (read-char-exclusive))
  9150. forcenew (car (assoc newchar org-recalc-marks))))
  9151. (if (and newchar (not forcenew))
  9152. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9153. newchar))
  9154. (if l1 (goto-line l1))
  9155. (save-excursion
  9156. (beginning-of-line 1)
  9157. (unless (looking-at org-table-dataline-regexp)
  9158. (error "Not at a table data line")))
  9159. (unless have-col
  9160. (org-table-goto-column 1)
  9161. (org-table-insert-column)
  9162. (org-table-goto-column (1+ col)))
  9163. (setq epos (point-at-eol))
  9164. (save-excursion
  9165. (beginning-of-line 1)
  9166. (org-table-get-field
  9167. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9168. (concat " "
  9169. (setq new (or forcenew
  9170. (cadr (member (match-string 1) marks))))
  9171. " ")
  9172. " # ")))
  9173. (if (and l1 l2)
  9174. (progn
  9175. (goto-line l1)
  9176. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9177. (and (looking-at org-table-dataline-regexp)
  9178. (org-table-get-field 1 (concat " " new " "))))
  9179. (goto-line l1)))
  9180. (if (not (= epos (point-at-eol))) (org-table-align))
  9181. (goto-line l)
  9182. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  9183. (defun org-table-maybe-recalculate-line ()
  9184. "Recompute the current line if marked for it, and if we haven't just done it."
  9185. (interactive)
  9186. (and org-table-allow-automatic-line-recalculation
  9187. (not (and (memq last-command org-recalc-commands)
  9188. (equal org-last-recalc-line (org-current-line))))
  9189. (save-excursion (beginning-of-line 1)
  9190. (looking-at org-table-auto-recalculate-regexp))
  9191. (org-table-recalculate) t))
  9192. (defvar org-table-formula-debug nil
  9193. "Non-nil means, debug table formulas.
  9194. When nil, simply write \"#ERROR\" in corrupted fields.")
  9195. (make-variable-buffer-local 'org-table-formula-debug)
  9196. (defvar modes)
  9197. (defsubst org-set-calc-mode (var &optional value)
  9198. (if (stringp var)
  9199. (setq var (assoc var '(("D" calc-angle-mode deg)
  9200. ("R" calc-angle-mode rad)
  9201. ("F" calc-prefer-frac t)
  9202. ("S" calc-symbolic-mode t)))
  9203. value (nth 2 var) var (nth 1 var)))
  9204. (if (memq var modes)
  9205. (setcar (cdr (memq var modes)) value)
  9206. (cons var (cons value modes)))
  9207. modes)
  9208. (defun org-table-eval-formula (&optional arg equation
  9209. suppress-align suppress-const
  9210. suppress-store suppress-analysis)
  9211. "Replace the table field value at the cursor by the result of a calculation.
  9212. This function makes use of Dave Gillespie's Calc package, in my view the
  9213. most exciting program ever written for GNU Emacs. So you need to have Calc
  9214. installed in order to use this function.
  9215. In a table, this command replaces the value in the current field with the
  9216. result of a formula. It also installs the formula as the \"current\" column
  9217. formula, by storing it in a special line below the table. When called
  9218. with a `C-u' prefix, the current field must ba a named field, and the
  9219. formula is installed as valid in only this specific field.
  9220. When called with two `C-u' prefixes, insert the active equation
  9221. for the field back into the current field, so that it can be
  9222. edited there. This is useful in order to use \\[org-table-show-reference]
  9223. to check the referenced fields.
  9224. When called, the command first prompts for a formula, which is read in
  9225. the minibuffer. Previously entered formulas are available through the
  9226. history list, and the last used formula is offered as a default.
  9227. These stored formulas are adapted correctly when moving, inserting, or
  9228. deleting columns with the corresponding commands.
  9229. The formula can be any algebraic expression understood by the Calc package.
  9230. For details, see the Org-mode manual.
  9231. This function can also be called from Lisp programs and offers
  9232. additional arguments: EQUATION can be the formula to apply. If this
  9233. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9234. used to speed-up recursive calls by by-passing unnecessary aligns.
  9235. SUPPRESS-CONST suppresses the interpretation of constants in the
  9236. formula, assuming that this has been done already outside the function.
  9237. SUPPRESS-STORE means the formula should not be stored, either because
  9238. it is already stored, or because it is a modified equation that should
  9239. not overwrite the stored one."
  9240. (interactive "P")
  9241. (org-table-check-inside-data-field)
  9242. (or suppress-analysis (org-table-get-specials))
  9243. (if (equal arg '(16))
  9244. (let ((eq (org-table-current-field-formula)))
  9245. (or eq (error "No equation active for current field"))
  9246. (org-table-get-field nil eq)
  9247. (org-table-align)
  9248. (setq org-table-may-need-update t))
  9249. (let* (fields
  9250. (ndown (if (integerp arg) arg 1))
  9251. (org-table-automatic-realign nil)
  9252. (case-fold-search nil)
  9253. (down (> ndown 1))
  9254. (formula (if (and equation suppress-store)
  9255. equation
  9256. (org-table-get-formula equation (equal arg '(4)))))
  9257. (n0 (org-table-current-column))
  9258. (modes (copy-sequence org-calc-default-modes))
  9259. (numbers nil) ; was a variable, now fixed default
  9260. (keep-empty nil)
  9261. n form form0 bw fmt x ev orig c lispp literal)
  9262. ;; Parse the format string. Since we have a lot of modes, this is
  9263. ;; a lot of work. However, I think calc still uses most of the time.
  9264. (if (string-match ";" formula)
  9265. (let ((tmp (org-split-string formula ";")))
  9266. (setq formula (car tmp)
  9267. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9268. (nth 1 tmp)))
  9269. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9270. (setq c (string-to-char (match-string 1 fmt))
  9271. n (string-to-number (match-string 2 fmt)))
  9272. (if (= c ?p)
  9273. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9274. (setq modes (org-set-calc-mode
  9275. 'calc-float-format
  9276. (list (cdr (assoc c '((?n . float) (?f . fix)
  9277. (?s . sci) (?e . eng))))
  9278. n))))
  9279. (setq fmt (replace-match "" t t fmt)))
  9280. (if (string-match "[NT]" fmt)
  9281. (setq numbers (equal (match-string 0 fmt) "N")
  9282. fmt (replace-match "" t t fmt)))
  9283. (if (string-match "L" fmt)
  9284. (setq literal t
  9285. fmt (replace-match "" t t fmt)))
  9286. (if (string-match "E" fmt)
  9287. (setq keep-empty t
  9288. fmt (replace-match "" t t fmt)))
  9289. (while (string-match "[DRFS]" fmt)
  9290. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9291. (setq fmt (replace-match "" t t fmt)))
  9292. (unless (string-match "\\S-" fmt)
  9293. (setq fmt nil))))
  9294. (if (and (not suppress-const) org-table-formula-use-constants)
  9295. (setq formula (org-table-formula-substitute-names formula)))
  9296. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9297. (while (> ndown 0)
  9298. (setq fields (org-split-string
  9299. (org-no-properties
  9300. (buffer-substring (point-at-bol) (point-at-eol)))
  9301. " *| *"))
  9302. (if (eq numbers t)
  9303. (setq fields (mapcar
  9304. (lambda (x) (number-to-string (string-to-number x)))
  9305. fields)))
  9306. (setq ndown (1- ndown))
  9307. (setq form (copy-sequence formula)
  9308. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9309. (if (and lispp literal) (setq lispp 'literal))
  9310. ;; Check for old vertical references
  9311. (setq form (org-rewrite-old-row-references form))
  9312. ;; Insert complex ranges
  9313. (while (string-match org-table-range-regexp form)
  9314. (setq form
  9315. (replace-match
  9316. (save-match-data
  9317. (org-table-make-reference
  9318. (org-table-get-range (match-string 0 form) nil n0)
  9319. keep-empty numbers lispp))
  9320. t t form)))
  9321. ;; Insert simple ranges
  9322. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9323. (setq form
  9324. (replace-match
  9325. (save-match-data
  9326. (org-table-make-reference
  9327. (org-sublist
  9328. fields (string-to-number (match-string 1 form))
  9329. (string-to-number (match-string 2 form)))
  9330. keep-empty numbers lispp))
  9331. t t form)))
  9332. (setq form0 form)
  9333. ;; Insert the references to fields in same row
  9334. (while (string-match "\\$\\([0-9]+\\)" form)
  9335. (setq n (string-to-number (match-string 1 form))
  9336. x (nth (1- (if (= n 0) n0 n)) fields))
  9337. (unless x (error "Invalid field specifier \"%s\""
  9338. (match-string 0 form)))
  9339. (setq form (replace-match
  9340. (save-match-data
  9341. (org-table-make-reference x nil numbers lispp))
  9342. t t form)))
  9343. (if lispp
  9344. (setq ev (condition-case nil
  9345. (eval (eval (read form)))
  9346. (error "#ERROR"))
  9347. ev (if (numberp ev) (number-to-string ev) ev))
  9348. (or (fboundp 'calc-eval)
  9349. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9350. (setq ev (calc-eval (cons form modes)
  9351. (if numbers 'num))))
  9352. (when org-table-formula-debug
  9353. (with-output-to-temp-buffer "*Substitution History*"
  9354. (princ (format "Substitution history of formula
  9355. Orig: %s
  9356. $xyz-> %s
  9357. @r$c-> %s
  9358. $1-> %s\n" orig formula form0 form))
  9359. (if (listp ev)
  9360. (princ (format " %s^\nError: %s"
  9361. (make-string (car ev) ?\-) (nth 1 ev)))
  9362. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9363. ev (or fmt "NONE")
  9364. (if fmt (format fmt (string-to-number ev)) ev)))))
  9365. (setq bw (get-buffer-window "*Substitution History*"))
  9366. (shrink-window-if-larger-than-buffer bw)
  9367. (unless (and (interactive-p) (not ndown))
  9368. (unless (let (inhibit-redisplay)
  9369. (y-or-n-p "Debugging Formula. Continue to next? "))
  9370. (org-table-align)
  9371. (error "Abort"))
  9372. (delete-window bw)
  9373. (message "")))
  9374. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9375. (org-table-justify-field-maybe
  9376. (if fmt (format fmt (string-to-number ev)) ev))
  9377. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9378. (call-interactively 'org-return)
  9379. (setq ndown 0)))
  9380. (and down (org-table-maybe-recalculate-line))
  9381. (or suppress-align (and org-table-may-need-update
  9382. (org-table-align))))))
  9383. (defun org-table-put-field-property (prop value)
  9384. (save-excursion
  9385. (put-text-property (progn (skip-chars-backward "^|") (point))
  9386. (progn (skip-chars-forward "^|") (point))
  9387. prop value)))
  9388. (defun org-table-get-range (desc &optional tbeg col highlight)
  9389. "Get a calc vector from a column, accorting to descriptor DESC.
  9390. Optional arguments TBEG and COL can give the beginning of the table and
  9391. the current column, to avoid unnecessary parsing.
  9392. HIGHLIGHT means, just highlight the range."
  9393. (if (not (equal (string-to-char desc) ?@))
  9394. (setq desc (concat "@" desc)))
  9395. (save-excursion
  9396. (or tbeg (setq tbeg (org-table-begin)))
  9397. (or col (setq col (org-table-current-column)))
  9398. (let ((thisline (org-current-line))
  9399. beg end c1 c2 r1 r2 rangep tmp)
  9400. (unless (string-match org-table-range-regexp desc)
  9401. (error "Invalid table range specifier `%s'" desc))
  9402. (setq rangep (match-end 3)
  9403. r1 (and (match-end 1) (match-string 1 desc))
  9404. r2 (and (match-end 4) (match-string 4 desc))
  9405. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9406. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9407. (and c1 (setq c1 (+ (string-to-number c1)
  9408. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9409. (and c2 (setq c2 (+ (string-to-number c2)
  9410. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9411. (if (equal r1 "") (setq r1 nil))
  9412. (if (equal r2 "") (setq r2 nil))
  9413. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9414. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9415. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9416. (if (not r1) (setq r1 thisline))
  9417. (if (not r2) (setq r2 thisline))
  9418. (if (not c1) (setq c1 col))
  9419. (if (not c2) (setq c2 col))
  9420. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9421. ;; just one field
  9422. (progn
  9423. (goto-line r1)
  9424. (while (not (looking-at org-table-dataline-regexp))
  9425. (beginning-of-line 2))
  9426. (prog1 (org-trim (org-table-get-field c1))
  9427. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9428. ;; A range, return a vector
  9429. ;; First sort the numbers to get a regular ractangle
  9430. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9431. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9432. (goto-line r1)
  9433. (while (not (looking-at org-table-dataline-regexp))
  9434. (beginning-of-line 2))
  9435. (org-table-goto-column c1)
  9436. (setq beg (point))
  9437. (goto-line r2)
  9438. (while (not (looking-at org-table-dataline-regexp))
  9439. (beginning-of-line 0))
  9440. (org-table-goto-column c2)
  9441. (setq end (point))
  9442. (if highlight
  9443. (org-table-highlight-rectangle
  9444. beg (progn (skip-chars-forward "^|\n") (point))))
  9445. ;; return string representation of calc vector
  9446. (mapcar 'org-trim
  9447. (apply 'append (org-table-copy-region beg end)))))))
  9448. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9449. "Analyze descriptor DESC and retrieve the corresponding line number.
  9450. The cursor is currently in line CLINE, the table begins in line BLINE,
  9451. and TABLE is a vector with line types."
  9452. (if (string-match "^[0-9]+$" desc)
  9453. (aref org-table-dlines (string-to-number desc))
  9454. (setq cline (or cline (org-current-line))
  9455. bline (or bline org-table-current-begin-line)
  9456. table (or table org-table-current-line-types))
  9457. (if (or
  9458. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9459. ;; 1 2 3 4 5 6
  9460. (and (not (match-end 3)) (not (match-end 6)))
  9461. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9462. (error "invalid row descriptor `%s'" desc))
  9463. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9464. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9465. (odir (and (match-end 5) (match-string 5 desc)))
  9466. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9467. (i (- cline bline))
  9468. (rel (and (match-end 6)
  9469. (or (and (match-end 1) (not (match-end 3)))
  9470. (match-end 5)))))
  9471. (if (and hn (not hdir))
  9472. (progn
  9473. (setq i 0 hdir "+")
  9474. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9475. (if (and (not hn) on (not odir))
  9476. (error "should never happen");;(aref org-table-dlines on)
  9477. (if (and hn (> hn 0))
  9478. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9479. (if on
  9480. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9481. (+ bline i)))))
  9482. (defun org-find-row-type (table i type backwards relative n)
  9483. (let ((l (length table)))
  9484. (while (> n 0)
  9485. (while (and (setq i (+ i (if backwards -1 1)))
  9486. (>= i 0) (< i l)
  9487. (not (eq (aref table i) type))
  9488. (if (and relative (eq (aref table i) 'hline))
  9489. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9490. t)))
  9491. (setq n (1- n)))
  9492. (if (or (< i 0) (>= i l))
  9493. (error "Row descriptior leads outside table")
  9494. i)))
  9495. (defun org-rewrite-old-row-references (s)
  9496. (if (string-match "&[-+0-9I]" s)
  9497. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9498. s))
  9499. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9500. "Convert list ELEMENTS to something appropriate to insert into formula.
  9501. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9502. NUMBERS indicates that everything should be converted to numbers.
  9503. LISPP means to return something appropriate for a Lisp list."
  9504. (if (stringp elements) ; just a single val
  9505. (if lispp
  9506. (if (eq lispp 'literal)
  9507. elements
  9508. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9509. (if (equal elements "") (setq elements "0"))
  9510. (if numbers (number-to-string (string-to-number elements)) elements))
  9511. (unless keep-empty
  9512. (setq elements
  9513. (delq nil
  9514. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9515. elements))))
  9516. (setq elements (or elements '("0")))
  9517. (if lispp
  9518. (mapconcat
  9519. (lambda (x)
  9520. (if (eq lispp 'literal)
  9521. x
  9522. (prin1-to-string (if numbers (string-to-number x) x))))
  9523. elements " ")
  9524. (concat "[" (mapconcat
  9525. (lambda (x)
  9526. (if numbers (number-to-string (string-to-number x)) x))
  9527. elements
  9528. ",") "]"))))
  9529. (defun org-table-recalculate (&optional all noalign)
  9530. "Recalculate the current table line by applying all stored formulas.
  9531. With prefix arg ALL, do this for all lines in the table."
  9532. (interactive "P")
  9533. (or (memq this-command org-recalc-commands)
  9534. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9535. (unless (org-at-table-p) (error "Not at a table"))
  9536. (if (equal all '(16))
  9537. (org-table-iterate)
  9538. (org-table-get-specials)
  9539. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9540. (lambda (a b) (string< (car a) (car b)))))
  9541. (inhibit-redisplay (not debug-on-error))
  9542. (line-re org-table-dataline-regexp)
  9543. (thisline (org-current-line))
  9544. (thiscol (org-table-current-column))
  9545. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9546. ;; Insert constants in all formulas
  9547. (setq eqlist
  9548. (mapcar (lambda (x)
  9549. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9550. x)
  9551. eqlist))
  9552. ;; Split the equation list
  9553. (while (setq eq (pop eqlist))
  9554. (if (<= (string-to-char (car eq)) ?9)
  9555. (push eq eqlnum)
  9556. (push eq eqlname)))
  9557. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9558. (if all
  9559. (progn
  9560. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9561. (goto-char (setq beg (org-table-begin)))
  9562. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9563. ;; This is a table with marked lines, compute selected lines
  9564. (setq line-re org-table-recalculate-regexp)
  9565. ;; Move forward to the first non-header line
  9566. (if (and (re-search-forward org-table-dataline-regexp end t)
  9567. (re-search-forward org-table-hline-regexp end t)
  9568. (re-search-forward org-table-dataline-regexp end t))
  9569. (setq beg (match-beginning 0))
  9570. nil))) ;; just leave beg where it is
  9571. (setq beg (point-at-bol)
  9572. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9573. (goto-char beg)
  9574. (and all (message "Re-applying formulas to full table..."))
  9575. ;; First find the named fields, and mark them untouchanble
  9576. (remove-text-properties beg end '(org-untouchable t))
  9577. (while (setq eq (pop eqlname))
  9578. (setq name (car eq)
  9579. a (assoc name org-table-named-field-locations))
  9580. (and (not a)
  9581. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9582. (setq a (list name
  9583. (aref org-table-dlines
  9584. (string-to-number (match-string 1 name)))
  9585. (string-to-number (match-string 2 name)))))
  9586. (when (and a (or all (equal (nth 1 a) thisline)))
  9587. (message "Re-applying formula to field: %s" name)
  9588. (goto-line (nth 1 a))
  9589. (org-table-goto-column (nth 2 a))
  9590. (push (append a (list (cdr eq))) eqlname1)
  9591. (org-table-put-field-property :org-untouchable t)))
  9592. ;; Now evauluate the column formulas, but skip fields covered by
  9593. ;; field formulas
  9594. (goto-char beg)
  9595. (while (re-search-forward line-re end t)
  9596. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9597. ;; Unprotected line, recalculate
  9598. (and all (message "Re-applying formulas to full table...(line %d)"
  9599. (setq cnt (1+ cnt))))
  9600. (setq org-last-recalc-line (org-current-line))
  9601. (setq eql eqlnum)
  9602. (while (setq entry (pop eql))
  9603. (goto-line org-last-recalc-line)
  9604. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9605. (unless (get-text-property (point) :org-untouchable)
  9606. (org-table-eval-formula nil (cdr entry)
  9607. 'noalign 'nocst 'nostore 'noanalysis)))))
  9608. ;; Now evaluate the field formulas
  9609. (while (setq eq (pop eqlname1))
  9610. (message "Re-applying formula to field: %s" (car eq))
  9611. (goto-line (nth 1 eq))
  9612. (org-table-goto-column (nth 2 eq))
  9613. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9614. 'nostore 'noanalysis))
  9615. (goto-line thisline)
  9616. (org-table-goto-column thiscol)
  9617. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9618. (or noalign (and org-table-may-need-update (org-table-align))
  9619. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9620. ;; back to initial position
  9621. (message "Re-applying formulas...done")
  9622. (goto-line thisline)
  9623. (org-table-goto-column thiscol)
  9624. (or noalign (and org-table-may-need-update (org-table-align))
  9625. (and all (message "Re-applying formulas...done"))))))
  9626. (defun org-table-iterate (&optional arg)
  9627. "Recalculate the table until it does not change anymore."
  9628. (interactive "P")
  9629. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9630. (i 0)
  9631. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9632. thistbl)
  9633. (catch 'exit
  9634. (while (< i imax)
  9635. (setq i (1+ i))
  9636. (org-table-recalculate 'all)
  9637. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9638. (if (not (string= lasttbl thistbl))
  9639. (setq lasttbl thistbl)
  9640. (if (> i 1)
  9641. (message "Convergence after %d iterations" i)
  9642. (message "Table was already stable"))
  9643. (throw 'exit t)))
  9644. (error "No convergence after %d iterations" i))))
  9645. (defun org-table-formula-substitute-names (f)
  9646. "Replace $const with values in string F."
  9647. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9648. ;; First, check for column names
  9649. (while (setq start (string-match org-table-column-name-regexp f start))
  9650. (setq start (1+ start))
  9651. (setq a (assoc (match-string 1 f) org-table-column-names))
  9652. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9653. ;; Parameters and constants
  9654. (setq start 0)
  9655. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9656. (setq start (1+ start))
  9657. (if (setq a (save-match-data
  9658. (org-table-get-constant (match-string 1 f))))
  9659. (setq f (replace-match
  9660. (concat (if pp "(") a (if pp ")")) t t f))))
  9661. (if org-table-formula-debug
  9662. (put-text-property 0 (length f) :orig-formula f1 f))
  9663. f))
  9664. (defun org-table-get-constant (const)
  9665. "Find the value for a parameter or constant in a formula.
  9666. Parameters get priority."
  9667. (or (cdr (assoc const org-table-local-parameters))
  9668. (cdr (assoc const org-table-formula-constants-local))
  9669. (cdr (assoc const org-table-formula-constants))
  9670. (and (fboundp 'constants-get) (constants-get const))
  9671. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9672. (org-entry-get nil (substring const 5) 'inherit))
  9673. "#UNDEFINED_NAME"))
  9674. (defvar org-table-fedit-map
  9675. (let ((map (make-sparse-keymap)))
  9676. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9677. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9678. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9679. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9680. (org-defkey map "\C-c?" 'org-table-show-reference)
  9681. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9682. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9683. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9684. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9685. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9686. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9687. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9688. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9689. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9690. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9691. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9692. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9693. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9694. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9695. map))
  9696. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9697. '("Edit-Formulas"
  9698. ["Finish and Install" org-table-fedit-finish t]
  9699. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9700. ["Abort" org-table-fedit-abort t]
  9701. "--"
  9702. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9703. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9704. "--"
  9705. "Shift Reference at Point"
  9706. ["Up" org-table-fedit-ref-up t]
  9707. ["Down" org-table-fedit-ref-down t]
  9708. ["Left" org-table-fedit-ref-left t]
  9709. ["Right" org-table-fedit-ref-right t]
  9710. "-"
  9711. "Change Test Row for Column Formulas"
  9712. ["Up" org-table-fedit-line-up t]
  9713. ["Down" org-table-fedit-line-down t]
  9714. "--"
  9715. ["Scroll Table Window" org-table-fedit-scroll t]
  9716. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9717. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9718. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9719. org-table-overlay-coordinates)]
  9720. "--"
  9721. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9722. :style toggle :selected org-table-buffer-is-an]))
  9723. (defvar org-pos)
  9724. (defun org-table-edit-formulas ()
  9725. "Edit the formulas of the current table in a separate buffer."
  9726. (interactive)
  9727. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9728. (beginning-of-line 0))
  9729. (unless (org-at-table-p) (error "Not at a table"))
  9730. (org-table-get-specials)
  9731. (let ((key (org-table-current-field-formula 'key 'noerror))
  9732. (eql (sort (org-table-get-stored-formulas 'noerror)
  9733. 'org-table-formula-less-p))
  9734. (pos (move-marker (make-marker) (point)))
  9735. (startline 1)
  9736. (wc (current-window-configuration))
  9737. (titles '((column . "# Column Formulas\n")
  9738. (field . "# Field Formulas\n")
  9739. (named . "# Named Field Formulas\n")))
  9740. entry s type title)
  9741. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9742. (erase-buffer)
  9743. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9744. (let ((font-lock-global-modes '(not fundamental-mode)))
  9745. (fundamental-mode))
  9746. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9747. (org-set-local 'org-pos pos)
  9748. (org-set-local 'org-window-configuration wc)
  9749. (use-local-map org-table-fedit-map)
  9750. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9751. (easy-menu-add org-table-fedit-menu)
  9752. (setq startline (org-current-line))
  9753. (while (setq entry (pop eql))
  9754. (setq type (cond
  9755. ((equal (string-to-char (car entry)) ?@) 'field)
  9756. ((string-match "^[0-9]" (car entry)) 'column)
  9757. (t 'named)))
  9758. (when (setq title (assq type titles))
  9759. (or (bobp) (insert "\n"))
  9760. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9761. (setq titles (delq title titles)))
  9762. (if (equal key (car entry)) (setq startline (org-current-line)))
  9763. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9764. (car entry) " = " (cdr entry) "\n"))
  9765. (remove-text-properties 0 (length s) '(face nil) s)
  9766. (insert s))
  9767. (if (eq org-table-use-standard-references t)
  9768. (org-table-fedit-toggle-ref-type))
  9769. (goto-line startline)
  9770. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9771. (defun org-table-fedit-post-command ()
  9772. (when (not (memq this-command '(lisp-complete-symbol)))
  9773. (let ((win (selected-window)))
  9774. (save-excursion
  9775. (condition-case nil
  9776. (org-table-show-reference)
  9777. (error nil))
  9778. (select-window win)))))
  9779. (defun org-table-formula-to-user (s)
  9780. "Convert a formula from internal to user representation."
  9781. (if (eq org-table-use-standard-references t)
  9782. (org-table-convert-refs-to-an s)
  9783. s))
  9784. (defun org-table-formula-from-user (s)
  9785. "Convert a formula from user to internal representation."
  9786. (if org-table-use-standard-references
  9787. (org-table-convert-refs-to-rc s)
  9788. s))
  9789. (defun org-table-convert-refs-to-rc (s)
  9790. "Convert spreadsheet references from AB7 to @7$28.
  9791. Works for single references, but also for entire formulas and even the
  9792. full TBLFM line."
  9793. (let ((start 0))
  9794. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9795. (cond
  9796. ((match-end 3)
  9797. ;; format match, just advance
  9798. (setq start (match-end 0)))
  9799. ((and (> (match-beginning 0) 0)
  9800. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9801. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9802. ;; 3.e5 or something like this.
  9803. (setq start (match-end 0)))
  9804. (t
  9805. (setq start (match-beginning 0)
  9806. s (replace-match
  9807. (if (equal (match-string 2 s) "&")
  9808. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9809. (format "@%d$%d"
  9810. (string-to-number (match-string 2 s))
  9811. (org-letters-to-number (match-string 1 s))))
  9812. t t s)))))
  9813. s))
  9814. (defun org-table-convert-refs-to-an (s)
  9815. "Convert spreadsheet references from to @7$28 to AB7.
  9816. Works for single references, but also for entire formulas and even the
  9817. full TBLFM line."
  9818. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9819. (setq s (replace-match
  9820. (format "%s%d"
  9821. (org-number-to-letters
  9822. (string-to-number (match-string 2 s)))
  9823. (string-to-number (match-string 1 s)))
  9824. t t s)))
  9825. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9826. (setq s (replace-match (concat "\\1"
  9827. (org-number-to-letters
  9828. (string-to-number (match-string 2 s))) "&")
  9829. t nil s)))
  9830. s)
  9831. (defun org-letters-to-number (s)
  9832. "Convert a base 26 number represented by letters into an integer.
  9833. For example: AB -> 28."
  9834. (let ((n 0))
  9835. (setq s (upcase s))
  9836. (while (> (length s) 0)
  9837. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9838. s (substring s 1)))
  9839. n))
  9840. (defun org-number-to-letters (n)
  9841. "Convert an integer into a base 26 number represented by letters.
  9842. For example: 28 -> AB."
  9843. (let ((s ""))
  9844. (while (> n 0)
  9845. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9846. n (/ (1- n) 26)))
  9847. s))
  9848. (defun org-table-fedit-convert-buffer (function)
  9849. "Convert all references in this buffer, using FUNTION."
  9850. (let ((line (org-current-line)))
  9851. (goto-char (point-min))
  9852. (while (not (eobp))
  9853. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9854. (delete-region (point) (point-at-eol))
  9855. (or (eobp) (forward-char 1)))
  9856. (goto-line line)))
  9857. (defun org-table-fedit-toggle-ref-type ()
  9858. "Convert all references in the buffer from B3 to @3$2 and back."
  9859. (interactive)
  9860. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9861. (org-table-fedit-convert-buffer
  9862. (if org-table-buffer-is-an
  9863. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9864. (message "Reference type switched to %s"
  9865. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9866. (defun org-table-fedit-ref-up ()
  9867. "Shift the reference at point one row/hline up."
  9868. (interactive)
  9869. (org-table-fedit-shift-reference 'up))
  9870. (defun org-table-fedit-ref-down ()
  9871. "Shift the reference at point one row/hline down."
  9872. (interactive)
  9873. (org-table-fedit-shift-reference 'down))
  9874. (defun org-table-fedit-ref-left ()
  9875. "Shift the reference at point one field to the left."
  9876. (interactive)
  9877. (org-table-fedit-shift-reference 'left))
  9878. (defun org-table-fedit-ref-right ()
  9879. "Shift the reference at point one field to the right."
  9880. (interactive)
  9881. (org-table-fedit-shift-reference 'right))
  9882. (defun org-table-fedit-shift-reference (dir)
  9883. (cond
  9884. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9885. (if (memq dir '(left right))
  9886. (org-rematch-and-replace 1 (eq dir 'left))
  9887. (error "Cannot shift reference in this direction")))
  9888. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9889. ;; A B3-like reference
  9890. (if (memq dir '(up down))
  9891. (org-rematch-and-replace 2 (eq dir 'up))
  9892. (org-rematch-and-replace 1 (eq dir 'left))))
  9893. ((org-at-regexp-p
  9894. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9895. ;; An internal reference
  9896. (if (memq dir '(up down))
  9897. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9898. (org-rematch-and-replace 5 (eq dir 'left))))))
  9899. (defun org-rematch-and-replace (n &optional decr hline)
  9900. "Re-match the group N, and replace it with the shifted refrence."
  9901. (or (match-end n) (error "Cannot shift reference in this direction"))
  9902. (goto-char (match-beginning n))
  9903. (and (looking-at (regexp-quote (match-string n)))
  9904. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9905. t t)))
  9906. (defun org-shift-refpart (ref &optional decr hline)
  9907. "Shift a refrence part REF.
  9908. If DECR is set, decrease the references row/column, else increase.
  9909. If HLINE is set, this may be a hline reference, it certainly is not
  9910. a translation reference."
  9911. (save-match-data
  9912. (let* ((sign (string-match "^[-+]" ref)) n)
  9913. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9914. (cond
  9915. ((and hline (string-match "^I+" ref))
  9916. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9917. (setq n (+ n (if decr -1 1)))
  9918. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9919. (if sign
  9920. (setq sign (if (< n 0) "-" "+") n (abs n))
  9921. (setq n (max 1 n)))
  9922. (concat sign (make-string n ?I)))
  9923. ((string-match "^[0-9]+" ref)
  9924. (setq n (string-to-number (concat sign ref)))
  9925. (setq n (+ n (if decr -1 1)))
  9926. (if sign
  9927. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9928. (number-to-string (max 1 n))))
  9929. ((string-match "^[a-zA-Z]+" ref)
  9930. (org-number-to-letters
  9931. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9932. (t (error "Cannot shift reference"))))))
  9933. (defun org-table-fedit-toggle-coordinates ()
  9934. "Toggle the display of coordinates in the refrenced table."
  9935. (interactive)
  9936. (let ((pos (marker-position org-pos)))
  9937. (with-current-buffer (marker-buffer org-pos)
  9938. (save-excursion
  9939. (goto-char pos)
  9940. (org-table-toggle-coordinate-overlays)))))
  9941. (defun org-table-fedit-finish (&optional arg)
  9942. "Parse the buffer for formula definitions and install them.
  9943. With prefix ARG, apply the new formulas to the table."
  9944. (interactive "P")
  9945. (org-table-remove-rectangle-highlight)
  9946. (if org-table-use-standard-references
  9947. (progn
  9948. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9949. (setq org-table-buffer-is-an nil)))
  9950. (let ((pos org-pos) eql var form)
  9951. (goto-char (point-min))
  9952. (while (re-search-forward
  9953. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9954. nil t)
  9955. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9956. form (match-string 3))
  9957. (setq form (org-trim form))
  9958. (when (not (equal form ""))
  9959. (while (string-match "[ \t]*\n[ \t]*" form)
  9960. (setq form (replace-match " " t t form)))
  9961. (when (assoc var eql)
  9962. (error "Double formulas for %s" var))
  9963. (push (cons var form) eql)))
  9964. (setq org-pos nil)
  9965. (set-window-configuration org-window-configuration)
  9966. (select-window (get-buffer-window (marker-buffer pos)))
  9967. (goto-char pos)
  9968. (unless (org-at-table-p)
  9969. (error "Lost table position - cannot install formulae"))
  9970. (org-table-store-formulas eql)
  9971. (move-marker pos nil)
  9972. (kill-buffer "*Edit Formulas*")
  9973. (if arg
  9974. (org-table-recalculate 'all)
  9975. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9976. (defun org-table-fedit-abort ()
  9977. "Abort editing formulas, without installing the changes."
  9978. (interactive)
  9979. (org-table-remove-rectangle-highlight)
  9980. (let ((pos org-pos))
  9981. (set-window-configuration org-window-configuration)
  9982. (select-window (get-buffer-window (marker-buffer pos)))
  9983. (goto-char pos)
  9984. (move-marker pos nil)
  9985. (message "Formula editing aborted without installing changes")))
  9986. (defun org-table-fedit-lisp-indent ()
  9987. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9988. (interactive)
  9989. (let ((pos (point)) beg end ind)
  9990. (beginning-of-line 1)
  9991. (cond
  9992. ((looking-at "[ \t]")
  9993. (goto-char pos)
  9994. (call-interactively 'lisp-indent-line))
  9995. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9996. ((not (fboundp 'pp-buffer))
  9997. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9998. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9999. (goto-char (- (match-end 0) 2))
  10000. (setq beg (point))
  10001. (setq ind (make-string (current-column) ?\ ))
  10002. (condition-case nil (forward-sexp 1)
  10003. (error
  10004. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  10005. (setq end (point))
  10006. (save-restriction
  10007. (narrow-to-region beg end)
  10008. (if (eq last-command this-command)
  10009. (progn
  10010. (goto-char (point-min))
  10011. (setq this-command nil)
  10012. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  10013. (replace-match " ")))
  10014. (pp-buffer)
  10015. (untabify (point-min) (point-max))
  10016. (goto-char (1+ (point-min)))
  10017. (while (re-search-forward "^." nil t)
  10018. (beginning-of-line 1)
  10019. (insert ind))
  10020. (goto-char (point-max))
  10021. (backward-delete-char 1)))
  10022. (goto-char beg))
  10023. (t nil))))
  10024. (defvar org-show-positions nil)
  10025. (defun org-table-show-reference (&optional local)
  10026. "Show the location/value of the $ expression at point."
  10027. (interactive)
  10028. (org-table-remove-rectangle-highlight)
  10029. (catch 'exit
  10030. (let ((pos (if local (point) org-pos))
  10031. (face2 'highlight)
  10032. (org-inhibit-highlight-removal t)
  10033. (win (selected-window))
  10034. (org-show-positions nil)
  10035. var name e what match dest)
  10036. (if local (org-table-get-specials))
  10037. (setq what (cond
  10038. ((or (org-at-regexp-p org-table-range-regexp2)
  10039. (org-at-regexp-p org-table-translate-regexp)
  10040. (org-at-regexp-p org-table-range-regexp))
  10041. (setq match
  10042. (save-match-data
  10043. (org-table-convert-refs-to-rc (match-string 0))))
  10044. 'range)
  10045. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  10046. ((org-at-regexp-p "\\$[0-9]+") 'column)
  10047. ((not local) nil)
  10048. (t (error "No reference at point")))
  10049. match (and what (or match (match-string 0))))
  10050. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  10051. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  10052. 'secondary-selection))
  10053. (org-add-hook 'before-change-functions
  10054. 'org-table-remove-rectangle-highlight)
  10055. (if (eq what 'name) (setq var (substring match 1)))
  10056. (when (eq what 'range)
  10057. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  10058. (setq match (org-table-formula-substitute-names match)))
  10059. (unless local
  10060. (save-excursion
  10061. (end-of-line 1)
  10062. (re-search-backward "^\\S-" nil t)
  10063. (beginning-of-line 1)
  10064. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  10065. (setq dest
  10066. (save-match-data
  10067. (org-table-convert-refs-to-rc (match-string 1))))
  10068. (org-table-add-rectangle-overlay
  10069. (match-beginning 1) (match-end 1) face2))))
  10070. (if (and (markerp pos) (marker-buffer pos))
  10071. (if (get-buffer-window (marker-buffer pos))
  10072. (select-window (get-buffer-window (marker-buffer pos)))
  10073. (org-switch-to-buffer-other-window (get-buffer-window
  10074. (marker-buffer pos)))))
  10075. (goto-char pos)
  10076. (org-table-force-dataline)
  10077. (when dest
  10078. (setq name (substring dest 1))
  10079. (cond
  10080. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  10081. (setq e (assoc name org-table-named-field-locations))
  10082. (goto-line (nth 1 e))
  10083. (org-table-goto-column (nth 2 e)))
  10084. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  10085. (let ((l (string-to-number (match-string 1 dest)))
  10086. (c (string-to-number (match-string 2 dest))))
  10087. (goto-line (aref org-table-dlines l))
  10088. (org-table-goto-column c)))
  10089. (t (org-table-goto-column (string-to-number name))))
  10090. (move-marker pos (point))
  10091. (org-table-highlight-rectangle nil nil face2))
  10092. (cond
  10093. ((equal dest match))
  10094. ((not match))
  10095. ((eq what 'range)
  10096. (condition-case nil
  10097. (save-excursion
  10098. (org-table-get-range match nil nil 'highlight))
  10099. (error nil)))
  10100. ((setq e (assoc var org-table-named-field-locations))
  10101. (goto-line (nth 1 e))
  10102. (org-table-goto-column (nth 2 e))
  10103. (org-table-highlight-rectangle (point) (point))
  10104. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10105. ((setq e (assoc var org-table-column-names))
  10106. (org-table-goto-column (string-to-number (cdr e)))
  10107. (org-table-highlight-rectangle (point) (point))
  10108. (goto-char (org-table-begin))
  10109. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10110. (org-table-end) t)
  10111. (progn
  10112. (goto-char (match-beginning 1))
  10113. (org-table-highlight-rectangle)
  10114. (message "Named column (column %s)" (cdr e)))
  10115. (error "Column name not found")))
  10116. ((eq what 'column)
  10117. ;; column number
  10118. (org-table-goto-column (string-to-number (substring match 1)))
  10119. (org-table-highlight-rectangle (point) (point))
  10120. (message "Column %s" (substring match 1)))
  10121. ((setq e (assoc var org-table-local-parameters))
  10122. (goto-char (org-table-begin))
  10123. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10124. (progn
  10125. (goto-char (match-beginning 1))
  10126. (org-table-highlight-rectangle)
  10127. (message "Local parameter."))
  10128. (error "Parameter not found")))
  10129. (t
  10130. (cond
  10131. ((not var) (error "No reference at point"))
  10132. ((setq e (assoc var org-table-formula-constants-local))
  10133. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  10134. var (cdr e)))
  10135. ((setq e (assoc var org-table-formula-constants))
  10136. (message "Constant: $%s=%s in `org-table-formula-constants'."
  10137. var (cdr e)))
  10138. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10139. (message "Constant: $%s=%s, from `constants.el'%s."
  10140. var e (format " (%s units)" constants-unit-system)))
  10141. (t (error "Undefined name $%s" var)))))
  10142. (goto-char pos)
  10143. (when (and org-show-positions
  10144. (not (memq this-command '(org-table-fedit-scroll
  10145. org-table-fedit-scroll-down))))
  10146. (push pos org-show-positions)
  10147. (push org-table-current-begin-pos org-show-positions)
  10148. (let ((min (apply 'min org-show-positions))
  10149. (max (apply 'max org-show-positions)))
  10150. (goto-char min) (recenter 0)
  10151. (goto-char max)
  10152. (or (pos-visible-in-window-p max) (recenter -1))))
  10153. (select-window win))))
  10154. (defun org-table-force-dataline ()
  10155. "Make sure the cursor is in a dataline in a table."
  10156. (unless (save-excursion
  10157. (beginning-of-line 1)
  10158. (looking-at org-table-dataline-regexp))
  10159. (let* ((re org-table-dataline-regexp)
  10160. (p1 (save-excursion (re-search-forward re nil 'move)))
  10161. (p2 (save-excursion (re-search-backward re nil 'move))))
  10162. (cond ((and p1 p2)
  10163. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  10164. p1 p2)))
  10165. ((or p1 p2) (goto-char (or p1 p2)))
  10166. (t (error "No table dataline around here"))))))
  10167. (defun org-table-fedit-line-up ()
  10168. "Move cursor one line up in the window showing the table."
  10169. (interactive)
  10170. (org-table-fedit-move 'previous-line))
  10171. (defun org-table-fedit-line-down ()
  10172. "Move cursor one line down in the window showing the table."
  10173. (interactive)
  10174. (org-table-fedit-move 'next-line))
  10175. (defun org-table-fedit-move (command)
  10176. "Move the cursor in the window shoinw the table.
  10177. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  10178. (let ((org-table-allow-automatic-line-recalculation nil)
  10179. (pos org-pos) (win (selected-window)) p)
  10180. (select-window (get-buffer-window (marker-buffer org-pos)))
  10181. (setq p (point))
  10182. (call-interactively command)
  10183. (while (and (org-at-table-p)
  10184. (org-at-table-hline-p))
  10185. (call-interactively command))
  10186. (or (org-at-table-p) (goto-char p))
  10187. (move-marker pos (point))
  10188. (select-window win)))
  10189. (defun org-table-fedit-scroll (N)
  10190. (interactive "p")
  10191. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  10192. (scroll-other-window N)))
  10193. (defun org-table-fedit-scroll-down (N)
  10194. (interactive "p")
  10195. (org-table-fedit-scroll (- N)))
  10196. (defvar org-table-rectangle-overlays nil)
  10197. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10198. "Add a new overlay."
  10199. (let ((ov (org-make-overlay beg end)))
  10200. (org-overlay-put ov 'face (or face 'secondary-selection))
  10201. (push ov org-table-rectangle-overlays)))
  10202. (defun org-table-highlight-rectangle (&optional beg end face)
  10203. "Highlight rectangular region in a table."
  10204. (setq beg (or beg (point)) end (or end (point)))
  10205. (let ((b (min beg end))
  10206. (e (max beg end))
  10207. l1 c1 l2 c2 tmp)
  10208. (and (boundp 'org-show-positions)
  10209. (setq org-show-positions (cons b (cons e org-show-positions))))
  10210. (goto-char (min beg end))
  10211. (setq l1 (org-current-line)
  10212. c1 (org-table-current-column))
  10213. (goto-char (max beg end))
  10214. (setq l2 (org-current-line)
  10215. c2 (org-table-current-column))
  10216. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10217. (goto-line l1)
  10218. (beginning-of-line 1)
  10219. (loop for line from l1 to l2 do
  10220. (when (looking-at org-table-dataline-regexp)
  10221. (org-table-goto-column c1)
  10222. (skip-chars-backward "^|\n") (setq beg (point))
  10223. (org-table-goto-column c2)
  10224. (skip-chars-forward "^|\n") (setq end (point))
  10225. (org-table-add-rectangle-overlay beg end face))
  10226. (beginning-of-line 2))
  10227. (goto-char b))
  10228. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10229. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10230. "Remove the rectangle overlays."
  10231. (unless org-inhibit-highlight-removal
  10232. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10233. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10234. (setq org-table-rectangle-overlays nil)))
  10235. (defvar org-table-coordinate-overlays nil
  10236. "Collects the cooordinate grid overlays, so that they can be removed.")
  10237. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10238. (defun org-table-overlay-coordinates ()
  10239. "Add overlays to the table at point, to show row/column coordinates."
  10240. (interactive)
  10241. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10242. (setq org-table-coordinate-overlays nil)
  10243. (save-excursion
  10244. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10245. (goto-char (org-table-begin))
  10246. (while (org-at-table-p)
  10247. (setq eol (point-at-eol))
  10248. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10249. (push ov org-table-coordinate-overlays)
  10250. (setq hline (looking-at org-table-hline-regexp))
  10251. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10252. (format "%4d" (setq id (1+ id)))))
  10253. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10254. (when hline
  10255. (setq ic 0)
  10256. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10257. (setq beg (1+ (match-beginning 0))
  10258. ic (1+ ic)
  10259. s1 (concat "$" (int-to-string ic))
  10260. s2 (org-number-to-letters ic)
  10261. str (if (eq org-table-use-standard-references t) s2 s1))
  10262. (setq ov (org-make-overlay beg (+ beg (length str))))
  10263. (push ov org-table-coordinate-overlays)
  10264. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10265. (beginning-of-line 2)))))
  10266. (defun org-table-toggle-coordinate-overlays ()
  10267. "Toggle the display of Row/Column numbers in tables."
  10268. (interactive)
  10269. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10270. (message "Row/Column number display turned %s"
  10271. (if org-table-overlay-coordinates "on" "off"))
  10272. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10273. (org-table-align))
  10274. (unless org-table-overlay-coordinates
  10275. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10276. (setq org-table-coordinate-overlays nil)))
  10277. (defun org-table-toggle-formula-debugger ()
  10278. "Toggle the formula debugger in tables."
  10279. (interactive)
  10280. (setq org-table-formula-debug (not org-table-formula-debug))
  10281. (message "Formula debugging has been turned %s"
  10282. (if org-table-formula-debug "on" "off")))
  10283. ;;; The orgtbl minor mode
  10284. ;; Define a minor mode which can be used in other modes in order to
  10285. ;; integrate the org-mode table editor.
  10286. ;; This is really a hack, because the org-mode table editor uses several
  10287. ;; keys which normally belong to the major mode, for example the TAB and
  10288. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10289. ;; necessary to operate the table editor, but wraps the commands into a
  10290. ;; function which tests if the cursor is currently inside a table. If that
  10291. ;; is the case, the table editor command is executed. However, when any of
  10292. ;; those keys is used outside a table, the function uses `key-binding' to
  10293. ;; look up if the key has an associated command in another currently active
  10294. ;; keymap (minor modes, major mode, global), and executes that command.
  10295. ;; There might be problems if any of the keys used by the table editor is
  10296. ;; otherwise used as a prefix key.
  10297. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10298. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10299. ;; addresses this by checking explicitly for both bindings.
  10300. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10301. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10302. ;; Some modes bind other commands to simple characters, for example
  10303. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10304. ;; active, this binding is ignored inside tables and replaced with a
  10305. ;; modified self-insert.
  10306. (defvar orgtbl-mode nil
  10307. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10308. table editor in arbitrary modes.")
  10309. (make-variable-buffer-local 'orgtbl-mode)
  10310. (defvar orgtbl-mode-map (make-keymap)
  10311. "Keymap for `orgtbl-mode'.")
  10312. ;;;###autoload
  10313. (defun turn-on-orgtbl ()
  10314. "Unconditionally turn on `orgtbl-mode'."
  10315. (orgtbl-mode 1))
  10316. (defvar org-old-auto-fill-inhibit-regexp nil
  10317. "Local variable used by `orgtbl-mode'")
  10318. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10319. "Matches a line belonging to an orgtbl.")
  10320. (defconst orgtbl-extra-font-lock-keywords
  10321. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10322. 0 (quote 'org-table) 'prepend))
  10323. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10324. ;;;###autoload
  10325. (defun orgtbl-mode (&optional arg)
  10326. "The `org-mode' table editor as a minor mode for use in other modes."
  10327. (interactive)
  10328. (if (org-mode-p)
  10329. ;; Exit without error, in case some hook functions calls this
  10330. ;; by accident in org-mode.
  10331. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10332. (setq orgtbl-mode
  10333. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10334. (if orgtbl-mode
  10335. (progn
  10336. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10337. ;; Make sure we are first in minor-mode-map-alist
  10338. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10339. (and c (setq minor-mode-map-alist
  10340. (cons c (delq c minor-mode-map-alist)))))
  10341. (org-set-local (quote org-table-may-need-update) t)
  10342. (org-add-hook 'before-change-functions 'org-before-change-function
  10343. nil 'local)
  10344. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10345. auto-fill-inhibit-regexp)
  10346. (org-set-local 'auto-fill-inhibit-regexp
  10347. (if auto-fill-inhibit-regexp
  10348. (concat orgtbl-line-start-regexp "\\|"
  10349. auto-fill-inhibit-regexp)
  10350. orgtbl-line-start-regexp))
  10351. (org-add-to-invisibility-spec '(org-cwidth))
  10352. (when (fboundp 'font-lock-add-keywords)
  10353. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10354. (org-restart-font-lock))
  10355. (easy-menu-add orgtbl-mode-menu)
  10356. (run-hooks 'orgtbl-mode-hook))
  10357. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10358. (org-cleanup-narrow-column-properties)
  10359. (org-remove-from-invisibility-spec '(org-cwidth))
  10360. (remove-hook 'before-change-functions 'org-before-change-function t)
  10361. (when (fboundp 'font-lock-remove-keywords)
  10362. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10363. (org-restart-font-lock))
  10364. (easy-menu-remove orgtbl-mode-menu)
  10365. (force-mode-line-update 'all))))
  10366. (defun org-cleanup-narrow-column-properties ()
  10367. "Remove all properties related to narrow-column invisibility."
  10368. (let ((s 1))
  10369. (while (setq s (text-property-any s (point-max)
  10370. 'display org-narrow-column-arrow))
  10371. (remove-text-properties s (1+ s) '(display t)))
  10372. (setq s 1)
  10373. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10374. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10375. (setq s 1)
  10376. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10377. (remove-text-properties s (1+ s) '(invisible t)))))
  10378. ;; Install it as a minor mode.
  10379. (put 'orgtbl-mode :included t)
  10380. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10381. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10382. (defun orgtbl-make-binding (fun n &rest keys)
  10383. "Create a function for binding in the table minor mode.
  10384. FUN is the command to call inside a table. N is used to create a unique
  10385. command name. KEYS are keys that should be checked in for a command
  10386. to execute outside of tables."
  10387. (eval
  10388. (list 'defun
  10389. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10390. '(arg)
  10391. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10392. "Outside of tables, run the binding of `"
  10393. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10394. "'.")
  10395. '(interactive "p")
  10396. (list 'if
  10397. '(org-at-table-p)
  10398. (list 'call-interactively (list 'quote fun))
  10399. (list 'let '(orgtbl-mode)
  10400. (list 'call-interactively
  10401. (append '(or)
  10402. (mapcar (lambda (k)
  10403. (list 'key-binding k))
  10404. keys)
  10405. '('orgtbl-error))))))))
  10406. (defun orgtbl-error ()
  10407. "Error when there is no default binding for a table key."
  10408. (interactive)
  10409. (error "This key has no function outside tables"))
  10410. (defun orgtbl-setup ()
  10411. "Setup orgtbl keymaps."
  10412. (let ((nfunc 0)
  10413. (bindings
  10414. (list
  10415. '([(meta shift left)] org-table-delete-column)
  10416. '([(meta left)] org-table-move-column-left)
  10417. '([(meta right)] org-table-move-column-right)
  10418. '([(meta shift right)] org-table-insert-column)
  10419. '([(meta shift up)] org-table-kill-row)
  10420. '([(meta shift down)] org-table-insert-row)
  10421. '([(meta up)] org-table-move-row-up)
  10422. '([(meta down)] org-table-move-row-down)
  10423. '("\C-c\C-w" org-table-cut-region)
  10424. '("\C-c\M-w" org-table-copy-region)
  10425. '("\C-c\C-y" org-table-paste-rectangle)
  10426. '("\C-c-" org-table-insert-hline)
  10427. '("\C-c}" org-table-toggle-coordinate-overlays)
  10428. '("\C-c{" org-table-toggle-formula-debugger)
  10429. '("\C-m" org-table-next-row)
  10430. '([(shift return)] org-table-copy-down)
  10431. '("\C-c\C-q" org-table-wrap-region)
  10432. '("\C-c?" org-table-field-info)
  10433. '("\C-c " org-table-blank-field)
  10434. '("\C-c+" org-table-sum)
  10435. '("\C-c=" org-table-eval-formula)
  10436. '("\C-c'" org-table-edit-formulas)
  10437. '("\C-c`" org-table-edit-field)
  10438. '("\C-c*" org-table-recalculate)
  10439. '("\C-c|" org-table-create-or-convert-from-region)
  10440. '("\C-c^" org-table-sort-lines)
  10441. '([(control ?#)] org-table-rotate-recalc-marks)))
  10442. elt key fun cmd)
  10443. (while (setq elt (pop bindings))
  10444. (setq nfunc (1+ nfunc))
  10445. (setq key (org-key (car elt))
  10446. fun (nth 1 elt)
  10447. cmd (orgtbl-make-binding fun nfunc key))
  10448. (org-defkey orgtbl-mode-map key cmd))
  10449. ;; Special treatment needed for TAB and RET
  10450. (org-defkey orgtbl-mode-map [(return)]
  10451. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10452. (org-defkey orgtbl-mode-map "\C-m"
  10453. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10454. (org-defkey orgtbl-mode-map [(tab)]
  10455. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10456. (org-defkey orgtbl-mode-map "\C-i"
  10457. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10458. (org-defkey orgtbl-mode-map [(shift tab)]
  10459. (orgtbl-make-binding 'org-table-previous-field 104
  10460. [(shift tab)] [(tab)] "\C-i"))
  10461. (org-defkey orgtbl-mode-map "\M-\C-m"
  10462. (orgtbl-make-binding 'org-table-wrap-region 105
  10463. "\M-\C-m" [(meta return)]))
  10464. (org-defkey orgtbl-mode-map [(meta return)]
  10465. (orgtbl-make-binding 'org-table-wrap-region 106
  10466. [(meta return)] "\M-\C-m"))
  10467. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10468. (when orgtbl-optimized
  10469. ;; If the user wants maximum table support, we need to hijack
  10470. ;; some standard editing functions
  10471. (org-remap orgtbl-mode-map
  10472. 'self-insert-command 'orgtbl-self-insert-command
  10473. 'delete-char 'org-delete-char
  10474. 'delete-backward-char 'org-delete-backward-char)
  10475. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10476. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10477. '("OrgTbl"
  10478. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10479. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10480. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10481. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10482. "--"
  10483. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10484. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10485. ["Copy Field from Above"
  10486. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10487. "--"
  10488. ("Column"
  10489. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10490. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10491. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10492. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10493. ("Row"
  10494. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10495. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10496. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10497. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10498. ["Sort lines in region" org-table-sort-lines :active (org-at-table-p) :keys "C-c ^"]
  10499. "--"
  10500. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10501. ("Rectangle"
  10502. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10503. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10504. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10505. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10506. "--"
  10507. ("Radio tables"
  10508. ["Insert table template" orgtbl-insert-radio-table
  10509. (assq major-mode orgtbl-radio-table-templates)]
  10510. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10511. "--"
  10512. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10513. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10514. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10515. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10516. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10517. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10518. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10519. ["Sum Column/Rectangle" org-table-sum
  10520. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10521. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10522. ["Debug Formulas"
  10523. org-table-toggle-formula-debugger :active (org-at-table-p)
  10524. :keys "C-c {"
  10525. :style toggle :selected org-table-formula-debug]
  10526. ["Show Col/Row Numbers"
  10527. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10528. :keys "C-c }"
  10529. :style toggle :selected org-table-overlay-coordinates]
  10530. ))
  10531. t))
  10532. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10533. "If the cursor is inside a table, realign the table.
  10534. It it is a table to be sent away to a receiver, do it.
  10535. With prefix arg, also recompute table."
  10536. (interactive "P")
  10537. (let ((pos (point)) action)
  10538. (save-excursion
  10539. (beginning-of-line 1)
  10540. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10541. ((looking-at "[ \t]*|") pos)
  10542. ((looking-at "#\\+TBLFM:") 'recalc))))
  10543. (cond
  10544. ((integerp action)
  10545. (goto-char action)
  10546. (org-table-maybe-eval-formula)
  10547. (if arg
  10548. (call-interactively 'org-table-recalculate)
  10549. (org-table-maybe-recalculate-line))
  10550. (call-interactively 'org-table-align)
  10551. (orgtbl-send-table 'maybe))
  10552. ((eq action 'recalc)
  10553. (save-excursion
  10554. (beginning-of-line 1)
  10555. (skip-chars-backward " \r\n\t")
  10556. (if (org-at-table-p)
  10557. (org-call-with-arg 'org-table-recalculate t))))
  10558. (t (let (orgtbl-mode)
  10559. (call-interactively (key-binding "\C-c\C-c")))))))
  10560. (defun orgtbl-tab (arg)
  10561. "Justification and field motion for `orgtbl-mode'."
  10562. (interactive "P")
  10563. (if arg (org-table-edit-field t)
  10564. (org-table-justify-field-maybe)
  10565. (org-table-next-field)))
  10566. (defun orgtbl-ret ()
  10567. "Justification and field motion for `orgtbl-mode'."
  10568. (interactive)
  10569. (org-table-justify-field-maybe)
  10570. (org-table-next-row))
  10571. (defun orgtbl-self-insert-command (N)
  10572. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10573. If the cursor is in a table looking at whitespace, the whitespace is
  10574. overwritten, and the table is not marked as requiring realignment."
  10575. (interactive "p")
  10576. (if (and (org-at-table-p)
  10577. (or
  10578. (and org-table-auto-blank-field
  10579. (member last-command
  10580. '(orgtbl-hijacker-command-100
  10581. orgtbl-hijacker-command-101
  10582. orgtbl-hijacker-command-102
  10583. orgtbl-hijacker-command-103
  10584. orgtbl-hijacker-command-104
  10585. orgtbl-hijacker-command-105))
  10586. (org-table-blank-field))
  10587. t)
  10588. (eq N 1)
  10589. (looking-at "[^|\n]* +|"))
  10590. (let (org-table-may-need-update)
  10591. (goto-char (1- (match-end 0)))
  10592. (delete-backward-char 1)
  10593. (goto-char (match-beginning 0))
  10594. (self-insert-command N))
  10595. (setq org-table-may-need-update t)
  10596. (let (orgtbl-mode)
  10597. (call-interactively (key-binding (vector last-input-event))))))
  10598. (defun org-force-self-insert (N)
  10599. "Needed to enforce self-insert under remapping."
  10600. (interactive "p")
  10601. (self-insert-command N))
  10602. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10603. "Regula expression matching exponentials as produced by calc.")
  10604. (defvar org-table-clean-did-remove-column nil)
  10605. (defun orgtbl-export (table target)
  10606. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10607. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10608. org-table-last-alignment org-table-last-column-widths
  10609. maxcol column)
  10610. (if (not (fboundp func))
  10611. (error "Cannot export orgtbl table to %s" target))
  10612. (setq lines (org-table-clean-before-export lines))
  10613. (setq table
  10614. (mapcar
  10615. (lambda (x)
  10616. (if (string-match org-table-hline-regexp x)
  10617. 'hline
  10618. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10619. lines))
  10620. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10621. table)))
  10622. (loop for i from (1- maxcol) downto 0 do
  10623. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10624. (setq column (delq nil column))
  10625. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10626. (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))
  10627. (funcall func table nil)))
  10628. (defun orgtbl-send-table (&optional maybe)
  10629. "Send a tranformed version of this table to the receiver position.
  10630. With argument MAYBE, fail quietly if no transformation is defined for
  10631. this table."
  10632. (interactive)
  10633. (catch 'exit
  10634. (unless (org-at-table-p) (error "Not at a table"))
  10635. ;; when non-interactive, we assume align has just happened.
  10636. (when (interactive-p) (org-table-align))
  10637. (save-excursion
  10638. (goto-char (org-table-begin))
  10639. (beginning-of-line 0)
  10640. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10641. (if maybe
  10642. (throw 'exit nil)
  10643. (error "Don't know how to transform this table."))))
  10644. (let* ((name (match-string 1))
  10645. beg
  10646. (transform (intern (match-string 2)))
  10647. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10648. (skip (plist-get params :skip))
  10649. (skipcols (plist-get params :skipcols))
  10650. (txt (buffer-substring-no-properties
  10651. (org-table-begin) (org-table-end)))
  10652. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10653. (lines (org-table-clean-before-export lines))
  10654. (i0 (if org-table-clean-did-remove-column 2 1))
  10655. (table (mapcar
  10656. (lambda (x)
  10657. (if (string-match org-table-hline-regexp x)
  10658. 'hline
  10659. (org-remove-by-index
  10660. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10661. skipcols i0)))
  10662. lines))
  10663. (fun (if (= i0 2) 'cdr 'identity))
  10664. (org-table-last-alignment
  10665. (org-remove-by-index (funcall fun org-table-last-alignment)
  10666. skipcols i0))
  10667. (org-table-last-column-widths
  10668. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10669. skipcols i0)))
  10670. (unless (fboundp transform)
  10671. (error "No such transformation function %s" transform))
  10672. (setq txt (funcall transform table params))
  10673. ;; Find the insertion place
  10674. (save-excursion
  10675. (goto-char (point-min))
  10676. (unless (re-search-forward
  10677. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10678. (error "Don't know where to insert translated table"))
  10679. (goto-char (match-beginning 0))
  10680. (beginning-of-line 2)
  10681. (setq beg (point))
  10682. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10683. (error "Cannot find end of insertion region"))
  10684. (beginning-of-line 1)
  10685. (delete-region beg (point))
  10686. (goto-char beg)
  10687. (insert txt "\n"))
  10688. (message "Table converted and installed at receiver location"))))
  10689. (defun org-remove-by-index (list indices &optional i0)
  10690. "Remove the elements in LIST with indices in INDICES.
  10691. First element has index 0, or I0 if given."
  10692. (if (not indices)
  10693. list
  10694. (if (integerp indices) (setq indices (list indices)))
  10695. (setq i0 (1- (or i0 0)))
  10696. (delq :rm (mapcar (lambda (x)
  10697. (setq i0 (1+ i0))
  10698. (if (memq i0 indices) :rm x))
  10699. list))))
  10700. (defun orgtbl-toggle-comment ()
  10701. "Comment or uncomment the orgtbl at point."
  10702. (interactive)
  10703. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10704. (re2 (concat "^" orgtbl-line-start-regexp))
  10705. (commented (save-excursion (beginning-of-line 1)
  10706. (cond ((looking-at re1) t)
  10707. ((looking-at re2) nil)
  10708. (t (error "Not at an org table")))))
  10709. (re (if commented re1 re2))
  10710. beg end)
  10711. (save-excursion
  10712. (beginning-of-line 1)
  10713. (while (looking-at re) (beginning-of-line 0))
  10714. (beginning-of-line 2)
  10715. (setq beg (point))
  10716. (while (looking-at re) (beginning-of-line 2))
  10717. (setq end (point)))
  10718. (comment-region beg end (if commented '(4) nil))))
  10719. (defun orgtbl-insert-radio-table ()
  10720. "Insert a radio table template appropriate for this major mode."
  10721. (interactive)
  10722. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10723. (txt (nth 1 e))
  10724. name pos)
  10725. (unless e (error "No radio table setup defined for %s" major-mode))
  10726. (setq name (read-string "Table name: "))
  10727. (while (string-match "%n" txt)
  10728. (setq txt (replace-match name t t txt)))
  10729. (or (bolp) (insert "\n"))
  10730. (setq pos (point))
  10731. (insert txt)
  10732. (goto-char pos)))
  10733. (defun org-get-param (params header i sym &optional hsym)
  10734. "Get parameter value for symbol SYM.
  10735. If this is a header line, actually get the value for the symbol with an
  10736. additional \"h\" inserted after the colon.
  10737. If the value is a protperty list, get the element for the current column.
  10738. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10739. (let ((val (plist-get params sym)))
  10740. (and hsym header (setq val (or (plist-get params hsym) val)))
  10741. (if (consp val) (plist-get val i) val)))
  10742. (defun orgtbl-to-generic (table params)
  10743. "Convert the orgtbl-mode TABLE to some other format.
  10744. This generic routine can be used for many standard cases.
  10745. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10746. separator line, or a list of fields for that line.
  10747. PARAMS is a property list of parameters that can influence the conversion.
  10748. For the generic converter, some parameters are obligatory: You need to
  10749. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10750. :splice, you must have :tstart and :tend.
  10751. Valid parameters are
  10752. :tstart String to start the table. Ignored when :splice is t.
  10753. :tend String to end the table. Ignored when :splice is t.
  10754. :splice When set to t, return only table body lines, don't wrap
  10755. them into :tstart and :tend. Default is nil.
  10756. :hline String to be inserted on horizontal separation lines.
  10757. May be nil to ignore hlines.
  10758. :lstart String to start a new table line.
  10759. :lend String to end a table line
  10760. :sep Separator between two fields
  10761. :lfmt Format for entire line, with enough %s to capture all fields.
  10762. If this is present, :lstart, :lend, and :sep are ignored.
  10763. :fmt A format to be used to wrap the field, should contain
  10764. %s for the original field value. For example, to wrap
  10765. everything in dollars, you could use :fmt \"$%s$\".
  10766. This may also be a property list with column numbers and
  10767. formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10768. :hlstart :hlend :hlsep :hlfmt :hfmt
  10769. Same as above, specific for the header lines in the table.
  10770. All lines before the first hline are treated as header.
  10771. If any of these is not present, the data line value is used.
  10772. :efmt Use this format to print numbers with exponentials.
  10773. The format should have %s twice for inserting mantissa
  10774. and exponent, for example \"%s\\\\times10^{%s}\". This
  10775. may also be a property list with column numbers and
  10776. formats. :fmt will still be applied after :efmt.
  10777. In addition to this, the parameters :skip and :skipcols are always handled
  10778. directly by `orgtbl-send-table'. See manual."
  10779. (interactive)
  10780. (let* ((p params)
  10781. (splicep (plist-get p :splice))
  10782. (hline (plist-get p :hline))
  10783. rtn line i fm efm lfmt h)
  10784. ;; Do we have a header?
  10785. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10786. (setq h t))
  10787. ;; Put header
  10788. (unless splicep
  10789. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10790. ;; Now loop over all lines
  10791. (while (setq line (pop table))
  10792. (if (eq line 'hline)
  10793. ;; A horizontal separator line
  10794. (progn (if hline (push hline rtn))
  10795. (setq h nil)) ; no longer in header
  10796. ;; A normal line. Convert the fields, push line onto the result list
  10797. (setq i 0)
  10798. (setq line
  10799. (mapcar
  10800. (lambda (f)
  10801. (setq i (1+ i)
  10802. fm (org-get-param p h i :fmt :hfmt)
  10803. efm (org-get-param p h i :efmt))
  10804. (if (and efm (string-match orgtbl-exp-regexp f))
  10805. (setq f (format
  10806. efm (match-string 1 f) (match-string 2 f))))
  10807. (if fm (setq f (format fm f)))
  10808. f)
  10809. line))
  10810. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10811. (push (apply 'format lfmt line) rtn)
  10812. (push (concat
  10813. (org-get-param p h i :lstart :hlstart)
  10814. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10815. (org-get-param p h i :lend :hlend))
  10816. rtn))))
  10817. (unless splicep
  10818. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10819. (mapconcat 'identity (nreverse rtn) "\n")))
  10820. (defun orgtbl-to-latex (table params)
  10821. "Convert the orgtbl-mode TABLE to LaTeX.
  10822. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10823. separator line, or a list of fields for that line.
  10824. PARAMS is a property list of parameters that can influence the conversion.
  10825. Supports all parameters from `orgtbl-to-generic'. Most important for
  10826. LaTeX are:
  10827. :splice When set to t, return only table body lines, don't wrap
  10828. them into a tabular environment. Default is nil.
  10829. :fmt A format to be used to wrap the field, should contain %s for the
  10830. original field value. For example, to wrap everything in dollars,
  10831. use :fmt \"$%s$\". This may also be a property list with column
  10832. numbers and formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10833. :efmt Format for transforming numbers with exponentials. The format
  10834. should have %s twice for inserting mantissa and exponent, for
  10835. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10836. This may also be a property list with column numbers and formats.
  10837. The general parameters :skip and :skipcols have already been applied when
  10838. this function is called."
  10839. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10840. org-table-last-alignment ""))
  10841. (params2
  10842. (list
  10843. :tstart (concat "\\begin{tabular}{" alignment "}")
  10844. :tend "\\end{tabular}"
  10845. :lstart "" :lend " \\\\" :sep " & "
  10846. :efmt "%s\\,(%s)" :hline "\\hline")))
  10847. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10848. (defun orgtbl-to-html (table params)
  10849. "Convert the orgtbl-mode TABLE to LaTeX.
  10850. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10851. separator line, or a list of fields for that line.
  10852. PARAMS is a property list of parameters that can influence the conversion.
  10853. Currently this function recognizes the following parameters:
  10854. :splice When set to t, return only table body lines, don't wrap
  10855. them into a <table> environment. Default is nil.
  10856. The general parameters :skip and :skipcols have already been applied when
  10857. this function is called. The function does *not* use `orgtbl-to-generic',
  10858. so you cannot specify parameters for it."
  10859. (let* ((splicep (plist-get params :splice))
  10860. html)
  10861. ;; Just call the formatter we already have
  10862. ;; We need to make text lines for it, so put the fields back together.
  10863. (setq html (org-format-org-table-html
  10864. (mapcar
  10865. (lambda (x)
  10866. (if (eq x 'hline)
  10867. "|----+----|"
  10868. (concat "| " (mapconcat 'identity x " | ") " |")))
  10869. table)
  10870. splicep))
  10871. (if (string-match "\n+\\'" html)
  10872. (setq html (replace-match "" t t html)))
  10873. html))
  10874. (defun orgtbl-to-texinfo (table params)
  10875. "Convert the orgtbl-mode TABLE to TeXInfo.
  10876. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10877. separator line, or a list of fields for that line.
  10878. PARAMS is a property list of parameters that can influence the conversion.
  10879. Supports all parameters from `orgtbl-to-generic'. Most important for
  10880. TeXInfo are:
  10881. :splice nil/t When set to t, return only table body lines, don't wrap
  10882. them into a multitable environment. Default is nil.
  10883. :fmt fmt A format to be used to wrap the field, should contain
  10884. %s for the original field value. For example, to wrap
  10885. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10886. This may also be a property list with column numbers and
  10887. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10888. :cf \"f1 f2..\" The column fractions for the table. By default these
  10889. are computed automatically from the width of the columns
  10890. under org-mode.
  10891. The general parameters :skip and :skipcols have already been applied when
  10892. this function is called."
  10893. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10894. (colfrac (or (plist-get params :cf)
  10895. (mapconcat
  10896. (lambda (x) (format "%.3f" (/ (float x) total)))
  10897. org-table-last-column-widths " ")))
  10898. (params2
  10899. (list
  10900. :tstart (concat "@multitable @columnfractions " colfrac)
  10901. :tend "@end multitable"
  10902. :lstart "@item " :lend "" :sep " @tab "
  10903. :hlstart "@headitem ")))
  10904. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10905. ;;;; Link Stuff
  10906. ;;; Link abbreviations
  10907. (defun org-link-expand-abbrev (link)
  10908. "Apply replacements as defined in `org-link-abbrev-alist."
  10909. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10910. (let* ((key (match-string 1 link))
  10911. (as (or (assoc key org-link-abbrev-alist-local)
  10912. (assoc key org-link-abbrev-alist)))
  10913. (tag (and (match-end 2) (match-string 3 link)))
  10914. rpl)
  10915. (if (not as)
  10916. link
  10917. (setq rpl (cdr as))
  10918. (cond
  10919. ((symbolp rpl) (funcall rpl tag))
  10920. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10921. (t (concat rpl tag)))))
  10922. link))
  10923. ;;; Storing and inserting links
  10924. (defvar org-insert-link-history nil
  10925. "Minibuffer history for links inserted with `org-insert-link'.")
  10926. (defvar org-stored-links nil
  10927. "Contains the links stored with `org-store-link'.")
  10928. (defvar org-store-link-plist nil
  10929. "Plist with info about the most recently link created with `org-store-link'.")
  10930. (defvar org-link-protocols nil
  10931. "Link protocols added to Org-mode using `org-add-link-type'.")
  10932. (defvar org-store-link-functions nil
  10933. "List of functions that are called to create and store a link.
  10934. Each function will be called in turn until one returns a non-nil
  10935. value. Each function should check if it is responsible for creating
  10936. this link (for example by looking at the major mode).
  10937. If not, it must exit and return nil.
  10938. If yes, it should return a non-nil value after a calling
  10939. `org-store-link-props' with a list of properties and values.
  10940. Special properties are:
  10941. :type The link prefix. like \"http\". This must be given.
  10942. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10943. This is obligatory as well.
  10944. :description Optional default description for the second pair
  10945. of brackets in an Org-mode link. The user can still change
  10946. this when inserting this link into an Org-mode buffer.
  10947. In addition to these, any additional properties can be specified
  10948. and then used in remember templates.")
  10949. (defun org-add-link-type (type &optional follow publish)
  10950. "Add TYPE to the list of `org-link-types'.
  10951. Re-compute all regular expressions depending on `org-link-types'
  10952. FOLLOW and PUBLISH are two functions. Both take the link path as
  10953. an argument.
  10954. FOLLOW should do whatever is necessary to follow the link, for example
  10955. to find a file or display a mail message.
  10956. PUBLISH takes the path and retuns the string that should be used when
  10957. this document is published. FIMXE: This is actually not yet implemented."
  10958. (add-to-list 'org-link-types type t)
  10959. (org-make-link-regexps)
  10960. (add-to-list 'org-link-protocols
  10961. (list type follow publish)))
  10962. (defun org-add-agenda-custom-command (entry)
  10963. "Replace or add a command in `org-agenda-custom-commands'.
  10964. This is mostly for hacking and trying a new command - once the command
  10965. works you probably want to add it to `org-agenda-custom-commands' for good."
  10966. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10967. (if ass
  10968. (setcdr ass (cdr entry))
  10969. (push entry org-agenda-custom-commands))))
  10970. ;;;###autoload
  10971. (defun org-store-link (arg)
  10972. "\\<org-mode-map>Store an org-link to the current location.
  10973. This link is added to `org-stored-links' and can later be inserted
  10974. into an org-buffer with \\[org-insert-link].
  10975. For some link types, a prefix arg is interpreted:
  10976. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10977. For file links, arg negates `org-context-in-file-links'."
  10978. (interactive "P")
  10979. (setq org-store-link-plist nil) ; reset
  10980. (let (link cpltxt desc description search txt)
  10981. (cond
  10982. ((run-hook-with-args-until-success 'org-store-link-functions)
  10983. (setq link (plist-get org-store-link-plist :link)
  10984. desc (or (plist-get org-store-link-plist :description) link)))
  10985. ((eq major-mode 'bbdb-mode)
  10986. (let ((name (bbdb-record-name (bbdb-current-record)))
  10987. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10988. (setq cpltxt (concat "bbdb:" (or name company))
  10989. link (org-make-link cpltxt))
  10990. (org-store-link-props :type "bbdb" :name name :company company)))
  10991. ((eq major-mode 'Info-mode)
  10992. (setq link (org-make-link "info:"
  10993. (file-name-nondirectory Info-current-file)
  10994. ":" Info-current-node))
  10995. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10996. ":" Info-current-node))
  10997. (org-store-link-props :type "info" :file Info-current-file
  10998. :node Info-current-node))
  10999. ((eq major-mode 'calendar-mode)
  11000. (let ((cd (calendar-cursor-to-date)))
  11001. (setq link
  11002. (format-time-string
  11003. (car org-time-stamp-formats)
  11004. (apply 'encode-time
  11005. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  11006. nil nil nil))))
  11007. (org-store-link-props :type "calendar" :date cd)))
  11008. ((or (eq major-mode 'vm-summary-mode)
  11009. (eq major-mode 'vm-presentation-mode))
  11010. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  11011. (vm-follow-summary-cursor)
  11012. (save-excursion
  11013. (vm-select-folder-buffer)
  11014. (let* ((message (car vm-message-pointer))
  11015. (folder buffer-file-name)
  11016. (subject (vm-su-subject message))
  11017. (to (vm-get-header-contents message "To"))
  11018. (from (vm-get-header-contents message "From"))
  11019. (message-id (vm-su-message-id message)))
  11020. (org-store-link-props :type "vm" :from from :to to :subject subject
  11021. :message-id message-id)
  11022. (setq message-id (org-remove-angle-brackets message-id))
  11023. (setq folder (abbreviate-file-name folder))
  11024. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  11025. folder)
  11026. (setq folder (replace-match "" t t folder)))
  11027. (setq cpltxt (org-email-link-description))
  11028. (setq link (org-make-link "vm:" folder "#" message-id)))))
  11029. ((eq major-mode 'wl-summary-mode)
  11030. (let* ((msgnum (wl-summary-message-number))
  11031. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  11032. msgnum 'message-id))
  11033. (wl-message-entity
  11034. (if (fboundp 'elmo-message-entity)
  11035. (elmo-message-entity
  11036. wl-summary-buffer-elmo-folder msgnum)
  11037. (elmo-msgdb-overview-get-entity
  11038. msgnum (wl-summary-buffer-msgdb))))
  11039. (from (wl-summary-line-from))
  11040. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  11041. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  11042. (wl-summary-line-subject))))
  11043. (org-store-link-props :type "wl" :from from :to to
  11044. :subject subject :message-id message-id)
  11045. (setq message-id (org-remove-angle-brackets message-id))
  11046. (setq cpltxt (org-email-link-description))
  11047. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  11048. "#" message-id))))
  11049. ((or (equal major-mode 'mh-folder-mode)
  11050. (equal major-mode 'mh-show-mode))
  11051. (let ((from (org-mhe-get-header "From:"))
  11052. (to (org-mhe-get-header "To:"))
  11053. (message-id (org-mhe-get-header "Message-Id:"))
  11054. (subject (org-mhe-get-header "Subject:")))
  11055. (org-store-link-props :type "mh" :from from :to to
  11056. :subject subject :message-id message-id)
  11057. (setq cpltxt (org-email-link-description))
  11058. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  11059. (org-remove-angle-brackets message-id)))))
  11060. ((or (eq major-mode 'rmail-mode)
  11061. (eq major-mode 'rmail-summary-mode))
  11062. (save-window-excursion
  11063. (save-restriction
  11064. (when (eq major-mode 'rmail-summary-mode)
  11065. (rmail-show-message rmail-current-message))
  11066. (rmail-narrow-to-non-pruned-header)
  11067. (let ((folder buffer-file-name)
  11068. (message-id (mail-fetch-field "message-id"))
  11069. (from (mail-fetch-field "from"))
  11070. (to (mail-fetch-field "to"))
  11071. (subject (mail-fetch-field "subject")))
  11072. (org-store-link-props
  11073. :type "rmail" :from from :to to
  11074. :subject subject :message-id message-id)
  11075. (setq message-id (org-remove-angle-brackets message-id))
  11076. (setq cpltxt (org-email-link-description))
  11077. (setq link (org-make-link "rmail:" folder "#" message-id)))
  11078. (rmail-show-message rmail-current-message))))
  11079. ((eq major-mode 'gnus-group-mode)
  11080. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  11081. (gnus-group-group-name)) ; version
  11082. ((fboundp 'gnus-group-name)
  11083. (gnus-group-name))
  11084. (t "???"))))
  11085. (unless group (error "Not on a group"))
  11086. (org-store-link-props :type "gnus" :group group)
  11087. (setq cpltxt (concat
  11088. (if (org-xor arg org-usenet-links-prefer-google)
  11089. "http://groups.google.com/groups?group="
  11090. "gnus:")
  11091. group)
  11092. link (org-make-link cpltxt))))
  11093. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  11094. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  11095. (let* ((group gnus-newsgroup-name)
  11096. (article (gnus-summary-article-number))
  11097. (header (gnus-summary-article-header article))
  11098. (from (mail-header-from header))
  11099. (message-id (mail-header-id header))
  11100. (date (mail-header-date header))
  11101. (subject (gnus-summary-subject-string)))
  11102. (org-store-link-props :type "gnus" :from from :subject subject
  11103. :message-id message-id :group group)
  11104. (setq cpltxt (org-email-link-description))
  11105. (if (org-xor arg org-usenet-links-prefer-google)
  11106. (setq link
  11107. (concat
  11108. cpltxt "\n "
  11109. (format "http://groups.google.com/groups?as_umsgid=%s"
  11110. (org-fixup-message-id-for-http message-id))))
  11111. (setq link (org-make-link "gnus:" group
  11112. "#" (number-to-string article))))))
  11113. ((eq major-mode 'w3-mode)
  11114. (setq cpltxt (url-view-url t)
  11115. link (org-make-link cpltxt))
  11116. (org-store-link-props :type "w3" :url (url-view-url t)))
  11117. ((eq major-mode 'w3m-mode)
  11118. (setq cpltxt (or w3m-current-title w3m-current-url)
  11119. link (org-make-link w3m-current-url))
  11120. (org-store-link-props :type "w3m" :url (url-view-url t)))
  11121. ((setq search (run-hook-with-args-until-success
  11122. 'org-create-file-search-functions))
  11123. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  11124. "::" search))
  11125. (setq cpltxt (or description link)))
  11126. ((eq major-mode 'image-mode)
  11127. (setq cpltxt (concat "file:"
  11128. (abbreviate-file-name buffer-file-name))
  11129. link (org-make-link cpltxt))
  11130. (org-store-link-props :type "image" :file buffer-file-name))
  11131. ((eq major-mode 'dired-mode)
  11132. ;; link to the file in the current line
  11133. (setq cpltxt (concat "file:"
  11134. (abbreviate-file-name
  11135. (expand-file-name
  11136. (dired-get-filename nil t))))
  11137. link (org-make-link cpltxt)))
  11138. ((and buffer-file-name (org-mode-p))
  11139. ;; Just link to current headline
  11140. (setq cpltxt (concat "file:"
  11141. (abbreviate-file-name buffer-file-name)))
  11142. ;; Add a context search string
  11143. (when (org-xor org-context-in-file-links arg)
  11144. ;; Check if we are on a target
  11145. (if (org-in-regexp "<<\\(.*?\\)>>")
  11146. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  11147. (setq txt (cond
  11148. ((org-on-heading-p) nil)
  11149. ((org-region-active-p)
  11150. (buffer-substring (region-beginning) (region-end)))
  11151. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  11152. (when (or (null txt) (string-match "\\S-" txt))
  11153. (setq cpltxt
  11154. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11155. desc "NONE"))))
  11156. (if (string-match "::\\'" cpltxt)
  11157. (setq cpltxt (substring cpltxt 0 -2)))
  11158. (setq link (org-make-link cpltxt)))
  11159. ((buffer-file-name (buffer-base-buffer))
  11160. ;; Just link to this file here.
  11161. (setq cpltxt (concat "file:"
  11162. (abbreviate-file-name
  11163. (buffer-file-name (buffer-base-buffer)))))
  11164. ;; Add a context string
  11165. (when (org-xor org-context-in-file-links arg)
  11166. (setq txt (if (org-region-active-p)
  11167. (buffer-substring (region-beginning) (region-end))
  11168. (buffer-substring (point-at-bol) (point-at-eol))))
  11169. ;; Only use search option if there is some text.
  11170. (when (string-match "\\S-" txt)
  11171. (setq cpltxt
  11172. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  11173. desc "NONE")))
  11174. (setq link (org-make-link cpltxt)))
  11175. ((interactive-p)
  11176. (error "Cannot link to a buffer which is not visiting a file"))
  11177. (t (setq link nil)))
  11178. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  11179. (setq link (or link cpltxt)
  11180. desc (or desc cpltxt))
  11181. (if (equal desc "NONE") (setq desc nil))
  11182. (if (and (interactive-p) link)
  11183. (progn
  11184. (setq org-stored-links
  11185. (cons (list link desc) org-stored-links))
  11186. (message "Stored: %s" (or desc link)))
  11187. (and link (org-make-link-string link desc)))))
  11188. (defun org-store-link-props (&rest plist)
  11189. "Store link properties, extract names and addresses."
  11190. (let (x adr)
  11191. (when (setq x (plist-get plist :from))
  11192. (setq adr (mail-extract-address-components x))
  11193. (plist-put plist :fromname (car adr))
  11194. (plist-put plist :fromaddress (nth 1 adr)))
  11195. (when (setq x (plist-get plist :to))
  11196. (setq adr (mail-extract-address-components x))
  11197. (plist-put plist :toname (car adr))
  11198. (plist-put plist :toaddress (nth 1 adr))))
  11199. (let ((from (plist-get plist :from))
  11200. (to (plist-get plist :to)))
  11201. (when (and from to org-from-is-user-regexp)
  11202. (plist-put plist :fromto
  11203. (if (string-match org-from-is-user-regexp from)
  11204. (concat "to %t")
  11205. (concat "from %f")))))
  11206. (setq org-store-link-plist plist))
  11207. (defun org-email-link-description (&optional fmt)
  11208. "Return the description part of an email link.
  11209. This takes information from `org-store-link-plist' and formats it
  11210. according to FMT (default from `org-email-link-description-format')."
  11211. (setq fmt (or fmt org-email-link-description-format))
  11212. (let* ((p org-store-link-plist)
  11213. (to (plist-get p :toaddress))
  11214. (from (plist-get p :fromaddress))
  11215. (table
  11216. (list
  11217. (cons "%c" (plist-get p :fromto))
  11218. (cons "%F" (plist-get p :from))
  11219. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11220. (cons "%T" (plist-get p :to))
  11221. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11222. (cons "%s" (plist-get p :subject))
  11223. (cons "%m" (plist-get p :message-id)))))
  11224. (when (string-match "%c" fmt)
  11225. ;; Check if the user wrote this message
  11226. (if (and org-from-is-user-regexp from to
  11227. (save-match-data (string-match org-from-is-user-regexp from)))
  11228. (setq fmt (replace-match "to %t" t t fmt))
  11229. (setq fmt (replace-match "from %f" t t fmt))))
  11230. (org-replace-escapes fmt table)))
  11231. (defun org-make-org-heading-search-string (&optional string heading)
  11232. "Make search string for STRING or current headline."
  11233. (interactive)
  11234. (let ((s (or string (org-get-heading))))
  11235. (unless (and string (not heading))
  11236. ;; We are using a headline, clean up garbage in there.
  11237. (if (string-match org-todo-regexp s)
  11238. (setq s (replace-match "" t t s)))
  11239. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11240. (setq s (replace-match "" t t s)))
  11241. (setq s (org-trim s))
  11242. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11243. org-comment-string "\\)") s)
  11244. (setq s (replace-match "" t t s)))
  11245. (while (string-match org-ts-regexp s)
  11246. (setq s (replace-match "" t t s))))
  11247. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11248. (setq s (replace-match " " t t s)))
  11249. (or string (setq s (concat "*" s))) ; Add * for headlines
  11250. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11251. (defun org-make-link (&rest strings)
  11252. "Concatenate STRINGS."
  11253. (apply 'concat strings))
  11254. (defun org-make-link-string (link &optional description)
  11255. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11256. (unless (string-match "\\S-" link)
  11257. (error "Empty link"))
  11258. (when (stringp description)
  11259. ;; Remove brackets from the description, they are fatal.
  11260. (while (string-match "\\[" description)
  11261. (setq description (replace-match "{" t t description)))
  11262. (while (string-match "\\]" description)
  11263. (setq description (replace-match "}" t t description))))
  11264. (when (equal (org-link-escape link) description)
  11265. ;; No description needed, it is identical
  11266. (setq description nil))
  11267. (when (and (not description)
  11268. (not (equal link (org-link-escape link))))
  11269. (setq description link))
  11270. (concat "[[" (org-link-escape link) "]"
  11271. (if description (concat "[" description "]") "")
  11272. "]"))
  11273. (defconst org-link-escape-chars
  11274. '((?\ . "%20")
  11275. (?\[ . "%5B")
  11276. (?\] . "%5D")
  11277. (?\340 . "%E0") ; `a
  11278. (?\342 . "%E2") ; ^a
  11279. (?\347 . "%E7") ; ,c
  11280. (?\350 . "%E8") ; `e
  11281. (?\351 . "%E9") ; 'e
  11282. (?\352 . "%EA") ; ^e
  11283. (?\356 . "%EE") ; ^i
  11284. (?\364 . "%F4") ; ^o
  11285. (?\371 . "%F9") ; `u
  11286. (?\373 . "%FB") ; ^u
  11287. (?\; . "%3B")
  11288. (?? . "%3F")
  11289. (?= . "%3D")
  11290. (?+ . "%2B")
  11291. )
  11292. "Association list of escapes for some characters problematic in links.
  11293. This is the list that is used for internal purposes.")
  11294. (defconst org-link-escape-chars-browser
  11295. '((?\ . "%20")) ; 32 for the SPC char
  11296. "Association list of escapes for some characters problematic in links.
  11297. This is the list that is used before handing over to the browser.")
  11298. (defun org-link-escape (text &optional table)
  11299. "Escape charaters in TEXT that are problematic for links."
  11300. (setq table (or table org-link-escape-chars))
  11301. (when text
  11302. (let ((re (mapconcat (lambda (x) (regexp-quote
  11303. (char-to-string (car x))))
  11304. table "\\|")))
  11305. (while (string-match re text)
  11306. (setq text
  11307. (replace-match
  11308. (cdr (assoc (string-to-char (match-string 0 text))
  11309. table))
  11310. t t text)))
  11311. text)))
  11312. (defun org-link-unescape (text &optional table)
  11313. "Reverse the action of `org-link-escape'."
  11314. (setq table (or table org-link-escape-chars))
  11315. (when text
  11316. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11317. table "\\|")))
  11318. (while (string-match re text)
  11319. (setq text
  11320. (replace-match
  11321. (char-to-string (car (rassoc (match-string 0 text) table)))
  11322. t t text)))
  11323. text)))
  11324. (defun org-xor (a b)
  11325. "Exclusive or."
  11326. (if a (not b) b))
  11327. (defun org-get-header (header)
  11328. "Find a header field in the current buffer."
  11329. (save-excursion
  11330. (goto-char (point-min))
  11331. (let ((case-fold-search t) s)
  11332. (cond
  11333. ((eq header 'from)
  11334. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11335. (setq s (match-string 1)))
  11336. (while (string-match "\"" s)
  11337. (setq s (replace-match "" t t s)))
  11338. (if (string-match "[<(].*" s)
  11339. (setq s (replace-match "" t t s))))
  11340. ((eq header 'message-id)
  11341. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11342. (setq s (match-string 1))))
  11343. ((eq header 'subject)
  11344. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11345. (setq s (match-string 1)))))
  11346. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11347. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11348. s)))
  11349. (defun org-fixup-message-id-for-http (s)
  11350. "Replace special characters in a message id, so it can be used in an http query."
  11351. (while (string-match "<" s)
  11352. (setq s (replace-match "%3C" t t s)))
  11353. (while (string-match ">" s)
  11354. (setq s (replace-match "%3E" t t s)))
  11355. (while (string-match "@" s)
  11356. (setq s (replace-match "%40" t t s)))
  11357. s)
  11358. ;;;###autoload
  11359. (defun org-insert-link-global ()
  11360. "Insert a link like Org-mode does.
  11361. This command can be called in any mode to insert a link in Org-mode syntax."
  11362. (interactive)
  11363. (org-run-like-in-org-mode 'org-insert-link))
  11364. (defun org-insert-link (&optional complete-file)
  11365. "Insert a link. At the prompt, enter the link.
  11366. Completion can be used to select a link previously stored with
  11367. `org-store-link'. When the empty string is entered (i.e. if you just
  11368. press RET at the prompt), the link defaults to the most recently
  11369. stored link. As SPC triggers completion in the minibuffer, you need to
  11370. use M-SPC or C-q SPC to force the insertion of a space character.
  11371. You will also be prompted for a description, and if one is given, it will
  11372. be displayed in the buffer instead of the link.
  11373. If there is already a link at point, this command will allow you to edit link
  11374. and description parts.
  11375. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11376. selected using completion. The path to the file will be relative to
  11377. the current directory if the file is in the current directory or a
  11378. subdirectory. Otherwise, the link will be the absolute path as
  11379. completed in the minibuffer (i.e. normally ~/path/to/file).
  11380. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11381. is in the current directory or below.
  11382. With three \\[universal-argument] prefixes, negate the meaning of
  11383. `org-keep-stored-link-after-insertion'."
  11384. (interactive "P")
  11385. (let* ((wcf (current-window-configuration))
  11386. (region (if (org-region-active-p)
  11387. (buffer-substring (region-beginning) (region-end))))
  11388. (remove (and region (list (region-beginning) (region-end))))
  11389. (desc region)
  11390. tmphist ; byte-compile incorrectly complains about this
  11391. link entry file)
  11392. (cond
  11393. ((org-in-regexp org-bracket-link-regexp 1)
  11394. ;; We do have a link at point, and we are going to edit it.
  11395. (setq remove (list (match-beginning 0) (match-end 0)))
  11396. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11397. (setq link (read-string "Link: "
  11398. (org-link-unescape
  11399. (org-match-string-no-properties 1)))))
  11400. ((or (org-in-regexp org-angle-link-re)
  11401. (org-in-regexp org-plain-link-re))
  11402. ;; Convert to bracket link
  11403. (setq remove (list (match-beginning 0) (match-end 0))
  11404. link (read-string "Link: "
  11405. (org-remove-angle-brackets (match-string 0)))))
  11406. ((equal complete-file '(4))
  11407. ;; Completing read for file names.
  11408. (setq file (read-file-name "File: "))
  11409. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11410. (pwd1 (file-name-as-directory (abbreviate-file-name
  11411. (expand-file-name ".")))))
  11412. (cond
  11413. ((equal complete-file '(16))
  11414. (setq link (org-make-link
  11415. "file:"
  11416. (abbreviate-file-name (expand-file-name file)))))
  11417. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11418. (setq link (org-make-link "file:" (match-string 1 file))))
  11419. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11420. (expand-file-name file))
  11421. (setq link (org-make-link
  11422. "file:" (match-string 1 (expand-file-name file)))))
  11423. (t (setq link (org-make-link "file:" file))))))
  11424. (t
  11425. ;; Read link, with completion for stored links.
  11426. (with-output-to-temp-buffer "*Org Links*"
  11427. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11428. (when org-stored-links
  11429. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11430. (princ (mapconcat
  11431. (lambda (x)
  11432. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11433. (reverse org-stored-links) "\n"))))
  11434. (let ((cw (selected-window)))
  11435. (select-window (get-buffer-window "*Org Links*"))
  11436. (shrink-window-if-larger-than-buffer)
  11437. (setq truncate-lines t)
  11438. (select-window cw))
  11439. ;; Fake a link history, containing the stored links.
  11440. (setq tmphist (append (mapcar 'car org-stored-links)
  11441. org-insert-link-history))
  11442. (unwind-protect
  11443. (setq link (org-completing-read
  11444. "Link: "
  11445. (append
  11446. (mapcar (lambda (x) (list (concat (car x) ":")))
  11447. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11448. (mapcar (lambda (x) (list (concat x ":")))
  11449. org-link-types))
  11450. nil nil nil
  11451. 'tmphist
  11452. (or (car (car org-stored-links)))))
  11453. (set-window-configuration wcf)
  11454. (kill-buffer "*Org Links*"))
  11455. (setq entry (assoc link org-stored-links))
  11456. (or entry (push link org-insert-link-history))
  11457. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11458. (not org-keep-stored-link-after-insertion))
  11459. (setq org-stored-links (delq (assoc link org-stored-links)
  11460. org-stored-links)))
  11461. (setq desc (or desc (nth 1 entry)))))
  11462. (if (string-match org-plain-link-re link)
  11463. ;; URL-like link, normalize the use of angular brackets.
  11464. (setq link (org-make-link (org-remove-angle-brackets link))))
  11465. ;; Check if we are linking to the current file with a search option
  11466. ;; If yes, simplify the link by using only the search option.
  11467. (when (and buffer-file-name
  11468. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11469. (let* ((path (match-string 1 link))
  11470. (case-fold-search nil)
  11471. (search (match-string 2 link)))
  11472. (save-match-data
  11473. (if (equal (file-truename buffer-file-name) (file-truename path))
  11474. ;; We are linking to this same file, with a search option
  11475. (setq link search)))))
  11476. ;; Check if we can/should use a relative path. If yes, simplify the link
  11477. (when (string-match "\\<file:\\(.*\\)" link)
  11478. (let* ((path (match-string 1 link))
  11479. (origpath path)
  11480. (case-fold-search nil))
  11481. (cond
  11482. ((eq org-link-file-path-type 'absolute)
  11483. (setq path (abbreviate-file-name (expand-file-name path))))
  11484. ((eq org-link-file-path-type 'noabbrev)
  11485. (setq path (expand-file-name path)))
  11486. ((eq org-link-file-path-type 'relative)
  11487. (setq path (file-relative-name path)))
  11488. (t
  11489. (save-match-data
  11490. (if (string-match (concat "^" (regexp-quote
  11491. (file-name-as-directory
  11492. (expand-file-name "."))))
  11493. (expand-file-name path))
  11494. ;; We are linking a file with relative path name.
  11495. (setq path (substring (expand-file-name path)
  11496. (match-end 0)))))))
  11497. (setq link (concat "file:" path))
  11498. (if (equal desc origpath)
  11499. (setq desc path))))
  11500. (setq desc (read-string "Description: " desc))
  11501. (unless (string-match "\\S-" desc) (setq desc nil))
  11502. (if remove (apply 'delete-region remove))
  11503. (insert (org-make-link-string link desc))))
  11504. (defun org-completing-read (&rest args)
  11505. (let ((minibuffer-local-completion-map
  11506. (copy-keymap minibuffer-local-completion-map)))
  11507. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11508. (apply 'completing-read args)))
  11509. ;;; Opening/following a link
  11510. (defvar org-link-search-failed nil)
  11511. (defun org-next-link ()
  11512. "Move forward to the next link.
  11513. If the link is in hidden text, expose it."
  11514. (interactive)
  11515. (when (and org-link-search-failed (eq this-command last-command))
  11516. (goto-char (point-min))
  11517. (message "Link search wrapped back to beginning of buffer"))
  11518. (setq org-link-search-failed nil)
  11519. (let* ((pos (point))
  11520. (ct (org-context))
  11521. (a (assoc :link ct)))
  11522. (if a (goto-char (nth 2 a)))
  11523. (if (re-search-forward org-any-link-re nil t)
  11524. (progn
  11525. (goto-char (match-beginning 0))
  11526. (if (org-invisible-p) (org-show-context)))
  11527. (goto-char pos)
  11528. (setq org-link-search-failed t)
  11529. (error "No further link found"))))
  11530. (defun org-previous-link ()
  11531. "Move backward to the previous link.
  11532. If the link is in hidden text, expose it."
  11533. (interactive)
  11534. (when (and org-link-search-failed (eq this-command last-command))
  11535. (goto-char (point-max))
  11536. (message "Link search wrapped back to end of buffer"))
  11537. (setq org-link-search-failed nil)
  11538. (let* ((pos (point))
  11539. (ct (org-context))
  11540. (a (assoc :link ct)))
  11541. (if a (goto-char (nth 1 a)))
  11542. (if (re-search-backward org-any-link-re nil t)
  11543. (progn
  11544. (goto-char (match-beginning 0))
  11545. (if (org-invisible-p) (org-show-context)))
  11546. (goto-char pos)
  11547. (setq org-link-search-failed t)
  11548. (error "No further link found"))))
  11549. (defun org-find-file-at-mouse (ev)
  11550. "Open file link or URL at mouse."
  11551. (interactive "e")
  11552. (mouse-set-point ev)
  11553. (org-open-at-point 'in-emacs))
  11554. (defun org-open-at-mouse (ev)
  11555. "Open file link or URL at mouse."
  11556. (interactive "e")
  11557. (mouse-set-point ev)
  11558. (org-open-at-point))
  11559. (defvar org-window-config-before-follow-link nil
  11560. "The window configuration before following a link.
  11561. This is saved in case the need arises to restore it.")
  11562. (defvar org-open-link-marker (make-marker)
  11563. "Marker pointing to the location where `org-open-at-point; was called.")
  11564. ;;;###autoload
  11565. (defun org-open-at-point-global ()
  11566. "Follow a link like Org-mode does.
  11567. This command can be called in any mode to follow a link that has
  11568. Org-mode syntax."
  11569. (interactive)
  11570. (org-run-like-in-org-mode 'org-open-at-point))
  11571. (defun org-open-at-point (&optional in-emacs)
  11572. "Open link at or after point.
  11573. If there is no link at point, this function will search forward up to
  11574. the end of the current subtree.
  11575. Normally, files will be opened by an appropriate application. If the
  11576. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11577. (interactive "P")
  11578. (move-marker org-open-link-marker (point))
  11579. (setq org-window-config-before-follow-link (current-window-configuration))
  11580. (org-remove-occur-highlights nil nil t)
  11581. (if (org-at-timestamp-p t)
  11582. (org-follow-timestamp-link)
  11583. (let (type path link line search (pos (point)))
  11584. (catch 'match
  11585. (save-excursion
  11586. (skip-chars-forward "^]\n\r")
  11587. (when (org-in-regexp org-bracket-link-regexp)
  11588. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11589. (while (string-match " *\n *" link)
  11590. (setq link (replace-match " " t t link)))
  11591. (setq link (org-link-expand-abbrev link))
  11592. (if (string-match org-link-re-with-space2 link)
  11593. (setq type (match-string 1 link) path (match-string 2 link))
  11594. (setq type "thisfile" path link))
  11595. (throw 'match t)))
  11596. (when (get-text-property (point) 'org-linked-text)
  11597. (setq type "thisfile"
  11598. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11599. (1+ (point)) (point))
  11600. path (buffer-substring
  11601. (previous-single-property-change pos 'org-linked-text)
  11602. (next-single-property-change pos 'org-linked-text)))
  11603. (throw 'match t))
  11604. (save-excursion
  11605. (when (or (org-in-regexp org-angle-link-re)
  11606. (org-in-regexp org-plain-link-re))
  11607. (setq type (match-string 1) path (match-string 2))
  11608. (throw 'match t)))
  11609. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11610. (setq type "tree-match"
  11611. path (match-string 1))
  11612. (throw 'match t))
  11613. (save-excursion
  11614. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11615. (setq type "tags"
  11616. path (match-string 1))
  11617. (while (string-match ":" path)
  11618. (setq path (replace-match "+" t t path)))
  11619. (throw 'match t))))
  11620. (unless path
  11621. (error "No link found"))
  11622. ;; Remove any trailing spaces in path
  11623. (if (string-match " +\\'" path)
  11624. (setq path (replace-match "" t t path)))
  11625. (cond
  11626. ((assoc type org-link-protocols)
  11627. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11628. ((equal type "mailto")
  11629. (let ((cmd (car org-link-mailto-program))
  11630. (args (cdr org-link-mailto-program)) args1
  11631. (address path) (subject "") a)
  11632. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11633. (setq address (match-string 1 path)
  11634. subject (org-link-escape (match-string 2 path))))
  11635. (while args
  11636. (cond
  11637. ((not (stringp (car args))) (push (pop args) args1))
  11638. (t (setq a (pop args))
  11639. (if (string-match "%a" a)
  11640. (setq a (replace-match address t t a)))
  11641. (if (string-match "%s" a)
  11642. (setq a (replace-match subject t t a)))
  11643. (push a args1))))
  11644. (apply cmd (nreverse args1))))
  11645. ((member type '("http" "https" "ftp" "news"))
  11646. (browse-url (concat type ":" (org-link-escape
  11647. path org-link-escape-chars-browser))))
  11648. ((member type '("message"))
  11649. (browse-url (concat type ":" path)))
  11650. ((string= type "tags")
  11651. (org-tags-view in-emacs path))
  11652. ((string= type "thisfile")
  11653. (if in-emacs
  11654. (switch-to-buffer-other-window
  11655. (org-get-buffer-for-internal-link (current-buffer)))
  11656. (org-mark-ring-push))
  11657. (let ((cmd `(org-link-search
  11658. ,path
  11659. ,(cond ((equal in-emacs '(4)) 'occur)
  11660. ((equal in-emacs '(16)) 'org-occur)
  11661. (t nil))
  11662. ,pos)))
  11663. (condition-case nil (eval cmd)
  11664. (error (progn (widen) (eval cmd))))))
  11665. ((string= type "tree-match")
  11666. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11667. ((string= type "file")
  11668. (if (string-match "::\\([0-9]+\\)\\'" path)
  11669. (setq line (string-to-number (match-string 1 path))
  11670. path (substring path 0 (match-beginning 0)))
  11671. (if (string-match "::\\(.+\\)\\'" path)
  11672. (setq search (match-string 1 path)
  11673. path (substring path 0 (match-beginning 0)))))
  11674. (if (string-match "[*?{]" (file-name-nondirectory path))
  11675. (dired path)
  11676. (org-open-file path in-emacs line search)))
  11677. ((string= type "news")
  11678. (org-follow-gnus-link path))
  11679. ((string= type "bbdb")
  11680. (org-follow-bbdb-link path))
  11681. ((string= type "info")
  11682. (org-follow-info-link path))
  11683. ((string= type "gnus")
  11684. (let (group article)
  11685. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11686. (error "Error in Gnus link"))
  11687. (setq group (match-string 1 path)
  11688. article (match-string 3 path))
  11689. (org-follow-gnus-link group article)))
  11690. ((string= type "vm")
  11691. (let (folder article)
  11692. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11693. (error "Error in VM link"))
  11694. (setq folder (match-string 1 path)
  11695. article (match-string 3 path))
  11696. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11697. (org-follow-vm-link folder article in-emacs)))
  11698. ((string= type "wl")
  11699. (let (folder article)
  11700. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11701. (error "Error in Wanderlust link"))
  11702. (setq folder (match-string 1 path)
  11703. article (match-string 3 path))
  11704. (org-follow-wl-link folder article)))
  11705. ((string= type "mhe")
  11706. (let (folder article)
  11707. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11708. (error "Error in MHE link"))
  11709. (setq folder (match-string 1 path)
  11710. article (match-string 3 path))
  11711. (org-follow-mhe-link folder article)))
  11712. ((string= type "rmail")
  11713. (let (folder article)
  11714. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11715. (error "Error in RMAIL link"))
  11716. (setq folder (match-string 1 path)
  11717. article (match-string 3 path))
  11718. (org-follow-rmail-link folder article)))
  11719. ((string= type "shell")
  11720. (let ((cmd path))
  11721. (if (or (not org-confirm-shell-link-function)
  11722. (funcall org-confirm-shell-link-function
  11723. (format "Execute \"%s\" in shell? "
  11724. (org-add-props cmd nil
  11725. 'face 'org-warning))))
  11726. (progn
  11727. (message "Executing %s" cmd)
  11728. (shell-command cmd))
  11729. (error "Abort"))))
  11730. ((string= type "elisp")
  11731. (let ((cmd path))
  11732. (if (or (not org-confirm-elisp-link-function)
  11733. (funcall org-confirm-elisp-link-function
  11734. (format "Execute \"%s\" as elisp? "
  11735. (org-add-props cmd nil
  11736. 'face 'org-warning))))
  11737. (message "%s => %s" cmd (eval (read cmd)))
  11738. (error "Abort"))))
  11739. (t
  11740. (browse-url-at-point)))))
  11741. (move-marker org-open-link-marker nil))
  11742. ;;; File search
  11743. (defvar org-create-file-search-functions nil
  11744. "List of functions to construct the right search string for a file link.
  11745. These functions are called in turn with point at the location to
  11746. which the link should point.
  11747. A function in the hook should first test if it would like to
  11748. handle this file type, for example by checking the major-mode or
  11749. the file extension. If it decides not to handle this file, it
  11750. should just return nil to give other functions a chance. If it
  11751. does handle the file, it must return the search string to be used
  11752. when following the link. The search string will be part of the
  11753. file link, given after a double colon, and `org-open-at-point'
  11754. will automatically search for it. If special measures must be
  11755. taken to make the search successful, another function should be
  11756. added to the companion hook `org-execute-file-search-functions',
  11757. which see.
  11758. A function in this hook may also use `setq' to set the variable
  11759. `description' to provide a suggestion for the descriptive text to
  11760. be used for this link when it gets inserted into an Org-mode
  11761. buffer with \\[org-insert-link].")
  11762. (defvar org-execute-file-search-functions nil
  11763. "List of functions to execute a file search triggered by a link.
  11764. Functions added to this hook must accept a single argument, the
  11765. search string that was part of the file link, the part after the
  11766. double colon. The function must first check if it would like to
  11767. handle this search, for example by checking the major-mode or the
  11768. file extension. If it decides not to handle this search, it
  11769. should just return nil to give other functions a chance. If it
  11770. does handle the search, it must return a non-nil value to keep
  11771. other functions from trying.
  11772. Each function can access the current prefix argument through the
  11773. variable `current-prefix-argument'. Note that a single prefix is
  11774. used to force opening a link in Emacs, so it may be good to only
  11775. use a numeric or double prefix to guide the search function.
  11776. In case this is needed, a function in this hook can also restore
  11777. the window configuration before `org-open-at-point' was called using:
  11778. (set-window-configuration org-window-config-before-follow-link)")
  11779. (defun org-link-search (s &optional type avoid-pos)
  11780. "Search for a link search option.
  11781. If S is surrounded by forward slashes, it is interpreted as a
  11782. regular expression. In org-mode files, this will create an `org-occur'
  11783. sparse tree. In ordinary files, `occur' will be used to list matches.
  11784. If the current buffer is in `dired-mode', grep will be used to search
  11785. in all files. If AVOID-POS is given, ignore matches near that position."
  11786. (let ((case-fold-search t)
  11787. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11788. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11789. (append '(("") (" ") ("\t") ("\n"))
  11790. org-emphasis-alist)
  11791. "\\|") "\\)"))
  11792. (pos (point))
  11793. (pre "") (post "")
  11794. words re0 re1 re2 re3 re4 re5 re2a reall)
  11795. (cond
  11796. ;; First check if there are any special
  11797. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11798. ;; Now try the builtin stuff
  11799. ((save-excursion
  11800. (goto-char (point-min))
  11801. (and
  11802. (re-search-forward
  11803. (concat "<<" (regexp-quote s0) ">>") nil t)
  11804. (setq pos (match-beginning 0))))
  11805. ;; There is an exact target for this
  11806. (goto-char pos))
  11807. ((string-match "^/\\(.*\\)/$" s)
  11808. ;; A regular expression
  11809. (cond
  11810. ((org-mode-p)
  11811. (org-occur (match-string 1 s)))
  11812. ;;((eq major-mode 'dired-mode)
  11813. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11814. (t (org-do-occur (match-string 1 s)))))
  11815. (t
  11816. ;; A normal search strings
  11817. (when (equal (string-to-char s) ?*)
  11818. ;; Anchor on headlines, post may include tags.
  11819. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11820. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11821. s (substring s 1)))
  11822. (remove-text-properties
  11823. 0 (length s)
  11824. '(face nil mouse-face nil keymap nil fontified nil) s)
  11825. ;; Make a series of regular expressions to find a match
  11826. (setq words (org-split-string s "[ \n\r\t]+")
  11827. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11828. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11829. "\\)" markers)
  11830. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11831. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11832. re1 (concat pre re2 post)
  11833. re3 (concat pre re4 post)
  11834. re5 (concat pre ".*" re4)
  11835. re2 (concat pre re2)
  11836. re2a (concat pre re2a)
  11837. re4 (concat pre re4)
  11838. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11839. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11840. re5 "\\)"
  11841. ))
  11842. (cond
  11843. ((eq type 'org-occur) (org-occur reall))
  11844. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11845. (t (goto-char (point-min))
  11846. (if (or (org-search-not-self 1 re0 nil t)
  11847. (org-search-not-self 1 re1 nil t)
  11848. (org-search-not-self 1 re2 nil t)
  11849. (org-search-not-self 1 re2a nil t)
  11850. (org-search-not-self 1 re3 nil t)
  11851. (org-search-not-self 1 re4 nil t)
  11852. (org-search-not-self 1 re5 nil t)
  11853. )
  11854. (goto-char (match-beginning 1))
  11855. (goto-char pos)
  11856. (error "No match")))))
  11857. (t
  11858. ;; Normal string-search
  11859. (goto-char (point-min))
  11860. (if (search-forward s nil t)
  11861. (goto-char (match-beginning 0))
  11862. (error "No match"))))
  11863. (and (org-mode-p) (org-show-context 'link-search))))
  11864. (defun org-search-not-self (group &rest args)
  11865. "Execute `re-search-forward', but only accept matches that do not
  11866. enclose the position of `org-open-link-marker'."
  11867. (let ((m org-open-link-marker))
  11868. (catch 'exit
  11869. (while (apply 're-search-forward args)
  11870. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11871. (goto-char (match-end group))
  11872. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11873. (> (match-beginning 0) (marker-position m))
  11874. (< (match-end 0) (marker-position m)))
  11875. (save-match-data
  11876. (or (not (org-in-regexp
  11877. org-bracket-link-analytic-regexp 1))
  11878. (not (match-end 4)) ; no description
  11879. (and (<= (match-beginning 4) (point))
  11880. (>= (match-end 4) (point))))))
  11881. (throw 'exit (point))))))))
  11882. (defun org-get-buffer-for-internal-link (buffer)
  11883. "Return a buffer to be used for displaying the link target of internal links."
  11884. (cond
  11885. ((not org-display-internal-link-with-indirect-buffer)
  11886. buffer)
  11887. ((string-match "(Clone)$" (buffer-name buffer))
  11888. (message "Buffer is already a clone, not making another one")
  11889. ;; we also do not modify visibility in this case
  11890. buffer)
  11891. (t ; make a new indirect buffer for displaying the link
  11892. (let* ((bn (buffer-name buffer))
  11893. (ibn (concat bn "(Clone)"))
  11894. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11895. (with-current-buffer ib (org-overview))
  11896. ib))))
  11897. (defun org-do-occur (regexp &optional cleanup)
  11898. "Call the Emacs command `occur'.
  11899. If CLEANUP is non-nil, remove the printout of the regular expression
  11900. in the *Occur* buffer. This is useful if the regex is long and not useful
  11901. to read."
  11902. (occur regexp)
  11903. (when cleanup
  11904. (let ((cwin (selected-window)) win beg end)
  11905. (when (setq win (get-buffer-window "*Occur*"))
  11906. (select-window win))
  11907. (goto-char (point-min))
  11908. (when (re-search-forward "match[a-z]+" nil t)
  11909. (setq beg (match-end 0))
  11910. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11911. (setq end (1- (match-beginning 0)))))
  11912. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11913. (goto-char (point-min))
  11914. (select-window cwin))))
  11915. ;;; The mark ring for links jumps
  11916. (defvar org-mark-ring nil
  11917. "Mark ring for positions before jumps in Org-mode.")
  11918. (defvar org-mark-ring-last-goto nil
  11919. "Last position in the mark ring used to go back.")
  11920. ;; Fill and close the ring
  11921. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11922. (loop for i from 1 to org-mark-ring-length do
  11923. (push (make-marker) org-mark-ring))
  11924. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11925. org-mark-ring)
  11926. (defun org-mark-ring-push (&optional pos buffer)
  11927. "Put the current position or POS into the mark ring and rotate it."
  11928. (interactive)
  11929. (setq pos (or pos (point)))
  11930. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11931. (move-marker (car org-mark-ring)
  11932. (or pos (point))
  11933. (or buffer (current-buffer)))
  11934. (message "%s"
  11935. (substitute-command-keys
  11936. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11937. (defun org-mark-ring-goto (&optional n)
  11938. "Jump to the previous position in the mark ring.
  11939. With prefix arg N, jump back that many stored positions. When
  11940. called several times in succession, walk through the entire ring.
  11941. Org-mode commands jumping to a different position in the current file,
  11942. or to another Org-mode file, automatically push the old position
  11943. onto the ring."
  11944. (interactive "p")
  11945. (let (p m)
  11946. (if (eq last-command this-command)
  11947. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11948. (setq p org-mark-ring))
  11949. (setq org-mark-ring-last-goto p)
  11950. (setq m (car p))
  11951. (switch-to-buffer (marker-buffer m))
  11952. (goto-char m)
  11953. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11954. (defun org-remove-angle-brackets (s)
  11955. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11956. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11957. s)
  11958. (defun org-add-angle-brackets (s)
  11959. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11960. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11961. s)
  11962. ;;; Following specific links
  11963. (defun org-follow-timestamp-link ()
  11964. (cond
  11965. ((org-at-date-range-p t)
  11966. (let ((org-agenda-start-on-weekday)
  11967. (t1 (match-string 1))
  11968. (t2 (match-string 2)))
  11969. (setq t1 (time-to-days (org-time-string-to-time t1))
  11970. t2 (time-to-days (org-time-string-to-time t2)))
  11971. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11972. ((org-at-timestamp-p t)
  11973. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11974. (substring (match-string 1) 0 10)))
  11975. 1))
  11976. (t (error "This should not happen"))))
  11977. (defun org-follow-bbdb-link (name)
  11978. "Follow a BBDB link to NAME."
  11979. (require 'bbdb)
  11980. (let ((inhibit-redisplay (not debug-on-error))
  11981. (bbdb-electric-p nil))
  11982. (catch 'exit
  11983. ;; Exact match on name
  11984. (bbdb-name (concat "\\`" name "\\'") nil)
  11985. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11986. ;; Exact match on name
  11987. (bbdb-company (concat "\\`" name "\\'") nil)
  11988. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11989. ;; Partial match on name
  11990. (bbdb-name name nil)
  11991. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11992. ;; Partial match on company
  11993. (bbdb-company name nil)
  11994. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11995. ;; General match including network address and notes
  11996. (bbdb name nil)
  11997. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11998. (delete-window (get-buffer-window "*BBDB*"))
  11999. (error "No matching BBDB record")))))
  12000. (defun org-follow-info-link (name)
  12001. "Follow an info file & node link to NAME."
  12002. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  12003. (string-match "\\(.*\\)" name))
  12004. (progn
  12005. (require 'info)
  12006. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  12007. (Info-find-node (match-string 1 name) (match-string 2 name))
  12008. (Info-find-node (match-string 1 name) "Top")))
  12009. (message "Could not open: %s" name)))
  12010. (defun org-follow-gnus-link (&optional group article)
  12011. "Follow a Gnus link to GROUP and ARTICLE."
  12012. (require 'gnus)
  12013. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  12014. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  12015. (cond ((and group article)
  12016. (gnus-group-read-group 1 nil group)
  12017. (gnus-summary-goto-article (string-to-number article) nil t))
  12018. (group (gnus-group-jump-to-group group))))
  12019. (defun org-follow-vm-link (&optional folder article readonly)
  12020. "Follow a VM link to FOLDER and ARTICLE."
  12021. (require 'vm)
  12022. (setq article (org-add-angle-brackets article))
  12023. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  12024. ;; ange-ftp or efs or tramp access
  12025. (let ((user (or (match-string 1 folder) (user-login-name)))
  12026. (host (match-string 2 folder))
  12027. (file (match-string 3 folder)))
  12028. (cond
  12029. ((featurep 'tramp)
  12030. ;; use tramp to access the file
  12031. (if (featurep 'xemacs)
  12032. (setq folder (format "[%s@%s]%s" user host file))
  12033. (setq folder (format "/%s@%s:%s" user host file))))
  12034. (t
  12035. ;; use ange-ftp or efs
  12036. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  12037. (setq folder (format "/%s@%s:%s" user host file))))))
  12038. (when folder
  12039. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  12040. (sit-for 0.1)
  12041. (when article
  12042. (vm-select-folder-buffer)
  12043. (widen)
  12044. (let ((case-fold-search t))
  12045. (goto-char (point-min))
  12046. (if (not (re-search-forward
  12047. (concat "^" "message-id: *" (regexp-quote article))))
  12048. (error "Could not find the specified message in this folder"))
  12049. (vm-isearch-update)
  12050. (vm-isearch-narrow)
  12051. (vm-beginning-of-message)
  12052. (vm-summarize)))))
  12053. (defun org-follow-wl-link (folder article)
  12054. "Follow a Wanderlust link to FOLDER and ARTICLE."
  12055. (if (and (string= folder "%")
  12056. article
  12057. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  12058. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  12059. ;; Thus, we recompose folder and article ids.
  12060. (setq folder (format "%s#%s" folder (match-string 1 article))
  12061. article (match-string 3 article)))
  12062. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  12063. (error "No such folder: %s" folder))
  12064. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  12065. (and article
  12066. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  12067. (wl-summary-redisplay)))
  12068. (defun org-follow-rmail-link (folder article)
  12069. "Follow an RMAIL link to FOLDER and ARTICLE."
  12070. (setq article (org-add-angle-brackets article))
  12071. (let (message-number)
  12072. (save-excursion
  12073. (save-window-excursion
  12074. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12075. (setq message-number
  12076. (save-restriction
  12077. (widen)
  12078. (goto-char (point-max))
  12079. (if (re-search-backward
  12080. (concat "^Message-ID:\\s-+" (regexp-quote
  12081. (or article "")))
  12082. nil t)
  12083. (rmail-what-message))))))
  12084. (if message-number
  12085. (progn
  12086. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  12087. (rmail-show-message message-number)
  12088. message-number)
  12089. (error "Message not found"))))
  12090. ;;; mh-e integration based on planner-mode
  12091. (defun org-mhe-get-message-real-folder ()
  12092. "Return the name of the current message real folder, so if you use
  12093. sequences, it will now work."
  12094. (save-excursion
  12095. (let* ((folder
  12096. (if (equal major-mode 'mh-folder-mode)
  12097. mh-current-folder
  12098. ;; Refer to the show buffer
  12099. mh-show-folder-buffer))
  12100. (end-index
  12101. (if (boundp 'mh-index-folder)
  12102. (min (length mh-index-folder) (length folder))))
  12103. )
  12104. ;; a simple test on mh-index-data does not work, because
  12105. ;; mh-index-data is always nil in a show buffer.
  12106. (if (and (boundp 'mh-index-folder)
  12107. (string= mh-index-folder (substring folder 0 end-index)))
  12108. (if (equal major-mode 'mh-show-mode)
  12109. (save-window-excursion
  12110. (let (pop-up-frames)
  12111. (when (buffer-live-p (get-buffer folder))
  12112. (progn
  12113. (pop-to-buffer folder)
  12114. (org-mhe-get-message-folder-from-index)
  12115. )
  12116. )))
  12117. (org-mhe-get-message-folder-from-index)
  12118. )
  12119. folder
  12120. )
  12121. )))
  12122. (defun org-mhe-get-message-folder-from-index ()
  12123. "Returns the name of the message folder in a index folder buffer."
  12124. (save-excursion
  12125. (mh-index-previous-folder)
  12126. (re-search-forward "^\\(+.*\\)$" nil t)
  12127. (message "%s" (match-string 1))))
  12128. (defun org-mhe-get-message-folder ()
  12129. "Return the name of the current message folder. Be careful if you
  12130. use sequences."
  12131. (save-excursion
  12132. (if (equal major-mode 'mh-folder-mode)
  12133. mh-current-folder
  12134. ;; Refer to the show buffer
  12135. mh-show-folder-buffer)))
  12136. (defun org-mhe-get-message-num ()
  12137. "Return the number of the current message. Be careful if you
  12138. use sequences."
  12139. (save-excursion
  12140. (if (equal major-mode 'mh-folder-mode)
  12141. (mh-get-msg-num nil)
  12142. ;; Refer to the show buffer
  12143. (mh-show-buffer-message-number))))
  12144. (defun org-mhe-get-header (header)
  12145. "Return a header of the message in folder mode. This will create a
  12146. show buffer for the corresponding message. If you have a more clever
  12147. idea..."
  12148. (let* ((folder (org-mhe-get-message-folder))
  12149. (num (org-mhe-get-message-num))
  12150. (buffer (get-buffer-create (concat "show-" folder)))
  12151. (header-field))
  12152. (with-current-buffer buffer
  12153. (mh-display-msg num folder)
  12154. (if (equal major-mode 'mh-folder-mode)
  12155. (mh-header-display)
  12156. (mh-show-header-display))
  12157. (set-buffer buffer)
  12158. (setq header-field (mh-get-header-field header))
  12159. (if (equal major-mode 'mh-folder-mode)
  12160. (mh-show)
  12161. (mh-show-show))
  12162. header-field)))
  12163. (defun org-follow-mhe-link (folder article)
  12164. "Follow an MHE link to FOLDER and ARTICLE.
  12165. If ARTICLE is nil FOLDER is shown. If the configuration variable
  12166. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  12167. ARTICLE is searched in all folders. Indexed searches (swish++,
  12168. namazu, and others supported by MH-E) will always search in all
  12169. folders."
  12170. (require 'mh-e)
  12171. (require 'mh-search)
  12172. (require 'mh-utils)
  12173. (mh-find-path)
  12174. (if (not article)
  12175. (mh-visit-folder (mh-normalize-folder-name folder))
  12176. (setq article (org-add-angle-brackets article))
  12177. (mh-search-choose)
  12178. (if (equal mh-searcher 'pick)
  12179. (progn
  12180. (mh-search folder (list "--message-id" article))
  12181. (when (and org-mhe-search-all-folders
  12182. (not (org-mhe-get-message-real-folder)))
  12183. (kill-this-buffer)
  12184. (mh-search "+" (list "--message-id" article))))
  12185. (mh-search "+" article))
  12186. (if (org-mhe-get-message-real-folder)
  12187. (mh-show-msg 1)
  12188. (kill-this-buffer)
  12189. (error "Message not found"))))
  12190. ;;; BibTeX links
  12191. ;; Use the custom search meachnism to construct and use search strings for
  12192. ;; file links to BibTeX database entries.
  12193. (defun org-create-file-search-in-bibtex ()
  12194. "Create the search string and description for a BibTeX database entry."
  12195. (when (eq major-mode 'bibtex-mode)
  12196. ;; yes, we want to construct this search string.
  12197. ;; Make a good description for this entry, using names, year and the title
  12198. ;; Put it into the `description' variable which is dynamically scoped.
  12199. (let ((bibtex-autokey-names 1)
  12200. (bibtex-autokey-names-stretch 1)
  12201. (bibtex-autokey-name-case-convert-function 'identity)
  12202. (bibtex-autokey-name-separator " & ")
  12203. (bibtex-autokey-additional-names " et al.")
  12204. (bibtex-autokey-year-length 4)
  12205. (bibtex-autokey-name-year-separator " ")
  12206. (bibtex-autokey-titlewords 3)
  12207. (bibtex-autokey-titleword-separator " ")
  12208. (bibtex-autokey-titleword-case-convert-function 'identity)
  12209. (bibtex-autokey-titleword-length 'infty)
  12210. (bibtex-autokey-year-title-separator ": "))
  12211. (setq description (bibtex-generate-autokey)))
  12212. ;; Now parse the entry, get the key and return it.
  12213. (save-excursion
  12214. (bibtex-beginning-of-entry)
  12215. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12216. (defun org-execute-file-search-in-bibtex (s)
  12217. "Find the link search string S as a key for a database entry."
  12218. (when (eq major-mode 'bibtex-mode)
  12219. ;; Yes, we want to do the search in this file.
  12220. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12221. (goto-char (point-min))
  12222. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12223. (regexp-quote s) "[ \t\n]*,") nil t)
  12224. (goto-char (match-beginning 0)))
  12225. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12226. ;; Use double prefix to indicate that any web link should be browsed
  12227. (let ((b (current-buffer)) (p (point)))
  12228. ;; Restore the window configuration because we just use the web link
  12229. (set-window-configuration org-window-config-before-follow-link)
  12230. (save-excursion (set-buffer b) (goto-char p)
  12231. (bibtex-url)))
  12232. (recenter 0)) ; Move entry start to beginning of window
  12233. ;; return t to indicate that the search is done.
  12234. t))
  12235. ;; Finally add the functions to the right hooks.
  12236. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12237. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12238. ;; end of Bibtex link setup
  12239. ;;; Following file links
  12240. (defun org-open-file (path &optional in-emacs line search)
  12241. "Open the file at PATH.
  12242. First, this expands any special file name abbreviations. Then the
  12243. configuration variable `org-file-apps' is checked if it contains an
  12244. entry for this file type, and if yes, the corresponding command is launched.
  12245. If no application is found, Emacs simply visits the file.
  12246. With optional argument IN-EMACS, Emacs will visit the file.
  12247. Optional LINE specifies a line to go to, optional SEARCH a string to
  12248. search for. If LINE or SEARCH is given, the file will always be
  12249. opened in Emacs.
  12250. If the file does not exist, an error is thrown."
  12251. (setq in-emacs (or in-emacs line search))
  12252. (let* ((file (if (equal path "")
  12253. buffer-file-name
  12254. (substitute-in-file-name (expand-file-name path))))
  12255. (apps (append org-file-apps (org-default-apps)))
  12256. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12257. (dirp (if remp nil (file-directory-p file)))
  12258. (dfile (downcase file))
  12259. (old-buffer (current-buffer))
  12260. (old-pos (point))
  12261. (old-mode major-mode)
  12262. ext cmd)
  12263. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12264. (setq ext (match-string 1 dfile))
  12265. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12266. (setq ext (match-string 1 dfile))))
  12267. (if in-emacs
  12268. (setq cmd 'emacs)
  12269. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12270. (and dirp (cdr (assoc 'directory apps)))
  12271. (cdr (assoc ext apps))
  12272. (cdr (assoc t apps)))))
  12273. (when (eq cmd 'mailcap)
  12274. (require 'mailcap)
  12275. (mailcap-parse-mailcaps)
  12276. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12277. (command (mailcap-mime-info mime-type)))
  12278. (if (stringp command)
  12279. (setq cmd command)
  12280. (setq cmd 'emacs))))
  12281. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12282. (not (file-exists-p file))
  12283. (not org-open-non-existing-files))
  12284. (error "No such file: %s" file))
  12285. (cond
  12286. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12287. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12288. (while (string-match "['\"]%s['\"]" cmd)
  12289. (setq cmd (replace-match "%s" t t cmd)))
  12290. (while (string-match "%s" cmd)
  12291. (setq cmd (replace-match
  12292. (save-match-data (shell-quote-argument file))
  12293. t t cmd)))
  12294. (save-window-excursion
  12295. (start-process-shell-command cmd nil cmd)))
  12296. ((or (stringp cmd)
  12297. (eq cmd 'emacs))
  12298. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12299. (widen)
  12300. (if line (goto-line line)
  12301. (if search (org-link-search search))))
  12302. ((consp cmd)
  12303. (eval cmd))
  12304. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12305. (and (org-mode-p) (eq old-mode 'org-mode)
  12306. (or (not (equal old-buffer (current-buffer)))
  12307. (not (equal old-pos (point))))
  12308. (org-mark-ring-push old-pos old-buffer))))
  12309. (defun org-default-apps ()
  12310. "Return the default applications for this operating system."
  12311. (cond
  12312. ((eq system-type 'darwin)
  12313. org-file-apps-defaults-macosx)
  12314. ((eq system-type 'windows-nt)
  12315. org-file-apps-defaults-windowsnt)
  12316. (t org-file-apps-defaults-gnu)))
  12317. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12318. (defun org-file-remote-p (file)
  12319. "Test whether FILE specifies a location on a remote system.
  12320. Return non-nil if the location is indeed remote.
  12321. For example, the filename \"/user@host:/foo\" specifies a location
  12322. on the system \"/user@host:\"."
  12323. (cond ((fboundp 'file-remote-p)
  12324. (file-remote-p file))
  12325. ((fboundp 'tramp-handle-file-remote-p)
  12326. (tramp-handle-file-remote-p file))
  12327. ((and (boundp 'ange-ftp-name-format)
  12328. (string-match (car ange-ftp-name-format) file))
  12329. t)
  12330. (t nil)))
  12331. ;;;; Hooks for remember.el, and refiling
  12332. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12333. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12334. ;;;###autoload
  12335. (defun org-remember-insinuate ()
  12336. "Setup remember.el for use wiht Org-mode."
  12337. (require 'remember)
  12338. (setq remember-annotation-functions '(org-remember-annotation))
  12339. (setq remember-handler-functions '(org-remember-handler))
  12340. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12341. ;;;###autoload
  12342. (defun org-remember-annotation ()
  12343. "Return a link to the current location as an annotation for remember.el.
  12344. If you are using Org-mode files as target for data storage with
  12345. remember.el, then the annotations should include a link compatible with the
  12346. conventions in Org-mode. This function returns such a link."
  12347. (org-store-link nil))
  12348. (defconst org-remember-help
  12349. "Select a destination location for the note.
  12350. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12351. RET on headline -> Store as sublevel entry to current headline
  12352. RET at beg-of-buf -> Append to file as level 2 headline
  12353. <left>/<right> -> before/after current headline, same headings level")
  12354. (defvar org-remember-previous-location nil)
  12355. (defvar org-force-remember-template-char) ;; dynamically scoped
  12356. (defun org-select-remember-template (&optional use-char)
  12357. (when org-remember-templates
  12358. (let* ((templates (mapcar (lambda (x)
  12359. (if (stringp (car x))
  12360. (append (list (nth 1 x) (car x)) (cddr x))
  12361. (append (list (car x) "") (cdr x))))
  12362. org-remember-templates))
  12363. (char (or use-char
  12364. (cond
  12365. ((= (length templates) 1)
  12366. (caar templates))
  12367. ((and (boundp 'org-force-remember-template-char)
  12368. org-force-remember-template-char)
  12369. (if (stringp org-force-remember-template-char)
  12370. (string-to-char org-force-remember-template-char)
  12371. org-force-remember-template-char))
  12372. (t
  12373. (message "Select template: %s"
  12374. (mapconcat
  12375. (lambda (x)
  12376. (cond
  12377. ((not (string-match "\\S-" (nth 1 x)))
  12378. (format "[%c]" (car x)))
  12379. ((equal (downcase (car x))
  12380. (downcase (aref (nth 1 x) 0)))
  12381. (format "[%c]%s" (car x)
  12382. (substring (nth 1 x) 1)))
  12383. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12384. templates " "))
  12385. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12386. (when (equal char0 ?\C-g)
  12387. (jump-to-register remember-register)
  12388. (kill-buffer remember-buffer))
  12389. char0))))))
  12390. (cddr (assoc char templates)))))
  12391. (defvar x-last-selected-text)
  12392. (defvar x-last-selected-text-primary)
  12393. ;;;###autoload
  12394. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12395. "Initialize *remember* buffer with template, invoke `org-mode'.
  12396. This function should be placed into `remember-mode-hook' and in fact requires
  12397. to be run from that hook to function properly."
  12398. (if org-remember-templates
  12399. (let* ((entry (org-select-remember-template use-char))
  12400. (tpl (car entry))
  12401. (plist-p (if org-store-link-plist t nil))
  12402. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12403. (string-match "\\S-" (nth 1 entry)))
  12404. (nth 1 entry)
  12405. org-default-notes-file))
  12406. (headline (nth 2 entry))
  12407. (v-c (or (and (eq window-system 'x)
  12408. (fboundp 'x-cut-buffer-or-selection-value)
  12409. (x-cut-buffer-or-selection-value))
  12410. (org-bound-and-true-p x-last-selected-text)
  12411. (org-bound-and-true-p x-last-selected-text-primary)
  12412. (and (> (length kill-ring) 0) (current-kill 0))))
  12413. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12414. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12415. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12416. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12417. ;; `initial' and `annotation' are bound in `remember'
  12418. (v-i (if (boundp 'initial) initial))
  12419. (v-a (if (and (boundp 'annotation) annotation)
  12420. (if (equal annotation "[[]]") "" annotation)
  12421. ""))
  12422. (v-A (if (and v-a
  12423. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12424. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12425. v-a))
  12426. (v-n user-full-name)
  12427. (org-startup-folded nil)
  12428. org-time-was-given org-end-time-was-given x
  12429. prompt completions char time pos default histvar)
  12430. (setq org-store-link-plist
  12431. (append (list :annotation v-a :initial v-i)
  12432. org-store-link-plist))
  12433. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12434. (erase-buffer)
  12435. (insert (substitute-command-keys
  12436. (format
  12437. "## Filing location: Select interactively, default, or last used:
  12438. ## %s to select file and header location interactively.
  12439. ## %s \"%s\" -> \"* %s\"
  12440. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12441. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12442. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12443. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12444. (abbreviate-file-name (or file org-default-notes-file))
  12445. (or headline "")
  12446. (or (car org-remember-previous-location) "???")
  12447. (or (cdr org-remember-previous-location) "???"))))
  12448. (insert tpl) (goto-char (point-min))
  12449. ;; Simple %-escapes
  12450. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12451. (when (and initial (equal (match-string 0) "%i"))
  12452. (save-match-data
  12453. (let* ((lead (buffer-substring
  12454. (point-at-bol) (match-beginning 0))))
  12455. (setq v-i (mapconcat 'identity
  12456. (org-split-string initial "\n")
  12457. (concat "\n" lead))))))
  12458. (replace-match
  12459. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12460. t t))
  12461. ;; %[] Insert contents of a file.
  12462. (goto-char (point-min))
  12463. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12464. (let ((start (match-beginning 0))
  12465. (end (match-end 0))
  12466. (filename (expand-file-name (match-string 1))))
  12467. (goto-char start)
  12468. (delete-region start end)
  12469. (condition-case error
  12470. (insert-file-contents filename)
  12471. (error (insert (format "%%![Couldn't insert %s: %s]"
  12472. filename error))))))
  12473. ;; %() embedded elisp
  12474. (goto-char (point-min))
  12475. (while (re-search-forward "%\\((.+)\\)" nil t)
  12476. (goto-char (match-beginning 0))
  12477. (let ((template-start (point)))
  12478. (forward-char 1)
  12479. (let ((result
  12480. (condition-case error
  12481. (eval (read (current-buffer)))
  12482. (error (format "%%![Error: %s]" error)))))
  12483. (delete-region template-start (point))
  12484. (insert result))))
  12485. ;; From the property list
  12486. (when plist-p
  12487. (goto-char (point-min))
  12488. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12489. (and (setq x (or (plist-get org-store-link-plist
  12490. (intern (match-string 1))) ""))
  12491. (replace-match x t t))))
  12492. ;; Turn on org-mode in the remember buffer, set local variables
  12493. (org-mode)
  12494. (org-set-local 'org-finish-function 'org-remember-finalize)
  12495. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12496. (org-set-local 'org-default-notes-file file))
  12497. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12498. (org-set-local 'org-remember-default-headline headline))
  12499. ;; Interactive template entries
  12500. (goto-char (point-min))
  12501. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12502. (setq char (if (match-end 3) (match-string 3))
  12503. prompt (if (match-end 2) (match-string 2)))
  12504. (goto-char (match-beginning 0))
  12505. (replace-match "")
  12506. (setq completions nil default nil)
  12507. (when prompt
  12508. (setq completions (org-split-string prompt "|")
  12509. prompt (pop completions)
  12510. default (car completions)
  12511. histvar (intern (concat
  12512. "org-remember-template-prompt-history::"
  12513. (or prompt "")))
  12514. completions (mapcar 'list completions)))
  12515. (cond
  12516. ((member char '("G" "g"))
  12517. (let* ((org-last-tags-completion-table
  12518. (org-global-tags-completion-table
  12519. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12520. (org-add-colon-after-tag-completion t)
  12521. (ins (completing-read
  12522. (if prompt (concat prompt ": ") "Tags: ")
  12523. 'org-tags-completion-function nil nil nil
  12524. 'org-tags-history)))
  12525. (setq ins (mapconcat 'identity
  12526. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  12527. ":"))
  12528. (when (string-match "\\S-" ins)
  12529. (or (equal (char-before) ?:) (insert ":"))
  12530. (insert ins)
  12531. (or (equal (char-after) ?:) (insert ":")))))
  12532. (char
  12533. (setq org-time-was-given (equal (upcase char) char))
  12534. (setq time (org-read-date (equal (upcase char) "U") t nil
  12535. prompt))
  12536. (org-insert-time-stamp time org-time-was-given
  12537. (member char '("u" "U"))
  12538. nil nil (list org-end-time-was-given)))
  12539. (t
  12540. (insert (org-completing-read
  12541. (concat (if prompt prompt "Enter string")
  12542. (if default (concat " [" default "]"))
  12543. ": ")
  12544. completions nil nil nil histvar default)))))
  12545. (goto-char (point-min))
  12546. (if (re-search-forward "%\\?" nil t)
  12547. (replace-match "")
  12548. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12549. (org-mode)
  12550. (org-set-local 'org-finish-function 'org-remember-finalize))
  12551. (when (save-excursion
  12552. (goto-char (point-min))
  12553. (re-search-forward "%!" nil t))
  12554. (replace-match "")
  12555. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12556. (defun org-remember-finish-immediately ()
  12557. "File remember note immediately.
  12558. This should be run in `post-command-hook' and will remove itself
  12559. from that hook."
  12560. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12561. (when org-finish-function
  12562. (funcall org-finish-function)))
  12563. (defvar org-clock-marker) ; Defined below
  12564. (defun org-remember-finalize ()
  12565. "Finalize the remember process."
  12566. (unless (fboundp 'remember-finalize)
  12567. (defalias 'remember-finalize 'remember-buffer))
  12568. (when (and org-clock-marker
  12569. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12570. ;; FIXME: test this, this is w/o notetaking!
  12571. (let (org-log-note-clock-out) (org-clock-out)))
  12572. (when buffer-file-name
  12573. (save-buffer)
  12574. (setq buffer-file-name nil))
  12575. (remember-finalize))
  12576. ;;;###autoload
  12577. (defun org-remember (&optional goto org-force-remember-template-char)
  12578. "Call `remember'. If this is already a remember buffer, re-apply template.
  12579. If there is an active region, make sure remember uses it as initial content
  12580. of the remember buffer.
  12581. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12582. anything, just go to the file/headline where the selected template usually
  12583. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12584. note stored by remember.
  12585. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12586. associated with a template in `org-remember-templates'."
  12587. (interactive "P")
  12588. (cond
  12589. ((equal goto '(4)) (org-go-to-remember-target))
  12590. ((equal goto '(16)) (org-remember-goto-last-stored))
  12591. (t
  12592. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12593. (progn
  12594. (when (< (length org-remember-templates) 2)
  12595. (error "No other template available"))
  12596. (erase-buffer)
  12597. (let ((annotation (plist-get org-store-link-plist :annotation))
  12598. (initial (plist-get org-store-link-plist :initial)))
  12599. (org-remember-apply-template))
  12600. (message "Press C-c C-c to remember data"))
  12601. (if (org-region-active-p)
  12602. (remember (buffer-substring (point) (mark)))
  12603. (call-interactively 'remember))))))
  12604. (defun org-remember-goto-last-stored ()
  12605. "Go to the location where the last remember note was stored."
  12606. (interactive)
  12607. (bookmark-jump "org-remember-last-stored")
  12608. (message "This is the last note stored by remember"))
  12609. (defun org-go-to-remember-target (&optional template-key)
  12610. "Go to the target location of a remember template.
  12611. The user is queried for the template."
  12612. (interactive)
  12613. (let* ((entry (org-select-remember-template template-key))
  12614. (file (nth 1 entry))
  12615. (heading (nth 2 entry))
  12616. visiting)
  12617. (unless (and file (stringp file) (string-match "\\S-" file))
  12618. (setq file org-default-notes-file))
  12619. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12620. (setq heading org-remember-default-headline))
  12621. (setq visiting (org-find-base-buffer-visiting file))
  12622. (if (not visiting) (find-file-noselect file))
  12623. (switch-to-buffer (or visiting (get-file-buffer file)))
  12624. (widen)
  12625. (goto-char (point-min))
  12626. (if (re-search-forward
  12627. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12628. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12629. nil t)
  12630. (goto-char (match-beginning 0))
  12631. (error "Target headline not found: %s" heading))))
  12632. (defvar org-note-abort nil) ; dynamically scoped
  12633. ;;;###autoload
  12634. (defun org-remember-handler ()
  12635. "Store stuff from remember.el into an org file.
  12636. First prompts for an org file. If the user just presses return, the value
  12637. of `org-default-notes-file' is used.
  12638. Then the command offers the headings tree of the selected file in order to
  12639. file the text at a specific location.
  12640. You can either immediately press RET to get the note appended to the
  12641. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12642. find a better place. Then press RET or <left> or <right> in insert the note.
  12643. Key Cursor position Note gets inserted
  12644. -----------------------------------------------------------------------------
  12645. RET buffer-start as level 1 heading at end of file
  12646. RET on headline as sublevel of the heading at cursor
  12647. RET no heading at cursor position, level taken from context.
  12648. Or use prefix arg to specify level manually.
  12649. <left> on headline as same level, before current heading
  12650. <right> on headline as same level, after current heading
  12651. So the fastest way to store the note is to press RET RET to append it to
  12652. the default file. This way your current train of thought is not
  12653. interrupted, in accordance with the principles of remember.el.
  12654. You can also get the fast execution without prompting by using
  12655. C-u C-c C-c to exit the remember buffer. See also the variable
  12656. `org-remember-store-without-prompt'.
  12657. Before being stored away, the function ensures that the text has a
  12658. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12659. is constructed from the current date and some additional data.
  12660. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12661. also indented so that it starts in the same column as the headline
  12662. \(i.e. after the stars).
  12663. See also the variable `org-reverse-note-order'."
  12664. (goto-char (point-min))
  12665. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12666. (replace-match ""))
  12667. (goto-char (point-max))
  12668. (beginning-of-line 1)
  12669. (while (looking-at "[ \t]*$\\|##.*")
  12670. (delete-region (1- (point)) (point-max))
  12671. (beginning-of-line 1))
  12672. (catch 'quit
  12673. (if org-note-abort (throw 'quit nil))
  12674. (let* ((txt (buffer-substring (point-min) (point-max)))
  12675. (fastp (org-xor (equal current-prefix-arg '(4))
  12676. org-remember-store-without-prompt))
  12677. (file (cond
  12678. (fastp org-default-notes-file)
  12679. ((and (eq org-remember-interactive-interface 'refile)
  12680. org-refile-targets)
  12681. org-default-notes-file)
  12682. ((not (and (equal current-prefix-arg '(16))
  12683. org-remember-previous-location))
  12684. (org-get-org-file))))
  12685. (heading org-remember-default-headline)
  12686. (visiting (and file (org-find-base-buffer-visiting file)))
  12687. (org-startup-folded nil)
  12688. (org-startup-align-all-tables nil)
  12689. (org-goto-start-pos 1)
  12690. spos exitcmd level indent reversed)
  12691. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12692. (setq file (car org-remember-previous-location)
  12693. heading (cdr org-remember-previous-location)
  12694. fastp t))
  12695. (setq current-prefix-arg nil)
  12696. (if (string-match "[ \t\n]+\\'" txt)
  12697. (setq txt (replace-match "" t t txt)))
  12698. ;; Modify text so that it becomes a nice subtree which can be inserted
  12699. ;; into an org tree.
  12700. (let* ((lines (split-string txt "\n"))
  12701. first)
  12702. (setq first (car lines) lines (cdr lines))
  12703. (if (string-match "^\\*+ " first)
  12704. ;; Is already a headline
  12705. (setq indent nil)
  12706. ;; We need to add a headline: Use time and first buffer line
  12707. (setq lines (cons first lines)
  12708. first (concat "* " (current-time-string)
  12709. " (" (remember-buffer-desc) ")")
  12710. indent " "))
  12711. (if (and org-adapt-indentation indent)
  12712. (setq lines (mapcar
  12713. (lambda (x)
  12714. (if (string-match "\\S-" x)
  12715. (concat indent x) x))
  12716. lines)))
  12717. (setq txt (concat first "\n"
  12718. (mapconcat 'identity lines "\n"))))
  12719. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12720. (setq txt (replace-match "\n\n" t t txt))
  12721. (if (string-match "[ \t\n]*\\'" txt)
  12722. (setq txt (replace-match "\n" t t txt))))
  12723. ;; Put the modified text back into the remember buffer, for refile.
  12724. (erase-buffer)
  12725. (insert txt)
  12726. (goto-char (point-min))
  12727. (when (and (eq org-remember-interactive-interface 'refile)
  12728. (not fastp))
  12729. (org-refile nil (or visiting (find-file-noselect file)))
  12730. (throw 'quit t))
  12731. ;; Find the file
  12732. (if (not visiting) (find-file-noselect file))
  12733. (with-current-buffer (or visiting (get-file-buffer file))
  12734. (unless (org-mode-p)
  12735. (error "Target files for remember notes must be in Org-mode"))
  12736. (save-excursion
  12737. (save-restriction
  12738. (widen)
  12739. (and (goto-char (point-min))
  12740. (not (re-search-forward "^\\* " nil t))
  12741. (insert "\n* " (or heading "Notes") "\n"))
  12742. (setq reversed (org-notes-order-reversed-p))
  12743. ;; Find the default location
  12744. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12745. (goto-char (point-min))
  12746. (if (re-search-forward
  12747. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12748. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12749. nil t)
  12750. (setq org-goto-start-pos (match-beginning 0))
  12751. (when fastp
  12752. (goto-char (point-max))
  12753. (unless (bolp) (newline))
  12754. (insert "* " heading "\n")
  12755. (setq org-goto-start-pos (point-at-bol 0)))))
  12756. ;; Ask the User for a location, using the appropriate interface
  12757. (cond
  12758. (fastp (setq spos org-goto-start-pos
  12759. exitcmd 'return))
  12760. ((eq org-remember-interactive-interface 'outline)
  12761. (setq spos (org-get-location (current-buffer)
  12762. org-remember-help)
  12763. exitcmd (cdr spos)
  12764. spos (car spos)))
  12765. ((eq org-remember-interactive-interface 'outline-path-completion)
  12766. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12767. (org-refile-use-outline-path t))
  12768. (setq spos (org-refile-get-location "Heading: ")
  12769. exitcmd 'return
  12770. spos (nth 3 spos))))
  12771. (t (error "this should not hapen")))
  12772. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12773. ; not handle this note
  12774. (goto-char spos)
  12775. (cond ((org-on-heading-p t)
  12776. (org-back-to-heading t)
  12777. (setq level (funcall outline-level))
  12778. (cond
  12779. ((eq exitcmd 'return)
  12780. ;; sublevel of current
  12781. (setq org-remember-previous-location
  12782. (cons (abbreviate-file-name file)
  12783. (org-get-heading 'notags)))
  12784. (if reversed
  12785. (outline-next-heading)
  12786. (org-end-of-subtree t)
  12787. (if (not (bolp))
  12788. (if (looking-at "[ \t]*\n")
  12789. (beginning-of-line 2)
  12790. (end-of-line 1)
  12791. (insert "\n"))))
  12792. (bookmark-set "org-remember-last-stored")
  12793. (org-paste-subtree (org-get-legal-level level 1) txt))
  12794. ((eq exitcmd 'left)
  12795. ;; before current
  12796. (bookmark-set "org-remember-last-stored")
  12797. (org-paste-subtree level txt))
  12798. ((eq exitcmd 'right)
  12799. ;; after current
  12800. (org-end-of-subtree t)
  12801. (bookmark-set "org-remember-last-stored")
  12802. (org-paste-subtree level txt))
  12803. (t (error "This should not happen"))))
  12804. ((and (bobp) (not reversed))
  12805. ;; Put it at the end, one level below level 1
  12806. (save-restriction
  12807. (widen)
  12808. (goto-char (point-max))
  12809. (if (not (bolp)) (newline))
  12810. (bookmark-set "org-remember-last-stored")
  12811. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12812. ((and (bobp) reversed)
  12813. ;; Put it at the start, as level 1
  12814. (save-restriction
  12815. (widen)
  12816. (goto-char (point-min))
  12817. (re-search-forward "^\\*+ " nil t)
  12818. (beginning-of-line 1)
  12819. (bookmark-set "org-remember-last-stored")
  12820. (org-paste-subtree 1 txt)))
  12821. (t
  12822. ;; Put it right there, with automatic level determined by
  12823. ;; org-paste-subtree or from prefix arg
  12824. (bookmark-set "org-remember-last-stored")
  12825. (org-paste-subtree
  12826. (if (numberp current-prefix-arg) current-prefix-arg)
  12827. txt)))
  12828. (when remember-save-after-remembering
  12829. (save-buffer)
  12830. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12831. t) ;; return t to indicate that we took care of this note.
  12832. (defun org-get-org-file ()
  12833. "Read a filename, with default directory `org-directory'."
  12834. (let ((default (or org-default-notes-file remember-data-file)))
  12835. (read-file-name (format "File name [%s]: " default)
  12836. (file-name-as-directory org-directory)
  12837. default)))
  12838. (defun org-notes-order-reversed-p ()
  12839. "Check if the current file should receive notes in reversed order."
  12840. (cond
  12841. ((not org-reverse-note-order) nil)
  12842. ((eq t org-reverse-note-order) t)
  12843. ((not (listp org-reverse-note-order)) nil)
  12844. (t (catch 'exit
  12845. (let ((all org-reverse-note-order)
  12846. entry)
  12847. (while (setq entry (pop all))
  12848. (if (string-match (car entry) buffer-file-name)
  12849. (throw 'exit (cdr entry))))
  12850. nil)))))
  12851. ;;; Refiling
  12852. (defvar org-refile-target-table nil
  12853. "The list of refile targets, created by `org-refile'.")
  12854. (defvar org-agenda-new-buffers nil
  12855. "Buffers created to visit agenda files.")
  12856. (defun org-get-refile-targets (&optional default-buffer)
  12857. "Produce a table with refile targets."
  12858. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12859. targets txt re files f desc descre)
  12860. (with-current-buffer (or default-buffer (current-buffer))
  12861. (while (setq entry (pop entries))
  12862. (setq files (car entry) desc (cdr entry))
  12863. (cond
  12864. ((null files) (setq files (list (current-buffer))))
  12865. ((eq files 'org-agenda-files)
  12866. (setq files (org-agenda-files 'unrestricted)))
  12867. ((and (symbolp files) (fboundp files))
  12868. (setq files (funcall files)))
  12869. ((and (symbolp files) (boundp files))
  12870. (setq files (symbol-value files))))
  12871. (if (stringp files) (setq files (list files)))
  12872. (cond
  12873. ((eq (car desc) :tag)
  12874. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12875. ((eq (car desc) :todo)
  12876. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12877. ((eq (car desc) :regexp)
  12878. (setq descre (cdr desc)))
  12879. ((eq (car desc) :level)
  12880. (setq descre (concat "^\\*\\{" (number-to-string
  12881. (if org-odd-levels-only
  12882. (1- (* 2 (cdr desc)))
  12883. (cdr desc)))
  12884. "\\}[ \t]")))
  12885. ((eq (car desc) :maxlevel)
  12886. (setq descre (concat "^\\*\\{1," (number-to-string
  12887. (if org-odd-levels-only
  12888. (1- (* 2 (cdr desc)))
  12889. (cdr desc)))
  12890. "\\}[ \t]")))
  12891. (t (error "Bad refiling target description %s" desc)))
  12892. (while (setq f (pop files))
  12893. (save-excursion
  12894. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12895. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12896. (save-excursion
  12897. (save-restriction
  12898. (widen)
  12899. (goto-char (point-min))
  12900. (while (re-search-forward descre nil t)
  12901. (goto-char (point-at-bol))
  12902. (when (looking-at org-complex-heading-regexp)
  12903. (setq txt (match-string 4)
  12904. re (concat "^" (regexp-quote
  12905. (buffer-substring (match-beginning 1)
  12906. (match-end 4)))))
  12907. (if (match-end 5) (setq re (concat re "[ \t]+"
  12908. (regexp-quote
  12909. (match-string 5)))))
  12910. (setq re (concat re "[ \t]*$"))
  12911. (when org-refile-use-outline-path
  12912. (setq txt (mapconcat 'identity
  12913. (append
  12914. (if (eq org-refile-use-outline-path 'file)
  12915. (list (file-name-nondirectory
  12916. (buffer-file-name (buffer-base-buffer))))
  12917. (if (eq org-refile-use-outline-path 'full-file-path)
  12918. (list (buffer-file-name (buffer-base-buffer)))))
  12919. (org-get-outline-path)
  12920. (list txt))
  12921. "/")))
  12922. (push (list txt f re (point)) targets))
  12923. (goto-char (point-at-eol))))))))
  12924. (nreverse targets))))
  12925. (defun org-get-outline-path ()
  12926. "Return the outline path to the current entry, as a list."
  12927. (let (rtn)
  12928. (save-excursion
  12929. (while (org-up-heading-safe)
  12930. (when (looking-at org-complex-heading-regexp)
  12931. (push (org-match-string-no-properties 4) rtn)))
  12932. rtn)))
  12933. (defvar org-refile-history nil
  12934. "History for refiling operations.")
  12935. (defun org-refile (&optional goto default-buffer)
  12936. "Move the entry at point to another heading.
  12937. The list of target headings is compiled using the information in
  12938. `org-refile-targets', which see. This list is created upon first use, and
  12939. you can update it by calling this command with a double prefix (`C-u C-u').
  12940. FIXME: Can we find a better way of updating?
  12941. At the target location, the entry is filed as a subitem of the target heading.
  12942. Depending on `org-reverse-note-order', the new subitem will either be the
  12943. first of the last subitem.
  12944. With prefix arg GOTO, the command will only visit the target location,
  12945. not actually move anything.
  12946. With a double prefix `C-c C-c', go to the location where the last refiling
  12947. operation has put the subtree.
  12948. With a double prefix argument, the command can be used to jump to any
  12949. heading in the current buffer."
  12950. (interactive "P")
  12951. (let* ((cbuf (current-buffer))
  12952. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12953. pos it nbuf file re level reversed)
  12954. (if (equal goto '(16))
  12955. (org-refile-goto-last-stored)
  12956. (when (setq it (org-refile-get-location
  12957. (if goto "Goto: " "Refile to: ") default-buffer))
  12958. (setq file (nth 1 it)
  12959. re (nth 2 it)
  12960. pos (nth 3 it))
  12961. (setq nbuf (or (find-buffer-visiting file)
  12962. (find-file-noselect file)))
  12963. (if goto
  12964. (progn
  12965. (switch-to-buffer nbuf)
  12966. (goto-char pos)
  12967. (org-show-context 'org-goto))
  12968. (org-copy-special)
  12969. (save-excursion
  12970. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12971. (find-file-noselect file))))
  12972. (setq reversed (org-notes-order-reversed-p))
  12973. (save-excursion
  12974. (save-restriction
  12975. (widen)
  12976. (goto-char pos)
  12977. (looking-at outline-regexp)
  12978. (setq level (org-get-legal-level (funcall outline-level) 1))
  12979. (goto-char
  12980. (if reversed
  12981. (outline-next-heading)
  12982. (or (save-excursion (outline-get-next-sibling))
  12983. (org-end-of-subtree t t)
  12984. (point-max))))
  12985. (bookmark-set "org-refile-last-stored")
  12986. (org-paste-subtree level))))
  12987. (org-cut-special)
  12988. (message "Entry refiled to \"%s\"" (car it)))))))
  12989. (defun org-refile-goto-last-stored ()
  12990. "Go to the location where the last refile was stored."
  12991. (interactive)
  12992. (bookmark-jump "org-refile-last-stored")
  12993. (message "This is the location of the last refile"))
  12994. (defun org-refile-get-location (&optional prompt default-buffer)
  12995. "Prompt the user for a refile location, using PROMPT."
  12996. (let ((org-refile-targets org-refile-targets)
  12997. (org-refile-use-outline-path org-refile-use-outline-path))
  12998. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  12999. (unless org-refile-target-table
  13000. (error "No refile targets"))
  13001. (let* ((cbuf (current-buffer))
  13002. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  13003. (fname (and filename (file-truename filename)))
  13004. (tbl (mapcar
  13005. (lambda (x)
  13006. (if (not (equal fname (file-truename (nth 1 x))))
  13007. (cons (concat (car x) " (" (file-name-nondirectory
  13008. (nth 1 x)) ")")
  13009. (cdr x))
  13010. x))
  13011. org-refile-target-table))
  13012. (completion-ignore-case t))
  13013. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  13014. tbl)))
  13015. ;;;; Dynamic blocks
  13016. (defun org-find-dblock (name)
  13017. "Find the first dynamic block with name NAME in the buffer.
  13018. If not found, stay at current position and return nil."
  13019. (let (pos)
  13020. (save-excursion
  13021. (goto-char (point-min))
  13022. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  13023. nil t)
  13024. (match-beginning 0))))
  13025. (if pos (goto-char pos))
  13026. pos))
  13027. (defconst org-dblock-start-re
  13028. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  13029. "Matches the startline of a dynamic block, with parameters.")
  13030. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  13031. "Matches the end of a dyhamic block.")
  13032. (defun org-create-dblock (plist)
  13033. "Create a dynamic block section, with parameters taken from PLIST.
  13034. PLIST must containe a :name entry which is used as name of the block."
  13035. (unless (bolp) (newline))
  13036. (let ((name (plist-get plist :name)))
  13037. (insert "#+BEGIN: " name)
  13038. (while plist
  13039. (if (eq (car plist) :name)
  13040. (setq plist (cddr plist))
  13041. (insert " " (prin1-to-string (pop plist)))))
  13042. (insert "\n\n#+END:\n")
  13043. (beginning-of-line -2)))
  13044. (defun org-prepare-dblock ()
  13045. "Prepare dynamic block for refresh.
  13046. This empties the block, puts the cursor at the insert position and returns
  13047. the property list including an extra property :name with the block name."
  13048. (unless (looking-at org-dblock-start-re)
  13049. (error "Not at a dynamic block"))
  13050. (let* ((begdel (1+ (match-end 0)))
  13051. (name (org-no-properties (match-string 1)))
  13052. (params (append (list :name name)
  13053. (read (concat "(" (match-string 3) ")")))))
  13054. (unless (re-search-forward org-dblock-end-re nil t)
  13055. (error "Dynamic block not terminated"))
  13056. (delete-region begdel (match-beginning 0))
  13057. (goto-char begdel)
  13058. (open-line 1)
  13059. params))
  13060. (defun org-map-dblocks (&optional command)
  13061. "Apply COMMAND to all dynamic blocks in the current buffer.
  13062. If COMMAND is not given, use `org-update-dblock'."
  13063. (let ((cmd (or command 'org-update-dblock))
  13064. pos)
  13065. (save-excursion
  13066. (goto-char (point-min))
  13067. (while (re-search-forward org-dblock-start-re nil t)
  13068. (goto-char (setq pos (match-beginning 0)))
  13069. (condition-case nil
  13070. (funcall cmd)
  13071. (error (message "Error during update of dynamic block")))
  13072. (goto-char pos)
  13073. (unless (re-search-forward org-dblock-end-re nil t)
  13074. (error "Dynamic block not terminated"))))))
  13075. (defun org-dblock-update (&optional arg)
  13076. "User command for updating dynamic blocks.
  13077. Update the dynamic block at point. With prefix ARG, update all dynamic
  13078. blocks in the buffer."
  13079. (interactive "P")
  13080. (if arg
  13081. (org-update-all-dblocks)
  13082. (or (looking-at org-dblock-start-re)
  13083. (org-beginning-of-dblock))
  13084. (org-update-dblock)))
  13085. (defun org-update-dblock ()
  13086. "Update the dynamic block at point
  13087. This means to empty the block, parse for parameters and then call
  13088. the correct writing function."
  13089. (save-window-excursion
  13090. (let* ((pos (point))
  13091. (line (org-current-line))
  13092. (params (org-prepare-dblock))
  13093. (name (plist-get params :name))
  13094. (cmd (intern (concat "org-dblock-write:" name))))
  13095. (message "Updating dynamic block `%s' at line %d..." name line)
  13096. (funcall cmd params)
  13097. (message "Updating dynamic block `%s' at line %d...done" name line)
  13098. (goto-char pos))))
  13099. (defun org-beginning-of-dblock ()
  13100. "Find the beginning of the dynamic block at point.
  13101. Error if there is no scuh block at point."
  13102. (let ((pos (point))
  13103. beg)
  13104. (end-of-line 1)
  13105. (if (and (re-search-backward org-dblock-start-re nil t)
  13106. (setq beg (match-beginning 0))
  13107. (re-search-forward org-dblock-end-re nil t)
  13108. (> (match-end 0) pos))
  13109. (goto-char beg)
  13110. (goto-char pos)
  13111. (error "Not in a dynamic block"))))
  13112. (defun org-update-all-dblocks ()
  13113. "Update all dynamic blocks in the buffer.
  13114. This function can be used in a hook."
  13115. (when (org-mode-p)
  13116. (org-map-dblocks 'org-update-dblock)))
  13117. ;;;; Completion
  13118. (defconst org-additional-option-like-keywords
  13119. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  13120. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  13121. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  13122. (defun org-complete (&optional arg)
  13123. "Perform completion on word at point.
  13124. At the beginning of a headline, this completes TODO keywords as given in
  13125. `org-todo-keywords'.
  13126. If the current word is preceded by a backslash, completes the TeX symbols
  13127. that are supported for HTML support.
  13128. If the current word is preceded by \"#+\", completes special words for
  13129. setting file options.
  13130. In the line after \"#+STARTUP:, complete valid keywords.\"
  13131. At all other locations, this simply calls the value of
  13132. `org-completion-fallback-command'."
  13133. (interactive "P")
  13134. (org-without-partial-completion
  13135. (catch 'exit
  13136. (let* ((end (point))
  13137. (beg1 (save-excursion
  13138. (skip-chars-backward (org-re "[:alnum:]_@"))
  13139. (point)))
  13140. (beg (save-excursion
  13141. (skip-chars-backward "a-zA-Z0-9_:$")
  13142. (point)))
  13143. (confirm (lambda (x) (stringp (car x))))
  13144. (searchhead (equal (char-before beg) ?*))
  13145. (tag (and (equal (char-before beg1) ?:)
  13146. (equal (char-after (point-at-bol)) ?*)))
  13147. (prop (and (equal (char-before beg1) ?:)
  13148. (not (equal (char-after (point-at-bol)) ?*))))
  13149. (texp (equal (char-before beg) ?\\))
  13150. (link (equal (char-before beg) ?\[))
  13151. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  13152. beg)
  13153. "#+"))
  13154. (startup (string-match "^#\\+STARTUP:.*"
  13155. (buffer-substring (point-at-bol) (point))))
  13156. (completion-ignore-case opt)
  13157. (type nil)
  13158. (tbl nil)
  13159. (table (cond
  13160. (opt
  13161. (setq type :opt)
  13162. (append
  13163. (mapcar
  13164. (lambda (x)
  13165. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  13166. (cons (match-string 2 x) (match-string 1 x)))
  13167. (org-split-string (org-get-current-options) "\n"))
  13168. (mapcar 'list org-additional-option-like-keywords)))
  13169. (startup
  13170. (setq type :startup)
  13171. org-startup-options)
  13172. (link (append org-link-abbrev-alist-local
  13173. org-link-abbrev-alist))
  13174. (texp
  13175. (setq type :tex)
  13176. org-html-entities)
  13177. ((string-match "\\`\\*+[ \t]+\\'"
  13178. (buffer-substring (point-at-bol) beg))
  13179. (setq type :todo)
  13180. (mapcar 'list org-todo-keywords-1))
  13181. (searchhead
  13182. (setq type :searchhead)
  13183. (save-excursion
  13184. (goto-char (point-min))
  13185. (while (re-search-forward org-todo-line-regexp nil t)
  13186. (push (list
  13187. (org-make-org-heading-search-string
  13188. (match-string 3) t))
  13189. tbl)))
  13190. tbl)
  13191. (tag (setq type :tag beg beg1)
  13192. (or org-tag-alist (org-get-buffer-tags)))
  13193. (prop (setq type :prop beg beg1)
  13194. (mapcar 'list (org-buffer-property-keys nil t t)))
  13195. (t (progn
  13196. (call-interactively org-completion-fallback-command)
  13197. (throw 'exit nil)))))
  13198. (pattern (buffer-substring-no-properties beg end))
  13199. (completion (try-completion pattern table confirm)))
  13200. (cond ((eq completion t)
  13201. (if (not (assoc (upcase pattern) table))
  13202. (message "Already complete")
  13203. (if (equal type :opt)
  13204. (insert (substring (cdr (assoc (upcase pattern) table))
  13205. (length pattern)))
  13206. (if (memq type '(:tag :prop)) (insert ":")))))
  13207. ((null completion)
  13208. (message "Can't find completion for \"%s\"" pattern)
  13209. (ding))
  13210. ((not (string= pattern completion))
  13211. (delete-region beg end)
  13212. (if (string-match " +$" completion)
  13213. (setq completion (replace-match "" t t completion)))
  13214. (insert completion)
  13215. (if (get-buffer-window "*Completions*")
  13216. (delete-window (get-buffer-window "*Completions*")))
  13217. (if (assoc completion table)
  13218. (if (eq type :todo) (insert " ")
  13219. (if (memq type '(:tag :prop)) (insert ":"))))
  13220. (if (and (equal type :opt) (assoc completion table))
  13221. (message "%s" (substitute-command-keys
  13222. "Press \\[org-complete] again to insert example settings"))))
  13223. (t
  13224. (message "Making completion list...")
  13225. (let ((list (sort (all-completions pattern table confirm)
  13226. 'string<)))
  13227. (with-output-to-temp-buffer "*Completions*"
  13228. (condition-case nil
  13229. ;; Protection needed for XEmacs and emacs 21
  13230. (display-completion-list list pattern)
  13231. (error (display-completion-list list)))))
  13232. (message "Making completion list...%s" "done")))))))
  13233. ;;;; TODO, DEADLINE, Comments
  13234. (defun org-toggle-comment ()
  13235. "Change the COMMENT state of an entry."
  13236. (interactive)
  13237. (save-excursion
  13238. (org-back-to-heading)
  13239. (let (case-fold-search)
  13240. (if (looking-at (concat outline-regexp
  13241. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13242. (replace-match "" t t nil 1)
  13243. (if (looking-at outline-regexp)
  13244. (progn
  13245. (goto-char (match-end 0))
  13246. (insert org-comment-string " ")))))))
  13247. (defvar org-last-todo-state-is-todo nil
  13248. "This is non-nil when the last TODO state change led to a TODO state.
  13249. If the last change removed the TODO tag or switched to DONE, then
  13250. this is nil.")
  13251. (defvar org-setting-tags nil) ; dynamically skiped
  13252. ;; FIXME: better place
  13253. (defun org-property-or-variable-value (var &optional inherit)
  13254. "Check if there is a property fixing the value of VAR.
  13255. If yes, return this value. If not, return the current value of the variable."
  13256. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13257. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13258. (read prop)
  13259. (symbol-value var))))
  13260. (defun org-parse-local-options (string var)
  13261. "Parse STRING for startup setting relevant for variable VAR."
  13262. (let ((rtn (symbol-value var))
  13263. e opts)
  13264. (save-match-data
  13265. (if (or (not string) (not (string-match "\\S-" string)))
  13266. rtn
  13267. (setq opts (delq nil (mapcar (lambda (x)
  13268. (setq e (assoc x org-startup-options))
  13269. (if (eq (nth 1 e) var) e nil))
  13270. (org-split-string string "[ \t]+"))))
  13271. (if (not opts)
  13272. rtn
  13273. (setq rtn nil)
  13274. (while (setq e (pop opts))
  13275. (if (not (nth 3 e))
  13276. (setq rtn (nth 2 e))
  13277. (if (not (listp rtn)) (setq rtn nil))
  13278. (push (nth 2 e) rtn)))
  13279. rtn)))))
  13280. (defvar org-blocker-hook nil
  13281. "Hook for functions that are allowed to block a state change.
  13282. Each function gets as its single argument a property list, see
  13283. `org-trigger-hook' for more information about this list.
  13284. If any of the functions in this hook returns nil, the state change
  13285. is blocked.")
  13286. (defvar org-trigger-hook nil
  13287. "Hook for functions that are triggered by a state change.
  13288. Each function gets as its single argument a property list with at least
  13289. the following elements:
  13290. (:type type-of-change :position pos-at-entry-start
  13291. :from old-state :to new-state)
  13292. Depending on the type, more properties may be present.
  13293. This mechanism is currently implemented for:
  13294. TODO state changes
  13295. ------------------
  13296. :type todo-state-change
  13297. :from previous state (keyword as a string), or nil
  13298. :to new state (keyword as a string), or nil")
  13299. (defun org-todo (&optional arg)
  13300. "Change the TODO state of an item.
  13301. The state of an item is given by a keyword at the start of the heading,
  13302. like
  13303. *** TODO Write paper
  13304. *** DONE Call mom
  13305. The different keywords are specified in the variable `org-todo-keywords'.
  13306. By default the available states are \"TODO\" and \"DONE\".
  13307. So for this example: when the item starts with TODO, it is changed to DONE.
  13308. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13309. DONE are present, add TODO at the beginning of the heading.
  13310. With C-u prefix arg, use completion to determine the new state.
  13311. With numeric prefix arg, switch to that state.
  13312. For calling through lisp, arg is also interpreted in the following way:
  13313. 'none -> empty state
  13314. \"\"(empty string) -> switch to empty state
  13315. 'done -> switch to DONE
  13316. 'nextset -> switch to the next set of keywords
  13317. 'previousset -> switch to the previous set of keywords
  13318. \"WAITING\" -> switch to the specified keyword, but only if it
  13319. really is a member of `org-todo-keywords'."
  13320. (interactive "P")
  13321. (save-excursion
  13322. (catch 'exit
  13323. (org-back-to-heading)
  13324. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13325. (or (looking-at (concat " +" org-todo-regexp " *"))
  13326. (looking-at " *"))
  13327. (let* ((match-data (match-data))
  13328. (startpos (point-at-bol))
  13329. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13330. (org-log-done org-log-done)
  13331. (org-log-repeat org-log-repeat)
  13332. (org-todo-log-states org-todo-log-states)
  13333. (this (match-string 1))
  13334. (hl-pos (match-beginning 0))
  13335. (head (org-get-todo-sequence-head this))
  13336. (ass (assoc head org-todo-kwd-alist))
  13337. (interpret (nth 1 ass))
  13338. (done-word (nth 3 ass))
  13339. (final-done-word (nth 4 ass))
  13340. (last-state (or this ""))
  13341. (completion-ignore-case t)
  13342. (member (member this org-todo-keywords-1))
  13343. (tail (cdr member))
  13344. (state (cond
  13345. ((and org-todo-key-trigger
  13346. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13347. (and (not arg) org-use-fast-todo-selection
  13348. (not (eq org-use-fast-todo-selection 'prefix)))))
  13349. ;; Use fast selection
  13350. (org-fast-todo-selection))
  13351. ((and (equal arg '(4))
  13352. (or (not org-use-fast-todo-selection)
  13353. (not org-todo-key-trigger)))
  13354. ;; Read a state with completion
  13355. (completing-read "State: " (mapcar (lambda(x) (list x))
  13356. org-todo-keywords-1)
  13357. nil t))
  13358. ((eq arg 'right)
  13359. (if this
  13360. (if tail (car tail) nil)
  13361. (car org-todo-keywords-1)))
  13362. ((eq arg 'left)
  13363. (if (equal member org-todo-keywords-1)
  13364. nil
  13365. (if this
  13366. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13367. org-todo-keywords-1)
  13368. (org-last org-todo-keywords-1))))
  13369. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13370. (setq arg nil))) ; hack to fall back to cycling
  13371. (arg
  13372. ;; user or caller requests a specific state
  13373. (cond
  13374. ((equal arg "") nil)
  13375. ((eq arg 'none) nil)
  13376. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13377. ((eq arg 'nextset)
  13378. (or (car (cdr (member head org-todo-heads)))
  13379. (car org-todo-heads)))
  13380. ((eq arg 'previousset)
  13381. (let ((org-todo-heads (reverse org-todo-heads)))
  13382. (or (car (cdr (member head org-todo-heads)))
  13383. (car org-todo-heads))))
  13384. ((car (member arg org-todo-keywords-1)))
  13385. ((nth (1- (prefix-numeric-value arg))
  13386. org-todo-keywords-1))))
  13387. ((null member) (or head (car org-todo-keywords-1)))
  13388. ((equal this final-done-word) nil) ;; -> make empty
  13389. ((null tail) nil) ;; -> first entry
  13390. ((eq interpret 'sequence)
  13391. (car tail))
  13392. ((memq interpret '(type priority))
  13393. (if (eq this-command last-command)
  13394. (car tail)
  13395. (if (> (length tail) 0)
  13396. (or done-word (car org-done-keywords))
  13397. nil)))
  13398. (t nil)))
  13399. (next (if state (concat " " state " ") " "))
  13400. (change-plist (list :type 'todo-state-change :from this :to state
  13401. :position startpos))
  13402. dolog now-done-p)
  13403. (when org-blocker-hook
  13404. (unless (save-excursion
  13405. (save-match-data
  13406. (run-hook-with-args-until-failure
  13407. 'org-blocker-hook change-plist)))
  13408. (if (interactive-p)
  13409. (error "TODO state change from %s to %s blocked" this state)
  13410. ;; fail silently
  13411. (message "TODO state change from %s to %s blocked" this state)
  13412. (throw 'exit nil))))
  13413. (store-match-data match-data)
  13414. (replace-match next t t)
  13415. (unless (pos-visible-in-window-p hl-pos)
  13416. (message "TODO state changed to %s" (org-trim next)))
  13417. (unless head
  13418. (setq head (org-get-todo-sequence-head state)
  13419. ass (assoc head org-todo-kwd-alist)
  13420. interpret (nth 1 ass)
  13421. done-word (nth 3 ass)
  13422. final-done-word (nth 4 ass)))
  13423. (when (memq arg '(nextset previousset))
  13424. (message "Keyword-Set %d/%d: %s"
  13425. (- (length org-todo-sets) -1
  13426. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13427. (length org-todo-sets)
  13428. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13429. (setq org-last-todo-state-is-todo
  13430. (not (member state org-done-keywords)))
  13431. (setq now-done-p (and (member state org-done-keywords)
  13432. (not (member this org-done-keywords))))
  13433. (and logging (org-local-logging logging))
  13434. (when (and (or org-todo-log-states org-log-done)
  13435. (not (memq arg '(nextset previousset))))
  13436. ;; we need to look at recording a time and note
  13437. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  13438. (nth 2 (assoc this org-todo-log-states))))
  13439. (when (and state
  13440. (member state org-not-done-keywords)
  13441. (not (member this org-not-done-keywords)))
  13442. ;; This is now a todo state and was not one before
  13443. ;; If there was a CLOSED time stamp, get rid of it.
  13444. (org-add-planning-info nil nil 'closed))
  13445. (when (and now-done-p org-log-done)
  13446. ;; It is now done, and it was not done before
  13447. (org-add-planning-info 'closed (org-current-time))
  13448. (if (and (not dolog) (eq 'note org-log-done))
  13449. (org-add-log-maybe 'done state 'findpos 'note)))
  13450. (when (and state dolog)
  13451. ;; This is a non-nil state, and we need to log it
  13452. (org-add-log-maybe 'state state 'findpos dolog)))
  13453. ;; Fixup tag positioning
  13454. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13455. (run-hooks 'org-after-todo-state-change-hook)
  13456. (if (and arg (not (member state org-done-keywords)))
  13457. (setq head (org-get-todo-sequence-head state)))
  13458. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13459. ;; Do we need to trigger a repeat?
  13460. (when now-done-p (org-auto-repeat-maybe state))
  13461. ;; Fixup cursor location if close to the keyword
  13462. (if (and (outline-on-heading-p)
  13463. (not (bolp))
  13464. (save-excursion (beginning-of-line 1)
  13465. (looking-at org-todo-line-regexp))
  13466. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13467. (progn
  13468. (goto-char (or (match-end 2) (match-end 1)))
  13469. (just-one-space)))
  13470. (when org-trigger-hook
  13471. (save-excursion
  13472. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13473. (defun org-local-logging (value)
  13474. "Get logging settings from a property VALUE."
  13475. (let* (words w a)
  13476. ;; directly set the variables, they are already local.
  13477. (setq org-log-done nil
  13478. org-log-repeat nil
  13479. org-todo-log-states nil)
  13480. (setq words (org-split-string value))
  13481. (while (setq w (pop words))
  13482. (cond
  13483. ((setq a (assoc w org-startup-options))
  13484. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  13485. (set (nth 1 a) (nth 2 a))))
  13486. ((setq a (org-extract-log-state-settings w))
  13487. (and (member (car a) org-todo-keywords-1)
  13488. (push a org-todo-log-states)))))))
  13489. (defun org-get-todo-sequence-head (kwd)
  13490. "Return the head of the TODO sequence to which KWD belongs.
  13491. If KWD is not set, check if there is a text property remembering the
  13492. right sequence."
  13493. (let (p)
  13494. (cond
  13495. ((not kwd)
  13496. (or (get-text-property (point-at-bol) 'org-todo-head)
  13497. (progn
  13498. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13499. nil (point-at-eol)))
  13500. (get-text-property p 'org-todo-head))))
  13501. ((not (member kwd org-todo-keywords-1))
  13502. (car org-todo-keywords-1))
  13503. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13504. (defun org-fast-todo-selection ()
  13505. "Fast TODO keyword selection with single keys.
  13506. Returns the new TODO keyword, or nil if no state change should occur."
  13507. (let* ((fulltable org-todo-key-alist)
  13508. (done-keywords org-done-keywords) ;; needed for the faces.
  13509. (maxlen (apply 'max (mapcar
  13510. (lambda (x)
  13511. (if (stringp (car x)) (string-width (car x)) 0))
  13512. fulltable)))
  13513. (expert nil)
  13514. (fwidth (+ maxlen 3 1 3))
  13515. (ncol (/ (- (window-width) 4) fwidth))
  13516. tg cnt e c tbl
  13517. groups ingroup)
  13518. (save-window-excursion
  13519. (if expert
  13520. (set-buffer (get-buffer-create " *Org todo*"))
  13521. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13522. (erase-buffer)
  13523. (org-set-local 'org-done-keywords done-keywords)
  13524. (setq tbl fulltable cnt 0)
  13525. (while (setq e (pop tbl))
  13526. (cond
  13527. ((equal e '(:startgroup))
  13528. (push '() groups) (setq ingroup t)
  13529. (when (not (= cnt 0))
  13530. (setq cnt 0)
  13531. (insert "\n"))
  13532. (insert "{ "))
  13533. ((equal e '(:endgroup))
  13534. (setq ingroup nil cnt 0)
  13535. (insert "}\n"))
  13536. (t
  13537. (setq tg (car e) c (cdr e))
  13538. (if ingroup (push tg (car groups)))
  13539. (setq tg (org-add-props tg nil 'face
  13540. (org-get-todo-face tg)))
  13541. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13542. (insert "[" c "] " tg (make-string
  13543. (- fwidth 4 (length tg)) ?\ ))
  13544. (when (= (setq cnt (1+ cnt)) ncol)
  13545. (insert "\n")
  13546. (if ingroup (insert " "))
  13547. (setq cnt 0)))))
  13548. (insert "\n")
  13549. (goto-char (point-min))
  13550. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13551. (fit-window-to-buffer))
  13552. (message "[a-z..]:Set [SPC]:clear")
  13553. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13554. (cond
  13555. ((or (= c ?\C-g)
  13556. (and (= c ?q) (not (rassoc c fulltable))))
  13557. (setq quit-flag t))
  13558. ((= c ?\ ) nil)
  13559. ((setq e (rassoc c fulltable) tg (car e))
  13560. tg)
  13561. (t (setq quit-flag t))))))
  13562. (defun org-get-repeat ()
  13563. "Check if tere is a deadline/schedule with repeater in this entry."
  13564. (save-match-data
  13565. (save-excursion
  13566. (org-back-to-heading t)
  13567. (if (re-search-forward
  13568. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13569. (match-string 1)))))
  13570. (defvar org-last-changed-timestamp)
  13571. (defvar org-log-post-message)
  13572. (defvar org-log-note-purpose)
  13573. (defun org-auto-repeat-maybe (done-word)
  13574. "Check if the current headline contains a repeated deadline/schedule.
  13575. If yes, set TODO state back to what it was and change the base date
  13576. of repeating deadline/scheduled time stamps to new date.
  13577. This function is run automatically after each state change to a DONE state."
  13578. ;; last-state is dynamically scoped into this function
  13579. (let* ((repeat (org-get-repeat))
  13580. (aa (assoc last-state org-todo-kwd-alist))
  13581. (interpret (nth 1 aa))
  13582. (head (nth 2 aa))
  13583. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13584. (msg "Entry repeats: ")
  13585. (org-log-done nil)
  13586. (org-todo-log-states nil)
  13587. re type n what ts)
  13588. (when repeat
  13589. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  13590. (org-todo (if (eq interpret 'type) last-state head))
  13591. (when (and org-log-repeat
  13592. (or (not (memq 'org-add-log-note
  13593. (default-value 'post-command-hook)))
  13594. (eq org-log-note-purpose 'done)))
  13595. ;; Make sure a note is taken;
  13596. (org-add-log-maybe 'state (or done-word (car org-done-keywords))
  13597. 'findpos org-log-repeat))
  13598. (org-back-to-heading t)
  13599. (org-add-planning-info nil nil 'closed)
  13600. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13601. org-deadline-time-regexp "\\)\\|\\("
  13602. org-ts-regexp "\\)"))
  13603. (while (re-search-forward
  13604. re (save-excursion (outline-next-heading) (point)) t)
  13605. (setq type (if (match-end 1) org-scheduled-string
  13606. (if (match-end 3) org-deadline-string "Plain:"))
  13607. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13608. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13609. (setq n (string-to-number (match-string 1 ts))
  13610. what (match-string 2 ts))
  13611. (if (equal what "w") (setq n (* n 7) what "d"))
  13612. (org-timestamp-change n (cdr (assoc what whata)))
  13613. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13614. (setq org-log-post-message msg)
  13615. (message "%s" msg))))
  13616. (defun org-show-todo-tree (arg)
  13617. "Make a compact tree which shows all headlines marked with TODO.
  13618. The tree will show the lines where the regexp matches, and all higher
  13619. headlines above the match.
  13620. With a \\[universal-argument] prefix, also show the DONE entries.
  13621. With a numeric prefix N, construct a sparse tree for the Nth element
  13622. of `org-todo-keywords-1'."
  13623. (interactive "P")
  13624. (let ((case-fold-search nil)
  13625. (kwd-re
  13626. (cond ((null arg) org-not-done-regexp)
  13627. ((equal arg '(4))
  13628. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13629. (mapcar 'list org-todo-keywords-1))))
  13630. (concat "\\("
  13631. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13632. "\\)\\>")))
  13633. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13634. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13635. org-todo-keywords-1)))
  13636. (t (error "Invalid prefix argument: %s" arg)))))
  13637. (message "%d TODO entries found"
  13638. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13639. (defun org-deadline (&optional remove)
  13640. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13641. With argument REMOVE, remove any deadline from the item."
  13642. (interactive "P")
  13643. (if remove
  13644. (progn
  13645. (org-remove-timestamp-with-keyword org-deadline-string)
  13646. (message "Item no longer has a deadline."))
  13647. (org-add-planning-info 'deadline nil 'closed)))
  13648. (defun org-schedule (&optional remove)
  13649. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13650. With argument REMOVE, remove any scheduling date from the item."
  13651. (interactive "P")
  13652. (if remove
  13653. (progn
  13654. (org-remove-timestamp-with-keyword org-scheduled-string)
  13655. (message "Item is no longer scheduled."))
  13656. (org-add-planning-info 'scheduled nil 'closed)))
  13657. (defun org-remove-timestamp-with-keyword (keyword)
  13658. "Remove all time stamps with KEYWORD in the current entry."
  13659. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  13660. beg)
  13661. (save-excursion
  13662. (org-back-to-heading t)
  13663. (setq beg (point))
  13664. (org-end-of-subtree t t)
  13665. (while (re-search-backward re beg t)
  13666. (replace-match "")
  13667. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  13668. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  13669. (defun org-add-planning-info (what &optional time &rest remove)
  13670. "Insert new timestamp with keyword in the line directly after the headline.
  13671. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13672. If non is given, the user is prompted for a date.
  13673. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13674. be removed."
  13675. (interactive)
  13676. (let (org-time-was-given org-end-time-was-given)
  13677. (when what (setq time (or time (org-read-date nil 'to-time))))
  13678. (when (and org-insert-labeled-timestamps-at-point
  13679. (member what '(scheduled deadline)))
  13680. (insert
  13681. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13682. (org-insert-time-stamp time org-time-was-given
  13683. nil nil nil (list org-end-time-was-given))
  13684. (setq what nil))
  13685. (save-excursion
  13686. (save-restriction
  13687. (let (col list elt ts buffer-invisibility-spec)
  13688. (org-back-to-heading t)
  13689. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13690. (goto-char (match-end 1))
  13691. (setq col (current-column))
  13692. (goto-char (match-end 0))
  13693. (if (eobp) (insert "\n") (forward-char 1))
  13694. (if (and (not (looking-at outline-regexp))
  13695. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13696. "[^\r\n]*"))
  13697. (not (equal (match-string 1) org-clock-string)))
  13698. (narrow-to-region (match-beginning 0) (match-end 0))
  13699. (insert-before-markers "\n")
  13700. (backward-char 1)
  13701. (narrow-to-region (point) (point))
  13702. (indent-to-column col))
  13703. ;; Check if we have to remove something.
  13704. (setq list (cons what remove))
  13705. (while list
  13706. (setq elt (pop list))
  13707. (goto-char (point-min))
  13708. (when (or (and (eq elt 'scheduled)
  13709. (re-search-forward org-scheduled-time-regexp nil t))
  13710. (and (eq elt 'deadline)
  13711. (re-search-forward org-deadline-time-regexp nil t))
  13712. (and (eq elt 'closed)
  13713. (re-search-forward org-closed-time-regexp nil t)))
  13714. (replace-match "")
  13715. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13716. (if (looking-at " +") (replace-match ""))))
  13717. (goto-char (point-max))
  13718. (when what
  13719. (insert
  13720. (if (not (equal (char-before) ?\ )) " " "")
  13721. (cond ((eq what 'scheduled) org-scheduled-string)
  13722. ((eq what 'deadline) org-deadline-string)
  13723. ((eq what 'closed) org-closed-string))
  13724. " ")
  13725. (setq ts (org-insert-time-stamp
  13726. time
  13727. (or org-time-was-given
  13728. (and (eq what 'closed) org-log-done-with-time))
  13729. (eq what 'closed)
  13730. nil nil (list org-end-time-was-given)))
  13731. (end-of-line 1))
  13732. (goto-char (point-min))
  13733. (widen)
  13734. (if (looking-at "[ \t]+\r?\n")
  13735. (replace-match ""))
  13736. ts)))))
  13737. (defvar org-log-note-marker (make-marker))
  13738. (defvar org-log-note-purpose nil)
  13739. (defvar org-log-note-state nil)
  13740. (defvar org-log-note-how nil)
  13741. (defvar org-log-note-window-configuration nil)
  13742. (defvar org-log-note-return-to (make-marker))
  13743. (defvar org-log-post-message nil
  13744. "Message to be displayed after a log note has been stored.
  13745. The auto-repeater uses this.")
  13746. (defun org-add-log-maybe (&optional purpose state findpos how)
  13747. "Set up the post command hook to take a note.
  13748. If this is about to TODO state change, the new state is expected in STATE.
  13749. When FINDPOS is non-nil, find the correct position for the note in
  13750. the current entry. If not, assume that it can be inserted at point."
  13751. (save-excursion
  13752. (when findpos
  13753. (org-back-to-heading t)
  13754. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13755. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13756. "[^\r\n]*\\)?"))
  13757. (goto-char (match-end 0))
  13758. (unless org-log-states-order-reversed
  13759. (and (= (char-after) ?\n) (forward-char 1))
  13760. (org-skip-over-state-notes)
  13761. (skip-chars-backward " \t\n\r")))
  13762. (move-marker org-log-note-marker (point))
  13763. (setq org-log-note-purpose purpose
  13764. org-log-note-state state
  13765. org-log-note-how how)
  13766. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  13767. (defun org-skip-over-state-notes ()
  13768. "Skip past the list of State notes in an entry."
  13769. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13770. (while (looking-at "[ \t]*- State")
  13771. (condition-case nil
  13772. (org-next-item)
  13773. (error (org-end-of-item)))))
  13774. (defun org-add-log-note (&optional purpose)
  13775. "Pop up a window for taking a note, and add this note later at point."
  13776. (remove-hook 'post-command-hook 'org-add-log-note)
  13777. (setq org-log-note-window-configuration (current-window-configuration))
  13778. (delete-other-windows)
  13779. (move-marker org-log-note-return-to (point))
  13780. (switch-to-buffer (marker-buffer org-log-note-marker))
  13781. (goto-char org-log-note-marker)
  13782. (org-switch-to-buffer-other-window "*Org Note*")
  13783. (erase-buffer)
  13784. (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
  13785. (org-store-log-note)
  13786. (let ((org-inhibit-startup t)) (org-mode))
  13787. (insert (format "# Insert note for %s.
  13788. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13789. (cond
  13790. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13791. ((eq org-log-note-purpose 'done) "closed todo item")
  13792. ((eq org-log-note-purpose 'state)
  13793. (format "state change to \"%s\"" org-log-note-state))
  13794. (t (error "This should not happen")))))
  13795. (org-set-local 'org-finish-function 'org-store-log-note)))
  13796. (defun org-store-log-note ()
  13797. "Finish taking a log note, and insert it to where it belongs."
  13798. (let ((txt (buffer-string))
  13799. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13800. lines ind)
  13801. (kill-buffer (current-buffer))
  13802. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13803. (setq txt (replace-match "" t t txt)))
  13804. (if (string-match "\\s-+\\'" txt)
  13805. (setq txt (replace-match "" t t txt)))
  13806. (setq lines (org-split-string txt "\n"))
  13807. (when (and note (string-match "\\S-" note))
  13808. (setq note
  13809. (org-replace-escapes
  13810. note
  13811. (list (cons "%u" (user-login-name))
  13812. (cons "%U" user-full-name)
  13813. (cons "%t" (format-time-string
  13814. (org-time-stamp-format 'long 'inactive)
  13815. (current-time)))
  13816. (cons "%s" (if org-log-note-state
  13817. (concat "\"" org-log-note-state "\"")
  13818. "")))))
  13819. (if lines (setq note (concat note " \\\\")))
  13820. (push note lines))
  13821. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13822. (when lines
  13823. (save-excursion
  13824. (set-buffer (marker-buffer org-log-note-marker))
  13825. (save-excursion
  13826. (goto-char org-log-note-marker)
  13827. (move-marker org-log-note-marker nil)
  13828. (end-of-line 1)
  13829. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13830. (indent-relative nil)
  13831. (insert "- " (pop lines))
  13832. (org-indent-line-function)
  13833. (beginning-of-line 1)
  13834. (looking-at "[ \t]*")
  13835. (setq ind (concat (match-string 0) " "))
  13836. (end-of-line 1)
  13837. (while lines (insert "\n" ind (pop lines)))))))
  13838. (set-window-configuration org-log-note-window-configuration)
  13839. (with-current-buffer (marker-buffer org-log-note-return-to)
  13840. (goto-char org-log-note-return-to))
  13841. (move-marker org-log-note-return-to nil)
  13842. (and org-log-post-message (message "%s" org-log-post-message)))
  13843. ;; FIXME: what else would be useful?
  13844. ;; - priority
  13845. ;; - date
  13846. (defun org-sparse-tree (&optional arg)
  13847. "Create a sparse tree, prompt for the details.
  13848. This command can create sparse trees. You first need to select the type
  13849. of match used to create the tree:
  13850. t Show entries with a specific TODO keyword.
  13851. T Show entries selected by a tags match.
  13852. p Enter a property name and its value (both with completion on existing
  13853. names/values) and show entries with that property.
  13854. r Show entries matching a regular expression
  13855. d Show deadlines due within `org-deadline-warning-days'."
  13856. (interactive "P")
  13857. (let (ans kwd value)
  13858. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13859. (setq ans (read-char-exclusive))
  13860. (cond
  13861. ((equal ans ?d)
  13862. (call-interactively 'org-check-deadlines))
  13863. ((equal ans ?b)
  13864. (call-interactively 'org-check-before-date))
  13865. ((equal ans ?t)
  13866. (org-show-todo-tree '(4)))
  13867. ((equal ans ?T)
  13868. (call-interactively 'org-tags-sparse-tree))
  13869. ((member ans '(?p ?P))
  13870. (setq kwd (completing-read "Property: "
  13871. (mapcar 'list (org-buffer-property-keys))))
  13872. (setq value (completing-read "Value: "
  13873. (mapcar 'list (org-property-values kwd))))
  13874. (unless (string-match "\\`{.*}\\'" value)
  13875. (setq value (concat "\"" value "\"")))
  13876. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13877. ((member ans '(?r ?R ?/))
  13878. (call-interactively 'org-occur))
  13879. (t (error "No such sparse tree command \"%c\"" ans)))))
  13880. (defvar org-occur-highlights nil)
  13881. (make-variable-buffer-local 'org-occur-highlights)
  13882. (defun org-occur (regexp &optional keep-previous callback)
  13883. "Make a compact tree which shows all matches of REGEXP.
  13884. The tree will show the lines where the regexp matches, and all higher
  13885. headlines above the match. It will also show the heading after the match,
  13886. to make sure editing the matching entry is easy.
  13887. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13888. call to `org-occur' will be kept, to allow stacking of calls to this
  13889. command.
  13890. If CALLBACK is non-nil, it is a function which is called to confirm
  13891. that the match should indeed be shown."
  13892. (interactive "sRegexp: \nP")
  13893. (or keep-previous (org-remove-occur-highlights nil nil t))
  13894. (let ((cnt 0))
  13895. (save-excursion
  13896. (goto-char (point-min))
  13897. (if (or (not keep-previous) ; do not want to keep
  13898. (not org-occur-highlights)) ; no previous matches
  13899. ;; hide everything
  13900. (org-overview))
  13901. (while (re-search-forward regexp nil t)
  13902. (when (or (not callback)
  13903. (save-match-data (funcall callback)))
  13904. (setq cnt (1+ cnt))
  13905. (when org-highlight-sparse-tree-matches
  13906. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13907. (org-show-context 'occur-tree))))
  13908. (when org-remove-highlights-with-change
  13909. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13910. nil 'local))
  13911. (unless org-sparse-tree-open-archived-trees
  13912. (org-hide-archived-subtrees (point-min) (point-max)))
  13913. (run-hooks 'org-occur-hook)
  13914. (if (interactive-p)
  13915. (message "%d match(es) for regexp %s" cnt regexp))
  13916. cnt))
  13917. (defun org-show-context (&optional key)
  13918. "Make sure point and context and visible.
  13919. How much context is shown depends upon the variables
  13920. `org-show-hierarchy-above', `org-show-following-heading'. and
  13921. `org-show-siblings'."
  13922. (let ((heading-p (org-on-heading-p t))
  13923. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13924. (following-p (org-get-alist-option org-show-following-heading key))
  13925. (entry-p (org-get-alist-option org-show-entry-below key))
  13926. (siblings-p (org-get-alist-option org-show-siblings key)))
  13927. (catch 'exit
  13928. ;; Show heading or entry text
  13929. (if (and heading-p (not entry-p))
  13930. (org-flag-heading nil) ; only show the heading
  13931. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13932. (org-show-hidden-entry))) ; show entire entry
  13933. (when following-p
  13934. ;; Show next sibling, or heading below text
  13935. (save-excursion
  13936. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13937. (org-flag-heading nil))))
  13938. (when siblings-p (org-show-siblings))
  13939. (when hierarchy-p
  13940. ;; show all higher headings, possibly with siblings
  13941. (save-excursion
  13942. (while (and (condition-case nil
  13943. (progn (org-up-heading-all 1) t)
  13944. (error nil))
  13945. (not (bobp)))
  13946. (org-flag-heading nil)
  13947. (when siblings-p (org-show-siblings))))))))
  13948. (defun org-reveal (&optional siblings)
  13949. "Show current entry, hierarchy above it, and the following headline.
  13950. This can be used to show a consistent set of context around locations
  13951. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13952. not t for the search context.
  13953. With optional argument SIBLINGS, on each level of the hierarchy all
  13954. siblings are shown. This repairs the tree structure to what it would
  13955. look like when opened with hierarchical calls to `org-cycle'."
  13956. (interactive "P")
  13957. (let ((org-show-hierarchy-above t)
  13958. (org-show-following-heading t)
  13959. (org-show-siblings (if siblings t org-show-siblings)))
  13960. (org-show-context nil)))
  13961. (defun org-highlight-new-match (beg end)
  13962. "Highlight from BEG to END and mark the highlight is an occur headline."
  13963. (let ((ov (org-make-overlay beg end)))
  13964. (org-overlay-put ov 'face 'secondary-selection)
  13965. (push ov org-occur-highlights)))
  13966. (defun org-remove-occur-highlights (&optional beg end noremove)
  13967. "Remove the occur highlights from the buffer.
  13968. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13969. from the `before-change-functions' in the current buffer."
  13970. (interactive)
  13971. (unless org-inhibit-highlight-removal
  13972. (mapc 'org-delete-overlay org-occur-highlights)
  13973. (setq org-occur-highlights nil)
  13974. (unless noremove
  13975. (remove-hook 'before-change-functions
  13976. 'org-remove-occur-highlights 'local))))
  13977. ;;;; Priorities
  13978. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13979. "Regular expression matching the priority indicator.")
  13980. (defvar org-remove-priority-next-time nil)
  13981. (defun org-priority-up ()
  13982. "Increase the priority of the current item."
  13983. (interactive)
  13984. (org-priority 'up))
  13985. (defun org-priority-down ()
  13986. "Decrease the priority of the current item."
  13987. (interactive)
  13988. (org-priority 'down))
  13989. (defun org-priority (&optional action)
  13990. "Change the priority of an item by ARG.
  13991. ACTION can be `set', `up', `down', or a character."
  13992. (interactive)
  13993. (setq action (or action 'set))
  13994. (let (current new news have remove)
  13995. (save-excursion
  13996. (org-back-to-heading)
  13997. (if (looking-at org-priority-regexp)
  13998. (setq current (string-to-char (match-string 2))
  13999. have t)
  14000. (setq current org-default-priority))
  14001. (cond
  14002. ((or (eq action 'set) (integerp action))
  14003. (if (integerp action)
  14004. (setq new action)
  14005. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  14006. (setq new (read-char-exclusive)))
  14007. (if (and (= (upcase org-highest-priority) org-highest-priority)
  14008. (= (upcase org-lowest-priority) org-lowest-priority))
  14009. (setq new (upcase new)))
  14010. (cond ((equal new ?\ ) (setq remove t))
  14011. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  14012. (error "Priority must be between `%c' and `%c'"
  14013. org-highest-priority org-lowest-priority))))
  14014. ((eq action 'up)
  14015. (if (and (not have) (eq last-command this-command))
  14016. (setq new org-lowest-priority)
  14017. (setq new (if (and org-priority-start-cycle-with-default (not have))
  14018. org-default-priority (1- current)))))
  14019. ((eq action 'down)
  14020. (if (and (not have) (eq last-command this-command))
  14021. (setq new org-highest-priority)
  14022. (setq new (if (and org-priority-start-cycle-with-default (not have))
  14023. org-default-priority (1+ current)))))
  14024. (t (error "Invalid action")))
  14025. (if (or (< (upcase new) org-highest-priority)
  14026. (> (upcase new) org-lowest-priority))
  14027. (setq remove t))
  14028. (setq news (format "%c" new))
  14029. (if have
  14030. (if remove
  14031. (replace-match "" t t nil 1)
  14032. (replace-match news t t nil 2))
  14033. (if remove
  14034. (error "No priority cookie found in line")
  14035. (looking-at org-todo-line-regexp)
  14036. (if (match-end 2)
  14037. (progn
  14038. (goto-char (match-end 2))
  14039. (insert " [#" news "]"))
  14040. (goto-char (match-beginning 3))
  14041. (insert "[#" news "] ")))))
  14042. (org-preserve-lc (org-set-tags nil 'align))
  14043. (if remove
  14044. (message "Priority removed")
  14045. (message "Priority of current item set to %s" news))))
  14046. (defun org-get-priority (s)
  14047. "Find priority cookie and return priority."
  14048. (save-match-data
  14049. (if (not (string-match org-priority-regexp s))
  14050. (* 1000 (- org-lowest-priority org-default-priority))
  14051. (* 1000 (- org-lowest-priority
  14052. (string-to-char (match-string 2 s)))))))
  14053. ;;;; Tags
  14054. (defun org-scan-tags (action matcher &optional todo-only)
  14055. "Scan headline tags with inheritance and produce output ACTION.
  14056. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  14057. evaluated, testing if a given set of tags qualifies a headline for
  14058. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  14059. are included in the output."
  14060. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  14061. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  14062. (org-re
  14063. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  14064. (props (list 'face nil
  14065. 'done-face 'org-done
  14066. 'undone-face nil
  14067. 'mouse-face 'highlight
  14068. 'org-not-done-regexp org-not-done-regexp
  14069. 'org-todo-regexp org-todo-regexp
  14070. 'keymap org-agenda-keymap
  14071. 'help-echo
  14072. (format "mouse-2 or RET jump to org file %s"
  14073. (abbreviate-file-name
  14074. (or (buffer-file-name (buffer-base-buffer))
  14075. (buffer-name (buffer-base-buffer)))))))
  14076. (case-fold-search nil)
  14077. lspos
  14078. tags tags-list tags-alist (llast 0) rtn level category i txt
  14079. todo marker entry priority)
  14080. (save-excursion
  14081. (goto-char (point-min))
  14082. (when (eq action 'sparse-tree)
  14083. (org-overview)
  14084. (org-remove-occur-highlights))
  14085. (while (re-search-forward re nil t)
  14086. (catch :skip
  14087. (setq todo (if (match-end 1) (match-string 2))
  14088. tags (if (match-end 4) (match-string 4)))
  14089. (goto-char (setq lspos (1+ (match-beginning 0))))
  14090. (setq level (org-reduced-level (funcall outline-level))
  14091. category (org-get-category))
  14092. (setq i llast llast level)
  14093. ;; remove tag lists from same and sublevels
  14094. (while (>= i level)
  14095. (when (setq entry (assoc i tags-alist))
  14096. (setq tags-alist (delete entry tags-alist)))
  14097. (setq i (1- i)))
  14098. ;; add the nex tags
  14099. (when tags
  14100. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  14101. tags-alist
  14102. (cons (cons level tags) tags-alist)))
  14103. ;; compile tags for current headline
  14104. (setq tags-list
  14105. (if org-use-tag-inheritance
  14106. (apply 'append (mapcar 'cdr tags-alist))
  14107. tags))
  14108. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  14109. (eval matcher)
  14110. (or (not org-agenda-skip-archived-trees)
  14111. (not (member org-archive-tag tags-list))))
  14112. (and (eq action 'agenda) (org-agenda-skip))
  14113. ;; list this headline
  14114. (if (eq action 'sparse-tree)
  14115. (progn
  14116. (and org-highlight-sparse-tree-matches
  14117. (org-get-heading) (match-end 0)
  14118. (org-highlight-new-match
  14119. (match-beginning 0) (match-beginning 1)))
  14120. (org-show-context 'tags-tree))
  14121. (setq txt (org-format-agenda-item
  14122. ""
  14123. (concat
  14124. (if org-tags-match-list-sublevels
  14125. (make-string (1- level) ?.) "")
  14126. (org-get-heading))
  14127. category tags-list)
  14128. priority (org-get-priority txt))
  14129. (goto-char lspos)
  14130. (setq marker (org-agenda-new-marker))
  14131. (org-add-props txt props
  14132. 'org-marker marker 'org-hd-marker marker 'org-category category
  14133. 'priority priority 'type "tagsmatch")
  14134. (push txt rtn))
  14135. ;; if we are to skip sublevels, jump to end of subtree
  14136. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  14137. (when (and (eq action 'sparse-tree)
  14138. (not org-sparse-tree-open-archived-trees))
  14139. (org-hide-archived-subtrees (point-min) (point-max)))
  14140. (nreverse rtn)))
  14141. (defvar todo-only) ;; dynamically scoped
  14142. (defun org-tags-sparse-tree (&optional todo-only match)
  14143. "Create a sparse tree according to tags string MATCH.
  14144. MATCH can contain positive and negative selection of tags, like
  14145. \"+WORK+URGENT-WITHBOSS\".
  14146. If optional argument TODO_ONLY is non-nil, only select lines that are
  14147. also TODO lines."
  14148. (interactive "P")
  14149. (org-prepare-agenda-buffers (list (current-buffer)))
  14150. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  14151. (defvar org-cached-props nil)
  14152. (defun org-cached-entry-get (pom property)
  14153. (if (or (eq t org-use-property-inheritance)
  14154. (member property org-use-property-inheritance))
  14155. ;; Caching is not possible, check it directly
  14156. (org-entry-get pom property 'inherit)
  14157. ;; Get all properties, so that we can do complicated checks easily
  14158. (cdr (assoc property (or org-cached-props
  14159. (setq org-cached-props
  14160. (org-entry-properties pom)))))))
  14161. (defun org-global-tags-completion-table (&optional files)
  14162. "Return the list of all tags in all agenda buffer/files."
  14163. (save-excursion
  14164. (org-uniquify
  14165. (delq nil
  14166. (apply 'append
  14167. (mapcar
  14168. (lambda (file)
  14169. (set-buffer (find-file-noselect file))
  14170. (append (org-get-buffer-tags)
  14171. (mapcar (lambda (x) (if (stringp (car-safe x))
  14172. (list (car-safe x)) nil))
  14173. org-tag-alist)))
  14174. (if (and files (car files))
  14175. files
  14176. (org-agenda-files))))))))
  14177. (defun org-make-tags-matcher (match)
  14178. "Create the TAGS//TODO matcher form for the selection string MATCH."
  14179. ;; todo-only is scoped dynamically into this function, and the function
  14180. ;; may change it it the matcher asksk for it.
  14181. (unless match
  14182. ;; Get a new match request, with completion
  14183. (let ((org-last-tags-completion-table
  14184. (org-global-tags-completion-table)))
  14185. (setq match (completing-read
  14186. "Match: " 'org-tags-completion-function nil nil nil
  14187. 'org-tags-history))))
  14188. ;; Parse the string and create a lisp form
  14189. (let ((match0 match)
  14190. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  14191. minus tag mm
  14192. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  14193. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  14194. (if (string-match "/+" match)
  14195. ;; match contains also a todo-matching request
  14196. (progn
  14197. (setq tagsmatch (substring match 0 (match-beginning 0))
  14198. todomatch (substring match (match-end 0)))
  14199. (if (string-match "^!" todomatch)
  14200. (setq todo-only t todomatch (substring todomatch 1)))
  14201. (if (string-match "^\\s-*$" todomatch)
  14202. (setq todomatch nil)))
  14203. ;; only matching tags
  14204. (setq tagsmatch match todomatch nil))
  14205. ;; Make the tags matcher
  14206. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  14207. (setq tagsmatcher t)
  14208. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  14209. (while (setq term (pop orterms))
  14210. (while (and (equal (substring term -1) "\\") orterms)
  14211. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  14212. (while (string-match re term)
  14213. (setq minus (and (match-end 1)
  14214. (equal (match-string 1 term) "-"))
  14215. tag (match-string 2 term)
  14216. re-p (equal (string-to-char tag) ?{)
  14217. level-p (match-end 3)
  14218. prop-p (match-end 4)
  14219. mm (cond
  14220. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  14221. (level-p `(= level ,(string-to-number
  14222. (match-string 3 term))))
  14223. (prop-p
  14224. (setq pn (match-string 4 term)
  14225. pv (match-string 5 term)
  14226. cat-p (equal pn "CATEGORY")
  14227. re-p (equal (string-to-char pv) ?{)
  14228. pv (substring pv 1 -1))
  14229. (if (equal pn "CATEGORY")
  14230. (setq gv '(get-text-property (point) 'org-category))
  14231. (setq gv `(org-cached-entry-get nil ,pn)))
  14232. (if re-p
  14233. `(string-match ,pv (or ,gv ""))
  14234. `(equal ,pv (or ,gv ""))))
  14235. (t `(member ,(downcase tag) tags-list)))
  14236. mm (if minus (list 'not mm) mm)
  14237. term (substring term (match-end 0)))
  14238. (push mm tagsmatcher))
  14239. (push (if (> (length tagsmatcher) 1)
  14240. (cons 'and tagsmatcher)
  14241. (car tagsmatcher))
  14242. orlist)
  14243. (setq tagsmatcher nil))
  14244. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14245. (setq tagsmatcher
  14246. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14247. ;; Make the todo matcher
  14248. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14249. (setq todomatcher t)
  14250. (setq orterms (org-split-string todomatch "|") orlist nil)
  14251. (while (setq term (pop orterms))
  14252. (while (string-match re term)
  14253. (setq minus (and (match-end 1)
  14254. (equal (match-string 1 term) "-"))
  14255. kwd (match-string 2 term)
  14256. re-p (equal (string-to-char kwd) ?{)
  14257. term (substring term (match-end 0))
  14258. mm (if re-p
  14259. `(string-match ,(substring kwd 1 -1) todo)
  14260. (list 'equal 'todo kwd))
  14261. mm (if minus (list 'not mm) mm))
  14262. (push mm todomatcher))
  14263. (push (if (> (length todomatcher) 1)
  14264. (cons 'and todomatcher)
  14265. (car todomatcher))
  14266. orlist)
  14267. (setq todomatcher nil))
  14268. (setq todomatcher (if (> (length orlist) 1)
  14269. (cons 'or orlist) (car orlist))))
  14270. ;; Return the string and lisp forms of the matcher
  14271. (setq matcher (if todomatcher
  14272. (list 'and tagsmatcher todomatcher)
  14273. tagsmatcher))
  14274. (cons match0 matcher)))
  14275. (defun org-match-any-p (re list)
  14276. "Does re match any element of list?"
  14277. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14278. (delq nil list))
  14279. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14280. (defvar org-tags-overlay (org-make-overlay 1 1))
  14281. (org-detach-overlay org-tags-overlay)
  14282. (defun org-align-tags-here (to-col)
  14283. ;; Assumes that this is a headline
  14284. (let ((pos (point)) (col (current-column)) tags)
  14285. (beginning-of-line 1)
  14286. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14287. (< pos (match-beginning 2)))
  14288. (progn
  14289. (setq tags (match-string 2))
  14290. (goto-char (match-beginning 1))
  14291. (insert " ")
  14292. (delete-region (point) (1+ (match-end 0)))
  14293. (backward-char 1)
  14294. (move-to-column
  14295. (max (1+ (current-column))
  14296. (1+ col)
  14297. (if (> to-col 0)
  14298. to-col
  14299. (- (abs to-col) (length tags))))
  14300. t)
  14301. (insert tags)
  14302. (move-to-column (min (current-column) col) t))
  14303. (goto-char pos))))
  14304. (defun org-set-tags (&optional arg just-align)
  14305. "Set the tags for the current headline.
  14306. With prefix ARG, realign all tags in headings in the current buffer."
  14307. (interactive "P")
  14308. (let* ((re (concat "^" outline-regexp))
  14309. (current (org-get-tags-string))
  14310. (col (current-column))
  14311. (org-setting-tags t)
  14312. table current-tags inherited-tags ; computed below when needed
  14313. tags p0 c0 c1 rpl)
  14314. (if arg
  14315. (save-excursion
  14316. (goto-char (point-min))
  14317. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14318. (while (re-search-forward re nil t)
  14319. (org-set-tags nil t)
  14320. (end-of-line 1)))
  14321. (message "All tags realigned to column %d" org-tags-column))
  14322. (if just-align
  14323. (setq tags current)
  14324. ;; Get a new set of tags from the user
  14325. (save-excursion
  14326. (setq table (or org-tag-alist (org-get-buffer-tags))
  14327. org-last-tags-completion-table table
  14328. current-tags (org-split-string current ":")
  14329. inherited-tags (nreverse
  14330. (nthcdr (length current-tags)
  14331. (nreverse (org-get-tags-at))))
  14332. tags
  14333. (if (or (eq t org-use-fast-tag-selection)
  14334. (and org-use-fast-tag-selection
  14335. (delq nil (mapcar 'cdr table))))
  14336. (org-fast-tag-selection
  14337. current-tags inherited-tags table
  14338. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14339. (let ((org-add-colon-after-tag-completion t))
  14340. (org-trim
  14341. (org-without-partial-completion
  14342. (completing-read "Tags: " 'org-tags-completion-function
  14343. nil nil current 'org-tags-history)))))))
  14344. (while (string-match "[-+&]+" tags)
  14345. ;; No boolean logic, just a list
  14346. (setq tags (replace-match ":" t t tags))))
  14347. (if (string-match "\\`[\t ]*\\'" tags)
  14348. (setq tags "")
  14349. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14350. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14351. ;; Insert new tags at the correct column
  14352. (beginning-of-line 1)
  14353. (cond
  14354. ((and (equal current "") (equal tags "")))
  14355. ((re-search-forward
  14356. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14357. (point-at-eol) t)
  14358. (if (equal tags "")
  14359. (setq rpl "")
  14360. (goto-char (match-beginning 0))
  14361. (setq c0 (current-column) p0 (point)
  14362. c1 (max (1+ c0) (if (> org-tags-column 0)
  14363. org-tags-column
  14364. (- (- org-tags-column) (length tags))))
  14365. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14366. (replace-match rpl t t)
  14367. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14368. tags)
  14369. (t (error "Tags alignment failed")))
  14370. (move-to-column col)
  14371. (unless just-align
  14372. (run-hooks 'org-after-tags-change-hook)))))
  14373. (defun org-change-tag-in-region (beg end tag off)
  14374. "Add or remove TAG for each entry in the region.
  14375. This works in the agenda, and also in an org-mode buffer."
  14376. (interactive
  14377. (list (region-beginning) (region-end)
  14378. (let ((org-last-tags-completion-table
  14379. (if (org-mode-p)
  14380. (org-get-buffer-tags)
  14381. (org-global-tags-completion-table))))
  14382. (completing-read
  14383. "Tag: " 'org-tags-completion-function nil nil nil
  14384. 'org-tags-history))
  14385. (progn
  14386. (message "[s]et or [r]emove? ")
  14387. (equal (read-char-exclusive) ?r))))
  14388. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14389. (let ((agendap (equal major-mode 'org-agenda-mode))
  14390. l1 l2 m buf pos newhead (cnt 0))
  14391. (goto-char end)
  14392. (setq l2 (1- (org-current-line)))
  14393. (goto-char beg)
  14394. (setq l1 (org-current-line))
  14395. (loop for l from l1 to l2 do
  14396. (goto-line l)
  14397. (setq m (get-text-property (point) 'org-hd-marker))
  14398. (when (or (and (org-mode-p) (org-on-heading-p))
  14399. (and agendap m))
  14400. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14401. pos (if agendap m (point)))
  14402. (with-current-buffer buf
  14403. (save-excursion
  14404. (save-restriction
  14405. (goto-char pos)
  14406. (setq cnt (1+ cnt))
  14407. (org-toggle-tag tag (if off 'off 'on))
  14408. (setq newhead (org-get-heading)))))
  14409. (and agendap (org-agenda-change-all-lines newhead m))))
  14410. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14411. (defun org-tags-completion-function (string predicate &optional flag)
  14412. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14413. (confirm (lambda (x) (stringp (car x)))))
  14414. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14415. (setq s1 (match-string 1 string)
  14416. s2 (match-string 2 string))
  14417. (setq s1 "" s2 string))
  14418. (cond
  14419. ((eq flag nil)
  14420. ;; try completion
  14421. (setq rtn (try-completion s2 ctable confirm))
  14422. (if (stringp rtn)
  14423. (setq rtn
  14424. (concat s1 s2 (substring rtn (length s2))
  14425. (if (and org-add-colon-after-tag-completion
  14426. (assoc rtn ctable))
  14427. ":" ""))))
  14428. rtn)
  14429. ((eq flag t)
  14430. ;; all-completions
  14431. (all-completions s2 ctable confirm)
  14432. )
  14433. ((eq flag 'lambda)
  14434. ;; exact match?
  14435. (assoc s2 ctable)))
  14436. ))
  14437. (defun org-fast-tag-insert (kwd tags face &optional end)
  14438. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14439. (insert (format "%-12s" (concat kwd ":"))
  14440. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14441. (or end "")))
  14442. (defun org-fast-tag-show-exit (flag)
  14443. (save-excursion
  14444. (goto-line 3)
  14445. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14446. (replace-match ""))
  14447. (when flag
  14448. (end-of-line 1)
  14449. (move-to-column (- (window-width) 19) t)
  14450. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14451. (defun org-set-current-tags-overlay (current prefix)
  14452. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14453. (if (featurep 'xemacs)
  14454. (org-overlay-display org-tags-overlay (concat prefix s)
  14455. 'secondary-selection)
  14456. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14457. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14458. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14459. "Fast tag selection with single keys.
  14460. CURRENT is the current list of tags in the headline, INHERITED is the
  14461. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14462. possibly with grouping information. TODO-TABLE is a similar table with
  14463. TODO keywords, should these have keys assigned to them.
  14464. If the keys are nil, a-z are automatically assigned.
  14465. Returns the new tags string, or nil to not change the current settings."
  14466. (let* ((fulltable (append table todo-table))
  14467. (maxlen (apply 'max (mapcar
  14468. (lambda (x)
  14469. (if (stringp (car x)) (string-width (car x)) 0))
  14470. fulltable)))
  14471. (buf (current-buffer))
  14472. (expert (eq org-fast-tag-selection-single-key 'expert))
  14473. (buffer-tags nil)
  14474. (fwidth (+ maxlen 3 1 3))
  14475. (ncol (/ (- (window-width) 4) fwidth))
  14476. (i-face 'org-done)
  14477. (c-face 'org-todo)
  14478. tg cnt e c char c1 c2 ntable tbl rtn
  14479. ov-start ov-end ov-prefix
  14480. (exit-after-next org-fast-tag-selection-single-key)
  14481. (done-keywords org-done-keywords)
  14482. groups ingroup)
  14483. (save-excursion
  14484. (beginning-of-line 1)
  14485. (if (looking-at
  14486. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14487. (setq ov-start (match-beginning 1)
  14488. ov-end (match-end 1)
  14489. ov-prefix "")
  14490. (setq ov-start (1- (point-at-eol))
  14491. ov-end (1+ ov-start))
  14492. (skip-chars-forward "^\n\r")
  14493. (setq ov-prefix
  14494. (concat
  14495. (buffer-substring (1- (point)) (point))
  14496. (if (> (current-column) org-tags-column)
  14497. " "
  14498. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14499. (org-move-overlay org-tags-overlay ov-start ov-end)
  14500. (save-window-excursion
  14501. (if expert
  14502. (set-buffer (get-buffer-create " *Org tags*"))
  14503. (delete-other-windows)
  14504. (split-window-vertically)
  14505. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14506. (erase-buffer)
  14507. (org-set-local 'org-done-keywords done-keywords)
  14508. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14509. (org-fast-tag-insert "Current" current c-face "\n\n")
  14510. (org-fast-tag-show-exit exit-after-next)
  14511. (org-set-current-tags-overlay current ov-prefix)
  14512. (setq tbl fulltable char ?a cnt 0)
  14513. (while (setq e (pop tbl))
  14514. (cond
  14515. ((equal e '(:startgroup))
  14516. (push '() groups) (setq ingroup t)
  14517. (when (not (= cnt 0))
  14518. (setq cnt 0)
  14519. (insert "\n"))
  14520. (insert "{ "))
  14521. ((equal e '(:endgroup))
  14522. (setq ingroup nil cnt 0)
  14523. (insert "}\n"))
  14524. (t
  14525. (setq tg (car e) c2 nil)
  14526. (if (cdr e)
  14527. (setq c (cdr e))
  14528. ;; automatically assign a character.
  14529. (setq c1 (string-to-char
  14530. (downcase (substring
  14531. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14532. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14533. (while (or (rassoc char ntable) (rassoc char table))
  14534. (setq char (1+ char)))
  14535. (setq c2 c1))
  14536. (setq c (or c2 char)))
  14537. (if ingroup (push tg (car groups)))
  14538. (setq tg (org-add-props tg nil 'face
  14539. (cond
  14540. ((not (assoc tg table))
  14541. (org-get-todo-face tg))
  14542. ((member tg current) c-face)
  14543. ((member tg inherited) i-face)
  14544. (t nil))))
  14545. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14546. (insert "[" c "] " tg (make-string
  14547. (- fwidth 4 (length tg)) ?\ ))
  14548. (push (cons tg c) ntable)
  14549. (when (= (setq cnt (1+ cnt)) ncol)
  14550. (insert "\n")
  14551. (if ingroup (insert " "))
  14552. (setq cnt 0)))))
  14553. (setq ntable (nreverse ntable))
  14554. (insert "\n")
  14555. (goto-char (point-min))
  14556. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14557. (fit-window-to-buffer))
  14558. (setq rtn
  14559. (catch 'exit
  14560. (while t
  14561. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14562. (if groups " [!] no groups" " [!]groups")
  14563. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14564. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14565. (cond
  14566. ((= c ?\r) (throw 'exit t))
  14567. ((= c ?!)
  14568. (setq groups (not groups))
  14569. (goto-char (point-min))
  14570. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14571. ((= c ?\C-c)
  14572. (if (not expert)
  14573. (org-fast-tag-show-exit
  14574. (setq exit-after-next (not exit-after-next)))
  14575. (setq expert nil)
  14576. (delete-other-windows)
  14577. (split-window-vertically)
  14578. (org-switch-to-buffer-other-window " *Org tags*")
  14579. (and (fboundp 'fit-window-to-buffer)
  14580. (fit-window-to-buffer))))
  14581. ((or (= c ?\C-g)
  14582. (and (= c ?q) (not (rassoc c ntable))))
  14583. (org-detach-overlay org-tags-overlay)
  14584. (setq quit-flag t))
  14585. ((= c ?\ )
  14586. (setq current nil)
  14587. (if exit-after-next (setq exit-after-next 'now)))
  14588. ((= c ?\t)
  14589. (condition-case nil
  14590. (setq tg (completing-read
  14591. "Tag: "
  14592. (or buffer-tags
  14593. (with-current-buffer buf
  14594. (org-get-buffer-tags)))))
  14595. (quit (setq tg "")))
  14596. (when (string-match "\\S-" tg)
  14597. (add-to-list 'buffer-tags (list tg))
  14598. (if (member tg current)
  14599. (setq current (delete tg current))
  14600. (push tg current)))
  14601. (if exit-after-next (setq exit-after-next 'now)))
  14602. ((setq e (rassoc c todo-table) tg (car e))
  14603. (with-current-buffer buf
  14604. (save-excursion (org-todo tg)))
  14605. (if exit-after-next (setq exit-after-next 'now)))
  14606. ((setq e (rassoc c ntable) tg (car e))
  14607. (if (member tg current)
  14608. (setq current (delete tg current))
  14609. (loop for g in groups do
  14610. (if (member tg g)
  14611. (mapc (lambda (x)
  14612. (setq current (delete x current)))
  14613. g)))
  14614. (push tg current))
  14615. (if exit-after-next (setq exit-after-next 'now))))
  14616. ;; Create a sorted list
  14617. (setq current
  14618. (sort current
  14619. (lambda (a b)
  14620. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14621. (if (eq exit-after-next 'now) (throw 'exit t))
  14622. (goto-char (point-min))
  14623. (beginning-of-line 2)
  14624. (delete-region (point) (point-at-eol))
  14625. (org-fast-tag-insert "Current" current c-face)
  14626. (org-set-current-tags-overlay current ov-prefix)
  14627. (while (re-search-forward
  14628. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14629. (setq tg (match-string 1))
  14630. (add-text-properties
  14631. (match-beginning 1) (match-end 1)
  14632. (list 'face
  14633. (cond
  14634. ((member tg current) c-face)
  14635. ((member tg inherited) i-face)
  14636. (t (get-text-property (match-beginning 1) 'face))))))
  14637. (goto-char (point-min)))))
  14638. (org-detach-overlay org-tags-overlay)
  14639. (if rtn
  14640. (mapconcat 'identity current ":")
  14641. nil))))
  14642. (defun org-get-tags-string ()
  14643. "Get the TAGS string in the current headline."
  14644. (unless (org-on-heading-p t)
  14645. (error "Not on a heading"))
  14646. (save-excursion
  14647. (beginning-of-line 1)
  14648. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14649. (org-match-string-no-properties 1)
  14650. "")))
  14651. (defun org-get-tags ()
  14652. "Get the list of tags specified in the current headline."
  14653. (org-split-string (org-get-tags-string) ":"))
  14654. (defun org-get-buffer-tags ()
  14655. "Get a table of all tags used in the buffer, for completion."
  14656. (let (tags)
  14657. (save-excursion
  14658. (goto-char (point-min))
  14659. (while (re-search-forward
  14660. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14661. (when (equal (char-after (point-at-bol 0)) ?*)
  14662. (mapc (lambda (x) (add-to-list 'tags x))
  14663. (org-split-string (org-match-string-no-properties 1) ":")))))
  14664. (mapcar 'list tags)))
  14665. ;;;; Properties
  14666. ;;; Setting and retrieving properties
  14667. (defconst org-special-properties
  14668. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14669. "TIMESTAMP" "TIMESTAMP_IA")
  14670. "The special properties valid in Org-mode.
  14671. These are properties that are not defined in the property drawer,
  14672. but in some other way.")
  14673. (defconst org-default-properties
  14674. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14675. "LOCATION" "LOGGING" "COLUMNS")
  14676. "Some properties that are used by Org-mode for various purposes.
  14677. Being in this list makes sure that they are offered for completion.")
  14678. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14679. "Regular expression matching the first line of a property drawer.")
  14680. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14681. "Regular expression matching the first line of a property drawer.")
  14682. (defun org-property-action ()
  14683. "Do an action on properties."
  14684. (interactive)
  14685. (let (c)
  14686. (org-at-property-p)
  14687. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14688. (setq c (read-char-exclusive))
  14689. (cond
  14690. ((equal c ?s)
  14691. (call-interactively 'org-set-property))
  14692. ((equal c ?d)
  14693. (call-interactively 'org-delete-property))
  14694. ((equal c ?D)
  14695. (call-interactively 'org-delete-property-globally))
  14696. ((equal c ?c)
  14697. (call-interactively 'org-compute-property-at-point))
  14698. (t (error "No such property action %c" c)))))
  14699. (defun org-at-property-p ()
  14700. "Is the cursor in a property line?"
  14701. ;; FIXME: Does not check if we are actually in the drawer.
  14702. ;; FIXME: also returns true on any drawers.....
  14703. ;; This is used by C-c C-c for property action.
  14704. (save-excursion
  14705. (beginning-of-line 1)
  14706. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14707. (defmacro org-with-point-at (pom &rest body)
  14708. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14709. (declare (indent 1) (debug t))
  14710. `(save-excursion
  14711. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14712. (save-excursion
  14713. (goto-char (or pom (point)))
  14714. ,@body)))
  14715. (defun org-get-property-block (&optional beg end force)
  14716. "Return the (beg . end) range of the body of the property drawer.
  14717. BEG and END can be beginning and end of subtree, if not given
  14718. they will be found.
  14719. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14720. (catch 'exit
  14721. (save-excursion
  14722. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14723. (end (or end (progn (outline-next-heading) (point)))))
  14724. (goto-char beg)
  14725. (if (re-search-forward org-property-start-re end t)
  14726. (setq beg (1+ (match-end 0)))
  14727. (if force
  14728. (save-excursion
  14729. (org-insert-property-drawer)
  14730. (setq end (progn (outline-next-heading) (point))))
  14731. (throw 'exit nil))
  14732. (goto-char beg)
  14733. (if (re-search-forward org-property-start-re end t)
  14734. (setq beg (1+ (match-end 0)))))
  14735. (if (re-search-forward org-property-end-re end t)
  14736. (setq end (match-beginning 0))
  14737. (or force (throw 'exit nil))
  14738. (goto-char beg)
  14739. (setq end beg)
  14740. (org-indent-line-function)
  14741. (insert ":END:\n"))
  14742. (cons beg end)))))
  14743. (defun org-entry-properties (&optional pom which)
  14744. "Get all properties of the entry at point-or-marker POM.
  14745. This includes the TODO keyword, the tags, time strings for deadline,
  14746. scheduled, and clocking, and any additional properties defined in the
  14747. entry. The return value is an alist, keys may occur multiple times
  14748. if the property key was used several times.
  14749. POM may also be nil, in which case the current entry is used.
  14750. If WHICH is nil or `all', get all properties. If WHICH is
  14751. `special' or `standard', only get that subclass."
  14752. (setq which (or which 'all))
  14753. (org-with-point-at pom
  14754. (let ((clockstr (substring org-clock-string 0 -1))
  14755. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14756. beg end range props sum-props key value string clocksum)
  14757. (save-excursion
  14758. (when (condition-case nil (org-back-to-heading t) (error nil))
  14759. (setq beg (point))
  14760. (setq sum-props (get-text-property (point) 'org-summaries))
  14761. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14762. (outline-next-heading)
  14763. (setq end (point))
  14764. (when (memq which '(all special))
  14765. ;; Get the special properties, like TODO and tags
  14766. (goto-char beg)
  14767. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14768. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14769. (when (looking-at org-priority-regexp)
  14770. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14771. (when (and (setq value (org-get-tags-string))
  14772. (string-match "\\S-" value))
  14773. (push (cons "TAGS" value) props))
  14774. (when (setq value (org-get-tags-at))
  14775. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14776. props))
  14777. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14778. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14779. string (if (equal key clockstr)
  14780. (org-no-properties
  14781. (org-trim
  14782. (buffer-substring
  14783. (match-beginning 3) (goto-char (point-at-eol)))))
  14784. (substring (org-match-string-no-properties 3) 1 -1)))
  14785. (unless key
  14786. (if (= (char-after (match-beginning 3)) ?\[)
  14787. (setq key "TIMESTAMP_IA")
  14788. (setq key "TIMESTAMP")))
  14789. (when (or (equal key clockstr) (not (assoc key props)))
  14790. (push (cons key string) props)))
  14791. )
  14792. (when (memq which '(all standard))
  14793. ;; Get the standard properties, like :PORP: ...
  14794. (setq range (org-get-property-block beg end))
  14795. (when range
  14796. (goto-char (car range))
  14797. (while (re-search-forward
  14798. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14799. (cdr range) t)
  14800. (setq key (org-match-string-no-properties 1)
  14801. value (org-trim (or (org-match-string-no-properties 2) "")))
  14802. (unless (member key excluded)
  14803. (push (cons key (or value "")) props)))))
  14804. (if clocksum
  14805. (push (cons "CLOCKSUM"
  14806. (org-column-number-to-string (/ (float clocksum) 60.)
  14807. 'add_times))
  14808. props))
  14809. (append sum-props (nreverse props)))))))
  14810. (defun org-entry-get (pom property &optional inherit)
  14811. "Get value of PROPERTY for entry at point-or-marker POM.
  14812. If INHERIT is non-nil and the entry does not have the property,
  14813. then also check higher levels of the hierarchy.
  14814. If the property is present but empty, the return value is the empty string.
  14815. If the property is not present at all, nil is returned."
  14816. (org-with-point-at pom
  14817. (if inherit
  14818. (org-entry-get-with-inheritance property)
  14819. (if (member property org-special-properties)
  14820. ;; We need a special property. Use brute force, get all properties.
  14821. (cdr (assoc property (org-entry-properties nil 'special)))
  14822. (let ((range (org-get-property-block)))
  14823. (if (and range
  14824. (goto-char (car range))
  14825. (re-search-forward
  14826. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14827. (cdr range) t))
  14828. ;; Found the property, return it.
  14829. (if (match-end 1)
  14830. (org-match-string-no-properties 1)
  14831. "")))))))
  14832. (defun org-entry-delete (pom property)
  14833. "Delete the property PROPERTY from entry at point-or-marker POM."
  14834. (org-with-point-at pom
  14835. (if (member property org-special-properties)
  14836. nil ; cannot delete these properties.
  14837. (let ((range (org-get-property-block)))
  14838. (if (and range
  14839. (goto-char (car range))
  14840. (re-search-forward
  14841. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14842. (cdr range) t))
  14843. (progn
  14844. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14845. t)
  14846. nil)))))
  14847. ;; Multi-values properties are properties that contain multiple values
  14848. ;; These values are assumed to be single words, separated by whitespace.
  14849. (defun org-entry-add-to-multivalued-property (pom property value)
  14850. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14851. (let* ((old (org-entry-get pom property))
  14852. (values (and old (org-split-string old "[ \t]"))))
  14853. (unless (member value values)
  14854. (setq values (cons value values))
  14855. (org-entry-put pom property
  14856. (mapconcat 'identity values " ")))))
  14857. (defun org-entry-remove-from-multivalued-property (pom property value)
  14858. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14859. (let* ((old (org-entry-get pom property))
  14860. (values (and old (org-split-string old "[ \t]"))))
  14861. (when (member value values)
  14862. (setq values (delete value values))
  14863. (org-entry-put pom property
  14864. (mapconcat 'identity values " ")))))
  14865. (defun org-entry-member-in-multivalued-property (pom property value)
  14866. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14867. (let* ((old (org-entry-get pom property))
  14868. (values (and old (org-split-string old "[ \t]"))))
  14869. (member value values)))
  14870. (defvar org-entry-property-inherited-from (make-marker))
  14871. (defun org-entry-get-with-inheritance (property)
  14872. "Get entry property, and search higher levels if not present."
  14873. (let (tmp)
  14874. (save-excursion
  14875. (save-restriction
  14876. (widen)
  14877. (catch 'ex
  14878. (while t
  14879. (when (setq tmp (org-entry-get nil property))
  14880. (org-back-to-heading t)
  14881. (move-marker org-entry-property-inherited-from (point))
  14882. (throw 'ex tmp))
  14883. (or (org-up-heading-safe) (throw 'ex nil)))))
  14884. (or tmp (cdr (assoc property org-local-properties))
  14885. (cdr (assoc property org-global-properties))))))
  14886. (defun org-entry-put (pom property value)
  14887. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14888. (org-with-point-at pom
  14889. (org-back-to-heading t)
  14890. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14891. range)
  14892. (cond
  14893. ((equal property "TODO")
  14894. (when (and (stringp value) (string-match "\\S-" value)
  14895. (not (member value org-todo-keywords-1)))
  14896. (error "\"%s\" is not a valid TODO state" value))
  14897. (if (or (not value)
  14898. (not (string-match "\\S-" value)))
  14899. (setq value 'none))
  14900. (org-todo value)
  14901. (org-set-tags nil 'align))
  14902. ((equal property "PRIORITY")
  14903. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14904. (string-to-char value) ?\ ))
  14905. (org-set-tags nil 'align))
  14906. ((equal property "SCHEDULED")
  14907. (if (re-search-forward org-scheduled-time-regexp end t)
  14908. (cond
  14909. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14910. ((eq value 'later) (org-timestamp-change 1 'day))
  14911. (t (call-interactively 'org-schedule)))
  14912. (call-interactively 'org-schedule)))
  14913. ((equal property "DEADLINE")
  14914. (if (re-search-forward org-deadline-time-regexp end t)
  14915. (cond
  14916. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14917. ((eq value 'later) (org-timestamp-change 1 'day))
  14918. (t (call-interactively 'org-deadline)))
  14919. (call-interactively 'org-deadline)))
  14920. ((member property org-special-properties)
  14921. (error "The %s property can not yet be set with `org-entry-put'"
  14922. property))
  14923. (t ; a non-special property
  14924. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14925. (setq range (org-get-property-block beg end 'force))
  14926. (goto-char (car range))
  14927. (if (re-search-forward
  14928. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14929. (progn
  14930. (delete-region (match-beginning 1) (match-end 1))
  14931. (goto-char (match-beginning 1)))
  14932. (goto-char (cdr range))
  14933. (insert "\n")
  14934. (backward-char 1)
  14935. (org-indent-line-function)
  14936. (insert ":" property ":"))
  14937. (and value (insert " " value))
  14938. (org-indent-line-function)))))))
  14939. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14940. "Get all property keys in the current buffer.
  14941. With INCLUDE-SPECIALS, also list the special properties that relect things
  14942. like tags and TODO state.
  14943. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14944. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  14945. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14946. formats in the current buffer."
  14947. (let (rtn range cfmt cols s p)
  14948. (save-excursion
  14949. (save-restriction
  14950. (widen)
  14951. (goto-char (point-min))
  14952. (while (re-search-forward org-property-start-re nil t)
  14953. (setq range (org-get-property-block))
  14954. (goto-char (car range))
  14955. (while (re-search-forward
  14956. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14957. (cdr range) t)
  14958. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14959. (outline-next-heading))))
  14960. (when include-specials
  14961. (setq rtn (append org-special-properties rtn)))
  14962. (when include-defaults
  14963. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14964. (when include-columns
  14965. (save-excursion
  14966. (save-restriction
  14967. (widen)
  14968. (goto-char (point-min))
  14969. (while (re-search-forward
  14970. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14971. nil t)
  14972. (setq cfmt (match-string 2) s 0)
  14973. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14974. cfmt s)
  14975. (setq s (match-end 0)
  14976. p (match-string 1 cfmt))
  14977. (unless (or (equal p "ITEM")
  14978. (member p org-special-properties))
  14979. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14980. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14981. (defun org-property-values (key)
  14982. "Return a list of all values of property KEY."
  14983. (save-excursion
  14984. (save-restriction
  14985. (widen)
  14986. (goto-char (point-min))
  14987. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14988. values)
  14989. (while (re-search-forward re nil t)
  14990. (add-to-list 'values (org-trim (match-string 1))))
  14991. (delete "" values)))))
  14992. (defun org-insert-property-drawer ()
  14993. "Insert a property drawer into the current entry."
  14994. (interactive)
  14995. (org-back-to-heading t)
  14996. (looking-at outline-regexp)
  14997. (let ((indent (- (match-end 0)(match-beginning 0)))
  14998. (beg (point))
  14999. (re (concat "^[ \t]*" org-keyword-time-regexp))
  15000. end hiddenp)
  15001. (outline-next-heading)
  15002. (setq end (point))
  15003. (goto-char beg)
  15004. (while (re-search-forward re end t))
  15005. (setq hiddenp (org-invisible-p))
  15006. (end-of-line 1)
  15007. (and (equal (char-after) ?\n) (forward-char 1))
  15008. (org-skip-over-state-notes)
  15009. (skip-chars-backward " \t\n\r")
  15010. (if (eq (char-before) ?*) (forward-char 1))
  15011. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  15012. (beginning-of-line 0)
  15013. (indent-to-column indent)
  15014. (beginning-of-line 2)
  15015. (indent-to-column indent)
  15016. (beginning-of-line 0)
  15017. (if hiddenp
  15018. (save-excursion
  15019. (org-back-to-heading t)
  15020. (hide-entry))
  15021. (org-flag-drawer t))))
  15022. (defun org-set-property (property value)
  15023. "In the current entry, set PROPERTY to VALUE.
  15024. When called interactively, this will prompt for a property name, offering
  15025. completion on existing and default properties. And then it will prompt
  15026. for a value, offering competion either on allowed values (via an inherited
  15027. xxx_ALL property) or on existing values in other instances of this property
  15028. in the current file."
  15029. (interactive
  15030. (let* ((prop (completing-read
  15031. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  15032. (cur (org-entry-get nil prop))
  15033. (allowed (org-property-get-allowed-values nil prop 'table))
  15034. (existing (mapcar 'list (org-property-values prop)))
  15035. (val (if allowed
  15036. (completing-read "Value: " allowed nil 'req-match)
  15037. (completing-read
  15038. (concat "Value" (if (and cur (string-match "\\S-" cur))
  15039. (concat "[" cur "]") "")
  15040. ": ")
  15041. existing nil nil "" nil cur))))
  15042. (list prop (if (equal val "") cur val))))
  15043. (unless (equal (org-entry-get nil property) value)
  15044. (org-entry-put nil property value)))
  15045. (defun org-delete-property (property)
  15046. "In the current entry, delete PROPERTY."
  15047. (interactive
  15048. (let* ((prop (completing-read
  15049. "Property: " (org-entry-properties nil 'standard))))
  15050. (list prop)))
  15051. (message "Property %s %s" property
  15052. (if (org-entry-delete nil property)
  15053. "deleted"
  15054. "was not present in the entry")))
  15055. (defun org-delete-property-globally (property)
  15056. "Remove PROPERTY globally, from all entries."
  15057. (interactive
  15058. (let* ((prop (completing-read
  15059. "Globally remove property: "
  15060. (mapcar 'list (org-buffer-property-keys)))))
  15061. (list prop)))
  15062. (save-excursion
  15063. (save-restriction
  15064. (widen)
  15065. (goto-char (point-min))
  15066. (let ((cnt 0))
  15067. (while (re-search-forward
  15068. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  15069. nil t)
  15070. (setq cnt (1+ cnt))
  15071. (replace-match ""))
  15072. (message "Property \"%s\" removed from %d entries" property cnt)))))
  15073. (defvar org-columns-current-fmt-compiled) ; defined below
  15074. (defun org-compute-property-at-point ()
  15075. "Compute the property at point.
  15076. This looks for an enclosing column format, extracts the operator and
  15077. then applies it to the proerty in the column format's scope."
  15078. (interactive)
  15079. (unless (org-at-property-p)
  15080. (error "Not at a property"))
  15081. (let ((prop (org-match-string-no-properties 2)))
  15082. (org-columns-get-format-and-top-level)
  15083. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15084. (error "No operator defined for property %s" prop))
  15085. (org-columns-compute prop)))
  15086. (defun org-property-get-allowed-values (pom property &optional table)
  15087. "Get allowed values for the property PROPERTY.
  15088. When TABLE is non-nil, return an alist that can directly be used for
  15089. completion."
  15090. (let (vals)
  15091. (cond
  15092. ((equal property "TODO")
  15093. (setq vals (org-with-point-at pom
  15094. (append org-todo-keywords-1 '("")))))
  15095. ((equal property "PRIORITY")
  15096. (let ((n org-lowest-priority))
  15097. (while (>= n org-highest-priority)
  15098. (push (char-to-string n) vals)
  15099. (setq n (1- n)))))
  15100. ((member property org-special-properties))
  15101. (t
  15102. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15103. (when (and vals (string-match "\\S-" vals))
  15104. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15105. (setq vals (mapcar (lambda (x)
  15106. (cond ((stringp x) x)
  15107. ((numberp x) (number-to-string x))
  15108. ((symbolp x) (symbol-name x))
  15109. (t "???")))
  15110. vals)))))
  15111. (if table (mapcar 'list vals) vals)))
  15112. (defun org-property-previous-allowed-value (&optional previous)
  15113. "Switch to the next allowed value for this property."
  15114. (interactive)
  15115. (org-property-next-allowed-value t))
  15116. (defun org-property-next-allowed-value (&optional previous)
  15117. "Switch to the next allowed value for this property."
  15118. (interactive)
  15119. (unless (org-at-property-p)
  15120. (error "Not at a property"))
  15121. (let* ((key (match-string 2))
  15122. (value (match-string 3))
  15123. (allowed (or (org-property-get-allowed-values (point) key)
  15124. (and (member value '("[ ]" "[-]" "[X]"))
  15125. '("[ ]" "[X]"))))
  15126. nval)
  15127. (unless allowed
  15128. (error "Allowed values for this property have not been defined"))
  15129. (if previous (setq allowed (reverse allowed)))
  15130. (if (member value allowed)
  15131. (setq nval (car (cdr (member value allowed)))))
  15132. (setq nval (or nval (car allowed)))
  15133. (if (equal nval value)
  15134. (error "Only one allowed value for this property"))
  15135. (org-at-property-p)
  15136. (replace-match (concat " :" key ": " nval) t t)
  15137. (org-indent-line-function)
  15138. (beginning-of-line 1)
  15139. (skip-chars-forward " \t")))
  15140. (defun org-find-entry-with-id (ident)
  15141. "Locate the entry that contains the ID property with exact value IDENT.
  15142. IDENT can be a string, a symbol or a number, this function will search for
  15143. the string representation of it.
  15144. Return the position where this entry starts, or nil if there is no such entry."
  15145. (let ((id (cond
  15146. ((stringp ident) ident)
  15147. ((symbol-name ident) (symbol-name ident))
  15148. ((numberp ident) (number-to-string ident))
  15149. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15150. (case-fold-search nil))
  15151. (save-excursion
  15152. (save-restriction
  15153. (widen)
  15154. (goto-char (point-min))
  15155. (when (re-search-forward
  15156. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15157. nil t)
  15158. (org-back-to-heading)
  15159. (point))))))
  15160. ;;; Column View
  15161. (defvar org-columns-overlays nil
  15162. "Holds the list of current column overlays.")
  15163. (defvar org-columns-current-fmt nil
  15164. "Local variable, holds the currently active column format.")
  15165. (defvar org-columns-current-fmt-compiled nil
  15166. "Local variable, holds the currently active column format.
  15167. This is the compiled version of the format.")
  15168. (defvar org-columns-current-widths nil
  15169. "Loval variable, holds the currently widths of fields.")
  15170. (defvar org-columns-current-maxwidths nil
  15171. "Loval variable, holds the currently active maximum column widths.")
  15172. (defvar org-columns-begin-marker (make-marker)
  15173. "Points to the position where last a column creation command was called.")
  15174. (defvar org-columns-top-level-marker (make-marker)
  15175. "Points to the position where current columns region starts.")
  15176. (defvar org-columns-map (make-sparse-keymap)
  15177. "The keymap valid in column display.")
  15178. (defun org-columns-content ()
  15179. "Switch to contents view while in columns view."
  15180. (interactive)
  15181. (org-overview)
  15182. (org-content))
  15183. (org-defkey org-columns-map "c" 'org-columns-content)
  15184. (org-defkey org-columns-map "o" 'org-overview)
  15185. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  15186. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  15187. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  15188. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  15189. (org-defkey org-columns-map "v" 'org-columns-show-value)
  15190. (org-defkey org-columns-map "q" 'org-columns-quit)
  15191. (org-defkey org-columns-map "r" 'org-columns-redo)
  15192. (org-defkey org-columns-map "g" 'org-columns-redo)
  15193. (org-defkey org-columns-map [left] 'backward-char)
  15194. (org-defkey org-columns-map "\M-b" 'backward-char)
  15195. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  15196. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  15197. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  15198. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  15199. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  15200. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  15201. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  15202. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  15203. (org-defkey org-columns-map "<" 'org-columns-narrow)
  15204. (org-defkey org-columns-map ">" 'org-columns-widen)
  15205. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  15206. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  15207. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  15208. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  15209. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  15210. '("Column"
  15211. ["Edit property" org-columns-edit-value t]
  15212. ["Next allowed value" org-columns-next-allowed-value t]
  15213. ["Previous allowed value" org-columns-previous-allowed-value t]
  15214. ["Show full value" org-columns-show-value t]
  15215. ["Edit allowed values" org-columns-edit-allowed t]
  15216. "--"
  15217. ["Edit column attributes" org-columns-edit-attributes t]
  15218. ["Increase column width" org-columns-widen t]
  15219. ["Decrease column width" org-columns-narrow t]
  15220. "--"
  15221. ["Move column right" org-columns-move-right t]
  15222. ["Move column left" org-columns-move-left t]
  15223. ["Add column" org-columns-new t]
  15224. ["Delete column" org-columns-delete t]
  15225. "--"
  15226. ["CONTENTS" org-columns-content t]
  15227. ["OVERVIEW" org-overview t]
  15228. ["Refresh columns display" org-columns-redo t]
  15229. "--"
  15230. ["Open link" org-columns-open-link t]
  15231. "--"
  15232. ["Quit" org-columns-quit t]))
  15233. (defun org-columns-new-overlay (beg end &optional string face)
  15234. "Create a new column overlay and add it to the list."
  15235. (let ((ov (org-make-overlay beg end)))
  15236. (org-overlay-put ov 'face (or face 'secondary-selection))
  15237. (org-overlay-display ov string face)
  15238. (push ov org-columns-overlays)
  15239. ov))
  15240. (defun org-columns-display-here (&optional props)
  15241. "Overlay the current line with column display."
  15242. (interactive)
  15243. (let* ((fmt org-columns-current-fmt-compiled)
  15244. (beg (point-at-bol))
  15245. (level-face (save-excursion
  15246. (beginning-of-line 1)
  15247. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  15248. (org-get-level-face 2))))
  15249. (color (list :foreground
  15250. (face-attribute (or level-face 'default) :foreground)))
  15251. props pom property ass width f string ov column val modval)
  15252. ;; Check if the entry is in another buffer.
  15253. (unless props
  15254. (if (eq major-mode 'org-agenda-mode)
  15255. (setq pom (or (get-text-property (point) 'org-hd-marker)
  15256. (get-text-property (point) 'org-marker))
  15257. props (if pom (org-entry-properties pom) nil))
  15258. (setq props (org-entry-properties nil))))
  15259. ;; Walk the format
  15260. (while (setq column (pop fmt))
  15261. (setq property (car column)
  15262. ass (if (equal property "ITEM")
  15263. (cons "ITEM"
  15264. (save-match-data
  15265. (org-no-properties
  15266. (org-remove-tabs
  15267. (buffer-substring-no-properties
  15268. (point-at-bol) (point-at-eol))))))
  15269. (assoc property props))
  15270. width (or (cdr (assoc property org-columns-current-maxwidths))
  15271. (nth 2 column)
  15272. (length property))
  15273. f (format "%%-%d.%ds | " width width)
  15274. val (or (cdr ass) "")
  15275. modval (if (equal property "ITEM")
  15276. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15277. string (format f (or modval val)))
  15278. ;; Create the overlay
  15279. (org-unmodified
  15280. (setq ov (org-columns-new-overlay
  15281. beg (setq beg (1+ beg)) string
  15282. (list color 'org-column)))
  15283. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15284. (org-overlay-put ov 'keymap org-columns-map)
  15285. (org-overlay-put ov 'org-columns-key property)
  15286. (org-overlay-put ov 'org-columns-value (cdr ass))
  15287. (org-overlay-put ov 'org-columns-value-modified modval)
  15288. (org-overlay-put ov 'org-columns-pom pom)
  15289. (org-overlay-put ov 'org-columns-format f))
  15290. (if (or (not (char-after beg))
  15291. (equal (char-after beg) ?\n))
  15292. (let ((inhibit-read-only t))
  15293. (save-excursion
  15294. (goto-char beg)
  15295. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15296. ;; Make the rest of the line disappear.
  15297. (org-unmodified
  15298. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15299. (org-overlay-put ov 'invisible t)
  15300. (org-overlay-put ov 'keymap org-columns-map)
  15301. (org-overlay-put ov 'intangible t)
  15302. (push ov org-columns-overlays)
  15303. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15304. (org-overlay-put ov 'keymap org-columns-map)
  15305. (push ov org-columns-overlays)
  15306. (let ((inhibit-read-only t))
  15307. (put-text-property (max (point-min) (1- (point-at-bol)))
  15308. (min (point-max) (1+ (point-at-eol)))
  15309. 'read-only "Type `e' to edit property")))))
  15310. (defvar org-previous-header-line-format nil
  15311. "The header line format before column view was turned on.")
  15312. (defvar org-columns-inhibit-recalculation nil
  15313. "Inhibit recomputing of columns on column view startup.")
  15314. (defvar header-line-format)
  15315. (defun org-columns-display-here-title ()
  15316. "Overlay the newline before the current line with the table title."
  15317. (interactive)
  15318. (let ((fmt org-columns-current-fmt-compiled)
  15319. string (title "")
  15320. property width f column str widths)
  15321. (while (setq column (pop fmt))
  15322. (setq property (car column)
  15323. str (or (nth 1 column) property)
  15324. width (or (cdr (assoc property org-columns-current-maxwidths))
  15325. (nth 2 column)
  15326. (length str))
  15327. widths (push width widths)
  15328. f (format "%%-%d.%ds | " width width)
  15329. string (format f str)
  15330. title (concat title string)))
  15331. (setq title (concat
  15332. (org-add-props " " nil 'display '(space :align-to 0))
  15333. (org-add-props title nil 'face '(:weight bold :underline t))))
  15334. (org-set-local 'org-previous-header-line-format header-line-format)
  15335. (org-set-local 'org-columns-current-widths (nreverse widths))
  15336. (setq header-line-format title)))
  15337. (defun org-columns-remove-overlays ()
  15338. "Remove all currently active column overlays."
  15339. (interactive)
  15340. (when (marker-buffer org-columns-begin-marker)
  15341. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15342. (when (local-variable-p 'org-previous-header-line-format)
  15343. (setq header-line-format org-previous-header-line-format)
  15344. (kill-local-variable 'org-previous-header-line-format))
  15345. (move-marker org-columns-begin-marker nil)
  15346. (move-marker org-columns-top-level-marker nil)
  15347. (org-unmodified
  15348. (mapc 'org-delete-overlay org-columns-overlays)
  15349. (setq org-columns-overlays nil)
  15350. (let ((inhibit-read-only t))
  15351. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15352. (defun org-columns-cleanup-item (item fmt)
  15353. "Remove from ITEM what is a column in the format FMT."
  15354. (if (not org-complex-heading-regexp)
  15355. item
  15356. (when (string-match org-complex-heading-regexp item)
  15357. (concat
  15358. (org-add-props (concat (match-string 1 item) " ") nil
  15359. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15360. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15361. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15362. " " (match-string 4 item)
  15363. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15364. (defun org-columns-show-value ()
  15365. "Show the full value of the property."
  15366. (interactive)
  15367. (let ((value (get-char-property (point) 'org-columns-value)))
  15368. (message "Value is: %s" (or value ""))))
  15369. (defun org-columns-quit ()
  15370. "Remove the column overlays and in this way exit column editing."
  15371. (interactive)
  15372. (org-unmodified
  15373. (org-columns-remove-overlays)
  15374. (let ((inhibit-read-only t))
  15375. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15376. (when (eq major-mode 'org-agenda-mode)
  15377. (message
  15378. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15379. (defun org-columns-check-computed ()
  15380. "Check if this column value is computed.
  15381. If yes, throw an error indicating that changing it does not make sense."
  15382. (let ((val (get-char-property (point) 'org-columns-value)))
  15383. (when (and (stringp val)
  15384. (get-char-property 0 'org-computed val))
  15385. (error "This value is computed from the entry's children"))))
  15386. (defun org-columns-todo (&optional arg)
  15387. "Change the TODO state during column view."
  15388. (interactive "P")
  15389. (org-columns-edit-value "TODO"))
  15390. (defun org-columns-set-tags-or-toggle (&optional arg)
  15391. "Toggle checkbox at point, or set tags for current headline."
  15392. (interactive "P")
  15393. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15394. (get-char-property (point) 'org-columns-value))
  15395. (org-columns-next-allowed-value)
  15396. (org-columns-edit-value "TAGS")))
  15397. (defun org-columns-edit-value (&optional key)
  15398. "Edit the value of the property at point in column view.
  15399. Where possible, use the standard interface for changing this line."
  15400. (interactive)
  15401. (org-columns-check-computed)
  15402. (let* ((external-key key)
  15403. (col (current-column))
  15404. (key (or key (get-char-property (point) 'org-columns-key)))
  15405. (value (get-char-property (point) 'org-columns-value))
  15406. (bol (point-at-bol)) (eol (point-at-eol))
  15407. (pom (or (get-text-property bol 'org-hd-marker)
  15408. (point))) ; keep despite of compiler waring
  15409. (line-overlays
  15410. (delq nil (mapcar (lambda (x)
  15411. (and (eq (overlay-buffer x) (current-buffer))
  15412. (>= (overlay-start x) bol)
  15413. (<= (overlay-start x) eol)
  15414. x))
  15415. org-columns-overlays)))
  15416. nval eval allowed)
  15417. (cond
  15418. ((equal key "CLOCKSUM")
  15419. (error "This special column cannot be edited"))
  15420. ((equal key "ITEM")
  15421. (setq eval '(org-with-point-at pom
  15422. (org-edit-headline))))
  15423. ((equal key "TODO")
  15424. (setq eval '(org-with-point-at pom
  15425. (let ((current-prefix-arg
  15426. (if external-key current-prefix-arg '(4))))
  15427. (call-interactively 'org-todo)))))
  15428. ((equal key "PRIORITY")
  15429. (setq eval '(org-with-point-at pom
  15430. (call-interactively 'org-priority))))
  15431. ((equal key "TAGS")
  15432. (setq eval '(org-with-point-at pom
  15433. (let ((org-fast-tag-selection-single-key
  15434. (if (eq org-fast-tag-selection-single-key 'expert)
  15435. t org-fast-tag-selection-single-key)))
  15436. (call-interactively 'org-set-tags)))))
  15437. ((equal key "DEADLINE")
  15438. (setq eval '(org-with-point-at pom
  15439. (call-interactively 'org-deadline))))
  15440. ((equal key "SCHEDULED")
  15441. (setq eval '(org-with-point-at pom
  15442. (call-interactively 'org-schedule))))
  15443. (t
  15444. (setq allowed (org-property-get-allowed-values pom key 'table))
  15445. (if allowed
  15446. (setq nval (completing-read "Value: " allowed nil t))
  15447. (setq nval (read-string "Edit: " value)))
  15448. (setq nval (org-trim nval))
  15449. (when (not (equal nval value))
  15450. (setq eval '(org-entry-put pom key nval)))))
  15451. (when eval
  15452. (let ((inhibit-read-only t))
  15453. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15454. (unwind-protect
  15455. (progn
  15456. (setq org-columns-overlays
  15457. (org-delete-all line-overlays org-columns-overlays))
  15458. (mapc 'org-delete-overlay line-overlays)
  15459. (org-columns-eval eval))
  15460. (org-columns-display-here))))
  15461. (move-to-column col)
  15462. (if (and (org-mode-p)
  15463. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15464. (org-columns-update key))))
  15465. (defun org-edit-headline () ; FIXME: this is not columns specific
  15466. "Edit the current headline, the part without TODO keyword, TAGS."
  15467. (org-back-to-heading)
  15468. (when (looking-at org-todo-line-regexp)
  15469. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15470. (txt (match-string 3))
  15471. (post "")
  15472. txt2)
  15473. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15474. (setq post (match-string 0 txt)
  15475. txt (substring txt 0 (match-beginning 0))))
  15476. (setq txt2 (read-string "Edit: " txt))
  15477. (when (not (equal txt txt2))
  15478. (beginning-of-line 1)
  15479. (insert pre txt2 post)
  15480. (delete-region (point) (point-at-eol))
  15481. (org-set-tags nil t)))))
  15482. (defun org-columns-edit-allowed ()
  15483. "Edit the list of allowed values for the current property."
  15484. (interactive)
  15485. (let* ((key (get-char-property (point) 'org-columns-key))
  15486. (key1 (concat key "_ALL"))
  15487. (allowed (org-entry-get (point) key1 t))
  15488. nval)
  15489. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15490. (setq nval (read-string "Allowed: " allowed))
  15491. (org-entry-put
  15492. (cond ((marker-position org-entry-property-inherited-from)
  15493. org-entry-property-inherited-from)
  15494. ((marker-position org-columns-top-level-marker)
  15495. org-columns-top-level-marker))
  15496. key1 nval)))
  15497. (defmacro org-no-warnings (&rest body)
  15498. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15499. (defun org-columns-eval (form)
  15500. (let (hidep)
  15501. (save-excursion
  15502. (beginning-of-line 1)
  15503. ;; `next-line' is needed here, because it skips invisible line.
  15504. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15505. (setq hidep (org-on-heading-p 1)))
  15506. (eval form)
  15507. (and hidep (hide-entry))))
  15508. (defun org-columns-previous-allowed-value ()
  15509. "Switch to the previous allowed value for this column."
  15510. (interactive)
  15511. (org-columns-next-allowed-value t))
  15512. (defun org-columns-next-allowed-value (&optional previous)
  15513. "Switch to the next allowed value for this column."
  15514. (interactive)
  15515. (org-columns-check-computed)
  15516. (let* ((col (current-column))
  15517. (key (get-char-property (point) 'org-columns-key))
  15518. (value (get-char-property (point) 'org-columns-value))
  15519. (bol (point-at-bol)) (eol (point-at-eol))
  15520. (pom (or (get-text-property bol 'org-hd-marker)
  15521. (point))) ; keep despite of compiler waring
  15522. (line-overlays
  15523. (delq nil (mapcar (lambda (x)
  15524. (and (eq (overlay-buffer x) (current-buffer))
  15525. (>= (overlay-start x) bol)
  15526. (<= (overlay-start x) eol)
  15527. x))
  15528. org-columns-overlays)))
  15529. (allowed (or (org-property-get-allowed-values pom key)
  15530. (and (memq
  15531. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15532. '(checkbox checkbox-n-of-m checkbox-percent))
  15533. '("[ ]" "[X]"))))
  15534. nval)
  15535. (when (equal key "ITEM")
  15536. (error "Cannot edit item headline from here"))
  15537. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15538. (error "Allowed values for this property have not been defined"))
  15539. (if (member key '("SCHEDULED" "DEADLINE"))
  15540. (setq nval (if previous 'earlier 'later))
  15541. (if previous (setq allowed (reverse allowed)))
  15542. (if (member value allowed)
  15543. (setq nval (car (cdr (member value allowed)))))
  15544. (setq nval (or nval (car allowed)))
  15545. (if (equal nval value)
  15546. (error "Only one allowed value for this property")))
  15547. (let ((inhibit-read-only t))
  15548. (remove-text-properties (1- bol) eol '(read-only t))
  15549. (unwind-protect
  15550. (progn
  15551. (setq org-columns-overlays
  15552. (org-delete-all line-overlays org-columns-overlays))
  15553. (mapc 'org-delete-overlay line-overlays)
  15554. (org-columns-eval '(org-entry-put pom key nval)))
  15555. (org-columns-display-here)))
  15556. (move-to-column col)
  15557. (if (and (org-mode-p)
  15558. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15559. (org-columns-update key))))
  15560. (defun org-verify-version (task)
  15561. (cond
  15562. ((eq task 'columns)
  15563. (if (or (featurep 'xemacs)
  15564. (< emacs-major-version 22))
  15565. (error "Emacs 22 is required for the columns feature")))))
  15566. (defun org-columns-open-link (&optional arg)
  15567. (interactive "P")
  15568. (let ((value (get-char-property (point) 'org-columns-value)))
  15569. (org-open-link-from-string value arg)))
  15570. (defun org-open-link-from-string (s &optional arg)
  15571. "Open a link in the string S, as if it was in Org-mode."
  15572. (interactive)
  15573. (with-temp-buffer
  15574. (let ((org-inhibit-startup t))
  15575. (org-mode)
  15576. (insert s)
  15577. (goto-char (point-min))
  15578. (org-open-at-point arg))))
  15579. (defun org-columns-get-format-and-top-level ()
  15580. (let (fmt)
  15581. (when (condition-case nil (org-back-to-heading) (error nil))
  15582. (move-marker org-entry-property-inherited-from nil)
  15583. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15584. (setq fmt (or fmt org-columns-default-format))
  15585. (org-set-local 'org-columns-current-fmt fmt)
  15586. (org-columns-compile-format fmt)
  15587. (if (marker-position org-entry-property-inherited-from)
  15588. (move-marker org-columns-top-level-marker
  15589. org-entry-property-inherited-from)
  15590. (move-marker org-columns-top-level-marker (point)))
  15591. fmt))
  15592. (defun org-columns ()
  15593. "Turn on column view on an org-mode file."
  15594. (interactive)
  15595. (org-verify-version 'columns)
  15596. (org-columns-remove-overlays)
  15597. (move-marker org-columns-begin-marker (point))
  15598. (let (beg end fmt cache maxwidths)
  15599. (setq fmt (org-columns-get-format-and-top-level))
  15600. (save-excursion
  15601. (goto-char org-columns-top-level-marker)
  15602. (setq beg (point))
  15603. (unless org-columns-inhibit-recalculation
  15604. (org-columns-compute-all))
  15605. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15606. (point-max)))
  15607. ;; Get and cache the properties
  15608. (goto-char beg)
  15609. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15610. (save-excursion
  15611. (save-restriction
  15612. (narrow-to-region beg end)
  15613. (org-clock-sum))))
  15614. (while (re-search-forward (concat "^" outline-regexp) end t)
  15615. (push (cons (org-current-line) (org-entry-properties)) cache))
  15616. (when cache
  15617. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15618. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15619. (org-columns-display-here-title)
  15620. (mapc (lambda (x)
  15621. (goto-line (car x))
  15622. (org-columns-display-here (cdr x)))
  15623. cache)))))
  15624. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15625. "Insert a new column, to the leeft o the current column."
  15626. (interactive)
  15627. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15628. cell)
  15629. (setq prop (completing-read
  15630. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  15631. nil nil prop))
  15632. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15633. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15634. (if (string-match "\\S-" width)
  15635. (setq width (string-to-number width))
  15636. (setq width nil))
  15637. (setq fmt (completing-read "Summary [none]: "
  15638. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  15639. nil t))
  15640. (if (string-match "\\S-" fmt)
  15641. (setq fmt (intern fmt))
  15642. (setq fmt nil))
  15643. (if (eq fmt 'none) (setq fmt nil))
  15644. (if editp
  15645. (progn
  15646. (setcar editp prop)
  15647. (setcdr editp (list title width nil fmt)))
  15648. (setq cell (nthcdr (1- (current-column))
  15649. org-columns-current-fmt-compiled))
  15650. (setcdr cell (cons (list prop title width nil fmt)
  15651. (cdr cell))))
  15652. (org-columns-store-format)
  15653. (org-columns-redo)))
  15654. (defun org-columns-delete ()
  15655. "Delete the column at point from columns view."
  15656. (interactive)
  15657. (let* ((n (current-column))
  15658. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15659. (when (y-or-n-p
  15660. (format "Are you sure you want to remove column \"%s\"? " title))
  15661. (setq org-columns-current-fmt-compiled
  15662. (delq (nth n org-columns-current-fmt-compiled)
  15663. org-columns-current-fmt-compiled))
  15664. (org-columns-store-format)
  15665. (org-columns-redo)
  15666. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15667. (backward-char 1)))))
  15668. (defun org-columns-edit-attributes ()
  15669. "Edit the attributes of the current column."
  15670. (interactive)
  15671. (let* ((n (current-column))
  15672. (info (nth n org-columns-current-fmt-compiled)))
  15673. (apply 'org-columns-new info)))
  15674. (defun org-columns-widen (arg)
  15675. "Make the column wider by ARG characters."
  15676. (interactive "p")
  15677. (let* ((n (current-column))
  15678. (entry (nth n org-columns-current-fmt-compiled))
  15679. (width (or (nth 2 entry)
  15680. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15681. (setq width (max 1 (+ width arg)))
  15682. (setcar (nthcdr 2 entry) width)
  15683. (org-columns-store-format)
  15684. (org-columns-redo)))
  15685. (defun org-columns-narrow (arg)
  15686. "Make the column nrrower by ARG characters."
  15687. (interactive "p")
  15688. (org-columns-widen (- arg)))
  15689. (defun org-columns-move-right ()
  15690. "Swap this column with the one to the right."
  15691. (interactive)
  15692. (let* ((n (current-column))
  15693. (cell (nthcdr n org-columns-current-fmt-compiled))
  15694. e)
  15695. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15696. (error "Cannot shift this column further to the right"))
  15697. (setq e (car cell))
  15698. (setcar cell (car (cdr cell)))
  15699. (setcdr cell (cons e (cdr (cdr cell))))
  15700. (org-columns-store-format)
  15701. (org-columns-redo)
  15702. (forward-char 1)))
  15703. (defun org-columns-move-left ()
  15704. "Swap this column with the one to the left."
  15705. (interactive)
  15706. (let* ((n (current-column)))
  15707. (when (= n 0)
  15708. (error "Cannot shift this column further to the left"))
  15709. (backward-char 1)
  15710. (org-columns-move-right)
  15711. (backward-char 1)))
  15712. (defun org-columns-store-format ()
  15713. "Store the text version of the current columns format in appropriate place.
  15714. This is either in the COLUMNS property of the node starting the current column
  15715. display, or in the #+COLUMNS line of the current buffer."
  15716. (let (fmt (cnt 0))
  15717. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15718. (org-set-local 'org-columns-current-fmt fmt)
  15719. (if (marker-position org-columns-top-level-marker)
  15720. (save-excursion
  15721. (goto-char org-columns-top-level-marker)
  15722. (if (and (org-at-heading-p)
  15723. (org-entry-get nil "COLUMNS"))
  15724. (org-entry-put nil "COLUMNS" fmt)
  15725. (goto-char (point-min))
  15726. ;; Overwrite all #+COLUMNS lines....
  15727. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15728. (setq cnt (1+ cnt))
  15729. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15730. (unless (> cnt 0)
  15731. (goto-char (point-min))
  15732. (or (org-on-heading-p t) (outline-next-heading))
  15733. (let ((inhibit-read-only t))
  15734. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15735. (org-set-local 'org-columns-default-format fmt))))))
  15736. (defvar org-overriding-columns-format nil
  15737. "When set, overrides any other definition.")
  15738. (defvar org-agenda-view-columns-initially nil
  15739. "When set, switch to columns view immediately after creating the agenda.")
  15740. (defun org-agenda-columns ()
  15741. "Turn on column view in the agenda."
  15742. (interactive)
  15743. (org-verify-version 'columns)
  15744. (org-columns-remove-overlays)
  15745. (move-marker org-columns-begin-marker (point))
  15746. (let (fmt cache maxwidths m)
  15747. (cond
  15748. ((and (local-variable-p 'org-overriding-columns-format)
  15749. org-overriding-columns-format)
  15750. (setq fmt org-overriding-columns-format))
  15751. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15752. (setq fmt (org-entry-get m "COLUMNS" t)))
  15753. ((and (boundp 'org-columns-current-fmt)
  15754. (local-variable-p 'org-columns-current-fmt)
  15755. org-columns-current-fmt)
  15756. (setq fmt org-columns-current-fmt))
  15757. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15758. (setq m (get-text-property m 'org-hd-marker))
  15759. (setq fmt (org-entry-get m "COLUMNS" t))))
  15760. (setq fmt (or fmt org-columns-default-format))
  15761. (org-set-local 'org-columns-current-fmt fmt)
  15762. (org-columns-compile-format fmt)
  15763. (save-excursion
  15764. ;; Get and cache the properties
  15765. (goto-char (point-min))
  15766. (while (not (eobp))
  15767. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15768. (get-text-property (point) 'org-marker)))
  15769. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15770. (beginning-of-line 2))
  15771. (when cache
  15772. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15773. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15774. (org-columns-display-here-title)
  15775. (mapc (lambda (x)
  15776. (goto-line (car x))
  15777. (org-columns-display-here (cdr x)))
  15778. cache)))))
  15779. (defun org-columns-get-autowidth-alist (s cache)
  15780. "Derive the maximum column widths from the format and the cache."
  15781. (let ((start 0) rtn)
  15782. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  15783. (push (cons (match-string 1 s) 1) rtn)
  15784. (setq start (match-end 0)))
  15785. (mapc (lambda (x)
  15786. (setcdr x (apply 'max
  15787. (mapcar
  15788. (lambda (y)
  15789. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15790. cache))))
  15791. rtn)
  15792. rtn))
  15793. (defun org-columns-compute-all ()
  15794. "Compute all columns that have operators defined."
  15795. (org-unmodified
  15796. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15797. (let ((columns org-columns-current-fmt-compiled) col)
  15798. (while (setq col (pop columns))
  15799. (when (nth 3 col)
  15800. (save-excursion
  15801. (org-columns-compute (car col)))))))
  15802. (defun org-columns-update (property)
  15803. "Recompute PROPERTY, and update the columns display for it."
  15804. (org-columns-compute property)
  15805. (let (fmt val pos)
  15806. (save-excursion
  15807. (mapc (lambda (ov)
  15808. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15809. (setq pos (org-overlay-start ov))
  15810. (goto-char pos)
  15811. (when (setq val (cdr (assoc property
  15812. (get-text-property
  15813. (point-at-bol) 'org-summaries))))
  15814. (setq fmt (org-overlay-get ov 'org-columns-format))
  15815. (org-overlay-put ov 'org-columns-value val)
  15816. (org-overlay-put ov 'display (format fmt val)))))
  15817. org-columns-overlays))))
  15818. (defun org-columns-compute (property)
  15819. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15820. (interactive)
  15821. (let* ((re (concat "^" outline-regexp))
  15822. (lmax 30) ; Does anyone use deeper levels???
  15823. (lsum (make-vector lmax 0))
  15824. (lflag (make-vector lmax nil))
  15825. (level 0)
  15826. (ass (assoc property org-columns-current-fmt-compiled))
  15827. (format (nth 4 ass))
  15828. (printf (nth 5 ass))
  15829. (beg org-columns-top-level-marker)
  15830. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15831. (save-excursion
  15832. ;; Find the region to compute
  15833. (goto-char beg)
  15834. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15835. (goto-char end)
  15836. ;; Walk the tree from the back and do the computations
  15837. (while (re-search-backward re beg t)
  15838. (setq sumpos (match-beginning 0)
  15839. last-level level
  15840. level (org-outline-level)
  15841. val (org-entry-get nil property)
  15842. valflag (and val (string-match "\\S-" val)))
  15843. (cond
  15844. ((< level last-level)
  15845. ;; put the sum of lower levels here as a property
  15846. (setq sum (aref lsum last-level) ; current sum
  15847. flag (aref lflag last-level) ; any valid entries from children?
  15848. str (org-column-number-to-string sum format printf)
  15849. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15850. useval (if flag str1 (if valflag val ""))
  15851. sum-alist (get-text-property sumpos 'org-summaries))
  15852. (if (assoc property sum-alist)
  15853. (setcdr (assoc property sum-alist) useval)
  15854. (push (cons property useval) sum-alist)
  15855. (org-unmodified
  15856. (add-text-properties sumpos (1+ sumpos)
  15857. (list 'org-summaries sum-alist))))
  15858. (when val
  15859. (org-entry-put nil property (if flag str val)))
  15860. ;; add current to current level accumulator
  15861. (when (or flag valflag)
  15862. (aset lsum level (+ (aref lsum level)
  15863. (if flag sum (org-column-string-to-number
  15864. (if flag str val) format))))
  15865. (aset lflag level t))
  15866. ;; clear accumulators for deeper levels
  15867. (loop for l from (1+ level) to (1- lmax) do
  15868. (aset lsum l 0)
  15869. (aset lflag l nil)))
  15870. ((>= level last-level)
  15871. ;; add what we have here to the accumulator for this level
  15872. (aset lsum level (+ (aref lsum level)
  15873. (org-column-string-to-number (or val "0") format)))
  15874. (and valflag (aset lflag level t)))
  15875. (t (error "This should not happen")))))))
  15876. (defun org-columns-redo ()
  15877. "Construct the column display again."
  15878. (interactive)
  15879. (message "Recomputing columns...")
  15880. (save-excursion
  15881. (if (marker-position org-columns-begin-marker)
  15882. (goto-char org-columns-begin-marker))
  15883. (org-columns-remove-overlays)
  15884. (if (org-mode-p)
  15885. (call-interactively 'org-columns)
  15886. (call-interactively 'org-agenda-columns)))
  15887. (message "Recomputing columns...done"))
  15888. (defun org-columns-not-in-agenda ()
  15889. (if (eq major-mode 'org-agenda-mode)
  15890. (error "This command is only allowed in Org-mode buffers")))
  15891. (defun org-string-to-number (s)
  15892. "Convert string to number, and interpret hh:mm:ss."
  15893. (if (not (string-match ":" s))
  15894. (string-to-number s)
  15895. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15896. (while l
  15897. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15898. sum)))
  15899. (defun org-column-number-to-string (n fmt &optional printf)
  15900. "Convert a computed column number to a string value, according to FMT."
  15901. (cond
  15902. ((eq fmt 'add_times)
  15903. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15904. (format "%d:%02d" h m)))
  15905. ((eq fmt 'checkbox)
  15906. (cond ((= n (floor n)) "[X]")
  15907. ((> n 1.) "[-]")
  15908. (t "[ ]")))
  15909. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  15910. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  15911. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  15912. (printf (format printf n))
  15913. ((eq fmt 'currency)
  15914. (format "%.2f" n))
  15915. (t (number-to-string n))))
  15916. (defun org-nofm-to-completion (n m &optional percent)
  15917. (if (not percent)
  15918. (format "[%d/%d]" n m)
  15919. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  15920. (defun org-column-string-to-number (s fmt)
  15921. "Convert a column value to a number that can be used for column computing."
  15922. (cond
  15923. ((string-match ":" s)
  15924. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15925. (while l
  15926. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15927. sum))
  15928. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  15929. (if (equal s "[X]") 1. 0.000001))
  15930. (t (string-to-number s))))
  15931. (defun org-columns-uncompile-format (cfmt)
  15932. "Turn the compiled columns format back into a string representation."
  15933. (let ((rtn "") e s prop title op width fmt printf)
  15934. (while (setq e (pop cfmt))
  15935. (setq prop (car e)
  15936. title (nth 1 e)
  15937. width (nth 2 e)
  15938. op (nth 3 e)
  15939. fmt (nth 4 e)
  15940. printf (nth 5 e))
  15941. (cond
  15942. ((eq fmt 'add_times) (setq op ":"))
  15943. ((eq fmt 'checkbox) (setq op "X"))
  15944. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  15945. ((eq fmt 'checkbox-percent) (setq op "X%"))
  15946. ((eq fmt 'add_numbers) (setq op "+"))
  15947. ((eq fmt 'currency) (setq op "$")))
  15948. (if (and op printf) (setq op (concat op ";" printf)))
  15949. (if (equal title prop) (setq title nil))
  15950. (setq s (concat "%" (if width (number-to-string width))
  15951. prop
  15952. (if title (concat "(" title ")"))
  15953. (if op (concat "{" op "}"))))
  15954. (setq rtn (concat rtn " " s)))
  15955. (org-trim rtn)))
  15956. (defun org-columns-compile-format (fmt)
  15957. "Turn a column format string into an alist of specifications.
  15958. The alist has one entry for each column in the format. The elements of
  15959. that list are:
  15960. property the property
  15961. title the title field for the columns
  15962. width the column width in characters, can be nil for automatic
  15963. operator the operator if any
  15964. format the output format for computed results, derived from operator
  15965. printf a printf format for computed values"
  15966. (let ((start 0) width prop title op f printf)
  15967. (setq org-columns-current-fmt-compiled nil)
  15968. (while (string-match
  15969. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15970. fmt start)
  15971. (setq start (match-end 0)
  15972. width (match-string 1 fmt)
  15973. prop (match-string 2 fmt)
  15974. title (or (match-string 3 fmt) prop)
  15975. op (match-string 4 fmt)
  15976. f nil
  15977. printf nil)
  15978. (if width (setq width (string-to-number width)))
  15979. (when (and op (string-match ";" op))
  15980. (setq printf (substring op (match-end 0))
  15981. op (substring op 0 (match-beginning 0))))
  15982. (cond
  15983. ((equal op "+") (setq f 'add_numbers))
  15984. ((equal op "$") (setq f 'currency))
  15985. ((equal op ":") (setq f 'add_times))
  15986. ((equal op "X") (setq f 'checkbox))
  15987. ((equal op "X/") (setq f 'checkbox-n-of-m))
  15988. ((equal op "X%") (setq f 'checkbox-percent))
  15989. )
  15990. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15991. (setq org-columns-current-fmt-compiled
  15992. (nreverse org-columns-current-fmt-compiled))))
  15993. ;;; Dynamic block for Column view
  15994. (defun org-columns-capture-view ()
  15995. "Get the column view of the current buffer and return it as a list.
  15996. The list will contains the title row and all other rows. Each row is
  15997. a list of fields."
  15998. (save-excursion
  15999. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  16000. (n (length title)) row tbl)
  16001. (goto-char (point-min))
  16002. (while (re-search-forward "^\\*+ " nil t)
  16003. (when (get-char-property (match-beginning 0) 'org-columns-key)
  16004. (setq row nil)
  16005. (loop for i from 0 to (1- n) do
  16006. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  16007. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  16008. "")
  16009. row))
  16010. (setq row (nreverse row))
  16011. (push row tbl)))
  16012. (append (list title 'hline) (nreverse tbl)))))
  16013. (defun org-dblock-write:columnview (params)
  16014. "Write the column view table.
  16015. PARAMS is a property list of parameters:
  16016. :width enforce same column widths with <N> specifiers.
  16017. :id the :ID: property of the entry where the columns view
  16018. should be built, as a string. When `local', call locally.
  16019. When `global' call column view with the cursor at the beginning
  16020. of the buffer (usually this means that the whole buffer switches
  16021. to column view).
  16022. :hlines When t, insert a hline before each item. When a number, insert
  16023. a hline before each level <= that number.
  16024. :vlines When t, make each column a colgroup to enforce vertical lines."
  16025. (let ((pos (move-marker (make-marker) (point)))
  16026. (hlines (plist-get params :hlines))
  16027. (vlines (plist-get params :vlines))
  16028. tbl id idpos nfields tmp)
  16029. (save-excursion
  16030. (save-restriction
  16031. (when (setq id (plist-get params :id))
  16032. (cond ((not id) nil)
  16033. ((eq id 'global) (goto-char (point-min)))
  16034. ((eq id 'local) nil)
  16035. ((setq idpos (org-find-entry-with-id id))
  16036. (goto-char idpos))
  16037. (t (error "Cannot find entry with :ID: %s" id))))
  16038. (org-columns)
  16039. (setq tbl (org-columns-capture-view))
  16040. (setq nfields (length (car tbl)))
  16041. (org-columns-quit)))
  16042. (goto-char pos)
  16043. (move-marker pos nil)
  16044. (when tbl
  16045. (when (plist-get params :hlines)
  16046. (setq tmp nil)
  16047. (while tbl
  16048. (if (eq (car tbl) 'hline)
  16049. (push (pop tbl) tmp)
  16050. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  16051. (if (and (not (eq (car tmp) 'hline))
  16052. (or (eq hlines t)
  16053. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  16054. (push 'hline tmp)))
  16055. (push (pop tbl) tmp)))
  16056. (setq tbl (nreverse tmp)))
  16057. (when vlines
  16058. (setq tbl (mapcar (lambda (x)
  16059. (if (eq 'hline x) x (cons "" x)))
  16060. tbl))
  16061. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  16062. (setq pos (point))
  16063. (insert (org-listtable-to-string tbl))
  16064. (when (plist-get params :width)
  16065. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  16066. org-columns-current-widths "|")))
  16067. (goto-char pos)
  16068. (org-table-align))))
  16069. (defun org-listtable-to-string (tbl)
  16070. "Convert a listtable TBL to a string that contains the Org-mode table.
  16071. The table still need to be alligned. The resulting string has no leading
  16072. and tailing newline characters."
  16073. (mapconcat
  16074. (lambda (x)
  16075. (cond
  16076. ((listp x)
  16077. (concat "|" (mapconcat 'identity x "|") "|"))
  16078. ((eq x 'hline) "|-|")
  16079. (t (error "Garbage in listtable: %s" x))))
  16080. tbl "\n"))
  16081. (defun org-insert-columns-dblock ()
  16082. "Create a dynamic block capturing a column view table."
  16083. (interactive)
  16084. (let ((defaults '(:name "columnview" :hlines 1))
  16085. (id (completing-read
  16086. "Capture columns (local, global, entry with :ID: property) [local]: "
  16087. (append '(("global") ("local"))
  16088. (mapcar 'list (org-property-values "ID"))))))
  16089. (if (equal id "") (setq id 'local))
  16090. (if (equal id "global") (setq id 'global))
  16091. (setq defaults (append defaults (list :id id)))
  16092. (org-create-dblock defaults)
  16093. (org-update-dblock)))
  16094. ;;;; Timestamps
  16095. (defvar org-last-changed-timestamp nil)
  16096. (defvar org-time-was-given) ; dynamically scoped parameter
  16097. (defvar org-end-time-was-given) ; dynamically scoped parameter
  16098. (defvar org-ts-what) ; dynamically scoped parameter
  16099. (defun org-time-stamp (arg)
  16100. "Prompt for a date/time and insert a time stamp.
  16101. If the user specifies a time like HH:MM, or if this command is called
  16102. with a prefix argument, the time stamp will contain date and time.
  16103. Otherwise, only the date will be included. All parts of a date not
  16104. specified by the user will be filled in from the current date/time.
  16105. So if you press just return without typing anything, the time stamp
  16106. will represent the current date/time. If there is already a timestamp
  16107. at the cursor, it will be modified."
  16108. (interactive "P")
  16109. (let* ((ts nil)
  16110. (default-time
  16111. ;; Default time is either today, or, when entering a range,
  16112. ;; the range start.
  16113. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  16114. (save-excursion
  16115. (re-search-backward
  16116. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  16117. (- (point) 20) t)))
  16118. (apply 'encode-time (org-parse-time-string (match-string 1)))
  16119. (current-time)))
  16120. (default-input (and ts (org-get-compact-tod ts)))
  16121. org-time-was-given org-end-time-was-given time)
  16122. (cond
  16123. ((and (org-at-timestamp-p)
  16124. (eq last-command 'org-time-stamp)
  16125. (eq this-command 'org-time-stamp))
  16126. (insert "--")
  16127. (setq time (let ((this-command this-command))
  16128. (org-read-date arg 'totime nil nil default-time default-input)))
  16129. (org-insert-time-stamp time (or org-time-was-given arg)))
  16130. ((org-at-timestamp-p)
  16131. (setq time (let ((this-command this-command))
  16132. (org-read-date arg 'totime nil nil default-time default-input)))
  16133. (when (org-at-timestamp-p) ; just to get the match data
  16134. (replace-match "")
  16135. (setq org-last-changed-timestamp
  16136. (org-insert-time-stamp
  16137. time (or org-time-was-given arg)
  16138. nil nil nil (list org-end-time-was-given))))
  16139. (message "Timestamp updated"))
  16140. (t
  16141. (setq time (let ((this-command this-command))
  16142. (org-read-date arg 'totime nil nil default-time default-input)))
  16143. (org-insert-time-stamp time (or org-time-was-given arg)
  16144. nil nil nil (list org-end-time-was-given))))))
  16145. ;; FIXME: can we use this for something else????
  16146. ;; like computing time differences?????
  16147. (defun org-get-compact-tod (s)
  16148. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  16149. (let* ((t1 (match-string 1 s))
  16150. (h1 (string-to-number (match-string 2 s)))
  16151. (m1 (string-to-number (match-string 3 s)))
  16152. (t2 (and (match-end 4) (match-string 5 s)))
  16153. (h2 (and t2 (string-to-number (match-string 6 s))))
  16154. (m2 (and t2 (string-to-number (match-string 7 s))))
  16155. dh dm)
  16156. (if (not t2)
  16157. t1
  16158. (setq dh (- h2 h1) dm (- m2 m1))
  16159. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  16160. (concat t1 "+" (number-to-string dh)
  16161. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  16162. (defun org-time-stamp-inactive (&optional arg)
  16163. "Insert an inactive time stamp.
  16164. An inactive time stamp is enclosed in square brackets instead of angle
  16165. brackets. It is inactive in the sense that it does not trigger agenda entries,
  16166. does not link to the calendar and cannot be changed with the S-cursor keys.
  16167. So these are more for recording a certain time/date."
  16168. (interactive "P")
  16169. (let (org-time-was-given org-end-time-was-given time)
  16170. (setq time (org-read-date arg 'totime))
  16171. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  16172. nil nil (list org-end-time-was-given))))
  16173. (defvar org-date-ovl (org-make-overlay 1 1))
  16174. (org-overlay-put org-date-ovl 'face 'org-warning)
  16175. (org-detach-overlay org-date-ovl)
  16176. (defvar org-ans1) ; dynamically scoped parameter
  16177. (defvar org-ans2) ; dynamically scoped parameter
  16178. (defvar org-plain-time-of-day-regexp) ; defined below
  16179. (defvar org-read-date-overlay nil)
  16180. (defvar org-dcst nil) ; dynamically scoped
  16181. (defun org-read-date (&optional with-time to-time from-string prompt
  16182. default-time default-input)
  16183. "Read a date, possibly a time, and make things smooth for the user.
  16184. The prompt will suggest to enter an ISO date, but you can also enter anything
  16185. which will at least partially be understood by `parse-time-string'.
  16186. Unrecognized parts of the date will default to the current day, month, year,
  16187. hour and minute. If this command is called to replace a timestamp at point,
  16188. of to enter the second timestamp of a range, the default time is taken from the
  16189. existing stamp. For example,
  16190. 3-2-5 --> 2003-02-05
  16191. feb 15 --> currentyear-02-15
  16192. sep 12 9 --> 2009-09-12
  16193. 12:45 --> today 12:45
  16194. 22 sept 0:34 --> currentyear-09-22 0:34
  16195. 12 --> currentyear-currentmonth-12
  16196. Fri --> nearest Friday (today or later)
  16197. etc.
  16198. Furthermore you can specify a relative date by giving, as the *first* thing
  16199. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  16200. change in days weeks, months, years.
  16201. With a single plus or minus, the date is relative to today. With a double
  16202. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  16203. +4d --> four days from today
  16204. +4 --> same as above
  16205. +2w --> two weeks from today
  16206. ++5 --> five days from default date
  16207. The function understands only English month and weekday abbreviations,
  16208. but this can be configured with the variables `parse-time-months' and
  16209. `parse-time-weekdays'.
  16210. While prompting, a calendar is popped up - you can also select the
  16211. date with the mouse (button 1). The calendar shows a period of three
  16212. months. To scroll it to other months, use the keys `>' and `<'.
  16213. If you don't like the calendar, turn it off with
  16214. \(setq org-read-date-popup-calendar nil)
  16215. With optional argument TO-TIME, the date will immediately be converted
  16216. to an internal time.
  16217. With an optional argument WITH-TIME, the prompt will suggest to also
  16218. insert a time. Note that when WITH-TIME is not set, you can still
  16219. enter a time, and this function will inform the calling routine about
  16220. this change. The calling routine may then choose to change the format
  16221. used to insert the time stamp into the buffer to include the time.
  16222. With optional argument FROM-STRING, read from this string instead from
  16223. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  16224. the time/date that is used for everything that is not specified by the
  16225. user."
  16226. (require 'parse-time)
  16227. (let* ((org-time-stamp-rounding-minutes
  16228. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  16229. (org-dcst org-display-custom-times)
  16230. (ct (org-current-time))
  16231. (def (or default-time ct))
  16232. (defdecode (decode-time def))
  16233. (dummy (progn
  16234. (when (< (nth 2 defdecode) org-extend-today-until)
  16235. (setcar (nthcdr 2 defdecode) -1)
  16236. (setcar (nthcdr 1 defdecode) 59)
  16237. (setq def (apply 'encode-time defdecode)
  16238. defdecode (decode-time def)))))
  16239. (calendar-move-hook nil)
  16240. (view-diary-entries-initially nil)
  16241. (view-calendar-holidays-initially nil)
  16242. (timestr (format-time-string
  16243. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  16244. (prompt (concat (if prompt (concat prompt " ") "")
  16245. (format "Date+time [%s]: " timestr)))
  16246. ans (org-ans0 "") org-ans1 org-ans2 final)
  16247. (cond
  16248. (from-string (setq ans from-string))
  16249. (org-read-date-popup-calendar
  16250. (save-excursion
  16251. (save-window-excursion
  16252. (calendar)
  16253. (calendar-forward-day (- (time-to-days def)
  16254. (calendar-absolute-from-gregorian
  16255. (calendar-current-date))))
  16256. (org-eval-in-calendar nil t)
  16257. (let* ((old-map (current-local-map))
  16258. (map (copy-keymap calendar-mode-map))
  16259. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  16260. (org-defkey map (kbd "RET") 'org-calendar-select)
  16261. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  16262. 'org-calendar-select-mouse)
  16263. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  16264. 'org-calendar-select-mouse)
  16265. (org-defkey minibuffer-local-map [(meta shift left)]
  16266. (lambda () (interactive)
  16267. (org-eval-in-calendar '(calendar-backward-month 1))))
  16268. (org-defkey minibuffer-local-map [(meta shift right)]
  16269. (lambda () (interactive)
  16270. (org-eval-in-calendar '(calendar-forward-month 1))))
  16271. (org-defkey minibuffer-local-map [(meta shift up)]
  16272. (lambda () (interactive)
  16273. (org-eval-in-calendar '(calendar-backward-year 1))))
  16274. (org-defkey minibuffer-local-map [(meta shift down)]
  16275. (lambda () (interactive)
  16276. (org-eval-in-calendar '(calendar-forward-year 1))))
  16277. (org-defkey minibuffer-local-map [(shift up)]
  16278. (lambda () (interactive)
  16279. (org-eval-in-calendar '(calendar-backward-week 1))))
  16280. (org-defkey minibuffer-local-map [(shift down)]
  16281. (lambda () (interactive)
  16282. (org-eval-in-calendar '(calendar-forward-week 1))))
  16283. (org-defkey minibuffer-local-map [(shift left)]
  16284. (lambda () (interactive)
  16285. (org-eval-in-calendar '(calendar-backward-day 1))))
  16286. (org-defkey minibuffer-local-map [(shift right)]
  16287. (lambda () (interactive)
  16288. (org-eval-in-calendar '(calendar-forward-day 1))))
  16289. (org-defkey minibuffer-local-map ">"
  16290. (lambda () (interactive)
  16291. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16292. (org-defkey minibuffer-local-map "<"
  16293. (lambda () (interactive)
  16294. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16295. (unwind-protect
  16296. (progn
  16297. (use-local-map map)
  16298. (add-hook 'post-command-hook 'org-read-date-display)
  16299. (setq org-ans0 (read-string prompt default-input nil nil))
  16300. ;; org-ans0: from prompt
  16301. ;; org-ans1: from mouse click
  16302. ;; org-ans2: from calendar motion
  16303. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16304. (remove-hook 'post-command-hook 'org-read-date-display)
  16305. (use-local-map old-map)
  16306. (when org-read-date-overlay
  16307. (org-delete-overlay org-read-date-overlay)
  16308. (setq org-read-date-overlay nil)))))))
  16309. (t ; Naked prompt only
  16310. (unwind-protect
  16311. (setq ans (read-string prompt default-input nil timestr))
  16312. (when org-read-date-overlay
  16313. (org-delete-overlay org-read-date-overlay)
  16314. (setq org-read-date-overlay nil)))))
  16315. (setq final (org-read-date-analyze ans def defdecode))
  16316. (if to-time
  16317. (apply 'encode-time final)
  16318. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16319. (format "%04d-%02d-%02d %02d:%02d"
  16320. (nth 5 final) (nth 4 final) (nth 3 final)
  16321. (nth 2 final) (nth 1 final))
  16322. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16323. (defvar def)
  16324. (defvar defdecode)
  16325. (defvar with-time)
  16326. (defun org-read-date-display ()
  16327. "Display the currrent date prompt interpretation in the minibuffer."
  16328. (when org-read-date-display-live
  16329. (when org-read-date-overlay
  16330. (org-delete-overlay org-read-date-overlay))
  16331. (let ((p (point)))
  16332. (end-of-line 1)
  16333. (while (not (equal (buffer-substring
  16334. (max (point-min) (- (point) 4)) (point))
  16335. " "))
  16336. (insert " "))
  16337. (goto-char p))
  16338. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16339. " " (or org-ans1 org-ans2)))
  16340. (org-end-time-was-given nil)
  16341. (f (org-read-date-analyze ans def defdecode))
  16342. (fmts (if org-dcst
  16343. org-time-stamp-custom-formats
  16344. org-time-stamp-formats))
  16345. (fmt (if (or with-time
  16346. (and (boundp 'org-time-was-given) org-time-was-given))
  16347. (cdr fmts)
  16348. (car fmts)))
  16349. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16350. (when (and org-end-time-was-given
  16351. (string-match org-plain-time-of-day-regexp txt))
  16352. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16353. org-end-time-was-given
  16354. (substring txt (match-end 0)))))
  16355. (setq org-read-date-overlay
  16356. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16357. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16358. (defun org-read-date-analyze (ans def defdecode)
  16359. "Analyze the combined answer of the date prompt."
  16360. ;; FIXME: cleanup and comment
  16361. (let (delta deltan deltaw deltadef year month day
  16362. hour minute second wday pm h2 m2 tl wday1)
  16363. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16364. (setq ans (replace-match "" t t ans)
  16365. deltan (car delta)
  16366. deltaw (nth 1 delta)
  16367. deltadef (nth 2 delta)))
  16368. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16369. (when (string-match
  16370. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16371. (setq year (if (match-end 2)
  16372. (string-to-number (match-string 2 ans))
  16373. (string-to-number (format-time-string "%Y")))
  16374. month (string-to-number (match-string 3 ans))
  16375. day (string-to-number (match-string 4 ans)))
  16376. (if (< year 100) (setq year (+ 2000 year)))
  16377. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16378. t nil ans)))
  16379. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16380. ;; If there is a time with am/pm, and *no* time without it, we convert
  16381. ;; so that matching will be successful.
  16382. (loop for i from 1 to 2 do ; twice, for end time as well
  16383. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16384. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16385. (setq hour (string-to-number (match-string 1 ans))
  16386. minute (if (match-end 3)
  16387. (string-to-number (match-string 3 ans))
  16388. 0)
  16389. pm (equal ?p
  16390. (string-to-char (downcase (match-string 4 ans)))))
  16391. (if (and (= hour 12) (not pm))
  16392. (setq hour 0)
  16393. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16394. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16395. t t ans))))
  16396. ;; Check if a time range is given as a duration
  16397. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16398. (setq hour (string-to-number (match-string 1 ans))
  16399. h2 (+ hour (string-to-number (match-string 3 ans)))
  16400. minute (string-to-number (match-string 2 ans))
  16401. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16402. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16403. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16404. ;; Check if there is a time range
  16405. (when (boundp 'org-end-time-was-given)
  16406. (setq org-time-was-given nil)
  16407. (when (and (string-match org-plain-time-of-day-regexp ans)
  16408. (match-end 8))
  16409. (setq org-end-time-was-given (match-string 8 ans))
  16410. (setq ans (concat (substring ans 0 (match-beginning 7))
  16411. (substring ans (match-end 7))))))
  16412. (setq tl (parse-time-string ans)
  16413. day (or (nth 3 tl) (nth 3 defdecode))
  16414. month (or (nth 4 tl)
  16415. (if (and org-read-date-prefer-future
  16416. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16417. (1+ (nth 4 defdecode))
  16418. (nth 4 defdecode)))
  16419. year (or (nth 5 tl)
  16420. (if (and org-read-date-prefer-future
  16421. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16422. (1+ (nth 5 defdecode))
  16423. (nth 5 defdecode)))
  16424. hour (or (nth 2 tl) (nth 2 defdecode))
  16425. minute (or (nth 1 tl) (nth 1 defdecode))
  16426. second (or (nth 0 tl) 0)
  16427. wday (nth 6 tl))
  16428. (when deltan
  16429. (unless deltadef
  16430. (let ((now (decode-time (current-time))))
  16431. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16432. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16433. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16434. ((equal deltaw "m") (setq month (+ month deltan)))
  16435. ((equal deltaw "y") (setq year (+ year deltan)))))
  16436. (when (and wday (not (nth 3 tl)))
  16437. ;; Weekday was given, but no day, so pick that day in the week
  16438. ;; on or after the derived date.
  16439. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16440. (unless (equal wday wday1)
  16441. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16442. (if (and (boundp 'org-time-was-given)
  16443. (nth 2 tl))
  16444. (setq org-time-was-given t))
  16445. (if (< year 100) (setq year (+ 2000 year)))
  16446. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16447. (list second minute hour day month year)))
  16448. (defvar parse-time-weekdays)
  16449. (defun org-read-date-get-relative (s today default)
  16450. "Check string S for special relative date string.
  16451. TODAY and DEFAULT are internal times, for today and for a default.
  16452. Return shift list (N what def-flag)
  16453. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16454. N is the number of WHATs to shift.
  16455. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16456. the DEFAULT date rather than TODAY."
  16457. (when (string-match
  16458. (concat
  16459. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16460. "\\([0-9]+\\)?"
  16461. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16462. "\\([ \t]\\|$\\)") s)
  16463. (let* ((dir (if (match-end 1)
  16464. (string-to-char (substring (match-string 1 s) -1))
  16465. ?+))
  16466. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16467. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16468. (what (if (match-end 3) (match-string 3 s) "d"))
  16469. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16470. (date (if rel default today))
  16471. (wday (nth 6 (decode-time date)))
  16472. delta)
  16473. (if wday1
  16474. (progn
  16475. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16476. (if (= dir ?-) (setq delta (- delta 7)))
  16477. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16478. (list delta "d" rel))
  16479. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16480. (defun org-eval-in-calendar (form &optional keepdate)
  16481. "Eval FORM in the calendar window and return to current window.
  16482. Also, store the cursor date in variable org-ans2."
  16483. (let ((sw (selected-window)))
  16484. (select-window (get-buffer-window "*Calendar*"))
  16485. (eval form)
  16486. (when (and (not keepdate) (calendar-cursor-to-date))
  16487. (let* ((date (calendar-cursor-to-date))
  16488. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16489. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16490. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16491. (select-window sw)))
  16492. ; ;; Update the prompt to show new default date
  16493. ; (save-excursion
  16494. ; (goto-char (point-min))
  16495. ; (when (and org-ans2
  16496. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16497. ; (get-text-property (match-end 0) 'field))
  16498. ; (let ((inhibit-read-only t))
  16499. ; (replace-match (concat "[" org-ans2 "]") t t)
  16500. ; (add-text-properties (point-min) (1+ (match-end 0))
  16501. ; (text-properties-at (1+ (point-min)))))))))
  16502. (defun org-calendar-select ()
  16503. "Return to `org-read-date' with the date currently selected.
  16504. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16505. (interactive)
  16506. (when (calendar-cursor-to-date)
  16507. (let* ((date (calendar-cursor-to-date))
  16508. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16509. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16510. (if (active-minibuffer-window) (exit-minibuffer))))
  16511. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16512. "Insert a date stamp for the date given by the internal TIME.
  16513. WITH-HM means, use the stamp format that includes the time of the day.
  16514. INACTIVE means use square brackets instead of angular ones, so that the
  16515. stamp will not contribute to the agenda.
  16516. PRE and POST are optional strings to be inserted before and after the
  16517. stamp.
  16518. The command returns the inserted time stamp."
  16519. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16520. stamp)
  16521. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16522. (insert-before-markers (or pre ""))
  16523. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16524. (when (listp extra)
  16525. (setq extra (car extra))
  16526. (if (and (stringp extra)
  16527. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16528. (setq extra (format "-%02d:%02d"
  16529. (string-to-number (match-string 1 extra))
  16530. (string-to-number (match-string 2 extra))))
  16531. (setq extra nil)))
  16532. (when extra
  16533. (backward-char 1)
  16534. (insert-before-markers extra)
  16535. (forward-char 1))
  16536. (insert-before-markers (or post ""))
  16537. stamp))
  16538. (defun org-toggle-time-stamp-overlays ()
  16539. "Toggle the use of custom time stamp formats."
  16540. (interactive)
  16541. (setq org-display-custom-times (not org-display-custom-times))
  16542. (unless org-display-custom-times
  16543. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16544. (while (setq p (next-single-property-change p 'display))
  16545. (if (and (get-text-property p 'display)
  16546. (eq (get-text-property p 'face) 'org-date))
  16547. (remove-text-properties
  16548. p (setq p (next-single-property-change p 'display))
  16549. '(display t))))
  16550. (set-buffer-modified-p bmp)))
  16551. (if (featurep 'xemacs)
  16552. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16553. (org-restart-font-lock)
  16554. (setq org-table-may-need-update t)
  16555. (if org-display-custom-times
  16556. (message "Time stamps are overlayed with custom format")
  16557. (message "Time stamp overlays removed")))
  16558. (defun org-display-custom-time (beg end)
  16559. "Overlay modified time stamp format over timestamp between BED and END."
  16560. (let* ((ts (buffer-substring beg end))
  16561. t1 w1 with-hm tf time str w2 (off 0))
  16562. (save-match-data
  16563. (setq t1 (org-parse-time-string ts t))
  16564. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16565. (setq off (- (match-end 0) (match-beginning 0)))))
  16566. (setq end (- end off))
  16567. (setq w1 (- end beg)
  16568. with-hm (and (nth 1 t1) (nth 2 t1))
  16569. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16570. time (org-fix-decoded-time t1)
  16571. str (org-add-props
  16572. (format-time-string
  16573. (substring tf 1 -1) (apply 'encode-time time))
  16574. nil 'mouse-face 'highlight)
  16575. w2 (length str))
  16576. (if (not (= w2 w1))
  16577. (add-text-properties (1+ beg) (+ 2 beg)
  16578. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16579. (if (featurep 'xemacs)
  16580. (progn
  16581. (put-text-property beg end 'invisible t)
  16582. (put-text-property beg end 'end-glyph (make-glyph str)))
  16583. (put-text-property beg end 'display str))))
  16584. (defun org-translate-time (string)
  16585. "Translate all timestamps in STRING to custom format.
  16586. But do this only if the variable `org-display-custom-times' is set."
  16587. (when org-display-custom-times
  16588. (save-match-data
  16589. (let* ((start 0)
  16590. (re org-ts-regexp-both)
  16591. t1 with-hm inactive tf time str beg end)
  16592. (while (setq start (string-match re string start))
  16593. (setq beg (match-beginning 0)
  16594. end (match-end 0)
  16595. t1 (save-match-data
  16596. (org-parse-time-string (substring string beg end) t))
  16597. with-hm (and (nth 1 t1) (nth 2 t1))
  16598. inactive (equal (substring string beg (1+ beg)) "[")
  16599. tf (funcall (if with-hm 'cdr 'car)
  16600. org-time-stamp-custom-formats)
  16601. time (org-fix-decoded-time t1)
  16602. str (format-time-string
  16603. (concat
  16604. (if inactive "[" "<") (substring tf 1 -1)
  16605. (if inactive "]" ">"))
  16606. (apply 'encode-time time))
  16607. string (replace-match str t t string)
  16608. start (+ start (length str)))))))
  16609. string)
  16610. (defun org-fix-decoded-time (time)
  16611. "Set 0 instead of nil for the first 6 elements of time.
  16612. Don't touch the rest."
  16613. (let ((n 0))
  16614. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16615. (defun org-days-to-time (timestamp-string)
  16616. "Difference between TIMESTAMP-STRING and now in days."
  16617. (- (time-to-days (org-time-string-to-time timestamp-string))
  16618. (time-to-days (current-time))))
  16619. (defun org-deadline-close (timestamp-string &optional ndays)
  16620. "Is the time in TIMESTAMP-STRING close to the current date?"
  16621. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16622. (and (< (org-days-to-time timestamp-string) ndays)
  16623. (not (org-entry-is-done-p))))
  16624. (defun org-get-wdays (ts)
  16625. "Get the deadline lead time appropriate for timestring TS."
  16626. (cond
  16627. ((<= org-deadline-warning-days 0)
  16628. ;; 0 or negative, enforce this value no matter what
  16629. (- org-deadline-warning-days))
  16630. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16631. ;; lead time is specified.
  16632. (floor (* (string-to-number (match-string 1 ts))
  16633. (cdr (assoc (match-string 2 ts)
  16634. '(("d" . 1) ("w" . 7)
  16635. ("m" . 30.4) ("y" . 365.25)))))))
  16636. ;; go for the default.
  16637. (t org-deadline-warning-days)))
  16638. (defun org-calendar-select-mouse (ev)
  16639. "Return to `org-read-date' with the date currently selected.
  16640. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16641. (interactive "e")
  16642. (mouse-set-point ev)
  16643. (when (calendar-cursor-to-date)
  16644. (let* ((date (calendar-cursor-to-date))
  16645. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16646. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16647. (if (active-minibuffer-window) (exit-minibuffer))))
  16648. (defun org-check-deadlines (ndays)
  16649. "Check if there are any deadlines due or past due.
  16650. A deadline is considered due if it happens within `org-deadline-warning-days'
  16651. days from today's date. If the deadline appears in an entry marked DONE,
  16652. it is not shown. The prefix arg NDAYS can be used to test that many
  16653. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16654. (interactive "P")
  16655. (let* ((org-warn-days
  16656. (cond
  16657. ((equal ndays '(4)) 100000)
  16658. (ndays (prefix-numeric-value ndays))
  16659. (t (abs org-deadline-warning-days))))
  16660. (case-fold-search nil)
  16661. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16662. (callback
  16663. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16664. (message "%d deadlines past-due or due within %d days"
  16665. (org-occur regexp nil callback)
  16666. org-warn-days)))
  16667. (defun org-check-before-date (date)
  16668. "Check if there are deadlines or scheduled entries before DATE."
  16669. (interactive (list (org-read-date)))
  16670. (let ((case-fold-search nil)
  16671. (regexp (concat "\\<\\(" org-deadline-string
  16672. "\\|" org-scheduled-string
  16673. "\\) *<\\([^>]+\\)>"))
  16674. (callback
  16675. (lambda () (time-less-p
  16676. (org-time-string-to-time (match-string 2))
  16677. (org-time-string-to-time date)))))
  16678. (message "%d entries before %s"
  16679. (org-occur regexp nil callback) date)))
  16680. (defun org-evaluate-time-range (&optional to-buffer)
  16681. "Evaluate a time range by computing the difference between start and end.
  16682. Normally the result is just printed in the echo area, but with prefix arg
  16683. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16684. If the time range is actually in a table, the result is inserted into the
  16685. next column.
  16686. For time difference computation, a year is assumed to be exactly 365
  16687. days in order to avoid rounding problems."
  16688. (interactive "P")
  16689. (or
  16690. (org-clock-update-time-maybe)
  16691. (save-excursion
  16692. (unless (org-at-date-range-p t)
  16693. (goto-char (point-at-bol))
  16694. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16695. (if (not (org-at-date-range-p t))
  16696. (error "Not at a time-stamp range, and none found in current line")))
  16697. (let* ((ts1 (match-string 1))
  16698. (ts2 (match-string 2))
  16699. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16700. (match-end (match-end 0))
  16701. (time1 (org-time-string-to-time ts1))
  16702. (time2 (org-time-string-to-time ts2))
  16703. (t1 (time-to-seconds time1))
  16704. (t2 (time-to-seconds time2))
  16705. (diff (abs (- t2 t1)))
  16706. (negative (< (- t2 t1) 0))
  16707. ;; (ys (floor (* 365 24 60 60)))
  16708. (ds (* 24 60 60))
  16709. (hs (* 60 60))
  16710. (fy "%dy %dd %02d:%02d")
  16711. (fy1 "%dy %dd")
  16712. (fd "%dd %02d:%02d")
  16713. (fd1 "%dd")
  16714. (fh "%02d:%02d")
  16715. y d h m align)
  16716. (if havetime
  16717. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16718. y 0
  16719. d (floor (/ diff ds)) diff (mod diff ds)
  16720. h (floor (/ diff hs)) diff (mod diff hs)
  16721. m (floor (/ diff 60)))
  16722. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16723. y 0
  16724. d (floor (+ (/ diff ds) 0.5))
  16725. h 0 m 0))
  16726. (if (not to-buffer)
  16727. (message "%s" (org-make-tdiff-string y d h m))
  16728. (if (org-at-table-p)
  16729. (progn
  16730. (goto-char match-end)
  16731. (setq align t)
  16732. (and (looking-at " *|") (goto-char (match-end 0))))
  16733. (goto-char match-end))
  16734. (if (looking-at
  16735. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16736. (replace-match ""))
  16737. (if negative (insert " -"))
  16738. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16739. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16740. (insert " " (format fh h m))))
  16741. (if align (org-table-align))
  16742. (message "Time difference inserted")))))
  16743. (defun org-make-tdiff-string (y d h m)
  16744. (let ((fmt "")
  16745. (l nil))
  16746. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16747. l (push y l)))
  16748. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16749. l (push d l)))
  16750. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16751. l (push h l)))
  16752. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16753. l (push m l)))
  16754. (apply 'format fmt (nreverse l))))
  16755. (defun org-time-string-to-time (s)
  16756. (apply 'encode-time (org-parse-time-string s)))
  16757. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16758. "Convert a time stamp to an absolute day number.
  16759. If there is a specifyer for a cyclic time stamp, get the closest date to
  16760. DAYNR."
  16761. (cond
  16762. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16763. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16764. daynr
  16765. (+ daynr 1000)))
  16766. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16767. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16768. (time-to-days (current-time))) (match-string 0 s)
  16769. prefer))
  16770. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16771. (defun org-time-from-absolute (d)
  16772. "Return the time corresponding to date D.
  16773. D may be an absolute day number, or a calendar-type list (month day year)."
  16774. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16775. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16776. (defun org-calendar-holiday ()
  16777. "List of holidays, for Diary display in Org-mode."
  16778. (require 'holidays)
  16779. (let ((hl (funcall
  16780. (if (fboundp 'calendar-check-holidays)
  16781. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16782. (if hl (mapconcat 'identity hl "; "))))
  16783. (defun org-diary-sexp-entry (sexp entry date)
  16784. "Process a SEXP diary ENTRY for DATE."
  16785. (require 'diary-lib)
  16786. (let ((result (if calendar-debug-sexp
  16787. (let ((stack-trace-on-error t))
  16788. (eval (car (read-from-string sexp))))
  16789. (condition-case nil
  16790. (eval (car (read-from-string sexp)))
  16791. (error
  16792. (beep)
  16793. (message "Bad sexp at line %d in %s: %s"
  16794. (org-current-line)
  16795. (buffer-file-name) sexp)
  16796. (sleep-for 2))))))
  16797. (cond ((stringp result) result)
  16798. ((and (consp result)
  16799. (stringp (cdr result))) (cdr result))
  16800. (result entry)
  16801. (t nil))))
  16802. (defun org-diary-to-ical-string (frombuf)
  16803. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16804. This uses the icalendar.el library."
  16805. (let* ((tmpdir (if (featurep 'xemacs)
  16806. (temp-directory)
  16807. temporary-file-directory))
  16808. (tmpfile (make-temp-name
  16809. (expand-file-name "orgics" tmpdir)))
  16810. buf rtn b e)
  16811. (save-excursion
  16812. (set-buffer frombuf)
  16813. (icalendar-export-region (point-min) (point-max) tmpfile)
  16814. (setq buf (find-buffer-visiting tmpfile))
  16815. (set-buffer buf)
  16816. (goto-char (point-min))
  16817. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16818. (setq b (match-beginning 0)))
  16819. (goto-char (point-max))
  16820. (if (re-search-backward "^END:VEVENT" nil t)
  16821. (setq e (match-end 0)))
  16822. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16823. (kill-buffer buf)
  16824. (kill-buffer frombuf)
  16825. (delete-file tmpfile)
  16826. rtn))
  16827. (defun org-closest-date (start current change prefer)
  16828. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16829. When PREFER is `past' return a date that is either CURRENT or past.
  16830. When PREFER is `future', return a date that is either CURRENT or future."
  16831. ;; Make the proper lists from the dates
  16832. (catch 'exit
  16833. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16834. dn dw sday cday n1 n2
  16835. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16836. (setq start (org-date-to-gregorian start)
  16837. current (org-date-to-gregorian
  16838. (if org-agenda-repeating-timestamp-show-all
  16839. current
  16840. (time-to-days (current-time))))
  16841. sday (calendar-absolute-from-gregorian start)
  16842. cday (calendar-absolute-from-gregorian current))
  16843. (if (<= cday sday) (throw 'exit sday))
  16844. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16845. (setq dn (string-to-number (match-string 1 change))
  16846. dw (cdr (assoc (match-string 2 change) a1)))
  16847. (error "Invalid change specifyer: %s" change))
  16848. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16849. (cond
  16850. ((eq dw 'day)
  16851. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16852. n2 (+ n1 dn)))
  16853. ((eq dw 'year)
  16854. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16855. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16856. (setq date1 (list m d y1)
  16857. n1 (calendar-absolute-from-gregorian date1)
  16858. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16859. n2 (calendar-absolute-from-gregorian date2)))
  16860. ((eq dw 'month)
  16861. ;; approx number of month between the tow dates
  16862. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16863. ;; How often does dn fit in there?
  16864. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16865. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16866. m (+ m nm)
  16867. ny (floor (/ m 12))
  16868. y (+ y ny)
  16869. m (- m (* ny 12)))
  16870. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16871. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16872. (setq m2 (+ m dn) y2 y)
  16873. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16874. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16875. (while (< n2 cday)
  16876. (setq n1 n2 m m2 y y2)
  16877. (setq m2 (+ m dn) y2 y)
  16878. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16879. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16880. (if org-agenda-repeating-timestamp-show-all
  16881. (cond
  16882. ((eq prefer 'past) n1)
  16883. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16884. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16885. (cond
  16886. ((eq prefer 'past) n1)
  16887. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16888. (t (if (= cday n1) n1 n2)))))))
  16889. (defun org-date-to-gregorian (date)
  16890. "Turn any specification of DATE into a gregorian date for the calendar."
  16891. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16892. ((and (listp date) (= (length date) 3)) date)
  16893. ((stringp date)
  16894. (setq date (org-parse-time-string date))
  16895. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16896. ((listp date)
  16897. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16898. (defun org-parse-time-string (s &optional nodefault)
  16899. "Parse the standard Org-mode time string.
  16900. This should be a lot faster than the normal `parse-time-string'.
  16901. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16902. hour and minute fields will be nil if not given."
  16903. (if (string-match org-ts-regexp0 s)
  16904. (list 0
  16905. (if (or (match-beginning 8) (not nodefault))
  16906. (string-to-number (or (match-string 8 s) "0")))
  16907. (if (or (match-beginning 7) (not nodefault))
  16908. (string-to-number (or (match-string 7 s) "0")))
  16909. (string-to-number (match-string 4 s))
  16910. (string-to-number (match-string 3 s))
  16911. (string-to-number (match-string 2 s))
  16912. nil nil nil)
  16913. (make-list 9 0)))
  16914. (defun org-timestamp-up (&optional arg)
  16915. "Increase the date item at the cursor by one.
  16916. If the cursor is on the year, change the year. If it is on the month or
  16917. the day, change that.
  16918. With prefix ARG, change by that many units."
  16919. (interactive "p")
  16920. (org-timestamp-change (prefix-numeric-value arg)))
  16921. (defun org-timestamp-down (&optional arg)
  16922. "Decrease the date item at the cursor by one.
  16923. If the cursor is on the year, change the year. If it is on the month or
  16924. the day, change that.
  16925. With prefix ARG, change by that many units."
  16926. (interactive "p")
  16927. (org-timestamp-change (- (prefix-numeric-value arg))))
  16928. (defun org-timestamp-up-day (&optional arg)
  16929. "Increase the date in the time stamp by one day.
  16930. With prefix ARG, change that many days."
  16931. (interactive "p")
  16932. (if (and (not (org-at-timestamp-p t))
  16933. (org-on-heading-p))
  16934. (org-todo 'up)
  16935. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16936. (defun org-timestamp-down-day (&optional arg)
  16937. "Decrease the date in the time stamp by one day.
  16938. With prefix ARG, change that many days."
  16939. (interactive "p")
  16940. (if (and (not (org-at-timestamp-p t))
  16941. (org-on-heading-p))
  16942. (org-todo 'down)
  16943. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16944. (defsubst org-pos-in-match-range (pos n)
  16945. (and (match-beginning n)
  16946. (<= (match-beginning n) pos)
  16947. (>= (match-end n) pos)))
  16948. (defun org-at-timestamp-p (&optional inactive-ok)
  16949. "Determine if the cursor is in or at a timestamp."
  16950. (interactive)
  16951. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16952. (pos (point))
  16953. (ans (or (looking-at tsr)
  16954. (save-excursion
  16955. (skip-chars-backward "^[<\n\r\t")
  16956. (if (> (point) (point-min)) (backward-char 1))
  16957. (and (looking-at tsr)
  16958. (> (- (match-end 0) pos) -1))))))
  16959. (and ans
  16960. (boundp 'org-ts-what)
  16961. (setq org-ts-what
  16962. (cond
  16963. ((= pos (match-beginning 0)) 'bracket)
  16964. ((= pos (1- (match-end 0))) 'bracket)
  16965. ((org-pos-in-match-range pos 2) 'year)
  16966. ((org-pos-in-match-range pos 3) 'month)
  16967. ((org-pos-in-match-range pos 7) 'hour)
  16968. ((org-pos-in-match-range pos 8) 'minute)
  16969. ((or (org-pos-in-match-range pos 4)
  16970. (org-pos-in-match-range pos 5)) 'day)
  16971. ((and (> pos (or (match-end 8) (match-end 5)))
  16972. (< pos (match-end 0)))
  16973. (- pos (or (match-end 8) (match-end 5))))
  16974. (t 'day))))
  16975. ans))
  16976. (defun org-toggle-timestamp-type ()
  16977. ""
  16978. (interactive)
  16979. (when (org-at-timestamp-p t)
  16980. (save-excursion
  16981. (goto-char (match-beginning 0))
  16982. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16983. (goto-char (1- (match-end 0)))
  16984. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16985. (message "Timestamp is now %sactive"
  16986. (if (equal (char-before) ?>) "in" ""))))
  16987. (defun org-timestamp-change (n &optional what)
  16988. "Change the date in the time stamp at point.
  16989. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16990. `year', `minute', `second'. If WHAT is not given, the cursor position
  16991. in the timestamp determines what will be changed."
  16992. (let ((pos (point))
  16993. with-hm inactive
  16994. org-ts-what
  16995. extra
  16996. ts time time0)
  16997. (if (not (org-at-timestamp-p t))
  16998. (error "Not at a timestamp"))
  16999. (if (and (not what) (eq org-ts-what 'bracket))
  17000. (org-toggle-timestamp-type)
  17001. (if (and (not what) (not (eq org-ts-what 'day))
  17002. org-display-custom-times
  17003. (get-text-property (point) 'display)
  17004. (not (get-text-property (1- (point)) 'display)))
  17005. (setq org-ts-what 'day))
  17006. (setq org-ts-what (or what org-ts-what)
  17007. inactive (= (char-after (match-beginning 0)) ?\[)
  17008. ts (match-string 0))
  17009. (replace-match "")
  17010. (if (string-match
  17011. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  17012. ts)
  17013. (setq extra (match-string 1 ts)))
  17014. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  17015. (setq with-hm t))
  17016. (setq time0 (org-parse-time-string ts))
  17017. (setq time
  17018. (encode-time (or (car time0) 0)
  17019. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  17020. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  17021. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  17022. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  17023. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  17024. (nthcdr 6 time0)))
  17025. (when (integerp org-ts-what)
  17026. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  17027. (if (eq what 'calendar)
  17028. (let ((cal-date (org-get-date-from-calendar)))
  17029. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  17030. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  17031. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  17032. (setcar time0 (or (car time0) 0))
  17033. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  17034. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  17035. (setq time (apply 'encode-time time0))))
  17036. (setq org-last-changed-timestamp
  17037. (org-insert-time-stamp time with-hm inactive nil nil extra))
  17038. (org-clock-update-time-maybe)
  17039. (goto-char pos)
  17040. ;; Try to recenter the calendar window, if any
  17041. (if (and org-calendar-follow-timestamp-change
  17042. (get-buffer-window "*Calendar*" t)
  17043. (memq org-ts-what '(day month year)))
  17044. (org-recenter-calendar (time-to-days time))))))
  17045. ;; FIXME: does not yet work for lead times
  17046. (defun org-modify-ts-extra (s pos n)
  17047. "Change the different parts of the lead-time and repeat fields in timestamp."
  17048. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  17049. ng h m new)
  17050. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  17051. (cond
  17052. ((or (org-pos-in-match-range pos 2)
  17053. (org-pos-in-match-range pos 3))
  17054. (setq m (string-to-number (match-string 3 s))
  17055. h (string-to-number (match-string 2 s)))
  17056. (if (org-pos-in-match-range pos 2)
  17057. (setq h (+ h n))
  17058. (setq m (+ m n)))
  17059. (if (< m 0) (setq m (+ m 60) h (1- h)))
  17060. (if (> m 59) (setq m (- m 60) h (1+ h)))
  17061. (setq h (min 24 (max 0 h)))
  17062. (setq ng 1 new (format "-%02d:%02d" h m)))
  17063. ((org-pos-in-match-range pos 6)
  17064. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  17065. ((org-pos-in-match-range pos 5)
  17066. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  17067. (when ng
  17068. (setq s (concat
  17069. (substring s 0 (match-beginning ng))
  17070. new
  17071. (substring s (match-end ng))))))
  17072. s))
  17073. (defun org-recenter-calendar (date)
  17074. "If the calendar is visible, recenter it to DATE."
  17075. (let* ((win (selected-window))
  17076. (cwin (get-buffer-window "*Calendar*" t))
  17077. (calendar-move-hook nil))
  17078. (when cwin
  17079. (select-window cwin)
  17080. (calendar-goto-date (if (listp date) date
  17081. (calendar-gregorian-from-absolute date)))
  17082. (select-window win))))
  17083. (defun org-goto-calendar (&optional arg)
  17084. "Go to the Emacs calendar at the current date.
  17085. If there is a time stamp in the current line, go to that date.
  17086. A prefix ARG can be used to force the current date."
  17087. (interactive "P")
  17088. (let ((tsr org-ts-regexp) diff
  17089. (calendar-move-hook nil)
  17090. (view-calendar-holidays-initially nil)
  17091. (view-diary-entries-initially nil))
  17092. (if (or (org-at-timestamp-p)
  17093. (save-excursion
  17094. (beginning-of-line 1)
  17095. (looking-at (concat ".*" tsr))))
  17096. (let ((d1 (time-to-days (current-time)))
  17097. (d2 (time-to-days
  17098. (org-time-string-to-time (match-string 1)))))
  17099. (setq diff (- d2 d1))))
  17100. (calendar)
  17101. (calendar-goto-today)
  17102. (if (and diff (not arg)) (calendar-forward-day diff))))
  17103. (defun org-get-date-from-calendar ()
  17104. "Return a list (month day year) of date at point in calendar."
  17105. (with-current-buffer "*Calendar*"
  17106. (save-match-data
  17107. (calendar-cursor-to-date))))
  17108. (defun org-date-from-calendar ()
  17109. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  17110. If there is already a time stamp at the cursor position, update it."
  17111. (interactive)
  17112. (if (org-at-timestamp-p t)
  17113. (org-timestamp-change 0 'calendar)
  17114. (let ((cal-date (org-get-date-from-calendar)))
  17115. (org-insert-time-stamp
  17116. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  17117. (defvar appt-time-msg-list)
  17118. ;;;###autoload
  17119. (defun org-agenda-to-appt (&optional refresh filter)
  17120. "Activate appointments found in `org-agenda-files'.
  17121. With a \\[universal-argument] prefix, refresh the list of
  17122. appointements.
  17123. If FILTER is t, interactively prompt the user for a regular
  17124. expression, and filter out entries that don't match it.
  17125. If FILTER is a string, use this string as a regular expression
  17126. for filtering entries out.
  17127. FILTER can also be an alist with the car of each cell being
  17128. either 'headline or 'category. For example:
  17129. '((headline \"IMPORTANT\")
  17130. (category \"Work\"))
  17131. will only add headlines containing IMPORTANT or headlines
  17132. belonging to the \"Work\" category."
  17133. (interactive "P")
  17134. (require 'calendar)
  17135. (if refresh (setq appt-time-msg-list nil))
  17136. (if (eq filter t)
  17137. (setq filter (read-from-minibuffer "Regexp filter: ")))
  17138. (let* ((cnt 0) ; count added events
  17139. (org-agenda-new-buffers nil)
  17140. (org-deadline-warning-days 0)
  17141. (today (org-date-to-gregorian
  17142. (time-to-days (current-time))))
  17143. (files (org-agenda-files)) entries file)
  17144. ;; Get all entries which may contain an appt
  17145. (while (setq file (pop files))
  17146. (setq entries
  17147. (append entries
  17148. (org-agenda-get-day-entries
  17149. file today :timestamp :scheduled :deadline))))
  17150. (setq entries (delq nil entries))
  17151. ;; Map thru entries and find if we should filter them out
  17152. (mapc
  17153. (lambda(x)
  17154. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  17155. (cat (get-text-property 1 'org-category x))
  17156. (tod (get-text-property 1 'time-of-day x))
  17157. (ok (or (null filter)
  17158. (and (stringp filter) (string-match filter evt))
  17159. (and (listp filter)
  17160. (or (string-match
  17161. (cadr (assoc 'category filter)) cat)
  17162. (string-match
  17163. (cadr (assoc 'headline filter)) evt))))))
  17164. ;; FIXME: Shall we remove text-properties for the appt text?
  17165. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  17166. (when (and ok tod)
  17167. (setq tod (number-to-string tod)
  17168. tod (when (string-match
  17169. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  17170. (concat (match-string 1 tod) ":"
  17171. (match-string 2 tod))))
  17172. (appt-add tod evt)
  17173. (setq cnt (1+ cnt))))) entries)
  17174. (org-release-buffers org-agenda-new-buffers)
  17175. (if (eq cnt 0)
  17176. (message "No event to add")
  17177. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
  17178. ;;; The clock for measuring work time.
  17179. (defvar org-mode-line-string "")
  17180. (put 'org-mode-line-string 'risky-local-variable t)
  17181. (defvar org-mode-line-timer nil)
  17182. (defvar org-clock-heading "")
  17183. (defvar org-clock-start-time "")
  17184. (defun org-update-mode-line ()
  17185. (let* ((delta (- (time-to-seconds (current-time))
  17186. (time-to-seconds org-clock-start-time)))
  17187. (h (floor delta 3600))
  17188. (m (floor (- delta (* 3600 h)) 60)))
  17189. (setq org-mode-line-string
  17190. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  17191. 'help-echo "Org-mode clock is running"))
  17192. (force-mode-line-update)))
  17193. (defvar org-clock-marker (make-marker)
  17194. "Marker recording the last clock-in.")
  17195. (defvar org-clock-mode-line-entry nil
  17196. "Information for the modeline about the running clock.")
  17197. (defun org-clock-in ()
  17198. "Start the clock on the current item.
  17199. If necessary, clock-out of the currently active clock."
  17200. (interactive)
  17201. (org-clock-out t)
  17202. (let (ts)
  17203. (save-excursion
  17204. (org-back-to-heading t)
  17205. (when (and org-clock-in-switch-to-state
  17206. (not (looking-at (concat outline-regexp "[ \t]*"
  17207. org-clock-in-switch-to-state
  17208. "\\>"))))
  17209. (org-todo org-clock-in-switch-to-state))
  17210. (if (and org-clock-heading-function
  17211. (functionp org-clock-heading-function))
  17212. (setq org-clock-heading (funcall org-clock-heading-function))
  17213. (if (looking-at org-complex-heading-regexp)
  17214. (setq org-clock-heading (match-string 4))
  17215. (setq org-clock-heading "???")))
  17216. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  17217. (org-clock-find-position)
  17218. (insert "\n") (backward-char 1)
  17219. (indent-relative)
  17220. (insert org-clock-string " ")
  17221. (setq org-clock-start-time (current-time))
  17222. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17223. (move-marker org-clock-marker (point) (buffer-base-buffer))
  17224. (or global-mode-string (setq global-mode-string '("")))
  17225. (or (memq 'org-mode-line-string global-mode-string)
  17226. (setq global-mode-string
  17227. (append global-mode-string '(org-mode-line-string))))
  17228. (org-update-mode-line)
  17229. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  17230. (message "Clock started at %s" ts))))
  17231. (defun org-clock-find-position ()
  17232. "Find the location where the next clock line should be inserted."
  17233. (org-back-to-heading t)
  17234. (catch 'exit
  17235. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  17236. (re (concat "^[ \t]*" org-clock-string))
  17237. (cnt 0)
  17238. first last)
  17239. (goto-char beg)
  17240. (when (eobp) (newline) (setq end (max (point) end)))
  17241. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  17242. ;; we seem to have a CLOCK drawer, so go there.
  17243. (beginning-of-line 2)
  17244. (throw 'exit t))
  17245. ;; Lets count the CLOCK lines
  17246. (goto-char beg)
  17247. (while (re-search-forward re end t)
  17248. (setq first (or first (match-beginning 0))
  17249. last (match-beginning 0)
  17250. cnt (1+ cnt)))
  17251. (when (and (integerp org-clock-into-drawer)
  17252. (>= (1+ cnt) org-clock-into-drawer))
  17253. ;; Wrap current entries into a new drawer
  17254. (goto-char last)
  17255. (beginning-of-line 2)
  17256. (if (org-at-item-p) (org-end-of-item))
  17257. (insert ":END:\n")
  17258. (beginning-of-line 0)
  17259. (org-indent-line-function)
  17260. (goto-char first)
  17261. (insert ":CLOCK:\n")
  17262. (beginning-of-line 0)
  17263. (org-indent-line-function)
  17264. (org-flag-drawer t)
  17265. (beginning-of-line 2)
  17266. (throw 'exit nil))
  17267. (goto-char beg)
  17268. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17269. (not (equal (match-string 1) org-clock-string)))
  17270. ;; Planning info, skip to after it
  17271. (beginning-of-line 2)
  17272. (or (bolp) (newline)))
  17273. (when (eq t org-clock-into-drawer)
  17274. (insert ":CLOCK:\n:END:\n")
  17275. (beginning-of-line -1)
  17276. (org-indent-line-function)
  17277. (org-flag-drawer t)
  17278. (beginning-of-line 2)
  17279. (org-indent-line-function)))))
  17280. (defun org-clock-out (&optional fail-quietly)
  17281. "Stop the currently running clock.
  17282. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17283. (interactive)
  17284. (catch 'exit
  17285. (if (not (marker-buffer org-clock-marker))
  17286. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17287. (let (ts te s h m)
  17288. (save-excursion
  17289. (set-buffer (marker-buffer org-clock-marker))
  17290. (goto-char org-clock-marker)
  17291. (beginning-of-line 1)
  17292. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17293. (equal (match-string 1) org-clock-string))
  17294. (setq ts (match-string 2))
  17295. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17296. (goto-char (match-end 0))
  17297. (delete-region (point) (point-at-eol))
  17298. (insert "--")
  17299. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17300. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17301. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17302. h (floor (/ s 3600))
  17303. s (- s (* 3600 h))
  17304. m (floor (/ s 60))
  17305. s (- s (* 60 s)))
  17306. (insert " => " (format "%2d:%02d" h m))
  17307. (move-marker org-clock-marker nil)
  17308. (when org-log-note-clock-out
  17309. (org-add-log-maybe 'clock-out))
  17310. (when org-mode-line-timer
  17311. (cancel-timer org-mode-line-timer)
  17312. (setq org-mode-line-timer nil))
  17313. (setq global-mode-string
  17314. (delq 'org-mode-line-string global-mode-string))
  17315. (force-mode-line-update)
  17316. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17317. (defun org-clock-cancel ()
  17318. "Cancel the running clock be removing the start timestamp."
  17319. (interactive)
  17320. (if (not (marker-buffer org-clock-marker))
  17321. (error "No active clock"))
  17322. (save-excursion
  17323. (set-buffer (marker-buffer org-clock-marker))
  17324. (goto-char org-clock-marker)
  17325. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17326. (setq global-mode-string
  17327. (delq 'org-mode-line-string global-mode-string))
  17328. (force-mode-line-update)
  17329. (message "Clock canceled"))
  17330. (defun org-clock-goto (&optional delete-windows)
  17331. "Go to the currently clocked-in entry."
  17332. (interactive "P")
  17333. (if (not (marker-buffer org-clock-marker))
  17334. (error "No active clock"))
  17335. (switch-to-buffer-other-window
  17336. (marker-buffer org-clock-marker))
  17337. (if delete-windows (delete-other-windows))
  17338. (goto-char org-clock-marker)
  17339. (org-show-entry)
  17340. (org-back-to-heading)
  17341. (recenter))
  17342. (defvar org-clock-file-total-minutes nil
  17343. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17344. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17345. (defun org-clock-sum (&optional tstart tend)
  17346. "Sum the times for each subtree.
  17347. Puts the resulting times in minutes as a text property on each headline."
  17348. (interactive)
  17349. (let* ((bmp (buffer-modified-p))
  17350. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17351. org-clock-string
  17352. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17353. (lmax 30)
  17354. (ltimes (make-vector lmax 0))
  17355. (t1 0)
  17356. (level 0)
  17357. ts te dt
  17358. time)
  17359. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17360. (save-excursion
  17361. (goto-char (point-max))
  17362. (while (re-search-backward re nil t)
  17363. (cond
  17364. ((match-end 2)
  17365. ;; Two time stamps
  17366. (setq ts (match-string 2)
  17367. te (match-string 3)
  17368. ts (time-to-seconds
  17369. (apply 'encode-time (org-parse-time-string ts)))
  17370. te (time-to-seconds
  17371. (apply 'encode-time (org-parse-time-string te)))
  17372. ts (if tstart (max ts tstart) ts)
  17373. te (if tend (min te tend) te)
  17374. dt (- te ts)
  17375. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17376. ((match-end 4)
  17377. ;; A naket time
  17378. (setq t1 (+ t1 (string-to-number (match-string 5))
  17379. (* 60 (string-to-number (match-string 4))))))
  17380. (t ;; A headline
  17381. (setq level (- (match-end 1) (match-beginning 1)))
  17382. (when (or (> t1 0) (> (aref ltimes level) 0))
  17383. (loop for l from 0 to level do
  17384. (aset ltimes l (+ (aref ltimes l) t1)))
  17385. (setq t1 0 time (aref ltimes level))
  17386. (loop for l from level to (1- lmax) do
  17387. (aset ltimes l 0))
  17388. (goto-char (match-beginning 0))
  17389. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17390. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17391. (set-buffer-modified-p bmp)))
  17392. (defun org-clock-display (&optional total-only)
  17393. "Show subtree times in the entire buffer.
  17394. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17395. in the echo area."
  17396. (interactive)
  17397. (org-remove-clock-overlays)
  17398. (let (time h m p)
  17399. (org-clock-sum)
  17400. (unless total-only
  17401. (save-excursion
  17402. (goto-char (point-min))
  17403. (while (or (and (equal (setq p (point)) (point-min))
  17404. (get-text-property p :org-clock-minutes))
  17405. (setq p (next-single-property-change
  17406. (point) :org-clock-minutes)))
  17407. (goto-char p)
  17408. (when (setq time (get-text-property p :org-clock-minutes))
  17409. (org-put-clock-overlay time (funcall outline-level))))
  17410. (setq h (/ org-clock-file-total-minutes 60)
  17411. m (- org-clock-file-total-minutes (* 60 h)))
  17412. ;; Arrange to remove the overlays upon next change.
  17413. (when org-remove-highlights-with-change
  17414. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17415. nil 'local))))
  17416. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17417. (defvar org-clock-overlays nil)
  17418. (make-variable-buffer-local 'org-clock-overlays)
  17419. (defun org-put-clock-overlay (time &optional level)
  17420. "Put an overlays on the current line, displaying TIME.
  17421. If LEVEL is given, prefix time with a corresponding number of stars.
  17422. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17423. will be easy to remove."
  17424. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17425. (l (if level (org-get-legal-level level 0) 0))
  17426. (off 0)
  17427. ov tx)
  17428. (move-to-column c)
  17429. (unless (eolp) (skip-chars-backward "^ \t"))
  17430. (skip-chars-backward " \t")
  17431. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17432. tx (concat (buffer-substring (1- (point)) (point))
  17433. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17434. (org-add-props (format "%s %2d:%02d%s"
  17435. (make-string l ?*) h m
  17436. (make-string (- 16 l) ?\ ))
  17437. '(face secondary-selection))
  17438. ""))
  17439. (if (not (featurep 'xemacs))
  17440. (org-overlay-put ov 'display tx)
  17441. (org-overlay-put ov 'invisible t)
  17442. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17443. (push ov org-clock-overlays)))
  17444. (defun org-remove-clock-overlays (&optional beg end noremove)
  17445. "Remove the occur highlights from the buffer.
  17446. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17447. from the `before-change-functions' in the current buffer."
  17448. (interactive)
  17449. (unless org-inhibit-highlight-removal
  17450. (mapc 'org-delete-overlay org-clock-overlays)
  17451. (setq org-clock-overlays nil)
  17452. (unless noremove
  17453. (remove-hook 'before-change-functions
  17454. 'org-remove-clock-overlays 'local))))
  17455. (defun org-clock-out-if-current ()
  17456. "Clock out if the current entry contains the running clock.
  17457. This is used to stop the clock after a TODO entry is marked DONE,
  17458. and is only done if the variable `org-clock-out-when-done' is not nil."
  17459. (when (and org-clock-out-when-done
  17460. (member state org-done-keywords)
  17461. (equal (marker-buffer org-clock-marker) (current-buffer))
  17462. (< (point) org-clock-marker)
  17463. (> (save-excursion (outline-next-heading) (point))
  17464. org-clock-marker))
  17465. ;; Clock out, but don't accept a logging message for this.
  17466. (let ((org-log-note-clock-out nil))
  17467. (org-clock-out))))
  17468. (add-hook 'org-after-todo-state-change-hook
  17469. 'org-clock-out-if-current)
  17470. (defun org-check-running-clock ()
  17471. "Check if the current buffer contains the running clock.
  17472. If yes, offer to stop it and to save the buffer with the changes."
  17473. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17474. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17475. (buffer-name))))
  17476. (org-clock-out)
  17477. (when (y-or-n-p "Save changed buffer?")
  17478. (save-buffer))))
  17479. (defun org-clock-report (&optional arg)
  17480. "Create a table containing a report about clocked time.
  17481. If the cursor is inside an existing clocktable block, then the table
  17482. will be updated. If not, a new clocktable will be inserted.
  17483. When called with a prefix argument, move to the first clock table in the
  17484. buffer and update it."
  17485. (interactive "P")
  17486. (org-remove-clock-overlays)
  17487. (when arg
  17488. (org-find-dblock "clocktable")
  17489. (org-show-entry))
  17490. (if (org-in-clocktable-p)
  17491. (goto-char (org-in-clocktable-p))
  17492. (org-create-dblock (list :name "clocktable"
  17493. :maxlevel 2 :scope 'file)))
  17494. (org-update-dblock))
  17495. (defun org-in-clocktable-p ()
  17496. "Check if the cursor is in a clocktable."
  17497. (let ((pos (point)) start)
  17498. (save-excursion
  17499. (end-of-line 1)
  17500. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17501. (setq start (match-beginning 0))
  17502. (re-search-forward "^#\\+END:.*" nil t)
  17503. (>= (match-end 0) pos)
  17504. start))))
  17505. (defun org-clock-update-time-maybe ()
  17506. "If this is a CLOCK line, update it and return t.
  17507. Otherwise, return nil."
  17508. (interactive)
  17509. (save-excursion
  17510. (beginning-of-line 1)
  17511. (skip-chars-forward " \t")
  17512. (when (looking-at org-clock-string)
  17513. (let ((re (concat "[ \t]*" org-clock-string
  17514. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17515. "\\([ \t]*=>.*\\)?"))
  17516. ts te h m s)
  17517. (if (not (looking-at re))
  17518. nil
  17519. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17520. (end-of-line 1)
  17521. (setq ts (match-string 1)
  17522. te (match-string 2))
  17523. (setq s (- (time-to-seconds
  17524. (apply 'encode-time (org-parse-time-string te)))
  17525. (time-to-seconds
  17526. (apply 'encode-time (org-parse-time-string ts))))
  17527. h (floor (/ s 3600))
  17528. s (- s (* 3600 h))
  17529. m (floor (/ s 60))
  17530. s (- s (* 60 s)))
  17531. (insert " => " (format "%2d:%02d" h m))
  17532. t)))))
  17533. (defun org-clock-special-range (key &optional time as-strings)
  17534. "Return two times bordering a special time range.
  17535. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17536. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17537. A week starts Monday 0:00 and ends Sunday 24:00.
  17538. The range is determined relative to TIME. TIME defaults to the current time.
  17539. The return value is a cons cell with two internal times like the ones
  17540. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17541. the returned times will be formatted strings."
  17542. (let* ((tm (decode-time (or time (current-time))))
  17543. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17544. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17545. (dow (nth 6 tm))
  17546. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17547. (cond
  17548. ((eq key 'today)
  17549. (setq h 0 m 0 h1 24 m1 0))
  17550. ((eq key 'yesterday)
  17551. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17552. ((eq key 'thisweek)
  17553. (setq diff (if (= dow 0) 6 (1- dow))
  17554. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17555. ((eq key 'lastweek)
  17556. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17557. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17558. ((eq key 'thismonth)
  17559. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17560. ((eq key 'lastmonth)
  17561. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17562. ((eq key 'thisyear)
  17563. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17564. ((eq key 'lastyear)
  17565. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17566. (t (error "No such time block %s" key)))
  17567. (setq ts (encode-time s m h d month y)
  17568. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17569. (or d1 d) (or month1 month) (or y1 y)))
  17570. (setq fm (cdr org-time-stamp-formats))
  17571. (if as-strings
  17572. (cons (format-time-string fm ts) (format-time-string fm te))
  17573. (cons ts te))))
  17574. (defun org-dblock-write:clocktable (params)
  17575. "Write the standard clocktable."
  17576. (catch 'exit
  17577. (let* ((hlchars '((1 . "*") (2 . "/")))
  17578. (ins (make-marker))
  17579. (total-time nil)
  17580. (scope (plist-get params :scope))
  17581. (tostring (plist-get params :tostring))
  17582. (multifile (plist-get params :multifile))
  17583. (header (plist-get params :header))
  17584. (maxlevel (or (plist-get params :maxlevel) 3))
  17585. (step (plist-get params :step))
  17586. (emph (plist-get params :emphasize))
  17587. (ts (plist-get params :tstart))
  17588. (te (plist-get params :tend))
  17589. (block (plist-get params :block))
  17590. ipos time h m p level hlc hdl
  17591. cc beg end pos tbl)
  17592. (when step
  17593. (org-clocktable-steps params)
  17594. (throw 'exit nil))
  17595. (when block
  17596. (setq cc (org-clock-special-range block nil t)
  17597. ts (car cc) te (cdr cc)))
  17598. (if ts (setq ts (time-to-seconds
  17599. (apply 'encode-time (org-parse-time-string ts)))))
  17600. (if te (setq te (time-to-seconds
  17601. (apply 'encode-time (org-parse-time-string te)))))
  17602. (move-marker ins (point))
  17603. (setq ipos (point))
  17604. ;; Get the right scope
  17605. (setq pos (point))
  17606. (save-restriction
  17607. (cond
  17608. ((not scope))
  17609. ((eq scope 'file) (widen))
  17610. ((eq scope 'subtree) (org-narrow-to-subtree))
  17611. ((eq scope 'tree)
  17612. (while (org-up-heading-safe))
  17613. (org-narrow-to-subtree))
  17614. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17615. (symbol-name scope)))
  17616. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17617. (catch 'exit
  17618. (while (org-up-heading-safe)
  17619. (looking-at outline-regexp)
  17620. (if (<= (org-reduced-level (funcall outline-level)) level)
  17621. (throw 'exit nil))))
  17622. (org-narrow-to-subtree))
  17623. ((or (listp scope) (eq scope 'agenda))
  17624. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17625. (scope 'agenda)
  17626. (p1 (copy-sequence params))
  17627. file)
  17628. (plist-put p1 :tostring t)
  17629. (plist-put p1 :multifile t)
  17630. (plist-put p1 :scope 'file)
  17631. (org-prepare-agenda-buffers files)
  17632. (while (setq file (pop files))
  17633. (with-current-buffer (find-buffer-visiting file)
  17634. (push (org-clocktable-add-file
  17635. file (org-dblock-write:clocktable p1)) tbl)
  17636. (setq total-time (+ (or total-time 0)
  17637. org-clock-file-total-minutes)))))))
  17638. (goto-char pos)
  17639. (unless (eq scope 'agenda)
  17640. (org-clock-sum ts te)
  17641. (goto-char (point-min))
  17642. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17643. (goto-char p)
  17644. (when (setq time (get-text-property p :org-clock-minutes))
  17645. (save-excursion
  17646. (beginning-of-line 1)
  17647. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17648. (setq level (org-reduced-level
  17649. (- (match-end 1) (match-beginning 1))))
  17650. (<= level maxlevel))
  17651. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17652. hdl (match-string 2)
  17653. h (/ time 60)
  17654. m (- time (* 60 h)))
  17655. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17656. (push (concat
  17657. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17658. (make-string (1- level) ?|)
  17659. hlc (format "%d:%02d" h m) hlc
  17660. " |") tbl))))))
  17661. (setq tbl (nreverse tbl))
  17662. (if tostring
  17663. (if tbl (mapconcat 'identity tbl "\n") nil)
  17664. (goto-char ins)
  17665. (insert-before-markers
  17666. (or header
  17667. (concat
  17668. "Clock summary at ["
  17669. (substring
  17670. (format-time-string (cdr org-time-stamp-formats))
  17671. 1 -1)
  17672. "]."
  17673. (if block
  17674. (format " Considered range is /%s/." block)
  17675. "")
  17676. "\n\n"))
  17677. (if (eq scope 'agenda) "|File" "")
  17678. "|L|Headline|Time|\n")
  17679. (setq total-time (or total-time org-clock-file-total-minutes)
  17680. h (/ total-time 60)
  17681. m (- total-time (* 60 h)))
  17682. (insert-before-markers
  17683. "|-\n|"
  17684. (if (eq scope 'agenda) "|" "")
  17685. "|"
  17686. "*Total time*| "
  17687. (format "*%d:%02d*" h m)
  17688. "|\n|-\n")
  17689. (setq tbl (delq nil tbl))
  17690. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17691. (equal (substring (car tbl) 0 2) "|-"))
  17692. (pop tbl))
  17693. (insert-before-markers (mapconcat
  17694. 'identity (delq nil tbl)
  17695. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17696. (backward-delete-char 1)
  17697. (goto-char ipos)
  17698. (skip-chars-forward "^|")
  17699. (org-table-align))))))
  17700. (defun org-clocktable-steps (params)
  17701. (let* ((p1 (copy-sequence params))
  17702. (ts (plist-get p1 :tstart))
  17703. (te (plist-get p1 :tend))
  17704. (step0 (plist-get p1 :step))
  17705. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17706. (block (plist-get p1 :block))
  17707. cc)
  17708. (when block
  17709. (setq cc (org-clock-special-range block nil t)
  17710. ts (car cc) te (cdr cc)))
  17711. (if ts (setq ts (time-to-seconds
  17712. (apply 'encode-time (org-parse-time-string ts)))))
  17713. (if te (setq te (time-to-seconds
  17714. (apply 'encode-time (org-parse-time-string te)))))
  17715. (plist-put p1 :header "")
  17716. (plist-put p1 :step nil)
  17717. (plist-put p1 :block nil)
  17718. (while (< ts te)
  17719. (or (bolp) (insert "\n"))
  17720. (plist-put p1 :tstart (format-time-string
  17721. (car org-time-stamp-formats)
  17722. (seconds-to-time ts)))
  17723. (plist-put p1 :tend (format-time-string
  17724. (car org-time-stamp-formats)
  17725. (seconds-to-time (setq ts (+ ts step)))))
  17726. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17727. (plist-get p1 :tstart) "\n")
  17728. (org-dblock-write:clocktable p1)
  17729. (re-search-forward "#\\+END:")
  17730. (end-of-line 0))))
  17731. (defun org-clocktable-add-file (file table)
  17732. (if table
  17733. (let ((lines (org-split-string table "\n"))
  17734. (ff (file-name-nondirectory file)))
  17735. (mapconcat 'identity
  17736. (mapcar (lambda (x)
  17737. (if (string-match org-table-dataline-regexp x)
  17738. (concat "|" ff x)
  17739. x))
  17740. lines)
  17741. "\n"))))
  17742. ;; FIXME: I don't think anybody uses this, ask David
  17743. (defun org-collect-clock-time-entries ()
  17744. "Return an internal list with clocking information.
  17745. This list has one entry for each CLOCK interval.
  17746. FIXME: describe the elements."
  17747. (interactive)
  17748. (let ((re (concat "^[ \t]*" org-clock-string
  17749. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17750. rtn beg end next cont level title total closedp leafp
  17751. clockpos titlepos h m donep)
  17752. (save-excursion
  17753. (org-clock-sum)
  17754. (goto-char (point-min))
  17755. (while (re-search-forward re nil t)
  17756. (setq clockpos (match-beginning 0)
  17757. beg (match-string 1) end (match-string 2)
  17758. cont (match-end 0))
  17759. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17760. end (apply 'encode-time (org-parse-time-string end)))
  17761. (org-back-to-heading t)
  17762. (setq donep (org-entry-is-done-p))
  17763. (setq titlepos (point)
  17764. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17765. h (/ total 60) m (- total (* 60 h))
  17766. total (cons h m))
  17767. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17768. (setq level (- (match-end 1) (match-beginning 1))
  17769. title (org-match-string-no-properties 2))
  17770. (save-excursion (outline-next-heading) (setq next (point)))
  17771. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17772. (goto-char next)
  17773. (setq leafp (and (looking-at "^\\*+ ")
  17774. (<= (- (match-end 0) (point)) level)))
  17775. (push (list beg end clockpos closedp donep
  17776. total title titlepos level leafp)
  17777. rtn)
  17778. (goto-char cont)))
  17779. (nreverse rtn)))
  17780. ;;;; Agenda, and Diary Integration
  17781. ;;; Define the Org-agenda-mode
  17782. (defvar org-agenda-mode-map (make-sparse-keymap)
  17783. "Keymap for `org-agenda-mode'.")
  17784. (defvar org-agenda-menu) ; defined later in this file.
  17785. (defvar org-agenda-follow-mode nil)
  17786. (defvar org-agenda-show-log nil)
  17787. (defvar org-agenda-redo-command nil)
  17788. (defvar org-agenda-query-string nil)
  17789. (defvar org-agenda-mode-hook nil)
  17790. (defvar org-agenda-type nil)
  17791. (defvar org-agenda-force-single-file nil)
  17792. (defun org-agenda-mode ()
  17793. "Mode for time-sorted view on action items in Org-mode files.
  17794. The following commands are available:
  17795. \\{org-agenda-mode-map}"
  17796. (interactive)
  17797. (kill-all-local-variables)
  17798. (setq org-agenda-undo-list nil
  17799. org-agenda-pending-undo-list nil)
  17800. (setq major-mode 'org-agenda-mode)
  17801. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17802. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17803. (setq mode-name "Org-Agenda")
  17804. (use-local-map org-agenda-mode-map)
  17805. (easy-menu-add org-agenda-menu)
  17806. (if org-startup-truncated (setq truncate-lines t))
  17807. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17808. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17809. ;; Make sure properties are removed when copying text
  17810. (when (boundp 'buffer-substring-filters)
  17811. (org-set-local 'buffer-substring-filters
  17812. (cons (lambda (x)
  17813. (set-text-properties 0 (length x) nil x) x)
  17814. buffer-substring-filters)))
  17815. (unless org-agenda-keep-modes
  17816. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17817. org-agenda-show-log nil))
  17818. (easy-menu-change
  17819. '("Agenda") "Agenda Files"
  17820. (append
  17821. (list
  17822. (vector
  17823. (if (get 'org-agenda-files 'org-restrict)
  17824. "Restricted to single file"
  17825. "Edit File List")
  17826. '(org-edit-agenda-file-list)
  17827. (not (get 'org-agenda-files 'org-restrict)))
  17828. "--")
  17829. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17830. (org-agenda-set-mode-name)
  17831. (apply
  17832. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17833. (list 'org-agenda-mode-hook)))
  17834. (substitute-key-definition 'undo 'org-agenda-undo
  17835. org-agenda-mode-map global-map)
  17836. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17837. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17838. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17839. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17840. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17841. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17842. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17843. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17844. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17845. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17846. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17847. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17848. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17849. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17850. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17851. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17852. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17853. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17854. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17855. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17856. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17857. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17858. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17859. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17860. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17861. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17862. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17863. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17864. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17865. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17866. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17867. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17868. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17869. (while l (org-defkey org-agenda-mode-map
  17870. (int-to-string (pop l)) 'digit-argument)))
  17871. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17872. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17873. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17874. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17875. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17876. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17877. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17878. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17879. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17880. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17881. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17882. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17883. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17884. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17885. (org-defkey org-agenda-mode-map "n" 'next-line)
  17886. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17887. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17888. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17889. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17890. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17891. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17892. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17893. (eval-after-load "calendar"
  17894. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17895. 'org-calendar-goto-agenda))
  17896. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17897. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17898. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17899. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17900. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17901. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17902. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17903. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17904. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17905. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17906. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17907. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17908. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17909. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17910. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17911. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17912. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17913. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17914. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17915. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17916. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17917. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17918. (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
  17919. (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
  17920. (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
  17921. (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
  17922. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17923. "Local keymap for agenda entries from Org-mode.")
  17924. (org-defkey org-agenda-keymap
  17925. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17926. (org-defkey org-agenda-keymap
  17927. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17928. (when org-agenda-mouse-1-follows-link
  17929. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17930. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17931. '("Agenda"
  17932. ("Agenda Files")
  17933. "--"
  17934. ["Show" org-agenda-show t]
  17935. ["Go To (other window)" org-agenda-goto t]
  17936. ["Go To (this window)" org-agenda-switch-to t]
  17937. ["Follow Mode" org-agenda-follow-mode
  17938. :style toggle :selected org-agenda-follow-mode :active t]
  17939. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17940. "--"
  17941. ["Cycle TODO" org-agenda-todo t]
  17942. ["Archive subtree" org-agenda-archive t]
  17943. ["Delete subtree" org-agenda-kill t]
  17944. "--"
  17945. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17946. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17947. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17948. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17949. "--"
  17950. ("Tags and Properties"
  17951. ["Show all Tags" org-agenda-show-tags t]
  17952. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17953. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17954. "--"
  17955. ["Column View" org-columns t])
  17956. ("Date/Schedule"
  17957. ["Schedule" org-agenda-schedule t]
  17958. ["Set Deadline" org-agenda-deadline t]
  17959. "--"
  17960. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17961. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17962. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17963. ("Clock"
  17964. ["Clock in" org-agenda-clock-in t]
  17965. ["Clock out" org-agenda-clock-out t]
  17966. ["Clock cancel" org-agenda-clock-cancel t]
  17967. ["Goto running clock" org-clock-goto t])
  17968. ("Priority"
  17969. ["Set Priority" org-agenda-priority t]
  17970. ["Increase Priority" org-agenda-priority-up t]
  17971. ["Decrease Priority" org-agenda-priority-down t]
  17972. ["Show Priority" org-agenda-show-priority t])
  17973. ("Calendar/Diary"
  17974. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17975. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17976. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17977. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17978. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17979. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17980. "--"
  17981. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17982. "--"
  17983. ("View"
  17984. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17985. :style radio :selected (equal org-agenda-ndays 1)]
  17986. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17987. :style radio :selected (equal org-agenda-ndays 7)]
  17988. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17989. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17990. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17991. :style radio :selected (member org-agenda-ndays '(365 366))]
  17992. "--"
  17993. ["Show Logbook entries" org-agenda-log-mode
  17994. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17995. ["Include Diary" org-agenda-toggle-diary
  17996. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17997. ["Use Time Grid" org-agenda-toggle-time-grid
  17998. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17999. ["Write view to file" org-write-agenda t]
  18000. ["Rebuild buffer" org-agenda-redo t]
  18001. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  18002. "--"
  18003. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  18004. "--"
  18005. ["Quit" org-agenda-quit t]
  18006. ["Exit and Release Buffers" org-agenda-exit t]
  18007. ))
  18008. ;;; Agenda undo
  18009. (defvar org-agenda-allow-remote-undo t
  18010. "Non-nil means, allow remote undo from the agenda buffer.")
  18011. (defvar org-agenda-undo-list nil
  18012. "List of undoable operations in the agenda since last refresh.")
  18013. (defvar org-agenda-undo-has-started-in nil
  18014. "Buffers that have already seen `undo-start' in the current undo sequence.")
  18015. (defvar org-agenda-pending-undo-list nil
  18016. "In a series of undo commands, this is the list of remaning undo items.")
  18017. (defmacro org-if-unprotected (&rest body)
  18018. "Execute BODY if there is no `org-protected' text property at point."
  18019. (declare (debug t))
  18020. `(unless (get-text-property (point) 'org-protected)
  18021. ,@body))
  18022. (defmacro org-with-remote-undo (_buffer &rest _body)
  18023. "Execute BODY while recording undo information in two buffers."
  18024. (declare (indent 1) (debug t))
  18025. `(let ((_cline (org-current-line))
  18026. (_cmd this-command)
  18027. (_buf1 (current-buffer))
  18028. (_buf2 ,_buffer)
  18029. (_undo1 buffer-undo-list)
  18030. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  18031. _c1 _c2)
  18032. ,@_body
  18033. (when org-agenda-allow-remote-undo
  18034. (setq _c1 (org-verify-change-for-undo
  18035. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  18036. _c2 (org-verify-change-for-undo
  18037. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  18038. (when (or _c1 _c2)
  18039. ;; make sure there are undo boundaries
  18040. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  18041. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  18042. ;; remember which buffer to undo
  18043. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  18044. org-agenda-undo-list)))))
  18045. (defun org-agenda-undo ()
  18046. "Undo a remote editing step in the agenda.
  18047. This undoes changes both in the agenda buffer and in the remote buffer
  18048. that have been changed along."
  18049. (interactive)
  18050. (or org-agenda-allow-remote-undo
  18051. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  18052. (if (not (eq this-command last-command))
  18053. (setq org-agenda-undo-has-started-in nil
  18054. org-agenda-pending-undo-list org-agenda-undo-list))
  18055. (if (not org-agenda-pending-undo-list)
  18056. (error "No further undo information"))
  18057. (let* ((entry (pop org-agenda-pending-undo-list))
  18058. buf line cmd rembuf)
  18059. (setq cmd (pop entry) line (pop entry))
  18060. (setq rembuf (nth 2 entry))
  18061. (org-with-remote-undo rembuf
  18062. (while (bufferp (setq buf (pop entry)))
  18063. (if (pop entry)
  18064. (with-current-buffer buf
  18065. (let ((last-undo-buffer buf)
  18066. (inhibit-read-only t))
  18067. (unless (memq buf org-agenda-undo-has-started-in)
  18068. (push buf org-agenda-undo-has-started-in)
  18069. (make-local-variable 'pending-undo-list)
  18070. (undo-start))
  18071. (while (and pending-undo-list
  18072. (listp pending-undo-list)
  18073. (not (car pending-undo-list)))
  18074. (pop pending-undo-list))
  18075. (undo-more 1))))))
  18076. (goto-line line)
  18077. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  18078. (defun org-verify-change-for-undo (l1 l2)
  18079. "Verify that a real change occurred between the undo lists L1 and L2."
  18080. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  18081. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  18082. (not (eq l1 l2)))
  18083. ;;; Agenda dispatch
  18084. (defvar org-agenda-restrict nil)
  18085. (defvar org-agenda-restrict-begin (make-marker))
  18086. (defvar org-agenda-restrict-end (make-marker))
  18087. (defvar org-agenda-last-dispatch-buffer nil)
  18088. (defvar org-agenda-overriding-restriction nil)
  18089. ;;;###autoload
  18090. (defun org-agenda (arg &optional keys restriction)
  18091. "Dispatch agenda commands to collect entries to the agenda buffer.
  18092. Prompts for a command to execute. Any prefix arg will be passed
  18093. on to the selected command. The default selections are:
  18094. a Call `org-agenda-list' to display the agenda for current day or week.
  18095. t Call `org-todo-list' to display the global todo list.
  18096. T Call `org-todo-list' to display the global todo list, select only
  18097. entries with a specific TODO keyword (the user gets a prompt).
  18098. m Call `org-tags-view' to display headlines with tags matching
  18099. a condition (the user is prompted for the condition).
  18100. M Like `m', but select only TODO entries, no ordinary headlines.
  18101. L Create a timeline for the current buffer.
  18102. e Export views to associated files.
  18103. More commands can be added by configuring the variable
  18104. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  18105. searches can be pre-defined in this way.
  18106. If the current buffer is in Org-mode and visiting a file, you can also
  18107. first press `<' once to indicate that the agenda should be temporarily
  18108. \(until the next use of \\[org-agenda]) restricted to the current file.
  18109. Pressing `<' twice means to restrict to the current subtree or region
  18110. \(if active)."
  18111. (interactive "P")
  18112. (catch 'exit
  18113. (let* ((prefix-descriptions nil)
  18114. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  18115. (org-agenda-custom-commands
  18116. ;; normalize different versions
  18117. (delq nil
  18118. (mapcar
  18119. (lambda (x)
  18120. (cond ((stringp (cdr x))
  18121. (push x prefix-descriptions)
  18122. nil)
  18123. ((stringp (nth 1 x)) x)
  18124. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18125. (t (cons (car x) (cons "" (cdr x))))))
  18126. org-agenda-custom-commands)))
  18127. (buf (current-buffer))
  18128. (bfn (buffer-file-name (buffer-base-buffer)))
  18129. entry key type match lprops ans)
  18130. ;; Turn off restriction unless there is an overriding one
  18131. (unless org-agenda-overriding-restriction
  18132. (put 'org-agenda-files 'org-restrict nil)
  18133. (setq org-agenda-restrict nil)
  18134. (move-marker org-agenda-restrict-begin nil)
  18135. (move-marker org-agenda-restrict-end nil))
  18136. ;; Delete old local properties
  18137. (put 'org-agenda-redo-command 'org-lprops nil)
  18138. ;; Remember where this call originated
  18139. (setq org-agenda-last-dispatch-buffer (current-buffer))
  18140. (unless keys
  18141. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  18142. keys (car ans)
  18143. restriction (cdr ans)))
  18144. ;; Estabish the restriction, if any
  18145. (when (and (not org-agenda-overriding-restriction) restriction)
  18146. (put 'org-agenda-files 'org-restrict (list bfn))
  18147. (cond
  18148. ((eq restriction 'region)
  18149. (setq org-agenda-restrict t)
  18150. (move-marker org-agenda-restrict-begin (region-beginning))
  18151. (move-marker org-agenda-restrict-end (region-end)))
  18152. ((eq restriction 'subtree)
  18153. (save-excursion
  18154. (setq org-agenda-restrict t)
  18155. (org-back-to-heading t)
  18156. (move-marker org-agenda-restrict-begin (point))
  18157. (move-marker org-agenda-restrict-end
  18158. (progn (org-end-of-subtree t)))))))
  18159. (require 'calendar) ; FIXME: can we avoid this for some commands?
  18160. ;; For example the todo list should not need it (but does...)
  18161. (cond
  18162. ((setq entry (assoc keys org-agenda-custom-commands))
  18163. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  18164. (progn
  18165. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  18166. (put 'org-agenda-redo-command 'org-lprops lprops)
  18167. (cond
  18168. ((eq type 'agenda)
  18169. (org-let lprops '(org-agenda-list current-prefix-arg)))
  18170. ((eq type 'alltodo)
  18171. (org-let lprops '(org-todo-list current-prefix-arg)))
  18172. ((eq type 'search)
  18173. (org-let lprops '(org-search-view current-prefix-arg match)))
  18174. ((eq type 'stuck)
  18175. (org-let lprops '(org-agenda-list-stuck-projects
  18176. current-prefix-arg)))
  18177. ((eq type 'tags)
  18178. (org-let lprops '(org-tags-view current-prefix-arg match)))
  18179. ((eq type 'tags-todo)
  18180. (org-let lprops '(org-tags-view '(4) match)))
  18181. ((eq type 'todo)
  18182. (org-let lprops '(org-todo-list match)))
  18183. ((eq type 'tags-tree)
  18184. (org-check-for-org-mode)
  18185. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  18186. ((eq type 'todo-tree)
  18187. (org-check-for-org-mode)
  18188. (org-let lprops
  18189. '(org-occur (concat "^" outline-regexp "[ \t]*"
  18190. (regexp-quote match) "\\>"))))
  18191. ((eq type 'occur-tree)
  18192. (org-check-for-org-mode)
  18193. (org-let lprops '(org-occur match)))
  18194. ((functionp type)
  18195. (org-let lprops '(funcall type match)))
  18196. ((fboundp type)
  18197. (org-let lprops '(funcall type match)))
  18198. (t (error "Invalid custom agenda command type %s" type))))
  18199. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  18200. ((equal keys "C")
  18201. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  18202. (customize-variable 'org-agenda-custom-commands))
  18203. ((equal keys "a") (call-interactively 'org-agenda-list))
  18204. ((equal keys "s") (call-interactively 'org-search-view))
  18205. ((equal keys "t") (call-interactively 'org-todo-list))
  18206. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  18207. ((equal keys "m") (call-interactively 'org-tags-view))
  18208. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  18209. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  18210. ((equal keys "L")
  18211. (unless (org-mode-p)
  18212. (error "This is not an Org-mode file"))
  18213. (unless restriction
  18214. (put 'org-agenda-files 'org-restrict (list bfn))
  18215. (org-call-with-arg 'org-timeline arg)))
  18216. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  18217. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  18218. ((equal keys "!") (customize-variable 'org-stuck-projects))
  18219. (t (error "Invalid agenda key"))))))
  18220. (defun org-agenda-normalize-custom-commands (cmds)
  18221. (delq nil
  18222. (mapcar
  18223. (lambda (x)
  18224. (cond ((stringp (cdr x)) nil)
  18225. ((stringp (nth 1 x)) x)
  18226. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  18227. (t (cons (car x) (cons "" (cdr x))))))
  18228. cmds)))
  18229. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  18230. "The user interface for selecting an agenda command."
  18231. (catch 'exit
  18232. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  18233. (restrict-ok (and bfn (org-mode-p)))
  18234. (region-p (org-region-active-p))
  18235. (custom org-agenda-custom-commands)
  18236. (selstring "")
  18237. restriction second-time
  18238. c entry key type match prefixes rmheader header-end custom1 desc)
  18239. (save-window-excursion
  18240. (delete-other-windows)
  18241. (org-switch-to-buffer-other-window " *Agenda Commands*")
  18242. (erase-buffer)
  18243. (insert (eval-when-compile
  18244. (let ((header
  18245. "
  18246. Press key for an agenda command: < Buffer,subtree/region restriction
  18247. -------------------------------- > Remove restriction
  18248. a Agenda for current week or day e Export agenda views
  18249. t List of all TODO entries T Entries with special TODO kwd
  18250. m Match a TAGS query M Like m, but only TODO entries
  18251. L Timeline for current buffer # List stuck projects (!=configure)
  18252. s Search for keywords C Configure custom agenda commands
  18253. / Multi-occur
  18254. ")
  18255. (start 0))
  18256. (while (string-match
  18257. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  18258. header start)
  18259. (setq start (match-end 0))
  18260. (add-text-properties (match-beginning 2) (match-end 2)
  18261. '(face bold) header))
  18262. header)))
  18263. (setq header-end (move-marker (make-marker) (point)))
  18264. (while t
  18265. (setq custom1 custom)
  18266. (when (eq rmheader t)
  18267. (goto-line 1)
  18268. (re-search-forward ":" nil t)
  18269. (delete-region (match-end 0) (point-at-eol))
  18270. (forward-char 1)
  18271. (looking-at "-+")
  18272. (delete-region (match-end 0) (point-at-eol))
  18273. (move-marker header-end (match-end 0)))
  18274. (goto-char header-end)
  18275. (delete-region (point) (point-max))
  18276. (while (setq entry (pop custom1))
  18277. (setq key (car entry) desc (nth 1 entry)
  18278. type (nth 2 entry) match (nth 3 entry))
  18279. (if (> (length key) 1)
  18280. (add-to-list 'prefixes (string-to-char key))
  18281. (insert
  18282. (format
  18283. "\n%-4s%-14s: %s"
  18284. (org-add-props (copy-sequence key)
  18285. '(face bold))
  18286. (cond
  18287. ((string-match "\\S-" desc) desc)
  18288. ((eq type 'agenda) "Agenda for current week or day")
  18289. ((eq type 'alltodo) "List of all TODO entries")
  18290. ((eq type 'search) "Word search")
  18291. ((eq type 'stuck) "List of stuck projects")
  18292. ((eq type 'todo) "TODO keyword")
  18293. ((eq type 'tags) "Tags query")
  18294. ((eq type 'tags-todo) "Tags (TODO)")
  18295. ((eq type 'tags-tree) "Tags tree")
  18296. ((eq type 'todo-tree) "TODO kwd tree")
  18297. ((eq type 'occur-tree) "Occur tree")
  18298. ((functionp type) (if (symbolp type)
  18299. (symbol-name type)
  18300. "Lambda expression"))
  18301. (t "???"))
  18302. (cond
  18303. ((stringp match)
  18304. (org-add-props match nil 'face 'org-warning))
  18305. (match
  18306. (format "set of %d commands" (length match)))
  18307. (t ""))))))
  18308. (when prefixes
  18309. (mapc (lambda (x)
  18310. (insert
  18311. (format "\n%s %s"
  18312. (org-add-props (char-to-string x)
  18313. nil 'face 'bold)
  18314. (or (cdr (assoc (concat selstring (char-to-string x))
  18315. prefix-descriptions))
  18316. "Prefix key"))))
  18317. prefixes))
  18318. (goto-char (point-min))
  18319. (when (fboundp 'fit-window-to-buffer)
  18320. (if second-time
  18321. (if (not (pos-visible-in-window-p (point-max)))
  18322. (fit-window-to-buffer))
  18323. (setq second-time t)
  18324. (fit-window-to-buffer)))
  18325. (message "Press key for agenda command%s:"
  18326. (if (or restrict-ok org-agenda-overriding-restriction)
  18327. (if org-agenda-overriding-restriction
  18328. " (restriction lock active)"
  18329. (if restriction
  18330. (format " (restricted to %s)" restriction)
  18331. " (unrestricted)"))
  18332. ""))
  18333. (setq c (read-char-exclusive))
  18334. (message "")
  18335. (cond
  18336. ((assoc (char-to-string c) custom)
  18337. (setq selstring (concat selstring (char-to-string c)))
  18338. (throw 'exit (cons selstring restriction)))
  18339. ((memq c prefixes)
  18340. (setq selstring (concat selstring (char-to-string c))
  18341. prefixes nil
  18342. rmheader (or rmheader t)
  18343. custom (delq nil (mapcar
  18344. (lambda (x)
  18345. (if (or (= (length (car x)) 1)
  18346. (/= (string-to-char (car x)) c))
  18347. nil
  18348. (cons (substring (car x) 1) (cdr x))))
  18349. custom))))
  18350. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18351. (message "Restriction is only possible in Org-mode buffers")
  18352. (ding) (sit-for 1))
  18353. ((eq c ?1)
  18354. (org-agenda-remove-restriction-lock 'noupdate)
  18355. (setq restriction 'buffer))
  18356. ((eq c ?0)
  18357. (org-agenda-remove-restriction-lock 'noupdate)
  18358. (setq restriction (if region-p 'region 'subtree)))
  18359. ((eq c ?<)
  18360. (org-agenda-remove-restriction-lock 'noupdate)
  18361. (setq restriction
  18362. (cond
  18363. ((eq restriction 'buffer)
  18364. (if region-p 'region 'subtree))
  18365. ((memq restriction '(subtree region))
  18366. nil)
  18367. (t 'buffer))))
  18368. ((eq c ?>)
  18369. (org-agenda-remove-restriction-lock 'noupdate)
  18370. (setq restriction nil))
  18371. ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18372. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18373. ((and (> (length selstring) 0) (eq c ?\d))
  18374. (delete-window)
  18375. (org-agenda-get-restriction-and-command prefix-descriptions))
  18376. ((equal c ?q) (error "Abort"))
  18377. (t (error "Invalid key %c" c))))))))
  18378. (defun org-run-agenda-series (name series)
  18379. (org-prepare-agenda name)
  18380. (let* ((org-agenda-multi t)
  18381. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18382. (cmds (car series))
  18383. (gprops (nth 1 series))
  18384. match ;; The byte compiler incorrectly complains about this. Keep it!
  18385. cmd type lprops)
  18386. (while (setq cmd (pop cmds))
  18387. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18388. (cond
  18389. ((eq type 'agenda)
  18390. (org-let2 gprops lprops
  18391. '(call-interactively 'org-agenda-list)))
  18392. ((eq type 'alltodo)
  18393. (org-let2 gprops lprops
  18394. '(call-interactively 'org-todo-list)))
  18395. ((eq type 'search)
  18396. (org-let2 gprops lprops
  18397. '(org-search-view current-prefix-arg match)))
  18398. ((eq type 'stuck)
  18399. (org-let2 gprops lprops
  18400. '(call-interactively 'org-agenda-list-stuck-projects)))
  18401. ((eq type 'tags)
  18402. (org-let2 gprops lprops
  18403. '(org-tags-view current-prefix-arg match)))
  18404. ((eq type 'tags-todo)
  18405. (org-let2 gprops lprops
  18406. '(org-tags-view '(4) match)))
  18407. ((eq type 'todo)
  18408. (org-let2 gprops lprops
  18409. '(org-todo-list match)))
  18410. ((fboundp type)
  18411. (org-let2 gprops lprops
  18412. '(funcall type match)))
  18413. (t (error "Invalid type in command series"))))
  18414. (widen)
  18415. (setq org-agenda-redo-command redo)
  18416. (goto-char (point-min)))
  18417. (org-finalize-agenda))
  18418. ;;;###autoload
  18419. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18420. "Run an agenda command in batch mode and send the result to STDOUT.
  18421. If CMD-KEY is a string of length 1, it is used as a key in
  18422. `org-agenda-custom-commands' and triggers this command. If it is a
  18423. longer string it is used as a tags/todo match string.
  18424. Paramters are alternating variable names and values that will be bound
  18425. before running the agenda command."
  18426. (let (pars)
  18427. (while parameters
  18428. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18429. (if (> (length cmd-key) 2)
  18430. (eval (list 'let (nreverse pars)
  18431. (list 'org-tags-view nil cmd-key)))
  18432. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18433. (set-buffer org-agenda-buffer-name)
  18434. (princ (org-encode-for-stdout (buffer-string)))))
  18435. (defun org-encode-for-stdout (string)
  18436. (if (fboundp 'encode-coding-string)
  18437. (encode-coding-string string buffer-file-coding-system)
  18438. string))
  18439. (defvar org-agenda-info nil)
  18440. ;;;###autoload
  18441. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18442. "Run an agenda command in batch mode and send the result to STDOUT.
  18443. If CMD-KEY is a string of length 1, it is used as a key in
  18444. `org-agenda-custom-commands' and triggers this command. If it is a
  18445. longer string it is used as a tags/todo match string.
  18446. Paramters are alternating variable names and values that will be bound
  18447. before running the agenda command.
  18448. The output gives a line for each selected agenda item. Each
  18449. item is a list of comma-separated values, like this:
  18450. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18451. category The category of the item
  18452. head The headline, without TODO kwd, TAGS and PRIORITY
  18453. type The type of the agenda entry, can be
  18454. todo selected in TODO match
  18455. tagsmatch selected in tags match
  18456. diary imported from diary
  18457. deadline a deadline on given date
  18458. scheduled scheduled on given date
  18459. timestamp entry has timestamp on given date
  18460. closed entry was closed on given date
  18461. upcoming-deadline warning about deadline
  18462. past-scheduled forwarded scheduled item
  18463. block entry has date block including g. date
  18464. todo The todo keyword, if any
  18465. tags All tags including inherited ones, separated by colons
  18466. date The relevant date, like 2007-2-14
  18467. time The time, like 15:00-16:50
  18468. extra Sting with extra planning info
  18469. priority-l The priority letter if any was given
  18470. priority-n The computed numerical priority
  18471. agenda-day The day in the agenda where this is listed"
  18472. (let (pars)
  18473. (while parameters
  18474. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18475. (push (list 'org-agenda-remove-tags t) pars)
  18476. (if (> (length cmd-key) 2)
  18477. (eval (list 'let (nreverse pars)
  18478. (list 'org-tags-view nil cmd-key)))
  18479. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18480. (set-buffer org-agenda-buffer-name)
  18481. (let* ((lines (org-split-string (buffer-string) "\n"))
  18482. line)
  18483. (while (setq line (pop lines))
  18484. (catch 'next
  18485. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18486. (setq org-agenda-info
  18487. (org-fix-agenda-info (text-properties-at 0 line)))
  18488. (princ
  18489. (org-encode-for-stdout
  18490. (mapconcat 'org-agenda-export-csv-mapper
  18491. '(org-category txt type todo tags date time-of-day extra
  18492. priority-letter priority agenda-day)
  18493. ",")))
  18494. (princ "\n"))))))
  18495. (defun org-fix-agenda-info (props)
  18496. "Make sure all properties on an agenda item have a canonical form,
  18497. so the export commands can easily use it."
  18498. (let (tmp re)
  18499. (when (setq tmp (plist-get props 'tags))
  18500. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18501. (when (setq tmp (plist-get props 'date))
  18502. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18503. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18504. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18505. (setq tmp (calendar-date-string tmp)))
  18506. (setq props (plist-put props 'date tmp)))
  18507. (when (setq tmp (plist-get props 'day))
  18508. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18509. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18510. (setq tmp (calendar-date-string tmp)))
  18511. (setq props (plist-put props 'day tmp))
  18512. (setq props (plist-put props 'agenda-day tmp)))
  18513. (when (setq tmp (plist-get props 'txt))
  18514. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18515. (plist-put props 'priority-letter (match-string 1 tmp))
  18516. (setq tmp (replace-match "" t t tmp)))
  18517. (when (and (setq re (plist-get props 'org-todo-regexp))
  18518. (setq re (concat "\\`\\.*" re " ?"))
  18519. (string-match re tmp))
  18520. (plist-put props 'todo (match-string 1 tmp))
  18521. (setq tmp (replace-match "" t t tmp)))
  18522. (plist-put props 'txt tmp)))
  18523. props)
  18524. (defun org-agenda-export-csv-mapper (prop)
  18525. (let ((res (plist-get org-agenda-info prop)))
  18526. (setq res
  18527. (cond
  18528. ((not res) "")
  18529. ((stringp res) res)
  18530. (t (prin1-to-string res))))
  18531. (while (string-match "," res)
  18532. (setq res (replace-match ";" t t res)))
  18533. (org-trim res)))
  18534. ;;;###autoload
  18535. (defun org-store-agenda-views (&rest parameters)
  18536. (interactive)
  18537. (eval (list 'org-batch-store-agenda-views)))
  18538. ;; FIXME, why is this a macro?????
  18539. ;;;###autoload
  18540. (defmacro org-batch-store-agenda-views (&rest parameters)
  18541. "Run all custom agenda commands that have a file argument."
  18542. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18543. (pop-up-frames nil)
  18544. (dir default-directory)
  18545. pars cmd thiscmdkey files opts)
  18546. (while parameters
  18547. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18548. (setq pars (reverse pars))
  18549. (save-window-excursion
  18550. (while cmds
  18551. (setq cmd (pop cmds)
  18552. thiscmdkey (car cmd)
  18553. opts (nth 4 cmd)
  18554. files (nth 5 cmd))
  18555. (if (stringp files) (setq files (list files)))
  18556. (when files
  18557. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18558. (list 'org-agenda nil thiscmdkey)))
  18559. (set-buffer org-agenda-buffer-name)
  18560. (while files
  18561. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18562. (list 'org-write-agenda
  18563. (expand-file-name (pop files) dir) t))))
  18564. (and (get-buffer org-agenda-buffer-name)
  18565. (kill-buffer org-agenda-buffer-name)))))))
  18566. (defun org-write-agenda (file &optional nosettings)
  18567. "Write the current buffer (an agenda view) as a file.
  18568. Depending on the extension of the file name, plain text (.txt),
  18569. HTML (.html or .htm) or Postscript (.ps) is produced.
  18570. If NOSETTINGS is given, do not scope the settings of
  18571. `org-agenda-exporter-settings' into the export commands. This is used when
  18572. the settings have already been scoped and we do not wish to overrule other,
  18573. higher priority settings."
  18574. (interactive "FWrite agenda to file: ")
  18575. (if (not (file-writable-p file))
  18576. (error "Cannot write agenda to file %s" file))
  18577. (cond
  18578. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18579. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18580. (org-let (if nosettings nil org-agenda-exporter-settings)
  18581. '(save-excursion
  18582. (save-window-excursion
  18583. (cond
  18584. ((string-match "\\.html?\\'" file)
  18585. (set-buffer (htmlize-buffer (current-buffer)))
  18586. (when (and org-agenda-export-html-style
  18587. (string-match "<style>" org-agenda-export-html-style))
  18588. ;; replace <style> section with org-agenda-export-html-style
  18589. (goto-char (point-min))
  18590. (kill-region (- (search-forward "<style") 6)
  18591. (search-forward "</style>"))
  18592. (insert org-agenda-export-html-style))
  18593. (write-file file)
  18594. (kill-buffer (current-buffer))
  18595. (message "HTML written to %s" file))
  18596. ((string-match "\\.ps\\'" file)
  18597. (ps-print-buffer-with-faces file)
  18598. (message "Postscript written to %s" file))
  18599. (t
  18600. (let ((bs (buffer-string)))
  18601. (find-file file)
  18602. (insert bs)
  18603. (save-buffer 0)
  18604. (kill-buffer (current-buffer))
  18605. (message "Plain text written to %s" file))))))
  18606. (set-buffer org-agenda-buffer-name)))
  18607. (defmacro org-no-read-only (&rest body)
  18608. "Inhibit read-only for BODY."
  18609. `(let ((inhibit-read-only t)) ,@body))
  18610. (defun org-check-for-org-mode ()
  18611. "Make sure current buffer is in org-mode. Error if not."
  18612. (or (org-mode-p)
  18613. (error "Cannot execute org-mode agenda command on buffer in %s."
  18614. major-mode)))
  18615. (defun org-fit-agenda-window ()
  18616. "Fit the window to the buffer size."
  18617. (and (memq org-agenda-window-setup '(reorganize-frame))
  18618. (fboundp 'fit-window-to-buffer)
  18619. (fit-window-to-buffer
  18620. nil
  18621. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18622. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18623. ;;; Agenda file list
  18624. (defun org-agenda-files (&optional unrestricted)
  18625. "Get the list of agenda files.
  18626. Optional UNRESTRICTED means return the full list even if a restriction
  18627. is currently in place."
  18628. (let ((files
  18629. (cond
  18630. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18631. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18632. ((listp org-agenda-files) org-agenda-files)
  18633. (t (error "Invalid value of `org-agenda-files'")))))
  18634. (setq files (apply 'append
  18635. (mapcar (lambda (f)
  18636. (if (file-directory-p f)
  18637. (directory-files f t
  18638. org-agenda-file-regexp)
  18639. (list f)))
  18640. files)))
  18641. (if org-agenda-skip-unavailable-files
  18642. (delq nil
  18643. (mapcar (function
  18644. (lambda (file)
  18645. (and (file-readable-p file) file)))
  18646. files))
  18647. files))) ; `org-check-agenda-file' will remove them from the list
  18648. (defun org-edit-agenda-file-list ()
  18649. "Edit the list of agenda files.
  18650. Depending on setup, this either uses customize to edit the variable
  18651. `org-agenda-files', or it visits the file that is holding the list. In the
  18652. latter case, the buffer is set up in a way that saving it automatically kills
  18653. the buffer and restores the previous window configuration."
  18654. (interactive)
  18655. (if (stringp org-agenda-files)
  18656. (let ((cw (current-window-configuration)))
  18657. (find-file org-agenda-files)
  18658. (org-set-local 'org-window-configuration cw)
  18659. (org-add-hook 'after-save-hook
  18660. (lambda ()
  18661. (set-window-configuration
  18662. (prog1 org-window-configuration
  18663. (kill-buffer (current-buffer))))
  18664. (org-install-agenda-files-menu)
  18665. (message "New agenda file list installed"))
  18666. nil 'local)
  18667. (message "%s" (substitute-command-keys
  18668. "Edit list and finish with \\[save-buffer]")))
  18669. (customize-variable 'org-agenda-files)))
  18670. (defun org-store-new-agenda-file-list (list)
  18671. "Set new value for the agenda file list and save it correcly."
  18672. (if (stringp org-agenda-files)
  18673. (let ((f org-agenda-files) b)
  18674. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18675. (with-temp-file f
  18676. (insert (mapconcat 'identity list "\n") "\n")))
  18677. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18678. (setq org-agenda-files list)
  18679. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18680. (defun org-read-agenda-file-list ()
  18681. "Read the list of agenda files from a file."
  18682. (when (stringp org-agenda-files)
  18683. (with-temp-buffer
  18684. (insert-file-contents org-agenda-files)
  18685. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18686. ;;;###autoload
  18687. (defun org-cycle-agenda-files ()
  18688. "Cycle through the files in `org-agenda-files'.
  18689. If the current buffer visits an agenda file, find the next one in the list.
  18690. If the current buffer does not, find the first agenda file."
  18691. (interactive)
  18692. (let* ((fs (org-agenda-files t))
  18693. (files (append fs (list (car fs))))
  18694. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18695. file)
  18696. (unless files (error "No agenda files"))
  18697. (catch 'exit
  18698. (while (setq file (pop files))
  18699. (if (equal (file-truename file) tcf)
  18700. (when (car files)
  18701. (find-file (car files))
  18702. (throw 'exit t))))
  18703. (find-file (car fs)))
  18704. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18705. (defun org-agenda-file-to-front (&optional to-end)
  18706. "Move/add the current file to the top of the agenda file list.
  18707. If the file is not present in the list, it is added to the front. If it is
  18708. present, it is moved there. With optional argument TO-END, add/move to the
  18709. end of the list."
  18710. (interactive "P")
  18711. (let ((org-agenda-skip-unavailable-files nil)
  18712. (file-alist (mapcar (lambda (x)
  18713. (cons (file-truename x) x))
  18714. (org-agenda-files t)))
  18715. (ctf (file-truename buffer-file-name))
  18716. x had)
  18717. (setq x (assoc ctf file-alist) had x)
  18718. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18719. (if to-end
  18720. (setq file-alist (append (delq x file-alist) (list x)))
  18721. (setq file-alist (cons x (delq x file-alist))))
  18722. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18723. (org-install-agenda-files-menu)
  18724. (message "File %s to %s of agenda file list"
  18725. (if had "moved" "added") (if to-end "end" "front"))))
  18726. (defun org-remove-file (&optional file)
  18727. "Remove current file from the list of files in variable `org-agenda-files'.
  18728. These are the files which are being checked for agenda entries.
  18729. Optional argument FILE means, use this file instead of the current."
  18730. (interactive)
  18731. (let* ((org-agenda-skip-unavailable-files nil)
  18732. (file (or file buffer-file-name))
  18733. (true-file (file-truename file))
  18734. (afile (abbreviate-file-name file))
  18735. (files (delq nil (mapcar
  18736. (lambda (x)
  18737. (if (equal true-file
  18738. (file-truename x))
  18739. nil x))
  18740. (org-agenda-files t)))))
  18741. (if (not (= (length files) (length (org-agenda-files t))))
  18742. (progn
  18743. (org-store-new-agenda-file-list files)
  18744. (org-install-agenda-files-menu)
  18745. (message "Removed file: %s" afile))
  18746. (message "File was not in list: %s (not removed)" afile))))
  18747. (defun org-file-menu-entry (file)
  18748. (vector file (list 'find-file file) t))
  18749. (defun org-check-agenda-file (file)
  18750. "Make sure FILE exists. If not, ask user what to do."
  18751. (when (not (file-exists-p file))
  18752. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18753. (abbreviate-file-name file))
  18754. (let ((r (downcase (read-char-exclusive))))
  18755. (cond
  18756. ((equal r ?r)
  18757. (org-remove-file file)
  18758. (throw 'nextfile t))
  18759. (t (error "Abort"))))))
  18760. ;;; Agenda prepare and finalize
  18761. (defvar org-agenda-multi nil) ; dynammically scoped
  18762. (defvar org-agenda-buffer-name "*Org Agenda*")
  18763. (defvar org-pre-agenda-window-conf nil)
  18764. (defvar org-agenda-name nil)
  18765. (defun org-prepare-agenda (&optional name)
  18766. (setq org-todo-keywords-for-agenda nil)
  18767. (setq org-done-keywords-for-agenda nil)
  18768. (if org-agenda-multi
  18769. (progn
  18770. (setq buffer-read-only nil)
  18771. (goto-char (point-max))
  18772. (unless (or (bobp) org-agenda-compact-blocks)
  18773. (insert "\n" (make-string (window-width) ?=) "\n"))
  18774. (narrow-to-region (point) (point-max)))
  18775. (org-agenda-reset-markers)
  18776. (org-prepare-agenda-buffers (org-agenda-files))
  18777. (setq org-todo-keywords-for-agenda
  18778. (org-uniquify org-todo-keywords-for-agenda))
  18779. (setq org-done-keywords-for-agenda
  18780. (org-uniquify org-done-keywords-for-agenda))
  18781. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18782. (awin (get-buffer-window abuf)))
  18783. (cond
  18784. ((equal (current-buffer) abuf) nil)
  18785. (awin (select-window awin))
  18786. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18787. ((equal org-agenda-window-setup 'current-window)
  18788. (switch-to-buffer abuf))
  18789. ((equal org-agenda-window-setup 'other-window)
  18790. (org-switch-to-buffer-other-window abuf))
  18791. ((equal org-agenda-window-setup 'other-frame)
  18792. (switch-to-buffer-other-frame abuf))
  18793. ((equal org-agenda-window-setup 'reorganize-frame)
  18794. (delete-other-windows)
  18795. (org-switch-to-buffer-other-window abuf))))
  18796. (setq buffer-read-only nil)
  18797. (erase-buffer)
  18798. (org-agenda-mode)
  18799. (and name (not org-agenda-name)
  18800. (org-set-local 'org-agenda-name name)))
  18801. (setq buffer-read-only nil))
  18802. (defun org-finalize-agenda ()
  18803. "Finishing touch for the agenda buffer, called just before displaying it."
  18804. (unless org-agenda-multi
  18805. (save-excursion
  18806. (let ((inhibit-read-only t))
  18807. (goto-char (point-min))
  18808. (while (org-activate-bracket-links (point-max))
  18809. (add-text-properties (match-beginning 0) (match-end 0)
  18810. '(face org-link)))
  18811. (org-agenda-align-tags)
  18812. (unless org-agenda-with-colors
  18813. (remove-text-properties (point-min) (point-max) '(face nil))))
  18814. (if (and (boundp 'org-overriding-columns-format)
  18815. org-overriding-columns-format)
  18816. (org-set-local 'org-overriding-columns-format
  18817. org-overriding-columns-format))
  18818. (if (and (boundp 'org-agenda-view-columns-initially)
  18819. org-agenda-view-columns-initially)
  18820. (org-agenda-columns))
  18821. (when org-agenda-fontify-priorities
  18822. (org-fontify-priorities))
  18823. (run-hooks 'org-finalize-agenda-hook)
  18824. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18825. )))
  18826. (defun org-fontify-priorities ()
  18827. "Make highest priority lines bold, and lowest italic."
  18828. (interactive)
  18829. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18830. (org-delete-overlay o)))
  18831. (org-overlays-in (point-min) (point-max)))
  18832. (save-excursion
  18833. (let ((inhibit-read-only t)
  18834. b e p ov h l)
  18835. (goto-char (point-min))
  18836. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18837. (setq h (or (get-char-property (point) 'org-highest-priority)
  18838. org-highest-priority)
  18839. l (or (get-char-property (point) 'org-lowest-priority)
  18840. org-lowest-priority)
  18841. p (string-to-char (match-string 1))
  18842. b (match-beginning 0) e (point-at-eol)
  18843. ov (org-make-overlay b e))
  18844. (org-overlay-put
  18845. ov 'face
  18846. (cond ((listp org-agenda-fontify-priorities)
  18847. (cdr (assoc p org-agenda-fontify-priorities)))
  18848. ((equal p l) 'italic)
  18849. ((equal p h) 'bold)))
  18850. (org-overlay-put ov 'org-type 'org-priority)))))
  18851. (defun org-prepare-agenda-buffers (files)
  18852. "Create buffers for all agenda files, protect archived trees and comments."
  18853. (interactive)
  18854. (let ((pa '(:org-archived t))
  18855. (pc '(:org-comment t))
  18856. (pall '(:org-archived t :org-comment t))
  18857. (inhibit-read-only t)
  18858. (rea (concat ":" org-archive-tag ":"))
  18859. bmp file re)
  18860. (save-excursion
  18861. (save-restriction
  18862. (while (setq file (pop files))
  18863. (if (bufferp file)
  18864. (set-buffer file)
  18865. (org-check-agenda-file file)
  18866. (set-buffer (org-get-agenda-file-buffer file)))
  18867. (widen)
  18868. (setq bmp (buffer-modified-p))
  18869. (org-refresh-category-properties)
  18870. (setq org-todo-keywords-for-agenda
  18871. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18872. (setq org-done-keywords-for-agenda
  18873. (append org-done-keywords-for-agenda org-done-keywords))
  18874. (save-excursion
  18875. (remove-text-properties (point-min) (point-max) pall)
  18876. (when org-agenda-skip-archived-trees
  18877. (goto-char (point-min))
  18878. (while (re-search-forward rea nil t)
  18879. (if (org-on-heading-p t)
  18880. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18881. (goto-char (point-min))
  18882. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18883. (while (re-search-forward re nil t)
  18884. (add-text-properties
  18885. (match-beginning 0) (org-end-of-subtree t) pc)))
  18886. (set-buffer-modified-p bmp))))))
  18887. (defvar org-agenda-skip-function nil
  18888. "Function to be called at each match during agenda construction.
  18889. If this function returns nil, the current match should not be skipped.
  18890. Otherwise, the function must return a position from where the search
  18891. should be continued.
  18892. This may also be a Lisp form, it will be evaluated.
  18893. Never set this variable using `setq' or so, because then it will apply
  18894. to all future agenda commands. Instead, bind it with `let' to scope
  18895. it dynamically into the agenda-constructing command. A good way to set
  18896. it is through options in org-agenda-custom-commands.")
  18897. (defun org-agenda-skip ()
  18898. "Throw to `:skip' in places that should be skipped.
  18899. Also moves point to the end of the skipped region, so that search can
  18900. continue from there."
  18901. (let ((p (point-at-bol)) to fp)
  18902. (and org-agenda-skip-archived-trees
  18903. (get-text-property p :org-archived)
  18904. (org-end-of-subtree t)
  18905. (throw :skip t))
  18906. (and (get-text-property p :org-comment)
  18907. (org-end-of-subtree t)
  18908. (throw :skip t))
  18909. (if (equal (char-after p) ?#) (throw :skip t))
  18910. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18911. (consp org-agenda-skip-function))
  18912. (setq to (save-excursion
  18913. (save-match-data
  18914. (if fp
  18915. (funcall org-agenda-skip-function)
  18916. (eval org-agenda-skip-function))))))
  18917. (goto-char to)
  18918. (throw :skip t))))
  18919. (defvar org-agenda-markers nil
  18920. "List of all currently active markers created by `org-agenda'.")
  18921. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18922. "Creation time of the last agenda marker.")
  18923. (defun org-agenda-new-marker (&optional pos)
  18924. "Return a new agenda marker.
  18925. Org-mode keeps a list of these markers and resets them when they are
  18926. no longer in use."
  18927. (let ((m (copy-marker (or pos (point)))))
  18928. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18929. (push m org-agenda-markers)
  18930. m))
  18931. (defun org-agenda-reset-markers ()
  18932. "Reset markers created by `org-agenda'."
  18933. (while org-agenda-markers
  18934. (move-marker (pop org-agenda-markers) nil)))
  18935. (defun org-get-agenda-file-buffer (file)
  18936. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18937. it to the list of buffers which might be released later."
  18938. (let ((buf (org-find-base-buffer-visiting file)))
  18939. (if buf
  18940. buf ; just return it
  18941. ;; Make a new buffer and remember it
  18942. (setq buf (find-file-noselect file))
  18943. (if buf (push buf org-agenda-new-buffers))
  18944. buf)))
  18945. (defun org-release-buffers (blist)
  18946. "Release all buffers in list, asking the user for confirmation when needed.
  18947. When a buffer is unmodified, it is just killed. When modified, it is saved
  18948. \(if the user agrees) and then killed."
  18949. (let (buf file)
  18950. (while (setq buf (pop blist))
  18951. (setq file (buffer-file-name buf))
  18952. (when (and (buffer-modified-p buf)
  18953. file
  18954. (y-or-n-p (format "Save file %s? " file)))
  18955. (with-current-buffer buf (save-buffer)))
  18956. (kill-buffer buf))))
  18957. (defun org-get-category (&optional pos)
  18958. "Get the category applying to position POS."
  18959. (get-text-property (or pos (point)) 'org-category))
  18960. ;;; Agenda timeline
  18961. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18962. (defun org-timeline (&optional include-all)
  18963. "Show a time-sorted view of the entries in the current org file.
  18964. Only entries with a time stamp of today or later will be listed. With
  18965. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18966. under the current date.
  18967. If the buffer contains an active region, only check the region for
  18968. dates."
  18969. (interactive "P")
  18970. (require 'calendar)
  18971. (org-compile-prefix-format 'timeline)
  18972. (org-set-sorting-strategy 'timeline)
  18973. (let* ((dopast t)
  18974. (dotodo include-all)
  18975. (doclosed org-agenda-show-log)
  18976. (entry buffer-file-name)
  18977. (date (calendar-current-date))
  18978. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18979. (end (if (org-region-active-p) (region-end) (point-max)))
  18980. (day-numbers (org-get-all-dates beg end 'no-ranges
  18981. t doclosed ; always include today
  18982. org-timeline-show-empty-dates))
  18983. (org-deadline-warning-days 0)
  18984. (org-agenda-only-exact-dates t)
  18985. (today (time-to-days (current-time)))
  18986. (past t)
  18987. args
  18988. s e rtn d emptyp)
  18989. (setq org-agenda-redo-command
  18990. (list 'progn
  18991. (list 'org-switch-to-buffer-other-window (current-buffer))
  18992. (list 'org-timeline (list 'quote include-all))))
  18993. (if (not dopast)
  18994. ;; Remove past dates from the list of dates.
  18995. (setq day-numbers (delq nil (mapcar (lambda(x)
  18996. (if (>= x today) x nil))
  18997. day-numbers))))
  18998. (org-prepare-agenda (concat "Timeline "
  18999. (file-name-nondirectory buffer-file-name)))
  19000. (if doclosed (push :closed args))
  19001. (push :timestamp args)
  19002. (push :deadline args)
  19003. (push :scheduled args)
  19004. (push :sexp args)
  19005. (if dotodo (push :todo args))
  19006. (while (setq d (pop day-numbers))
  19007. (if (and (listp d) (eq (car d) :omitted))
  19008. (progn
  19009. (setq s (point))
  19010. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  19011. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  19012. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  19013. (if (and (>= d today)
  19014. dopast
  19015. past)
  19016. (progn
  19017. (setq past nil)
  19018. (insert (make-string 79 ?-) "\n")))
  19019. (setq date (calendar-gregorian-from-absolute d))
  19020. (setq s (point))
  19021. (setq rtn (and (not emptyp)
  19022. (apply 'org-agenda-get-day-entries entry
  19023. date args)))
  19024. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  19025. (progn
  19026. (insert
  19027. (if (stringp org-agenda-format-date)
  19028. (format-time-string org-agenda-format-date
  19029. (org-time-from-absolute date))
  19030. (funcall org-agenda-format-date date))
  19031. "\n")
  19032. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19033. (put-text-property s (1- (point)) 'org-date-line t)
  19034. (if (equal d today)
  19035. (put-text-property s (1- (point)) 'org-today t))
  19036. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  19037. (put-text-property s (1- (point)) 'day d)))))
  19038. (goto-char (point-min))
  19039. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  19040. (point-min)))
  19041. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  19042. (org-finalize-agenda)
  19043. (setq buffer-read-only t)))
  19044. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  19045. "Return a list of all relevant day numbers from BEG to END buffer positions.
  19046. If NO-RANGES is non-nil, include only the start and end dates of a range,
  19047. not every single day in the range. If FORCE-TODAY is non-nil, make
  19048. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  19049. inactive time stamps (those in square brackets) are included.
  19050. When EMPTY is non-nil, also include days without any entries."
  19051. (let ((re (concat
  19052. (if pre-re pre-re "")
  19053. (if inactive org-ts-regexp-both org-ts-regexp)))
  19054. dates dates1 date day day1 day2 ts1 ts2)
  19055. (if force-today
  19056. (setq dates (list (time-to-days (current-time)))))
  19057. (save-excursion
  19058. (goto-char beg)
  19059. (while (re-search-forward re end t)
  19060. (setq day (time-to-days (org-time-string-to-time
  19061. (substring (match-string 1) 0 10))))
  19062. (or (memq day dates) (push day dates)))
  19063. (unless no-ranges
  19064. (goto-char beg)
  19065. (while (re-search-forward org-tr-regexp end t)
  19066. (setq ts1 (substring (match-string 1) 0 10)
  19067. ts2 (substring (match-string 2) 0 10)
  19068. day1 (time-to-days (org-time-string-to-time ts1))
  19069. day2 (time-to-days (org-time-string-to-time ts2)))
  19070. (while (< (setq day1 (1+ day1)) day2)
  19071. (or (memq day1 dates) (push day1 dates)))))
  19072. (setq dates (sort dates '<))
  19073. (when empty
  19074. (while (setq day (pop dates))
  19075. (setq day2 (car dates))
  19076. (push day dates1)
  19077. (when (and day2 empty)
  19078. (if (or (eq empty t)
  19079. (and (numberp empty) (<= (- day2 day) empty)))
  19080. (while (< (setq day (1+ day)) day2)
  19081. (push (list day) dates1))
  19082. (push (cons :omitted (- day2 day)) dates1))))
  19083. (setq dates (nreverse dates1)))
  19084. dates)))
  19085. ;;; Agenda Daily/Weekly
  19086. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  19087. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  19088. (defvar org-agenda-last-arguments nil
  19089. "The arguments of the previous call to org-agenda")
  19090. (defvar org-starting-day nil) ; local variable in the agenda buffer
  19091. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  19092. (defvar org-include-all-loc nil) ; local variable
  19093. (defvar org-agenda-remove-date nil) ; dynamically scoped
  19094. ;;;###autoload
  19095. (defun org-agenda-list (&optional include-all start-day ndays)
  19096. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  19097. The view will be for the current day or week, but from the overview buffer
  19098. you will be able to go to other days/weeks.
  19099. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  19100. all unfinished TODO items will also be shown, before the agenda.
  19101. This feature is considered obsolete, please use the TODO list or a block
  19102. agenda instead.
  19103. With a numeric prefix argument in an interactive call, the agenda will
  19104. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  19105. the number of days. NDAYS defaults to `org-agenda-ndays'.
  19106. START-DAY defaults to TODAY, or to the most recent match for the weekday
  19107. given in `org-agenda-start-on-weekday'."
  19108. (interactive "P")
  19109. (if (and (integerp include-all) (> include-all 0))
  19110. (setq ndays include-all include-all nil))
  19111. (setq ndays (or ndays org-agenda-ndays)
  19112. start-day (or start-day org-agenda-start-day))
  19113. (if org-agenda-overriding-arguments
  19114. (setq include-all (car org-agenda-overriding-arguments)
  19115. start-day (nth 1 org-agenda-overriding-arguments)
  19116. ndays (nth 2 org-agenda-overriding-arguments)))
  19117. (if (stringp start-day)
  19118. ;; Convert to an absolute day number
  19119. (setq start-day (time-to-days (org-read-date nil t start-day))))
  19120. (setq org-agenda-last-arguments (list include-all start-day ndays))
  19121. (org-compile-prefix-format 'agenda)
  19122. (org-set-sorting-strategy 'agenda)
  19123. (require 'calendar)
  19124. (let* ((org-agenda-start-on-weekday
  19125. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  19126. org-agenda-start-on-weekday nil))
  19127. (thefiles (org-agenda-files))
  19128. (files thefiles)
  19129. (today (time-to-days
  19130. (time-subtract (current-time)
  19131. (list 0 (* 3600 org-extend-today-until) 0))))
  19132. (sd (or start-day today))
  19133. (start (if (or (null org-agenda-start-on-weekday)
  19134. (< org-agenda-ndays 7))
  19135. sd
  19136. (let* ((nt (calendar-day-of-week
  19137. (calendar-gregorian-from-absolute sd)))
  19138. (n1 org-agenda-start-on-weekday)
  19139. (d (- nt n1)))
  19140. (- sd (+ (if (< d 0) 7 0) d)))))
  19141. (day-numbers (list start))
  19142. (day-cnt 0)
  19143. (inhibit-redisplay (not debug-on-error))
  19144. s e rtn rtnall file date d start-pos end-pos todayp nd)
  19145. (setq org-agenda-redo-command
  19146. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  19147. ;; Make the list of days
  19148. (setq ndays (or ndays org-agenda-ndays)
  19149. nd ndays)
  19150. (while (> ndays 1)
  19151. (push (1+ (car day-numbers)) day-numbers)
  19152. (setq ndays (1- ndays)))
  19153. (setq day-numbers (nreverse day-numbers))
  19154. (org-prepare-agenda "Day/Week")
  19155. (org-set-local 'org-starting-day (car day-numbers))
  19156. (org-set-local 'org-include-all-loc include-all)
  19157. (org-set-local 'org-agenda-span
  19158. (org-agenda-ndays-to-span nd))
  19159. (when (and (or include-all org-agenda-include-all-todo)
  19160. (member today day-numbers))
  19161. (setq files thefiles
  19162. rtnall nil)
  19163. (while (setq file (pop files))
  19164. (catch 'nextfile
  19165. (org-check-agenda-file file)
  19166. (setq date (calendar-gregorian-from-absolute today)
  19167. rtn (org-agenda-get-day-entries
  19168. file date :todo))
  19169. (setq rtnall (append rtnall rtn))))
  19170. (when rtnall
  19171. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  19172. (add-text-properties (point-min) (1- (point))
  19173. (list 'face 'org-agenda-structure))
  19174. (insert (org-finalize-agenda-entries rtnall) "\n")))
  19175. (unless org-agenda-compact-blocks
  19176. (setq s (point))
  19177. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  19178. "-agenda:\n")
  19179. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  19180. 'org-date-line t)))
  19181. (while (setq d (pop day-numbers))
  19182. (setq date (calendar-gregorian-from-absolute d)
  19183. s (point))
  19184. (if (or (setq todayp (= d today))
  19185. (and (not start-pos) (= d sd)))
  19186. (setq start-pos (point))
  19187. (if (and start-pos (not end-pos))
  19188. (setq end-pos (point))))
  19189. (setq files thefiles
  19190. rtnall nil)
  19191. (while (setq file (pop files))
  19192. (catch 'nextfile
  19193. (org-check-agenda-file file)
  19194. (if org-agenda-show-log
  19195. (setq rtn (org-agenda-get-day-entries
  19196. file date
  19197. :deadline :scheduled :timestamp :sexp :closed))
  19198. (setq rtn (org-agenda-get-day-entries
  19199. file date
  19200. :deadline :scheduled :sexp :timestamp)))
  19201. (setq rtnall (append rtnall rtn))))
  19202. (if org-agenda-include-diary
  19203. (progn
  19204. (require 'diary-lib)
  19205. (setq rtn (org-get-entries-from-diary date))
  19206. (setq rtnall (append rtnall rtn))))
  19207. (if (or rtnall org-agenda-show-all-dates)
  19208. (progn
  19209. (setq day-cnt (1+ day-cnt))
  19210. (insert
  19211. (if (stringp org-agenda-format-date)
  19212. (format-time-string org-agenda-format-date
  19213. (org-time-from-absolute date))
  19214. (funcall org-agenda-format-date date))
  19215. "\n")
  19216. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  19217. (put-text-property s (1- (point)) 'org-date-line t)
  19218. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  19219. (if todayp (put-text-property s (1- (point)) 'org-today t))
  19220. (if rtnall (insert
  19221. (org-finalize-agenda-entries
  19222. (org-agenda-add-time-grid-maybe
  19223. rtnall nd todayp))
  19224. "\n"))
  19225. (put-text-property s (1- (point)) 'day d)
  19226. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  19227. (goto-char (point-min))
  19228. (org-fit-agenda-window)
  19229. (unless (and (pos-visible-in-window-p (point-min))
  19230. (pos-visible-in-window-p (point-max)))
  19231. (goto-char (1- (point-max)))
  19232. (recenter -1)
  19233. (if (not (pos-visible-in-window-p (or start-pos 1)))
  19234. (progn
  19235. (goto-char (or start-pos 1))
  19236. (recenter 1))))
  19237. (goto-char (or start-pos 1))
  19238. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  19239. (org-finalize-agenda)
  19240. (setq buffer-read-only t)
  19241. (message "")))
  19242. (defun org-agenda-ndays-to-span (n)
  19243. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  19244. ;;; Agenda word search
  19245. (defvar org-agenda-search-history nil)
  19246. ;;;###autoload
  19247. (defun org-search-view (&optional arg string)
  19248. "Show all entries that contain words or regular expressions.
  19249. If the first character of the search string is an asterisks,
  19250. search only the headlines.
  19251. The search string is broken into \"words\" by splitting at whitespace.
  19252. The individual words are then interpreted as a boolean expression with
  19253. logical AND. Words prefixed with a minus must not occur in the entry.
  19254. Words without a prefix or prefixed with a plus must occur in the entry.
  19255. Matching is case-insensitive and the words are enclosed by word delimiters.
  19256. Words enclosed by curly braces are interpreted as regular expressions
  19257. that must or must not match in the entry.
  19258. This command searches the agenda files, and in addition the files listed
  19259. in `org-agenda-text-search-extra-files'."
  19260. (interactive "P")
  19261. (org-compile-prefix-format 'search)
  19262. (org-set-sorting-strategy 'search)
  19263. (org-prepare-agenda "SEARCH")
  19264. (let* ((props (list 'face nil
  19265. 'done-face 'org-done
  19266. 'org-not-done-regexp org-not-done-regexp
  19267. 'org-todo-regexp org-todo-regexp
  19268. 'mouse-face 'highlight
  19269. 'keymap org-agenda-keymap
  19270. 'help-echo (format "mouse-2 or RET jump to location")))
  19271. regexp rtn rtnall files file pos
  19272. marker priority category tags c neg re
  19273. ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
  19274. (unless (and (not arg)
  19275. (stringp string)
  19276. (string-match "\\S-" string))
  19277. (setq string (read-string "[+-]Word/{Regexp} ...: "
  19278. (cond
  19279. ((integerp arg) (cons string arg))
  19280. (arg string))
  19281. 'org-agenda-search-history)))
  19282. (setq org-agenda-redo-command
  19283. (list 'org-search-view 'current-prefix-arg string))
  19284. (setq org-agenda-query-string string)
  19285. (if (equal (string-to-char string) ?*)
  19286. (setq hdl-only t
  19287. words (substring string 1))
  19288. (setq words string))
  19289. (setq words (org-split-string words))
  19290. (mapc (lambda (w)
  19291. (setq c (string-to-char w))
  19292. (if (equal c ?-)
  19293. (setq neg t w (substring w 1))
  19294. (if (equal c ?+)
  19295. (setq neg nil w (substring w 1))
  19296. (setq neg nil)))
  19297. (if (string-match "\\`{.*}\\'" w)
  19298. (setq re (substring w 1 -1))
  19299. (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
  19300. (if neg (push re regexps-) (push re regexps+)))
  19301. words)
  19302. (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
  19303. (if (not regexps+)
  19304. (setq regexp (concat "^" org-outline-regexp))
  19305. (setq regexp (pop regexps+))
  19306. (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
  19307. regexp))))
  19308. (setq files (append (org-agenda-files) org-agenda-text-search-extra-files)
  19309. rtnall nil)
  19310. (while (setq file (pop files))
  19311. (setq ee nil)
  19312. (catch 'nextfile
  19313. (org-check-agenda-file file)
  19314. (setq buffer (if (file-exists-p file)
  19315. (org-get-agenda-file-buffer file)
  19316. (error "No such file %s" file)))
  19317. (if (not buffer)
  19318. ;; If file does not exist, make sure an error message is sent
  19319. (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
  19320. file))))
  19321. (with-current-buffer buffer
  19322. (unless (org-mode-p)
  19323. (error "Agenda file %s is not in `org-mode'" file))
  19324. (let ((case-fold-search t))
  19325. (save-excursion
  19326. (save-restriction
  19327. (if org-agenda-restrict
  19328. (narrow-to-region org-agenda-restrict-begin
  19329. org-agenda-restrict-end)
  19330. (widen))
  19331. (goto-char (point-min))
  19332. (unless (or (org-on-heading-p)
  19333. (outline-next-heading))
  19334. (throw 'nextfile t))
  19335. (goto-char (max (point-min) (1- (point))))
  19336. (while (re-search-forward regexp nil t)
  19337. (org-back-to-heading t)
  19338. (skip-chars-forward "* ")
  19339. (setq beg (point-at-bol)
  19340. beg1 (point)
  19341. end (progn (outline-next-heading) (point)))
  19342. (catch :skip
  19343. (goto-char beg)
  19344. (org-agenda-skip)
  19345. (setq str (buffer-substring-no-properties
  19346. (point-at-bol)
  19347. (if hdl-only (point-at-eol) end)))
  19348. (mapc (lambda (wr) (when (string-match wr str)
  19349. (goto-char (1- end))
  19350. (throw :skip t)))
  19351. regexps-)
  19352. (mapc (lambda (wr) (unless (string-match wr str)
  19353. (goto-char (1- end))
  19354. (throw :skip t)))
  19355. regexps+)
  19356. (goto-char beg)
  19357. (setq marker (org-agenda-new-marker (point))
  19358. category (org-get-category)
  19359. tags (org-get-tags-at (point))
  19360. txt (org-format-agenda-item
  19361. ""
  19362. (buffer-substring-no-properties
  19363. beg1 (point-at-eol))
  19364. category tags))
  19365. (org-add-props txt props
  19366. 'org-marker marker 'org-hd-marker marker
  19367. 'priority 1000 'org-category category
  19368. 'type "search")
  19369. (push txt ee)
  19370. (goto-char (1- end)))))))))
  19371. (setq rtn (nreverse ee))
  19372. (setq rtnall (append rtnall rtn)))
  19373. (if org-agenda-overriding-header
  19374. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19375. nil 'face 'org-agenda-structure) "\n")
  19376. (insert "Search words: ")
  19377. (add-text-properties (point-min) (1- (point))
  19378. (list 'face 'org-agenda-structure))
  19379. (setq pos (point))
  19380. (insert string "\n")
  19381. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19382. (setq pos (point))
  19383. (unless org-agenda-multi
  19384. (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
  19385. (add-text-properties pos (1- (point))
  19386. (list 'face 'org-agenda-structure))))
  19387. (when rtnall
  19388. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19389. (goto-char (point-min))
  19390. (org-fit-agenda-window)
  19391. (add-text-properties (point-min) (point-max) '(org-agenda-type search))
  19392. (org-finalize-agenda)
  19393. (setq buffer-read-only t)))
  19394. ;;; Agenda TODO list
  19395. (defvar org-select-this-todo-keyword nil)
  19396. (defvar org-last-arg nil)
  19397. ;;;###autoload
  19398. (defun org-todo-list (arg)
  19399. "Show all TODO entries from all agenda file in a single list.
  19400. The prefix arg can be used to select a specific TODO keyword and limit
  19401. the list to these. When using \\[universal-argument], you will be prompted
  19402. for a keyword. A numeric prefix directly selects the Nth keyword in
  19403. `org-todo-keywords-1'."
  19404. (interactive "P")
  19405. (require 'calendar)
  19406. (org-compile-prefix-format 'todo)
  19407. (org-set-sorting-strategy 'todo)
  19408. (org-prepare-agenda "TODO")
  19409. (let* ((today (time-to-days (current-time)))
  19410. (date (calendar-gregorian-from-absolute today))
  19411. (kwds org-todo-keywords-for-agenda)
  19412. (completion-ignore-case t)
  19413. (org-select-this-todo-keyword
  19414. (if (stringp arg) arg
  19415. (and arg (integerp arg) (> arg 0)
  19416. (nth (1- arg) kwds))))
  19417. rtn rtnall files file pos)
  19418. (when (equal arg '(4))
  19419. (setq org-select-this-todo-keyword
  19420. (completing-read "Keyword (or KWD1|K2D2|...): "
  19421. (mapcar 'list kwds) nil nil)))
  19422. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  19423. (org-set-local 'org-last-arg arg)
  19424. (setq org-agenda-redo-command
  19425. '(org-todo-list (or current-prefix-arg org-last-arg)))
  19426. (setq files (org-agenda-files)
  19427. rtnall nil)
  19428. (while (setq file (pop files))
  19429. (catch 'nextfile
  19430. (org-check-agenda-file file)
  19431. (setq rtn (org-agenda-get-day-entries file date :todo))
  19432. (setq rtnall (append rtnall rtn))))
  19433. (if org-agenda-overriding-header
  19434. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19435. nil 'face 'org-agenda-structure) "\n")
  19436. (insert "Global list of TODO items of type: ")
  19437. (add-text-properties (point-min) (1- (point))
  19438. (list 'face 'org-agenda-structure))
  19439. (setq pos (point))
  19440. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19441. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19442. (setq pos (point))
  19443. (unless org-agenda-multi
  19444. (insert "Available with `N r': (0)ALL")
  19445. (let ((n 0) s)
  19446. (mapc (lambda (x)
  19447. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19448. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19449. (insert "\n "))
  19450. (insert " " s))
  19451. kwds))
  19452. (insert "\n"))
  19453. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19454. (when rtnall
  19455. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19456. (goto-char (point-min))
  19457. (org-fit-agenda-window)
  19458. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19459. (org-finalize-agenda)
  19460. (setq buffer-read-only t)))
  19461. ;;; Agenda tags match
  19462. ;;;###autoload
  19463. (defun org-tags-view (&optional todo-only match)
  19464. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19465. The prefix arg TODO-ONLY limits the search to TODO entries."
  19466. (interactive "P")
  19467. (org-compile-prefix-format 'tags)
  19468. (org-set-sorting-strategy 'tags)
  19469. (let* ((org-tags-match-list-sublevels
  19470. (if todo-only t org-tags-match-list-sublevels))
  19471. (completion-ignore-case t)
  19472. rtn rtnall files file pos matcher
  19473. buffer)
  19474. (setq matcher (org-make-tags-matcher match)
  19475. match (car matcher) matcher (cdr matcher))
  19476. (org-prepare-agenda (concat "TAGS " match))
  19477. (setq org-agenda-redo-command
  19478. (list 'org-tags-view (list 'quote todo-only)
  19479. (list 'if 'current-prefix-arg nil match)))
  19480. (setq files (org-agenda-files)
  19481. rtnall nil)
  19482. (while (setq file (pop files))
  19483. (catch 'nextfile
  19484. (org-check-agenda-file file)
  19485. (setq buffer (if (file-exists-p file)
  19486. (org-get-agenda-file-buffer file)
  19487. (error "No such file %s" file)))
  19488. (if (not buffer)
  19489. ;; If file does not exist, merror message to agenda
  19490. (setq rtn (list
  19491. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19492. rtnall (append rtnall rtn))
  19493. (with-current-buffer buffer
  19494. (unless (org-mode-p)
  19495. (error "Agenda file %s is not in `org-mode'" file))
  19496. (save-excursion
  19497. (save-restriction
  19498. (if org-agenda-restrict
  19499. (narrow-to-region org-agenda-restrict-begin
  19500. org-agenda-restrict-end)
  19501. (widen))
  19502. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19503. (setq rtnall (append rtnall rtn))))))))
  19504. (if org-agenda-overriding-header
  19505. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19506. nil 'face 'org-agenda-structure) "\n")
  19507. (insert "Headlines with TAGS match: ")
  19508. (add-text-properties (point-min) (1- (point))
  19509. (list 'face 'org-agenda-structure))
  19510. (setq pos (point))
  19511. (insert match "\n")
  19512. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19513. (setq pos (point))
  19514. (unless org-agenda-multi
  19515. (insert "Press `C-u r' to search again with new search string\n"))
  19516. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19517. (when rtnall
  19518. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19519. (goto-char (point-min))
  19520. (org-fit-agenda-window)
  19521. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19522. (org-finalize-agenda)
  19523. (setq buffer-read-only t)))
  19524. ;;; Agenda Finding stuck projects
  19525. (defvar org-agenda-skip-regexp nil
  19526. "Regular expression used in skipping subtrees for the agenda.
  19527. This is basically a temporary global variable that can be set and then
  19528. used by user-defined selections using `org-agenda-skip-function'.")
  19529. (defvar org-agenda-overriding-header nil
  19530. "When this is set during todo and tags searches, will replace header.")
  19531. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19532. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19533. If yes, it returns the end position of this tree, causing agenda commands
  19534. to skip this subtree. This is a function that can be put into
  19535. `org-agenda-skip-function' for the duration of a command."
  19536. (let ((end (save-excursion (org-end-of-subtree t)))
  19537. skip)
  19538. (save-excursion
  19539. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19540. (and skip end)))
  19541. (defun org-agenda-skip-entry-if (&rest conditions)
  19542. "Skip entry if any of CONDITIONS is true.
  19543. See `org-agenda-skip-if' for details."
  19544. (org-agenda-skip-if nil conditions))
  19545. (defun org-agenda-skip-subtree-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 t conditions))
  19549. (defun org-agenda-skip-if (subtree conditions)
  19550. "Checks current entity for CONDITIONS.
  19551. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19552. the entry, i.e. the text before the next heading is checked.
  19553. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19554. from different tests. Valid conditions are:
  19555. scheduled Check if there is a scheduled cookie
  19556. notscheduled Check if there is no scheduled cookie
  19557. deadline Check if there is a deadline
  19558. notdeadline Check if there is no deadline
  19559. regexp Check if regexp matches
  19560. notregexp Check if regexp does not match.
  19561. The regexp is taken from the conditions list, it must come right after
  19562. the `regexp' or `notregexp' element.
  19563. If any of these conditions is met, this function returns the end point of
  19564. the entity, causing the search to continue from there. This is a function
  19565. that can be put into `org-agenda-skip-function' for the duration of a command."
  19566. (let (beg end m)
  19567. (org-back-to-heading t)
  19568. (setq beg (point)
  19569. end (if subtree
  19570. (progn (org-end-of-subtree t) (point))
  19571. (progn (outline-next-heading) (1- (point)))))
  19572. (goto-char beg)
  19573. (and
  19574. (or
  19575. (and (memq 'scheduled conditions)
  19576. (re-search-forward org-scheduled-time-regexp end t))
  19577. (and (memq 'notscheduled conditions)
  19578. (not (re-search-forward org-scheduled-time-regexp end t)))
  19579. (and (memq 'deadline conditions)
  19580. (re-search-forward org-deadline-time-regexp end t))
  19581. (and (memq 'notdeadline conditions)
  19582. (not (re-search-forward org-deadline-time-regexp end t)))
  19583. (and (setq m (memq 'regexp conditions))
  19584. (stringp (nth 1 m))
  19585. (re-search-forward (nth 1 m) end t))
  19586. (and (setq m (memq 'notregexp conditions))
  19587. (stringp (nth 1 m))
  19588. (not (re-search-forward (nth 1 m) end t))))
  19589. end)))
  19590. ;;;###autoload
  19591. (defun org-agenda-list-stuck-projects (&rest ignore)
  19592. "Create agenda view for projects that are stuck.
  19593. Stuck projects are project that have no next actions. For the definitions
  19594. of what a project is and how to check if it stuck, customize the variable
  19595. `org-stuck-projects'.
  19596. MATCH is being ignored."
  19597. (interactive)
  19598. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19599. ;; FIXME: we could have used org-agenda-skip-if here.
  19600. (org-agenda-overriding-header "List of stuck projects: ")
  19601. (matcher (nth 0 org-stuck-projects))
  19602. (todo (nth 1 org-stuck-projects))
  19603. (todo-wds (if (member "*" todo)
  19604. (progn
  19605. (org-prepare-agenda-buffers (org-agenda-files))
  19606. (org-delete-all
  19607. org-done-keywords-for-agenda
  19608. (copy-sequence org-todo-keywords-for-agenda)))
  19609. todo))
  19610. (todo-re (concat "^\\*+[ \t]+\\("
  19611. (mapconcat 'identity todo-wds "\\|")
  19612. "\\)\\>"))
  19613. (tags (nth 2 org-stuck-projects))
  19614. (tags-re (if (member "*" tags)
  19615. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19616. (concat "^\\*+ .*:\\("
  19617. (mapconcat 'identity tags "\\|")
  19618. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19619. (gen-re (nth 3 org-stuck-projects))
  19620. (re-list
  19621. (delq nil
  19622. (list
  19623. (if todo todo-re)
  19624. (if tags tags-re)
  19625. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19626. gen-re)))))
  19627. (setq org-agenda-skip-regexp
  19628. (if re-list
  19629. (mapconcat 'identity re-list "\\|")
  19630. (error "No information how to identify unstuck projects")))
  19631. (org-tags-view nil matcher)
  19632. (with-current-buffer org-agenda-buffer-name
  19633. (setq org-agenda-redo-command
  19634. '(org-agenda-list-stuck-projects
  19635. (or current-prefix-arg org-last-arg))))))
  19636. ;;; Diary integration
  19637. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19638. (defun org-get-entries-from-diary (date)
  19639. "Get the (Emacs Calendar) diary entries for DATE."
  19640. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19641. (diary-display-hook '(fancy-diary-display))
  19642. (pop-up-frames nil)
  19643. (list-diary-entries-hook
  19644. (cons 'org-diary-default-entry list-diary-entries-hook))
  19645. (diary-file-name-prefix-function nil) ; turn this feature off
  19646. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19647. entries
  19648. (org-disable-agenda-to-diary t))
  19649. (save-excursion
  19650. (save-window-excursion
  19651. (funcall (if (fboundp 'diary-list-entries)
  19652. 'diary-list-entries 'list-diary-entries)
  19653. date 1)))
  19654. (if (not (get-buffer fancy-diary-buffer))
  19655. (setq entries nil)
  19656. (with-current-buffer fancy-diary-buffer
  19657. (setq buffer-read-only nil)
  19658. (if (zerop (buffer-size))
  19659. ;; No entries
  19660. (setq entries nil)
  19661. ;; Omit the date and other unnecessary stuff
  19662. (org-agenda-cleanup-fancy-diary)
  19663. ;; Add prefix to each line and extend the text properties
  19664. (if (zerop (buffer-size))
  19665. (setq entries nil)
  19666. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19667. (set-buffer-modified-p nil)
  19668. (kill-buffer fancy-diary-buffer)))
  19669. (when entries
  19670. (setq entries (org-split-string entries "\n"))
  19671. (setq entries
  19672. (mapcar
  19673. (lambda (x)
  19674. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19675. ;; Extend the text properties to the beginning of the line
  19676. (org-add-props x (text-properties-at (1- (length x)) x)
  19677. 'type "diary" 'date date))
  19678. entries)))))
  19679. (defun org-agenda-cleanup-fancy-diary ()
  19680. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19681. This gets rid of the date, the underline under the date, and
  19682. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19683. date. It also removes lines that contain only whitespace."
  19684. (goto-char (point-min))
  19685. (if (looking-at ".*?:[ \t]*")
  19686. (progn
  19687. (replace-match "")
  19688. (re-search-forward "\n=+$" nil t)
  19689. (replace-match "")
  19690. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19691. (re-search-forward "\n=+$" nil t)
  19692. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19693. (goto-char (point-min))
  19694. (while (re-search-forward "^ +\n" nil t)
  19695. (replace-match ""))
  19696. (goto-char (point-min))
  19697. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19698. (replace-match "")))
  19699. ;; Make sure entries from the diary have the right text properties.
  19700. (eval-after-load "diary-lib"
  19701. '(if (boundp 'diary-modify-entry-list-string-function)
  19702. ;; We can rely on the hook, nothing to do
  19703. nil
  19704. ;; Hook not avaiable, must use advice to make this work
  19705. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19706. "Make the position visible."
  19707. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19708. (stringp string)
  19709. buffer-file-name)
  19710. (setq string (org-modify-diary-entry-string string))))))
  19711. (defun org-modify-diary-entry-string (string)
  19712. "Add text properties to string, allowing org-mode to act on it."
  19713. (org-add-props string nil
  19714. 'mouse-face 'highlight
  19715. 'keymap org-agenda-keymap
  19716. 'help-echo (if buffer-file-name
  19717. (format "mouse-2 or RET jump to diary file %s"
  19718. (abbreviate-file-name buffer-file-name))
  19719. "")
  19720. 'org-agenda-diary-link t
  19721. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19722. (defun org-diary-default-entry ()
  19723. "Add a dummy entry to the diary.
  19724. Needed to avoid empty dates which mess up holiday display."
  19725. ;; Catch the error if dealing with the new add-to-diary-alist
  19726. (when org-disable-agenda-to-diary
  19727. (condition-case nil
  19728. (add-to-diary-list original-date "Org-mode dummy" "")
  19729. (error
  19730. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19731. ;;;###autoload
  19732. (defun org-diary (&rest args)
  19733. "Return diary information from org-files.
  19734. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19735. It accesses org files and extracts information from those files to be
  19736. listed in the diary. The function accepts arguments specifying what
  19737. items should be listed. The following arguments are allowed:
  19738. :timestamp List the headlines of items containing a date stamp or
  19739. date range matching the selected date. Deadlines will
  19740. also be listed, on the expiration day.
  19741. :sexp List entries resulting from diary-like sexps.
  19742. :deadline List any deadlines past due, or due within
  19743. `org-deadline-warning-days'. The listing occurs only
  19744. in the diary for *today*, not at any other date. If
  19745. an entry is marked DONE, it is no longer listed.
  19746. :scheduled List all items which are scheduled for the given date.
  19747. The diary for *today* also contains items which were
  19748. scheduled earlier and are not yet marked DONE.
  19749. :todo List all TODO items from the org-file. This may be a
  19750. long list - so this is not turned on by default.
  19751. Like deadlines, these entries only show up in the
  19752. diary for *today*, not at any other date.
  19753. The call in the diary file should look like this:
  19754. &%%(org-diary) ~/path/to/some/orgfile.org
  19755. Use a separate line for each org file to check. Or, if you omit the file name,
  19756. all files listed in `org-agenda-files' will be checked automatically:
  19757. &%%(org-diary)
  19758. If you don't give any arguments (as in the example above), the default
  19759. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19760. So the example above may also be written as
  19761. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19762. The function expects the lisp variables `entry' and `date' to be provided
  19763. by the caller, because this is how the calendar works. Don't use this
  19764. function from a program - use `org-agenda-get-day-entries' instead."
  19765. (when (> (- (time-to-seconds (current-time))
  19766. org-agenda-last-marker-time)
  19767. 5)
  19768. (org-agenda-reset-markers))
  19769. (org-compile-prefix-format 'agenda)
  19770. (org-set-sorting-strategy 'agenda)
  19771. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19772. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19773. (list entry)
  19774. (org-agenda-files t)))
  19775. file rtn results)
  19776. (org-prepare-agenda-buffers files)
  19777. ;; If this is called during org-agenda, don't return any entries to
  19778. ;; the calendar. Org Agenda will list these entries itself.
  19779. (if org-disable-agenda-to-diary (setq files nil))
  19780. (while (setq file (pop files))
  19781. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19782. (setq results (append results rtn)))
  19783. (if results
  19784. (concat (org-finalize-agenda-entries results) "\n"))))
  19785. ;;; Agenda entry finders
  19786. (defun org-agenda-get-day-entries (file date &rest args)
  19787. "Does the work for `org-diary' and `org-agenda'.
  19788. FILE is the path to a file to be checked for entries. DATE is date like
  19789. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19790. which kind of entries should be extracted. For details about these, see
  19791. the documentation of `org-diary'."
  19792. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19793. (let* ((org-startup-folded nil)
  19794. (org-startup-align-all-tables nil)
  19795. (buffer (if (file-exists-p file)
  19796. (org-get-agenda-file-buffer file)
  19797. (error "No such file %s" file)))
  19798. arg results rtn)
  19799. (if (not buffer)
  19800. ;; If file does not exist, make sure an error message ends up in diary
  19801. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19802. (with-current-buffer buffer
  19803. (unless (org-mode-p)
  19804. (error "Agenda file %s is not in `org-mode'" file))
  19805. (let ((case-fold-search nil))
  19806. (save-excursion
  19807. (save-restriction
  19808. (if org-agenda-restrict
  19809. (narrow-to-region org-agenda-restrict-begin
  19810. org-agenda-restrict-end)
  19811. (widen))
  19812. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19813. (while (setq arg (pop args))
  19814. (cond
  19815. ((and (eq arg :todo)
  19816. (equal date (calendar-current-date)))
  19817. (setq rtn (org-agenda-get-todos))
  19818. (setq results (append results rtn)))
  19819. ((eq arg :timestamp)
  19820. (setq rtn (org-agenda-get-blocks))
  19821. (setq results (append results rtn))
  19822. (setq rtn (org-agenda-get-timestamps))
  19823. (setq results (append results rtn)))
  19824. ((eq arg :sexp)
  19825. (setq rtn (org-agenda-get-sexps))
  19826. (setq results (append results rtn)))
  19827. ((eq arg :scheduled)
  19828. (setq rtn (org-agenda-get-scheduled))
  19829. (setq results (append results rtn)))
  19830. ((eq arg :closed)
  19831. (setq rtn (org-agenda-get-closed))
  19832. (setq results (append results rtn)))
  19833. ((eq arg :deadline)
  19834. (setq rtn (org-agenda-get-deadlines))
  19835. (setq results (append results rtn))))))))
  19836. results))))
  19837. (defun org-entry-is-todo-p ()
  19838. (member (org-get-todo-state) org-not-done-keywords))
  19839. (defun org-entry-is-done-p ()
  19840. (member (org-get-todo-state) org-done-keywords))
  19841. (defun org-get-todo-state ()
  19842. (save-excursion
  19843. (org-back-to-heading t)
  19844. (and (looking-at org-todo-line-regexp)
  19845. (match-end 2)
  19846. (match-string 2))))
  19847. (defun org-at-date-range-p (&optional inactive-ok)
  19848. "Is the cursor inside a date range?"
  19849. (interactive)
  19850. (save-excursion
  19851. (catch 'exit
  19852. (let ((pos (point)))
  19853. (skip-chars-backward "^[<\r\n")
  19854. (skip-chars-backward "<[")
  19855. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19856. (>= (match-end 0) pos)
  19857. (throw 'exit t))
  19858. (skip-chars-backward "^<[\r\n")
  19859. (skip-chars-backward "<[")
  19860. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19861. (>= (match-end 0) pos)
  19862. (throw 'exit t)))
  19863. nil)))
  19864. (defun org-agenda-get-todos ()
  19865. "Return the TODO information for agenda display."
  19866. (let* ((props (list 'face nil
  19867. 'done-face 'org-done
  19868. 'org-not-done-regexp org-not-done-regexp
  19869. 'org-todo-regexp org-todo-regexp
  19870. 'mouse-face 'highlight
  19871. 'keymap org-agenda-keymap
  19872. 'help-echo
  19873. (format "mouse-2 or RET jump to org file %s"
  19874. (abbreviate-file-name buffer-file-name))))
  19875. ;; FIXME: get rid of the \n at some point but watch out
  19876. (regexp (concat "^\\*+[ \t]+\\("
  19877. (if org-select-this-todo-keyword
  19878. (if (equal org-select-this-todo-keyword "*")
  19879. org-todo-regexp
  19880. (concat "\\<\\("
  19881. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19882. "\\)\\>"))
  19883. org-not-done-regexp)
  19884. "[^\n\r]*\\)"))
  19885. marker priority category tags
  19886. ee txt beg end)
  19887. (goto-char (point-min))
  19888. (while (re-search-forward regexp nil t)
  19889. (catch :skip
  19890. (save-match-data
  19891. (beginning-of-line)
  19892. (setq beg (point) end (progn (outline-next-heading) (point)))
  19893. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19894. (re-search-forward org-ts-regexp end t))
  19895. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19896. (re-search-forward org-scheduled-time-regexp end t))
  19897. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19898. (re-search-forward org-deadline-time-regexp end t)
  19899. (org-deadline-close (match-string 1))))
  19900. (goto-char (1+ beg))
  19901. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19902. (throw :skip nil)))
  19903. (goto-char beg)
  19904. (org-agenda-skip)
  19905. (goto-char (match-beginning 1))
  19906. (setq marker (org-agenda-new-marker (match-beginning 0))
  19907. category (org-get-category)
  19908. tags (org-get-tags-at (point))
  19909. txt (org-format-agenda-item "" (match-string 1) category tags)
  19910. priority (1+ (org-get-priority txt)))
  19911. (org-add-props txt props
  19912. 'org-marker marker 'org-hd-marker marker
  19913. 'priority priority 'org-category category
  19914. 'type "todo")
  19915. (push txt ee)
  19916. (if org-agenda-todo-list-sublevels
  19917. (goto-char (match-end 1))
  19918. (org-end-of-subtree 'invisible))))
  19919. (nreverse ee)))
  19920. (defconst org-agenda-no-heading-message
  19921. "No heading for this item in buffer or region.")
  19922. (defun org-agenda-get-timestamps ()
  19923. "Return the date stamp information for agenda display."
  19924. (let* ((props (list 'face nil
  19925. 'org-not-done-regexp org-not-done-regexp
  19926. 'org-todo-regexp org-todo-regexp
  19927. 'mouse-face 'highlight
  19928. 'keymap org-agenda-keymap
  19929. 'help-echo
  19930. (format "mouse-2 or RET jump to org file %s"
  19931. (abbreviate-file-name buffer-file-name))))
  19932. (d1 (calendar-absolute-from-gregorian date))
  19933. (remove-re
  19934. (concat
  19935. (regexp-quote
  19936. (format-time-string
  19937. "<%Y-%m-%d"
  19938. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19939. ".*?>"))
  19940. (regexp
  19941. (concat
  19942. (regexp-quote
  19943. (substring
  19944. (format-time-string
  19945. (car org-time-stamp-formats)
  19946. (apply 'encode-time ; DATE bound by calendar
  19947. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19948. 0 11))
  19949. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19950. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19951. marker hdmarker deadlinep scheduledp donep tmp priority category
  19952. ee txt timestr tags b0 b3 e3 head)
  19953. (goto-char (point-min))
  19954. (while (re-search-forward regexp nil t)
  19955. (setq b0 (match-beginning 0)
  19956. b3 (match-beginning 3) e3 (match-end 3))
  19957. (catch :skip
  19958. (and (org-at-date-range-p) (throw :skip nil))
  19959. (org-agenda-skip)
  19960. (if (and (match-end 1)
  19961. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19962. (throw :skip nil))
  19963. (if (and e3
  19964. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19965. (throw :skip nil))
  19966. (setq marker (org-agenda-new-marker b0)
  19967. category (org-get-category b0)
  19968. tmp (buffer-substring (max (point-min)
  19969. (- b0 org-ds-keyword-length))
  19970. b0)
  19971. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19972. deadlinep (string-match org-deadline-regexp tmp)
  19973. scheduledp (string-match org-scheduled-regexp tmp)
  19974. donep (org-entry-is-done-p))
  19975. (if (or scheduledp deadlinep) (throw :skip t))
  19976. (if (string-match ">" timestr)
  19977. ;; substring should only run to end of time stamp
  19978. (setq timestr (substring timestr 0 (match-end 0))))
  19979. (save-excursion
  19980. (if (re-search-backward "^\\*+ " nil t)
  19981. (progn
  19982. (goto-char (match-beginning 0))
  19983. (setq hdmarker (org-agenda-new-marker)
  19984. tags (org-get-tags-at))
  19985. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19986. (setq head (match-string 1))
  19987. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19988. (setq txt (org-format-agenda-item
  19989. nil head category tags timestr nil
  19990. remove-re)))
  19991. (setq txt org-agenda-no-heading-message))
  19992. (setq priority (org-get-priority txt))
  19993. (org-add-props txt props
  19994. 'org-marker marker 'org-hd-marker hdmarker)
  19995. (org-add-props txt nil 'priority priority
  19996. 'org-category category 'date date
  19997. 'type "timestamp")
  19998. (push txt ee))
  19999. (outline-next-heading)))
  20000. (nreverse ee)))
  20001. (defun org-agenda-get-sexps ()
  20002. "Return the sexp information for agenda display."
  20003. (require 'diary-lib)
  20004. (let* ((props (list 'face nil
  20005. 'mouse-face 'highlight
  20006. 'keymap org-agenda-keymap
  20007. 'help-echo
  20008. (format "mouse-2 or RET jump to org file %s"
  20009. (abbreviate-file-name buffer-file-name))))
  20010. (regexp "^&?%%(")
  20011. marker category ee txt tags entry result beg b sexp sexp-entry)
  20012. (goto-char (point-min))
  20013. (while (re-search-forward regexp nil t)
  20014. (catch :skip
  20015. (org-agenda-skip)
  20016. (setq beg (match-beginning 0))
  20017. (goto-char (1- (match-end 0)))
  20018. (setq b (point))
  20019. (forward-sexp 1)
  20020. (setq sexp (buffer-substring b (point)))
  20021. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  20022. (org-trim (match-string 1))
  20023. ""))
  20024. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  20025. (when result
  20026. (setq marker (org-agenda-new-marker beg)
  20027. category (org-get-category beg))
  20028. (if (string-match "\\S-" result)
  20029. (setq txt result)
  20030. (setq txt "SEXP entry returned empty string"))
  20031. (setq txt (org-format-agenda-item
  20032. "" txt category tags 'time))
  20033. (org-add-props txt props 'org-marker marker)
  20034. (org-add-props txt nil
  20035. 'org-category category 'date date
  20036. 'type "sexp")
  20037. (push txt ee))))
  20038. (nreverse ee)))
  20039. (defun org-agenda-get-closed ()
  20040. "Return the logged TODO entries for agenda display."
  20041. (let* ((props (list 'mouse-face 'highlight
  20042. 'org-not-done-regexp org-not-done-regexp
  20043. 'org-todo-regexp org-todo-regexp
  20044. 'keymap org-agenda-keymap
  20045. 'help-echo
  20046. (format "mouse-2 or RET jump to org file %s"
  20047. (abbreviate-file-name buffer-file-name))))
  20048. (regexp (concat
  20049. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  20050. (regexp-quote
  20051. (substring
  20052. (format-time-string
  20053. (car org-time-stamp-formats)
  20054. (apply 'encode-time ; DATE bound by calendar
  20055. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  20056. 1 11))))
  20057. marker hdmarker priority category tags closedp
  20058. ee txt timestr)
  20059. (goto-char (point-min))
  20060. (while (re-search-forward regexp nil t)
  20061. (catch :skip
  20062. (org-agenda-skip)
  20063. (setq marker (org-agenda-new-marker (match-beginning 0))
  20064. closedp (equal (match-string 1) org-closed-string)
  20065. category (org-get-category (match-beginning 0))
  20066. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  20067. ;; donep (org-entry-is-done-p)
  20068. )
  20069. (if (string-match "\\]" timestr)
  20070. ;; substring should only run to end of time stamp
  20071. (setq timestr (substring timestr 0 (match-end 0))))
  20072. (save-excursion
  20073. (if (re-search-backward "^\\*+ " nil t)
  20074. (progn
  20075. (goto-char (match-beginning 0))
  20076. (setq hdmarker (org-agenda-new-marker)
  20077. tags (org-get-tags-at))
  20078. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20079. (setq txt (org-format-agenda-item
  20080. (if closedp "Closed: " "Clocked: ")
  20081. (match-string 1) category tags timestr)))
  20082. (setq txt org-agenda-no-heading-message))
  20083. (setq priority 100000)
  20084. (org-add-props txt props
  20085. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  20086. 'priority priority 'org-category category
  20087. 'type "closed" 'date date
  20088. 'undone-face 'org-warning 'done-face 'org-done)
  20089. (push txt ee))
  20090. (goto-char (point-at-eol))))
  20091. (nreverse ee)))
  20092. (defun org-agenda-get-deadlines ()
  20093. "Return the deadline information for agenda display."
  20094. (let* ((props (list 'mouse-face 'highlight
  20095. 'org-not-done-regexp org-not-done-regexp
  20096. 'org-todo-regexp org-todo-regexp
  20097. 'keymap org-agenda-keymap
  20098. 'help-echo
  20099. (format "mouse-2 or RET jump to org file %s"
  20100. (abbreviate-file-name buffer-file-name))))
  20101. (regexp org-deadline-time-regexp)
  20102. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20103. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20104. d2 diff dfrac wdays pos pos1 category tags
  20105. ee txt head face s upcomingp donep timestr)
  20106. (goto-char (point-min))
  20107. (while (re-search-forward regexp nil t)
  20108. (catch :skip
  20109. (org-agenda-skip)
  20110. (setq s (match-string 1)
  20111. pos (1- (match-beginning 1))
  20112. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20113. diff (- d2 d1)
  20114. wdays (org-get-wdays s)
  20115. dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
  20116. upcomingp (and todayp (> diff 0)))
  20117. ;; When to show a deadline in the calendar:
  20118. ;; If the expiration is within wdays warning time.
  20119. ;; Past-due deadlines are only shown on the current date
  20120. (if (or (and (<= diff wdays)
  20121. (and todayp (not org-agenda-only-exact-dates)))
  20122. (= diff 0))
  20123. (save-excursion
  20124. (setq category (org-get-category))
  20125. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20126. (progn
  20127. (goto-char (match-end 0))
  20128. (setq pos1 (match-beginning 0))
  20129. (setq tags (org-get-tags-at pos1))
  20130. (setq head (buffer-substring-no-properties
  20131. (point)
  20132. (progn (skip-chars-forward "^\r\n")
  20133. (point))))
  20134. (setq donep (string-match org-looking-at-done-regexp head))
  20135. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20136. (setq timestr
  20137. (concat (substring s (match-beginning 1)) " "))
  20138. (setq timestr 'time))
  20139. (if (and donep
  20140. (or org-agenda-skip-deadline-if-done
  20141. (not (= diff 0))))
  20142. (setq txt nil)
  20143. (setq txt (org-format-agenda-item
  20144. (if (= diff 0)
  20145. (car org-agenda-deadline-leaders)
  20146. (format (nth 1 org-agenda-deadline-leaders)
  20147. diff))
  20148. head category tags timestr))))
  20149. (setq txt org-agenda-no-heading-message))
  20150. (when txt
  20151. (setq face (org-agenda-deadline-face dfrac wdays))
  20152. (org-add-props txt props
  20153. 'org-marker (org-agenda-new-marker pos)
  20154. 'org-hd-marker (org-agenda-new-marker pos1)
  20155. 'priority (+ (- diff)
  20156. (org-get-priority txt))
  20157. 'org-category category
  20158. 'type (if upcomingp "upcoming-deadline" "deadline")
  20159. 'date (if upcomingp date d2)
  20160. 'face (if donep 'org-done face)
  20161. 'undone-face face 'done-face 'org-done)
  20162. (push txt ee))))))
  20163. (nreverse ee)))
  20164. (defun org-agenda-deadline-face (fraction &optional wdays)
  20165. "Return the face to displaying a deadline item.
  20166. FRACTION is what fraction of the head-warning time has passed."
  20167. (if (equal wdays 0) (setq fraction 1.))
  20168. (let ((faces org-agenda-deadline-faces) f)
  20169. (catch 'exit
  20170. (while (setq f (pop faces))
  20171. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  20172. (defun org-agenda-get-scheduled ()
  20173. "Return the scheduled information for agenda display."
  20174. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  20175. 'org-todo-regexp org-todo-regexp
  20176. 'done-face 'org-done
  20177. 'mouse-face 'highlight
  20178. 'keymap org-agenda-keymap
  20179. 'help-echo
  20180. (format "mouse-2 or RET jump to org file %s"
  20181. (abbreviate-file-name buffer-file-name))))
  20182. (regexp org-scheduled-time-regexp)
  20183. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  20184. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  20185. d2 diff pos pos1 category tags
  20186. ee txt head pastschedp donep face timestr s)
  20187. (goto-char (point-min))
  20188. (while (re-search-forward regexp nil t)
  20189. (catch :skip
  20190. (org-agenda-skip)
  20191. (setq s (match-string 1)
  20192. pos (1- (match-beginning 1))
  20193. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  20194. ;;; is this right?
  20195. ;;; do we need to do this for deadleine too????
  20196. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  20197. diff (- d2 d1))
  20198. (setq pastschedp (and todayp (< diff 0)))
  20199. ;; When to show a scheduled item in the calendar:
  20200. ;; If it is on or past the date.
  20201. (if (or (and (< diff 0)
  20202. (and todayp (not org-agenda-only-exact-dates)))
  20203. (= diff 0))
  20204. (save-excursion
  20205. (setq category (org-get-category))
  20206. (if (re-search-backward "^\\*+[ \t]+" nil t)
  20207. (progn
  20208. (goto-char (match-end 0))
  20209. (setq pos1 (match-beginning 0))
  20210. (setq tags (org-get-tags-at))
  20211. (setq head (buffer-substring-no-properties
  20212. (point)
  20213. (progn (skip-chars-forward "^\r\n") (point))))
  20214. (setq donep (string-match org-looking-at-done-regexp head))
  20215. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  20216. (setq timestr
  20217. (concat (substring s (match-beginning 1)) " "))
  20218. (setq timestr 'time))
  20219. (if (and donep
  20220. (or org-agenda-skip-scheduled-if-done
  20221. (not (= diff 0))))
  20222. (setq txt nil)
  20223. (setq txt (org-format-agenda-item
  20224. (if (= diff 0)
  20225. (car org-agenda-scheduled-leaders)
  20226. (format (nth 1 org-agenda-scheduled-leaders)
  20227. (- 1 diff)))
  20228. head category tags timestr))))
  20229. (setq txt org-agenda-no-heading-message))
  20230. (when txt
  20231. (setq face (if pastschedp
  20232. 'org-scheduled-previously
  20233. 'org-scheduled-today))
  20234. (org-add-props txt props
  20235. 'undone-face face
  20236. 'face (if donep 'org-done face)
  20237. 'org-marker (org-agenda-new-marker pos)
  20238. 'org-hd-marker (org-agenda-new-marker pos1)
  20239. 'type (if pastschedp "past-scheduled" "scheduled")
  20240. 'date (if pastschedp d2 date)
  20241. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  20242. 'org-category category)
  20243. (push txt ee))))))
  20244. (nreverse ee)))
  20245. (defun org-agenda-get-blocks ()
  20246. "Return the date-range information for agenda display."
  20247. (let* ((props (list 'face nil
  20248. 'org-not-done-regexp org-not-done-regexp
  20249. 'org-todo-regexp org-todo-regexp
  20250. 'mouse-face 'highlight
  20251. 'keymap org-agenda-keymap
  20252. 'help-echo
  20253. (format "mouse-2 or RET jump to org file %s"
  20254. (abbreviate-file-name buffer-file-name))))
  20255. (regexp org-tr-regexp)
  20256. (d0 (calendar-absolute-from-gregorian date))
  20257. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  20258. donep head)
  20259. (goto-char (point-min))
  20260. (while (re-search-forward regexp nil t)
  20261. (catch :skip
  20262. (org-agenda-skip)
  20263. (setq pos (point))
  20264. (setq timestr (match-string 0)
  20265. s1 (match-string 1)
  20266. s2 (match-string 2)
  20267. d1 (time-to-days (org-time-string-to-time s1))
  20268. d2 (time-to-days (org-time-string-to-time s2)))
  20269. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  20270. ;; Only allow days between the limits, because the normal
  20271. ;; date stamps will catch the limits.
  20272. (save-excursion
  20273. (setq marker (org-agenda-new-marker (point)))
  20274. (setq category (org-get-category))
  20275. (if (re-search-backward "^\\*+ " nil t)
  20276. (progn
  20277. (goto-char (match-beginning 0))
  20278. (setq hdmarker (org-agenda-new-marker (point)))
  20279. (setq tags (org-get-tags-at))
  20280. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  20281. (setq head (match-string 1))
  20282. (and org-agenda-skip-timestamp-if-done
  20283. (org-entry-is-done-p)
  20284. (throw :skip t))
  20285. (setq txt (org-format-agenda-item
  20286. (format (if (= d1 d2) "" "(%d/%d): ")
  20287. (1+ (- d0 d1)) (1+ (- d2 d1)))
  20288. head category tags
  20289. (if (= d0 d1) timestr))))
  20290. (setq txt org-agenda-no-heading-message))
  20291. (org-add-props txt props
  20292. 'org-marker marker 'org-hd-marker hdmarker
  20293. 'type "block" 'date date
  20294. 'priority (org-get-priority txt) 'org-category category)
  20295. (push txt ee)))
  20296. (goto-char pos)))
  20297. ;; Sort the entries by expiration date.
  20298. (nreverse ee)))
  20299. ;;; Agenda presentation and sorting
  20300. (defconst org-plain-time-of-day-regexp
  20301. (concat
  20302. "\\(\\<[012]?[0-9]"
  20303. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20304. "\\(--?"
  20305. "\\(\\<[012]?[0-9]"
  20306. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20307. "\\)?")
  20308. "Regular expression to match a plain time or time range.
  20309. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20310. groups carry important information:
  20311. 0 the full match
  20312. 1 the first time, range or not
  20313. 8 the second time, if it is a range.")
  20314. (defconst org-plain-time-extension-regexp
  20315. (concat
  20316. "\\(\\<[012]?[0-9]"
  20317. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  20318. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  20319. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  20320. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  20321. groups carry important information:
  20322. 0 the full match
  20323. 7 hours of duration
  20324. 9 minutes of duration")
  20325. (defconst org-stamp-time-of-day-regexp
  20326. (concat
  20327. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  20328. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  20329. "\\(--?"
  20330. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  20331. "Regular expression to match a timestamp time or time range.
  20332. After a match, the following groups carry important information:
  20333. 0 the full match
  20334. 1 date plus weekday, for backreferencing to make sure both times on same day
  20335. 2 the first time, range or not
  20336. 4 the second time, if it is a range.")
  20337. (defvar org-prefix-has-time nil
  20338. "A flag, set by `org-compile-prefix-format'.
  20339. The flag is set if the currently compiled format contains a `%t'.")
  20340. (defvar org-prefix-has-tag nil
  20341. "A flag, set by `org-compile-prefix-format'.
  20342. The flag is set if the currently compiled format contains a `%T'.")
  20343. (defun org-format-agenda-item (extra txt &optional category tags dotime
  20344. noprefix remove-re)
  20345. "Format TXT to be inserted into the agenda buffer.
  20346. In particular, it adds the prefix and corresponding text properties. EXTRA
  20347. must be a string and replaces the `%s' specifier in the prefix format.
  20348. CATEGORY (string, symbol or nil) may be used to overrule the default
  20349. category taken from local variable or file name. It will replace the `%c'
  20350. specifier in the format. DOTIME, when non-nil, indicates that a
  20351. time-of-day should be extracted from TXT for sorting of this entry, and for
  20352. the `%t' specifier in the format. When DOTIME is a string, this string is
  20353. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  20354. only the correctly processes TXT should be returned - this is used by
  20355. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  20356. Any match of REMOVE-RE will be removed from TXT."
  20357. (save-match-data
  20358. ;; Diary entries sometimes have extra whitespace at the beginning
  20359. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  20360. (let* ((category (or category
  20361. org-category
  20362. (if buffer-file-name
  20363. (file-name-sans-extension
  20364. (file-name-nondirectory buffer-file-name))
  20365. "")))
  20366. (tag (if tags (nth (1- (length tags)) tags) ""))
  20367. time ; time and tag are needed for the eval of the prefix format
  20368. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  20369. (time-of-day (and dotime (org-get-time-of-day ts)))
  20370. stamp plain s0 s1 s2 rtn srp)
  20371. (when (and dotime time-of-day org-prefix-has-time)
  20372. ;; Extract starting and ending time and move them to prefix
  20373. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  20374. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  20375. (setq s0 (match-string 0 ts)
  20376. srp (and stamp (match-end 3))
  20377. s1 (match-string (if plain 1 2) ts)
  20378. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  20379. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  20380. ;; them, we might want to remove them there to avoid duplication.
  20381. ;; The user can turn this off with a variable.
  20382. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  20383. (string-match (concat (regexp-quote s0) " *") txt)
  20384. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  20385. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  20386. (= (match-beginning 0) 0)
  20387. t))
  20388. (setq txt (replace-match "" nil nil txt))))
  20389. ;; Normalize the time(s) to 24 hour
  20390. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  20391. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  20392. (when (and s1 (not s2) org-agenda-default-appointment-duration
  20393. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  20394. (let ((m (+ (string-to-number (match-string 2 s1))
  20395. (* 60 (string-to-number (match-string 1 s1)))
  20396. org-agenda-default-appointment-duration))
  20397. h)
  20398. (setq h (/ m 60) m (- m (* h 60)))
  20399. (setq s2 (format "%02d:%02d" h m))))
  20400. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20401. txt)
  20402. ;; Tags are in the string
  20403. (if (or (eq org-agenda-remove-tags t)
  20404. (and org-agenda-remove-tags
  20405. org-prefix-has-tag))
  20406. (setq txt (replace-match "" t t txt))
  20407. (setq txt (replace-match
  20408. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  20409. (match-string 2 txt))
  20410. t t txt))))
  20411. (when remove-re
  20412. (while (string-match remove-re txt)
  20413. (setq txt (replace-match "" t t txt))))
  20414. ;; Create the final string
  20415. (if noprefix
  20416. (setq rtn txt)
  20417. ;; Prepare the variables needed in the eval of the compiled format
  20418. (setq time (cond (s2 (concat s1 "-" s2))
  20419. (s1 (concat s1 "......"))
  20420. (t ""))
  20421. extra (or extra "")
  20422. category (if (symbolp category) (symbol-name category) category))
  20423. ;; Evaluate the compiled format
  20424. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  20425. ;; And finally add the text properties
  20426. (org-add-props rtn nil
  20427. 'org-category (downcase category) 'tags tags
  20428. 'org-highest-priority org-highest-priority
  20429. 'org-lowest-priority org-lowest-priority
  20430. 'prefix-length (- (length rtn) (length txt))
  20431. 'time-of-day time-of-day
  20432. 'txt txt
  20433. 'time time
  20434. 'extra extra
  20435. 'dotime dotime))))
  20436. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20437. (defvar org-agenda-sorting-strategy-selected nil)
  20438. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20439. (catch 'exit
  20440. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20441. ((and todayp (member 'today (car org-agenda-time-grid))))
  20442. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20443. ((member 'weekly (car org-agenda-time-grid)))
  20444. (t (throw 'exit list)))
  20445. (let* ((have (delq nil (mapcar
  20446. (lambda (x) (get-text-property 1 'time-of-day x))
  20447. list)))
  20448. (string (nth 1 org-agenda-time-grid))
  20449. (gridtimes (nth 2 org-agenda-time-grid))
  20450. (req (car org-agenda-time-grid))
  20451. (remove (member 'remove-match req))
  20452. new time)
  20453. (if (and (member 'require-timed req) (not have))
  20454. ;; don't show empty grid
  20455. (throw 'exit list))
  20456. (while (setq time (pop gridtimes))
  20457. (unless (and remove (member time have))
  20458. (setq time (int-to-string time))
  20459. (push (org-format-agenda-item
  20460. nil string "" nil
  20461. (concat (substring time 0 -2) ":" (substring time -2)))
  20462. new)
  20463. (put-text-property
  20464. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20465. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20466. (append new list)
  20467. (append list new)))))
  20468. (defun org-compile-prefix-format (key)
  20469. "Compile the prefix format into a Lisp form that can be evaluated.
  20470. The resulting form is returned and stored in the variable
  20471. `org-prefix-format-compiled'."
  20472. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20473. (let ((s (cond
  20474. ((stringp org-agenda-prefix-format)
  20475. org-agenda-prefix-format)
  20476. ((assq key org-agenda-prefix-format)
  20477. (cdr (assq key org-agenda-prefix-format)))
  20478. (t " %-12:c%?-12t% s")))
  20479. (start 0)
  20480. varform vars var e c f opt)
  20481. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20482. s start)
  20483. (setq var (cdr (assoc (match-string 4 s)
  20484. '(("c" . category) ("t" . time) ("s" . extra)
  20485. ("T" . tag))))
  20486. c (or (match-string 3 s) "")
  20487. opt (match-beginning 1)
  20488. start (1+ (match-beginning 0)))
  20489. (if (equal var 'time) (setq org-prefix-has-time t))
  20490. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20491. (setq f (concat "%" (match-string 2 s) "s"))
  20492. (if opt
  20493. (setq varform
  20494. `(if (equal "" ,var)
  20495. ""
  20496. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20497. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20498. (setq s (replace-match "%s" t nil s))
  20499. (push varform vars))
  20500. (setq vars (nreverse vars))
  20501. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20502. (defun org-set-sorting-strategy (key)
  20503. (if (symbolp (car org-agenda-sorting-strategy))
  20504. ;; the old format
  20505. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20506. (setq org-agenda-sorting-strategy-selected
  20507. (or (cdr (assq key org-agenda-sorting-strategy))
  20508. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20509. '(time-up category-keep priority-down)))))
  20510. (defun org-get-time-of-day (s &optional string mod24)
  20511. "Check string S for a time of day.
  20512. If found, return it as a military time number between 0 and 2400.
  20513. If not found, return nil.
  20514. The optional STRING argument forces conversion into a 5 character wide string
  20515. HH:MM."
  20516. (save-match-data
  20517. (when
  20518. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20519. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20520. (let* ((h (string-to-number (match-string 1 s)))
  20521. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20522. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20523. (am-p (equal ampm "am"))
  20524. (h1 (cond ((not ampm) h)
  20525. ((= h 12) (if am-p 0 12))
  20526. (t (+ h (if am-p 0 12)))))
  20527. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20528. (mod h1 24) h1))
  20529. (t0 (+ (* 100 h2) m))
  20530. (t1 (concat (if (>= h1 24) "+" " ")
  20531. (if (< t0 100) "0" "")
  20532. (if (< t0 10) "0" "")
  20533. (int-to-string t0))))
  20534. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20535. (defun org-finalize-agenda-entries (list &optional nosort)
  20536. "Sort and concatenate the agenda items."
  20537. (setq list (mapcar 'org-agenda-highlight-todo list))
  20538. (if nosort
  20539. list
  20540. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20541. (defun org-agenda-highlight-todo (x)
  20542. (let (re pl)
  20543. (if (eq x 'line)
  20544. (save-excursion
  20545. (beginning-of-line 1)
  20546. (setq re (get-text-property (point) 'org-todo-regexp))
  20547. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20548. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20549. (add-text-properties (match-beginning 0) (match-end 0)
  20550. (list 'face (org-get-todo-face 0)))
  20551. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20552. (delete-region (match-beginning 1) (1- (match-end 0)))
  20553. (goto-char (match-beginning 1))
  20554. (insert (format org-agenda-todo-keyword-format s)))))
  20555. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20556. pl (get-text-property 0 'prefix-length x))
  20557. (when (and re
  20558. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20559. x (or pl 0)) pl))
  20560. (add-text-properties
  20561. (or (match-end 1) (match-end 0)) (match-end 0)
  20562. (list 'face (org-get-todo-face (match-string 2 x)))
  20563. x)
  20564. (setq x (concat (substring x 0 (match-end 1))
  20565. (format org-agenda-todo-keyword-format
  20566. (match-string 2 x))
  20567. " "
  20568. (substring x (match-end 3)))))
  20569. x)))
  20570. (defsubst org-cmp-priority (a b)
  20571. "Compare the priorities of string A and B."
  20572. (let ((pa (or (get-text-property 1 'priority a) 0))
  20573. (pb (or (get-text-property 1 'priority b) 0)))
  20574. (cond ((> pa pb) +1)
  20575. ((< pa pb) -1)
  20576. (t nil))))
  20577. (defsubst org-cmp-category (a b)
  20578. "Compare the string values of categories of strings A and B."
  20579. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20580. (cb (or (get-text-property 1 'org-category b) "")))
  20581. (cond ((string-lessp ca cb) -1)
  20582. ((string-lessp cb ca) +1)
  20583. (t nil))))
  20584. (defsubst org-cmp-tag (a b)
  20585. "Compare the string values of categories of strings A and B."
  20586. (let ((ta (car (last (get-text-property 1 'tags a))))
  20587. (tb (car (last (get-text-property 1 'tags b)))))
  20588. (cond ((not ta) +1)
  20589. ((not tb) -1)
  20590. ((string-lessp ta tb) -1)
  20591. ((string-lessp tb ta) +1)
  20592. (t nil))))
  20593. (defsubst org-cmp-time (a b)
  20594. "Compare the time-of-day values of strings A and B."
  20595. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20596. (ta (or (get-text-property 1 'time-of-day a) def))
  20597. (tb (or (get-text-property 1 'time-of-day b) def)))
  20598. (cond ((< ta tb) -1)
  20599. ((< tb ta) +1)
  20600. (t nil))))
  20601. (defun org-entries-lessp (a b)
  20602. "Predicate for sorting agenda entries."
  20603. ;; The following variables will be used when the form is evaluated.
  20604. ;; So even though the compiler complains, keep them.
  20605. (let* ((time-up (org-cmp-time a b))
  20606. (time-down (if time-up (- time-up) nil))
  20607. (priority-up (org-cmp-priority a b))
  20608. (priority-down (if priority-up (- priority-up) nil))
  20609. (category-up (org-cmp-category a b))
  20610. (category-down (if category-up (- category-up) nil))
  20611. (category-keep (if category-up +1 nil))
  20612. (tag-up (org-cmp-tag a b))
  20613. (tag-down (if tag-up (- tag-up) nil)))
  20614. (cdr (assoc
  20615. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20616. '((-1 . t) (1 . nil) (nil . nil))))))
  20617. ;;; Agenda restriction lock
  20618. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20619. "Overlay to mark the headline to which arenda commands are restricted.")
  20620. (org-overlay-put org-agenda-restriction-lock-overlay
  20621. 'face 'org-agenda-restriction-lock)
  20622. (org-overlay-put org-agenda-restriction-lock-overlay
  20623. 'help-echo "Agendas are currently limited to this subtree.")
  20624. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20625. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20626. "Overlay marking the agenda restriction line in speedbar.")
  20627. (org-overlay-put org-speedbar-restriction-lock-overlay
  20628. 'face 'org-agenda-restriction-lock)
  20629. (org-overlay-put org-speedbar-restriction-lock-overlay
  20630. 'help-echo "Agendas are currently limited to this item.")
  20631. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20632. (defun org-agenda-set-restriction-lock (&optional type)
  20633. "Set restriction lock for agenda, to current subtree or file.
  20634. Restriction will be the file if TYPE is `file', or if type is the
  20635. universal prefix '(4), or if the cursor is before the first headline
  20636. in the file. Otherwise, restriction will be to the current subtree."
  20637. (interactive "P")
  20638. (and (equal type '(4)) (setq type 'file))
  20639. (setq type (cond
  20640. (type type)
  20641. ((org-at-heading-p) 'subtree)
  20642. ((condition-case nil (org-back-to-heading t) (error nil))
  20643. 'subtree)
  20644. (t 'file)))
  20645. (if (eq type 'subtree)
  20646. (progn
  20647. (setq org-agenda-restrict t)
  20648. (setq org-agenda-overriding-restriction 'subtree)
  20649. (put 'org-agenda-files 'org-restrict
  20650. (list (buffer-file-name (buffer-base-buffer))))
  20651. (org-back-to-heading t)
  20652. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20653. (move-marker org-agenda-restrict-begin (point))
  20654. (move-marker org-agenda-restrict-end
  20655. (save-excursion (org-end-of-subtree t)))
  20656. (message "Locking agenda restriction to subtree"))
  20657. (put 'org-agenda-files 'org-restrict
  20658. (list (buffer-file-name (buffer-base-buffer))))
  20659. (setq org-agenda-restrict nil)
  20660. (setq org-agenda-overriding-restriction 'file)
  20661. (move-marker org-agenda-restrict-begin nil)
  20662. (move-marker org-agenda-restrict-end nil)
  20663. (message "Locking agenda restriction to file"))
  20664. (setq current-prefix-arg nil)
  20665. (org-agenda-maybe-redo))
  20666. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20667. "Remove the agenda restriction lock."
  20668. (interactive "P")
  20669. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20670. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20671. (setq org-agenda-overriding-restriction nil)
  20672. (setq org-agenda-restrict nil)
  20673. (put 'org-agenda-files 'org-restrict nil)
  20674. (move-marker org-agenda-restrict-begin nil)
  20675. (move-marker org-agenda-restrict-end nil)
  20676. (setq current-prefix-arg nil)
  20677. (message "Agenda restriction lock removed")
  20678. (or noupdate (org-agenda-maybe-redo)))
  20679. (defun org-agenda-maybe-redo ()
  20680. "If there is any window showing the agenda view, update it."
  20681. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20682. (w0 (selected-window)))
  20683. (when w
  20684. (select-window w)
  20685. (org-agenda-redo)
  20686. (select-window w0)
  20687. (if org-agenda-overriding-restriction
  20688. (message "Agenda view shifted to new %s restriction"
  20689. org-agenda-overriding-restriction)
  20690. (message "Agenda restriction lock removed")))))
  20691. ;;; Agenda commands
  20692. (defun org-agenda-check-type (error &rest types)
  20693. "Check if agenda buffer is of allowed type.
  20694. If ERROR is non-nil, throw an error, otherwise just return nil."
  20695. (if (memq org-agenda-type types)
  20696. t
  20697. (if error
  20698. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20699. nil)))
  20700. (defun org-agenda-quit ()
  20701. "Exit agenda by removing the window or the buffer."
  20702. (interactive)
  20703. (let ((buf (current-buffer)))
  20704. (if (not (one-window-p)) (delete-window))
  20705. (kill-buffer buf)
  20706. (org-agenda-reset-markers)
  20707. (org-columns-remove-overlays))
  20708. ;; Maybe restore the pre-agenda window configuration.
  20709. (and org-agenda-restore-windows-after-quit
  20710. (not (eq org-agenda-window-setup 'other-frame))
  20711. org-pre-agenda-window-conf
  20712. (set-window-configuration org-pre-agenda-window-conf)))
  20713. (defun org-agenda-exit ()
  20714. "Exit agenda by removing the window or the buffer.
  20715. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20716. Org-mode buffers visited directly by the user will not be touched."
  20717. (interactive)
  20718. (org-release-buffers org-agenda-new-buffers)
  20719. (setq org-agenda-new-buffers nil)
  20720. (org-agenda-quit))
  20721. (defun org-agenda-execute (arg)
  20722. "Execute another agenda command, keeping same window.\\<global-map>
  20723. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20724. (interactive "P")
  20725. (let ((org-agenda-window-setup 'current-window))
  20726. (org-agenda arg)))
  20727. (defun org-save-all-org-buffers ()
  20728. "Save all Org-mode buffers without user confirmation."
  20729. (interactive)
  20730. (message "Saving all Org-mode buffers...")
  20731. (save-some-buffers t 'org-mode-p)
  20732. (message "Saving all Org-mode buffers... done"))
  20733. (defun org-agenda-redo ()
  20734. "Rebuild Agenda.
  20735. When this is the global TODO list, a prefix argument will be interpreted."
  20736. (interactive)
  20737. (let* ((org-agenda-keep-modes t)
  20738. (line (org-current-line))
  20739. (window-line (- line (org-current-line (window-start))))
  20740. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20741. (message "Rebuilding agenda buffer...")
  20742. (org-let lprops '(eval org-agenda-redo-command))
  20743. (setq org-agenda-undo-list nil
  20744. org-agenda-pending-undo-list nil)
  20745. (message "Rebuilding agenda buffer...done")
  20746. (goto-line line)
  20747. (recenter window-line)))
  20748. (defun org-agenda-manipulate-query-add ()
  20749. "Manipulate the query by adding a search term with positive selection.
  20750. Positive selection means, the term must be matched for selection of an entry."
  20751. (interactive)
  20752. (org-agenda-manipulate-query ?\[))
  20753. (defun org-agenda-manipulate-query-subtract ()
  20754. "Manipulate the query by adding a search term with negative selection.
  20755. Negative selection means, term must not be matched for selection of an entry."
  20756. (interactive)
  20757. (org-agenda-manipulate-query ?\]))
  20758. (defun org-agenda-manipulate-query-add-re ()
  20759. "Manipulate the query by adding a search regexp with positive selection.
  20760. Positive selection means, the regexp must match for selection of an entry."
  20761. (interactive)
  20762. (org-agenda-manipulate-query ?\{))
  20763. (defun org-agenda-manipulate-query-subtract-re ()
  20764. "Manipulate the query by adding a search regexp with negative selection.
  20765. Negative selection means, regexp must not match for selection of an entry."
  20766. (interactive)
  20767. (org-agenda-manipulate-query ?\}))
  20768. (defun org-agenda-manipulate-query (char)
  20769. (cond
  20770. ((eq org-agenda-type 'search)
  20771. (org-add-to-string
  20772. 'org-agenda-query-string
  20773. (cdr (assoc char '((?\[ . " +") (?\] . " -")
  20774. (?\{ . " +{}") (?\} . " -{}")))))
  20775. (setq org-agenda-redo-command
  20776. (list 'org-search-view
  20777. (+ (length org-agenda-query-string)
  20778. (if (member char '(?\{ ?\})) 0 1))
  20779. org-agenda-query-string))
  20780. (set-register org-agenda-query-register org-agenda-query-string)
  20781. (org-agenda-redo))
  20782. (t (error "Canot manipulate query for %s-type agenda buffers"
  20783. org-agenda-type))))
  20784. (defun org-add-to-string (var string)
  20785. (set var (concat (symbol-value var) string)))
  20786. (defun org-agenda-goto-date (date)
  20787. "Jump to DATE in agenda."
  20788. (interactive (list (org-read-date)))
  20789. (org-agenda-list nil date))
  20790. (defun org-agenda-goto-today ()
  20791. "Go to today."
  20792. (interactive)
  20793. (org-agenda-check-type t 'timeline 'agenda)
  20794. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20795. (cond
  20796. (tdpos (goto-char tdpos))
  20797. ((eq org-agenda-type 'agenda)
  20798. (let* ((sd (time-to-days
  20799. (time-subtract (current-time)
  20800. (list 0 (* 3600 org-extend-today-until) 0))))
  20801. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20802. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20803. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20804. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20805. (org-agenda-redo)
  20806. (org-agenda-find-same-or-today-or-agenda)))
  20807. (t (error "Cannot find today")))))
  20808. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20809. (goto-char
  20810. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20811. (text-property-any (point-min) (point-max) 'org-today t)
  20812. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20813. (point-min))))
  20814. (defun org-agenda-later (arg)
  20815. "Go forward in time by thee current span.
  20816. With prefix ARG, go forward that many times the current span."
  20817. (interactive "p")
  20818. (org-agenda-check-type t 'agenda)
  20819. (let* ((span org-agenda-span)
  20820. (sd org-starting-day)
  20821. (greg (calendar-gregorian-from-absolute sd))
  20822. (cnt (get-text-property (point) 'org-day-cnt))
  20823. greg2 nd)
  20824. (cond
  20825. ((eq span 'day)
  20826. (setq sd (+ arg sd) nd 1))
  20827. ((eq span 'week)
  20828. (setq sd (+ (* 7 arg) sd) nd 7))
  20829. ((eq span 'month)
  20830. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20831. sd (calendar-absolute-from-gregorian greg2))
  20832. (setcar greg2 (1+ (car greg2)))
  20833. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20834. ((eq span 'year)
  20835. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20836. sd (calendar-absolute-from-gregorian greg2))
  20837. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20838. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20839. (let ((org-agenda-overriding-arguments
  20840. (list (car org-agenda-last-arguments) sd nd t)))
  20841. (org-agenda-redo)
  20842. (org-agenda-find-same-or-today-or-agenda cnt))))
  20843. (defun org-agenda-earlier (arg)
  20844. "Go backward in time by the current span.
  20845. With prefix ARG, go backward that many times the current span."
  20846. (interactive "p")
  20847. (org-agenda-later (- arg)))
  20848. (defun org-agenda-day-view ()
  20849. "Switch to daily view for agenda."
  20850. (interactive)
  20851. (setq org-agenda-ndays 1)
  20852. (org-agenda-change-time-span 'day))
  20853. (defun org-agenda-week-view ()
  20854. "Switch to daily view for agenda."
  20855. (interactive)
  20856. (setq org-agenda-ndays 7)
  20857. (org-agenda-change-time-span 'week))
  20858. (defun org-agenda-month-view ()
  20859. "Switch to daily view for agenda."
  20860. (interactive)
  20861. (org-agenda-change-time-span 'month))
  20862. (defun org-agenda-year-view ()
  20863. "Switch to daily view for agenda."
  20864. (interactive)
  20865. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20866. (org-agenda-change-time-span 'year)
  20867. (error "Abort")))
  20868. (defun org-agenda-change-time-span (span)
  20869. "Change the agenda view to SPAN.
  20870. SPAN may be `day', `week', `month', `year'."
  20871. (org-agenda-check-type t 'agenda)
  20872. (if (equal org-agenda-span span)
  20873. (error "Viewing span is already \"%s\"" span))
  20874. (let* ((sd (or (get-text-property (point) 'day)
  20875. org-starting-day))
  20876. (computed (org-agenda-compute-time-span sd span))
  20877. (org-agenda-overriding-arguments
  20878. (list (car org-agenda-last-arguments)
  20879. (car computed) (cdr computed) t)))
  20880. (org-agenda-redo)
  20881. (org-agenda-find-same-or-today-or-agenda))
  20882. (org-agenda-set-mode-name)
  20883. (message "Switched to %s view" span))
  20884. (defun org-agenda-compute-time-span (sd span)
  20885. "Compute starting date and number of days for agenda.
  20886. SPAN may be `day', `week', `month', `year'. The return value
  20887. is a cons cell with the starting date and the number of days,
  20888. so that the date SD will be in that range."
  20889. (let* ((greg (calendar-gregorian-from-absolute sd))
  20890. nd)
  20891. (cond
  20892. ((eq span 'day)
  20893. (setq nd 1))
  20894. ((eq span 'week)
  20895. (let* ((nt (calendar-day-of-week
  20896. (calendar-gregorian-from-absolute sd)))
  20897. (d (if org-agenda-start-on-weekday
  20898. (- nt org-agenda-start-on-weekday)
  20899. 0)))
  20900. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20901. (setq nd 7)))
  20902. ((eq span 'month)
  20903. (setq sd (calendar-absolute-from-gregorian
  20904. (list (car greg) 1 (nth 2 greg)))
  20905. nd (- (calendar-absolute-from-gregorian
  20906. (list (1+ (car greg)) 1 (nth 2 greg)))
  20907. sd)))
  20908. ((eq span 'year)
  20909. (setq sd (calendar-absolute-from-gregorian
  20910. (list 1 1 (nth 2 greg)))
  20911. nd (- (calendar-absolute-from-gregorian
  20912. (list 1 1 (1+ (nth 2 greg))))
  20913. sd))))
  20914. (cons sd nd)))
  20915. ;; FIXME: does not work if user makes date format that starts with a blank
  20916. (defun org-agenda-next-date-line (&optional arg)
  20917. "Jump to the next line indicating a date in agenda buffer."
  20918. (interactive "p")
  20919. (org-agenda-check-type t 'agenda 'timeline)
  20920. (beginning-of-line 1)
  20921. (if (looking-at "^\\S-") (forward-char 1))
  20922. (if (not (re-search-forward "^\\S-" nil t arg))
  20923. (progn
  20924. (backward-char 1)
  20925. (error "No next date after this line in this buffer")))
  20926. (goto-char (match-beginning 0)))
  20927. (defun org-agenda-previous-date-line (&optional arg)
  20928. "Jump to the previous line indicating a date in agenda buffer."
  20929. (interactive "p")
  20930. (org-agenda-check-type t 'agenda 'timeline)
  20931. (beginning-of-line 1)
  20932. (if (not (re-search-backward "^\\S-" nil t arg))
  20933. (error "No previous date before this line in this buffer")))
  20934. ;; Initialize the highlight
  20935. (defvar org-hl (org-make-overlay 1 1))
  20936. (org-overlay-put org-hl 'face 'highlight)
  20937. (defun org-highlight (begin end &optional buffer)
  20938. "Highlight a region with overlay."
  20939. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20940. org-hl begin end (or buffer (current-buffer))))
  20941. (defun org-unhighlight ()
  20942. "Detach overlay INDEX."
  20943. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20944. ;; FIXME this is currently not used.
  20945. (defun org-highlight-until-next-command (beg end &optional buffer)
  20946. (org-highlight beg end buffer)
  20947. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20948. (defun org-unhighlight-once ()
  20949. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20950. (org-unhighlight))
  20951. (defun org-agenda-follow-mode ()
  20952. "Toggle follow mode in an agenda buffer."
  20953. (interactive)
  20954. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20955. (org-agenda-set-mode-name)
  20956. (message "Follow mode is %s"
  20957. (if org-agenda-follow-mode "on" "off")))
  20958. (defun org-agenda-log-mode ()
  20959. "Toggle log mode in an agenda buffer."
  20960. (interactive)
  20961. (org-agenda-check-type t 'agenda 'timeline)
  20962. (setq org-agenda-show-log (not org-agenda-show-log))
  20963. (org-agenda-set-mode-name)
  20964. (org-agenda-redo)
  20965. (message "Log mode is %s"
  20966. (if org-agenda-show-log "on" "off")))
  20967. (defun org-agenda-toggle-diary ()
  20968. "Toggle diary inclusion in an agenda buffer."
  20969. (interactive)
  20970. (org-agenda-check-type t 'agenda)
  20971. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20972. (org-agenda-redo)
  20973. (org-agenda-set-mode-name)
  20974. (message "Diary inclusion turned %s"
  20975. (if org-agenda-include-diary "on" "off")))
  20976. (defun org-agenda-toggle-time-grid ()
  20977. "Toggle time grid in an agenda buffer."
  20978. (interactive)
  20979. (org-agenda-check-type t 'agenda)
  20980. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20981. (org-agenda-redo)
  20982. (org-agenda-set-mode-name)
  20983. (message "Time-grid turned %s"
  20984. (if org-agenda-use-time-grid "on" "off")))
  20985. (defun org-agenda-set-mode-name ()
  20986. "Set the mode name to indicate all the small mode settings."
  20987. (setq mode-name
  20988. (concat "Org-Agenda"
  20989. (if (equal org-agenda-ndays 1) " Day" "")
  20990. (if (equal org-agenda-ndays 7) " Week" "")
  20991. (if org-agenda-follow-mode " Follow" "")
  20992. (if org-agenda-include-diary " Diary" "")
  20993. (if org-agenda-use-time-grid " Grid" "")
  20994. (if org-agenda-show-log " Log" "")))
  20995. (force-mode-line-update))
  20996. (defun org-agenda-post-command-hook ()
  20997. (and (eolp) (not (bolp)) (backward-char 1))
  20998. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  20999. (if (and org-agenda-follow-mode
  21000. (get-text-property (point) 'org-marker))
  21001. (org-agenda-show)))
  21002. (defun org-agenda-show-priority ()
  21003. "Show the priority of the current item.
  21004. This priority is composed of the main priority given with the [#A] cookies,
  21005. and by additional input from the age of a schedules or deadline entry."
  21006. (interactive)
  21007. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  21008. (message "Priority is %d" (if pri pri -1000))))
  21009. (defun org-agenda-show-tags ()
  21010. "Show the tags applicable to the current item."
  21011. (interactive)
  21012. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  21013. (if tags
  21014. (message "Tags are :%s:"
  21015. (org-no-properties (mapconcat 'identity tags ":")))
  21016. (message "No tags associated with this line"))))
  21017. (defun org-agenda-goto (&optional highlight)
  21018. "Go to the Org-mode file which contains the item at point."
  21019. (interactive)
  21020. (let* ((marker (or (get-text-property (point) 'org-marker)
  21021. (org-agenda-error)))
  21022. (buffer (marker-buffer marker))
  21023. (pos (marker-position marker)))
  21024. (switch-to-buffer-other-window buffer)
  21025. (widen)
  21026. (goto-char pos)
  21027. (when (org-mode-p)
  21028. (org-show-context 'agenda)
  21029. (save-excursion
  21030. (and (outline-next-heading)
  21031. (org-flag-heading nil)))) ; show the next heading
  21032. (recenter (/ (window-height) 2))
  21033. (run-hooks 'org-agenda-after-show-hook)
  21034. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  21035. (defvar org-agenda-after-show-hook nil
  21036. "Normal hook run after an item has been shown from the agenda.
  21037. Point is in the buffer where the item originated.")
  21038. (defun org-agenda-kill ()
  21039. "Kill the entry or subtree belonging to the current agenda entry."
  21040. (interactive)
  21041. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  21042. (let* ((marker (or (get-text-property (point) 'org-marker)
  21043. (org-agenda-error)))
  21044. (buffer (marker-buffer marker))
  21045. (pos (marker-position marker))
  21046. (type (get-text-property (point) 'type))
  21047. dbeg dend (n 0) conf)
  21048. (org-with-remote-undo buffer
  21049. (with-current-buffer buffer
  21050. (save-excursion
  21051. (goto-char pos)
  21052. (if (and (org-mode-p) (not (member type '("sexp"))))
  21053. (setq dbeg (progn (org-back-to-heading t) (point))
  21054. dend (org-end-of-subtree t t))
  21055. (setq dbeg (point-at-bol)
  21056. dend (min (point-max) (1+ (point-at-eol)))))
  21057. (goto-char dbeg)
  21058. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  21059. (setq conf (or (eq t org-agenda-confirm-kill)
  21060. (and (numberp org-agenda-confirm-kill)
  21061. (> n org-agenda-confirm-kill))))
  21062. (and conf
  21063. (not (y-or-n-p
  21064. (format "Delete entry with %d lines in buffer \"%s\"? "
  21065. n (buffer-name buffer))))
  21066. (error "Abort"))
  21067. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  21068. (with-current-buffer buffer (delete-region dbeg dend))
  21069. (message "Agenda item and source killed"))))
  21070. (defun org-agenda-archive ()
  21071. "Kill the entry or subtree belonging to the current agenda entry."
  21072. (interactive)
  21073. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  21074. (let* ((marker (or (get-text-property (point) 'org-marker)
  21075. (org-agenda-error)))
  21076. (buffer (marker-buffer marker))
  21077. (pos (marker-position marker)))
  21078. (org-with-remote-undo buffer
  21079. (with-current-buffer buffer
  21080. (if (org-mode-p)
  21081. (save-excursion
  21082. (goto-char pos)
  21083. (org-remove-subtree-entries-from-agenda)
  21084. (org-back-to-heading t)
  21085. (org-archive-subtree))
  21086. (error "Archiving works only in Org-mode files"))))))
  21087. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  21088. "Remove all lines in the agenda that correspond to a given subtree.
  21089. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  21090. If this information is not given, the function uses the tree at point."
  21091. (let ((buf (or buf (current-buffer))) m p)
  21092. (save-excursion
  21093. (unless (and beg end)
  21094. (org-back-to-heading t)
  21095. (setq beg (point))
  21096. (org-end-of-subtree t)
  21097. (setq end (point)))
  21098. (set-buffer (get-buffer org-agenda-buffer-name))
  21099. (save-excursion
  21100. (goto-char (point-max))
  21101. (beginning-of-line 1)
  21102. (while (not (bobp))
  21103. (when (and (setq m (get-text-property (point) 'org-marker))
  21104. (equal buf (marker-buffer m))
  21105. (setq p (marker-position m))
  21106. (>= p beg)
  21107. (<= p end))
  21108. (let ((inhibit-read-only t))
  21109. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  21110. (beginning-of-line 0))))))
  21111. (defun org-agenda-open-link ()
  21112. "Follow the link in the current line, if any."
  21113. (interactive)
  21114. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  21115. (save-excursion
  21116. (save-restriction
  21117. (narrow-to-region (point-at-bol) (point-at-eol))
  21118. (org-open-at-point))))
  21119. (defun org-agenda-copy-local-variable (var)
  21120. "Get a variable from a referenced buffer and install it here."
  21121. (let ((m (get-text-property (point) 'org-marker)))
  21122. (when (and m (buffer-live-p (marker-buffer m)))
  21123. (org-set-local var (with-current-buffer (marker-buffer m)
  21124. (symbol-value var))))))
  21125. (defun org-agenda-switch-to (&optional delete-other-windows)
  21126. "Go to the Org-mode file which contains the item at point."
  21127. (interactive)
  21128. (let* ((marker (or (get-text-property (point) 'org-marker)
  21129. (org-agenda-error)))
  21130. (buffer (marker-buffer marker))
  21131. (pos (marker-position marker)))
  21132. (switch-to-buffer buffer)
  21133. (and delete-other-windows (delete-other-windows))
  21134. (widen)
  21135. (goto-char pos)
  21136. (when (org-mode-p)
  21137. (org-show-context 'agenda)
  21138. (save-excursion
  21139. (and (outline-next-heading)
  21140. (org-flag-heading nil)))))) ; show the next heading
  21141. (defun org-agenda-goto-mouse (ev)
  21142. "Go to the Org-mode file which contains the item at the mouse click."
  21143. (interactive "e")
  21144. (mouse-set-point ev)
  21145. (org-agenda-goto))
  21146. (defun org-agenda-show ()
  21147. "Display the Org-mode file which contains the item at point."
  21148. (interactive)
  21149. (let ((win (selected-window)))
  21150. (org-agenda-goto t)
  21151. (select-window win)))
  21152. (defun org-agenda-recenter (arg)
  21153. "Display the Org-mode file which contains the item at point and recenter."
  21154. (interactive "P")
  21155. (let ((win (selected-window)))
  21156. (org-agenda-goto t)
  21157. (recenter arg)
  21158. (select-window win)))
  21159. (defun org-agenda-show-mouse (ev)
  21160. "Display the Org-mode file which contains the item at the mouse click."
  21161. (interactive "e")
  21162. (mouse-set-point ev)
  21163. (org-agenda-show))
  21164. (defun org-agenda-check-no-diary ()
  21165. "Check if the entry is a diary link and abort if yes."
  21166. (if (get-text-property (point) 'org-agenda-diary-link)
  21167. (org-agenda-error)))
  21168. (defun org-agenda-error ()
  21169. (error "Command not allowed in this line"))
  21170. (defun org-agenda-tree-to-indirect-buffer ()
  21171. "Show the subtree corresponding to the current entry in an indirect buffer.
  21172. This calls the command `org-tree-to-indirect-buffer' from the original
  21173. Org-mode buffer.
  21174. With numerical prefix arg ARG, go up to this level and then take that tree.
  21175. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  21176. dedicated frame)."
  21177. (interactive)
  21178. (org-agenda-check-no-diary)
  21179. (let* ((marker (or (get-text-property (point) 'org-marker)
  21180. (org-agenda-error)))
  21181. (buffer (marker-buffer marker))
  21182. (pos (marker-position marker)))
  21183. (with-current-buffer buffer
  21184. (save-excursion
  21185. (goto-char pos)
  21186. (call-interactively 'org-tree-to-indirect-buffer)))))
  21187. (defvar org-last-heading-marker (make-marker)
  21188. "Marker pointing to the headline that last changed its TODO state
  21189. by a remote command from the agenda.")
  21190. (defun org-agenda-todo-nextset ()
  21191. "Switch TODO entry to next sequence."
  21192. (interactive)
  21193. (org-agenda-todo 'nextset))
  21194. (defun org-agenda-todo-previousset ()
  21195. "Switch TODO entry to previous sequence."
  21196. (interactive)
  21197. (org-agenda-todo 'previousset))
  21198. (defun org-agenda-todo (&optional arg)
  21199. "Cycle TODO state of line at point, also in Org-mode file.
  21200. This changes the line at point, all other lines in the agenda referring to
  21201. the same tree node, and the headline of the tree node in the Org-mode file."
  21202. (interactive "P")
  21203. (org-agenda-check-no-diary)
  21204. (let* ((col (current-column))
  21205. (marker (or (get-text-property (point) 'org-marker)
  21206. (org-agenda-error)))
  21207. (buffer (marker-buffer marker))
  21208. (pos (marker-position marker))
  21209. (hdmarker (get-text-property (point) 'org-hd-marker))
  21210. (inhibit-read-only t)
  21211. newhead)
  21212. (org-with-remote-undo buffer
  21213. (with-current-buffer buffer
  21214. (widen)
  21215. (goto-char pos)
  21216. (org-show-context 'agenda)
  21217. (save-excursion
  21218. (and (outline-next-heading)
  21219. (org-flag-heading nil))) ; show the next heading
  21220. (org-todo arg)
  21221. (and (bolp) (forward-char 1))
  21222. (setq newhead (org-get-heading))
  21223. (save-excursion
  21224. (org-back-to-heading)
  21225. (move-marker org-last-heading-marker (point))))
  21226. (beginning-of-line 1)
  21227. (save-excursion
  21228. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  21229. (move-to-column col))))
  21230. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  21231. "Change all lines in the agenda buffer which match HDMARKER.
  21232. The new content of the line will be NEWHEAD (as modified by
  21233. `org-format-agenda-item'). HDMARKER is checked with
  21234. `equal' against all `org-hd-marker' text properties in the file.
  21235. If FIXFACE is non-nil, the face of each item is modified acording to
  21236. the new TODO state."
  21237. (let* ((inhibit-read-only t)
  21238. props m pl undone-face done-face finish new dotime cat tags)
  21239. (save-excursion
  21240. (goto-char (point-max))
  21241. (beginning-of-line 1)
  21242. (while (not finish)
  21243. (setq finish (bobp))
  21244. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  21245. (equal m hdmarker))
  21246. (setq props (text-properties-at (point))
  21247. dotime (get-text-property (point) 'dotime)
  21248. cat (get-text-property (point) 'org-category)
  21249. tags (get-text-property (point) 'tags)
  21250. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  21251. pl (get-text-property (point) 'prefix-length)
  21252. undone-face (get-text-property (point) 'undone-face)
  21253. done-face (get-text-property (point) 'done-face))
  21254. (move-to-column pl)
  21255. (cond
  21256. ((equal new "")
  21257. (beginning-of-line 1)
  21258. (and (looking-at ".*\n?") (replace-match "")))
  21259. ((looking-at ".*")
  21260. (replace-match new t t)
  21261. (beginning-of-line 1)
  21262. (add-text-properties (point-at-bol) (point-at-eol) props)
  21263. (when fixface
  21264. (add-text-properties
  21265. (point-at-bol) (point-at-eol)
  21266. (list 'face
  21267. (if org-last-todo-state-is-todo
  21268. undone-face done-face))))
  21269. (org-agenda-highlight-todo 'line)
  21270. (beginning-of-line 1))
  21271. (t (error "Line update did not work"))))
  21272. (beginning-of-line 0)))
  21273. (org-finalize-agenda)))
  21274. (defun org-agenda-align-tags (&optional line)
  21275. "Align all tags in agenda items to `org-agenda-tags-column'."
  21276. (let ((inhibit-read-only t) l c)
  21277. (save-excursion
  21278. (goto-char (if line (point-at-bol) (point-min)))
  21279. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  21280. (if line (point-at-eol) nil) t)
  21281. (add-text-properties
  21282. (match-beginning 2) (match-end 2)
  21283. (list 'face (list 'org-tag (get-text-property
  21284. (match-beginning 2) 'face))))
  21285. (setq l (- (match-end 2) (match-beginning 2))
  21286. c (if (< org-agenda-tags-column 0)
  21287. (- (abs org-agenda-tags-column) l)
  21288. org-agenda-tags-column))
  21289. (delete-region (match-beginning 1) (match-end 1))
  21290. (goto-char (match-beginning 1))
  21291. (insert (org-add-props
  21292. (make-string (max 1 (- c (current-column))) ?\ )
  21293. (text-properties-at (point))))))))
  21294. (defun org-agenda-priority-up ()
  21295. "Increase the priority of line at point, also in Org-mode file."
  21296. (interactive)
  21297. (org-agenda-priority 'up))
  21298. (defun org-agenda-priority-down ()
  21299. "Decrease the priority of line at point, also in Org-mode file."
  21300. (interactive)
  21301. (org-agenda-priority 'down))
  21302. (defun org-agenda-priority (&optional force-direction)
  21303. "Set the priority of line at point, also in Org-mode file.
  21304. This changes the line at point, all other lines in the agenda referring to
  21305. the same tree node, and the headline of the tree node in the Org-mode file."
  21306. (interactive)
  21307. (org-agenda-check-no-diary)
  21308. (let* ((marker (or (get-text-property (point) 'org-marker)
  21309. (org-agenda-error)))
  21310. (hdmarker (get-text-property (point) 'org-hd-marker))
  21311. (buffer (marker-buffer hdmarker))
  21312. (pos (marker-position hdmarker))
  21313. (inhibit-read-only t)
  21314. newhead)
  21315. (org-with-remote-undo buffer
  21316. (with-current-buffer buffer
  21317. (widen)
  21318. (goto-char pos)
  21319. (org-show-context 'agenda)
  21320. (save-excursion
  21321. (and (outline-next-heading)
  21322. (org-flag-heading nil))) ; show the next heading
  21323. (funcall 'org-priority force-direction)
  21324. (end-of-line 1)
  21325. (setq newhead (org-get-heading)))
  21326. (org-agenda-change-all-lines newhead hdmarker)
  21327. (beginning-of-line 1))))
  21328. (defun org-get-tags-at (&optional pos)
  21329. "Get a list of all headline tags applicable at POS.
  21330. POS defaults to point. If tags are inherited, the list contains
  21331. the targets in the same sequence as the headlines appear, i.e.
  21332. the tags of the current headline come last."
  21333. (interactive)
  21334. (let (tags lastpos)
  21335. (save-excursion
  21336. (save-restriction
  21337. (widen)
  21338. (goto-char (or pos (point)))
  21339. (save-match-data
  21340. (org-back-to-heading t)
  21341. (condition-case nil
  21342. (while (not (equal lastpos (point)))
  21343. (setq lastpos (point))
  21344. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  21345. (setq tags (append (org-split-string
  21346. (org-match-string-no-properties 1) ":")
  21347. tags)))
  21348. (or org-use-tag-inheritance (error ""))
  21349. (org-up-heading-all 1))
  21350. (error nil))))
  21351. tags)))
  21352. ;; FIXME: should fix the tags property of the agenda line.
  21353. (defun org-agenda-set-tags ()
  21354. "Set tags for the current headline."
  21355. (interactive)
  21356. (org-agenda-check-no-diary)
  21357. (if (and (org-region-active-p) (interactive-p))
  21358. (call-interactively 'org-change-tag-in-region)
  21359. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21360. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21361. (org-agenda-error)))
  21362. (buffer (marker-buffer hdmarker))
  21363. (pos (marker-position hdmarker))
  21364. (inhibit-read-only t)
  21365. newhead)
  21366. (org-with-remote-undo buffer
  21367. (with-current-buffer buffer
  21368. (widen)
  21369. (goto-char pos)
  21370. (save-excursion
  21371. (org-show-context 'agenda))
  21372. (save-excursion
  21373. (and (outline-next-heading)
  21374. (org-flag-heading nil))) ; show the next heading
  21375. (goto-char pos)
  21376. (call-interactively 'org-set-tags)
  21377. (end-of-line 1)
  21378. (setq newhead (org-get-heading)))
  21379. (org-agenda-change-all-lines newhead hdmarker)
  21380. (beginning-of-line 1)))))
  21381. (defun org-agenda-toggle-archive-tag ()
  21382. "Toggle the archive tag for the current entry."
  21383. (interactive)
  21384. (org-agenda-check-no-diary)
  21385. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  21386. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  21387. (org-agenda-error)))
  21388. (buffer (marker-buffer hdmarker))
  21389. (pos (marker-position hdmarker))
  21390. (inhibit-read-only t)
  21391. newhead)
  21392. (org-with-remote-undo buffer
  21393. (with-current-buffer buffer
  21394. (widen)
  21395. (goto-char pos)
  21396. (org-show-context 'agenda)
  21397. (save-excursion
  21398. (and (outline-next-heading)
  21399. (org-flag-heading nil))) ; show the next heading
  21400. (call-interactively 'org-toggle-archive-tag)
  21401. (end-of-line 1)
  21402. (setq newhead (org-get-heading)))
  21403. (org-agenda-change-all-lines newhead hdmarker)
  21404. (beginning-of-line 1))))
  21405. (defun org-agenda-date-later (arg &optional what)
  21406. "Change the date of this item to one day later."
  21407. (interactive "p")
  21408. (org-agenda-check-type t 'agenda 'timeline)
  21409. (org-agenda-check-no-diary)
  21410. (let* ((marker (or (get-text-property (point) 'org-marker)
  21411. (org-agenda-error)))
  21412. (buffer (marker-buffer marker))
  21413. (pos (marker-position marker)))
  21414. (org-with-remote-undo buffer
  21415. (with-current-buffer buffer
  21416. (widen)
  21417. (goto-char pos)
  21418. (if (not (org-at-timestamp-p))
  21419. (error "Cannot find time stamp"))
  21420. (org-timestamp-change arg (or what 'day)))
  21421. (org-agenda-show-new-time marker org-last-changed-timestamp))
  21422. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  21423. (defun org-agenda-date-earlier (arg &optional what)
  21424. "Change the date of this item to one day earlier."
  21425. (interactive "p")
  21426. (org-agenda-date-later (- arg) what))
  21427. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  21428. "Show new date stamp via text properties."
  21429. ;; We use text properties to make this undoable
  21430. (let ((inhibit-read-only t))
  21431. (setq stamp (concat " " prefix " => " stamp))
  21432. (save-excursion
  21433. (goto-char (point-max))
  21434. (while (not (bobp))
  21435. (when (equal marker (get-text-property (point) 'org-marker))
  21436. (move-to-column (- (window-width) (length stamp)) t)
  21437. (if (featurep 'xemacs)
  21438. ;; Use `duplicable' property to trigger undo recording
  21439. (let ((ex (make-extent nil nil))
  21440. (gl (make-glyph stamp)))
  21441. (set-glyph-face gl 'secondary-selection)
  21442. (set-extent-properties
  21443. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  21444. (insert-extent ex (1- (point)) (point-at-eol)))
  21445. (add-text-properties
  21446. (1- (point)) (point-at-eol)
  21447. (list 'display (org-add-props stamp nil
  21448. 'face 'secondary-selection))))
  21449. (beginning-of-line 1))
  21450. (beginning-of-line 0)))))
  21451. (defun org-agenda-date-prompt (arg)
  21452. "Change the date of this item. Date is prompted for, with default today.
  21453. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  21454. be used to request time specification in the time stamp."
  21455. (interactive "P")
  21456. (org-agenda-check-type t 'agenda 'timeline)
  21457. (org-agenda-check-no-diary)
  21458. (let* ((marker (or (get-text-property (point) 'org-marker)
  21459. (org-agenda-error)))
  21460. (buffer (marker-buffer marker))
  21461. (pos (marker-position marker)))
  21462. (org-with-remote-undo buffer
  21463. (with-current-buffer buffer
  21464. (widen)
  21465. (goto-char pos)
  21466. (if (not (org-at-timestamp-p))
  21467. (error "Cannot find time stamp"))
  21468. (org-time-stamp arg)
  21469. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21470. (defun org-agenda-schedule (arg)
  21471. "Schedule the item at point."
  21472. (interactive "P")
  21473. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21474. (org-agenda-check-no-diary)
  21475. (let* ((marker (or (get-text-property (point) 'org-marker)
  21476. (org-agenda-error)))
  21477. (buffer (marker-buffer marker))
  21478. (pos (marker-position marker))
  21479. (org-insert-labeled-timestamps-at-point nil)
  21480. ts)
  21481. (message "%s" (marker-insertion-type marker)) (sit-for 3)
  21482. (set-marker-insertion-type marker t)
  21483. (org-with-remote-undo buffer
  21484. (with-current-buffer buffer
  21485. (widen)
  21486. (goto-char pos)
  21487. (setq ts (org-schedule arg)))
  21488. (org-agenda-show-new-time marker ts "S"))
  21489. (message "Item scheduled for %s" ts)))
  21490. (defun org-agenda-deadline (arg)
  21491. "Schedule the item at point."
  21492. (interactive "P")
  21493. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21494. (org-agenda-check-no-diary)
  21495. (let* ((marker (or (get-text-property (point) 'org-marker)
  21496. (org-agenda-error)))
  21497. (buffer (marker-buffer marker))
  21498. (pos (marker-position marker))
  21499. (org-insert-labeled-timestamps-at-point nil)
  21500. ts)
  21501. (org-with-remote-undo buffer
  21502. (with-current-buffer buffer
  21503. (widen)
  21504. (goto-char pos)
  21505. (setq ts (org-deadline arg)))
  21506. (org-agenda-show-new-time marker ts "S"))
  21507. (message "Deadline for this item set to %s" ts)))
  21508. (defun org-get-heading (&optional no-tags)
  21509. "Return the heading of the current entry, without the stars."
  21510. (save-excursion
  21511. (org-back-to-heading t)
  21512. (if (looking-at
  21513. (if no-tags
  21514. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21515. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21516. (match-string 1) "")))
  21517. (defun org-agenda-clock-in (&optional arg)
  21518. "Start the clock on the currently selected item."
  21519. (interactive "P")
  21520. (org-agenda-check-no-diary)
  21521. (let* ((marker (or (get-text-property (point) 'org-marker)
  21522. (org-agenda-error)))
  21523. (pos (marker-position marker)))
  21524. (org-with-remote-undo (marker-buffer marker)
  21525. (with-current-buffer (marker-buffer marker)
  21526. (widen)
  21527. (goto-char pos)
  21528. (org-clock-in)))))
  21529. (defun org-agenda-clock-out (&optional arg)
  21530. "Stop the currently running clock."
  21531. (interactive "P")
  21532. (unless (marker-buffer org-clock-marker)
  21533. (error "No running clock"))
  21534. (org-with-remote-undo (marker-buffer org-clock-marker)
  21535. (org-clock-out)))
  21536. (defun org-agenda-clock-cancel (&optional arg)
  21537. "Cancel the currently running clock."
  21538. (interactive "P")
  21539. (unless (marker-buffer org-clock-marker)
  21540. (error "No running clock"))
  21541. (org-with-remote-undo (marker-buffer org-clock-marker)
  21542. (org-clock-cancel)))
  21543. (defun org-agenda-diary-entry ()
  21544. "Make a diary entry, like the `i' command from the calendar.
  21545. All the standard commands work: block, weekly etc."
  21546. (interactive)
  21547. (org-agenda-check-type t 'agenda 'timeline)
  21548. (require 'diary-lib)
  21549. (let* ((char (progn
  21550. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21551. (read-char-exclusive)))
  21552. (cmd (cdr (assoc char
  21553. '((?d . insert-diary-entry)
  21554. (?w . insert-weekly-diary-entry)
  21555. (?m . insert-monthly-diary-entry)
  21556. (?y . insert-yearly-diary-entry)
  21557. (?a . insert-anniversary-diary-entry)
  21558. (?b . insert-block-diary-entry)
  21559. (?c . insert-cyclic-diary-entry)))))
  21560. (oldf (symbol-function 'calendar-cursor-to-date))
  21561. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21562. (point (point))
  21563. (mark (or (mark t) (point))))
  21564. (unless cmd
  21565. (error "No command associated with <%c>" char))
  21566. (unless (and (get-text-property point 'day)
  21567. (or (not (equal ?b char))
  21568. (get-text-property mark 'day)))
  21569. (error "Don't know which date to use for diary entry"))
  21570. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21571. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21572. (let ((calendar-mark-ring
  21573. (list (calendar-gregorian-from-absolute
  21574. (or (get-text-property mark 'day)
  21575. (get-text-property point 'day))))))
  21576. (unwind-protect
  21577. (progn
  21578. (fset 'calendar-cursor-to-date
  21579. (lambda (&optional error)
  21580. (calendar-gregorian-from-absolute
  21581. (get-text-property point 'day))))
  21582. (call-interactively cmd))
  21583. (fset 'calendar-cursor-to-date oldf)))))
  21584. (defun org-agenda-execute-calendar-command (cmd)
  21585. "Execute a calendar command from the agenda, with the date associated to
  21586. the cursor position."
  21587. (org-agenda-check-type t 'agenda 'timeline)
  21588. (require 'diary-lib)
  21589. (unless (get-text-property (point) 'day)
  21590. (error "Don't know which date to use for calendar command"))
  21591. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21592. (point (point))
  21593. (date (calendar-gregorian-from-absolute
  21594. (get-text-property point 'day)))
  21595. ;; the following 3 vars are needed in the calendar
  21596. (displayed-day (extract-calendar-day date))
  21597. (displayed-month (extract-calendar-month date))
  21598. (displayed-year (extract-calendar-year date)))
  21599. (unwind-protect
  21600. (progn
  21601. (fset 'calendar-cursor-to-date
  21602. (lambda (&optional error)
  21603. (calendar-gregorian-from-absolute
  21604. (get-text-property point 'day))))
  21605. (call-interactively cmd))
  21606. (fset 'calendar-cursor-to-date oldf))))
  21607. (defun org-agenda-phases-of-moon ()
  21608. "Display the phases of the moon for the 3 months around the cursor date."
  21609. (interactive)
  21610. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21611. (defun org-agenda-holidays ()
  21612. "Display the holidays for the 3 months around the cursor date."
  21613. (interactive)
  21614. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21615. (defun org-agenda-sunrise-sunset (arg)
  21616. "Display sunrise and sunset for the cursor date.
  21617. Latitude and longitude can be specified with the variables
  21618. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21619. argument, latitude and longitude will be prompted for."
  21620. (interactive "P")
  21621. (let ((calendar-longitude (if arg nil calendar-longitude))
  21622. (calendar-latitude (if arg nil calendar-latitude))
  21623. (calendar-location-name
  21624. (if arg "the given coordinates" calendar-location-name)))
  21625. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21626. (defun org-agenda-goto-calendar ()
  21627. "Open the Emacs calendar with the date at the cursor."
  21628. (interactive)
  21629. (org-agenda-check-type t 'agenda 'timeline)
  21630. (let* ((day (or (get-text-property (point) 'day)
  21631. (error "Don't know which date to open in calendar")))
  21632. (date (calendar-gregorian-from-absolute day))
  21633. (calendar-move-hook nil)
  21634. (view-calendar-holidays-initially nil)
  21635. (view-diary-entries-initially nil))
  21636. (calendar)
  21637. (calendar-goto-date date)))
  21638. (defun org-calendar-goto-agenda ()
  21639. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21640. This is a command that has to be installed in `calendar-mode-map'."
  21641. (interactive)
  21642. (org-agenda-list nil (calendar-absolute-from-gregorian
  21643. (calendar-cursor-to-date))
  21644. nil))
  21645. (defun org-agenda-convert-date ()
  21646. (interactive)
  21647. (org-agenda-check-type t 'agenda 'timeline)
  21648. (let ((day (get-text-property (point) 'day))
  21649. date s)
  21650. (unless day
  21651. (error "Don't know which date to convert"))
  21652. (setq date (calendar-gregorian-from-absolute day))
  21653. (setq s (concat
  21654. "Gregorian: " (calendar-date-string date) "\n"
  21655. "ISO: " (calendar-iso-date-string date) "\n"
  21656. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21657. "Julian: " (calendar-julian-date-string date) "\n"
  21658. "Astron. JD: " (calendar-astro-date-string date)
  21659. " (Julian date number at noon UTC)\n"
  21660. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21661. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21662. "French: " (calendar-french-date-string date) "\n"
  21663. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21664. "Mayan: " (calendar-mayan-date-string date) "\n"
  21665. "Coptic: " (calendar-coptic-date-string date) "\n"
  21666. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21667. "Persian: " (calendar-persian-date-string date) "\n"
  21668. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21669. (with-output-to-temp-buffer "*Dates*"
  21670. (princ s))
  21671. (if (fboundp 'fit-window-to-buffer)
  21672. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21673. ;;;; Embedded LaTeX
  21674. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21675. "Keymap for the minor `org-cdlatex-mode'.")
  21676. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21677. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21678. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21679. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21680. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21681. (defvar org-cdlatex-texmathp-advice-is-done nil
  21682. "Flag remembering if we have applied the advice to texmathp already.")
  21683. (define-minor-mode org-cdlatex-mode
  21684. "Toggle the minor `org-cdlatex-mode'.
  21685. This mode supports entering LaTeX environment and math in LaTeX fragments
  21686. in Org-mode.
  21687. \\{org-cdlatex-mode-map}"
  21688. nil " OCDL" nil
  21689. (when org-cdlatex-mode (require 'cdlatex))
  21690. (unless org-cdlatex-texmathp-advice-is-done
  21691. (setq org-cdlatex-texmathp-advice-is-done t)
  21692. (defadvice texmathp (around org-math-always-on activate)
  21693. "Always return t in org-mode buffers.
  21694. This is because we want to insert math symbols without dollars even outside
  21695. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21696. en embedded LaTeX fragement, let texmathp do its job.
  21697. \\[org-cdlatex-mode-map]"
  21698. (interactive)
  21699. (let (p)
  21700. (cond
  21701. ((not (org-mode-p)) ad-do-it)
  21702. ((eq this-command 'cdlatex-math-symbol)
  21703. (setq ad-return-value t
  21704. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21705. (t
  21706. (let ((p (org-inside-LaTeX-fragment-p)))
  21707. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21708. (setq ad-return-value t
  21709. texmathp-why '("Org-mode embedded math" . 0))
  21710. (if p ad-do-it)))))))))
  21711. (defun turn-on-org-cdlatex ()
  21712. "Unconditionally turn on `org-cdlatex-mode'."
  21713. (org-cdlatex-mode 1))
  21714. (defun org-inside-LaTeX-fragment-p ()
  21715. "Test if point is inside a LaTeX fragment.
  21716. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21717. sequence appearing also before point.
  21718. Even though the matchers for math are configurable, this function assumes
  21719. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21720. delimiters are skipped when they have been removed by customization.
  21721. The return value is nil, or a cons cell with the delimiter and
  21722. and the position of this delimiter.
  21723. This function does a reasonably good job, but can locally be fooled by
  21724. for example currency specifications. For example it will assume being in
  21725. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21726. fragments that are properly closed, but during editing, we have to live
  21727. with the uncertainty caused by missing closing delimiters. This function
  21728. looks only before point, not after."
  21729. (catch 'exit
  21730. (let ((pos (point))
  21731. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21732. (lim (progn
  21733. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21734. (point)))
  21735. dd-on str (start 0) m re)
  21736. (goto-char pos)
  21737. (when dodollar
  21738. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21739. re (nth 1 (assoc "$" org-latex-regexps)))
  21740. (while (string-match re str start)
  21741. (cond
  21742. ((= (match-end 0) (length str))
  21743. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21744. ((= (match-end 0) (- (length str) 5))
  21745. (throw 'exit nil))
  21746. (t (setq start (match-end 0))))))
  21747. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21748. (goto-char pos)
  21749. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21750. (and (match-beginning 2) (throw 'exit nil))
  21751. ;; count $$
  21752. (while (re-search-backward "\\$\\$" lim t)
  21753. (setq dd-on (not dd-on)))
  21754. (goto-char pos)
  21755. (if dd-on (cons "$$" m))))))
  21756. (defun org-try-cdlatex-tab ()
  21757. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21758. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21759. - inside a LaTeX fragment, or
  21760. - after the first word in a line, where an abbreviation expansion could
  21761. insert a LaTeX environment."
  21762. (when org-cdlatex-mode
  21763. (cond
  21764. ((save-excursion
  21765. (skip-chars-backward "a-zA-Z0-9*")
  21766. (skip-chars-backward " \t")
  21767. (bolp))
  21768. (cdlatex-tab) t)
  21769. ((org-inside-LaTeX-fragment-p)
  21770. (cdlatex-tab) t)
  21771. (t nil))))
  21772. (defun org-cdlatex-underscore-caret (&optional arg)
  21773. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21774. Revert to the normal definition outside of these fragments."
  21775. (interactive "P")
  21776. (if (org-inside-LaTeX-fragment-p)
  21777. (call-interactively 'cdlatex-sub-superscript)
  21778. (let (org-cdlatex-mode)
  21779. (call-interactively (key-binding (vector last-input-event))))))
  21780. (defun org-cdlatex-math-modify (&optional arg)
  21781. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21782. Revert to the normal definition outside of these fragments."
  21783. (interactive "P")
  21784. (if (org-inside-LaTeX-fragment-p)
  21785. (call-interactively 'cdlatex-math-modify)
  21786. (let (org-cdlatex-mode)
  21787. (call-interactively (key-binding (vector last-input-event))))))
  21788. (defvar org-latex-fragment-image-overlays nil
  21789. "List of overlays carrying the images of latex fragments.")
  21790. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21791. (defun org-remove-latex-fragment-image-overlays ()
  21792. "Remove all overlays with LaTeX fragment images in current buffer."
  21793. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21794. (setq org-latex-fragment-image-overlays nil))
  21795. (defun org-preview-latex-fragment (&optional subtree)
  21796. "Preview the LaTeX fragment at point, or all locally or globally.
  21797. If the cursor is in a LaTeX fragment, create the image and overlay
  21798. it over the source code. If there is no fragment at point, display
  21799. all fragments in the current text, from one headline to the next. With
  21800. prefix SUBTREE, display all fragments in the current subtree. With a
  21801. double prefix `C-u C-u', or when the cursor is before the first headline,
  21802. display all fragments in the buffer.
  21803. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21804. (interactive "P")
  21805. (org-remove-latex-fragment-image-overlays)
  21806. (save-excursion
  21807. (save-restriction
  21808. (let (beg end at msg)
  21809. (cond
  21810. ((or (equal subtree '(16))
  21811. (not (save-excursion
  21812. (re-search-backward (concat "^" outline-regexp) nil t))))
  21813. (setq beg (point-min) end (point-max)
  21814. msg "Creating images for buffer...%s"))
  21815. ((equal subtree '(4))
  21816. (org-back-to-heading)
  21817. (setq beg (point) end (org-end-of-subtree t)
  21818. msg "Creating images for subtree...%s"))
  21819. (t
  21820. (if (setq at (org-inside-LaTeX-fragment-p))
  21821. (goto-char (max (point-min) (- (cdr at) 2)))
  21822. (org-back-to-heading))
  21823. (setq beg (point) end (progn (outline-next-heading) (point))
  21824. msg (if at "Creating image...%s"
  21825. "Creating images for entry...%s"))))
  21826. (message msg "")
  21827. (narrow-to-region beg end)
  21828. (goto-char beg)
  21829. (org-format-latex
  21830. (concat "ltxpng/" (file-name-sans-extension
  21831. (file-name-nondirectory
  21832. buffer-file-name)))
  21833. default-directory 'overlays msg at 'forbuffer)
  21834. (message msg "done. Use `C-c C-c' to remove images.")))))
  21835. (defvar org-latex-regexps
  21836. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21837. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21838. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21839. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21840. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21841. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21842. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21843. "Regular expressions for matching embedded LaTeX.")
  21844. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21845. "Replace LaTeX fragments with links to an image, and produce images."
  21846. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21847. (let* ((prefixnodir (file-name-nondirectory prefix))
  21848. (absprefix (expand-file-name prefix dir))
  21849. (todir (file-name-directory absprefix))
  21850. (opt org-format-latex-options)
  21851. (matchers (plist-get opt :matchers))
  21852. (re-list org-latex-regexps)
  21853. (cnt 0) txt link beg end re e checkdir
  21854. m n block linkfile movefile ov)
  21855. ;; Check if there are old images files with this prefix, and remove them
  21856. (when (file-directory-p todir)
  21857. (mapc 'delete-file
  21858. (directory-files
  21859. todir 'full
  21860. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21861. ;; Check the different regular expressions
  21862. (while (setq e (pop re-list))
  21863. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21864. block (if (nth 3 e) "\n\n" ""))
  21865. (when (member m matchers)
  21866. (goto-char (point-min))
  21867. (while (re-search-forward re nil t)
  21868. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21869. (setq txt (match-string n)
  21870. beg (match-beginning n) end (match-end n)
  21871. cnt (1+ cnt)
  21872. linkfile (format "%s_%04d.png" prefix cnt)
  21873. movefile (format "%s_%04d.png" absprefix cnt)
  21874. link (concat block "[[file:" linkfile "]]" block))
  21875. (if msg (message msg cnt))
  21876. (goto-char beg)
  21877. (unless checkdir ; make sure the directory exists
  21878. (setq checkdir t)
  21879. (or (file-directory-p todir) (make-directory todir)))
  21880. (org-create-formula-image
  21881. txt movefile opt forbuffer)
  21882. (if overlays
  21883. (progn
  21884. (setq ov (org-make-overlay beg end))
  21885. (if (featurep 'xemacs)
  21886. (progn
  21887. (org-overlay-put ov 'invisible t)
  21888. (org-overlay-put
  21889. ov 'end-glyph
  21890. (make-glyph (vector 'png :file movefile))))
  21891. (org-overlay-put
  21892. ov 'display
  21893. (list 'image :type 'png :file movefile :ascent 'center)))
  21894. (push ov org-latex-fragment-image-overlays)
  21895. (goto-char end))
  21896. (delete-region beg end)
  21897. (insert link))))))))
  21898. ;; This function borrows from Ganesh Swami's latex2png.el
  21899. (defun org-create-formula-image (string tofile options buffer)
  21900. (let* ((tmpdir (if (featurep 'xemacs)
  21901. (temp-directory)
  21902. temporary-file-directory))
  21903. (texfilebase (make-temp-name
  21904. (expand-file-name "orgtex" tmpdir)))
  21905. (texfile (concat texfilebase ".tex"))
  21906. (dvifile (concat texfilebase ".dvi"))
  21907. (pngfile (concat texfilebase ".png"))
  21908. (fnh (face-attribute 'default :height nil))
  21909. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21910. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21911. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21912. "Black"))
  21913. (bg (or (plist-get options (if buffer :background :html-background))
  21914. "Transparent")))
  21915. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21916. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21917. (with-temp-file texfile
  21918. (insert org-format-latex-header
  21919. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21920. (let ((dir default-directory))
  21921. (condition-case nil
  21922. (progn
  21923. (cd tmpdir)
  21924. (call-process "latex" nil nil nil texfile))
  21925. (error nil))
  21926. (cd dir))
  21927. (if (not (file-exists-p dvifile))
  21928. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21929. (call-process "dvipng" nil nil nil
  21930. "-E" "-fg" fg "-bg" bg
  21931. "-D" dpi
  21932. ;;"-x" scale "-y" scale
  21933. "-T" "tight"
  21934. "-o" pngfile
  21935. dvifile)
  21936. (if (not (file-exists-p pngfile))
  21937. (progn (message "Failed to create png file from %s" texfile) nil)
  21938. ;; Use the requested file name and clean up
  21939. (copy-file pngfile tofile 'replace)
  21940. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21941. (delete-file (concat texfilebase e)))
  21942. pngfile))))
  21943. (defun org-dvipng-color (attr)
  21944. "Return an rgb color specification for dvipng."
  21945. (apply 'format "rgb %s %s %s"
  21946. (mapcar 'org-normalize-color
  21947. (color-values (face-attribute 'default attr nil)))))
  21948. (defun org-normalize-color (value)
  21949. "Return string to be used as color value for an RGB component."
  21950. (format "%g" (/ value 65535.0)))
  21951. ;;;; Exporting
  21952. ;;; Variables, constants, and parameter plists
  21953. (defconst org-level-max 20)
  21954. (defvar org-export-html-preamble nil
  21955. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21956. (defvar org-export-html-postamble nil
  21957. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21958. (defvar org-export-html-auto-preamble t
  21959. "Should default preamble be inserted? Set by publishing functions.")
  21960. (defvar org-export-html-auto-postamble t
  21961. "Should default postamble be inserted? Set by publishing functions.")
  21962. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21963. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21964. (defconst org-export-plist-vars
  21965. '((:language . org-export-default-language)
  21966. (:customtime . org-display-custom-times)
  21967. (:headline-levels . org-export-headline-levels)
  21968. (:section-numbers . org-export-with-section-numbers)
  21969. (:table-of-contents . org-export-with-toc)
  21970. (:preserve-breaks . org-export-preserve-breaks)
  21971. (:archived-trees . org-export-with-archived-trees)
  21972. (:emphasize . org-export-with-emphasize)
  21973. (:sub-superscript . org-export-with-sub-superscripts)
  21974. (:special-strings . org-export-with-special-strings)
  21975. (:footnotes . org-export-with-footnotes)
  21976. (:drawers . org-export-with-drawers)
  21977. (:tags . org-export-with-tags)
  21978. (:TeX-macros . org-export-with-TeX-macros)
  21979. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21980. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21981. (:fixed-width . org-export-with-fixed-width)
  21982. (:timestamps . org-export-with-timestamps)
  21983. (:author-info . org-export-author-info)
  21984. (:time-stamp-file . org-export-time-stamp-file)
  21985. (:tables . org-export-with-tables)
  21986. (:table-auto-headline . org-export-highlight-first-table-line)
  21987. (:style . org-export-html-style)
  21988. (:agenda-style . org-agenda-export-html-style)
  21989. (:convert-org-links . org-export-html-link-org-files-as-html)
  21990. (:inline-images . org-export-html-inline-images)
  21991. (:html-extension . org-export-html-extension)
  21992. (:html-table-tag . org-export-html-table-tag)
  21993. (:expand-quoted-html . org-export-html-expand)
  21994. (:timestamp . org-export-html-with-timestamp)
  21995. (:publishing-directory . org-export-publishing-directory)
  21996. (:preamble . org-export-html-preamble)
  21997. (:postamble . org-export-html-postamble)
  21998. (:auto-preamble . org-export-html-auto-preamble)
  21999. (:auto-postamble . org-export-html-auto-postamble)
  22000. (:author . user-full-name)
  22001. (:email . user-mail-address)))
  22002. (defun org-default-export-plist ()
  22003. "Return the property list with default settings for the export variables."
  22004. (let ((l org-export-plist-vars) rtn e)
  22005. (while (setq e (pop l))
  22006. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  22007. rtn))
  22008. (defun org-infile-export-plist ()
  22009. "Return the property list with file-local settings for export."
  22010. (save-excursion
  22011. (save-restriction
  22012. (widen)
  22013. (goto-char 0)
  22014. (let ((re (org-make-options-regexp
  22015. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  22016. p key val text options)
  22017. (while (re-search-forward re nil t)
  22018. (setq key (org-match-string-no-properties 1)
  22019. val (org-match-string-no-properties 2))
  22020. (cond
  22021. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  22022. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  22023. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  22024. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  22025. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  22026. ((string-equal key "TEXT")
  22027. (setq text (if text (concat text "\n" val) val)))
  22028. ((string-equal key "OPTIONS") (setq options val))))
  22029. (setq p (plist-put p :text text))
  22030. (when options
  22031. (let ((op '(("H" . :headline-levels)
  22032. ("num" . :section-numbers)
  22033. ("toc" . :table-of-contents)
  22034. ("\\n" . :preserve-breaks)
  22035. ("@" . :expand-quoted-html)
  22036. (":" . :fixed-width)
  22037. ("|" . :tables)
  22038. ("^" . :sub-superscript)
  22039. ("-" . :special-strings)
  22040. ("f" . :footnotes)
  22041. ("d" . :drawers)
  22042. ("tags" . :tags)
  22043. ("*" . :emphasize)
  22044. ("TeX" . :TeX-macros)
  22045. ("LaTeX" . :LaTeX-fragments)
  22046. ("skip" . :skip-before-1st-heading)
  22047. ("author" . :author-info)
  22048. ("timestamp" . :time-stamp-file)))
  22049. o)
  22050. (while (setq o (pop op))
  22051. (if (string-match (concat (regexp-quote (car o))
  22052. ":\\([^ \t\n\r;,.]*\\)")
  22053. options)
  22054. (setq p (plist-put p (cdr o)
  22055. (car (read-from-string
  22056. (match-string 1 options)))))))))
  22057. p))))
  22058. (defun org-export-directory (type plist)
  22059. (let* ((val (plist-get plist :publishing-directory))
  22060. (dir (if (listp val)
  22061. (or (cdr (assoc type val)) ".")
  22062. val)))
  22063. dir))
  22064. (defun org-skip-comments (lines)
  22065. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  22066. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  22067. (re2 "^\\(\\*+\\)[ \t\n\r]")
  22068. (case-fold-search nil)
  22069. rtn line level)
  22070. (while (setq line (pop lines))
  22071. (cond
  22072. ((and (string-match re1 line)
  22073. (setq level (- (match-end 1) (match-beginning 1))))
  22074. ;; Beginning of a COMMENT subtree. Skip it.
  22075. (while (and (setq line (pop lines))
  22076. (or (not (string-match re2 line))
  22077. (> (- (match-end 1) (match-beginning 1)) level))))
  22078. (setq lines (cons line lines)))
  22079. ((string-match "^#" line)
  22080. ;; an ordinary comment line
  22081. )
  22082. ((and org-export-table-remove-special-lines
  22083. (string-match "^[ \t]*|" line)
  22084. (or (string-match "^[ \t]*| *[!_^] *|" line)
  22085. (and (string-match "| *<[0-9]+> *|" line)
  22086. (not (string-match "| *[^ <|]" line)))))
  22087. ;; a special table line that should be removed
  22088. )
  22089. (t (setq rtn (cons line rtn)))))
  22090. (nreverse rtn)))
  22091. (defun org-export (&optional arg)
  22092. (interactive)
  22093. (let ((help "[t] insert the export option template
  22094. \[v] limit export to visible part of outline tree
  22095. \[a] export as ASCII
  22096. \[h] export as HTML
  22097. \[H] export as HTML to temporary buffer
  22098. \[R] export region as HTML
  22099. \[b] export as HTML and browse immediately
  22100. \[x] export as XOXO
  22101. \[l] export as LaTeX
  22102. \[L] export as LaTeX to temporary buffer
  22103. \[i] export current file as iCalendar file
  22104. \[I] export all agenda files as iCalendar files
  22105. \[c] export agenda files into combined iCalendar file
  22106. \[F] publish current file
  22107. \[P] publish current project
  22108. \[X] publish... (project will be prompted for)
  22109. \[A] publish all projects")
  22110. (cmds
  22111. '((?t . org-insert-export-options-template)
  22112. (?v . org-export-visible)
  22113. (?a . org-export-as-ascii)
  22114. (?h . org-export-as-html)
  22115. (?b . org-export-as-html-and-open)
  22116. (?H . org-export-as-html-to-buffer)
  22117. (?R . org-export-region-as-html)
  22118. (?x . org-export-as-xoxo)
  22119. (?l . org-export-as-latex)
  22120. (?L . org-export-as-latex-to-buffer)
  22121. (?i . org-export-icalendar-this-file)
  22122. (?I . org-export-icalendar-all-agenda-files)
  22123. (?c . org-export-icalendar-combine-agenda-files)
  22124. (?F . org-publish-current-file)
  22125. (?P . org-publish-current-project)
  22126. (?X . org-publish)
  22127. (?A . org-publish-all)))
  22128. r1 r2 ass)
  22129. (save-window-excursion
  22130. (delete-other-windows)
  22131. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  22132. (princ help))
  22133. (message "Select command: ")
  22134. (setq r1 (read-char-exclusive)))
  22135. (setq r2 (if (< r1 27) (+ r1 96) r1))
  22136. (if (setq ass (assq r2 cmds))
  22137. (call-interactively (cdr ass))
  22138. (error "No command associated with key %c" r1))))
  22139. (defconst org-html-entities
  22140. '(("nbsp")
  22141. ("iexcl")
  22142. ("cent")
  22143. ("pound")
  22144. ("curren")
  22145. ("yen")
  22146. ("brvbar")
  22147. ("vert" . "&#124;")
  22148. ("sect")
  22149. ("uml")
  22150. ("copy")
  22151. ("ordf")
  22152. ("laquo")
  22153. ("not")
  22154. ("shy")
  22155. ("reg")
  22156. ("macr")
  22157. ("deg")
  22158. ("plusmn")
  22159. ("sup2")
  22160. ("sup3")
  22161. ("acute")
  22162. ("micro")
  22163. ("para")
  22164. ("middot")
  22165. ("odot"."o")
  22166. ("star"."*")
  22167. ("cedil")
  22168. ("sup1")
  22169. ("ordm")
  22170. ("raquo")
  22171. ("frac14")
  22172. ("frac12")
  22173. ("frac34")
  22174. ("iquest")
  22175. ("Agrave")
  22176. ("Aacute")
  22177. ("Acirc")
  22178. ("Atilde")
  22179. ("Auml")
  22180. ("Aring") ("AA"."&Aring;")
  22181. ("AElig")
  22182. ("Ccedil")
  22183. ("Egrave")
  22184. ("Eacute")
  22185. ("Ecirc")
  22186. ("Euml")
  22187. ("Igrave")
  22188. ("Iacute")
  22189. ("Icirc")
  22190. ("Iuml")
  22191. ("ETH")
  22192. ("Ntilde")
  22193. ("Ograve")
  22194. ("Oacute")
  22195. ("Ocirc")
  22196. ("Otilde")
  22197. ("Ouml")
  22198. ("times")
  22199. ("Oslash")
  22200. ("Ugrave")
  22201. ("Uacute")
  22202. ("Ucirc")
  22203. ("Uuml")
  22204. ("Yacute")
  22205. ("THORN")
  22206. ("szlig")
  22207. ("agrave")
  22208. ("aacute")
  22209. ("acirc")
  22210. ("atilde")
  22211. ("auml")
  22212. ("aring")
  22213. ("aelig")
  22214. ("ccedil")
  22215. ("egrave")
  22216. ("eacute")
  22217. ("ecirc")
  22218. ("euml")
  22219. ("igrave")
  22220. ("iacute")
  22221. ("icirc")
  22222. ("iuml")
  22223. ("eth")
  22224. ("ntilde")
  22225. ("ograve")
  22226. ("oacute")
  22227. ("ocirc")
  22228. ("otilde")
  22229. ("ouml")
  22230. ("divide")
  22231. ("oslash")
  22232. ("ugrave")
  22233. ("uacute")
  22234. ("ucirc")
  22235. ("uuml")
  22236. ("yacute")
  22237. ("thorn")
  22238. ("yuml")
  22239. ("fnof")
  22240. ("Alpha")
  22241. ("Beta")
  22242. ("Gamma")
  22243. ("Delta")
  22244. ("Epsilon")
  22245. ("Zeta")
  22246. ("Eta")
  22247. ("Theta")
  22248. ("Iota")
  22249. ("Kappa")
  22250. ("Lambda")
  22251. ("Mu")
  22252. ("Nu")
  22253. ("Xi")
  22254. ("Omicron")
  22255. ("Pi")
  22256. ("Rho")
  22257. ("Sigma")
  22258. ("Tau")
  22259. ("Upsilon")
  22260. ("Phi")
  22261. ("Chi")
  22262. ("Psi")
  22263. ("Omega")
  22264. ("alpha")
  22265. ("beta")
  22266. ("gamma")
  22267. ("delta")
  22268. ("epsilon")
  22269. ("varepsilon"."&epsilon;")
  22270. ("zeta")
  22271. ("eta")
  22272. ("theta")
  22273. ("iota")
  22274. ("kappa")
  22275. ("lambda")
  22276. ("mu")
  22277. ("nu")
  22278. ("xi")
  22279. ("omicron")
  22280. ("pi")
  22281. ("rho")
  22282. ("sigmaf") ("varsigma"."&sigmaf;")
  22283. ("sigma")
  22284. ("tau")
  22285. ("upsilon")
  22286. ("phi")
  22287. ("chi")
  22288. ("psi")
  22289. ("omega")
  22290. ("thetasym") ("vartheta"."&thetasym;")
  22291. ("upsih")
  22292. ("piv")
  22293. ("bull") ("bullet"."&bull;")
  22294. ("hellip") ("dots"."&hellip;")
  22295. ("prime")
  22296. ("Prime")
  22297. ("oline")
  22298. ("frasl")
  22299. ("weierp")
  22300. ("image")
  22301. ("real")
  22302. ("trade")
  22303. ("alefsym")
  22304. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  22305. ("uarr") ("uparrow"."&uarr;")
  22306. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  22307. ("darr")("downarrow"."&darr;")
  22308. ("harr") ("leftrightarrow"."&harr;")
  22309. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  22310. ("lArr") ("Leftarrow"."&lArr;")
  22311. ("uArr") ("Uparrow"."&uArr;")
  22312. ("rArr") ("Rightarrow"."&rArr;")
  22313. ("dArr") ("Downarrow"."&dArr;")
  22314. ("hArr") ("Leftrightarrow"."&hArr;")
  22315. ("forall")
  22316. ("part") ("partial"."&part;")
  22317. ("exist") ("exists"."&exist;")
  22318. ("empty") ("emptyset"."&empty;")
  22319. ("nabla")
  22320. ("isin") ("in"."&isin;")
  22321. ("notin")
  22322. ("ni")
  22323. ("prod")
  22324. ("sum")
  22325. ("minus")
  22326. ("lowast") ("ast"."&lowast;")
  22327. ("radic")
  22328. ("prop") ("proptp"."&prop;")
  22329. ("infin") ("infty"."&infin;")
  22330. ("ang") ("angle"."&ang;")
  22331. ("and") ("wedge"."&and;")
  22332. ("or") ("vee"."&or;")
  22333. ("cap")
  22334. ("cup")
  22335. ("int")
  22336. ("there4")
  22337. ("sim")
  22338. ("cong") ("simeq"."&cong;")
  22339. ("asymp")("approx"."&asymp;")
  22340. ("ne") ("neq"."&ne;")
  22341. ("equiv")
  22342. ("le")
  22343. ("ge")
  22344. ("sub") ("subset"."&sub;")
  22345. ("sup") ("supset"."&sup;")
  22346. ("nsub")
  22347. ("sube")
  22348. ("supe")
  22349. ("oplus")
  22350. ("otimes")
  22351. ("perp")
  22352. ("sdot") ("cdot"."&sdot;")
  22353. ("lceil")
  22354. ("rceil")
  22355. ("lfloor")
  22356. ("rfloor")
  22357. ("lang")
  22358. ("rang")
  22359. ("loz") ("Diamond"."&loz;")
  22360. ("spades") ("spadesuit"."&spades;")
  22361. ("clubs") ("clubsuit"."&clubs;")
  22362. ("hearts") ("diamondsuit"."&hearts;")
  22363. ("diams") ("diamondsuit"."&diams;")
  22364. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  22365. ("quot")
  22366. ("amp")
  22367. ("lt")
  22368. ("gt")
  22369. ("OElig")
  22370. ("oelig")
  22371. ("Scaron")
  22372. ("scaron")
  22373. ("Yuml")
  22374. ("circ")
  22375. ("tilde")
  22376. ("ensp")
  22377. ("emsp")
  22378. ("thinsp")
  22379. ("zwnj")
  22380. ("zwj")
  22381. ("lrm")
  22382. ("rlm")
  22383. ("ndash")
  22384. ("mdash")
  22385. ("lsquo")
  22386. ("rsquo")
  22387. ("sbquo")
  22388. ("ldquo")
  22389. ("rdquo")
  22390. ("bdquo")
  22391. ("dagger")
  22392. ("Dagger")
  22393. ("permil")
  22394. ("lsaquo")
  22395. ("rsaquo")
  22396. ("euro")
  22397. ("arccos"."arccos")
  22398. ("arcsin"."arcsin")
  22399. ("arctan"."arctan")
  22400. ("arg"."arg")
  22401. ("cos"."cos")
  22402. ("cosh"."cosh")
  22403. ("cot"."cot")
  22404. ("coth"."coth")
  22405. ("csc"."csc")
  22406. ("deg"."deg")
  22407. ("det"."det")
  22408. ("dim"."dim")
  22409. ("exp"."exp")
  22410. ("gcd"."gcd")
  22411. ("hom"."hom")
  22412. ("inf"."inf")
  22413. ("ker"."ker")
  22414. ("lg"."lg")
  22415. ("lim"."lim")
  22416. ("liminf"."liminf")
  22417. ("limsup"."limsup")
  22418. ("ln"."ln")
  22419. ("log"."log")
  22420. ("max"."max")
  22421. ("min"."min")
  22422. ("Pr"."Pr")
  22423. ("sec"."sec")
  22424. ("sin"."sin")
  22425. ("sinh"."sinh")
  22426. ("sup"."sup")
  22427. ("tan"."tan")
  22428. ("tanh"."tanh")
  22429. )
  22430. "Entities for TeX->HTML translation.
  22431. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  22432. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  22433. In that case, \"\\ent\" will be translated to \"&other;\".
  22434. The list contains HTML entities for Latin-1, Greek and other symbols.
  22435. It is supplemented by a number of commonly used TeX macros with appropriate
  22436. translations. There is currently no way for users to extend this.")
  22437. ;;; General functions for all backends
  22438. (defun org-cleaned-string-for-export (string &rest parameters)
  22439. "Cleanup a buffer STRING so that links can be created safely."
  22440. (interactive)
  22441. (let* ((re-radio (and org-target-link-regexp
  22442. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  22443. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  22444. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  22445. (re-archive (concat ":" org-archive-tag ":"))
  22446. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  22447. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  22448. (htmlp (plist-get parameters :for-html))
  22449. (asciip (plist-get parameters :for-ascii))
  22450. (latexp (plist-get parameters :for-LaTeX))
  22451. (commentsp (plist-get parameters :comments))
  22452. (archived-trees (plist-get parameters :archived-trees))
  22453. (inhibit-read-only t)
  22454. (drawers org-drawers)
  22455. (exp-drawers (plist-get parameters :drawers))
  22456. (outline-regexp "\\*+ ")
  22457. a b xx
  22458. rtn p)
  22459. (with-current-buffer (get-buffer-create " org-mode-tmp")
  22460. (erase-buffer)
  22461. (insert string)
  22462. ;; Remove license-to-kill stuff
  22463. (while (setq p (text-property-any (point-min) (point-max)
  22464. :org-license-to-kill t))
  22465. (delete-region p (next-single-property-change p :org-license-to-kill)))
  22466. (let ((org-inhibit-startup t)) (org-mode))
  22467. (untabify (point-min) (point-max))
  22468. ;; Get rid of drawers
  22469. (unless (eq t exp-drawers)
  22470. (goto-char (point-min))
  22471. (let ((re (concat "^[ \t]*:\\("
  22472. (mapconcat
  22473. 'identity
  22474. (org-delete-all exp-drawers
  22475. (copy-sequence drawers))
  22476. "\\|")
  22477. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22478. (while (re-search-forward re nil t)
  22479. (replace-match ""))))
  22480. ;; Get the correct stuff before the first headline
  22481. (when (plist-get parameters :skip-before-1st-heading)
  22482. (goto-char (point-min))
  22483. (when (re-search-forward "^\\*+[ \t]" nil t)
  22484. (delete-region (point-min) (match-beginning 0))
  22485. (goto-char (point-min))
  22486. (insert "\n")))
  22487. (when (plist-get parameters :add-text)
  22488. (goto-char (point-min))
  22489. (insert (plist-get parameters :add-text) "\n"))
  22490. ;; Get rid of archived trees
  22491. (when (not (eq archived-trees t))
  22492. (goto-char (point-min))
  22493. (while (re-search-forward re-archive nil t)
  22494. (if (not (org-on-heading-p t))
  22495. (org-end-of-subtree t)
  22496. (beginning-of-line 1)
  22497. (setq a (if archived-trees
  22498. (1+ (point-at-eol)) (point))
  22499. b (org-end-of-subtree t))
  22500. (if (> b a) (delete-region a b)))))
  22501. ;; Find targets in comments and move them out of comments,
  22502. ;; but mark them as targets that should be invisible
  22503. (goto-char (point-min))
  22504. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22505. (replace-match "\\1(INVISIBLE)"))
  22506. ;; Protect backend specific stuff, throw away the others.
  22507. (let ((formatters
  22508. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22509. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22510. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22511. fmt)
  22512. (goto-char (point-min))
  22513. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22514. (goto-char (match-end 0))
  22515. (while (not (looking-at "#\\+END_EXAMPLE"))
  22516. (insert ": ")
  22517. (beginning-of-line 2)))
  22518. (goto-char (point-min))
  22519. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22520. (add-text-properties (match-beginning 0) (match-end 0)
  22521. '(org-protected t)))
  22522. (while formatters
  22523. (setq fmt (pop formatters))
  22524. (when (car fmt)
  22525. (goto-char (point-min))
  22526. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22527. ":[ \t]*\\(.*\\)") nil t)
  22528. (replace-match "\\1" t)
  22529. (add-text-properties
  22530. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22531. '(org-protected t))))
  22532. (goto-char (point-min))
  22533. (while (re-search-forward
  22534. (concat "^#\\+"
  22535. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22536. (cadddr fmt) "\\>.*\n?") nil t)
  22537. (if (car fmt)
  22538. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22539. '(org-protected t))
  22540. (delete-region (match-beginning 0) (match-end 0))))))
  22541. ;; Protect quoted subtrees
  22542. (goto-char (point-min))
  22543. (while (re-search-forward re-quote nil t)
  22544. (goto-char (match-beginning 0))
  22545. (end-of-line 1)
  22546. (add-text-properties (point) (org-end-of-subtree t)
  22547. '(org-protected t)))
  22548. ;; Protect verbatim elements
  22549. (goto-char (point-min))
  22550. (while (re-search-forward org-verbatim-re nil t)
  22551. (add-text-properties (match-beginning 4) (match-end 4)
  22552. '(org-protected t))
  22553. (goto-char (1+ (match-end 4))))
  22554. ;; Remove subtrees that are commented
  22555. (goto-char (point-min))
  22556. (while (re-search-forward re-commented nil t)
  22557. (goto-char (match-beginning 0))
  22558. (delete-region (point) (org-end-of-subtree t)))
  22559. ;; Remove special table lines
  22560. (when org-export-table-remove-special-lines
  22561. (goto-char (point-min))
  22562. (while (re-search-forward "^[ \t]*|" nil t)
  22563. (beginning-of-line 1)
  22564. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22565. (and (looking-at ".*?| *<[0-9]+> *|")
  22566. (not (looking-at ".*?| *[^ <|]"))))
  22567. (delete-region (max (point-min) (1- (point-at-bol)))
  22568. (point-at-eol))
  22569. (end-of-line 1))))
  22570. ;; Specific LaTeX stuff
  22571. (when latexp
  22572. (require 'org-export-latex nil)
  22573. (org-export-latex-cleaned-string))
  22574. (when asciip
  22575. (org-export-ascii-clean-string))
  22576. ;; Specific HTML stuff
  22577. (when htmlp
  22578. ;; Convert LaTeX fragments to images
  22579. (when (plist-get parameters :LaTeX-fragments)
  22580. (org-format-latex
  22581. (concat "ltxpng/" (file-name-sans-extension
  22582. (file-name-nondirectory
  22583. org-current-export-file)))
  22584. org-current-export-dir nil "Creating LaTeX image %s"))
  22585. (message "Exporting..."))
  22586. ;; Remove or replace comments
  22587. (goto-char (point-min))
  22588. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22589. (if commentsp
  22590. (progn (add-text-properties
  22591. (match-beginning 0) (match-end 0) '(org-protected t))
  22592. (replace-match (format commentsp (match-string 1)) t t))
  22593. (replace-match "")))
  22594. ;; Find matches for radio targets and turn them into internal links
  22595. (goto-char (point-min))
  22596. (when re-radio
  22597. (while (re-search-forward re-radio nil t)
  22598. (org-if-unprotected
  22599. (replace-match "\\1[[\\2]]"))))
  22600. ;; Find all links that contain a newline and put them into a single line
  22601. (goto-char (point-min))
  22602. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22603. (org-if-unprotected
  22604. (replace-match "\\1 \\3")
  22605. (goto-char (match-beginning 0))))
  22606. ;; Normalize links: Convert angle and plain links into bracket links
  22607. ;; Expand link abbreviations
  22608. (goto-char (point-min))
  22609. (while (re-search-forward re-plain-link nil t)
  22610. (goto-char (1- (match-end 0)))
  22611. (org-if-unprotected
  22612. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22613. ":" (match-string 3) "]]")))
  22614. ;; added 'org-link face to links
  22615. (put-text-property 0 (length s) 'face 'org-link s)
  22616. (replace-match s t t))))
  22617. (goto-char (point-min))
  22618. (while (re-search-forward re-angle-link nil t)
  22619. (goto-char (1- (match-end 0)))
  22620. (org-if-unprotected
  22621. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22622. ":" (match-string 3) "]]")))
  22623. (put-text-property 0 (length s) 'face 'org-link s)
  22624. (replace-match s t t))))
  22625. (goto-char (point-min))
  22626. (while (re-search-forward org-bracket-link-regexp nil t)
  22627. (org-if-unprotected
  22628. (let* ((s (concat "[[" (setq xx (save-match-data
  22629. (org-link-expand-abbrev (match-string 1))))
  22630. "]"
  22631. (if (match-end 3)
  22632. (match-string 2)
  22633. (concat "[" xx "]"))
  22634. "]")))
  22635. (put-text-property 0 (length s) 'face 'org-link s)
  22636. (replace-match s t t))))
  22637. ;; Find multiline emphasis and put them into single line
  22638. (when (plist-get parameters :emph-multiline)
  22639. (goto-char (point-min))
  22640. (while (re-search-forward org-emph-re nil t)
  22641. (if (not (= (char-after (match-beginning 3))
  22642. (char-after (match-beginning 4))))
  22643. (org-if-unprotected
  22644. (subst-char-in-region (match-beginning 0) (match-end 0)
  22645. ?\n ?\ t)
  22646. (goto-char (1- (match-end 0))))
  22647. (goto-char (1+ (match-beginning 0))))))
  22648. (setq rtn (buffer-string)))
  22649. (kill-buffer " org-mode-tmp")
  22650. rtn))
  22651. (defun org-export-grab-title-from-buffer ()
  22652. "Get a title for the current document, from looking at the buffer."
  22653. (let ((inhibit-read-only t))
  22654. (save-excursion
  22655. (goto-char (point-min))
  22656. (let ((end (save-excursion (outline-next-heading) (point))))
  22657. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22658. ;; Mark the line so that it will not be exported as normal text.
  22659. (org-unmodified
  22660. (add-text-properties (match-beginning 0) (match-end 0)
  22661. (list :org-license-to-kill t)))
  22662. ;; Return the title string
  22663. (org-trim (match-string 0)))))))
  22664. (defun org-export-get-title-from-subtree ()
  22665. "Return subtree title and exclude it from export."
  22666. (let (title (m (mark)))
  22667. (save-excursion
  22668. (goto-char (region-beginning))
  22669. (when (and (org-at-heading-p)
  22670. (>= (org-end-of-subtree t t) (region-end)))
  22671. ;; This is a subtree, we take the title from the first heading
  22672. (goto-char (region-beginning))
  22673. (looking-at org-todo-line-regexp)
  22674. (setq title (match-string 3))
  22675. (org-unmodified
  22676. (add-text-properties (point) (1+ (point-at-eol))
  22677. (list :org-license-to-kill t)))))
  22678. title))
  22679. (defun org-solidify-link-text (s &optional alist)
  22680. "Take link text and make a safe target out of it."
  22681. (save-match-data
  22682. (let* ((rtn
  22683. (mapconcat
  22684. 'identity
  22685. (org-split-string s "[ \t\r\n]+") "--"))
  22686. (a (assoc rtn alist)))
  22687. (or (cdr a) rtn))))
  22688. (defun org-get-min-level (lines)
  22689. "Get the minimum level in LINES."
  22690. (let ((re "^\\(\\*+\\) ") l min)
  22691. (catch 'exit
  22692. (while (setq l (pop lines))
  22693. (if (string-match re l)
  22694. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22695. 1)))
  22696. ;; Variable holding the vector with section numbers
  22697. (defvar org-section-numbers (make-vector org-level-max 0))
  22698. (defun org-init-section-numbers ()
  22699. "Initialize the vector for the section numbers."
  22700. (let* ((level -1)
  22701. (numbers (nreverse (org-split-string "" "\\.")))
  22702. (depth (1- (length org-section-numbers)))
  22703. (i depth) number-string)
  22704. (while (>= i 0)
  22705. (if (> i level)
  22706. (aset org-section-numbers i 0)
  22707. (setq number-string (or (car numbers) "0"))
  22708. (if (string-match "\\`[A-Z]\\'" number-string)
  22709. (aset org-section-numbers i
  22710. (- (string-to-char number-string) ?A -1))
  22711. (aset org-section-numbers i (string-to-number number-string)))
  22712. (pop numbers))
  22713. (setq i (1- i)))))
  22714. (defun org-section-number (&optional level)
  22715. "Return a string with the current section number.
  22716. When LEVEL is non-nil, increase section numbers on that level."
  22717. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22718. (when level
  22719. (when (> level -1)
  22720. (aset org-section-numbers
  22721. level (1+ (aref org-section-numbers level))))
  22722. (setq idx (1+ level))
  22723. (while (<= idx depth)
  22724. (if (not (= idx 1))
  22725. (aset org-section-numbers idx 0))
  22726. (setq idx (1+ idx))))
  22727. (setq idx 0)
  22728. (while (<= idx depth)
  22729. (setq n (aref org-section-numbers idx))
  22730. (setq string (concat string (if (not (string= string "")) "." "")
  22731. (int-to-string n)))
  22732. (setq idx (1+ idx)))
  22733. (save-match-data
  22734. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22735. (setq string (replace-match "" t nil string)))
  22736. (if (string-match "\\(\\.0\\)+\\'" string)
  22737. (setq string (replace-match "" t nil string))))
  22738. string))
  22739. ;;; ASCII export
  22740. (defvar org-last-level nil) ; dynamically scoped variable
  22741. (defvar org-min-level nil) ; dynamically scoped variable
  22742. (defvar org-levels-open nil) ; dynamically scoped parameter
  22743. (defvar org-ascii-current-indentation nil) ; For communication
  22744. (defun org-export-as-ascii (arg)
  22745. "Export the outline as a pretty ASCII file.
  22746. If there is an active region, export only the region.
  22747. The prefix ARG specifies how many levels of the outline should become
  22748. underlined headlines. The default is 3."
  22749. (interactive "P")
  22750. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22751. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22752. (org-infile-export-plist)))
  22753. (region-p (org-region-active-p))
  22754. (subtree-p
  22755. (when region-p
  22756. (save-excursion
  22757. (goto-char (region-beginning))
  22758. (and (org-at-heading-p)
  22759. (>= (org-end-of-subtree t t) (region-end))))))
  22760. (custom-times org-display-custom-times)
  22761. (org-ascii-current-indentation '(0 . 0))
  22762. (level 0) line txt
  22763. (umax nil)
  22764. (umax-toc nil)
  22765. (case-fold-search nil)
  22766. (filename (concat (file-name-as-directory
  22767. (org-export-directory :ascii opt-plist))
  22768. (file-name-sans-extension
  22769. (or (and subtree-p
  22770. (org-entry-get (region-beginning)
  22771. "EXPORT_FILE_NAME" t))
  22772. (file-name-nondirectory buffer-file-name)))
  22773. ".txt"))
  22774. (filename (if (equal (file-truename filename)
  22775. (file-truename buffer-file-name))
  22776. (concat filename ".txt")
  22777. filename))
  22778. (buffer (find-file-noselect filename))
  22779. (org-levels-open (make-vector org-level-max nil))
  22780. (odd org-odd-levels-only)
  22781. (date (plist-get opt-plist :date))
  22782. (author (plist-get opt-plist :author))
  22783. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22784. (plist-get opt-plist :title)
  22785. (and (not
  22786. (plist-get opt-plist :skip-before-1st-heading))
  22787. (org-export-grab-title-from-buffer))
  22788. (file-name-sans-extension
  22789. (file-name-nondirectory buffer-file-name))))
  22790. (email (plist-get opt-plist :email))
  22791. (language (plist-get opt-plist :language))
  22792. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22793. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22794. (todo nil)
  22795. (lang-words nil)
  22796. (region
  22797. (buffer-substring
  22798. (if (org-region-active-p) (region-beginning) (point-min))
  22799. (if (org-region-active-p) (region-end) (point-max))))
  22800. (lines (org-split-string
  22801. (org-cleaned-string-for-export
  22802. region
  22803. :for-ascii t
  22804. :skip-before-1st-heading
  22805. (plist-get opt-plist :skip-before-1st-heading)
  22806. :drawers (plist-get opt-plist :drawers)
  22807. :verbatim-multiline t
  22808. :archived-trees
  22809. (plist-get opt-plist :archived-trees)
  22810. :add-text (plist-get opt-plist :text))
  22811. "\n"))
  22812. thetoc have-headings first-heading-pos
  22813. table-open table-buffer)
  22814. (let ((inhibit-read-only t))
  22815. (org-unmodified
  22816. (remove-text-properties (point-min) (point-max)
  22817. '(:org-license-to-kill t))))
  22818. (setq org-min-level (org-get-min-level lines))
  22819. (setq org-last-level org-min-level)
  22820. (org-init-section-numbers)
  22821. (find-file-noselect filename)
  22822. (setq lang-words (or (assoc language org-export-language-setup)
  22823. (assoc "en" org-export-language-setup)))
  22824. (switch-to-buffer-other-window buffer)
  22825. (erase-buffer)
  22826. (fundamental-mode)
  22827. ;; create local variables for all options, to make sure all called
  22828. ;; functions get the correct information
  22829. (mapc (lambda (x)
  22830. (set (make-local-variable (cdr x))
  22831. (plist-get opt-plist (car x))))
  22832. org-export-plist-vars)
  22833. (org-set-local 'org-odd-levels-only odd)
  22834. (setq umax (if arg (prefix-numeric-value arg)
  22835. org-export-headline-levels))
  22836. (setq umax-toc (if (integerp org-export-with-toc)
  22837. (min org-export-with-toc umax)
  22838. umax))
  22839. ;; File header
  22840. (if title (org-insert-centered title ?=))
  22841. (insert "\n")
  22842. (if (and (or author email)
  22843. org-export-author-info)
  22844. (insert (concat (nth 1 lang-words) ": " (or author "")
  22845. (if email (concat " <" email ">") "")
  22846. "\n")))
  22847. (cond
  22848. ((and date (string-match "%" date))
  22849. (setq date (format-time-string date (current-time))))
  22850. (date)
  22851. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22852. (if (and date org-export-time-stamp-file)
  22853. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22854. (insert "\n\n")
  22855. (if org-export-with-toc
  22856. (progn
  22857. (push (concat (nth 3 lang-words) "\n") thetoc)
  22858. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22859. (mapc '(lambda (line)
  22860. (if (string-match org-todo-line-regexp
  22861. line)
  22862. ;; This is a headline
  22863. (progn
  22864. (setq have-headings t)
  22865. (setq level (- (match-end 1) (match-beginning 1))
  22866. level (org-tr-level level)
  22867. txt (match-string 3 line)
  22868. todo
  22869. (or (and org-export-mark-todo-in-toc
  22870. (match-beginning 2)
  22871. (not (member (match-string 2 line)
  22872. org-done-keywords)))
  22873. ; TODO, not DONE
  22874. (and org-export-mark-todo-in-toc
  22875. (= level umax-toc)
  22876. (org-search-todo-below
  22877. line lines level))))
  22878. (setq txt (org-html-expand-for-ascii txt))
  22879. (while (string-match org-bracket-link-regexp txt)
  22880. (setq txt
  22881. (replace-match
  22882. (match-string (if (match-end 2) 3 1) txt)
  22883. t t txt)))
  22884. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22885. (string-match
  22886. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22887. txt))
  22888. (setq txt (replace-match "" t t txt)))
  22889. (if (string-match quote-re0 txt)
  22890. (setq txt (replace-match "" t t txt)))
  22891. (if org-export-with-section-numbers
  22892. (setq txt (concat (org-section-number level)
  22893. " " txt)))
  22894. (if (<= level umax-toc)
  22895. (progn
  22896. (push
  22897. (concat
  22898. (make-string
  22899. (* (max 0 (- level org-min-level)) 4) ?\ )
  22900. (format (if todo "%s (*)\n" "%s\n") txt))
  22901. thetoc)
  22902. (setq org-last-level level))
  22903. ))))
  22904. lines)
  22905. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22906. (org-init-section-numbers)
  22907. (while (setq line (pop lines))
  22908. ;; Remove the quoted HTML tags.
  22909. (setq line (org-html-expand-for-ascii line))
  22910. ;; Remove targets
  22911. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22912. (setq line (replace-match "" t t line)))
  22913. ;; Replace internal links
  22914. (while (string-match org-bracket-link-regexp line)
  22915. (setq line (replace-match
  22916. (if (match-end 3) "[\\3]" "[\\1]")
  22917. t nil line)))
  22918. (when custom-times
  22919. (setq line (org-translate-time line)))
  22920. (cond
  22921. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22922. ;; a Headline
  22923. (setq first-heading-pos (or first-heading-pos (point)))
  22924. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22925. txt (match-string 2 line))
  22926. (org-ascii-level-start level txt umax lines))
  22927. ((and org-export-with-tables
  22928. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22929. (if (not table-open)
  22930. ;; New table starts
  22931. (setq table-open t table-buffer nil))
  22932. ;; Accumulate lines
  22933. (setq table-buffer (cons line table-buffer))
  22934. (when (or (not lines)
  22935. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22936. (car lines))))
  22937. (setq table-open nil
  22938. table-buffer (nreverse table-buffer))
  22939. (insert (mapconcat
  22940. (lambda (x)
  22941. (org-fix-indentation x org-ascii-current-indentation))
  22942. (org-format-table-ascii table-buffer)
  22943. "\n") "\n")))
  22944. (t
  22945. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22946. (if (and org-export-with-fixed-width
  22947. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22948. (setq line (replace-match "\\1" nil nil line)))
  22949. (insert line "\n"))))
  22950. (normal-mode)
  22951. ;; insert the table of contents
  22952. (when thetoc
  22953. (goto-char (point-min))
  22954. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22955. (progn
  22956. (goto-char (match-beginning 0))
  22957. (replace-match ""))
  22958. (goto-char first-heading-pos))
  22959. (mapc 'insert thetoc)
  22960. (or (looking-at "[ \t]*\n[ \t]*\n")
  22961. (insert "\n\n")))
  22962. ;; Convert whitespace place holders
  22963. (goto-char (point-min))
  22964. (let (beg end)
  22965. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22966. (setq end (next-single-property-change beg 'org-whitespace))
  22967. (goto-char beg)
  22968. (delete-region beg end)
  22969. (insert (make-string (- end beg) ?\ ))))
  22970. (save-buffer)
  22971. ;; remove display and invisible chars
  22972. (let (beg end)
  22973. (goto-char (point-min))
  22974. (while (setq beg (next-single-property-change (point) 'display))
  22975. (setq end (next-single-property-change beg 'display))
  22976. (delete-region beg end)
  22977. (goto-char beg)
  22978. (insert "=>"))
  22979. (goto-char (point-min))
  22980. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22981. (setq end (next-single-property-change beg 'org-cwidth))
  22982. (delete-region beg end)
  22983. (goto-char beg)))
  22984. (goto-char (point-min))))
  22985. (defun org-export-ascii-clean-string ()
  22986. "Do extra work for ASCII export"
  22987. (goto-char (point-min))
  22988. (while (re-search-forward org-verbatim-re nil t)
  22989. (goto-char (match-end 2))
  22990. (backward-delete-char 1) (insert "'")
  22991. (goto-char (match-beginning 2))
  22992. (delete-char 1) (insert "`")
  22993. (goto-char (match-end 2))))
  22994. (defun org-search-todo-below (line lines level)
  22995. "Search the subtree below LINE for any TODO entries."
  22996. (let ((rest (cdr (memq line lines)))
  22997. (re org-todo-line-regexp)
  22998. line lv todo)
  22999. (catch 'exit
  23000. (while (setq line (pop rest))
  23001. (if (string-match re line)
  23002. (progn
  23003. (setq lv (- (match-end 1) (match-beginning 1))
  23004. todo (and (match-beginning 2)
  23005. (not (member (match-string 2 line)
  23006. org-done-keywords))))
  23007. ; TODO, not DONE
  23008. (if (<= lv level) (throw 'exit nil))
  23009. (if todo (throw 'exit t))))))))
  23010. (defun org-html-expand-for-ascii (line)
  23011. "Handle quoted HTML for ASCII export."
  23012. (if org-export-html-expand
  23013. (while (string-match "@<[^<>\n]*>" line)
  23014. ;; We just remove the tags for now.
  23015. (setq line (replace-match "" nil nil line))))
  23016. line)
  23017. (defun org-insert-centered (s &optional underline)
  23018. "Insert the string S centered and underline it with character UNDERLINE."
  23019. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  23020. (insert (make-string ind ?\ ) s "\n")
  23021. (if underline
  23022. (insert (make-string ind ?\ )
  23023. (make-string (string-width s) underline)
  23024. "\n"))))
  23025. (defun org-ascii-level-start (level title umax &optional lines)
  23026. "Insert a new level in ASCII export."
  23027. (let (char (n (- level umax 1)) (ind 0))
  23028. (if (> level umax)
  23029. (progn
  23030. (insert (make-string (* 2 n) ?\ )
  23031. (char-to-string (nth (% n (length org-export-ascii-bullets))
  23032. org-export-ascii-bullets))
  23033. " " title "\n")
  23034. ;; find the indentation of the next non-empty line
  23035. (catch 'stop
  23036. (while lines
  23037. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  23038. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  23039. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  23040. (pop lines)))
  23041. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  23042. (if (or (not (equal (char-before) ?\n))
  23043. (not (equal (char-before (1- (point))) ?\n)))
  23044. (insert "\n"))
  23045. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  23046. (unless org-export-with-tags
  23047. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  23048. (setq title (replace-match "" t t title))))
  23049. (if org-export-with-section-numbers
  23050. (setq title (concat (org-section-number level) " " title)))
  23051. (insert title "\n" (make-string (string-width title) char) "\n")
  23052. (setq org-ascii-current-indentation '(0 . 0)))))
  23053. (defun org-export-visible (type arg)
  23054. "Create a copy of the visible part of the current buffer, and export it.
  23055. The copy is created in a temporary buffer and removed after use.
  23056. TYPE is the final key (as a string) that also select the export command in
  23057. the `C-c C-e' export dispatcher.
  23058. As a special case, if the you type SPC at the prompt, the temporary
  23059. org-mode file will not be removed but presented to you so that you can
  23060. continue to use it. The prefix arg ARG is passed through to the exporting
  23061. command."
  23062. (interactive
  23063. (list (progn
  23064. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  23065. (read-char-exclusive))
  23066. current-prefix-arg))
  23067. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  23068. (error "Invalid export key"))
  23069. (let* ((binding (cdr (assoc type
  23070. '((?a . org-export-as-ascii)
  23071. (?\C-a . org-export-as-ascii)
  23072. (?b . org-export-as-html-and-open)
  23073. (?\C-b . org-export-as-html-and-open)
  23074. (?h . org-export-as-html)
  23075. (?H . org-export-as-html-to-buffer)
  23076. (?R . org-export-region-as-html)
  23077. (?x . org-export-as-xoxo)))))
  23078. (keepp (equal type ?\ ))
  23079. (file buffer-file-name)
  23080. (buffer (get-buffer-create "*Org Export Visible*"))
  23081. s e)
  23082. ;; Need to hack the drawers here.
  23083. (save-excursion
  23084. (goto-char (point-min))
  23085. (while (re-search-forward org-drawer-regexp nil t)
  23086. (goto-char (match-beginning 1))
  23087. (or (org-invisible-p) (org-flag-drawer nil))))
  23088. (with-current-buffer buffer (erase-buffer))
  23089. (save-excursion
  23090. (setq s (goto-char (point-min)))
  23091. (while (not (= (point) (point-max)))
  23092. (goto-char (org-find-invisible))
  23093. (append-to-buffer buffer s (point))
  23094. (setq s (goto-char (org-find-visible))))
  23095. (org-cycle-hide-drawers 'all)
  23096. (goto-char (point-min))
  23097. (unless keepp
  23098. ;; Copy all comment lines to the end, to make sure #+ settings are
  23099. ;; still available for the second export step. Kind of a hack, but
  23100. ;; does do the trick.
  23101. (if (looking-at "#[^\r\n]*")
  23102. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  23103. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  23104. (append-to-buffer buffer (1+ (match-beginning 0))
  23105. (min (point-max) (1+ (match-end 0))))))
  23106. (set-buffer buffer)
  23107. (let ((buffer-file-name file)
  23108. (org-inhibit-startup t))
  23109. (org-mode)
  23110. (show-all)
  23111. (unless keepp (funcall binding arg))))
  23112. (if (not keepp)
  23113. (kill-buffer buffer)
  23114. (switch-to-buffer-other-window buffer)
  23115. (goto-char (point-min)))))
  23116. (defun org-find-visible ()
  23117. (let ((s (point)))
  23118. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23119. (get-char-property s 'invisible)))
  23120. s))
  23121. (defun org-find-invisible ()
  23122. (let ((s (point)))
  23123. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  23124. (not (get-char-property s 'invisible))))
  23125. s))
  23126. ;;; HTML export
  23127. (defun org-get-current-options ()
  23128. "Return a string with current options as keyword options.
  23129. Does include HTML export options as well as TODO and CATEGORY stuff."
  23130. (format
  23131. "#+TITLE: %s
  23132. #+AUTHOR: %s
  23133. #+EMAIL: %s
  23134. #+LANGUAGE: %s
  23135. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  23136. #+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
  23137. #+CATEGORY: %s
  23138. #+SEQ_TODO: %s
  23139. #+TYP_TODO: %s
  23140. #+PRIORITIES: %c %c %c
  23141. #+DRAWERS: %s
  23142. #+STARTUP: %s %s %s %s %s
  23143. #+TAGS: %s
  23144. #+ARCHIVE: %s
  23145. #+LINK: %s
  23146. "
  23147. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  23148. org-export-headline-levels
  23149. org-export-with-section-numbers
  23150. org-export-with-toc
  23151. org-export-preserve-breaks
  23152. org-export-html-expand
  23153. org-export-with-fixed-width
  23154. org-export-with-tables
  23155. org-export-with-sub-superscripts
  23156. org-export-with-special-strings
  23157. org-export-with-footnotes
  23158. org-export-with-emphasize
  23159. org-export-with-TeX-macros
  23160. org-export-with-LaTeX-fragments
  23161. org-export-skip-text-before-1st-heading
  23162. org-export-with-drawers
  23163. org-export-with-tags
  23164. (file-name-nondirectory buffer-file-name)
  23165. "TODO FEEDBACK VERIFY DONE"
  23166. "Me Jason Marie DONE"
  23167. org-highest-priority org-lowest-priority org-default-priority
  23168. (mapconcat 'identity org-drawers " ")
  23169. (cdr (assoc org-startup-folded
  23170. '((nil . "showall") (t . "overview") (content . "content"))))
  23171. (if org-odd-levels-only "odd" "oddeven")
  23172. (if org-hide-leading-stars "hidestars" "showstars")
  23173. (if org-startup-align-all-tables "align" "noalign")
  23174. (cond ((eq org-log-done t) "logdone")
  23175. ((equal org-log-done 'note) "lognotedone")
  23176. ((not org-log-done) "nologdone"))
  23177. (or (mapconcat (lambda (x)
  23178. (cond
  23179. ((equal '(:startgroup) x) "{")
  23180. ((equal '(:endgroup) x) "}")
  23181. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  23182. (t (car x))))
  23183. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  23184. org-archive-location
  23185. "org file:~/org/%s.org"
  23186. ))
  23187. (defun org-insert-export-options-template ()
  23188. "Insert into the buffer a template with information for exporting."
  23189. (interactive)
  23190. (if (not (bolp)) (newline))
  23191. (let ((s (org-get-current-options)))
  23192. (and (string-match "#\\+CATEGORY" s)
  23193. (setq s (substring s 0 (match-beginning 0))))
  23194. (insert s)))
  23195. (defun org-toggle-fixed-width-section (arg)
  23196. "Toggle the fixed-width export.
  23197. If there is no active region, the QUOTE keyword at the current headline is
  23198. inserted or removed. When present, it causes the text between this headline
  23199. and the next to be exported as fixed-width text, and unmodified.
  23200. If there is an active region, this command adds or removes a colon as the
  23201. first character of this line. If the first character of a line is a colon,
  23202. this line is also exported in fixed-width font."
  23203. (interactive "P")
  23204. (let* ((cc 0)
  23205. (regionp (org-region-active-p))
  23206. (beg (if regionp (region-beginning) (point)))
  23207. (end (if regionp (region-end)))
  23208. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  23209. (case-fold-search nil)
  23210. (re "[ \t]*\\(:\\)")
  23211. off)
  23212. (if regionp
  23213. (save-excursion
  23214. (goto-char beg)
  23215. (setq cc (current-column))
  23216. (beginning-of-line 1)
  23217. (setq off (looking-at re))
  23218. (while (> nlines 0)
  23219. (setq nlines (1- nlines))
  23220. (beginning-of-line 1)
  23221. (cond
  23222. (arg
  23223. (move-to-column cc t)
  23224. (insert ":\n")
  23225. (forward-line -1))
  23226. ((and off (looking-at re))
  23227. (replace-match "" t t nil 1))
  23228. ((not off) (move-to-column cc t) (insert ":")))
  23229. (forward-line 1)))
  23230. (save-excursion
  23231. (org-back-to-heading)
  23232. (if (looking-at (concat outline-regexp
  23233. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  23234. (replace-match "" t t nil 1)
  23235. (if (looking-at outline-regexp)
  23236. (progn
  23237. (goto-char (match-end 0))
  23238. (insert org-quote-string " "))))))))
  23239. (defun org-export-as-html-and-open (arg)
  23240. "Export the outline as HTML and immediately open it with a browser.
  23241. If there is an active region, export only the region.
  23242. The prefix ARG specifies how many levels of the outline should become
  23243. headlines. The default is 3. Lower levels will become bulleted lists."
  23244. (interactive "P")
  23245. (org-export-as-html arg 'hidden)
  23246. (org-open-file buffer-file-name))
  23247. (defun org-export-as-html-batch ()
  23248. "Call `org-export-as-html', may be used in batch processing as
  23249. emacs --batch
  23250. --load=$HOME/lib/emacs/org.el
  23251. --eval \"(setq org-export-headline-levels 2)\"
  23252. --visit=MyFile --funcall org-export-as-html-batch"
  23253. (org-export-as-html org-export-headline-levels 'hidden))
  23254. (defun org-export-as-html-to-buffer (arg)
  23255. "Call `org-exort-as-html` with output to a temporary buffer.
  23256. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  23257. (interactive "P")
  23258. (org-export-as-html arg nil nil "*Org HTML Export*")
  23259. (switch-to-buffer-other-window "*Org HTML Export*"))
  23260. (defun org-replace-region-by-html (beg end)
  23261. "Assume the current region has org-mode syntax, and convert it to HTML.
  23262. This can be used in any buffer. For example, you could write an
  23263. itemized list in org-mode syntax in an HTML buffer and then use this
  23264. command to convert it."
  23265. (interactive "r")
  23266. (let (reg html buf pop-up-frames)
  23267. (save-window-excursion
  23268. (if (org-mode-p)
  23269. (setq html (org-export-region-as-html
  23270. beg end t 'string))
  23271. (setq reg (buffer-substring beg end)
  23272. buf (get-buffer-create "*Org tmp*"))
  23273. (with-current-buffer buf
  23274. (erase-buffer)
  23275. (insert reg)
  23276. (org-mode)
  23277. (setq html (org-export-region-as-html
  23278. (point-min) (point-max) t 'string)))
  23279. (kill-buffer buf)))
  23280. (delete-region beg end)
  23281. (insert html)))
  23282. (defun org-export-region-as-html (beg end &optional body-only buffer)
  23283. "Convert region from BEG to END in org-mode buffer to HTML.
  23284. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  23285. contents, and only produce the region of converted text, useful for
  23286. cut-and-paste operations.
  23287. If BUFFER is a buffer or a string, use/create that buffer as a target
  23288. of the converted HTML. If BUFFER is the symbol `string', return the
  23289. produced HTML as a string and leave not buffer behind. For example,
  23290. a Lisp program could call this function in the following way:
  23291. (setq html (org-export-region-as-html beg end t 'string))
  23292. When called interactively, the output buffer is selected, and shown
  23293. in a window. A non-interactive call will only retunr the buffer."
  23294. (interactive "r\nP")
  23295. (when (interactive-p)
  23296. (setq buffer "*Org HTML Export*"))
  23297. (let ((transient-mark-mode t) (zmacs-regions t)
  23298. rtn)
  23299. (goto-char end)
  23300. (set-mark (point)) ;; to activate the region
  23301. (goto-char beg)
  23302. (setq rtn (org-export-as-html
  23303. nil nil nil
  23304. buffer body-only))
  23305. (if (fboundp 'deactivate-mark) (deactivate-mark))
  23306. (if (and (interactive-p) (bufferp rtn))
  23307. (switch-to-buffer-other-window rtn)
  23308. rtn)))
  23309. (defvar html-table-tag nil) ; dynamically scoped into this.
  23310. (defun org-export-as-html (arg &optional hidden ext-plist
  23311. to-buffer body-only)
  23312. "Export the outline as a pretty HTML file.
  23313. If there is an active region, export only the region. The prefix
  23314. ARG specifies how many levels of the outline should become
  23315. headlines. The default is 3. Lower levels will become bulleted
  23316. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  23317. EXT-PLIST is a property list with external parameters overriding
  23318. org-mode's default settings, but still inferior to file-local
  23319. settings. When TO-BUFFER is non-nil, create a buffer with that
  23320. name and export to that buffer. If TO-BUFFER is the symbol `string',
  23321. don't leave any buffer behind but just return the resulting HTML as
  23322. a string. When BODY-ONLY is set, don't produce the file header and footer,
  23323. simply return the content of <body>...</body>, without even
  23324. the body tags themselves."
  23325. (interactive "P")
  23326. ;; Make sure we have a file name when we need it.
  23327. (when (and (not (or to-buffer body-only))
  23328. (not buffer-file-name))
  23329. (if (buffer-base-buffer)
  23330. (org-set-local 'buffer-file-name
  23331. (with-current-buffer (buffer-base-buffer)
  23332. buffer-file-name))
  23333. (error "Need a file name to be able to export.")))
  23334. (message "Exporting...")
  23335. (setq-default org-todo-line-regexp org-todo-line-regexp)
  23336. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  23337. (setq-default org-done-keywords org-done-keywords)
  23338. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  23339. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  23340. ext-plist
  23341. (org-infile-export-plist)))
  23342. (style (plist-get opt-plist :style))
  23343. (html-extension (plist-get opt-plist :html-extension))
  23344. (link-validate (plist-get opt-plist :link-validation-function))
  23345. valid thetoc have-headings first-heading-pos
  23346. (odd org-odd-levels-only)
  23347. (region-p (org-region-active-p))
  23348. (subtree-p
  23349. (when region-p
  23350. (save-excursion
  23351. (goto-char (region-beginning))
  23352. (and (org-at-heading-p)
  23353. (>= (org-end-of-subtree t t) (region-end))))))
  23354. ;; The following two are dynamically scoped into other
  23355. ;; routines below.
  23356. (org-current-export-dir (org-export-directory :html opt-plist))
  23357. (org-current-export-file buffer-file-name)
  23358. (level 0) (line "") (origline "") txt todo
  23359. (umax nil)
  23360. (umax-toc nil)
  23361. (filename (if to-buffer nil
  23362. (expand-file-name
  23363. (concat
  23364. (file-name-sans-extension
  23365. (or (and subtree-p
  23366. (org-entry-get (region-beginning)
  23367. "EXPORT_FILE_NAME" t))
  23368. (file-name-nondirectory buffer-file-name)))
  23369. "." html-extension)
  23370. (file-name-as-directory
  23371. (org-export-directory :html opt-plist)))))
  23372. (current-dir (if buffer-file-name
  23373. (file-name-directory buffer-file-name)
  23374. default-directory))
  23375. (buffer (if to-buffer
  23376. (cond
  23377. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  23378. (t (get-buffer-create to-buffer)))
  23379. (find-file-noselect filename)))
  23380. (org-levels-open (make-vector org-level-max nil))
  23381. (date (plist-get opt-plist :date))
  23382. (author (plist-get opt-plist :author))
  23383. (title (or (and subtree-p (org-export-get-title-from-subtree))
  23384. (plist-get opt-plist :title)
  23385. (and (not
  23386. (plist-get opt-plist :skip-before-1st-heading))
  23387. (org-export-grab-title-from-buffer))
  23388. (and buffer-file-name
  23389. (file-name-sans-extension
  23390. (file-name-nondirectory buffer-file-name)))
  23391. "UNTITLED"))
  23392. (html-table-tag (plist-get opt-plist :html-table-tag))
  23393. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  23394. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  23395. (inquote nil)
  23396. (infixed nil)
  23397. (in-local-list nil)
  23398. (local-list-num nil)
  23399. (local-list-indent nil)
  23400. (llt org-plain-list-ordered-item-terminator)
  23401. (email (plist-get opt-plist :email))
  23402. (language (plist-get opt-plist :language))
  23403. (lang-words nil)
  23404. (target-alist nil) tg
  23405. (head-count 0) cnt
  23406. (start 0)
  23407. (coding-system (and (boundp 'buffer-file-coding-system)
  23408. buffer-file-coding-system))
  23409. (coding-system-for-write (or org-export-html-coding-system
  23410. coding-system))
  23411. (save-buffer-coding-system (or org-export-html-coding-system
  23412. coding-system))
  23413. (charset (and coding-system-for-write
  23414. (fboundp 'coding-system-get)
  23415. (coding-system-get coding-system-for-write
  23416. 'mime-charset)))
  23417. (region
  23418. (buffer-substring
  23419. (if region-p (region-beginning) (point-min))
  23420. (if region-p (region-end) (point-max))))
  23421. (lines
  23422. (org-split-string
  23423. (org-cleaned-string-for-export
  23424. region
  23425. :emph-multiline t
  23426. :for-html t
  23427. :skip-before-1st-heading
  23428. (plist-get opt-plist :skip-before-1st-heading)
  23429. :drawers (plist-get opt-plist :drawers)
  23430. :archived-trees
  23431. (plist-get opt-plist :archived-trees)
  23432. :add-text
  23433. (plist-get opt-plist :text)
  23434. :LaTeX-fragments
  23435. (plist-get opt-plist :LaTeX-fragments))
  23436. "[\r\n]"))
  23437. table-open type
  23438. table-buffer table-orig-buffer
  23439. ind start-is-num starter didclose
  23440. rpl path desc descp desc1 desc2 link
  23441. )
  23442. (let ((inhibit-read-only t))
  23443. (org-unmodified
  23444. (remove-text-properties (point-min) (point-max)
  23445. '(:org-license-to-kill t))))
  23446. (message "Exporting...")
  23447. (setq org-min-level (org-get-min-level lines))
  23448. (setq org-last-level org-min-level)
  23449. (org-init-section-numbers)
  23450. (cond
  23451. ((and date (string-match "%" date))
  23452. (setq date (format-time-string date (current-time))))
  23453. (date)
  23454. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  23455. ;; Get the language-dependent settings
  23456. (setq lang-words (or (assoc language org-export-language-setup)
  23457. (assoc "en" org-export-language-setup)))
  23458. ;; Switch to the output buffer
  23459. (set-buffer buffer)
  23460. (let ((inhibit-read-only t)) (erase-buffer))
  23461. (fundamental-mode)
  23462. (and (fboundp 'set-buffer-file-coding-system)
  23463. (set-buffer-file-coding-system coding-system-for-write))
  23464. (let ((case-fold-search nil)
  23465. (org-odd-levels-only odd))
  23466. ;; create local variables for all options, to make sure all called
  23467. ;; functions get the correct information
  23468. (mapc (lambda (x)
  23469. (set (make-local-variable (cdr x))
  23470. (plist-get opt-plist (car x))))
  23471. org-export-plist-vars)
  23472. (setq umax (if arg (prefix-numeric-value arg)
  23473. org-export-headline-levels))
  23474. (setq umax-toc (if (integerp org-export-with-toc)
  23475. (min org-export-with-toc umax)
  23476. umax))
  23477. (unless body-only
  23478. ;; File header
  23479. (insert (format
  23480. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23481. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23482. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23483. lang=\"%s\" xml:lang=\"%s\">
  23484. <head>
  23485. <title>%s</title>
  23486. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23487. <meta name=\"generator\" content=\"Org-mode\"/>
  23488. <meta name=\"generated\" content=\"%s\"/>
  23489. <meta name=\"author\" content=\"%s\"/>
  23490. %s
  23491. </head><body>
  23492. "
  23493. language language (org-html-expand title)
  23494. (or charset "iso-8859-1") date author style))
  23495. (insert (or (plist-get opt-plist :preamble) ""))
  23496. (when (plist-get opt-plist :auto-preamble)
  23497. (if title (insert (format org-export-html-title-format
  23498. (org-html-expand title))))))
  23499. (if (and org-export-with-toc (not body-only))
  23500. (progn
  23501. (push (format "<h%d>%s</h%d>\n"
  23502. org-export-html-toplevel-hlevel
  23503. (nth 3 lang-words)
  23504. org-export-html-toplevel-hlevel)
  23505. thetoc)
  23506. (push "<ul>\n<li>" thetoc)
  23507. (setq lines
  23508. (mapcar '(lambda (line)
  23509. (if (string-match org-todo-line-regexp line)
  23510. ;; This is a headline
  23511. (progn
  23512. (setq have-headings t)
  23513. (setq level (- (match-end 1) (match-beginning 1))
  23514. level (org-tr-level level)
  23515. txt (save-match-data
  23516. (org-html-expand
  23517. (org-export-cleanup-toc-line
  23518. (match-string 3 line))))
  23519. todo
  23520. (or (and org-export-mark-todo-in-toc
  23521. (match-beginning 2)
  23522. (not (member (match-string 2 line)
  23523. org-done-keywords)))
  23524. ; TODO, not DONE
  23525. (and org-export-mark-todo-in-toc
  23526. (= level umax-toc)
  23527. (org-search-todo-below
  23528. line lines level))))
  23529. (if (string-match
  23530. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23531. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23532. (if (string-match quote-re0 txt)
  23533. (setq txt (replace-match "" t t txt)))
  23534. (if org-export-with-section-numbers
  23535. (setq txt (concat (org-section-number level)
  23536. " " txt)))
  23537. (if (<= level (max umax umax-toc))
  23538. (setq head-count (+ head-count 1)))
  23539. (if (<= level umax-toc)
  23540. (progn
  23541. (if (> level org-last-level)
  23542. (progn
  23543. (setq cnt (- level org-last-level))
  23544. (while (>= (setq cnt (1- cnt)) 0)
  23545. (push "\n<ul>\n<li>" thetoc))
  23546. (push "\n" thetoc)))
  23547. (if (< level org-last-level)
  23548. (progn
  23549. (setq cnt (- org-last-level level))
  23550. (while (>= (setq cnt (1- cnt)) 0)
  23551. (push "</li>\n</ul>" thetoc))
  23552. (push "\n" thetoc)))
  23553. ;; Check for targets
  23554. (while (string-match org-target-regexp line)
  23555. (setq tg (match-string 1 line)
  23556. line (replace-match
  23557. (concat "@<span class=\"target\">" tg "@</span> ")
  23558. t t line))
  23559. (push (cons (org-solidify-link-text tg)
  23560. (format "sec-%d" head-count))
  23561. target-alist))
  23562. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23563. (setq txt (replace-match "" t t txt)))
  23564. (push
  23565. (format
  23566. (if todo
  23567. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23568. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23569. head-count txt) thetoc)
  23570. (setq org-last-level level))
  23571. )))
  23572. line)
  23573. lines))
  23574. (while (> org-last-level (1- org-min-level))
  23575. (setq org-last-level (1- org-last-level))
  23576. (push "</li>\n</ul>\n" thetoc))
  23577. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23578. (setq head-count 0)
  23579. (org-init-section-numbers)
  23580. (while (setq line (pop lines) origline line)
  23581. (catch 'nextline
  23582. ;; end of quote section?
  23583. (when (and inquote (string-match "^\\*+ " line))
  23584. (insert "</pre>\n")
  23585. (setq inquote nil))
  23586. ;; inside a quote section?
  23587. (when inquote
  23588. (insert (org-html-protect line) "\n")
  23589. (throw 'nextline nil))
  23590. ;; verbatim lines
  23591. (when (and org-export-with-fixed-width
  23592. (string-match "^[ \t]*:\\(.*\\)" line))
  23593. (when (not infixed)
  23594. (setq infixed t)
  23595. (insert "<pre>\n"))
  23596. (insert (org-html-protect (match-string 1 line)) "\n")
  23597. (when (and lines
  23598. (not (string-match "^[ \t]*\\(:.*\\)"
  23599. (car lines))))
  23600. (setq infixed nil)
  23601. (insert "</pre>\n"))
  23602. (throw 'nextline nil))
  23603. ;; Protected HTML
  23604. (when (get-text-property 0 'org-protected line)
  23605. (let (par)
  23606. (when (re-search-backward
  23607. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23608. (setq par (match-string 1))
  23609. (replace-match "\\2\n"))
  23610. (insert line "\n")
  23611. (while (and lines
  23612. (or (= (length (car lines)) 0)
  23613. (get-text-property 0 'org-protected (car lines))))
  23614. (insert (pop lines) "\n"))
  23615. (and par (insert "<p>\n")))
  23616. (throw 'nextline nil))
  23617. ;; Horizontal line
  23618. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23619. (insert "\n<hr/>\n")
  23620. (throw 'nextline nil))
  23621. ;; make targets to anchors
  23622. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23623. (cond
  23624. ((match-end 2)
  23625. (setq line (replace-match
  23626. (concat "@<a name=\""
  23627. (org-solidify-link-text (match-string 1 line))
  23628. "\">\\nbsp@</a>")
  23629. t t line)))
  23630. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23631. (setq line (replace-match
  23632. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23633. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23634. t t line)))
  23635. (t
  23636. (setq line (replace-match
  23637. (concat "@<a name=\""
  23638. (org-solidify-link-text (match-string 1 line))
  23639. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23640. t t line)))))
  23641. (setq line (org-html-handle-time-stamps line))
  23642. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23643. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23644. ;; Also handle sub_superscripts and checkboxes
  23645. (or (string-match org-table-hline-regexp line)
  23646. (setq line (org-html-expand line)))
  23647. ;; Format the links
  23648. (setq start 0)
  23649. (while (string-match org-bracket-link-analytic-regexp line start)
  23650. (setq start (match-beginning 0))
  23651. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23652. (setq path (match-string 3 line))
  23653. (setq desc1 (if (match-end 5) (match-string 5 line))
  23654. desc2 (if (match-end 2) (concat type ":" path) path)
  23655. descp (and desc1 (not (equal desc1 desc2)))
  23656. desc (or desc1 desc2))
  23657. ;; Make an image out of the description if that is so wanted
  23658. (when (and descp (org-file-image-p desc))
  23659. (save-match-data
  23660. (if (string-match "^file:" desc)
  23661. (setq desc (substring desc (match-end 0)))))
  23662. (setq desc (concat "<img src=\"" desc "\"/>")))
  23663. ;; FIXME: do we need to unescape here somewhere?
  23664. (cond
  23665. ((equal type "internal")
  23666. (setq rpl
  23667. (concat
  23668. "<a href=\"#"
  23669. (org-solidify-link-text
  23670. (save-match-data (org-link-unescape path)) target-alist)
  23671. "\">" desc "</a>")))
  23672. ((member type '("http" "https"))
  23673. ;; standard URL, just check if we need to inline an image
  23674. (if (and (or (eq t org-export-html-inline-images)
  23675. (and org-export-html-inline-images (not descp)))
  23676. (org-file-image-p path))
  23677. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23678. (setq link (concat type ":" path))
  23679. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23680. ((member type '("ftp" "mailto" "news"))
  23681. ;; standard URL
  23682. (setq link (concat type ":" path))
  23683. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23684. ((string= type "file")
  23685. ;; FILE link
  23686. (let* ((filename path)
  23687. (abs-p (file-name-absolute-p filename))
  23688. thefile file-is-image-p search)
  23689. (save-match-data
  23690. (if (string-match "::\\(.*\\)" filename)
  23691. (setq search (match-string 1 filename)
  23692. filename (replace-match "" t nil filename)))
  23693. (setq valid
  23694. (if (functionp link-validate)
  23695. (funcall link-validate filename current-dir)
  23696. t))
  23697. (setq file-is-image-p (org-file-image-p filename))
  23698. (setq thefile (if abs-p (expand-file-name filename) filename))
  23699. (when (and org-export-html-link-org-files-as-html
  23700. (string-match "\\.org$" thefile))
  23701. (setq thefile (concat (substring thefile 0
  23702. (match-beginning 0))
  23703. "." html-extension))
  23704. (if (and search
  23705. ;; make sure this is can be used as target search
  23706. (not (string-match "^[0-9]*$" search))
  23707. (not (string-match "^\\*" search))
  23708. (not (string-match "^/.*/$" search)))
  23709. (setq thefile (concat thefile "#"
  23710. (org-solidify-link-text
  23711. (org-link-unescape search)))))
  23712. (when (string-match "^file:" desc)
  23713. (setq desc (replace-match "" t t desc))
  23714. (if (string-match "\\.org$" desc)
  23715. (setq desc (replace-match "" t t desc))))))
  23716. (setq rpl (if (and file-is-image-p
  23717. (or (eq t org-export-html-inline-images)
  23718. (and org-export-html-inline-images
  23719. (not descp))))
  23720. (concat "<img src=\"" thefile "\"/>")
  23721. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23722. (if (not valid) (setq rpl desc))))
  23723. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23724. (setq rpl (concat "<i>&lt;" type ":"
  23725. (save-match-data (org-link-unescape path))
  23726. "&gt;</i>"))))
  23727. (setq line (replace-match rpl t t line)
  23728. start (+ start (length rpl))))
  23729. ;; TODO items
  23730. (if (and (string-match org-todo-line-regexp line)
  23731. (match-beginning 2))
  23732. (setq line
  23733. (concat (substring line 0 (match-beginning 2))
  23734. "<span class=\""
  23735. (if (member (match-string 2 line)
  23736. org-done-keywords)
  23737. "done" "todo")
  23738. "\">" (match-string 2 line)
  23739. "</span>" (substring line (match-end 2)))))
  23740. ;; Does this contain a reference to a footnote?
  23741. (when org-export-with-footnotes
  23742. (setq start 0)
  23743. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23744. (if (get-text-property (match-beginning 2) 'org-protected line)
  23745. (setq start (match-end 2))
  23746. (let ((n (match-string 2 line)))
  23747. (setq line
  23748. (replace-match
  23749. (format
  23750. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23751. (match-string 1 line) n n n)
  23752. t t line))))))
  23753. (cond
  23754. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23755. ;; This is a headline
  23756. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23757. txt (match-string 2 line))
  23758. (if (string-match quote-re0 txt)
  23759. (setq txt (replace-match "" t t txt)))
  23760. (if (<= level (max umax umax-toc))
  23761. (setq head-count (+ head-count 1)))
  23762. (when in-local-list
  23763. ;; Close any local lists before inserting a new header line
  23764. (while local-list-num
  23765. (org-close-li)
  23766. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23767. (pop local-list-num))
  23768. (setq local-list-indent nil
  23769. in-local-list nil))
  23770. (setq first-heading-pos (or first-heading-pos (point)))
  23771. (org-html-level-start level txt umax
  23772. (and org-export-with-toc (<= level umax))
  23773. head-count)
  23774. ;; QUOTES
  23775. (when (string-match quote-re line)
  23776. (insert "<pre>")
  23777. (setq inquote t)))
  23778. ((and org-export-with-tables
  23779. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23780. (if (not table-open)
  23781. ;; New table starts
  23782. (setq table-open t table-buffer nil table-orig-buffer nil))
  23783. ;; Accumulate lines
  23784. (setq table-buffer (cons line table-buffer)
  23785. table-orig-buffer (cons origline table-orig-buffer))
  23786. (when (or (not lines)
  23787. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23788. (car lines))))
  23789. (setq table-open nil
  23790. table-buffer (nreverse table-buffer)
  23791. table-orig-buffer (nreverse table-orig-buffer))
  23792. (org-close-par-maybe)
  23793. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23794. (t
  23795. ;; Normal lines
  23796. (when (string-match
  23797. (cond
  23798. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23799. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23800. ((= llt ?\)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23801. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23802. line)
  23803. (setq ind (org-get-string-indentation line)
  23804. start-is-num (match-beginning 4)
  23805. starter (if (match-beginning 2)
  23806. (substring (match-string 2 line) 0 -1))
  23807. line (substring line (match-beginning 5)))
  23808. (unless (string-match "[^ \t]" line)
  23809. ;; empty line. Pretend indentation is large.
  23810. (setq ind (if org-empty-line-terminates-plain-lists
  23811. 0
  23812. (1+ (or (car local-list-indent) 1)))))
  23813. (setq didclose nil)
  23814. (while (and in-local-list
  23815. (or (and (= ind (car local-list-indent))
  23816. (not starter))
  23817. (< ind (car local-list-indent))))
  23818. (setq didclose t)
  23819. (org-close-li)
  23820. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23821. (pop local-list-num) (pop local-list-indent)
  23822. (setq in-local-list local-list-indent))
  23823. (cond
  23824. ((and starter
  23825. (or (not in-local-list)
  23826. (> ind (car local-list-indent))))
  23827. ;; Start new (level of) list
  23828. (org-close-par-maybe)
  23829. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23830. (push start-is-num local-list-num)
  23831. (push ind local-list-indent)
  23832. (setq in-local-list t))
  23833. (starter
  23834. ;; continue current list
  23835. (org-close-li)
  23836. (insert "<li>\n"))
  23837. (didclose
  23838. ;; we did close a list, normal text follows: need <p>
  23839. (org-open-par)))
  23840. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23841. (setq line
  23842. (replace-match
  23843. (if (equal (match-string 1 line) "X")
  23844. "<b>[X]</b>"
  23845. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23846. t t line))))
  23847. ;; Empty lines start a new paragraph. If hand-formatted lists
  23848. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23849. ;; also start a new paragraph.
  23850. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23851. ;; Is this the start of a footnote?
  23852. (when org-export-with-footnotes
  23853. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23854. (org-close-par-maybe)
  23855. (let ((n (match-string 1 line)))
  23856. (setq line (replace-match
  23857. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23858. ;; Check if the line break needs to be conserved
  23859. (cond
  23860. ((string-match "\\\\\\\\[ \t]*$" line)
  23861. (setq line (replace-match "<br/>" t t line)))
  23862. (org-export-preserve-breaks
  23863. (setq line (concat line "<br/>"))))
  23864. (insert line "\n")))))
  23865. ;; Properly close all local lists and other lists
  23866. (when inquote (insert "</pre>\n"))
  23867. (when in-local-list
  23868. ;; Close any local lists before inserting a new header line
  23869. (while local-list-num
  23870. (org-close-li)
  23871. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23872. (pop local-list-num))
  23873. (setq local-list-indent nil
  23874. in-local-list nil))
  23875. (org-html-level-start 1 nil umax
  23876. (and org-export-with-toc (<= level umax))
  23877. head-count)
  23878. (unless body-only
  23879. (when (plist-get opt-plist :auto-postamble)
  23880. (insert "<div id=\"postamble\">")
  23881. (when (and org-export-author-info author)
  23882. (insert "<p class=\"author\"> "
  23883. (nth 1 lang-words) ": " author "\n")
  23884. (when email
  23885. (if (listp (split-string email ",+ *"))
  23886. (mapc (lambda(e)
  23887. (insert "<a href=\"mailto:" e "\">&lt;"
  23888. e "&gt;</a>\n"))
  23889. (split-string email ",+ *"))
  23890. (insert "<a href=\"mailto:" email "\">&lt;"
  23891. email "&gt;</a>\n")))
  23892. (insert "</p>\n"))
  23893. (when (and date org-export-time-stamp-file)
  23894. (insert "<p class=\"date\"> "
  23895. (nth 2 lang-words) ": "
  23896. date "</p>\n"))
  23897. (insert "</div>"))
  23898. (if org-export-html-with-timestamp
  23899. (insert org-export-html-html-helper-timestamp))
  23900. (insert (or (plist-get opt-plist :postamble) ""))
  23901. (insert "</body>\n</html>\n"))
  23902. (normal-mode)
  23903. (if (eq major-mode default-major-mode) (html-mode))
  23904. ;; insert the table of contents
  23905. (goto-char (point-min))
  23906. (when thetoc
  23907. (if (or (re-search-forward
  23908. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23909. (re-search-forward
  23910. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23911. (progn
  23912. (goto-char (match-beginning 0))
  23913. (replace-match ""))
  23914. (goto-char first-heading-pos)
  23915. (when (looking-at "\\s-*</p>")
  23916. (goto-char (match-end 0))
  23917. (insert "\n")))
  23918. (insert "<div id=\"table-of-contents\">\n")
  23919. (mapc 'insert thetoc)
  23920. (insert "</div>\n"))
  23921. ;; remove empty paragraphs and lists
  23922. (goto-char (point-min))
  23923. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23924. (replace-match ""))
  23925. (goto-char (point-min))
  23926. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23927. (replace-match ""))
  23928. (goto-char (point-min))
  23929. (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t)
  23930. (replace-match ""))
  23931. ;; Convert whitespace place holders
  23932. (goto-char (point-min))
  23933. (let (beg end n)
  23934. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23935. (setq n (get-text-property beg 'org-whitespace)
  23936. end (next-single-property-change beg 'org-whitespace))
  23937. (goto-char beg)
  23938. (delete-region beg end)
  23939. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23940. (make-string n ?x)))))
  23941. (or to-buffer (save-buffer))
  23942. (goto-char (point-min))
  23943. (message "Exporting... done")
  23944. (if (eq to-buffer 'string)
  23945. (prog1 (buffer-substring (point-min) (point-max))
  23946. (kill-buffer (current-buffer)))
  23947. (current-buffer)))))
  23948. (defvar org-table-colgroup-info nil)
  23949. (defun org-format-table-ascii (lines)
  23950. "Format a table for ascii export."
  23951. (if (stringp lines)
  23952. (setq lines (org-split-string lines "\n")))
  23953. (if (not (string-match "^[ \t]*|" (car lines)))
  23954. ;; Table made by table.el - test for spanning
  23955. lines
  23956. ;; A normal org table
  23957. ;; Get rid of hlines at beginning and end
  23958. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23959. (setq lines (nreverse lines))
  23960. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23961. (setq lines (nreverse lines))
  23962. (when org-export-table-remove-special-lines
  23963. ;; Check if the table has a marking column. If yes remove the
  23964. ;; column and the special lines
  23965. (setq lines (org-table-clean-before-export lines)))
  23966. ;; Get rid of the vertical lines except for grouping
  23967. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23968. rtn line vl1 start)
  23969. (while (setq line (pop lines))
  23970. (if (string-match org-table-hline-regexp line)
  23971. (and (string-match "|\\(.*\\)|" line)
  23972. (setq line (replace-match " \\1" t nil line)))
  23973. (setq start 0 vl1 vl)
  23974. (while (string-match "|" line start)
  23975. (setq start (match-end 0))
  23976. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23977. (push line rtn))
  23978. (nreverse rtn))))
  23979. (defun org-colgroup-info-to-vline-list (info)
  23980. (let (vl new last)
  23981. (while info
  23982. (setq last new new (pop info))
  23983. (if (or (memq last '(:end :startend))
  23984. (memq new '(:start :startend)))
  23985. (push t vl)
  23986. (push nil vl)))
  23987. (setq vl (nreverse vl))
  23988. (and vl (setcar vl nil))
  23989. vl))
  23990. (defun org-format-table-html (lines olines)
  23991. "Find out which HTML converter to use and return the HTML code."
  23992. (if (stringp lines)
  23993. (setq lines (org-split-string lines "\n")))
  23994. (if (string-match "^[ \t]*|" (car lines))
  23995. ;; A normal org table
  23996. (org-format-org-table-html lines)
  23997. ;; Table made by table.el - test for spanning
  23998. (let* ((hlines (delq nil (mapcar
  23999. (lambda (x)
  24000. (if (string-match "^[ \t]*\\+-" x) x
  24001. nil))
  24002. lines)))
  24003. (first (car hlines))
  24004. (ll (and (string-match "\\S-+" first)
  24005. (match-string 0 first)))
  24006. (re (concat "^[ \t]*" (regexp-quote ll)))
  24007. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  24008. hlines))))
  24009. (if (and (not spanning)
  24010. (not org-export-prefer-native-exporter-for-tables))
  24011. ;; We can use my own converter with HTML conversions
  24012. (org-format-table-table-html lines)
  24013. ;; Need to use the code generator in table.el, with the original text.
  24014. (org-format-table-table-html-using-table-generate-source olines)))))
  24015. (defun org-format-org-table-html (lines &optional splice)
  24016. "Format a table into HTML."
  24017. ;; Get rid of hlines at beginning and end
  24018. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24019. (setq lines (nreverse lines))
  24020. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  24021. (setq lines (nreverse lines))
  24022. (when org-export-table-remove-special-lines
  24023. ;; Check if the table has a marking column. If yes remove the
  24024. ;; column and the special lines
  24025. (setq lines (org-table-clean-before-export lines)))
  24026. (let ((head (and org-export-highlight-first-table-line
  24027. (delq nil (mapcar
  24028. (lambda (x) (string-match "^[ \t]*|-" x))
  24029. (cdr lines)))))
  24030. (nlines 0) fnum i
  24031. tbopen line fields html gr colgropen)
  24032. (if splice (setq head nil))
  24033. (unless splice (push (if head "<thead>" "<tbody>") html))
  24034. (setq tbopen t)
  24035. (while (setq line (pop lines))
  24036. (catch 'next-line
  24037. (if (string-match "^[ \t]*|-" line)
  24038. (progn
  24039. (unless splice
  24040. (push (if head "</thead>" "</tbody>") html)
  24041. (if lines (push "<tbody>" html) (setq tbopen nil)))
  24042. (setq head nil) ;; head ends here, first time around
  24043. ;; ignore this line
  24044. (throw 'next-line t)))
  24045. ;; Break the line into fields
  24046. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24047. (unless fnum (setq fnum (make-vector (length fields) 0)))
  24048. (setq nlines (1+ nlines) i -1)
  24049. (push (concat "<tr>"
  24050. (mapconcat
  24051. (lambda (x)
  24052. (setq i (1+ i))
  24053. (if (and (< i nlines)
  24054. (string-match org-table-number-regexp x))
  24055. (incf (aref fnum i)))
  24056. (if head
  24057. (concat (car org-export-table-header-tags) x
  24058. (cdr org-export-table-header-tags))
  24059. (concat (car org-export-table-data-tags) x
  24060. (cdr org-export-table-data-tags))))
  24061. fields "")
  24062. "</tr>")
  24063. html)))
  24064. (unless splice (if tbopen (push "</tbody>" html)))
  24065. (unless splice (push "</table>\n" html))
  24066. (setq html (nreverse html))
  24067. (unless splice
  24068. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  24069. (push (mapconcat
  24070. (lambda (x)
  24071. (setq gr (pop org-table-colgroup-info))
  24072. (format "%s<col align=\"%s\"></col>%s"
  24073. (if (memq gr '(:start :startend))
  24074. (prog1
  24075. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  24076. (setq colgropen t))
  24077. "")
  24078. (if (> (/ (float x) nlines) org-table-number-fraction)
  24079. "right" "left")
  24080. (if (memq gr '(:end :startend))
  24081. (progn (setq colgropen nil) "</colgroup>")
  24082. "")))
  24083. fnum "")
  24084. html)
  24085. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  24086. (push html-table-tag html))
  24087. (concat (mapconcat 'identity html "\n") "\n")))
  24088. (defun org-table-clean-before-export (lines)
  24089. "Check if the table has a marking column.
  24090. If yes remove the column and the special lines."
  24091. (setq org-table-colgroup-info nil)
  24092. (if (memq nil
  24093. (mapcar
  24094. (lambda (x) (or (string-match "^[ \t]*|-" x)
  24095. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  24096. lines))
  24097. (progn
  24098. (setq org-table-clean-did-remove-column nil)
  24099. (delq nil
  24100. (mapcar
  24101. (lambda (x)
  24102. (cond
  24103. ((string-match "^[ \t]*| */ *|" x)
  24104. (setq org-table-colgroup-info
  24105. (mapcar (lambda (x)
  24106. (cond ((member x '("<" "&lt;")) :start)
  24107. ((member x '(">" "&gt;")) :end)
  24108. ((member x '("<>" "&lt;&gt;")) :startend)
  24109. (t nil)))
  24110. (org-split-string x "[ \t]*|[ \t]*")))
  24111. nil)
  24112. (t x)))
  24113. lines)))
  24114. (setq org-table-clean-did-remove-column t)
  24115. (delq nil
  24116. (mapcar
  24117. (lambda (x)
  24118. (cond
  24119. ((string-match "^[ \t]*| */ *|" x)
  24120. (setq org-table-colgroup-info
  24121. (mapcar (lambda (x)
  24122. (cond ((member x '("<" "&lt;")) :start)
  24123. ((member x '(">" "&gt;")) :end)
  24124. ((member x '("<>" "&lt;&gt;")) :startend)
  24125. (t nil)))
  24126. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  24127. nil)
  24128. ((string-match "^[ \t]*| *[!_^/] *|" x)
  24129. nil) ; ignore this line
  24130. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  24131. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  24132. ;; remove the first column
  24133. (replace-match "\\1|" t nil x))))
  24134. lines))))
  24135. (defun org-format-table-table-html (lines)
  24136. "Format a table generated by table.el into HTML.
  24137. This conversion does *not* use `table-generate-source' from table.el.
  24138. This has the advantage that Org-mode's HTML conversions can be used.
  24139. But it has the disadvantage, that no cell- or row-spanning is allowed."
  24140. (let (line field-buffer
  24141. (head org-export-highlight-first-table-line)
  24142. fields html empty)
  24143. (setq html (concat html-table-tag "\n"))
  24144. (while (setq line (pop lines))
  24145. (setq empty "&nbsp;")
  24146. (catch 'next-line
  24147. (if (string-match "^[ \t]*\\+-" line)
  24148. (progn
  24149. (if field-buffer
  24150. (progn
  24151. (setq
  24152. html
  24153. (concat
  24154. html
  24155. "<tr>"
  24156. (mapconcat
  24157. (lambda (x)
  24158. (if (equal x "") (setq x empty))
  24159. (if head
  24160. (concat (car org-export-table-header-tags) x
  24161. (cdr org-export-table-header-tags))
  24162. (concat (car org-export-table-data-tags) x
  24163. (cdr org-export-table-data-tags))))
  24164. field-buffer "\n")
  24165. "</tr>\n"))
  24166. (setq head nil)
  24167. (setq field-buffer nil)))
  24168. ;; Ignore this line
  24169. (throw 'next-line t)))
  24170. ;; Break the line into fields and store the fields
  24171. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  24172. (if field-buffer
  24173. (setq field-buffer (mapcar
  24174. (lambda (x)
  24175. (concat x "<br/>" (pop fields)))
  24176. field-buffer))
  24177. (setq field-buffer fields))))
  24178. (setq html (concat html "</table>\n"))
  24179. html))
  24180. (defun org-format-table-table-html-using-table-generate-source (lines)
  24181. "Format a table into html, using `table-generate-source' from table.el.
  24182. This has the advantage that cell- or row-spanning is allowed.
  24183. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  24184. (require 'table)
  24185. (with-current-buffer (get-buffer-create " org-tmp1 ")
  24186. (erase-buffer)
  24187. (insert (mapconcat 'identity lines "\n"))
  24188. (goto-char (point-min))
  24189. (if (not (re-search-forward "|[^+]" nil t))
  24190. (error "Error processing table"))
  24191. (table-recognize-table)
  24192. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  24193. (table-generate-source 'html " org-tmp2 ")
  24194. (set-buffer " org-tmp2 ")
  24195. (buffer-substring (point-min) (point-max))))
  24196. (defun org-html-handle-time-stamps (s)
  24197. "Format time stamps in string S, or remove them."
  24198. (catch 'exit
  24199. (let (r b)
  24200. (while (string-match org-maybe-keyword-time-regexp s)
  24201. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  24202. ;; never export CLOCK
  24203. (throw 'exit ""))
  24204. (or b (setq b (substring s 0 (match-beginning 0))))
  24205. (if (not org-export-with-timestamps)
  24206. (setq r (concat r (substring s 0 (match-beginning 0)))
  24207. s (substring s (match-end 0)))
  24208. (setq r (concat
  24209. r (substring s 0 (match-beginning 0))
  24210. (if (match-end 1)
  24211. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  24212. (match-string 1 s)))
  24213. (format " @<span class=\"timestamp\">%s@</span>"
  24214. (substring
  24215. (org-translate-time (match-string 3 s)) 1 -1)))
  24216. s (substring s (match-end 0)))))
  24217. ;; Line break if line started and ended with time stamp stuff
  24218. (if (not r)
  24219. s
  24220. (setq r (concat r s))
  24221. (unless (string-match "\\S-" (concat b s))
  24222. (setq r (concat r "@<br/>")))
  24223. r))))
  24224. (defun org-html-protect (s)
  24225. ;; convert & to &amp;, < to &lt; and > to &gt;
  24226. (let ((start 0))
  24227. (while (string-match "&" s start)
  24228. (setq s (replace-match "&amp;" t t s)
  24229. start (1+ (match-beginning 0))))
  24230. (while (string-match "<" s)
  24231. (setq s (replace-match "&lt;" t t s)))
  24232. (while (string-match ">" s)
  24233. (setq s (replace-match "&gt;" t t s))))
  24234. s)
  24235. (defun org-export-cleanup-toc-line (s)
  24236. "Remove tags and time staps from lines going into the toc."
  24237. (when (memq org-export-with-tags '(not-in-toc nil))
  24238. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  24239. (setq s (replace-match "" t t s))))
  24240. (when org-export-remove-timestamps-from-toc
  24241. (while (string-match org-maybe-keyword-time-regexp s)
  24242. (setq s (replace-match "" t t s))))
  24243. (while (string-match org-bracket-link-regexp s)
  24244. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  24245. t t s)))
  24246. s)
  24247. (defun org-html-expand (string)
  24248. "Prepare STRING for HTML export. Applies all active conversions.
  24249. If there are links in the string, don't modify these."
  24250. (let* ((re (concat org-bracket-link-regexp "\\|"
  24251. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  24252. m s l res)
  24253. (while (setq m (string-match re string))
  24254. (setq s (substring string 0 m)
  24255. l (match-string 0 string)
  24256. string (substring string (match-end 0)))
  24257. (push (org-html-do-expand s) res)
  24258. (push l res))
  24259. (push (org-html-do-expand string) res)
  24260. (apply 'concat (nreverse res))))
  24261. (defun org-html-do-expand (s)
  24262. "Apply all active conversions to translate special ASCII to HTML."
  24263. (setq s (org-html-protect s))
  24264. (if org-export-html-expand
  24265. (let ((start 0))
  24266. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  24267. (setq s (replace-match "<\\1>" t nil s)))))
  24268. (if org-export-with-emphasize
  24269. (setq s (org-export-html-convert-emphasize s)))
  24270. (if org-export-with-special-strings
  24271. (setq s (org-export-html-convert-special-strings s)))
  24272. (if org-export-with-sub-superscripts
  24273. (setq s (org-export-html-convert-sub-super s)))
  24274. (if org-export-with-TeX-macros
  24275. (let ((start 0) wd ass)
  24276. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  24277. (if (get-text-property (match-beginning 0) 'org-protected s)
  24278. (setq start (match-end 0))
  24279. (setq wd (match-string 1 s))
  24280. (if (setq ass (assoc wd org-html-entities))
  24281. (setq s (replace-match (or (cdr ass)
  24282. (concat "&" (car ass) ";"))
  24283. t t s))
  24284. (setq start (+ start (length wd))))))))
  24285. s)
  24286. (defun org-create-multibrace-regexp (left right n)
  24287. "Create a regular expression which will match a balanced sexp.
  24288. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  24289. as single character strings.
  24290. The regexp returned will match the entire expression including the
  24291. delimiters. It will also define a single group which contains the
  24292. match except for the outermost delimiters. The maximum depth of
  24293. stacked delimiters is N. Escaping delimiters is not possible."
  24294. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  24295. (or "\\|")
  24296. (re nothing)
  24297. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  24298. (while (> n 1)
  24299. (setq n (1- n)
  24300. re (concat re or next)
  24301. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  24302. (concat left "\\(" re "\\)" right)))
  24303. (defvar org-match-substring-regexp
  24304. (concat
  24305. "\\([^\\]\\)\\([_^]\\)\\("
  24306. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24307. "\\|"
  24308. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  24309. "\\|"
  24310. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  24311. "The regular expression matching a sub- or superscript.")
  24312. (defvar org-match-substring-with-braces-regexp
  24313. (concat
  24314. "\\([^\\]\\)\\([_^]\\)\\("
  24315. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  24316. "\\)")
  24317. "The regular expression matching a sub- or superscript, forcing braces.")
  24318. (defconst org-export-html-special-string-regexps
  24319. '(("\\\\-" . "&shy;")
  24320. ("---\\([^-]\\)" . "&mdash;\\1")
  24321. ("--\\([^-]\\)" . "&ndash;\\1")
  24322. ("\\.\\.\\." . "&hellip;"))
  24323. "Regular expressions for special string conversion.")
  24324. (defun org-export-html-convert-special-strings (string)
  24325. "Convert special characters in STRING to HTML."
  24326. (let ((all org-export-html-special-string-regexps)
  24327. e a re rpl start)
  24328. (while (setq a (pop all))
  24329. (setq re (car a) rpl (cdr a) start 0)
  24330. (while (string-match re string start)
  24331. (if (get-text-property (match-beginning 0) 'org-protected string)
  24332. (setq start (match-end 0))
  24333. (setq string (replace-match rpl t nil string)))))
  24334. string))
  24335. (defun org-export-html-convert-sub-super (string)
  24336. "Convert sub- and superscripts in STRING to HTML."
  24337. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  24338. (while (string-match org-match-substring-regexp string s)
  24339. (cond
  24340. ((and requireb (match-end 8)) (setq s (match-end 2)))
  24341. ((get-text-property (match-beginning 2) 'org-protected string)
  24342. (setq s (match-end 2)))
  24343. (t
  24344. (setq s (match-end 1)
  24345. key (if (string= (match-string 2 string) "_") "sub" "sup")
  24346. c (or (match-string 8 string)
  24347. (match-string 6 string)
  24348. (match-string 5 string))
  24349. string (replace-match
  24350. (concat (match-string 1 string)
  24351. "<" key ">" c "</" key ">")
  24352. t t string)))))
  24353. (while (string-match "\\\\\\([_^]\\)" string)
  24354. (setq string (replace-match (match-string 1 string) t t string)))
  24355. string))
  24356. (defun org-export-html-convert-emphasize (string)
  24357. "Apply emphasis."
  24358. (let ((s 0) rpl)
  24359. (while (string-match org-emph-re string s)
  24360. (if (not (equal
  24361. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  24362. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  24363. (setq s (match-beginning 0)
  24364. rpl
  24365. (concat
  24366. (match-string 1 string)
  24367. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  24368. (match-string 4 string)
  24369. (nth 3 (assoc (match-string 3 string)
  24370. org-emphasis-alist))
  24371. (match-string 5 string))
  24372. string (replace-match rpl t t string)
  24373. s (+ s (- (length rpl) 2)))
  24374. (setq s (1+ s))))
  24375. string))
  24376. (defvar org-par-open nil)
  24377. (defun org-open-par ()
  24378. "Insert <p>, but first close previous paragraph if any."
  24379. (org-close-par-maybe)
  24380. (insert "\n<p>")
  24381. (setq org-par-open t))
  24382. (defun org-close-par-maybe ()
  24383. "Close paragraph if there is one open."
  24384. (when org-par-open
  24385. (insert "</p>")
  24386. (setq org-par-open nil)))
  24387. (defun org-close-li ()
  24388. "Close <li> if necessary."
  24389. (org-close-par-maybe)
  24390. (insert "</li>\n"))
  24391. (defvar body-only) ; dynamically scoped into this.
  24392. (defun org-html-level-start (level title umax with-toc head-count)
  24393. "Insert a new level in HTML export.
  24394. When TITLE is nil, just close all open levels."
  24395. (org-close-par-maybe)
  24396. (let ((l org-level-max))
  24397. (while (>= l level)
  24398. (if (aref org-levels-open (1- l))
  24399. (progn
  24400. (org-html-level-close l umax)
  24401. (aset org-levels-open (1- l) nil)))
  24402. (setq l (1- l)))
  24403. (when title
  24404. ;; If title is nil, this means this function is called to close
  24405. ;; all levels, so the rest is done only if title is given
  24406. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  24407. (setq title (replace-match
  24408. (if org-export-with-tags
  24409. (save-match-data
  24410. (concat
  24411. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  24412. (mapconcat 'identity (org-split-string
  24413. (match-string 1 title) ":")
  24414. "&nbsp;")
  24415. "</span>"))
  24416. "")
  24417. t t title)))
  24418. (if (> level umax)
  24419. (progn
  24420. (if (aref org-levels-open (1- level))
  24421. (progn
  24422. (org-close-li)
  24423. (insert "<li>" title "<br/>\n"))
  24424. (aset org-levels-open (1- level) t)
  24425. (org-close-par-maybe)
  24426. (insert "<ul>\n<li>" title "<br/>\n")))
  24427. (aset org-levels-open (1- level) t)
  24428. (if (and org-export-with-section-numbers (not body-only))
  24429. (setq title (concat (org-section-number level) " " title)))
  24430. (setq level (+ level org-export-html-toplevel-hlevel -1))
  24431. (if with-toc
  24432. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  24433. level level head-count title level))
  24434. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  24435. (org-open-par)))))
  24436. (defun org-html-level-close (level max-outline-level)
  24437. "Terminate one level in HTML export."
  24438. (if (<= level max-outline-level)
  24439. (insert "</div>\n")
  24440. (org-close-li)
  24441. (insert "</ul>\n")))
  24442. ;;; iCalendar export
  24443. ;;;###autoload
  24444. (defun org-export-icalendar-this-file ()
  24445. "Export current file as an iCalendar file.
  24446. The iCalendar file will be located in the same directory as the Org-mode
  24447. file, but with extension `.ics'."
  24448. (interactive)
  24449. (org-export-icalendar nil buffer-file-name))
  24450. ;;;###autoload
  24451. (defun org-export-icalendar-all-agenda-files ()
  24452. "Export all files in `org-agenda-files' to iCalendar .ics files.
  24453. Each iCalendar file will be located in the same directory as the Org-mode
  24454. file, but with extension `.ics'."
  24455. (interactive)
  24456. (apply 'org-export-icalendar nil (org-agenda-files t)))
  24457. ;;;###autoload
  24458. (defun org-export-icalendar-combine-agenda-files ()
  24459. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  24460. The file is stored under the name `org-combined-agenda-icalendar-file'."
  24461. (interactive)
  24462. (apply 'org-export-icalendar t (org-agenda-files t)))
  24463. (defun org-export-icalendar (combine &rest files)
  24464. "Create iCalendar files for all elements of FILES.
  24465. If COMBINE is non-nil, combine all calendar entries into a single large
  24466. file and store it under the name `org-combined-agenda-icalendar-file'."
  24467. (save-excursion
  24468. (org-prepare-agenda-buffers files)
  24469. (let* ((dir (org-export-directory
  24470. :ical (list :publishing-directory
  24471. org-export-publishing-directory)))
  24472. file ical-file ical-buffer category started org-agenda-new-buffers)
  24473. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24474. (when combine
  24475. (setq ical-file
  24476. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24477. org-combined-agenda-icalendar-file
  24478. (expand-file-name org-combined-agenda-icalendar-file dir))
  24479. ical-buffer (org-get-agenda-file-buffer ical-file))
  24480. (set-buffer ical-buffer) (erase-buffer))
  24481. (while (setq file (pop files))
  24482. (catch 'nextfile
  24483. (org-check-agenda-file file)
  24484. (set-buffer (org-get-agenda-file-buffer file))
  24485. (unless combine
  24486. (setq ical-file (concat (file-name-as-directory dir)
  24487. (file-name-sans-extension
  24488. (file-name-nondirectory buffer-file-name))
  24489. ".ics"))
  24490. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24491. (with-current-buffer ical-buffer (erase-buffer)))
  24492. (setq category (or org-category
  24493. (file-name-sans-extension
  24494. (file-name-nondirectory buffer-file-name))))
  24495. (if (symbolp category) (setq category (symbol-name category)))
  24496. (let ((standard-output ical-buffer))
  24497. (if combine
  24498. (and (not started) (setq started t)
  24499. (org-start-icalendar-file org-icalendar-combined-name))
  24500. (org-start-icalendar-file category))
  24501. (org-print-icalendar-entries combine)
  24502. (when (or (and combine (not files)) (not combine))
  24503. (org-finish-icalendar-file)
  24504. (set-buffer ical-buffer)
  24505. (save-buffer)
  24506. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24507. (org-release-buffers org-agenda-new-buffers))))
  24508. (defvar org-after-save-iCalendar-file-hook nil
  24509. "Hook run after an iCalendar file has been saved.
  24510. The iCalendar buffer is still current when this hook is run.
  24511. A good way to use this is to tell a desktop calenndar application to re-read
  24512. the iCalendar file.")
  24513. (defun org-print-icalendar-entries (&optional combine)
  24514. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24515. When COMBINE is non nil, add the category to each line."
  24516. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24517. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24518. (dts (org-ical-ts-to-string
  24519. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24520. "DTSTART"))
  24521. hd ts ts2 state status (inc t) pos b sexp rrule
  24522. scheduledp deadlinep tmp pri category entry location summary desc
  24523. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24524. (org-refresh-category-properties)
  24525. (save-excursion
  24526. (goto-char (point-min))
  24527. (while (re-search-forward re1 nil t)
  24528. (catch :skip
  24529. (org-agenda-skip)
  24530. (setq pos (match-beginning 0)
  24531. ts (match-string 0)
  24532. inc t
  24533. hd (org-get-heading)
  24534. summary (org-icalendar-cleanup-string
  24535. (org-entry-get nil "SUMMARY"))
  24536. desc (org-icalendar-cleanup-string
  24537. (or (org-entry-get nil "DESCRIPTION")
  24538. (and org-icalendar-include-body (org-get-entry)))
  24539. t org-icalendar-include-body)
  24540. location (org-icalendar-cleanup-string
  24541. (org-entry-get nil "LOCATION"))
  24542. category (org-get-category))
  24543. (if (looking-at re2)
  24544. (progn
  24545. (goto-char (match-end 0))
  24546. (setq ts2 (match-string 1) inc nil))
  24547. (setq tmp (buffer-substring (max (point-min)
  24548. (- pos org-ds-keyword-length))
  24549. pos)
  24550. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24551. (progn
  24552. (setq inc nil)
  24553. (replace-match "\\1" t nil ts))
  24554. ts)
  24555. deadlinep (string-match org-deadline-regexp tmp)
  24556. scheduledp (string-match org-scheduled-regexp tmp)
  24557. ;; donep (org-entry-is-done-p)
  24558. ))
  24559. (if (or (string-match org-tr-regexp hd)
  24560. (string-match org-ts-regexp hd))
  24561. (setq hd (replace-match "" t t hd)))
  24562. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24563. (setq rrule
  24564. (concat "\nRRULE:FREQ="
  24565. (cdr (assoc
  24566. (match-string 2 ts)
  24567. '(("d" . "DAILY")("w" . "WEEKLY")
  24568. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24569. ";INTERVAL=" (match-string 1 ts)))
  24570. (setq rrule ""))
  24571. (setq summary (or summary hd))
  24572. (if (string-match org-bracket-link-regexp summary)
  24573. (setq summary
  24574. (replace-match (if (match-end 3)
  24575. (match-string 3 summary)
  24576. (match-string 1 summary))
  24577. t t summary)))
  24578. (if deadlinep (setq summary (concat "DL: " summary)))
  24579. (if scheduledp (setq summary (concat "S: " summary)))
  24580. (if (string-match "\\`<%%" ts)
  24581. (with-current-buffer sexp-buffer
  24582. (insert (substring ts 1 -1) " " summary "\n"))
  24583. (princ (format "BEGIN:VEVENT
  24584. %s
  24585. %s%s
  24586. SUMMARY:%s%s%s
  24587. CATEGORIES:%s
  24588. END:VEVENT\n"
  24589. (org-ical-ts-to-string ts "DTSTART")
  24590. (org-ical-ts-to-string ts2 "DTEND" inc)
  24591. rrule summary
  24592. (if (and desc (string-match "\\S-" desc))
  24593. (concat "\nDESCRIPTION: " desc) "")
  24594. (if (and location (string-match "\\S-" location))
  24595. (concat "\nLOCATION: " location) "")
  24596. category)))))
  24597. (when (and org-icalendar-include-sexps
  24598. (condition-case nil (require 'icalendar) (error nil))
  24599. (fboundp 'icalendar-export-region))
  24600. ;; Get all the literal sexps
  24601. (goto-char (point-min))
  24602. (while (re-search-forward "^&?%%(" nil t)
  24603. (catch :skip
  24604. (org-agenda-skip)
  24605. (setq b (match-beginning 0))
  24606. (goto-char (1- (match-end 0)))
  24607. (forward-sexp 1)
  24608. (end-of-line 1)
  24609. (setq sexp (buffer-substring b (point)))
  24610. (with-current-buffer sexp-buffer
  24611. (insert sexp "\n"))
  24612. (princ (org-diary-to-ical-string sexp-buffer)))))
  24613. (when org-icalendar-include-todo
  24614. (goto-char (point-min))
  24615. (while (re-search-forward org-todo-line-regexp nil t)
  24616. (catch :skip
  24617. (org-agenda-skip)
  24618. (setq state (match-string 2))
  24619. (setq status (if (member state org-done-keywords)
  24620. "COMPLETED" "NEEDS-ACTION"))
  24621. (when (and state
  24622. (or (not (member state org-done-keywords))
  24623. (eq org-icalendar-include-todo 'all))
  24624. (not (member org-archive-tag (org-get-tags-at)))
  24625. )
  24626. (setq hd (match-string 3)
  24627. summary (org-icalendar-cleanup-string
  24628. (org-entry-get nil "SUMMARY"))
  24629. desc (org-icalendar-cleanup-string
  24630. (or (org-entry-get nil "DESCRIPTION")
  24631. (and org-icalendar-include-body (org-get-entry)))
  24632. t org-icalendar-include-body)
  24633. location (org-icalendar-cleanup-string
  24634. (org-entry-get nil "LOCATION")))
  24635. (if (string-match org-bracket-link-regexp hd)
  24636. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24637. (match-string 1 hd))
  24638. t t hd)))
  24639. (if (string-match org-priority-regexp hd)
  24640. (setq pri (string-to-char (match-string 2 hd))
  24641. hd (concat (substring hd 0 (match-beginning 1))
  24642. (substring hd (match-end 1))))
  24643. (setq pri org-default-priority))
  24644. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24645. (- org-lowest-priority org-highest-priority))))))
  24646. (princ (format "BEGIN:VTODO
  24647. %s
  24648. SUMMARY:%s%s%s
  24649. CATEGORIES:%s
  24650. SEQUENCE:1
  24651. PRIORITY:%d
  24652. STATUS:%s
  24653. END:VTODO\n"
  24654. dts
  24655. (or summary hd)
  24656. (if (and location (string-match "\\S-" location))
  24657. (concat "\nLOCATION: " location) "")
  24658. (if (and desc (string-match "\\S-" desc))
  24659. (concat "\nDESCRIPTION: " desc) "")
  24660. category pri status)))))))))
  24661. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24662. "Take out stuff and quote what needs to be quoted.
  24663. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24664. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24665. characters."
  24666. (if (not s)
  24667. nil
  24668. (when is-body
  24669. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24670. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24671. (while (string-match re s) (setq s (replace-match "" t t s)))
  24672. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24673. (let ((start 0))
  24674. (while (string-match "\\([,;\\]\\)" s start)
  24675. (setq start (+ (match-beginning 0) 2)
  24676. s (replace-match "\\\\\\1" nil nil s))))
  24677. (when is-body
  24678. (while (string-match "[ \t]*\n[ \t]*" s)
  24679. (setq s (replace-match "\\n" t t s))))
  24680. (setq s (org-trim s))
  24681. (if is-body
  24682. (if maxlength
  24683. (if (and (numberp maxlength)
  24684. (> (length s) maxlength))
  24685. (setq s (substring s 0 maxlength)))))
  24686. s))
  24687. (defun org-get-entry ()
  24688. "Clean-up description string."
  24689. (save-excursion
  24690. (org-back-to-heading t)
  24691. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24692. (defun org-start-icalendar-file (name)
  24693. "Start an iCalendar file by inserting the header."
  24694. (let ((user user-full-name)
  24695. (name (or name "unknown"))
  24696. (timezone (cadr (current-time-zone))))
  24697. (princ
  24698. (format "BEGIN:VCALENDAR
  24699. VERSION:2.0
  24700. X-WR-CALNAME:%s
  24701. PRODID:-//%s//Emacs with Org-mode//EN
  24702. X-WR-TIMEZONE:%s
  24703. CALSCALE:GREGORIAN\n" name user timezone))))
  24704. (defun org-finish-icalendar-file ()
  24705. "Finish an iCalendar file by inserting the END statement."
  24706. (princ "END:VCALENDAR\n"))
  24707. (defun org-ical-ts-to-string (s keyword &optional inc)
  24708. "Take a time string S and convert it to iCalendar format.
  24709. KEYWORD is added in front, to make a complete line like DTSTART....
  24710. When INC is non-nil, increase the hour by two (if time string contains
  24711. a time), or the day by one (if it does not contain a time)."
  24712. (let ((t1 (org-parse-time-string s 'nodefault))
  24713. t2 fmt have-time time)
  24714. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24715. (setq t2 t1 have-time t)
  24716. (setq t2 (org-parse-time-string s)))
  24717. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24718. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24719. (when inc
  24720. (if have-time
  24721. (if org-agenda-default-appointment-duration
  24722. (setq mi (+ org-agenda-default-appointment-duration mi))
  24723. (setq h (+ 2 h)))
  24724. (setq d (1+ d))))
  24725. (setq time (encode-time s mi h d m y)))
  24726. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24727. (concat keyword (format-time-string fmt time))))
  24728. ;;; XOXO export
  24729. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24730. (with-current-buffer buffer
  24731. (apply 'insert output)))
  24732. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24733. (defun org-export-as-xoxo (&optional buffer)
  24734. "Export the org buffer as XOXO.
  24735. The XOXO buffer is named *xoxo-<source buffer name>*"
  24736. (interactive (list (current-buffer)))
  24737. ;; A quickie abstraction
  24738. ;; Output everything as XOXO
  24739. (with-current-buffer (get-buffer buffer)
  24740. (let* ((pos (point))
  24741. (opt-plist (org-combine-plists (org-default-export-plist)
  24742. (org-infile-export-plist)))
  24743. (filename (concat (file-name-as-directory
  24744. (org-export-directory :xoxo opt-plist))
  24745. (file-name-sans-extension
  24746. (file-name-nondirectory buffer-file-name))
  24747. ".html"))
  24748. (out (find-file-noselect filename))
  24749. (last-level 1)
  24750. (hanging-li nil))
  24751. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24752. ;; Check the output buffer is empty.
  24753. (with-current-buffer out (erase-buffer))
  24754. ;; Kick off the output
  24755. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24756. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24757. (let* ((hd (match-string-no-properties 1))
  24758. (level (length hd))
  24759. (text (concat
  24760. (match-string-no-properties 2)
  24761. (save-excursion
  24762. (goto-char (match-end 0))
  24763. (let ((str ""))
  24764. (catch 'loop
  24765. (while 't
  24766. (forward-line)
  24767. (if (looking-at "^[ \t]\\(.*\\)")
  24768. (setq str (concat str (match-string-no-properties 1)))
  24769. (throw 'loop str)))))))))
  24770. ;; Handle level rendering
  24771. (cond
  24772. ((> level last-level)
  24773. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24774. ((< level last-level)
  24775. (dotimes (- (- last-level level) 1)
  24776. (if hanging-li
  24777. (org-export-as-xoxo-insert-into out "</li>\n"))
  24778. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24779. (when hanging-li
  24780. (org-export-as-xoxo-insert-into out "</li>\n")
  24781. (setq hanging-li nil)))
  24782. ((equal level last-level)
  24783. (if hanging-li
  24784. (org-export-as-xoxo-insert-into out "</li>\n")))
  24785. )
  24786. (setq last-level level)
  24787. ;; And output the new li
  24788. (setq hanging-li 't)
  24789. (if (equal ?+ (elt text 0))
  24790. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24791. (org-export-as-xoxo-insert-into out "<li>" text))))
  24792. ;; Finally finish off the ol
  24793. (dotimes (- last-level 1)
  24794. (if hanging-li
  24795. (org-export-as-xoxo-insert-into out "</li>\n"))
  24796. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24797. (goto-char pos)
  24798. ;; Finish the buffer off and clean it up.
  24799. (switch-to-buffer-other-window out)
  24800. (indent-region (point-min) (point-max) nil)
  24801. (save-buffer)
  24802. (goto-char (point-min))
  24803. )))
  24804. ;;;; Key bindings
  24805. ;; Make `C-c C-x' a prefix key
  24806. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24807. ;; TAB key with modifiers
  24808. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24809. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24810. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24811. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24812. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24813. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24814. ;; The following line is necessary under Suse GNU/Linux
  24815. (unless (featurep 'xemacs)
  24816. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24817. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24818. (define-key org-mode-map [backtab] 'org-shifttab)
  24819. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24820. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24821. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24822. ;; Cursor keys with modifiers
  24823. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24824. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24825. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24826. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24827. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24828. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24829. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24830. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24831. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24832. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24833. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24834. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24835. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24836. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24837. ;;; Extra keys for tty access.
  24838. ;; We only set them when really needed because otherwise the
  24839. ;; menus don't show the simple keys
  24840. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24841. (not window-system))
  24842. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24843. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24844. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24845. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24846. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24847. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24848. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24849. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24850. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24851. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24852. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24853. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24854. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24855. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24856. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24857. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24858. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24859. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24860. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24861. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24862. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24863. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24864. ;; All the other keys
  24865. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24866. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24867. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24868. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24869. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24870. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24871. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24872. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24873. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24874. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24875. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24876. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24877. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24878. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24879. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24880. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24881. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24882. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24883. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24884. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24885. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24886. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24887. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24888. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24889. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24890. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24891. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24892. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24893. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24894. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24895. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24896. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24897. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24898. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24899. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24900. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24901. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24902. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24903. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24904. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24905. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24906. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24907. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24908. (org-defkey org-mode-map "\C-m" 'org-return)
  24909. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24910. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24911. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24912. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24913. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24914. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24915. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24916. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24917. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  24918. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24919. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24920. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24921. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24922. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24923. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24924. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24925. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24926. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24927. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24928. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24929. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24930. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24931. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24932. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24933. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24934. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24935. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24936. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24937. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24938. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24939. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24940. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24941. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24942. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24943. (when (featurep 'xemacs)
  24944. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24945. (defsubst org-table-p () (org-at-table-p))
  24946. (defun org-self-insert-command (N)
  24947. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24948. If the cursor is in a table looking at whitespace, the whitespace is
  24949. overwritten, and the table is not marked as requiring realignment."
  24950. (interactive "p")
  24951. (if (and (org-table-p)
  24952. (progn
  24953. ;; check if we blank the field, and if that triggers align
  24954. (and org-table-auto-blank-field
  24955. (member last-command
  24956. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24957. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24958. ;; got extra space, this field does not determine column width
  24959. (let (org-table-may-need-update) (org-table-blank-field))
  24960. ;; no extra space, this field may determine column width
  24961. (org-table-blank-field)))
  24962. t)
  24963. (eq N 1)
  24964. (looking-at "[^|\n]* |"))
  24965. (let (org-table-may-need-update)
  24966. (goto-char (1- (match-end 0)))
  24967. (delete-backward-char 1)
  24968. (goto-char (match-beginning 0))
  24969. (self-insert-command N))
  24970. (setq org-table-may-need-update t)
  24971. (self-insert-command N)
  24972. (org-fix-tags-on-the-fly)))
  24973. (defun org-fix-tags-on-the-fly ()
  24974. (when (and (equal (char-after (point-at-bol)) ?*)
  24975. (org-on-heading-p))
  24976. (org-align-tags-here org-tags-column)))
  24977. (defun org-delete-backward-char (N)
  24978. "Like `delete-backward-char', insert whitespace at field end in tables.
  24979. When deleting backwards, in tables this function will insert whitespace in
  24980. front of the next \"|\" separator, to keep the table aligned. The table will
  24981. still be marked for re-alignment if the field did fill the entire column,
  24982. because, in this case the deletion might narrow the column."
  24983. (interactive "p")
  24984. (if (and (org-table-p)
  24985. (eq N 1)
  24986. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24987. (looking-at ".*?|"))
  24988. (let ((pos (point))
  24989. (noalign (looking-at "[^|\n\r]* |"))
  24990. (c org-table-may-need-update))
  24991. (backward-delete-char N)
  24992. (skip-chars-forward "^|")
  24993. (insert " ")
  24994. (goto-char (1- pos))
  24995. ;; noalign: if there were two spaces at the end, this field
  24996. ;; does not determine the width of the column.
  24997. (if noalign (setq org-table-may-need-update c)))
  24998. (backward-delete-char N)
  24999. (org-fix-tags-on-the-fly)))
  25000. (defun org-delete-char (N)
  25001. "Like `delete-char', but insert whitespace at field end in tables.
  25002. When deleting characters, in tables this function will insert whitespace in
  25003. front of the next \"|\" separator, to keep the table aligned. The table will
  25004. still be marked for re-alignment if the field did fill the entire column,
  25005. because, in this case the deletion might narrow the column."
  25006. (interactive "p")
  25007. (if (and (org-table-p)
  25008. (not (bolp))
  25009. (not (= (char-after) ?|))
  25010. (eq N 1))
  25011. (if (looking-at ".*?|")
  25012. (let ((pos (point))
  25013. (noalign (looking-at "[^|\n\r]* |"))
  25014. (c org-table-may-need-update))
  25015. (replace-match (concat
  25016. (substring (match-string 0) 1 -1)
  25017. " |"))
  25018. (goto-char pos)
  25019. ;; noalign: if there were two spaces at the end, this field
  25020. ;; does not determine the width of the column.
  25021. (if noalign (setq org-table-may-need-update c)))
  25022. (delete-char N))
  25023. (delete-char N)
  25024. (org-fix-tags-on-the-fly)))
  25025. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  25026. (put 'org-self-insert-command 'delete-selection t)
  25027. (put 'orgtbl-self-insert-command 'delete-selection t)
  25028. (put 'org-delete-char 'delete-selection 'supersede)
  25029. (put 'org-delete-backward-char 'delete-selection 'supersede)
  25030. ;; Make `flyspell-mode' delay after some commands
  25031. (put 'org-self-insert-command 'flyspell-delayed t)
  25032. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  25033. (put 'org-delete-char 'flyspell-delayed t)
  25034. (put 'org-delete-backward-char 'flyspell-delayed t)
  25035. ;; Make pabbrev-mode expand after org-mode commands
  25036. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  25037. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  25038. ;; How to do this: Measure non-white length of current string
  25039. ;; If equal to column width, we should realign.
  25040. (defun org-remap (map &rest commands)
  25041. "In MAP, remap the functions given in COMMANDS.
  25042. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  25043. (let (new old)
  25044. (while commands
  25045. (setq old (pop commands) new (pop commands))
  25046. (if (fboundp 'command-remapping)
  25047. (org-defkey map (vector 'remap old) new)
  25048. (substitute-key-definition old new map global-map)))))
  25049. (when (eq org-enable-table-editor 'optimized)
  25050. ;; If the user wants maximum table support, we need to hijack
  25051. ;; some standard editing functions
  25052. (org-remap org-mode-map
  25053. 'self-insert-command 'org-self-insert-command
  25054. 'delete-char 'org-delete-char
  25055. 'delete-backward-char 'org-delete-backward-char)
  25056. (org-defkey org-mode-map "|" 'org-force-self-insert))
  25057. (defun org-shiftcursor-error ()
  25058. "Throw an error because Shift-Cursor command was applied in wrong context."
  25059. (error "This command is active in special context like tables, headlines or timestamps"))
  25060. (defun org-shifttab (&optional arg)
  25061. "Global visibility cycling or move to previous table field.
  25062. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  25063. on context.
  25064. See the individual commands for more information."
  25065. (interactive "P")
  25066. (cond
  25067. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  25068. (arg (message "Content view to level: ")
  25069. (org-content (prefix-numeric-value arg))
  25070. (setq org-cycle-global-status 'overview))
  25071. (t (call-interactively 'org-global-cycle))))
  25072. (defun org-shiftmetaleft ()
  25073. "Promote subtree or delete table column.
  25074. Calls `org-promote-subtree', `org-outdent-item',
  25075. or `org-table-delete-column', depending on context.
  25076. See the individual commands for more information."
  25077. (interactive)
  25078. (cond
  25079. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  25080. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  25081. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25082. (t (org-shiftcursor-error))))
  25083. (defun org-shiftmetaright ()
  25084. "Demote subtree or insert table column.
  25085. Calls `org-demote-subtree', `org-indent-item',
  25086. or `org-table-insert-column', depending on context.
  25087. See the individual commands for more information."
  25088. (interactive)
  25089. (cond
  25090. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  25091. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  25092. ((org-at-item-p) (call-interactively 'org-indent-item))
  25093. (t (org-shiftcursor-error))))
  25094. (defun org-shiftmetaup (&optional arg)
  25095. "Move subtree up or kill table row.
  25096. Calls `org-move-subtree-up' or `org-table-kill-row' or
  25097. `org-move-item-up' depending on context. See the individual commands
  25098. for more information."
  25099. (interactive "P")
  25100. (cond
  25101. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  25102. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25103. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25104. (t (org-shiftcursor-error))))
  25105. (defun org-shiftmetadown (&optional arg)
  25106. "Move subtree down or insert table row.
  25107. Calls `org-move-subtree-down' or `org-table-insert-row' or
  25108. `org-move-item-down', depending on context. See the individual
  25109. commands for more information."
  25110. (interactive "P")
  25111. (cond
  25112. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  25113. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25114. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25115. (t (org-shiftcursor-error))))
  25116. (defun org-metaleft (&optional arg)
  25117. "Promote heading or move table column to left.
  25118. Calls `org-do-promote' or `org-table-move-column', depending on context.
  25119. With no specific context, calls the Emacs default `backward-word'.
  25120. See the individual commands for more information."
  25121. (interactive "P")
  25122. (cond
  25123. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  25124. ((or (org-on-heading-p) (org-region-active-p))
  25125. (call-interactively 'org-do-promote))
  25126. ((org-at-item-p) (call-interactively 'org-outdent-item))
  25127. (t (call-interactively 'backward-word))))
  25128. (defun org-metaright (&optional arg)
  25129. "Demote subtree or move table column to right.
  25130. Calls `org-do-demote' or `org-table-move-column', depending on context.
  25131. With no specific context, calls the Emacs default `forward-word'.
  25132. See the individual commands for more information."
  25133. (interactive "P")
  25134. (cond
  25135. ((org-at-table-p) (call-interactively 'org-table-move-column))
  25136. ((or (org-on-heading-p) (org-region-active-p))
  25137. (call-interactively 'org-do-demote))
  25138. ((org-at-item-p) (call-interactively 'org-indent-item))
  25139. (t (call-interactively 'forward-word))))
  25140. (defun org-metaup (&optional arg)
  25141. "Move subtree up or move table row up.
  25142. Calls `org-move-subtree-up' or `org-table-move-row' or
  25143. `org-move-item-up', depending on context. See the individual commands
  25144. for more information."
  25145. (interactive "P")
  25146. (cond
  25147. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  25148. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  25149. ((org-at-item-p) (call-interactively 'org-move-item-up))
  25150. (t (transpose-lines 1) (beginning-of-line -1))))
  25151. (defun org-metadown (&optional arg)
  25152. "Move subtree down or move table row down.
  25153. Calls `org-move-subtree-down' or `org-table-move-row' or
  25154. `org-move-item-down', depending on context. See the individual
  25155. commands for more information."
  25156. (interactive "P")
  25157. (cond
  25158. ((org-at-table-p) (call-interactively 'org-table-move-row))
  25159. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  25160. ((org-at-item-p) (call-interactively 'org-move-item-down))
  25161. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  25162. (defun org-shiftup (&optional arg)
  25163. "Increase item in timestamp or increase priority of current headline.
  25164. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  25165. depending on context. See the individual commands for more information."
  25166. (interactive "P")
  25167. (cond
  25168. ((org-at-timestamp-p t)
  25169. (call-interactively (if org-edit-timestamp-down-means-later
  25170. 'org-timestamp-down 'org-timestamp-up)))
  25171. ((org-on-heading-p) (call-interactively 'org-priority-up))
  25172. ((org-at-item-p) (call-interactively 'org-previous-item))
  25173. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  25174. (defun org-shiftdown (&optional arg)
  25175. "Decrease item in timestamp or decrease priority of current headline.
  25176. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  25177. depending on context. See the individual commands for more information."
  25178. (interactive "P")
  25179. (cond
  25180. ((org-at-timestamp-p t)
  25181. (call-interactively (if org-edit-timestamp-down-means-later
  25182. 'org-timestamp-up 'org-timestamp-down)))
  25183. ((org-on-heading-p) (call-interactively 'org-priority-down))
  25184. (t (call-interactively 'org-next-item))))
  25185. (defun org-shiftright ()
  25186. "Next TODO keyword or timestamp one day later, depending on context."
  25187. (interactive)
  25188. (cond
  25189. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  25190. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  25191. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  25192. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  25193. (t (org-shiftcursor-error))))
  25194. (defun org-shiftleft ()
  25195. "Previous TODO keyword or timestamp one day earlier, depending on context."
  25196. (interactive)
  25197. (cond
  25198. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  25199. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  25200. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  25201. ((org-at-property-p)
  25202. (call-interactively 'org-property-previous-allowed-value))
  25203. (t (org-shiftcursor-error))))
  25204. (defun org-shiftcontrolright ()
  25205. "Switch to next TODO set."
  25206. (interactive)
  25207. (cond
  25208. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  25209. (t (org-shiftcursor-error))))
  25210. (defun org-shiftcontrolleft ()
  25211. "Switch to previous TODO set."
  25212. (interactive)
  25213. (cond
  25214. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  25215. (t (org-shiftcursor-error))))
  25216. (defun org-ctrl-c-ret ()
  25217. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  25218. (interactive)
  25219. (cond
  25220. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  25221. (t (call-interactively 'org-insert-heading))))
  25222. (defun org-copy-special ()
  25223. "Copy region in table or copy current subtree.
  25224. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  25225. See the individual commands for more information."
  25226. (interactive)
  25227. (call-interactively
  25228. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  25229. (defun org-cut-special ()
  25230. "Cut region in table or cut current subtree.
  25231. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  25232. See the individual commands for more information."
  25233. (interactive)
  25234. (call-interactively
  25235. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  25236. (defun org-paste-special (arg)
  25237. "Paste rectangular region into table, or past subtree relative to level.
  25238. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  25239. See the individual commands for more information."
  25240. (interactive "P")
  25241. (if (org-at-table-p)
  25242. (org-table-paste-rectangle)
  25243. (org-paste-subtree arg)))
  25244. (defun org-ctrl-c-ctrl-c (&optional arg)
  25245. "Set tags in headline, or update according to changed information at point.
  25246. This command does many different things, depending on context:
  25247. - If the cursor is in a headline, prompt for tags and insert them
  25248. into the current line, aligned to `org-tags-column'. When called
  25249. with prefix arg, realign all tags in the current buffer.
  25250. - If the cursor is in one of the special #+KEYWORD lines, this
  25251. triggers scanning the buffer for these lines and updating the
  25252. information.
  25253. - If the cursor is inside a table, realign the table. This command
  25254. works even if the automatic table editor has been turned off.
  25255. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  25256. the entire table.
  25257. - If the cursor is a the beginning of a dynamic block, update it.
  25258. - If the cursor is inside a table created by the table.el package,
  25259. activate that table.
  25260. - If the current buffer is a remember buffer, close note and file it.
  25261. with a prefix argument, file it without further interaction to the default
  25262. location.
  25263. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  25264. links in this buffer.
  25265. - If the cursor is on a numbered item in a plain list, renumber the
  25266. ordered list.
  25267. - If the cursor is on a checkbox, toggle it."
  25268. (interactive "P")
  25269. (let ((org-enable-table-editor t))
  25270. (cond
  25271. ((or org-clock-overlays
  25272. org-occur-highlights
  25273. org-latex-fragment-image-overlays)
  25274. (org-remove-clock-overlays)
  25275. (org-remove-occur-highlights)
  25276. (org-remove-latex-fragment-image-overlays)
  25277. (message "Temporary highlights/overlays removed from current buffer"))
  25278. ((and (local-variable-p 'org-finish-function (current-buffer))
  25279. (fboundp org-finish-function))
  25280. (funcall org-finish-function))
  25281. ((org-at-property-p)
  25282. (call-interactively 'org-property-action))
  25283. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  25284. ((org-on-heading-p) (call-interactively 'org-set-tags))
  25285. ((org-at-table.el-p)
  25286. (require 'table)
  25287. (beginning-of-line 1)
  25288. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  25289. (call-interactively 'table-recognize-table))
  25290. ((org-at-table-p)
  25291. (org-table-maybe-eval-formula)
  25292. (if arg
  25293. (call-interactively 'org-table-recalculate)
  25294. (org-table-maybe-recalculate-line))
  25295. (call-interactively 'org-table-align))
  25296. ((org-at-item-checkbox-p)
  25297. (call-interactively 'org-toggle-checkbox))
  25298. ((org-at-item-p)
  25299. (call-interactively 'org-maybe-renumber-ordered-list))
  25300. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  25301. ;; Dynamic block
  25302. (beginning-of-line 1)
  25303. (org-update-dblock))
  25304. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  25305. (cond
  25306. ((equal (match-string 1) "TBLFM")
  25307. ;; Recalculate the table before this line
  25308. (save-excursion
  25309. (beginning-of-line 1)
  25310. (skip-chars-backward " \r\n\t")
  25311. (if (org-at-table-p)
  25312. (org-call-with-arg 'org-table-recalculate t))))
  25313. (t
  25314. (call-interactively 'org-mode-restart))))
  25315. (t (error "C-c C-c can do nothing useful at this location.")))))
  25316. (defun org-mode-restart ()
  25317. "Restart Org-mode, to scan again for special lines.
  25318. Also updates the keyword regular expressions."
  25319. (interactive)
  25320. (let ((org-inhibit-startup t)) (org-mode))
  25321. (message "Org-mode restarted to refresh keyword and special line setup"))
  25322. (defun org-kill-note-or-show-branches ()
  25323. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  25324. (interactive)
  25325. (if (not org-finish-function)
  25326. (call-interactively 'show-branches)
  25327. (let ((org-note-abort t))
  25328. (funcall org-finish-function))))
  25329. (defun org-return (&optional indent)
  25330. "Goto next table row or insert a newline.
  25331. Calls `org-table-next-row' or `newline', depending on context.
  25332. See the individual commands for more information."
  25333. (interactive)
  25334. (cond
  25335. ((bobp) (if indent (newline-and-indent) (newline)))
  25336. ((and (org-at-heading-p)
  25337. (looking-at
  25338. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  25339. (org-show-entry)
  25340. (end-of-line 1)
  25341. (newline))
  25342. ((org-at-table-p)
  25343. (org-table-justify-field-maybe)
  25344. (call-interactively 'org-table-next-row))
  25345. (t (if indent (newline-and-indent) (newline)))))
  25346. (defun org-return-indent ()
  25347. (interactive)
  25348. "Goto next table row or insert a newline and indent.
  25349. Calls `org-table-next-row' or `newline-and-indent', depending on
  25350. context. See the individual commands for more information."
  25351. (org-return t))
  25352. (defun org-ctrl-c-minus ()
  25353. "Insert separator line in table or modify bullet type in list.
  25354. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  25355. depending on context."
  25356. (interactive)
  25357. (cond
  25358. ((org-at-table-p)
  25359. (call-interactively 'org-table-insert-hline))
  25360. ((org-on-heading-p)
  25361. ;; Convert to item
  25362. (save-excursion
  25363. (beginning-of-line 1)
  25364. (if (looking-at "\\*+ ")
  25365. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  25366. ((org-in-item-p)
  25367. (call-interactively 'org-cycle-list-bullet))
  25368. (t (error "`C-c -' does have no function here."))))
  25369. (defun org-meta-return (&optional arg)
  25370. "Insert a new heading or wrap a region in a table.
  25371. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  25372. See the individual commands for more information."
  25373. (interactive "P")
  25374. (cond
  25375. ((org-at-table-p)
  25376. (call-interactively 'org-table-wrap-region))
  25377. (t (call-interactively 'org-insert-heading))))
  25378. ;;; Menu entries
  25379. ;; Define the Org-mode menus
  25380. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  25381. '("Tbl"
  25382. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  25383. ["Next Field" org-cycle (org-at-table-p)]
  25384. ["Previous Field" org-shifttab (org-at-table-p)]
  25385. ["Next Row" org-return (org-at-table-p)]
  25386. "--"
  25387. ["Blank Field" org-table-blank-field (org-at-table-p)]
  25388. ["Edit Field" org-table-edit-field (org-at-table-p)]
  25389. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  25390. "--"
  25391. ("Column"
  25392. ["Move Column Left" org-metaleft (org-at-table-p)]
  25393. ["Move Column Right" org-metaright (org-at-table-p)]
  25394. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  25395. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  25396. ("Row"
  25397. ["Move Row Up" org-metaup (org-at-table-p)]
  25398. ["Move Row Down" org-metadown (org-at-table-p)]
  25399. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  25400. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  25401. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  25402. "--"
  25403. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  25404. ("Rectangle"
  25405. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  25406. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  25407. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  25408. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  25409. "--"
  25410. ("Calculate"
  25411. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  25412. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  25413. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  25414. "--"
  25415. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  25416. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  25417. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  25418. "--"
  25419. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  25420. "--"
  25421. ["Sum Column/Rectangle" org-table-sum
  25422. (or (org-at-table-p) (org-region-active-p))]
  25423. ["Which Column?" org-table-current-column (org-at-table-p)])
  25424. ["Debug Formulas"
  25425. org-table-toggle-formula-debugger
  25426. :style toggle :selected org-table-formula-debug]
  25427. ["Show Col/Row Numbers"
  25428. org-table-toggle-coordinate-overlays
  25429. :style toggle :selected org-table-overlay-coordinates]
  25430. "--"
  25431. ["Create" org-table-create (and (not (org-at-table-p))
  25432. org-enable-table-editor)]
  25433. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  25434. ["Import from File" org-table-import (not (org-at-table-p))]
  25435. ["Export to File" org-table-export (org-at-table-p)]
  25436. "--"
  25437. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  25438. (easy-menu-define org-org-menu org-mode-map "Org menu"
  25439. '("Org"
  25440. ("Show/Hide"
  25441. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  25442. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  25443. ["Sparse Tree" org-occur t]
  25444. ["Reveal Context" org-reveal t]
  25445. ["Show All" show-all t]
  25446. "--"
  25447. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  25448. "--"
  25449. ["New Heading" org-insert-heading t]
  25450. ("Navigate Headings"
  25451. ["Up" outline-up-heading t]
  25452. ["Next" outline-next-visible-heading t]
  25453. ["Previous" outline-previous-visible-heading t]
  25454. ["Next Same Level" outline-forward-same-level t]
  25455. ["Previous Same Level" outline-backward-same-level t]
  25456. "--"
  25457. ["Jump" org-goto t])
  25458. ("Edit Structure"
  25459. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  25460. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  25461. "--"
  25462. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  25463. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  25464. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  25465. "--"
  25466. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  25467. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  25468. ["Demote Heading" org-metaright (not (org-at-table-p))]
  25469. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  25470. "--"
  25471. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  25472. "--"
  25473. ["Convert to odd levels" org-convert-to-odd-levels t]
  25474. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  25475. ("Editing"
  25476. ["Emphasis..." org-emphasize t])
  25477. ("Archive"
  25478. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25479. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25480. ; :active t :keys "C-u C-c C-x C-a"]
  25481. ["Sparse trees open ARCHIVE trees"
  25482. (setq org-sparse-tree-open-archived-trees
  25483. (not org-sparse-tree-open-archived-trees))
  25484. :style toggle :selected org-sparse-tree-open-archived-trees]
  25485. ["Cycling opens ARCHIVE trees"
  25486. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25487. :style toggle :selected org-cycle-open-archived-trees]
  25488. ["Agenda includes ARCHIVE trees"
  25489. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25490. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25491. "--"
  25492. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25493. ; ["Check and Move Children" (org-archive-subtree '(4))
  25494. ; :active t :keys "C-u C-c C-x C-s"]
  25495. )
  25496. "--"
  25497. ("TODO Lists"
  25498. ["TODO/DONE/-" org-todo t]
  25499. ("Select keyword"
  25500. ["Next keyword" org-shiftright (org-on-heading-p)]
  25501. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25502. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25503. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25504. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25505. ["Show TODO Tree" org-show-todo-tree t]
  25506. ["Global TODO list" org-todo-list t]
  25507. "--"
  25508. ["Set Priority" org-priority t]
  25509. ["Priority Up" org-shiftup t]
  25510. ["Priority Down" org-shiftdown t])
  25511. ("TAGS and Properties"
  25512. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25513. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25514. "--"
  25515. ["Set property" 'org-set-property t]
  25516. ["Column view of properties" org-columns t]
  25517. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25518. ("Dates and Scheduling"
  25519. ["Timestamp" org-time-stamp t]
  25520. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25521. ("Change Date"
  25522. ["1 Day Later" org-shiftright t]
  25523. ["1 Day Earlier" org-shiftleft t]
  25524. ["1 ... Later" org-shiftup t]
  25525. ["1 ... Earlier" org-shiftdown t])
  25526. ["Compute Time Range" org-evaluate-time-range t]
  25527. ["Schedule Item" org-schedule t]
  25528. ["Deadline" org-deadline t]
  25529. "--"
  25530. ["Custom time format" org-toggle-time-stamp-overlays
  25531. :style radio :selected org-display-custom-times]
  25532. "--"
  25533. ["Goto Calendar" org-goto-calendar t]
  25534. ["Date from Calendar" org-date-from-calendar t])
  25535. ("Logging work"
  25536. ["Clock in" org-clock-in t]
  25537. ["Clock out" org-clock-out t]
  25538. ["Clock cancel" org-clock-cancel t]
  25539. ["Goto running clock" org-clock-goto t]
  25540. ["Display times" org-clock-display t]
  25541. ["Create clock table" org-clock-report t]
  25542. "--"
  25543. ["Record DONE time"
  25544. (progn (setq org-log-done (not org-log-done))
  25545. (message "Switching to %s will %s record a timestamp"
  25546. (car org-done-keywords)
  25547. (if org-log-done "automatically" "not")))
  25548. :style toggle :selected org-log-done])
  25549. "--"
  25550. ["Agenda Command..." org-agenda t]
  25551. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25552. ("File List for Agenda")
  25553. ("Special views current file"
  25554. ["TODO Tree" org-show-todo-tree t]
  25555. ["Check Deadlines" org-check-deadlines t]
  25556. ["Timeline" org-timeline t]
  25557. ["Tags Tree" org-tags-sparse-tree t])
  25558. "--"
  25559. ("Hyperlinks"
  25560. ["Store Link (Global)" org-store-link t]
  25561. ["Insert Link" org-insert-link t]
  25562. ["Follow Link" org-open-at-point t]
  25563. "--"
  25564. ["Next link" org-next-link t]
  25565. ["Previous link" org-previous-link t]
  25566. "--"
  25567. ["Descriptive Links"
  25568. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25569. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25570. ["Literal Links"
  25571. (progn
  25572. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25573. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25574. "--"
  25575. ["Export/Publish..." org-export t]
  25576. ("LaTeX"
  25577. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25578. :selected org-cdlatex-mode]
  25579. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25580. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25581. ["Modify math symbol" org-cdlatex-math-modify
  25582. (org-inside-LaTeX-fragment-p)]
  25583. ["Export LaTeX fragments as images"
  25584. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25585. :style toggle :selected org-export-with-LaTeX-fragments])
  25586. "--"
  25587. ("Documentation"
  25588. ["Show Version" org-version t]
  25589. ["Info Documentation" org-info t])
  25590. ("Customize"
  25591. ["Browse Org Group" org-customize t]
  25592. "--"
  25593. ["Expand This Menu" org-create-customize-menu
  25594. (fboundp 'customize-menu-create)])
  25595. "--"
  25596. ["Refresh setup" org-mode-restart t]
  25597. ))
  25598. (defun org-info (&optional node)
  25599. "Read documentation for Org-mode in the info system.
  25600. With optional NODE, go directly to that node."
  25601. (interactive)
  25602. (require 'info)
  25603. (info (format "(org)%s" (or node ""))))
  25604. (defun org-install-agenda-files-menu ()
  25605. (let ((bl (buffer-list)))
  25606. (save-excursion
  25607. (while bl
  25608. (set-buffer (pop bl))
  25609. (if (org-mode-p) (setq bl nil)))
  25610. (when (org-mode-p)
  25611. (easy-menu-change
  25612. '("Org") "File List for Agenda"
  25613. (append
  25614. (list
  25615. ["Edit File List" (org-edit-agenda-file-list) t]
  25616. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25617. ["Remove Current File from List" org-remove-file t]
  25618. ["Cycle through agenda files" org-cycle-agenda-files t]
  25619. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25620. "--")
  25621. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25622. ;;;; Documentation
  25623. (defun org-customize ()
  25624. "Call the customize function with org as argument."
  25625. (interactive)
  25626. (customize-browse 'org))
  25627. (defun org-create-customize-menu ()
  25628. "Create a full customization menu for Org-mode, insert it into the menu."
  25629. (interactive)
  25630. (if (fboundp 'customize-menu-create)
  25631. (progn
  25632. (easy-menu-change
  25633. '("Org") "Customize"
  25634. `(["Browse Org group" org-customize t]
  25635. "--"
  25636. ,(customize-menu-create 'org)
  25637. ["Set" Custom-set t]
  25638. ["Save" Custom-save t]
  25639. ["Reset to Current" Custom-reset-current t]
  25640. ["Reset to Saved" Custom-reset-saved t]
  25641. ["Reset to Standard Settings" Custom-reset-standard t]))
  25642. (message "\"Org\"-menu now contains full customization menu"))
  25643. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25644. ;;;; Miscellaneous stuff
  25645. ;;; Generally useful functions
  25646. (defun org-context ()
  25647. "Return a list of contexts of the current cursor position.
  25648. If several contexts apply, all are returned.
  25649. Each context entry is a list with a symbol naming the context, and
  25650. two positions indicating start and end of the context. Possible
  25651. contexts are:
  25652. :headline anywhere in a headline
  25653. :headline-stars on the leading stars in a headline
  25654. :todo-keyword on a TODO keyword (including DONE) in a headline
  25655. :tags on the TAGS in a headline
  25656. :priority on the priority cookie in a headline
  25657. :item on the first line of a plain list item
  25658. :item-bullet on the bullet/number of a plain list item
  25659. :checkbox on the checkbox in a plain list item
  25660. :table in an org-mode table
  25661. :table-special on a special filed in a table
  25662. :table-table in a table.el table
  25663. :link on a hyperlink
  25664. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25665. :target on a <<target>>
  25666. :radio-target on a <<<radio-target>>>
  25667. :latex-fragment on a LaTeX fragment
  25668. :latex-preview on a LaTeX fragment with overlayed preview image
  25669. This function expects the position to be visible because it uses font-lock
  25670. faces as a help to recognize the following contexts: :table-special, :link,
  25671. and :keyword."
  25672. (let* ((f (get-text-property (point) 'face))
  25673. (faces (if (listp f) f (list f)))
  25674. (p (point)) clist o)
  25675. ;; First the large context
  25676. (cond
  25677. ((org-on-heading-p t)
  25678. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25679. (when (progn
  25680. (beginning-of-line 1)
  25681. (looking-at org-todo-line-tags-regexp))
  25682. (push (org-point-in-group p 1 :headline-stars) clist)
  25683. (push (org-point-in-group p 2 :todo-keyword) clist)
  25684. (push (org-point-in-group p 4 :tags) clist))
  25685. (goto-char p)
  25686. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25687. (if (looking-at "\\[#[A-Z0-9]\\]")
  25688. (push (org-point-in-group p 0 :priority) clist)))
  25689. ((org-at-item-p)
  25690. (push (org-point-in-group p 2 :item-bullet) clist)
  25691. (push (list :item (point-at-bol)
  25692. (save-excursion (org-end-of-item) (point)))
  25693. clist)
  25694. (and (org-at-item-checkbox-p)
  25695. (push (org-point-in-group p 0 :checkbox) clist)))
  25696. ((org-at-table-p)
  25697. (push (list :table (org-table-begin) (org-table-end)) clist)
  25698. (if (memq 'org-formula faces)
  25699. (push (list :table-special
  25700. (previous-single-property-change p 'face)
  25701. (next-single-property-change p 'face)) clist)))
  25702. ((org-at-table-p 'any)
  25703. (push (list :table-table) clist)))
  25704. (goto-char p)
  25705. ;; Now the small context
  25706. (cond
  25707. ((org-at-timestamp-p)
  25708. (push (org-point-in-group p 0 :timestamp) clist))
  25709. ((memq 'org-link faces)
  25710. (push (list :link
  25711. (previous-single-property-change p 'face)
  25712. (next-single-property-change p 'face)) clist))
  25713. ((memq 'org-special-keyword faces)
  25714. (push (list :keyword
  25715. (previous-single-property-change p 'face)
  25716. (next-single-property-change p 'face)) clist))
  25717. ((org-on-target-p)
  25718. (push (org-point-in-group p 0 :target) clist)
  25719. (goto-char (1- (match-beginning 0)))
  25720. (if (looking-at org-radio-target-regexp)
  25721. (push (org-point-in-group p 0 :radio-target) clist))
  25722. (goto-char p))
  25723. ((setq o (car (delq nil
  25724. (mapcar
  25725. (lambda (x)
  25726. (if (memq x org-latex-fragment-image-overlays) x))
  25727. (org-overlays-at (point))))))
  25728. (push (list :latex-fragment
  25729. (org-overlay-start o) (org-overlay-end o)) clist)
  25730. (push (list :latex-preview
  25731. (org-overlay-start o) (org-overlay-end o)) clist))
  25732. ((org-inside-LaTeX-fragment-p)
  25733. ;; FIXME: positions wrong.
  25734. (push (list :latex-fragment (point) (point)) clist)))
  25735. (setq clist (nreverse (delq nil clist)))
  25736. clist))
  25737. ;; FIXME: Compare with at-regexp-p Do we need both?
  25738. (defun org-in-regexp (re &optional nlines visually)
  25739. "Check if point is inside a match of regexp.
  25740. Normally only the current line is checked, but you can include NLINES extra
  25741. lines both before and after point into the search.
  25742. If VISUALLY is set, require that the cursor is not after the match but
  25743. really on, so that the block visually is on the match."
  25744. (catch 'exit
  25745. (let ((pos (point))
  25746. (eol (point-at-eol (+ 1 (or nlines 0))))
  25747. (inc (if visually 1 0)))
  25748. (save-excursion
  25749. (beginning-of-line (- 1 (or nlines 0)))
  25750. (while (re-search-forward re eol t)
  25751. (if (and (<= (match-beginning 0) pos)
  25752. (>= (+ inc (match-end 0)) pos))
  25753. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25754. (defun org-at-regexp-p (regexp)
  25755. "Is point inside a match of REGEXP in the current line?"
  25756. (catch 'exit
  25757. (save-excursion
  25758. (let ((pos (point)) (end (point-at-eol)))
  25759. (beginning-of-line 1)
  25760. (while (re-search-forward regexp end t)
  25761. (if (and (<= (match-beginning 0) pos)
  25762. (>= (match-end 0) pos))
  25763. (throw 'exit t)))
  25764. nil))))
  25765. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25766. "Call `multi-occur' with buffers for all agenda files."
  25767. (interactive "sOrg-files matching: \np")
  25768. (let* ((files (org-agenda-files))
  25769. (tnames (mapcar 'file-truename files))
  25770. (extra org-agenda-text-search-extra-files)
  25771. f)
  25772. (while (setq f (pop extra))
  25773. (unless (member (file-truename f) tnames)
  25774. (add-to-list 'files f 'append)
  25775. (add-to-list 'tnames (file-truename f) 'append)))
  25776. (multi-occur
  25777. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25778. regexp)))
  25779. (if (boundp 'occur-mode-find-occurrence-hook)
  25780. ;; Emacs 23
  25781. (add-hook 'occur-mode-find-occurrence-hook
  25782. (lambda ()
  25783. (when (org-mode-p)
  25784. (org-reveal))))
  25785. ;; Emacs 22
  25786. (defadvice occur-mode-goto-occurrence
  25787. (after org-occur-reveal activate)
  25788. (and (org-mode-p) (org-reveal)))
  25789. (defadvice occur-mode-goto-occurrence-other-window
  25790. (after org-occur-reveal activate)
  25791. (and (org-mode-p) (org-reveal)))
  25792. (defadvice occur-mode-display-occurrence
  25793. (after org-occur-reveal activate)
  25794. (when (org-mode-p)
  25795. (let ((pos (occur-mode-find-occurrence)))
  25796. (with-current-buffer (marker-buffer pos)
  25797. (save-excursion
  25798. (goto-char pos)
  25799. (org-reveal)))))))
  25800. (defun org-uniquify (list)
  25801. "Remove duplicate elements from LIST."
  25802. (let (res)
  25803. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25804. res))
  25805. (defun org-delete-all (elts list)
  25806. "Remove all elements in ELTS from LIST."
  25807. (while elts
  25808. (setq list (delete (pop elts) list)))
  25809. list)
  25810. (defun org-back-over-empty-lines ()
  25811. "Move backwards over witespace, to the beginning of the first empty line.
  25812. Returns the number o empty lines passed."
  25813. (let ((pos (point)))
  25814. (skip-chars-backward " \t\n\r")
  25815. (beginning-of-line 2)
  25816. (goto-char (min (point) pos))
  25817. (count-lines (point) pos)))
  25818. (defun org-skip-whitespace ()
  25819. (skip-chars-forward " \t\n\r"))
  25820. (defun org-point-in-group (point group &optional context)
  25821. "Check if POINT is in match-group GROUP.
  25822. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25823. match. If the match group does ot exist or point is not inside it,
  25824. return nil."
  25825. (and (match-beginning group)
  25826. (>= point (match-beginning group))
  25827. (<= point (match-end group))
  25828. (if context
  25829. (list context (match-beginning group) (match-end group))
  25830. t)))
  25831. (defun org-switch-to-buffer-other-window (&rest args)
  25832. "Switch to buffer in a second window on the current frame.
  25833. In particular, do not allow pop-up frames."
  25834. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25835. special-display-function)
  25836. (apply 'switch-to-buffer-other-window args)))
  25837. (defun org-combine-plists (&rest plists)
  25838. "Create a single property list from all plists in PLISTS.
  25839. The process starts by copying the first list, and then setting properties
  25840. from the other lists. Settings in the last list are the most significant
  25841. ones and overrule settings in the other lists."
  25842. (let ((rtn (copy-sequence (pop plists)))
  25843. p v ls)
  25844. (while plists
  25845. (setq ls (pop plists))
  25846. (while ls
  25847. (setq p (pop ls) v (pop ls))
  25848. (setq rtn (plist-put rtn p v))))
  25849. rtn))
  25850. (defun org-move-line-down (arg)
  25851. "Move the current line down. With prefix argument, move it past ARG lines."
  25852. (interactive "p")
  25853. (let ((col (current-column))
  25854. beg end pos)
  25855. (beginning-of-line 1) (setq beg (point))
  25856. (beginning-of-line 2) (setq end (point))
  25857. (beginning-of-line (+ 1 arg))
  25858. (setq pos (move-marker (make-marker) (point)))
  25859. (insert (delete-and-extract-region beg end))
  25860. (goto-char pos)
  25861. (move-to-column col)))
  25862. (defun org-move-line-up (arg)
  25863. "Move the current line up. With prefix argument, move it past ARG lines."
  25864. (interactive "p")
  25865. (let ((col (current-column))
  25866. beg end pos)
  25867. (beginning-of-line 1) (setq beg (point))
  25868. (beginning-of-line 2) (setq end (point))
  25869. (beginning-of-line (- arg))
  25870. (setq pos (move-marker (make-marker) (point)))
  25871. (insert (delete-and-extract-region beg end))
  25872. (goto-char pos)
  25873. (move-to-column col)))
  25874. (defun org-replace-escapes (string table)
  25875. "Replace %-escapes in STRING with values in TABLE.
  25876. TABLE is an association list with keys like \"%a\" and string values.
  25877. The sequences in STRING may contain normal field width and padding information,
  25878. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25879. so values can contain further %-escapes if they are define later in TABLE."
  25880. (let ((case-fold-search nil)
  25881. e re rpl)
  25882. (while (setq e (pop table))
  25883. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25884. (while (string-match re string)
  25885. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25886. (cdr e)))
  25887. (setq string (replace-match rpl t t string))))
  25888. string))
  25889. (defun org-sublist (list start end)
  25890. "Return a section of LIST, from START to END.
  25891. Counting starts at 1."
  25892. (let (rtn (c start))
  25893. (setq list (nthcdr (1- start) list))
  25894. (while (and list (<= c end))
  25895. (push (pop list) rtn)
  25896. (setq c (1+ c)))
  25897. (nreverse rtn)))
  25898. (defun org-find-base-buffer-visiting (file)
  25899. "Like `find-buffer-visiting' but alway return the base buffer and
  25900. not an indirect buffer"
  25901. (let ((buf (find-buffer-visiting file)))
  25902. (if buf
  25903. (or (buffer-base-buffer buf) buf)
  25904. nil)))
  25905. (defun org-image-file-name-regexp ()
  25906. "Return regexp matching the file names of images."
  25907. (if (fboundp 'image-file-name-regexp)
  25908. (image-file-name-regexp)
  25909. (let ((image-file-name-extensions
  25910. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25911. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25912. (concat "\\."
  25913. (regexp-opt (nconc (mapcar 'upcase
  25914. image-file-name-extensions)
  25915. image-file-name-extensions)
  25916. t)
  25917. "\\'"))))
  25918. (defun org-file-image-p (file)
  25919. "Return non-nil if FILE is an image."
  25920. (save-match-data
  25921. (string-match (org-image-file-name-regexp) file)))
  25922. ;;; Paragraph filling stuff.
  25923. ;; We want this to be just right, so use the full arsenal.
  25924. (defun org-indent-line-function ()
  25925. "Indent line like previous, but further if previous was headline or item."
  25926. (interactive)
  25927. (let* ((pos (point))
  25928. (itemp (org-at-item-p))
  25929. column bpos bcol tpos tcol bullet btype bullet-type)
  25930. ;; Find the previous relevant line
  25931. (beginning-of-line 1)
  25932. (cond
  25933. ((looking-at "#") (setq column 0))
  25934. ((looking-at "\\*+ ") (setq column 0))
  25935. (t
  25936. (beginning-of-line 0)
  25937. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25938. (beginning-of-line 0))
  25939. (cond
  25940. ((looking-at "\\*+[ \t]+")
  25941. (goto-char (match-end 0))
  25942. (setq column (current-column)))
  25943. ((org-in-item-p)
  25944. (org-beginning-of-item)
  25945. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25946. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  25947. (setq bpos (match-beginning 1) tpos (match-end 0)
  25948. bcol (progn (goto-char bpos) (current-column))
  25949. tcol (progn (goto-char tpos) (current-column))
  25950. bullet (match-string 1)
  25951. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  25952. (if (not itemp)
  25953. (setq column tcol)
  25954. (goto-char pos)
  25955. (beginning-of-line 1)
  25956. (if (looking-at "\\S-")
  25957. (progn
  25958. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25959. (setq bullet (match-string 1)
  25960. btype (if (string-match "[0-9]" bullet) "n" bullet))
  25961. (setq column (if (equal btype bullet-type) bcol tcol)))
  25962. (setq column (org-get-indentation)))))
  25963. (t (setq column (org-get-indentation))))))
  25964. (goto-char pos)
  25965. (if (<= (current-column) (current-indentation))
  25966. (indent-line-to column)
  25967. (save-excursion (indent-line-to column)))
  25968. (setq column (current-column))
  25969. (beginning-of-line 1)
  25970. (if (looking-at
  25971. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  25972. (replace-match (concat "\\1" (format org-property-format
  25973. (match-string 2) (match-string 3)))
  25974. t nil))
  25975. (move-to-column column)))
  25976. (defun org-set-autofill-regexps ()
  25977. (interactive)
  25978. ;; In the paragraph separator we include headlines, because filling
  25979. ;; text in a line directly attached to a headline would otherwise
  25980. ;; fill the headline as well.
  25981. (org-set-local 'comment-start-skip "^#+[ \t]*")
  25982. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  25983. ;; The paragraph starter includes hand-formatted lists.
  25984. (org-set-local 'paragraph-start
  25985. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  25986. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  25987. ;; But only if the user has not turned off tables or fixed-width regions
  25988. (org-set-local
  25989. 'auto-fill-inhibit-regexp
  25990. (concat "\\*+ \\|#\\+"
  25991. "\\|[ \t]*" org-keyword-time-regexp
  25992. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  25993. (concat
  25994. "\\|[ \t]*["
  25995. (if org-enable-table-editor "|" "")
  25996. (if org-enable-fixed-width-editor ":" "")
  25997. "]"))))
  25998. ;; We use our own fill-paragraph function, to make sure that tables
  25999. ;; and fixed-width regions are not wrapped. That function will pass
  26000. ;; through to `fill-paragraph' when appropriate.
  26001. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  26002. ; Adaptive filling: To get full control, first make sure that
  26003. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  26004. (org-set-local 'adaptive-fill-regexp "\000")
  26005. (org-set-local 'adaptive-fill-function
  26006. 'org-adaptive-fill-function)
  26007. (org-set-local
  26008. 'align-mode-rules-list
  26009. '((org-in-buffer-settings
  26010. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  26011. (modes . '(org-mode))))))
  26012. (defun org-fill-paragraph (&optional justify)
  26013. "Re-align a table, pass through to fill-paragraph if no table."
  26014. (let ((table-p (org-at-table-p))
  26015. (table.el-p (org-at-table.el-p)))
  26016. (cond ((and (equal (char-after (point-at-bol)) ?*)
  26017. (save-excursion (goto-char (point-at-bol))
  26018. (looking-at outline-regexp)))
  26019. t) ; skip headlines
  26020. (table.el-p t) ; skip table.el tables
  26021. (table-p (org-table-align) t) ; align org-mode tables
  26022. (t nil)))) ; call paragraph-fill
  26023. ;; For reference, this is the default value of adaptive-fill-regexp
  26024. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  26025. (defun org-adaptive-fill-function ()
  26026. "Return a fill prefix for org-mode files.
  26027. In particular, this makes sure hanging paragraphs for hand-formatted lists
  26028. work correctly."
  26029. (cond ((looking-at "#[ \t]+")
  26030. (match-string 0))
  26031. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  26032. (save-excursion
  26033. (goto-char (match-end 0))
  26034. (make-string (current-column) ?\ )))
  26035. (t nil)))
  26036. ;;;; Functions extending outline functionality
  26037. (defun org-beginning-of-line (&optional arg)
  26038. "Go to the beginning of the current line. If that is invisible, continue
  26039. to a visible line beginning. This makes the function of C-a more intuitive.
  26040. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  26041. first attempt, and only move to after the tags when the cursor is already
  26042. beyond the end of the headline."
  26043. (interactive "P")
  26044. (let ((pos (point)))
  26045. (beginning-of-line 1)
  26046. (if (bobp)
  26047. nil
  26048. (backward-char 1)
  26049. (if (org-invisible-p)
  26050. (while (and (not (bobp)) (org-invisible-p))
  26051. (backward-char 1)
  26052. (beginning-of-line 1))
  26053. (forward-char 1)))
  26054. (when org-special-ctrl-a/e
  26055. (cond
  26056. ((and (looking-at org-todo-line-regexp)
  26057. (= (char-after (match-end 1)) ?\ ))
  26058. (goto-char
  26059. (if (eq org-special-ctrl-a/e t)
  26060. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  26061. ((= pos (point)) (match-beginning 3))
  26062. (t (point)))
  26063. (cond ((> pos (point)) (point))
  26064. ((not (eq last-command this-command)) (point))
  26065. (t (match-beginning 3))))))
  26066. ((org-at-item-p)
  26067. (goto-char
  26068. (if (eq org-special-ctrl-a/e t)
  26069. (cond ((> pos (match-end 4)) (match-end 4))
  26070. ((= pos (point)) (match-end 4))
  26071. (t (point)))
  26072. (cond ((> pos (point)) (point))
  26073. ((not (eq last-command this-command)) (point))
  26074. (t (match-end 4))))))))))
  26075. (defun org-end-of-line (&optional arg)
  26076. "Go to the end of the line.
  26077. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  26078. first attempt, and only move to after the tags when the cursor is already
  26079. beyond the end of the headline."
  26080. (interactive "P")
  26081. (if (or (not org-special-ctrl-a/e)
  26082. (not (org-on-heading-p)))
  26083. (end-of-line arg)
  26084. (let ((pos (point)))
  26085. (beginning-of-line 1)
  26086. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  26087. (if (eq org-special-ctrl-a/e t)
  26088. (if (or (< pos (match-beginning 1))
  26089. (= pos (match-end 0)))
  26090. (goto-char (match-beginning 1))
  26091. (goto-char (match-end 0)))
  26092. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  26093. (goto-char (match-end 0))
  26094. (goto-char (match-beginning 1))))
  26095. (end-of-line arg)))))
  26096. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  26097. (define-key org-mode-map "\C-e" 'org-end-of-line)
  26098. (defun org-kill-line (&optional arg)
  26099. "Kill line, to tags or end of line."
  26100. (interactive "P")
  26101. (cond
  26102. ((or (not org-special-ctrl-k)
  26103. (bolp)
  26104. (not (org-on-heading-p)))
  26105. (call-interactively 'kill-line))
  26106. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  26107. (kill-region (point) (match-beginning 1))
  26108. (org-set-tags nil t))
  26109. (t (kill-region (point) (point-at-eol)))))
  26110. (define-key org-mode-map "\C-k" 'org-kill-line)
  26111. (defun org-invisible-p ()
  26112. "Check if point is at a character currently not visible."
  26113. ;; Early versions of noutline don't have `outline-invisible-p'.
  26114. (if (fboundp 'outline-invisible-p)
  26115. (outline-invisible-p)
  26116. (get-char-property (point) 'invisible)))
  26117. (defun org-invisible-p2 ()
  26118. "Check if point is at a character currently not visible."
  26119. (save-excursion
  26120. (if (and (eolp) (not (bobp))) (backward-char 1))
  26121. ;; Early versions of noutline don't have `outline-invisible-p'.
  26122. (if (fboundp 'outline-invisible-p)
  26123. (outline-invisible-p)
  26124. (get-char-property (point) 'invisible))))
  26125. (defalias 'org-back-to-heading 'outline-back-to-heading)
  26126. (defalias 'org-on-heading-p 'outline-on-heading-p)
  26127. (defalias 'org-at-heading-p 'outline-on-heading-p)
  26128. (defun org-at-heading-or-item-p ()
  26129. (or (org-on-heading-p) (org-at-item-p)))
  26130. (defun org-on-target-p ()
  26131. (or (org-in-regexp org-radio-target-regexp)
  26132. (org-in-regexp org-target-regexp)))
  26133. (defun org-up-heading-all (arg)
  26134. "Move to the heading line of which the present line is a subheading.
  26135. This function considers both visible and invisible heading lines.
  26136. With argument, move up ARG levels."
  26137. (if (fboundp 'outline-up-heading-all)
  26138. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  26139. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  26140. (defun org-up-heading-safe ()
  26141. "Move to the heading line of which the present line is a subheading.
  26142. This version will not throw an error. It will return the level of the
  26143. headline found, or nil if no higher level is found."
  26144. (let ((pos (point)) start-level level
  26145. (re (concat "^" outline-regexp)))
  26146. (catch 'exit
  26147. (outline-back-to-heading t)
  26148. (setq start-level (funcall outline-level))
  26149. (if (equal start-level 1) (throw 'exit nil))
  26150. (while (re-search-backward re nil t)
  26151. (setq level (funcall outline-level))
  26152. (if (< level start-level) (throw 'exit level)))
  26153. nil)))
  26154. (defun org-first-sibling-p ()
  26155. "Is this heading the first child of its parents?"
  26156. (interactive)
  26157. (let ((re (concat "^" outline-regexp))
  26158. level l)
  26159. (unless (org-at-heading-p t)
  26160. (error "Not at a heading"))
  26161. (setq level (funcall outline-level))
  26162. (save-excursion
  26163. (if (not (re-search-backward re nil t))
  26164. t
  26165. (setq l (funcall outline-level))
  26166. (< l level)))))
  26167. (defun org-goto-sibling (&optional previous)
  26168. "Goto the next sibling, even if it is invisible.
  26169. When PREVIOUS is set, go to the previous sibling instead. Returns t
  26170. when a sibling was found. When none is found, return nil and don't
  26171. move point."
  26172. (let ((fun (if previous 're-search-backward 're-search-forward))
  26173. (pos (point))
  26174. (re (concat "^" outline-regexp))
  26175. level l)
  26176. (when (condition-case nil (org-back-to-heading t) (error nil))
  26177. (setq level (funcall outline-level))
  26178. (catch 'exit
  26179. (or previous (forward-char 1))
  26180. (while (funcall fun re nil t)
  26181. (setq l (funcall outline-level))
  26182. (when (< l level) (goto-char pos) (throw 'exit nil))
  26183. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  26184. (goto-char pos)
  26185. nil))))
  26186. (defun org-show-siblings ()
  26187. "Show all siblings of the current headline."
  26188. (save-excursion
  26189. (while (org-goto-sibling) (org-flag-heading nil)))
  26190. (save-excursion
  26191. (while (org-goto-sibling 'previous)
  26192. (org-flag-heading nil))))
  26193. (defun org-show-hidden-entry ()
  26194. "Show an entry where even the heading is hidden."
  26195. (save-excursion
  26196. (org-show-entry)))
  26197. (defun org-flag-heading (flag &optional entry)
  26198. "Flag the current heading. FLAG non-nil means make invisible.
  26199. When ENTRY is non-nil, show the entire entry."
  26200. (save-excursion
  26201. (org-back-to-heading t)
  26202. ;; Check if we should show the entire entry
  26203. (if entry
  26204. (progn
  26205. (org-show-entry)
  26206. (save-excursion
  26207. (and (outline-next-heading)
  26208. (org-flag-heading nil))))
  26209. (outline-flag-region (max (point-min) (1- (point)))
  26210. (save-excursion (outline-end-of-heading) (point))
  26211. flag))))
  26212. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  26213. ;; This is an exact copy of the original function, but it uses
  26214. ;; `org-back-to-heading', to make it work also in invisible
  26215. ;; trees. And is uses an invisible-OK argument.
  26216. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  26217. (org-back-to-heading invisible-OK)
  26218. (let ((first t)
  26219. (level (funcall outline-level)))
  26220. (while (and (not (eobp))
  26221. (or first (> (funcall outline-level) level)))
  26222. (setq first nil)
  26223. (outline-next-heading))
  26224. (unless to-heading
  26225. (if (memq (preceding-char) '(?\n ?\^M))
  26226. (progn
  26227. ;; Go to end of line before heading
  26228. (forward-char -1)
  26229. (if (memq (preceding-char) '(?\n ?\^M))
  26230. ;; leave blank line before heading
  26231. (forward-char -1))))))
  26232. (point))
  26233. (defun org-show-subtree ()
  26234. "Show everything after this heading at deeper levels."
  26235. (outline-flag-region
  26236. (point)
  26237. (save-excursion
  26238. (outline-end-of-subtree) (outline-next-heading) (point))
  26239. nil))
  26240. (defun org-show-entry ()
  26241. "Show the body directly following this heading.
  26242. Show the heading too, if it is currently invisible."
  26243. (interactive)
  26244. (save-excursion
  26245. (condition-case nil
  26246. (progn
  26247. (org-back-to-heading t)
  26248. (outline-flag-region
  26249. (max (point-min) (1- (point)))
  26250. (save-excursion
  26251. (re-search-forward
  26252. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  26253. (or (match-beginning 1) (point-max)))
  26254. nil))
  26255. (error nil))))
  26256. (defun org-make-options-regexp (kwds)
  26257. "Make a regular expression for keyword lines."
  26258. (concat
  26259. "^"
  26260. "#?[ \t]*\\+\\("
  26261. (mapconcat 'regexp-quote kwds "\\|")
  26262. "\\):[ \t]*"
  26263. "\\(.+\\)"))
  26264. ;; Make isearch reveal the necessary context
  26265. (defun org-isearch-end ()
  26266. "Reveal context after isearch exits."
  26267. (when isearch-success ; only if search was successful
  26268. (if (featurep 'xemacs)
  26269. ;; Under XEmacs, the hook is run in the correct place,
  26270. ;; we directly show the context.
  26271. (org-show-context 'isearch)
  26272. ;; In Emacs the hook runs *before* restoring the overlays.
  26273. ;; So we have to use a one-time post-command-hook to do this.
  26274. ;; (Emacs 22 has a special variable, see function `org-mode')
  26275. (unless (and (boundp 'isearch-mode-end-hook-quit)
  26276. isearch-mode-end-hook-quit)
  26277. ;; Only when the isearch was not quitted.
  26278. (org-add-hook 'post-command-hook 'org-isearch-post-command
  26279. 'append 'local)))))
  26280. (defun org-isearch-post-command ()
  26281. "Remove self from hook, and show context."
  26282. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  26283. (org-show-context 'isearch))
  26284. ;;;; Integration with and fixes for other packages
  26285. ;;; Imenu support
  26286. (defvar org-imenu-markers nil
  26287. "All markers currently used by Imenu.")
  26288. (make-variable-buffer-local 'org-imenu-markers)
  26289. (defun org-imenu-new-marker (&optional pos)
  26290. "Return a new marker for use by Imenu, and remember the marker."
  26291. (let ((m (make-marker)))
  26292. (move-marker m (or pos (point)))
  26293. (push m org-imenu-markers)
  26294. m))
  26295. (defun org-imenu-get-tree ()
  26296. "Produce the index for Imenu."
  26297. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  26298. (setq org-imenu-markers nil)
  26299. (let* ((n org-imenu-depth)
  26300. (re (concat "^" outline-regexp))
  26301. (subs (make-vector (1+ n) nil))
  26302. (last-level 0)
  26303. m tree level head)
  26304. (save-excursion
  26305. (save-restriction
  26306. (widen)
  26307. (goto-char (point-max))
  26308. (while (re-search-backward re nil t)
  26309. (setq level (org-reduced-level (funcall outline-level)))
  26310. (when (<= level n)
  26311. (looking-at org-complex-heading-regexp)
  26312. (setq head (org-match-string-no-properties 4)
  26313. m (org-imenu-new-marker))
  26314. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  26315. (if (>= level last-level)
  26316. (push (cons head m) (aref subs level))
  26317. (push (cons head (aref subs (1+ level))) (aref subs level))
  26318. (loop for i from (1+ level) to n do (aset subs i nil)))
  26319. (setq last-level level)))))
  26320. (aref subs 1)))
  26321. (eval-after-load "imenu"
  26322. '(progn
  26323. (add-hook 'imenu-after-jump-hook
  26324. (lambda () (org-show-context 'org-goto)))))
  26325. ;; Speedbar support
  26326. (defun org-speedbar-set-agenda-restriction ()
  26327. "Restrict future agenda commands to the location at point in speedbar.
  26328. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  26329. (interactive)
  26330. (let (p m tp np dir txt w)
  26331. (cond
  26332. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26333. 'org-imenu t))
  26334. (setq m (get-text-property p 'org-imenu-marker))
  26335. (save-excursion
  26336. (save-restriction
  26337. (set-buffer (marker-buffer m))
  26338. (goto-char m)
  26339. (org-agenda-set-restriction-lock 'subtree))))
  26340. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  26341. 'speedbar-function 'speedbar-find-file))
  26342. (setq tp (previous-single-property-change
  26343. (1+ p) 'speedbar-function)
  26344. np (next-single-property-change
  26345. tp 'speedbar-function)
  26346. dir (speedbar-line-directory)
  26347. txt (buffer-substring-no-properties (or tp (point-min))
  26348. (or np (point-max))))
  26349. (save-excursion
  26350. (save-restriction
  26351. (set-buffer (find-file-noselect
  26352. (let ((default-directory dir))
  26353. (expand-file-name txt))))
  26354. (unless (org-mode-p)
  26355. (error "Cannot restrict to non-Org-mode file"))
  26356. (org-agenda-set-restriction-lock 'file))))
  26357. (t (error "Don't know how to restrict Org-mode's agenda")))
  26358. (org-move-overlay org-speedbar-restriction-lock-overlay
  26359. (point-at-bol) (point-at-eol))
  26360. (setq current-prefix-arg nil)
  26361. (org-agenda-maybe-redo)))
  26362. (eval-after-load "speedbar"
  26363. '(progn
  26364. (speedbar-add-supported-extension ".org")
  26365. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  26366. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  26367. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  26368. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  26369. (add-hook 'speedbar-visiting-tag-hook
  26370. (lambda () (org-show-context 'org-goto)))))
  26371. ;;; Fixes and Hacks
  26372. ;; Make flyspell not check words in links, to not mess up our keymap
  26373. (defun org-mode-flyspell-verify ()
  26374. "Don't let flyspell put overlays at active buttons."
  26375. (not (get-text-property (point) 'keymap)))
  26376. ;; Make `bookmark-jump' show the jump location if it was hidden.
  26377. (eval-after-load "bookmark"
  26378. '(if (boundp 'bookmark-after-jump-hook)
  26379. ;; We can use the hook
  26380. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  26381. ;; Hook not available, use advice
  26382. (defadvice bookmark-jump (after org-make-visible activate)
  26383. "Make the position visible."
  26384. (org-bookmark-jump-unhide))))
  26385. (defun org-bookmark-jump-unhide ()
  26386. "Unhide the current position, to show the bookmark location."
  26387. (and (org-mode-p)
  26388. (or (org-invisible-p)
  26389. (save-excursion (goto-char (max (point-min) (1- (point))))
  26390. (org-invisible-p)))
  26391. (org-show-context 'bookmark-jump)))
  26392. ;; Fix a bug in htmlize where there are text properties (face nil)
  26393. (eval-after-load "htmlize"
  26394. '(progn
  26395. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  26396. "Make sure there are no nil faces"
  26397. (setq ad-return-value (delq nil ad-return-value)))))
  26398. ;; Make session.el ignore our circular variable
  26399. (eval-after-load "session"
  26400. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  26401. ;;;; Experimental code
  26402. (defun org-closed-in-range ()
  26403. "Sparse tree of items closed in a certain time range.
  26404. Still experimental, may disappear in the future."
  26405. (interactive)
  26406. ;; Get the time interval from the user.
  26407. (let* ((time1 (time-to-seconds
  26408. (org-read-date nil 'to-time nil "Starting date: ")))
  26409. (time2 (time-to-seconds
  26410. (org-read-date nil 'to-time nil "End date:")))
  26411. ;; callback function
  26412. (callback (lambda ()
  26413. (let ((time
  26414. (time-to-seconds
  26415. (apply 'encode-time
  26416. (org-parse-time-string
  26417. (match-string 1))))))
  26418. ;; check if time in interval
  26419. (and (>= time time1) (<= time time2))))))
  26420. ;; make tree, check each match with the callback
  26421. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  26422. ;;;; Finish up
  26423. (provide 'org)
  26424. (run-hooks 'org-load-hook)
  26425. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  26426. ;;; org.el ends here