org.el 1.0 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087
  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.20
  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.20"
  81. "The version number of the file org.el.")
  82. (defun org-version ()
  83. (interactive)
  84. (message "Org-mode version %s" org-version))
  85. ;;; Compatibility constants
  86. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  87. (defconst org-format-transports-properties-p
  88. (let ((x "a"))
  89. (add-text-properties 0 1 '(test t) x)
  90. (get-text-property 0 'test (format "%s" x)))
  91. "Does format transport text properties?")
  92. (defmacro org-bound-and-true-p (var)
  93. "Return the value of symbol VAR if it is bound, else nil."
  94. `(and (boundp (quote ,var)) ,var))
  95. (defmacro org-unmodified (&rest body)
  96. "Execute body without changing `buffer-modified-p'."
  97. `(set-buffer-modified-p
  98. (prog1 (buffer-modified-p) ,@body)))
  99. (defmacro org-re (s)
  100. "Replace posix classes in regular expression."
  101. (if (featurep 'xemacs)
  102. (let ((ss s))
  103. (save-match-data
  104. (while (string-match "\\[:alnum:\\]" ss)
  105. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  106. (while (string-match "\\[:alpha:\\]" ss)
  107. (setq ss (replace-match "a-zA-Z" t t ss)))
  108. ss))
  109. s))
  110. (defmacro org-preserve-lc (&rest body)
  111. `(let ((_line (org-current-line))
  112. (_col (current-column)))
  113. (unwind-protect
  114. (progn ,@body)
  115. (goto-line _line)
  116. (move-to-column _col))))
  117. (defmacro org-without-partial-completion (&rest body)
  118. `(let ((pc-mode (and (boundp 'partial-completion-mode)
  119. partial-completion-mode)))
  120. (unwind-protect
  121. (progn
  122. (if pc-mode (partial-completion-mode -1))
  123. ,@body)
  124. (if pc-mode (partial-completion-mode 1)))))
  125. ;;; The custom variables
  126. (defgroup org nil
  127. "Outline-based notes management and organizer."
  128. :tag "Org"
  129. :group 'outlines
  130. :group 'hypermedia
  131. :group 'calendar)
  132. ;; FIXME: Needs a separate group...
  133. (defcustom org-completion-fallback-command 'hippie-expand
  134. "The expansion command called by \\[org-complete] in normal context.
  135. Normal means, no org-mode-specific context."
  136. :group 'org
  137. :type 'function)
  138. (defgroup org-startup nil
  139. "Options concerning startup of Org-mode."
  140. :tag "Org Startup"
  141. :group 'org)
  142. (defcustom org-startup-folded t
  143. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  144. This can also be configured on a per-file basis by adding one of
  145. the following lines anywhere in the buffer:
  146. #+STARTUP: fold
  147. #+STARTUP: nofold
  148. #+STARTUP: content"
  149. :group 'org-startup
  150. :type '(choice
  151. (const :tag "nofold: show all" nil)
  152. (const :tag "fold: overview" t)
  153. (const :tag "content: all headlines" content)))
  154. (defcustom org-startup-truncated t
  155. "Non-nil means, entering Org-mode will set `truncate-lines'.
  156. This is useful since some lines containing links can be very long and
  157. uninteresting. Also tables look terrible when wrapped."
  158. :group 'org-startup
  159. :type 'boolean)
  160. (defcustom org-startup-align-all-tables nil
  161. "Non-nil means, align all tables when visiting a file.
  162. This is useful when the column width in tables is forced with <N> cookies
  163. in table fields. Such tables will look correct only after the first re-align.
  164. This can also be configured on a per-file basis by adding one of
  165. the following lines anywhere in the buffer:
  166. #+STARTUP: align
  167. #+STARTUP: noalign"
  168. :group 'org-startup
  169. :type 'boolean)
  170. (defcustom org-insert-mode-line-in-empty-file nil
  171. "Non-nil means insert the first line setting Org-mode in empty files.
  172. When the function `org-mode' is called interactively in an empty file, this
  173. normally means that the file name does not automatically trigger Org-mode.
  174. To ensure that the file will always be in Org-mode in the future, a
  175. line enforcing Org-mode will be inserted into the buffer, if this option
  176. has been set."
  177. :group 'org-startup
  178. :type 'boolean)
  179. (defcustom org-replace-disputed-keys nil
  180. "Non-nil means use alternative key bindings for some keys.
  181. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  182. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  183. If you want to use Org-mode together with one of these other modes,
  184. or more generally if you would like to move some Org-mode commands to
  185. other keys, set this variable and configure the keys with the variable
  186. `org-disputed-keys'.
  187. This option is only relevant at load-time of Org-mode, and must be set
  188. *before* org.el is loaded. Changing it requires a restart of Emacs to
  189. become effective."
  190. :group 'org-startup
  191. :type 'boolean)
  192. (if (fboundp 'defvaralias)
  193. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  194. (defcustom org-disputed-keys
  195. '(([(shift up)] . [(meta p)])
  196. ([(shift down)] . [(meta n)])
  197. ([(shift left)] . [(meta -)])
  198. ([(shift right)] . [(meta +)])
  199. ([(control shift right)] . [(meta shift +)])
  200. ([(control shift left)] . [(meta shift -)]))
  201. "Keys for which Org-mode and other modes compete.
  202. This is an alist, cars are the default keys, second element specifies
  203. the alternative to use when `org-replace-disputed-keys' is t.
  204. Keys can be specified in any syntax supported by `define-key'.
  205. The value of this option takes effect only at Org-mode's startup,
  206. therefore you'll have to restart Emacs to apply it after changing."
  207. :group 'org-startup
  208. :type 'alist)
  209. (defun org-key (key)
  210. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  211. Or return the original if not disputed."
  212. (if org-replace-disputed-keys
  213. (let* ((nkey (key-description key))
  214. (x (org-find-if (lambda (x)
  215. (equal (key-description (car x)) nkey))
  216. org-disputed-keys)))
  217. (if x (cdr x) key))
  218. key))
  219. (defun org-find-if (predicate seq)
  220. (catch 'exit
  221. (while seq
  222. (if (funcall predicate (car seq))
  223. (throw 'exit (car seq))
  224. (pop seq)))))
  225. (defun org-defkey (keymap key def)
  226. "Define a key, possibly translated, as returned by `org-key'."
  227. (define-key keymap (org-key key) def))
  228. (defcustom org-ellipsis nil
  229. "The ellipsis to use in the Org-mode outline.
  230. When nil, just use the standard three dots. When a string, use that instead,
  231. When a face, use the standart 3 dots, but with the specified face.
  232. The change affects only Org-mode (which will then use its own display table).
  233. Changing this requires executing `M-x org-mode' in a buffer to become
  234. effective."
  235. :group 'org-startup
  236. :type '(choice (const :tag "Default" nil)
  237. (face :tag "Face" :value org-warning)
  238. (string :tag "String" :value "...#")))
  239. (defvar org-display-table nil
  240. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  241. (defgroup org-keywords nil
  242. "Keywords in Org-mode."
  243. :tag "Org Keywords"
  244. :group 'org)
  245. (defcustom org-deadline-string "DEADLINE:"
  246. "String to mark deadline entries.
  247. A deadline is this string, followed by a time stamp. Should be a word,
  248. terminated by a colon. You can insert a schedule keyword and
  249. a timestamp with \\[org-deadline].
  250. Changes become only effective after restarting Emacs."
  251. :group 'org-keywords
  252. :type 'string)
  253. (defcustom org-scheduled-string "SCHEDULED:"
  254. "String to mark scheduled TODO entries.
  255. A schedule is this string, followed by a time stamp. Should be a word,
  256. terminated by a colon. You can insert a schedule keyword and
  257. a timestamp with \\[org-schedule].
  258. Changes become only effective after restarting Emacs."
  259. :group 'org-keywords
  260. :type 'string)
  261. (defcustom org-closed-string "CLOSED:"
  262. "String used as the prefix for timestamps logging closing a TODO entry."
  263. :group 'org-keywords
  264. :type 'string)
  265. (defcustom org-clock-string "CLOCK:"
  266. "String used as prefix for timestamps clocking work hours on an item."
  267. :group 'org-keywords
  268. :type 'string)
  269. (defcustom org-comment-string "COMMENT"
  270. "Entries starting with this keyword will never be exported.
  271. An entry can be toggled between COMMENT and normal with
  272. \\[org-toggle-comment].
  273. Changes become only effective after restarting Emacs."
  274. :group 'org-keywords
  275. :type 'string)
  276. (defcustom org-quote-string "QUOTE"
  277. "Entries starting with this keyword will be exported in fixed-width font.
  278. Quoting applies only to the text in the entry following the headline, and does
  279. not extend beyond the next headline, even if that is lower level.
  280. An entry can be toggled between QUOTE and normal with
  281. \\[org-toggle-fixed-width-section]."
  282. :group 'org-keywords
  283. :type 'string)
  284. (defconst org-repeat-re
  285. ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
  286. ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
  287. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
  288. "Regular expression for specifying repeated events.
  289. After a match, group 1 contains the repeat expression.")
  290. (defgroup org-structure nil
  291. "Options concerning the general structure of Org-mode files."
  292. :tag "Org Structure"
  293. :group 'org)
  294. (defgroup org-reveal-location nil
  295. "Options about how to make context of a location visible."
  296. :tag "Org Reveal Location"
  297. :group 'org-structure)
  298. (defconst org-context-choice
  299. '(choice
  300. (const :tag "Always" t)
  301. (const :tag "Never" nil)
  302. (repeat :greedy t :tag "Individual contexts"
  303. (cons
  304. (choice :tag "Context"
  305. (const agenda)
  306. (const org-goto)
  307. (const occur-tree)
  308. (const tags-tree)
  309. (const link-search)
  310. (const mark-goto)
  311. (const bookmark-jump)
  312. (const isearch)
  313. (const default))
  314. (boolean))))
  315. "Contexts for the reveal options.")
  316. (defcustom org-show-hierarchy-above '((default . t))
  317. "Non-nil means, show full hierarchy when revealing a location.
  318. Org-mode often shows locations in an org-mode file which might have
  319. been invisible before. When this is set, the hierarchy of headings
  320. above the exposed location is shown.
  321. Turning this off for example for sparse trees makes them very compact.
  322. Instead of t, this can also be an alist specifying this option for different
  323. contexts. Valid contexts are
  324. agenda when exposing an entry from the agenda
  325. org-goto when using the command `org-goto' on key C-c C-j
  326. occur-tree when using the command `org-occur' on key C-c /
  327. tags-tree when constructing a sparse tree based on tags matches
  328. link-search when exposing search matches associated with a link
  329. mark-goto when exposing the jump goal of a mark
  330. bookmark-jump when exposing a bookmark location
  331. isearch when exiting from an incremental search
  332. default default for all contexts not set explicitly"
  333. :group 'org-reveal-location
  334. :type org-context-choice)
  335. (defcustom org-show-following-heading '((default . nil))
  336. "Non-nil means, show following heading when revealing a location.
  337. Org-mode often shows locations in an org-mode file which might have
  338. been invisible before. When this is set, the heading following the
  339. match is shown.
  340. Turning this off for example for sparse trees makes them very compact,
  341. but makes it harder to edit the location of the match. In such a case,
  342. use the command \\[org-reveal] to show more context.
  343. Instead of t, this can also be an alist specifying this option for different
  344. contexts. See `org-show-hierarchy-above' for valid contexts."
  345. :group 'org-reveal-location
  346. :type org-context-choice)
  347. (defcustom org-show-siblings '((default . nil) (isearch t))
  348. "Non-nil means, show all sibling heading when revealing a location.
  349. Org-mode often shows locations in an org-mode file which might have
  350. been invisible before. When this is set, the sibling of the current entry
  351. heading are all made visible. If `org-show-hierarchy-above' is t,
  352. the same happens on each level of the hierarchy above the current entry.
  353. By default this is on for the isearch context, off for all other contexts.
  354. Turning this off for example for sparse trees makes them very compact,
  355. but makes it harder to edit the location of the match. In such a case,
  356. use the command \\[org-reveal] to show more context.
  357. Instead of t, this can also be an alist specifying this option for different
  358. contexts. See `org-show-hierarchy-above' for valid contexts."
  359. :group 'org-reveal-location
  360. :type org-context-choice)
  361. (defcustom org-show-entry-below '((default . nil))
  362. "Non-nil means, show the entry below a headline 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 text below the headline that is
  365. exposed is also shown.
  366. By default this is off for all contexts.
  367. Instead of t, this can also be an alist specifying this option for different
  368. contexts. See `org-show-hierarchy-above' for valid contexts."
  369. :group 'org-reveal-location
  370. :type org-context-choice)
  371. (defgroup org-cycle nil
  372. "Options concerning visibility cycling in Org-mode."
  373. :tag "Org Cycle"
  374. :group 'org-structure)
  375. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  376. "Names of drawers. Drawers are not opened by cycling on the headline above.
  377. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  378. this:
  379. :DRAWERNAME:
  380. .....
  381. :END:
  382. The drawer \"PROPERTIES\" is special for capturing properties through
  383. the property API.
  384. Drawers can be defined on the per-file basis with a line like:
  385. #+DRAWERS: HIDDEN STATE PROPERTIES"
  386. :group 'org-structure
  387. :type '(repeat (string :tag "Drawer Name")))
  388. (defcustom org-cycle-global-at-bob nil
  389. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  390. This makes it possible to do global cycling without having to use S-TAB or
  391. C-u TAB. For this special case to work, the first line of the buffer
  392. must not be a headline - it may be empty ot some other text. When used in
  393. this way, `org-cycle-hook' is disables temporarily, to make sure the
  394. cursor stays at the beginning of the buffer.
  395. When this option is nil, don't do anything special at the beginning
  396. of the buffer."
  397. :group 'org-cycle
  398. :type 'boolean)
  399. (defcustom org-cycle-emulate-tab t
  400. "Where should `org-cycle' emulate TAB.
  401. nil Never
  402. white Only in completely white lines
  403. whitestart Only at the beginning of lines, before the first non-white char
  404. t Everywhere except in headlines
  405. exc-hl-bol Everywhere except at the start of a headline
  406. If TAB is used in a place where it does not emulate TAB, the current subtree
  407. visibility is cycled."
  408. :group 'org-cycle
  409. :type '(choice (const :tag "Never" nil)
  410. (const :tag "Only in completely white lines" white)
  411. (const :tag "Before first char in a line" whitestart)
  412. (const :tag "Everywhere except in headlines" t)
  413. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  414. ))
  415. (defcustom org-cycle-separator-lines 2
  416. "Number of empty lines needed to keep an empty line between collapsed trees.
  417. If you leave an empty line between the end of a subtree and the following
  418. headline, this empty line is hidden when the subtree is folded.
  419. Org-mode will leave (exactly) one empty line visible if the number of
  420. empty lines is equal or larger to the number given in this variable.
  421. So the default 2 means, at least 2 empty lines after the end of a subtree
  422. are needed to produce free space between a collapsed subtree and the
  423. following headline.
  424. Special case: when 0, never leave empty lines in collapsed view."
  425. :group 'org-cycle
  426. :type 'integer)
  427. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  428. org-cycle-hide-drawers
  429. org-cycle-show-empty-lines
  430. org-optimize-window-after-visibility-change)
  431. "Hook that is run after `org-cycle' has changed the buffer visibility.
  432. The function(s) in this hook must accept a single argument which indicates
  433. the new state that was set by the most recent `org-cycle' command. The
  434. argument is a symbol. After a global state change, it can have the values
  435. `overview', `content', or `all'. After a local state change, it can have
  436. the values `folded', `children', or `subtree'."
  437. :group 'org-cycle
  438. :type 'hook)
  439. (defgroup org-edit-structure nil
  440. "Options concerning structure editing in Org-mode."
  441. :tag "Org Edit Structure"
  442. :group 'org-structure)
  443. (defcustom org-special-ctrl-a/e nil
  444. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  445. When t, `C-a' will bring back the cursor to the beginning of the
  446. headline text, i.e. after the stars and after a possible TODO keyword.
  447. In an item, this will be the position after the bullet.
  448. When the cursor is already at that position, another `C-a' will bring
  449. it to the beginning of the line.
  450. `C-e' will jump to the end of the headline, ignoring the presence of tags
  451. in the headline. A second `C-e' will then jump to the true end of the
  452. line, after any tags.
  453. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  454. and only a directly following, identical keypress will bring the cursor
  455. to the special positions."
  456. :group 'org-edit-structure
  457. :type '(choice
  458. (const :tag "off" nil)
  459. (const :tag "after bullet first" t)
  460. (const :tag "border first" reversed)))
  461. (if (fboundp 'defvaralias)
  462. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  463. (defcustom org-special-ctrl-k nil
  464. "Non-nil means `C-k' will behave specially in headlines.
  465. When nil, `C-k' will call the default `kill-line' command.
  466. When t, the following will happen while the cursor is in the headline:
  467. - When the cursor is at the beginning of a headline, kill the entire
  468. line and possible the folded subtree below the line.
  469. - When in the middle of the headline text, kill the headline up to the tags.
  470. - When after the headline text, kill the tags."
  471. :group 'org-edit-structure
  472. :type 'boolean)
  473. (defcustom org-odd-levels-only nil
  474. "Non-nil means, skip even levels and only use odd levels for the outline.
  475. This has the effect that two stars are being added/taken away in
  476. promotion/demotion commands. It also influences how levels are
  477. handled by the exporters.
  478. Changing it requires restart of `font-lock-mode' to become effective
  479. for fontification also in regions already fontified.
  480. You may also set this on a per-file basis by adding one of the following
  481. lines to the buffer:
  482. #+STARTUP: odd
  483. #+STARTUP: oddeven"
  484. :group 'org-edit-structure
  485. :group 'org-font-lock
  486. :type 'boolean)
  487. (defcustom org-adapt-indentation t
  488. "Non-nil means, adapt indentation when promoting and demoting.
  489. When this is set and the *entire* text in an entry is indented, the
  490. indentation is increased by one space in a demotion command, and
  491. decreased by one in a promotion command. If any line in the entry
  492. body starts at column 0, indentation is not changed at all."
  493. :group 'org-edit-structure
  494. :type 'boolean)
  495. (defcustom org-blank-before-new-entry '((heading . nil)
  496. (plain-list-item . nil))
  497. "Should `org-insert-heading' leave a blank line before new heading/item?
  498. The value is an alist, with `heading' and `plain-list-item' as car,
  499. and a boolean flag as cdr."
  500. :group 'org-edit-structure
  501. :type '(list
  502. (cons (const heading) (boolean))
  503. (cons (const plain-list-item) (boolean))))
  504. (defcustom org-insert-heading-hook nil
  505. "Hook being run after inserting a new heading."
  506. :group 'org-edit-structure
  507. :type 'hook)
  508. (defcustom org-enable-fixed-width-editor t
  509. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  510. This currently only means, they are never auto-wrapped.
  511. When nil, such lines will be treated like ordinary lines.
  512. See also the QUOTE keyword."
  513. :group 'org-edit-structure
  514. :type 'boolean)
  515. (defcustom org-goto-auto-isearch t
  516. "Non-nil means, typing characters in org-goto starts incremental search."
  517. :group 'org-edit-structure
  518. :type 'boolean)
  519. (defgroup org-sparse-trees nil
  520. "Options concerning sparse trees in Org-mode."
  521. :tag "Org Sparse Trees"
  522. :group 'org-structure)
  523. (defcustom org-highlight-sparse-tree-matches t
  524. "Non-nil means, highlight all matches that define a sparse tree.
  525. The highlights will automatically disappear the next time the buffer is
  526. changed by an edit command."
  527. :group 'org-sparse-trees
  528. :type 'boolean)
  529. (defcustom org-remove-highlights-with-change t
  530. "Non-nil means, any change to the buffer will remove temporary highlights.
  531. Such highlights are created by `org-occur' and `org-clock-display'.
  532. When nil, `C-c C-c needs to be used to get rid of the highlights.
  533. The highlights created by `org-preview-latex-fragment' always need
  534. `C-c C-c' to be removed."
  535. :group 'org-sparse-trees
  536. :group 'org-time
  537. :type 'boolean)
  538. (defcustom org-occur-hook '(org-first-headline-recenter)
  539. "Hook that is run after `org-occur' has constructed a sparse tree.
  540. This can be used to recenter the window to show as much of the structure
  541. as possible."
  542. :group 'org-sparse-trees
  543. :type 'hook)
  544. (defgroup org-plain-lists nil
  545. "Options concerning plain lists in Org-mode."
  546. :tag "Org Plain lists"
  547. :group 'org-structure)
  548. (defcustom org-cycle-include-plain-lists nil
  549. "Non-nil means, include plain lists into visibility cycling.
  550. This means that during cycling, plain list items will *temporarily* be
  551. interpreted as outline headlines with a level given by 1000+i where i is the
  552. indentation of the bullet. In all other operations, plain list items are
  553. not seen as headlines. For example, you cannot assign a TODO keyword to
  554. such an item."
  555. :group 'org-plain-lists
  556. :type 'boolean)
  557. (defcustom org-plain-list-ordered-item-terminator t
  558. "The character that makes a line with leading number an ordered list item.
  559. Valid values are ?. and ?\). To get both terminators, use t. While
  560. ?. may look nicer, it creates the danger that a line with leading
  561. number may be incorrectly interpreted as an item. ?\) therefore is
  562. the safe choice."
  563. :group 'org-plain-lists
  564. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  565. (const :tag "paren like in \"2)\"" ?\))
  566. (const :tab "both" t)))
  567. (defcustom org-auto-renumber-ordered-lists t
  568. "Non-nil means, automatically renumber ordered plain lists.
  569. Renumbering happens when the sequence have been changed with
  570. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  571. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  572. :group 'org-plain-lists
  573. :type 'boolean)
  574. (defcustom org-provide-checkbox-statistics t
  575. "Non-nil means, update checkbox statistics after insert and toggle.
  576. When this is set, checkbox statistics is updated each time you either insert
  577. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  578. with \\[org-ctrl-c-ctrl-c\\]."
  579. :group 'org-plain-lists
  580. :type 'boolean)
  581. (defgroup org-archive nil
  582. "Options concerning archiving in Org-mode."
  583. :tag "Org Archive"
  584. :group 'org-structure)
  585. (defcustom org-archive-tag "ARCHIVE"
  586. "The tag that marks a subtree as archived.
  587. An archived subtree does not open during visibility cycling, and does
  588. not contribute to the agenda listings.
  589. After changing this, font-lock must be restarted in the relevant buffers to
  590. get the proper fontification."
  591. :group 'org-archive
  592. :group 'org-keywords
  593. :type 'string)
  594. (defcustom org-agenda-skip-archived-trees t
  595. "Non-nil means, the agenda will skip any items located in archived trees.
  596. An archived tree is a tree marked with the tag ARCHIVE."
  597. :group 'org-archive
  598. :group 'org-agenda-skip
  599. :type 'boolean)
  600. (defcustom org-cycle-open-archived-trees nil
  601. "Non-nil means, `org-cycle' will open archived trees.
  602. An archived tree is a tree marked with the tag ARCHIVE.
  603. When nil, archived trees will stay folded. You can still open them with
  604. normal outline commands like `show-all', but not with the cycling commands."
  605. :group 'org-archive
  606. :group 'org-cycle
  607. :type 'boolean)
  608. (defcustom org-sparse-tree-open-archived-trees nil
  609. "Non-nil means sparse tree construction shows matches in archived trees.
  610. When nil, matches in these trees are highlighted, but the trees are kept in
  611. collapsed state."
  612. :group 'org-archive
  613. :group 'org-sparse-trees
  614. :type 'boolean)
  615. (defcustom org-archive-location "%s_archive::"
  616. "The location where subtrees should be archived.
  617. This string consists of two parts, separated by a double-colon.
  618. The first part is a file name - when omitted, archiving happens in the same
  619. file. %s will be replaced by the current file name (without directory part).
  620. Archiving to a different file is useful to keep archived entries from
  621. contributing to the Org-mode Agenda.
  622. The part after the double colon is a headline. The archived entries will be
  623. filed under that headline. When omitted, the subtrees are simply filed away
  624. at the end of the file, as top-level entries.
  625. Here are a few examples:
  626. \"%s_archive::\"
  627. If the current file is Projects.org, archive in file
  628. Projects.org_archive, as top-level trees. This is the default.
  629. \"::* Archived Tasks\"
  630. Archive in the current file, under the top-level headline
  631. \"* Archived Tasks\".
  632. \"~/org/archive.org::\"
  633. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  634. \"basement::** Finished Tasks\"
  635. Archive in file ./basement (relative path), as level 3 trees
  636. below the level 2 heading \"** Finished Tasks\".
  637. You may set this option on a per-file basis by adding to the buffer a
  638. line like
  639. #+ARCHIVE: basement::** Finished Tasks"
  640. :group 'org-archive
  641. :type 'string)
  642. (defcustom org-archive-mark-done t
  643. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  644. This can be a string to set the keyword to use. When t, Org-mode will
  645. use the first keyword in its list that means done."
  646. :group 'org-archive
  647. :type '(choice
  648. (const :tag "No" nil)
  649. (const :tag "Yes" t)
  650. (string :tag "Use this keyword")))
  651. (defcustom org-archive-stamp-time t
  652. "Non-nil means, add a time stamp to entries moved to an archive file.
  653. This variable is obsolete and has no effect anymore, instead add ot remove
  654. `time' from the variablle `org-archive-save-context-info'."
  655. :group 'org-archive
  656. :type 'boolean)
  657. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  658. "Parts of context info that should be stored as properties when archiving.
  659. When a subtree is moved to an archive file, it looses information given by
  660. context, like inherited tags, the category, and possibly also the TODO
  661. state (depending on the variable `org-archive-mark-done').
  662. This variable can be a list of any of the following symbols:
  663. time The time of archiving.
  664. file The file where the entry originates.
  665. itags The local tags, in the headline of the subtree.
  666. ltags The tags the subtree inherits from further up the hierarchy.
  667. todo The pre-archive TODO state.
  668. category The category, taken from file name or #+CATEGORY lines.
  669. olpath The outline path to the item. These are all headlines above
  670. the current item, separated by /, like a file path.
  671. For each symbol present in the list, a property will be created in
  672. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  673. information."
  674. :group 'org-archive
  675. :type '(set :greedy t
  676. (const :tag "Time" time)
  677. (const :tag "File" file)
  678. (const :tag "Category" category)
  679. (const :tag "TODO state" todo)
  680. (const :tag "TODO state" priority)
  681. (const :tag "Inherited tags" itags)
  682. (const :tag "Outline path" olpath)
  683. (const :tag "Local tags" ltags)))
  684. (defgroup org-imenu-and-speedbar nil
  685. "Options concerning imenu and speedbar in Org-mode."
  686. :tag "Org Imenu and Speedbar"
  687. :group 'org-structure)
  688. (defcustom org-imenu-depth 2
  689. "The maximum level for Imenu access to Org-mode headlines.
  690. This also applied for speedbar access."
  691. :group 'org-imenu-and-speedbar
  692. :type 'number)
  693. (defgroup org-table nil
  694. "Options concerning tables in Org-mode."
  695. :tag "Org Table"
  696. :group 'org)
  697. (defcustom org-enable-table-editor 'optimized
  698. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  699. When nil, such lines will be treated like ordinary lines.
  700. When equal to the symbol `optimized', the table editor will be optimized to
  701. do the following:
  702. - Automatic overwrite mode in front of whitespace in table fields.
  703. This makes the structure of the table stay in tact as long as the edited
  704. field does not exceed the column width.
  705. - Minimize the number of realigns. Normally, the table is aligned each time
  706. TAB or RET are pressed to move to another field. With optimization this
  707. happens only if changes to a field might have changed the column width.
  708. Optimization requires replacing the functions `self-insert-command',
  709. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  710. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  711. very good at guessing when a re-align will be necessary, but you can always
  712. force one with \\[org-ctrl-c-ctrl-c].
  713. If you would like to use the optimized version in Org-mode, but the
  714. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  715. This variable can be used to turn on and off the table editor during a session,
  716. but in order to toggle optimization, a restart is required.
  717. See also the variable `org-table-auto-blank-field'."
  718. :group 'org-table
  719. :type '(choice
  720. (const :tag "off" nil)
  721. (const :tag "on" t)
  722. (const :tag "on, optimized" optimized)))
  723. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  724. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  725. In the optimized version, the table editor takes over all simple keys that
  726. normally just insert a character. In tables, the characters are inserted
  727. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  728. for empty fields). Outside tables, the correct binding of the keys is
  729. restored.
  730. The default for this option is t if the optimized version is also used in
  731. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  732. this variable requires a restart of Emacs to become effective."
  733. :group 'org-table
  734. :type 'boolean)
  735. (defcustom orgtbl-radio-table-templates
  736. '((latex-mode "% BEGIN RECEIVE ORGTBL %n
  737. % END RECEIVE ORGTBL %n
  738. \\begin{comment}
  739. #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
  740. | | |
  741. \\end{comment}\n")
  742. (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
  743. @c END RECEIVE ORGTBL %n
  744. @ignore
  745. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  746. | | |
  747. @end ignore\n")
  748. (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
  749. <!-- END RECEIVE ORGTBL %n -->
  750. <!--
  751. #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
  752. | | |
  753. -->\n"))
  754. "Templates for radio tables in different major modes.
  755. All occurrences of %n in a template will be replaced with the name of the
  756. table, obtained by prompting the user."
  757. :group 'org-table
  758. :type '(repeat
  759. (list (symbol :tag "Major mode")
  760. (string :tag "Format"))))
  761. (defgroup org-table-settings nil
  762. "Settings for tables in Org-mode."
  763. :tag "Org Table Settings"
  764. :group 'org-table)
  765. (defcustom org-table-default-size "5x2"
  766. "The default size for newly created tables, Columns x Rows."
  767. :group 'org-table-settings
  768. :type 'string)
  769. (defcustom org-table-number-regexp
  770. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
  771. "Regular expression for recognizing numbers in table columns.
  772. If a table column contains mostly numbers, it will be aligned to the
  773. right. If not, it will be aligned to the left.
  774. The default value of this option is a regular expression which allows
  775. anything which looks remotely like a number as used in scientific
  776. context. For example, all of the following will be considered a
  777. number:
  778. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  779. Other options offered by the customize interface are more restrictive."
  780. :group 'org-table-settings
  781. :type '(choice
  782. (const :tag "Positive Integers"
  783. "^[0-9]+$")
  784. (const :tag "Integers"
  785. "^[-+]?[0-9]+$")
  786. (const :tag "Floating Point Numbers"
  787. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  788. (const :tag "Floating Point Number or Integer"
  789. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  790. (const :tag "Exponential, Floating point, Integer"
  791. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  792. (const :tag "Very General Number-Like, including hex"
  793. "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
  794. (string :tag "Regexp:")))
  795. (defcustom org-table-number-fraction 0.5
  796. "Fraction of numbers in a column required to make the column align right.
  797. In a column all non-white fields are considered. If at least this
  798. fraction of fields is matched by `org-table-number-fraction',
  799. alignment to the right border applies."
  800. :group 'org-table-settings
  801. :type 'number)
  802. (defgroup org-table-editing nil
  803. "Behavior of tables during editing in Org-mode."
  804. :tag "Org Table Editing"
  805. :group 'org-table)
  806. (defcustom org-table-automatic-realign t
  807. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  808. When nil, aligning is only done with \\[org-table-align], or after column
  809. removal/insertion."
  810. :group 'org-table-editing
  811. :type 'boolean)
  812. (defcustom org-table-auto-blank-field t
  813. "Non-nil means, automatically blank table field when starting to type into it.
  814. This only happens when typing immediately after a field motion
  815. command (TAB, S-TAB or RET).
  816. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  817. :group 'org-table-editing
  818. :type 'boolean)
  819. (defcustom org-table-tab-jumps-over-hlines t
  820. "Non-nil means, tab in the last column of a table with jump over a hline.
  821. If a horizontal separator line is following the current line,
  822. `org-table-next-field' can either create a new row before that line, or jump
  823. over the line. When this option is nil, a new line will be created before
  824. this line."
  825. :group 'org-table-editing
  826. :type 'boolean)
  827. (defcustom org-table-tab-recognizes-table.el t
  828. "Non-nil means, TAB will automatically notice a table.el table.
  829. When it sees such a table, it moves point into it and - if necessary -
  830. calls `table-recognize-table'."
  831. :group 'org-table-editing
  832. :type 'boolean)
  833. (defgroup org-table-calculation nil
  834. "Options concerning tables in Org-mode."
  835. :tag "Org Table Calculation"
  836. :group 'org-table)
  837. (defcustom org-table-use-standard-references t
  838. "Should org-mode work with table refrences like B3 instead of @3$2?
  839. Possible values are:
  840. nil never use them
  841. from accept as input, do not present for editing
  842. t: accept as input and present for editing"
  843. :group 'org-table-calculation
  844. :type '(choice
  845. (const :tag "Never, don't even check unser input for them" nil)
  846. (const :tag "Always, both as user input, and when editing" t)
  847. (const :tag "Convert user input, don't offer during editing" 'from)))
  848. (defcustom org-table-copy-increment t
  849. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  850. :group 'org-table-calculation
  851. :type 'boolean)
  852. (defcustom org-calc-default-modes
  853. '(calc-internal-prec 12
  854. calc-float-format (float 5)
  855. calc-angle-mode deg
  856. calc-prefer-frac nil
  857. calc-symbolic-mode nil
  858. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  859. calc-display-working-message t
  860. )
  861. "List with Calc mode settings for use in calc-eval for table formulas.
  862. The list must contain alternating symbols (Calc modes variables and values).
  863. Don't remove any of the default settings, just change the values. Org-mode
  864. relies on the variables to be present in the list."
  865. :group 'org-table-calculation
  866. :type 'plist)
  867. (defcustom org-table-formula-evaluate-inline t
  868. "Non-nil means, TAB and RET evaluate a formula in current table field.
  869. If the current field starts with an equal sign, it is assumed to be a formula
  870. which should be evaluated as described in the manual and in the documentation
  871. string of the command `org-table-eval-formula'. This feature requires the
  872. Emacs calc package.
  873. When this variable is nil, formula calculation is only available through
  874. the command \\[org-table-eval-formula]."
  875. :group 'org-table-calculation
  876. :type 'boolean)
  877. (defcustom org-table-formula-use-constants t
  878. "Non-nil means, interpret constants in formulas in tables.
  879. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  880. by the value given in `org-table-formula-constants', or by a value obtained
  881. from the `constants.el' package."
  882. :group 'org-table-calculation
  883. :type 'boolean)
  884. (defcustom org-table-formula-constants nil
  885. "Alist with constant names and values, for use in table formulas.
  886. The car of each element is a name of a constant, without the `$' before it.
  887. The cdr is the value as a string. For example, if you'd like to use the
  888. speed of light in a formula, you would configure
  889. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  890. and then use it in an equation like `$1*$c'.
  891. Constants can also be defined on a per-file basis using a line like
  892. #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  893. :group 'org-table-calculation
  894. :type '(repeat
  895. (cons (string :tag "name")
  896. (string :tag "value"))))
  897. (defvar org-table-formula-constants-local nil
  898. "Local version of `org-table-formula-constants'.")
  899. (make-variable-buffer-local 'org-table-formula-constants-local)
  900. (defcustom org-table-allow-automatic-line-recalculation t
  901. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  902. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  903. :group 'org-table-calculation
  904. :type 'boolean)
  905. (defgroup org-link nil
  906. "Options concerning links in Org-mode."
  907. :tag "Org Link"
  908. :group 'org)
  909. (defvar org-link-abbrev-alist-local nil
  910. "Buffer-local version of `org-link-abbrev-alist', which see.
  911. The value of this is taken from the #+LINK lines.")
  912. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  913. (defcustom org-link-abbrev-alist nil
  914. "Alist of link abbreviations.
  915. The car of each element is a string, to be replaced at the start of a link.
  916. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  917. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  918. [[linkkey:tag][description]]
  919. If REPLACE is a string, the tag will simply be appended to create the link.
  920. If the string contains \"%s\", the tag will be inserted there.
  921. REPLACE may also be a function that will be called with the tag as the
  922. only argument to create the link, which should be returned as a string.
  923. See the manual for examples."
  924. :group 'org-link
  925. :type 'alist)
  926. (defcustom org-descriptive-links t
  927. "Non-nil means, hide link part and only show description of bracket links.
  928. Bracket links are like [[link][descritpion]]. This variable sets the initial
  929. state in new org-mode buffers. The setting can then be toggled on a
  930. per-buffer basis from the Org->Hyperlinks menu."
  931. :group 'org-link
  932. :type 'boolean)
  933. (defcustom org-link-file-path-type 'adaptive
  934. "How the path name in file links should be stored.
  935. Valid values are:
  936. relative Relative to the current directory, i.e. the directory of the file
  937. into which the link is being inserted.
  938. absolute Absolute path, if possible with ~ for home directory.
  939. noabbrev Absolute path, no abbreviation of home directory.
  940. adaptive Use relative path for files in the current directory and sub-
  941. directories of it. For other files, use an absolute path."
  942. :group 'org-link
  943. :type '(choice
  944. (const relative)
  945. (const absolute)
  946. (const noabbrev)
  947. (const adaptive)))
  948. (defcustom org-activate-links '(bracket angle plain radio tag date)
  949. "Types of links that should be activated in Org-mode files.
  950. This is a list of symbols, each leading to the activation of a certain link
  951. type. In principle, it does not hurt to turn on most link types - there may
  952. be a small gain when turning off unused link types. The types are:
  953. bracket The recommended [[link][description]] or [[link]] links with hiding.
  954. angular Links in angular brackes that may contain whitespace like
  955. <bbdb:Carsten Dominik>.
  956. plain Plain links in normal text, no whitespace, like http://google.com.
  957. radio Text that is matched by a radio target, see manual for details.
  958. tag Tag settings in a headline (link to tag search).
  959. date Time stamps (link to calendar).
  960. Changing this variable requires a restart of Emacs to become effective."
  961. :group 'org-link
  962. :type '(set (const :tag "Double bracket links (new style)" bracket)
  963. (const :tag "Angular bracket links (old style)" angular)
  964. (const :tag "plain text links" plain)
  965. (const :tag "Radio target matches" radio)
  966. (const :tag "Tags" tag)
  967. (const :tag "Tags" target)
  968. (const :tag "Timestamps" date)))
  969. (defgroup org-link-store nil
  970. "Options concerning storing links in Org-mode"
  971. :tag "Org Store Link"
  972. :group 'org-link)
  973. (defcustom org-email-link-description-format "Email %c: %.30s"
  974. "Format of the description part of a link to an email or usenet message.
  975. The following %-excapes will be replaced by corresponding information:
  976. %F full \"From\" field
  977. %f name, taken from \"From\" field, address if no name
  978. %T full \"To\" field
  979. %t first name in \"To\" field, address if no name
  980. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  981. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  982. %s subject
  983. %m message-id.
  984. You may use normal field width specification between the % and the letter.
  985. This is for example useful to limit the length of the subject.
  986. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  987. :group 'org-link-store
  988. :type 'string)
  989. (defcustom org-from-is-user-regexp
  990. (let (r1 r2)
  991. (when (and user-mail-address (not (string= user-mail-address "")))
  992. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  993. (when (and user-full-name (not (string= user-full-name "")))
  994. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  995. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  996. "Regexp mached against the \"From:\" header of an email or usenet message.
  997. It should match if the message is from the user him/herself."
  998. :group 'org-link-store
  999. :type 'regexp)
  1000. (defcustom org-context-in-file-links t
  1001. "Non-nil means, file links from `org-store-link' contain context.
  1002. A search string will be added to the file name with :: as separator and
  1003. used to find the context when the link is activated by the command
  1004. `org-open-at-point'.
  1005. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1006. negates this setting for the duration of the command."
  1007. :group 'org-link-store
  1008. :type 'boolean)
  1009. (defcustom org-keep-stored-link-after-insertion nil
  1010. "Non-nil means, keep link in list for entire session.
  1011. The command `org-store-link' adds a link pointing to the current
  1012. location to an internal list. These links accumulate during a session.
  1013. The command `org-insert-link' can be used to insert links into any
  1014. Org-mode file (offering completion for all stored links). When this
  1015. option is nil, every link which has been inserted once using \\[org-insert-link]
  1016. will be removed from the list, to make completing the unused links
  1017. more efficient."
  1018. :group 'org-link-store
  1019. :type 'boolean)
  1020. (defcustom org-usenet-links-prefer-google nil
  1021. "Non-nil means, `org-store-link' will create web links to Google groups.
  1022. When nil, Gnus will be used for such links.
  1023. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1024. negates this setting for the duration of the command."
  1025. :group 'org-link-store
  1026. :type 'boolean)
  1027. (defgroup org-link-follow nil
  1028. "Options concerning following links in Org-mode"
  1029. :tag "Org Follow Link"
  1030. :group 'org-link)
  1031. (defcustom org-tab-follows-link nil
  1032. "Non-nil means, on links TAB will follow the link.
  1033. Needs to be set before org.el is loaded."
  1034. :group 'org-link-follow
  1035. :type 'boolean)
  1036. (defcustom org-return-follows-link nil
  1037. "Non-nil means, on links RET will follow the link.
  1038. Needs to be set before org.el is loaded."
  1039. :group 'org-link-follow
  1040. :type 'boolean)
  1041. (defcustom org-mouse-1-follows-link t
  1042. "Non-nil means, mouse-1 on a link will follow the link.
  1043. A longer mouse click will still set point. Does not wortk on XEmacs.
  1044. Needs to be set before org.el is loaded."
  1045. :group 'org-link-follow
  1046. :type 'boolean)
  1047. (defcustom org-mark-ring-length 4
  1048. "Number of different positions to be recorded in the ring
  1049. Changing this requires a restart of Emacs to work correctly."
  1050. :group 'org-link-follow
  1051. :type 'interger)
  1052. (defcustom org-link-frame-setup
  1053. '((vm . vm-visit-folder-other-frame)
  1054. (gnus . gnus-other-frame)
  1055. (file . find-file-other-window))
  1056. "Setup the frame configuration for following links.
  1057. When following a link with Emacs, it may often be useful to display
  1058. this link in another window or frame. This variable can be used to
  1059. set this up for the different types of links.
  1060. For VM, use any of
  1061. `vm-visit-folder'
  1062. `vm-visit-folder-other-frame'
  1063. For Gnus, use any of
  1064. `gnus'
  1065. `gnus-other-frame'
  1066. For FILE, use any of
  1067. `find-file'
  1068. `find-file-other-window'
  1069. `find-file-other-frame'
  1070. For the calendar, use the variable `calendar-setup'.
  1071. For BBDB, it is currently only possible to display the matches in
  1072. another window."
  1073. :group 'org-link-follow
  1074. :type '(list
  1075. (cons (const vm)
  1076. (choice
  1077. (const vm-visit-folder)
  1078. (const vm-visit-folder-other-window)
  1079. (const vm-visit-folder-other-frame)))
  1080. (cons (const gnus)
  1081. (choice
  1082. (const gnus)
  1083. (const gnus-other-frame)))
  1084. (cons (const file)
  1085. (choice
  1086. (const find-file)
  1087. (const find-file-other-window)
  1088. (const find-file-other-frame)))))
  1089. (defcustom org-display-internal-link-with-indirect-buffer nil
  1090. "Non-nil means, use indirect buffer to display infile links.
  1091. Activating internal links (from one location in a file to another location
  1092. in the same file) normally just jumps to the location. When the link is
  1093. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1094. another window. When this option is set, the other window actually displays
  1095. an indirect buffer clone of the current buffer, to avoid any visibility
  1096. changes to the current buffer."
  1097. :group 'org-link-follow
  1098. :type 'boolean)
  1099. (defcustom org-open-non-existing-files nil
  1100. "Non-nil means, `org-open-file' will open non-existing files.
  1101. When nil, an error will be generated."
  1102. :group 'org-link-follow
  1103. :type 'boolean)
  1104. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1105. "Function and arguments to call for following mailto links.
  1106. This is a list with the first element being a lisp function, and the
  1107. remaining elements being arguments to the function. In string arguments,
  1108. %a will be replaced by the address, and %s will be replaced by the subject
  1109. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1110. :group 'org-link-follow
  1111. :type '(choice
  1112. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1113. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1114. (const :tag "message-mail" (message-mail "%a" "%s"))
  1115. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1116. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1117. "Non-nil means, ask for confirmation before executing shell links.
  1118. Shell links can be dangerous: just think about a link
  1119. [[shell:rm -rf ~/*][Google Search]]
  1120. This link would show up in your Org-mode document as \"Google Search\",
  1121. but really it would remove your entire home directory.
  1122. Therefore we advise against setting this variable to nil.
  1123. Just change it to `y-or-n-p' of you want to confirm with a
  1124. single keystroke rather than having to type \"yes\"."
  1125. :group 'org-link-follow
  1126. :type '(choice
  1127. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1128. (const :tag "with y-or-n (faster)" y-or-n-p)
  1129. (const :tag "no confirmation (dangerous)" nil)))
  1130. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1131. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1132. Elisp links can be dangerous: just think about a link
  1133. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1134. This link would show up in your Org-mode document as \"Google Search\",
  1135. but really it would remove your entire home directory.
  1136. Therefore we advise against setting this variable to nil.
  1137. Just change it to `y-or-n-p' of you want to confirm with a
  1138. single keystroke rather than having to type \"yes\"."
  1139. :group 'org-link-follow
  1140. :type '(choice
  1141. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1142. (const :tag "with y-or-n (faster)" y-or-n-p)
  1143. (const :tag "no confirmation (dangerous)" nil)))
  1144. (defconst org-file-apps-defaults-gnu
  1145. '((remote . emacs)
  1146. (t . mailcap))
  1147. "Default file applications on a UNIX or GNU/Linux system.
  1148. See `org-file-apps'.")
  1149. (defconst org-file-apps-defaults-macosx
  1150. '((remote . emacs)
  1151. (t . "open %s")
  1152. ("ps" . "gv %s")
  1153. ("ps.gz" . "gv %s")
  1154. ("eps" . "gv %s")
  1155. ("eps.gz" . "gv %s")
  1156. ("dvi" . "xdvi %s")
  1157. ("fig" . "xfig %s"))
  1158. "Default file applications on a MacOS X system.
  1159. The system \"open\" is known as a default, but we use X11 applications
  1160. for some files for which the OS does not have a good default.
  1161. See `org-file-apps'.")
  1162. (defconst org-file-apps-defaults-windowsnt
  1163. (list
  1164. '(remote . emacs)
  1165. (cons t
  1166. (list (if (featurep 'xemacs)
  1167. 'mswindows-shell-execute
  1168. 'w32-shell-execute)
  1169. "open" 'file)))
  1170. "Default file applications on a Windows NT system.
  1171. The system \"open\" is used for most files.
  1172. See `org-file-apps'.")
  1173. (defcustom org-file-apps
  1174. '(
  1175. ("txt" . emacs)
  1176. ("tex" . emacs)
  1177. ("ltx" . emacs)
  1178. ("org" . emacs)
  1179. ("el" . emacs)
  1180. ("bib" . emacs)
  1181. )
  1182. "External applications for opening `file:path' items in a document.
  1183. Org-mode uses system defaults for different file types, but
  1184. you can use this variable to set the application for a given file
  1185. extension. The entries in this list are cons cells where the car identifies
  1186. files and the cdr the corresponding command. Possible values for the
  1187. file identifier are
  1188. \"ext\" A string identifying an extension
  1189. `directory' Matches a directory
  1190. `remote' Matches a remote file, accessible through tramp or efs.
  1191. Remote files most likely should be visited through Emacs
  1192. because external applications cannot handle such paths.
  1193. t Default for all remaining files
  1194. Possible values for the command are:
  1195. `emacs' The file will be visited by the current Emacs process.
  1196. `default' Use the default application for this file type.
  1197. string A command to be executed by a shell; %s will be replaced
  1198. by the path to the file.
  1199. sexp A Lisp form which will be evaluated. The file path will
  1200. be available in the Lisp variable `file'.
  1201. For more examples, see the system specific constants
  1202. `org-file-apps-defaults-macosx'
  1203. `org-file-apps-defaults-windowsnt'
  1204. `org-file-apps-defaults-gnu'."
  1205. :group 'org-link-follow
  1206. :type '(repeat
  1207. (cons (choice :value ""
  1208. (string :tag "Extension")
  1209. (const :tag "Default for unrecognized files" t)
  1210. (const :tag "Remote file" remote)
  1211. (const :tag "Links to a directory" directory))
  1212. (choice :value ""
  1213. (const :tag "Visit with Emacs" emacs)
  1214. (const :tag "Use system default" default)
  1215. (string :tag "Command")
  1216. (sexp :tag "Lisp form")))))
  1217. (defcustom org-mhe-search-all-folders nil
  1218. "Non-nil means, that the search for the mh-message will be extended to
  1219. all folders if the message cannot be found in the folder given in the link.
  1220. Searching all folders is very efficient with one of the search engines
  1221. supported by MH-E, but will be slow with pick."
  1222. :group 'org-link-follow
  1223. :type 'boolean)
  1224. (defgroup org-remember nil
  1225. "Options concerning interaction with remember.el."
  1226. :tag "Org Remember"
  1227. :group 'org)
  1228. (defcustom org-directory "~/org"
  1229. "Directory with org files.
  1230. This directory will be used as default to prompt for org files.
  1231. Used by the hooks for remember.el."
  1232. :group 'org-remember
  1233. :type 'directory)
  1234. (defcustom org-default-notes-file "~/.notes"
  1235. "Default target for storing notes.
  1236. Used by the hooks for remember.el. This can be a string, or nil to mean
  1237. the value of `remember-data-file'.
  1238. You can set this on a per-template basis with the variable
  1239. `org-remember-templates'."
  1240. :group 'org-remember
  1241. :type '(choice
  1242. (const :tag "Default from remember-data-file" nil)
  1243. file))
  1244. (defcustom org-remember-store-without-prompt t
  1245. "Non-nil means, `C-c C-c' stores remember note without further promts.
  1246. In this case, you need `C-u C-c C-c' to get the prompts for
  1247. note file and headline.
  1248. When this variable is nil, `C-c C-c' give you the prompts, and
  1249. `C-u C-c C-c' trigger the fasttrack."
  1250. :group 'org-remember
  1251. :type 'boolean)
  1252. (defcustom org-remember-interactive-interface 'refile
  1253. "The interface to be used for interactive filing of remember notes.
  1254. This is only used when the interactive mode for selecting a filing
  1255. location is used (see the variable `org-remember-store-without-prompt').
  1256. Allowed vaues are:
  1257. outline The interface shows an outline of the relevant file
  1258. and the correct heading is found by moving through
  1259. the outline or by searching with incremental search.
  1260. outline-path-completion Headlines in the current buffer are offered via
  1261. completion.
  1262. refile Use the refile interface, and offer headlines,
  1263. possibly from different buffers."
  1264. :group 'org-remember
  1265. :type '(choice
  1266. (const :tag "Refile" refile)
  1267. (const :tag "Outline" outline)
  1268. (const :tag "Outline-path-completion" outline-path-completion)))
  1269. (defcustom org-goto-interface 'outline
  1270. "The default interface to be used for `org-goto'.
  1271. Allowed vaues are:
  1272. outline The interface shows an outline of the relevant file
  1273. and the correct heading is found by moving through
  1274. the outline or by searching with incremental search.
  1275. outline-path-completion Headlines in the current buffer are offered via
  1276. completion."
  1277. :group 'org-remember ; FIXME: different group for org-goto and org-refile
  1278. :type '(choice
  1279. (const :tag "Outline" outline)
  1280. (const :tag "Outline-path-completion" outline-path-completion)))
  1281. (defcustom org-remember-default-headline ""
  1282. "The headline that should be the default location in the notes file.
  1283. When filing remember notes, the cursor will start at that position.
  1284. You can set this on a per-template basis with the variable
  1285. `org-remember-templates'."
  1286. :group 'org-remember
  1287. :type 'string)
  1288. (defcustom org-remember-templates nil
  1289. "Templates for the creation of remember buffers.
  1290. When nil, just let remember make the buffer.
  1291. When not nil, this is a list of 5-element lists. In each entry, the first
  1292. element is the name of the template, which should be a single short word.
  1293. The second element is a character, a unique key to select this template.
  1294. The third element is the template. The fourth element is optional and can
  1295. specify a destination file for remember items created with this template.
  1296. The default file is given by `org-default-notes-file'. An optional fifth
  1297. element can specify the headline in that file that should be offered
  1298. first when the user is asked to file the entry. The default headline is
  1299. given in the variable `org-remember-default-headline'.
  1300. The template specifies the structure of the remember buffer. It should have
  1301. a first line starting with a star, to act as the org-mode headline.
  1302. Furthermore, the following %-escapes will be replaced with content:
  1303. %^{prompt} Prompt the user for a string and replace this sequence with it.
  1304. A default value and a completion table ca be specified like this:
  1305. %^{prompt|default|completion2|completion3|...}
  1306. %t time stamp, date only
  1307. %T time stamp with date and time
  1308. %u, %U like the above, but inactive time stamps
  1309. %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
  1310. You may define a prompt like %^{Please specify birthday}t
  1311. %n user name (taken from `user-full-name')
  1312. %a annotation, normally the link created with org-store-link
  1313. %i initial content, the region when remember is called with C-u.
  1314. If %i is indented, the entire inserted text will be indented
  1315. as well.
  1316. %c content of the clipboard, or current kill ring head
  1317. %^g prompt for tags, with completion on tags in target file
  1318. %^G prompt for tags, with completion all tags in all agenda files
  1319. %:keyword specific information for certain link types, see below
  1320. %[pathname] insert the contents of the file given by `pathname'
  1321. %(sexp) evaluate elisp `(sexp)' and replace with the result
  1322. %! Store this note immediately after filling the template
  1323. %? After completing the template, position cursor here.
  1324. Apart from these general escapes, you can access information specific to the
  1325. link type that is created. For example, calling `remember' in emails or gnus
  1326. will record the author and the subject of the message, which you can access
  1327. with %:author and %:subject, respectively. Here is a complete list of what
  1328. is recorded for each link type.
  1329. Link type | Available information
  1330. -------------------+------------------------------------------------------
  1331. bbdb | %:type %:name %:company
  1332. vm, wl, mh, rmail | %:type %:subject %:message-id
  1333. | %:from %:fromname %:fromaddress
  1334. | %:to %:toname %:toaddress
  1335. | %:fromto (either \"to NAME\" or \"from NAME\")
  1336. gnus | %:group, for messages also all email fields
  1337. w3, w3m | %:type %:url
  1338. info | %:type %:file %:node
  1339. calendar | %:type %:date"
  1340. :group 'org-remember
  1341. :get (lambda (var) ; Make sure all entries have 5 elements
  1342. (mapcar (lambda (x)
  1343. (if (not (stringp (car x))) (setq x (cons "" x)))
  1344. (cond ((= (length x) 4) (append x '("")))
  1345. ((= (length x) 3) (append x '("" "")))
  1346. (t x)))
  1347. (default-value var)))
  1348. :type '(repeat
  1349. :tag "enabled"
  1350. (list :value ("" ?a "\n" nil nil)
  1351. (string :tag "Name")
  1352. (character :tag "Selection Key")
  1353. (string :tag "Template")
  1354. (choice
  1355. (file :tag "Destination file")
  1356. (const :tag "Prompt for file" nil))
  1357. (choice
  1358. (string :tag "Destination headline")
  1359. (const :tag "Selection interface for heading")))))
  1360. (defcustom org-reverse-note-order nil
  1361. "Non-nil means, store new notes at the beginning of a file or entry.
  1362. When nil, new notes will be filed to the end of a file or entry.
  1363. This can also be a list with cons cells of regular expressions that
  1364. are matched against file names, and values."
  1365. :group 'org-remember
  1366. :type '(choice
  1367. (const :tag "Reverse always" t)
  1368. (const :tag "Reverse never" nil)
  1369. (repeat :tag "By file name regexp"
  1370. (cons regexp boolean))))
  1371. (defcustom org-refile-targets nil
  1372. "Targets for refiling entries with \\[org-refile].
  1373. This is list of cons cells. Each cell contains:
  1374. - a specification of the files to be considered, either a list of files,
  1375. or a symbol whose function or value fields will be used to retrieve
  1376. a file name or a list of file names. Nil means, refile to a different
  1377. heading in the current buffer.
  1378. - A specification of how to find candidate refile targets. This may be
  1379. any of
  1380. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1381. This tag has to be present in all target headlines, inheritance will
  1382. not be considered.
  1383. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1384. todo keyword.
  1385. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1386. headlines that are refiling targets.
  1387. - a cons cell (:level . N). Any headline of level N is considered a target.
  1388. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1389. ;; FIXME: what if there are a var and func with same name???
  1390. :group 'org-remember
  1391. :type '(repeat
  1392. (cons
  1393. (choice :value org-agenda-files
  1394. (const :tag "All agenda files" org-agenda-files)
  1395. (const :tag "Current buffer" nil)
  1396. (function) (variable) (file))
  1397. (choice :tag "Identify target headline by"
  1398. (cons :tag "Specific tag" (const :tag) (string))
  1399. (cons :tag "TODO keyword" (const :todo) (string))
  1400. (cons :tag "Regular expression" (const :regexp) (regexp))
  1401. (cons :tag "Level number" (const :level) (integer))
  1402. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1403. (defcustom org-refile-use-outline-path nil
  1404. "Non-nil means, provide refile targets as paths.
  1405. So a level 3 headline will be available as level1/level2/level3.
  1406. When the value is `file', also include the file name (without directory)
  1407. into the path. When `full-file-path', include the full file path."
  1408. :group 'org-remember
  1409. :type '(choice
  1410. (const :tag "Not" nil)
  1411. (const :tag "Yes" t)
  1412. (const :tag "Start with file name" file)
  1413. (const :tag "Start with full file path" full-file-path)))
  1414. (defgroup org-todo nil
  1415. "Options concerning TODO items in Org-mode."
  1416. :tag "Org TODO"
  1417. :group 'org)
  1418. (defgroup org-progress nil
  1419. "Options concerning Progress logging in Org-mode."
  1420. :tag "Org Progress"
  1421. :group 'org-time)
  1422. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1423. "List of TODO entry keyword sequences and their interpretation.
  1424. \\<org-mode-map>This is a list of sequences.
  1425. Each sequence starts with a symbol, either `sequence' or `type',
  1426. indicating if the keywords should be interpreted as a sequence of
  1427. action steps, or as different types of TODO items. The first
  1428. keywords are states requiring action - these states will select a headline
  1429. for inclusion into the global TODO list Org-mode produces. If one of
  1430. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1431. signify that no further action is necessary. If \"|\" is not found,
  1432. the last keyword is treated as the only DONE state of the sequence.
  1433. The command \\[org-todo] cycles an entry through these states, and one
  1434. additional state where no keyword is present. For details about this
  1435. cycling, see the manual.
  1436. TODO keywords and interpretation can also be set on a per-file basis with
  1437. the special #+SEQ_TODO and #+TYP_TODO lines.
  1438. For backward compatibility, this variable may also be just a list
  1439. of keywords - in this case the interptetation (sequence or type) will be
  1440. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1441. :group 'org-todo
  1442. :group 'org-keywords
  1443. :type '(choice
  1444. (repeat :tag "Old syntax, just keywords"
  1445. (string :tag "Keyword"))
  1446. (repeat :tag "New syntax"
  1447. (cons
  1448. (choice
  1449. :tag "Interpretation"
  1450. (const :tag "Sequence (cycling hits every state)" sequence)
  1451. (const :tag "Type (cycling directly to DONE)" type))
  1452. (repeat
  1453. (string :tag "Keyword"))))))
  1454. (defvar org-todo-keywords-1 nil)
  1455. (make-variable-buffer-local 'org-todo-keywords-1)
  1456. (defvar org-todo-keywords-for-agenda nil)
  1457. (defvar org-done-keywords-for-agenda nil)
  1458. (defvar org-not-done-keywords nil)
  1459. (make-variable-buffer-local 'org-not-done-keywords)
  1460. (defvar org-done-keywords nil)
  1461. (make-variable-buffer-local 'org-done-keywords)
  1462. (defvar org-todo-heads nil)
  1463. (make-variable-buffer-local 'org-todo-heads)
  1464. (defvar org-todo-sets nil)
  1465. (make-variable-buffer-local 'org-todo-sets)
  1466. (defvar org-todo-log-states nil)
  1467. (make-variable-buffer-local 'org-todo-log-states)
  1468. (defvar org-todo-kwd-alist nil)
  1469. (make-variable-buffer-local 'org-todo-kwd-alist)
  1470. (defvar org-todo-key-alist nil)
  1471. (make-variable-buffer-local 'org-todo-key-alist)
  1472. (defvar org-todo-key-trigger nil)
  1473. (make-variable-buffer-local 'org-todo-key-trigger)
  1474. (defcustom org-todo-interpretation 'sequence
  1475. "Controls how TODO keywords are interpreted.
  1476. This variable is in principle obsolete and is only used for
  1477. backward compatibility, if the interpretation of todo keywords is
  1478. not given already in `org-todo-keywords'. See that variable for
  1479. more information."
  1480. :group 'org-todo
  1481. :group 'org-keywords
  1482. :type '(choice (const sequence)
  1483. (const type)))
  1484. (defcustom org-use-fast-todo-selection 'prefix
  1485. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1486. This variable describes if and under what circumstances the cycling
  1487. mechanism for TODO keywords will be replaced by a single-key, direct
  1488. selection scheme.
  1489. When nil, fast selection is never used.
  1490. When the symbol `prefix', it will be used when `org-todo' is called with
  1491. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1492. in an agenda buffer.
  1493. When t, fast selection is used by default. In this case, the prefix
  1494. argument forces cycling instead.
  1495. In all cases, the special interface is only used if access keys have actually
  1496. been assigned by the user, i.e. if keywords in the configuration are followed
  1497. by a letter in parenthesis, like TODO(t)."
  1498. :group 'org-todo
  1499. :type '(choice
  1500. (const :tag "Never" nil)
  1501. (const :tag "By default" t)
  1502. (const :tag "Only with C-u C-c C-t" prefix)))
  1503. (defcustom org-after-todo-state-change-hook nil
  1504. "Hook which is run after the state of a TODO item was changed.
  1505. The new state (a string with a TODO keyword, or nil) is available in the
  1506. Lisp variable `state'."
  1507. :group 'org-todo
  1508. :type 'hook)
  1509. (defcustom org-log-done nil
  1510. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1511. When the state of an entry is changed from nothing or a DONE state to
  1512. a not-done TODO state, remove a previous closing date.
  1513. This can also be a list of symbols indicating under which conditions
  1514. the time stamp recording the action should be annotated with a short note.
  1515. Valid members of this list are
  1516. done Offer to record a note when marking entries done
  1517. state Offer to record a note whenever changing the TODO state
  1518. of an item. This is only relevant if TODO keywords are
  1519. interpreted as sequence, see variable `org-todo-interpretation'.
  1520. When `state' is set, this includes tracking `done'.
  1521. clock-out Offer to record a note when clocking out of an item.
  1522. A separate window will then pop up and allow you to type a note.
  1523. After finishing with C-c C-c, the note will be added directly after the
  1524. timestamp, as a plain list item. See also the variable
  1525. `org-log-note-headings'.
  1526. Logging can also be configured on a per-file basis by adding one of
  1527. the following lines anywhere in the buffer:
  1528. #+STARTUP: logdone
  1529. #+STARTUP: nologging
  1530. #+STARTUP: lognotedone
  1531. #+STARTUP: lognotestate
  1532. #+STARTUP: lognoteclock-out
  1533. You can have local logging settings for a subtree by setting the LOGGING
  1534. property to one or more of these keywords."
  1535. :group 'org-todo
  1536. :group 'org-progress
  1537. :type '(choice
  1538. (const :tag "off" nil)
  1539. (const :tag "on" t)
  1540. (set :tag "on, with notes, detailed control" :greedy t :value (done)
  1541. (const :tag "when item is marked DONE" done)
  1542. (const :tag "when TODO state changes" state)
  1543. (const :tag "when clocking out" clock-out))))
  1544. (defcustom org-log-done-with-time t
  1545. "Non-nil means, the CLOSED time stamp will contain date and time.
  1546. When nil, only the date will be recorded."
  1547. :group 'org-progress
  1548. :type 'boolean)
  1549. (defcustom org-log-note-headings
  1550. '((done . "CLOSING NOTE %t")
  1551. (state . "State %-12s %t")
  1552. (clock-out . ""))
  1553. "Headings for notes added when clocking out or closing TODO items.
  1554. The value is an alist, with the car being a symbol indicating the note
  1555. context, and the cdr is the heading to be used. The heading may also be the
  1556. empty string.
  1557. %t in the heading will be replaced by a time stamp.
  1558. %s will be replaced by the new TODO state, in double quotes.
  1559. %u will be replaced by the user name.
  1560. %U will be replaced by the full user name."
  1561. :group 'org-todo
  1562. :group 'org-progress
  1563. :type '(list :greedy t
  1564. (cons (const :tag "Heading when closing an item" done) string)
  1565. (cons (const :tag
  1566. "Heading when changing todo state (todo sequence only)"
  1567. state) string)
  1568. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1569. (defcustom org-log-states-order-reversed t
  1570. "Non-nil means, the latest state change note will be directly after heading.
  1571. When nil, the notes will be orderer according to time."
  1572. :group 'org-todo
  1573. :group 'org-progress
  1574. :type 'boolean)
  1575. (defcustom org-log-repeat t
  1576. "Non-nil means, prompt for a note when REPEAT is resetting a TODO entry.
  1577. When nil, no note will be taken.
  1578. This option can also be set with on a per-file-basis with
  1579. #+STARTUP: logrepeat
  1580. #+STARTUP: nologrepeat
  1581. You can have local logging settings for a subtree by setting the LOGGING
  1582. property to one or more of these keywords."
  1583. :group 'org-todo
  1584. :group 'org-progress
  1585. :type 'boolean)
  1586. (defcustom org-clock-into-drawer 2
  1587. "Should clocking info be wrapped into a drawer?
  1588. When t, clocking info will always be inserted into a :CLOCK: drawer.
  1589. If necessary, the drawer will be created.
  1590. When nil, the drawer will not be created, but used when present.
  1591. When an integer and the number of clocking entries in an item
  1592. reaches or exceeds this number, a drawer will be created."
  1593. :group 'org-todo
  1594. :group 'org-progress
  1595. :type '(choice
  1596. (const :tag "Always" t)
  1597. (const :tag "Only when drawer exists" nil)
  1598. (integer :tag "When at least N clock entries")))
  1599. (defcustom org-clock-out-when-done t
  1600. "When t, the clock will be stopped when the relevant entry is marked DONE.
  1601. Nil means, clock will keep running until stopped explicitly with
  1602. `C-c C-x C-o', or until the clock is started in a different item."
  1603. :group 'org-progress
  1604. :type 'boolean)
  1605. (defcustom org-clock-in-switch-to-state nil
  1606. "Set task to a special todo state while clocking it.
  1607. The value should be the state to which the entry should be switched."
  1608. :group 'org-progress
  1609. :group 'org-todo
  1610. :type '(choice
  1611. (const :tag "Don't force a state" nil)
  1612. (string :tag "State")))
  1613. (defgroup org-priorities nil
  1614. "Priorities in Org-mode."
  1615. :tag "Org Priorities"
  1616. :group 'org-todo)
  1617. (defcustom org-highest-priority ?A
  1618. "The highest priority of TODO items. A character like ?A, ?B etc.
  1619. Must have a smaller ASCII number than `org-lowest-priority'."
  1620. :group 'org-priorities
  1621. :type 'character)
  1622. (defcustom org-lowest-priority ?C
  1623. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1624. Must have a larger ASCII number than `org-highest-priority'."
  1625. :group 'org-priorities
  1626. :type 'character)
  1627. (defcustom org-default-priority ?B
  1628. "The default priority of TODO items.
  1629. This is the priority an item get if no explicit priority is given."
  1630. :group 'org-priorities
  1631. :type 'character)
  1632. (defcustom org-priority-start-cycle-with-default t
  1633. "Non-nil means, start with default priority when starting to cycle.
  1634. When this is nil, the first step in the cycle will be (depending on the
  1635. command used) one higher or lower that the default priority."
  1636. :group 'org-priorities
  1637. :type 'boolean)
  1638. (defgroup org-time nil
  1639. "Options concerning time stamps and deadlines in Org-mode."
  1640. :tag "Org Time"
  1641. :group 'org)
  1642. (defcustom org-insert-labeled-timestamps-at-point nil
  1643. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1644. When nil, these labeled time stamps are forces into the second line of an
  1645. entry, just after the headline. When scheduling from the global TODO list,
  1646. the time stamp will always be forced into the second line."
  1647. :group 'org-time
  1648. :type 'boolean)
  1649. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1650. "Formats for `format-time-string' which are used for time stamps.
  1651. It is not recommended to change this constant.")
  1652. (defcustom org-time-stamp-rounding-minutes 0
  1653. "Number of minutes to round time stamps to upon insertion.
  1654. When zero, insert the time unmodified. Useful rounding numbers
  1655. should be factors of 60, so for example 5, 10, 15.
  1656. When this is not zero, you can still force an exact time-stamp by using
  1657. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1658. :group 'org-time
  1659. :type 'integer)
  1660. (defcustom org-display-custom-times nil
  1661. "Non-nil means, overlay custom formats over all time stamps.
  1662. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1663. To turn this on on a per-file basis, insert anywhere in the file:
  1664. #+STARTUP: customtime"
  1665. :group 'org-time
  1666. :set 'set-default
  1667. :type 'sexp)
  1668. (make-variable-buffer-local 'org-display-custom-times)
  1669. (defcustom org-time-stamp-custom-formats
  1670. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1671. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1672. These are overlayed over the default ISO format if the variable
  1673. `org-display-custom-times' is set. Time like %H:%M should be at the
  1674. end of the second format."
  1675. :group 'org-time
  1676. :type 'sexp)
  1677. (defun org-time-stamp-format (&optional long inactive)
  1678. "Get the right format for a time string."
  1679. (let ((f (if long (cdr org-time-stamp-formats)
  1680. (car org-time-stamp-formats))))
  1681. (if inactive
  1682. (concat "[" (substring f 1 -1) "]")
  1683. f)))
  1684. (defcustom org-read-date-prefer-future t
  1685. "Non-nil means, assume future for incomplete date input from user.
  1686. This affects the following situations:
  1687. 1. The user gives a day, but no month.
  1688. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1689. read this as the third of *next* month. However, if you enter \"17\",
  1690. it will be considered as *this* month.
  1691. 2. The user gives a month but not a year.
  1692. For example, if it is april and you enter \"feb 2\", this will be read
  1693. as feb 2, *next* year. \"May 5\", however, will be this year.
  1694. When this option is nil, the current month and year will always be used
  1695. as defaults."
  1696. :group 'org-time
  1697. :type 'boolean)
  1698. (defcustom org-read-date-display-live t
  1699. "Non-nil means, display current interpretation of date prompt live.
  1700. This display will be in an overlay, in the minibuffer."
  1701. :group 'org-time
  1702. :type 'boolean)
  1703. (defcustom org-read-date-popup-calendar t
  1704. "Non-nil means, pop up a calendar when prompting for a date.
  1705. In the calendar, the date can be selected with mouse-1. However, the
  1706. minibuffer will also be active, and you can simply enter the date as well.
  1707. When nil, only the minibuffer will be available."
  1708. :group 'org-time
  1709. :type 'boolean)
  1710. (if (fboundp 'defvaralias)
  1711. (defvaralias 'org-popup-calendar-for-date-prompt
  1712. 'org-read-date-popup-calendar))
  1713. (defcustom org-extend-today-until 0
  1714. "The hour when your day really ends.
  1715. This has influence for the following applications:
  1716. - When switching the agenda to \"today\". It it is still earlier than
  1717. the time given here, the day recognized as TODAY is actually yesterday.
  1718. - When a date is read from the user and it is still before the time given
  1719. here, the current date and time will be assumed to be yesterday, 23:59.
  1720. FIXME:
  1721. IMPORTANT: This is still a very experimental feature, it may disappear
  1722. again or it may be extended to mean more things."
  1723. :group 'org-time
  1724. :type 'number)
  1725. (defcustom org-edit-timestamp-down-means-later nil
  1726. "Non-nil means, S-down will increase the time in a time stamp.
  1727. When nil, S-up will increase."
  1728. :group 'org-time
  1729. :type 'boolean)
  1730. (defcustom org-calendar-follow-timestamp-change t
  1731. "Non-nil means, make the calendar window follow timestamp changes.
  1732. When a timestamp is modified and the calendar window is visible, it will be
  1733. moved to the new date."
  1734. :group 'org-time
  1735. :type 'boolean)
  1736. (defcustom org-clock-heading-function nil
  1737. "When non-nil, should be a function to create `org-clock-heading'.
  1738. This is the string shown in the mode line when a clock is running.
  1739. The function is called with point at the beginning of the headline."
  1740. :group 'org-time ; FIXME: Should we have a separate group????
  1741. :type 'function)
  1742. (defgroup org-tags nil
  1743. "Options concerning tags in Org-mode."
  1744. :tag "Org Tags"
  1745. :group 'org)
  1746. (defcustom org-tag-alist nil
  1747. "List of tags allowed in Org-mode files.
  1748. When this list is nil, Org-mode will base TAG input on what is already in the
  1749. buffer.
  1750. The value of this variable is an alist, the car of each entry must be a
  1751. keyword as a string, the cdr may be a character that is used to select
  1752. that tag through the fast-tag-selection interface.
  1753. See the manual for details."
  1754. :group 'org-tags
  1755. :type '(repeat
  1756. (choice
  1757. (cons (string :tag "Tag name")
  1758. (character :tag "Access char"))
  1759. (const :tag "Start radio group" (:startgroup))
  1760. (const :tag "End radio group" (:endgroup)))))
  1761. (defcustom org-use-fast-tag-selection 'auto
  1762. "Non-nil means, use fast tag selection scheme.
  1763. This is a special interface to select and deselect tags with single keys.
  1764. When nil, fast selection is never used.
  1765. When the symbol `auto', fast selection is used if and only if selection
  1766. characters for tags have been configured, either through the variable
  1767. `org-tag-alist' or through a #+TAGS line in the buffer.
  1768. When t, fast selection is always used and selection keys are assigned
  1769. automatically if necessary."
  1770. :group 'org-tags
  1771. :type '(choice
  1772. (const :tag "Always" t)
  1773. (const :tag "Never" nil)
  1774. (const :tag "When selection characters are configured" 'auto)))
  1775. (defcustom org-fast-tag-selection-single-key nil
  1776. "Non-nil means, fast tag selection exits after first change.
  1777. When nil, you have to press RET to exit it.
  1778. During fast tag selection, you can toggle this flag with `C-c'.
  1779. This variable can also have the value `expert'. In this case, the window
  1780. displaying the tags menu is not even shown, until you press C-c again."
  1781. :group 'org-tags
  1782. :type '(choice
  1783. (const :tag "No" nil)
  1784. (const :tag "Yes" t)
  1785. (const :tag "Expert" expert)))
  1786. (defvar org-fast-tag-selection-include-todo nil
  1787. "Non-nil means, fast tags selection interface will also offer TODO states.
  1788. This is an undocumented feature, you should not rely on it.")
  1789. (defcustom org-tags-column -80
  1790. "The column to which tags should be indented in a headline.
  1791. If this number is positive, it specifies the column. If it is negative,
  1792. it means that the tags should be flushright to that column. For example,
  1793. -80 works well for a normal 80 character screen."
  1794. :group 'org-tags
  1795. :type 'integer)
  1796. (defcustom org-auto-align-tags t
  1797. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1798. These operations change the length of a headline and therefore shift
  1799. the tags around. With this options turned on, after each such operation
  1800. the tags are again aligned to `org-tags-column'."
  1801. :group 'org-tags
  1802. :type 'boolean)
  1803. (defcustom org-use-tag-inheritance t
  1804. "Non-nil means, tags in levels apply also for sublevels.
  1805. When nil, only the tags directly given in a specific line apply there.
  1806. If you turn off this option, you very likely want to turn on the
  1807. companion option `org-tags-match-list-sublevels'."
  1808. :group 'org-tags
  1809. :type 'boolean)
  1810. (defcustom org-tags-match-list-sublevels nil
  1811. "Non-nil means list also sublevels of headlines matching tag search.
  1812. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1813. the sublevels of a headline matching a tag search often also match
  1814. the same search. Listing all of them can create very long lists.
  1815. Setting this variable to nil causes subtrees of a match to be skipped.
  1816. This option is off by default, because inheritance in on. If you turn
  1817. inheritance off, you very likely want to turn this option on.
  1818. As a special case, if the tag search is restricted to TODO items, the
  1819. value of this variable is ignored and sublevels are always checked, to
  1820. make sure all corresponding TODO items find their way into the list."
  1821. :group 'org-tags
  1822. :type 'boolean)
  1823. (defvar org-tags-history nil
  1824. "History of minibuffer reads for tags.")
  1825. (defvar org-last-tags-completion-table nil
  1826. "The last used completion table for tags.")
  1827. (defvar org-after-tags-change-hook nil
  1828. "Hook that is run after the tags in a line have changed.")
  1829. (defgroup org-properties nil
  1830. "Options concerning properties in Org-mode."
  1831. :tag "Org Properties"
  1832. :group 'org)
  1833. (defcustom org-property-format "%-10s %s"
  1834. "How property key/value pairs should be formatted by `indent-line'.
  1835. When `indent-line' hits a property definition, it will format the line
  1836. according to this format, mainly to make sure that the values are
  1837. lined-up with respect to each other."
  1838. :group 'org-properties
  1839. :type 'string)
  1840. (defcustom org-use-property-inheritance nil
  1841. "Non-nil means, properties apply also for sublevels.
  1842. This setting is only relevant during property searches, not when querying
  1843. an entry with `org-entry-get'. To retrieve a property with inheritance,
  1844. you need to call `org-entry-get' with the inheritance flag.
  1845. Turning this on can cause significant overhead when doing a search, so
  1846. this is turned off by default.
  1847. When nil, only the properties directly given in the current entry count.
  1848. The value may also be a list of properties that shouldhave inheritance.
  1849. However, note that some special properties use inheritance under special
  1850. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1851. and the properties ending in \"_ALL\" when they are used as descriptor
  1852. for valid values of a property."
  1853. :group 'org-properties
  1854. :type '(choice
  1855. (const :tag "Not" nil)
  1856. (const :tag "Always" nil)
  1857. (repeat :tag "Specific properties" (string :tag "Property"))))
  1858. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1859. "The default column format, if no other format has been defined.
  1860. This variable can be set on the per-file basis by inserting a line
  1861. #+COLUMNS: %25ITEM ....."
  1862. :group 'org-properties
  1863. :type 'string)
  1864. (defcustom org-global-properties nil
  1865. "List of property/value pairs that can be inherited by any entry.
  1866. You can set buffer-local values for this by adding lines like
  1867. #+PROPERTY: NAME VALUE"
  1868. :group 'org-properties
  1869. :type '(repeat
  1870. (cons (string :tag "Property")
  1871. (string :tag "Value"))))
  1872. (defvar org-local-properties nil
  1873. "List of property/value pairs that can be inherited by any entry.
  1874. Valid for the current buffer.
  1875. This variable is populated from #+PROPERTY lines.")
  1876. (defgroup org-agenda nil
  1877. "Options concerning agenda views in Org-mode."
  1878. :tag "Org Agenda"
  1879. :group 'org)
  1880. (defvar org-category nil
  1881. "Variable used by org files to set a category for agenda display.
  1882. Such files should use a file variable to set it, for example
  1883. # -*- mode: org; org-category: \"ELisp\"
  1884. or contain a special line
  1885. #+CATEGORY: ELisp
  1886. If the file does not specify a category, then file's base name
  1887. is used instead.")
  1888. (make-variable-buffer-local 'org-category)
  1889. (defcustom org-agenda-files nil
  1890. "The files to be used for agenda display.
  1891. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1892. \\[org-remove-file]. You can also use customize to edit the list.
  1893. If an entry is a directory, all files in that directory that are matched by
  1894. `org-agenda-file-regexp' will be part of the file list.
  1895. If the value of the variable is not a list but a single file name, then
  1896. the list of agenda files is actually stored and maintained in that file, one
  1897. agenda file per line."
  1898. :group 'org-agenda
  1899. :type '(choice
  1900. (repeat :tag "List of files and directories" file)
  1901. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1902. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1903. "Regular expression to match files for `org-agenda-files'.
  1904. If any element in the list in that variable contains a directory instead
  1905. of a normal file, all files in that directory that are matched by this
  1906. regular expression will be included."
  1907. :group 'org-agenda
  1908. :type 'regexp)
  1909. (defcustom org-agenda-skip-unavailable-files nil
  1910. "t means to just skip non-reachable files in `org-agenda-files'.
  1911. Nil means to remove them, after a query, from the list."
  1912. :group 'org-agenda
  1913. :type 'boolean)
  1914. (defcustom org-agenda-multi-occur-extra-files nil
  1915. "List of extra files to be searched by `org-occur-in-agenda-files'.
  1916. The files in `org-agenda-files' are always searched."
  1917. :group 'org-agenda
  1918. :type '(repeat file))
  1919. (defcustom org-agenda-confirm-kill 1
  1920. "When set, remote killing from the agenda buffer needs confirmation.
  1921. When t, a confirmation is always needed. When a number N, confirmation is
  1922. only needed when the text to be killed contains more than N non-white lines."
  1923. :group 'org-agenda
  1924. :type '(choice
  1925. (const :tag "Never" nil)
  1926. (const :tag "Always" t)
  1927. (number :tag "When more than N lines")))
  1928. (defcustom org-calendar-to-agenda-key [?c]
  1929. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1930. The command `org-calendar-goto-agenda' will be bound to this key. The
  1931. default is the character `c' because then `c' can be used to switch back and
  1932. forth between agenda and calendar."
  1933. :group 'org-agenda
  1934. :type 'sexp)
  1935. (defcustom org-agenda-compact-blocks nil
  1936. "Non-nil means, make the block agenda more compact.
  1937. This is done by leaving out unnecessary lines."
  1938. :group 'org-agenda
  1939. :type nil)
  1940. (defgroup org-agenda-export nil
  1941. "Options concerning exporting agenda views in Org-mode."
  1942. :tag "Org Agenda Export"
  1943. :group 'org-agenda)
  1944. (defcustom org-agenda-with-colors t
  1945. "Non-nil means, use colors in agenda views."
  1946. :group 'org-agenda-export
  1947. :type 'boolean)
  1948. (defcustom org-agenda-exporter-settings nil
  1949. "Alist of variable/value pairs that should be active during agenda export.
  1950. This is a good place to set uptions for ps-print and for htmlize."
  1951. :group 'org-agenda-export
  1952. :type '(repeat
  1953. (list
  1954. (variable)
  1955. (sexp :tag "Value"))))
  1956. (defcustom org-agenda-export-html-style ""
  1957. "The style specification for exported HTML Agenda files.
  1958. If this variable contains a string, it will replace the default <style>
  1959. section as produced by `htmlize'.
  1960. Since there are different ways of setting style information, this variable
  1961. needs to contain the full HTML structure to provide a style, including the
  1962. surrounding HTML tags. The style specifications should include definitions
  1963. the fonts used by the agenda, here is an example:
  1964. <style type=\"text/css\">
  1965. p { font-weight: normal; color: gray; }
  1966. .org-agenda-structure {
  1967. font-size: 110%;
  1968. color: #003399;
  1969. font-weight: 600;
  1970. }
  1971. .org-todo {
  1972. color: #cc6666;Week-agenda:
  1973. font-weight: bold;
  1974. }
  1975. .org-done {
  1976. color: #339933;
  1977. }
  1978. .title { text-align: center; }
  1979. .todo, .deadline { color: red; }
  1980. .done { color: green; }
  1981. </style>
  1982. or, if you want to keep the style in a file,
  1983. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1984. As the value of this option simply gets inserted into the HTML <head> header,
  1985. you can \"misuse\" it to also add other text to the header. However,
  1986. <style>...</style> is required, if not present the variable will be ignored."
  1987. :group 'org-agenda-export
  1988. :group 'org-export-html
  1989. :type 'string)
  1990. (defgroup org-agenda-custom-commands nil
  1991. "Options concerning agenda views in Org-mode."
  1992. :tag "Org Agenda Custom Commands"
  1993. :group 'org-agenda)
  1994. (defcustom org-agenda-custom-commands nil
  1995. "Custom commands for the agenda.
  1996. These commands will be offered on the splash screen displayed by the
  1997. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1998. (key desc type match options files)
  1999. key The key (one or more characters as a string) to be associated
  2000. with the command.
  2001. desc A description of the commend, when omitted or nil, a default
  2002. description is built using MATCH.
  2003. type The command type, any of the following symbols:
  2004. todo Entries with a specific TODO keyword, in all agenda files.
  2005. tags Tags match in all agenda files.
  2006. tags-todo Tags match in all agenda files, TODO entries only.
  2007. todo-tree Sparse tree of specific TODO keyword in *current* file.
  2008. tags-tree Sparse tree with all tags matches in *current* file.
  2009. occur-tree Occur sparse tree for *current* file.
  2010. ... A user-defined function.
  2011. match What to search for:
  2012. - a single keyword for TODO keyword searches
  2013. - a tags match expression for tags searches
  2014. - a regular expression for occur searches
  2015. options A list of option settings, similar to that in a let form, so like
  2016. this: ((opt1 val1) (opt2 val2) ...)
  2017. files A list of files file to write the produced agenda buffer to
  2018. with the command `org-store-agenda-views'.
  2019. If a file name ends in \".html\", an HTML version of the buffer
  2020. is written out. If it ends in \".ps\", a postscript version is
  2021. produced. Otherwide, only the plain text is written to the file.
  2022. You can also define a set of commands, to create a composite agenda buffer.
  2023. In this case, an entry looks like this:
  2024. (key desc (cmd1 cmd2 ...) general-options file)
  2025. where
  2026. desc A description string to be displayed in the dispatcher menu.
  2027. cmd An agenda command, similar to the above. However, tree commands
  2028. are no allowed, but instead you can get agenda and global todo list.
  2029. So valid commands for a set are:
  2030. (agenda)
  2031. (alltodo)
  2032. (stuck)
  2033. (todo \"match\" options files)
  2034. (tags \"match\" options files)
  2035. (tags-todo \"match\" options files)
  2036. Each command can carry a list of options, and another set of options can be
  2037. given for the whole set of commands. Individual command options take
  2038. precedence over the general options.
  2039. When using several characters as key to a command, the first characters
  2040. are prefix commands. For the dispatcher to display useful information, you
  2041. should provide a description for the prefix, like
  2042. (setq org-agenda-custom-commands
  2043. '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
  2044. (\"hl\" tags \"+HOME+Lisa\")
  2045. (\"hp\" tags \"+HOME+Peter\")
  2046. (\"hk\" tags \"+HOME+Kim\")))"
  2047. :group 'org-agenda-custom-commands
  2048. :type '(repeat
  2049. (choice :value ("a" "" tags "" nil)
  2050. (list :tag "Single command"
  2051. (string :tag "Access Key(s) ")
  2052. (option (string :tag "Description"))
  2053. (choice
  2054. (const :tag "Agenda" agenda)
  2055. (const :tag "TODO list" alltodo)
  2056. (const :tag "Stuck projects" stuck)
  2057. (const :tag "Tags search (all agenda files)" tags)
  2058. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  2059. (const :tag "TODO keyword search (all agenda files)" todo)
  2060. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  2061. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  2062. (const :tag "Occur tree (current buffer)" occur-tree)
  2063. (sexp :tag "Other, user-defined function"))
  2064. (string :tag "Match")
  2065. (repeat :tag "Local options"
  2066. (list (variable :tag "Option") (sexp :tag "Value")))
  2067. (option (repeat :tag "Export" (file :tag "Export to"))))
  2068. (list :tag "Command series, all agenda files"
  2069. (string :tag "Access Key(s)")
  2070. (string :tag "Description ")
  2071. (repeat
  2072. (choice
  2073. (const :tag "Agenda" (agenda))
  2074. (const :tag "TODO list" (alltodo))
  2075. (const :tag "Stuck projects" (stuck))
  2076. (list :tag "Tags search"
  2077. (const :format "" tags)
  2078. (string :tag "Match")
  2079. (repeat :tag "Local options"
  2080. (list (variable :tag "Option")
  2081. (sexp :tag "Value"))))
  2082. (list :tag "Tags search, TODO entries only"
  2083. (const :format "" tags-todo)
  2084. (string :tag "Match")
  2085. (repeat :tag "Local options"
  2086. (list (variable :tag "Option")
  2087. (sexp :tag "Value"))))
  2088. (list :tag "TODO keyword search"
  2089. (const :format "" todo)
  2090. (string :tag "Match")
  2091. (repeat :tag "Local options"
  2092. (list (variable :tag "Option")
  2093. (sexp :tag "Value"))))
  2094. (list :tag "Other, user-defined function"
  2095. (symbol :tag "function")
  2096. (string :tag "Match")
  2097. (repeat :tag "Local options"
  2098. (list (variable :tag "Option")
  2099. (sexp :tag "Value"))))))
  2100. (repeat :tag "General options"
  2101. (list (variable :tag "Option")
  2102. (sexp :tag "Value")))
  2103. (option (repeat :tag "Export" (file :tag "Export to"))))
  2104. (cons :tag "Prefix key documentation"
  2105. (string :tag "Access Key(s)")
  2106. (string :tag "Description ")))))
  2107. (defcustom org-stuck-projects
  2108. '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
  2109. "How to identify stuck projects.
  2110. This is a list of four items:
  2111. 1. A tags/todo matcher string that is used to identify a project.
  2112. The entire tree below a headline matched by this is considered one project.
  2113. 2. A list of TODO keywords identifying non-stuck projects.
  2114. If the project subtree contains any headline with one of these todo
  2115. keywords, the project is considered to be not stuck. If you specify
  2116. \"*\" as a keyword, any TODO keyword will mark the project unstuck.
  2117. 3. A list of tags identifying non-stuck projects.
  2118. If the project subtree contains any headline with one of these tags,
  2119. the project is considered to be not stuck. If you specify \"*\" as
  2120. a tag, any tag will mark the project unstuck.
  2121. 4. An arbitrary regular expression matching non-stuck projects.
  2122. After defining this variable, you may use \\[org-agenda-list-stuck-projects]
  2123. or `C-c a #' to produce the list."
  2124. :group 'org-agenda-custom-commands
  2125. :type '(list
  2126. (string :tag "Tags/TODO match to identify a project")
  2127. (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
  2128. (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
  2129. (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
  2130. (defgroup org-agenda-skip nil
  2131. "Options concerning skipping parts of agenda files."
  2132. :tag "Org Agenda Skip"
  2133. :group 'org-agenda)
  2134. (defcustom org-agenda-todo-list-sublevels t
  2135. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  2136. When nil, the sublevels of a TODO entry are not checked, resulting in
  2137. potentially much shorter TODO lists."
  2138. :group 'org-agenda-skip
  2139. :group 'org-todo
  2140. :type 'boolean)
  2141. (defcustom org-agenda-todo-ignore-with-date nil
  2142. "Non-nil means, don't show entries with a date in the global todo list.
  2143. You can use this if you prefer to mark mere appointments with a TODO keyword,
  2144. but don't want them to show up in the TODO list.
  2145. When this is set, it also covers deadlines and scheduled items, the settings
  2146. of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
  2147. will be ignored."
  2148. :group 'org-agenda-skip
  2149. :group 'org-todo
  2150. :type 'boolean)
  2151. (defcustom org-agenda-todo-ignore-scheduled nil
  2152. "Non-nil means, don't show scheduled entries in the global todo list.
  2153. The idea behind this is that by scheduling it, you have already taken care
  2154. of this item.
  2155. See also `org-agenda-todo-ignore-with-date'."
  2156. :group 'org-agenda-skip
  2157. :group 'org-todo
  2158. :type 'boolean)
  2159. (defcustom org-agenda-todo-ignore-deadlines nil
  2160. "Non-nil means, don't show near deadline entries in the global todo list.
  2161. Near means closer than `org-deadline-warning-days' days.
  2162. The idea behind this is that such items will appear in the agenda anyway.
  2163. See also `org-agenda-todo-ignore-with-date'."
  2164. :group 'org-agenda-skip
  2165. :group 'org-todo
  2166. :type 'boolean)
  2167. (defcustom org-agenda-skip-scheduled-if-done nil
  2168. "Non-nil means don't show scheduled items in agenda when they are done.
  2169. This is relevant for the daily/weekly agenda, not for the TODO list. And
  2170. it applies only to the actual date of the scheduling. Warnings about
  2171. an item with a past scheduling dates are always turned off when the item
  2172. is DONE."
  2173. :group 'org-agenda-skip
  2174. :type 'boolean)
  2175. (defcustom org-agenda-skip-deadline-if-done nil
  2176. "Non-nil means don't show deadines when the corresponding item is done.
  2177. When nil, the deadline is still shown and should give you a happy feeling.
  2178. This is relevant for the daily/weekly agenda. And it applied only to the
  2179. actualy date of the deadline. Warnings about approching and past-due
  2180. deadlines are always turned off when the item is DONE."
  2181. :group 'org-agenda-skip
  2182. :type 'boolean)
  2183. (defcustom org-agenda-skip-timestamp-if-done nil
  2184. "Non-nil means don't select item by timestamp or -range if it is DONE."
  2185. :group 'org-agenda-skip
  2186. :type 'boolean)
  2187. (defcustom org-timeline-show-empty-dates 3
  2188. "Non-nil means, `org-timeline' also shows dates without an entry.
  2189. When nil, only the days which actually have entries are shown.
  2190. When t, all days between the first and the last date are shown.
  2191. When an integer, show also empty dates, but if there is a gap of more than
  2192. N days, just insert a special line indicating the size of the gap."
  2193. :group 'org-agenda-skip
  2194. :type '(choice
  2195. (const :tag "None" nil)
  2196. (const :tag "All" t)
  2197. (number :tag "at most")))
  2198. (defgroup org-agenda-startup nil
  2199. "Options concerning initial settings in the Agenda in Org Mode."
  2200. :tag "Org Agenda Startup"
  2201. :group 'org-agenda)
  2202. (defcustom org-finalize-agenda-hook nil
  2203. "Hook run just before displaying an agenda buffer."
  2204. :group 'org-agenda-startup
  2205. :type 'hook)
  2206. (defcustom org-agenda-mouse-1-follows-link nil
  2207. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  2208. A longer mouse click will still set point. Does not wortk on XEmacs.
  2209. Needs to be set before org.el is loaded."
  2210. :group 'org-agenda-startup
  2211. :type 'boolean)
  2212. (defcustom org-agenda-start-with-follow-mode nil
  2213. "The initial value of follow-mode in a newly created agenda window."
  2214. :group 'org-agenda-startup
  2215. :type 'boolean)
  2216. (defgroup org-agenda-windows nil
  2217. "Options concerning the windows used by the Agenda in Org Mode."
  2218. :tag "Org Agenda Windows"
  2219. :group 'org-agenda)
  2220. (defcustom org-agenda-window-setup 'reorganize-frame
  2221. "How the agenda buffer should be displayed.
  2222. Possible values for this option are:
  2223. current-window Show agenda in the current window, keeping all other windows.
  2224. other-frame Use `switch-to-buffer-other-frame' to display agenda.
  2225. other-window Use `switch-to-buffer-other-window' to display agenda.
  2226. reorganize-frame Show only two windows on the current frame, the current
  2227. window and the agenda.
  2228. See also the variable `org-agenda-restore-windows-after-quit'."
  2229. :group 'org-agenda-windows
  2230. :type '(choice
  2231. (const current-window)
  2232. (const other-frame)
  2233. (const other-window)
  2234. (const reorganize-frame)))
  2235. (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
  2236. "The min and max height of the agenda window as a fraction of frame height.
  2237. The value of the variable is a cons cell with two numbers between 0 and 1.
  2238. It only matters if `org-agenda-window-setup' is `reorganize-frame'."
  2239. :group 'org-agenda-windows
  2240. :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
  2241. (defcustom org-agenda-restore-windows-after-quit nil
  2242. "Non-nil means, restore window configuration open exiting agenda.
  2243. Before the window configuration is changed for displaying the agenda,
  2244. the current status is recorded. When the agenda is exited with
  2245. `q' or `x' and this option is set, the old state is restored. If
  2246. `org-agenda-window-setup' is `other-frame', the value of this
  2247. option will be ignored.."
  2248. :group 'org-agenda-windows
  2249. :type 'boolean)
  2250. (defcustom org-indirect-buffer-display 'other-window
  2251. "How should indirect tree buffers be displayed?
  2252. This applies to indirect buffers created with the commands
  2253. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  2254. Valid values are:
  2255. current-window Display in the current window
  2256. other-window Just display in another window.
  2257. dedicated-frame Create one new frame, and re-use it each time.
  2258. new-frame Make a new frame each time. Note that in this case
  2259. previously-made indirect buffers are kept, and you need to
  2260. kill these buffers yourself."
  2261. :group 'org-structure
  2262. :group 'org-agenda-windows
  2263. :type '(choice
  2264. (const :tag "In current window" current-window)
  2265. (const :tag "In current frame, other window" other-window)
  2266. (const :tag "Each time a new frame" new-frame)
  2267. (const :tag "One dedicated frame" dedicated-frame)))
  2268. (defgroup org-agenda-daily/weekly nil
  2269. "Options concerning the daily/weekly agenda."
  2270. :tag "Org Agenda Daily/Weekly"
  2271. :group 'org-agenda)
  2272. (defcustom org-agenda-ndays 7
  2273. "Number of days to include in overview display.
  2274. Should be 1 or 7."
  2275. :group 'org-agenda-daily/weekly
  2276. :type 'number)
  2277. (defcustom org-agenda-start-on-weekday 1
  2278. "Non-nil means, start the overview always on the specified weekday.
  2279. 0 denotes Sunday, 1 denotes Monday etc.
  2280. When nil, always start on the current day."
  2281. :group 'org-agenda-daily/weekly
  2282. :type '(choice (const :tag "Today" nil)
  2283. (number :tag "Weekday No.")))
  2284. (defcustom org-agenda-show-all-dates t
  2285. "Non-nil means, `org-agenda' shows every day in the selected range.
  2286. When nil, only the days which actually have entries are shown."
  2287. :group 'org-agenda-daily/weekly
  2288. :type 'boolean)
  2289. (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
  2290. "Format string for displaying dates in the agenda.
  2291. Used by the daily/weekly agenda and by the timeline. This should be
  2292. a format string understood by `format-time-string', or a function returning
  2293. the formatted date as a string. The function must take a single argument,
  2294. a calendar-style date list like (month day year)."
  2295. :group 'org-agenda-daily/weekly
  2296. :type '(choice
  2297. (string :tag "Format string")
  2298. (function :tag "Function")))
  2299. (defun org-agenda-format-date-aligned (date)
  2300. "Format a date string for display in the daily/weekly agenda, or timeline.
  2301. This function makes sure that dates are aligned for easy reading."
  2302. (format "%-9s %2d %s %4d"
  2303. (calendar-day-name date)
  2304. (extract-calendar-day date)
  2305. (calendar-month-name (extract-calendar-month date))
  2306. (extract-calendar-year date)))
  2307. (defcustom org-agenda-include-diary nil
  2308. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  2309. :group 'org-agenda-daily/weekly
  2310. :type 'boolean)
  2311. (defcustom org-agenda-include-all-todo nil
  2312. "Set means weekly/daily agenda will always contain all TODO entries.
  2313. The TODO entries will be listed at the top of the agenda, before
  2314. the entries for specific days."
  2315. :group 'org-agenda-daily/weekly
  2316. :type 'boolean)
  2317. (defcustom org-agenda-repeating-timestamp-show-all t
  2318. "Non-nil means, show all occurences of a repeating stamp in the agenda.
  2319. When nil, only one occurence is shown, either today or the
  2320. nearest into the future."
  2321. :group 'org-agenda-daily/weekly
  2322. :type 'boolean)
  2323. (defcustom org-deadline-warning-days 14
  2324. "No. of days before expiration during which a deadline becomes active.
  2325. This variable governs the display in sparse trees and in the agenda.
  2326. When negative, it means use this number (the absolute value of it)
  2327. even if a deadline has a different individual lead time specified."
  2328. :group 'org-time
  2329. :group 'org-agenda-daily/weekly
  2330. :type 'number)
  2331. (defcustom org-scheduled-past-days 10000
  2332. "No. of days to continue listing scheduled items that are not marked DONE.
  2333. When an item is scheduled on a date, it shows up in the agenda on this
  2334. day and will be listed until it is marked done for the number of days
  2335. given here."
  2336. :group 'org-agenda-daily/weekly
  2337. :type 'number)
  2338. (defgroup org-agenda-time-grid nil
  2339. "Options concerning the time grid in the Org-mode Agenda."
  2340. :tag "Org Agenda Time Grid"
  2341. :group 'org-agenda)
  2342. (defcustom org-agenda-use-time-grid t
  2343. "Non-nil means, show a time grid in the agenda schedule.
  2344. A time grid is a set of lines for specific times (like every two hours between
  2345. 8:00 and 20:00). The items scheduled for a day at specific times are
  2346. sorted in between these lines.
  2347. For details about when the grid will be shown, and what it will look like, see
  2348. the variable `org-agenda-time-grid'."
  2349. :group 'org-agenda-time-grid
  2350. :type 'boolean)
  2351. (defcustom org-agenda-time-grid
  2352. '((daily today require-timed)
  2353. "----------------"
  2354. (800 1000 1200 1400 1600 1800 2000))
  2355. "The settings for time grid for agenda display.
  2356. This is a list of three items. The first item is again a list. It contains
  2357. symbols specifying conditions when the grid should be displayed:
  2358. daily if the agenda shows a single day
  2359. weekly if the agenda shows an entire week
  2360. today show grid on current date, independent of daily/weekly display
  2361. require-timed show grid only if at least one item has a time specification
  2362. The second item is a string which will be places behing the grid time.
  2363. The third item is a list of integers, indicating the times that should have
  2364. a grid line."
  2365. :group 'org-agenda-time-grid
  2366. :type
  2367. '(list
  2368. (set :greedy t :tag "Grid Display Options"
  2369. (const :tag "Show grid in single day agenda display" daily)
  2370. (const :tag "Show grid in weekly agenda display" weekly)
  2371. (const :tag "Always show grid for today" today)
  2372. (const :tag "Show grid only if any timed entries are present"
  2373. require-timed)
  2374. (const :tag "Skip grid times already present in an entry"
  2375. remove-match))
  2376. (string :tag "Grid String")
  2377. (repeat :tag "Grid Times" (integer :tag "Time"))))
  2378. (defgroup org-agenda-sorting nil
  2379. "Options concerning sorting in the Org-mode Agenda."
  2380. :tag "Org Agenda Sorting"
  2381. :group 'org-agenda)
  2382. (defconst org-sorting-choice
  2383. '(choice
  2384. (const time-up) (const time-down)
  2385. (const category-keep) (const category-up) (const category-down)
  2386. (const tag-down) (const tag-up)
  2387. (const priority-up) (const priority-down))
  2388. "Sorting choices.")
  2389. (defcustom org-agenda-sorting-strategy
  2390. '((agenda time-up category-keep priority-down)
  2391. (todo category-keep priority-down)
  2392. (tags category-keep priority-down))
  2393. "Sorting structure for the agenda items of a single day.
  2394. This is a list of symbols which will be used in sequence to determine
  2395. if an entry should be listed before another entry. The following
  2396. symbols are recognized:
  2397. time-up Put entries with time-of-day indications first, early first
  2398. time-down Put entries with time-of-day indications first, late first
  2399. category-keep Keep the default order of categories, corresponding to the
  2400. sequence in `org-agenda-files'.
  2401. category-up Sort alphabetically by category, A-Z.
  2402. category-down Sort alphabetically by category, Z-A.
  2403. tag-up Sort alphabetically by last tag, A-Z.
  2404. tag-down Sort alphabetically by last tag, Z-A.
  2405. priority-up Sort numerically by priority, high priority last.
  2406. priority-down Sort numerically by priority, high priority first.
  2407. The different possibilities will be tried in sequence, and testing stops
  2408. if one comparison returns a \"not-equal\". For example, the default
  2409. '(time-up category-keep priority-down)
  2410. means: Pull out all entries having a specified time of day and sort them,
  2411. in order to make a time schedule for the current day the first thing in the
  2412. agenda listing for the day. Of the entries without a time indication, keep
  2413. the grouped in categories, don't sort the categories, but keep them in
  2414. the sequence given in `org-agenda-files'. Within each category sort by
  2415. priority.
  2416. Leaving out `category-keep' would mean that items will be sorted across
  2417. categories by priority.
  2418. Instead of a single list, this can also be a set of list for specific
  2419. contents, with a context symbol in the car of the list, any of
  2420. `agenda', `todo', `tags' for the corresponding agenda views."
  2421. :group 'org-agenda-sorting
  2422. :type `(choice
  2423. (repeat :tag "General" ,org-sorting-choice)
  2424. (list :tag "Individually"
  2425. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  2426. (repeat ,org-sorting-choice))
  2427. (cons (const :tag "Strategy for TODO lists" todo)
  2428. (repeat ,org-sorting-choice))
  2429. (cons (const :tag "Strategy for Tags matches" tags)
  2430. (repeat ,org-sorting-choice)))))
  2431. (defcustom org-sort-agenda-notime-is-late t
  2432. "Non-nil means, items without time are considered late.
  2433. This is only relevant for sorting. When t, items which have no explicit
  2434. time like 15:30 will be considered as 99:01, i.e. later than any items which
  2435. do have a time. When nil, the default time is before 0:00. You can use this
  2436. option to decide if the schedule for today should come before or after timeless
  2437. agenda entries."
  2438. :group 'org-agenda-sorting
  2439. :type 'boolean)
  2440. (defgroup org-agenda-line-format nil
  2441. "Options concerning the entry prefix in the Org-mode agenda display."
  2442. :tag "Org Agenda Line Format"
  2443. :group 'org-agenda)
  2444. (defcustom org-agenda-prefix-format
  2445. '((agenda . " %-12:c%?-12t% s")
  2446. (timeline . " % s")
  2447. (todo . " %-12:c")
  2448. (tags . " %-12:c"))
  2449. "Format specifications for the prefix of items in the agenda views.
  2450. An alist with four entries, for the different agenda types. The keys to the
  2451. sublists are `agenda', `timeline', `todo', and `tags'. The values
  2452. are format strings.
  2453. This format works similar to a printf format, with the following meaning:
  2454. %c the category of the item, \"Diary\" for entries from the diary, or
  2455. as given by the CATEGORY keyword or derived from the file name.
  2456. %T the *last* tag of the item. Last because inherited tags come
  2457. first in the list.
  2458. %t the time-of-day specification if one applies to the entry, in the
  2459. format HH:MM
  2460. %s Scheduling/Deadline information, a short string
  2461. All specifiers work basically like the standard `%s' of printf, but may
  2462. contain two additional characters: A question mark just after the `%' and
  2463. a whitespace/punctuation character just before the final letter.
  2464. If the first character after `%' is a question mark, the entire field
  2465. will only be included if the corresponding value applies to the
  2466. current entry. This is useful for fields which should have fixed
  2467. width when present, but zero width when absent. For example,
  2468. \"%?-12t\" will result in a 12 character time field if a time of the
  2469. day is specified, but will completely disappear in entries which do
  2470. not contain a time.
  2471. If there is punctuation or whitespace character just before the final
  2472. format letter, this character will be appended to the field value if
  2473. the value is not empty. For example, the format \"%-12:c\" leads to
  2474. \"Diary: \" if the category is \"Diary\". If the category were be
  2475. empty, no additional colon would be interted.
  2476. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  2477. - Indent the line with two space characters
  2478. - Give the category in a 12 chars wide field, padded with whitespace on
  2479. the right (because of `-'). Append a colon if there is a category
  2480. (because of `:').
  2481. - If there is a time-of-day, put it into a 12 chars wide field. If no
  2482. time, don't put in an empty field, just skip it (because of '?').
  2483. - Finally, put the scheduling information and append a whitespace.
  2484. As another example, if you don't want the time-of-day of entries in
  2485. the prefix, you could use:
  2486. (setq org-agenda-prefix-format \" %-11:c% s\")
  2487. See also the variables `org-agenda-remove-times-when-in-prefix' and
  2488. `org-agenda-remove-tags'."
  2489. :type '(choice
  2490. (string :tag "General format")
  2491. (list :greedy t :tag "View dependent"
  2492. (cons (const agenda) (string :tag "Format"))
  2493. (cons (const timeline) (string :tag "Format"))
  2494. (cons (const todo) (string :tag "Format"))
  2495. (cons (const tags) (string :tag "Format"))))
  2496. :group 'org-agenda-line-format)
  2497. (defvar org-prefix-format-compiled nil
  2498. "The compiled version of the most recently used prefix format.
  2499. See the variable `org-agenda-prefix-format'.")
  2500. (defcustom org-agenda-todo-keyword-format "%-1s"
  2501. "Format for the TODO keyword in agenda lines.
  2502. Set this to something like \"%-12s\" if you want all TODO keywords
  2503. to occupy a fixed space in the agenda display."
  2504. :group 'org-agenda-line-format
  2505. :type 'string)
  2506. (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
  2507. "Text preceeding scheduled items in the agenda view.
  2508. This is a list with two strings. The first applies when the item is
  2509. scheduled on the current day. The second applies when it has been scheduled
  2510. previously, it may contain a %d to capture how many days ago the item was
  2511. scheduled."
  2512. :group 'org-agenda-line-format
  2513. :type '(list
  2514. (string :tag "Scheduled today ")
  2515. (string :tag "Scheduled previously")))
  2516. (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
  2517. "Text preceeding deadline items in the agenda view.
  2518. This is a list with two strings. The first applies when the item has its
  2519. deadline on the current day. The second applies when it is in the past or
  2520. in the future, it may contain %d to capture how many days away the deadline
  2521. is (was)."
  2522. :group 'org-agenda-line-format
  2523. :type '(list
  2524. (string :tag "Deadline today ")
  2525. (string :tag "Deadline relative")))
  2526. (defcustom org-agenda-remove-times-when-in-prefix t
  2527. "Non-nil means, remove duplicate time specifications in agenda items.
  2528. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  2529. time-of-day specification in a headline or diary entry is extracted and
  2530. placed into the prefix. If this option is non-nil, the original specification
  2531. \(a timestamp or -range, or just a plain time(range) specification like
  2532. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  2533. cluttered.
  2534. The option can be t or nil. It may also be the symbol `beg', indicating
  2535. that the time should only be removed what it is located at the beginning of
  2536. the headline/diary entry."
  2537. :group 'org-agenda-line-format
  2538. :type '(choice
  2539. (const :tag "Always" t)
  2540. (const :tag "Never" nil)
  2541. (const :tag "When at beginning of entry" beg)))
  2542. (defcustom org-agenda-default-appointment-duration nil
  2543. "Default duration for appointments that only have a starting time.
  2544. When nil, no duration is specified in such cases.
  2545. When non-nil, this must be the number of minutes, e.g. 60 for one hour."
  2546. :group 'org-agenda-line-format
  2547. :type '(choice
  2548. (integer :tag "Minutes")
  2549. (const :tag "No default duration")))
  2550. (defcustom org-agenda-remove-tags nil
  2551. "Non-nil means, remove the tags from the headline copy in the agenda.
  2552. When this is the symbol `prefix', only remove tags when
  2553. `org-agenda-prefix-format' contains a `%T' specifier."
  2554. :group 'org-agenda-line-format
  2555. :type '(choice
  2556. (const :tag "Always" t)
  2557. (const :tag "Never" nil)
  2558. (const :tag "When prefix format contains %T" prefix)))
  2559. (if (fboundp 'defvaralias)
  2560. (defvaralias 'org-agenda-remove-tags-when-in-prefix
  2561. 'org-agenda-remove-tags))
  2562. (defcustom org-agenda-tags-column -80
  2563. "Shift tags in agenda items to this column.
  2564. If this number is positive, it specifies the column. If it is negative,
  2565. it means that the tags should be flushright to that column. For example,
  2566. -80 works well for a normal 80 character screen."
  2567. :group 'org-agenda-line-format
  2568. :type 'integer)
  2569. (if (fboundp 'defvaralias)
  2570. (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
  2571. (defcustom org-agenda-fontify-priorities t
  2572. "Non-nil means, highlight low and high priorities in agenda.
  2573. When t, the highest priority entries are bold, lowest priority italic.
  2574. This may also be an association list of priority faces. The face may be
  2575. a names face, or a list like `(:background \"Red\")'."
  2576. :group 'org-agenda-line-format
  2577. :type '(choice
  2578. (const :tag "Never" nil)
  2579. (const :tag "Defaults" t)
  2580. (repeat :tag "Specify"
  2581. (list (character :tag "Priority" :value ?A)
  2582. (sexp :tag "face")))))
  2583. (defgroup org-latex nil
  2584. "Options for embedding LaTeX code into Org-mode"
  2585. :tag "Org LaTeX"
  2586. :group 'org)
  2587. (defcustom org-format-latex-options
  2588. '(:foreground default :background default :scale 1.0
  2589. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2590. :matchers ("begin" "$" "$$" "\\(" "\\["))
  2591. "Options for creating images from LaTeX fragments.
  2592. This is a property list with the following properties:
  2593. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  2594. `default' means use the forground of the default face.
  2595. :background the background color, or \"Transparent\".
  2596. `default' means use the background of the default face.
  2597. :scale a scaling factor for the size of the images
  2598. :html-foreground, :html-background, :html-scale
  2599. The same numbers for HTML export.
  2600. :matchers a list indicating which matchers should be used to
  2601. find LaTeX fragments. Valid members of this list are:
  2602. \"begin\" find environments
  2603. \"$\" find math expressions surrounded by $...$
  2604. \"$$\" find math expressions surrounded by $$....$$
  2605. \"\\(\" find math expressions surrounded by \\(...\\)
  2606. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2607. :group 'org-latex
  2608. :type 'plist)
  2609. (defcustom org-format-latex-header "\\documentclass{article}
  2610. \\usepackage{fullpage} % do not remove
  2611. \\usepackage{amssymb}
  2612. \\usepackage[usenames]{color}
  2613. \\usepackage{amsmath}
  2614. \\usepackage{latexsym}
  2615. \\usepackage[mathscr]{eucal}
  2616. \\pagestyle{empty} % do not remove"
  2617. "The document header used for processing LaTeX fragments."
  2618. :group 'org-latex
  2619. :type 'string)
  2620. (defgroup org-export nil
  2621. "Options for exporting org-listings."
  2622. :tag "Org Export"
  2623. :group 'org)
  2624. (defgroup org-export-general nil
  2625. "General options for exporting Org-mode files."
  2626. :tag "Org Export General"
  2627. :group 'org-export)
  2628. ;; FIXME
  2629. (defvar org-export-publishing-directory nil)
  2630. (defcustom org-export-with-special-strings t
  2631. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2632. When this option is turned on, these strings will be exported as:
  2633. Org HTML LaTeX
  2634. -----+----------+--------
  2635. \\- &shy; \\-
  2636. -- &ndash; --
  2637. --- &mdash; ---
  2638. ... &hellip; \ldots
  2639. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2640. :group 'org-export-translation
  2641. :type 'boolean)
  2642. (defcustom org-export-language-setup
  2643. '(("en" "Author" "Date" "Table of Contents")
  2644. ("cs" "Autor" "Datum" "Obsah")
  2645. ("da" "Ophavsmand" "Dato" "Indhold")
  2646. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  2647. ("es" "Autor" "Fecha" "\xcdndice")
  2648. ("fr" "Auteur" "Date" "Table des mati\xe8res")
  2649. ("it" "Autore" "Data" "Indice")
  2650. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  2651. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  2652. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  2653. "Terms used in export text, translated to different languages.
  2654. Use the variable `org-export-default-language' to set the language,
  2655. or use the +OPTION lines for a per-file setting."
  2656. :group 'org-export-general
  2657. :type '(repeat
  2658. (list
  2659. (string :tag "HTML language tag")
  2660. (string :tag "Author")
  2661. (string :tag "Date")
  2662. (string :tag "Table of Contents"))))
  2663. (defcustom org-export-default-language "en"
  2664. "The default language of HTML export, as a string.
  2665. This should have an association in `org-export-language-setup'."
  2666. :group 'org-export-general
  2667. :type 'string)
  2668. (defcustom org-export-skip-text-before-1st-heading t
  2669. "Non-nil means, skip all text before the first headline when exporting.
  2670. When nil, that text is exported as well."
  2671. :group 'org-export-general
  2672. :type 'boolean)
  2673. (defcustom org-export-headline-levels 3
  2674. "The last level which is still exported as a headline.
  2675. Inferior levels will produce itemize lists when exported.
  2676. Note that a numeric prefix argument to an exporter function overrides
  2677. this setting.
  2678. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  2679. :group 'org-export-general
  2680. :type 'number)
  2681. (defcustom org-export-with-section-numbers t
  2682. "Non-nil means, add section numbers to headlines when exporting.
  2683. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  2684. :group 'org-export-general
  2685. :type 'boolean)
  2686. (defcustom org-export-with-toc t
  2687. "Non-nil means, create a table of contents in exported files.
  2688. The TOC contains headlines with levels up to`org-export-headline-levels'.
  2689. When an integer, include levels up to N in the toc, this may then be
  2690. different from `org-export-headline-levels', but it will not be allowed
  2691. to be larger than the number of headline levels.
  2692. When nil, no table of contents is made.
  2693. Headlines which contain any TODO items will be marked with \"(*)\" in
  2694. ASCII export, and with red color in HTML output, if the option
  2695. `org-export-mark-todo-in-toc' is set.
  2696. In HTML output, the TOC will be clickable.
  2697. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
  2698. or \"toc:3\"."
  2699. :group 'org-export-general
  2700. :type '(choice
  2701. (const :tag "No Table of Contents" nil)
  2702. (const :tag "Full Table of Contents" t)
  2703. (integer :tag "TOC to level")))
  2704. (defcustom org-export-mark-todo-in-toc nil
  2705. "Non-nil means, mark TOC lines that contain any open TODO items."
  2706. :group 'org-export-general
  2707. :type 'boolean)
  2708. (defcustom org-export-preserve-breaks nil
  2709. "Non-nil means, preserve all line breaks when exporting.
  2710. Normally, in HTML output paragraphs will be reformatted. In ASCII
  2711. export, line breaks will always be preserved, regardless of this variable.
  2712. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  2713. :group 'org-export-general
  2714. :type 'boolean)
  2715. (defcustom org-export-with-archived-trees 'headline
  2716. "Whether subtrees with the ARCHIVE tag should be exported.
  2717. This can have three different values
  2718. nil Do not export, pretend this tree is not present
  2719. t Do export the entire tree
  2720. headline Only export the headline, but skip the tree below it."
  2721. :group 'org-export-general
  2722. :group 'org-archive
  2723. :type '(choice
  2724. (const :tag "not at all" nil)
  2725. (const :tag "headline only" 'headline)
  2726. (const :tag "entirely" t)))
  2727. (defcustom org-export-author-info t
  2728. "Non-nil means, insert author name and email into the exported file.
  2729. This option can also be set with the +OPTIONS line,
  2730. e.g. \"author-info:nil\"."
  2731. :group 'org-export-general
  2732. :type 'boolean)
  2733. (defcustom org-export-time-stamp-file t
  2734. "Non-nil means, insert a time stamp into the exported file.
  2735. The time stamp shows when the file was created.
  2736. This option can also be set with the +OPTIONS line,
  2737. e.g. \"timestamp:nil\"."
  2738. :group 'org-export-general
  2739. :type 'boolean)
  2740. (defcustom org-export-with-timestamps t
  2741. "If nil, do not export time stamps and associated keywords."
  2742. :group 'org-export-general
  2743. :type 'boolean)
  2744. (defcustom org-export-remove-timestamps-from-toc t
  2745. "If nil, remove timestamps from the table of contents entries."
  2746. :group 'org-export-general
  2747. :type 'boolean)
  2748. (defcustom org-export-with-tags 'not-in-toc
  2749. "If nil, do not export tags, just remove them from headlines.
  2750. If this is the symbol `not-in-toc', tags will be removed from table of
  2751. contents entries, but still be shown in the headlines of the document.
  2752. This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
  2753. :group 'org-export-general
  2754. :type '(choice
  2755. (const :tag "Off" nil)
  2756. (const :tag "Not in TOC" not-in-toc)
  2757. (const :tag "On" t)))
  2758. (defcustom org-export-with-drawers nil
  2759. "Non-nil means, export with drawers like the property drawer.
  2760. When t, all drawers are exported. This may also be a list of
  2761. drawer names to export."
  2762. :group 'org-export-general
  2763. :type '(choice
  2764. (const :tag "All drawers" t)
  2765. (const :tag "None" nil)
  2766. (repeat :tag "Selected drawers"
  2767. (string :tag "Drawer name"))))
  2768. (defgroup org-export-translation nil
  2769. "Options for translating special ascii sequences for the export backends."
  2770. :tag "Org Export Translation"
  2771. :group 'org-export)
  2772. (defcustom org-export-with-emphasize t
  2773. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  2774. If the export target supports emphasizing text, the word will be
  2775. typeset in bold, italic, or underlined, respectively. Works only for
  2776. single words, but you can say: I *really* *mean* *this*.
  2777. Not all export backends support this.
  2778. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  2779. :group 'org-export-translation
  2780. :type 'boolean)
  2781. (defcustom org-export-with-footnotes t
  2782. "If nil, export [1] as a footnote marker.
  2783. Lines starting with [1] will be formatted as footnotes.
  2784. This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
  2785. :group 'org-export-translation
  2786. :type 'boolean)
  2787. (defcustom org-export-with-sub-superscripts t
  2788. "Non-nil means, interpret \"_\" and \"^\" for export.
  2789. When this option is turned on, you can use TeX-like syntax for sub- and
  2790. superscripts. Several characters after \"_\" or \"^\" will be
  2791. considered as a single item - so grouping with {} is normally not
  2792. needed. For example, the following things will be parsed as single
  2793. sub- or superscripts.
  2794. 10^24 or 10^tau several digits will be considered 1 item.
  2795. 10^-12 or 10^-tau a leading sign with digits or a word
  2796. x^2-y^3 will be read as x^2 - y^3, because items are
  2797. terminated by almost any nonword/nondigit char.
  2798. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  2799. Still, ambiguity is possible - so when in doubt use {} to enclose the
  2800. sub/superscript. If you set this variable to the symbol `{}',
  2801. the braces are *required* in order to trigger interpretations as
  2802. sub/superscript. This can be helpful in documents that need \"_\"
  2803. frequently in plain text.
  2804. Not all export backends support this, but HTML does.
  2805. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  2806. :group 'org-export-translation
  2807. :type '(choice
  2808. (const :tag "Always interpret" t)
  2809. (const :tag "Only with braces" {})
  2810. (const :tag "Never interpret" nil)))
  2811. (defcustom org-export-with-special-strings t
  2812. "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
  2813. When this option is turned on, these strings will be exported as:
  2814. \\- : &shy;
  2815. -- : &ndash;
  2816. --- : &mdash;
  2817. Not all export backends support this, but HTML does.
  2818. This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
  2819. :group 'org-export-translation
  2820. :type 'boolean)
  2821. (defcustom org-export-with-TeX-macros t
  2822. "Non-nil means, interpret simple TeX-like macros when exporting.
  2823. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  2824. No only real TeX macros will work here, but the standard HTML entities
  2825. for math can be used as macro names as well. For a list of supported
  2826. names in HTML export, see the constant `org-html-entities'.
  2827. Not all export backends support this.
  2828. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  2829. :group 'org-export-translation
  2830. :group 'org-export-latex
  2831. :type 'boolean)
  2832. (defcustom org-export-with-LaTeX-fragments nil
  2833. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  2834. When set, the exporter will find LaTeX environments if the \\begin line is
  2835. the first non-white thing on a line. It will also find the math delimiters
  2836. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  2837. display math.
  2838. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  2839. :group 'org-export-translation
  2840. :group 'org-export-latex
  2841. :type 'boolean)
  2842. (defcustom org-export-with-fixed-width t
  2843. "Non-nil means, lines starting with \":\" will be in fixed width font.
  2844. This can be used to have pre-formatted text, fragments of code etc. For
  2845. example:
  2846. : ;; Some Lisp examples
  2847. : (while (defc cnt)
  2848. : (ding))
  2849. will be looking just like this in also HTML. See also the QUOTE keyword.
  2850. Not all export backends support this.
  2851. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  2852. :group 'org-export-translation
  2853. :type 'boolean)
  2854. (defcustom org-match-sexp-depth 3
  2855. "Number of stacked braces for sub/superscript matching.
  2856. This has to be set before loading org.el to be effective."
  2857. :group 'org-export-translation
  2858. :type 'integer)
  2859. (defgroup org-export-tables nil
  2860. "Options for exporting tables in Org-mode."
  2861. :tag "Org Export Tables"
  2862. :group 'org-export)
  2863. (defcustom org-export-with-tables t
  2864. "If non-nil, lines starting with \"|\" define a table.
  2865. For example:
  2866. | Name | Address | Birthday |
  2867. |-------------+----------+-----------|
  2868. | Arthur Dent | England | 29.2.2100 |
  2869. Not all export backends support this.
  2870. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  2871. :group 'org-export-tables
  2872. :type 'boolean)
  2873. (defcustom org-export-highlight-first-table-line t
  2874. "Non-nil means, highlight the first table line.
  2875. In HTML export, this means use <th> instead of <td>.
  2876. In tables created with table.el, this applies to the first table line.
  2877. In Org-mode tables, all lines before the first horizontal separator
  2878. line will be formatted with <th> tags."
  2879. :group 'org-export-tables
  2880. :type 'boolean)
  2881. (defcustom org-export-table-remove-special-lines t
  2882. "Remove special lines and marking characters in calculating tables.
  2883. This removes the special marking character column from tables that are set
  2884. up for spreadsheet calculations. It also removes the entire lines
  2885. marked with `!', `_', or `^'. The lines with `$' are kept, because
  2886. the values of constants may be useful to have."
  2887. :group 'org-export-tables
  2888. :type 'boolean)
  2889. (defcustom org-export-prefer-native-exporter-for-tables nil
  2890. "Non-nil means, always export tables created with table.el natively.
  2891. Natively means, use the HTML code generator in table.el.
  2892. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  2893. the table does not use row- or column-spanning). This has the
  2894. advantage, that the automatic HTML conversions for math symbols and
  2895. sub/superscripts can be applied. Org-mode's HTML generator is also
  2896. much faster."
  2897. :group 'org-export-tables
  2898. :type 'boolean)
  2899. (defgroup org-export-ascii nil
  2900. "Options specific for ASCII export of Org-mode files."
  2901. :tag "Org Export ASCII"
  2902. :group 'org-export)
  2903. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  2904. "Characters for underlining headings in ASCII export.
  2905. In the given sequence, these characters will be used for level 1, 2, ..."
  2906. :group 'org-export-ascii
  2907. :type '(repeat character))
  2908. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  2909. "Bullet characters for headlines converted to lists in ASCII export.
  2910. The first character is used for the first lest level generated in this
  2911. way, and so on. If there are more levels than characters given here,
  2912. the list will be repeated.
  2913. Note that plain lists will keep the same bullets as the have in the
  2914. Org-mode file."
  2915. :group 'org-export-ascii
  2916. :type '(repeat character))
  2917. (defgroup org-export-xml nil
  2918. "Options specific for XML export of Org-mode files."
  2919. :tag "Org Export XML"
  2920. :group 'org-export)
  2921. (defgroup org-export-html nil
  2922. "Options specific for HTML export of Org-mode files."
  2923. :tag "Org Export HTML"
  2924. :group 'org-export)
  2925. (defcustom org-export-html-coding-system nil
  2926. ""
  2927. :group 'org-export-html
  2928. :type 'coding-system)
  2929. (defcustom org-export-html-extension "html"
  2930. "The extension for exported HTML files."
  2931. :group 'org-export-html
  2932. :type 'string)
  2933. (defcustom org-export-html-style
  2934. "<style type=\"text/css\">
  2935. html {
  2936. font-family: Times, serif;
  2937. font-size: 12pt;
  2938. }
  2939. .title { text-align: center; }
  2940. .todo { color: red; }
  2941. .done { color: green; }
  2942. .timestamp { color: grey }
  2943. .timestamp-kwd { color: CadetBlue }
  2944. .tag { background-color:lightblue; font-weight:normal }
  2945. .target { background-color: lavender; }
  2946. pre {
  2947. border: 1pt solid #AEBDCC;
  2948. background-color: #F3F5F7;
  2949. padding: 5pt;
  2950. font-family: courier, monospace;
  2951. }
  2952. table { border-collapse: collapse; }
  2953. td, th {
  2954. vertical-align: top;
  2955. <!--border: 1pt solid #ADB9CC;-->
  2956. }
  2957. </style>"
  2958. "The default style specification for exported HTML files.
  2959. Since there are different ways of setting style information, this variable
  2960. needs to contain the full HTML structure to provide a style, including the
  2961. surrounding HTML tags. The style specifications should include definitions
  2962. for new classes todo, done, title, and deadline. For example, legal values
  2963. would be:
  2964. <style type=\"text/css\">
  2965. p { font-weight: normal; color: gray; }
  2966. h1 { color: black; }
  2967. .title { text-align: center; }
  2968. .todo, .deadline { color: red; }
  2969. .done { color: green; }
  2970. </style>
  2971. or, if you want to keep the style in a file,
  2972. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  2973. As the value of this option simply gets inserted into the HTML <head> header,
  2974. you can \"misuse\" it to add arbitrary text to the header."
  2975. :group 'org-export-html
  2976. :type 'string)
  2977. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  2978. "Format for typesetting the document title in HTML export."
  2979. :group 'org-export-html
  2980. :type 'string)
  2981. (defcustom org-export-html-toplevel-hlevel 2
  2982. "The <H> level for level 1 headings in HTML export."
  2983. :group 'org-export-html
  2984. :type 'string)
  2985. (defcustom org-export-html-link-org-files-as-html t
  2986. "Non-nil means, make file links to `file.org' point to `file.html'.
  2987. When org-mode is exporting an org-mode file to HTML, links to
  2988. non-html files are directly put into a href tag in HTML.
  2989. However, links to other Org-mode files (recognized by the
  2990. extension `.org.) should become links to the corresponding html
  2991. file, assuming that the linked org-mode file will also be
  2992. converted to HTML.
  2993. When nil, the links still point to the plain `.org' file."
  2994. :group 'org-export-html
  2995. :type 'boolean)
  2996. (defcustom org-export-html-inline-images 'maybe
  2997. "Non-nil means, inline images into exported HTML pages.
  2998. This is done using an <img> tag. When nil, an anchor with href is used to
  2999. link to the image. If this option is `maybe', then images in links with
  3000. an empty description will be inlined, while images with a description will
  3001. be linked only."
  3002. :group 'org-export-html
  3003. :type '(choice (const :tag "Never" nil)
  3004. (const :tag "Always" t)
  3005. (const :tag "When there is no description" maybe)))
  3006. ;; FIXME: rename
  3007. (defcustom org-export-html-expand t
  3008. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  3009. When nil, these tags will be exported as plain text and therefore
  3010. not be interpreted by a browser.
  3011. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  3012. :group 'org-export-html
  3013. :type 'boolean)
  3014. (defcustom org-export-html-table-tag
  3015. "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
  3016. "The HTML tag that is used to start a table.
  3017. This must be a <table> tag, but you may change the options like
  3018. borders and spacing."
  3019. :group 'org-export-html
  3020. :type 'string)
  3021. (defcustom org-export-table-header-tags '("<th>" . "</th>")
  3022. "The opening tag for table header fields.
  3023. This is customizable so that alignment options can be specified."
  3024. :group 'org-export-tables
  3025. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3026. (defcustom org-export-table-data-tags '("<td>" . "</td>")
  3027. "The opening tag for table data fields.
  3028. This is customizable so that alignment options can be specified."
  3029. :group 'org-export-tables
  3030. :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
  3031. (defcustom org-export-html-with-timestamp nil
  3032. "If non-nil, write `org-export-html-html-helper-timestamp'
  3033. into the exported HTML text. Otherwise, the buffer will just be saved
  3034. to a file."
  3035. :group 'org-export-html
  3036. :type 'boolean)
  3037. (defcustom org-export-html-html-helper-timestamp
  3038. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  3039. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  3040. :group 'org-export-html
  3041. :type 'string)
  3042. (defgroup org-export-icalendar nil
  3043. "Options specific for iCalendar export of Org-mode files."
  3044. :tag "Org Export iCalendar"
  3045. :group 'org-export)
  3046. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  3047. "The file name for the iCalendar file covering all agenda files.
  3048. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  3049. The file name should be absolute, the file will be overwritten without warning."
  3050. :group 'org-export-icalendar
  3051. :type 'file)
  3052. (defcustom org-icalendar-include-todo nil
  3053. "Non-nil means, export to iCalendar files should also cover TODO items."
  3054. :group 'org-export-icalendar
  3055. :type '(choice
  3056. (const :tag "None" nil)
  3057. (const :tag "Unfinished" t)
  3058. (const :tag "All" all)))
  3059. (defcustom org-icalendar-include-sexps t
  3060. "Non-nil means, export to iCalendar files should also cover sexp entries.
  3061. These are entries like in the diary, but directly in an Org-mode file."
  3062. :group 'org-export-icalendar
  3063. :type 'boolean)
  3064. (defcustom org-icalendar-include-body 100
  3065. "Amount of text below headline to be included in iCalendar export.
  3066. This is a number of characters that should maximally be included.
  3067. Properties, scheduling and clocking lines will always be removed.
  3068. The text will be inserted into the DESCRIPTION field."
  3069. :group 'org-export-icalendar
  3070. :type '(choice
  3071. (const :tag "Nothing" nil)
  3072. (const :tag "Everything" t)
  3073. (integer :tag "Max characters")))
  3074. (defcustom org-icalendar-combined-name "OrgMode"
  3075. "Calendar name for the combined iCalendar representing all agenda files."
  3076. :group 'org-export-icalendar
  3077. :type 'string)
  3078. (defgroup org-font-lock nil
  3079. "Font-lock settings for highlighting in Org-mode."
  3080. :tag "Org Font Lock"
  3081. :group 'org)
  3082. (defcustom org-level-color-stars-only nil
  3083. "Non-nil means fontify only the stars in each headline.
  3084. When nil, the entire headline is fontified.
  3085. Changing it requires restart of `font-lock-mode' to become effective
  3086. also in regions already fontified."
  3087. :group 'org-font-lock
  3088. :type 'boolean)
  3089. (defcustom org-hide-leading-stars nil
  3090. "Non-nil means, hide the first N-1 stars in a headline.
  3091. This works by using the face `org-hide' for these stars. This
  3092. face is white for a light background, and black for a dark
  3093. background. You may have to customize the face `org-hide' to
  3094. make this work.
  3095. Changing it requires restart of `font-lock-mode' to become effective
  3096. also in regions already fontified.
  3097. You may also set this on a per-file basis by adding one of the following
  3098. lines to the buffer:
  3099. #+STARTUP: hidestars
  3100. #+STARTUP: showstars"
  3101. :group 'org-font-lock
  3102. :type 'boolean)
  3103. (defcustom org-fontify-done-headline nil
  3104. "Non-nil means, change the face of a headline if it is marked DONE.
  3105. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3106. When this is non-nil, the headline after the keyword is set to the
  3107. `org-headline-done' as an additional indication."
  3108. :group 'org-font-lock
  3109. :type 'boolean)
  3110. (defcustom org-fontify-emphasized-text t
  3111. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3112. Changing this variable requires a restart of Emacs to take effect."
  3113. :group 'org-font-lock
  3114. :type 'boolean)
  3115. (defcustom org-highlight-latex-fragments-and-specials nil
  3116. "Non-nil means, fontify what is treated specially by the exporters."
  3117. :group 'org-font-lock
  3118. :type 'boolean)
  3119. (defcustom org-hide-emphasis-markers nil
  3120. "Non-nil mean font-lock should hide the emphasis marker characters."
  3121. :group 'org-font-lock
  3122. :type 'boolean)
  3123. (defvar org-emph-re nil
  3124. "Regular expression for matching emphasis.")
  3125. (defvar org-verbatim-re nil
  3126. "Regular expression for matching verbatim text.")
  3127. (defvar org-emphasis-regexp-components) ; defined just below
  3128. (defvar org-emphasis-alist) ; defined just below
  3129. (defun org-set-emph-re (var val)
  3130. "Set variable and compute the emphasis regular expression."
  3131. (set var val)
  3132. (when (and (boundp 'org-emphasis-alist)
  3133. (boundp 'org-emphasis-regexp-components)
  3134. org-emphasis-alist org-emphasis-regexp-components)
  3135. (let* ((e org-emphasis-regexp-components)
  3136. (pre (car e))
  3137. (post (nth 1 e))
  3138. (border (nth 2 e))
  3139. (body (nth 3 e))
  3140. (nl (nth 4 e))
  3141. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  3142. (body1 (concat body "*?"))
  3143. (markers (mapconcat 'car org-emphasis-alist ""))
  3144. (vmarkers (mapconcat
  3145. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3146. org-emphasis-alist "")))
  3147. ;; make sure special characters appear at the right position in the class
  3148. (if (string-match "\\^" markers)
  3149. (setq markers (concat (replace-match "" t t markers) "^")))
  3150. (if (string-match "-" markers)
  3151. (setq markers (concat (replace-match "" t t markers) "-")))
  3152. (if (string-match "\\^" vmarkers)
  3153. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3154. (if (string-match "-" vmarkers)
  3155. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3156. (if (> nl 0)
  3157. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3158. (int-to-string nl) "\\}")))
  3159. ;; Make the regexp
  3160. (setq org-emph-re
  3161. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  3162. "\\("
  3163. "\\([" markers "]\\)"
  3164. "\\("
  3165. "[^" border "]\\|"
  3166. "[^" border (if (and nil stacked) markers) "]"
  3167. body1
  3168. "[^" border (if (and nil stacked) markers) "]"
  3169. "\\)"
  3170. "\\3\\)"
  3171. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  3172. (setq org-verbatim-re
  3173. (concat "\\([" pre "]\\|^\\)"
  3174. "\\("
  3175. "\\([" vmarkers "]\\)"
  3176. "\\("
  3177. "[^" border "]\\|"
  3178. "[^" border "]"
  3179. body1
  3180. "[^" border "]"
  3181. "\\)"
  3182. "\\3\\)"
  3183. "\\([" post "]\\|$\\)")))))
  3184. (defcustom org-emphasis-regexp-components
  3185. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  3186. "Components used to build the regular expression for emphasis.
  3187. This is a list with 6 entries. Terminology: In an emphasis string
  3188. like \" *strong word* \", we call the initial space PREMATCH, the final
  3189. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3190. and \"trong wor\" is the body. The different components in this variable
  3191. specify what is allowed/forbidden in each part:
  3192. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3193. post Chars allowed as postmatch. End of line will be allowed too.
  3194. border The chars *forbidden* as border characters.
  3195. body-regexp A regexp like \".\" to match a body character. Don't use
  3196. non-shy groups here, and don't allow newline here.
  3197. newline The maximum number of newlines allowed in an emphasis exp.
  3198. Use customize to modify this, or restart Emacs after changing it."
  3199. :group 'org-font-lock
  3200. :set 'org-set-emph-re
  3201. :type '(list
  3202. (sexp :tag "Allowed chars in pre ")
  3203. (sexp :tag "Allowed chars in post ")
  3204. (sexp :tag "Forbidden chars in border ")
  3205. (sexp :tag "Regexp for body ")
  3206. (integer :tag "number of newlines allowed")
  3207. (option (boolean :tag "Stacking (DISABLED) "))))
  3208. (defcustom org-emphasis-alist
  3209. '(("*" bold "<b>" "</b>")
  3210. ("/" italic "<i>" "</i>")
  3211. ("_" underline "<u>" "</u>")
  3212. ("=" org-code "<code>" "</code>" verbatim)
  3213. ("~" org-verbatim "" "" verbatim)
  3214. ("+" (:strike-through t) "<del>" "</del>")
  3215. )
  3216. "Special syntax for emphasized text.
  3217. Text starting and ending with a special character will be emphasized, for
  3218. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3219. characters, the face to be used by font-lock for highlighting in Org-mode
  3220. Emacs buffers, and the HTML tags to be used for this.
  3221. Use customize to modify this, or restart Emacs after changing it."
  3222. :group 'org-font-lock
  3223. :set 'org-set-emph-re
  3224. :type '(repeat
  3225. (list
  3226. (string :tag "Marker character")
  3227. (choice
  3228. (face :tag "Font-lock-face")
  3229. (plist :tag "Face property list"))
  3230. (string :tag "HTML start tag")
  3231. (string :tag "HTML end tag")
  3232. (option (const verbatim)))))
  3233. ;;; The faces
  3234. (defgroup org-faces nil
  3235. "Faces in Org-mode."
  3236. :tag "Org Faces"
  3237. :group 'org-font-lock)
  3238. (defun org-compatible-face (inherits specs)
  3239. "Make a compatible face specification.
  3240. If INHERITS is an existing face and if the Emacs version supports it,
  3241. just inherit the face. If not, use SPECS to define the face.
  3242. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  3243. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  3244. to the top of the list. The `min-colors' attribute will be removed from
  3245. any other entries, and any resulting duplicates will be removed entirely."
  3246. (cond
  3247. ((and inherits (facep inherits)
  3248. (not (featurep 'xemacs)) (> emacs-major-version 22))
  3249. ;; In Emacs 23, we use inheritance where possible.
  3250. ;; We only do this in Emacs 23, because only there the outline
  3251. ;; faces have been changed to the original org-mode-level-faces.
  3252. (list (list t :inherit inherits)))
  3253. ((or (featurep 'xemacs) (< emacs-major-version 22))
  3254. ;; These do not understand the `min-colors' attribute.
  3255. (let (r e a)
  3256. (while (setq e (pop specs))
  3257. (cond
  3258. ((memq (car e) '(t default)) (push e r))
  3259. ((setq a (member '(min-colors 8) (car e)))
  3260. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  3261. (cdr e)))))
  3262. ((setq a (assq 'min-colors (car e)))
  3263. (setq e (cons (delq a (car e)) (cdr e)))
  3264. (or (assoc (car e) r) (push e r)))
  3265. (t (or (assoc (car e) r) (push e r)))))
  3266. (nreverse r)))
  3267. (t specs)))
  3268. (put 'org-compatible-face 'lisp-indent-function 1)
  3269. (defface org-hide
  3270. '((((background light)) (:foreground "white"))
  3271. (((background dark)) (:foreground "black")))
  3272. "Face used to hide leading stars in headlines.
  3273. The forground color of this face should be equal to the background
  3274. color of the frame."
  3275. :group 'org-faces)
  3276. (defface org-level-1 ;; font-lock-function-name-face
  3277. (org-compatible-face 'outline-1
  3278. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3279. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3280. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3281. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3282. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3283. (t (:bold t))))
  3284. "Face used for level 1 headlines."
  3285. :group 'org-faces)
  3286. (defface org-level-2 ;; font-lock-variable-name-face
  3287. (org-compatible-face 'outline-2
  3288. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3289. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3290. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  3291. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  3292. (t (:bold t))))
  3293. "Face used for level 2 headlines."
  3294. :group 'org-faces)
  3295. (defface org-level-3 ;; font-lock-keyword-face
  3296. (org-compatible-face 'outline-3
  3297. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  3298. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  3299. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  3300. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  3301. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  3302. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  3303. (t (:bold t))))
  3304. "Face used for level 3 headlines."
  3305. :group 'org-faces)
  3306. (defface org-level-4 ;; font-lock-comment-face
  3307. (org-compatible-face 'outline-4
  3308. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3309. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3310. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  3311. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  3312. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3313. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3314. (t (:bold t))))
  3315. "Face used for level 4 headlines."
  3316. :group 'org-faces)
  3317. (defface org-level-5 ;; font-lock-type-face
  3318. (org-compatible-face 'outline-5
  3319. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  3320. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  3321. (((class color) (min-colors 8)) (:foreground "green"))))
  3322. "Face used for level 5 headlines."
  3323. :group 'org-faces)
  3324. (defface org-level-6 ;; font-lock-constant-face
  3325. (org-compatible-face 'outline-6
  3326. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  3327. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  3328. (((class color) (min-colors 8)) (:foreground "magenta"))))
  3329. "Face used for level 6 headlines."
  3330. :group 'org-faces)
  3331. (defface org-level-7 ;; font-lock-builtin-face
  3332. (org-compatible-face 'outline-7
  3333. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  3334. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  3335. (((class color) (min-colors 8)) (:foreground "blue"))))
  3336. "Face used for level 7 headlines."
  3337. :group 'org-faces)
  3338. (defface org-level-8 ;; font-lock-string-face
  3339. (org-compatible-face 'outline-8
  3340. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3341. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3342. (((class color) (min-colors 8)) (:foreground "green"))))
  3343. "Face used for level 8 headlines."
  3344. :group 'org-faces)
  3345. (defface org-special-keyword ;; font-lock-string-face
  3346. (org-compatible-face nil
  3347. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3348. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3349. (t (:italic t))))
  3350. "Face used for special keywords."
  3351. :group 'org-faces)
  3352. (defface org-drawer ;; font-lock-function-name-face
  3353. (org-compatible-face nil
  3354. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3355. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3356. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3357. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3358. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3359. (t (:bold t))))
  3360. "Face used for drawers."
  3361. :group 'org-faces)
  3362. (defface org-property-value nil
  3363. "Face used for the value of a property."
  3364. :group 'org-faces)
  3365. (defface org-column
  3366. (org-compatible-face nil
  3367. '((((class color) (min-colors 16) (background light))
  3368. (:background "grey90"))
  3369. (((class color) (min-colors 16) (background dark))
  3370. (:background "grey30"))
  3371. (((class color) (min-colors 8))
  3372. (:background "cyan" :foreground "black"))
  3373. (t (:inverse-video t))))
  3374. "Face for column display of entry properties."
  3375. :group 'org-faces)
  3376. (when (fboundp 'set-face-attribute)
  3377. ;; Make sure that a fixed-width face is used when we have a column table.
  3378. (set-face-attribute 'org-column nil
  3379. :height (face-attribute 'default :height)
  3380. :family (face-attribute 'default :family)))
  3381. (defface org-warning
  3382. (org-compatible-face 'font-lock-warning-face
  3383. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3384. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3385. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3386. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3387. (t (:bold t))))
  3388. "Face for deadlines and TODO keywords."
  3389. :group 'org-faces)
  3390. (defface org-archived ; similar to shadow
  3391. (org-compatible-face 'shadow
  3392. '((((class color grayscale) (min-colors 88) (background light))
  3393. (:foreground "grey50"))
  3394. (((class color grayscale) (min-colors 88) (background dark))
  3395. (:foreground "grey70"))
  3396. (((class color) (min-colors 8) (background light))
  3397. (:foreground "green"))
  3398. (((class color) (min-colors 8) (background dark))
  3399. (:foreground "yellow"))))
  3400. "Face for headline with the ARCHIVE tag."
  3401. :group 'org-faces)
  3402. (defface org-link
  3403. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3404. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3405. (t (:underline t)))
  3406. "Face for links."
  3407. :group 'org-faces)
  3408. (defface org-ellipsis
  3409. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  3410. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  3411. (t (:strike-through t)))
  3412. "Face for the ellipsis in folded text."
  3413. :group 'org-faces)
  3414. (defface org-target
  3415. '((((class color) (background light)) (:underline t))
  3416. (((class color) (background dark)) (:underline t))
  3417. (t (:underline t)))
  3418. "Face for links."
  3419. :group 'org-faces)
  3420. (defface org-date
  3421. '((((class color) (background light)) (:foreground "Purple" :underline t))
  3422. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  3423. (t (:underline t)))
  3424. "Face for links."
  3425. :group 'org-faces)
  3426. (defface org-sexp-date
  3427. '((((class color) (background light)) (:foreground "Purple"))
  3428. (((class color) (background dark)) (:foreground "Cyan"))
  3429. (t (:underline t)))
  3430. "Face for links."
  3431. :group 'org-faces)
  3432. (defface org-tag
  3433. '((t (:bold t)))
  3434. "Face for tags."
  3435. :group 'org-faces)
  3436. (defface org-todo ; font-lock-warning-face
  3437. (org-compatible-face nil
  3438. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  3439. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  3440. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  3441. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3442. (t (:inverse-video t :bold t))))
  3443. "Face for TODO keywords."
  3444. :group 'org-faces)
  3445. (defface org-done ;; font-lock-type-face
  3446. (org-compatible-face nil
  3447. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  3448. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  3449. (((class color) (min-colors 8)) (:foreground "green"))
  3450. (t (:bold t))))
  3451. "Face used for todo keywords that indicate DONE items."
  3452. :group 'org-faces)
  3453. (defface org-headline-done ;; font-lock-string-face
  3454. (org-compatible-face nil
  3455. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  3456. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  3457. (((class color) (min-colors 8) (background light)) (:bold nil))))
  3458. "Face used to indicate that a headline is DONE.
  3459. This face is only used if `org-fontify-done-headline' is set. If applies
  3460. to the part of the headline after the DONE keyword."
  3461. :group 'org-faces)
  3462. (defcustom org-todo-keyword-faces nil
  3463. "Faces for specific TODO keywords.
  3464. This is a list of cons cells, with TODO keywords in the car
  3465. and faces in the cdr. The face can be a symbol, or a property
  3466. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  3467. :group 'org-faces
  3468. :group 'org-todo
  3469. :type '(repeat
  3470. (cons
  3471. (string :tag "keyword")
  3472. (sexp :tag "face"))))
  3473. (defface org-table ;; font-lock-function-name-face
  3474. (org-compatible-face nil
  3475. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3476. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3477. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3478. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3479. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  3480. (((class color) (min-colors 8) (background dark)))))
  3481. "Face used for tables."
  3482. :group 'org-faces)
  3483. (defface org-formula
  3484. (org-compatible-face nil
  3485. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3486. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3487. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3488. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  3489. (t (:bold t :italic t))))
  3490. "Face for formulas."
  3491. :group 'org-faces)
  3492. (defface org-code
  3493. (org-compatible-face nil
  3494. '((((class color grayscale) (min-colors 88) (background light))
  3495. (:foreground "grey50"))
  3496. (((class color grayscale) (min-colors 88) (background dark))
  3497. (:foreground "grey70"))
  3498. (((class color) (min-colors 8) (background light))
  3499. (:foreground "green"))
  3500. (((class color) (min-colors 8) (background dark))
  3501. (:foreground "yellow"))))
  3502. "Face for fixed-with text like code snippets."
  3503. :group 'org-faces
  3504. :version "22.1")
  3505. (defface org-verbatim
  3506. (org-compatible-face nil
  3507. '((((class color grayscale) (min-colors 88) (background light))
  3508. (:foreground "grey50" :underline t))
  3509. (((class color grayscale) (min-colors 88) (background dark))
  3510. (:foreground "grey70" :underline t))
  3511. (((class color) (min-colors 8) (background light))
  3512. (:foreground "green" :underline t))
  3513. (((class color) (min-colors 8) (background dark))
  3514. (:foreground "yellow" :underline t))))
  3515. "Face for fixed-with text like code snippets."
  3516. :group 'org-faces
  3517. :version "22.1")
  3518. (defface org-agenda-structure ;; font-lock-function-name-face
  3519. (org-compatible-face nil
  3520. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  3521. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  3522. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  3523. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  3524. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  3525. (t (:bold t))))
  3526. "Face used in agenda for captions and dates."
  3527. :group 'org-faces)
  3528. (defface org-scheduled-today
  3529. (org-compatible-face nil
  3530. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  3531. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  3532. (((class color) (min-colors 8)) (:foreground "green"))
  3533. (t (:bold t :italic t))))
  3534. "Face for items scheduled for a certain day."
  3535. :group 'org-faces)
  3536. (defface org-scheduled-previously
  3537. (org-compatible-face nil
  3538. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3539. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3540. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3541. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3542. (t (:bold t))))
  3543. "Face for items scheduled previously, and not yet done."
  3544. :group 'org-faces)
  3545. (defface org-upcoming-deadline
  3546. (org-compatible-face nil
  3547. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  3548. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  3549. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  3550. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  3551. (t (:bold t))))
  3552. "Face for items scheduled previously, and not yet done."
  3553. :group 'org-faces)
  3554. (defcustom org-agenda-deadline-faces
  3555. '((1.0 . org-warning)
  3556. (0.5 . org-upcoming-deadline)
  3557. (0.0 . default))
  3558. "Faces for showing deadlines in the agenda.
  3559. This is a list of cons cells. The cdr of each cell is a face to be used,
  3560. and it can also just be like '(:foreground \"yellow\").
  3561. Each car is a fraction of the head-warning time that must have passed for
  3562. this the face in the cdr to be used for display. The numbers must be
  3563. given in descending order. The head-warning time is normally taken
  3564. from `org-deadline-warning-days', but can also be specified in the deadline
  3565. timestamp itself, like this:
  3566. DEADLINE: <2007-08-13 Mon -8d>
  3567. You may use d for days, w for weeks, m for months and y for years. Months
  3568. and years will only be treated in an approximate fashion (30.4 days for a
  3569. month and 365.24 days for a year)."
  3570. :group 'org-faces
  3571. :group 'org-agenda-daily/weekly
  3572. :type '(repeat
  3573. (cons
  3574. (number :tag "Fraction of head-warning time passed")
  3575. (sexp :tag "Face"))))
  3576. ;; FIXME: this is not a good face yet.
  3577. (defface org-agenda-restriction-lock
  3578. (org-compatible-face nil
  3579. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  3580. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  3581. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  3582. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  3583. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  3584. (t (:inverse-video t))))
  3585. "Face for showing the agenda restriction lock."
  3586. :group 'org-faces)
  3587. (defface org-time-grid ;; font-lock-variable-name-face
  3588. (org-compatible-face nil
  3589. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  3590. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  3591. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  3592. "Face used for time grids."
  3593. :group 'org-faces)
  3594. (defconst org-level-faces
  3595. '(org-level-1 org-level-2 org-level-3 org-level-4
  3596. org-level-5 org-level-6 org-level-7 org-level-8
  3597. ))
  3598. (defcustom org-n-level-faces (length org-level-faces)
  3599. "The number different faces to be used for headlines.
  3600. Org-mode defines 8 different headline faces, so this can be at most 8.
  3601. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  3602. :type 'number
  3603. :group 'org-faces)
  3604. ;;; Functions and variables from ther packages
  3605. ;; Declared here to avoid compiler warnings
  3606. (eval-and-compile
  3607. (unless (fboundp 'declare-function)
  3608. (defmacro declare-function (fn file &optional arglist fileonly))))
  3609. ;; XEmacs only
  3610. (defvar outline-mode-menu-heading)
  3611. (defvar outline-mode-menu-show)
  3612. (defvar outline-mode-menu-hide)
  3613. (defvar zmacs-regions) ; XEmacs regions
  3614. ;; Emacs only
  3615. (defvar mark-active)
  3616. ;; Various packages
  3617. ;; FIXME: get the argument lists for the UNKNOWN stuff
  3618. (declare-function add-to-diary-list "diary-lib"
  3619. (date string specifier &optional marker globcolor literal))
  3620. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3621. (declare-function Info-find-node "info" (filename nodename &optional no-going-back))
  3622. (declare-function Info-goto-node "info" (nodename &optional fork))
  3623. (declare-function bbdb "ext:bbdb-com" (string elidep))
  3624. (declare-function bbdb-company "ext:bbdb-com" (string elidep))
  3625. (declare-function bbdb-current-record "ext:bbdb-com" (&optional planning-on-modifying))
  3626. (declare-function bbdb-name "ext:bbdb-com" (string elidep))
  3627. (declare-function bbdb-record-getprop "ext:bbdb" (record property))
  3628. (declare-function bbdb-record-name "ext:bbdb" (record))
  3629. (declare-function bibtex-beginning-of-entry "bibtex" ())
  3630. (declare-function bibtex-generate-autokey "bibtex" ())
  3631. (declare-function bibtex-parse-entry "bibtex" (&optional content))
  3632. (declare-function bibtex-url "bibtex" (&optional pos no-browse))
  3633. (defvar calc-embedded-close-formula)
  3634. (defvar calc-embedded-open-formula)
  3635. (declare-function calendar-astro-date-string "cal-julian" (&optional date))
  3636. (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
  3637. (declare-function calendar-check-holidays "holidays" (date))
  3638. (declare-function calendar-chinese-date-string "cal-china" (&optional date))
  3639. (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
  3640. (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
  3641. (declare-function calendar-forward-day "cal-move" (arg))
  3642. (declare-function calendar-french-date-string "cal-french" (&optional date))
  3643. (declare-function calendar-goto-date "cal-move" (date))
  3644. (declare-function calendar-goto-today "cal-move" ())
  3645. (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
  3646. (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
  3647. (declare-function calendar-iso-date-string "cal-iso" (&optional date))
  3648. (declare-function calendar-julian-date-string "cal-julian" (&optional date))
  3649. (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
  3650. (declare-function calendar-persian-date-string "cal-persia" (&optional date))
  3651. (defvar calendar-mode-map)
  3652. (defvar original-date) ; dynamically scoped in calendar.el does scope this
  3653. (declare-function cdlatex-tab "ext:cdlatex" ())
  3654. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3655. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  3656. (declare-function elmo-message-entity-field "ext:elmo-msgdb" (entity field &optional type))
  3657. (declare-function elmo-message-field "ext:elmo" (folder number field &optional type) t)
  3658. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (&rest unknown) t)
  3659. (defvar font-lock-unfontify-region-function)
  3660. (declare-function gnus-article-show-summary "gnus-art" ())
  3661. (declare-function gnus-summary-last-subject "gnus-sum" ())
  3662. (defvar gnus-other-frame-object)
  3663. (defvar gnus-group-name)
  3664. (defvar gnus-article-current)
  3665. (defvar Info-current-file)
  3666. (defvar Info-current-node)
  3667. (declare-function mh-display-msg "mh-show" (msg-num folder-name))
  3668. (declare-function mh-find-path "mh-utils" ())
  3669. (declare-function mh-get-header-field "mh-utils" (field))
  3670. (declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
  3671. (declare-function mh-header-display "mh-show" ())
  3672. (declare-function mh-index-previous-folder "mh-search" ())
  3673. (declare-function mh-normalize-folder-name "mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty))
  3674. (declare-function mh-search "mh-search" (folder search-regexp &optional redo-search-flag window-config))
  3675. (declare-function mh-search-choose "mh-search" (&optional searcher))
  3676. (declare-function mh-show "mh-show" (&optional message redisplay-flag))
  3677. (declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
  3678. (declare-function mh-show-header-display "mh-show" t t)
  3679. (declare-function mh-show-msg "mh-show" (msg))
  3680. (declare-function mh-show-show "mh-show" t t)
  3681. (declare-function mh-visit-folder "mh-folder" (folder &optional range index-data))
  3682. (defvar mh-progs)
  3683. (defvar mh-current-folder)
  3684. (defvar mh-show-folder-buffer)
  3685. (defvar mh-index-folder)
  3686. (defvar mh-searcher)
  3687. (declare-function org-export-latex-cleaned-string "org-export-latex" ())
  3688. (declare-function parse-time-string "parse-time" (string))
  3689. (declare-function remember "remember" (&optional initial))
  3690. (declare-function remember-buffer-desc "remember" ())
  3691. (defvar remember-save-after-remembering)
  3692. (defvar remember-data-file)
  3693. (defvar remember-register)
  3694. (defvar remember-buffer)
  3695. (defvar remember-handler-functions)
  3696. (defvar remember-annotation-functions)
  3697. (declare-function rmail-narrow-to-non-pruned-header "rmail" ())
  3698. (declare-function rmail-show-message "rmail" (&optional n no-summary))
  3699. (declare-function rmail-what-message "rmail" ())
  3700. (defvar texmathp-why)
  3701. (declare-function vm-beginning-of-message "ext:vm-page" ())
  3702. (declare-function vm-follow-summary-cursor "ext:vm-motion" ())
  3703. (declare-function vm-get-header-contents "ext:vm-summary" (message header-name-regexp &optional clump-sep))
  3704. (declare-function vm-isearch-narrow "ext:vm-search" ())
  3705. (declare-function vm-isearch-update "ext:vm-search" ())
  3706. (declare-function vm-select-folder-buffer "ext:vm-macro" ())
  3707. (declare-function vm-su-message-id "ext:vm-summary" (m))
  3708. (declare-function vm-su-subject "ext:vm-summary" (m))
  3709. (declare-function vm-summarize "ext:vm-summary" (&optional display raise))
  3710. (defvar vm-message-pointer)
  3711. (defvar vm-folder-directory)
  3712. (defvar w3m-current-url)
  3713. (defvar w3m-current-title)
  3714. ;; backward compatibility to old version of wl
  3715. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" (&rest unknown) t)
  3716. (declare-function wl-folder-get-elmo-folder "ext:wl-folder" (entity &optional no-cache))
  3717. (declare-function wl-summary-goto-folder-subr "ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit))
  3718. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary" (&optional id))
  3719. (declare-function wl-summary-line-from "ext:wl-summary" ())
  3720. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  3721. (declare-function wl-summary-message-number "ext:wl-summary" ())
  3722. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  3723. (defvar wl-summary-buffer-elmo-folder)
  3724. (defvar wl-summary-buffer-folder-name)
  3725. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3726. (defvar org-latex-regexps)
  3727. (defvar constants-unit-system)
  3728. ;;; Variables for pre-computed regular expressions, all buffer local
  3729. (defvar org-drawer-regexp nil
  3730. "Matches first line of a hidden block.")
  3731. (make-variable-buffer-local 'org-drawer-regexp)
  3732. (defvar org-todo-regexp nil
  3733. "Matches any of the TODO state keywords.")
  3734. (make-variable-buffer-local 'org-todo-regexp)
  3735. (defvar org-not-done-regexp nil
  3736. "Matches any of the TODO state keywords except the last one.")
  3737. (make-variable-buffer-local 'org-not-done-regexp)
  3738. (defvar org-todo-line-regexp nil
  3739. "Matches a headline and puts TODO state into group 2 if present.")
  3740. (make-variable-buffer-local 'org-todo-line-regexp)
  3741. (defvar org-complex-heading-regexp nil
  3742. "Matches a headline and puts everything into groups:
  3743. group 1: the stars
  3744. group 2: The todo keyword, maybe
  3745. group 3: Priority cookie
  3746. group 4: True headline
  3747. group 5: Tags")
  3748. (make-variable-buffer-local 'org-complex-heading-regexp)
  3749. (defvar org-todo-line-tags-regexp nil
  3750. "Matches a headline and puts TODO state into group 2 if present.
  3751. Also put tags into group 4 if tags are present.")
  3752. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3753. (defvar org-nl-done-regexp nil
  3754. "Matches newline followed by a headline with the DONE keyword.")
  3755. (make-variable-buffer-local 'org-nl-done-regexp)
  3756. (defvar org-looking-at-done-regexp nil
  3757. "Matches the DONE keyword a point.")
  3758. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3759. (defvar org-ds-keyword-length 12
  3760. "Maximum length of the Deadline and SCHEDULED keywords.")
  3761. (make-variable-buffer-local 'org-ds-keyword-length)
  3762. (defvar org-deadline-regexp nil
  3763. "Matches the DEADLINE keyword.")
  3764. (make-variable-buffer-local 'org-deadline-regexp)
  3765. (defvar org-deadline-time-regexp nil
  3766. "Matches the DEADLINE keyword together with a time stamp.")
  3767. (make-variable-buffer-local 'org-deadline-time-regexp)
  3768. (defvar org-deadline-line-regexp nil
  3769. "Matches the DEADLINE keyword and the rest of the line.")
  3770. (make-variable-buffer-local 'org-deadline-line-regexp)
  3771. (defvar org-scheduled-regexp nil
  3772. "Matches the SCHEDULED keyword.")
  3773. (make-variable-buffer-local 'org-scheduled-regexp)
  3774. (defvar org-scheduled-time-regexp nil
  3775. "Matches the SCHEDULED keyword together with a time stamp.")
  3776. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3777. (defvar org-closed-time-regexp nil
  3778. "Matches the CLOSED keyword together with a time stamp.")
  3779. (make-variable-buffer-local 'org-closed-time-regexp)
  3780. (defvar org-keyword-time-regexp nil
  3781. "Matches any of the 4 keywords, together with the time stamp.")
  3782. (make-variable-buffer-local 'org-keyword-time-regexp)
  3783. (defvar org-keyword-time-not-clock-regexp nil
  3784. "Matches any of the 3 keywords, together with the time stamp.")
  3785. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3786. (defvar org-maybe-keyword-time-regexp nil
  3787. "Matches a timestamp, possibly preceeded by a keyword.")
  3788. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3789. (defvar org-planning-or-clock-line-re nil
  3790. "Matches a line with planning or clock info.")
  3791. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3792. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  3793. rear-nonsticky t mouse-map t fontified t)
  3794. "Properties to remove when a string without properties is wanted.")
  3795. (defsubst org-match-string-no-properties (num &optional string)
  3796. (if (featurep 'xemacs)
  3797. (let ((s (match-string num string)))
  3798. (remove-text-properties 0 (length s) org-rm-props s)
  3799. s)
  3800. (match-string-no-properties num string)))
  3801. (defsubst org-no-properties (s)
  3802. (if (fboundp 'set-text-properties)
  3803. (set-text-properties 0 (length s) nil s)
  3804. (remove-text-properties 0 (length s) org-rm-props s))
  3805. s)
  3806. (defsubst org-get-alist-option (option key)
  3807. (cond ((eq key t) t)
  3808. ((eq option t) t)
  3809. ((assoc key option) (cdr (assoc key option)))
  3810. (t (cdr (assq 'default option)))))
  3811. (defsubst org-inhibit-invisibility ()
  3812. "Modified `buffer-invisibility-spec' for Emacs 21.
  3813. Some ops with invisible text do not work correctly on Emacs 21. For these
  3814. we turn off invisibility temporarily. Use this in a `let' form."
  3815. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  3816. (defsubst org-set-local (var value)
  3817. "Make VAR local in current buffer and set it to VALUE."
  3818. (set (make-variable-buffer-local var) value))
  3819. (defsubst org-mode-p ()
  3820. "Check if the current buffer is in Org-mode."
  3821. (eq major-mode 'org-mode))
  3822. (defsubst org-last (list)
  3823. "Return the last element of LIST."
  3824. (car (last list)))
  3825. (defun org-let (list &rest body)
  3826. (eval (cons 'let (cons list body))))
  3827. (put 'org-let 'lisp-indent-function 1)
  3828. (defun org-let2 (list1 list2 &rest body)
  3829. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  3830. (put 'org-let2 'lisp-indent-function 2)
  3831. (defconst org-startup-options
  3832. '(("fold" org-startup-folded t)
  3833. ("overview" org-startup-folded t)
  3834. ("nofold" org-startup-folded nil)
  3835. ("showall" org-startup-folded nil)
  3836. ("content" org-startup-folded content)
  3837. ("hidestars" org-hide-leading-stars t)
  3838. ("showstars" org-hide-leading-stars nil)
  3839. ("odd" org-odd-levels-only t)
  3840. ("oddeven" org-odd-levels-only nil)
  3841. ("align" org-startup-align-all-tables t)
  3842. ("noalign" org-startup-align-all-tables nil)
  3843. ("customtime" org-display-custom-times t)
  3844. ("logging" org-log-done t)
  3845. ("logdone" org-log-done t)
  3846. ("nologging" org-log-done nil)
  3847. ("lognotedone" org-log-done done push)
  3848. ("lognotestate" org-log-done state push)
  3849. ("lognoteclock-out" org-log-done clock-out push)
  3850. ("logrepeat" org-log-repeat t)
  3851. ("nologrepeat" org-log-repeat nil)
  3852. ("constcgs" constants-unit-system cgs)
  3853. ("constSI" constants-unit-system SI))
  3854. "Variable associated with STARTUP options for org-mode.
  3855. Each element is a list of three items: The startup options as written
  3856. in the #+STARTUP line, the corresponding variable, and the value to
  3857. set this variable to if the option is found. An optional forth element PUSH
  3858. means to push this value onto the list in the variable.")
  3859. (defun org-set-regexps-and-options ()
  3860. "Precompute regular expressions for current buffer."
  3861. (when (org-mode-p)
  3862. (org-set-local 'org-todo-kwd-alist nil)
  3863. (org-set-local 'org-todo-key-alist nil)
  3864. (org-set-local 'org-todo-key-trigger nil)
  3865. (org-set-local 'org-todo-keywords-1 nil)
  3866. (org-set-local 'org-done-keywords nil)
  3867. (org-set-local 'org-todo-heads nil)
  3868. (org-set-local 'org-todo-sets nil)
  3869. (org-set-local 'org-todo-log-states nil)
  3870. (let ((re (org-make-options-regexp
  3871. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  3872. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  3873. "CONSTANTS" "PROPERTY" "DRAWERS")))
  3874. (splitre "[ \t]+")
  3875. kwds kws0 kwsa key value cat arch tags const links hw dws
  3876. tail sep kws1 prio props drawers
  3877. ex log)
  3878. (save-excursion
  3879. (save-restriction
  3880. (widen)
  3881. (goto-char (point-min))
  3882. (while (re-search-forward re nil t)
  3883. (setq key (match-string 1) value (org-match-string-no-properties 2))
  3884. (cond
  3885. ((equal key "CATEGORY")
  3886. (if (string-match "[ \t]+$" value)
  3887. (setq value (replace-match "" t t value)))
  3888. (setq cat value))
  3889. ((member key '("SEQ_TODO" "TODO"))
  3890. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3891. ((equal key "TYP_TODO")
  3892. (push (cons 'type (org-split-string value splitre)) kwds))
  3893. ((equal key "TAGS")
  3894. (setq tags (append tags (org-split-string value splitre))))
  3895. ((equal key "COLUMNS")
  3896. (org-set-local 'org-columns-default-format value))
  3897. ((equal key "LINK")
  3898. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3899. (push (cons (match-string 1 value)
  3900. (org-trim (match-string 2 value)))
  3901. links)))
  3902. ((equal key "PRIORITIES")
  3903. (setq prio (org-split-string value " +")))
  3904. ((equal key "PROPERTY")
  3905. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3906. (push (cons (match-string 1 value) (match-string 2 value))
  3907. props)))
  3908. ((equal key "DRAWERS")
  3909. (setq drawers (org-split-string value splitre)))
  3910. ((equal key "CONSTANTS")
  3911. (setq const (append const (org-split-string value splitre))))
  3912. ((equal key "STARTUP")
  3913. (let ((opts (org-split-string value splitre))
  3914. l var val)
  3915. (while (setq l (pop opts))
  3916. (when (setq l (assoc l org-startup-options))
  3917. (setq var (nth 1 l) val (nth 2 l))
  3918. (if (not (nth 3 l))
  3919. (set (make-local-variable var) val)
  3920. (if (not (listp (symbol-value var)))
  3921. (set (make-local-variable var) nil))
  3922. (set (make-local-variable var) (symbol-value var))
  3923. (add-to-list var val))))))
  3924. ((equal key "ARCHIVE")
  3925. (string-match " *$" value)
  3926. (setq arch (replace-match "" t t value))
  3927. (remove-text-properties 0 (length arch)
  3928. '(face t fontified t) arch)))
  3929. )))
  3930. (when cat
  3931. (org-set-local 'org-category (intern cat))
  3932. (push (cons "CATEGORY" cat) props))
  3933. (when prio
  3934. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3935. (setq prio (mapcar 'string-to-char prio))
  3936. (org-set-local 'org-highest-priority (nth 0 prio))
  3937. (org-set-local 'org-lowest-priority (nth 1 prio))
  3938. (org-set-local 'org-default-priority (nth 2 prio)))
  3939. (and props (org-set-local 'org-local-properties (nreverse props)))
  3940. (and drawers (org-set-local 'org-drawers drawers))
  3941. (and arch (org-set-local 'org-archive-location arch))
  3942. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3943. ;; Process the TODO keywords
  3944. (unless kwds
  3945. ;; Use the global values as if they had been given locally.
  3946. (setq kwds (default-value 'org-todo-keywords))
  3947. (if (stringp (car kwds))
  3948. (setq kwds (list (cons org-todo-interpretation
  3949. (default-value 'org-todo-keywords)))))
  3950. (setq kwds (reverse kwds)))
  3951. (setq kwds (nreverse kwds))
  3952. (let (inter kws kw)
  3953. (while (setq kws (pop kwds))
  3954. (setq inter (pop kws) sep (member "|" kws)
  3955. kws0 (delete "|" (copy-sequence kws))
  3956. kwsa nil
  3957. kws1 (mapcar
  3958. (lambda (x)
  3959. (if (string-match "^\\(.*?\\)\\(?:(\\(..?\\))\\)?$" x)
  3960. (progn
  3961. (setq kw (match-string 1 x)
  3962. ex (and (match-end 2) (match-string 2 x))
  3963. log (and ex (string-match "@" ex))
  3964. key (and ex (substring ex 0 1)))
  3965. (if (equal key "@") (setq key nil))
  3966. (push (cons kw (and key (string-to-char key))) kwsa)
  3967. (and log (push kw org-todo-log-states))
  3968. kw)
  3969. (error "Invalid TODO keyword %s" x)))
  3970. kws0)
  3971. kwsa (if kwsa (append '((:startgroup))
  3972. (nreverse kwsa)
  3973. '((:endgroup))))
  3974. hw (car kws1)
  3975. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3976. tail (list inter hw (car dws) (org-last dws)))
  3977. (add-to-list 'org-todo-heads hw 'append)
  3978. (push kws1 org-todo-sets)
  3979. (setq org-done-keywords (append org-done-keywords dws nil))
  3980. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3981. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3982. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3983. (setq org-todo-sets (nreverse org-todo-sets)
  3984. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3985. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3986. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3987. ;; Process the constants
  3988. (when const
  3989. (let (e cst)
  3990. (while (setq e (pop const))
  3991. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3992. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3993. (setq org-table-formula-constants-local cst)))
  3994. ;; Process the tags.
  3995. (when tags
  3996. (let (e tgs)
  3997. (while (setq e (pop tags))
  3998. (cond
  3999. ((equal e "{") (push '(:startgroup) tgs))
  4000. ((equal e "}") (push '(:endgroup) tgs))
  4001. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  4002. (push (cons (match-string 1 e)
  4003. (string-to-char (match-string 2 e)))
  4004. tgs))
  4005. (t (push (list e) tgs))))
  4006. (org-set-local 'org-tag-alist nil)
  4007. (while (setq e (pop tgs))
  4008. (or (and (stringp (car e))
  4009. (assoc (car e) org-tag-alist))
  4010. (push e org-tag-alist))))))
  4011. ;; Compute the regular expressions and other local variables
  4012. (if (not org-done-keywords)
  4013. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  4014. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4015. (length org-scheduled-string)))
  4016. org-drawer-regexp
  4017. (concat "^[ \t]*:\\("
  4018. (mapconcat 'regexp-quote org-drawers "\\|")
  4019. "\\):[ \t]*$")
  4020. org-not-done-keywords
  4021. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4022. org-todo-regexp
  4023. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  4024. "\\|") "\\)\\>")
  4025. org-not-done-regexp
  4026. (concat "\\<\\("
  4027. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4028. "\\)\\>")
  4029. org-todo-line-regexp
  4030. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4031. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4032. "\\)\\>\\)?[ \t]*\\(.*\\)")
  4033. org-complex-heading-regexp
  4034. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  4035. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4036. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  4037. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4038. org-nl-done-regexp
  4039. (concat "\n\\*+[ \t]+"
  4040. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4041. "\\)" "\\>")
  4042. org-todo-line-tags-regexp
  4043. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4044. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4045. (org-re
  4046. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  4047. org-looking-at-done-regexp
  4048. (concat "^" "\\(?:"
  4049. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4050. "\\>")
  4051. org-deadline-regexp (concat "\\<" org-deadline-string)
  4052. org-deadline-time-regexp
  4053. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4054. org-deadline-line-regexp
  4055. (concat "\\<\\(" org-deadline-string "\\).*")
  4056. org-scheduled-regexp
  4057. (concat "\\<" org-scheduled-string)
  4058. org-scheduled-time-regexp
  4059. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4060. org-closed-time-regexp
  4061. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4062. org-keyword-time-regexp
  4063. (concat "\\<\\(" org-scheduled-string
  4064. "\\|" org-deadline-string
  4065. "\\|" org-closed-string
  4066. "\\|" org-clock-string "\\)"
  4067. " *[[<]\\([^]>]+\\)[]>]")
  4068. org-keyword-time-not-clock-regexp
  4069. (concat "\\<\\(" org-scheduled-string
  4070. "\\|" org-deadline-string
  4071. "\\|" org-closed-string
  4072. "\\)"
  4073. " *[[<]\\([^]>]+\\)[]>]")
  4074. org-maybe-keyword-time-regexp
  4075. (concat "\\(\\<\\(" org-scheduled-string
  4076. "\\|" org-deadline-string
  4077. "\\|" org-closed-string
  4078. "\\|" org-clock-string "\\)\\)?"
  4079. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4080. org-planning-or-clock-line-re
  4081. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4082. "\\|" org-deadline-string
  4083. "\\|" org-closed-string "\\|" org-clock-string
  4084. "\\)\\>\\)")
  4085. )
  4086. (org-compute-latex-and-specials-regexp)
  4087. (org-set-font-lock-defaults)))
  4088. (defun org-remove-keyword-keys (list)
  4089. (mapcar (lambda (x)
  4090. (if (string-match "(..?)$" x)
  4091. (substring x 0 (match-beginning 0))
  4092. x))
  4093. list))
  4094. ;; FIXME: this could be done much better, using second characters etc.
  4095. (defun org-assign-fast-keys (alist)
  4096. "Assign fast keys to a keyword-key alist.
  4097. Respect keys that are already there."
  4098. (let (new e k c c1 c2 (char ?a))
  4099. (while (setq e (pop alist))
  4100. (cond
  4101. ((equal e '(:startgroup)) (push e new))
  4102. ((equal e '(:endgroup)) (push e new))
  4103. (t
  4104. (setq k (car e) c2 nil)
  4105. (if (cdr e)
  4106. (setq c (cdr e))
  4107. ;; automatically assign a character.
  4108. (setq c1 (string-to-char
  4109. (downcase (substring
  4110. k (if (= (string-to-char k) ?@) 1 0)))))
  4111. (if (or (rassoc c1 new) (rassoc c1 alist))
  4112. (while (or (rassoc char new) (rassoc char alist))
  4113. (setq char (1+ char)))
  4114. (setq c2 c1))
  4115. (setq c (or c2 char)))
  4116. (push (cons k c) new))))
  4117. (nreverse new)))
  4118. ;;; Some variables ujsed in various places
  4119. (defvar org-window-configuration nil
  4120. "Used in various places to store a window configuration.")
  4121. (defvar org-finish-function nil
  4122. "Function to be called when `C-c C-c' is used.
  4123. This is for getting out of special buffers like remember.")
  4124. ;; FIXME: Occasionally check by commenting these, to make sure
  4125. ;; no other functions uses these, forgetting to let-bind them.
  4126. (defvar entry)
  4127. (defvar state)
  4128. (defvar last-state)
  4129. (defvar date)
  4130. (defvar description)
  4131. ;; Defined somewhere in this file, but used before definition.
  4132. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  4133. (defvar org-agenda-buffer-name)
  4134. (defvar org-agenda-undo-list)
  4135. (defvar org-agenda-pending-undo-list)
  4136. (defvar org-agenda-overriding-header)
  4137. (defvar orgtbl-mode)
  4138. (defvar org-html-entities)
  4139. (defvar org-struct-menu)
  4140. (defvar org-org-menu)
  4141. (defvar org-tbl-menu)
  4142. (defvar org-agenda-keymap)
  4143. ;;;; Emacs/XEmacs compatibility
  4144. ;; Overlay compatibility functions
  4145. (defun org-make-overlay (beg end &optional buffer)
  4146. (if (featurep 'xemacs)
  4147. (make-extent beg end buffer)
  4148. (make-overlay beg end buffer)))
  4149. (defun org-delete-overlay (ovl)
  4150. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4151. (defun org-detach-overlay (ovl)
  4152. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4153. (defun org-move-overlay (ovl beg end &optional buffer)
  4154. (if (featurep 'xemacs)
  4155. (set-extent-endpoints ovl beg end (or buffer (current-buffer)))
  4156. (move-overlay ovl beg end buffer)))
  4157. (defun org-overlay-put (ovl prop value)
  4158. (if (featurep 'xemacs)
  4159. (set-extent-property ovl prop value)
  4160. (overlay-put ovl prop value)))
  4161. (defun org-overlay-display (ovl text &optional face evap)
  4162. "Make overlay OVL display TEXT with face FACE."
  4163. (if (featurep 'xemacs)
  4164. (let ((gl (make-glyph text)))
  4165. (and face (set-glyph-face gl face))
  4166. (set-extent-property ovl 'invisible t)
  4167. (set-extent-property ovl 'end-glyph gl))
  4168. (overlay-put ovl 'display text)
  4169. (if face (overlay-put ovl 'face face))
  4170. (if evap (overlay-put ovl 'evaporate t))))
  4171. (defun org-overlay-before-string (ovl text &optional face evap)
  4172. "Make overlay OVL display TEXT with face FACE."
  4173. (if (featurep 'xemacs)
  4174. (let ((gl (make-glyph text)))
  4175. (and face (set-glyph-face gl face))
  4176. (set-extent-property ovl 'begin-glyph gl))
  4177. (if face (org-add-props text nil 'face face))
  4178. (overlay-put ovl 'before-string text)
  4179. (if evap (overlay-put ovl 'evaporate t))))
  4180. (defun org-overlay-get (ovl prop)
  4181. (if (featurep 'xemacs)
  4182. (extent-property ovl prop)
  4183. (overlay-get ovl prop)))
  4184. (defun org-overlays-at (pos)
  4185. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4186. (defun org-overlays-in (&optional start end)
  4187. (if (featurep 'xemacs)
  4188. (extent-list nil start end)
  4189. (overlays-in start end)))
  4190. (defun org-overlay-start (o)
  4191. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4192. (defun org-overlay-end (o)
  4193. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4194. (defun org-find-overlays (prop &optional pos delete)
  4195. "Find all overlays specifying PROP at POS or point.
  4196. If DELETE is non-nil, delete all those overlays."
  4197. (let ((overlays (org-overlays-at (or pos (point))))
  4198. ov found)
  4199. (while (setq ov (pop overlays))
  4200. (if (org-overlay-get ov prop)
  4201. (if delete (org-delete-overlay ov) (push ov found))))
  4202. found))
  4203. ;; Region compatibility
  4204. (defun org-add-hook (hook function &optional append local)
  4205. "Add-hook, compatible with both Emacsen."
  4206. (if (and local (featurep 'xemacs))
  4207. (add-local-hook hook function append)
  4208. (add-hook hook function append local)))
  4209. (defvar org-ignore-region nil
  4210. "To temporarily disable the active region.")
  4211. (defun org-region-active-p ()
  4212. "Is `transient-mark-mode' on and the region active?
  4213. Works on both Emacs and XEmacs."
  4214. (if org-ignore-region
  4215. nil
  4216. (if (featurep 'xemacs)
  4217. (and zmacs-regions (region-active-p))
  4218. (if (fboundp 'use-region-p)
  4219. (use-region-p)
  4220. (and transient-mark-mode mark-active))))) ; Emacs 22 and before
  4221. ;; Invisibility compatibility
  4222. (defun org-add-to-invisibility-spec (arg)
  4223. "Add elements to `buffer-invisibility-spec'.
  4224. See documentation for `buffer-invisibility-spec' for the kind of elements
  4225. that can be added."
  4226. (cond
  4227. ((fboundp 'add-to-invisibility-spec)
  4228. (add-to-invisibility-spec arg))
  4229. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  4230. (setq buffer-invisibility-spec (list arg)))
  4231. (t
  4232. (setq buffer-invisibility-spec
  4233. (cons arg buffer-invisibility-spec)))))
  4234. (defun org-remove-from-invisibility-spec (arg)
  4235. "Remove elements from `buffer-invisibility-spec'."
  4236. (if (fboundp 'remove-from-invisibility-spec)
  4237. (remove-from-invisibility-spec arg)
  4238. (if (consp buffer-invisibility-spec)
  4239. (setq buffer-invisibility-spec
  4240. (delete arg buffer-invisibility-spec)))))
  4241. (defun org-in-invisibility-spec-p (arg)
  4242. "Is ARG a member of `buffer-invisibility-spec'?"
  4243. (if (consp buffer-invisibility-spec)
  4244. (member arg buffer-invisibility-spec)
  4245. nil))
  4246. ;;;; Define the Org-mode
  4247. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4248. (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."))
  4249. ;; We use a before-change function to check if a table might need
  4250. ;; an update.
  4251. (defvar org-table-may-need-update t
  4252. "Indicates that a table might need an update.
  4253. This variable is set by `org-before-change-function'.
  4254. `org-table-align' sets it back to nil.")
  4255. (defvar org-mode-map)
  4256. (defvar org-mode-hook nil)
  4257. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4258. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4259. (defvar org-table-buffer-is-an nil)
  4260. (defconst org-outline-regexp "\\*+ ")
  4261. ;;;###autoload
  4262. (define-derived-mode org-mode outline-mode "Org"
  4263. "Outline-based notes management and organizer, alias
  4264. \"Carsten's outline-mode for keeping track of everything.\"
  4265. Org-mode develops organizational tasks around a NOTES file which
  4266. contains information about projects as plain text. Org-mode is
  4267. implemented on top of outline-mode, which is ideal to keep the content
  4268. of large files well structured. It supports ToDo items, deadlines and
  4269. time stamps, which magically appear in the diary listing of the Emacs
  4270. calendar. Tables are easily created with a built-in table editor.
  4271. Plain text URL-like links connect to websites, emails (VM), Usenet
  4272. messages (Gnus), BBDB entries, and any files related to the project.
  4273. For printing and sharing of notes, an Org-mode file (or a part of it)
  4274. can be exported as a structured ASCII or HTML file.
  4275. The following commands are available:
  4276. \\{org-mode-map}"
  4277. ;; Get rid of Outline menus, they are not needed
  4278. ;; Need to do this here because define-derived-mode sets up
  4279. ;; the keymap so late. Still, it is a waste to call this each time
  4280. ;; we switch another buffer into org-mode.
  4281. (if (featurep 'xemacs)
  4282. (when (boundp 'outline-mode-menu-heading)
  4283. ;; Assume this is Greg's port, it used easymenu
  4284. (easy-menu-remove outline-mode-menu-heading)
  4285. (easy-menu-remove outline-mode-menu-show)
  4286. (easy-menu-remove outline-mode-menu-hide))
  4287. (define-key org-mode-map [menu-bar headings] 'undefined)
  4288. (define-key org-mode-map [menu-bar hide] 'undefined)
  4289. (define-key org-mode-map [menu-bar show] 'undefined))
  4290. (easy-menu-add org-org-menu)
  4291. (easy-menu-add org-tbl-menu)
  4292. (org-install-agenda-files-menu)
  4293. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  4294. (org-add-to-invisibility-spec '(org-cwidth))
  4295. (when (featurep 'xemacs)
  4296. (org-set-local 'line-move-ignore-invisible t))
  4297. (org-set-local 'outline-regexp org-outline-regexp)
  4298. (org-set-local 'outline-level 'org-outline-level)
  4299. (when (and org-ellipsis
  4300. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4301. (fboundp 'make-glyph-code))
  4302. (unless org-display-table
  4303. (setq org-display-table (make-display-table)))
  4304. (set-display-table-slot
  4305. org-display-table 4
  4306. (vconcat (mapcar
  4307. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4308. org-ellipsis)))
  4309. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4310. (setq buffer-display-table org-display-table))
  4311. (org-set-regexps-and-options)
  4312. ;; Calc embedded
  4313. (org-set-local 'calc-embedded-open-mode "# ")
  4314. (modify-syntax-entry ?# "<")
  4315. (modify-syntax-entry ?@ "w")
  4316. (if org-startup-truncated (setq truncate-lines t))
  4317. (org-set-local 'font-lock-unfontify-region-function
  4318. 'org-unfontify-region)
  4319. ;; Activate before-change-function
  4320. (org-set-local 'org-table-may-need-update t)
  4321. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4322. 'local)
  4323. ;; Check for running clock before killing a buffer
  4324. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4325. ;; Paragraphs and auto-filling
  4326. (org-set-autofill-regexps)
  4327. (setq indent-line-function 'org-indent-line-function)
  4328. (org-update-radio-target-regexp)
  4329. ;; Comment characters
  4330. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  4331. (org-set-local 'comment-padding " ")
  4332. ;; Imenu
  4333. (org-set-local 'imenu-create-index-function
  4334. 'org-imenu-get-tree)
  4335. ;; Make isearch reveal context
  4336. (if (or (featurep 'xemacs)
  4337. (not (boundp 'outline-isearch-open-invisible-function)))
  4338. ;; Emacs 21 and XEmacs make use of the hook
  4339. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4340. ;; Emacs 22 deals with this through a special variable
  4341. (org-set-local 'outline-isearch-open-invisible-function
  4342. (lambda (&rest ignore) (org-show-context 'isearch))))
  4343. ;; If empty file that did not turn on org-mode automatically, make it to.
  4344. (if (and org-insert-mode-line-in-empty-file
  4345. (interactive-p)
  4346. (= (point-min) (point-max)))
  4347. (insert "# -*- mode: org -*-\n\n"))
  4348. (unless org-inhibit-startup
  4349. (when org-startup-align-all-tables
  4350. (let ((bmp (buffer-modified-p)))
  4351. (org-table-map-tables 'org-table-align)
  4352. (set-buffer-modified-p bmp)))
  4353. (org-cycle-hide-drawers 'all)
  4354. (cond
  4355. ((eq org-startup-folded t)
  4356. (org-cycle '(4)))
  4357. ((eq org-startup-folded 'content)
  4358. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4359. (org-cycle '(4)) (org-cycle '(4)))))))
  4360. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4361. (defsubst org-call-with-arg (command arg)
  4362. "Call COMMAND interactively, but pretend prefix are was ARG."
  4363. (let ((current-prefix-arg arg)) (call-interactively command)))
  4364. (defsubst org-current-line (&optional pos)
  4365. (save-excursion
  4366. (and pos (goto-char pos))
  4367. ;; works also in narrowed buffer, because we start at 1, not point-min
  4368. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  4369. (defun org-current-time ()
  4370. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4371. (if (> org-time-stamp-rounding-minutes 0)
  4372. (let ((r org-time-stamp-rounding-minutes)
  4373. (time (decode-time)))
  4374. (apply 'encode-time
  4375. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4376. (nthcdr 2 time))))
  4377. (current-time)))
  4378. (defun org-add-props (string plist &rest props)
  4379. "Add text properties to entire string, from beginning to end.
  4380. PLIST may be a list of properties, PROPS are individual properties and values
  4381. that will be added to PLIST. Returns the string that was modified."
  4382. (add-text-properties
  4383. 0 (length string) (if props (append plist props) plist) string)
  4384. string)
  4385. (put 'org-add-props 'lisp-indent-function 2)
  4386. ;;;; Font-Lock stuff, including the activators
  4387. (defvar org-mouse-map (make-sparse-keymap))
  4388. (org-defkey org-mouse-map
  4389. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  4390. (org-defkey org-mouse-map
  4391. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  4392. (when org-mouse-1-follows-link
  4393. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4394. (when org-tab-follows-link
  4395. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4396. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4397. (when org-return-follows-link
  4398. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  4399. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  4400. (require 'font-lock)
  4401. (defconst org-non-link-chars "]\t\n\r<>")
  4402. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
  4403. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
  4404. (defvar org-link-re-with-space nil
  4405. "Matches a link with spaces, optional angular brackets around it.")
  4406. (defvar org-link-re-with-space2 nil
  4407. "Matches a link with spaces, optional angular brackets around it.")
  4408. (defvar org-angle-link-re nil
  4409. "Matches link with angular brackets, spaces are allowed.")
  4410. (defvar org-plain-link-re nil
  4411. "Matches plain link, without spaces.")
  4412. (defvar org-bracket-link-regexp nil
  4413. "Matches a link in double brackets.")
  4414. (defvar org-bracket-link-analytic-regexp nil
  4415. "Regular expression used to analyze links.
  4416. Here is what the match groups contain after a match:
  4417. 1: http:
  4418. 2: http
  4419. 3: path
  4420. 4: [desc]
  4421. 5: desc")
  4422. (defvar org-any-link-re nil
  4423. "Regular expression matching any link.")
  4424. (defun org-make-link-regexps ()
  4425. "Update the link regular expressions.
  4426. This should be called after the variable `org-link-types' has changed."
  4427. (setq org-link-re-with-space
  4428. (concat
  4429. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4430. "\\([^" org-non-link-chars " ]"
  4431. "[^" org-non-link-chars "]*"
  4432. "[^" org-non-link-chars " ]\\)>?")
  4433. org-link-re-with-space2
  4434. (concat
  4435. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4436. "\\([^" org-non-link-chars " ]"
  4437. "[^]\t\n\r]*"
  4438. "[^" org-non-link-chars " ]\\)>?")
  4439. org-angle-link-re
  4440. (concat
  4441. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4442. "\\([^" org-non-link-chars " ]"
  4443. "[^" org-non-link-chars "]*"
  4444. "\\)>")
  4445. org-plain-link-re
  4446. (concat
  4447. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  4448. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4449. org-bracket-link-regexp
  4450. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4451. org-bracket-link-analytic-regexp
  4452. (concat
  4453. "\\[\\["
  4454. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  4455. "\\([^]]+\\)"
  4456. "\\]"
  4457. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4458. "\\]")
  4459. org-any-link-re
  4460. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4461. org-angle-link-re "\\)\\|\\("
  4462. org-plain-link-re "\\)")))
  4463. (org-make-link-regexps)
  4464. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4465. "Regular expression for fast time stamp matching.")
  4466. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4467. "Regular expression for fast time stamp matching.")
  4468. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4469. "Regular expression matching time strings for analysis.
  4470. This one does not require the space after the date.")
  4471. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) \\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4472. "Regular expression matching time strings for analysis.")
  4473. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4474. "Regular expression matching time stamps, with groups.")
  4475. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4476. "Regular expression matching time stamps (also [..]), with groups.")
  4477. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4478. "Regular expression matching a time stamp range.")
  4479. (defconst org-tr-regexp-both
  4480. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4481. "Regular expression matching a time stamp range.")
  4482. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4483. org-ts-regexp "\\)?")
  4484. "Regular expression matching a time stamp or time stamp range.")
  4485. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4486. org-ts-regexp-both "\\)?")
  4487. "Regular expression matching a time stamp or time stamp range.
  4488. The time stamps may be either active or inactive.")
  4489. (defvar org-emph-face nil)
  4490. (defun org-do-emphasis-faces (limit)
  4491. "Run through the buffer and add overlays to links."
  4492. (let (rtn)
  4493. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4494. (if (not (= (char-after (match-beginning 3))
  4495. (char-after (match-beginning 4))))
  4496. (progn
  4497. (setq rtn t)
  4498. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4499. 'face
  4500. (nth 1 (assoc (match-string 3)
  4501. org-emphasis-alist)))
  4502. (add-text-properties (match-beginning 2) (match-end 2)
  4503. '(font-lock-multiline t))
  4504. (when org-hide-emphasis-markers
  4505. (add-text-properties (match-end 4) (match-beginning 5)
  4506. '(invisible org-link))
  4507. (add-text-properties (match-beginning 3) (match-end 3)
  4508. '(invisible org-link)))))
  4509. (backward-char 1))
  4510. rtn))
  4511. (defun org-emphasize (&optional char)
  4512. "Insert or change an emphasis, i.e. a font like bold or italic.
  4513. If there is an active region, change that region to a new emphasis.
  4514. If there is no region, just insert the marker characters and position
  4515. the cursor between them.
  4516. CHAR should be either the marker character, or the first character of the
  4517. HTML tag associated with that emphasis. If CHAR is a space, the means
  4518. to remove the emphasis of the selected region.
  4519. If char is not given (for example in an interactive call) it
  4520. will be prompted for."
  4521. (interactive)
  4522. (let ((eal org-emphasis-alist) e det
  4523. (erc org-emphasis-regexp-components)
  4524. (prompt "")
  4525. (string "") beg end move tag c s)
  4526. (if (org-region-active-p)
  4527. (setq beg (region-beginning) end (region-end)
  4528. string (buffer-substring beg end))
  4529. (setq move t))
  4530. (while (setq e (pop eal))
  4531. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4532. c (aref tag 0))
  4533. (push (cons c (string-to-char (car e))) det)
  4534. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4535. (substring tag 1)))))
  4536. (unless char
  4537. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4538. (setq char (read-char-exclusive)))
  4539. (setq char (or (cdr (assoc char det)) char))
  4540. (if (equal char ?\ )
  4541. (setq s "" move nil)
  4542. (unless (assoc (char-to-string char) org-emphasis-alist)
  4543. (error "No such emphasis marker: \"%c\"" char))
  4544. (setq s (char-to-string char)))
  4545. (while (and (> (length string) 1)
  4546. (equal (substring string 0 1) (substring string -1))
  4547. (assoc (substring string 0 1) org-emphasis-alist))
  4548. (setq string (substring string 1 -1)))
  4549. (setq string (concat s string s))
  4550. (if beg (delete-region beg end))
  4551. (unless (or (bolp)
  4552. (string-match (concat "[" (nth 0 erc) "\n]")
  4553. (char-to-string (char-before (point)))))
  4554. (insert " "))
  4555. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  4556. (char-to-string (char-after (point))))
  4557. (insert " ") (backward-char 1))
  4558. (insert string)
  4559. (and move (backward-char 1))))
  4560. (defconst org-nonsticky-props
  4561. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4562. (defun org-activate-plain-links (limit)
  4563. "Run through the buffer and add overlays to links."
  4564. (catch 'exit
  4565. (let (f)
  4566. (while (re-search-forward org-plain-link-re limit t)
  4567. (setq f (get-text-property (match-beginning 0) 'face))
  4568. (if (or (eq f 'org-tag)
  4569. (and (listp f) (memq 'org-tag f)))
  4570. nil
  4571. (add-text-properties (match-beginning 0) (match-end 0)
  4572. (list 'mouse-face 'highlight
  4573. 'rear-nonsticky org-nonsticky-props
  4574. 'keymap org-mouse-map
  4575. ))
  4576. (throw 'exit t))))))
  4577. (defun org-activate-code (limit)
  4578. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  4579. (unless (get-text-property (match-beginning 1) 'face)
  4580. (remove-text-properties (match-beginning 0) (match-end 0)
  4581. '(display t invisible t intangible t))
  4582. t)))
  4583. (defun org-activate-angle-links (limit)
  4584. "Run through the buffer and add overlays to links."
  4585. (if (re-search-forward org-angle-link-re limit t)
  4586. (progn
  4587. (add-text-properties (match-beginning 0) (match-end 0)
  4588. (list 'mouse-face 'highlight
  4589. 'rear-nonsticky org-nonsticky-props
  4590. 'keymap org-mouse-map
  4591. ))
  4592. t)))
  4593. (defmacro org-maybe-intangible (props)
  4594. "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
  4595. In emacs 21, invisible text is not avoided by the command loop, so the
  4596. intangible property is needed to make sure point skips this text.
  4597. In Emacs 22, this is not necessary. The intangible text property has
  4598. led to problems with flyspell. These problems are fixed in flyspell.el,
  4599. but we still avoid setting the property in Emacs 22 and later.
  4600. We use a macro so that the test can happen at compilation time."
  4601. (if (< emacs-major-version 22)
  4602. `(append '(intangible t) ,props)
  4603. props))
  4604. (defun org-activate-bracket-links (limit)
  4605. "Run through the buffer and add overlays to bracketed links."
  4606. (if (re-search-forward org-bracket-link-regexp limit t)
  4607. (let* ((help (concat "LINK: "
  4608. (org-match-string-no-properties 1)))
  4609. ;; FIXME: above we should remove the escapes.
  4610. ;; but that requires another match, protecting match data,
  4611. ;; a lot of overhead for font-lock.
  4612. (ip (org-maybe-intangible
  4613. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  4614. 'keymap org-mouse-map 'mouse-face 'highlight
  4615. 'font-lock-multiline t 'help-echo help)))
  4616. (vp (list 'rear-nonsticky org-nonsticky-props
  4617. 'keymap org-mouse-map 'mouse-face 'highlight
  4618. ' font-lock-multiline t 'help-echo help)))
  4619. ;; We need to remove the invisible property here. Table narrowing
  4620. ;; may have made some of this invisible.
  4621. (remove-text-properties (match-beginning 0) (match-end 0)
  4622. '(invisible nil))
  4623. (if (match-end 3)
  4624. (progn
  4625. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4626. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4627. (add-text-properties (match-end 3) (match-end 0) ip))
  4628. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4629. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4630. (add-text-properties (match-end 1) (match-end 0) ip))
  4631. t)))
  4632. (defun org-activate-dates (limit)
  4633. "Run through the buffer and add overlays to dates."
  4634. (if (re-search-forward org-tsr-regexp-both limit t)
  4635. (progn
  4636. (add-text-properties (match-beginning 0) (match-end 0)
  4637. (list 'mouse-face 'highlight
  4638. 'rear-nonsticky org-nonsticky-props
  4639. 'keymap org-mouse-map))
  4640. (when org-display-custom-times
  4641. (if (match-end 3)
  4642. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4643. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4644. t)))
  4645. (defvar org-target-link-regexp nil
  4646. "Regular expression matching radio targets in plain text.")
  4647. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4648. "Regular expression matching a link target.")
  4649. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4650. "Regular expression matching a radio target.")
  4651. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4652. "Regular expression matching any target.")
  4653. (defun org-activate-target-links (limit)
  4654. "Run through the buffer and add overlays to target matches."
  4655. (when org-target-link-regexp
  4656. (let ((case-fold-search t))
  4657. (if (re-search-forward org-target-link-regexp limit t)
  4658. (progn
  4659. (add-text-properties (match-beginning 0) (match-end 0)
  4660. (list 'mouse-face 'highlight
  4661. 'rear-nonsticky org-nonsticky-props
  4662. 'keymap org-mouse-map
  4663. 'help-echo "Radio target link"
  4664. 'org-linked-text t))
  4665. t)))))
  4666. (defun org-update-radio-target-regexp ()
  4667. "Find all radio targets in this file and update the regular expression."
  4668. (interactive)
  4669. (when (memq 'radio org-activate-links)
  4670. (setq org-target-link-regexp
  4671. (org-make-target-link-regexp (org-all-targets 'radio)))
  4672. (org-restart-font-lock)))
  4673. (defun org-hide-wide-columns (limit)
  4674. (let (s e)
  4675. (setq s (text-property-any (point) (or limit (point-max))
  4676. 'org-cwidth t))
  4677. (when s
  4678. (setq e (next-single-property-change s 'org-cwidth))
  4679. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4680. (goto-char e)
  4681. t)))
  4682. (defvar org-latex-and-specials-regexp nil
  4683. "Regular expression for highlighting export special stuff.")
  4684. (defvar org-match-substring-regexp)
  4685. (defvar org-match-substring-with-braces-regexp)
  4686. (defvar org-export-html-special-string-regexps)
  4687. (defun org-compute-latex-and-specials-regexp ()
  4688. "Compute regular expression for stuff treated specially by exporters."
  4689. (if (not org-highlight-latex-fragments-and-specials)
  4690. (org-set-local 'org-latex-and-specials-regexp nil)
  4691. (let*
  4692. ((matchers (plist-get org-format-latex-options :matchers))
  4693. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4694. org-latex-regexps)))
  4695. (options (org-combine-plists (org-default-export-plist)
  4696. (org-infile-export-plist)))
  4697. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4698. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4699. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4700. (org-export-html-expand (plist-get options :expand-quoted-html))
  4701. (org-export-with-special-strings (plist-get options :special-strings))
  4702. (re-sub
  4703. (cond
  4704. ((equal org-export-with-sub-superscripts '{})
  4705. (list org-match-substring-with-braces-regexp))
  4706. (org-export-with-sub-superscripts
  4707. (list org-match-substring-regexp))
  4708. (t nil)))
  4709. (re-latex
  4710. (if org-export-with-LaTeX-fragments
  4711. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4712. (re-macros
  4713. (if org-export-with-TeX-macros
  4714. (list (concat "\\\\"
  4715. (regexp-opt
  4716. (append (mapcar 'car org-html-entities)
  4717. (if (boundp 'org-latex-entities)
  4718. org-latex-entities nil))
  4719. 'words))) ; FIXME
  4720. ))
  4721. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4722. (re-special (if org-export-with-special-strings
  4723. (mapcar (lambda (x) (car x))
  4724. org-export-html-special-string-regexps)))
  4725. (re-rest
  4726. (delq nil
  4727. (list
  4728. (if org-export-html-expand "@<[^>\n]+>")
  4729. ))))
  4730. (org-set-local
  4731. 'org-latex-and-specials-regexp
  4732. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4733. re-rest) "\\|")))))
  4734. (defface org-latex-and-export-specials
  4735. (let ((font (cond ((assq :inherit custom-face-attributes)
  4736. '(:inherit underline))
  4737. (t '(:underline t)))))
  4738. `((((class grayscale) (background light))
  4739. (:foreground "DimGray" ,@font))
  4740. (((class grayscale) (background dark))
  4741. (:foreground "LightGray" ,@font))
  4742. (((class color) (background light))
  4743. (:foreground "SaddleBrown"))
  4744. (((class color) (background dark))
  4745. (:foreground "burlywood"))
  4746. (t (,@font))))
  4747. "Face used to highlight math latex and other special exporter stuff."
  4748. :group 'org-faces)
  4749. (defun org-do-latex-and-special-faces (limit)
  4750. "Run through the buffer and add overlays to links."
  4751. (when org-latex-and-specials-regexp
  4752. (let (rtn d)
  4753. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4754. limit t))
  4755. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4756. 'face))
  4757. '(org-code org-verbatim underline)))
  4758. (progn
  4759. (setq rtn t
  4760. d (cond ((member (char-after (1+ (match-beginning 0)))
  4761. '(?_ ?^)) 1)
  4762. (t 0)))
  4763. (font-lock-prepend-text-property
  4764. (+ d (match-beginning 0)) (match-end 0)
  4765. 'face 'org-latex-and-export-specials)
  4766. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4767. '(font-lock-multiline t)))))
  4768. rtn)))
  4769. (defun org-restart-font-lock ()
  4770. "Restart font-lock-mode, to force refontification."
  4771. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4772. (font-lock-mode -1)
  4773. (font-lock-mode 1)))
  4774. (defun org-all-targets (&optional radio)
  4775. "Return a list of all targets in this file.
  4776. With optional argument RADIO, only find radio targets."
  4777. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4778. rtn)
  4779. (save-excursion
  4780. (goto-char (point-min))
  4781. (while (re-search-forward re nil t)
  4782. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4783. rtn)))
  4784. (defun org-make-target-link-regexp (targets)
  4785. "Make regular expression matching all strings in TARGETS.
  4786. The regular expression finds the targets also if there is a line break
  4787. between words."
  4788. (and targets
  4789. (concat
  4790. "\\<\\("
  4791. (mapconcat
  4792. (lambda (x)
  4793. (while (string-match " +" x)
  4794. (setq x (replace-match "\\s-+" t t x)))
  4795. x)
  4796. targets
  4797. "\\|")
  4798. "\\)\\>")))
  4799. (defun org-activate-tags (limit)
  4800. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4801. (progn
  4802. (add-text-properties (match-beginning 1) (match-end 1)
  4803. (list 'mouse-face 'highlight
  4804. 'rear-nonsticky org-nonsticky-props
  4805. 'keymap org-mouse-map))
  4806. t)))
  4807. (defun org-outline-level ()
  4808. (save-excursion
  4809. (looking-at outline-regexp)
  4810. (if (match-beginning 1)
  4811. (+ (org-get-string-indentation (match-string 1)) 1000)
  4812. (1- (- (match-end 0) (match-beginning 0))))))
  4813. (defvar org-font-lock-keywords nil)
  4814. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  4815. "Regular expression matching a property line.")
  4816. (defun org-set-font-lock-defaults ()
  4817. (let* ((em org-fontify-emphasized-text)
  4818. (lk org-activate-links)
  4819. (org-font-lock-extra-keywords
  4820. (list
  4821. ;; Headlines
  4822. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  4823. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  4824. ;; Table lines
  4825. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4826. (1 'org-table t))
  4827. ;; Table internals
  4828. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4829. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4830. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4831. ;; Drawers
  4832. (list org-drawer-regexp '(0 'org-special-keyword t))
  4833. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4834. ;; Properties
  4835. (list org-property-re
  4836. '(1 'org-special-keyword t)
  4837. '(3 'org-property-value t))
  4838. (if org-format-transports-properties-p
  4839. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  4840. ;; Links
  4841. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4842. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4843. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4844. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4845. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4846. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4847. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4848. '(org-hide-wide-columns (0 nil append))
  4849. ;; TODO lines
  4850. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  4851. '(1 (org-get-todo-face 1) t))
  4852. ;; DONE
  4853. (if org-fontify-done-headline
  4854. (list (concat "^[*]+ +\\<\\("
  4855. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4856. "\\)\\(.*\\)")
  4857. '(2 'org-headline-done t))
  4858. nil)
  4859. ;; Priorities
  4860. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  4861. ;; Special keywords
  4862. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4863. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4864. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4865. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4866. ;; Emphasis
  4867. (if em
  4868. (if (featurep 'xemacs)
  4869. '(org-do-emphasis-faces (0 nil append))
  4870. '(org-do-emphasis-faces)))
  4871. ;; Checkboxes
  4872. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4873. 2 'bold prepend)
  4874. (if org-provide-checkbox-statistics
  4875. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4876. (0 (org-get-checkbox-statistics-face) t)))
  4877. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4878. '(1 'org-archived prepend))
  4879. ;; Specials
  4880. '(org-do-latex-and-special-faces)
  4881. ;; Code
  4882. '(org-activate-code (1 'org-code t))
  4883. ;; COMMENT
  4884. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4885. "\\|" org-quote-string "\\)\\>")
  4886. '(1 'org-special-keyword t))
  4887. '("^#.*" (0 'font-lock-comment-face t))
  4888. )))
  4889. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4890. ;; Now set the full font-lock-keywords
  4891. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4892. (org-set-local 'font-lock-defaults
  4893. '(org-font-lock-keywords t nil nil backward-paragraph))
  4894. (kill-local-variable 'font-lock-keywords) nil))
  4895. (defvar org-m nil)
  4896. (defvar org-l nil)
  4897. (defvar org-f nil)
  4898. (defun org-get-level-face (n)
  4899. "Get the right face for match N in font-lock matching of healdines."
  4900. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4901. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4902. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4903. (cond
  4904. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4905. ((eq n 2) org-f)
  4906. (t (if org-level-color-stars-only nil org-f))))
  4907. (defun org-get-todo-face (kwd)
  4908. "Get the right face for a TODO keyword KWD.
  4909. If KWD is a number, get the corresponding match group."
  4910. (if (numberp kwd) (setq kwd (match-string kwd)))
  4911. (or (cdr (assoc kwd org-todo-keyword-faces))
  4912. (and (member kwd org-done-keywords) 'org-done)
  4913. 'org-todo))
  4914. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4915. "Remove fontification and activation overlays from links."
  4916. (font-lock-default-unfontify-region beg end)
  4917. (let* ((buffer-undo-list t)
  4918. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4919. (inhibit-modification-hooks t)
  4920. deactivate-mark buffer-file-name buffer-file-truename)
  4921. (remove-text-properties beg end
  4922. '(mouse-face t keymap t org-linked-text t
  4923. invisible t intangible t))))
  4924. ;;;; Visibility cycling, including org-goto and indirect buffer
  4925. ;;; Cycling
  4926. (defvar org-cycle-global-status nil)
  4927. (make-variable-buffer-local 'org-cycle-global-status)
  4928. (defvar org-cycle-subtree-status nil)
  4929. (make-variable-buffer-local 'org-cycle-subtree-status)
  4930. ;;;###autoload
  4931. (defun org-cycle (&optional arg)
  4932. "Visibility cycling for Org-mode.
  4933. - When this function is called with a prefix argument, rotate the entire
  4934. buffer through 3 states (global cycling)
  4935. 1. OVERVIEW: Show only top-level headlines.
  4936. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4937. 3. SHOW ALL: Show everything.
  4938. - When point is at the beginning of a headline, rotate the subtree started
  4939. by this line through 3 different states (local cycling)
  4940. 1. FOLDED: Only the main headline is shown.
  4941. 2. CHILDREN: The main headline and the direct children are shown.
  4942. From this state, you can move to one of the children
  4943. and zoom in further.
  4944. 3. SUBTREE: Show the entire subtree, including body text.
  4945. - When there is a numeric prefix, go up to a heading with level ARG, do
  4946. a `show-subtree' and return to the previous cursor position. If ARG
  4947. is negative, go up that many levels.
  4948. - When point is not at the beginning of a headline, execute
  4949. `indent-relative', like TAB normally does. See the option
  4950. `org-cycle-emulate-tab' for details.
  4951. - Special case: if point is at the beginning of the buffer and there is
  4952. no headline in line 1, this function will act as if called with prefix arg.
  4953. But only if also the variable `org-cycle-global-at-bob' is t."
  4954. (interactive "P")
  4955. (let* ((outline-regexp
  4956. (if (and (org-mode-p) org-cycle-include-plain-lists)
  4957. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  4958. outline-regexp))
  4959. (bob-special (and org-cycle-global-at-bob (bobp)
  4960. (not (looking-at outline-regexp))))
  4961. (org-cycle-hook
  4962. (if bob-special
  4963. (delq 'org-optimize-window-after-visibility-change
  4964. (copy-sequence org-cycle-hook))
  4965. org-cycle-hook))
  4966. (pos (point)))
  4967. (if (or bob-special (equal arg '(4)))
  4968. ;; special case: use global cycling
  4969. (setq arg t))
  4970. (cond
  4971. ((org-at-table-p 'any)
  4972. ;; Enter the table or move to the next field in the table
  4973. (or (org-table-recognize-table.el)
  4974. (progn
  4975. (if arg (org-table-edit-field t)
  4976. (org-table-justify-field-maybe)
  4977. (call-interactively 'org-table-next-field)))))
  4978. ((eq arg t) ;; Global cycling
  4979. (cond
  4980. ((and (eq last-command this-command)
  4981. (eq org-cycle-global-status 'overview))
  4982. ;; We just created the overview - now do table of contents
  4983. ;; This can be slow in very large buffers, so indicate action
  4984. (message "CONTENTS...")
  4985. (org-content)
  4986. (message "CONTENTS...done")
  4987. (setq org-cycle-global-status 'contents)
  4988. (run-hook-with-args 'org-cycle-hook 'contents))
  4989. ((and (eq last-command this-command)
  4990. (eq org-cycle-global-status 'contents))
  4991. ;; We just showed the table of contents - now show everything
  4992. (show-all)
  4993. (message "SHOW ALL")
  4994. (setq org-cycle-global-status 'all)
  4995. (run-hook-with-args 'org-cycle-hook 'all))
  4996. (t
  4997. ;; Default action: go to overview
  4998. (org-overview)
  4999. (message "OVERVIEW")
  5000. (setq org-cycle-global-status 'overview)
  5001. (run-hook-with-args 'org-cycle-hook 'overview))))
  5002. ((and org-drawers org-drawer-regexp
  5003. (save-excursion
  5004. (beginning-of-line 1)
  5005. (looking-at org-drawer-regexp)))
  5006. ;; Toggle block visibility
  5007. (org-flag-drawer
  5008. (not (get-char-property (match-end 0) 'invisible))))
  5009. ((integerp arg)
  5010. ;; Show-subtree, ARG levels up from here.
  5011. (save-excursion
  5012. (org-back-to-heading)
  5013. (outline-up-heading (if (< arg 0) (- arg)
  5014. (- (funcall outline-level) arg)))
  5015. (org-show-subtree)))
  5016. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  5017. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5018. ;; At a heading: rotate between three different views
  5019. (org-back-to-heading)
  5020. (let ((goal-column 0) eoh eol eos)
  5021. ;; First, some boundaries
  5022. (save-excursion
  5023. (org-back-to-heading)
  5024. (save-excursion
  5025. (beginning-of-line 2)
  5026. (while (and (not (eobp)) ;; this is like `next-line'
  5027. (get-char-property (1- (point)) 'invisible))
  5028. (beginning-of-line 2)) (setq eol (point)))
  5029. (outline-end-of-heading) (setq eoh (point))
  5030. (org-end-of-subtree t)
  5031. (unless (eobp)
  5032. (skip-chars-forward " \t\n")
  5033. (beginning-of-line 1) ; in case this is an item
  5034. )
  5035. (setq eos (1- (point))))
  5036. ;; Find out what to do next and set `this-command'
  5037. (cond
  5038. ((= eos eoh)
  5039. ;; Nothing is hidden behind this heading
  5040. (message "EMPTY ENTRY")
  5041. (setq org-cycle-subtree-status nil)
  5042. (save-excursion
  5043. (goto-char eos)
  5044. (outline-next-heading)
  5045. (if (org-invisible-p) (org-flag-heading nil))))
  5046. ((or (>= eol eos)
  5047. (not (string-match "\\S-" (buffer-substring eol eos))))
  5048. ;; Entire subtree is hidden in one line: open it
  5049. (org-show-entry)
  5050. (show-children)
  5051. (message "CHILDREN")
  5052. (save-excursion
  5053. (goto-char eos)
  5054. (outline-next-heading)
  5055. (if (org-invisible-p) (org-flag-heading nil)))
  5056. (setq org-cycle-subtree-status 'children)
  5057. (run-hook-with-args 'org-cycle-hook 'children))
  5058. ((and (eq last-command this-command)
  5059. (eq org-cycle-subtree-status 'children))
  5060. ;; We just showed the children, now show everything.
  5061. (org-show-subtree)
  5062. (message "SUBTREE")
  5063. (setq org-cycle-subtree-status 'subtree)
  5064. (run-hook-with-args 'org-cycle-hook 'subtree))
  5065. (t
  5066. ;; Default action: hide the subtree.
  5067. (hide-subtree)
  5068. (message "FOLDED")
  5069. (setq org-cycle-subtree-status 'folded)
  5070. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5071. ;; TAB emulation
  5072. (buffer-read-only (org-back-to-heading))
  5073. ((org-try-cdlatex-tab))
  5074. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5075. (or (not (bolp))
  5076. (not (looking-at outline-regexp))))
  5077. (call-interactively (global-key-binding "\t")))
  5078. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5079. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5080. (or (and (eq org-cycle-emulate-tab 'white)
  5081. (= (match-end 0) (point-at-eol)))
  5082. (and (eq org-cycle-emulate-tab 'whitestart)
  5083. (>= (match-end 0) pos))))
  5084. t
  5085. (eq org-cycle-emulate-tab t))
  5086. ; (if (and (looking-at "[ \n\r\t]")
  5087. ; (string-match "^[ \t]*$" (buffer-substring
  5088. ; (point-at-bol) (point))))
  5089. ; (progn
  5090. ; (beginning-of-line 1)
  5091. ; (and (looking-at "[ \t]+") (replace-match ""))))
  5092. (call-interactively (global-key-binding "\t")))
  5093. (t (save-excursion
  5094. (org-back-to-heading)
  5095. (org-cycle))))))
  5096. ;;;###autoload
  5097. (defun org-global-cycle (&optional arg)
  5098. "Cycle the global visibility. For details see `org-cycle'."
  5099. (interactive "P")
  5100. (let ((org-cycle-include-plain-lists
  5101. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5102. (if (integerp arg)
  5103. (progn
  5104. (show-all)
  5105. (hide-sublevels arg)
  5106. (setq org-cycle-global-status 'contents))
  5107. (org-cycle '(4)))))
  5108. (defun org-overview ()
  5109. "Switch to overview mode, shoing only top-level headlines.
  5110. Really, this shows all headlines with level equal or greater than the level
  5111. of the first headline in the buffer. This is important, because if the
  5112. first headline is not level one, then (hide-sublevels 1) gives confusing
  5113. results."
  5114. (interactive)
  5115. (let ((level (save-excursion
  5116. (goto-char (point-min))
  5117. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5118. (progn
  5119. (goto-char (match-beginning 0))
  5120. (funcall outline-level))))))
  5121. (and level (hide-sublevels level))))
  5122. (defun org-content (&optional arg)
  5123. "Show all headlines in the buffer, like a table of contents.
  5124. With numerical argument N, show content up to level N."
  5125. (interactive "P")
  5126. (save-excursion
  5127. ;; Visit all headings and show their offspring
  5128. (and (integerp arg) (org-overview))
  5129. (goto-char (point-max))
  5130. (catch 'exit
  5131. (while (and (progn (condition-case nil
  5132. (outline-previous-visible-heading 1)
  5133. (error (goto-char (point-min))))
  5134. t)
  5135. (looking-at outline-regexp))
  5136. (if (integerp arg)
  5137. (show-children (1- arg))
  5138. (show-branches))
  5139. (if (bobp) (throw 'exit nil))))))
  5140. (defun org-optimize-window-after-visibility-change (state)
  5141. "Adjust the window after a change in outline visibility.
  5142. This function is the default value of the hook `org-cycle-hook'."
  5143. (when (get-buffer-window (current-buffer))
  5144. (cond
  5145. ; ((eq state 'overview) (org-first-headline-recenter 1))
  5146. ; ((eq state 'overview) (org-beginning-of-line))
  5147. ((eq state 'content) nil)
  5148. ((eq state 'all) nil)
  5149. ((eq state 'folded) nil)
  5150. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5151. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5152. (defun org-compact-display-after-subtree-move ()
  5153. (let (beg end)
  5154. (save-excursion
  5155. (if (org-up-heading-safe)
  5156. (progn
  5157. (hide-subtree)
  5158. (show-entry)
  5159. (show-children)
  5160. (org-cycle-show-empty-lines 'children)
  5161. (org-cycle-hide-drawers 'children))
  5162. (org-overview)))))
  5163. (defun org-cycle-show-empty-lines (state)
  5164. "Show empty lines above all visible headlines.
  5165. The region to be covered depends on STATE when called through
  5166. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5167. entire buffer covered. Note that an empty line is only shown if there
  5168. are at least `org-cycle-separator-lines' empty lines before the headeline."
  5169. (when (> org-cycle-separator-lines 0)
  5170. (save-excursion
  5171. (let* ((n org-cycle-separator-lines)
  5172. (re (cond
  5173. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5174. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5175. (t (let ((ns (number-to-string (- n 2))))
  5176. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5177. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5178. beg end)
  5179. (cond
  5180. ((memq state '(overview contents t))
  5181. (setq beg (point-min) end (point-max)))
  5182. ((memq state '(children folded))
  5183. (setq beg (point) end (progn (org-end-of-subtree t t)
  5184. (beginning-of-line 2)
  5185. (point)))))
  5186. (when beg
  5187. (goto-char beg)
  5188. (while (re-search-forward re end t)
  5189. (if (not (get-char-property (match-end 1) 'invisible))
  5190. (outline-flag-region
  5191. (match-beginning 1) (match-end 1) nil)))))))
  5192. ;; Never hide empty lines at the end of the file.
  5193. (save-excursion
  5194. (goto-char (point-max))
  5195. (outline-previous-heading)
  5196. (outline-end-of-heading)
  5197. (if (and (looking-at "[ \t\n]+")
  5198. (= (match-end 0) (point-max)))
  5199. (outline-flag-region (point) (match-end 0) nil))))
  5200. (defun org-subtree-end-visible-p ()
  5201. "Is the end of the current subtree visible?"
  5202. (pos-visible-in-window-p
  5203. (save-excursion (org-end-of-subtree t) (point))))
  5204. (defun org-first-headline-recenter (&optional N)
  5205. "Move cursor to the first headline and recenter the headline.
  5206. Optional argument N means, put the headline into the Nth line of the window."
  5207. (goto-char (point-min))
  5208. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5209. (beginning-of-line)
  5210. (recenter (prefix-numeric-value N))))
  5211. ;;; Org-goto
  5212. (defvar org-goto-window-configuration nil)
  5213. (defvar org-goto-marker nil)
  5214. (defvar org-goto-map
  5215. (let ((map (make-sparse-keymap)))
  5216. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5217. (while (setq cmd (pop cmds))
  5218. (substitute-key-definition cmd cmd map global-map)))
  5219. (suppress-keymap map)
  5220. (org-defkey map "\C-m" 'org-goto-ret)
  5221. (org-defkey map [(return)] 'org-goto-ret)
  5222. (org-defkey map [(left)] 'org-goto-left)
  5223. (org-defkey map [(right)] 'org-goto-right)
  5224. (org-defkey map [(control ?g)] 'org-goto-quit)
  5225. (org-defkey map "\C-i" 'org-cycle)
  5226. (org-defkey map [(tab)] 'org-cycle)
  5227. (org-defkey map [(down)] 'outline-next-visible-heading)
  5228. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5229. (if org-goto-auto-isearch
  5230. (if (fboundp 'define-key-after)
  5231. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5232. nil)
  5233. (org-defkey map "q" 'org-goto-quit)
  5234. (org-defkey map "n" 'outline-next-visible-heading)
  5235. (org-defkey map "p" 'outline-previous-visible-heading)
  5236. (org-defkey map "f" 'outline-forward-same-level)
  5237. (org-defkey map "b" 'outline-backward-same-level)
  5238. (org-defkey map "u" 'outline-up-heading))
  5239. (org-defkey map "/" 'org-occur)
  5240. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5241. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5242. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5243. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5244. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5245. map))
  5246. (defconst org-goto-help
  5247. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5248. RET=jump to location [Q]uit and return to previous location
  5249. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5250. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5251. (defun org-goto (&optional alternative-interface)
  5252. "Look up a different location in the current file, keeping current visibility.
  5253. When you want look-up or go to a different location in a document, the
  5254. fastest way is often to fold the entire buffer and then dive into the tree.
  5255. This method has the disadvantage, that the previous location will be folded,
  5256. which may not be what you want.
  5257. This command works around this by showing a copy of the current buffer
  5258. in an indirect buffer, in overview mode. You can dive into the tree in
  5259. that copy, use org-occur and incremental search to find a location.
  5260. When pressing RET or `Q', the command returns to the original buffer in
  5261. which the visibility is still unchanged. After RET is will also jump to
  5262. the location selected in the indirect buffer and expose the
  5263. the headline hierarchy above."
  5264. (interactive "P")
  5265. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  5266. (org-refile-use-outline-path t)
  5267. (interface
  5268. (if (not alternative-interface)
  5269. org-goto-interface
  5270. (if (eq org-goto-interface 'outline)
  5271. 'outline-path-completion
  5272. 'outline)))
  5273. (org-goto-start-pos (point))
  5274. (selected-point
  5275. (if (eq interface 'outline)
  5276. (car (org-get-location (current-buffer) org-goto-help))
  5277. (nth 3 (org-refile-get-location "Goto: ")))))
  5278. (if selected-point
  5279. (progn
  5280. (org-mark-ring-push org-goto-start-pos)
  5281. (goto-char selected-point)
  5282. (if (or (org-invisible-p) (org-invisible-p2))
  5283. (org-show-context 'org-goto)))
  5284. (message "Quit"))))
  5285. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5286. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5287. (defun org-get-location (buf help)
  5288. "Let the user select a location in the Org-mode buffer BUF.
  5289. This function uses a recursive edit. It returns the selected position
  5290. or nil."
  5291. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5292. (isearch-hide-immediately nil)
  5293. (isearch-search-fun-function
  5294. (lambda () 'org-goto-local-search-forward-headings))
  5295. (org-goto-selected-point org-goto-exit-command))
  5296. (save-excursion
  5297. (save-window-excursion
  5298. (delete-other-windows)
  5299. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5300. (switch-to-buffer
  5301. (condition-case nil
  5302. (make-indirect-buffer (current-buffer) "*org-goto*")
  5303. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5304. (with-output-to-temp-buffer "*Help*"
  5305. (princ help))
  5306. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  5307. (setq buffer-read-only nil)
  5308. (let ((org-startup-truncated t)
  5309. (org-startup-folded nil)
  5310. (org-startup-align-all-tables nil))
  5311. (org-mode)
  5312. (org-overview))
  5313. (setq buffer-read-only t)
  5314. (if (and (boundp 'org-goto-start-pos)
  5315. (integer-or-marker-p org-goto-start-pos))
  5316. (let ((org-show-hierarchy-above t)
  5317. (org-show-siblings t)
  5318. (org-show-following-heading t))
  5319. (goto-char org-goto-start-pos)
  5320. (and (org-invisible-p) (org-show-context)))
  5321. (goto-char (point-min)))
  5322. (org-beginning-of-line)
  5323. (message "Select location and press RET")
  5324. (use-local-map org-goto-map)
  5325. (recursive-edit)
  5326. ))
  5327. (kill-buffer "*org-goto*")
  5328. (cons org-goto-selected-point org-goto-exit-command)))
  5329. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5330. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5331. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5332. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5333. (defun org-goto-local-search-forward-headings (string bound noerror)
  5334. "Search and make sure that anu matches are in headlines."
  5335. (catch 'return
  5336. (while (search-forward string bound noerror)
  5337. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5338. (and (member :headline context)
  5339. (not (member :tags context))))
  5340. (throw 'return (point))))))
  5341. (defun org-goto-local-auto-isearch ()
  5342. "Start isearch."
  5343. (interactive)
  5344. (goto-char (point-min))
  5345. (let ((keys (this-command-keys)))
  5346. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5347. (isearch-mode t)
  5348. (isearch-process-search-char (string-to-char keys)))))
  5349. (defun org-goto-ret (&optional arg)
  5350. "Finish `org-goto' by going to the new location."
  5351. (interactive "P")
  5352. (setq org-goto-selected-point (point)
  5353. org-goto-exit-command 'return)
  5354. (throw 'exit nil))
  5355. (defun org-goto-left ()
  5356. "Finish `org-goto' by going to the new location."
  5357. (interactive)
  5358. (if (org-on-heading-p)
  5359. (progn
  5360. (beginning-of-line 1)
  5361. (setq org-goto-selected-point (point)
  5362. org-goto-exit-command 'left)
  5363. (throw 'exit nil))
  5364. (error "Not on a heading")))
  5365. (defun org-goto-right ()
  5366. "Finish `org-goto' by going to the new location."
  5367. (interactive)
  5368. (if (org-on-heading-p)
  5369. (progn
  5370. (setq org-goto-selected-point (point)
  5371. org-goto-exit-command 'right)
  5372. (throw 'exit nil))
  5373. (error "Not on a heading")))
  5374. (defun org-goto-quit ()
  5375. "Finish `org-goto' without cursor motion."
  5376. (interactive)
  5377. (setq org-goto-selected-point nil)
  5378. (setq org-goto-exit-command 'quit)
  5379. (throw 'exit nil))
  5380. ;;; Indirect buffer display of subtrees
  5381. (defvar org-indirect-dedicated-frame nil
  5382. "This is the frame being used for indirect tree display.")
  5383. (defvar org-last-indirect-buffer nil)
  5384. (defun org-tree-to-indirect-buffer (&optional arg)
  5385. "Create indirect buffer and narrow it to current subtree.
  5386. With numerical prefix ARG, go up to this level and then take that tree.
  5387. If ARG is negative, go up that many levels.
  5388. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5389. indirect buffer previously made with this command, to avoid proliferation of
  5390. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5391. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5392. is kept so that you can work with several indirect buffers at the same time.
  5393. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5394. requests that a new frame be made for the new buffer, so that the dedicated
  5395. frame is not changed."
  5396. (interactive "P")
  5397. (let ((cbuf (current-buffer))
  5398. (cwin (selected-window))
  5399. (pos (point))
  5400. beg end level heading ibuf)
  5401. (save-excursion
  5402. (org-back-to-heading t)
  5403. (when (numberp arg)
  5404. (setq level (org-outline-level))
  5405. (if (< arg 0) (setq arg (+ level arg)))
  5406. (while (> (setq level (org-outline-level)) arg)
  5407. (outline-up-heading 1 t)))
  5408. (setq beg (point)
  5409. heading (org-get-heading))
  5410. (org-end-of-subtree t) (setq end (point)))
  5411. (if (and (buffer-live-p org-last-indirect-buffer)
  5412. (not (eq org-indirect-buffer-display 'new-frame))
  5413. (not arg))
  5414. (kill-buffer org-last-indirect-buffer))
  5415. (setq ibuf (org-get-indirect-buffer cbuf)
  5416. org-last-indirect-buffer ibuf)
  5417. (cond
  5418. ((or (eq org-indirect-buffer-display 'new-frame)
  5419. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5420. (select-frame (make-frame))
  5421. (delete-other-windows)
  5422. (switch-to-buffer ibuf)
  5423. (org-set-frame-title heading))
  5424. ((eq org-indirect-buffer-display 'dedicated-frame)
  5425. (raise-frame
  5426. (select-frame (or (and org-indirect-dedicated-frame
  5427. (frame-live-p org-indirect-dedicated-frame)
  5428. org-indirect-dedicated-frame)
  5429. (setq org-indirect-dedicated-frame (make-frame)))))
  5430. (delete-other-windows)
  5431. (switch-to-buffer ibuf)
  5432. (org-set-frame-title (concat "Indirect: " heading)))
  5433. ((eq org-indirect-buffer-display 'current-window)
  5434. (switch-to-buffer ibuf))
  5435. ((eq org-indirect-buffer-display 'other-window)
  5436. (pop-to-buffer ibuf))
  5437. (t (error "Invalid value.")))
  5438. (if (featurep 'xemacs)
  5439. (save-excursion (org-mode) (turn-on-font-lock)))
  5440. (narrow-to-region beg end)
  5441. (show-all)
  5442. (goto-char pos)
  5443. (and (window-live-p cwin) (select-window cwin))))
  5444. (defun org-get-indirect-buffer (&optional buffer)
  5445. (setq buffer (or buffer (current-buffer)))
  5446. (let ((n 1) (base (buffer-name buffer)) bname)
  5447. (while (buffer-live-p
  5448. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5449. (setq n (1+ n)))
  5450. (condition-case nil
  5451. (make-indirect-buffer buffer bname 'clone)
  5452. (error (make-indirect-buffer buffer bname)))))
  5453. (defun org-set-frame-title (title)
  5454. "Set the title of the current frame to the string TITLE."
  5455. ;; FIXME: how to name a single frame in XEmacs???
  5456. (unless (featurep 'xemacs)
  5457. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5458. ;;;; Structure editing
  5459. ;;; Inserting headlines
  5460. (defun org-insert-heading (&optional force-heading)
  5461. "Insert a new heading or item with same depth at point.
  5462. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5463. If point is at the beginning of a headline, insert a sibling before the
  5464. current headline. If point is in the middle of a headline, split the headline
  5465. at that position and make the rest of the headline part of the sibling below
  5466. the current headline."
  5467. (interactive "P")
  5468. (if (= (buffer-size) 0)
  5469. (insert "\n* ")
  5470. (when (or force-heading (not (org-insert-item)))
  5471. (let* ((head (save-excursion
  5472. (condition-case nil
  5473. (progn
  5474. (org-back-to-heading)
  5475. (match-string 0))
  5476. (error "*"))))
  5477. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  5478. pos)
  5479. (cond
  5480. ((and (org-on-heading-p) (bolp)
  5481. (or (bobp)
  5482. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5483. (open-line (if blank 2 1)))
  5484. ((and (bolp)
  5485. (or (bobp)
  5486. (save-excursion
  5487. (backward-char 1) (not (org-invisible-p)))))
  5488. nil)
  5489. (t (newline (if blank 2 1))))
  5490. (insert head) (just-one-space)
  5491. (setq pos (point))
  5492. (end-of-line 1)
  5493. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5494. (run-hooks 'org-insert-heading-hook)))))
  5495. (defun org-insert-heading-after-current ()
  5496. "Insert a new heading with same level as current, after current subtree."
  5497. (interactive)
  5498. (org-back-to-heading)
  5499. (org-insert-heading)
  5500. (org-move-subtree-down)
  5501. (end-of-line 1))
  5502. (defun org-insert-todo-heading (arg)
  5503. "Insert a new heading with the same level and TODO state as current heading.
  5504. If the heading has no TODO state, or if the state is DONE, use the first
  5505. state (TODO by default). Also with prefix arg, force first state."
  5506. (interactive "P")
  5507. (when (not (org-insert-item 'checkbox))
  5508. (org-insert-heading)
  5509. (save-excursion
  5510. (org-back-to-heading)
  5511. (outline-previous-heading)
  5512. (looking-at org-todo-line-regexp))
  5513. (if (or arg
  5514. (not (match-beginning 2))
  5515. (member (match-string 2) org-done-keywords))
  5516. (insert (car org-todo-keywords-1) " ")
  5517. (insert (match-string 2) " "))))
  5518. (defun org-insert-subheading (arg)
  5519. "Insert a new subheading and demote it.
  5520. Works for outline headings and for plain lists alike."
  5521. (interactive "P")
  5522. (org-insert-heading arg)
  5523. (cond
  5524. ((org-on-heading-p) (org-do-demote))
  5525. ((org-at-item-p) (org-indent-item 1))))
  5526. (defun org-insert-todo-subheading (arg)
  5527. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5528. Works for outline headings and for plain lists alike."
  5529. (interactive "P")
  5530. (org-insert-todo-heading arg)
  5531. (cond
  5532. ((org-on-heading-p) (org-do-demote))
  5533. ((org-at-item-p) (org-indent-item 1))))
  5534. ;;; Promotion and Demotion
  5535. (defun org-promote-subtree ()
  5536. "Promote the entire subtree.
  5537. See also `org-promote'."
  5538. (interactive)
  5539. (save-excursion
  5540. (org-map-tree 'org-promote))
  5541. (org-fix-position-after-promote))
  5542. (defun org-demote-subtree ()
  5543. "Demote the entire subtree. See `org-demote'.
  5544. See also `org-promote'."
  5545. (interactive)
  5546. (save-excursion
  5547. (org-map-tree 'org-demote))
  5548. (org-fix-position-after-promote))
  5549. (defun org-do-promote ()
  5550. "Promote the current heading higher up the tree.
  5551. If the region is active in `transient-mark-mode', promote all headings
  5552. in the region."
  5553. (interactive)
  5554. (save-excursion
  5555. (if (org-region-active-p)
  5556. (org-map-region 'org-promote (region-beginning) (region-end))
  5557. (org-promote)))
  5558. (org-fix-position-after-promote))
  5559. (defun org-do-demote ()
  5560. "Demote the current heading lower down the tree.
  5561. If the region is active in `transient-mark-mode', demote all headings
  5562. in the region."
  5563. (interactive)
  5564. (save-excursion
  5565. (if (org-region-active-p)
  5566. (org-map-region 'org-demote (region-beginning) (region-end))
  5567. (org-demote)))
  5568. (org-fix-position-after-promote))
  5569. (defun org-fix-position-after-promote ()
  5570. "Make sure that after pro/demotion cursor position is right."
  5571. (let ((pos (point)))
  5572. (when (save-excursion
  5573. (beginning-of-line 1)
  5574. (looking-at org-todo-line-regexp)
  5575. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5576. (cond ((eobp) (insert " "))
  5577. ((eolp) (insert " "))
  5578. ((equal (char-after) ?\ ) (forward-char 1))))))
  5579. (defun org-reduced-level (l)
  5580. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5581. (defun org-get-legal-level (level &optional change)
  5582. "Rectify a level change under the influence of `org-odd-levels-only'
  5583. LEVEL is a current level, CHANGE is by how much the level should be
  5584. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5585. even level numbers will become the next higher odd number."
  5586. (if org-odd-levels-only
  5587. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5588. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5589. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5590. (max 1 (+ level change))))
  5591. (defun org-promote ()
  5592. "Promote the current heading higher up the tree.
  5593. If the region is active in `transient-mark-mode', promote all headings
  5594. in the region."
  5595. (org-back-to-heading t)
  5596. (let* ((level (save-match-data (funcall outline-level)))
  5597. (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
  5598. (diff (abs (- level (length up-head) -1))))
  5599. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5600. (replace-match up-head nil t)
  5601. ;; Fixup tag positioning
  5602. (and org-auto-align-tags (org-set-tags nil t))
  5603. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  5604. (defun org-demote ()
  5605. "Demote the current heading lower down the tree.
  5606. If the region is active in `transient-mark-mode', demote all headings
  5607. in the region."
  5608. (org-back-to-heading t)
  5609. (let* ((level (save-match-data (funcall outline-level)))
  5610. (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
  5611. (diff (abs (- level (length down-head) -1))))
  5612. (replace-match down-head nil t)
  5613. ;; Fixup tag positioning
  5614. (and org-auto-align-tags (org-set-tags nil t))
  5615. (if org-adapt-indentation (org-fixup-indentation diff))))
  5616. (defun org-map-tree (fun)
  5617. "Call FUN for every heading underneath the current one."
  5618. (org-back-to-heading)
  5619. (let ((level (funcall outline-level)))
  5620. (save-excursion
  5621. (funcall fun)
  5622. (while (and (progn
  5623. (outline-next-heading)
  5624. (> (funcall outline-level) level))
  5625. (not (eobp)))
  5626. (funcall fun)))))
  5627. (defun org-map-region (fun beg end)
  5628. "Call FUN for every heading between BEG and END."
  5629. (let ((org-ignore-region t))
  5630. (save-excursion
  5631. (setq end (copy-marker end))
  5632. (goto-char beg)
  5633. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5634. (< (point) end))
  5635. (funcall fun))
  5636. (while (and (progn
  5637. (outline-next-heading)
  5638. (< (point) end))
  5639. (not (eobp)))
  5640. (funcall fun)))))
  5641. (defun org-fixup-indentation (diff)
  5642. "Change the indentation in the current entry by DIFF
  5643. However, if any line in the current entry has no indentation, or if it
  5644. would end up with no indentation after the change, nothing at all is done."
  5645. (save-excursion
  5646. (let ((end (save-excursion (outline-next-heading)
  5647. (point-marker)))
  5648. (prohibit (if (> diff 0)
  5649. "^\\S-"
  5650. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5651. col)
  5652. (unless (save-excursion (end-of-line 1)
  5653. (re-search-forward prohibit end t))
  5654. (while (and (< (point) end)
  5655. (re-search-forward "^[ \t]+" end t))
  5656. (goto-char (match-end 0))
  5657. (setq col (current-column))
  5658. (if (< diff 0) (replace-match ""))
  5659. (indent-to (+ diff col))))
  5660. (move-marker end nil))))
  5661. (defun org-convert-to-odd-levels ()
  5662. "Convert an org-mode file with all levels allowed to one with odd levels.
  5663. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5664. level 5 etc."
  5665. (interactive)
  5666. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5667. (let ((org-odd-levels-only nil) n)
  5668. (save-excursion
  5669. (goto-char (point-min))
  5670. (while (re-search-forward "^\\*\\*+ " nil t)
  5671. (setq n (- (length (match-string 0)) 2))
  5672. (while (>= (setq n (1- n)) 0)
  5673. (org-demote))
  5674. (end-of-line 1))))))
  5675. (defun org-convert-to-oddeven-levels ()
  5676. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5677. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5678. section with an even level, conversion would destroy the structure of the file. An error
  5679. is signaled in this case."
  5680. (interactive)
  5681. (goto-char (point-min))
  5682. ;; First check if there are no even levels
  5683. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5684. (org-show-context t)
  5685. (error "Not all levels are odd in this file. Conversion not possible."))
  5686. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5687. (let ((org-odd-levels-only nil) n)
  5688. (save-excursion
  5689. (goto-char (point-min))
  5690. (while (re-search-forward "^\\*\\*+ " nil t)
  5691. (setq n (/ (1- (length (match-string 0))) 2))
  5692. (while (>= (setq n (1- n)) 0)
  5693. (org-promote))
  5694. (end-of-line 1))))))
  5695. (defun org-tr-level (n)
  5696. "Make N odd if required."
  5697. (if org-odd-levels-only (1+ (/ n 2)) n))
  5698. ;;; Vertical tree motion, cutting and pasting of subtrees
  5699. (defun org-move-subtree-up (&optional arg)
  5700. "Move the current subtree up past ARG headlines of the same level."
  5701. (interactive "p")
  5702. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5703. (defun org-move-subtree-down (&optional arg)
  5704. "Move the current subtree down past ARG headlines of the same level."
  5705. (interactive "p")
  5706. (setq arg (prefix-numeric-value arg))
  5707. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  5708. 'outline-get-last-sibling))
  5709. (ins-point (make-marker))
  5710. (cnt (abs arg))
  5711. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5712. ;; Select the tree
  5713. (org-back-to-heading)
  5714. (setq beg0 (point))
  5715. (save-excursion
  5716. (setq ne-beg (org-back-over-empty-lines))
  5717. (setq beg (point)))
  5718. (save-match-data
  5719. (save-excursion (outline-end-of-heading)
  5720. (setq folded (org-invisible-p)))
  5721. (outline-end-of-subtree))
  5722. (outline-next-heading)
  5723. (setq ne-end (org-back-over-empty-lines))
  5724. (setq end (point))
  5725. (goto-char beg0)
  5726. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5727. ;; include less whitespace
  5728. (save-excursion
  5729. (goto-char beg)
  5730. (forward-line (- ne-beg ne-end))
  5731. (setq beg (point))))
  5732. ;; Find insertion point, with error handling
  5733. (while (> cnt 0)
  5734. (or (and (funcall movfunc) (looking-at outline-regexp))
  5735. (progn (goto-char beg0)
  5736. (error "Cannot move past superior level or buffer limit")))
  5737. (setq cnt (1- cnt)))
  5738. (if (> arg 0)
  5739. ;; Moving forward - still need to move over subtree
  5740. (progn (org-end-of-subtree t t)
  5741. (save-excursion
  5742. (org-back-over-empty-lines)
  5743. (or (bolp) (newline)))))
  5744. (setq ne-ins (org-back-over-empty-lines))
  5745. (move-marker ins-point (point))
  5746. (setq txt (buffer-substring beg end))
  5747. (delete-region beg end)
  5748. (outline-flag-region (1- beg) beg nil)
  5749. (outline-flag-region (1- (point)) (point) nil)
  5750. (insert txt)
  5751. (or (bolp) (insert "\n"))
  5752. (setq ins-end (point))
  5753. (goto-char ins-point)
  5754. (org-skip-whitespace)
  5755. (when (and (< arg 0)
  5756. (org-first-sibling-p)
  5757. (> ne-ins ne-beg))
  5758. ;; Move whitespace back to beginning
  5759. (save-excursion
  5760. (goto-char ins-end)
  5761. (let ((kill-whole-line t))
  5762. (kill-line (- ne-ins ne-beg)) (point)))
  5763. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5764. (move-marker ins-point nil)
  5765. (org-compact-display-after-subtree-move)
  5766. (unless folded
  5767. (org-show-entry)
  5768. (show-children)
  5769. (org-cycle-hide-drawers 'children))))
  5770. (defvar org-subtree-clip ""
  5771. "Clipboard for cut and paste of subtrees.
  5772. This is actually only a copy of the kill, because we use the normal kill
  5773. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5774. (defvar org-subtree-clip-folded nil
  5775. "Was the last copied subtree folded?
  5776. This is used to fold the tree back after pasting.")
  5777. (defun org-cut-subtree (&optional n)
  5778. "Cut the current subtree into the clipboard.
  5779. With prefix arg N, cut this many sequential subtrees.
  5780. This is a short-hand for marking the subtree and then cutting it."
  5781. (interactive "p")
  5782. (org-copy-subtree n 'cut))
  5783. (defun org-copy-subtree (&optional n cut)
  5784. "Cut the current subtree into the clipboard.
  5785. With prefix arg N, cut this many sequential subtrees.
  5786. This is a short-hand for marking the subtree and then copying it.
  5787. If CUT is non-nil, actually cut the subtree."
  5788. (interactive "p")
  5789. (let (beg end folded (beg0 (point)))
  5790. (if (interactive-p)
  5791. (org-back-to-heading nil) ; take what looks like a subtree
  5792. (org-back-to-heading t)) ; take what is really there
  5793. (org-back-over-empty-lines)
  5794. (setq beg (point))
  5795. (skip-chars-forward " \t\r\n")
  5796. (save-match-data
  5797. (save-excursion (outline-end-of-heading)
  5798. (setq folded (org-invisible-p)))
  5799. (condition-case nil
  5800. (outline-forward-same-level (1- n))
  5801. (error nil))
  5802. (org-end-of-subtree t t))
  5803. (org-back-over-empty-lines)
  5804. (setq end (point))
  5805. (goto-char beg0)
  5806. (when (> end beg)
  5807. (setq org-subtree-clip-folded folded)
  5808. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5809. (setq org-subtree-clip (current-kill 0))
  5810. (message "%s: Subtree(s) with %d characters"
  5811. (if cut "Cut" "Copied")
  5812. (length org-subtree-clip)))))
  5813. (defun org-paste-subtree (&optional level tree)
  5814. "Paste the clipboard as a subtree, with modification of headline level.
  5815. The entire subtree is promoted or demoted in order to match a new headline
  5816. level. By default, the new level is derived from the visible headings
  5817. before and after the insertion point, and taken to be the inferior headline
  5818. level of the two. So if the previous visible heading is level 3 and the
  5819. next is level 4 (or vice versa), level 4 will be used for insertion.
  5820. This makes sure that the subtree remains an independent subtree and does
  5821. not swallow low level entries.
  5822. You can also force a different level, either by using a numeric prefix
  5823. argument, or by inserting the heading marker by hand. For example, if the
  5824. cursor is after \"*****\", then the tree will be shifted to level 5.
  5825. If you want to insert the tree as is, just use \\[yank].
  5826. If optional TREE is given, use this text instead of the kill ring."
  5827. (interactive "P")
  5828. (unless (org-kill-is-subtree-p tree)
  5829. (error "%s"
  5830. (substitute-command-keys
  5831. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5832. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  5833. (^re (concat "^\\(" outline-regexp "\\)"))
  5834. (re (concat "\\(" outline-regexp "\\)"))
  5835. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5836. (old-level (if (string-match ^re txt)
  5837. (- (match-end 0) (match-beginning 0) 1)
  5838. -1))
  5839. (force-level (cond (level (prefix-numeric-value level))
  5840. ((string-match
  5841. ^re_ (buffer-substring (point-at-bol) (point)))
  5842. (- (match-end 1) (match-beginning 1)))
  5843. (t nil)))
  5844. (previous-level (save-excursion
  5845. (condition-case nil
  5846. (progn
  5847. (outline-previous-visible-heading 1)
  5848. (if (looking-at re)
  5849. (- (match-end 0) (match-beginning 0) 1)
  5850. 1))
  5851. (error 1))))
  5852. (next-level (save-excursion
  5853. (condition-case nil
  5854. (progn
  5855. (or (looking-at outline-regexp)
  5856. (outline-next-visible-heading 1))
  5857. (if (looking-at re)
  5858. (- (match-end 0) (match-beginning 0) 1)
  5859. 1))
  5860. (error 1))))
  5861. (new-level (or force-level (max previous-level next-level)))
  5862. (shift (if (or (= old-level -1)
  5863. (= new-level -1)
  5864. (= old-level new-level))
  5865. 0
  5866. (- new-level old-level)))
  5867. (delta (if (> shift 0) -1 1))
  5868. (func (if (> shift 0) 'org-demote 'org-promote))
  5869. (org-odd-levels-only nil)
  5870. beg end)
  5871. ;; Remove the forced level indicator
  5872. (if force-level
  5873. (delete-region (point-at-bol) (point)))
  5874. ;; Paste
  5875. (beginning-of-line 1)
  5876. (org-back-over-empty-lines) ;; FIXME: correct fix????
  5877. (setq beg (point))
  5878. (insert-before-markers txt) ;; FIXME: correct fix????
  5879. (unless (string-match "\n\\'" txt) (insert "\n"))
  5880. (setq end (point))
  5881. (goto-char beg)
  5882. (skip-chars-forward " \t\n\r")
  5883. (setq beg (point))
  5884. ;; Shift if necessary
  5885. (unless (= shift 0)
  5886. (save-restriction
  5887. (narrow-to-region beg end)
  5888. (while (not (= shift 0))
  5889. (org-map-region func (point-min) (point-max))
  5890. (setq shift (+ delta shift)))
  5891. (goto-char (point-min))))
  5892. (when (interactive-p)
  5893. (message "Clipboard pasted as level %d subtree" new-level))
  5894. (if (and kill-ring
  5895. (eq org-subtree-clip (current-kill 0))
  5896. org-subtree-clip-folded)
  5897. ;; The tree was folded before it was killed/copied
  5898. (hide-subtree))))
  5899. (defun org-kill-is-subtree-p (&optional txt)
  5900. "Check if the current kill is an outline subtree, or a set of trees.
  5901. Returns nil if kill does not start with a headline, or if the first
  5902. headline level is not the largest headline level in the tree.
  5903. So this will actually accept several entries of equal levels as well,
  5904. which is OK for `org-paste-subtree'.
  5905. If optional TXT is given, check this string instead of the current kill."
  5906. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5907. (start-level (and kill
  5908. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5909. org-outline-regexp "\\)")
  5910. kill)
  5911. (- (match-end 2) (match-beginning 2) 1)))
  5912. (re (concat "^" org-outline-regexp))
  5913. (start (1+ (match-beginning 2))))
  5914. (if (not start-level)
  5915. (progn
  5916. nil) ;; does not even start with a heading
  5917. (catch 'exit
  5918. (while (setq start (string-match re kill (1+ start)))
  5919. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5920. (throw 'exit nil)))
  5921. t))))
  5922. (defun org-narrow-to-subtree ()
  5923. "Narrow buffer to the current subtree."
  5924. (interactive)
  5925. (save-excursion
  5926. (narrow-to-region
  5927. (progn (org-back-to-heading) (point))
  5928. (progn (org-end-of-subtree t t) (point)))))
  5929. ;;; Outline Sorting
  5930. (defun org-sort (with-case)
  5931. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5932. Optional argument WITH-CASE means sort case-sensitively."
  5933. (interactive "P")
  5934. (if (org-at-table-p)
  5935. (org-call-with-arg 'org-table-sort-lines with-case)
  5936. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5937. (defvar org-priority-regexp) ; defined later in the file
  5938. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  5939. "Sort entries on a certain level of an outline tree.
  5940. If there is an active region, the entries in the region are sorted.
  5941. Else, if the cursor is before the first entry, sort the top-level items.
  5942. Else, the children of the entry at point are sorted.
  5943. Sorting can be alphabetically, numerically, and by date/time as given by
  5944. the first time stamp in the entry. The command prompts for the sorting
  5945. type unless it has been given to the function through the SORTING-TYPE
  5946. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5947. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5948. called with point at the beginning of the record. It must return either
  5949. a string or a number that should serve as the sorting key for that record.
  5950. Comparing entries ignores case by default. However, with an optional argument
  5951. WITH-CASE, the sorting considers case as well."
  5952. (interactive "P")
  5953. (let ((case-func (if with-case 'identity 'downcase))
  5954. start beg end stars re re2
  5955. txt what tmp plain-list-p)
  5956. ;; Find beginning and end of region to sort
  5957. (cond
  5958. ((org-region-active-p)
  5959. ;; we will sort the region
  5960. (setq end (region-end)
  5961. what "region")
  5962. (goto-char (region-beginning))
  5963. (if (not (org-on-heading-p)) (outline-next-heading))
  5964. (setq start (point)))
  5965. ((org-at-item-p)
  5966. ;; we will sort this plain list
  5967. (org-beginning-of-item-list) (setq start (point))
  5968. (org-end-of-item-list) (setq end (point))
  5969. (goto-char start)
  5970. (setq plain-list-p t
  5971. what "plain list"))
  5972. ((or (org-on-heading-p)
  5973. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5974. ;; we will sort the children of the current headline
  5975. (org-back-to-heading)
  5976. (setq start (point)
  5977. end (progn (org-end-of-subtree t t)
  5978. (org-back-over-empty-lines)
  5979. (point))
  5980. what "children")
  5981. (goto-char start)
  5982. (show-subtree)
  5983. (outline-next-heading))
  5984. (t
  5985. ;; we will sort the top-level entries in this file
  5986. (goto-char (point-min))
  5987. (or (org-on-heading-p) (outline-next-heading))
  5988. (setq start (point) end (point-max) what "top-level")
  5989. (goto-char start)
  5990. (show-all)))
  5991. (setq beg (point))
  5992. (if (>= beg end) (error "Nothing to sort"))
  5993. (unless plain-list-p
  5994. (looking-at "\\(\\*+\\)")
  5995. (setq stars (match-string 1)
  5996. re (concat "^" (regexp-quote stars) " +")
  5997. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5998. txt (buffer-substring beg end))
  5999. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6000. (if (and (not (equal stars "*")) (string-match re2 txt))
  6001. (error "Region to sort contains a level above the first entry")))
  6002. (unless sorting-type
  6003. (message
  6004. (if plain-list-p
  6005. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6006. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  6007. what)
  6008. (setq sorting-type (read-char-exclusive))
  6009. (and (= (downcase sorting-type) ?f)
  6010. (setq getkey-func
  6011. (completing-read "Sort using function: "
  6012. obarray 'fboundp t nil nil))
  6013. (setq getkey-func (intern getkey-func)))
  6014. (and (= (downcase sorting-type) ?r)
  6015. (setq property
  6016. (completing-read "Property: "
  6017. (mapcar 'list (org-buffer-property-keys t))
  6018. nil t))))
  6019. (message "Sorting entries...")
  6020. (save-restriction
  6021. (narrow-to-region start end)
  6022. (let ((dcst (downcase sorting-type))
  6023. (now (current-time)))
  6024. (sort-subr
  6025. (/= dcst sorting-type)
  6026. ;; This function moves to the beginning character of the "record" to
  6027. ;; be sorted.
  6028. (if plain-list-p
  6029. (lambda nil
  6030. (if (org-at-item-p) t (goto-char (point-max))))
  6031. (lambda nil
  6032. (if (re-search-forward re nil t)
  6033. (goto-char (match-beginning 0))
  6034. (goto-char (point-max)))))
  6035. ;; This function moves to the last character of the "record" being
  6036. ;; sorted.
  6037. (if plain-list-p
  6038. 'org-end-of-item
  6039. (lambda nil
  6040. (save-match-data
  6041. (condition-case nil
  6042. (outline-forward-same-level 1)
  6043. (error
  6044. (goto-char (point-max)))))))
  6045. ;; This function returns the value that gets sorted against.
  6046. (if plain-list-p
  6047. (lambda nil
  6048. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6049. (cond
  6050. ((= dcst ?n)
  6051. (string-to-number (buffer-substring (match-end 0)
  6052. (point-at-eol))))
  6053. ((= dcst ?a)
  6054. (buffer-substring (match-end 0) (point-at-eol)))
  6055. ((= dcst ?t)
  6056. (if (re-search-forward org-ts-regexp
  6057. (point-at-eol) t)
  6058. (org-time-string-to-time (match-string 0))
  6059. now))
  6060. ((= dcst ?f)
  6061. (if getkey-func
  6062. (progn
  6063. (setq tmp (funcall getkey-func))
  6064. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6065. tmp)
  6066. (error "Invalid key function `%s'" getkey-func)))
  6067. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6068. (lambda nil
  6069. (cond
  6070. ((= dcst ?n)
  6071. (if (looking-at outline-regexp)
  6072. (string-to-number (buffer-substring (match-end 0)
  6073. (point-at-eol)))
  6074. nil))
  6075. ((= dcst ?a)
  6076. (funcall case-func (buffer-substring (point-at-bol)
  6077. (point-at-eol))))
  6078. ((= dcst ?t)
  6079. (if (re-search-forward org-ts-regexp
  6080. (save-excursion
  6081. (forward-line 2)
  6082. (point)) t)
  6083. (org-time-string-to-time (match-string 0))
  6084. now))
  6085. ((= dcst ?p)
  6086. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6087. (string-to-char (match-string 2))
  6088. org-default-priority))
  6089. ((= dcst ?r)
  6090. (or (org-entry-get nil property) ""))
  6091. ((= dcst ?f)
  6092. (if getkey-func
  6093. (progn
  6094. (setq tmp (funcall getkey-func))
  6095. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6096. tmp)
  6097. (error "Invalid key function `%s'" getkey-func)))
  6098. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6099. nil
  6100. (cond
  6101. ((= dcst ?a) 'string<)
  6102. ((= dcst ?t) 'time-less-p)
  6103. (t nil)))))
  6104. (message "Sorting entries...done")))
  6105. (defun org-do-sort (table what &optional with-case sorting-type)
  6106. "Sort TABLE of WHAT according to SORTING-TYPE.
  6107. The user will be prompted for the SORTING-TYPE if the call to this
  6108. function does not specify it. WHAT is only for the prompt, to indicate
  6109. what is being sorted. The sorting key will be extracted from
  6110. the car of the elements of the table.
  6111. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6112. (unless sorting-type
  6113. (message
  6114. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6115. what)
  6116. (setq sorting-type (read-char-exclusive)))
  6117. (let ((dcst (downcase sorting-type))
  6118. extractfun comparefun)
  6119. ;; Define the appropriate functions
  6120. (cond
  6121. ((= dcst ?n)
  6122. (setq extractfun 'string-to-number
  6123. comparefun (if (= dcst sorting-type) '< '>)))
  6124. ((= dcst ?a)
  6125. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6126. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6127. comparefun (if (= dcst sorting-type)
  6128. 'string<
  6129. (lambda (a b) (and (not (string< a b))
  6130. (not (string= a b)))))))
  6131. ((= dcst ?t)
  6132. (setq extractfun
  6133. (lambda (x)
  6134. (if (string-match org-ts-regexp x)
  6135. (time-to-seconds
  6136. (org-time-string-to-time (match-string 0 x)))
  6137. 0))
  6138. comparefun (if (= dcst sorting-type) '< '>)))
  6139. (t (error "Invalid sorting type `%c'" sorting-type)))
  6140. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6141. table)
  6142. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6143. ;;;; Plain list items, including checkboxes
  6144. ;;; Plain list items
  6145. (defun org-at-item-p ()
  6146. "Is point in a line starting a hand-formatted item?"
  6147. (let ((llt org-plain-list-ordered-item-terminator))
  6148. (save-excursion
  6149. (goto-char (point-at-bol))
  6150. (looking-at
  6151. (cond
  6152. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6153. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6154. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  6155. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  6156. (defun org-in-item-p ()
  6157. "It the cursor inside a plain list item.
  6158. Does not have to be the first line."
  6159. (save-excursion
  6160. (condition-case nil
  6161. (progn
  6162. (org-beginning-of-item)
  6163. (org-at-item-p)
  6164. t)
  6165. (error nil))))
  6166. (defun org-insert-item (&optional checkbox)
  6167. "Insert a new item at the current level.
  6168. Return t when things worked, nil when we are not in an item."
  6169. (when (save-excursion
  6170. (condition-case nil
  6171. (progn
  6172. (org-beginning-of-item)
  6173. (org-at-item-p)
  6174. (if (org-invisible-p) (error "Invisible item"))
  6175. t)
  6176. (error nil)))
  6177. (let* ((bul (match-string 0))
  6178. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  6179. (match-end 0)))
  6180. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  6181. pos)
  6182. (cond
  6183. ((and (org-at-item-p) (<= (point) eow))
  6184. ;; before the bullet
  6185. (beginning-of-line 1)
  6186. (open-line (if blank 2 1)))
  6187. ((<= (point) eow)
  6188. (beginning-of-line 1))
  6189. (t (newline (if blank 2 1))))
  6190. (insert bul (if checkbox "[ ]" ""))
  6191. (just-one-space)
  6192. (setq pos (point))
  6193. (end-of-line 1)
  6194. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  6195. (org-maybe-renumber-ordered-list)
  6196. (and checkbox (org-update-checkbox-count-maybe))
  6197. t))
  6198. ;;; Checkboxes
  6199. (defun org-at-item-checkbox-p ()
  6200. "Is point at a line starting a plain-list item with a checklet?"
  6201. (and (org-at-item-p)
  6202. (save-excursion
  6203. (goto-char (match-end 0))
  6204. (skip-chars-forward " \t")
  6205. (looking-at "\\[[- X]\\]"))))
  6206. (defun org-toggle-checkbox (&optional arg)
  6207. "Toggle the checkbox in the current line."
  6208. (interactive "P")
  6209. (catch 'exit
  6210. (let (beg end status (firstnew 'unknown))
  6211. (cond
  6212. ((org-region-active-p)
  6213. (setq beg (region-beginning) end (region-end)))
  6214. ((org-on-heading-p)
  6215. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  6216. ((org-at-item-checkbox-p)
  6217. (let ((pos (point)))
  6218. (replace-match
  6219. (cond (arg "[-]")
  6220. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  6221. (t "[ ]"))
  6222. t t)
  6223. (goto-char pos))
  6224. (throw 'exit t))
  6225. (t (error "Not at a checkbox or heading, and no active region")))
  6226. (save-excursion
  6227. (goto-char beg)
  6228. (while (< (point) end)
  6229. (when (org-at-item-checkbox-p)
  6230. (setq status (equal (match-string 0) "[X]"))
  6231. (when (eq firstnew 'unknown)
  6232. (setq firstnew (not status)))
  6233. (replace-match
  6234. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  6235. (beginning-of-line 2)))))
  6236. (org-update-checkbox-count-maybe))
  6237. (defun org-update-checkbox-count-maybe ()
  6238. "Update checkbox statistics unless turned off by user."
  6239. (when org-provide-checkbox-statistics
  6240. (org-update-checkbox-count)))
  6241. (defun org-update-checkbox-count (&optional all)
  6242. "Update the checkbox statistics in the current section.
  6243. This will find all statistic cookies like [57%] and [6/12] and update them
  6244. with the current numbers. With optional prefix argument ALL, do this for
  6245. the whole buffer."
  6246. (interactive "P")
  6247. (save-excursion
  6248. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  6249. (beg (condition-case nil
  6250. (progn (outline-back-to-heading) (point))
  6251. (error (point-min))))
  6252. (end (move-marker (make-marker)
  6253. (progn (outline-next-heading) (point))))
  6254. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  6255. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  6256. b1 e1 f1 c-on c-off lim (cstat 0))
  6257. (when all
  6258. (goto-char (point-min))
  6259. (outline-next-heading)
  6260. (setq beg (point) end (point-max)))
  6261. (goto-char beg)
  6262. (while (re-search-forward re end t)
  6263. (setq cstat (1+ cstat)
  6264. b1 (match-beginning 0)
  6265. e1 (match-end 0)
  6266. f1 (match-beginning 1)
  6267. lim (cond
  6268. ((org-on-heading-p) (outline-next-heading) (point))
  6269. ((org-at-item-p) (org-end-of-item) (point))
  6270. (t nil))
  6271. c-on 0 c-off 0)
  6272. (goto-char e1)
  6273. (when lim
  6274. (while (re-search-forward re-box lim t)
  6275. (if (member (match-string 2) '("[ ]" "[-]"))
  6276. (setq c-off (1+ c-off))
  6277. (setq c-on (1+ c-on))))
  6278. ; (delete-region b1 e1)
  6279. (goto-char b1)
  6280. (insert (if f1
  6281. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  6282. (format "[%d/%d]" c-on (+ c-on c-off))))
  6283. (and (looking-at "\\[.*?\\]")
  6284. (replace-match ""))))
  6285. (when (interactive-p)
  6286. (message "Checkbox satistics updated %s (%d places)"
  6287. (if all "in entire file" "in current outline entry") cstat)))))
  6288. (defun org-get-checkbox-statistics-face ()
  6289. "Select the face for checkbox statistics.
  6290. The face will be `org-done' when all relevant boxes are checked. Otherwise
  6291. it will be `org-todo'."
  6292. (if (match-end 1)
  6293. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  6294. (if (and (> (match-end 2) (match-beginning 2))
  6295. (equal (match-string 2) (match-string 3)))
  6296. 'org-done
  6297. 'org-todo)))
  6298. (defun org-get-indentation (&optional line)
  6299. "Get the indentation of the current line, interpreting tabs.
  6300. When LINE is given, assume it represents a line and compute its indentation."
  6301. (if line
  6302. (if (string-match "^ *" (org-remove-tabs line))
  6303. (match-end 0))
  6304. (save-excursion
  6305. (beginning-of-line 1)
  6306. (skip-chars-forward " \t")
  6307. (current-column))))
  6308. (defun org-remove-tabs (s &optional width)
  6309. "Replace tabulators in S with spaces.
  6310. Assumes that s is a single line, starting in column 0."
  6311. (setq width (or width tab-width))
  6312. (while (string-match "\t" s)
  6313. (setq s (replace-match
  6314. (make-string
  6315. (- (* width (/ (+ (match-beginning 0) width) width))
  6316. (match-beginning 0)) ?\ )
  6317. t t s)))
  6318. s)
  6319. (defun org-fix-indentation (line ind)
  6320. "Fix indentation in LINE.
  6321. IND is a cons cell with target and minimum indentation.
  6322. If the current indenation in LINE is smaller than the minimum,
  6323. leave it alone. If it is larger than ind, set it to the target."
  6324. (let* ((l (org-remove-tabs line))
  6325. (i (org-get-indentation l))
  6326. (i1 (car ind)) (i2 (cdr ind)))
  6327. (if (>= i i2) (setq l (substring line i2)))
  6328. (if (> i1 0)
  6329. (concat (make-string i1 ?\ ) l)
  6330. l)))
  6331. (defcustom org-empty-line-terminates-plain-lists nil
  6332. "Non-nil means, an empty line ends all plain list levels.
  6333. When nil, empty lines are part of the preceeding item."
  6334. :group 'org-plain-lists
  6335. :type 'boolean)
  6336. (defun org-beginning-of-item ()
  6337. "Go to the beginning of the current hand-formatted item.
  6338. If the cursor is not in an item, throw an error."
  6339. (interactive)
  6340. (let ((pos (point))
  6341. (limit (save-excursion
  6342. (condition-case nil
  6343. (progn
  6344. (org-back-to-heading)
  6345. (beginning-of-line 2) (point))
  6346. (error (point-min)))))
  6347. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6348. ind ind1)
  6349. (if (org-at-item-p)
  6350. (beginning-of-line 1)
  6351. (beginning-of-line 1)
  6352. (skip-chars-forward " \t")
  6353. (setq ind (current-column))
  6354. (if (catch 'exit
  6355. (while t
  6356. (beginning-of-line 0)
  6357. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  6358. (if (looking-at "[ \t]*$")
  6359. (setq ind1 ind-empty)
  6360. (skip-chars-forward " \t")
  6361. (setq ind1 (current-column)))
  6362. (if (< ind1 ind)
  6363. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  6364. nil
  6365. (goto-char pos)
  6366. (error "Not in an item")))))
  6367. (defun org-end-of-item ()
  6368. "Go to the end of the current hand-formatted item.
  6369. If the cursor is not in an item, throw an error."
  6370. (interactive)
  6371. (let* ((pos (point))
  6372. ind1
  6373. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  6374. (limit (save-excursion (outline-next-heading) (point)))
  6375. (ind (save-excursion
  6376. (org-beginning-of-item)
  6377. (skip-chars-forward " \t")
  6378. (current-column)))
  6379. (end (catch 'exit
  6380. (while t
  6381. (beginning-of-line 2)
  6382. (if (eobp) (throw 'exit (point)))
  6383. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  6384. (if (looking-at "[ \t]*$")
  6385. (setq ind1 ind-empty)
  6386. (skip-chars-forward " \t")
  6387. (setq ind1 (current-column)))
  6388. (if (<= ind1 ind)
  6389. (throw 'exit (point-at-bol)))))))
  6390. (if end
  6391. (goto-char end)
  6392. (goto-char pos)
  6393. (error "Not in an item"))))
  6394. (defun org-next-item ()
  6395. "Move to the beginning of the next item in the current plain list.
  6396. Error if not at a plain list, or if this is the last item in the list."
  6397. (interactive)
  6398. (let (ind ind1 (pos (point)))
  6399. (org-beginning-of-item)
  6400. (setq ind (org-get-indentation))
  6401. (org-end-of-item)
  6402. (setq ind1 (org-get-indentation))
  6403. (unless (and (org-at-item-p) (= ind ind1))
  6404. (goto-char pos)
  6405. (error "On last item"))))
  6406. (defun org-previous-item ()
  6407. "Move to the beginning of the previous item in the current plain list.
  6408. Error if not at a plain list, or if this is the first item in the list."
  6409. (interactive)
  6410. (let (beg ind ind1 (pos (point)))
  6411. (org-beginning-of-item)
  6412. (setq beg (point))
  6413. (setq ind (org-get-indentation))
  6414. (goto-char beg)
  6415. (catch 'exit
  6416. (while t
  6417. (beginning-of-line 0)
  6418. (if (looking-at "[ \t]*$")
  6419. nil
  6420. (if (<= (setq ind1 (org-get-indentation)) ind)
  6421. (throw 'exit t)))))
  6422. (condition-case nil
  6423. (if (or (not (org-at-item-p))
  6424. (< ind1 (1- ind)))
  6425. (error "")
  6426. (org-beginning-of-item))
  6427. (error (goto-char pos)
  6428. (error "On first item")))))
  6429. (defun org-first-list-item-p ()
  6430. "Is this heading the item in a plain list?"
  6431. (unless (org-at-item-p)
  6432. (error "Not at a plain list item"))
  6433. (org-beginning-of-item)
  6434. (= (point) (save-excursion (org-beginning-of-item-list))))
  6435. (defun org-move-item-down ()
  6436. "Move the plain list item at point down, i.e. swap with following item.
  6437. Subitems (items with larger indentation) are considered part of the item,
  6438. so this really moves item trees."
  6439. (interactive)
  6440. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  6441. (org-beginning-of-item)
  6442. (setq beg0 (point))
  6443. (save-excursion
  6444. (setq ne-beg (org-back-over-empty-lines))
  6445. (setq beg (point)))
  6446. (goto-char beg0)
  6447. (setq ind (org-get-indentation))
  6448. (org-end-of-item)
  6449. (setq end0 (point))
  6450. (setq ind1 (org-get-indentation))
  6451. (setq ne-end (org-back-over-empty-lines))
  6452. (setq end (point))
  6453. (goto-char beg0)
  6454. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  6455. ;; include less whitespace
  6456. (save-excursion
  6457. (goto-char beg)
  6458. (forward-line (- ne-beg ne-end))
  6459. (setq beg (point))))
  6460. (goto-char end0)
  6461. (if (and (org-at-item-p) (= ind ind1))
  6462. (progn
  6463. (org-end-of-item)
  6464. (org-back-over-empty-lines)
  6465. (setq txt (buffer-substring beg end))
  6466. (save-excursion
  6467. (delete-region beg end))
  6468. (setq pos (point))
  6469. (insert txt)
  6470. (goto-char pos) (org-skip-whitespace)
  6471. (org-maybe-renumber-ordered-list))
  6472. (goto-char pos)
  6473. (error "Cannot move this item further down"))))
  6474. (defun org-move-item-up (arg)
  6475. "Move the plain list item at point up, i.e. swap with previous item.
  6476. Subitems (items with larger indentation) are considered part of the item,
  6477. so this really moves item trees."
  6478. (interactive "p")
  6479. (let (beg beg0 end end0 ind ind1 (pos (point)) txt
  6480. ne-beg ne-end ne-ins ins-end)
  6481. (org-beginning-of-item)
  6482. (setq beg0 (point))
  6483. (setq ind (org-get-indentation))
  6484. (save-excursion
  6485. (setq ne-beg (org-back-over-empty-lines))
  6486. (setq beg (point)))
  6487. (goto-char beg0)
  6488. (org-end-of-item)
  6489. (setq ne-end (org-back-over-empty-lines))
  6490. (setq end (point))
  6491. (goto-char beg0)
  6492. (catch 'exit
  6493. (while t
  6494. (beginning-of-line 0)
  6495. (if (looking-at "[ \t]*$")
  6496. (if org-empty-line-terminates-plain-lists
  6497. (progn
  6498. (goto-char pos)
  6499. (error "Cannot move this item further up"))
  6500. nil)
  6501. (if (<= (setq ind1 (org-get-indentation)) ind)
  6502. (throw 'exit t)))))
  6503. (condition-case nil
  6504. (org-beginning-of-item)
  6505. (error (goto-char beg)
  6506. (error "Cannot move this item further up")))
  6507. (setq ind1 (org-get-indentation))
  6508. (if (and (org-at-item-p) (= ind ind1))
  6509. (progn
  6510. (setq ne-ins (org-back-over-empty-lines))
  6511. (setq txt (buffer-substring beg end))
  6512. (save-excursion
  6513. (delete-region beg end))
  6514. (setq pos (point))
  6515. (insert txt)
  6516. (setq ins-end (point))
  6517. (goto-char pos) (org-skip-whitespace)
  6518. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  6519. ;; Move whitespace back to beginning
  6520. (save-excursion
  6521. (goto-char ins-end)
  6522. (let ((kill-whole-line t))
  6523. (kill-line (- ne-ins ne-beg)) (point)))
  6524. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6525. (org-maybe-renumber-ordered-list))
  6526. (goto-char pos)
  6527. (error "Cannot move this item further up"))))
  6528. (defun org-maybe-renumber-ordered-list ()
  6529. "Renumber the ordered list at point if setup allows it.
  6530. This tests the user option `org-auto-renumber-ordered-lists' before
  6531. doing the renumbering."
  6532. (interactive)
  6533. (when (and org-auto-renumber-ordered-lists
  6534. (org-at-item-p))
  6535. (if (match-beginning 3)
  6536. (org-renumber-ordered-list 1)
  6537. (org-fix-bullet-type))))
  6538. (defun org-maybe-renumber-ordered-list-safe ()
  6539. (condition-case nil
  6540. (save-excursion
  6541. (org-maybe-renumber-ordered-list))
  6542. (error nil)))
  6543. (defun org-cycle-list-bullet (&optional which)
  6544. "Cycle through the different itemize/enumerate bullets.
  6545. This cycle the entire list level through the sequence:
  6546. `-' -> `+' -> `*' -> `1.' -> `1)'
  6547. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  6548. 0 meand `-', 1 means `+' etc."
  6549. (interactive "P")
  6550. (org-preserve-lc
  6551. (org-beginning-of-item-list)
  6552. (org-at-item-p)
  6553. (beginning-of-line 1)
  6554. (let ((current (match-string 0))
  6555. (prevp (eq which 'previous))
  6556. new)
  6557. (setq new (cond
  6558. ((and (numberp which)
  6559. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  6560. ((string-match "-" current) (if prevp "1)" "+"))
  6561. ((string-match "\\+" current)
  6562. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  6563. ((string-match "\\*" current) (if prevp "+" "1."))
  6564. ((string-match "\\." current) (if prevp "*" "1)"))
  6565. ((string-match ")" current) (if prevp "1." "-"))
  6566. (t (error "This should not happen"))))
  6567. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  6568. (org-fix-bullet-type)
  6569. (org-maybe-renumber-ordered-list))))
  6570. (defun org-get-string-indentation (s)
  6571. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  6572. (let ((n -1) (i 0) (w tab-width) c)
  6573. (catch 'exit
  6574. (while (< (setq n (1+ n)) (length s))
  6575. (setq c (aref s n))
  6576. (cond ((= c ?\ ) (setq i (1+ i)))
  6577. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  6578. (t (throw 'exit t)))))
  6579. i))
  6580. (defun org-renumber-ordered-list (arg)
  6581. "Renumber an ordered plain list.
  6582. Cursor needs to be in the first line of an item, the line that starts
  6583. with something like \"1.\" or \"2)\"."
  6584. (interactive "p")
  6585. (unless (and (org-at-item-p)
  6586. (match-beginning 3))
  6587. (error "This is not an ordered list"))
  6588. (let ((line (org-current-line))
  6589. (col (current-column))
  6590. (ind (org-get-string-indentation
  6591. (buffer-substring (point-at-bol) (match-beginning 3))))
  6592. ;; (term (substring (match-string 3) -1))
  6593. ind1 (n (1- arg))
  6594. fmt)
  6595. ;; find where this list begins
  6596. (org-beginning-of-item-list)
  6597. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  6598. (setq fmt (concat "%d" (match-string 1)))
  6599. (beginning-of-line 0)
  6600. ;; walk forward and replace these numbers
  6601. (catch 'exit
  6602. (while t
  6603. (catch 'next
  6604. (beginning-of-line 2)
  6605. (if (eobp) (throw 'exit nil))
  6606. (if (looking-at "[ \t]*$") (throw 'next nil))
  6607. (skip-chars-forward " \t") (setq ind1 (current-column))
  6608. (if (> ind1 ind) (throw 'next t))
  6609. (if (< ind1 ind) (throw 'exit t))
  6610. (if (not (org-at-item-p)) (throw 'exit nil))
  6611. (delete-region (match-beginning 2) (match-end 2))
  6612. (goto-char (match-beginning 2))
  6613. (insert (format fmt (setq n (1+ n)))))))
  6614. (goto-line line)
  6615. (move-to-column col)))
  6616. (defun org-fix-bullet-type ()
  6617. "Make sure all items in this list have the same bullet as the firsst item."
  6618. (interactive)
  6619. (unless (org-at-item-p) (error "This is not a list"))
  6620. (let ((line (org-current-line))
  6621. (col (current-column))
  6622. (ind (current-indentation))
  6623. ind1 bullet)
  6624. ;; find where this list begins
  6625. (org-beginning-of-item-list)
  6626. (beginning-of-line 1)
  6627. ;; find out what the bullet type is
  6628. (looking-at "[ \t]*\\(\\S-+\\)")
  6629. (setq bullet (match-string 1))
  6630. ;; walk forward and replace these numbers
  6631. (beginning-of-line 0)
  6632. (catch 'exit
  6633. (while t
  6634. (catch 'next
  6635. (beginning-of-line 2)
  6636. (if (eobp) (throw 'exit nil))
  6637. (if (looking-at "[ \t]*$") (throw 'next nil))
  6638. (skip-chars-forward " \t") (setq ind1 (current-column))
  6639. (if (> ind1 ind) (throw 'next t))
  6640. (if (< ind1 ind) (throw 'exit t))
  6641. (if (not (org-at-item-p)) (throw 'exit nil))
  6642. (skip-chars-forward " \t")
  6643. (looking-at "\\S-+")
  6644. (replace-match bullet))))
  6645. (goto-line line)
  6646. (move-to-column col)
  6647. (if (string-match "[0-9]" bullet)
  6648. (org-renumber-ordered-list 1))))
  6649. (defun org-beginning-of-item-list ()
  6650. "Go to the beginning of the current item list.
  6651. I.e. to the first item in this list."
  6652. (interactive)
  6653. (org-beginning-of-item)
  6654. (let ((pos (point-at-bol))
  6655. (ind (org-get-indentation))
  6656. ind1)
  6657. ;; find where this list begins
  6658. (catch 'exit
  6659. (while t
  6660. (catch 'next
  6661. (beginning-of-line 0)
  6662. (if (looking-at "[ \t]*$")
  6663. (throw (if (bobp) 'exit 'next) t))
  6664. (skip-chars-forward " \t") (setq ind1 (current-column))
  6665. (if (or (< ind1 ind)
  6666. (and (= ind1 ind)
  6667. (not (org-at-item-p)))
  6668. (bobp))
  6669. (throw 'exit t)
  6670. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  6671. (goto-char pos)))
  6672. (defun org-end-of-item-list ()
  6673. "Go to the end of the current item list.
  6674. I.e. to the text after the last item."
  6675. (interactive)
  6676. (org-beginning-of-item)
  6677. (let ((pos (point-at-bol))
  6678. (ind (org-get-indentation))
  6679. ind1)
  6680. ;; find where this list begins
  6681. (catch 'exit
  6682. (while t
  6683. (catch 'next
  6684. (beginning-of-line 2)
  6685. (if (looking-at "[ \t]*$")
  6686. (throw (if (eobp) 'exit 'next) t))
  6687. (skip-chars-forward " \t") (setq ind1 (current-column))
  6688. (if (or (< ind1 ind)
  6689. (and (= ind1 ind)
  6690. (not (org-at-item-p)))
  6691. (eobp))
  6692. (progn
  6693. (setq pos (point-at-bol))
  6694. (throw 'exit t))))))
  6695. (goto-char pos)))
  6696. (defvar org-last-indent-begin-marker (make-marker))
  6697. (defvar org-last-indent-end-marker (make-marker))
  6698. (defun org-outdent-item (arg)
  6699. "Outdent a local list item."
  6700. (interactive "p")
  6701. (org-indent-item (- arg)))
  6702. (defun org-indent-item (arg)
  6703. "Indent a local list item."
  6704. (interactive "p")
  6705. (unless (org-at-item-p)
  6706. (error "Not on an item"))
  6707. (save-excursion
  6708. (let (beg end ind ind1 tmp delta ind-down ind-up)
  6709. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  6710. (setq beg org-last-indent-begin-marker
  6711. end org-last-indent-end-marker)
  6712. (org-beginning-of-item)
  6713. (setq beg (move-marker org-last-indent-begin-marker (point)))
  6714. (org-end-of-item)
  6715. (setq end (move-marker org-last-indent-end-marker (point))))
  6716. (goto-char beg)
  6717. (setq tmp (org-item-indent-positions)
  6718. ind (car tmp)
  6719. ind-down (nth 2 tmp)
  6720. ind-up (nth 1 tmp)
  6721. delta (if (> arg 0)
  6722. (if ind-down (- ind-down ind) 2)
  6723. (if ind-up (- ind-up ind) -2)))
  6724. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  6725. (while (< (point) end)
  6726. (beginning-of-line 1)
  6727. (skip-chars-forward " \t") (setq ind1 (current-column))
  6728. (delete-region (point-at-bol) (point))
  6729. (or (eolp) (indent-to-column (+ ind1 delta)))
  6730. (beginning-of-line 2))))
  6731. (org-fix-bullet-type)
  6732. (org-maybe-renumber-ordered-list-safe)
  6733. (save-excursion
  6734. (beginning-of-line 0)
  6735. (condition-case nil (org-beginning-of-item) (error nil))
  6736. (org-maybe-renumber-ordered-list-safe)))
  6737. (defun org-item-indent-positions ()
  6738. "Return indentation for plain list items.
  6739. This returns a list with three values: The current indentation, the
  6740. parent indentation and the indentation a child should habe.
  6741. Assumes cursor in item line."
  6742. (let* ((bolpos (point-at-bol))
  6743. (ind (org-get-indentation))
  6744. ind-down ind-up pos)
  6745. (save-excursion
  6746. (org-beginning-of-item-list)
  6747. (skip-chars-backward "\n\r \t")
  6748. (when (org-in-item-p)
  6749. (org-beginning-of-item)
  6750. (setq ind-up (org-get-indentation))))
  6751. (setq pos (point))
  6752. (save-excursion
  6753. (cond
  6754. ((and (condition-case nil (progn (org-previous-item) t)
  6755. (error nil))
  6756. (or (forward-char 1) t)
  6757. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  6758. (setq ind-down (org-get-indentation)))
  6759. ((and (goto-char pos)
  6760. (org-at-item-p))
  6761. (goto-char (match-end 0))
  6762. (skip-chars-forward " \t")
  6763. (setq ind-down (current-column)))))
  6764. (list ind ind-up ind-down)))
  6765. ;;; The orgstruct minor mode
  6766. ;; Define a minor mode which can be used in other modes in order to
  6767. ;; integrate the org-mode structure editing commands.
  6768. ;; This is really a hack, because the org-mode structure commands use
  6769. ;; keys which normally belong to the major mode. Here is how it
  6770. ;; works: The minor mode defines all the keys necessary to operate the
  6771. ;; structure commands, but wraps the commands into a function which
  6772. ;; tests if the cursor is currently at a headline or a plain list
  6773. ;; item. If that is the case, the structure command is used,
  6774. ;; temporarily setting many Org-mode variables like regular
  6775. ;; expressions for filling etc. However, when any of those keys is
  6776. ;; used at a different location, function uses `key-binding' to look
  6777. ;; up if the key has an associated command in another currently active
  6778. ;; keymap (minor modes, major mode, global), and executes that
  6779. ;; command. There might be problems if any of the keys is otherwise
  6780. ;; used as a prefix key.
  6781. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6782. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6783. ;; addresses this by checking explicitly for both bindings.
  6784. (defvar orgstruct-mode-map (make-sparse-keymap)
  6785. "Keymap for the minor `orgstruct-mode'.")
  6786. (defvar org-local-vars nil
  6787. "List of local variables, for use by `orgstruct-mode'")
  6788. ;;;###autoload
  6789. (define-minor-mode orgstruct-mode
  6790. "Toggle the minor more `orgstruct-mode'.
  6791. This mode is for using Org-mode structure commands in other modes.
  6792. The following key behave as if Org-mode was active, if the cursor
  6793. is on a headline, or on a plain list item (both in the definition
  6794. of Org-mode).
  6795. M-up Move entry/item up
  6796. M-down Move entry/item down
  6797. M-left Promote
  6798. M-right Demote
  6799. M-S-up Move entry/item up
  6800. M-S-down Move entry/item down
  6801. M-S-left Promote subtree
  6802. M-S-right Demote subtree
  6803. M-q Fill paragraph and items like in Org-mode
  6804. C-c ^ Sort entries
  6805. C-c - Cycle list bullet
  6806. TAB Cycle item visibility
  6807. M-RET Insert new heading/item
  6808. S-M-RET Insert new TODO heading / Chekbox item
  6809. C-c C-c Set tags / toggle checkbox"
  6810. nil " OrgStruct" nil
  6811. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6812. ;;;###autoload
  6813. (defun turn-on-orgstruct ()
  6814. "Unconditionally turn on `orgstruct-mode'."
  6815. (orgstruct-mode 1))
  6816. ;;;###autoload
  6817. (defun turn-on-orgstruct++ ()
  6818. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  6819. In addition to setting orgstruct-mode, this also exports all indentation and
  6820. autofilling variables from org-mode into the buffer. Note that turning
  6821. off orgstruct-mode will *not* remove these additional settings."
  6822. (orgstruct-mode 1)
  6823. (let (var val)
  6824. (mapc
  6825. (lambda (x)
  6826. (when (string-match
  6827. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6828. (symbol-name (car x)))
  6829. (setq var (car x) val (nth 1 x))
  6830. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6831. org-local-vars)))
  6832. (defun orgstruct-error ()
  6833. "Error when there is no default binding for a structure key."
  6834. (interactive)
  6835. (error "This key has no function outside structure elements"))
  6836. (defun orgstruct-setup ()
  6837. "Setup orgstruct keymaps."
  6838. (let ((nfunc 0)
  6839. (bindings
  6840. (list
  6841. '([(meta up)] org-metaup)
  6842. '([(meta down)] org-metadown)
  6843. '([(meta left)] org-metaleft)
  6844. '([(meta right)] org-metaright)
  6845. '([(meta shift up)] org-shiftmetaup)
  6846. '([(meta shift down)] org-shiftmetadown)
  6847. '([(meta shift left)] org-shiftmetaleft)
  6848. '([(meta shift right)] org-shiftmetaright)
  6849. '([(shift up)] org-shiftup)
  6850. '([(shift down)] org-shiftdown)
  6851. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6852. '("\M-q" fill-paragraph)
  6853. '("\C-c^" org-sort)
  6854. '("\C-c-" org-cycle-list-bullet)))
  6855. elt key fun cmd)
  6856. (while (setq elt (pop bindings))
  6857. (setq nfunc (1+ nfunc))
  6858. (setq key (org-key (car elt))
  6859. fun (nth 1 elt)
  6860. cmd (orgstruct-make-binding fun nfunc key))
  6861. (org-defkey orgstruct-mode-map key cmd))
  6862. ;; Special treatment needed for TAB and RET
  6863. (org-defkey orgstruct-mode-map [(tab)]
  6864. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6865. (org-defkey orgstruct-mode-map "\C-i"
  6866. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6867. (org-defkey orgstruct-mode-map "\M-\C-m"
  6868. (orgstruct-make-binding 'org-insert-heading 105
  6869. "\M-\C-m" [(meta return)]))
  6870. (org-defkey orgstruct-mode-map [(meta return)]
  6871. (orgstruct-make-binding 'org-insert-heading 106
  6872. [(meta return)] "\M-\C-m"))
  6873. (org-defkey orgstruct-mode-map [(shift meta return)]
  6874. (orgstruct-make-binding 'org-insert-todo-heading 107
  6875. [(meta return)] "\M-\C-m"))
  6876. (unless org-local-vars
  6877. (setq org-local-vars (org-get-local-variables)))
  6878. t))
  6879. (defun orgstruct-make-binding (fun n &rest keys)
  6880. "Create a function for binding in the structure minor mode.
  6881. FUN is the command to call inside a table. N is used to create a unique
  6882. command name. KEYS are keys that should be checked in for a command
  6883. to execute outside of tables."
  6884. (eval
  6885. (list 'defun
  6886. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6887. '(arg)
  6888. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6889. "Outside of structure, run the binding of `"
  6890. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6891. "'.")
  6892. '(interactive "p")
  6893. (list 'if
  6894. '(org-context-p 'headline 'item)
  6895. (list 'org-run-like-in-org-mode (list 'quote fun))
  6896. (list 'let '(orgstruct-mode)
  6897. (list 'call-interactively
  6898. (append '(or)
  6899. (mapcar (lambda (k)
  6900. (list 'key-binding k))
  6901. keys)
  6902. '('orgstruct-error))))))))
  6903. (defun org-context-p (&rest contexts)
  6904. "Check if local context is and of CONTEXTS.
  6905. Possible values in the list of contexts are `table', `headline', and `item'."
  6906. (let ((pos (point)))
  6907. (goto-char (point-at-bol))
  6908. (prog1 (or (and (memq 'table contexts)
  6909. (looking-at "[ \t]*|"))
  6910. (and (memq 'headline contexts)
  6911. (looking-at "\\*+"))
  6912. (and (memq 'item contexts)
  6913. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  6914. (goto-char pos))))
  6915. (defun org-get-local-variables ()
  6916. "Return a list of all local variables in an org-mode buffer."
  6917. (let (varlist)
  6918. (with-current-buffer (get-buffer-create "*Org tmp*")
  6919. (erase-buffer)
  6920. (org-mode)
  6921. (setq varlist (buffer-local-variables)))
  6922. (kill-buffer "*Org tmp*")
  6923. (delq nil
  6924. (mapcar
  6925. (lambda (x)
  6926. (setq x
  6927. (if (symbolp x)
  6928. (list x)
  6929. (list (car x) (list 'quote (cdr x)))))
  6930. (if (string-match
  6931. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6932. (symbol-name (car x)))
  6933. x nil))
  6934. varlist))))
  6935. ;;;###autoload
  6936. (defun org-run-like-in-org-mode (cmd)
  6937. (unless org-local-vars
  6938. (setq org-local-vars (org-get-local-variables)))
  6939. (eval (list 'let org-local-vars
  6940. (list 'call-interactively (list 'quote cmd)))))
  6941. ;;;; Archiving
  6942. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  6943. (defun org-archive-subtree (&optional find-done)
  6944. "Move the current subtree to the archive.
  6945. The archive can be a certain top-level heading in the current file, or in
  6946. a different file. The tree will be moved to that location, the subtree
  6947. heading be marked DONE, and the current time will be added.
  6948. When called with prefix argument FIND-DONE, find whole trees without any
  6949. open TODO items and archive them (after getting confirmation from the user).
  6950. If the cursor is not at a headline when this comand is called, try all level
  6951. 1 trees. If the cursor is on a headline, only try the direct children of
  6952. this heading."
  6953. (interactive "P")
  6954. (if find-done
  6955. (org-archive-all-done)
  6956. ;; Save all relevant TODO keyword-relatex variables
  6957. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  6958. (tr-org-todo-keywords-1 org-todo-keywords-1)
  6959. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  6960. (tr-org-done-keywords org-done-keywords)
  6961. (tr-org-todo-regexp org-todo-regexp)
  6962. (tr-org-todo-line-regexp org-todo-line-regexp)
  6963. (tr-org-odd-levels-only org-odd-levels-only)
  6964. (this-buffer (current-buffer))
  6965. (org-archive-location org-archive-location)
  6966. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  6967. ;; start of variables that will be used for saving context
  6968. ;; The compiler complains about them - keep them anyway!
  6969. (file (abbreviate-file-name (buffer-file-name)))
  6970. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  6971. (time (format-time-string
  6972. (substring (cdr org-time-stamp-formats) 1 -1)
  6973. (current-time)))
  6974. afile heading buffer level newfile-p
  6975. category todo priority
  6976. ;; start of variables that will be used for savind context
  6977. ltags itags prop)
  6978. ;; Try to find a local archive location
  6979. (save-excursion
  6980. (save-restriction
  6981. (widen)
  6982. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  6983. (if (and prop (string-match "\\S-" prop))
  6984. (setq org-archive-location prop)
  6985. (if (or (re-search-backward re nil t)
  6986. (re-search-forward re nil t))
  6987. (setq org-archive-location (match-string 1))))))
  6988. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  6989. (progn
  6990. (setq afile (format (match-string 1 org-archive-location)
  6991. (file-name-nondirectory buffer-file-name))
  6992. heading (match-string 2 org-archive-location)))
  6993. (error "Invalid `org-archive-location'"))
  6994. (if (> (length afile) 0)
  6995. (setq newfile-p (not (file-exists-p afile))
  6996. buffer (find-file-noselect afile))
  6997. (setq buffer (current-buffer)))
  6998. (unless buffer
  6999. (error "Cannot access file \"%s\"" afile))
  7000. (if (and (> (length heading) 0)
  7001. (string-match "^\\*+" heading))
  7002. (setq level (match-end 0))
  7003. (setq heading nil level 0))
  7004. (save-excursion
  7005. (org-back-to-heading t)
  7006. ;; Get context information that will be lost by moving the tree
  7007. (org-refresh-category-properties)
  7008. (setq category (org-get-category)
  7009. todo (and (looking-at org-todo-line-regexp)
  7010. (match-string 2))
  7011. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  7012. ltags (org-get-tags)
  7013. itags (org-delete-all ltags (org-get-tags-at)))
  7014. (setq ltags (mapconcat 'identity ltags " ")
  7015. itags (mapconcat 'identity itags " "))
  7016. ;; We first only copy, in case something goes wrong
  7017. ;; we need to protect this-command, to avoid kill-region sets it,
  7018. ;; which would lead to duplication of subtrees
  7019. (let (this-command) (org-copy-subtree))
  7020. (set-buffer buffer)
  7021. ;; Enforce org-mode for the archive buffer
  7022. (if (not (org-mode-p))
  7023. ;; Force the mode for future visits.
  7024. (let ((org-insert-mode-line-in-empty-file t)
  7025. (org-inhibit-startup t))
  7026. (call-interactively 'org-mode)))
  7027. (when newfile-p
  7028. (goto-char (point-max))
  7029. (insert (format "\nArchived entries from file %s\n\n"
  7030. (buffer-file-name this-buffer))))
  7031. ;; Force the TODO keywords of the original buffer
  7032. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  7033. (org-todo-keywords-1 tr-org-todo-keywords-1)
  7034. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  7035. (org-done-keywords tr-org-done-keywords)
  7036. (org-todo-regexp tr-org-todo-regexp)
  7037. (org-todo-line-regexp tr-org-todo-line-regexp)
  7038. (org-odd-levels-only
  7039. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  7040. org-odd-levels-only
  7041. tr-org-odd-levels-only)))
  7042. (goto-char (point-min))
  7043. (if heading
  7044. (progn
  7045. (if (re-search-forward
  7046. (concat "^" (regexp-quote heading)
  7047. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  7048. nil t)
  7049. (goto-char (match-end 0))
  7050. ;; Heading not found, just insert it at the end
  7051. (goto-char (point-max))
  7052. (or (bolp) (insert "\n"))
  7053. (insert "\n" heading "\n")
  7054. (end-of-line 0))
  7055. ;; Make the subtree visible
  7056. (show-subtree)
  7057. (org-end-of-subtree t)
  7058. (skip-chars-backward " \t\r\n")
  7059. (and (looking-at "[ \t\r\n]*")
  7060. (replace-match "\n\n")))
  7061. ;; No specific heading, just go to end of file.
  7062. (goto-char (point-max)) (insert "\n"))
  7063. ;; Paste
  7064. (org-paste-subtree (org-get-legal-level level 1))
  7065. ;; Mark the entry as done
  7066. (when (and org-archive-mark-done
  7067. (looking-at org-todo-line-regexp)
  7068. (or (not (match-end 2))
  7069. (not (member (match-string 2) org-done-keywords))))
  7070. (let (org-log-done)
  7071. (org-todo
  7072. (car (or (member org-archive-mark-done org-done-keywords)
  7073. org-done-keywords)))))
  7074. ;; Add the context info
  7075. (when org-archive-save-context-info
  7076. (let ((l org-archive-save-context-info) e n v)
  7077. (while (setq e (pop l))
  7078. (when (and (setq v (symbol-value e))
  7079. (stringp v) (string-match "\\S-" v))
  7080. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  7081. (org-entry-put (point) n v)))))
  7082. ;; Save the buffer, if it is not the same buffer.
  7083. (if (not (eq this-buffer buffer)) (save-buffer))))
  7084. ;; Here we are back in the original buffer. Everything seems to have
  7085. ;; worked. So now cut the tree and finish up.
  7086. (let (this-command) (org-cut-subtree))
  7087. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  7088. (message "Subtree archived %s"
  7089. (if (eq this-buffer buffer)
  7090. (concat "under heading: " heading)
  7091. (concat "in file: " (abbreviate-file-name afile)))))))
  7092. (defun org-refresh-category-properties ()
  7093. "Refresh category text properties in teh buffer."
  7094. (let ((def-cat (cond
  7095. ((null org-category)
  7096. (if buffer-file-name
  7097. (file-name-sans-extension
  7098. (file-name-nondirectory buffer-file-name))
  7099. "???"))
  7100. ((symbolp org-category) (symbol-name org-category))
  7101. (t org-category)))
  7102. beg end cat pos optionp)
  7103. (org-unmodified
  7104. (save-excursion
  7105. (save-restriction
  7106. (widen)
  7107. (goto-char (point-min))
  7108. (put-text-property (point) (point-max) 'org-category def-cat)
  7109. (while (re-search-forward
  7110. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7111. (setq pos (match-end 0)
  7112. optionp (equal (char-after (match-beginning 0)) ?#)
  7113. cat (org-trim (match-string 2)))
  7114. (if optionp
  7115. (setq beg (point-at-bol) end (point-max))
  7116. (org-back-to-heading t)
  7117. (setq beg (point) end (org-end-of-subtree t t)))
  7118. (put-text-property beg end 'org-category cat)
  7119. (goto-char pos)))))))
  7120. (defun org-archive-all-done (&optional tag)
  7121. "Archive sublevels of the current tree without open TODO items.
  7122. If the cursor is not on a headline, try all level 1 trees. If
  7123. it is on a headline, try all direct children.
  7124. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  7125. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  7126. (rea (concat ".*:" org-archive-tag ":"))
  7127. (begm (make-marker))
  7128. (endm (make-marker))
  7129. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  7130. "Move subtree to archive (no open TODO items)? "))
  7131. beg end (cntarch 0))
  7132. (if (org-on-heading-p)
  7133. (progn
  7134. (setq re1 (concat "^" (regexp-quote
  7135. (make-string
  7136. (1+ (- (match-end 0) (match-beginning 0) 1))
  7137. ?*))
  7138. " "))
  7139. (move-marker begm (point))
  7140. (move-marker endm (org-end-of-subtree t)))
  7141. (setq re1 "^* ")
  7142. (move-marker begm (point-min))
  7143. (move-marker endm (point-max)))
  7144. (save-excursion
  7145. (goto-char begm)
  7146. (while (re-search-forward re1 endm t)
  7147. (setq beg (match-beginning 0)
  7148. end (save-excursion (org-end-of-subtree t) (point)))
  7149. (goto-char beg)
  7150. (if (re-search-forward re end t)
  7151. (goto-char end)
  7152. (goto-char beg)
  7153. (if (and (or (not tag) (not (looking-at rea)))
  7154. (y-or-n-p question))
  7155. (progn
  7156. (if tag
  7157. (org-toggle-tag org-archive-tag 'on)
  7158. (org-archive-subtree))
  7159. (setq cntarch (1+ cntarch)))
  7160. (goto-char end)))))
  7161. (message "%d trees archived" cntarch)))
  7162. (defun org-cycle-hide-drawers (state)
  7163. "Re-hide all drawers after a visibility state change."
  7164. (when (and (org-mode-p)
  7165. (not (memq state '(overview folded))))
  7166. (save-excursion
  7167. (let* ((globalp (memq state '(contents all)))
  7168. (beg (if globalp (point-min) (point)))
  7169. (end (if globalp (point-max) (org-end-of-subtree t))))
  7170. (goto-char beg)
  7171. (while (re-search-forward org-drawer-regexp end t)
  7172. (org-flag-drawer t))))))
  7173. (defun org-flag-drawer (flag)
  7174. (save-excursion
  7175. (beginning-of-line 1)
  7176. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  7177. (let ((b (match-end 0))
  7178. (outline-regexp org-outline-regexp))
  7179. (if (re-search-forward
  7180. "^[ \t]*:END:"
  7181. (save-excursion (outline-next-heading) (point)) t)
  7182. (outline-flag-region b (point-at-eol) flag)
  7183. (error ":END: line missing"))))))
  7184. (defun org-cycle-hide-archived-subtrees (state)
  7185. "Re-hide all archived subtrees after a visibility state change."
  7186. (when (and (not org-cycle-open-archived-trees)
  7187. (not (memq state '(overview folded))))
  7188. (save-excursion
  7189. (let* ((globalp (memq state '(contents all)))
  7190. (beg (if globalp (point-min) (point)))
  7191. (end (if globalp (point-max) (org-end-of-subtree t))))
  7192. (org-hide-archived-subtrees beg end)
  7193. (goto-char beg)
  7194. (if (looking-at (concat ".*:" org-archive-tag ":"))
  7195. (message "%s" (substitute-command-keys
  7196. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  7197. (defun org-force-cycle-archived ()
  7198. "Cycle subtree even if it is archived."
  7199. (interactive)
  7200. (setq this-command 'org-cycle)
  7201. (let ((org-cycle-open-archived-trees t))
  7202. (call-interactively 'org-cycle)))
  7203. (defun org-hide-archived-subtrees (beg end)
  7204. "Re-hide all archived subtrees after a visibility state change."
  7205. (save-excursion
  7206. (let* ((re (concat ":" org-archive-tag ":")))
  7207. (goto-char beg)
  7208. (while (re-search-forward re end t)
  7209. (and (org-on-heading-p) (hide-subtree))
  7210. (org-end-of-subtree t)))))
  7211. (defun org-toggle-tag (tag &optional onoff)
  7212. "Toggle the tag TAG for the current line.
  7213. If ONOFF is `on' or `off', don't toggle but set to this state."
  7214. (unless (org-on-heading-p t) (error "Not on headling"))
  7215. (let (res current)
  7216. (save-excursion
  7217. (beginning-of-line)
  7218. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  7219. (point-at-eol) t)
  7220. (progn
  7221. (setq current (match-string 1))
  7222. (replace-match ""))
  7223. (setq current ""))
  7224. (setq current (nreverse (org-split-string current ":")))
  7225. (cond
  7226. ((eq onoff 'on)
  7227. (setq res t)
  7228. (or (member tag current) (push tag current)))
  7229. ((eq onoff 'off)
  7230. (or (not (member tag current)) (setq current (delete tag current))))
  7231. (t (if (member tag current)
  7232. (setq current (delete tag current))
  7233. (setq res t)
  7234. (push tag current))))
  7235. (end-of-line 1)
  7236. (if current
  7237. (progn
  7238. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  7239. (org-set-tags nil t))
  7240. (delete-horizontal-space))
  7241. (run-hooks 'org-after-tags-change-hook))
  7242. res))
  7243. (defun org-toggle-archive-tag (&optional arg)
  7244. "Toggle the archive tag for the current headline.
  7245. With prefix ARG, check all children of current headline and offer tagging
  7246. the children that do not contain any open TODO items."
  7247. (interactive "P")
  7248. (if arg
  7249. (org-archive-all-done 'tag)
  7250. (let (set)
  7251. (save-excursion
  7252. (org-back-to-heading t)
  7253. (setq set (org-toggle-tag org-archive-tag))
  7254. (when set (hide-subtree)))
  7255. (and set (beginning-of-line 1))
  7256. (message "Subtree %s" (if set "archived" "unarchived")))))
  7257. ;;;; Tables
  7258. ;;; The table editor
  7259. ;; Watch out: Here we are talking about two different kind of tables.
  7260. ;; Most of the code is for the tables created with the Org-mode table editor.
  7261. ;; Sometimes, we talk about tables created and edited with the table.el
  7262. ;; Emacs package. We call the former org-type tables, and the latter
  7263. ;; table.el-type tables.
  7264. (defun org-before-change-function (beg end)
  7265. "Every change indicates that a table might need an update."
  7266. (setq org-table-may-need-update t))
  7267. (defconst org-table-line-regexp "^[ \t]*|"
  7268. "Detects an org-type table line.")
  7269. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7270. "Detects an org-type table line.")
  7271. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7272. "Detects a table line marked for automatic recalculation.")
  7273. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7274. "Detects a table line marked for automatic recalculation.")
  7275. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7276. "Detects a table line marked for automatic recalculation.")
  7277. (defconst org-table-hline-regexp "^[ \t]*|-"
  7278. "Detects an org-type table hline.")
  7279. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7280. "Detects a table-type table hline.")
  7281. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7282. "Detects an org-type or table-type table.")
  7283. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7284. "Searching from within a table (any type) this finds the first line
  7285. outside the table.")
  7286. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7287. "Searching from within a table (any type) this finds the first line
  7288. outside the table.")
  7289. (defvar org-table-last-highlighted-reference nil)
  7290. (defvar org-table-formula-history nil)
  7291. (defvar org-table-column-names nil
  7292. "Alist with column names, derived from the `!' line.")
  7293. (defvar org-table-column-name-regexp nil
  7294. "Regular expression matching the current column names.")
  7295. (defvar org-table-local-parameters nil
  7296. "Alist with parameter names, derived from the `$' line.")
  7297. (defvar org-table-named-field-locations nil
  7298. "Alist with locations of named fields.")
  7299. (defvar org-table-current-line-types nil
  7300. "Table row types, non-nil only for the duration of a comand.")
  7301. (defvar org-table-current-begin-line nil
  7302. "Table begin line, non-nil only for the duration of a comand.")
  7303. (defvar org-table-current-begin-pos nil
  7304. "Table begin position, non-nil only for the duration of a comand.")
  7305. (defvar org-table-dlines nil
  7306. "Vector of data line line numbers in the current table.")
  7307. (defvar org-table-hlines nil
  7308. "Vector of hline line numbers in the current table.")
  7309. (defconst org-table-range-regexp
  7310. "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
  7311. ;; 1 2 3 4 5
  7312. "Regular expression for matching ranges in formulas.")
  7313. (defconst org-table-range-regexp2
  7314. (concat
  7315. "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
  7316. "\\.\\."
  7317. "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  7318. "Match a range for reference display.")
  7319. (defconst org-table-translate-regexp
  7320. (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  7321. "Match a reference that needs translation, for reference display.")
  7322. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  7323. (defun org-table-create-with-table.el ()
  7324. "Use the table.el package to insert a new table.
  7325. If there is already a table at point, convert between Org-mode tables
  7326. and table.el tables."
  7327. (interactive)
  7328. (require 'table)
  7329. (cond
  7330. ((org-at-table.el-p)
  7331. (if (y-or-n-p "Convert table to Org-mode table? ")
  7332. (org-table-convert)))
  7333. ((org-at-table-p)
  7334. (if (y-or-n-p "Convert table to table.el table? ")
  7335. (org-table-convert)))
  7336. (t (call-interactively 'table-insert))))
  7337. (defun org-table-create-or-convert-from-region (arg)
  7338. "Convert region to table, or create an empty table.
  7339. If there is an active region, convert it to a table, using the function
  7340. `org-table-convert-region'. See the documentation of that function
  7341. to learn how the prefix argument is interpreted to determine the field
  7342. separator.
  7343. If there is no such region, create an empty table with `org-table-create'."
  7344. (interactive "P")
  7345. (if (org-region-active-p)
  7346. (org-table-convert-region (region-beginning) (region-end) arg)
  7347. (org-table-create arg)))
  7348. (defun org-table-create (&optional size)
  7349. "Query for a size and insert a table skeleton.
  7350. SIZE is a string Columns x Rows like for example \"3x2\"."
  7351. (interactive "P")
  7352. (unless size
  7353. (setq size (read-string
  7354. (concat "Table size Columns x Rows [e.g. "
  7355. org-table-default-size "]: ")
  7356. "" nil org-table-default-size)))
  7357. (let* ((pos (point))
  7358. (indent (make-string (current-column) ?\ ))
  7359. (split (org-split-string size " *x *"))
  7360. (rows (string-to-number (nth 1 split)))
  7361. (columns (string-to-number (car split)))
  7362. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7363. "\n")))
  7364. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7365. (point-at-bol) (point)))
  7366. (beginning-of-line 1)
  7367. (newline))
  7368. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7369. (dotimes (i rows) (insert line))
  7370. (goto-char pos)
  7371. (if (> rows 1)
  7372. ;; Insert a hline after the first row.
  7373. (progn
  7374. (end-of-line 1)
  7375. (insert "\n|-")
  7376. (goto-char pos)))
  7377. (org-table-align)))
  7378. (defun org-table-convert-region (beg0 end0 &optional separator)
  7379. "Convert region to a table.
  7380. The region goes from BEG0 to END0, but these borders will be moved
  7381. slightly, to make sure a beginning of line in the first line is included.
  7382. SEPARATOR specifies the field separator in the lines. It can have the
  7383. following values:
  7384. '(4) Use the comma as a field separator
  7385. '(16) Use a TAB as field separator
  7386. integer When a number, use that many spaces as field separator
  7387. nil When nil, the command tries to be smart and figure out the
  7388. separator in the following way:
  7389. - when each line contains a TAB, assume TAB-separated material
  7390. - when each line contains a comme, assume CSV material
  7391. - else, assume one or more SPACE charcters as separator."
  7392. (interactive "rP")
  7393. (let* ((beg (min beg0 end0))
  7394. (end (max beg0 end0))
  7395. re)
  7396. (goto-char beg)
  7397. (beginning-of-line 1)
  7398. (setq beg (move-marker (make-marker) (point)))
  7399. (goto-char end)
  7400. (if (bolp) (backward-char 1) (end-of-line 1))
  7401. (setq end (move-marker (make-marker) (point)))
  7402. ;; Get the right field separator
  7403. (unless separator
  7404. (goto-char beg)
  7405. (setq separator
  7406. (cond
  7407. ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
  7408. ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
  7409. (t 1))))
  7410. (setq re (cond
  7411. ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
  7412. ((equal separator '(16)) "^\\|\t")
  7413. ((integerp separator)
  7414. (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
  7415. (t (error "This should not happen"))))
  7416. (goto-char beg)
  7417. (while (re-search-forward re end t)
  7418. (replace-match "| " t t))
  7419. (goto-char beg)
  7420. (insert " ")
  7421. (org-table-align)))
  7422. (defun org-table-import (file arg)
  7423. "Import FILE as a table.
  7424. The file is assumed to be tab-separated. Such files can be produced by most
  7425. spreadsheet and database applications. If no tabs (at least one per line)
  7426. are found, lines will be split on whitespace into fields."
  7427. (interactive "f\nP")
  7428. (or (bolp) (newline))
  7429. (let ((beg (point))
  7430. (pm (point-max)))
  7431. (insert-file-contents file)
  7432. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7433. (defun org-table-export ()
  7434. "Export table as a tab-separated file.
  7435. Such a file can be imported into a spreadsheet program like Excel."
  7436. (interactive)
  7437. (let* ((beg (org-table-begin))
  7438. (end (org-table-end))
  7439. (table (buffer-substring beg end))
  7440. (file (read-file-name "Export table to: "))
  7441. buf)
  7442. (unless (or (not (file-exists-p file))
  7443. (y-or-n-p (format "Overwrite file %s? " file)))
  7444. (error "Abort"))
  7445. (with-current-buffer (find-file-noselect file)
  7446. (setq buf (current-buffer))
  7447. (erase-buffer)
  7448. (fundamental-mode)
  7449. (insert table)
  7450. (goto-char (point-min))
  7451. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7452. (replace-match "" t t)
  7453. (end-of-line 1))
  7454. (goto-char (point-min))
  7455. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7456. (replace-match "" t t)
  7457. (goto-char (min (1+ (point)) (point-max))))
  7458. (goto-char (point-min))
  7459. (while (re-search-forward "^-[-+]*$" nil t)
  7460. (replace-match "")
  7461. (if (looking-at "\n")
  7462. (delete-char 1)))
  7463. (goto-char (point-min))
  7464. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7465. (replace-match "\t" t t))
  7466. (save-buffer))
  7467. (kill-buffer buf)))
  7468. (defvar org-table-aligned-begin-marker (make-marker)
  7469. "Marker at the beginning of the table last aligned.
  7470. Used to check if cursor still is in that table, to minimize realignment.")
  7471. (defvar org-table-aligned-end-marker (make-marker)
  7472. "Marker at the end of the table last aligned.
  7473. Used to check if cursor still is in that table, to minimize realignment.")
  7474. (defvar org-table-last-alignment nil
  7475. "List of flags for flushright alignment, from the last re-alignment.
  7476. This is being used to correctly align a single field after TAB or RET.")
  7477. (defvar org-table-last-column-widths nil
  7478. "List of max width of fields in each column.
  7479. This is being used to correctly align a single field after TAB or RET.")
  7480. (defvar org-table-overlay-coordinates nil
  7481. "Overlay coordinates after each align of a table.")
  7482. (make-variable-buffer-local 'org-table-overlay-coordinates)
  7483. (defvar org-last-recalc-line nil)
  7484. (defconst org-narrow-column-arrow "=>"
  7485. "Used as display property in narrowed table columns.")
  7486. (defun org-table-align ()
  7487. "Align the table at point by aligning all vertical bars."
  7488. (interactive)
  7489. (let* (
  7490. ;; Limits of table
  7491. (beg (org-table-begin))
  7492. (end (org-table-end))
  7493. ;; Current cursor position
  7494. (linepos (org-current-line))
  7495. (colpos (org-table-current-column))
  7496. (winstart (window-start))
  7497. (winstartline (org-current-line (min winstart (1- (point-max)))))
  7498. lines (new "") lengths l typenums ty fields maxfields i
  7499. column
  7500. (indent "") cnt frac
  7501. rfmt hfmt
  7502. (spaces '(1 . 1))
  7503. (sp1 (car spaces))
  7504. (sp2 (cdr spaces))
  7505. (rfmt1 (concat
  7506. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7507. (hfmt1 (concat
  7508. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7509. emptystrings links dates emph narrow fmax f1 len c e)
  7510. (untabify beg end)
  7511. (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
  7512. ;; Check if we have links or dates
  7513. (goto-char beg)
  7514. (setq links (re-search-forward org-bracket-link-regexp end t))
  7515. (goto-char beg)
  7516. (setq emph (and org-hide-emphasis-markers
  7517. (re-search-forward org-emph-re end t)))
  7518. (goto-char beg)
  7519. (setq dates (and org-display-custom-times
  7520. (re-search-forward org-ts-regexp-both end t)))
  7521. ;; Make sure the link properties are right
  7522. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  7523. ;; Make sure the date properties are right
  7524. (when dates (goto-char beg) (while (org-activate-dates end)))
  7525. (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
  7526. ;; Check if we are narrowing any columns
  7527. (goto-char beg)
  7528. (setq narrow (and org-format-transports-properties-p
  7529. (re-search-forward "<[0-9]+>" end t)))
  7530. ;; Get the rows
  7531. (setq lines (org-split-string
  7532. (buffer-substring beg end) "\n"))
  7533. ;; Store the indentation of the first line
  7534. (if (string-match "^ *" (car lines))
  7535. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7536. ;; Mark the hlines by setting the corresponding element to nil
  7537. ;; At the same time, we remove trailing space.
  7538. (setq lines (mapcar (lambda (l)
  7539. (if (string-match "^ *|-" l)
  7540. nil
  7541. (if (string-match "[ \t]+$" l)
  7542. (substring l 0 (match-beginning 0))
  7543. l)))
  7544. lines))
  7545. ;; Get the data fields by splitting the lines.
  7546. (setq fields (mapcar
  7547. (lambda (l)
  7548. (org-split-string l " *| *"))
  7549. (delq nil (copy-sequence lines))))
  7550. ;; How many fields in the longest line?
  7551. (condition-case nil
  7552. (setq maxfields (apply 'max (mapcar 'length fields)))
  7553. (error
  7554. (kill-region beg end)
  7555. (org-table-create org-table-default-size)
  7556. (error "Empty table - created default table")))
  7557. ;; A list of empty strings to fill any short rows on output
  7558. (setq emptystrings (make-list maxfields ""))
  7559. ;; Check for special formatting.
  7560. (setq i -1)
  7561. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7562. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7563. ;; Check if there is an explicit width specified
  7564. (when narrow
  7565. (setq c column fmax nil)
  7566. (while c
  7567. (setq e (pop c))
  7568. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7569. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7570. ;; Find fields that are wider than fmax, and shorten them
  7571. (when fmax
  7572. (loop for xx in column do
  7573. (when (and (stringp xx)
  7574. (> (org-string-width xx) fmax))
  7575. (org-add-props xx nil
  7576. 'help-echo
  7577. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  7578. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7579. (unless (> f1 1)
  7580. (error "Cannot narrow field starting with wide link \"%s\""
  7581. (match-string 0 xx)))
  7582. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7583. (add-text-properties (- f1 2) f1
  7584. (list 'display org-narrow-column-arrow)
  7585. xx)))))
  7586. ;; Get the maximum width for each column
  7587. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7588. ;; Get the fraction of numbers, to decide about alignment of the column
  7589. (setq cnt 0 frac 0.0)
  7590. (loop for x in column do
  7591. (if (equal x "")
  7592. nil
  7593. (setq frac ( / (+ (* frac cnt)
  7594. (if (string-match org-table-number-regexp x) 1 0))
  7595. (setq cnt (1+ cnt))))))
  7596. (push (>= frac org-table-number-fraction) typenums))
  7597. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7598. ;; Store the alignment of this table, for later editing of single fields
  7599. (setq org-table-last-alignment typenums
  7600. org-table-last-column-widths lengths)
  7601. ;; With invisible characters, `format' does not get the field width right
  7602. ;; So we need to make these fields wide by hand.
  7603. (when (or links emph)
  7604. (loop for i from 0 upto (1- maxfields) do
  7605. (setq len (nth i lengths))
  7606. (loop for j from 0 upto (1- (length fields)) do
  7607. (setq c (nthcdr i (car (nthcdr j fields))))
  7608. (if (and (stringp (car c))
  7609. (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
  7610. ; (string-match org-bracket-link-regexp (car c))
  7611. (< (org-string-width (car c)) len))
  7612. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7613. ;; Compute the formats needed for output of the table
  7614. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7615. (while (setq l (pop lengths))
  7616. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7617. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7618. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7619. (setq rfmt (concat rfmt "\n")
  7620. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7621. (setq new (mapconcat
  7622. (lambda (l)
  7623. (if l (apply 'format rfmt
  7624. (append (pop fields) emptystrings))
  7625. hfmt))
  7626. lines ""))
  7627. ;; Replace the old one
  7628. (delete-region beg end)
  7629. (move-marker end nil)
  7630. (move-marker org-table-aligned-begin-marker (point))
  7631. (insert new)
  7632. (move-marker org-table-aligned-end-marker (point))
  7633. (when (and orgtbl-mode (not (org-mode-p)))
  7634. (goto-char org-table-aligned-begin-marker)
  7635. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  7636. ;; Try to move to the old location
  7637. (goto-line winstartline)
  7638. (setq winstart (point-at-bol))
  7639. (goto-line linepos)
  7640. (set-window-start (selected-window) winstart 'noforce)
  7641. (org-table-goto-column colpos)
  7642. (and org-table-overlay-coordinates (org-table-overlay-coordinates))
  7643. (setq org-table-may-need-update nil)
  7644. ))
  7645. (defun org-string-width (s)
  7646. "Compute width of string, ignoring invisible characters.
  7647. This ignores character with invisibility property `org-link', and also
  7648. characters with property `org-cwidth', because these will become invisible
  7649. upon the next fontification round."
  7650. (let (b l)
  7651. (when (or (eq t buffer-invisibility-spec)
  7652. (assq 'org-link buffer-invisibility-spec))
  7653. (while (setq b (text-property-any 0 (length s)
  7654. 'invisible 'org-link s))
  7655. (setq s (concat (substring s 0 b)
  7656. (substring s (or (next-single-property-change
  7657. b 'invisible s) (length s)))))))
  7658. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7659. (setq s (concat (substring s 0 b)
  7660. (substring s (or (next-single-property-change
  7661. b 'org-cwidth s) (length s))))))
  7662. (setq l (string-width s) b -1)
  7663. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  7664. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  7665. l))
  7666. (defun org-table-begin (&optional table-type)
  7667. "Find the beginning of the table and return its position.
  7668. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7669. (save-excursion
  7670. (if (not (re-search-backward
  7671. (if table-type org-table-any-border-regexp
  7672. org-table-border-regexp)
  7673. nil t))
  7674. (progn (goto-char (point-min)) (point))
  7675. (goto-char (match-beginning 0))
  7676. (beginning-of-line 2)
  7677. (point))))
  7678. (defun org-table-end (&optional table-type)
  7679. "Find the end of the table and return its position.
  7680. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7681. (save-excursion
  7682. (if (not (re-search-forward
  7683. (if table-type org-table-any-border-regexp
  7684. org-table-border-regexp)
  7685. nil t))
  7686. (goto-char (point-max))
  7687. (goto-char (match-beginning 0)))
  7688. (point-marker)))
  7689. (defun org-table-justify-field-maybe (&optional new)
  7690. "Justify the current field, text to left, number to right.
  7691. Optional argument NEW may specify text to replace the current field content."
  7692. (cond
  7693. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7694. ((org-at-table-hline-p))
  7695. ((and (not new)
  7696. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7697. (current-buffer)))
  7698. (< (point) org-table-aligned-begin-marker)
  7699. (>= (point) org-table-aligned-end-marker)))
  7700. ;; This is not the same table, force a full re-align
  7701. (setq org-table-may-need-update t))
  7702. (t ;; realign the current field, based on previous full realign
  7703. (let* ((pos (point)) s
  7704. (col (org-table-current-column))
  7705. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  7706. l f n o e)
  7707. (when (> col 0)
  7708. (skip-chars-backward "^|\n")
  7709. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7710. (progn
  7711. (setq s (match-string 1)
  7712. o (match-string 0)
  7713. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7714. e (not (= (match-beginning 2) (match-end 2))))
  7715. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7716. l (if e "|" (setq org-table-may-need-update t) ""))
  7717. n (format f s))
  7718. (if new
  7719. (if (<= (length new) l) ;; FIXME: length -> str-width?
  7720. (setq n (format f new))
  7721. (setq n (concat new "|") org-table-may-need-update t)))
  7722. (or (equal n o)
  7723. (let (org-table-may-need-update)
  7724. (replace-match n t t))))
  7725. (setq org-table-may-need-update t))
  7726. (goto-char pos))))))
  7727. (defun org-table-next-field ()
  7728. "Go to the next field in the current table, creating new lines as needed.
  7729. Before doing so, re-align the table if necessary."
  7730. (interactive)
  7731. (org-table-maybe-eval-formula)
  7732. (org-table-maybe-recalculate-line)
  7733. (if (and org-table-automatic-realign
  7734. org-table-may-need-update)
  7735. (org-table-align))
  7736. (let ((end (org-table-end)))
  7737. (if (org-at-table-hline-p)
  7738. (end-of-line 1))
  7739. (condition-case nil
  7740. (progn
  7741. (re-search-forward "|" end)
  7742. (if (looking-at "[ \t]*$")
  7743. (re-search-forward "|" end))
  7744. (if (and (looking-at "-")
  7745. org-table-tab-jumps-over-hlines
  7746. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7747. (goto-char (match-beginning 1)))
  7748. (if (looking-at "-")
  7749. (progn
  7750. (beginning-of-line 0)
  7751. (org-table-insert-row 'below))
  7752. (if (looking-at " ") (forward-char 1))))
  7753. (error
  7754. (org-table-insert-row 'below)))))
  7755. (defun org-table-previous-field ()
  7756. "Go to the previous field in the table.
  7757. Before doing so, re-align the table if necessary."
  7758. (interactive)
  7759. (org-table-justify-field-maybe)
  7760. (org-table-maybe-recalculate-line)
  7761. (if (and org-table-automatic-realign
  7762. org-table-may-need-update)
  7763. (org-table-align))
  7764. (if (org-at-table-hline-p)
  7765. (end-of-line 1))
  7766. (re-search-backward "|" (org-table-begin))
  7767. (re-search-backward "|" (org-table-begin))
  7768. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7769. (re-search-backward "|" (org-table-begin)))
  7770. (if (looking-at "| ?")
  7771. (goto-char (match-end 0))))
  7772. (defun org-table-next-row ()
  7773. "Go to the next row (same column) in the current table.
  7774. Before doing so, re-align the table if necessary."
  7775. (interactive)
  7776. (org-table-maybe-eval-formula)
  7777. (org-table-maybe-recalculate-line)
  7778. (if (or (looking-at "[ \t]*$")
  7779. (save-excursion (skip-chars-backward " \t") (bolp)))
  7780. (newline)
  7781. (if (and org-table-automatic-realign
  7782. org-table-may-need-update)
  7783. (org-table-align))
  7784. (let ((col (org-table-current-column)))
  7785. (beginning-of-line 2)
  7786. (if (or (not (org-at-table-p))
  7787. (org-at-table-hline-p))
  7788. (progn
  7789. (beginning-of-line 0)
  7790. (org-table-insert-row 'below)))
  7791. (org-table-goto-column col)
  7792. (skip-chars-backward "^|\n\r")
  7793. (if (looking-at " ") (forward-char 1)))))
  7794. (defun org-table-copy-down (n)
  7795. "Copy a field down in the current column.
  7796. If the field at the cursor is empty, copy into it the content of the nearest
  7797. non-empty field above. With argument N, use the Nth non-empty field.
  7798. If the current field is not empty, it is copied down to the next row, and
  7799. the cursor is moved with it. Therefore, repeating this command causes the
  7800. column to be filled row-by-row.
  7801. If the variable `org-table-copy-increment' is non-nil and the field is an
  7802. integer or a timestamp, it will be incremented while copying. In the case of
  7803. a timestamp, if the cursor is on the year, change the year. If it is on the
  7804. month or the day, change that. Point will stay on the current date field
  7805. in order to easily repeat the interval."
  7806. (interactive "p")
  7807. (let* ((colpos (org-table-current-column))
  7808. (col (current-column))
  7809. (field (org-table-get-field))
  7810. (non-empty (string-match "[^ \t]" field))
  7811. (beg (org-table-begin))
  7812. txt)
  7813. (org-table-check-inside-data-field)
  7814. (if non-empty
  7815. (progn
  7816. (setq txt (org-trim field))
  7817. (org-table-next-row)
  7818. (org-table-blank-field))
  7819. (save-excursion
  7820. (setq txt
  7821. (catch 'exit
  7822. (while (progn (beginning-of-line 1)
  7823. (re-search-backward org-table-dataline-regexp
  7824. beg t))
  7825. (org-table-goto-column colpos t)
  7826. (if (and (looking-at
  7827. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  7828. (= (setq n (1- n)) 0))
  7829. (throw 'exit (match-string 1))))))))
  7830. (if txt
  7831. (progn
  7832. (if (and org-table-copy-increment
  7833. (string-match "^[0-9]+$" txt))
  7834. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  7835. (insert txt)
  7836. (move-to-column col)
  7837. (if (and org-table-copy-increment (org-at-timestamp-p t))
  7838. (org-timestamp-up 1)
  7839. (org-table-maybe-recalculate-line))
  7840. (org-table-align)
  7841. (move-to-column col))
  7842. (error "No non-empty field found"))))
  7843. (defun org-table-check-inside-data-field ()
  7844. "Is point inside a table data field?
  7845. I.e. not on a hline or before the first or after the last column?
  7846. This actually throws an error, so it aborts the current command."
  7847. (if (or (not (org-at-table-p))
  7848. (= (org-table-current-column) 0)
  7849. (org-at-table-hline-p)
  7850. (looking-at "[ \t]*$"))
  7851. (error "Not in table data field")))
  7852. (defvar org-table-clip nil
  7853. "Clipboard for table regions.")
  7854. (defun org-table-blank-field ()
  7855. "Blank the current table field or active region."
  7856. (interactive)
  7857. (org-table-check-inside-data-field)
  7858. (if (and (interactive-p) (org-region-active-p))
  7859. (let (org-table-clip)
  7860. (org-table-cut-region (region-beginning) (region-end)))
  7861. (skip-chars-backward "^|")
  7862. (backward-char 1)
  7863. (if (looking-at "|[^|\n]+")
  7864. (let* ((pos (match-beginning 0))
  7865. (match (match-string 0))
  7866. (len (org-string-width match)))
  7867. (replace-match (concat "|" (make-string (1- len) ?\ )))
  7868. (goto-char (+ 2 pos))
  7869. (substring match 1)))))
  7870. (defun org-table-get-field (&optional n replace)
  7871. "Return the value of the field in column N of current row.
  7872. N defaults to current field.
  7873. If REPLACE is a string, replace field with this value. The return value
  7874. is always the old value."
  7875. (and n (org-table-goto-column n))
  7876. (skip-chars-backward "^|\n")
  7877. (backward-char 1)
  7878. (if (looking-at "|[^|\r\n]*")
  7879. (let* ((pos (match-beginning 0))
  7880. (val (buffer-substring (1+ pos) (match-end 0))))
  7881. (if replace
  7882. (replace-match (concat "|" replace) t t))
  7883. (goto-char (min (point-at-eol) (+ 2 pos)))
  7884. val)
  7885. (forward-char 1) ""))
  7886. (defun org-table-field-info (arg)
  7887. "Show info about the current field, and highlight any reference at point."
  7888. (interactive "P")
  7889. (org-table-get-specials)
  7890. (save-excursion
  7891. (let* ((pos (point))
  7892. (col (org-table-current-column))
  7893. (cname (car (rassoc (int-to-string col) org-table-column-names)))
  7894. (name (car (rassoc (list (org-current-line) col)
  7895. org-table-named-field-locations)))
  7896. (eql (org-table-get-stored-formulas))
  7897. (dline (org-table-current-dline))
  7898. (ref (format "@%d$%d" dline col))
  7899. (ref1 (org-table-convert-refs-to-an ref))
  7900. (fequation (or (assoc name eql) (assoc ref eql)))
  7901. (cequation (assoc (int-to-string col) eql))
  7902. (eqn (or fequation cequation)))
  7903. (goto-char pos)
  7904. (condition-case nil
  7905. (org-table-show-reference 'local)
  7906. (error nil))
  7907. (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
  7908. dline col
  7909. (if cname (concat " or $" cname) "")
  7910. dline col ref1
  7911. (if name (concat " or $" name) "")
  7912. ;; FIXME: formula info not correct if special table line
  7913. (if eqn
  7914. (concat ", formula: "
  7915. (org-table-formula-to-user
  7916. (concat
  7917. (if (string-match "^[$@]"(car eqn)) "" "$")
  7918. (car eqn) "=" (cdr eqn))))
  7919. "")))))
  7920. (defun org-table-current-column ()
  7921. "Find out which column we are in.
  7922. When called interactively, column is also displayed in echo area."
  7923. (interactive)
  7924. (if (interactive-p) (org-table-check-inside-data-field))
  7925. (save-excursion
  7926. (let ((cnt 0) (pos (point)))
  7927. (beginning-of-line 1)
  7928. (while (search-forward "|" pos t)
  7929. (setq cnt (1+ cnt)))
  7930. (if (interactive-p) (message "This is table column %d" cnt))
  7931. cnt)))
  7932. (defun org-table-current-dline ()
  7933. "Find out what table data line we are in.
  7934. Only datalins count for this."
  7935. (interactive)
  7936. (if (interactive-p) (org-table-check-inside-data-field))
  7937. (save-excursion
  7938. (let ((cnt 0) (pos (point)))
  7939. (goto-char (org-table-begin))
  7940. (while (<= (point) pos)
  7941. (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
  7942. (beginning-of-line 2))
  7943. (if (interactive-p) (message "This is table line %d" cnt))
  7944. cnt)))
  7945. (defun org-table-goto-column (n &optional on-delim force)
  7946. "Move the cursor to the Nth column in the current table line.
  7947. With optional argument ON-DELIM, stop with point before the left delimiter
  7948. of the field.
  7949. If there are less than N fields, just go to after the last delimiter.
  7950. However, when FORCE is non-nil, create new columns if necessary."
  7951. (interactive "p")
  7952. (let ((pos (point-at-eol)))
  7953. (beginning-of-line 1)
  7954. (when (> n 0)
  7955. (while (and (> (setq n (1- n)) -1)
  7956. (or (search-forward "|" pos t)
  7957. (and force
  7958. (progn (end-of-line 1)
  7959. (skip-chars-backward "^|")
  7960. (insert " | "))))))
  7961. ; (backward-char 2) t)))))
  7962. (when (and force (not (looking-at ".*|")))
  7963. (save-excursion (end-of-line 1) (insert " | ")))
  7964. (if on-delim
  7965. (backward-char 1)
  7966. (if (looking-at " ") (forward-char 1))))))
  7967. (defun org-at-table-p (&optional table-type)
  7968. "Return t if the cursor is inside an org-type table.
  7969. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  7970. (if org-enable-table-editor
  7971. (save-excursion
  7972. (beginning-of-line 1)
  7973. (looking-at (if table-type org-table-any-line-regexp
  7974. org-table-line-regexp)))
  7975. nil))
  7976. (defun org-at-table.el-p ()
  7977. "Return t if and only if we are at a table.el table."
  7978. (and (org-at-table-p 'any)
  7979. (save-excursion
  7980. (goto-char (org-table-begin 'any))
  7981. (looking-at org-table1-hline-regexp))))
  7982. (defun org-table-recognize-table.el ()
  7983. "If there is a table.el table nearby, recognize it and move into it."
  7984. (if org-table-tab-recognizes-table.el
  7985. (if (org-at-table.el-p)
  7986. (progn
  7987. (beginning-of-line 1)
  7988. (if (looking-at org-table-dataline-regexp)
  7989. nil
  7990. (if (looking-at org-table1-hline-regexp)
  7991. (progn
  7992. (beginning-of-line 2)
  7993. (if (looking-at org-table-any-border-regexp)
  7994. (beginning-of-line -1)))))
  7995. (if (re-search-forward "|" (org-table-end t) t)
  7996. (progn
  7997. (require 'table)
  7998. (if (table--at-cell-p (point))
  7999. t
  8000. (message "recognizing table.el table...")
  8001. (table-recognize-table)
  8002. (message "recognizing table.el table...done")))
  8003. (error "This should not happen..."))
  8004. t)
  8005. nil)
  8006. nil))
  8007. (defun org-at-table-hline-p ()
  8008. "Return t if the cursor is inside a hline in a table."
  8009. (if org-enable-table-editor
  8010. (save-excursion
  8011. (beginning-of-line 1)
  8012. (looking-at org-table-hline-regexp))
  8013. nil))
  8014. (defun org-table-insert-column ()
  8015. "Insert a new column into the table."
  8016. (interactive)
  8017. (if (not (org-at-table-p))
  8018. (error "Not at a table"))
  8019. (org-table-find-dataline)
  8020. (let* ((col (max 1 (org-table-current-column)))
  8021. (beg (org-table-begin))
  8022. (end (org-table-end))
  8023. ;; Current cursor position
  8024. (linepos (org-current-line))
  8025. (colpos col))
  8026. (goto-char beg)
  8027. (while (< (point) end)
  8028. (if (org-at-table-hline-p)
  8029. nil
  8030. (org-table-goto-column col t)
  8031. (insert "| "))
  8032. (beginning-of-line 2))
  8033. (move-marker end nil)
  8034. (goto-line linepos)
  8035. (org-table-goto-column colpos)
  8036. (org-table-align)
  8037. (org-table-fix-formulas "$" nil (1- col) 1)))
  8038. (defun org-table-find-dataline ()
  8039. "Find a dataline in the current table, which is needed for column commands."
  8040. (if (and (org-at-table-p)
  8041. (not (org-at-table-hline-p)))
  8042. t
  8043. (let ((col (current-column))
  8044. (end (org-table-end)))
  8045. (move-to-column col)
  8046. (while (and (< (point) end)
  8047. (or (not (= (current-column) col))
  8048. (org-at-table-hline-p)))
  8049. (beginning-of-line 2)
  8050. (move-to-column col))
  8051. (if (and (org-at-table-p)
  8052. (not (org-at-table-hline-p)))
  8053. t
  8054. (error
  8055. "Please position cursor in a data line for column operations")))))
  8056. (defun org-table-delete-column ()
  8057. "Delete a column from the table."
  8058. (interactive)
  8059. (if (not (org-at-table-p))
  8060. (error "Not at a table"))
  8061. (org-table-find-dataline)
  8062. (org-table-check-inside-data-field)
  8063. (let* ((col (org-table-current-column))
  8064. (beg (org-table-begin))
  8065. (end (org-table-end))
  8066. ;; Current cursor position
  8067. (linepos (org-current-line))
  8068. (colpos col))
  8069. (goto-char beg)
  8070. (while (< (point) end)
  8071. (if (org-at-table-hline-p)
  8072. nil
  8073. (org-table-goto-column col t)
  8074. (and (looking-at "|[^|\n]+|")
  8075. (replace-match "|")))
  8076. (beginning-of-line 2))
  8077. (move-marker end nil)
  8078. (goto-line linepos)
  8079. (org-table-goto-column colpos)
  8080. (org-table-align)
  8081. (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
  8082. col -1 col)))
  8083. (defun org-table-move-column-right ()
  8084. "Move column to the right."
  8085. (interactive)
  8086. (org-table-move-column nil))
  8087. (defun org-table-move-column-left ()
  8088. "Move column to the left."
  8089. (interactive)
  8090. (org-table-move-column 'left))
  8091. (defun org-table-move-column (&optional left)
  8092. "Move the current column to the right. With arg LEFT, move to the left."
  8093. (interactive "P")
  8094. (if (not (org-at-table-p))
  8095. (error "Not at a table"))
  8096. (org-table-find-dataline)
  8097. (org-table-check-inside-data-field)
  8098. (let* ((col (org-table-current-column))
  8099. (col1 (if left (1- col) col))
  8100. (beg (org-table-begin))
  8101. (end (org-table-end))
  8102. ;; Current cursor position
  8103. (linepos (org-current-line))
  8104. (colpos (if left (1- col) (1+ col))))
  8105. (if (and left (= col 1))
  8106. (error "Cannot move column further left"))
  8107. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8108. (error "Cannot move column further right"))
  8109. (goto-char beg)
  8110. (while (< (point) end)
  8111. (if (org-at-table-hline-p)
  8112. nil
  8113. (org-table-goto-column col1 t)
  8114. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8115. (replace-match "|\\2|\\1|")))
  8116. (beginning-of-line 2))
  8117. (move-marker end nil)
  8118. (goto-line linepos)
  8119. (org-table-goto-column colpos)
  8120. (org-table-align)
  8121. (org-table-fix-formulas
  8122. "$" (list (cons (number-to-string col) (number-to-string colpos))
  8123. (cons (number-to-string colpos) (number-to-string col))))))
  8124. (defun org-table-move-row-down ()
  8125. "Move table row down."
  8126. (interactive)
  8127. (org-table-move-row nil))
  8128. (defun org-table-move-row-up ()
  8129. "Move table row up."
  8130. (interactive)
  8131. (org-table-move-row 'up))
  8132. (defun org-table-move-row (&optional up)
  8133. "Move the current table line down. With arg UP, move it up."
  8134. (interactive "P")
  8135. (let* ((col (current-column))
  8136. (pos (point))
  8137. (hline1p (save-excursion (beginning-of-line 1)
  8138. (looking-at org-table-hline-regexp)))
  8139. (dline1 (org-table-current-dline))
  8140. (dline2 (+ dline1 (if up -1 1)))
  8141. (tonew (if up 0 2))
  8142. txt hline2p)
  8143. (beginning-of-line tonew)
  8144. (unless (org-at-table-p)
  8145. (goto-char pos)
  8146. (error "Cannot move row further"))
  8147. (setq hline2p (looking-at org-table-hline-regexp))
  8148. (goto-char pos)
  8149. (beginning-of-line 1)
  8150. (setq pos (point))
  8151. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8152. (delete-region (point) (1+ (point-at-eol)))
  8153. (beginning-of-line tonew)
  8154. (insert txt)
  8155. (beginning-of-line 0)
  8156. (move-to-column col)
  8157. (unless (or hline1p hline2p)
  8158. (org-table-fix-formulas
  8159. "@" (list (cons (number-to-string dline1) (number-to-string dline2))
  8160. (cons (number-to-string dline2) (number-to-string dline1)))))))
  8161. (defun org-table-insert-row (&optional arg)
  8162. "Insert a new row above the current line into the table.
  8163. With prefix ARG, insert below the current line."
  8164. (interactive "P")
  8165. (if (not (org-at-table-p))
  8166. (error "Not at a table"))
  8167. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  8168. (new (org-table-clean-line line)))
  8169. ;; Fix the first field if necessary
  8170. (if (string-match "^[ \t]*| *[#$] *|" line)
  8171. (setq new (replace-match (match-string 0 line) t t new)))
  8172. (beginning-of-line (if arg 2 1))
  8173. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  8174. (beginning-of-line 0)
  8175. (re-search-forward "| ?" (point-at-eol) t)
  8176. (and (or org-table-may-need-update org-table-overlay-coordinates)
  8177. (org-table-align))
  8178. (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
  8179. (defun org-table-insert-hline (&optional above)
  8180. "Insert a horizontal-line below the current line into the table.
  8181. With prefix ABOVE, insert above the current line."
  8182. (interactive "P")
  8183. (if (not (org-at-table-p))
  8184. (error "Not at a table"))
  8185. (let ((line (org-table-clean-line
  8186. (buffer-substring (point-at-bol) (point-at-eol))))
  8187. (col (current-column)))
  8188. (while (string-match "|\\( +\\)|" line)
  8189. (setq line (replace-match
  8190. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  8191. ?-) "|") t t line)))
  8192. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  8193. (beginning-of-line (if above 1 2))
  8194. (insert line "\n")
  8195. (beginning-of-line (if above 1 -1))
  8196. (move-to-column col)
  8197. (and org-table-overlay-coordinates (org-table-align))))
  8198. (defun org-table-hline-and-move (&optional same-column)
  8199. "Insert a hline and move to the row below that line."
  8200. (interactive "P")
  8201. (let ((col (org-table-current-column)))
  8202. (org-table-maybe-eval-formula)
  8203. (org-table-maybe-recalculate-line)
  8204. (org-table-insert-hline)
  8205. (end-of-line 2)
  8206. (if (looking-at "\n[ \t]*|-")
  8207. (progn (insert "\n|") (org-table-align))
  8208. (org-table-next-field))
  8209. (if same-column (org-table-goto-column col))))
  8210. (defun org-table-clean-line (s)
  8211. "Convert a table line S into a string with only \"|\" and space.
  8212. In particular, this does handle wide and invisible characters."
  8213. (if (string-match "^[ \t]*|-" s)
  8214. ;; It's a hline, just map the characters
  8215. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  8216. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  8217. (setq s (replace-match
  8218. (concat "|" (make-string (org-string-width (match-string 1 s))
  8219. ?\ ) "|")
  8220. t t s)))
  8221. s))
  8222. (defun org-table-kill-row ()
  8223. "Delete the current row or horizontal line from the table."
  8224. (interactive)
  8225. (if (not (org-at-table-p))
  8226. (error "Not at a table"))
  8227. (let ((col (current-column))
  8228. (dline (org-table-current-dline)))
  8229. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8230. (if (not (org-at-table-p)) (beginning-of-line 0))
  8231. (move-to-column col)
  8232. (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
  8233. dline -1 dline)))
  8234. (defun org-table-sort-lines (with-case &optional sorting-type)
  8235. "Sort table lines according to the column at point.
  8236. The position of point indicates the column to be used for
  8237. sorting, and the range of lines is the range between the nearest
  8238. horizontal separator lines, or the entire table of no such lines
  8239. exist. If point is before the first column, you will be prompted
  8240. for the sorting column. If there is an active region, the mark
  8241. specifies the first line and the sorting column, while point
  8242. should be in the last line to be included into the sorting.
  8243. The command then prompts for the sorting type which can be
  8244. alphabetically, numerically, or by time (as given in a time stamp
  8245. in the field). Sorting in reverse order is also possible.
  8246. With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
  8247. If SORTING-TYPE is specified when this function is called from a Lisp
  8248. program, no prompting will take place. SORTING-TYPE must be a character,
  8249. any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
  8250. should be done in reverse order."
  8251. (interactive "P")
  8252. (let* ((thisline (org-current-line))
  8253. (thiscol (org-table-current-column))
  8254. beg end bcol ecol tend tbeg column lns pos)
  8255. (when (equal thiscol 0)
  8256. (if (interactive-p)
  8257. (setq thiscol
  8258. (string-to-number
  8259. (read-string "Use column N for sorting: ")))
  8260. (setq thiscol 1))
  8261. (org-table-goto-column thiscol))
  8262. (org-table-check-inside-data-field)
  8263. (if (org-region-active-p)
  8264. (progn
  8265. (setq beg (region-beginning) end (region-end))
  8266. (goto-char beg)
  8267. (setq column (org-table-current-column)
  8268. beg (point-at-bol))
  8269. (goto-char end)
  8270. (setq end (point-at-bol 2)))
  8271. (setq column (org-table-current-column)
  8272. pos (point)
  8273. tbeg (org-table-begin)
  8274. tend (org-table-end))
  8275. (if (re-search-backward org-table-hline-regexp tbeg t)
  8276. (setq beg (point-at-bol 2))
  8277. (goto-char tbeg)
  8278. (setq beg (point-at-bol 1)))
  8279. (goto-char pos)
  8280. (if (re-search-forward org-table-hline-regexp tend t)
  8281. (setq end (point-at-bol 1))
  8282. (goto-char tend)
  8283. (setq end (point-at-bol))))
  8284. (setq beg (move-marker (make-marker) beg)
  8285. end (move-marker (make-marker) end))
  8286. (untabify beg end)
  8287. (goto-char beg)
  8288. (org-table-goto-column column)
  8289. (skip-chars-backward "^|")
  8290. (setq bcol (current-column))
  8291. (org-table-goto-column (1+ column))
  8292. (skip-chars-backward "^|")
  8293. (setq ecol (1- (current-column)))
  8294. (org-table-goto-column column)
  8295. (setq lns (mapcar (lambda(x) (cons
  8296. (org-sort-remove-invisible
  8297. (nth (1- column)
  8298. (org-split-string x "[ \t]*|[ \t]*")))
  8299. x))
  8300. (org-split-string (buffer-substring beg end) "\n")))
  8301. (setq lns (org-do-sort lns "Table" with-case sorting-type))
  8302. (delete-region beg end)
  8303. (move-marker beg nil)
  8304. (move-marker end nil)
  8305. (insert (mapconcat 'cdr lns "\n") "\n")
  8306. (goto-line thisline)
  8307. (org-table-goto-column thiscol)
  8308. (message "%d lines sorted, based on column %d" (length lns) column)))
  8309. ;; FIXME: maybe we will not need this? Table sorting is broken....
  8310. (defun org-sort-remove-invisible (s)
  8311. (remove-text-properties 0 (length s) org-rm-props s)
  8312. (while (string-match org-bracket-link-regexp s)
  8313. (setq s (replace-match (if (match-end 2)
  8314. (match-string 3 s)
  8315. (match-string 1 s)) t t s)))
  8316. s)
  8317. (defun org-table-cut-region (beg end)
  8318. "Copy region in table to the clipboard and blank all relevant fields."
  8319. (interactive "r")
  8320. (org-table-copy-region beg end 'cut))
  8321. (defun org-table-copy-region (beg end &optional cut)
  8322. "Copy rectangular region in table to clipboard.
  8323. A special clipboard is used which can only be accessed
  8324. with `org-table-paste-rectangle'."
  8325. (interactive "rP")
  8326. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8327. region cols
  8328. (rpl (if cut " " nil)))
  8329. (goto-char beg)
  8330. (org-table-check-inside-data-field)
  8331. (setq l01 (org-current-line)
  8332. c01 (org-table-current-column))
  8333. (goto-char end)
  8334. (org-table-check-inside-data-field)
  8335. (setq l02 (org-current-line)
  8336. c02 (org-table-current-column))
  8337. (setq l1 (min l01 l02) l2 (max l01 l02)
  8338. c1 (min c01 c02) c2 (max c01 c02))
  8339. (catch 'exit
  8340. (while t
  8341. (catch 'nextline
  8342. (if (> l1 l2) (throw 'exit t))
  8343. (goto-line l1)
  8344. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8345. (setq cols nil ic1 c1 ic2 c2)
  8346. (while (< ic1 (1+ ic2))
  8347. (push (org-table-get-field ic1 rpl) cols)
  8348. (setq ic1 (1+ ic1)))
  8349. (push (nreverse cols) region)
  8350. (setq l1 (1+ l1)))))
  8351. (setq org-table-clip (nreverse region))
  8352. (if cut (org-table-align))
  8353. org-table-clip))
  8354. (defun org-table-paste-rectangle ()
  8355. "Paste a rectangular region into a table.
  8356. The upper right corner ends up in the current field. All involved fields
  8357. will be overwritten. If the rectangle does not fit into the present table,
  8358. the table is enlarged as needed. The process ignores horizontal separator
  8359. lines."
  8360. (interactive)
  8361. (unless (and org-table-clip (listp org-table-clip))
  8362. (error "First cut/copy a region to paste!"))
  8363. (org-table-check-inside-data-field)
  8364. (let* ((clip org-table-clip)
  8365. (line (org-current-line))
  8366. (col (org-table-current-column))
  8367. (org-enable-table-editor t)
  8368. (org-table-automatic-realign nil)
  8369. c cols field)
  8370. (while (setq cols (pop clip))
  8371. (while (org-at-table-hline-p) (beginning-of-line 2))
  8372. (if (not (org-at-table-p))
  8373. (progn (end-of-line 0) (org-table-next-field)))
  8374. (setq c col)
  8375. (while (setq field (pop cols))
  8376. (org-table-goto-column c nil 'force)
  8377. (org-table-get-field nil field)
  8378. (setq c (1+ c)))
  8379. (beginning-of-line 2))
  8380. (goto-line line)
  8381. (org-table-goto-column col)
  8382. (org-table-align)))
  8383. (defun org-table-convert ()
  8384. "Convert from `org-mode' table to table.el and back.
  8385. Obviously, this only works within limits. When an Org-mode table is
  8386. converted to table.el, all horizontal separator lines get lost, because
  8387. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8388. A table.el table can be converted to an Org-mode table only if it does not
  8389. do row or column spanning. Multiline cells will become multiple cells.
  8390. Beware, Org-mode does not test if the table can be successfully converted - it
  8391. blindly applies a recipe that works for simple tables."
  8392. (interactive)
  8393. (require 'table)
  8394. (if (org-at-table.el-p)
  8395. ;; convert to Org-mode table
  8396. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8397. (end (move-marker (make-marker) (org-table-end t))))
  8398. (table-unrecognize-region beg end)
  8399. (goto-char beg)
  8400. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8401. (replace-match ""))
  8402. (goto-char beg))
  8403. (if (org-at-table-p)
  8404. ;; convert to table.el table
  8405. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8406. (end (move-marker (make-marker) (org-table-end))))
  8407. ;; first, get rid of all horizontal lines
  8408. (goto-char beg)
  8409. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8410. (replace-match ""))
  8411. ;; insert a hline before first
  8412. (goto-char beg)
  8413. (org-table-insert-hline 'above)
  8414. (beginning-of-line -1)
  8415. ;; insert a hline after each line
  8416. (while (progn (beginning-of-line 3) (< (point) end))
  8417. (org-table-insert-hline))
  8418. (goto-char beg)
  8419. (setq end (move-marker end (org-table-end)))
  8420. ;; replace "+" at beginning and ending of hlines
  8421. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8422. (replace-match "\\1+-"))
  8423. (goto-char beg)
  8424. (while (re-search-forward "-|[ \t]*$" end t)
  8425. (replace-match "-+"))
  8426. (goto-char beg)))))
  8427. (defun org-table-wrap-region (arg)
  8428. "Wrap several fields in a column like a paragraph.
  8429. This is useful if you'd like to spread the contents of a field over several
  8430. lines, in order to keep the table compact.
  8431. If there is an active region, and both point and mark are in the same column,
  8432. the text in the column is wrapped to minimum width for the given number of
  8433. lines. Generally, this makes the table more compact. A prefix ARG may be
  8434. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8435. formats the selected text to two lines. If the region was longer than two
  8436. lines, the remaining lines remain empty. A negative prefix argument reduces
  8437. the current number of lines by that amount. The wrapped text is pasted back
  8438. into the table. If you formatted it to more lines than it was before, fields
  8439. further down in the table get overwritten - so you might need to make space in
  8440. the table first.
  8441. If there is no region, the current field is split at the cursor position and
  8442. the text fragment to the right of the cursor is prepended to the field one
  8443. line down.
  8444. If there is no region, but you specify a prefix ARG, the current field gets
  8445. blank, and the content is appended to the field above."
  8446. (interactive "P")
  8447. (org-table-check-inside-data-field)
  8448. (if (org-region-active-p)
  8449. ;; There is a region: fill as a paragraph
  8450. (let* ((beg (region-beginning))
  8451. (cline (save-excursion (goto-char beg) (org-current-line)))
  8452. (ccol (save-excursion (goto-char beg) (org-table-current-column)))
  8453. nlines)
  8454. (org-table-cut-region (region-beginning) (region-end))
  8455. (if (> (length (car org-table-clip)) 1)
  8456. (error "Region must be limited to single column"))
  8457. (setq nlines (if arg
  8458. (if (< arg 1)
  8459. (+ (length org-table-clip) arg)
  8460. arg)
  8461. (length org-table-clip)))
  8462. (setq org-table-clip
  8463. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8464. nil nlines)))
  8465. (goto-line cline)
  8466. (org-table-goto-column ccol)
  8467. (org-table-paste-rectangle))
  8468. ;; No region, split the current field at point
  8469. (if arg
  8470. ;; combine with field above
  8471. (let ((s (org-table-blank-field))
  8472. (col (org-table-current-column)))
  8473. (beginning-of-line 0)
  8474. (while (org-at-table-hline-p) (beginning-of-line 0))
  8475. (org-table-goto-column col)
  8476. (skip-chars-forward "^|")
  8477. (skip-chars-backward " ")
  8478. (insert " " (org-trim s))
  8479. (org-table-align))
  8480. ;; split field
  8481. (when (looking-at "\\([^|]+\\)+|")
  8482. (let ((s (match-string 1)))
  8483. (replace-match " |")
  8484. (goto-char (match-beginning 0))
  8485. (org-table-next-row)
  8486. (insert (org-trim s) " ")
  8487. (org-table-align))))))
  8488. (defvar org-field-marker nil)
  8489. (defun org-table-edit-field (arg)
  8490. "Edit table field in a different window.
  8491. This is mainly useful for fields that contain hidden parts.
  8492. When called with a \\[universal-argument] prefix, just make the full field visible so that
  8493. it can be edited in place."
  8494. (interactive "P")
  8495. (if arg
  8496. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  8497. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  8498. (remove-text-properties b e '(org-cwidth t invisible t
  8499. display t intangible t))
  8500. (if (and (boundp 'font-lock-mode) font-lock-mode)
  8501. (font-lock-fontify-block)))
  8502. (let ((pos (move-marker (make-marker) (point)))
  8503. (field (org-table-get-field))
  8504. (cw (current-window-configuration))
  8505. p)
  8506. (org-switch-to-buffer-other-window "*Org tmp*")
  8507. (erase-buffer)
  8508. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  8509. (let ((org-inhibit-startup t)) (org-mode))
  8510. (goto-char (setq p (point-max)))
  8511. (insert (org-trim field))
  8512. (remove-text-properties p (point-max)
  8513. '(invisible t org-cwidth t display t
  8514. intangible t))
  8515. (goto-char p)
  8516. (org-set-local 'org-finish-function 'org-table-finish-edit-field)
  8517. (org-set-local 'org-window-configuration cw)
  8518. (org-set-local 'org-field-marker pos)
  8519. (message "Edit and finish with C-c C-c"))))
  8520. (defun org-table-finish-edit-field ()
  8521. "Finish editing a table data field.
  8522. Remove all newline characters, insert the result into the table, realign
  8523. the table and kill the editing buffer."
  8524. (let ((pos org-field-marker)
  8525. (cw org-window-configuration)
  8526. (cb (current-buffer))
  8527. text)
  8528. (goto-char (point-min))
  8529. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  8530. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  8531. (replace-match " "))
  8532. (setq text (org-trim (buffer-string)))
  8533. (set-window-configuration cw)
  8534. (kill-buffer cb)
  8535. (select-window (get-buffer-window (marker-buffer pos)))
  8536. (goto-char pos)
  8537. (move-marker pos nil)
  8538. (org-table-check-inside-data-field)
  8539. (org-table-get-field nil text)
  8540. (org-table-align)
  8541. (message "New field value inserted")))
  8542. (defun org-trim (s)
  8543. "Remove whitespace at beginning and end of string."
  8544. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  8545. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  8546. s)
  8547. (defun org-wrap (string &optional width lines)
  8548. "Wrap string to either a number of lines, or a width in characters.
  8549. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8550. that costs. If there is a word longer than WIDTH, the text is actually
  8551. wrapped to the length of that word.
  8552. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8553. many lines, whatever width that takes.
  8554. The return value is a list of lines, without newlines at the end."
  8555. (let* ((words (org-split-string string "[ \t\n]+"))
  8556. (maxword (apply 'max (mapcar 'org-string-width words)))
  8557. w ll)
  8558. (cond (width
  8559. (org-do-wrap words (max maxword width)))
  8560. (lines
  8561. (setq w maxword)
  8562. (setq ll (org-do-wrap words maxword))
  8563. (if (<= (length ll) lines)
  8564. ll
  8565. (setq ll words)
  8566. (while (> (length ll) lines)
  8567. (setq w (1+ w))
  8568. (setq ll (org-do-wrap words w)))
  8569. ll))
  8570. (t (error "Cannot wrap this")))))
  8571. (defun org-do-wrap (words width)
  8572. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8573. (let (lines line)
  8574. (while words
  8575. (setq line (pop words))
  8576. (while (and words (< (+ (length line) (length (car words))) width))
  8577. (setq line (concat line " " (pop words))))
  8578. (setq lines (push line lines)))
  8579. (nreverse lines)))
  8580. (defun org-split-string (string &optional separators)
  8581. "Splits STRING into substrings at SEPARATORS.
  8582. No empty strings are returned if there are matches at the beginning
  8583. and end of string."
  8584. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8585. (start 0)
  8586. notfirst
  8587. (list nil))
  8588. (while (and (string-match rexp string
  8589. (if (and notfirst
  8590. (= start (match-beginning 0))
  8591. (< start (length string)))
  8592. (1+ start) start))
  8593. (< (match-beginning 0) (length string)))
  8594. (setq notfirst t)
  8595. (or (eq (match-beginning 0) 0)
  8596. (and (eq (match-beginning 0) (match-end 0))
  8597. (eq (match-beginning 0) start))
  8598. (setq list
  8599. (cons (substring string start (match-beginning 0))
  8600. list)))
  8601. (setq start (match-end 0)))
  8602. (or (eq start (length string))
  8603. (setq list
  8604. (cons (substring string start)
  8605. list)))
  8606. (nreverse list)))
  8607. (defun org-table-map-tables (function)
  8608. "Apply FUNCTION to the start of all tables in the buffer."
  8609. (save-excursion
  8610. (save-restriction
  8611. (widen)
  8612. (goto-char (point-min))
  8613. (while (re-search-forward org-table-any-line-regexp nil t)
  8614. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8615. (beginning-of-line 1)
  8616. (if (looking-at org-table-line-regexp)
  8617. (save-excursion (funcall function)))
  8618. (re-search-forward org-table-any-border-regexp nil 1))))
  8619. (message "Mapping tables: done"))
  8620. (defvar org-timecnt) ; dynamically scoped parameter
  8621. (defun org-table-sum (&optional beg end nlast)
  8622. "Sum numbers in region of current table column.
  8623. The result will be displayed in the echo area, and will be available
  8624. as kill to be inserted with \\[yank].
  8625. If there is an active region, it is interpreted as a rectangle and all
  8626. numbers in that rectangle will be summed. If there is no active
  8627. region and point is located in a table column, sum all numbers in that
  8628. column.
  8629. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8630. numbers are assumed to be times as well (in decimal hours) and the
  8631. numbers are added as such.
  8632. If NLAST is a number, only the NLAST fields will actually be summed."
  8633. (interactive)
  8634. (save-excursion
  8635. (let (col (org-timecnt 0) diff h m s org-table-clip)
  8636. (cond
  8637. ((and beg end)) ; beg and end given explicitly
  8638. ((org-region-active-p)
  8639. (setq beg (region-beginning) end (region-end)))
  8640. (t
  8641. (setq col (org-table-current-column))
  8642. (goto-char (org-table-begin))
  8643. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8644. (error "No table data"))
  8645. (org-table-goto-column col)
  8646. (setq beg (point))
  8647. (goto-char (org-table-end))
  8648. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8649. (error "No table data"))
  8650. (org-table-goto-column col)
  8651. (setq end (point))))
  8652. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8653. (items1 (cond ((not nlast) items)
  8654. ((>= nlast (length items)) items)
  8655. (t (setq items (reverse items))
  8656. (setcdr (nthcdr (1- nlast) items) nil)
  8657. (nreverse items))))
  8658. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8659. items1)))
  8660. (res (apply '+ numbers))
  8661. (sres (if (= org-timecnt 0)
  8662. (format "%g" res)
  8663. (setq diff (* 3600 res)
  8664. h (floor (/ diff 3600)) diff (mod diff 3600)
  8665. m (floor (/ diff 60)) diff (mod diff 60)
  8666. s diff)
  8667. (format "%d:%02d:%02d" h m s))))
  8668. (kill-new sres)
  8669. (if (interactive-p)
  8670. (message "%s"
  8671. (substitute-command-keys
  8672. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8673. (length numbers) sres))))
  8674. sres))))
  8675. (defun org-table-get-number-for-summing (s)
  8676. (let (n)
  8677. (if (string-match "^ *|? *" s)
  8678. (setq s (replace-match "" nil nil s)))
  8679. (if (string-match " *|? *$" s)
  8680. (setq s (replace-match "" nil nil s)))
  8681. (setq n (string-to-number s))
  8682. (cond
  8683. ((and (string-match "0" s)
  8684. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8685. ((string-match "\\`[ \t]+\\'" s) nil)
  8686. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8687. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8688. (m (string-to-number (or (match-string 2 s) "0")))
  8689. (s (string-to-number (or (match-string 4 s) "0"))))
  8690. (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
  8691. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8692. ((equal n 0) nil)
  8693. (t n))))
  8694. (defun org-table-current-field-formula (&optional key noerror)
  8695. "Return the formula active for the current field.
  8696. Assumes that specials are in place.
  8697. If KEY is given, return the key to this formula.
  8698. Otherwise return the formula preceeded with \"=\" or \":=\"."
  8699. (let* ((name (car (rassoc (list (org-current-line)
  8700. (org-table-current-column))
  8701. org-table-named-field-locations)))
  8702. (col (org-table-current-column))
  8703. (scol (int-to-string col))
  8704. (ref (format "@%d$%d" (org-table-current-dline) col))
  8705. (stored-list (org-table-get-stored-formulas noerror))
  8706. (ass (or (assoc name stored-list)
  8707. (assoc ref stored-list)
  8708. (assoc scol stored-list))))
  8709. (if key
  8710. (car ass)
  8711. (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
  8712. (cdr ass))))))
  8713. (defun org-table-get-formula (&optional equation named)
  8714. "Read a formula from the minibuffer, offer stored formula as default.
  8715. When NAMED is non-nil, look for a named equation."
  8716. (let* ((stored-list (org-table-get-stored-formulas))
  8717. (name (car (rassoc (list (org-current-line)
  8718. (org-table-current-column))
  8719. org-table-named-field-locations)))
  8720. (ref (format "@%d$%d" (org-table-current-dline)
  8721. (org-table-current-column)))
  8722. (refass (assoc ref stored-list))
  8723. (scol (if named
  8724. (if name name ref)
  8725. (int-to-string (org-table-current-column))))
  8726. (dummy (and (or name refass) (not named)
  8727. (not (y-or-n-p "Replace field formula with column formula? " ))
  8728. (error "Abort")))
  8729. (name (or name ref))
  8730. (org-table-may-need-update nil)
  8731. (stored (cdr (assoc scol stored-list)))
  8732. (eq (cond
  8733. ((and stored equation (string-match "^ *=? *$" equation))
  8734. stored)
  8735. ((stringp equation)
  8736. equation)
  8737. (t (org-table-formula-from-user
  8738. (read-string
  8739. (org-table-formula-to-user
  8740. (format "%s formula %s%s="
  8741. (if named "Field" "Column")
  8742. (if (member (string-to-char scol) '(?$ ?@)) "" "$")
  8743. scol))
  8744. (if stored (org-table-formula-to-user stored) "")
  8745. 'org-table-formula-history
  8746. )))))
  8747. mustsave)
  8748. (when (not (string-match "\\S-" eq))
  8749. ;; remove formula
  8750. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8751. (org-table-store-formulas stored-list)
  8752. (error "Formula removed"))
  8753. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8754. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8755. (if (and name (not named))
  8756. ;; We set the column equation, delete the named one.
  8757. (setq stored-list (delq (assoc name stored-list) stored-list)
  8758. mustsave t))
  8759. (if stored
  8760. (setcdr (assoc scol stored-list) eq)
  8761. (setq stored-list (cons (cons scol eq) stored-list)))
  8762. (if (or mustsave (not (equal stored eq)))
  8763. (org-table-store-formulas stored-list))
  8764. eq))
  8765. (defun org-table-store-formulas (alist)
  8766. "Store the list of formulas below the current table."
  8767. (setq alist (sort alist 'org-table-formula-less-p))
  8768. (save-excursion
  8769. (goto-char (org-table-end))
  8770. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
  8771. (progn
  8772. ;; don't overwrite TBLFM, we might use text properties to store stuff
  8773. (goto-char (match-beginning 2))
  8774. (delete-region (match-beginning 2) (match-end 0)))
  8775. (insert "#+TBLFM:"))
  8776. (insert " "
  8777. (mapconcat (lambda (x)
  8778. (concat
  8779. (if (equal (string-to-char (car x)) ?@) "" "$")
  8780. (car x) "=" (cdr x)))
  8781. alist "::")
  8782. "\n")))
  8783. (defsubst org-table-formula-make-cmp-string (a)
  8784. (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
  8785. (concat
  8786. (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
  8787. (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
  8788. (if (match-end 5) (concat "@@" (match-string 5 a))))))
  8789. (defun org-table-formula-less-p (a b)
  8790. "Compare two formulas for sorting."
  8791. (let ((as (org-table-formula-make-cmp-string (car a)))
  8792. (bs (org-table-formula-make-cmp-string (car b))))
  8793. (and as bs (string< as bs))))
  8794. (defun org-table-get-stored-formulas (&optional noerror)
  8795. "Return an alist with the stored formulas directly after current table."
  8796. (interactive)
  8797. (let (scol eq eq-alist strings string seen)
  8798. (save-excursion
  8799. (goto-char (org-table-end))
  8800. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8801. (setq strings (org-split-string (match-string 2) " *:: *"))
  8802. (while (setq string (pop strings))
  8803. (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
  8804. (setq scol (if (match-end 2)
  8805. (match-string 2 string)
  8806. (match-string 1 string))
  8807. eq (match-string 3 string)
  8808. eq-alist (cons (cons scol eq) eq-alist))
  8809. (if (member scol seen)
  8810. (if noerror
  8811. (progn
  8812. (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8813. (ding)
  8814. (sit-for 2))
  8815. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
  8816. (push scol seen))))))
  8817. (nreverse eq-alist)))
  8818. (defun org-table-fix-formulas (key replace &optional limit delta remove)
  8819. "Modify the equations after the table structure has been edited.
  8820. KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
  8821. For all numbers larger than LIMIT, shift them by DELTA."
  8822. (save-excursion
  8823. (goto-char (org-table-end))
  8824. (when (looking-at "#\\+TBLFM:")
  8825. (let ((re (concat key "\\([0-9]+\\)"))
  8826. (re2
  8827. (when remove
  8828. (if (equal key "$")
  8829. (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
  8830. (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
  8831. s n a)
  8832. (when remove
  8833. (while (re-search-forward re2 (point-at-eol) t)
  8834. (replace-match "")))
  8835. (while (re-search-forward re (point-at-eol) t)
  8836. (setq s (match-string 1) n (string-to-number s))
  8837. (cond
  8838. ((setq a (assoc s replace))
  8839. (replace-match (concat key (cdr a)) t t))
  8840. ((and limit (> n limit))
  8841. (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
  8842. (defun org-table-get-specials ()
  8843. "Get the column names and local parameters for this table."
  8844. (save-excursion
  8845. (let ((beg (org-table-begin)) (end (org-table-end))
  8846. names name fields fields1 field cnt
  8847. c v l line col types dlines hlines)
  8848. (setq org-table-column-names nil
  8849. org-table-local-parameters nil
  8850. org-table-named-field-locations nil
  8851. org-table-current-begin-line nil
  8852. org-table-current-begin-pos nil
  8853. org-table-current-line-types nil)
  8854. (goto-char beg)
  8855. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  8856. (setq names (org-split-string (match-string 1) " *| *")
  8857. cnt 1)
  8858. (while (setq name (pop names))
  8859. (setq cnt (1+ cnt))
  8860. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  8861. (push (cons name (int-to-string cnt)) org-table-column-names))))
  8862. (setq org-table-column-names (nreverse org-table-column-names))
  8863. (setq org-table-column-name-regexp
  8864. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  8865. (goto-char beg)
  8866. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  8867. (setq fields (org-split-string (match-string 1) " *| *"))
  8868. (while (setq field (pop fields))
  8869. (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  8870. (push (cons (match-string 1 field) (match-string 2 field))
  8871. org-table-local-parameters))))
  8872. (goto-char beg)
  8873. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  8874. (setq c (match-string 1)
  8875. fields (org-split-string (match-string 2) " *| *"))
  8876. (save-excursion
  8877. (beginning-of-line (if (equal c "_") 2 0))
  8878. (setq line (org-current-line) col 1)
  8879. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  8880. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  8881. (while (and fields1 (setq field (pop fields)))
  8882. (setq v (pop fields1) col (1+ col))
  8883. (when (and (stringp field) (stringp v)
  8884. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  8885. (push (cons field v) org-table-local-parameters)
  8886. (push (list field line col) org-table-named-field-locations))))
  8887. ;; Analyse the line types
  8888. (goto-char beg)
  8889. (setq org-table-current-begin-line (org-current-line)
  8890. org-table-current-begin-pos (point)
  8891. l org-table-current-begin-line)
  8892. (while (looking-at "[ \t]*|\\(-\\)?")
  8893. (push (if (match-end 1) 'hline 'dline) types)
  8894. (if (match-end 1) (push l hlines) (push l dlines))
  8895. (beginning-of-line 2)
  8896. (setq l (1+ l)))
  8897. (setq org-table-current-line-types (apply 'vector (nreverse types))
  8898. org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
  8899. org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
  8900. (defun org-table-maybe-eval-formula ()
  8901. "Check if the current field starts with \"=\" or \":=\".
  8902. If yes, store the formula and apply it."
  8903. ;; We already know we are in a table. Get field will only return a formula
  8904. ;; when appropriate. It might return a separator line, but no problem.
  8905. (when org-table-formula-evaluate-inline
  8906. (let* ((field (org-trim (or (org-table-get-field) "")))
  8907. named eq)
  8908. (when (string-match "^:?=\\(.*\\)" field)
  8909. (setq named (equal (string-to-char field) ?:)
  8910. eq (match-string 1 field))
  8911. (if (or (fboundp 'calc-eval)
  8912. (equal (substring eq 0 (min 2 (length eq))) "'("))
  8913. (org-table-eval-formula (if named '(4) nil)
  8914. (org-table-formula-from-user eq))
  8915. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
  8916. (defvar org-recalc-commands nil
  8917. "List of commands triggering the recalculation of a line.
  8918. Will be filled automatically during use.")
  8919. (defvar org-recalc-marks
  8920. '((" " . "Unmarked: no special line, no automatic recalculation")
  8921. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  8922. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  8923. ("!" . "Column name definition line. Reference in formula as $name.")
  8924. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  8925. ("_" . "Names for values in row below this one.")
  8926. ("^" . "Names for values in row above this one.")))
  8927. (defun org-table-rotate-recalc-marks (&optional newchar)
  8928. "Rotate the recalculation mark in the first column.
  8929. If in any row, the first field is not consistent with a mark,
  8930. insert a new column for the markers.
  8931. When there is an active region, change all the lines in the region,
  8932. after prompting for the marking character.
  8933. After each change, a message will be displayed indicating the meaning
  8934. of the new mark."
  8935. (interactive)
  8936. (unless (org-at-table-p) (error "Not at a table"))
  8937. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  8938. (beg (org-table-begin))
  8939. (end (org-table-end))
  8940. (l (org-current-line))
  8941. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  8942. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  8943. (have-col
  8944. (save-excursion
  8945. (goto-char beg)
  8946. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  8947. (col (org-table-current-column))
  8948. (forcenew (car (assoc newchar org-recalc-marks)))
  8949. epos new)
  8950. (when l1
  8951. (message "Change region to what mark? Type # * ! $ or SPC: ")
  8952. (setq newchar (char-to-string (read-char-exclusive))
  8953. forcenew (car (assoc newchar org-recalc-marks))))
  8954. (if (and newchar (not forcenew))
  8955. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  8956. newchar))
  8957. (if l1 (goto-line l1))
  8958. (save-excursion
  8959. (beginning-of-line 1)
  8960. (unless (looking-at org-table-dataline-regexp)
  8961. (error "Not at a table data line")))
  8962. (unless have-col
  8963. (org-table-goto-column 1)
  8964. (org-table-insert-column)
  8965. (org-table-goto-column (1+ col)))
  8966. (setq epos (point-at-eol))
  8967. (save-excursion
  8968. (beginning-of-line 1)
  8969. (org-table-get-field
  8970. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  8971. (concat " "
  8972. (setq new (or forcenew
  8973. (cadr (member (match-string 1) marks))))
  8974. " ")
  8975. " # ")))
  8976. (if (and l1 l2)
  8977. (progn
  8978. (goto-line l1)
  8979. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  8980. (and (looking-at org-table-dataline-regexp)
  8981. (org-table-get-field 1 (concat " " new " "))))
  8982. (goto-line l1)))
  8983. (if (not (= epos (point-at-eol))) (org-table-align))
  8984. (goto-line l)
  8985. (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
  8986. (defun org-table-maybe-recalculate-line ()
  8987. "Recompute the current line if marked for it, and if we haven't just done it."
  8988. (interactive)
  8989. (and org-table-allow-automatic-line-recalculation
  8990. (not (and (memq last-command org-recalc-commands)
  8991. (equal org-last-recalc-line (org-current-line))))
  8992. (save-excursion (beginning-of-line 1)
  8993. (looking-at org-table-auto-recalculate-regexp))
  8994. (org-table-recalculate) t))
  8995. (defvar org-table-formula-debug nil
  8996. "Non-nil means, debug table formulas.
  8997. When nil, simply write \"#ERROR\" in corrupted fields.")
  8998. (make-variable-buffer-local 'org-table-formula-debug)
  8999. (defvar modes)
  9000. (defsubst org-set-calc-mode (var &optional value)
  9001. (if (stringp var)
  9002. (setq var (assoc var '(("D" calc-angle-mode deg)
  9003. ("R" calc-angle-mode rad)
  9004. ("F" calc-prefer-frac t)
  9005. ("S" calc-symbolic-mode t)))
  9006. value (nth 2 var) var (nth 1 var)))
  9007. (if (memq var modes)
  9008. (setcar (cdr (memq var modes)) value)
  9009. (cons var (cons value modes)))
  9010. modes)
  9011. (defun org-table-eval-formula (&optional arg equation
  9012. suppress-align suppress-const
  9013. suppress-store suppress-analysis)
  9014. "Replace the table field value at the cursor by the result of a calculation.
  9015. This function makes use of Dave Gillespie's Calc package, in my view the
  9016. most exciting program ever written for GNU Emacs. So you need to have Calc
  9017. installed in order to use this function.
  9018. In a table, this command replaces the value in the current field with the
  9019. result of a formula. It also installs the formula as the \"current\" column
  9020. formula, by storing it in a special line below the table. When called
  9021. with a `C-u' prefix, the current field must ba a named field, and the
  9022. formula is installed as valid in only this specific field.
  9023. When called with two `C-u' prefixes, insert the active equation
  9024. for the field back into the current field, so that it can be
  9025. edited there. This is useful in order to use \\[org-table-show-reference]
  9026. to check the referenced fields.
  9027. When called, the command first prompts for a formula, which is read in
  9028. the minibuffer. Previously entered formulas are available through the
  9029. history list, and the last used formula is offered as a default.
  9030. These stored formulas are adapted correctly when moving, inserting, or
  9031. deleting columns with the corresponding commands.
  9032. The formula can be any algebraic expression understood by the Calc package.
  9033. For details, see the Org-mode manual.
  9034. This function can also be called from Lisp programs and offers
  9035. additional arguments: EQUATION can be the formula to apply. If this
  9036. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9037. used to speed-up recursive calls by by-passing unnecessary aligns.
  9038. SUPPRESS-CONST suppresses the interpretation of constants in the
  9039. formula, assuming that this has been done already outside the function.
  9040. SUPPRESS-STORE means the formula should not be stored, either because
  9041. it is already stored, or because it is a modified equation that should
  9042. not overwrite the stored one."
  9043. (interactive "P")
  9044. (org-table-check-inside-data-field)
  9045. (or suppress-analysis (org-table-get-specials))
  9046. (if (equal arg '(16))
  9047. (let ((eq (org-table-current-field-formula)))
  9048. (or eq (error "No equation active for current field"))
  9049. (org-table-get-field nil eq)
  9050. (org-table-align)
  9051. (setq org-table-may-need-update t))
  9052. (let* (fields
  9053. (ndown (if (integerp arg) arg 1))
  9054. (org-table-automatic-realign nil)
  9055. (case-fold-search nil)
  9056. (down (> ndown 1))
  9057. (formula (if (and equation suppress-store)
  9058. equation
  9059. (org-table-get-formula equation (equal arg '(4)))))
  9060. (n0 (org-table-current-column))
  9061. (modes (copy-sequence org-calc-default-modes))
  9062. (numbers nil) ; was a variable, now fixed default
  9063. (keep-empty nil)
  9064. n form form0 bw fmt x ev orig c lispp literal)
  9065. ;; Parse the format string. Since we have a lot of modes, this is
  9066. ;; a lot of work. However, I think calc still uses most of the time.
  9067. (if (string-match ";" formula)
  9068. (let ((tmp (org-split-string formula ";")))
  9069. (setq formula (car tmp)
  9070. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9071. (nth 1 tmp)))
  9072. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  9073. (setq c (string-to-char (match-string 1 fmt))
  9074. n (string-to-number (match-string 2 fmt)))
  9075. (if (= c ?p)
  9076. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9077. (setq modes (org-set-calc-mode
  9078. 'calc-float-format
  9079. (list (cdr (assoc c '((?n . float) (?f . fix)
  9080. (?s . sci) (?e . eng))))
  9081. n))))
  9082. (setq fmt (replace-match "" t t fmt)))
  9083. (if (string-match "[NT]" fmt)
  9084. (setq numbers (equal (match-string 0 fmt) "N")
  9085. fmt (replace-match "" t t fmt)))
  9086. (if (string-match "L" fmt)
  9087. (setq literal t
  9088. fmt (replace-match "" t t fmt)))
  9089. (if (string-match "E" fmt)
  9090. (setq keep-empty t
  9091. fmt (replace-match "" t t fmt)))
  9092. (while (string-match "[DRFS]" fmt)
  9093. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9094. (setq fmt (replace-match "" t t fmt)))
  9095. (unless (string-match "\\S-" fmt)
  9096. (setq fmt nil))))
  9097. (if (and (not suppress-const) org-table-formula-use-constants)
  9098. (setq formula (org-table-formula-substitute-names formula)))
  9099. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9100. (while (> ndown 0)
  9101. (setq fields (org-split-string
  9102. (org-no-properties
  9103. (buffer-substring (point-at-bol) (point-at-eol)))
  9104. " *| *"))
  9105. (if (eq numbers t)
  9106. (setq fields (mapcar
  9107. (lambda (x) (number-to-string (string-to-number x)))
  9108. fields)))
  9109. (setq ndown (1- ndown))
  9110. (setq form (copy-sequence formula)
  9111. lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
  9112. (if (and lispp literal) (setq lispp 'literal))
  9113. ;; Check for old vertical references
  9114. (setq form (org-rewrite-old-row-references form))
  9115. ;; Insert complex ranges
  9116. (while (string-match org-table-range-regexp form)
  9117. (setq form
  9118. (replace-match
  9119. (save-match-data
  9120. (org-table-make-reference
  9121. (org-table-get-range (match-string 0 form) nil n0)
  9122. keep-empty numbers lispp))
  9123. t t form)))
  9124. ;; Insert simple ranges
  9125. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
  9126. (setq form
  9127. (replace-match
  9128. (save-match-data
  9129. (org-table-make-reference
  9130. (org-sublist
  9131. fields (string-to-number (match-string 1 form))
  9132. (string-to-number (match-string 2 form)))
  9133. keep-empty numbers lispp))
  9134. t t form)))
  9135. (setq form0 form)
  9136. ;; Insert the references to fields in same row
  9137. (while (string-match "\\$\\([0-9]+\\)" form)
  9138. (setq n (string-to-number (match-string 1 form))
  9139. x (nth (1- (if (= n 0) n0 n)) fields))
  9140. (unless x (error "Invalid field specifier \"%s\""
  9141. (match-string 0 form)))
  9142. (setq form (replace-match
  9143. (save-match-data
  9144. (org-table-make-reference x nil numbers lispp))
  9145. t t form)))
  9146. (if lispp
  9147. (setq ev (condition-case nil
  9148. (eval (eval (read form)))
  9149. (error "#ERROR"))
  9150. ev (if (numberp ev) (number-to-string ev) ev))
  9151. (or (fboundp 'calc-eval)
  9152. (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
  9153. (setq ev (calc-eval (cons form modes)
  9154. (if numbers 'num))))
  9155. (when org-table-formula-debug
  9156. (with-output-to-temp-buffer "*Substitution History*"
  9157. (princ (format "Substitution history of formula
  9158. Orig: %s
  9159. $xyz-> %s
  9160. @r$c-> %s
  9161. $1-> %s\n" orig formula form0 form))
  9162. (if (listp ev)
  9163. (princ (format " %s^\nError: %s"
  9164. (make-string (car ev) ?\-) (nth 1 ev)))
  9165. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9166. ev (or fmt "NONE")
  9167. (if fmt (format fmt (string-to-number ev)) ev)))))
  9168. (setq bw (get-buffer-window "*Substitution History*"))
  9169. (shrink-window-if-larger-than-buffer bw)
  9170. (unless (and (interactive-p) (not ndown))
  9171. (unless (let (inhibit-redisplay)
  9172. (y-or-n-p "Debugging Formula. Continue to next? "))
  9173. (org-table-align)
  9174. (error "Abort"))
  9175. (delete-window bw)
  9176. (message "")))
  9177. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9178. (org-table-justify-field-maybe
  9179. (if fmt (format fmt (string-to-number ev)) ev))
  9180. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9181. (call-interactively 'org-return)
  9182. (setq ndown 0)))
  9183. (and down (org-table-maybe-recalculate-line))
  9184. (or suppress-align (and org-table-may-need-update
  9185. (org-table-align))))))
  9186. (defun org-table-put-field-property (prop value)
  9187. (save-excursion
  9188. (put-text-property (progn (skip-chars-backward "^|") (point))
  9189. (progn (skip-chars-forward "^|") (point))
  9190. prop value)))
  9191. (defun org-table-get-range (desc &optional tbeg col highlight)
  9192. "Get a calc vector from a column, accorting to descriptor DESC.
  9193. Optional arguments TBEG and COL can give the beginning of the table and
  9194. the current column, to avoid unnecessary parsing.
  9195. HIGHLIGHT means, just highlight the range."
  9196. (if (not (equal (string-to-char desc) ?@))
  9197. (setq desc (concat "@" desc)))
  9198. (save-excursion
  9199. (or tbeg (setq tbeg (org-table-begin)))
  9200. (or col (setq col (org-table-current-column)))
  9201. (let ((thisline (org-current-line))
  9202. beg end c1 c2 r1 r2 rangep tmp)
  9203. (unless (string-match org-table-range-regexp desc)
  9204. (error "Invalid table range specifier `%s'" desc))
  9205. (setq rangep (match-end 3)
  9206. r1 (and (match-end 1) (match-string 1 desc))
  9207. r2 (and (match-end 4) (match-string 4 desc))
  9208. c1 (and (match-end 2) (substring (match-string 2 desc) 1))
  9209. c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
  9210. (and c1 (setq c1 (+ (string-to-number c1)
  9211. (if (memq (string-to-char c1) '(?- ?+)) col 0))))
  9212. (and c2 (setq c2 (+ (string-to-number c2)
  9213. (if (memq (string-to-char c2) '(?- ?+)) col 0))))
  9214. (if (equal r1 "") (setq r1 nil))
  9215. (if (equal r2 "") (setq r2 nil))
  9216. (if r1 (setq r1 (org-table-get-descriptor-line r1)))
  9217. (if r2 (setq r2 (org-table-get-descriptor-line r2)))
  9218. ; (setq r2 (or r2 r1) c2 (or c2 c1))
  9219. (if (not r1) (setq r1 thisline))
  9220. (if (not r2) (setq r2 thisline))
  9221. (if (not c1) (setq c1 col))
  9222. (if (not c2) (setq c2 col))
  9223. (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
  9224. ;; just one field
  9225. (progn
  9226. (goto-line r1)
  9227. (while (not (looking-at org-table-dataline-regexp))
  9228. (beginning-of-line 2))
  9229. (prog1 (org-trim (org-table-get-field c1))
  9230. (if highlight (org-table-highlight-rectangle (point) (point)))))
  9231. ;; A range, return a vector
  9232. ;; First sort the numbers to get a regular ractangle
  9233. (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
  9234. (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
  9235. (goto-line r1)
  9236. (while (not (looking-at org-table-dataline-regexp))
  9237. (beginning-of-line 2))
  9238. (org-table-goto-column c1)
  9239. (setq beg (point))
  9240. (goto-line r2)
  9241. (while (not (looking-at org-table-dataline-regexp))
  9242. (beginning-of-line 0))
  9243. (org-table-goto-column c2)
  9244. (setq end (point))
  9245. (if highlight
  9246. (org-table-highlight-rectangle
  9247. beg (progn (skip-chars-forward "^|\n") (point))))
  9248. ;; return string representation of calc vector
  9249. (mapcar 'org-trim
  9250. (apply 'append (org-table-copy-region beg end)))))))
  9251. (defun org-table-get-descriptor-line (desc &optional cline bline table)
  9252. "Analyze descriptor DESC and retrieve the corresponding line number.
  9253. The cursor is currently in line CLINE, the table begins in line BLINE,
  9254. and TABLE is a vector with line types."
  9255. (if (string-match "^[0-9]+$" desc)
  9256. (aref org-table-dlines (string-to-number desc))
  9257. (setq cline (or cline (org-current-line))
  9258. bline (or bline org-table-current-begin-line)
  9259. table (or table org-table-current-line-types))
  9260. (if (or
  9261. (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
  9262. ;; 1 2 3 4 5 6
  9263. (and (not (match-end 3)) (not (match-end 6)))
  9264. (and (match-end 3) (match-end 6) (not (match-end 5))))
  9265. (error "invalid row descriptor `%s'" desc))
  9266. (let* ((hdir (and (match-end 2) (match-string 2 desc)))
  9267. (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
  9268. (odir (and (match-end 5) (match-string 5 desc)))
  9269. (on (if (match-end 6) (string-to-number (match-string 6 desc))))
  9270. (i (- cline bline))
  9271. (rel (and (match-end 6)
  9272. (or (and (match-end 1) (not (match-end 3)))
  9273. (match-end 5)))))
  9274. (if (and hn (not hdir))
  9275. (progn
  9276. (setq i 0 hdir "+")
  9277. (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
  9278. (if (and (not hn) on (not odir))
  9279. (error "should never happen");;(aref org-table-dlines on)
  9280. (if (and hn (> hn 0))
  9281. (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
  9282. (if on
  9283. (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
  9284. (+ bline i)))))
  9285. (defun org-find-row-type (table i type backwards relative n)
  9286. (let ((l (length table)))
  9287. (while (> n 0)
  9288. (while (and (setq i (+ i (if backwards -1 1)))
  9289. (>= i 0) (< i l)
  9290. (not (eq (aref table i) type))
  9291. (if (and relative (eq (aref table i) 'hline))
  9292. (progn (setq i (- i (if backwards -1 1)) n 1) nil)
  9293. t)))
  9294. (setq n (1- n)))
  9295. (if (or (< i 0) (>= i l))
  9296. (error "Row descriptior leads outside table")
  9297. i)))
  9298. (defun org-rewrite-old-row-references (s)
  9299. (if (string-match "&[-+0-9I]" s)
  9300. (error "Formula contains old &row reference, please rewrite using @-syntax")
  9301. s))
  9302. (defun org-table-make-reference (elements keep-empty numbers lispp)
  9303. "Convert list ELEMENTS to something appropriate to insert into formula.
  9304. KEEP-EMPTY indicated to keep empty fields, default is to skip them.
  9305. NUMBERS indicates that everything should be converted to numbers.
  9306. LISPP means to return something appropriate for a Lisp list."
  9307. (if (stringp elements) ; just a single val
  9308. (if lispp
  9309. (if (eq lispp 'literal)
  9310. elements
  9311. (prin1-to-string (if numbers (string-to-number elements) elements)))
  9312. (if (equal elements "") (setq elements "0"))
  9313. (if numbers (number-to-string (string-to-number elements)) elements))
  9314. (unless keep-empty
  9315. (setq elements
  9316. (delq nil
  9317. (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
  9318. elements))))
  9319. (setq elements (or elements '("0")))
  9320. (if lispp
  9321. (mapconcat
  9322. (lambda (x)
  9323. (if (eq lispp 'literal)
  9324. x
  9325. (prin1-to-string (if numbers (string-to-number x) x))))
  9326. elements " ")
  9327. (concat "[" (mapconcat
  9328. (lambda (x)
  9329. (if numbers (number-to-string (string-to-number x)) x))
  9330. elements
  9331. ",") "]"))))
  9332. (defun org-table-recalculate (&optional all noalign)
  9333. "Recalculate the current table line by applying all stored formulas.
  9334. With prefix arg ALL, do this for all lines in the table."
  9335. (interactive "P")
  9336. (or (memq this-command org-recalc-commands)
  9337. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9338. (unless (org-at-table-p) (error "Not at a table"))
  9339. (if (equal all '(16))
  9340. (org-table-iterate)
  9341. (org-table-get-specials)
  9342. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9343. (lambda (a b) (string< (car a) (car b)))))
  9344. (inhibit-redisplay (not debug-on-error))
  9345. (line-re org-table-dataline-regexp)
  9346. (thisline (org-current-line))
  9347. (thiscol (org-table-current-column))
  9348. beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
  9349. ;; Insert constants in all formulas
  9350. (setq eqlist
  9351. (mapcar (lambda (x)
  9352. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9353. x)
  9354. eqlist))
  9355. ;; Split the equation list
  9356. (while (setq eq (pop eqlist))
  9357. (if (<= (string-to-char (car eq)) ?9)
  9358. (push eq eqlnum)
  9359. (push eq eqlname)))
  9360. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9361. (if all
  9362. (progn
  9363. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9364. (goto-char (setq beg (org-table-begin)))
  9365. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9366. ;; This is a table with marked lines, compute selected lines
  9367. (setq line-re org-table-recalculate-regexp)
  9368. ;; Move forward to the first non-header line
  9369. (if (and (re-search-forward org-table-dataline-regexp end t)
  9370. (re-search-forward org-table-hline-regexp end t)
  9371. (re-search-forward org-table-dataline-regexp end t))
  9372. (setq beg (match-beginning 0))
  9373. nil))) ;; just leave beg where it is
  9374. (setq beg (point-at-bol)
  9375. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9376. (goto-char beg)
  9377. (and all (message "Re-applying formulas to full table..."))
  9378. ;; First find the named fields, and mark them untouchanble
  9379. (remove-text-properties beg end '(org-untouchable t))
  9380. (while (setq eq (pop eqlname))
  9381. (setq name (car eq)
  9382. a (assoc name org-table-named-field-locations))
  9383. (and (not a)
  9384. (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
  9385. (setq a (list name
  9386. (aref org-table-dlines
  9387. (string-to-number (match-string 1 name)))
  9388. (string-to-number (match-string 2 name)))))
  9389. (when (and a (or all (equal (nth 1 a) thisline)))
  9390. (message "Re-applying formula to field: %s" name)
  9391. (goto-line (nth 1 a))
  9392. (org-table-goto-column (nth 2 a))
  9393. (push (append a (list (cdr eq))) eqlname1)
  9394. (org-table-put-field-property :org-untouchable t)))
  9395. ;; Now evauluate the column formulas, but skip fields covered by
  9396. ;; field formulas
  9397. (goto-char beg)
  9398. (while (re-search-forward line-re end t)
  9399. (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
  9400. ;; Unprotected line, recalculate
  9401. (and all (message "Re-applying formulas to full table...(line %d)"
  9402. (setq cnt (1+ cnt))))
  9403. (setq org-last-recalc-line (org-current-line))
  9404. (setq eql eqlnum)
  9405. (while (setq entry (pop eql))
  9406. (goto-line org-last-recalc-line)
  9407. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9408. (unless (get-text-property (point) :org-untouchable)
  9409. (org-table-eval-formula nil (cdr entry)
  9410. 'noalign 'nocst 'nostore 'noanalysis)))))
  9411. ;; Now evaluate the field formulas
  9412. (while (setq eq (pop eqlname1))
  9413. (message "Re-applying formula to field: %s" (car eq))
  9414. (goto-line (nth 1 eq))
  9415. (org-table-goto-column (nth 2 eq))
  9416. (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
  9417. 'nostore 'noanalysis))
  9418. (goto-line thisline)
  9419. (org-table-goto-column thiscol)
  9420. (remove-text-properties (point-min) (point-max) '(org-untouchable t))
  9421. (or noalign (and org-table-may-need-update (org-table-align))
  9422. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9423. ;; back to initial position
  9424. (message "Re-applying formulas...done")
  9425. (goto-line thisline)
  9426. (org-table-goto-column thiscol)
  9427. (or noalign (and org-table-may-need-update (org-table-align))
  9428. (and all (message "Re-applying formulas...done"))))))
  9429. (defun org-table-iterate (&optional arg)
  9430. "Recalculate the table until it does not change anymore."
  9431. (interactive "P")
  9432. (let ((imax (if arg (prefix-numeric-value arg) 10))
  9433. (i 0)
  9434. (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
  9435. thistbl)
  9436. (catch 'exit
  9437. (while (< i imax)
  9438. (setq i (1+ i))
  9439. (org-table-recalculate 'all)
  9440. (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
  9441. (if (not (string= lasttbl thistbl))
  9442. (setq lasttbl thistbl)
  9443. (if (> i 1)
  9444. (message "Convergence after %d iterations" i)
  9445. (message "Table was already stable"))
  9446. (throw 'exit t)))
  9447. (error "No convergence after %d iterations" i))))
  9448. (defun org-table-formula-substitute-names (f)
  9449. "Replace $const with values in string F."
  9450. (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
  9451. ;; First, check for column names
  9452. (while (setq start (string-match org-table-column-name-regexp f start))
  9453. (setq start (1+ start))
  9454. (setq a (assoc (match-string 1 f) org-table-column-names))
  9455. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9456. ;; Parameters and constants
  9457. (setq start 0)
  9458. (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
  9459. (setq start (1+ start))
  9460. (if (setq a (save-match-data
  9461. (org-table-get-constant (match-string 1 f))))
  9462. (setq f (replace-match
  9463. (concat (if pp "(") a (if pp ")")) t t f))))
  9464. (if org-table-formula-debug
  9465. (put-text-property 0 (length f) :orig-formula f1 f))
  9466. f))
  9467. (defun org-table-get-constant (const)
  9468. "Find the value for a parameter or constant in a formula.
  9469. Parameters get priority."
  9470. (or (cdr (assoc const org-table-local-parameters))
  9471. (cdr (assoc const org-table-formula-constants-local))
  9472. (cdr (assoc const org-table-formula-constants))
  9473. (and (fboundp 'constants-get) (constants-get const))
  9474. (and (string= (substring const 0 (min 5 (length const))) "PROP_")
  9475. (org-entry-get nil (substring const 5) 'inherit))
  9476. "#UNDEFINED_NAME"))
  9477. (defvar org-table-fedit-map
  9478. (let ((map (make-sparse-keymap)))
  9479. (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
  9480. (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
  9481. (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
  9482. (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
  9483. (org-defkey map "\C-c?" 'org-table-show-reference)
  9484. (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
  9485. (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
  9486. (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
  9487. (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
  9488. (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
  9489. (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
  9490. (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
  9491. (org-defkey map [(meta down)] 'org-table-fedit-scroll)
  9492. (org-defkey map [(meta tab)] 'lisp-complete-symbol)
  9493. (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
  9494. (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
  9495. (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
  9496. (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
  9497. (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
  9498. map))
  9499. (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  9500. '("Edit-Formulas"
  9501. ["Finish and Install" org-table-fedit-finish t]
  9502. ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
  9503. ["Abort" org-table-fedit-abort t]
  9504. "--"
  9505. ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
  9506. ["Complete Lisp Symbol" lisp-complete-symbol t]
  9507. "--"
  9508. "Shift Reference at Point"
  9509. ["Up" org-table-fedit-ref-up t]
  9510. ["Down" org-table-fedit-ref-down t]
  9511. ["Left" org-table-fedit-ref-left t]
  9512. ["Right" org-table-fedit-ref-right t]
  9513. "-"
  9514. "Change Test Row for Column Formulas"
  9515. ["Up" org-table-fedit-line-up t]
  9516. ["Down" org-table-fedit-line-down t]
  9517. "--"
  9518. ["Scroll Table Window" org-table-fedit-scroll t]
  9519. ["Scroll Table Window down" org-table-fedit-scroll-down t]
  9520. ["Show Table Grid" org-table-fedit-toggle-coordinates
  9521. :style toggle :selected (with-current-buffer (marker-buffer org-pos)
  9522. org-table-overlay-coordinates)]
  9523. "--"
  9524. ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
  9525. :style toggle :selected org-table-buffer-is-an]))
  9526. (defvar org-pos)
  9527. (defun org-table-edit-formulas ()
  9528. "Edit the formulas of the current table in a separate buffer."
  9529. (interactive)
  9530. (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
  9531. (beginning-of-line 0))
  9532. (unless (org-at-table-p) (error "Not at a table"))
  9533. (org-table-get-specials)
  9534. (let ((key (org-table-current-field-formula 'key 'noerror))
  9535. (eql (sort (org-table-get-stored-formulas 'noerror)
  9536. 'org-table-formula-less-p))
  9537. (pos (move-marker (make-marker) (point)))
  9538. (startline 1)
  9539. (wc (current-window-configuration))
  9540. (titles '((column . "# Column Formulas\n")
  9541. (field . "# Field Formulas\n")
  9542. (named . "# Named Field Formulas\n")))
  9543. entry s type title)
  9544. (org-switch-to-buffer-other-window "*Edit Formulas*")
  9545. (erase-buffer)
  9546. ;; Keep global-font-lock-mode from turning on font-lock-mode
  9547. (let ((font-lock-global-modes '(not fundamental-mode)))
  9548. (fundamental-mode))
  9549. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  9550. (org-set-local 'org-pos pos)
  9551. (org-set-local 'org-window-configuration wc)
  9552. (use-local-map org-table-fedit-map)
  9553. (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
  9554. (easy-menu-add org-table-fedit-menu)
  9555. (setq startline (org-current-line))
  9556. (while (setq entry (pop eql))
  9557. (setq type (cond
  9558. ((equal (string-to-char (car entry)) ?@) 'field)
  9559. ((string-match "^[0-9]" (car entry)) 'column)
  9560. (t 'named)))
  9561. (when (setq title (assq type titles))
  9562. (or (bobp) (insert "\n"))
  9563. (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
  9564. (setq titles (delq title titles)))
  9565. (if (equal key (car entry)) (setq startline (org-current-line)))
  9566. (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
  9567. (car entry) " = " (cdr entry) "\n"))
  9568. (remove-text-properties 0 (length s) '(face nil) s)
  9569. (insert s))
  9570. (if (eq org-table-use-standard-references t)
  9571. (org-table-fedit-toggle-ref-type))
  9572. (goto-line startline)
  9573. (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
  9574. (defun org-table-fedit-post-command ()
  9575. (when (not (memq this-command '(lisp-complete-symbol)))
  9576. (let ((win (selected-window)))
  9577. (save-excursion
  9578. (condition-case nil
  9579. (org-table-show-reference)
  9580. (error nil))
  9581. (select-window win)))))
  9582. (defun org-table-formula-to-user (s)
  9583. "Convert a formula from internal to user representation."
  9584. (if (eq org-table-use-standard-references t)
  9585. (org-table-convert-refs-to-an s)
  9586. s))
  9587. (defun org-table-formula-from-user (s)
  9588. "Convert a formula from user to internal representation."
  9589. (if org-table-use-standard-references
  9590. (org-table-convert-refs-to-rc s)
  9591. s))
  9592. (defun org-table-convert-refs-to-rc (s)
  9593. "Convert spreadsheet references from AB7 to @7$28.
  9594. Works for single references, but also for entire formulas and even the
  9595. full TBLFM line."
  9596. (let ((start 0))
  9597. (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
  9598. (cond
  9599. ((match-end 3)
  9600. ;; format match, just advance
  9601. (setq start (match-end 0)))
  9602. ((and (> (match-beginning 0) 0)
  9603. (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
  9604. (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
  9605. ;; 3.e5 or something like this.
  9606. (setq start (match-end 0)))
  9607. (t
  9608. (setq start (match-beginning 0)
  9609. s (replace-match
  9610. (if (equal (match-string 2 s) "&")
  9611. (format "$%d" (org-letters-to-number (match-string 1 s)))
  9612. (format "@%d$%d"
  9613. (string-to-number (match-string 2 s))
  9614. (org-letters-to-number (match-string 1 s))))
  9615. t t s)))))
  9616. s))
  9617. (defun org-table-convert-refs-to-an (s)
  9618. "Convert spreadsheet references from to @7$28 to AB7.
  9619. Works for single references, but also for entire formulas and even the
  9620. full TBLFM line."
  9621. (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
  9622. (setq s (replace-match
  9623. (format "%s%d"
  9624. (org-number-to-letters
  9625. (string-to-number (match-string 2 s)))
  9626. (string-to-number (match-string 1 s)))
  9627. t t s)))
  9628. (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
  9629. (setq s (replace-match (concat "\\1"
  9630. (org-number-to-letters
  9631. (string-to-number (match-string 2 s))) "&")
  9632. t nil s)))
  9633. s)
  9634. (defun org-letters-to-number (s)
  9635. "Convert a base 26 number represented by letters into an integer.
  9636. For example: AB -> 28."
  9637. (let ((n 0))
  9638. (setq s (upcase s))
  9639. (while (> (length s) 0)
  9640. (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
  9641. s (substring s 1)))
  9642. n))
  9643. (defun org-number-to-letters (n)
  9644. "Convert an integer into a base 26 number represented by letters.
  9645. For example: 28 -> AB."
  9646. (let ((s ""))
  9647. (while (> n 0)
  9648. (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
  9649. n (/ (1- n) 26)))
  9650. s))
  9651. (defun org-table-fedit-convert-buffer (function)
  9652. "Convert all references in this buffer, using FUNTION."
  9653. (let ((line (org-current-line)))
  9654. (goto-char (point-min))
  9655. (while (not (eobp))
  9656. (insert (funcall function (buffer-substring (point) (point-at-eol))))
  9657. (delete-region (point) (point-at-eol))
  9658. (or (eobp) (forward-char 1)))
  9659. (goto-line line)))
  9660. (defun org-table-fedit-toggle-ref-type ()
  9661. "Convert all references in the buffer from B3 to @3$2 and back."
  9662. (interactive)
  9663. (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
  9664. (org-table-fedit-convert-buffer
  9665. (if org-table-buffer-is-an
  9666. 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  9667. (message "Reference type switched to %s"
  9668. (if org-table-buffer-is-an "A1 etc" "@row$column")))
  9669. (defun org-table-fedit-ref-up ()
  9670. "Shift the reference at point one row/hline up."
  9671. (interactive)
  9672. (org-table-fedit-shift-reference 'up))
  9673. (defun org-table-fedit-ref-down ()
  9674. "Shift the reference at point one row/hline down."
  9675. (interactive)
  9676. (org-table-fedit-shift-reference 'down))
  9677. (defun org-table-fedit-ref-left ()
  9678. "Shift the reference at point one field to the left."
  9679. (interactive)
  9680. (org-table-fedit-shift-reference 'left))
  9681. (defun org-table-fedit-ref-right ()
  9682. "Shift the reference at point one field to the right."
  9683. (interactive)
  9684. (org-table-fedit-shift-reference 'right))
  9685. (defun org-table-fedit-shift-reference (dir)
  9686. (cond
  9687. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
  9688. (if (memq dir '(left right))
  9689. (org-rematch-and-replace 1 (eq dir 'left))
  9690. (error "Cannot shift reference in this direction")))
  9691. ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
  9692. ;; A B3-like reference
  9693. (if (memq dir '(up down))
  9694. (org-rematch-and-replace 2 (eq dir 'up))
  9695. (org-rematch-and-replace 1 (eq dir 'left))))
  9696. ((org-at-regexp-p
  9697. "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
  9698. ;; An internal reference
  9699. (if (memq dir '(up down))
  9700. (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
  9701. (org-rematch-and-replace 5 (eq dir 'left))))))
  9702. (defun org-rematch-and-replace (n &optional decr hline)
  9703. "Re-match the group N, and replace it with the shifted refrence."
  9704. (or (match-end n) (error "Cannot shift reference in this direction"))
  9705. (goto-char (match-beginning n))
  9706. (and (looking-at (regexp-quote (match-string n)))
  9707. (replace-match (org-shift-refpart (match-string 0) decr hline)
  9708. t t)))
  9709. (defun org-shift-refpart (ref &optional decr hline)
  9710. "Shift a refrence part REF.
  9711. If DECR is set, decrease the references row/column, else increase.
  9712. If HLINE is set, this may be a hline reference, it certainly is not
  9713. a translation reference."
  9714. (save-match-data
  9715. (let* ((sign (string-match "^[-+]" ref)) n)
  9716. (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
  9717. (cond
  9718. ((and hline (string-match "^I+" ref))
  9719. (setq n (string-to-number (concat sign (number-to-string (length ref)))))
  9720. (setq n (+ n (if decr -1 1)))
  9721. (if (= n 0) (setq n (+ n (if decr -1 1))))
  9722. (if sign
  9723. (setq sign (if (< n 0) "-" "+") n (abs n))
  9724. (setq n (max 1 n)))
  9725. (concat sign (make-string n ?I)))
  9726. ((string-match "^[0-9]+" ref)
  9727. (setq n (string-to-number (concat sign ref)))
  9728. (setq n (+ n (if decr -1 1)))
  9729. (if sign
  9730. (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
  9731. (number-to-string (max 1 n))))
  9732. ((string-match "^[a-zA-Z]+" ref)
  9733. (org-number-to-letters
  9734. (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
  9735. (t (error "Cannot shift reference"))))))
  9736. (defun org-table-fedit-toggle-coordinates ()
  9737. "Toggle the display of coordinates in the refrenced table."
  9738. (interactive)
  9739. (let ((pos (marker-position org-pos)))
  9740. (with-current-buffer (marker-buffer org-pos)
  9741. (save-excursion
  9742. (goto-char pos)
  9743. (org-table-toggle-coordinate-overlays)))))
  9744. (defun org-table-fedit-finish (&optional arg)
  9745. "Parse the buffer for formula definitions and install them.
  9746. With prefix ARG, apply the new formulas to the table."
  9747. (interactive "P")
  9748. (org-table-remove-rectangle-highlight)
  9749. (if org-table-use-standard-references
  9750. (progn
  9751. (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
  9752. (setq org-table-buffer-is-an nil)))
  9753. (let ((pos org-pos) eql var form)
  9754. (goto-char (point-min))
  9755. (while (re-search-forward
  9756. "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
  9757. nil t)
  9758. (setq var (if (match-end 2) (match-string 2) (match-string 1))
  9759. form (match-string 3))
  9760. (setq form (org-trim form))
  9761. (when (not (equal form ""))
  9762. (while (string-match "[ \t]*\n[ \t]*" form)
  9763. (setq form (replace-match " " t t form)))
  9764. (when (assoc var eql)
  9765. (error "Double formulas for %s" var))
  9766. (push (cons var form) eql)))
  9767. (setq org-pos nil)
  9768. (set-window-configuration org-window-configuration)
  9769. (select-window (get-buffer-window (marker-buffer pos)))
  9770. (goto-char pos)
  9771. (unless (org-at-table-p)
  9772. (error "Lost table position - cannot install formulae"))
  9773. (org-table-store-formulas eql)
  9774. (move-marker pos nil)
  9775. (kill-buffer "*Edit Formulas*")
  9776. (if arg
  9777. (org-table-recalculate 'all)
  9778. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9779. (defun org-table-fedit-abort ()
  9780. "Abort editing formulas, without installing the changes."
  9781. (interactive)
  9782. (org-table-remove-rectangle-highlight)
  9783. (let ((pos org-pos))
  9784. (set-window-configuration org-window-configuration)
  9785. (select-window (get-buffer-window (marker-buffer pos)))
  9786. (goto-char pos)
  9787. (move-marker pos nil)
  9788. (message "Formula editing aborted without installing changes")))
  9789. (defun org-table-fedit-lisp-indent ()
  9790. "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  9791. (interactive)
  9792. (let ((pos (point)) beg end ind)
  9793. (beginning-of-line 1)
  9794. (cond
  9795. ((looking-at "[ \t]")
  9796. (goto-char pos)
  9797. (call-interactively 'lisp-indent-line))
  9798. ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
  9799. ((not (fboundp 'pp-buffer))
  9800. (error "Cannot pretty-print. Command `pp-buffer' is not available."))
  9801. ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
  9802. (goto-char (- (match-end 0) 2))
  9803. (setq beg (point))
  9804. (setq ind (make-string (current-column) ?\ ))
  9805. (condition-case nil (forward-sexp 1)
  9806. (error
  9807. (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
  9808. (setq end (point))
  9809. (save-restriction
  9810. (narrow-to-region beg end)
  9811. (if (eq last-command this-command)
  9812. (progn
  9813. (goto-char (point-min))
  9814. (setq this-command nil)
  9815. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  9816. (replace-match " ")))
  9817. (pp-buffer)
  9818. (untabify (point-min) (point-max))
  9819. (goto-char (1+ (point-min)))
  9820. (while (re-search-forward "^." nil t)
  9821. (beginning-of-line 1)
  9822. (insert ind))
  9823. (goto-char (point-max))
  9824. (backward-delete-char 1)))
  9825. (goto-char beg))
  9826. (t nil))))
  9827. (defvar org-show-positions nil)
  9828. (defun org-table-show-reference (&optional local)
  9829. "Show the location/value of the $ expression at point."
  9830. (interactive)
  9831. (org-table-remove-rectangle-highlight)
  9832. (catch 'exit
  9833. (let ((pos (if local (point) org-pos))
  9834. (face2 'highlight)
  9835. (org-inhibit-highlight-removal t)
  9836. (win (selected-window))
  9837. (org-show-positions nil)
  9838. var name e what match dest)
  9839. (if local (org-table-get-specials))
  9840. (setq what (cond
  9841. ((or (org-at-regexp-p org-table-range-regexp2)
  9842. (org-at-regexp-p org-table-translate-regexp)
  9843. (org-at-regexp-p org-table-range-regexp))
  9844. (setq match
  9845. (save-match-data
  9846. (org-table-convert-refs-to-rc (match-string 0))))
  9847. 'range)
  9848. ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
  9849. ((org-at-regexp-p "\\$[0-9]+") 'column)
  9850. ((not local) nil)
  9851. (t (error "No reference at point")))
  9852. match (and what (or match (match-string 0))))
  9853. (when (and match (not (equal (match-beginning 0) (point-at-bol))))
  9854. (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
  9855. 'secondary-selection))
  9856. (org-add-hook 'before-change-functions
  9857. 'org-table-remove-rectangle-highlight)
  9858. (if (eq what 'name) (setq var (substring match 1)))
  9859. (when (eq what 'range)
  9860. (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
  9861. (setq match (org-table-formula-substitute-names match)))
  9862. (unless local
  9863. (save-excursion
  9864. (end-of-line 1)
  9865. (re-search-backward "^\\S-" nil t)
  9866. (beginning-of-line 1)
  9867. (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
  9868. (setq dest
  9869. (save-match-data
  9870. (org-table-convert-refs-to-rc (match-string 1))))
  9871. (org-table-add-rectangle-overlay
  9872. (match-beginning 1) (match-end 1) face2))))
  9873. (if (and (markerp pos) (marker-buffer pos))
  9874. (if (get-buffer-window (marker-buffer pos))
  9875. (select-window (get-buffer-window (marker-buffer pos)))
  9876. (org-switch-to-buffer-other-window (get-buffer-window
  9877. (marker-buffer pos)))))
  9878. (goto-char pos)
  9879. (org-table-force-dataline)
  9880. (when dest
  9881. (setq name (substring dest 1))
  9882. (cond
  9883. ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
  9884. (setq e (assoc name org-table-named-field-locations))
  9885. (goto-line (nth 1 e))
  9886. (org-table-goto-column (nth 2 e)))
  9887. ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
  9888. (let ((l (string-to-number (match-string 1 dest)))
  9889. (c (string-to-number (match-string 2 dest))))
  9890. (goto-line (aref org-table-dlines l))
  9891. (org-table-goto-column c)))
  9892. (t (org-table-goto-column (string-to-number name))))
  9893. (move-marker pos (point))
  9894. (org-table-highlight-rectangle nil nil face2))
  9895. (cond
  9896. ((equal dest match))
  9897. ((not match))
  9898. ((eq what 'range)
  9899. (condition-case nil
  9900. (save-excursion
  9901. (org-table-get-range match nil nil 'highlight))
  9902. (error nil)))
  9903. ((setq e (assoc var org-table-named-field-locations))
  9904. (goto-line (nth 1 e))
  9905. (org-table-goto-column (nth 2 e))
  9906. (org-table-highlight-rectangle (point) (point))
  9907. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  9908. ((setq e (assoc var org-table-column-names))
  9909. (org-table-goto-column (string-to-number (cdr e)))
  9910. (org-table-highlight-rectangle (point) (point))
  9911. (goto-char (org-table-begin))
  9912. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  9913. (org-table-end) t)
  9914. (progn
  9915. (goto-char (match-beginning 1))
  9916. (org-table-highlight-rectangle)
  9917. (message "Named column (column %s)" (cdr e)))
  9918. (error "Column name not found")))
  9919. ((eq what 'column)
  9920. ;; column number
  9921. (org-table-goto-column (string-to-number (substring match 1)))
  9922. (org-table-highlight-rectangle (point) (point))
  9923. (message "Column %s" (substring match 1)))
  9924. ((setq e (assoc var org-table-local-parameters))
  9925. (goto-char (org-table-begin))
  9926. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  9927. (progn
  9928. (goto-char (match-beginning 1))
  9929. (org-table-highlight-rectangle)
  9930. (message "Local parameter."))
  9931. (error "Parameter not found")))
  9932. (t
  9933. (cond
  9934. ((not var) (error "No reference at point"))
  9935. ((setq e (assoc var org-table-formula-constants-local))
  9936. (message "Local Constant: $%s=%s in #+CONSTANTS line."
  9937. var (cdr e)))
  9938. ((setq e (assoc var org-table-formula-constants))
  9939. (message "Constant: $%s=%s in `org-table-formula-constants'."
  9940. var (cdr e)))
  9941. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  9942. (message "Constant: $%s=%s, from `constants.el'%s."
  9943. var e (format " (%s units)" constants-unit-system)))
  9944. (t (error "Undefined name $%s" var)))))
  9945. (goto-char pos)
  9946. (when (and org-show-positions
  9947. (not (memq this-command '(org-table-fedit-scroll
  9948. org-table-fedit-scroll-down))))
  9949. (push pos org-show-positions)
  9950. (push org-table-current-begin-pos org-show-positions)
  9951. (let ((min (apply 'min org-show-positions))
  9952. (max (apply 'max org-show-positions)))
  9953. (goto-char min) (recenter 0)
  9954. (goto-char max)
  9955. (or (pos-visible-in-window-p max) (recenter -1))))
  9956. (select-window win))))
  9957. (defun org-table-force-dataline ()
  9958. "Make sure the cursor is in a dataline in a table."
  9959. (unless (save-excursion
  9960. (beginning-of-line 1)
  9961. (looking-at org-table-dataline-regexp))
  9962. (let* ((re org-table-dataline-regexp)
  9963. (p1 (save-excursion (re-search-forward re nil 'move)))
  9964. (p2 (save-excursion (re-search-backward re nil 'move))))
  9965. (cond ((and p1 p2)
  9966. (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
  9967. p1 p2)))
  9968. ((or p1 p2) (goto-char (or p1 p2)))
  9969. (t (error "No table dataline around here"))))))
  9970. (defun org-table-fedit-line-up ()
  9971. "Move cursor one line up in the window showing the table."
  9972. (interactive)
  9973. (org-table-fedit-move 'previous-line))
  9974. (defun org-table-fedit-line-down ()
  9975. "Move cursor one line down in the window showing the table."
  9976. (interactive)
  9977. (org-table-fedit-move 'next-line))
  9978. (defun org-table-fedit-move (command)
  9979. "Move the cursor in the window shoinw the table.
  9980. Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  9981. (let ((org-table-allow-automatic-line-recalculation nil)
  9982. (pos org-pos) (win (selected-window)) p)
  9983. (select-window (get-buffer-window (marker-buffer org-pos)))
  9984. (setq p (point))
  9985. (call-interactively command)
  9986. (while (and (org-at-table-p)
  9987. (org-at-table-hline-p))
  9988. (call-interactively command))
  9989. (or (org-at-table-p) (goto-char p))
  9990. (move-marker pos (point))
  9991. (select-window win)))
  9992. (defun org-table-fedit-scroll (N)
  9993. (interactive "p")
  9994. (let ((other-window-scroll-buffer (marker-buffer org-pos)))
  9995. (scroll-other-window N)))
  9996. (defun org-table-fedit-scroll-down (N)
  9997. (interactive "p")
  9998. (org-table-fedit-scroll (- N)))
  9999. (defvar org-table-rectangle-overlays nil)
  10000. (defun org-table-add-rectangle-overlay (beg end &optional face)
  10001. "Add a new overlay."
  10002. (let ((ov (org-make-overlay beg end)))
  10003. (org-overlay-put ov 'face (or face 'secondary-selection))
  10004. (push ov org-table-rectangle-overlays)))
  10005. (defun org-table-highlight-rectangle (&optional beg end face)
  10006. "Highlight rectangular region in a table."
  10007. (setq beg (or beg (point)) end (or end (point)))
  10008. (let ((b (min beg end))
  10009. (e (max beg end))
  10010. l1 c1 l2 c2 tmp)
  10011. (and (boundp 'org-show-positions)
  10012. (setq org-show-positions (cons b (cons e org-show-positions))))
  10013. (goto-char (min beg end))
  10014. (setq l1 (org-current-line)
  10015. c1 (org-table-current-column))
  10016. (goto-char (max beg end))
  10017. (setq l2 (org-current-line)
  10018. c2 (org-table-current-column))
  10019. (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
  10020. (goto-line l1)
  10021. (beginning-of-line 1)
  10022. (loop for line from l1 to l2 do
  10023. (when (looking-at org-table-dataline-regexp)
  10024. (org-table-goto-column c1)
  10025. (skip-chars-backward "^|\n") (setq beg (point))
  10026. (org-table-goto-column c2)
  10027. (skip-chars-forward "^|\n") (setq end (point))
  10028. (org-table-add-rectangle-overlay beg end face))
  10029. (beginning-of-line 2))
  10030. (goto-char b))
  10031. (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
  10032. (defun org-table-remove-rectangle-highlight (&rest ignore)
  10033. "Remove the rectangle overlays."
  10034. (unless org-inhibit-highlight-removal
  10035. (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
  10036. (mapc 'org-delete-overlay org-table-rectangle-overlays)
  10037. (setq org-table-rectangle-overlays nil)))
  10038. (defvar org-table-coordinate-overlays nil
  10039. "Collects the cooordinate grid overlays, so that they can be removed.")
  10040. (make-variable-buffer-local 'org-table-coordinate-overlays)
  10041. (defun org-table-overlay-coordinates ()
  10042. "Add overlays to the table at point, to show row/column coordinates."
  10043. (interactive)
  10044. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10045. (setq org-table-coordinate-overlays nil)
  10046. (save-excursion
  10047. (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
  10048. (goto-char (org-table-begin))
  10049. (while (org-at-table-p)
  10050. (setq eol (point-at-eol))
  10051. (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
  10052. (push ov org-table-coordinate-overlays)
  10053. (setq hline (looking-at org-table-hline-regexp))
  10054. (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
  10055. (format "%4d" (setq id (1+ id)))))
  10056. (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
  10057. (when hline
  10058. (setq ic 0)
  10059. (while (re-search-forward "[+|]\\(-+\\)" eol t)
  10060. (setq beg (1+ (match-beginning 0))
  10061. ic (1+ ic)
  10062. s1 (concat "$" (int-to-string ic))
  10063. s2 (org-number-to-letters ic)
  10064. str (if (eq org-table-use-standard-references t) s2 s1))
  10065. (setq ov (org-make-overlay beg (+ beg (length str))))
  10066. (push ov org-table-coordinate-overlays)
  10067. (org-overlay-display ov str 'org-special-keyword 'evaporate)))
  10068. (beginning-of-line 2)))))
  10069. (defun org-table-toggle-coordinate-overlays ()
  10070. "Toggle the display of Row/Column numbers in tables."
  10071. (interactive)
  10072. (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  10073. (message "Row/Column number display turned %s"
  10074. (if org-table-overlay-coordinates "on" "off"))
  10075. (if (and (org-at-table-p) org-table-overlay-coordinates)
  10076. (org-table-align))
  10077. (unless org-table-overlay-coordinates
  10078. (mapc 'org-delete-overlay org-table-coordinate-overlays)
  10079. (setq org-table-coordinate-overlays nil)))
  10080. (defun org-table-toggle-formula-debugger ()
  10081. "Toggle the formula debugger in tables."
  10082. (interactive)
  10083. (setq org-table-formula-debug (not org-table-formula-debug))
  10084. (message "Formula debugging has been turned %s"
  10085. (if org-table-formula-debug "on" "off")))
  10086. ;;; The orgtbl minor mode
  10087. ;; Define a minor mode which can be used in other modes in order to
  10088. ;; integrate the org-mode table editor.
  10089. ;; This is really a hack, because the org-mode table editor uses several
  10090. ;; keys which normally belong to the major mode, for example the TAB and
  10091. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10092. ;; necessary to operate the table editor, but wraps the commands into a
  10093. ;; function which tests if the cursor is currently inside a table. If that
  10094. ;; is the case, the table editor command is executed. However, when any of
  10095. ;; those keys is used outside a table, the function uses `key-binding' to
  10096. ;; look up if the key has an associated command in another currently active
  10097. ;; keymap (minor modes, major mode, global), and executes that command.
  10098. ;; There might be problems if any of the keys used by the table editor is
  10099. ;; otherwise used as a prefix key.
  10100. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10101. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10102. ;; addresses this by checking explicitly for both bindings.
  10103. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10104. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10105. ;; Some modes bind other commands to simple characters, for example
  10106. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10107. ;; active, this binding is ignored inside tables and replaced with a
  10108. ;; modified self-insert.
  10109. (defvar orgtbl-mode nil
  10110. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10111. table editor in arbitrary modes.")
  10112. (make-variable-buffer-local 'orgtbl-mode)
  10113. (defvar orgtbl-mode-map (make-keymap)
  10114. "Keymap for `orgtbl-mode'.")
  10115. ;;;###autoload
  10116. (defun turn-on-orgtbl ()
  10117. "Unconditionally turn on `orgtbl-mode'."
  10118. (orgtbl-mode 1))
  10119. (defvar org-old-auto-fill-inhibit-regexp nil
  10120. "Local variable used by `orgtbl-mode'")
  10121. (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
  10122. "Matches a line belonging to an orgtbl.")
  10123. (defconst orgtbl-extra-font-lock-keywords
  10124. (list (list (concat "^" orgtbl-line-start-regexp ".*")
  10125. 0 (quote 'org-table) 'prepend))
  10126. "Extra font-lock-keywords to be added when orgtbl-mode is active.")
  10127. ;;;###autoload
  10128. (defun orgtbl-mode (&optional arg)
  10129. "The `org-mode' table editor as a minor mode for use in other modes."
  10130. (interactive)
  10131. (if (org-mode-p)
  10132. ;; Exit without error, in case some hook functions calls this
  10133. ;; by accident in org-mode.
  10134. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10135. (setq orgtbl-mode
  10136. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10137. (if orgtbl-mode
  10138. (progn
  10139. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10140. ;; Make sure we are first in minor-mode-map-alist
  10141. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10142. (and c (setq minor-mode-map-alist
  10143. (cons c (delq c minor-mode-map-alist)))))
  10144. (org-set-local (quote org-table-may-need-update) t)
  10145. (org-add-hook 'before-change-functions 'org-before-change-function
  10146. nil 'local)
  10147. (org-set-local 'org-old-auto-fill-inhibit-regexp
  10148. auto-fill-inhibit-regexp)
  10149. (org-set-local 'auto-fill-inhibit-regexp
  10150. (if auto-fill-inhibit-regexp
  10151. (concat orgtbl-line-start-regexp "\\|"
  10152. auto-fill-inhibit-regexp)
  10153. orgtbl-line-start-regexp))
  10154. (org-add-to-invisibility-spec '(org-cwidth))
  10155. (when (fboundp 'font-lock-add-keywords)
  10156. (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
  10157. (org-restart-font-lock))
  10158. (easy-menu-add orgtbl-mode-menu)
  10159. (run-hooks 'orgtbl-mode-hook))
  10160. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10161. (org-cleanup-narrow-column-properties)
  10162. (org-remove-from-invisibility-spec '(org-cwidth))
  10163. (remove-hook 'before-change-functions 'org-before-change-function t)
  10164. (when (fboundp 'font-lock-remove-keywords)
  10165. (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
  10166. (org-restart-font-lock))
  10167. (easy-menu-remove orgtbl-mode-menu)
  10168. (force-mode-line-update 'all))))
  10169. (defun org-cleanup-narrow-column-properties ()
  10170. "Remove all properties related to narrow-column invisibility."
  10171. (let ((s 1))
  10172. (while (setq s (text-property-any s (point-max)
  10173. 'display org-narrow-column-arrow))
  10174. (remove-text-properties s (1+ s) '(display t)))
  10175. (setq s 1)
  10176. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10177. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10178. (setq s 1)
  10179. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10180. (remove-text-properties s (1+ s) '(invisible t)))))
  10181. ;; Install it as a minor mode.
  10182. (put 'orgtbl-mode :included t)
  10183. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10184. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10185. (defun orgtbl-make-binding (fun n &rest keys)
  10186. "Create a function for binding in the table minor mode.
  10187. FUN is the command to call inside a table. N is used to create a unique
  10188. command name. KEYS are keys that should be checked in for a command
  10189. to execute outside of tables."
  10190. (eval
  10191. (list 'defun
  10192. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10193. '(arg)
  10194. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10195. "Outside of tables, run the binding of `"
  10196. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10197. "'.")
  10198. '(interactive "p")
  10199. (list 'if
  10200. '(org-at-table-p)
  10201. (list 'call-interactively (list 'quote fun))
  10202. (list 'let '(orgtbl-mode)
  10203. (list 'call-interactively
  10204. (append '(or)
  10205. (mapcar (lambda (k)
  10206. (list 'key-binding k))
  10207. keys)
  10208. '('orgtbl-error))))))))
  10209. (defun orgtbl-error ()
  10210. "Error when there is no default binding for a table key."
  10211. (interactive)
  10212. (error "This key has no function outside tables"))
  10213. (defun orgtbl-setup ()
  10214. "Setup orgtbl keymaps."
  10215. (let ((nfunc 0)
  10216. (bindings
  10217. (list
  10218. '([(meta shift left)] org-table-delete-column)
  10219. '([(meta left)] org-table-move-column-left)
  10220. '([(meta right)] org-table-move-column-right)
  10221. '([(meta shift right)] org-table-insert-column)
  10222. '([(meta shift up)] org-table-kill-row)
  10223. '([(meta shift down)] org-table-insert-row)
  10224. '([(meta up)] org-table-move-row-up)
  10225. '([(meta down)] org-table-move-row-down)
  10226. '("\C-c\C-w" org-table-cut-region)
  10227. '("\C-c\M-w" org-table-copy-region)
  10228. '("\C-c\C-y" org-table-paste-rectangle)
  10229. '("\C-c-" org-table-insert-hline)
  10230. '("\C-c}" org-table-toggle-coordinate-overlays)
  10231. '("\C-c{" org-table-toggle-formula-debugger)
  10232. '("\C-m" org-table-next-row)
  10233. '([(shift return)] org-table-copy-down)
  10234. '("\C-c\C-q" org-table-wrap-region)
  10235. '("\C-c?" org-table-field-info)
  10236. '("\C-c " org-table-blank-field)
  10237. '("\C-c+" org-table-sum)
  10238. '("\C-c=" org-table-eval-formula)
  10239. '("\C-c'" org-table-edit-formulas)
  10240. '("\C-c`" org-table-edit-field)
  10241. '("\C-c*" org-table-recalculate)
  10242. '("\C-c|" org-table-create-or-convert-from-region)
  10243. '("\C-c^" org-table-sort-lines)
  10244. '([(control ?#)] org-table-rotate-recalc-marks)))
  10245. elt key fun cmd)
  10246. (while (setq elt (pop bindings))
  10247. (setq nfunc (1+ nfunc))
  10248. (setq key (org-key (car elt))
  10249. fun (nth 1 elt)
  10250. cmd (orgtbl-make-binding fun nfunc key))
  10251. (org-defkey orgtbl-mode-map key cmd))
  10252. ;; Special treatment needed for TAB and RET
  10253. (org-defkey orgtbl-mode-map [(return)]
  10254. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10255. (org-defkey orgtbl-mode-map "\C-m"
  10256. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10257. (org-defkey orgtbl-mode-map [(tab)]
  10258. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10259. (org-defkey orgtbl-mode-map "\C-i"
  10260. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
  10261. (org-defkey orgtbl-mode-map [(shift tab)]
  10262. (orgtbl-make-binding 'org-table-previous-field 104
  10263. [(shift tab)] [(tab)] "\C-i"))
  10264. (org-defkey orgtbl-mode-map "\M-\C-m"
  10265. (orgtbl-make-binding 'org-table-wrap-region 105
  10266. "\M-\C-m" [(meta return)]))
  10267. (org-defkey orgtbl-mode-map [(meta return)]
  10268. (orgtbl-make-binding 'org-table-wrap-region 106
  10269. [(meta return)] "\M-\C-m"))
  10270. (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
  10271. (when orgtbl-optimized
  10272. ;; If the user wants maximum table support, we need to hijack
  10273. ;; some standard editing functions
  10274. (org-remap orgtbl-mode-map
  10275. 'self-insert-command 'orgtbl-self-insert-command
  10276. 'delete-char 'org-delete-char
  10277. 'delete-backward-char 'org-delete-backward-char)
  10278. (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
  10279. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10280. '("OrgTbl"
  10281. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10282. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10283. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10284. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10285. "--"
  10286. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10287. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10288. ["Copy Field from Above"
  10289. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10290. "--"
  10291. ("Column"
  10292. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10293. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10294. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10295. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  10296. ("Row"
  10297. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10298. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10299. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10300. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10301. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10302. "--"
  10303. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10304. ("Rectangle"
  10305. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10306. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10307. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10308. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10309. "--"
  10310. ("Radio tables"
  10311. ["Insert table template" orgtbl-insert-radio-table
  10312. (assq major-mode orgtbl-radio-table-templates)]
  10313. ["Comment/uncomment table" orgtbl-toggle-comment t])
  10314. "--"
  10315. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10316. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10317. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10318. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10319. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10320. ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  10321. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10322. ["Sum Column/Rectangle" org-table-sum
  10323. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10324. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10325. ["Debug Formulas"
  10326. org-table-toggle-formula-debugger :active (org-at-table-p)
  10327. :keys "C-c {"
  10328. :style toggle :selected org-table-formula-debug]
  10329. ["Show Col/Row Numbers"
  10330. org-table-toggle-coordinate-overlays :active (org-at-table-p)
  10331. :keys "C-c }"
  10332. :style toggle :selected org-table-overlay-coordinates]
  10333. ))
  10334. t))
  10335. (defun orgtbl-ctrl-c-ctrl-c (arg)
  10336. "If the cursor is inside a table, realign the table.
  10337. It it is a table to be sent away to a receiver, do it.
  10338. With prefix arg, also recompute table."
  10339. (interactive "P")
  10340. (let ((pos (point)) action)
  10341. (save-excursion
  10342. (beginning-of-line 1)
  10343. (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
  10344. ((looking-at "[ \t]*|") pos)
  10345. ((looking-at "#\\+TBLFM:") 'recalc))))
  10346. (cond
  10347. ((integerp action)
  10348. (goto-char action)
  10349. (org-table-maybe-eval-formula)
  10350. (if arg
  10351. (call-interactively 'org-table-recalculate)
  10352. (org-table-maybe-recalculate-line))
  10353. (call-interactively 'org-table-align)
  10354. (orgtbl-send-table 'maybe))
  10355. ((eq action 'recalc)
  10356. (save-excursion
  10357. (beginning-of-line 1)
  10358. (skip-chars-backward " \r\n\t")
  10359. (if (org-at-table-p)
  10360. (org-call-with-arg 'org-table-recalculate t))))
  10361. (t (let (orgtbl-mode)
  10362. (call-interactively (key-binding "\C-c\C-c")))))))
  10363. (defun orgtbl-tab (arg)
  10364. "Justification and field motion for `orgtbl-mode'."
  10365. (interactive "P")
  10366. (if arg (org-table-edit-field t)
  10367. (org-table-justify-field-maybe)
  10368. (org-table-next-field)))
  10369. (defun orgtbl-ret ()
  10370. "Justification and field motion for `orgtbl-mode'."
  10371. (interactive)
  10372. (org-table-justify-field-maybe)
  10373. (org-table-next-row))
  10374. (defun orgtbl-self-insert-command (N)
  10375. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10376. If the cursor is in a table looking at whitespace, the whitespace is
  10377. overwritten, and the table is not marked as requiring realignment."
  10378. (interactive "p")
  10379. (if (and (org-at-table-p)
  10380. (or
  10381. (and org-table-auto-blank-field
  10382. (member last-command
  10383. '(orgtbl-hijacker-command-100
  10384. orgtbl-hijacker-command-101
  10385. orgtbl-hijacker-command-102
  10386. orgtbl-hijacker-command-103
  10387. orgtbl-hijacker-command-104
  10388. orgtbl-hijacker-command-105))
  10389. (org-table-blank-field))
  10390. t)
  10391. (eq N 1)
  10392. (looking-at "[^|\n]* +|"))
  10393. (let (org-table-may-need-update)
  10394. (goto-char (1- (match-end 0)))
  10395. (delete-backward-char 1)
  10396. (goto-char (match-beginning 0))
  10397. (self-insert-command N))
  10398. (setq org-table-may-need-update t)
  10399. (let (orgtbl-mode)
  10400. (call-interactively (key-binding (vector last-input-event))))))
  10401. (defun org-force-self-insert (N)
  10402. "Needed to enforce self-insert under remapping."
  10403. (interactive "p")
  10404. (self-insert-command N))
  10405. (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  10406. "Regula expression matching exponentials as produced by calc.")
  10407. (defvar org-table-clean-did-remove-column nil)
  10408. (defun orgtbl-export (table target)
  10409. (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
  10410. (lines (org-split-string table "[ \t]*\n[ \t]*"))
  10411. org-table-last-alignment org-table-last-column-widths
  10412. maxcol column)
  10413. (if (not (fboundp func))
  10414. (error "Cannot export orgtbl table to %s" target))
  10415. (setq lines (org-table-clean-before-export lines))
  10416. (setq table
  10417. (mapcar
  10418. (lambda (x)
  10419. (if (string-match org-table-hline-regexp x)
  10420. 'hline
  10421. (org-split-string (org-trim x) "\\s-*|\\s-*")))
  10422. lines))
  10423. (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
  10424. table)))
  10425. (loop for i from (1- maxcol) downto 0 do
  10426. (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
  10427. (setq column (delq nil column))
  10428. (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
  10429. (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))
  10430. (funcall func table nil)))
  10431. (defun orgtbl-send-table (&optional maybe)
  10432. "Send a tranformed version of this table to the receiver position.
  10433. With argument MAYBE, fail quietly if no transformation is defined for
  10434. this table."
  10435. (interactive)
  10436. (catch 'exit
  10437. (unless (org-at-table-p) (error "Not at a table"))
  10438. ;; when non-interactive, we assume align has just happened.
  10439. (when (interactive-p) (org-table-align))
  10440. (save-excursion
  10441. (goto-char (org-table-begin))
  10442. (beginning-of-line 0)
  10443. (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
  10444. (if maybe
  10445. (throw 'exit nil)
  10446. (error "Don't know how to transform this table."))))
  10447. (let* ((name (match-string 1))
  10448. beg
  10449. (transform (intern (match-string 2)))
  10450. (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
  10451. (skip (plist-get params :skip))
  10452. (skipcols (plist-get params :skipcols))
  10453. (txt (buffer-substring-no-properties
  10454. (org-table-begin) (org-table-end)))
  10455. (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
  10456. (lines (org-table-clean-before-export lines))
  10457. (i0 (if org-table-clean-did-remove-column 2 1))
  10458. (table (mapcar
  10459. (lambda (x)
  10460. (if (string-match org-table-hline-regexp x)
  10461. 'hline
  10462. (org-remove-by-index
  10463. (org-split-string (org-trim x) "\\s-*|\\s-*")
  10464. skipcols i0)))
  10465. lines))
  10466. (fun (if (= i0 2) 'cdr 'identity))
  10467. (org-table-last-alignment
  10468. (org-remove-by-index (funcall fun org-table-last-alignment)
  10469. skipcols i0))
  10470. (org-table-last-column-widths
  10471. (org-remove-by-index (funcall fun org-table-last-column-widths)
  10472. skipcols i0)))
  10473. (unless (fboundp transform)
  10474. (error "No such transformation function %s" transform))
  10475. (setq txt (funcall transform table params))
  10476. ;; Find the insertion place
  10477. (save-excursion
  10478. (goto-char (point-min))
  10479. (unless (re-search-forward
  10480. (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
  10481. (error "Don't know where to insert translated table"))
  10482. (goto-char (match-beginning 0))
  10483. (beginning-of-line 2)
  10484. (setq beg (point))
  10485. (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
  10486. (error "Cannot find end of insertion region"))
  10487. (beginning-of-line 1)
  10488. (delete-region beg (point))
  10489. (goto-char beg)
  10490. (insert txt "\n"))
  10491. (message "Table converted and installed at receiver location"))))
  10492. (defun org-remove-by-index (list indices &optional i0)
  10493. "Remove the elements in LIST with indices in INDICES.
  10494. First element has index 0, or I0 if given."
  10495. (if (not indices)
  10496. list
  10497. (if (integerp indices) (setq indices (list indices)))
  10498. (setq i0 (1- (or i0 0)))
  10499. (delq :rm (mapcar (lambda (x)
  10500. (setq i0 (1+ i0))
  10501. (if (memq i0 indices) :rm x))
  10502. list))))
  10503. (defun orgtbl-toggle-comment ()
  10504. "Comment or uncomment the orgtbl at point."
  10505. (interactive)
  10506. (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
  10507. (re2 (concat "^" orgtbl-line-start-regexp))
  10508. (commented (save-excursion (beginning-of-line 1)
  10509. (cond ((looking-at re1) t)
  10510. ((looking-at re2) nil)
  10511. (t (error "Not at an org table")))))
  10512. (re (if commented re1 re2))
  10513. beg end)
  10514. (save-excursion
  10515. (beginning-of-line 1)
  10516. (while (looking-at re) (beginning-of-line 0))
  10517. (beginning-of-line 2)
  10518. (setq beg (point))
  10519. (while (looking-at re) (beginning-of-line 2))
  10520. (setq end (point)))
  10521. (comment-region beg end (if commented '(4) nil))))
  10522. (defun orgtbl-insert-radio-table ()
  10523. "Insert a radio table template appropriate for this major mode."
  10524. (interactive)
  10525. (let* ((e (assq major-mode orgtbl-radio-table-templates))
  10526. (txt (nth 1 e))
  10527. name pos)
  10528. (unless e (error "No radio table setup defined for %s" major-mode))
  10529. (setq name (read-string "Table name: "))
  10530. (while (string-match "%n" txt)
  10531. (setq txt (replace-match name t t txt)))
  10532. (or (bolp) (insert "\n"))
  10533. (setq pos (point))
  10534. (insert txt)
  10535. (goto-char pos)))
  10536. (defun org-get-param (params header i sym &optional hsym)
  10537. "Get parameter value for symbol SYM.
  10538. If this is a header line, actually get the value for the symbol with an
  10539. additional \"h\" inserted after the colon.
  10540. If the value is a protperty list, get the element for the current column.
  10541. Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
  10542. (let ((val (plist-get params sym)))
  10543. (and hsym header (setq val (or (plist-get params hsym) val)))
  10544. (if (consp val) (plist-get val i) val)))
  10545. (defun orgtbl-to-generic (table params)
  10546. "Convert the orgtbl-mode TABLE to some other format.
  10547. This generic routine can be used for many standard cases.
  10548. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10549. separator line, or a list of fields for that line.
  10550. PARAMS is a property list of parameters that can influence the conversion.
  10551. For the generic converter, some parameters are obligatory: You need to
  10552. specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
  10553. :splice, you must have :tstart and :tend.
  10554. Valid parameters are
  10555. :tstart String to start the table. Ignored when :splice is t.
  10556. :tend String to end the table. Ignored when :splice is t.
  10557. :splice When set to t, return only table body lines, don't wrap
  10558. them into :tstart and :tend. Default is nil.
  10559. :hline String to be inserted on horizontal separation lines.
  10560. May be nil to ignore hlines.
  10561. :lstart String to start a new table line.
  10562. :lend String to end a table line
  10563. :sep Separator between two fields
  10564. :lfmt Format for entire line, with enough %s to capture all fields.
  10565. If this is present, :lstart, :lend, and :sep are ignored.
  10566. :fmt A format to be used to wrap the field, should contain
  10567. %s for the original field value. For example, to wrap
  10568. everything in dollars, you could use :fmt \"$%s$\".
  10569. This may also be a property list with column numbers and
  10570. formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10571. :hlstart :hlend :hlsep :hlfmt :hfmt
  10572. Same as above, specific for the header lines in the table.
  10573. All lines before the first hline are treated as header.
  10574. If any of these is not present, the data line value is used.
  10575. :efmt Use this format to print numbers with exponentials.
  10576. The format should have %s twice for inserting mantissa
  10577. and exponent, for example \"%s\\\\times10^{%s}\". This
  10578. may also be a property list with column numbers and
  10579. formats. :fmt will still be applied after :efmt.
  10580. In addition to this, the parameters :skip and :skipcols are always handled
  10581. directly by `orgtbl-send-table'. See manual."
  10582. (interactive)
  10583. (let* ((p params)
  10584. (splicep (plist-get p :splice))
  10585. (hline (plist-get p :hline))
  10586. rtn line i fm efm lfmt h)
  10587. ;; Do we have a header?
  10588. (if (and (not splicep) (listp (car table)) (memq 'hline table))
  10589. (setq h t))
  10590. ;; Put header
  10591. (unless splicep
  10592. (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
  10593. ;; Now loop over all lines
  10594. (while (setq line (pop table))
  10595. (if (eq line 'hline)
  10596. ;; A horizontal separator line
  10597. (progn (if hline (push hline rtn))
  10598. (setq h nil)) ; no longer in header
  10599. ;; A normal line. Convert the fields, push line onto the result list
  10600. (setq i 0)
  10601. (setq line
  10602. (mapcar
  10603. (lambda (f)
  10604. (setq i (1+ i)
  10605. fm (org-get-param p h i :fmt :hfmt)
  10606. efm (org-get-param p h i :efmt))
  10607. (if (and efm (string-match orgtbl-exp-regexp f))
  10608. (setq f (format
  10609. efm (match-string 1 f) (match-string 2 f))))
  10610. (if fm (setq f (format fm f)))
  10611. f)
  10612. line))
  10613. (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
  10614. (push (apply 'format lfmt line) rtn)
  10615. (push (concat
  10616. (org-get-param p h i :lstart :hlstart)
  10617. (mapconcat 'identity line (org-get-param p h i :sep :hsep))
  10618. (org-get-param p h i :lend :hlend))
  10619. rtn))))
  10620. (unless splicep
  10621. (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
  10622. (mapconcat 'identity (nreverse rtn) "\n")))
  10623. (defun orgtbl-to-latex (table params)
  10624. "Convert the orgtbl-mode TABLE to LaTeX.
  10625. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10626. separator line, or a list of fields for that line.
  10627. PARAMS is a property list of parameters that can influence the conversion.
  10628. Supports all parameters from `orgtbl-to-generic'. Most important for
  10629. LaTeX are:
  10630. :splice When set to t, return only table body lines, don't wrap
  10631. them into a tabular environment. Default is nil.
  10632. :fmt A format to be used to wrap the field, should contain %s for the
  10633. original field value. For example, to wrap everything in dollars,
  10634. use :fmt \"$%s$\". This may also be a property list with column
  10635. numbers and formats. for example :fmt (2 \"$%s$\" 4 \"%s%%\")
  10636. :efmt Format for transforming numbers with exponentials. The format
  10637. should have %s twice for inserting mantissa and exponent, for
  10638. example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
  10639. This may also be a property list with column numbers and formats.
  10640. The general parameters :skip and :skipcols have already been applied when
  10641. this function is called."
  10642. (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
  10643. org-table-last-alignment ""))
  10644. (params2
  10645. (list
  10646. :tstart (concat "\\begin{tabular}{" alignment "}")
  10647. :tend "\\end{tabular}"
  10648. :lstart "" :lend " \\\\" :sep " & "
  10649. :efmt "%s\\,(%s)" :hline "\\hline")))
  10650. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10651. (defun orgtbl-to-html (table params)
  10652. "Convert the orgtbl-mode TABLE to LaTeX.
  10653. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10654. separator line, or a list of fields for that line.
  10655. PARAMS is a property list of parameters that can influence the conversion.
  10656. Currently this function recognizes the following parameters:
  10657. :splice When set to t, return only table body lines, don't wrap
  10658. them into a <table> environment. Default is nil.
  10659. The general parameters :skip and :skipcols have already been applied when
  10660. this function is called. The function does *not* use `orgtbl-to-generic',
  10661. so you cannot specify parameters for it."
  10662. (let* ((splicep (plist-get params :splice))
  10663. html)
  10664. ;; Just call the formatter we already have
  10665. ;; We need to make text lines for it, so put the fields back together.
  10666. (setq html (org-format-org-table-html
  10667. (mapcar
  10668. (lambda (x)
  10669. (if (eq x 'hline)
  10670. "|----+----|"
  10671. (concat "| " (mapconcat 'identity x " | ") " |")))
  10672. table)
  10673. splicep))
  10674. (if (string-match "\n+\\'" html)
  10675. (setq html (replace-match "" t t html)))
  10676. html))
  10677. (defun orgtbl-to-texinfo (table params)
  10678. "Convert the orgtbl-mode TABLE to TeXInfo.
  10679. TABLE is a list, each entry either the symbol `hline' for a horizontal
  10680. separator line, or a list of fields for that line.
  10681. PARAMS is a property list of parameters that can influence the conversion.
  10682. Supports all parameters from `orgtbl-to-generic'. Most important for
  10683. TeXInfo are:
  10684. :splice nil/t When set to t, return only table body lines, don't wrap
  10685. them into a multitable environment. Default is nil.
  10686. :fmt fmt A format to be used to wrap the field, should contain
  10687. %s for the original field value. For example, to wrap
  10688. everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
  10689. This may also be a property list with column numbers and
  10690. formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
  10691. :cf \"f1 f2..\" The column fractions for the table. By default these
  10692. are computed automatically from the width of the columns
  10693. under org-mode.
  10694. The general parameters :skip and :skipcols have already been applied when
  10695. this function is called."
  10696. (let* ((total (float (apply '+ org-table-last-column-widths)))
  10697. (colfrac (or (plist-get params :cf)
  10698. (mapconcat
  10699. (lambda (x) (format "%.3f" (/ (float x) total)))
  10700. org-table-last-column-widths " ")))
  10701. (params2
  10702. (list
  10703. :tstart (concat "@multitable @columnfractions " colfrac)
  10704. :tend "@end multitable"
  10705. :lstart "@item " :lend "" :sep " @tab "
  10706. :hlstart "@headitem ")))
  10707. (orgtbl-to-generic table (org-combine-plists params2 params))))
  10708. ;;;; Link Stuff
  10709. ;;; Link abbreviations
  10710. (defun org-link-expand-abbrev (link)
  10711. "Apply replacements as defined in `org-link-abbrev-alist."
  10712. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  10713. (let* ((key (match-string 1 link))
  10714. (as (or (assoc key org-link-abbrev-alist-local)
  10715. (assoc key org-link-abbrev-alist)))
  10716. (tag (and (match-end 2) (match-string 3 link)))
  10717. rpl)
  10718. (if (not as)
  10719. link
  10720. (setq rpl (cdr as))
  10721. (cond
  10722. ((symbolp rpl) (funcall rpl tag))
  10723. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  10724. (t (concat rpl tag)))))
  10725. link))
  10726. ;;; Storing and inserting links
  10727. (defvar org-insert-link-history nil
  10728. "Minibuffer history for links inserted with `org-insert-link'.")
  10729. (defvar org-stored-links nil
  10730. "Contains the links stored with `org-store-link'.")
  10731. (defvar org-store-link-plist nil
  10732. "Plist with info about the most recently link created with `org-store-link'.")
  10733. (defvar org-link-protocols nil
  10734. "Link protocols added to Org-mode using `org-add-link-type'.")
  10735. (defvar org-store-link-functions nil
  10736. "List of functions that are called to create and store a link.
  10737. Each function will be called in turn until one returns a non-nil
  10738. value. Each function should check if it is responsible for creating
  10739. this link (for example by looking at the major mode).
  10740. If not, it must exit and return nil.
  10741. If yes, it should return a non-nil value after a calling
  10742. `org-store-link-props' with a list of properties and values.
  10743. Special properties are:
  10744. :type The link prefix. like \"http\". This must be given.
  10745. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  10746. This is obligatory as well.
  10747. :description Optional default description for the second pair
  10748. of brackets in an Org-mode link. The user can still change
  10749. this when inserting this link into an Org-mode buffer.
  10750. In addition to these, any additional properties can be specified
  10751. and then used in remember templates.")
  10752. (defun org-add-link-type (type &optional follow publish)
  10753. "Add TYPE to the list of `org-link-types'.
  10754. Re-compute all regular expressions depending on `org-link-types'
  10755. FOLLOW and PUBLISH are two functions. Both take the link path as
  10756. an argument.
  10757. FOLLOW should do whatever is necessary to follow the link, for example
  10758. to find a file or display a mail message.
  10759. PUBLISH takes the path and retuns the string that should be used when
  10760. this document is published. FIMXE: This is actually not yet implemented."
  10761. (add-to-list 'org-link-types type t)
  10762. (org-make-link-regexps)
  10763. (add-to-list 'org-link-protocols
  10764. (list type follow publish)))
  10765. (defun org-add-agenda-custom-command (entry)
  10766. "Replace or add a command in `org-agenda-custom-commands'.
  10767. This is mostly for hacking and trying a new command - once the command
  10768. works you probably want to add it to `org-agenda-custom-commands' for good."
  10769. (let ((ass (assoc (car entry) org-agenda-custom-commands)))
  10770. (if ass
  10771. (setcdr ass (cdr entry))
  10772. (push entry org-agenda-custom-commands))))
  10773. ;;;###autoload
  10774. (defun org-store-link (arg)
  10775. "\\<org-mode-map>Store an org-link to the current location.
  10776. This link can later be inserted into an org-buffer with
  10777. \\[org-insert-link].
  10778. For some link types, a prefix arg is interpreted:
  10779. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  10780. For file links, arg negates `org-context-in-file-links'."
  10781. (interactive "P")
  10782. (setq org-store-link-plist nil) ; reset
  10783. (let (link cpltxt desc description search txt)
  10784. (cond
  10785. ((run-hook-with-args-until-success 'org-store-link-functions)
  10786. (setq link (plist-get org-store-link-plist :link)
  10787. desc (or (plist-get org-store-link-plist :description) link)))
  10788. ((eq major-mode 'bbdb-mode)
  10789. (let ((name (bbdb-record-name (bbdb-current-record)))
  10790. (company (bbdb-record-getprop (bbdb-current-record) 'company)))
  10791. (setq cpltxt (concat "bbdb:" (or name company))
  10792. link (org-make-link cpltxt))
  10793. (org-store-link-props :type "bbdb" :name name :company company)))
  10794. ((eq major-mode 'Info-mode)
  10795. (setq link (org-make-link "info:"
  10796. (file-name-nondirectory Info-current-file)
  10797. ":" Info-current-node))
  10798. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  10799. ":" Info-current-node))
  10800. (org-store-link-props :type "info" :file Info-current-file
  10801. :node Info-current-node))
  10802. ((eq major-mode 'calendar-mode)
  10803. (let ((cd (calendar-cursor-to-date)))
  10804. (setq link
  10805. (format-time-string
  10806. (car org-time-stamp-formats)
  10807. (apply 'encode-time
  10808. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  10809. nil nil nil))))
  10810. (org-store-link-props :type "calendar" :date cd)))
  10811. ((or (eq major-mode 'vm-summary-mode)
  10812. (eq major-mode 'vm-presentation-mode))
  10813. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  10814. (vm-follow-summary-cursor)
  10815. (save-excursion
  10816. (vm-select-folder-buffer)
  10817. (let* ((message (car vm-message-pointer))
  10818. (folder buffer-file-name)
  10819. (subject (vm-su-subject message))
  10820. (to (vm-get-header-contents message "To"))
  10821. (from (vm-get-header-contents message "From"))
  10822. (message-id (vm-su-message-id message)))
  10823. (org-store-link-props :type "vm" :from from :to to :subject subject
  10824. :message-id message-id)
  10825. (setq message-id (org-remove-angle-brackets message-id))
  10826. (setq folder (abbreviate-file-name folder))
  10827. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  10828. folder)
  10829. (setq folder (replace-match "" t t folder)))
  10830. (setq cpltxt (org-email-link-description))
  10831. (setq link (org-make-link "vm:" folder "#" message-id)))))
  10832. ((eq major-mode 'wl-summary-mode)
  10833. (let* ((msgnum (wl-summary-message-number))
  10834. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  10835. msgnum 'message-id))
  10836. (wl-message-entity
  10837. (if (fboundp 'elmo-message-entity)
  10838. (elmo-message-entity
  10839. wl-summary-buffer-elmo-folder msgnum)
  10840. (elmo-msgdb-overview-get-entity
  10841. msgnum (wl-summary-buffer-msgdb))))
  10842. (from (wl-summary-line-from))
  10843. (to (car (elmo-message-entity-field wl-message-entity 'to)))
  10844. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  10845. (wl-summary-line-subject))))
  10846. (org-store-link-props :type "wl" :from from :to to
  10847. :subject subject :message-id message-id)
  10848. (setq message-id (org-remove-angle-brackets message-id))
  10849. (setq cpltxt (org-email-link-description))
  10850. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  10851. "#" message-id))))
  10852. ((or (equal major-mode 'mh-folder-mode)
  10853. (equal major-mode 'mh-show-mode))
  10854. (let ((from (org-mhe-get-header "From:"))
  10855. (to (org-mhe-get-header "To:"))
  10856. (message-id (org-mhe-get-header "Message-Id:"))
  10857. (subject (org-mhe-get-header "Subject:")))
  10858. (org-store-link-props :type "mh" :from from :to to
  10859. :subject subject :message-id message-id)
  10860. (setq cpltxt (org-email-link-description))
  10861. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  10862. (org-remove-angle-brackets message-id)))))
  10863. ((eq major-mode 'rmail-mode)
  10864. (save-excursion
  10865. (save-restriction
  10866. (rmail-narrow-to-non-pruned-header)
  10867. (let ((folder buffer-file-name)
  10868. (message-id (mail-fetch-field "message-id"))
  10869. (from (mail-fetch-field "from"))
  10870. (to (mail-fetch-field "to"))
  10871. (subject (mail-fetch-field "subject")))
  10872. (org-store-link-props
  10873. :type "rmail" :from from :to to
  10874. :subject subject :message-id message-id)
  10875. (setq message-id (org-remove-angle-brackets message-id))
  10876. (setq cpltxt (org-email-link-description))
  10877. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  10878. ((eq major-mode 'gnus-group-mode)
  10879. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  10880. (gnus-group-group-name)) ; version
  10881. ((fboundp 'gnus-group-name)
  10882. (gnus-group-name))
  10883. (t "???"))))
  10884. (unless group (error "Not on a group"))
  10885. (org-store-link-props :type "gnus" :group group)
  10886. (setq cpltxt (concat
  10887. (if (org-xor arg org-usenet-links-prefer-google)
  10888. "http://groups.google.com/groups?group="
  10889. "gnus:")
  10890. group)
  10891. link (org-make-link cpltxt))))
  10892. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  10893. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  10894. (let* ((group gnus-newsgroup-name)
  10895. (article (gnus-summary-article-number))
  10896. (header (gnus-summary-article-header article))
  10897. (from (mail-header-from header))
  10898. (message-id (mail-header-id header))
  10899. (date (mail-header-date header))
  10900. (subject (gnus-summary-subject-string)))
  10901. (org-store-link-props :type "gnus" :from from :subject subject
  10902. :message-id message-id :group group)
  10903. (setq cpltxt (org-email-link-description))
  10904. (if (org-xor arg org-usenet-links-prefer-google)
  10905. (setq link
  10906. (concat
  10907. cpltxt "\n "
  10908. (format "http://groups.google.com/groups?as_umsgid=%s"
  10909. (org-fixup-message-id-for-http message-id))))
  10910. (setq link (org-make-link "gnus:" group
  10911. "#" (number-to-string article))))))
  10912. ((eq major-mode 'w3-mode)
  10913. (setq cpltxt (url-view-url t)
  10914. link (org-make-link cpltxt))
  10915. (org-store-link-props :type "w3" :url (url-view-url t)))
  10916. ((eq major-mode 'w3m-mode)
  10917. (setq cpltxt (or w3m-current-title w3m-current-url)
  10918. link (org-make-link w3m-current-url))
  10919. (org-store-link-props :type "w3m" :url (url-view-url t)))
  10920. ((setq search (run-hook-with-args-until-success
  10921. 'org-create-file-search-functions))
  10922. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  10923. "::" search))
  10924. (setq cpltxt (or description link)))
  10925. ((eq major-mode 'image-mode)
  10926. (setq cpltxt (concat "file:"
  10927. (abbreviate-file-name buffer-file-name))
  10928. link (org-make-link cpltxt))
  10929. (org-store-link-props :type "image" :file buffer-file-name))
  10930. ((eq major-mode 'dired-mode)
  10931. ;; link to the file in the current line
  10932. (setq cpltxt (concat "file:"
  10933. (abbreviate-file-name
  10934. (expand-file-name
  10935. (dired-get-filename nil t))))
  10936. link (org-make-link cpltxt)))
  10937. ((and buffer-file-name (org-mode-p))
  10938. ;; Just link to current headline
  10939. (setq cpltxt (concat "file:"
  10940. (abbreviate-file-name buffer-file-name)))
  10941. ;; Add a context search string
  10942. (when (org-xor org-context-in-file-links arg)
  10943. ;; Check if we are on a target
  10944. (if (org-in-regexp "<<\\(.*?\\)>>")
  10945. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  10946. (setq txt (cond
  10947. ((org-on-heading-p) nil)
  10948. ((org-region-active-p)
  10949. (buffer-substring (region-beginning) (region-end)))
  10950. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  10951. (when (or (null txt) (string-match "\\S-" txt))
  10952. (setq cpltxt
  10953. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10954. desc "NONE"))))
  10955. (if (string-match "::\\'" cpltxt)
  10956. (setq cpltxt (substring cpltxt 0 -2)))
  10957. (setq link (org-make-link cpltxt)))
  10958. ((buffer-file-name (buffer-base-buffer))
  10959. ;; Just link to this file here.
  10960. (setq cpltxt (concat "file:"
  10961. (abbreviate-file-name
  10962. (buffer-file-name (buffer-base-buffer)))))
  10963. ;; Add a context string
  10964. (when (org-xor org-context-in-file-links arg)
  10965. (setq txt (if (org-region-active-p)
  10966. (buffer-substring (region-beginning) (region-end))
  10967. (buffer-substring (point-at-bol) (point-at-eol))))
  10968. ;; Only use search option if there is some text.
  10969. (when (string-match "\\S-" txt)
  10970. (setq cpltxt
  10971. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  10972. desc "NONE")))
  10973. (setq link (org-make-link cpltxt)))
  10974. ((interactive-p)
  10975. (error "Cannot link to a buffer which is not visiting a file"))
  10976. (t (setq link nil)))
  10977. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  10978. (setq link (or link cpltxt)
  10979. desc (or desc cpltxt))
  10980. (if (equal desc "NONE") (setq desc nil))
  10981. (if (and (interactive-p) link)
  10982. (progn
  10983. (setq org-stored-links
  10984. (cons (list link desc) org-stored-links))
  10985. (message "Stored: %s" (or desc link)))
  10986. (and link (org-make-link-string link desc)))))
  10987. (defun org-store-link-props (&rest plist)
  10988. "Store link properties, extract names and addresses."
  10989. (let (x adr)
  10990. (when (setq x (plist-get plist :from))
  10991. (setq adr (mail-extract-address-components x))
  10992. (plist-put plist :fromname (car adr))
  10993. (plist-put plist :fromaddress (nth 1 adr)))
  10994. (when (setq x (plist-get plist :to))
  10995. (setq adr (mail-extract-address-components x))
  10996. (plist-put plist :toname (car adr))
  10997. (plist-put plist :toaddress (nth 1 adr))))
  10998. (let ((from (plist-get plist :from))
  10999. (to (plist-get plist :to)))
  11000. (when (and from to org-from-is-user-regexp)
  11001. (plist-put plist :fromto
  11002. (if (string-match org-from-is-user-regexp from)
  11003. (concat "to %t")
  11004. (concat "from %f")))))
  11005. (setq org-store-link-plist plist))
  11006. (defun org-email-link-description (&optional fmt)
  11007. "Return the description part of an email link.
  11008. This takes information from `org-store-link-plist' and formats it
  11009. according to FMT (default from `org-email-link-description-format')."
  11010. (setq fmt (or fmt org-email-link-description-format))
  11011. (let* ((p org-store-link-plist)
  11012. (to (plist-get p :toaddress))
  11013. (from (plist-get p :fromaddress))
  11014. (table
  11015. (list
  11016. (cons "%c" (plist-get p :fromto))
  11017. (cons "%F" (plist-get p :from))
  11018. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  11019. (cons "%T" (plist-get p :to))
  11020. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  11021. (cons "%s" (plist-get p :subject))
  11022. (cons "%m" (plist-get p :message-id)))))
  11023. (when (string-match "%c" fmt)
  11024. ;; Check if the user wrote this message
  11025. (if (and org-from-is-user-regexp from to
  11026. (save-match-data (string-match org-from-is-user-regexp from)))
  11027. (setq fmt (replace-match "to %t" t t fmt))
  11028. (setq fmt (replace-match "from %f" t t fmt))))
  11029. (org-replace-escapes fmt table)))
  11030. (defun org-make-org-heading-search-string (&optional string heading)
  11031. "Make search string for STRING or current headline."
  11032. (interactive)
  11033. (let ((s (or string (org-get-heading))))
  11034. (unless (and string (not heading))
  11035. ;; We are using a headline, clean up garbage in there.
  11036. (if (string-match org-todo-regexp s)
  11037. (setq s (replace-match "" t t s)))
  11038. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  11039. (setq s (replace-match "" t t s)))
  11040. (setq s (org-trim s))
  11041. (if (string-match (concat "^\\(" org-quote-string "\\|"
  11042. org-comment-string "\\)") s)
  11043. (setq s (replace-match "" t t s)))
  11044. (while (string-match org-ts-regexp s)
  11045. (setq s (replace-match "" t t s))))
  11046. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  11047. (setq s (replace-match " " t t s)))
  11048. (or string (setq s (concat "*" s))) ; Add * for headlines
  11049. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  11050. (defun org-make-link (&rest strings)
  11051. "Concatenate STRINGS."
  11052. (apply 'concat strings))
  11053. (defun org-make-link-string (link &optional description)
  11054. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  11055. (unless (string-match "\\S-" link)
  11056. (error "Empty link"))
  11057. (when (stringp description)
  11058. ;; Remove brackets from the description, they are fatal.
  11059. (while (string-match "\\[" description)
  11060. (setq description (replace-match "{" t t description)))
  11061. (while (string-match "\\]" description)
  11062. (setq description (replace-match "}" t t description))))
  11063. (when (equal (org-link-escape link) description)
  11064. ;; No description needed, it is identical
  11065. (setq description nil))
  11066. (when (and (not description)
  11067. (not (equal link (org-link-escape link))))
  11068. (setq description link))
  11069. (concat "[[" (org-link-escape link) "]"
  11070. (if description (concat "[" description "]") "")
  11071. "]"))
  11072. (defconst org-link-escape-chars
  11073. '((?\ . "%20")
  11074. (?\[ . "%5B")
  11075. (?\] . "%5D")
  11076. (?\340 . "%E0") ; `a
  11077. (?\342 . "%E2") ; ^a
  11078. (?\347 . "%E7") ; ,c
  11079. (?\350 . "%E8") ; `e
  11080. (?\351 . "%E9") ; 'e
  11081. (?\352 . "%EA") ; ^e
  11082. (?\356 . "%EE") ; ^i
  11083. (?\364 . "%F4") ; ^o
  11084. (?\371 . "%F9") ; `u
  11085. (?\373 . "%FB") ; ^u
  11086. (?\; . "%3B")
  11087. (?? . "%3F")
  11088. (?= . "%3D")
  11089. (?+ . "%2B")
  11090. )
  11091. "Association list of escapes for some characters problematic in links.
  11092. This is the list that is used for internal purposes.")
  11093. (defconst org-link-escape-chars-browser
  11094. '((?\ . "%20")) ; 32 for the SPC char
  11095. "Association list of escapes for some characters problematic in links.
  11096. This is the list that is used before handing over to the browser.")
  11097. (defun org-link-escape (text &optional table)
  11098. "Escape charaters in TEXT that are problematic for links."
  11099. (setq table (or table org-link-escape-chars))
  11100. (when text
  11101. (let ((re (mapconcat (lambda (x) (regexp-quote
  11102. (char-to-string (car x))))
  11103. table "\\|")))
  11104. (while (string-match re text)
  11105. (setq text
  11106. (replace-match
  11107. (cdr (assoc (string-to-char (match-string 0 text))
  11108. table))
  11109. t t text)))
  11110. text)))
  11111. (defun org-link-unescape (text &optional table)
  11112. "Reverse the action of `org-link-escape'."
  11113. (setq table (or table org-link-escape-chars))
  11114. (when text
  11115. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  11116. table "\\|")))
  11117. (while (string-match re text)
  11118. (setq text
  11119. (replace-match
  11120. (char-to-string (car (rassoc (match-string 0 text) table)))
  11121. t t text)))
  11122. text)))
  11123. (defun org-xor (a b)
  11124. "Exclusive or."
  11125. (if a (not b) b))
  11126. (defun org-get-header (header)
  11127. "Find a header field in the current buffer."
  11128. (save-excursion
  11129. (goto-char (point-min))
  11130. (let ((case-fold-search t) s)
  11131. (cond
  11132. ((eq header 'from)
  11133. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  11134. (setq s (match-string 1)))
  11135. (while (string-match "\"" s)
  11136. (setq s (replace-match "" t t s)))
  11137. (if (string-match "[<(].*" s)
  11138. (setq s (replace-match "" t t s))))
  11139. ((eq header 'message-id)
  11140. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  11141. (setq s (match-string 1))))
  11142. ((eq header 'subject)
  11143. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  11144. (setq s (match-string 1)))))
  11145. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  11146. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  11147. s)))
  11148. (defun org-fixup-message-id-for-http (s)
  11149. "Replace special characters in a message id, so it can be used in an http query."
  11150. (while (string-match "<" s)
  11151. (setq s (replace-match "%3C" t t s)))
  11152. (while (string-match ">" s)
  11153. (setq s (replace-match "%3E" t t s)))
  11154. (while (string-match "@" s)
  11155. (setq s (replace-match "%40" t t s)))
  11156. s)
  11157. ;;;###autoload
  11158. (defun org-insert-link-global ()
  11159. "Insert a link like Org-mode does.
  11160. This command can be called in any mode to insert a link in Org-mode syntax."
  11161. (interactive)
  11162. (org-run-like-in-org-mode 'org-insert-link))
  11163. (defun org-insert-link (&optional complete-file)
  11164. "Insert a link. At the prompt, enter the link.
  11165. Completion can be used to select a link previously stored with
  11166. `org-store-link'. When the empty string is entered (i.e. if you just
  11167. press RET at the prompt), the link defaults to the most recently
  11168. stored link. As SPC triggers completion in the minibuffer, you need to
  11169. use M-SPC or C-q SPC to force the insertion of a space character.
  11170. You will also be prompted for a description, and if one is given, it will
  11171. be displayed in the buffer instead of the link.
  11172. If there is already a link at point, this command will allow you to edit link
  11173. and description parts.
  11174. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  11175. selected using completion. The path to the file will be relative to
  11176. the current directory if the file is in the current directory or a
  11177. subdirectory. Otherwise, the link will be the absolute path as
  11178. completed in the minibuffer (i.e. normally ~/path/to/file).
  11179. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  11180. is in the current directory or below.
  11181. With three \\[universal-argument] prefixes, negate the meaning of
  11182. `org-keep-stored-link-after-insertion'."
  11183. (interactive "P")
  11184. (let* ((wcf (current-window-configuration))
  11185. (region (if (org-region-active-p)
  11186. (buffer-substring (region-beginning) (region-end))))
  11187. (remove (and region (list (region-beginning) (region-end))))
  11188. (desc region)
  11189. tmphist ; byte-compile incorrectly complains about this
  11190. link entry file)
  11191. (cond
  11192. ((org-in-regexp org-bracket-link-regexp 1)
  11193. ;; We do have a link at point, and we are going to edit it.
  11194. (setq remove (list (match-beginning 0) (match-end 0)))
  11195. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  11196. (setq link (read-string "Link: "
  11197. (org-link-unescape
  11198. (org-match-string-no-properties 1)))))
  11199. ((or (org-in-regexp org-angle-link-re)
  11200. (org-in-regexp org-plain-link-re))
  11201. ;; Convert to bracket link
  11202. (setq remove (list (match-beginning 0) (match-end 0))
  11203. link (read-string "Link: "
  11204. (org-remove-angle-brackets (match-string 0)))))
  11205. ((equal complete-file '(4))
  11206. ;; Completing read for file names.
  11207. (setq file (read-file-name "File: "))
  11208. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  11209. (pwd1 (file-name-as-directory (abbreviate-file-name
  11210. (expand-file-name ".")))))
  11211. (cond
  11212. ((equal complete-file '(16))
  11213. (setq link (org-make-link
  11214. "file:"
  11215. (abbreviate-file-name (expand-file-name file)))))
  11216. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  11217. (setq link (org-make-link "file:" (match-string 1 file))))
  11218. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  11219. (expand-file-name file))
  11220. (setq link (org-make-link
  11221. "file:" (match-string 1 (expand-file-name file)))))
  11222. (t (setq link (org-make-link "file:" file))))))
  11223. (t
  11224. ;; Read link, with completion for stored links.
  11225. (with-output-to-temp-buffer "*Org Links*"
  11226. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  11227. (when org-stored-links
  11228. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  11229. (princ (mapconcat
  11230. (lambda (x)
  11231. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  11232. (reverse org-stored-links) "\n"))))
  11233. (let ((cw (selected-window)))
  11234. (select-window (get-buffer-window "*Org Links*"))
  11235. (shrink-window-if-larger-than-buffer)
  11236. (setq truncate-lines t)
  11237. (select-window cw))
  11238. ;; Fake a link history, containing the stored links.
  11239. (setq tmphist (append (mapcar 'car org-stored-links)
  11240. org-insert-link-history))
  11241. (unwind-protect
  11242. (setq link (org-completing-read
  11243. "Link: "
  11244. (append
  11245. (mapcar (lambda (x) (list (concat (car x) ":")))
  11246. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  11247. (mapcar (lambda (x) (list (concat x ":")))
  11248. org-link-types))
  11249. nil nil nil
  11250. 'tmphist
  11251. (or (car (car org-stored-links)))))
  11252. (set-window-configuration wcf)
  11253. (kill-buffer "*Org Links*"))
  11254. (setq entry (assoc link org-stored-links))
  11255. (or entry (push link org-insert-link-history))
  11256. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  11257. (not org-keep-stored-link-after-insertion))
  11258. (setq org-stored-links (delq (assoc link org-stored-links)
  11259. org-stored-links)))
  11260. (setq desc (or desc (nth 1 entry)))))
  11261. (if (string-match org-plain-link-re link)
  11262. ;; URL-like link, normalize the use of angular brackets.
  11263. (setq link (org-make-link (org-remove-angle-brackets link))))
  11264. ;; Check if we are linking to the current file with a search option
  11265. ;; If yes, simplify the link by using only the search option.
  11266. (when (and buffer-file-name
  11267. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  11268. (let* ((path (match-string 1 link))
  11269. (case-fold-search nil)
  11270. (search (match-string 2 link)))
  11271. (save-match-data
  11272. (if (equal (file-truename buffer-file-name) (file-truename path))
  11273. ;; We are linking to this same file, with a search option
  11274. (setq link search)))))
  11275. ;; Check if we can/should use a relative path. If yes, simplify the link
  11276. (when (string-match "\\<file:\\(.*\\)" link)
  11277. (let* ((path (match-string 1 link))
  11278. (origpath path)
  11279. (desc-is-link (equal link desc))
  11280. (case-fold-search nil))
  11281. (cond
  11282. ((eq org-link-file-path-type 'absolute)
  11283. (setq path (abbreviate-file-name (expand-file-name path))))
  11284. ((eq org-link-file-path-type 'noabbrev)
  11285. (setq path (expand-file-name path)))
  11286. ((eq org-link-file-path-type 'relative)
  11287. (setq path (file-relative-name path)))
  11288. (t
  11289. (save-match-data
  11290. (if (string-match (concat "^" (regexp-quote
  11291. (file-name-as-directory
  11292. (expand-file-name "."))))
  11293. (expand-file-name path))
  11294. ;; We are linking a file with relative path name.
  11295. (setq path (substring (expand-file-name path)
  11296. (match-end 0)))))))
  11297. (setq link (concat "file:" path))
  11298. (if (equal desc origpath)
  11299. (setq desc path))))
  11300. (setq desc (read-string "Description: " desc))
  11301. (unless (string-match "\\S-" desc) (setq desc nil))
  11302. (if remove (apply 'delete-region remove))
  11303. (insert (org-make-link-string link desc))))
  11304. (defun org-completing-read (&rest args)
  11305. (let ((minibuffer-local-completion-map
  11306. (copy-keymap minibuffer-local-completion-map)))
  11307. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  11308. (apply 'completing-read args)))
  11309. ;;; Opening/following a link
  11310. (defvar org-link-search-failed nil)
  11311. (defun org-next-link ()
  11312. "Move forward to the next link.
  11313. If the link is in hidden text, expose it."
  11314. (interactive)
  11315. (when (and org-link-search-failed (eq this-command last-command))
  11316. (goto-char (point-min))
  11317. (message "Link search wrapped back to beginning of buffer"))
  11318. (setq org-link-search-failed nil)
  11319. (let* ((pos (point))
  11320. (ct (org-context))
  11321. (a (assoc :link ct)))
  11322. (if a (goto-char (nth 2 a)))
  11323. (if (re-search-forward org-any-link-re nil t)
  11324. (progn
  11325. (goto-char (match-beginning 0))
  11326. (if (org-invisible-p) (org-show-context)))
  11327. (goto-char pos)
  11328. (setq org-link-search-failed t)
  11329. (error "No further link found"))))
  11330. (defun org-previous-link ()
  11331. "Move backward to the previous link.
  11332. If the link is in hidden text, expose it."
  11333. (interactive)
  11334. (when (and org-link-search-failed (eq this-command last-command))
  11335. (goto-char (point-max))
  11336. (message "Link search wrapped back to end of buffer"))
  11337. (setq org-link-search-failed nil)
  11338. (let* ((pos (point))
  11339. (ct (org-context))
  11340. (a (assoc :link ct)))
  11341. (if a (goto-char (nth 1 a)))
  11342. (if (re-search-backward org-any-link-re nil t)
  11343. (progn
  11344. (goto-char (match-beginning 0))
  11345. (if (org-invisible-p) (org-show-context)))
  11346. (goto-char pos)
  11347. (setq org-link-search-failed t)
  11348. (error "No further link found"))))
  11349. (defun org-find-file-at-mouse (ev)
  11350. "Open file link or URL at mouse."
  11351. (interactive "e")
  11352. (mouse-set-point ev)
  11353. (org-open-at-point 'in-emacs))
  11354. (defun org-open-at-mouse (ev)
  11355. "Open file link or URL at mouse."
  11356. (interactive "e")
  11357. (mouse-set-point ev)
  11358. (org-open-at-point))
  11359. (defvar org-window-config-before-follow-link nil
  11360. "The window configuration before following a link.
  11361. This is saved in case the need arises to restore it.")
  11362. (defvar org-open-link-marker (make-marker)
  11363. "Marker pointing to the location where `org-open-at-point; was called.")
  11364. ;;;###autoload
  11365. (defun org-open-at-point-global ()
  11366. "Follow a link like Org-mode does.
  11367. This command can be called in any mode to follow a link that has
  11368. Org-mode syntax."
  11369. (interactive)
  11370. (org-run-like-in-org-mode 'org-open-at-point))
  11371. (defun org-open-at-point (&optional in-emacs)
  11372. "Open link at or after point.
  11373. If there is no link at point, this function will search forward up to
  11374. the end of the current subtree.
  11375. Normally, files will be opened by an appropriate application. If the
  11376. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  11377. (interactive "P")
  11378. (catch 'abort
  11379. (move-marker org-open-link-marker (point))
  11380. (setq org-window-config-before-follow-link (current-window-configuration))
  11381. (org-remove-occur-highlights nil nil t)
  11382. (if (org-at-timestamp-p t)
  11383. (org-follow-timestamp-link)
  11384. (let (type path link line search (pos (point)))
  11385. (catch 'match
  11386. (save-excursion
  11387. (skip-chars-forward "^]\n\r")
  11388. (when (org-in-regexp org-bracket-link-regexp)
  11389. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  11390. (while (string-match " *\n *" link)
  11391. (setq link (replace-match " " t t link)))
  11392. (setq link (org-link-expand-abbrev link))
  11393. (if (string-match org-link-re-with-space2 link)
  11394. (setq type (match-string 1 link) path (match-string 2 link))
  11395. (setq type "thisfile" path link))
  11396. (throw 'match t)))
  11397. (when (get-text-property (point) 'org-linked-text)
  11398. (setq type "thisfile"
  11399. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  11400. (1+ (point)) (point))
  11401. path (buffer-substring
  11402. (previous-single-property-change pos 'org-linked-text)
  11403. (next-single-property-change pos 'org-linked-text)))
  11404. (throw 'match t))
  11405. (save-excursion
  11406. (when (or (org-in-regexp org-angle-link-re)
  11407. (org-in-regexp org-plain-link-re))
  11408. (setq type (match-string 1) path (match-string 2))
  11409. (throw 'match t)))
  11410. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  11411. (setq type "tree-match"
  11412. path (match-string 1))
  11413. (throw 'match t))
  11414. (save-excursion
  11415. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  11416. (setq type "tags"
  11417. path (match-string 1))
  11418. (while (string-match ":" path)
  11419. (setq path (replace-match "+" t t path)))
  11420. (throw 'match t))))
  11421. (unless path
  11422. (error "No link found"))
  11423. ;; Remove any trailing spaces in path
  11424. (if (string-match " +\\'" path)
  11425. (setq path (replace-match "" t t path)))
  11426. (cond
  11427. ((assoc type org-link-protocols)
  11428. (funcall (nth 1 (assoc type org-link-protocols)) path))
  11429. ((equal type "mailto")
  11430. (let ((cmd (car org-link-mailto-program))
  11431. (args (cdr org-link-mailto-program)) args1
  11432. (address path) (subject "") a)
  11433. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  11434. (setq address (match-string 1 path)
  11435. subject (org-link-escape (match-string 2 path))))
  11436. (while args
  11437. (cond
  11438. ((not (stringp (car args))) (push (pop args) args1))
  11439. (t (setq a (pop args))
  11440. (if (string-match "%a" a)
  11441. (setq a (replace-match address t t a)))
  11442. (if (string-match "%s" a)
  11443. (setq a (replace-match subject t t a)))
  11444. (push a args1))))
  11445. (apply cmd (nreverse args1))))
  11446. ((member type '("http" "https" "ftp" "news"))
  11447. (browse-url (concat type ":" (org-link-escape
  11448. path org-link-escape-chars-browser))))
  11449. ((member type '("message"))
  11450. (browse-url (concat type ":" path)))
  11451. ((string= type "tags")
  11452. (org-tags-view in-emacs path))
  11453. ((string= type "thisfile")
  11454. (if in-emacs
  11455. (switch-to-buffer-other-window
  11456. (org-get-buffer-for-internal-link (current-buffer)))
  11457. (org-mark-ring-push))
  11458. (let ((cmd `(org-link-search
  11459. ,path
  11460. ,(cond ((equal in-emacs '(4)) 'occur)
  11461. ((equal in-emacs '(16)) 'org-occur)
  11462. (t nil))
  11463. ,pos)))
  11464. (condition-case nil (eval cmd)
  11465. (error (progn (widen) (eval cmd))))))
  11466. ((string= type "tree-match")
  11467. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  11468. ((string= type "file")
  11469. (if (string-match "::\\([0-9]+\\)\\'" path)
  11470. (setq line (string-to-number (match-string 1 path))
  11471. path (substring path 0 (match-beginning 0)))
  11472. (if (string-match "::\\(.+\\)\\'" path)
  11473. (setq search (match-string 1 path)
  11474. path (substring path 0 (match-beginning 0)))))
  11475. (if (string-match "[*?{]" (file-name-nondirectory path))
  11476. (dired path)
  11477. (org-open-file path in-emacs line search)))
  11478. ((string= type "news")
  11479. (org-follow-gnus-link path))
  11480. ((string= type "bbdb")
  11481. (org-follow-bbdb-link path))
  11482. ((string= type "info")
  11483. (org-follow-info-link path))
  11484. ((string= type "gnus")
  11485. (let (group article)
  11486. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11487. (error "Error in Gnus link"))
  11488. (setq group (match-string 1 path)
  11489. article (match-string 3 path))
  11490. (org-follow-gnus-link group article)))
  11491. ((string= type "vm")
  11492. (let (folder article)
  11493. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11494. (error "Error in VM link"))
  11495. (setq folder (match-string 1 path)
  11496. article (match-string 3 path))
  11497. ;; in-emacs is the prefix arg, will be interpreted as read-only
  11498. (org-follow-vm-link folder article in-emacs)))
  11499. ((string= type "wl")
  11500. (let (folder article)
  11501. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11502. (error "Error in Wanderlust link"))
  11503. (setq folder (match-string 1 path)
  11504. article (match-string 3 path))
  11505. (org-follow-wl-link folder article)))
  11506. ((string= type "mhe")
  11507. (let (folder article)
  11508. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11509. (error "Error in MHE link"))
  11510. (setq folder (match-string 1 path)
  11511. article (match-string 3 path))
  11512. (org-follow-mhe-link folder article)))
  11513. ((string= type "rmail")
  11514. (let (folder article)
  11515. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  11516. (error "Error in RMAIL link"))
  11517. (setq folder (match-string 1 path)
  11518. article (match-string 3 path))
  11519. (org-follow-rmail-link folder article)))
  11520. ((string= type "shell")
  11521. (let ((cmd path))
  11522. (if (or (not org-confirm-shell-link-function)
  11523. (funcall org-confirm-shell-link-function
  11524. (format "Execute \"%s\" in shell? "
  11525. (org-add-props cmd nil
  11526. 'face 'org-warning))))
  11527. (progn
  11528. (message "Executing %s" cmd)
  11529. (shell-command cmd))
  11530. (error "Abort"))))
  11531. ((string= type "elisp")
  11532. (let ((cmd path))
  11533. (if (or (not org-confirm-elisp-link-function)
  11534. (funcall org-confirm-elisp-link-function
  11535. (format "Execute \"%s\" as elisp? "
  11536. (org-add-props cmd nil
  11537. 'face 'org-warning))))
  11538. (message "%s => %s" cmd (eval (read cmd)))
  11539. (error "Abort"))))
  11540. (t
  11541. (browse-url-at-point)))))
  11542. (move-marker org-open-link-marker nil)))
  11543. ;;; File search
  11544. (defvar org-create-file-search-functions nil
  11545. "List of functions to construct the right search string for a file link.
  11546. These functions are called in turn with point at the location to
  11547. which the link should point.
  11548. A function in the hook should first test if it would like to
  11549. handle this file type, for example by checking the major-mode or
  11550. the file extension. If it decides not to handle this file, it
  11551. should just return nil to give other functions a chance. If it
  11552. does handle the file, it must return the search string to be used
  11553. when following the link. The search string will be part of the
  11554. file link, given after a double colon, and `org-open-at-point'
  11555. will automatically search for it. If special measures must be
  11556. taken to make the search successful, another function should be
  11557. added to the companion hook `org-execute-file-search-functions',
  11558. which see.
  11559. A function in this hook may also use `setq' to set the variable
  11560. `description' to provide a suggestion for the descriptive text to
  11561. be used for this link when it gets inserted into an Org-mode
  11562. buffer with \\[org-insert-link].")
  11563. (defvar org-execute-file-search-functions nil
  11564. "List of functions to execute a file search triggered by a link.
  11565. Functions added to this hook must accept a single argument, the
  11566. search string that was part of the file link, the part after the
  11567. double colon. The function must first check if it would like to
  11568. handle this search, for example by checking the major-mode or the
  11569. file extension. If it decides not to handle this search, it
  11570. should just return nil to give other functions a chance. If it
  11571. does handle the search, it must return a non-nil value to keep
  11572. other functions from trying.
  11573. Each function can access the current prefix argument through the
  11574. variable `current-prefix-argument'. Note that a single prefix is
  11575. used to force opening a link in Emacs, so it may be good to only
  11576. use a numeric or double prefix to guide the search function.
  11577. In case this is needed, a function in this hook can also restore
  11578. the window configuration before `org-open-at-point' was called using:
  11579. (set-window-configuration org-window-config-before-follow-link)")
  11580. (defun org-link-search (s &optional type avoid-pos)
  11581. "Search for a link search option.
  11582. If S is surrounded by forward slashes, it is interpreted as a
  11583. regular expression. In org-mode files, this will create an `org-occur'
  11584. sparse tree. In ordinary files, `occur' will be used to list matches.
  11585. If the current buffer is in `dired-mode', grep will be used to search
  11586. in all files. If AVOID-POS is given, ignore matches near that position."
  11587. (let ((case-fold-search t)
  11588. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  11589. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  11590. (append '(("") (" ") ("\t") ("\n"))
  11591. org-emphasis-alist)
  11592. "\\|") "\\)"))
  11593. (pos (point))
  11594. (pre "") (post "")
  11595. words re0 re1 re2 re3 re4 re5 re2a reall)
  11596. (cond
  11597. ;; First check if there are any special
  11598. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  11599. ;; Now try the builtin stuff
  11600. ((save-excursion
  11601. (goto-char (point-min))
  11602. (and
  11603. (re-search-forward
  11604. (concat "<<" (regexp-quote s0) ">>") nil t)
  11605. (setq pos (match-beginning 0))))
  11606. ;; There is an exact target for this
  11607. (goto-char pos))
  11608. ((string-match "^/\\(.*\\)/$" s)
  11609. ;; A regular expression
  11610. (cond
  11611. ((org-mode-p)
  11612. (org-occur (match-string 1 s)))
  11613. ;;((eq major-mode 'dired-mode)
  11614. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  11615. (t (org-do-occur (match-string 1 s)))))
  11616. (t
  11617. ;; A normal search strings
  11618. (when (equal (string-to-char s) ?*)
  11619. ;; Anchor on headlines, post may include tags.
  11620. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  11621. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  11622. s (substring s 1)))
  11623. (remove-text-properties
  11624. 0 (length s)
  11625. '(face nil mouse-face nil keymap nil fontified nil) s)
  11626. ;; Make a series of regular expressions to find a match
  11627. (setq words (org-split-string s "[ \n\r\t]+")
  11628. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  11629. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  11630. "\\)" markers)
  11631. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  11632. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  11633. re1 (concat pre re2 post)
  11634. re3 (concat pre re4 post)
  11635. re5 (concat pre ".*" re4)
  11636. re2 (concat pre re2)
  11637. re2a (concat pre re2a)
  11638. re4 (concat pre re4)
  11639. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  11640. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  11641. re5 "\\)"
  11642. ))
  11643. (cond
  11644. ((eq type 'org-occur) (org-occur reall))
  11645. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  11646. (t (goto-char (point-min))
  11647. (if (or (org-search-not-self 1 re0 nil t)
  11648. (org-search-not-self 1 re1 nil t)
  11649. (org-search-not-self 1 re2 nil t)
  11650. (org-search-not-self 1 re2a nil t)
  11651. (org-search-not-self 1 re3 nil t)
  11652. (org-search-not-self 1 re4 nil t)
  11653. (org-search-not-self 1 re5 nil t)
  11654. )
  11655. (goto-char (match-beginning 1))
  11656. (goto-char pos)
  11657. (error "No match")))))
  11658. (t
  11659. ;; Normal string-search
  11660. (goto-char (point-min))
  11661. (if (search-forward s nil t)
  11662. (goto-char (match-beginning 0))
  11663. (error "No match"))))
  11664. (and (org-mode-p) (org-show-context 'link-search))))
  11665. (defun org-search-not-self (group &rest args)
  11666. "Execute `re-search-forward', but only accept matches that do not
  11667. enclose the position of `org-open-link-marker'."
  11668. (let ((m org-open-link-marker))
  11669. (catch 'exit
  11670. (while (apply 're-search-forward args)
  11671. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  11672. (goto-char (match-end group))
  11673. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  11674. (> (match-beginning 0) (marker-position m))
  11675. (< (match-end 0) (marker-position m)))
  11676. (save-match-data
  11677. (or (not (org-in-regexp
  11678. org-bracket-link-analytic-regexp 1))
  11679. (not (match-end 4)) ; no description
  11680. (and (<= (match-beginning 4) (point))
  11681. (>= (match-end 4) (point))))))
  11682. (throw 'exit (point))))))))
  11683. (defun org-get-buffer-for-internal-link (buffer)
  11684. "Return a buffer to be used for displaying the link target of internal links."
  11685. (cond
  11686. ((not org-display-internal-link-with-indirect-buffer)
  11687. buffer)
  11688. ((string-match "(Clone)$" (buffer-name buffer))
  11689. (message "Buffer is already a clone, not making another one")
  11690. ;; we also do not modify visibility in this case
  11691. buffer)
  11692. (t ; make a new indirect buffer for displaying the link
  11693. (let* ((bn (buffer-name buffer))
  11694. (ibn (concat bn "(Clone)"))
  11695. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  11696. (with-current-buffer ib (org-overview))
  11697. ib))))
  11698. (defun org-do-occur (regexp &optional cleanup)
  11699. "Call the Emacs command `occur'.
  11700. If CLEANUP is non-nil, remove the printout of the regular expression
  11701. in the *Occur* buffer. This is useful if the regex is long and not useful
  11702. to read."
  11703. (occur regexp)
  11704. (when cleanup
  11705. (let ((cwin (selected-window)) win beg end)
  11706. (when (setq win (get-buffer-window "*Occur*"))
  11707. (select-window win))
  11708. (goto-char (point-min))
  11709. (when (re-search-forward "match[a-z]+" nil t)
  11710. (setq beg (match-end 0))
  11711. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  11712. (setq end (1- (match-beginning 0)))))
  11713. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  11714. (goto-char (point-min))
  11715. (select-window cwin))))
  11716. ;;; The mark ring for links jumps
  11717. (defvar org-mark-ring nil
  11718. "Mark ring for positions before jumps in Org-mode.")
  11719. (defvar org-mark-ring-last-goto nil
  11720. "Last position in the mark ring used to go back.")
  11721. ;; Fill and close the ring
  11722. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  11723. (loop for i from 1 to org-mark-ring-length do
  11724. (push (make-marker) org-mark-ring))
  11725. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  11726. org-mark-ring)
  11727. (defun org-mark-ring-push (&optional pos buffer)
  11728. "Put the current position or POS into the mark ring and rotate it."
  11729. (interactive)
  11730. (setq pos (or pos (point)))
  11731. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  11732. (move-marker (car org-mark-ring)
  11733. (or pos (point))
  11734. (or buffer (current-buffer)))
  11735. (message "%s"
  11736. (substitute-command-keys
  11737. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  11738. (defun org-mark-ring-goto (&optional n)
  11739. "Jump to the previous position in the mark ring.
  11740. With prefix arg N, jump back that many stored positions. When
  11741. called several times in succession, walk through the entire ring.
  11742. Org-mode commands jumping to a different position in the current file,
  11743. or to another Org-mode file, automatically push the old position
  11744. onto the ring."
  11745. (interactive "p")
  11746. (let (p m)
  11747. (if (eq last-command this-command)
  11748. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  11749. (setq p org-mark-ring))
  11750. (setq org-mark-ring-last-goto p)
  11751. (setq m (car p))
  11752. (switch-to-buffer (marker-buffer m))
  11753. (goto-char m)
  11754. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  11755. (defun org-remove-angle-brackets (s)
  11756. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  11757. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  11758. s)
  11759. (defun org-add-angle-brackets (s)
  11760. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  11761. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  11762. s)
  11763. ;;; Following specific links
  11764. (defun org-follow-timestamp-link ()
  11765. (cond
  11766. ((org-at-date-range-p t)
  11767. (let ((org-agenda-start-on-weekday)
  11768. (t1 (match-string 1))
  11769. (t2 (match-string 2)))
  11770. (setq t1 (time-to-days (org-time-string-to-time t1))
  11771. t2 (time-to-days (org-time-string-to-time t2)))
  11772. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  11773. ((org-at-timestamp-p t)
  11774. (org-agenda-list nil (time-to-days (org-time-string-to-time
  11775. (substring (match-string 1) 0 10)))
  11776. 1))
  11777. (t (error "This should not happen"))))
  11778. (defun org-follow-bbdb-link (name)
  11779. "Follow a BBDB link to NAME."
  11780. (require 'bbdb)
  11781. (let ((inhibit-redisplay (not debug-on-error))
  11782. (bbdb-electric-p nil))
  11783. (catch 'exit
  11784. ;; Exact match on name
  11785. (bbdb-name (concat "\\`" name "\\'") nil)
  11786. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11787. ;; Exact match on name
  11788. (bbdb-company (concat "\\`" name "\\'") nil)
  11789. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11790. ;; Partial match on name
  11791. (bbdb-name name nil)
  11792. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11793. ;; Partial match on company
  11794. (bbdb-company name nil)
  11795. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  11796. ;; General match including network address and notes
  11797. (bbdb name nil)
  11798. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  11799. (delete-window (get-buffer-window "*BBDB*"))
  11800. (error "No matching BBDB record")))))
  11801. (defun org-follow-info-link (name)
  11802. "Follow an info file & node link to NAME."
  11803. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  11804. (string-match "\\(.*\\)" name))
  11805. (progn
  11806. (require 'info)
  11807. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  11808. (Info-find-node (match-string 1 name) (match-string 2 name))
  11809. (Info-find-node (match-string 1 name) "Top")))
  11810. (message "Could not open: %s" name)))
  11811. (defun org-follow-gnus-link (&optional group article)
  11812. "Follow a Gnus link to GROUP and ARTICLE."
  11813. (require 'gnus)
  11814. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  11815. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  11816. (cond ((and group article)
  11817. (gnus-group-read-group 1 nil group)
  11818. (gnus-summary-goto-article (string-to-number article) nil t))
  11819. (group (gnus-group-jump-to-group group))))
  11820. (defun org-follow-vm-link (&optional folder article readonly)
  11821. "Follow a VM link to FOLDER and ARTICLE."
  11822. (require 'vm)
  11823. (setq article (org-add-angle-brackets article))
  11824. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  11825. ;; ange-ftp or efs or tramp access
  11826. (let ((user (or (match-string 1 folder) (user-login-name)))
  11827. (host (match-string 2 folder))
  11828. (file (match-string 3 folder)))
  11829. (cond
  11830. ((featurep 'tramp)
  11831. ;; use tramp to access the file
  11832. (if (featurep 'xemacs)
  11833. (setq folder (format "[%s@%s]%s" user host file))
  11834. (setq folder (format "/%s@%s:%s" user host file))))
  11835. (t
  11836. ;; use ange-ftp or efs
  11837. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  11838. (setq folder (format "/%s@%s:%s" user host file))))))
  11839. (when folder
  11840. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  11841. (sit-for 0.1)
  11842. (when article
  11843. (vm-select-folder-buffer)
  11844. (widen)
  11845. (let ((case-fold-search t))
  11846. (goto-char (point-min))
  11847. (if (not (re-search-forward
  11848. (concat "^" "message-id: *" (regexp-quote article))))
  11849. (error "Could not find the specified message in this folder"))
  11850. (vm-isearch-update)
  11851. (vm-isearch-narrow)
  11852. (vm-beginning-of-message)
  11853. (vm-summarize)))))
  11854. (defun org-follow-wl-link (folder article)
  11855. "Follow a Wanderlust link to FOLDER and ARTICLE."
  11856. (if (and (string= folder "%")
  11857. article
  11858. (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
  11859. ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
  11860. ;; Thus, we recompose folder and article ids.
  11861. (setq folder (format "%s#%s" folder (match-string 1 article))
  11862. article (match-string 3 article)))
  11863. (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
  11864. (error "No such folder: %s" folder))
  11865. (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
  11866. (and article
  11867. (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
  11868. (wl-summary-redisplay)))
  11869. (defun org-follow-rmail-link (folder article)
  11870. "Follow an RMAIL link to FOLDER and ARTICLE."
  11871. (setq article (org-add-angle-brackets article))
  11872. (let (message-number)
  11873. (save-excursion
  11874. (save-window-excursion
  11875. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11876. (setq message-number
  11877. (save-restriction
  11878. (widen)
  11879. (goto-char (point-max))
  11880. (if (re-search-backward
  11881. (concat "^Message-ID:\\s-+" (regexp-quote
  11882. (or article "")))
  11883. nil t)
  11884. (rmail-what-message))))))
  11885. (if message-number
  11886. (progn
  11887. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  11888. (rmail-show-message message-number)
  11889. message-number)
  11890. (error "Message not found"))))
  11891. ;;; mh-e integration based on planner-mode
  11892. (defun org-mhe-get-message-real-folder ()
  11893. "Return the name of the current message real folder, so if you use
  11894. sequences, it will now work."
  11895. (save-excursion
  11896. (let* ((folder
  11897. (if (equal major-mode 'mh-folder-mode)
  11898. mh-current-folder
  11899. ;; Refer to the show buffer
  11900. mh-show-folder-buffer))
  11901. (end-index
  11902. (if (boundp 'mh-index-folder)
  11903. (min (length mh-index-folder) (length folder))))
  11904. )
  11905. ;; a simple test on mh-index-data does not work, because
  11906. ;; mh-index-data is always nil in a show buffer.
  11907. (if (and (boundp 'mh-index-folder)
  11908. (string= mh-index-folder (substring folder 0 end-index)))
  11909. (if (equal major-mode 'mh-show-mode)
  11910. (save-window-excursion
  11911. (let (pop-up-frames)
  11912. (when (buffer-live-p (get-buffer folder))
  11913. (progn
  11914. (pop-to-buffer folder)
  11915. (org-mhe-get-message-folder-from-index)
  11916. )
  11917. )))
  11918. (org-mhe-get-message-folder-from-index)
  11919. )
  11920. folder
  11921. )
  11922. )))
  11923. (defun org-mhe-get-message-folder-from-index ()
  11924. "Returns the name of the message folder in a index folder buffer."
  11925. (save-excursion
  11926. (mh-index-previous-folder)
  11927. (re-search-forward "^\\(+.*\\)$" nil t)
  11928. (message "%s" (match-string 1))))
  11929. (defun org-mhe-get-message-folder ()
  11930. "Return the name of the current message folder. Be careful if you
  11931. use sequences."
  11932. (save-excursion
  11933. (if (equal major-mode 'mh-folder-mode)
  11934. mh-current-folder
  11935. ;; Refer to the show buffer
  11936. mh-show-folder-buffer)))
  11937. (defun org-mhe-get-message-num ()
  11938. "Return the number of the current message. Be careful if you
  11939. use sequences."
  11940. (save-excursion
  11941. (if (equal major-mode 'mh-folder-mode)
  11942. (mh-get-msg-num nil)
  11943. ;; Refer to the show buffer
  11944. (mh-show-buffer-message-number))))
  11945. (defun org-mhe-get-header (header)
  11946. "Return a header of the message in folder mode. This will create a
  11947. show buffer for the corresponding message. If you have a more clever
  11948. idea..."
  11949. (let* ((folder (org-mhe-get-message-folder))
  11950. (num (org-mhe-get-message-num))
  11951. (buffer (get-buffer-create (concat "show-" folder)))
  11952. (header-field))
  11953. (with-current-buffer buffer
  11954. (mh-display-msg num folder)
  11955. (if (equal major-mode 'mh-folder-mode)
  11956. (mh-header-display)
  11957. (mh-show-header-display))
  11958. (set-buffer buffer)
  11959. (setq header-field (mh-get-header-field header))
  11960. (if (equal major-mode 'mh-folder-mode)
  11961. (mh-show)
  11962. (mh-show-show))
  11963. header-field)))
  11964. (defun org-follow-mhe-link (folder article)
  11965. "Follow an MHE link to FOLDER and ARTICLE.
  11966. If ARTICLE is nil FOLDER is shown. If the configuration variable
  11967. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  11968. ARTICLE is searched in all folders. Indexed searches (swish++,
  11969. namazu, and others supported by MH-E) will always search in all
  11970. folders."
  11971. (require 'mh-e)
  11972. (require 'mh-search)
  11973. (require 'mh-utils)
  11974. (mh-find-path)
  11975. (if (not article)
  11976. (mh-visit-folder (mh-normalize-folder-name folder))
  11977. (setq article (org-add-angle-brackets article))
  11978. (mh-search-choose)
  11979. (if (equal mh-searcher 'pick)
  11980. (progn
  11981. (mh-search folder (list "--message-id" article))
  11982. (when (and org-mhe-search-all-folders
  11983. (not (org-mhe-get-message-real-folder)))
  11984. (kill-this-buffer)
  11985. (mh-search "+" (list "--message-id" article))))
  11986. (mh-search "+" article))
  11987. (if (org-mhe-get-message-real-folder)
  11988. (mh-show-msg 1)
  11989. (kill-this-buffer)
  11990. (error "Message not found"))))
  11991. ;;; BibTeX links
  11992. ;; Use the custom search meachnism to construct and use search strings for
  11993. ;; file links to BibTeX database entries.
  11994. (defun org-create-file-search-in-bibtex ()
  11995. "Create the search string and description for a BibTeX database entry."
  11996. (when (eq major-mode 'bibtex-mode)
  11997. ;; yes, we want to construct this search string.
  11998. ;; Make a good description for this entry, using names, year and the title
  11999. ;; Put it into the `description' variable which is dynamically scoped.
  12000. (let ((bibtex-autokey-names 1)
  12001. (bibtex-autokey-names-stretch 1)
  12002. (bibtex-autokey-name-case-convert-function 'identity)
  12003. (bibtex-autokey-name-separator " & ")
  12004. (bibtex-autokey-additional-names " et al.")
  12005. (bibtex-autokey-year-length 4)
  12006. (bibtex-autokey-name-year-separator " ")
  12007. (bibtex-autokey-titlewords 3)
  12008. (bibtex-autokey-titleword-separator " ")
  12009. (bibtex-autokey-titleword-case-convert-function 'identity)
  12010. (bibtex-autokey-titleword-length 'infty)
  12011. (bibtex-autokey-year-title-separator ": "))
  12012. (setq description (bibtex-generate-autokey)))
  12013. ;; Now parse the entry, get the key and return it.
  12014. (save-excursion
  12015. (bibtex-beginning-of-entry)
  12016. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  12017. (defun org-execute-file-search-in-bibtex (s)
  12018. "Find the link search string S as a key for a database entry."
  12019. (when (eq major-mode 'bibtex-mode)
  12020. ;; Yes, we want to do the search in this file.
  12021. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  12022. (goto-char (point-min))
  12023. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  12024. (regexp-quote s) "[ \t\n]*,") nil t)
  12025. (goto-char (match-beginning 0)))
  12026. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  12027. ;; Use double prefix to indicate that any web link should be browsed
  12028. (let ((b (current-buffer)) (p (point)))
  12029. ;; Restore the window configuration because we just use the web link
  12030. (set-window-configuration org-window-config-before-follow-link)
  12031. (save-excursion (set-buffer b) (goto-char p)
  12032. (bibtex-url)))
  12033. (recenter 0)) ; Move entry start to beginning of window
  12034. ;; return t to indicate that the search is done.
  12035. t))
  12036. ;; Finally add the functions to the right hooks.
  12037. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  12038. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  12039. ;; end of Bibtex link setup
  12040. ;;; Following file links
  12041. (defun org-open-file (path &optional in-emacs line search)
  12042. "Open the file at PATH.
  12043. First, this expands any special file name abbreviations. Then the
  12044. configuration variable `org-file-apps' is checked if it contains an
  12045. entry for this file type, and if yes, the corresponding command is launched.
  12046. If no application is found, Emacs simply visits the file.
  12047. With optional argument IN-EMACS, Emacs will visit the file.
  12048. Optional LINE specifies a line to go to, optional SEARCH a string to
  12049. search for. If LINE or SEARCH is given, the file will always be
  12050. opened in Emacs.
  12051. If the file does not exist, an error is thrown."
  12052. (setq in-emacs (or in-emacs line search))
  12053. (let* ((file (if (equal path "")
  12054. buffer-file-name
  12055. (substitute-in-file-name (expand-file-name path))))
  12056. (apps (append org-file-apps (org-default-apps)))
  12057. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  12058. (dirp (if remp nil (file-directory-p file)))
  12059. (dfile (downcase file))
  12060. (old-buffer (current-buffer))
  12061. (old-pos (point))
  12062. (old-mode major-mode)
  12063. ext cmd)
  12064. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  12065. (setq ext (match-string 1 dfile))
  12066. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  12067. (setq ext (match-string 1 dfile))))
  12068. (if in-emacs
  12069. (setq cmd 'emacs)
  12070. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  12071. (and dirp (cdr (assoc 'directory apps)))
  12072. (cdr (assoc ext apps))
  12073. (cdr (assoc t apps)))))
  12074. (when (eq cmd 'mailcap)
  12075. (require 'mailcap)
  12076. (mailcap-parse-mailcaps)
  12077. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  12078. (command (mailcap-mime-info mime-type)))
  12079. (if (stringp command)
  12080. (setq cmd command)
  12081. (setq cmd 'emacs))))
  12082. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  12083. (not (file-exists-p file))
  12084. (not org-open-non-existing-files))
  12085. (error "No such file: %s" file))
  12086. (cond
  12087. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  12088. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  12089. (while (string-match "['\"]%s['\"]" cmd)
  12090. (setq cmd (replace-match "%s" t t cmd)))
  12091. (while (string-match "%s" cmd)
  12092. (setq cmd (replace-match
  12093. (save-match-data (shell-quote-argument file))
  12094. t t cmd)))
  12095. (save-window-excursion
  12096. (start-process-shell-command cmd nil cmd)))
  12097. ((or (stringp cmd)
  12098. (eq cmd 'emacs))
  12099. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  12100. (widen)
  12101. (if line (goto-line line)
  12102. (if search (org-link-search search))))
  12103. ((consp cmd)
  12104. (eval cmd))
  12105. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  12106. (and (org-mode-p) (eq old-mode 'org-mode)
  12107. (or (not (equal old-buffer (current-buffer)))
  12108. (not (equal old-pos (point))))
  12109. (org-mark-ring-push old-pos old-buffer))))
  12110. (defun org-default-apps ()
  12111. "Return the default applications for this operating system."
  12112. (cond
  12113. ((eq system-type 'darwin)
  12114. org-file-apps-defaults-macosx)
  12115. ((eq system-type 'windows-nt)
  12116. org-file-apps-defaults-windowsnt)
  12117. (t org-file-apps-defaults-gnu)))
  12118. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  12119. (defun org-file-remote-p (file)
  12120. "Test whether FILE specifies a location on a remote system.
  12121. Return non-nil if the location is indeed remote.
  12122. For example, the filename \"/user@host:/foo\" specifies a location
  12123. on the system \"/user@host:\"."
  12124. (cond ((fboundp 'file-remote-p)
  12125. (file-remote-p file))
  12126. ((fboundp 'tramp-handle-file-remote-p)
  12127. (tramp-handle-file-remote-p file))
  12128. ((and (boundp 'ange-ftp-name-format)
  12129. (string-match (car ange-ftp-name-format) file))
  12130. t)
  12131. (t nil)))
  12132. ;;;; Hooks for remember.el, and refiling
  12133. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  12134. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  12135. ;;;###autoload
  12136. (defun org-remember-insinuate ()
  12137. "Setup remember.el for use wiht Org-mode."
  12138. (require 'remember)
  12139. (setq remember-annotation-functions '(org-remember-annotation))
  12140. (setq remember-handler-functions '(org-remember-handler))
  12141. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  12142. ;;;###autoload
  12143. (defun org-remember-annotation ()
  12144. "Return a link to the current location as an annotation for remember.el.
  12145. If you are using Org-mode files as target for data storage with
  12146. remember.el, then the annotations should include a link compatible with the
  12147. conventions in Org-mode. This function returns such a link."
  12148. (org-store-link nil))
  12149. (defconst org-remember-help
  12150. "Select a destination location for the note.
  12151. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  12152. RET on headline -> Store as sublevel entry to current headline
  12153. RET at beg-of-buf -> Append to file as level 2 headline
  12154. <left>/<right> -> before/after current headline, same headings level")
  12155. (defvar org-remember-previous-location nil)
  12156. (defvar org-force-remember-template-char) ;; dynamically scoped
  12157. (defun org-select-remember-template (&optional use-char)
  12158. (when org-remember-templates
  12159. (let* ((templates (mapcar (lambda (x)
  12160. (if (stringp (car x))
  12161. (append (list (nth 1 x) (car x)) (cddr x))
  12162. (append (list (car x) "") (cdr x))))
  12163. org-remember-templates))
  12164. (char (or use-char
  12165. (cond
  12166. ((= (length templates) 1)
  12167. (caar templates))
  12168. ((and (boundp 'org-force-remember-template-char)
  12169. org-force-remember-template-char)
  12170. (if (stringp org-force-remember-template-char)
  12171. (string-to-char org-force-remember-template-char)
  12172. org-force-remember-template-char))
  12173. (t
  12174. (message "Select template: %s"
  12175. (mapconcat
  12176. (lambda (x)
  12177. (cond
  12178. ((not (string-match "\\S-" (nth 1 x)))
  12179. (format "[%c]" (car x)))
  12180. ((equal (downcase (car x))
  12181. (downcase (aref (nth 1 x) 0)))
  12182. (format "[%c]%s" (car x)
  12183. (substring (nth 1 x) 1)))
  12184. (t (format "[%c]%s" (car x) (nth 1 x)))))
  12185. templates " "))
  12186. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  12187. (when (equal char0 ?\C-g)
  12188. (jump-to-register remember-register)
  12189. (kill-buffer remember-buffer))
  12190. char0))))))
  12191. (cddr (assoc char templates)))))
  12192. (defvar x-last-selected-text)
  12193. (defvar x-last-selected-text-primary)
  12194. ;;;###autoload
  12195. (defun org-remember-apply-template (&optional use-char skip-interactive)
  12196. "Initialize *remember* buffer with template, invoke `org-mode'.
  12197. This function should be placed into `remember-mode-hook' and in fact requires
  12198. to be run from that hook to function properly."
  12199. (if org-remember-templates
  12200. (let* ((entry (org-select-remember-template use-char))
  12201. (tpl (car entry))
  12202. (plist-p (if org-store-link-plist t nil))
  12203. (file (if (and (nth 1 entry) (stringp (nth 1 entry))
  12204. (string-match "\\S-" (nth 1 entry)))
  12205. (nth 1 entry)
  12206. org-default-notes-file))
  12207. (headline (nth 2 entry))
  12208. (v-c (or (and (eq window-system 'x)
  12209. (fboundp 'x-cut-buffer-or-selection-value)
  12210. (x-cut-buffer-or-selection-value))
  12211. (org-bound-and-true-p x-last-selected-text)
  12212. (org-bound-and-true-p x-last-selected-text-primary)
  12213. (and (> (length kill-ring) 0) (current-kill 0))))
  12214. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  12215. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  12216. (v-u (concat "[" (substring v-t 1 -1) "]"))
  12217. (v-U (concat "[" (substring v-T 1 -1) "]"))
  12218. ;; `initial' and `annotation' are bound in `remember'
  12219. (v-i (if (boundp 'initial) initial))
  12220. (v-a (if (and (boundp 'annotation) annotation)
  12221. (if (equal annotation "[[]]") "" annotation)
  12222. ""))
  12223. (v-A (if (and v-a
  12224. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  12225. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  12226. v-a))
  12227. (v-n user-full-name)
  12228. (org-startup-folded nil)
  12229. org-time-was-given org-end-time-was-given x
  12230. prompt completions char time pos default histvar)
  12231. (setq org-store-link-plist
  12232. (append (list :annotation v-a :initial v-i)
  12233. org-store-link-plist))
  12234. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  12235. (erase-buffer)
  12236. (insert (substitute-command-keys
  12237. (format
  12238. "## Filing location: Select interactively, default, or last used:
  12239. ## %s to select file and header location interactively.
  12240. ## %s \"%s\" -> \"* %s\"
  12241. ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
  12242. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  12243. (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
  12244. (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
  12245. (abbreviate-file-name (or file org-default-notes-file))
  12246. (or headline "")
  12247. (or (car org-remember-previous-location) "???")
  12248. (or (cdr org-remember-previous-location) "???"))))
  12249. (insert tpl) (goto-char (point-min))
  12250. ;; Simple %-escapes
  12251. (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
  12252. (when (and initial (equal (match-string 0) "%i"))
  12253. (save-match-data
  12254. (let* ((lead (buffer-substring
  12255. (point-at-bol) (match-beginning 0))))
  12256. (setq v-i (mapconcat 'identity
  12257. (org-split-string initial "\n")
  12258. (concat "\n" lead))))))
  12259. (replace-match
  12260. (or (eval (intern (concat "v-" (match-string 1)))) "")
  12261. t t))
  12262. ;; %[] Insert contents of a file.
  12263. (goto-char (point-min))
  12264. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  12265. (let ((start (match-beginning 0))
  12266. (end (match-end 0))
  12267. (filename (expand-file-name (match-string 1))))
  12268. (goto-char start)
  12269. (delete-region start end)
  12270. (condition-case error
  12271. (insert-file-contents filename)
  12272. (error (insert (format "%%![Couldn't insert %s: %s]"
  12273. filename error))))))
  12274. ;; %() embedded elisp
  12275. (goto-char (point-min))
  12276. (while (re-search-forward "%\\((.+)\\)" nil t)
  12277. (goto-char (match-beginning 0))
  12278. (let ((template-start (point)))
  12279. (forward-char 1)
  12280. (let ((result
  12281. (condition-case error
  12282. (eval (read (current-buffer)))
  12283. (error (format "%%![Error: %s]" error)))))
  12284. (delete-region template-start (point))
  12285. (insert result))))
  12286. ;; From the property list
  12287. (when plist-p
  12288. (goto-char (point-min))
  12289. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  12290. (and (setq x (or (plist-get org-store-link-plist
  12291. (intern (match-string 1))) ""))
  12292. (replace-match x t t))))
  12293. ;; Turn on org-mode in the remember buffer, set local variables
  12294. (org-mode)
  12295. (org-set-local 'org-finish-function 'org-remember-finalize)
  12296. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  12297. (org-set-local 'org-default-notes-file file))
  12298. (if (and headline (stringp headline) (string-match "\\S-" headline))
  12299. (org-set-local 'org-remember-default-headline headline))
  12300. ;; Interactive template entries
  12301. (goto-char (point-min))
  12302. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
  12303. (setq char (if (match-end 3) (match-string 3))
  12304. prompt (if (match-end 2) (match-string 2)))
  12305. (goto-char (match-beginning 0))
  12306. (replace-match "")
  12307. (setq completions nil default nil)
  12308. (when prompt
  12309. (setq completions (org-split-string prompt "|")
  12310. prompt (pop completions)
  12311. default (car completions)
  12312. histvar (intern (concat
  12313. "org-remember-template-prompt-history::"
  12314. (or prompt "")))
  12315. completions (mapcar 'list completions)))
  12316. (cond
  12317. ((member char '("G" "g"))
  12318. (let* ((org-last-tags-completion-table
  12319. (org-global-tags-completion-table
  12320. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  12321. (org-add-colon-after-tag-completion t)
  12322. (ins (completing-read
  12323. (if prompt (concat prompt ": ") "Tags: ")
  12324. 'org-tags-completion-function nil nil nil
  12325. 'org-tags-history)))
  12326. (setq ins (mapconcat 'identity
  12327. (org-split-string ins (org-re "[^[:alnum:]]+"))
  12328. ":"))
  12329. (when (string-match "\\S-" ins)
  12330. (or (equal (char-before) ?:) (insert ":"))
  12331. (insert ins)
  12332. (or (equal (char-after) ?:) (insert ":")))))
  12333. (char
  12334. (setq org-time-was-given (equal (upcase char) char))
  12335. (setq time (org-read-date (equal (upcase char) "U") t nil
  12336. prompt))
  12337. (org-insert-time-stamp time org-time-was-given
  12338. (member char '("u" "U"))
  12339. nil nil (list org-end-time-was-given)))
  12340. (t
  12341. (insert (org-completing-read
  12342. (concat (if prompt prompt "Enter string")
  12343. (if default (concat " [" default "]"))
  12344. ": ")
  12345. completions nil nil nil histvar default)))))
  12346. (goto-char (point-min))
  12347. (if (re-search-forward "%\\?" nil t)
  12348. (replace-match "")
  12349. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  12350. (org-mode)
  12351. (org-set-local 'org-finish-function 'org-remember-finalize))
  12352. (when (save-excursion
  12353. (goto-char (point-min))
  12354. (re-search-forward "%!" nil t))
  12355. (replace-match "")
  12356. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  12357. (defun org-remember-finish-immediately ()
  12358. "File remember note immediately.
  12359. This should be run in `post-command-hook' and will remove itself
  12360. from that hook."
  12361. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  12362. (when org-finish-function
  12363. (funcall org-finish-function)))
  12364. (defun org-remember-finalize ()
  12365. "Finalize the remember process."
  12366. (unless (fboundp 'remember-finalize)
  12367. (defalias 'remember-finalize 'remember-buffer))
  12368. (when (and org-clock-marker
  12369. (equal (marker-buffer org-clock-marker) (current-buffer)))
  12370. ;; FIXME: test this, this is w/o notetaking!
  12371. (let (org-log-done) (org-clock-out)))
  12372. (when buffer-file-name
  12373. (save-buffer)
  12374. (setq buffer-file-name nil))
  12375. (remember-finalize))
  12376. ;;;###autoload
  12377. (defun org-remember (&optional goto org-force-remember-template-char)
  12378. "Call `remember'. If this is already a remember buffer, re-apply template.
  12379. If there is an active region, make sure remember uses it as initial content
  12380. of the remember buffer.
  12381. When called interactively with a `C-u' prefix argument GOTO, don't remember
  12382. anything, just go to the file/headline where the selected template usually
  12383. stores its notes. With a double prefix arg `C-u C-u', go to the last
  12384. note stored by remember.
  12385. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  12386. associated with a template in `org-remember-templates'."
  12387. (interactive "P")
  12388. (cond
  12389. ((equal goto '(4)) (org-go-to-remember-target))
  12390. ((equal goto '(16)) (org-remember-goto-last-stored))
  12391. (t
  12392. (if (memq org-finish-function '(remember-buffer remember-finalize))
  12393. (progn
  12394. (when (< (length org-remember-templates) 2)
  12395. (error "No other template available"))
  12396. (erase-buffer)
  12397. (let ((annotation (plist-get org-store-link-plist :annotation))
  12398. (initial (plist-get org-store-link-plist :initial)))
  12399. (org-remember-apply-template))
  12400. (message "Press C-c C-c to remember data"))
  12401. (if (org-region-active-p)
  12402. (remember (buffer-substring (point) (mark)))
  12403. (call-interactively 'remember))))))
  12404. (defun org-remember-goto-last-stored ()
  12405. "Go to the location where the last remember note was stored."
  12406. (interactive)
  12407. (bookmark-jump "org-remember-last-stored")
  12408. (message "This is the last note stored by remember"))
  12409. (defun org-go-to-remember-target (&optional template-key)
  12410. "Go to the target location of a remember template.
  12411. The user is queried for the template."
  12412. (interactive)
  12413. (let* ((entry (org-select-remember-template template-key))
  12414. (file (nth 1 entry))
  12415. (heading (nth 2 entry))
  12416. visiting)
  12417. (unless (and file (stringp file) (string-match "\\S-" file))
  12418. (setq file org-default-notes-file))
  12419. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  12420. (setq heading org-remember-default-headline))
  12421. (setq visiting (org-find-base-buffer-visiting file))
  12422. (if (not visiting) (find-file-noselect file))
  12423. (switch-to-buffer (or visiting (get-file-buffer file)))
  12424. (widen)
  12425. (goto-char (point-min))
  12426. (if (re-search-forward
  12427. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12428. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12429. nil t)
  12430. (goto-char (match-beginning 0))
  12431. (error "Target headline not found: %s" heading))))
  12432. (defvar org-note-abort nil) ; dynamically scoped
  12433. ;;;###autoload
  12434. (defun org-remember-handler ()
  12435. "Store stuff from remember.el into an org file.
  12436. First prompts for an org file. If the user just presses return, the value
  12437. of `org-default-notes-file' is used.
  12438. Then the command offers the headings tree of the selected file in order to
  12439. file the text at a specific location.
  12440. You can either immediately press RET to get the note appended to the
  12441. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  12442. find a better place. Then press RET or <left> or <right> in insert the note.
  12443. Key Cursor position Note gets inserted
  12444. -----------------------------------------------------------------------------
  12445. RET buffer-start as level 1 heading at end of file
  12446. RET on headline as sublevel of the heading at cursor
  12447. RET no heading at cursor position, level taken from context.
  12448. Or use prefix arg to specify level manually.
  12449. <left> on headline as same level, before current heading
  12450. <right> on headline as same level, after current heading
  12451. So the fastest way to store the note is to press RET RET to append it to
  12452. the default file. This way your current train of thought is not
  12453. interrupted, in accordance with the principles of remember.el.
  12454. You can also get the fast execution without prompting by using
  12455. C-u C-c C-c to exit the remember buffer. See also the variable
  12456. `org-remember-store-without-prompt'.
  12457. Before being stored away, the function ensures that the text has a
  12458. headline, i.e. a first line that starts with a \"*\". If not, a headline
  12459. is constructed from the current date and some additional data.
  12460. If the variable `org-adapt-indentation' is non-nil, the entire text is
  12461. also indented so that it starts in the same column as the headline
  12462. \(i.e. after the stars).
  12463. See also the variable `org-reverse-note-order'."
  12464. (goto-char (point-min))
  12465. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  12466. (replace-match ""))
  12467. (goto-char (point-max))
  12468. (beginning-of-line 1)
  12469. (while (looking-at "[ \t]*$\\|##.*")
  12470. (delete-region (1- (point)) (point-max))
  12471. (beginning-of-line 1))
  12472. (catch 'quit
  12473. (if org-note-abort (throw 'quit nil))
  12474. (let* ((txt (buffer-substring (point-min) (point-max)))
  12475. (fastp (org-xor (equal current-prefix-arg '(4))
  12476. org-remember-store-without-prompt))
  12477. (file (cond
  12478. (fastp org-default-notes-file)
  12479. ((and (eq org-remember-interactive-interface 'refile)
  12480. org-refile-targets)
  12481. org-default-notes-file)
  12482. ((not (and (equal current-prefix-arg '(16))
  12483. org-remember-previous-location))
  12484. (org-get-org-file))))
  12485. (heading org-remember-default-headline)
  12486. (visiting (and file (org-find-base-buffer-visiting file)))
  12487. (org-startup-folded nil)
  12488. (org-startup-align-all-tables nil)
  12489. (org-goto-start-pos 1)
  12490. spos exitcmd level indent reversed)
  12491. (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
  12492. (setq file (car org-remember-previous-location)
  12493. heading (cdr org-remember-previous-location)
  12494. fastp t))
  12495. (setq current-prefix-arg nil)
  12496. (if (string-match "[ \t\n]+\\'" txt)
  12497. (setq txt (replace-match "" t t txt)))
  12498. ;; Modify text so that it becomes a nice subtree which can be inserted
  12499. ;; into an org tree.
  12500. (let* ((lines (split-string txt "\n"))
  12501. first)
  12502. (setq first (car lines) lines (cdr lines))
  12503. (if (string-match "^\\*+ " first)
  12504. ;; Is already a headline
  12505. (setq indent nil)
  12506. ;; We need to add a headline: Use time and first buffer line
  12507. (setq lines (cons first lines)
  12508. first (concat "* " (current-time-string)
  12509. " (" (remember-buffer-desc) ")")
  12510. indent " "))
  12511. (if (and org-adapt-indentation indent)
  12512. (setq lines (mapcar
  12513. (lambda (x)
  12514. (if (string-match "\\S-" x)
  12515. (concat indent x) x))
  12516. lines)))
  12517. (setq txt (concat first "\n"
  12518. (mapconcat 'identity lines "\n"))))
  12519. (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
  12520. (setq txt (replace-match "\n\n" t t txt))
  12521. (if (string-match "[ \t\n]*\\'" txt)
  12522. (setq txt (replace-match "\n" t t txt))))
  12523. ;; Put the modified text back into the remember buffer, for refile.
  12524. (erase-buffer)
  12525. (insert txt)
  12526. (goto-char (point-min))
  12527. (when (and (eq org-remember-interactive-interface 'refile)
  12528. (not fastp))
  12529. (org-refile nil (or visiting (find-file-noselect file)))
  12530. (throw 'quit t))
  12531. ;; Find the file
  12532. (if (not visiting) (find-file-noselect file))
  12533. (with-current-buffer (or visiting (get-file-buffer file))
  12534. (unless (org-mode-p)
  12535. (error "Target files for remember notes must be in Org-mode"))
  12536. (save-excursion
  12537. (save-restriction
  12538. (widen)
  12539. (and (goto-char (point-min))
  12540. (not (re-search-forward "^\\* " nil t))
  12541. (insert "\n* " (or heading "Notes") "\n"))
  12542. (setq reversed (org-notes-order-reversed-p))
  12543. ;; Find the default location
  12544. (when (and heading (stringp heading) (string-match "\\S-" heading))
  12545. (goto-char (point-min))
  12546. (if (re-search-forward
  12547. (concat "^\\*+[ \t]+" (regexp-quote heading)
  12548. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  12549. nil t)
  12550. (setq org-goto-start-pos (match-beginning 0))
  12551. (when fastp
  12552. (goto-char (point-max))
  12553. (unless (bolp) (newline))
  12554. (insert "* " heading "\n")
  12555. (setq org-goto-start-pos (point-at-bol 0)))))
  12556. ;; Ask the User for a location, using the appropriate interface
  12557. (cond
  12558. (fastp (setq spos org-goto-start-pos
  12559. exitcmd 'return))
  12560. ((eq org-remember-interactive-interface 'outline)
  12561. (setq spos (org-get-location (current-buffer)
  12562. org-remember-help)
  12563. exitcmd (cdr spos)
  12564. spos (car spos)))
  12565. ((eq org-remember-interactive-interface 'outline-path-completion)
  12566. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  12567. (org-refile-use-outline-path t))
  12568. (setq spos (org-refile-get-location "Heading: ")
  12569. exitcmd 'return
  12570. spos (nth 3 spos))))
  12571. (t (error "this should not hapen")))
  12572. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  12573. ; not handle this note
  12574. (goto-char spos)
  12575. (cond ((org-on-heading-p t)
  12576. (org-back-to-heading t)
  12577. (setq level (funcall outline-level))
  12578. (cond
  12579. ((eq exitcmd 'return)
  12580. ;; sublevel of current
  12581. (setq org-remember-previous-location
  12582. (cons (abbreviate-file-name file)
  12583. (org-get-heading 'notags)))
  12584. (if reversed
  12585. (outline-next-heading)
  12586. (org-end-of-subtree t)
  12587. (if (not (bolp))
  12588. (if (looking-at "[ \t]*\n")
  12589. (beginning-of-line 2)
  12590. (end-of-line 1)
  12591. (insert "\n"))))
  12592. (bookmark-set "org-remember-last-stored")
  12593. (org-paste-subtree (org-get-legal-level level 1) txt))
  12594. ((eq exitcmd 'left)
  12595. ;; before current
  12596. (bookmark-set "org-remember-last-stored")
  12597. (org-paste-subtree level txt))
  12598. ((eq exitcmd 'right)
  12599. ;; after current
  12600. (org-end-of-subtree t)
  12601. (bookmark-set "org-remember-last-stored")
  12602. (org-paste-subtree level txt))
  12603. (t (error "This should not happen"))))
  12604. ((and (bobp) (not reversed))
  12605. ;; Put it at the end, one level below level 1
  12606. (save-restriction
  12607. (widen)
  12608. (goto-char (point-max))
  12609. (if (not (bolp)) (newline))
  12610. (bookmark-set "org-remember-last-stored")
  12611. (org-paste-subtree (org-get-legal-level 1 1) txt)))
  12612. ((and (bobp) reversed)
  12613. ;; Put it at the start, as level 1
  12614. (save-restriction
  12615. (widen)
  12616. (goto-char (point-min))
  12617. (re-search-forward "^\\*+ " nil t)
  12618. (beginning-of-line 1)
  12619. (bookmark-set "org-remember-last-stored")
  12620. (org-paste-subtree 1 txt)))
  12621. (t
  12622. ;; Put it right there, with automatic level determined by
  12623. ;; org-paste-subtree or from prefix arg
  12624. (bookmark-set "org-remember-last-stored")
  12625. (org-paste-subtree
  12626. (if (numberp current-prefix-arg) current-prefix-arg)
  12627. txt)))
  12628. (when remember-save-after-remembering
  12629. (save-buffer)
  12630. (if (not visiting) (kill-buffer (current-buffer)))))))))
  12631. t) ;; return t to indicate that we took care of this note.
  12632. (defun org-get-org-file ()
  12633. "Read a filename, with default directory `org-directory'."
  12634. (let ((default (or org-default-notes-file remember-data-file)))
  12635. (read-file-name (format "File name [%s]: " default)
  12636. (file-name-as-directory org-directory)
  12637. default)))
  12638. (defun org-notes-order-reversed-p ()
  12639. "Check if the current file should receive notes in reversed order."
  12640. (cond
  12641. ((not org-reverse-note-order) nil)
  12642. ((eq t org-reverse-note-order) t)
  12643. ((not (listp org-reverse-note-order)) nil)
  12644. (t (catch 'exit
  12645. (let ((all org-reverse-note-order)
  12646. entry)
  12647. (while (setq entry (pop all))
  12648. (if (string-match (car entry) buffer-file-name)
  12649. (throw 'exit (cdr entry))))
  12650. nil)))))
  12651. ;;; Refiling
  12652. (defvar org-refile-target-table nil
  12653. "The list of refile targets, created by `org-refile'.")
  12654. (defvar org-agenda-new-buffers nil
  12655. "Buffers created to visit agenda files.")
  12656. (defun org-get-refile-targets (&optional default-buffer)
  12657. "Produce a table with refile targets."
  12658. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  12659. targets txt re files f desc descre)
  12660. (with-current-buffer (or default-buffer (current-buffer))
  12661. (while (setq entry (pop entries))
  12662. (setq files (car entry) desc (cdr entry))
  12663. (cond
  12664. ((null files) (setq files (list (current-buffer))))
  12665. ((eq files 'org-agenda-files)
  12666. (setq files (org-agenda-files 'unrestricted)))
  12667. ((and (symbolp files) (fboundp files))
  12668. (setq files (funcall files)))
  12669. ((and (symbolp files) (boundp files))
  12670. (setq files (symbol-value files))))
  12671. (if (stringp files) (setq files (list files)))
  12672. (cond
  12673. ((eq (car desc) :tag)
  12674. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  12675. ((eq (car desc) :todo)
  12676. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  12677. ((eq (car desc) :regexp)
  12678. (setq descre (cdr desc)))
  12679. ((eq (car desc) :level)
  12680. (setq descre (concat "^\\*\\{" (number-to-string
  12681. (if org-odd-levels-only
  12682. (1- (* 2 (cdr desc)))
  12683. (cdr desc)))
  12684. "\\}[ \t]")))
  12685. ((eq (car desc) :maxlevel)
  12686. (setq descre (concat "^\\*\\{1," (number-to-string
  12687. (if org-odd-levels-only
  12688. (1- (* 2 (cdr desc)))
  12689. (cdr desc)))
  12690. "\\}[ \t]")))
  12691. (t (error "Bad refiling target description %s" desc)))
  12692. (while (setq f (pop files))
  12693. (save-excursion
  12694. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  12695. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  12696. (save-excursion
  12697. (save-restriction
  12698. (widen)
  12699. (goto-char (point-min))
  12700. (while (re-search-forward descre nil t)
  12701. (goto-char (point-at-bol))
  12702. (when (looking-at org-complex-heading-regexp)
  12703. (setq txt (match-string 4)
  12704. re (concat "^" (regexp-quote
  12705. (buffer-substring (match-beginning 1)
  12706. (match-end 4)))))
  12707. (if (match-end 5) (setq re (concat re "[ \t]+"
  12708. (regexp-quote
  12709. (match-string 5)))))
  12710. (setq re (concat re "[ \t]*$"))
  12711. (when org-refile-use-outline-path
  12712. (setq txt (mapconcat 'identity
  12713. (append
  12714. (if (eq org-refile-use-outline-path 'file)
  12715. (list (file-name-nondirectory
  12716. (buffer-file-name (buffer-base-buffer))))
  12717. (if (eq org-refile-use-outline-path 'full-file-path)
  12718. (list (buffer-file-name (buffer-base-buffer)))))
  12719. (org-get-outline-path)
  12720. (list txt))
  12721. "/")))
  12722. (push (list txt f re (point)) targets))
  12723. (goto-char (point-at-eol))))))))
  12724. (nreverse targets))))
  12725. (defun org-get-outline-path ()
  12726. "Return the outline path to the current entry, as a list."
  12727. (let (rtn)
  12728. (save-excursion
  12729. (while (org-up-heading-safe)
  12730. (when (looking-at org-complex-heading-regexp)
  12731. (push (org-match-string-no-properties 4) rtn)))
  12732. rtn)))
  12733. (defvar org-refile-history nil
  12734. "History for refiling operations.")
  12735. (defun org-refile (&optional goto default-buffer)
  12736. "Move the entry at point to another heading.
  12737. The list of target headings is compiled using the information in
  12738. `org-refile-targets', which see. This list is created upon first use, and
  12739. you can update it by calling this command with a double prefix (`C-u C-u').
  12740. FIXME: Can we find a better way of updating?
  12741. At the target location, the entry is filed as a subitem of the target heading.
  12742. Depending on `org-reverse-note-order', the new subitem will either be the
  12743. first of the last subitem.
  12744. With prefix are GOTO, the command will only visit the target location,
  12745. not actually move anything.
  12746. With a double prefix `C-c C-c', go to the location where the last refiling
  12747. operation has put the subtree.
  12748. With a double prefix argument, the command can be used to jump to any
  12749. heading in the current buffer."
  12750. (interactive "P")
  12751. (let* ((cbuf (current-buffer))
  12752. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12753. (fname (and filename (file-truename filename)))
  12754. pos it nbuf file re level reversed)
  12755. (if (equal goto '(16))
  12756. (org-refile-goto-last-stored)
  12757. (when (setq it (org-refile-get-location
  12758. (if goto "Goto: " "Refile to: ") default-buffer))
  12759. (setq file (nth 1 it)
  12760. re (nth 2 it)
  12761. pos (nth 3 it))
  12762. (setq nbuf (or (find-buffer-visiting file)
  12763. (find-file-noselect file)))
  12764. (if goto
  12765. (progn
  12766. (switch-to-buffer nbuf)
  12767. (goto-char pos)
  12768. (org-show-context 'org-goto))
  12769. (org-copy-special)
  12770. (save-excursion
  12771. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  12772. (find-file-noselect file))))
  12773. (setq reversed (org-notes-order-reversed-p))
  12774. (save-excursion
  12775. (save-restriction
  12776. (widen)
  12777. (goto-char pos)
  12778. (looking-at outline-regexp)
  12779. (setq level (org-get-legal-level (funcall outline-level) 1))
  12780. (goto-char (or (save-excursion
  12781. (if reversed
  12782. (outline-next-heading)
  12783. (outline-get-next-sibling)))
  12784. (point-max)))
  12785. (bookmark-set "org-refile-last-stored")
  12786. (org-paste-subtree level))))
  12787. (org-cut-special)
  12788. (message "Entry refiled to \"%s\"" (car it)))))))
  12789. (defun org-refile-goto-last-stored ()
  12790. "Go to the location where the last refile was stored."
  12791. (interactive)
  12792. (bookmark-jump "org-refile-last-stored")
  12793. (message "This is the location of the last refile"))
  12794. (defun org-refile-get-location (&optional prompt default-buffer)
  12795. "Prompt the user for a refile location, using PROMPT."
  12796. (let ((org-refile-targets org-refile-targets)
  12797. (org-refile-use-outline-path org-refile-use-outline-path))
  12798. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  12799. (unless org-refile-target-table
  12800. (error "No refile targets"))
  12801. (let* ((cbuf (current-buffer))
  12802. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  12803. (fname (and filename (file-truename filename)))
  12804. (tbl (mapcar
  12805. (lambda (x)
  12806. (if (not (equal fname (file-truename (nth 1 x))))
  12807. (cons (concat (car x) " (" (file-name-nondirectory
  12808. (nth 1 x)) ")")
  12809. (cdr x))
  12810. x))
  12811. org-refile-target-table))
  12812. (completion-ignore-case t)
  12813. pos it nbuf file re level reversed)
  12814. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  12815. tbl)))
  12816. ;;;; Dynamic blocks
  12817. (defun org-find-dblock (name)
  12818. "Find the first dynamic block with name NAME in the buffer.
  12819. If not found, stay at current position and return nil."
  12820. (let (pos)
  12821. (save-excursion
  12822. (goto-char (point-min))
  12823. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  12824. nil t)
  12825. (match-beginning 0))))
  12826. (if pos (goto-char pos))
  12827. pos))
  12828. (defconst org-dblock-start-re
  12829. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  12830. "Matches the startline of a dynamic block, with parameters.")
  12831. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  12832. "Matches the end of a dyhamic block.")
  12833. (defun org-create-dblock (plist)
  12834. "Create a dynamic block section, with parameters taken from PLIST.
  12835. PLIST must containe a :name entry which is used as name of the block."
  12836. (unless (bolp) (newline))
  12837. (let ((name (plist-get plist :name)))
  12838. (insert "#+BEGIN: " name)
  12839. (while plist
  12840. (if (eq (car plist) :name)
  12841. (setq plist (cddr plist))
  12842. (insert " " (prin1-to-string (pop plist)))))
  12843. (insert "\n\n#+END:\n")
  12844. (beginning-of-line -2)))
  12845. (defun org-prepare-dblock ()
  12846. "Prepare dynamic block for refresh.
  12847. This empties the block, puts the cursor at the insert position and returns
  12848. the property list including an extra property :name with the block name."
  12849. (unless (looking-at org-dblock-start-re)
  12850. (error "Not at a dynamic block"))
  12851. (let* ((begdel (1+ (match-end 0)))
  12852. (name (org-no-properties (match-string 1)))
  12853. (params (append (list :name name)
  12854. (read (concat "(" (match-string 3) ")")))))
  12855. (unless (re-search-forward org-dblock-end-re nil t)
  12856. (error "Dynamic block not terminated"))
  12857. (delete-region begdel (match-beginning 0))
  12858. (goto-char begdel)
  12859. (open-line 1)
  12860. params))
  12861. (defun org-map-dblocks (&optional command)
  12862. "Apply COMMAND to all dynamic blocks in the current buffer.
  12863. If COMMAND is not given, use `org-update-dblock'."
  12864. (let ((cmd (or command 'org-update-dblock))
  12865. pos)
  12866. (save-excursion
  12867. (goto-char (point-min))
  12868. (while (re-search-forward org-dblock-start-re nil t)
  12869. (goto-char (setq pos (match-beginning 0)))
  12870. (condition-case nil
  12871. (funcall cmd)
  12872. (error (message "Error during update of dynamic block")))
  12873. (goto-char pos)
  12874. (unless (re-search-forward org-dblock-end-re nil t)
  12875. (error "Dynamic block not terminated"))))))
  12876. (defun org-dblock-update (&optional arg)
  12877. "User command for updating dynamic blocks.
  12878. Update the dynamic block at point. With prefix ARG, update all dynamic
  12879. blocks in the buffer."
  12880. (interactive "P")
  12881. (if arg
  12882. (org-update-all-dblocks)
  12883. (or (looking-at org-dblock-start-re)
  12884. (org-beginning-of-dblock))
  12885. (org-update-dblock)))
  12886. (defun org-update-dblock ()
  12887. "Update the dynamic block at point
  12888. This means to empty the block, parse for parameters and then call
  12889. the correct writing function."
  12890. (save-window-excursion
  12891. (let* ((pos (point))
  12892. (line (org-current-line))
  12893. (params (org-prepare-dblock))
  12894. (name (plist-get params :name))
  12895. (cmd (intern (concat "org-dblock-write:" name))))
  12896. (message "Updating dynamic block `%s' at line %d..." name line)
  12897. (funcall cmd params)
  12898. (message "Updating dynamic block `%s' at line %d...done" name line)
  12899. (goto-char pos))))
  12900. (defun org-beginning-of-dblock ()
  12901. "Find the beginning of the dynamic block at point.
  12902. Error if there is no scuh block at point."
  12903. (let ((pos (point))
  12904. beg)
  12905. (end-of-line 1)
  12906. (if (and (re-search-backward org-dblock-start-re nil t)
  12907. (setq beg (match-beginning 0))
  12908. (re-search-forward org-dblock-end-re nil t)
  12909. (> (match-end 0) pos))
  12910. (goto-char beg)
  12911. (goto-char pos)
  12912. (error "Not in a dynamic block"))))
  12913. (defun org-update-all-dblocks ()
  12914. "Update all dynamic blocks in the buffer.
  12915. This function can be used in a hook."
  12916. (when (org-mode-p)
  12917. (org-map-dblocks 'org-update-dblock)))
  12918. ;;;; Completion
  12919. (defconst org-additional-option-like-keywords
  12920. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  12921. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
  12922. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  12923. (defun org-complete (&optional arg)
  12924. "Perform completion on word at point.
  12925. At the beginning of a headline, this completes TODO keywords as given in
  12926. `org-todo-keywords'.
  12927. If the current word is preceded by a backslash, completes the TeX symbols
  12928. that are supported for HTML support.
  12929. If the current word is preceded by \"#+\", completes special words for
  12930. setting file options.
  12931. In the line after \"#+STARTUP:, complete valid keywords.\"
  12932. At all other locations, this simply calls the value of
  12933. `org-completion-fallback-command'."
  12934. (interactive "P")
  12935. (org-without-partial-completion
  12936. (catch 'exit
  12937. (let* ((end (point))
  12938. (beg1 (save-excursion
  12939. (skip-chars-backward (org-re "[:alnum:]_@"))
  12940. (point)))
  12941. (beg (save-excursion
  12942. (skip-chars-backward "a-zA-Z0-9_:$")
  12943. (point)))
  12944. (confirm (lambda (x) (stringp (car x))))
  12945. (searchhead (equal (char-before beg) ?*))
  12946. (tag (and (equal (char-before beg1) ?:)
  12947. (equal (char-after (point-at-bol)) ?*)))
  12948. (prop (and (equal (char-before beg1) ?:)
  12949. (not (equal (char-after (point-at-bol)) ?*))))
  12950. (texp (equal (char-before beg) ?\\))
  12951. (link (equal (char-before beg) ?\[))
  12952. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  12953. beg)
  12954. "#+"))
  12955. (startup (string-match "^#\\+STARTUP:.*"
  12956. (buffer-substring (point-at-bol) (point))))
  12957. (completion-ignore-case opt)
  12958. (type nil)
  12959. (tbl nil)
  12960. (table (cond
  12961. (opt
  12962. (setq type :opt)
  12963. (append
  12964. (mapcar
  12965. (lambda (x)
  12966. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  12967. (cons (match-string 2 x) (match-string 1 x)))
  12968. (org-split-string (org-get-current-options) "\n"))
  12969. (mapcar 'list org-additional-option-like-keywords)))
  12970. (startup
  12971. (setq type :startup)
  12972. org-startup-options)
  12973. (link (append org-link-abbrev-alist-local
  12974. org-link-abbrev-alist))
  12975. (texp
  12976. (setq type :tex)
  12977. org-html-entities)
  12978. ((string-match "\\`\\*+[ \t]+\\'"
  12979. (buffer-substring (point-at-bol) beg))
  12980. (setq type :todo)
  12981. (mapcar 'list org-todo-keywords-1))
  12982. (searchhead
  12983. (setq type :searchhead)
  12984. (save-excursion
  12985. (goto-char (point-min))
  12986. (while (re-search-forward org-todo-line-regexp nil t)
  12987. (push (list
  12988. (org-make-org-heading-search-string
  12989. (match-string 3) t))
  12990. tbl)))
  12991. tbl)
  12992. (tag (setq type :tag beg beg1)
  12993. (or org-tag-alist (org-get-buffer-tags)))
  12994. (prop (setq type :prop beg beg1)
  12995. (mapcar 'list (org-buffer-property-keys)))
  12996. (t (progn
  12997. (call-interactively org-completion-fallback-command)
  12998. (throw 'exit nil)))))
  12999. (pattern (buffer-substring-no-properties beg end))
  13000. (completion (try-completion pattern table confirm)))
  13001. (cond ((eq completion t)
  13002. (if (not (assoc (upcase pattern) table))
  13003. (message "Already complete")
  13004. (if (equal type :opt)
  13005. (insert (substring (cdr (assoc (upcase pattern) table))
  13006. (length pattern)))
  13007. (if (memq type '(:tag :prop)) (insert ":")))))
  13008. ((null completion)
  13009. (message "Can't find completion for \"%s\"" pattern)
  13010. (ding))
  13011. ((not (string= pattern completion))
  13012. (delete-region beg end)
  13013. (if (string-match " +$" completion)
  13014. (setq completion (replace-match "" t t completion)))
  13015. (insert completion)
  13016. (if (get-buffer-window "*Completions*")
  13017. (delete-window (get-buffer-window "*Completions*")))
  13018. (if (assoc completion table)
  13019. (if (eq type :todo) (insert " ")
  13020. (if (memq type '(:tag :prop)) (insert ":"))))
  13021. (if (and (equal type :opt) (assoc completion table))
  13022. (message "%s" (substitute-command-keys
  13023. "Press \\[org-complete] again to insert example settings"))))
  13024. (t
  13025. (message "Making completion list...")
  13026. (let ((list (sort (all-completions pattern table confirm)
  13027. 'string<)))
  13028. (with-output-to-temp-buffer "*Completions*"
  13029. (condition-case nil
  13030. ;; Protection needed for XEmacs and emacs 21
  13031. (display-completion-list list pattern)
  13032. (error (display-completion-list list)))))
  13033. (message "Making completion list...%s" "done")))))))
  13034. ;;;; TODO, DEADLINE, Comments
  13035. (defun org-toggle-comment ()
  13036. "Change the COMMENT state of an entry."
  13037. (interactive)
  13038. (save-excursion
  13039. (org-back-to-heading)
  13040. (let (case-fold-search)
  13041. (if (looking-at (concat outline-regexp
  13042. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  13043. (replace-match "" t t nil 1)
  13044. (if (looking-at outline-regexp)
  13045. (progn
  13046. (goto-char (match-end 0))
  13047. (insert org-comment-string " ")))))))
  13048. (defvar org-last-todo-state-is-todo nil
  13049. "This is non-nil when the last TODO state change led to a TODO state.
  13050. If the last change removed the TODO tag or switched to DONE, then
  13051. this is nil.")
  13052. (defvar org-setting-tags nil) ; dynamically skiped
  13053. ;; FIXME: better place
  13054. (defun org-property-or-variable-value (var &optional inherit)
  13055. "Check if there is a property fixing the value of VAR.
  13056. If yes, return this value. If not, return the current value of the variable."
  13057. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13058. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13059. (read prop)
  13060. (symbol-value var))))
  13061. (defun org-parse-local-options (string var)
  13062. "Parse STRING for startup setting relevant for variable VAR."
  13063. (let ((rtn (symbol-value var))
  13064. e opts)
  13065. (save-match-data
  13066. (if (or (not string) (not (string-match "\\S-" string)))
  13067. rtn
  13068. (setq opts (delq nil (mapcar (lambda (x)
  13069. (setq e (assoc x org-startup-options))
  13070. (if (eq (nth 1 e) var) e nil))
  13071. (org-split-string string "[ \t]+"))))
  13072. (if (not opts)
  13073. rtn
  13074. (setq rtn nil)
  13075. (while (setq e (pop opts))
  13076. (if (not (nth 3 e))
  13077. (setq rtn (nth 2 e))
  13078. (if (not (listp rtn)) (setq rtn nil))
  13079. (push (nth 2 e) rtn)))
  13080. rtn)))))
  13081. (defvar org-blocker-hook nil
  13082. "Hook for functions that are allowed to block a state change.
  13083. Each function gets as its single argument a property list, see
  13084. `org-trigger-hook' for more information about this list.
  13085. If any of the functions in this hook returns nil, the state change
  13086. is blocked.")
  13087. (defvar org-trigger-hook nil
  13088. "Hook for functions that are triggered by a state change.
  13089. Each function gets as its single argument a property list with at least
  13090. the following elements:
  13091. (:type type-of-change :position pos-at-entry-start
  13092. :from old-state :to new-state)
  13093. Depending on the type, more properties may be present.
  13094. This mechanism is currently implemented for:
  13095. TODO state changes
  13096. ------------------
  13097. :type todo-state-change
  13098. :from previous state (keyword as a string), or nil
  13099. :to new state (keyword as a string), or nil")
  13100. (defun org-todo (&optional arg)
  13101. "Change the TODO state of an item.
  13102. The state of an item is given by a keyword at the start of the heading,
  13103. like
  13104. *** TODO Write paper
  13105. *** DONE Call mom
  13106. The different keywords are specified in the variable `org-todo-keywords'.
  13107. By default the available states are \"TODO\" and \"DONE\".
  13108. So for this example: when the item starts with TODO, it is changed to DONE.
  13109. When it starts with DONE, the DONE is removed. And when neither TODO nor
  13110. DONE are present, add TODO at the beginning of the heading.
  13111. With C-u prefix arg, use completion to determine the new state.
  13112. With numeric prefix arg, switch to that state.
  13113. For calling through lisp, arg is also interpreted in the following way:
  13114. 'none -> empty state
  13115. \"\"(empty string) -> switch to empty state
  13116. 'done -> switch to DONE
  13117. 'nextset -> switch to the next set of keywords
  13118. 'previousset -> switch to the previous set of keywords
  13119. \"WAITING\" -> switch to the specified keyword, but only if it
  13120. really is a member of `org-todo-keywords'."
  13121. (interactive "P")
  13122. (save-excursion
  13123. (catch 'exit
  13124. (org-back-to-heading)
  13125. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  13126. (or (looking-at (concat " +" org-todo-regexp " *"))
  13127. (looking-at " *"))
  13128. (let* ((match-data (match-data))
  13129. (startpos (point-at-bol))
  13130. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  13131. (org-log-done (org-parse-local-options logging 'org-log-done))
  13132. (org-log-repeat (org-parse-local-options logging 'org-log-repeat))
  13133. (this (match-string 1))
  13134. (hl-pos (match-beginning 0))
  13135. (head (org-get-todo-sequence-head this))
  13136. (ass (assoc head org-todo-kwd-alist))
  13137. (interpret (nth 1 ass))
  13138. (done-word (nth 3 ass))
  13139. (final-done-word (nth 4 ass))
  13140. (last-state (or this ""))
  13141. (completion-ignore-case t)
  13142. (member (member this org-todo-keywords-1))
  13143. (tail (cdr member))
  13144. (state (cond
  13145. ((and org-todo-key-trigger
  13146. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  13147. (and (not arg) org-use-fast-todo-selection
  13148. (not (eq org-use-fast-todo-selection 'prefix)))))
  13149. ;; Use fast selection
  13150. (org-fast-todo-selection))
  13151. ((and (equal arg '(4))
  13152. (or (not org-use-fast-todo-selection)
  13153. (not org-todo-key-trigger)))
  13154. ;; Read a state with completion
  13155. (completing-read "State: " (mapcar (lambda(x) (list x))
  13156. org-todo-keywords-1)
  13157. nil t))
  13158. ((eq arg 'right)
  13159. (if this
  13160. (if tail (car tail) nil)
  13161. (car org-todo-keywords-1)))
  13162. ((eq arg 'left)
  13163. (if (equal member org-todo-keywords-1)
  13164. nil
  13165. (if this
  13166. (nth (- (length org-todo-keywords-1) (length tail) 2)
  13167. org-todo-keywords-1)
  13168. (org-last org-todo-keywords-1))))
  13169. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  13170. (setq arg nil))) ; hack to fall back to cycling
  13171. (arg
  13172. ;; user or caller requests a specific state
  13173. (cond
  13174. ((equal arg "") nil)
  13175. ((eq arg 'none) nil)
  13176. ((eq arg 'done) (or done-word (car org-done-keywords)))
  13177. ((eq arg 'nextset)
  13178. (or (car (cdr (member head org-todo-heads)))
  13179. (car org-todo-heads)))
  13180. ((eq arg 'previousset)
  13181. (let ((org-todo-heads (reverse org-todo-heads)))
  13182. (or (car (cdr (member head org-todo-heads)))
  13183. (car org-todo-heads))))
  13184. ((car (member arg org-todo-keywords-1)))
  13185. ((nth (1- (prefix-numeric-value arg))
  13186. org-todo-keywords-1))))
  13187. ((null member) (or head (car org-todo-keywords-1)))
  13188. ((equal this final-done-word) nil) ;; -> make empty
  13189. ((null tail) nil) ;; -> first entry
  13190. ((eq interpret 'sequence)
  13191. (car tail))
  13192. ((memq interpret '(type priority))
  13193. (if (eq this-command last-command)
  13194. (car tail)
  13195. (if (> (length tail) 0)
  13196. (or done-word (car org-done-keywords))
  13197. nil)))
  13198. (t nil)))
  13199. (next (if state (concat " " state " ") " "))
  13200. (change-plist (list :type 'todo-state-change :from this :to state
  13201. :position startpos))
  13202. dostates)
  13203. (when org-blocker-hook
  13204. (unless (save-excursion
  13205. (save-match-data
  13206. (run-hook-with-args-until-failure
  13207. 'org-blocker-hook change-plist)))
  13208. (if (interactive-p)
  13209. (error "TODO state change from %s to %s blocked" this state)
  13210. ;; fail silently
  13211. (message "TODO state change from %s to %s blocked" this state)
  13212. (throw 'exit nil))))
  13213. (store-match-data match-data)
  13214. (replace-match next t t)
  13215. (unless (pos-visible-in-window-p hl-pos)
  13216. (message "TODO state changed to %s" (org-trim next)))
  13217. (unless head
  13218. (setq head (org-get-todo-sequence-head state)
  13219. ass (assoc head org-todo-kwd-alist)
  13220. interpret (nth 1 ass)
  13221. done-word (nth 3 ass)
  13222. final-done-word (nth 4 ass)))
  13223. (when (memq arg '(nextset previousset))
  13224. (message "Keyword-Set %d/%d: %s"
  13225. (- (length org-todo-sets) -1
  13226. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  13227. (length org-todo-sets)
  13228. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  13229. (setq org-last-todo-state-is-todo
  13230. (not (member state org-done-keywords)))
  13231. (when (and org-log-done (not (memq arg '(nextset previousset))))
  13232. (setq dostates (and (listp org-log-done) (memq 'state org-log-done)
  13233. (or (not org-todo-log-states)
  13234. (member state org-todo-log-states))))
  13235. (cond
  13236. ((and state (member state org-not-done-keywords)
  13237. (not (member this org-not-done-keywords)))
  13238. ;; This is now a todo state and was not one before
  13239. ;; Remove any CLOSED timestamp, and possibly log the state change
  13240. (org-add-planning-info nil nil 'closed)
  13241. (and dostates (org-add-log-maybe 'state state 'findpos)))
  13242. ((and state dostates)
  13243. ;; This is a non-nil state, and we need to log it
  13244. (org-add-log-maybe 'state state 'findpos))
  13245. ((and (member state org-done-keywords)
  13246. (not (member this org-done-keywords)))
  13247. ;; It is now done, and it was not done before
  13248. (org-add-planning-info 'closed (org-current-time))
  13249. (org-add-log-maybe 'done state 'findpos))))
  13250. ;; Fixup tag positioning
  13251. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  13252. (run-hooks 'org-after-todo-state-change-hook)
  13253. (and (member state org-done-keywords) (org-auto-repeat-maybe))
  13254. (if (and arg (not (member state org-done-keywords)))
  13255. (setq head (org-get-todo-sequence-head state)))
  13256. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  13257. ;; Fixup cursor location if close to the keyword
  13258. (if (and (outline-on-heading-p)
  13259. (not (bolp))
  13260. (save-excursion (beginning-of-line 1)
  13261. (looking-at org-todo-line-regexp))
  13262. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  13263. (progn
  13264. (goto-char (or (match-end 2) (match-end 1)))
  13265. (just-one-space)))
  13266. (when org-trigger-hook
  13267. (save-excursion
  13268. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  13269. (defun org-get-todo-sequence-head (kwd)
  13270. "Return the head of the TODO sequence to which KWD belongs.
  13271. If KWD is not set, check if there is a text property remembering the
  13272. right sequence."
  13273. (let (p)
  13274. (cond
  13275. ((not kwd)
  13276. (or (get-text-property (point-at-bol) 'org-todo-head)
  13277. (progn
  13278. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  13279. nil (point-at-eol)))
  13280. (get-text-property p 'org-todo-head))))
  13281. ((not (member kwd org-todo-keywords-1))
  13282. (car org-todo-keywords-1))
  13283. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  13284. (defun org-fast-todo-selection ()
  13285. "Fast TODO keyword selection with single keys.
  13286. Returns the new TODO keyword, or nil if no state change should occur."
  13287. (let* ((fulltable org-todo-key-alist)
  13288. (done-keywords org-done-keywords) ;; needed for the faces.
  13289. (maxlen (apply 'max (mapcar
  13290. (lambda (x)
  13291. (if (stringp (car x)) (string-width (car x)) 0))
  13292. fulltable)))
  13293. (expert nil)
  13294. (fwidth (+ maxlen 3 1 3))
  13295. (ncol (/ (- (window-width) 4) fwidth))
  13296. tg cnt e c tbl
  13297. groups ingroup)
  13298. (save-window-excursion
  13299. (if expert
  13300. (set-buffer (get-buffer-create " *Org todo*"))
  13301. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  13302. (erase-buffer)
  13303. (org-set-local 'org-done-keywords done-keywords)
  13304. (setq tbl fulltable cnt 0)
  13305. (while (setq e (pop tbl))
  13306. (cond
  13307. ((equal e '(:startgroup))
  13308. (push '() groups) (setq ingroup t)
  13309. (when (not (= cnt 0))
  13310. (setq cnt 0)
  13311. (insert "\n"))
  13312. (insert "{ "))
  13313. ((equal e '(:endgroup))
  13314. (setq ingroup nil cnt 0)
  13315. (insert "}\n"))
  13316. (t
  13317. (setq tg (car e) c (cdr e))
  13318. (if ingroup (push tg (car groups)))
  13319. (setq tg (org-add-props tg nil 'face
  13320. (org-get-todo-face tg)))
  13321. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13322. (insert "[" c "] " tg (make-string
  13323. (- fwidth 4 (length tg)) ?\ ))
  13324. (when (= (setq cnt (1+ cnt)) ncol)
  13325. (insert "\n")
  13326. (if ingroup (insert " "))
  13327. (setq cnt 0)))))
  13328. (insert "\n")
  13329. (goto-char (point-min))
  13330. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  13331. (fit-window-to-buffer))
  13332. (message "[a-z..]:Set [SPC]:clear")
  13333. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13334. (cond
  13335. ((or (= c ?\C-g)
  13336. (and (= c ?q) (not (rassoc c fulltable))))
  13337. (setq quit-flag t))
  13338. ((= c ?\ ) nil)
  13339. ((setq e (rassoc c fulltable) tg (car e))
  13340. tg)
  13341. (t (setq quit-flag t))))))
  13342. (defun org-get-repeat ()
  13343. "Check if tere is a deadline/schedule with repeater in this entry."
  13344. (save-match-data
  13345. (save-excursion
  13346. (org-back-to-heading t)
  13347. (if (re-search-forward
  13348. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  13349. (match-string 1)))))
  13350. (defvar org-last-changed-timestamp)
  13351. (defvar org-log-post-message)
  13352. (defun org-auto-repeat-maybe ()
  13353. "Check if the current headline contains a repeated deadline/schedule.
  13354. If yes, set TODO state back to what it was and change the base date
  13355. of repeating deadline/scheduled time stamps to new date.
  13356. This function should be run in the `org-after-todo-state-change-hook'."
  13357. ;; last-state is dynamically scoped into this function
  13358. (let* ((repeat (org-get-repeat))
  13359. (aa (assoc last-state org-todo-kwd-alist))
  13360. (interpret (nth 1 aa))
  13361. (head (nth 2 aa))
  13362. (done-word (nth 3 aa))
  13363. (whata '(("d" . day) ("m" . month) ("y" . year)))
  13364. (msg "Entry repeats: ")
  13365. (org-log-done)
  13366. re type n what ts)
  13367. (when repeat
  13368. (org-todo (if (eq interpret 'type) last-state head))
  13369. (when (and org-log-repeat
  13370. (not (memq 'org-add-log-note
  13371. (default-value 'post-command-hook))))
  13372. ;; Make sure a note is taken
  13373. (let ((org-log-done '(done)))
  13374. (org-add-log-maybe 'done (or done-word (car org-done-keywords))
  13375. 'findpos)))
  13376. (org-back-to-heading t)
  13377. (org-add-planning-info nil nil 'closed)
  13378. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  13379. org-deadline-time-regexp "\\)\\|\\("
  13380. org-ts-regexp "\\)"))
  13381. (while (re-search-forward
  13382. re (save-excursion (outline-next-heading) (point)) t)
  13383. (setq type (if (match-end 1) org-scheduled-string
  13384. (if (match-end 3) org-deadline-string "Plain:"))
  13385. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  13386. (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
  13387. (setq n (string-to-number (match-string 1 ts))
  13388. what (match-string 2 ts))
  13389. (if (equal what "w") (setq n (* n 7) what "d"))
  13390. (org-timestamp-change n (cdr (assoc what whata)))
  13391. (setq msg (concat msg type org-last-changed-timestamp " "))))
  13392. (setq org-log-post-message msg)
  13393. (message "%s" msg))))
  13394. (defun org-show-todo-tree (arg)
  13395. "Make a compact tree which shows all headlines marked with TODO.
  13396. The tree will show the lines where the regexp matches, and all higher
  13397. headlines above the match.
  13398. With \\[universal-argument] prefix, also show the DONE entries.
  13399. With a numeric prefix N, construct a sparse tree for the Nth element
  13400. of `org-todo-keywords-1'."
  13401. (interactive "P")
  13402. (let ((case-fold-search nil)
  13403. (kwd-re
  13404. (cond ((null arg) org-not-done-regexp)
  13405. ((equal arg '(4))
  13406. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  13407. (mapcar 'list org-todo-keywords-1))))
  13408. (concat "\\("
  13409. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  13410. "\\)\\>")))
  13411. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  13412. (regexp-quote (nth (1- (prefix-numeric-value arg))
  13413. org-todo-keywords-1)))
  13414. (t (error "Invalid prefix argument: %s" arg)))))
  13415. (message "%d TODO entries found"
  13416. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  13417. (defun org-deadline (&optional remove)
  13418. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  13419. With argument REMOVE, remove any deadline from the item."
  13420. (interactive "P")
  13421. (if remove
  13422. (progn
  13423. (org-add-planning-info nil nil 'deadline)
  13424. (message "Item no longer has a deadline."))
  13425. (org-add-planning-info 'deadline nil 'closed)))
  13426. (defun org-schedule (&optional remove)
  13427. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  13428. With argument REMOVE, remove any scheduling date from the item."
  13429. (interactive "P")
  13430. (if remove
  13431. (progn
  13432. (org-add-planning-info nil nil 'scheduled)
  13433. (message "Item is no longer scheduled."))
  13434. (org-add-planning-info 'scheduled nil 'closed)))
  13435. (defun org-add-planning-info (what &optional time &rest remove)
  13436. "Insert new timestamp with keyword in the line directly after the headline.
  13437. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  13438. If non is given, the user is prompted for a date.
  13439. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  13440. be removed."
  13441. (interactive)
  13442. (let (org-time-was-given org-end-time-was-given)
  13443. (when what (setq time (or time (org-read-date nil 'to-time))))
  13444. (when (and org-insert-labeled-timestamps-at-point
  13445. (member what '(scheduled deadline)))
  13446. (insert
  13447. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  13448. (org-insert-time-stamp time org-time-was-given
  13449. nil nil nil (list org-end-time-was-given))
  13450. (setq what nil))
  13451. (save-excursion
  13452. (save-restriction
  13453. (let (col list elt ts buffer-invisibility-spec)
  13454. (org-back-to-heading t)
  13455. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  13456. (goto-char (match-end 1))
  13457. (setq col (current-column))
  13458. (goto-char (match-end 0))
  13459. (if (eobp) (insert "\n") (forward-char 1))
  13460. (if (and (not (looking-at outline-regexp))
  13461. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  13462. "[^\r\n]*"))
  13463. (not (equal (match-string 1) org-clock-string)))
  13464. (narrow-to-region (match-beginning 0) (match-end 0))
  13465. (insert-before-markers "\n")
  13466. (backward-char 1)
  13467. (narrow-to-region (point) (point))
  13468. (indent-to-column col))
  13469. ;; Check if we have to remove something.
  13470. (setq list (cons what remove))
  13471. (while list
  13472. (setq elt (pop list))
  13473. (goto-char (point-min))
  13474. (when (or (and (eq elt 'scheduled)
  13475. (re-search-forward org-scheduled-time-regexp nil t))
  13476. (and (eq elt 'deadline)
  13477. (re-search-forward org-deadline-time-regexp nil t))
  13478. (and (eq elt 'closed)
  13479. (re-search-forward org-closed-time-regexp nil t)))
  13480. (replace-match "")
  13481. (if (looking-at "--+<[^>]+>") (replace-match ""))
  13482. (if (looking-at " +") (replace-match ""))))
  13483. (goto-char (point-max))
  13484. (when what
  13485. (insert
  13486. (if (not (equal (char-before) ?\ )) " " "")
  13487. (cond ((eq what 'scheduled) org-scheduled-string)
  13488. ((eq what 'deadline) org-deadline-string)
  13489. ((eq what 'closed) org-closed-string))
  13490. " ")
  13491. (setq ts (org-insert-time-stamp
  13492. time
  13493. (or org-time-was-given
  13494. (and (eq what 'closed) org-log-done-with-time))
  13495. (eq what 'closed)
  13496. nil nil (list org-end-time-was-given)))
  13497. (end-of-line 1))
  13498. (goto-char (point-min))
  13499. (widen)
  13500. (if (looking-at "[ \t]+\r?\n")
  13501. (replace-match ""))
  13502. ts)))))
  13503. (defvar org-log-note-marker (make-marker))
  13504. (defvar org-log-note-purpose nil)
  13505. (defvar org-log-note-state nil)
  13506. (defvar org-log-note-window-configuration nil)
  13507. (defvar org-log-note-return-to (make-marker))
  13508. (defvar org-log-post-message nil
  13509. "Message to be displayed after a log note has been stored.
  13510. The auto-repeater uses this.")
  13511. (defun org-add-log-maybe (&optional purpose state findpos)
  13512. "Set up the post command hook to take a note."
  13513. (save-excursion
  13514. (when (and (listp org-log-done)
  13515. (memq purpose org-log-done))
  13516. (when findpos
  13517. (org-back-to-heading t)
  13518. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  13519. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  13520. "[^\r\n]*\\)?"))
  13521. (goto-char (match-end 0))
  13522. (unless org-log-states-order-reversed
  13523. (and (= (char-after) ?\n) (forward-char 1))
  13524. (org-skip-over-state-notes)
  13525. (skip-chars-backward " \t\n\r")))
  13526. (move-marker org-log-note-marker (point))
  13527. (setq org-log-note-purpose purpose)
  13528. (setq org-log-note-state state)
  13529. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  13530. (defun org-skip-over-state-notes ()
  13531. "Skip past the list of State notes in an entry."
  13532. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  13533. (while (looking-at "[ \t]*- State")
  13534. (condition-case nil
  13535. (org-next-item)
  13536. (error (org-end-of-item)))))
  13537. (defun org-add-log-note (&optional purpose)
  13538. "Pop up a window for taking a note, and add this note later at point."
  13539. (remove-hook 'post-command-hook 'org-add-log-note)
  13540. (setq org-log-note-window-configuration (current-window-configuration))
  13541. (delete-other-windows)
  13542. (move-marker org-log-note-return-to (point))
  13543. (switch-to-buffer (marker-buffer org-log-note-marker))
  13544. (goto-char org-log-note-marker)
  13545. (org-switch-to-buffer-other-window "*Org Note*")
  13546. (erase-buffer)
  13547. (let ((org-inhibit-startup t)) (org-mode))
  13548. (insert (format "# Insert note for %s.
  13549. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  13550. (cond
  13551. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  13552. ((eq org-log-note-purpose 'done) "closed todo item")
  13553. ((eq org-log-note-purpose 'state)
  13554. (format "state change to \"%s\"" org-log-note-state))
  13555. (t (error "This should not happen")))))
  13556. (org-set-local 'org-finish-function 'org-store-log-note))
  13557. (defun org-store-log-note ()
  13558. "Finish taking a log note, and insert it to where it belongs."
  13559. (let ((txt (buffer-string))
  13560. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  13561. lines ind)
  13562. (kill-buffer (current-buffer))
  13563. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  13564. (setq txt (replace-match "" t t txt)))
  13565. (if (string-match "\\s-+\\'" txt)
  13566. (setq txt (replace-match "" t t txt)))
  13567. (setq lines (org-split-string txt "\n"))
  13568. (when (and note (string-match "\\S-" note))
  13569. (setq note
  13570. (org-replace-escapes
  13571. note
  13572. (list (cons "%u" (user-login-name))
  13573. (cons "%U" user-full-name)
  13574. (cons "%t" (format-time-string
  13575. (org-time-stamp-format 'long 'inactive)
  13576. (current-time)))
  13577. (cons "%s" (if org-log-note-state
  13578. (concat "\"" org-log-note-state "\"")
  13579. "")))))
  13580. (if lines (setq note (concat note " \\\\")))
  13581. (push note lines))
  13582. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  13583. (when lines
  13584. (save-excursion
  13585. (set-buffer (marker-buffer org-log-note-marker))
  13586. (save-excursion
  13587. (goto-char org-log-note-marker)
  13588. (move-marker org-log-note-marker nil)
  13589. (end-of-line 1)
  13590. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  13591. (indent-relative nil)
  13592. (insert "- " (pop lines))
  13593. (org-indent-line-function)
  13594. (beginning-of-line 1)
  13595. (looking-at "[ \t]*")
  13596. (setq ind (concat (match-string 0) " "))
  13597. (end-of-line 1)
  13598. (while lines (insert "\n" ind (pop lines)))))))
  13599. (set-window-configuration org-log-note-window-configuration)
  13600. (with-current-buffer (marker-buffer org-log-note-return-to)
  13601. (goto-char org-log-note-return-to))
  13602. (move-marker org-log-note-return-to nil)
  13603. (and org-log-post-message (message "%s" org-log-post-message)))
  13604. ;; FIXME: what else would be useful?
  13605. ;; - priority
  13606. ;; - date
  13607. (defun org-sparse-tree (&optional arg)
  13608. "Create a sparse tree, prompt for the details.
  13609. This command can create sparse trees. You first need to select the type
  13610. of match used to create the tree:
  13611. t Show entries with a specific TODO keyword.
  13612. T Show entries selected by a tags match.
  13613. p Enter a property name and its value (both with completion on existing
  13614. names/values) and show entries with that property.
  13615. r Show entries matching a regular expression
  13616. d Show deadlines due within `org-deadline-warning-days'."
  13617. (interactive "P")
  13618. (let (ans kwd value)
  13619. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  13620. (setq ans (read-char-exclusive))
  13621. (cond
  13622. ((equal ans ?d)
  13623. (call-interactively 'org-check-deadlines))
  13624. ((equal ans ?b)
  13625. (call-interactively 'org-check-before-date))
  13626. ((equal ans ?t)
  13627. (org-show-todo-tree '(4)))
  13628. ((equal ans ?T)
  13629. (call-interactively 'org-tags-sparse-tree))
  13630. ((member ans '(?p ?P))
  13631. (setq kwd (completing-read "Property: "
  13632. (mapcar 'list (org-buffer-property-keys))))
  13633. (setq value (completing-read "Value: "
  13634. (mapcar 'list (org-property-values kwd))))
  13635. (unless (string-match "\\`{.*}\\'" value)
  13636. (setq value (concat "\"" value "\"")))
  13637. (org-tags-sparse-tree arg (concat kwd "=" value)))
  13638. ((member ans '(?r ?R ?/))
  13639. (call-interactively 'org-occur))
  13640. (t (error "No such sparse tree command \"%c\"" ans)))))
  13641. (defvar org-occur-highlights nil)
  13642. (make-variable-buffer-local 'org-occur-highlights)
  13643. (defun org-occur (regexp &optional keep-previous callback)
  13644. "Make a compact tree which shows all matches of REGEXP.
  13645. The tree will show the lines where the regexp matches, and all higher
  13646. headlines above the match. It will also show the heading after the match,
  13647. to make sure editing the matching entry is easy.
  13648. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  13649. call to `org-occur' will be kept, to allow stacking of calls to this
  13650. command.
  13651. If CALLBACK is non-nil, it is a function which is called to confirm
  13652. that the match should indeed be shown."
  13653. (interactive "sRegexp: \nP")
  13654. (or keep-previous (org-remove-occur-highlights nil nil t))
  13655. (let ((cnt 0))
  13656. (save-excursion
  13657. (goto-char (point-min))
  13658. (if (or (not keep-previous) ; do not want to keep
  13659. (not org-occur-highlights)) ; no previous matches
  13660. ;; hide everything
  13661. (org-overview))
  13662. (while (re-search-forward regexp nil t)
  13663. (when (or (not callback)
  13664. (save-match-data (funcall callback)))
  13665. (setq cnt (1+ cnt))
  13666. (when org-highlight-sparse-tree-matches
  13667. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  13668. (org-show-context 'occur-tree))))
  13669. (when org-remove-highlights-with-change
  13670. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  13671. nil 'local))
  13672. (unless org-sparse-tree-open-archived-trees
  13673. (org-hide-archived-subtrees (point-min) (point-max)))
  13674. (run-hooks 'org-occur-hook)
  13675. (if (interactive-p)
  13676. (message "%d match(es) for regexp %s" cnt regexp))
  13677. cnt))
  13678. (defun org-show-context (&optional key)
  13679. "Make sure point and context and visible.
  13680. How much context is shown depends upon the variables
  13681. `org-show-hierarchy-above', `org-show-following-heading'. and
  13682. `org-show-siblings'."
  13683. (let ((heading-p (org-on-heading-p t))
  13684. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  13685. (following-p (org-get-alist-option org-show-following-heading key))
  13686. (entry-p (org-get-alist-option org-show-entry-below key))
  13687. (siblings-p (org-get-alist-option org-show-siblings key)))
  13688. (catch 'exit
  13689. ;; Show heading or entry text
  13690. (if (and heading-p (not entry-p))
  13691. (org-flag-heading nil) ; only show the heading
  13692. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  13693. (org-show-hidden-entry))) ; show entire entry
  13694. (when following-p
  13695. ;; Show next sibling, or heading below text
  13696. (save-excursion
  13697. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  13698. (org-flag-heading nil))))
  13699. (when siblings-p (org-show-siblings))
  13700. (when hierarchy-p
  13701. ;; show all higher headings, possibly with siblings
  13702. (save-excursion
  13703. (while (and (condition-case nil
  13704. (progn (org-up-heading-all 1) t)
  13705. (error nil))
  13706. (not (bobp)))
  13707. (org-flag-heading nil)
  13708. (when siblings-p (org-show-siblings))))))))
  13709. (defun org-reveal (&optional siblings)
  13710. "Show current entry, hierarchy above it, and the following headline.
  13711. This can be used to show a consistent set of context around locations
  13712. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  13713. not t for the search context.
  13714. With optional argument SIBLINGS, on each level of the hierarchy all
  13715. siblings are shown. This repairs the tree structure to what it would
  13716. look like when opened with hierarchical calls to `org-cycle'."
  13717. (interactive "P")
  13718. (let ((org-show-hierarchy-above t)
  13719. (org-show-following-heading t)
  13720. (org-show-siblings (if siblings t org-show-siblings)))
  13721. (org-show-context nil)))
  13722. (defun org-highlight-new-match (beg end)
  13723. "Highlight from BEG to END and mark the highlight is an occur headline."
  13724. (let ((ov (org-make-overlay beg end)))
  13725. (org-overlay-put ov 'face 'secondary-selection)
  13726. (push ov org-occur-highlights)))
  13727. (defun org-remove-occur-highlights (&optional beg end noremove)
  13728. "Remove the occur highlights from the buffer.
  13729. BEG and END are ignored. If NOREMOVE is nil, remove this function
  13730. from the `before-change-functions' in the current buffer."
  13731. (interactive)
  13732. (unless org-inhibit-highlight-removal
  13733. (mapc 'org-delete-overlay org-occur-highlights)
  13734. (setq org-occur-highlights nil)
  13735. (unless noremove
  13736. (remove-hook 'before-change-functions
  13737. 'org-remove-occur-highlights 'local))))
  13738. ;;;; Priorities
  13739. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  13740. "Regular expression matching the priority indicator.")
  13741. (defvar org-remove-priority-next-time nil)
  13742. (defun org-priority-up ()
  13743. "Increase the priority of the current item."
  13744. (interactive)
  13745. (org-priority 'up))
  13746. (defun org-priority-down ()
  13747. "Decrease the priority of the current item."
  13748. (interactive)
  13749. (org-priority 'down))
  13750. (defun org-priority (&optional action)
  13751. "Change the priority of an item by ARG.
  13752. ACTION can be `set', `up', `down', or a character."
  13753. (interactive)
  13754. (setq action (or action 'set))
  13755. (let (current new news have remove)
  13756. (save-excursion
  13757. (org-back-to-heading)
  13758. (if (looking-at org-priority-regexp)
  13759. (setq current (string-to-char (match-string 2))
  13760. have t)
  13761. (setq current org-default-priority))
  13762. (cond
  13763. ((or (eq action 'set) (integerp action))
  13764. (if (integerp action)
  13765. (setq new action)
  13766. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  13767. (setq new (read-char-exclusive)))
  13768. (if (and (= (upcase org-highest-priority) org-highest-priority)
  13769. (= (upcase org-lowest-priority) org-lowest-priority))
  13770. (setq new (upcase new)))
  13771. (cond ((equal new ?\ ) (setq remove t))
  13772. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  13773. (error "Priority must be between `%c' and `%c'"
  13774. org-highest-priority org-lowest-priority))))
  13775. ((eq action 'up)
  13776. (if (and (not have) (eq last-command this-command))
  13777. (setq new org-lowest-priority)
  13778. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13779. org-default-priority (1- current)))))
  13780. ((eq action 'down)
  13781. (if (and (not have) (eq last-command this-command))
  13782. (setq new org-highest-priority)
  13783. (setq new (if (and org-priority-start-cycle-with-default (not have))
  13784. org-default-priority (1+ current)))))
  13785. (t (error "Invalid action")))
  13786. (if (or (< (upcase new) org-highest-priority)
  13787. (> (upcase new) org-lowest-priority))
  13788. (setq remove t))
  13789. (setq news (format "%c" new))
  13790. (if have
  13791. (if remove
  13792. (replace-match "" t t nil 1)
  13793. (replace-match news t t nil 2))
  13794. (if remove
  13795. (error "No priority cookie found in line")
  13796. (looking-at org-todo-line-regexp)
  13797. (if (match-end 2)
  13798. (progn
  13799. (goto-char (match-end 2))
  13800. (insert " [#" news "]"))
  13801. (goto-char (match-beginning 3))
  13802. (insert "[#" news "] ")))))
  13803. (org-preserve-lc (org-set-tags nil 'align))
  13804. (if remove
  13805. (message "Priority removed")
  13806. (message "Priority of current item set to %s" news))))
  13807. (defun org-get-priority (s)
  13808. "Find priority cookie and return priority."
  13809. (save-match-data
  13810. (if (not (string-match org-priority-regexp s))
  13811. (* 1000 (- org-lowest-priority org-default-priority))
  13812. (* 1000 (- org-lowest-priority
  13813. (string-to-char (match-string 2 s)))))))
  13814. ;;;; Tags
  13815. (defun org-scan-tags (action matcher &optional todo-only)
  13816. "Scan headline tags with inheritance and produce output ACTION.
  13817. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  13818. evaluated, testing if a given set of tags qualifies a headline for
  13819. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  13820. are included in the output."
  13821. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  13822. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  13823. (org-re
  13824. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  13825. (props (list 'face nil
  13826. 'done-face 'org-done
  13827. 'undone-face nil
  13828. 'mouse-face 'highlight
  13829. 'org-not-done-regexp org-not-done-regexp
  13830. 'org-todo-regexp org-todo-regexp
  13831. 'keymap org-agenda-keymap
  13832. 'help-echo
  13833. (format "mouse-2 or RET jump to org file %s"
  13834. (abbreviate-file-name
  13835. (or (buffer-file-name (buffer-base-buffer))
  13836. (buffer-name (buffer-base-buffer)))))))
  13837. (case-fold-search nil)
  13838. lspos
  13839. tags tags-list tags-alist (llast 0) rtn level category i txt
  13840. todo marker entry priority)
  13841. (save-excursion
  13842. (goto-char (point-min))
  13843. (when (eq action 'sparse-tree)
  13844. (org-overview)
  13845. (org-remove-occur-highlights))
  13846. (while (re-search-forward re nil t)
  13847. (catch :skip
  13848. (setq todo (if (match-end 1) (match-string 2))
  13849. tags (if (match-end 4) (match-string 4)))
  13850. (goto-char (setq lspos (1+ (match-beginning 0))))
  13851. (setq level (org-reduced-level (funcall outline-level))
  13852. category (org-get-category))
  13853. (setq i llast llast level)
  13854. ;; remove tag lists from same and sublevels
  13855. (while (>= i level)
  13856. (when (setq entry (assoc i tags-alist))
  13857. (setq tags-alist (delete entry tags-alist)))
  13858. (setq i (1- i)))
  13859. ;; add the nex tags
  13860. (when tags
  13861. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  13862. tags-alist
  13863. (cons (cons level tags) tags-alist)))
  13864. ;; compile tags for current headline
  13865. (setq tags-list
  13866. (if org-use-tag-inheritance
  13867. (apply 'append (mapcar 'cdr tags-alist))
  13868. tags))
  13869. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  13870. (eval matcher)
  13871. (or (not org-agenda-skip-archived-trees)
  13872. (not (member org-archive-tag tags-list))))
  13873. (and (eq action 'agenda) (org-agenda-skip))
  13874. ;; list this headline
  13875. (if (eq action 'sparse-tree)
  13876. (progn
  13877. (and org-highlight-sparse-tree-matches
  13878. (org-get-heading) (match-end 0)
  13879. (org-highlight-new-match
  13880. (match-beginning 0) (match-beginning 1)))
  13881. (org-show-context 'tags-tree))
  13882. (setq txt (org-format-agenda-item
  13883. ""
  13884. (concat
  13885. (if org-tags-match-list-sublevels
  13886. (make-string (1- level) ?.) "")
  13887. (org-get-heading))
  13888. category tags-list)
  13889. priority (org-get-priority txt))
  13890. (goto-char lspos)
  13891. (setq marker (org-agenda-new-marker))
  13892. (org-add-props txt props
  13893. 'org-marker marker 'org-hd-marker marker 'org-category category
  13894. 'priority priority 'type "tagsmatch")
  13895. (push txt rtn))
  13896. ;; if we are to skip sublevels, jump to end of subtree
  13897. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  13898. (when (and (eq action 'sparse-tree)
  13899. (not org-sparse-tree-open-archived-trees))
  13900. (org-hide-archived-subtrees (point-min) (point-max)))
  13901. (nreverse rtn)))
  13902. (defvar todo-only) ;; dynamically scoped
  13903. (defun org-tags-sparse-tree (&optional todo-only match)
  13904. "Create a sparse tree according to tags string MATCH.
  13905. MATCH can contain positive and negative selection of tags, like
  13906. \"+WORK+URGENT-WITHBOSS\".
  13907. If optional argument TODO_ONLY is non-nil, only select lines that are
  13908. also TODO lines."
  13909. (interactive "P")
  13910. (org-prepare-agenda-buffers (list (current-buffer)))
  13911. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13912. (defvar org-cached-props nil)
  13913. (defun org-cached-entry-get (pom property)
  13914. (if (or (eq t org-use-property-inheritance)
  13915. (member property org-use-property-inheritance))
  13916. ;; Caching is not possible, check it directly
  13917. (org-entry-get pom property 'inherit)
  13918. ;; Get all properties, so that we can do complicated checks easily
  13919. (cdr (assoc property (or org-cached-props
  13920. (setq org-cached-props
  13921. (org-entry-properties pom)))))))
  13922. (defun org-global-tags-completion-table (&optional files)
  13923. "Return the list of all tags in all agenda buffer/files."
  13924. (save-excursion
  13925. (org-uniquify
  13926. (delq nil
  13927. (apply 'append
  13928. (mapcar
  13929. (lambda (file)
  13930. (set-buffer (find-file-noselect file))
  13931. (append (org-get-buffer-tags)
  13932. (mapcar (lambda (x) (if (stringp (car-safe x))
  13933. (list (car-safe x)) nil))
  13934. org-tag-alist)))
  13935. (if (and files (car files))
  13936. files
  13937. (org-agenda-files))))))))
  13938. (defun org-make-tags-matcher (match)
  13939. "Create the TAGS//TODO matcher form for the selection string MATCH."
  13940. ;; todo-only is scoped dynamically into this function, and the function
  13941. ;; may change it it the matcher asksk for it.
  13942. (unless match
  13943. ;; Get a new match request, with completion
  13944. (let ((org-last-tags-completion-table
  13945. (org-global-tags-completion-table)))
  13946. (setq match (completing-read
  13947. "Match: " 'org-tags-completion-function nil nil nil
  13948. 'org-tags-history))))
  13949. ;; Parse the string and create a lisp form
  13950. (let ((match0 match)
  13951. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]+\"\\)\\|[[:alnum:]_@]+\\)"))
  13952. minus tag mm
  13953. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13954. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  13955. (if (string-match "/+" match)
  13956. ;; match contains also a todo-matching request
  13957. (progn
  13958. (setq tagsmatch (substring match 0 (match-beginning 0))
  13959. todomatch (substring match (match-end 0)))
  13960. (if (string-match "^!" todomatch)
  13961. (setq todo-only t todomatch (substring todomatch 1)))
  13962. (if (string-match "^\\s-*$" todomatch)
  13963. (setq todomatch nil)))
  13964. ;; only matching tags
  13965. (setq tagsmatch match todomatch nil))
  13966. ;; Make the tags matcher
  13967. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13968. (setq tagsmatcher t)
  13969. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13970. (while (setq term (pop orterms))
  13971. (while (and (equal (substring term -1) "\\") orterms)
  13972. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13973. (while (string-match re term)
  13974. (setq minus (and (match-end 1)
  13975. (equal (match-string 1 term) "-"))
  13976. tag (match-string 2 term)
  13977. re-p (equal (string-to-char tag) ?{)
  13978. level-p (match-end 3)
  13979. prop-p (match-end 4)
  13980. mm (cond
  13981. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13982. (level-p `(= level ,(string-to-number
  13983. (match-string 3 term))))
  13984. (prop-p
  13985. (setq pn (match-string 4 term)
  13986. pv (match-string 5 term)
  13987. cat-p (equal pn "CATEGORY")
  13988. re-p (equal (string-to-char pv) ?{)
  13989. pv (substring pv 1 -1))
  13990. (if (equal pn "CATEGORY")
  13991. (setq gv '(get-text-property (point) 'org-category))
  13992. (setq gv `(org-cached-entry-get nil ,pn)))
  13993. (if re-p
  13994. `(string-match ,pv (or ,gv ""))
  13995. `(equal ,pv ,gv)))
  13996. (t `(member ,(downcase tag) tags-list)))
  13997. mm (if minus (list 'not mm) mm)
  13998. term (substring term (match-end 0)))
  13999. (push mm tagsmatcher))
  14000. (push (if (> (length tagsmatcher) 1)
  14001. (cons 'and tagsmatcher)
  14002. (car tagsmatcher))
  14003. orlist)
  14004. (setq tagsmatcher nil))
  14005. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  14006. (setq tagsmatcher
  14007. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  14008. ;; Make the todo matcher
  14009. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  14010. (setq todomatcher t)
  14011. (setq orterms (org-split-string todomatch "|") orlist nil)
  14012. (while (setq term (pop orterms))
  14013. (while (string-match re term)
  14014. (setq minus (and (match-end 1)
  14015. (equal (match-string 1 term) "-"))
  14016. kwd (match-string 2 term)
  14017. re-p (equal (string-to-char kwd) ?{)
  14018. term (substring term (match-end 0))
  14019. mm (if re-p
  14020. `(string-match ,(substring kwd 1 -1) todo)
  14021. (list 'equal 'todo kwd))
  14022. mm (if minus (list 'not mm) mm))
  14023. (push mm todomatcher))
  14024. (push (if (> (length todomatcher) 1)
  14025. (cons 'and todomatcher)
  14026. (car todomatcher))
  14027. orlist)
  14028. (setq todomatcher nil))
  14029. (setq todomatcher (if (> (length orlist) 1)
  14030. (cons 'or orlist) (car orlist))))
  14031. ;; Return the string and lisp forms of the matcher
  14032. (setq matcher (if todomatcher
  14033. (list 'and tagsmatcher todomatcher)
  14034. tagsmatcher))
  14035. (cons match0 matcher)))
  14036. (defun org-match-any-p (re list)
  14037. "Does re match any element of list?"
  14038. (setq list (mapcar (lambda (x) (string-match re x)) list))
  14039. (delq nil list))
  14040. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  14041. (defvar org-tags-overlay (org-make-overlay 1 1))
  14042. (org-detach-overlay org-tags-overlay)
  14043. (defun org-align-tags-here (to-col)
  14044. ;; Assumes that this is a headline
  14045. (let ((pos (point)) (col (current-column)) tags)
  14046. (beginning-of-line 1)
  14047. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14048. (< pos (match-beginning 2)))
  14049. (progn
  14050. (setq tags (match-string 2))
  14051. (goto-char (match-beginning 1))
  14052. (insert " ")
  14053. (delete-region (point) (1+ (match-end 0)))
  14054. (backward-char 1)
  14055. (move-to-column
  14056. (max (1+ (current-column))
  14057. (1+ col)
  14058. (if (> to-col 0)
  14059. to-col
  14060. (- (abs to-col) (length tags))))
  14061. t)
  14062. (insert tags)
  14063. (move-to-column (min (current-column) col) t))
  14064. (goto-char pos))))
  14065. (defun org-set-tags (&optional arg just-align)
  14066. "Set the tags for the current headline.
  14067. With prefix ARG, realign all tags in headings in the current buffer."
  14068. (interactive "P")
  14069. (let* ((re (concat "^" outline-regexp))
  14070. (current (org-get-tags-string))
  14071. (col (current-column))
  14072. (org-setting-tags t)
  14073. table current-tags inherited-tags ; computed below when needed
  14074. tags p0 c0 c1 rpl)
  14075. (if arg
  14076. (save-excursion
  14077. (goto-char (point-min))
  14078. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  14079. (while (re-search-forward re nil t)
  14080. (org-set-tags nil t)
  14081. (end-of-line 1)))
  14082. (message "All tags realigned to column %d" org-tags-column))
  14083. (if just-align
  14084. (setq tags current)
  14085. ;; Get a new set of tags from the user
  14086. (save-excursion
  14087. (setq table (or org-tag-alist (org-get-buffer-tags))
  14088. org-last-tags-completion-table table
  14089. current-tags (org-split-string current ":")
  14090. inherited-tags (nreverse
  14091. (nthcdr (length current-tags)
  14092. (nreverse (org-get-tags-at))))
  14093. tags
  14094. (if (or (eq t org-use-fast-tag-selection)
  14095. (and org-use-fast-tag-selection
  14096. (delq nil (mapcar 'cdr table))))
  14097. (org-fast-tag-selection
  14098. current-tags inherited-tags table
  14099. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  14100. (let ((org-add-colon-after-tag-completion t))
  14101. (org-trim
  14102. (org-without-partial-completion
  14103. (completing-read "Tags: " 'org-tags-completion-function
  14104. nil nil current 'org-tags-history)))))))
  14105. (while (string-match "[-+&]+" tags)
  14106. ;; No boolean logic, just a list
  14107. (setq tags (replace-match ":" t t tags))))
  14108. (if (string-match "\\`[\t ]*\\'" tags)
  14109. (setq tags "")
  14110. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  14111. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  14112. ;; Insert new tags at the correct column
  14113. (beginning-of-line 1)
  14114. (cond
  14115. ((and (equal current "") (equal tags "")))
  14116. ((re-search-forward
  14117. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  14118. (point-at-eol) t)
  14119. (if (equal tags "")
  14120. (setq rpl "")
  14121. (goto-char (match-beginning 0))
  14122. (setq c0 (current-column) p0 (point)
  14123. c1 (max (1+ c0) (if (> org-tags-column 0)
  14124. org-tags-column
  14125. (- (- org-tags-column) (length tags))))
  14126. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  14127. (replace-match rpl t t)
  14128. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  14129. tags)
  14130. (t (error "Tags alignment failed")))
  14131. (move-to-column col)
  14132. (unless just-align
  14133. (run-hooks 'org-after-tags-change-hook)))))
  14134. (defun org-change-tag-in-region (beg end tag off)
  14135. "Add or remove TAG for each entry in the region.
  14136. This works in the agenda, and also in an org-mode buffer."
  14137. (interactive
  14138. (list (region-beginning) (region-end)
  14139. (let ((org-last-tags-completion-table
  14140. (if (org-mode-p)
  14141. (org-get-buffer-tags)
  14142. (org-global-tags-completion-table))))
  14143. (completing-read
  14144. "Tag: " 'org-tags-completion-function nil nil nil
  14145. 'org-tags-history))
  14146. (progn
  14147. (message "[s]et or [r]emove? ")
  14148. (equal (read-char-exclusive) ?r))))
  14149. (if (fboundp 'deactivate-mark) (deactivate-mark))
  14150. (let ((agendap (equal major-mode 'org-agenda-mode))
  14151. l1 l2 m buf pos newhead (cnt 0))
  14152. (goto-char end)
  14153. (setq l2 (1- (org-current-line)))
  14154. (goto-char beg)
  14155. (setq l1 (org-current-line))
  14156. (loop for l from l1 to l2 do
  14157. (goto-line l)
  14158. (setq m (get-text-property (point) 'org-hd-marker))
  14159. (when (or (and (org-mode-p) (org-on-heading-p))
  14160. (and agendap m))
  14161. (setq buf (if agendap (marker-buffer m) (current-buffer))
  14162. pos (if agendap m (point)))
  14163. (with-current-buffer buf
  14164. (save-excursion
  14165. (save-restriction
  14166. (goto-char pos)
  14167. (setq cnt (1+ cnt))
  14168. (org-toggle-tag tag (if off 'off 'on))
  14169. (setq newhead (org-get-heading)))))
  14170. (and agendap (org-agenda-change-all-lines newhead m))))
  14171. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  14172. (defun org-tags-completion-function (string predicate &optional flag)
  14173. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  14174. (confirm (lambda (x) (stringp (car x)))))
  14175. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  14176. (setq s1 (match-string 1 string)
  14177. s2 (match-string 2 string))
  14178. (setq s1 "" s2 string))
  14179. (cond
  14180. ((eq flag nil)
  14181. ;; try completion
  14182. (setq rtn (try-completion s2 ctable confirm))
  14183. (if (stringp rtn)
  14184. (setq rtn
  14185. (concat s1 s2 (substring rtn (length s2))
  14186. (if (and org-add-colon-after-tag-completion
  14187. (assoc rtn ctable))
  14188. ":" ""))))
  14189. rtn)
  14190. ((eq flag t)
  14191. ;; all-completions
  14192. (all-completions s2 ctable confirm)
  14193. )
  14194. ((eq flag 'lambda)
  14195. ;; exact match?
  14196. (assoc s2 ctable)))
  14197. ))
  14198. (defun org-fast-tag-insert (kwd tags face &optional end)
  14199. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  14200. (insert (format "%-12s" (concat kwd ":"))
  14201. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  14202. (or end "")))
  14203. (defun org-fast-tag-show-exit (flag)
  14204. (save-excursion
  14205. (goto-line 3)
  14206. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  14207. (replace-match ""))
  14208. (when flag
  14209. (end-of-line 1)
  14210. (move-to-column (- (window-width) 19) t)
  14211. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  14212. (defun org-set-current-tags-overlay (current prefix)
  14213. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  14214. (if (featurep 'xemacs)
  14215. (org-overlay-display org-tags-overlay (concat prefix s)
  14216. 'secondary-selection)
  14217. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  14218. (org-overlay-display org-tags-overlay (concat prefix s)))))
  14219. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  14220. "Fast tag selection with single keys.
  14221. CURRENT is the current list of tags in the headline, INHERITED is the
  14222. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  14223. possibly with grouping information. TODO-TABLE is a similar table with
  14224. TODO keywords, should these have keys assigned to them.
  14225. If the keys are nil, a-z are automatically assigned.
  14226. Returns the new tags string, or nil to not change the current settings."
  14227. (let* ((fulltable (append table todo-table))
  14228. (maxlen (apply 'max (mapcar
  14229. (lambda (x)
  14230. (if (stringp (car x)) (string-width (car x)) 0))
  14231. fulltable)))
  14232. (buf (current-buffer))
  14233. (expert (eq org-fast-tag-selection-single-key 'expert))
  14234. (buffer-tags nil)
  14235. (fwidth (+ maxlen 3 1 3))
  14236. (ncol (/ (- (window-width) 4) fwidth))
  14237. (i-face 'org-done)
  14238. (c-face 'org-todo)
  14239. tg cnt e c char c1 c2 ntable tbl rtn
  14240. ov-start ov-end ov-prefix
  14241. (exit-after-next org-fast-tag-selection-single-key)
  14242. (done-keywords org-done-keywords)
  14243. groups ingroup)
  14244. (save-excursion
  14245. (beginning-of-line 1)
  14246. (if (looking-at
  14247. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14248. (setq ov-start (match-beginning 1)
  14249. ov-end (match-end 1)
  14250. ov-prefix "")
  14251. (setq ov-start (1- (point-at-eol))
  14252. ov-end (1+ ov-start))
  14253. (skip-chars-forward "^\n\r")
  14254. (setq ov-prefix
  14255. (concat
  14256. (buffer-substring (1- (point)) (point))
  14257. (if (> (current-column) org-tags-column)
  14258. " "
  14259. (make-string (- org-tags-column (current-column)) ?\ ))))))
  14260. (org-move-overlay org-tags-overlay ov-start ov-end)
  14261. (save-window-excursion
  14262. (if expert
  14263. (set-buffer (get-buffer-create " *Org tags*"))
  14264. (delete-other-windows)
  14265. (split-window-vertically)
  14266. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  14267. (erase-buffer)
  14268. (org-set-local 'org-done-keywords done-keywords)
  14269. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  14270. (org-fast-tag-insert "Current" current c-face "\n\n")
  14271. (org-fast-tag-show-exit exit-after-next)
  14272. (org-set-current-tags-overlay current ov-prefix)
  14273. (setq tbl fulltable char ?a cnt 0)
  14274. (while (setq e (pop tbl))
  14275. (cond
  14276. ((equal e '(:startgroup))
  14277. (push '() groups) (setq ingroup t)
  14278. (when (not (= cnt 0))
  14279. (setq cnt 0)
  14280. (insert "\n"))
  14281. (insert "{ "))
  14282. ((equal e '(:endgroup))
  14283. (setq ingroup nil cnt 0)
  14284. (insert "}\n"))
  14285. (t
  14286. (setq tg (car e) c2 nil)
  14287. (if (cdr e)
  14288. (setq c (cdr e))
  14289. ;; automatically assign a character.
  14290. (setq c1 (string-to-char
  14291. (downcase (substring
  14292. tg (if (= (string-to-char tg) ?@) 1 0)))))
  14293. (if (or (rassoc c1 ntable) (rassoc c1 table))
  14294. (while (or (rassoc char ntable) (rassoc char table))
  14295. (setq char (1+ char)))
  14296. (setq c2 c1))
  14297. (setq c (or c2 char)))
  14298. (if ingroup (push tg (car groups)))
  14299. (setq tg (org-add-props tg nil 'face
  14300. (cond
  14301. ((not (assoc tg table))
  14302. (org-get-todo-face tg))
  14303. ((member tg current) c-face)
  14304. ((member tg inherited) i-face)
  14305. (t nil))))
  14306. (if (and (= cnt 0) (not ingroup)) (insert " "))
  14307. (insert "[" c "] " tg (make-string
  14308. (- fwidth 4 (length tg)) ?\ ))
  14309. (push (cons tg c) ntable)
  14310. (when (= (setq cnt (1+ cnt)) ncol)
  14311. (insert "\n")
  14312. (if ingroup (insert " "))
  14313. (setq cnt 0)))))
  14314. (setq ntable (nreverse ntable))
  14315. (insert "\n")
  14316. (goto-char (point-min))
  14317. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  14318. (fit-window-to-buffer))
  14319. (setq rtn
  14320. (catch 'exit
  14321. (while t
  14322. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  14323. (if groups " [!] no groups" " [!]groups")
  14324. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14325. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14326. (cond
  14327. ((= c ?\r) (throw 'exit t))
  14328. ((= c ?!)
  14329. (setq groups (not groups))
  14330. (goto-char (point-min))
  14331. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14332. ((= c ?\C-c)
  14333. (if (not expert)
  14334. (org-fast-tag-show-exit
  14335. (setq exit-after-next (not exit-after-next)))
  14336. (setq expert nil)
  14337. (delete-other-windows)
  14338. (split-window-vertically)
  14339. (org-switch-to-buffer-other-window " *Org tags*")
  14340. (and (fboundp 'fit-window-to-buffer)
  14341. (fit-window-to-buffer))))
  14342. ((or (= c ?\C-g)
  14343. (and (= c ?q) (not (rassoc c ntable))))
  14344. (org-detach-overlay org-tags-overlay)
  14345. (setq quit-flag t))
  14346. ((= c ?\ )
  14347. (setq current nil)
  14348. (if exit-after-next (setq exit-after-next 'now)))
  14349. ((= c ?\t)
  14350. (condition-case nil
  14351. (setq tg (completing-read
  14352. "Tag: "
  14353. (or buffer-tags
  14354. (with-current-buffer buf
  14355. (org-get-buffer-tags)))))
  14356. (quit (setq tg "")))
  14357. (when (string-match "\\S-" tg)
  14358. (add-to-list 'buffer-tags (list tg))
  14359. (if (member tg current)
  14360. (setq current (delete tg current))
  14361. (push tg current)))
  14362. (if exit-after-next (setq exit-after-next 'now)))
  14363. ((setq e (rassoc c todo-table) tg (car e))
  14364. (with-current-buffer buf
  14365. (save-excursion (org-todo tg)))
  14366. (if exit-after-next (setq exit-after-next 'now)))
  14367. ((setq e (rassoc c ntable) tg (car e))
  14368. (if (member tg current)
  14369. (setq current (delete tg current))
  14370. (loop for g in groups do
  14371. (if (member tg g)
  14372. (mapc (lambda (x)
  14373. (setq current (delete x current)))
  14374. g)))
  14375. (push tg current))
  14376. (if exit-after-next (setq exit-after-next 'now))))
  14377. ;; Create a sorted list
  14378. (setq current
  14379. (sort current
  14380. (lambda (a b)
  14381. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14382. (if (eq exit-after-next 'now) (throw 'exit t))
  14383. (goto-char (point-min))
  14384. (beginning-of-line 2)
  14385. (delete-region (point) (point-at-eol))
  14386. (org-fast-tag-insert "Current" current c-face)
  14387. (org-set-current-tags-overlay current ov-prefix)
  14388. (while (re-search-forward
  14389. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  14390. (setq tg (match-string 1))
  14391. (add-text-properties
  14392. (match-beginning 1) (match-end 1)
  14393. (list 'face
  14394. (cond
  14395. ((member tg current) c-face)
  14396. ((member tg inherited) i-face)
  14397. (t (get-text-property (match-beginning 1) 'face))))))
  14398. (goto-char (point-min)))))
  14399. (org-detach-overlay org-tags-overlay)
  14400. (if rtn
  14401. (mapconcat 'identity current ":")
  14402. nil))))
  14403. (defun org-get-tags-string ()
  14404. "Get the TAGS string in the current headline."
  14405. (unless (org-on-heading-p t)
  14406. (error "Not on a heading"))
  14407. (save-excursion
  14408. (beginning-of-line 1)
  14409. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14410. (org-match-string-no-properties 1)
  14411. "")))
  14412. (defun org-get-tags ()
  14413. "Get the list of tags specified in the current headline."
  14414. (org-split-string (org-get-tags-string) ":"))
  14415. (defun org-get-buffer-tags ()
  14416. "Get a table of all tags used in the buffer, for completion."
  14417. (let (tags)
  14418. (save-excursion
  14419. (goto-char (point-min))
  14420. (while (re-search-forward
  14421. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  14422. (when (equal (char-after (point-at-bol 0)) ?*)
  14423. (mapc (lambda (x) (add-to-list 'tags x))
  14424. (org-split-string (org-match-string-no-properties 1) ":")))))
  14425. (mapcar 'list tags)))
  14426. ;;;; Properties
  14427. ;;; Setting and retrieving properties
  14428. (defconst org-special-properties
  14429. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  14430. "TIMESTAMP" "TIMESTAMP_IA")
  14431. "The special properties valid in Org-mode.
  14432. These are properties that are not defined in the property drawer,
  14433. but in some other way.")
  14434. (defconst org-default-properties
  14435. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  14436. "LOCATION" "LOGGING" "COLUMNS")
  14437. "Some properties that are used by Org-mode for various purposes.
  14438. Being in this list makes sure that they are offered for completion.")
  14439. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  14440. "Regular expression matching the first line of a property drawer.")
  14441. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  14442. "Regular expression matching the first line of a property drawer.")
  14443. (defun org-property-action ()
  14444. "Do an action on properties."
  14445. (interactive)
  14446. (let (c)
  14447. (org-at-property-p)
  14448. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14449. (setq c (read-char-exclusive))
  14450. (cond
  14451. ((equal c ?s)
  14452. (call-interactively 'org-set-property))
  14453. ((equal c ?d)
  14454. (call-interactively 'org-delete-property))
  14455. ((equal c ?D)
  14456. (call-interactively 'org-delete-property-globally))
  14457. ((equal c ?c)
  14458. (call-interactively 'org-compute-property-at-point))
  14459. (t (error "No such property action %c" c)))))
  14460. (defun org-at-property-p ()
  14461. "Is the cursor in a property line?"
  14462. ;; FIXME: Does not check if we are actually in the drawer.
  14463. ;; FIXME: also returns true on any drawers.....
  14464. ;; This is used by C-c C-c for property action.
  14465. (save-excursion
  14466. (beginning-of-line 1)
  14467. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  14468. (defmacro org-with-point-at (pom &rest body)
  14469. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  14470. (declare (indent 1) (debug t))
  14471. `(save-excursion
  14472. (if (markerp pom) (set-buffer (marker-buffer pom)))
  14473. (save-excursion
  14474. (goto-char (or pom (point)))
  14475. ,@body)))
  14476. (defun org-get-property-block (&optional beg end force)
  14477. "Return the (beg . end) range of the body of the property drawer.
  14478. BEG and END can be beginning and end of subtree, if not given
  14479. they will be found.
  14480. If the drawer does not exist and FORCE is non-nil, create the drawer."
  14481. (catch 'exit
  14482. (save-excursion
  14483. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  14484. (end (or end (progn (outline-next-heading) (point)))))
  14485. (goto-char beg)
  14486. (if (re-search-forward org-property-start-re end t)
  14487. (setq beg (1+ (match-end 0)))
  14488. (if force
  14489. (save-excursion
  14490. (org-insert-property-drawer)
  14491. (setq end (progn (outline-next-heading) (point))))
  14492. (throw 'exit nil))
  14493. (goto-char beg)
  14494. (if (re-search-forward org-property-start-re end t)
  14495. (setq beg (1+ (match-end 0)))))
  14496. (if (re-search-forward org-property-end-re end t)
  14497. (setq end (match-beginning 0))
  14498. (or force (throw 'exit nil))
  14499. (goto-char beg)
  14500. (setq end beg)
  14501. (org-indent-line-function)
  14502. (insert ":END:\n"))
  14503. (cons beg end)))))
  14504. (defun org-entry-properties (&optional pom which)
  14505. "Get all properties of the entry at point-or-marker POM.
  14506. This includes the TODO keyword, the tags, time strings for deadline,
  14507. scheduled, and clocking, and any additional properties defined in the
  14508. entry. The return value is an alist, keys may occur multiple times
  14509. if the property key was used several times.
  14510. POM may also be nil, in which case the current entry is used.
  14511. If WHICH is nil or `all', get all properties. If WHICH is
  14512. `special' or `standard', only get that subclass."
  14513. (setq which (or which 'all))
  14514. (org-with-point-at pom
  14515. (let ((clockstr (substring org-clock-string 0 -1))
  14516. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  14517. beg end range props sum-props key value string clocksum)
  14518. (save-excursion
  14519. (when (condition-case nil (org-back-to-heading t) (error nil))
  14520. (setq beg (point))
  14521. (setq sum-props (get-text-property (point) 'org-summaries))
  14522. (setq clocksum (get-text-property (point) :org-clock-minutes))
  14523. (outline-next-heading)
  14524. (setq end (point))
  14525. (when (memq which '(all special))
  14526. ;; Get the special properties, like TODO and tags
  14527. (goto-char beg)
  14528. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14529. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14530. (when (looking-at org-priority-regexp)
  14531. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14532. (when (and (setq value (org-get-tags-string))
  14533. (string-match "\\S-" value))
  14534. (push (cons "TAGS" value) props))
  14535. (when (setq value (org-get-tags-at))
  14536. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  14537. props))
  14538. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14539. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  14540. string (if (equal key clockstr)
  14541. (org-no-properties
  14542. (org-trim
  14543. (buffer-substring
  14544. (match-beginning 3) (goto-char (point-at-eol)))))
  14545. (substring (org-match-string-no-properties 3) 1 -1)))
  14546. (unless key
  14547. (if (= (char-after (match-beginning 3)) ?\[)
  14548. (setq key "TIMESTAMP_IA")
  14549. (setq key "TIMESTAMP")))
  14550. (when (or (equal key clockstr) (not (assoc key props)))
  14551. (push (cons key string) props)))
  14552. )
  14553. (when (memq which '(all standard))
  14554. ;; Get the standard properties, like :PORP: ...
  14555. (setq range (org-get-property-block beg end))
  14556. (when range
  14557. (goto-char (car range))
  14558. (while (re-search-forward
  14559. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  14560. (cdr range) t)
  14561. (setq key (org-match-string-no-properties 1)
  14562. value (org-trim (or (org-match-string-no-properties 2) "")))
  14563. (unless (member key excluded)
  14564. (push (cons key (or value "")) props)))))
  14565. (if clocksum
  14566. (push (cons "CLOCKSUM"
  14567. (org-column-number-to-string (/ (float clocksum) 60.)
  14568. 'add_times))
  14569. props))
  14570. (append sum-props (nreverse props)))))))
  14571. (defun org-entry-get (pom property &optional inherit)
  14572. "Get value of PROPERTY for entry at point-or-marker POM.
  14573. If INHERIT is non-nil and the entry does not have the property,
  14574. then also check higher levels of the hierarchy.
  14575. If the property is present but empty, the return value is the empty string.
  14576. If the property is not present at all, nil is returned."
  14577. (org-with-point-at pom
  14578. (if inherit
  14579. (org-entry-get-with-inheritance property)
  14580. (if (member property org-special-properties)
  14581. ;; We need a special property. Use brute force, get all properties.
  14582. (cdr (assoc property (org-entry-properties nil 'special)))
  14583. (let ((range (org-get-property-block)))
  14584. (if (and range
  14585. (goto-char (car range))
  14586. (re-search-forward
  14587. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  14588. (cdr range) t))
  14589. ;; Found the property, return it.
  14590. (if (match-end 1)
  14591. (org-match-string-no-properties 1)
  14592. "")))))))
  14593. (defun org-entry-delete (pom property)
  14594. "Delete the property PROPERTY from entry at point-or-marker POM."
  14595. (org-with-point-at pom
  14596. (if (member property org-special-properties)
  14597. nil ; cannot delete these properties.
  14598. (let ((range (org-get-property-block)))
  14599. (if (and range
  14600. (goto-char (car range))
  14601. (re-search-forward
  14602. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  14603. (cdr range) t))
  14604. (progn
  14605. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14606. t)
  14607. nil)))))
  14608. ;; Multi-values properties are properties that contain multiple values
  14609. ;; These values are assumed to be single words, separated by whitespace.
  14610. (defun org-entry-add-to-multivalued-property (pom property value)
  14611. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14612. (let* ((old (org-entry-get pom property))
  14613. (values (and old (org-split-string old "[ \t]"))))
  14614. (unless (member value values)
  14615. (setq values (cons value values))
  14616. (org-entry-put pom property
  14617. (mapconcat 'identity values " ")))))
  14618. (defun org-entry-remove-from-multivalued-property (pom property value)
  14619. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14620. (let* ((old (org-entry-get pom property))
  14621. (values (and old (org-split-string old "[ \t]"))))
  14622. (when (member value values)
  14623. (setq values (delete value values))
  14624. (org-entry-put pom property
  14625. (mapconcat 'identity values " ")))))
  14626. (defun org-entry-member-in-multivalued-property (pom property value)
  14627. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14628. (let* ((old (org-entry-get pom property))
  14629. (values (and old (org-split-string old "[ \t]"))))
  14630. (member value values)))
  14631. (defvar org-entry-property-inherited-from (make-marker))
  14632. (defun org-entry-get-with-inheritance (property)
  14633. "Get entry property, and search higher levels if not present."
  14634. (let (tmp)
  14635. (save-excursion
  14636. (save-restriction
  14637. (widen)
  14638. (catch 'ex
  14639. (while t
  14640. (when (setq tmp (org-entry-get nil property))
  14641. (org-back-to-heading t)
  14642. (move-marker org-entry-property-inherited-from (point))
  14643. (throw 'ex tmp))
  14644. (or (org-up-heading-safe) (throw 'ex nil)))))
  14645. (or tmp (cdr (assoc property org-local-properties))
  14646. (cdr (assoc property org-global-properties))))))
  14647. (defun org-entry-put (pom property value)
  14648. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14649. (org-with-point-at pom
  14650. (org-back-to-heading t)
  14651. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14652. range)
  14653. (cond
  14654. ((equal property "TODO")
  14655. (when (and (stringp value) (string-match "\\S-" value)
  14656. (not (member value org-todo-keywords-1)))
  14657. (error "\"%s\" is not a valid TODO state" value))
  14658. (if (or (not value)
  14659. (not (string-match "\\S-" value)))
  14660. (setq value 'none))
  14661. (org-todo value)
  14662. (org-set-tags nil 'align))
  14663. ((equal property "PRIORITY")
  14664. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14665. (string-to-char value) ?\ ))
  14666. (org-set-tags nil 'align))
  14667. ((equal property "SCHEDULED")
  14668. (if (re-search-forward org-scheduled-time-regexp end t)
  14669. (cond
  14670. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14671. ((eq value 'later) (org-timestamp-change 1 'day))
  14672. (t (call-interactively 'org-schedule)))
  14673. (call-interactively 'org-schedule)))
  14674. ((equal property "DEADLINE")
  14675. (if (re-search-forward org-deadline-time-regexp end t)
  14676. (cond
  14677. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14678. ((eq value 'later) (org-timestamp-change 1 'day))
  14679. (t (call-interactively 'org-deadline)))
  14680. (call-interactively 'org-deadline)))
  14681. ((member property org-special-properties)
  14682. (error "The %s property can not yet be set with `org-entry-put'"
  14683. property))
  14684. (t ; a non-special property
  14685. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14686. (setq range (org-get-property-block beg end 'force))
  14687. (goto-char (car range))
  14688. (if (re-search-forward
  14689. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  14690. (progn
  14691. (delete-region (match-beginning 1) (match-end 1))
  14692. (goto-char (match-beginning 1)))
  14693. (goto-char (cdr range))
  14694. (insert "\n")
  14695. (backward-char 1)
  14696. (org-indent-line-function)
  14697. (insert ":" property ":"))
  14698. (and value (insert " " value))
  14699. (org-indent-line-function)))))))
  14700. (defun org-buffer-property-keys (&optional include-specials include-defaults)
  14701. "Get all property keys in the current buffer.
  14702. With INCLUDE-SPECIALS, also list the special properties that relect things
  14703. like tags and TODO state.
  14704. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14705. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING."
  14706. (let (rtn range)
  14707. (save-excursion
  14708. (save-restriction
  14709. (widen)
  14710. (goto-char (point-min))
  14711. (while (re-search-forward org-property-start-re nil t)
  14712. (setq range (org-get-property-block))
  14713. (goto-char (car range))
  14714. (while (re-search-forward
  14715. (org-re "^[ \t]*:\\([[:alnum:]_-]+\\):")
  14716. (cdr range) t)
  14717. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14718. (outline-next-heading))))
  14719. (when include-specials
  14720. (setq rtn (append org-special-properties rtn)))
  14721. (when include-defaults
  14722. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  14723. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14724. (defun org-property-values (key)
  14725. "Return a list of all values of property KEY."
  14726. (save-excursion
  14727. (save-restriction
  14728. (widen)
  14729. (goto-char (point-min))
  14730. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  14731. values)
  14732. (while (re-search-forward re nil t)
  14733. (add-to-list 'values (org-trim (match-string 1))))
  14734. (delete "" values)))))
  14735. (defun org-insert-property-drawer ()
  14736. "Insert a property drawer into the current entry."
  14737. (interactive)
  14738. (org-back-to-heading t)
  14739. (looking-at outline-regexp)
  14740. (let ((indent (- (match-end 0)(match-beginning 0)))
  14741. (beg (point))
  14742. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14743. end hiddenp)
  14744. (outline-next-heading)
  14745. (setq end (point))
  14746. (goto-char beg)
  14747. (while (re-search-forward re end t))
  14748. (setq hiddenp (org-invisible-p))
  14749. (end-of-line 1)
  14750. (and (equal (char-after) ?\n) (forward-char 1))
  14751. (org-skip-over-state-notes)
  14752. (skip-chars-backward " \t\n\r")
  14753. (if (eq (char-before) ?*) (forward-char 1))
  14754. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14755. (beginning-of-line 0)
  14756. (indent-to-column indent)
  14757. (beginning-of-line 2)
  14758. (indent-to-column indent)
  14759. (beginning-of-line 0)
  14760. (if hiddenp
  14761. (save-excursion
  14762. (org-back-to-heading t)
  14763. (hide-entry))
  14764. (org-flag-drawer t))))
  14765. (defun org-set-property (property value)
  14766. "In the current entry, set PROPERTY to VALUE.
  14767. When called interactively, this will prompt for a property name, offering
  14768. completion on existing and default properties. And then it will prompt
  14769. for a value, offering competion either on allowed values (via an inherited
  14770. xxx_ALL property) or on existing values in other instances of this property
  14771. in the current file."
  14772. (interactive
  14773. (let* ((prop (completing-read
  14774. "Property: " (mapcar 'list (org-buffer-property-keys nil t))))
  14775. (cur (org-entry-get nil prop))
  14776. (allowed (org-property-get-allowed-values nil prop 'table))
  14777. (existing (mapcar 'list (org-property-values prop)))
  14778. (val (if allowed
  14779. (completing-read "Value: " allowed nil 'req-match)
  14780. (completing-read
  14781. (concat "Value" (if (and cur (string-match "\\S-" cur))
  14782. (concat "[" cur "]") "")
  14783. ": ")
  14784. existing nil nil "" nil cur))))
  14785. (list prop (if (equal val "") cur val))))
  14786. (unless (equal (org-entry-get nil property) value)
  14787. (org-entry-put nil property value)))
  14788. (defun org-delete-property (property)
  14789. "In the current entry, delete PROPERTY."
  14790. (interactive
  14791. (let* ((prop (completing-read
  14792. "Property: " (org-entry-properties nil 'standard))))
  14793. (list prop)))
  14794. (message "Property %s %s" property
  14795. (if (org-entry-delete nil property)
  14796. "deleted"
  14797. "was not present in the entry")))
  14798. (defun org-delete-property-globally (property)
  14799. "Remove PROPERTY globally, from all entries."
  14800. (interactive
  14801. (let* ((prop (completing-read
  14802. "Globally remove property: "
  14803. (mapcar 'list (org-buffer-property-keys)))))
  14804. (list prop)))
  14805. (save-excursion
  14806. (save-restriction
  14807. (widen)
  14808. (goto-char (point-min))
  14809. (let ((cnt 0))
  14810. (while (re-search-forward
  14811. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  14812. nil t)
  14813. (setq cnt (1+ cnt))
  14814. (replace-match ""))
  14815. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14816. (defvar org-columns-current-fmt-compiled) ; defined below
  14817. (defun org-compute-property-at-point ()
  14818. "Compute the property at point.
  14819. This looks for an enclosing column format, extracts the operator and
  14820. then applies it to the proerty in the column format's scope."
  14821. (interactive)
  14822. (unless (org-at-property-p)
  14823. (error "Not at a property"))
  14824. (let ((prop (org-match-string-no-properties 2)))
  14825. (org-columns-get-format-and-top-level)
  14826. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14827. (error "No operator defined for property %s" prop))
  14828. (org-columns-compute prop)))
  14829. (defun org-property-get-allowed-values (pom property &optional table)
  14830. "Get allowed values for the property PROPERTY.
  14831. When TABLE is non-nil, return an alist that can directly be used for
  14832. completion."
  14833. (let (vals)
  14834. (cond
  14835. ((equal property "TODO")
  14836. (setq vals (org-with-point-at pom
  14837. (append org-todo-keywords-1 '("")))))
  14838. ((equal property "PRIORITY")
  14839. (let ((n org-lowest-priority))
  14840. (while (>= n org-highest-priority)
  14841. (push (char-to-string n) vals)
  14842. (setq n (1- n)))))
  14843. ((member property org-special-properties))
  14844. (t
  14845. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14846. (when (and vals (string-match "\\S-" vals))
  14847. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14848. (setq vals (mapcar (lambda (x)
  14849. (cond ((stringp x) x)
  14850. ((numberp x) (number-to-string x))
  14851. ((symbolp x) (symbol-name x))
  14852. (t "???")))
  14853. vals)))))
  14854. (if table (mapcar 'list vals) vals)))
  14855. (defun org-property-previous-allowed-value (&optional previous)
  14856. "Switch to the next allowed value for this property."
  14857. (interactive)
  14858. (org-property-next-allowed-value t))
  14859. (defun org-property-next-allowed-value (&optional previous)
  14860. "Switch to the next allowed value for this property."
  14861. (interactive)
  14862. (unless (org-at-property-p)
  14863. (error "Not at a property"))
  14864. (let* ((key (match-string 2))
  14865. (value (match-string 3))
  14866. (allowed (or (org-property-get-allowed-values (point) key)
  14867. (and (member value '("[ ]" "[-]" "[X]"))
  14868. '("[ ]" "[X]"))))
  14869. nval)
  14870. (unless allowed
  14871. (error "Allowed values for this property have not been defined"))
  14872. (if previous (setq allowed (reverse allowed)))
  14873. (if (member value allowed)
  14874. (setq nval (car (cdr (member value allowed)))))
  14875. (setq nval (or nval (car allowed)))
  14876. (if (equal nval value)
  14877. (error "Only one allowed value for this property"))
  14878. (org-at-property-p)
  14879. (replace-match (concat " :" key ": " nval) t t)
  14880. (org-indent-line-function)
  14881. (beginning-of-line 1)
  14882. (skip-chars-forward " \t")))
  14883. (defun org-find-entry-with-id (ident)
  14884. "Locate the entry that contains the ID property with exact value IDENT.
  14885. IDENT can be a string, a symbol or a number, this function will search for
  14886. the string representation of it.
  14887. Return the position where this entry starts, or nil if there is no such entry."
  14888. (let ((id (cond
  14889. ((stringp ident) ident)
  14890. ((symbol-name ident) (symbol-name ident))
  14891. ((numberp ident) (number-to-string ident))
  14892. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14893. (case-fold-search nil))
  14894. (save-excursion
  14895. (save-restriction
  14896. (goto-char (point-min))
  14897. (when (re-search-forward
  14898. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14899. nil t)
  14900. (org-back-to-heading)
  14901. (point))))))
  14902. ;;; Column View
  14903. (defvar org-columns-overlays nil
  14904. "Holds the list of current column overlays.")
  14905. (defvar org-columns-current-fmt nil
  14906. "Local variable, holds the currently active column format.")
  14907. (defvar org-columns-current-fmt-compiled nil
  14908. "Local variable, holds the currently active column format.
  14909. This is the compiled version of the format.")
  14910. (defvar org-columns-current-widths nil
  14911. "Loval variable, holds the currently widths of fields.")
  14912. (defvar org-columns-current-maxwidths nil
  14913. "Loval variable, holds the currently active maximum column widths.")
  14914. (defvar org-columns-begin-marker (make-marker)
  14915. "Points to the position where last a column creation command was called.")
  14916. (defvar org-columns-top-level-marker (make-marker)
  14917. "Points to the position where current columns region starts.")
  14918. (defvar org-columns-map (make-sparse-keymap)
  14919. "The keymap valid in column display.")
  14920. (defun org-columns-content ()
  14921. "Switch to contents view while in columns view."
  14922. (interactive)
  14923. (org-overview)
  14924. (org-content))
  14925. (org-defkey org-columns-map "c" 'org-columns-content)
  14926. (org-defkey org-columns-map "o" 'org-overview)
  14927. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  14928. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  14929. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  14930. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  14931. (org-defkey org-columns-map "v" 'org-columns-show-value)
  14932. (org-defkey org-columns-map "q" 'org-columns-quit)
  14933. (org-defkey org-columns-map "r" 'org-columns-redo)
  14934. (org-defkey org-columns-map "g" 'org-columns-redo)
  14935. (org-defkey org-columns-map [left] 'backward-char)
  14936. (org-defkey org-columns-map "\M-b" 'backward-char)
  14937. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  14938. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  14939. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  14940. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  14941. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  14942. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  14943. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  14944. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  14945. (org-defkey org-columns-map "<" 'org-columns-narrow)
  14946. (org-defkey org-columns-map ">" 'org-columns-widen)
  14947. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  14948. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  14949. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  14950. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  14951. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  14952. '("Column"
  14953. ["Edit property" org-columns-edit-value t]
  14954. ["Next allowed value" org-columns-next-allowed-value t]
  14955. ["Previous allowed value" org-columns-previous-allowed-value t]
  14956. ["Show full value" org-columns-show-value t]
  14957. ["Edit allowed values" org-columns-edit-allowed t]
  14958. "--"
  14959. ["Edit column attributes" org-columns-edit-attributes t]
  14960. ["Increase column width" org-columns-widen t]
  14961. ["Decrease column width" org-columns-narrow t]
  14962. "--"
  14963. ["Move column right" org-columns-move-right t]
  14964. ["Move column left" org-columns-move-left t]
  14965. ["Add column" org-columns-new t]
  14966. ["Delete column" org-columns-delete t]
  14967. "--"
  14968. ["CONTENTS" org-columns-content t]
  14969. ["OVERVIEW" org-overview t]
  14970. ["Refresh columns display" org-columns-redo t]
  14971. "--"
  14972. ["Open link" org-columns-open-link t]
  14973. "--"
  14974. ["Quit" org-columns-quit t]))
  14975. (defun org-columns-new-overlay (beg end &optional string face)
  14976. "Create a new column overlay and add it to the list."
  14977. (let ((ov (org-make-overlay beg end)))
  14978. (org-overlay-put ov 'face (or face 'secondary-selection))
  14979. (org-overlay-display ov string face)
  14980. (push ov org-columns-overlays)
  14981. ov))
  14982. (defun org-columns-display-here (&optional props)
  14983. "Overlay the current line with column display."
  14984. (interactive)
  14985. (let* ((fmt org-columns-current-fmt-compiled)
  14986. (beg (point-at-bol))
  14987. (level-face (save-excursion
  14988. (beginning-of-line 1)
  14989. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  14990. (org-get-level-face 2))))
  14991. (color (list :foreground
  14992. (face-attribute (or level-face 'default) :foreground)))
  14993. props pom property ass width f string ov column val modval)
  14994. ;; Check if the entry is in another buffer.
  14995. (unless props
  14996. (if (eq major-mode 'org-agenda-mode)
  14997. (setq pom (or (get-text-property (point) 'org-hd-marker)
  14998. (get-text-property (point) 'org-marker))
  14999. props (if pom (org-entry-properties pom) nil))
  15000. (setq props (org-entry-properties nil))))
  15001. ;; Walk the format
  15002. (while (setq column (pop fmt))
  15003. (setq property (car column)
  15004. ass (if (equal property "ITEM")
  15005. (cons "ITEM"
  15006. (save-match-data
  15007. (org-no-properties
  15008. (org-remove-tabs
  15009. (buffer-substring-no-properties
  15010. (point-at-bol) (point-at-eol))))))
  15011. (assoc property props))
  15012. width (or (cdr (assoc property org-columns-current-maxwidths))
  15013. (nth 2 column)
  15014. (length property))
  15015. f (format "%%-%d.%ds | " width width)
  15016. val (or (cdr ass) "")
  15017. modval (if (equal property "ITEM")
  15018. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  15019. string (format f (or modval val)))
  15020. ;; Create the overlay
  15021. (org-unmodified
  15022. (setq ov (org-columns-new-overlay
  15023. beg (setq beg (1+ beg)) string
  15024. (list color 'org-column)))
  15025. ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
  15026. (org-overlay-put ov 'keymap org-columns-map)
  15027. (org-overlay-put ov 'org-columns-key property)
  15028. (org-overlay-put ov 'org-columns-value (cdr ass))
  15029. (org-overlay-put ov 'org-columns-value-modified modval)
  15030. (org-overlay-put ov 'org-columns-pom pom)
  15031. (org-overlay-put ov 'org-columns-format f))
  15032. (if (or (not (char-after beg))
  15033. (equal (char-after beg) ?\n))
  15034. (let ((inhibit-read-only t))
  15035. (save-excursion
  15036. (goto-char beg)
  15037. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  15038. ;; Make the rest of the line disappear.
  15039. (org-unmodified
  15040. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  15041. (org-overlay-put ov 'invisible t)
  15042. (org-overlay-put ov 'keymap org-columns-map)
  15043. (org-overlay-put ov 'intangible t)
  15044. (push ov org-columns-overlays)
  15045. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  15046. (org-overlay-put ov 'keymap org-columns-map)
  15047. (push ov org-columns-overlays)
  15048. (let ((inhibit-read-only t))
  15049. (put-text-property (max (point-min) (1- (point-at-bol)))
  15050. (min (point-max) (1+ (point-at-eol)))
  15051. 'read-only "Type `e' to edit property")))))
  15052. (defvar org-previous-header-line-format nil
  15053. "The header line format before column view was turned on.")
  15054. (defvar org-columns-inhibit-recalculation nil
  15055. "Inhibit recomputing of columns on column view startup.")
  15056. (defvar header-line-format)
  15057. (defun org-columns-display-here-title ()
  15058. "Overlay the newline before the current line with the table title."
  15059. (interactive)
  15060. (let ((fmt org-columns-current-fmt-compiled)
  15061. string (title "")
  15062. property width f column str widths)
  15063. (while (setq column (pop fmt))
  15064. (setq property (car column)
  15065. str (or (nth 1 column) property)
  15066. width (or (cdr (assoc property org-columns-current-maxwidths))
  15067. (nth 2 column)
  15068. (length str))
  15069. widths (push width widths)
  15070. f (format "%%-%d.%ds | " width width)
  15071. string (format f str)
  15072. title (concat title string)))
  15073. (setq title (concat
  15074. (org-add-props " " nil 'display '(space :align-to 0))
  15075. (org-add-props title nil 'face '(:weight bold :underline t))))
  15076. (org-set-local 'org-previous-header-line-format header-line-format)
  15077. (org-set-local 'org-columns-current-widths (nreverse widths))
  15078. (setq header-line-format title)))
  15079. (defun org-columns-remove-overlays ()
  15080. "Remove all currently active column overlays."
  15081. (interactive)
  15082. (when (marker-buffer org-columns-begin-marker)
  15083. (with-current-buffer (marker-buffer org-columns-begin-marker)
  15084. (when (local-variable-p 'org-previous-header-line-format)
  15085. (setq header-line-format org-previous-header-line-format)
  15086. (kill-local-variable 'org-previous-header-line-format))
  15087. (move-marker org-columns-begin-marker nil)
  15088. (move-marker org-columns-top-level-marker nil)
  15089. (org-unmodified
  15090. (mapc 'org-delete-overlay org-columns-overlays)
  15091. (setq org-columns-overlays nil)
  15092. (let ((inhibit-read-only t))
  15093. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  15094. (defun org-columns-cleanup-item (item fmt)
  15095. "Remove from ITEM what is a column in the format FMT."
  15096. (if (not org-complex-heading-regexp)
  15097. item
  15098. (when (string-match org-complex-heading-regexp item)
  15099. (concat
  15100. (org-add-props (concat (match-string 1 item) " ") nil
  15101. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  15102. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  15103. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  15104. " " (match-string 4 item)
  15105. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  15106. (defun org-columns-show-value ()
  15107. "Show the full value of the property."
  15108. (interactive)
  15109. (let ((value (get-char-property (point) 'org-columns-value)))
  15110. (message "Value is: %s" (or value ""))))
  15111. (defun org-columns-quit ()
  15112. "Remove the column overlays and in this way exit column editing."
  15113. (interactive)
  15114. (org-unmodified
  15115. (org-columns-remove-overlays)
  15116. (let ((inhibit-read-only t))
  15117. (remove-text-properties (point-min) (point-max) '(read-only t))))
  15118. (when (eq major-mode 'org-agenda-mode)
  15119. (message
  15120. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  15121. (defun org-columns-check-computed ()
  15122. "Check if this column value is computed.
  15123. If yes, throw an error indicating that changing it does not make sense."
  15124. (let ((val (get-char-property (point) 'org-columns-value)))
  15125. (when (and (stringp val)
  15126. (get-char-property 0 'org-computed val))
  15127. (error "This value is computed from the entry's children"))))
  15128. (defun org-columns-todo (&optional arg)
  15129. "Change the TODO state during column view."
  15130. (interactive "P")
  15131. (org-columns-edit-value "TODO"))
  15132. (defun org-columns-set-tags-or-toggle (&optional arg)
  15133. "Toggle checkbox at point, or set tags for current headline."
  15134. (interactive "P")
  15135. (if (string-match "\\`\\[[ xX-]\\]\\'"
  15136. (get-char-property (point) 'org-columns-value))
  15137. (org-columns-next-allowed-value)
  15138. (org-columns-edit-value "TAGS")))
  15139. (defun org-columns-edit-value (&optional key)
  15140. "Edit the value of the property at point in column view.
  15141. Where possible, use the standard interface for changing this line."
  15142. (interactive)
  15143. (org-columns-check-computed)
  15144. (let* ((external-key key)
  15145. (col (current-column))
  15146. (key (or key (get-char-property (point) 'org-columns-key)))
  15147. (value (get-char-property (point) 'org-columns-value))
  15148. (bol (point-at-bol)) (eol (point-at-eol))
  15149. (pom (or (get-text-property bol 'org-hd-marker)
  15150. (point))) ; keep despite of compiler waring
  15151. (line-overlays
  15152. (delq nil (mapcar (lambda (x)
  15153. (and (eq (overlay-buffer x) (current-buffer))
  15154. (>= (overlay-start x) bol)
  15155. (<= (overlay-start x) eol)
  15156. x))
  15157. org-columns-overlays)))
  15158. nval eval allowed)
  15159. (cond
  15160. ((equal key "CLOCKSUM")
  15161. (error "This special column cannot be edited"))
  15162. ((equal key "ITEM")
  15163. (setq eval '(org-with-point-at pom
  15164. (org-edit-headline))))
  15165. ((equal key "TODO")
  15166. (setq eval '(org-with-point-at pom
  15167. (let ((current-prefix-arg
  15168. (if external-key current-prefix-arg '(4))))
  15169. (call-interactively 'org-todo)))))
  15170. ((equal key "PRIORITY")
  15171. (setq eval '(org-with-point-at pom
  15172. (call-interactively 'org-priority))))
  15173. ((equal key "TAGS")
  15174. (setq eval '(org-with-point-at pom
  15175. (let ((org-fast-tag-selection-single-key
  15176. (if (eq org-fast-tag-selection-single-key 'expert)
  15177. t org-fast-tag-selection-single-key)))
  15178. (call-interactively 'org-set-tags)))))
  15179. ((equal key "DEADLINE")
  15180. (setq eval '(org-with-point-at pom
  15181. (call-interactively 'org-deadline))))
  15182. ((equal key "SCHEDULED")
  15183. (setq eval '(org-with-point-at pom
  15184. (call-interactively 'org-schedule))))
  15185. (t
  15186. (setq allowed (org-property-get-allowed-values pom key 'table))
  15187. (if allowed
  15188. (setq nval (completing-read "Value: " allowed nil t))
  15189. (setq nval (read-string "Edit: " value)))
  15190. (setq nval (org-trim nval))
  15191. (when (not (equal nval value))
  15192. (setq eval '(org-entry-put pom key nval)))))
  15193. (when eval
  15194. (let ((inhibit-read-only t))
  15195. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  15196. (unwind-protect
  15197. (progn
  15198. (setq org-columns-overlays
  15199. (org-delete-all line-overlays org-columns-overlays))
  15200. (mapc 'org-delete-overlay line-overlays)
  15201. (org-columns-eval eval))
  15202. (org-columns-display-here))))
  15203. (move-to-column col)
  15204. (if (and (org-mode-p)
  15205. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15206. (org-columns-update key))))
  15207. (defun org-edit-headline () ; FIXME: this is not columns specific
  15208. "Edit the current headline, the part without TODO keyword, TAGS."
  15209. (org-back-to-heading)
  15210. (when (looking-at org-todo-line-regexp)
  15211. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  15212. (txt (match-string 3))
  15213. (post "")
  15214. txt2)
  15215. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  15216. (setq post (match-string 0 txt)
  15217. txt (substring txt 0 (match-beginning 0))))
  15218. (setq txt2 (read-string "Edit: " txt))
  15219. (when (not (equal txt txt2))
  15220. (beginning-of-line 1)
  15221. (insert pre txt2 post)
  15222. (delete-region (point) (point-at-eol))
  15223. (org-set-tags nil t)))))
  15224. (defun org-columns-edit-allowed ()
  15225. "Edit the list of allowed values for the current property."
  15226. (interactive)
  15227. (let* ((key (get-char-property (point) 'org-columns-key))
  15228. (key1 (concat key "_ALL"))
  15229. (allowed (org-entry-get (point) key1 t))
  15230. nval)
  15231. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  15232. (setq nval (read-string "Allowed: " allowed))
  15233. (org-entry-put
  15234. (cond ((marker-position org-entry-property-inherited-from)
  15235. org-entry-property-inherited-from)
  15236. ((marker-position org-columns-top-level-marker)
  15237. org-columns-top-level-marker))
  15238. key1 nval)))
  15239. (defmacro org-no-warnings (&rest body)
  15240. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  15241. (defun org-columns-eval (form)
  15242. (let (hidep)
  15243. (save-excursion
  15244. (beginning-of-line 1)
  15245. ;; `next-line' is needed here, because it skips invisible line.
  15246. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  15247. (setq hidep (org-on-heading-p 1)))
  15248. (eval form)
  15249. (and hidep (hide-entry))))
  15250. (defun org-columns-previous-allowed-value ()
  15251. "Switch to the previous allowed value for this column."
  15252. (interactive)
  15253. (org-columns-next-allowed-value t))
  15254. (defun org-columns-next-allowed-value (&optional previous)
  15255. "Switch to the next allowed value for this column."
  15256. (interactive)
  15257. (org-columns-check-computed)
  15258. (let* ((col (current-column))
  15259. (key (get-char-property (point) 'org-columns-key))
  15260. (value (get-char-property (point) 'org-columns-value))
  15261. (bol (point-at-bol)) (eol (point-at-eol))
  15262. (pom (or (get-text-property bol 'org-hd-marker)
  15263. (point))) ; keep despite of compiler waring
  15264. (line-overlays
  15265. (delq nil (mapcar (lambda (x)
  15266. (and (eq (overlay-buffer x) (current-buffer))
  15267. (>= (overlay-start x) bol)
  15268. (<= (overlay-start x) eol)
  15269. x))
  15270. org-columns-overlays)))
  15271. (allowed (or (org-property-get-allowed-values pom key)
  15272. (and (equal
  15273. (nth 4 (assoc key org-columns-current-fmt-compiled))
  15274. 'checkbox) '("[ ]" "[X]"))))
  15275. nval)
  15276. (when (equal key "ITEM")
  15277. (error "Cannot edit item headline from here"))
  15278. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  15279. (error "Allowed values for this property have not been defined"))
  15280. (if (member key '("SCHEDULED" "DEADLINE"))
  15281. (setq nval (if previous 'earlier 'later))
  15282. (if previous (setq allowed (reverse allowed)))
  15283. (if (member value allowed)
  15284. (setq nval (car (cdr (member value allowed)))))
  15285. (setq nval (or nval (car allowed)))
  15286. (if (equal nval value)
  15287. (error "Only one allowed value for this property")))
  15288. (let ((inhibit-read-only t))
  15289. (remove-text-properties (1- bol) eol '(read-only t))
  15290. (unwind-protect
  15291. (progn
  15292. (setq org-columns-overlays
  15293. (org-delete-all line-overlays org-columns-overlays))
  15294. (mapc 'org-delete-overlay line-overlays)
  15295. (org-columns-eval '(org-entry-put pom key nval)))
  15296. (org-columns-display-here)))
  15297. (move-to-column col)
  15298. (if (and (org-mode-p)
  15299. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  15300. (org-columns-update key))))
  15301. (defun org-verify-version (task)
  15302. (cond
  15303. ((eq task 'columns)
  15304. (if (or (featurep 'xemacs)
  15305. (< emacs-major-version 22))
  15306. (error "Emacs 22 is required for the columns feature")))))
  15307. (defun org-columns-open-link (&optional arg)
  15308. (interactive "P")
  15309. (let ((key (get-char-property (point) 'org-columns-key))
  15310. (value (get-char-property (point) 'org-columns-value)))
  15311. (org-open-link-from-string arg)))
  15312. (defun org-open-link-from-string (s &optional arg)
  15313. "Open a link in the string S, as if it was in Org-mode."
  15314. (interactive)
  15315. (with-temp-buffer
  15316. (let ((org-inhibit-startup t))
  15317. (org-mode)
  15318. (insert s)
  15319. (goto-char (point-min))
  15320. (org-open-at-point arg))))
  15321. (defun org-columns-get-format-and-top-level ()
  15322. (let (fmt)
  15323. (when (condition-case nil (org-back-to-heading) (error nil))
  15324. (move-marker org-entry-property-inherited-from nil)
  15325. (setq fmt (org-entry-get nil "COLUMNS" t)))
  15326. (setq fmt (or fmt org-columns-default-format))
  15327. (org-set-local 'org-columns-current-fmt fmt)
  15328. (org-columns-compile-format fmt)
  15329. (if (marker-position org-entry-property-inherited-from)
  15330. (move-marker org-columns-top-level-marker
  15331. org-entry-property-inherited-from)
  15332. (move-marker org-columns-top-level-marker (point)))
  15333. fmt))
  15334. (defun org-columns ()
  15335. "Turn on column view on an org-mode file."
  15336. (interactive)
  15337. (org-verify-version 'columns)
  15338. (org-columns-remove-overlays)
  15339. (move-marker org-columns-begin-marker (point))
  15340. (let (beg end fmt cache maxwidths clocksump)
  15341. (setq fmt (org-columns-get-format-and-top-level))
  15342. (save-excursion
  15343. (goto-char org-columns-top-level-marker)
  15344. (setq beg (point))
  15345. (unless org-columns-inhibit-recalculation
  15346. (org-columns-compute-all))
  15347. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  15348. (point-max)))
  15349. ;; Get and cache the properties
  15350. (goto-char beg)
  15351. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  15352. (setq clocksump t)
  15353. (save-excursion
  15354. (save-restriction
  15355. (narrow-to-region beg end)
  15356. (org-clock-sum))))
  15357. (while (re-search-forward (concat "^" outline-regexp) end t)
  15358. (push (cons (org-current-line) (org-entry-properties)) cache))
  15359. (when cache
  15360. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15361. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15362. (org-columns-display-here-title)
  15363. (mapc (lambda (x)
  15364. (goto-line (car x))
  15365. (org-columns-display-here (cdr x)))
  15366. cache)))))
  15367. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  15368. "Insert a new column, to the leeft o the current column."
  15369. (interactive)
  15370. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  15371. cell)
  15372. (setq prop (completing-read
  15373. "Property: " (mapcar 'list (org-buffer-property-keys t))
  15374. nil nil prop))
  15375. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  15376. (setq width (read-string "Column width: " (if width (number-to-string width))))
  15377. (if (string-match "\\S-" width)
  15378. (setq width (string-to-number width))
  15379. (setq width nil))
  15380. (setq fmt (completing-read "Summary [none]: "
  15381. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox"))
  15382. nil t))
  15383. (if (string-match "\\S-" fmt)
  15384. (setq fmt (intern fmt))
  15385. (setq fmt nil))
  15386. (if (eq fmt 'none) (setq fmt nil))
  15387. (if editp
  15388. (progn
  15389. (setcar editp prop)
  15390. (setcdr editp (list title width nil fmt)))
  15391. (setq cell (nthcdr (1- (current-column))
  15392. org-columns-current-fmt-compiled))
  15393. (setcdr cell (cons (list prop title width nil fmt)
  15394. (cdr cell))))
  15395. (org-columns-store-format)
  15396. (org-columns-redo)))
  15397. (defun org-columns-delete ()
  15398. "Delete the column at point from columns view."
  15399. (interactive)
  15400. (let* ((n (current-column))
  15401. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  15402. (when (y-or-n-p
  15403. (format "Are you sure you want to remove column \"%s\"? " title))
  15404. (setq org-columns-current-fmt-compiled
  15405. (delq (nth n org-columns-current-fmt-compiled)
  15406. org-columns-current-fmt-compiled))
  15407. (org-columns-store-format)
  15408. (org-columns-redo)
  15409. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  15410. (backward-char 1)))))
  15411. (defun org-columns-edit-attributes ()
  15412. "Edit the attributes of the current column."
  15413. (interactive)
  15414. (let* ((n (current-column))
  15415. (info (nth n org-columns-current-fmt-compiled)))
  15416. (apply 'org-columns-new info)))
  15417. (defun org-columns-widen (arg)
  15418. "Make the column wider by ARG characters."
  15419. (interactive "p")
  15420. (let* ((n (current-column))
  15421. (entry (nth n org-columns-current-fmt-compiled))
  15422. (width (or (nth 2 entry)
  15423. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  15424. (setq width (max 1 (+ width arg)))
  15425. (setcar (nthcdr 2 entry) width)
  15426. (org-columns-store-format)
  15427. (org-columns-redo)))
  15428. (defun org-columns-narrow (arg)
  15429. "Make the column nrrower by ARG characters."
  15430. (interactive "p")
  15431. (org-columns-widen (- arg)))
  15432. (defun org-columns-move-right ()
  15433. "Swap this column with the one to the right."
  15434. (interactive)
  15435. (let* ((n (current-column))
  15436. (cell (nthcdr n org-columns-current-fmt-compiled))
  15437. e)
  15438. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  15439. (error "Cannot shift this column further to the right"))
  15440. (setq e (car cell))
  15441. (setcar cell (car (cdr cell)))
  15442. (setcdr cell (cons e (cdr (cdr cell))))
  15443. (org-columns-store-format)
  15444. (org-columns-redo)
  15445. (forward-char 1)))
  15446. (defun org-columns-move-left ()
  15447. "Swap this column with the one to the left."
  15448. (interactive)
  15449. (let* ((n (current-column)))
  15450. (when (= n 0)
  15451. (error "Cannot shift this column further to the left"))
  15452. (backward-char 1)
  15453. (org-columns-move-right)
  15454. (backward-char 1)))
  15455. (defun org-columns-store-format ()
  15456. "Store the text version of the current columns format in appropriate place.
  15457. This is either in the COLUMNS property of the node starting the current column
  15458. display, or in the #+COLUMNS line of the current buffer."
  15459. (let (fmt (cnt 0))
  15460. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  15461. (org-set-local 'org-columns-current-fmt fmt)
  15462. (if (marker-position org-columns-top-level-marker)
  15463. (save-excursion
  15464. (goto-char org-columns-top-level-marker)
  15465. (if (and (org-at-heading-p)
  15466. (org-entry-get nil "COLUMNS"))
  15467. (org-entry-put nil "COLUMNS" fmt)
  15468. (goto-char (point-min))
  15469. ;; Overwrite all #+COLUMNS lines....
  15470. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  15471. (setq cnt (1+ cnt))
  15472. (replace-match (concat "#+COLUMNS: " fmt) t t))
  15473. (unless (> cnt 0)
  15474. (goto-char (point-min))
  15475. (or (org-on-heading-p t) (outline-next-heading))
  15476. (let ((inhibit-read-only t))
  15477. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  15478. (org-set-local 'org-columns-default-format fmt))))))
  15479. (defvar org-overriding-columns-format nil
  15480. "When set, overrides any other definition.")
  15481. (defvar org-agenda-view-columns-initially nil
  15482. "When set, switch to columns view immediately after creating the agenda.")
  15483. (defun org-agenda-columns ()
  15484. "Turn on column view in the agenda."
  15485. (interactive)
  15486. (org-verify-version 'columns)
  15487. (org-columns-remove-overlays)
  15488. (move-marker org-columns-begin-marker (point))
  15489. (let (fmt cache maxwidths m)
  15490. (cond
  15491. ((and (local-variable-p 'org-overriding-columns-format)
  15492. org-overriding-columns-format)
  15493. (setq fmt org-overriding-columns-format))
  15494. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  15495. (setq fmt (org-entry-get m "COLUMNS" t)))
  15496. ((and (boundp 'org-columns-current-fmt)
  15497. (local-variable-p 'org-columns-current-fmt)
  15498. org-columns-current-fmt)
  15499. (setq fmt org-columns-current-fmt))
  15500. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  15501. (setq m (get-text-property m 'org-hd-marker))
  15502. (setq fmt (org-entry-get m "COLUMNS" t))))
  15503. (setq fmt (or fmt org-columns-default-format))
  15504. (org-set-local 'org-columns-current-fmt fmt)
  15505. (org-columns-compile-format fmt)
  15506. (save-excursion
  15507. ;; Get and cache the properties
  15508. (goto-char (point-min))
  15509. (while (not (eobp))
  15510. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  15511. (get-text-property (point) 'org-marker)))
  15512. (push (cons (org-current-line) (org-entry-properties m)) cache))
  15513. (beginning-of-line 2))
  15514. (when cache
  15515. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  15516. (org-set-local 'org-columns-current-maxwidths maxwidths)
  15517. (org-columns-display-here-title)
  15518. (mapc (lambda (x)
  15519. (goto-line (car x))
  15520. (org-columns-display-here (cdr x)))
  15521. cache)))))
  15522. (defun org-columns-get-autowidth-alist (s cache)
  15523. "Derive the maximum column widths from the format and the cache."
  15524. (let ((start 0) rtn)
  15525. (while (string-match (org-re "%\\([[:alpha:]]\\S-*\\)") s start)
  15526. (push (cons (match-string 1 s) 1) rtn)
  15527. (setq start (match-end 0)))
  15528. (mapc (lambda (x)
  15529. (setcdr x (apply 'max
  15530. (mapcar
  15531. (lambda (y)
  15532. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  15533. cache))))
  15534. rtn)
  15535. rtn))
  15536. (defun org-columns-compute-all ()
  15537. "Compute all columns that have operators defined."
  15538. (org-unmodified
  15539. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  15540. (let ((columns org-columns-current-fmt-compiled) col)
  15541. (while (setq col (pop columns))
  15542. (when (nth 3 col)
  15543. (save-excursion
  15544. (org-columns-compute (car col)))))))
  15545. (defun org-columns-update (property)
  15546. "Recompute PROPERTY, and update the columns display for it."
  15547. (org-columns-compute property)
  15548. (let (fmt val pos)
  15549. (save-excursion
  15550. (mapc (lambda (ov)
  15551. (when (equal (org-overlay-get ov 'org-columns-key) property)
  15552. (setq pos (org-overlay-start ov))
  15553. (goto-char pos)
  15554. (when (setq val (cdr (assoc property
  15555. (get-text-property
  15556. (point-at-bol) 'org-summaries))))
  15557. (setq fmt (org-overlay-get ov 'org-columns-format))
  15558. (org-overlay-put ov 'org-columns-value val)
  15559. (org-overlay-put ov 'display (format fmt val)))))
  15560. org-columns-overlays))))
  15561. (defun org-columns-compute (property)
  15562. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  15563. (interactive)
  15564. (let* ((re (concat "^" outline-regexp))
  15565. (lmax 30) ; Does anyone use deeper levels???
  15566. (lsum (make-vector lmax 0))
  15567. (lflag (make-vector lmax nil))
  15568. (level 0)
  15569. (ass (assoc property org-columns-current-fmt-compiled))
  15570. (format (nth 4 ass))
  15571. (printf (nth 5 ass))
  15572. (beg org-columns-top-level-marker)
  15573. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  15574. (save-excursion
  15575. ;; Find the region to compute
  15576. (goto-char beg)
  15577. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  15578. (goto-char end)
  15579. ;; Walk the tree from the back and do the computations
  15580. (while (re-search-backward re beg t)
  15581. (setq sumpos (match-beginning 0)
  15582. last-level level
  15583. level (org-outline-level)
  15584. val (org-entry-get nil property)
  15585. valflag (and val (string-match "\\S-" val)))
  15586. (cond
  15587. ((< level last-level)
  15588. ;; put the sum of lower levels here as a property
  15589. (setq sum (aref lsum last-level) ; current sum
  15590. flag (aref lflag last-level) ; any valid entries from children?
  15591. str (org-column-number-to-string sum format printf)
  15592. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  15593. useval (if flag str1 (if valflag val ""))
  15594. sum-alist (get-text-property sumpos 'org-summaries))
  15595. (if (assoc property sum-alist)
  15596. (setcdr (assoc property sum-alist) useval)
  15597. (push (cons property useval) sum-alist)
  15598. (org-unmodified
  15599. (add-text-properties sumpos (1+ sumpos)
  15600. (list 'org-summaries sum-alist))))
  15601. (when val
  15602. (org-entry-put nil property (if flag str val)))
  15603. ;; add current to current level accumulator
  15604. (when (or flag valflag)
  15605. (aset lsum level (+ (aref lsum level)
  15606. (if flag sum (org-column-string-to-number
  15607. (if flag str val) format))))
  15608. (aset lflag level t))
  15609. ;; clear accumulators for deeper levels
  15610. (loop for l from (1+ level) to (1- lmax) do
  15611. (aset lsum l 0)
  15612. (aset lflag l nil)))
  15613. ((>= level last-level)
  15614. ;; add what we have here to the accumulator for this level
  15615. (aset lsum level (+ (aref lsum level)
  15616. (org-column-string-to-number (or val "0") format)))
  15617. (and valflag (aset lflag level t)))
  15618. (t (error "This should not happen")))))))
  15619. (defun org-columns-redo ()
  15620. "Construct the column display again."
  15621. (interactive)
  15622. (message "Recomputing columns...")
  15623. (save-excursion
  15624. (if (marker-position org-columns-begin-marker)
  15625. (goto-char org-columns-begin-marker))
  15626. (org-columns-remove-overlays)
  15627. (if (org-mode-p)
  15628. (call-interactively 'org-columns)
  15629. (call-interactively 'org-agenda-columns)))
  15630. (message "Recomputing columns...done"))
  15631. (defun org-columns-not-in-agenda ()
  15632. (if (eq major-mode 'org-agenda-mode)
  15633. (error "This command is only allowed in Org-mode buffers")))
  15634. (defun org-string-to-number (s)
  15635. "Convert string to number, and interpret hh:mm:ss."
  15636. (if (not (string-match ":" s))
  15637. (string-to-number s)
  15638. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15639. (while l
  15640. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15641. sum)))
  15642. (defun org-column-number-to-string (n fmt &optional printf)
  15643. "Convert a computed column number to a string value, according to FMT."
  15644. (cond
  15645. ((eq fmt 'add_times)
  15646. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  15647. (format "%d:%02d" h m)))
  15648. ((eq fmt 'checkbox)
  15649. (cond ((= n (floor n)) "[X]")
  15650. ((> n 1.) "[-]")
  15651. (t "[ ]")))
  15652. (printf (format printf n))
  15653. ((eq fmt 'currency)
  15654. (format "%.2f" n))
  15655. (t (number-to-string n))))
  15656. (defun org-column-string-to-number (s fmt)
  15657. "Convert a column value to a number that can be used for column computing."
  15658. (cond
  15659. ((string-match ":" s)
  15660. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  15661. (while l
  15662. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  15663. sum))
  15664. ((eq fmt 'checkbox)
  15665. (if (equal s "[X]") 1. 0.000001))
  15666. (t (string-to-number s))))
  15667. (defun org-columns-uncompile-format (cfmt)
  15668. "Turn the compiled columns format back into a string representation."
  15669. (let ((rtn "") e s prop title op width fmt printf)
  15670. (while (setq e (pop cfmt))
  15671. (setq prop (car e)
  15672. title (nth 1 e)
  15673. width (nth 2 e)
  15674. op (nth 3 e)
  15675. fmt (nth 4 e)
  15676. printf (nth 5 e))
  15677. (cond
  15678. ((eq fmt 'add_times) (setq op ":"))
  15679. ((eq fmt 'checkbox) (setq op "X"))
  15680. ((eq fmt 'add_numbers) (setq op "+"))
  15681. ((eq fmt 'currency) (setq op "$")))
  15682. (if (and op printf) (setq op (concat op ";" printf)))
  15683. (if (equal title prop) (setq title nil))
  15684. (setq s (concat "%" (if width (number-to-string width))
  15685. prop
  15686. (if title (concat "(" title ")"))
  15687. (if op (concat "{" op "}"))))
  15688. (setq rtn (concat rtn " " s)))
  15689. (org-trim rtn)))
  15690. (defun org-columns-compile-format (fmt)
  15691. "Turn a column format string into an alist of specifications.
  15692. The alist has one entry for each column in the format. The elements of
  15693. that list are:
  15694. property the property
  15695. title the title field for the columns
  15696. width the column width in characters, can be nil for automatic
  15697. operator the operator if any
  15698. format the output format for computed results, derived from operator
  15699. printf a printf format for computed values"
  15700. (let ((start 0) width prop title op f printf)
  15701. (setq org-columns-current-fmt-compiled nil)
  15702. (while (string-match
  15703. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  15704. fmt start)
  15705. (setq start (match-end 0)
  15706. width (match-string 1 fmt)
  15707. prop (match-string 2 fmt)
  15708. title (or (match-string 3 fmt) prop)
  15709. op (match-string 4 fmt)
  15710. f nil
  15711. printf nil)
  15712. (if width (setq width (string-to-number width)))
  15713. (when (and op (string-match ";" op))
  15714. (setq printf (substring op (match-end 0))
  15715. op (substring op 0 (match-beginning 0))))
  15716. (cond
  15717. ((equal op "+") (setq f 'add_numbers))
  15718. ((equal op "$") (setq f 'currency))
  15719. ((equal op ":") (setq f 'add_times))
  15720. ((equal op "X") (setq f 'checkbox)))
  15721. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  15722. (setq org-columns-current-fmt-compiled
  15723. (nreverse org-columns-current-fmt-compiled))))
  15724. ;;; Dynamic block for Column view
  15725. (defun org-columns-capture-view ()
  15726. "Get the column view of the current buffer and return it as a list.
  15727. The list will contains the title row and all other rows. Each row is
  15728. a list of fields."
  15729. (save-excursion
  15730. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  15731. (n (length title)) row tbl)
  15732. (goto-char (point-min))
  15733. (while (re-search-forward "^\\*+ " nil t)
  15734. (when (get-char-property (match-beginning 0) 'org-columns-key)
  15735. (setq row nil)
  15736. (loop for i from 0 to (1- n) do
  15737. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  15738. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  15739. "")
  15740. row))
  15741. (setq row (nreverse row))
  15742. (push row tbl)))
  15743. (append (list title 'hline) (nreverse tbl)))))
  15744. (defun org-dblock-write:columnview (params)
  15745. "Write the column view table.
  15746. PARAMS is a property list of parameters:
  15747. :width enforce same column widths with <N> specifiers.
  15748. :id the :ID: property of the entry where the columns view
  15749. should be built, as a string. When `local', call locally.
  15750. When `global' call column view with the cursor at the beginning
  15751. of the buffer (usually this means that the whole buffer switches
  15752. to column view).
  15753. :hlines When t, insert a hline before each item. When a number, insert
  15754. a hline before each level <= that number.
  15755. :vlines When t, make each column a colgroup to enforce vertical lines."
  15756. (let ((pos (move-marker (make-marker) (point)))
  15757. (hlines (plist-get params :hlines))
  15758. (vlines (plist-get params :vlines))
  15759. tbl id idpos nfields tmp)
  15760. (save-excursion
  15761. (save-restriction
  15762. (when (setq id (plist-get params :id))
  15763. (cond ((not id) nil)
  15764. ((eq id 'global) (goto-char (point-min)))
  15765. ((eq id 'local) nil)
  15766. ((setq idpos (org-find-entry-with-id id))
  15767. (goto-char idpos))
  15768. (t (error "Cannot find entry with :ID: %s" id))))
  15769. (org-columns)
  15770. (setq tbl (org-columns-capture-view))
  15771. (setq nfields (length (car tbl)))
  15772. (org-columns-quit)))
  15773. (goto-char pos)
  15774. (move-marker pos nil)
  15775. (when tbl
  15776. (when (plist-get params :hlines)
  15777. (setq tmp nil)
  15778. (while tbl
  15779. (if (eq (car tbl) 'hline)
  15780. (push (pop tbl) tmp)
  15781. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  15782. (if (and (not (eq (car tmp) 'hline))
  15783. (or (eq hlines t)
  15784. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  15785. (push 'hline tmp)))
  15786. (push (pop tbl) tmp)))
  15787. (setq tbl (nreverse tmp)))
  15788. (when vlines
  15789. (setq tbl (mapcar (lambda (x)
  15790. (if (eq 'hline x) x (cons "" x)))
  15791. tbl))
  15792. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  15793. (setq pos (point))
  15794. (insert (org-listtable-to-string tbl))
  15795. (when (plist-get params :width)
  15796. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  15797. org-columns-current-widths "|")))
  15798. (goto-char pos)
  15799. (org-table-align))))
  15800. (defun org-listtable-to-string (tbl)
  15801. "Convert a listtable TBL to a string that contains the Org-mode table.
  15802. The table still need to be alligned. The resulting string has no leading
  15803. and tailing newline characters."
  15804. (mapconcat
  15805. (lambda (x)
  15806. (cond
  15807. ((listp x)
  15808. (concat "|" (mapconcat 'identity x "|") "|"))
  15809. ((eq x 'hline) "|-|")
  15810. (t (error "Garbage in listtable: %s" x))))
  15811. tbl "\n"))
  15812. (defun org-insert-columns-dblock ()
  15813. "Create a dynamic block capturing a column view table."
  15814. (interactive)
  15815. (let ((defaults '(:name "columnview" :hlines 1))
  15816. (id (completing-read
  15817. "Capture columns (local, global, entry with :ID: property) [local]: "
  15818. (append '(("global") ("local"))
  15819. (mapcar 'list (org-property-values "ID"))))))
  15820. (if (equal id "") (setq id 'local))
  15821. (if (equal id "global") (setq id 'global))
  15822. (setq defaults (append defaults (list :id id)))
  15823. (org-create-dblock defaults)
  15824. (org-update-dblock)))
  15825. ;;;; Timestamps
  15826. (defvar org-last-changed-timestamp nil)
  15827. (defvar org-time-was-given) ; dynamically scoped parameter
  15828. (defvar org-end-time-was-given) ; dynamically scoped parameter
  15829. (defvar org-ts-what) ; dynamically scoped parameter
  15830. (defun org-time-stamp (arg)
  15831. "Prompt for a date/time and insert a time stamp.
  15832. If the user specifies a time like HH:MM, or if this command is called
  15833. with a prefix argument, the time stamp will contain date and time.
  15834. Otherwise, only the date will be included. All parts of a date not
  15835. specified by the user will be filled in from the current date/time.
  15836. So if you press just return without typing anything, the time stamp
  15837. will represent the current date/time. If there is already a timestamp
  15838. at the cursor, it will be modified."
  15839. (interactive "P")
  15840. (let* ((ts nil)
  15841. (default-time
  15842. ;; Default time is either today, or, when entering a range,
  15843. ;; the range start.
  15844. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  15845. (save-excursion
  15846. (re-search-backward
  15847. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  15848. (- (point) 20) t)))
  15849. (apply 'encode-time (org-parse-time-string (match-string 1)))
  15850. (current-time)))
  15851. (default-input (and ts (org-get-compact-tod ts)))
  15852. org-time-was-given org-end-time-was-given time)
  15853. (cond
  15854. ((and (org-at-timestamp-p)
  15855. (eq last-command 'org-time-stamp)
  15856. (eq this-command 'org-time-stamp))
  15857. (insert "--")
  15858. (setq time (let ((this-command this-command))
  15859. (org-read-date arg 'totime nil nil default-time default-input)))
  15860. (org-insert-time-stamp time (or org-time-was-given arg)))
  15861. ((org-at-timestamp-p)
  15862. (setq time (let ((this-command this-command))
  15863. (org-read-date arg 'totime nil nil default-time default-input)))
  15864. (when (org-at-timestamp-p) ; just to get the match data
  15865. (replace-match "")
  15866. (setq org-last-changed-timestamp
  15867. (org-insert-time-stamp
  15868. time (or org-time-was-given arg)
  15869. nil nil nil (list org-end-time-was-given))))
  15870. (message "Timestamp updated"))
  15871. (t
  15872. (setq time (let ((this-command this-command))
  15873. (org-read-date arg 'totime nil nil default-time default-input)))
  15874. (org-insert-time-stamp time (or org-time-was-given arg)
  15875. nil nil nil (list org-end-time-was-given))))))
  15876. ;; FIXME: can we use this for something else????
  15877. ;; like computing time differences?????
  15878. (defun org-get-compact-tod (s)
  15879. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15880. (let* ((t1 (match-string 1 s))
  15881. (h1 (string-to-number (match-string 2 s)))
  15882. (m1 (string-to-number (match-string 3 s)))
  15883. (t2 (and (match-end 4) (match-string 5 s)))
  15884. (h2 (and t2 (string-to-number (match-string 6 s))))
  15885. (m2 (and t2 (string-to-number (match-string 7 s))))
  15886. dh dm)
  15887. (if (not t2)
  15888. t1
  15889. (setq dh (- h2 h1) dm (- m2 m1))
  15890. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15891. (concat t1 "+" (number-to-string dh)
  15892. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  15893. (defun org-time-stamp-inactive (&optional arg)
  15894. "Insert an inactive time stamp.
  15895. An inactive time stamp is enclosed in square brackets instead of angle
  15896. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15897. does not link to the calendar and cannot be changed with the S-cursor keys.
  15898. So these are more for recording a certain time/date."
  15899. (interactive "P")
  15900. (let (org-time-was-given org-end-time-was-given time)
  15901. (setq time (org-read-date arg 'totime))
  15902. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  15903. nil nil (list org-end-time-was-given))))
  15904. (defvar org-date-ovl (org-make-overlay 1 1))
  15905. (org-overlay-put org-date-ovl 'face 'org-warning)
  15906. (org-detach-overlay org-date-ovl)
  15907. (defvar org-ans1) ; dynamically scoped parameter
  15908. (defvar org-ans2) ; dynamically scoped parameter
  15909. (defvar org-plain-time-of-day-regexp) ; defined below
  15910. (defvar org-read-date-overlay nil)
  15911. (defvar org-dcst nil) ; dynamically scoped
  15912. (defun org-read-date (&optional with-time to-time from-string prompt
  15913. default-time default-input)
  15914. "Read a date, possibly a time, and make things smooth for the user.
  15915. The prompt will suggest to enter an ISO date, but you can also enter anything
  15916. which will at least partially be understood by `parse-time-string'.
  15917. Unrecognized parts of the date will default to the current day, month, year,
  15918. hour and minute. If this command is called to replace a timestamp at point,
  15919. of to enter the second timestamp of a range, the default time is taken from the
  15920. existing stamp. For example,
  15921. 3-2-5 --> 2003-02-05
  15922. feb 15 --> currentyear-02-15
  15923. sep 12 9 --> 2009-09-12
  15924. 12:45 --> today 12:45
  15925. 22 sept 0:34 --> currentyear-09-22 0:34
  15926. 12 --> currentyear-currentmonth-12
  15927. Fri --> nearest Friday (today or later)
  15928. etc.
  15929. Furthermore you can specify a relative date by giving, as the *first* thing
  15930. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  15931. change in days weeks, months, years.
  15932. With a single plus or minus, the date is relative to today. With a double
  15933. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15934. +4d --> four days from today
  15935. +4 --> same as above
  15936. +2w --> two weeks from today
  15937. ++5 --> five days from default date
  15938. The function understands only English month and weekday abbreviations,
  15939. but this can be configured with the variables `parse-time-months' and
  15940. `parse-time-weekdays'.
  15941. While prompting, a calendar is popped up - you can also select the
  15942. date with the mouse (button 1). The calendar shows a period of three
  15943. months. To scroll it to other months, use the keys `>' and `<'.
  15944. If you don't like the calendar, turn it off with
  15945. \(setq org-read-date-popup-calendar nil)
  15946. With optional argument TO-TIME, the date will immediately be converted
  15947. to an internal time.
  15948. With an optional argument WITH-TIME, the prompt will suggest to also
  15949. insert a time. Note that when WITH-TIME is not set, you can still
  15950. enter a time, and this function will inform the calling routine about
  15951. this change. The calling routine may then choose to change the format
  15952. used to insert the time stamp into the buffer to include the time.
  15953. With optional argument FROM-STRING, read from this string instead from
  15954. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15955. the time/date that is used for everything that is not specified by the
  15956. user."
  15957. (require 'parse-time)
  15958. (let* ((org-time-stamp-rounding-minutes
  15959. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  15960. (org-dcst org-display-custom-times)
  15961. (ct (org-current-time))
  15962. (def (or default-time ct))
  15963. (defdecode (decode-time def))
  15964. (dummy (progn
  15965. (when (< (nth 2 defdecode) org-extend-today-until)
  15966. (setcar (nthcdr 2 defdecode) -1)
  15967. (setcar (nthcdr 1 defdecode) 59)
  15968. (setq def (apply 'encode-time defdecode)
  15969. defdecode (decode-time def)))))
  15970. (calendar-move-hook nil)
  15971. (view-diary-entries-initially nil)
  15972. (view-calendar-holidays-initially nil)
  15973. (timestr (format-time-string
  15974. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  15975. (prompt (concat (if prompt (concat prompt " ") "")
  15976. (format "Date+time [%s]: " timestr)))
  15977. ans (org-ans0 "") org-ans1 org-ans2 final)
  15978. (cond
  15979. (from-string (setq ans from-string))
  15980. (org-read-date-popup-calendar
  15981. (save-excursion
  15982. (save-window-excursion
  15983. (calendar)
  15984. (calendar-forward-day (- (time-to-days def)
  15985. (calendar-absolute-from-gregorian
  15986. (calendar-current-date))))
  15987. (org-eval-in-calendar nil t)
  15988. (let* ((old-map (current-local-map))
  15989. (map (copy-keymap calendar-mode-map))
  15990. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  15991. (org-defkey map (kbd "RET") 'org-calendar-select)
  15992. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  15993. 'org-calendar-select-mouse)
  15994. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  15995. 'org-calendar-select-mouse)
  15996. (org-defkey minibuffer-local-map [(meta shift left)]
  15997. (lambda () (interactive)
  15998. (org-eval-in-calendar '(calendar-backward-month 1))))
  15999. (org-defkey minibuffer-local-map [(meta shift right)]
  16000. (lambda () (interactive)
  16001. (org-eval-in-calendar '(calendar-forward-month 1))))
  16002. (org-defkey minibuffer-local-map [(meta shift up)]
  16003. (lambda () (interactive)
  16004. (org-eval-in-calendar '(calendar-backward-year 1))))
  16005. (org-defkey minibuffer-local-map [(meta shift down)]
  16006. (lambda () (interactive)
  16007. (org-eval-in-calendar '(calendar-forward-year 1))))
  16008. (org-defkey minibuffer-local-map [(shift up)]
  16009. (lambda () (interactive)
  16010. (org-eval-in-calendar '(calendar-backward-week 1))))
  16011. (org-defkey minibuffer-local-map [(shift down)]
  16012. (lambda () (interactive)
  16013. (org-eval-in-calendar '(calendar-forward-week 1))))
  16014. (org-defkey minibuffer-local-map [(shift left)]
  16015. (lambda () (interactive)
  16016. (org-eval-in-calendar '(calendar-backward-day 1))))
  16017. (org-defkey minibuffer-local-map [(shift right)]
  16018. (lambda () (interactive)
  16019. (org-eval-in-calendar '(calendar-forward-day 1))))
  16020. (org-defkey minibuffer-local-map ">"
  16021. (lambda () (interactive)
  16022. (org-eval-in-calendar '(scroll-calendar-left 1))))
  16023. (org-defkey minibuffer-local-map "<"
  16024. (lambda () (interactive)
  16025. (org-eval-in-calendar '(scroll-calendar-right 1))))
  16026. (unwind-protect
  16027. (progn
  16028. (use-local-map map)
  16029. (add-hook 'post-command-hook 'org-read-date-display)
  16030. (setq org-ans0 (read-string prompt default-input nil nil))
  16031. ;; org-ans0: from prompt
  16032. ;; org-ans1: from mouse click
  16033. ;; org-ans2: from calendar motion
  16034. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  16035. (remove-hook 'post-command-hook 'org-read-date-display)
  16036. (use-local-map old-map)
  16037. (when org-read-date-overlay
  16038. (org-delete-overlay org-read-date-overlay)
  16039. (setq org-read-date-overlay nil)))))))
  16040. (t ; Naked prompt only
  16041. (unwind-protect
  16042. (setq ans (read-string prompt default-input nil timestr))
  16043. (when org-read-date-overlay
  16044. (org-delete-overlay org-read-date-overlay)
  16045. (setq org-read-date-overlay nil)))))
  16046. (setq final (org-read-date-analyze ans def defdecode))
  16047. (if to-time
  16048. (apply 'encode-time final)
  16049. (if (and (boundp 'org-time-was-given) org-time-was-given)
  16050. (format "%04d-%02d-%02d %02d:%02d"
  16051. (nth 5 final) (nth 4 final) (nth 3 final)
  16052. (nth 2 final) (nth 1 final))
  16053. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  16054. (defvar def)
  16055. (defvar defdecode)
  16056. (defvar with-time)
  16057. (defun org-read-date-display ()
  16058. "Display the currrent date prompt interpretation in the minibuffer."
  16059. (when org-read-date-display-live
  16060. (when org-read-date-overlay
  16061. (org-delete-overlay org-read-date-overlay))
  16062. (let ((p (point)))
  16063. (end-of-line 1)
  16064. (while (not (equal (buffer-substring
  16065. (max (point-min) (- (point) 4)) (point))
  16066. " "))
  16067. (insert " "))
  16068. (goto-char p))
  16069. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  16070. " " (or org-ans1 org-ans2)))
  16071. (org-end-time-was-given nil)
  16072. (f (org-read-date-analyze ans def defdecode))
  16073. (fmts (if org-dcst
  16074. org-time-stamp-custom-formats
  16075. org-time-stamp-formats))
  16076. (fmt (if (or with-time
  16077. (and (boundp 'org-time-was-given) org-time-was-given))
  16078. (cdr fmts)
  16079. (car fmts)))
  16080. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  16081. (when (and org-end-time-was-given
  16082. (string-match org-plain-time-of-day-regexp txt))
  16083. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  16084. org-end-time-was-given
  16085. (substring txt (match-end 0)))))
  16086. (setq org-read-date-overlay
  16087. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  16088. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  16089. (defun org-read-date-analyze (ans def defdecode)
  16090. "Analyze the combined answer of the date prompt."
  16091. ;; FIXME: cleanup and comment
  16092. (let (delta deltan deltaw deltadef year month day
  16093. hour minute second wday pm h2 m2 tl wday1)
  16094. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  16095. (setq ans (replace-match "" t t ans)
  16096. deltan (car delta)
  16097. deltaw (nth 1 delta)
  16098. deltadef (nth 2 delta)))
  16099. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  16100. (when (string-match
  16101. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  16102. (setq year (if (match-end 2)
  16103. (string-to-number (match-string 2 ans))
  16104. (string-to-number (format-time-string "%Y")))
  16105. month (string-to-number (match-string 3 ans))
  16106. day (string-to-number (match-string 4 ans)))
  16107. (if (< year 100) (setq year (+ 2000 year)))
  16108. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  16109. t nil ans)))
  16110. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  16111. ;; If there is a time with am/pm, and *no* time without it, we convert
  16112. ;; so that matching will be successful.
  16113. (loop for i from 1 to 2 do ; twice, for end time as well
  16114. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  16115. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  16116. (setq hour (string-to-number (match-string 1 ans))
  16117. minute (if (match-end 3)
  16118. (string-to-number (match-string 3 ans))
  16119. 0)
  16120. pm (equal ?p
  16121. (string-to-char (downcase (match-string 4 ans)))))
  16122. (if (and (= hour 12) (not pm))
  16123. (setq hour 0)
  16124. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  16125. (setq ans (replace-match (format "%02d:%02d" hour minute)
  16126. t t ans))))
  16127. ;; Check if a time range is given as a duration
  16128. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  16129. (setq hour (string-to-number (match-string 1 ans))
  16130. h2 (+ hour (string-to-number (match-string 3 ans)))
  16131. minute (string-to-number (match-string 2 ans))
  16132. m2 (+ minute (if (match-end 5) (string-to-number (match-string 5 ans))0)))
  16133. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  16134. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2) t t ans)))
  16135. ;; Check if there is a time range
  16136. (when (boundp 'org-end-time-was-given)
  16137. (setq org-time-was-given nil)
  16138. (when (and (string-match org-plain-time-of-day-regexp ans)
  16139. (match-end 8))
  16140. (setq org-end-time-was-given (match-string 8 ans))
  16141. (setq ans (concat (substring ans 0 (match-beginning 7))
  16142. (substring ans (match-end 7))))))
  16143. (setq tl (parse-time-string ans)
  16144. day (or (nth 3 tl) (nth 3 defdecode))
  16145. month (or (nth 4 tl)
  16146. (if (and org-read-date-prefer-future
  16147. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  16148. (1+ (nth 4 defdecode))
  16149. (nth 4 defdecode)))
  16150. year (or (nth 5 tl)
  16151. (if (and org-read-date-prefer-future
  16152. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  16153. (1+ (nth 5 defdecode))
  16154. (nth 5 defdecode)))
  16155. hour (or (nth 2 tl) (nth 2 defdecode))
  16156. minute (or (nth 1 tl) (nth 1 defdecode))
  16157. second (or (nth 0 tl) 0)
  16158. wday (nth 6 tl))
  16159. (when deltan
  16160. (unless deltadef
  16161. (let ((now (decode-time (current-time))))
  16162. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  16163. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  16164. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  16165. ((equal deltaw "m") (setq month (+ month deltan)))
  16166. ((equal deltaw "y") (setq year (+ year deltan)))))
  16167. (when (and wday (not (nth 3 tl)))
  16168. ;; Weekday was given, but no day, so pick that day in the week
  16169. ;; on or after the derived date.
  16170. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  16171. (unless (equal wday wday1)
  16172. (setq day (+ day (% (- wday wday1 -7) 7)))))
  16173. (if (and (boundp 'org-time-was-given)
  16174. (nth 2 tl))
  16175. (setq org-time-was-given t))
  16176. (if (< year 100) (setq year (+ 2000 year)))
  16177. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  16178. (list second minute hour day month year)))
  16179. (defvar parse-time-weekdays)
  16180. (defun org-read-date-get-relative (s today default)
  16181. "Check string S for special relative date string.
  16182. TODAY and DEFAULT are internal times, for today and for a default.
  16183. Return shift list (N what def-flag)
  16184. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  16185. N is the number of WHATs to shift.
  16186. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  16187. the DEFAULT date rather than TODAY."
  16188. (when (string-match
  16189. (concat
  16190. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  16191. "\\([0-9]+\\)?"
  16192. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  16193. "\\([ \t]\\|$\\)") s)
  16194. (let* ((dir (if (match-end 1)
  16195. (string-to-char (substring (match-string 1 s) -1))
  16196. ?+))
  16197. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  16198. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  16199. (what (if (match-end 3) (match-string 3 s) "d"))
  16200. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  16201. (date (if rel default today))
  16202. (wday (nth 6 (decode-time date)))
  16203. delta)
  16204. (if wday1
  16205. (progn
  16206. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  16207. (if (= dir ?-) (setq delta (- delta 7)))
  16208. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  16209. (list delta "d" rel))
  16210. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  16211. (defun org-eval-in-calendar (form &optional keepdate)
  16212. "Eval FORM in the calendar window and return to current window.
  16213. Also, store the cursor date in variable org-ans2."
  16214. (let ((sw (selected-window)))
  16215. (select-window (get-buffer-window "*Calendar*"))
  16216. (eval form)
  16217. (when (and (not keepdate) (calendar-cursor-to-date))
  16218. (let* ((date (calendar-cursor-to-date))
  16219. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16220. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  16221. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  16222. (select-window sw)))
  16223. ; ;; Update the prompt to show new default date
  16224. ; (save-excursion
  16225. ; (goto-char (point-min))
  16226. ; (when (and org-ans2
  16227. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  16228. ; (get-text-property (match-end 0) 'field))
  16229. ; (let ((inhibit-read-only t))
  16230. ; (replace-match (concat "[" org-ans2 "]") t t)
  16231. ; (add-text-properties (point-min) (1+ (match-end 0))
  16232. ; (text-properties-at (1+ (point-min)))))))))
  16233. (defun org-calendar-select ()
  16234. "Return to `org-read-date' with the date currently selected.
  16235. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16236. (interactive)
  16237. (when (calendar-cursor-to-date)
  16238. (let* ((date (calendar-cursor-to-date))
  16239. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16240. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16241. (if (active-minibuffer-window) (exit-minibuffer))))
  16242. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  16243. "Insert a date stamp for the date given by the internal TIME.
  16244. WITH-HM means, use the stamp format that includes the time of the day.
  16245. INACTIVE means use square brackets instead of angular ones, so that the
  16246. stamp will not contribute to the agenda.
  16247. PRE and POST are optional strings to be inserted before and after the
  16248. stamp.
  16249. The command returns the inserted time stamp."
  16250. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  16251. stamp)
  16252. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  16253. (insert-before-markers (or pre ""))
  16254. (insert-before-markers (setq stamp (format-time-string fmt time)))
  16255. (when (listp extra)
  16256. (setq extra (car extra))
  16257. (if (and (stringp extra)
  16258. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  16259. (setq extra (format "-%02d:%02d"
  16260. (string-to-number (match-string 1 extra))
  16261. (string-to-number (match-string 2 extra))))
  16262. (setq extra nil)))
  16263. (when extra
  16264. (backward-char 1)
  16265. (insert-before-markers extra)
  16266. (forward-char 1))
  16267. (insert-before-markers (or post ""))
  16268. stamp))
  16269. (defun org-toggle-time-stamp-overlays ()
  16270. "Toggle the use of custom time stamp formats."
  16271. (interactive)
  16272. (setq org-display-custom-times (not org-display-custom-times))
  16273. (unless org-display-custom-times
  16274. (let ((p (point-min)) (bmp (buffer-modified-p)))
  16275. (while (setq p (next-single-property-change p 'display))
  16276. (if (and (get-text-property p 'display)
  16277. (eq (get-text-property p 'face) 'org-date))
  16278. (remove-text-properties
  16279. p (setq p (next-single-property-change p 'display))
  16280. '(display t))))
  16281. (set-buffer-modified-p bmp)))
  16282. (if (featurep 'xemacs)
  16283. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  16284. (org-restart-font-lock)
  16285. (setq org-table-may-need-update t)
  16286. (if org-display-custom-times
  16287. (message "Time stamps are overlayed with custom format")
  16288. (message "Time stamp overlays removed")))
  16289. (defun org-display-custom-time (beg end)
  16290. "Overlay modified time stamp format over timestamp between BED and END."
  16291. (let* ((ts (buffer-substring beg end))
  16292. t1 w1 with-hm tf time str w2 (off 0))
  16293. (save-match-data
  16294. (setq t1 (org-parse-time-string ts t))
  16295. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts)
  16296. (setq off (- (match-end 0) (match-beginning 0)))))
  16297. (setq end (- end off))
  16298. (setq w1 (- end beg)
  16299. with-hm (and (nth 1 t1) (nth 2 t1))
  16300. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  16301. time (org-fix-decoded-time t1)
  16302. str (org-add-props
  16303. (format-time-string
  16304. (substring tf 1 -1) (apply 'encode-time time))
  16305. nil 'mouse-face 'highlight)
  16306. w2 (length str))
  16307. (if (not (= w2 w1))
  16308. (add-text-properties (1+ beg) (+ 2 beg)
  16309. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  16310. (if (featurep 'xemacs)
  16311. (progn
  16312. (put-text-property beg end 'invisible t)
  16313. (put-text-property beg end 'end-glyph (make-glyph str)))
  16314. (put-text-property beg end 'display str))))
  16315. (defun org-translate-time (string)
  16316. "Translate all timestamps in STRING to custom format.
  16317. But do this only if the variable `org-display-custom-times' is set."
  16318. (when org-display-custom-times
  16319. (save-match-data
  16320. (let* ((start 0)
  16321. (re org-ts-regexp-both)
  16322. t1 with-hm inactive tf time str beg end)
  16323. (while (setq start (string-match re string start))
  16324. (setq beg (match-beginning 0)
  16325. end (match-end 0)
  16326. t1 (save-match-data
  16327. (org-parse-time-string (substring string beg end) t))
  16328. with-hm (and (nth 1 t1) (nth 2 t1))
  16329. inactive (equal (substring string beg (1+ beg)) "[")
  16330. tf (funcall (if with-hm 'cdr 'car)
  16331. org-time-stamp-custom-formats)
  16332. time (org-fix-decoded-time t1)
  16333. str (format-time-string
  16334. (concat
  16335. (if inactive "[" "<") (substring tf 1 -1)
  16336. (if inactive "]" ">"))
  16337. (apply 'encode-time time))
  16338. string (replace-match str t t string)
  16339. start (+ start (length str)))))))
  16340. string)
  16341. (defun org-fix-decoded-time (time)
  16342. "Set 0 instead of nil for the first 6 elements of time.
  16343. Don't touch the rest."
  16344. (let ((n 0))
  16345. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16346. (defun org-days-to-time (timestamp-string)
  16347. "Difference between TIMESTAMP-STRING and now in days."
  16348. (- (time-to-days (org-time-string-to-time timestamp-string))
  16349. (time-to-days (current-time))))
  16350. (defun org-deadline-close (timestamp-string &optional ndays)
  16351. "Is the time in TIMESTAMP-STRING close to the current date?"
  16352. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16353. (and (< (org-days-to-time timestamp-string) ndays)
  16354. (not (org-entry-is-done-p))))
  16355. (defun org-get-wdays (ts)
  16356. "Get the deadline lead time appropriate for timestring TS."
  16357. (cond
  16358. ((<= org-deadline-warning-days 0)
  16359. ;; 0 or negative, enforce this value no matter what
  16360. (- org-deadline-warning-days))
  16361. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  16362. ;; lead time is specified.
  16363. (floor (* (string-to-number (match-string 1 ts))
  16364. (cdr (assoc (match-string 2 ts)
  16365. '(("d" . 1) ("w" . 7)
  16366. ("m" . 30.4) ("y" . 365.25)))))))
  16367. ;; go for the default.
  16368. (t org-deadline-warning-days)))
  16369. (defun org-calendar-select-mouse (ev)
  16370. "Return to `org-read-date' with the date currently selected.
  16371. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16372. (interactive "e")
  16373. (mouse-set-point ev)
  16374. (when (calendar-cursor-to-date)
  16375. (let* ((date (calendar-cursor-to-date))
  16376. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16377. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16378. (if (active-minibuffer-window) (exit-minibuffer))))
  16379. (defun org-check-deadlines (ndays)
  16380. "Check if there are any deadlines due or past due.
  16381. A deadline is considered due if it happens within `org-deadline-warning-days'
  16382. days from today's date. If the deadline appears in an entry marked DONE,
  16383. it is not shown. The prefix arg NDAYS can be used to test that many
  16384. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16385. (interactive "P")
  16386. (let* ((org-warn-days
  16387. (cond
  16388. ((equal ndays '(4)) 100000)
  16389. (ndays (prefix-numeric-value ndays))
  16390. (t (abs org-deadline-warning-days))))
  16391. (case-fold-search nil)
  16392. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16393. (callback
  16394. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16395. (message "%d deadlines past-due or due within %d days"
  16396. (org-occur regexp nil callback)
  16397. org-warn-days)))
  16398. (defun org-check-before-date (date)
  16399. "Check if there are deadlines or scheduled entries before DATE."
  16400. (interactive (list (org-read-date)))
  16401. (let ((case-fold-search nil)
  16402. (regexp (concat "\\<\\(" org-deadline-string
  16403. "\\|" org-scheduled-string
  16404. "\\) *<\\([^>]+\\)>"))
  16405. (callback
  16406. (lambda () (time-less-p
  16407. (org-time-string-to-time (match-string 2))
  16408. (org-time-string-to-time date)))))
  16409. (message "%d entries before %s"
  16410. (org-occur regexp nil callback) date)))
  16411. (defun org-evaluate-time-range (&optional to-buffer)
  16412. "Evaluate a time range by computing the difference between start and end.
  16413. Normally the result is just printed in the echo area, but with prefix arg
  16414. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16415. If the time range is actually in a table, the result is inserted into the
  16416. next column.
  16417. For time difference computation, a year is assumed to be exactly 365
  16418. days in order to avoid rounding problems."
  16419. (interactive "P")
  16420. (or
  16421. (org-clock-update-time-maybe)
  16422. (save-excursion
  16423. (unless (org-at-date-range-p t)
  16424. (goto-char (point-at-bol))
  16425. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16426. (if (not (org-at-date-range-p t))
  16427. (error "Not at a time-stamp range, and none found in current line")))
  16428. (let* ((ts1 (match-string 1))
  16429. (ts2 (match-string 2))
  16430. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16431. (match-end (match-end 0))
  16432. (time1 (org-time-string-to-time ts1))
  16433. (time2 (org-time-string-to-time ts2))
  16434. (t1 (time-to-seconds time1))
  16435. (t2 (time-to-seconds time2))
  16436. (diff (abs (- t2 t1)))
  16437. (negative (< (- t2 t1) 0))
  16438. ;; (ys (floor (* 365 24 60 60)))
  16439. (ds (* 24 60 60))
  16440. (hs (* 60 60))
  16441. (fy "%dy %dd %02d:%02d")
  16442. (fy1 "%dy %dd")
  16443. (fd "%dd %02d:%02d")
  16444. (fd1 "%dd")
  16445. (fh "%02d:%02d")
  16446. y d h m align)
  16447. (if havetime
  16448. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16449. y 0
  16450. d (floor (/ diff ds)) diff (mod diff ds)
  16451. h (floor (/ diff hs)) diff (mod diff hs)
  16452. m (floor (/ diff 60)))
  16453. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16454. y 0
  16455. d (floor (+ (/ diff ds) 0.5))
  16456. h 0 m 0))
  16457. (if (not to-buffer)
  16458. (message "%s" (org-make-tdiff-string y d h m))
  16459. (if (org-at-table-p)
  16460. (progn
  16461. (goto-char match-end)
  16462. (setq align t)
  16463. (and (looking-at " *|") (goto-char (match-end 0))))
  16464. (goto-char match-end))
  16465. (if (looking-at
  16466. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16467. (replace-match ""))
  16468. (if negative (insert " -"))
  16469. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16470. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16471. (insert " " (format fh h m))))
  16472. (if align (org-table-align))
  16473. (message "Time difference inserted")))))
  16474. (defun org-make-tdiff-string (y d h m)
  16475. (let ((fmt "")
  16476. (l nil))
  16477. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16478. l (push y l)))
  16479. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16480. l (push d l)))
  16481. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16482. l (push h l)))
  16483. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16484. l (push m l)))
  16485. (apply 'format fmt (nreverse l))))
  16486. (defun org-time-string-to-time (s)
  16487. (apply 'encode-time (org-parse-time-string s)))
  16488. (defun org-time-string-to-absolute (s &optional daynr prefer)
  16489. "Convert a time stamp to an absolute day number.
  16490. If there is a specifyer for a cyclic time stamp, get the closest date to
  16491. DAYNR."
  16492. (cond
  16493. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16494. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16495. daynr
  16496. (+ daynr 1000)))
  16497. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  16498. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16499. (time-to-days (current-time))) (match-string 0 s)
  16500. prefer))
  16501. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  16502. (defun org-time-from-absolute (d)
  16503. "Return the time corresponding to date D.
  16504. D may be an absolute day number, or a calendar-type list (month day year)."
  16505. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16506. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16507. (defun org-calendar-holiday ()
  16508. "List of holidays, for Diary display in Org-mode."
  16509. (require 'holidays)
  16510. (let ((hl (funcall
  16511. (if (fboundp 'calendar-check-holidays)
  16512. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16513. (if hl (mapconcat 'identity hl "; "))))
  16514. (defun org-diary-sexp-entry (sexp entry date)
  16515. "Process a SEXP diary ENTRY for DATE."
  16516. (require 'diary-lib)
  16517. (let ((result (if calendar-debug-sexp
  16518. (let ((stack-trace-on-error t))
  16519. (eval (car (read-from-string sexp))))
  16520. (condition-case nil
  16521. (eval (car (read-from-string sexp)))
  16522. (error
  16523. (beep)
  16524. (message "Bad sexp at line %d in %s: %s"
  16525. (org-current-line)
  16526. (buffer-file-name) sexp)
  16527. (sleep-for 2))))))
  16528. (cond ((stringp result) result)
  16529. ((and (consp result)
  16530. (stringp (cdr result))) (cdr result))
  16531. (result entry)
  16532. (t nil))))
  16533. (defun org-diary-to-ical-string (frombuf)
  16534. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16535. This uses the icalendar.el library."
  16536. (let* ((tmpdir (if (featurep 'xemacs)
  16537. (temp-directory)
  16538. temporary-file-directory))
  16539. (tmpfile (make-temp-name
  16540. (expand-file-name "orgics" tmpdir)))
  16541. buf rtn b e)
  16542. (save-excursion
  16543. (set-buffer frombuf)
  16544. (icalendar-export-region (point-min) (point-max) tmpfile)
  16545. (setq buf (find-buffer-visiting tmpfile))
  16546. (set-buffer buf)
  16547. (goto-char (point-min))
  16548. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16549. (setq b (match-beginning 0)))
  16550. (goto-char (point-max))
  16551. (if (re-search-backward "^END:VEVENT" nil t)
  16552. (setq e (match-end 0)))
  16553. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16554. (kill-buffer buf)
  16555. (kill-buffer frombuf)
  16556. (delete-file tmpfile)
  16557. rtn))
  16558. (defun org-closest-date (start current change prefer)
  16559. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16560. When PREFER is `past' return a date that is either CURRENT or past.
  16561. When PREFER is `future', return a date that is either CURRENT or future."
  16562. ;; Make the proper lists from the dates
  16563. (catch 'exit
  16564. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  16565. dn dw sday cday n1 n2
  16566. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16567. (setq start (org-date-to-gregorian start)
  16568. current (org-date-to-gregorian
  16569. (if org-agenda-repeating-timestamp-show-all
  16570. current
  16571. (time-to-days (current-time))))
  16572. sday (calendar-absolute-from-gregorian start)
  16573. cday (calendar-absolute-from-gregorian current))
  16574. (if (<= cday sday) (throw 'exit sday))
  16575. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  16576. (setq dn (string-to-number (match-string 1 change))
  16577. dw (cdr (assoc (match-string 2 change) a1)))
  16578. (error "Invalid change specifyer: %s" change))
  16579. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16580. (cond
  16581. ((eq dw 'day)
  16582. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16583. n2 (+ n1 dn)))
  16584. ((eq dw 'year)
  16585. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16586. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16587. (setq date1 (list m d y1)
  16588. n1 (calendar-absolute-from-gregorian date1)
  16589. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16590. n2 (calendar-absolute-from-gregorian date2)))
  16591. ((eq dw 'month)
  16592. ;; approx number of month between the tow dates
  16593. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16594. ;; How often does dn fit in there?
  16595. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16596. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16597. m (+ m nm)
  16598. ny (floor (/ m 12))
  16599. y (+ y ny)
  16600. m (- m (* ny 12)))
  16601. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16602. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16603. (setq m2 (+ m dn) y2 y)
  16604. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16605. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16606. (while (< n2 cday)
  16607. (setq n1 n2 m m2 y y2)
  16608. (setq m2 (+ m dn) y2 y)
  16609. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16610. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16611. (if org-agenda-repeating-timestamp-show-all
  16612. (cond
  16613. ((eq prefer 'past) n1)
  16614. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16615. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16616. (cond
  16617. ((eq prefer 'past) n1)
  16618. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16619. (t (if (= cday n1) n1 n2)))))))
  16620. (defun org-date-to-gregorian (date)
  16621. "Turn any specification of DATE into a gregorian date for the calendar."
  16622. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16623. ((and (listp date) (= (length date) 3)) date)
  16624. ((stringp date)
  16625. (setq date (org-parse-time-string date))
  16626. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16627. ((listp date)
  16628. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16629. (defun org-parse-time-string (s &optional nodefault)
  16630. "Parse the standard Org-mode time string.
  16631. This should be a lot faster than the normal `parse-time-string'.
  16632. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16633. hour and minute fields will be nil if not given."
  16634. (if (string-match org-ts-regexp0 s)
  16635. (list 0
  16636. (if (or (match-beginning 8) (not nodefault))
  16637. (string-to-number (or (match-string 8 s) "0")))
  16638. (if (or (match-beginning 7) (not nodefault))
  16639. (string-to-number (or (match-string 7 s) "0")))
  16640. (string-to-number (match-string 4 s))
  16641. (string-to-number (match-string 3 s))
  16642. (string-to-number (match-string 2 s))
  16643. nil nil nil)
  16644. (make-list 9 0)))
  16645. (defun org-timestamp-up (&optional arg)
  16646. "Increase the date item at the cursor by one.
  16647. If the cursor is on the year, change the year. If it is on the month or
  16648. the day, change that.
  16649. With prefix ARG, change by that many units."
  16650. (interactive "p")
  16651. (org-timestamp-change (prefix-numeric-value arg)))
  16652. (defun org-timestamp-down (&optional arg)
  16653. "Decrease the date item at the cursor by one.
  16654. If the cursor is on the year, change the year. If it is on the month or
  16655. the day, change that.
  16656. With prefix ARG, change by that many units."
  16657. (interactive "p")
  16658. (org-timestamp-change (- (prefix-numeric-value arg))))
  16659. (defun org-timestamp-up-day (&optional arg)
  16660. "Increase the date in the time stamp by one day.
  16661. With prefix ARG, change that many days."
  16662. (interactive "p")
  16663. (if (and (not (org-at-timestamp-p t))
  16664. (org-on-heading-p))
  16665. (org-todo 'up)
  16666. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  16667. (defun org-timestamp-down-day (&optional arg)
  16668. "Decrease the date in the time stamp by one day.
  16669. With prefix ARG, change that many days."
  16670. (interactive "p")
  16671. (if (and (not (org-at-timestamp-p t))
  16672. (org-on-heading-p))
  16673. (org-todo 'down)
  16674. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  16675. (defsubst org-pos-in-match-range (pos n)
  16676. (and (match-beginning n)
  16677. (<= (match-beginning n) pos)
  16678. (>= (match-end n) pos)))
  16679. (defun org-at-timestamp-p (&optional inactive-ok)
  16680. "Determine if the cursor is in or at a timestamp."
  16681. (interactive)
  16682. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16683. (pos (point))
  16684. (ans (or (looking-at tsr)
  16685. (save-excursion
  16686. (skip-chars-backward "^[<\n\r\t")
  16687. (if (> (point) (point-min)) (backward-char 1))
  16688. (and (looking-at tsr)
  16689. (> (- (match-end 0) pos) -1))))))
  16690. (and ans
  16691. (boundp 'org-ts-what)
  16692. (setq org-ts-what
  16693. (cond
  16694. ((= pos (match-beginning 0)) 'bracket)
  16695. ((= pos (1- (match-end 0))) 'bracket)
  16696. ((org-pos-in-match-range pos 2) 'year)
  16697. ((org-pos-in-match-range pos 3) 'month)
  16698. ((org-pos-in-match-range pos 7) 'hour)
  16699. ((org-pos-in-match-range pos 8) 'minute)
  16700. ((or (org-pos-in-match-range pos 4)
  16701. (org-pos-in-match-range pos 5)) 'day)
  16702. ((and (> pos (or (match-end 8) (match-end 5)))
  16703. (< pos (match-end 0)))
  16704. (- pos (or (match-end 8) (match-end 5))))
  16705. (t 'day))))
  16706. ans))
  16707. (defun org-toggle-timestamp-type ()
  16708. ""
  16709. (interactive)
  16710. (when (org-at-timestamp-p t)
  16711. (save-excursion
  16712. (goto-char (match-beginning 0))
  16713. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  16714. (goto-char (1- (match-end 0)))
  16715. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  16716. (message "Timestamp is now %sactive"
  16717. (if (equal (char-before) ?>) "in" ""))))
  16718. (defun org-timestamp-change (n &optional what)
  16719. "Change the date in the time stamp at point.
  16720. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16721. `year', `minute', `second'. If WHAT is not given, the cursor position
  16722. in the timestamp determines what will be changed."
  16723. (let ((pos (point))
  16724. with-hm inactive
  16725. org-ts-what
  16726. extra
  16727. ts time time0)
  16728. (if (not (org-at-timestamp-p t))
  16729. (error "Not at a timestamp"))
  16730. (if (and (not what) (eq org-ts-what 'bracket))
  16731. (org-toggle-timestamp-type)
  16732. (if (and (not what) (not (eq org-ts-what 'day))
  16733. org-display-custom-times
  16734. (get-text-property (point) 'display)
  16735. (not (get-text-property (1- (point)) 'display)))
  16736. (setq org-ts-what 'day))
  16737. (setq org-ts-what (or what org-ts-what)
  16738. inactive (= (char-after (match-beginning 0)) ?\[)
  16739. ts (match-string 0))
  16740. (replace-match "")
  16741. (if (string-match
  16742. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( [-+][0-9]+[dwmy]\\)*\\)[]>]"
  16743. ts)
  16744. (setq extra (match-string 1 ts)))
  16745. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16746. (setq with-hm t))
  16747. (setq time0 (org-parse-time-string ts))
  16748. (setq time
  16749. (encode-time (or (car time0) 0)
  16750. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16751. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16752. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16753. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16754. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16755. (nthcdr 6 time0)))
  16756. (when (integerp org-ts-what)
  16757. (setq extra (org-modify-ts-extra extra org-ts-what n)))
  16758. (if (eq what 'calendar)
  16759. (let ((cal-date (org-get-date-from-calendar)))
  16760. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16761. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16762. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16763. (setcar time0 (or (car time0) 0))
  16764. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16765. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16766. (setq time (apply 'encode-time time0))))
  16767. (setq org-last-changed-timestamp
  16768. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16769. (org-clock-update-time-maybe)
  16770. (goto-char pos)
  16771. ;; Try to recenter the calendar window, if any
  16772. (if (and org-calendar-follow-timestamp-change
  16773. (get-buffer-window "*Calendar*" t)
  16774. (memq org-ts-what '(day month year)))
  16775. (org-recenter-calendar (time-to-days time))))))
  16776. ;; FIXME: does not yet work for lead times
  16777. (defun org-modify-ts-extra (s pos n)
  16778. "Change the different parts of the lead-time and repeat fields in timestamp."
  16779. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16780. ng h m new)
  16781. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( \\+\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16782. (cond
  16783. ((or (org-pos-in-match-range pos 2)
  16784. (org-pos-in-match-range pos 3))
  16785. (setq m (string-to-number (match-string 3 s))
  16786. h (string-to-number (match-string 2 s)))
  16787. (if (org-pos-in-match-range pos 2)
  16788. (setq h (+ h n))
  16789. (setq m (+ m n)))
  16790. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16791. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16792. (setq h (min 24 (max 0 h)))
  16793. (setq ng 1 new (format "-%02d:%02d" h m)))
  16794. ((org-pos-in-match-range pos 6)
  16795. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16796. ((org-pos-in-match-range pos 5)
  16797. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s))))))))
  16798. (when ng
  16799. (setq s (concat
  16800. (substring s 0 (match-beginning ng))
  16801. new
  16802. (substring s (match-end ng))))))
  16803. s))
  16804. (defun org-recenter-calendar (date)
  16805. "If the calendar is visible, recenter it to DATE."
  16806. (let* ((win (selected-window))
  16807. (cwin (get-buffer-window "*Calendar*" t))
  16808. (calendar-move-hook nil))
  16809. (when cwin
  16810. (select-window cwin)
  16811. (calendar-goto-date (if (listp date) date
  16812. (calendar-gregorian-from-absolute date)))
  16813. (select-window win))))
  16814. (defun org-goto-calendar (&optional arg)
  16815. "Go to the Emacs calendar at the current date.
  16816. If there is a time stamp in the current line, go to that date.
  16817. A prefix ARG can be used to force the current date."
  16818. (interactive "P")
  16819. (let ((tsr org-ts-regexp) diff
  16820. (calendar-move-hook nil)
  16821. (view-calendar-holidays-initially nil)
  16822. (view-diary-entries-initially nil))
  16823. (if (or (org-at-timestamp-p)
  16824. (save-excursion
  16825. (beginning-of-line 1)
  16826. (looking-at (concat ".*" tsr))))
  16827. (let ((d1 (time-to-days (current-time)))
  16828. (d2 (time-to-days
  16829. (org-time-string-to-time (match-string 1)))))
  16830. (setq diff (- d2 d1))))
  16831. (calendar)
  16832. (calendar-goto-today)
  16833. (if (and diff (not arg)) (calendar-forward-day diff))))
  16834. (defun org-get-date-from-calendar ()
  16835. "Return a list (month day year) of date at point in calendar."
  16836. (with-current-buffer "*Calendar*"
  16837. (save-match-data
  16838. (calendar-cursor-to-date))))
  16839. (defun org-date-from-calendar ()
  16840. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16841. If there is already a time stamp at the cursor position, update it."
  16842. (interactive)
  16843. (if (org-at-timestamp-p t)
  16844. (org-timestamp-change 0 'calendar)
  16845. (let ((cal-date (org-get-date-from-calendar)))
  16846. (org-insert-time-stamp
  16847. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16848. ;; Make appt aware of appointments from the agenda
  16849. ;;;###autoload
  16850. (defun org-agenda-to-appt (&optional filter)
  16851. "Activate appointments found in `org-agenda-files'.
  16852. When prefixed, prompt for a regular expression and use it as a
  16853. filter: only add entries if they match this regular expression.
  16854. FILTER can be a string. In this case, use this string as a
  16855. regular expression to filter results.
  16856. FILTER can also be an alist, with the car of each cell being
  16857. either 'headline or 'category. For example:
  16858. '((headline \"IMPORTANT\")
  16859. (category \"Work\"))
  16860. will only add headlines containing IMPORTANT or headlines
  16861. belonging to the category \"Work\"."
  16862. (interactive "P")
  16863. (require 'calendar)
  16864. (if (equal filter '(4))
  16865. (setq filter (read-from-minibuffer "Regexp filter: ")))
  16866. (let* ((cnt 0) ; count added events
  16867. (org-agenda-new-buffers nil)
  16868. (today (org-date-to-gregorian
  16869. (time-to-days (current-time))))
  16870. (files (org-agenda-files)) entries file)
  16871. ;; Get all entries which may contain an appt
  16872. (while (setq file (pop files))
  16873. (setq entries
  16874. (append entries
  16875. (org-agenda-get-day-entries
  16876. file today
  16877. :timestamp :scheduled :deadline))))
  16878. (setq entries (delq nil entries))
  16879. ;; Map thru entries and find if they pass thru the filter
  16880. (mapc
  16881. (lambda(x)
  16882. (let* ((evt (org-trim (get-text-property 1 'txt x)))
  16883. (cat (get-text-property 1 'org-category x))
  16884. (tod (get-text-property 1 'time-of-day x))
  16885. (ok (or (null filter)
  16886. (and (stringp filter) (string-match filter evt))
  16887. (and (listp filter)
  16888. (or (string-match
  16889. (cadr (assoc 'category filter)) cat)
  16890. (string-match
  16891. (cadr (assoc 'headline filter)) evt))))))
  16892. ;; FIXME: Shall we remove text-properties for the appt text?
  16893. ;; (setq evt (set-text-properties 0 (length evt) nil evt))
  16894. (when (and ok tod)
  16895. (setq tod (number-to-string tod)
  16896. tod (when (string-match
  16897. "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod)
  16898. (concat (match-string 1 tod) ":"
  16899. (match-string 2 tod))))
  16900. (appt-add tod evt)
  16901. (setq cnt (1+ cnt))))) entries)
  16902. (org-release-buffers org-agenda-new-buffers)
  16903. (message "Added %d event%s for today" cnt (if (> cnt 1) "s" ""))))
  16904. ;;; The clock for measuring work time.
  16905. (defvar org-mode-line-string "")
  16906. (put 'org-mode-line-string 'risky-local-variable t)
  16907. (defvar org-mode-line-timer nil)
  16908. (defvar org-clock-heading "")
  16909. (defvar org-clock-start-time "")
  16910. (defun org-update-mode-line ()
  16911. (let* ((delta (- (time-to-seconds (current-time))
  16912. (time-to-seconds org-clock-start-time)))
  16913. (h (floor delta 3600))
  16914. (m (floor (- delta (* 3600 h)) 60)))
  16915. (setq org-mode-line-string
  16916. (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading)
  16917. 'help-echo "Org-mode clock is running"))
  16918. (force-mode-line-update)))
  16919. (defvar org-clock-marker (make-marker)
  16920. "Marker recording the last clock-in.")
  16921. (defvar org-clock-mode-line-entry nil
  16922. "Information for the modeline about the running clock.")
  16923. (defun org-clock-in ()
  16924. "Start the clock on the current item.
  16925. If necessary, clock-out of the currently active clock."
  16926. (interactive)
  16927. (org-clock-out t)
  16928. (let (ts)
  16929. (save-excursion
  16930. (org-back-to-heading t)
  16931. (when (and org-clock-in-switch-to-state
  16932. (not (looking-at (concat outline-regexp "[ \t]*"
  16933. org-clock-in-switch-to-state
  16934. "\\>"))))
  16935. (org-todo org-clock-in-switch-to-state))
  16936. (if (and org-clock-heading-function
  16937. (functionp org-clock-heading-function))
  16938. (setq org-clock-heading (funcall org-clock-heading-function))
  16939. (if (looking-at org-complex-heading-regexp)
  16940. (setq org-clock-heading (match-string 4))
  16941. (setq org-clock-heading "???")))
  16942. (setq org-clock-heading (propertize org-clock-heading 'face nil))
  16943. (org-clock-find-position)
  16944. (insert "\n") (backward-char 1)
  16945. (indent-relative)
  16946. (insert org-clock-string " ")
  16947. (setq org-clock-start-time (current-time))
  16948. (setq ts (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  16949. (move-marker org-clock-marker (point) (buffer-base-buffer))
  16950. (or global-mode-string (setq global-mode-string '("")))
  16951. (or (memq 'org-mode-line-string global-mode-string)
  16952. (setq global-mode-string
  16953. (append global-mode-string '(org-mode-line-string))))
  16954. (org-update-mode-line)
  16955. (setq org-mode-line-timer (run-with-timer 60 60 'org-update-mode-line))
  16956. (message "Clock started at %s" ts))))
  16957. (defun org-clock-find-position ()
  16958. "Find the location where the next clock line should be inserted."
  16959. (org-back-to-heading t)
  16960. (catch 'exit
  16961. (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
  16962. (re (concat "^[ \t]*" org-clock-string))
  16963. (cnt 0)
  16964. first last)
  16965. (goto-char beg)
  16966. (when (eobp) (newline) (setq end (max (point) end)))
  16967. (when (re-search-forward "^[ \t]*:CLOCK:" end t)
  16968. ;; we seem to have a CLOCK drawer, so go there.
  16969. (beginning-of-line 2)
  16970. (throw 'exit t))
  16971. ;; Lets count the CLOCK lines
  16972. (goto-char beg)
  16973. (while (re-search-forward re end t)
  16974. (setq first (or first (match-beginning 0))
  16975. last (match-beginning 0)
  16976. cnt (1+ cnt)))
  16977. (when (and (integerp org-clock-into-drawer)
  16978. (>= (1+ cnt) org-clock-into-drawer))
  16979. ;; Wrap current entries into a new drawer
  16980. (goto-char last)
  16981. (beginning-of-line 2)
  16982. (if (org-at-item-p) (org-end-of-item))
  16983. (insert ":END:\n")
  16984. (beginning-of-line 0)
  16985. (org-indent-line-function)
  16986. (goto-char first)
  16987. (insert ":CLOCK:\n")
  16988. (beginning-of-line 0)
  16989. (org-indent-line-function)
  16990. (org-flag-drawer t)
  16991. (beginning-of-line 2)
  16992. (throw 'exit nil))
  16993. (goto-char beg)
  16994. (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  16995. (not (equal (match-string 1) org-clock-string)))
  16996. ;; Planning info, skip to after it
  16997. (beginning-of-line 2)
  16998. (or (bolp) (newline)))
  16999. (when (eq t org-clock-into-drawer)
  17000. (insert ":CLOCK:\n:END:\n")
  17001. (beginning-of-line -1)
  17002. (org-indent-line-function)
  17003. (org-flag-drawer t)
  17004. (beginning-of-line 2)
  17005. (org-indent-line-function)))))
  17006. (defun org-clock-out (&optional fail-quietly)
  17007. "Stop the currently running clock.
  17008. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  17009. (interactive)
  17010. (catch 'exit
  17011. (if (not (marker-buffer org-clock-marker))
  17012. (if fail-quietly (throw 'exit t) (error "No active clock")))
  17013. (let (ts te s h m)
  17014. (save-excursion
  17015. (set-buffer (marker-buffer org-clock-marker))
  17016. (goto-char org-clock-marker)
  17017. (beginning-of-line 1)
  17018. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  17019. (equal (match-string 1) org-clock-string))
  17020. (setq ts (match-string 2))
  17021. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  17022. (goto-char (match-end 0))
  17023. (delete-region (point) (point-at-eol))
  17024. (insert "--")
  17025. (setq te (org-insert-time-stamp (current-time) 'with-hm 'inactive))
  17026. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  17027. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  17028. h (floor (/ s 3600))
  17029. s (- s (* 3600 h))
  17030. m (floor (/ s 60))
  17031. s (- s (* 60 s)))
  17032. (insert " => " (format "%2d:%02d" h m))
  17033. (move-marker org-clock-marker nil)
  17034. (let* ((logging (save-match-data (org-entry-get nil "LOGGING" t)))
  17035. (org-log-done (org-parse-local-options logging 'org-log-done))
  17036. (org-log-repeat (org-parse-local-options logging 'org-log-repeat)))
  17037. (org-add-log-maybe 'clock-out))
  17038. (when org-mode-line-timer
  17039. (cancel-timer org-mode-line-timer)
  17040. (setq org-mode-line-timer nil))
  17041. (setq global-mode-string
  17042. (delq 'org-mode-line-string global-mode-string))
  17043. (force-mode-line-update)
  17044. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  17045. (defun org-clock-cancel ()
  17046. "Cancel the running clock be removing the start timestamp."
  17047. (interactive)
  17048. (if (not (marker-buffer org-clock-marker))
  17049. (error "No active clock"))
  17050. (save-excursion
  17051. (set-buffer (marker-buffer org-clock-marker))
  17052. (goto-char org-clock-marker)
  17053. (delete-region (1- (point-at-bol)) (point-at-eol)))
  17054. (setq global-mode-string
  17055. (delq 'org-mode-line-string global-mode-string))
  17056. (force-mode-line-update)
  17057. (message "Clock canceled"))
  17058. (defun org-clock-goto (&optional delete-windows)
  17059. "Go to the currently clocked-in entry."
  17060. (interactive "P")
  17061. (if (not (marker-buffer org-clock-marker))
  17062. (error "No active clock"))
  17063. (switch-to-buffer-other-window
  17064. (marker-buffer org-clock-marker))
  17065. (if delete-windows (delete-other-windows))
  17066. (goto-char org-clock-marker)
  17067. (org-show-entry)
  17068. (org-back-to-heading)
  17069. (recenter))
  17070. (defvar org-clock-file-total-minutes nil
  17071. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  17072. (make-variable-buffer-local 'org-clock-file-total-minutes)
  17073. (defun org-clock-sum (&optional tstart tend)
  17074. "Sum the times for each subtree.
  17075. Puts the resulting times in minutes as a text property on each headline."
  17076. (interactive)
  17077. (let* ((bmp (buffer-modified-p))
  17078. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  17079. org-clock-string
  17080. "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
  17081. (lmax 30)
  17082. (ltimes (make-vector lmax 0))
  17083. (t1 0)
  17084. (level 0)
  17085. ts te dt
  17086. time)
  17087. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  17088. (save-excursion
  17089. (goto-char (point-max))
  17090. (while (re-search-backward re nil t)
  17091. (cond
  17092. ((match-end 2)
  17093. ;; Two time stamps
  17094. (setq ts (match-string 2)
  17095. te (match-string 3)
  17096. ts (time-to-seconds
  17097. (apply 'encode-time (org-parse-time-string ts)))
  17098. te (time-to-seconds
  17099. (apply 'encode-time (org-parse-time-string te)))
  17100. ts (if tstart (max ts tstart) ts)
  17101. te (if tend (min te tend) te)
  17102. dt (- te ts)
  17103. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1)))
  17104. ((match-end 4)
  17105. ;; A naket time
  17106. (setq t1 (+ t1 (string-to-number (match-string 5))
  17107. (* 60 (string-to-number (match-string 4))))))
  17108. (t ;; A headline
  17109. (setq level (- (match-end 1) (match-beginning 1)))
  17110. (when (or (> t1 0) (> (aref ltimes level) 0))
  17111. (loop for l from 0 to level do
  17112. (aset ltimes l (+ (aref ltimes l) t1)))
  17113. (setq t1 0 time (aref ltimes level))
  17114. (loop for l from level to (1- lmax) do
  17115. (aset ltimes l 0))
  17116. (goto-char (match-beginning 0))
  17117. (put-text-property (point) (point-at-eol) :org-clock-minutes time)))))
  17118. (setq org-clock-file-total-minutes (aref ltimes 0)))
  17119. (set-buffer-modified-p bmp)))
  17120. (defun org-clock-display (&optional total-only)
  17121. "Show subtree times in the entire buffer.
  17122. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  17123. in the echo area."
  17124. (interactive)
  17125. (org-remove-clock-overlays)
  17126. (let (time h m p)
  17127. (org-clock-sum)
  17128. (unless total-only
  17129. (save-excursion
  17130. (goto-char (point-min))
  17131. (while (or (and (equal (setq p (point)) (point-min))
  17132. (get-text-property p :org-clock-minutes))
  17133. (setq p (next-single-property-change
  17134. (point) :org-clock-minutes)))
  17135. (goto-char p)
  17136. (when (setq time (get-text-property p :org-clock-minutes))
  17137. (org-put-clock-overlay time (funcall outline-level))))
  17138. (setq h (/ org-clock-file-total-minutes 60)
  17139. m (- org-clock-file-total-minutes (* 60 h)))
  17140. ;; Arrange to remove the overlays upon next change.
  17141. (when org-remove-highlights-with-change
  17142. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  17143. nil 'local))))
  17144. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  17145. (defvar org-clock-overlays nil)
  17146. (make-variable-buffer-local 'org-clock-overlays)
  17147. (defun org-put-clock-overlay (time &optional level)
  17148. "Put an overlays on the current line, displaying TIME.
  17149. If LEVEL is given, prefix time with a corresponding number of stars.
  17150. This creates a new overlay and stores it in `org-clock-overlays', so that it
  17151. will be easy to remove."
  17152. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  17153. (l (if level (org-get-legal-level level 0) 0))
  17154. (off 0)
  17155. ov tx)
  17156. (move-to-column c)
  17157. (unless (eolp) (skip-chars-backward "^ \t"))
  17158. (skip-chars-backward " \t")
  17159. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  17160. tx (concat (buffer-substring (1- (point)) (point))
  17161. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  17162. (org-add-props (format "%s %2d:%02d%s"
  17163. (make-string l ?*) h m
  17164. (make-string (- 10 l) ?\ ))
  17165. '(face secondary-selection))
  17166. ""))
  17167. (if (not (featurep 'xemacs))
  17168. (org-overlay-put ov 'display tx)
  17169. (org-overlay-put ov 'invisible t)
  17170. (org-overlay-put ov 'end-glyph (make-glyph tx)))
  17171. (push ov org-clock-overlays)))
  17172. (defun org-remove-clock-overlays (&optional beg end noremove)
  17173. "Remove the occur highlights from the buffer.
  17174. BEG and END are ignored. If NOREMOVE is nil, remove this function
  17175. from the `before-change-functions' in the current buffer."
  17176. (interactive)
  17177. (unless org-inhibit-highlight-removal
  17178. (mapc 'org-delete-overlay org-clock-overlays)
  17179. (setq org-clock-overlays nil)
  17180. (unless noremove
  17181. (remove-hook 'before-change-functions
  17182. 'org-remove-clock-overlays 'local))))
  17183. (defun org-clock-out-if-current ()
  17184. "Clock out if the current entry contains the running clock.
  17185. This is used to stop the clock after a TODO entry is marked DONE,
  17186. and is only done if the variable `org-clock-out-when-done' is not nil."
  17187. (when (and org-clock-out-when-done
  17188. (member state org-done-keywords)
  17189. (equal (marker-buffer org-clock-marker) (current-buffer))
  17190. (< (point) org-clock-marker)
  17191. (> (save-excursion (outline-next-heading) (point))
  17192. org-clock-marker))
  17193. ;; Clock out, but don't accept a logging message for this.
  17194. (let ((org-log-done (if (and (listp org-log-done)
  17195. (member 'clock-out org-log-done))
  17196. '(done)
  17197. org-log-done)))
  17198. (org-clock-out))))
  17199. (add-hook 'org-after-todo-state-change-hook
  17200. 'org-clock-out-if-current)
  17201. (defun org-check-running-clock ()
  17202. "Check if the current buffer contains the running clock.
  17203. If yes, offer to stop it and to save the buffer with the changes."
  17204. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  17205. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  17206. (buffer-name))))
  17207. (org-clock-out)
  17208. (when (y-or-n-p "Save changed buffer?")
  17209. (save-buffer))))
  17210. (defun org-clock-report (&optional arg)
  17211. "Create a table containing a report about clocked time.
  17212. If the cursor is inside an existing clocktable block, then the table
  17213. will be updated. If not, a new clocktable will be inserted.
  17214. When called with a prefix argument, move to the first clock table in the
  17215. buffer and update it."
  17216. (interactive "P")
  17217. (org-remove-clock-overlays)
  17218. (when arg (org-find-dblock "clocktable"))
  17219. (if (org-in-clocktable-p)
  17220. (goto-char (org-in-clocktable-p))
  17221. (org-create-dblock (list :name "clocktable"
  17222. :maxlevel 2 :scope 'file)))
  17223. (org-update-dblock))
  17224. (defun org-in-clocktable-p ()
  17225. "Check if the cursor is in a clocktable."
  17226. (let ((pos (point)) start)
  17227. (save-excursion
  17228. (end-of-line 1)
  17229. (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t)
  17230. (setq start (match-beginning 0))
  17231. (re-search-forward "^#\\+END:.*" nil t)
  17232. (>= (match-end 0) pos)
  17233. start))))
  17234. (defun org-clock-update-time-maybe ()
  17235. "If this is a CLOCK line, update it and return t.
  17236. Otherwise, return nil."
  17237. (interactive)
  17238. (save-excursion
  17239. (beginning-of-line 1)
  17240. (skip-chars-forward " \t")
  17241. (when (looking-at org-clock-string)
  17242. (let ((re (concat "[ \t]*" org-clock-string
  17243. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  17244. "\\([ \t]*=>.*\\)?"))
  17245. ts te h m s)
  17246. (if (not (looking-at re))
  17247. nil
  17248. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  17249. (end-of-line 1)
  17250. (setq ts (match-string 1)
  17251. te (match-string 2))
  17252. (setq s (- (time-to-seconds
  17253. (apply 'encode-time (org-parse-time-string te)))
  17254. (time-to-seconds
  17255. (apply 'encode-time (org-parse-time-string ts))))
  17256. h (floor (/ s 3600))
  17257. s (- s (* 3600 h))
  17258. m (floor (/ s 60))
  17259. s (- s (* 60 s)))
  17260. (insert " => " (format "%2d:%02d" h m))
  17261. t)))))
  17262. (defun org-clock-special-range (key &optional time as-strings)
  17263. "Return two times bordering a special time range.
  17264. Key is a symbol specifying the range and can be one of `today', `yesterday',
  17265. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  17266. A week starts Monday 0:00 and ends Sunday 24:00.
  17267. The range is determined relative to TIME. TIME defaults to the current time.
  17268. The return value is a cons cell with two internal times like the ones
  17269. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  17270. the returned times will be formatted strings."
  17271. (let* ((tm (decode-time (or time (current-time))))
  17272. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  17273. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  17274. (dow (nth 6 tm))
  17275. s1 m1 h1 d1 month1 y1 diff ts te fm)
  17276. (cond
  17277. ((eq key 'today)
  17278. (setq h 0 m 0 h1 24 m1 0))
  17279. ((eq key 'yesterday)
  17280. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  17281. ((eq key 'thisweek)
  17282. (setq diff (if (= dow 0) 6 (1- dow))
  17283. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17284. ((eq key 'lastweek)
  17285. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  17286. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  17287. ((eq key 'thismonth)
  17288. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  17289. ((eq key 'lastmonth)
  17290. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  17291. ((eq key 'thisyear)
  17292. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  17293. ((eq key 'lastyear)
  17294. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  17295. (t (error "No such time block %s" key)))
  17296. (setq ts (encode-time s m h d month y)
  17297. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  17298. (or d1 d) (or month1 month) (or y1 y)))
  17299. (setq fm (cdr org-time-stamp-formats))
  17300. (if as-strings
  17301. (cons (format-time-string fm ts) (format-time-string fm te))
  17302. (cons ts te))))
  17303. (defun org-dblock-write:clocktable (params)
  17304. "Write the standard clocktable."
  17305. (catch 'exit
  17306. (let* ((hlchars '((1 . "*") (2 . "/")))
  17307. (ins (make-marker))
  17308. (total-time nil)
  17309. (scope (plist-get params :scope))
  17310. (tostring (plist-get params :tostring))
  17311. (multifile (plist-get params :multifile))
  17312. (header (plist-get params :header))
  17313. (maxlevel (or (plist-get params :maxlevel) 3))
  17314. (step (plist-get params :step))
  17315. (emph (plist-get params :emphasize))
  17316. (ts (plist-get params :tstart))
  17317. (te (plist-get params :tend))
  17318. (block (plist-get params :block))
  17319. ipos time h m p level hlc hdl
  17320. cc beg end pos tbl)
  17321. (when step
  17322. (org-clocktable-steps params)
  17323. (throw 'exit nil))
  17324. (when block
  17325. (setq cc (org-clock-special-range block nil t)
  17326. ts (car cc) te (cdr cc)))
  17327. (if ts (setq ts (time-to-seconds
  17328. (apply 'encode-time (org-parse-time-string ts)))))
  17329. (if te (setq te (time-to-seconds
  17330. (apply 'encode-time (org-parse-time-string te)))))
  17331. (move-marker ins (point))
  17332. (setq ipos (point))
  17333. ;; Get the right scope
  17334. (setq pos (point))
  17335. (save-restriction
  17336. (cond
  17337. ((not scope))
  17338. ((eq scope 'file) (widen))
  17339. ((eq scope 'subtree) (org-narrow-to-subtree))
  17340. ((eq scope 'tree)
  17341. (while (org-up-heading-safe))
  17342. (org-narrow-to-subtree))
  17343. ((and (symbolp scope) (string-match "^tree\\([0-9]+\\)$"
  17344. (symbol-name scope)))
  17345. (setq level (string-to-number (match-string 1 (symbol-name scope))))
  17346. (catch 'exit
  17347. (while (org-up-heading-safe)
  17348. (looking-at outline-regexp)
  17349. (if (<= (org-reduced-level (funcall outline-level)) level)
  17350. (throw 'exit nil))))
  17351. (org-narrow-to-subtree))
  17352. ((or (listp scope) (eq scope 'agenda))
  17353. (let* ((files (if (listp scope) scope (org-agenda-files)))
  17354. (scope 'agenda)
  17355. (p1 (copy-sequence params))
  17356. file)
  17357. (plist-put p1 :tostring t)
  17358. (plist-put p1 :multifile t)
  17359. (plist-put p1 :scope 'file)
  17360. (org-prepare-agenda-buffers files)
  17361. (while (setq file (pop files))
  17362. (with-current-buffer (find-buffer-visiting file)
  17363. (push (org-clocktable-add-file
  17364. file (org-dblock-write:clocktable p1)) tbl)
  17365. (setq total-time (+ (or total-time 0)
  17366. org-clock-file-total-minutes)))))))
  17367. (goto-char pos)
  17368. (unless (eq scope 'agenda)
  17369. (org-clock-sum ts te)
  17370. (goto-char (point-min))
  17371. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  17372. (goto-char p)
  17373. (when (setq time (get-text-property p :org-clock-minutes))
  17374. (save-excursion
  17375. (beginning-of-line 1)
  17376. (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
  17377. (setq level (org-reduced-level
  17378. (- (match-end 1) (match-beginning 1))))
  17379. (<= level maxlevel))
  17380. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  17381. hdl (match-string 2)
  17382. h (/ time 60)
  17383. m (- time (* 60 h)))
  17384. (if (and (not multifile) (= level 1)) (push "|-" tbl))
  17385. (push (concat
  17386. "| " (int-to-string level) "|" hlc hdl hlc " |"
  17387. (make-string (1- level) ?|)
  17388. hlc (format "%d:%02d" h m) hlc
  17389. " |") tbl))))))
  17390. (setq tbl (nreverse tbl))
  17391. (if tostring
  17392. (if tbl (mapconcat 'identity tbl "\n") nil)
  17393. (goto-char ins)
  17394. (insert-before-markers
  17395. (or header
  17396. (concat
  17397. "Clock summary at ["
  17398. (substring
  17399. (format-time-string (cdr org-time-stamp-formats))
  17400. 1 -1)
  17401. "]."
  17402. (if block
  17403. (format " Considered range is /%s/." block)
  17404. "")
  17405. "\n\n"))
  17406. (if (eq scope 'agenda) "|File" "")
  17407. "|L|Headline|Time|\n")
  17408. (setq total-time (or total-time org-clock-file-total-minutes)
  17409. h (/ total-time 60)
  17410. m (- total-time (* 60 h)))
  17411. (insert-before-markers
  17412. "|-\n|"
  17413. (if (eq scope 'agenda) "|" "")
  17414. "|"
  17415. "*Total time*| "
  17416. (format "*%d:%02d*" h m)
  17417. "|\n|-\n")
  17418. (setq tbl (delq nil tbl))
  17419. (if (and (stringp (car tbl)) (> (length (car tbl)) 1)
  17420. (equal (substring (car tbl) 0 2) "|-"))
  17421. (pop tbl))
  17422. (insert-before-markers (mapconcat
  17423. 'identity (delq nil tbl)
  17424. (if (eq scope 'agenda) "\n|-\n" "\n")))
  17425. (backward-delete-char 1)
  17426. (goto-char ipos)
  17427. (skip-chars-forward "^|")
  17428. (org-table-align))))))
  17429. (defun org-clocktable-steps (params)
  17430. (let* ((p1 (copy-sequence params))
  17431. (ts (plist-get p1 :tstart))
  17432. (te (plist-get p1 :tend))
  17433. (step0 (plist-get p1 :step))
  17434. (step (cdr (assoc step0 '((day . 86400) (week . 604800)))))
  17435. (block (plist-get p1 :block))
  17436. cc)
  17437. (when block
  17438. (setq cc (org-clock-special-range block nil t)
  17439. ts (car cc) te (cdr cc)))
  17440. (if ts (setq ts (time-to-seconds
  17441. (apply 'encode-time (org-parse-time-string ts)))))
  17442. (if te (setq te (time-to-seconds
  17443. (apply 'encode-time (org-parse-time-string te)))))
  17444. (plist-put p1 :header "")
  17445. (plist-put p1 :step nil)
  17446. (plist-put p1 :block nil)
  17447. (while (< ts te)
  17448. (or (bolp) (insert "\n"))
  17449. (plist-put p1 :tstart (format-time-string
  17450. (car org-time-stamp-formats)
  17451. (seconds-to-time ts)))
  17452. (plist-put p1 :tend (format-time-string
  17453. (car org-time-stamp-formats)
  17454. (seconds-to-time (setq ts (+ ts step)))))
  17455. (insert "\n" (if (eq step0 'day) "Daily report: " "Weekly report starting on: ")
  17456. (plist-get p1 :tstart) "\n")
  17457. (org-dblock-write:clocktable p1)
  17458. (re-search-forward "#\\+END:")
  17459. (end-of-line 0))))
  17460. (defun org-clocktable-add-file (file table)
  17461. (if table
  17462. (let ((lines (org-split-string table "\n"))
  17463. (ff (file-name-nondirectory file)))
  17464. (mapconcat 'identity
  17465. (mapcar (lambda (x)
  17466. (if (string-match org-table-dataline-regexp x)
  17467. (concat "|" ff x)
  17468. x))
  17469. lines)
  17470. "\n"))))
  17471. ;; FIXME: I don't think anybody uses this, ask David
  17472. (defun org-collect-clock-time-entries ()
  17473. "Return an internal list with clocking information.
  17474. This list has one entry for each CLOCK interval.
  17475. FIXME: describe the elements."
  17476. (interactive)
  17477. (let ((re (concat "^[ \t]*" org-clock-string
  17478. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  17479. rtn beg end next cont level title total closedp leafp
  17480. clockpos titlepos h m donep)
  17481. (save-excursion
  17482. (org-clock-sum)
  17483. (goto-char (point-min))
  17484. (while (re-search-forward re nil t)
  17485. (setq clockpos (match-beginning 0)
  17486. beg (match-string 1) end (match-string 2)
  17487. cont (match-end 0))
  17488. (setq beg (apply 'encode-time (org-parse-time-string beg))
  17489. end (apply 'encode-time (org-parse-time-string end)))
  17490. (org-back-to-heading t)
  17491. (setq donep (org-entry-is-done-p))
  17492. (setq titlepos (point)
  17493. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  17494. h (/ total 60) m (- total (* 60 h))
  17495. total (cons h m))
  17496. (looking-at "\\(\\*+\\) +\\(.*\\)")
  17497. (setq level (- (match-end 1) (match-beginning 1))
  17498. title (org-match-string-no-properties 2))
  17499. (save-excursion (outline-next-heading) (setq next (point)))
  17500. (setq closedp (re-search-forward org-closed-time-regexp next t))
  17501. (goto-char next)
  17502. (setq leafp (and (looking-at "^\\*+ ")
  17503. (<= (- (match-end 0) (point)) level)))
  17504. (push (list beg end clockpos closedp donep
  17505. total title titlepos level leafp)
  17506. rtn)
  17507. (goto-char cont)))
  17508. (nreverse rtn)))
  17509. ;;;; Agenda, and Diary Integration
  17510. ;;; Define the Org-agenda-mode
  17511. (defvar org-agenda-mode-map (make-sparse-keymap)
  17512. "Keymap for `org-agenda-mode'.")
  17513. (defvar org-agenda-menu) ; defined later in this file.
  17514. (defvar org-agenda-follow-mode nil)
  17515. (defvar org-agenda-show-log nil)
  17516. (defvar org-agenda-redo-command nil)
  17517. (defvar org-agenda-mode-hook nil)
  17518. (defvar org-agenda-type nil)
  17519. (defvar org-agenda-force-single-file nil)
  17520. (defun org-agenda-mode ()
  17521. "Mode for time-sorted view on action items in Org-mode files.
  17522. The following commands are available:
  17523. \\{org-agenda-mode-map}"
  17524. (interactive)
  17525. (kill-all-local-variables)
  17526. (setq org-agenda-undo-list nil
  17527. org-agenda-pending-undo-list nil)
  17528. (setq major-mode 'org-agenda-mode)
  17529. ;; Keep global-font-lock-mode from turning on font-lock-mode
  17530. (org-set-local 'font-lock-global-modes (list 'not major-mode))
  17531. (setq mode-name "Org-Agenda")
  17532. (use-local-map org-agenda-mode-map)
  17533. (easy-menu-add org-agenda-menu)
  17534. (if org-startup-truncated (setq truncate-lines t))
  17535. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  17536. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  17537. ;; Make sure properties are removed when copying text
  17538. (when (boundp 'buffer-substring-filters)
  17539. (org-set-local 'buffer-substring-filters
  17540. (cons (lambda (x)
  17541. (set-text-properties 0 (length x) nil x) x)
  17542. buffer-substring-filters)))
  17543. (unless org-agenda-keep-modes
  17544. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  17545. org-agenda-show-log nil))
  17546. (easy-menu-change
  17547. '("Agenda") "Agenda Files"
  17548. (append
  17549. (list
  17550. (vector
  17551. (if (get 'org-agenda-files 'org-restrict)
  17552. "Restricted to single file"
  17553. "Edit File List")
  17554. '(org-edit-agenda-file-list)
  17555. (not (get 'org-agenda-files 'org-restrict)))
  17556. "--")
  17557. (mapcar 'org-file-menu-entry (org-agenda-files))))
  17558. (org-agenda-set-mode-name)
  17559. (apply
  17560. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  17561. (list 'org-agenda-mode-hook)))
  17562. (substitute-key-definition 'undo 'org-agenda-undo
  17563. org-agenda-mode-map global-map)
  17564. (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
  17565. (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
  17566. (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  17567. (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
  17568. (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
  17569. (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
  17570. (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
  17571. (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
  17572. (org-defkey org-agenda-mode-map " " 'org-agenda-show)
  17573. (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  17574. (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
  17575. (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
  17576. (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
  17577. (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
  17578. (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
  17579. (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
  17580. (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
  17581. (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  17582. (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
  17583. (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
  17584. (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
  17585. (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
  17586. (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
  17587. (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
  17588. (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
  17589. (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
  17590. (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
  17591. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  17592. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  17593. (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
  17594. (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  17595. (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  17596. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  17597. (while l (org-defkey org-agenda-mode-map
  17598. (int-to-string (pop l)) 'digit-argument)))
  17599. (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
  17600. (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
  17601. (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  17602. (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
  17603. (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
  17604. (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
  17605. (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
  17606. (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
  17607. (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
  17608. (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
  17609. (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
  17610. (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
  17611. (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
  17612. (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
  17613. (org-defkey org-agenda-mode-map "n" 'next-line)
  17614. (org-defkey org-agenda-mode-map "p" 'previous-line)
  17615. (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
  17616. (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
  17617. (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
  17618. (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
  17619. (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
  17620. (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  17621. (eval-after-load "calendar"
  17622. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  17623. 'org-calendar-goto-agenda))
  17624. (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
  17625. (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  17626. (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  17627. (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
  17628. (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
  17629. (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
  17630. (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
  17631. (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
  17632. (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
  17633. (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
  17634. (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
  17635. (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17636. (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
  17637. (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
  17638. (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
  17639. (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
  17640. (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
  17641. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  17642. (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  17643. (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
  17644. (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
  17645. (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
  17646. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  17647. "Local keymap for agenda entries from Org-mode.")
  17648. (org-defkey org-agenda-keymap
  17649. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  17650. (org-defkey org-agenda-keymap
  17651. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  17652. (when org-agenda-mouse-1-follows-link
  17653. (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
  17654. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  17655. '("Agenda"
  17656. ("Agenda Files")
  17657. "--"
  17658. ["Show" org-agenda-show t]
  17659. ["Go To (other window)" org-agenda-goto t]
  17660. ["Go To (this window)" org-agenda-switch-to t]
  17661. ["Follow Mode" org-agenda-follow-mode
  17662. :style toggle :selected org-agenda-follow-mode :active t]
  17663. ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
  17664. "--"
  17665. ["Cycle TODO" org-agenda-todo t]
  17666. ["Archive subtree" org-agenda-archive t]
  17667. ["Delete subtree" org-agenda-kill t]
  17668. "--"
  17669. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  17670. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  17671. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  17672. ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
  17673. "--"
  17674. ("Tags and Properties"
  17675. ["Show all Tags" org-agenda-show-tags t]
  17676. ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
  17677. ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
  17678. "--"
  17679. ["Column View" org-columns t])
  17680. ("Date/Schedule"
  17681. ["Schedule" org-agenda-schedule t]
  17682. ["Set Deadline" org-agenda-deadline t]
  17683. "--"
  17684. ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  17685. ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  17686. ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  17687. ("Clock"
  17688. ["Clock in" org-agenda-clock-in t]
  17689. ["Clock out" org-agenda-clock-out t]
  17690. ["Clock cancel" org-agenda-clock-cancel t]
  17691. ["Goto running clock" org-clock-goto t])
  17692. ("Priority"
  17693. ["Set Priority" org-agenda-priority t]
  17694. ["Increase Priority" org-agenda-priority-up t]
  17695. ["Decrease Priority" org-agenda-priority-down t]
  17696. ["Show Priority" org-agenda-show-priority t])
  17697. ("Calendar/Diary"
  17698. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  17699. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  17700. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  17701. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  17702. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  17703. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
  17704. "--"
  17705. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
  17706. "--"
  17707. ("View"
  17708. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  17709. :style radio :selected (equal org-agenda-ndays 1)]
  17710. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  17711. :style radio :selected (equal org-agenda-ndays 7)]
  17712. ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
  17713. :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
  17714. ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
  17715. :style radio :selected (member org-agenda-ndays '(365 366))]
  17716. "--"
  17717. ["Show Logbook entries" org-agenda-log-mode
  17718. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  17719. ["Include Diary" org-agenda-toggle-diary
  17720. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  17721. ["Use Time Grid" org-agenda-toggle-time-grid
  17722. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)])
  17723. ["Write view to file" org-write-agenda t]
  17724. ["Rebuild buffer" org-agenda-redo t]
  17725. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  17726. "--"
  17727. ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
  17728. "--"
  17729. ["Quit" org-agenda-quit t]
  17730. ["Exit and Release Buffers" org-agenda-exit t]
  17731. ))
  17732. ;;; Agenda undo
  17733. (defvar org-agenda-allow-remote-undo t
  17734. "Non-nil means, allow remote undo from the agenda buffer.")
  17735. (defvar org-agenda-undo-list nil
  17736. "List of undoable operations in the agenda since last refresh.")
  17737. (defvar org-agenda-undo-has-started-in nil
  17738. "Buffers that have already seen `undo-start' in the current undo sequence.")
  17739. (defvar org-agenda-pending-undo-list nil
  17740. "In a series of undo commands, this is the list of remaning undo items.")
  17741. (defmacro org-if-unprotected (&rest body)
  17742. "Execute BODY if there is no `org-protected' text property at point."
  17743. (declare (debug t))
  17744. `(unless (get-text-property (point) 'org-protected)
  17745. ,@body))
  17746. (defmacro org-with-remote-undo (_buffer &rest _body)
  17747. "Execute BODY while recording undo information in two buffers."
  17748. (declare (indent 1) (debug t))
  17749. `(let ((_cline (org-current-line))
  17750. (_cmd this-command)
  17751. (_buf1 (current-buffer))
  17752. (_buf2 ,_buffer)
  17753. (_undo1 buffer-undo-list)
  17754. (_undo2 (with-current-buffer ,_buffer buffer-undo-list))
  17755. _c1 _c2)
  17756. ,@_body
  17757. (when org-agenda-allow-remote-undo
  17758. (setq _c1 (org-verify-change-for-undo
  17759. _undo1 (with-current-buffer _buf1 buffer-undo-list))
  17760. _c2 (org-verify-change-for-undo
  17761. _undo2 (with-current-buffer _buf2 buffer-undo-list)))
  17762. (when (or _c1 _c2)
  17763. ;; make sure there are undo boundaries
  17764. (and _c1 (with-current-buffer _buf1 (undo-boundary)))
  17765. (and _c2 (with-current-buffer _buf2 (undo-boundary)))
  17766. ;; remember which buffer to undo
  17767. (push (list _cmd _cline _buf1 _c1 _buf2 _c2)
  17768. org-agenda-undo-list)))))
  17769. (defun org-agenda-undo ()
  17770. "Undo a remote editing step in the agenda.
  17771. This undoes changes both in the agenda buffer and in the remote buffer
  17772. that have been changed along."
  17773. (interactive)
  17774. (or org-agenda-allow-remote-undo
  17775. (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
  17776. (if (not (eq this-command last-command))
  17777. (setq org-agenda-undo-has-started-in nil
  17778. org-agenda-pending-undo-list org-agenda-undo-list))
  17779. (if (not org-agenda-pending-undo-list)
  17780. (error "No further undo information"))
  17781. (let* ((entry (pop org-agenda-pending-undo-list))
  17782. buf line cmd rembuf)
  17783. (setq cmd (pop entry) line (pop entry))
  17784. (setq rembuf (nth 2 entry))
  17785. (org-with-remote-undo rembuf
  17786. (while (bufferp (setq buf (pop entry)))
  17787. (if (pop entry)
  17788. (with-current-buffer buf
  17789. (let ((last-undo-buffer buf)
  17790. (inhibit-read-only t))
  17791. (unless (memq buf org-agenda-undo-has-started-in)
  17792. (push buf org-agenda-undo-has-started-in)
  17793. (make-local-variable 'pending-undo-list)
  17794. (undo-start))
  17795. (while (and pending-undo-list
  17796. (listp pending-undo-list)
  17797. (not (car pending-undo-list)))
  17798. (pop pending-undo-list))
  17799. (undo-more 1))))))
  17800. (goto-line line)
  17801. (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
  17802. (defun org-verify-change-for-undo (l1 l2)
  17803. "Verify that a real change occurred between the undo lists L1 and L2."
  17804. (while (and l1 (listp l1) (null (car l1))) (pop l1))
  17805. (while (and l2 (listp l2) (null (car l2))) (pop l2))
  17806. (not (eq l1 l2)))
  17807. ;;; Agenda dispatch
  17808. (defvar org-agenda-restrict nil)
  17809. (defvar org-agenda-restrict-begin (make-marker))
  17810. (defvar org-agenda-restrict-end (make-marker))
  17811. (defvar org-agenda-last-dispatch-buffer nil)
  17812. (defvar org-agenda-overriding-restriction nil)
  17813. ;;;###autoload
  17814. (defun org-agenda (arg &optional keys restriction)
  17815. "Dispatch agenda commands to collect entries to the agenda buffer.
  17816. Prompts for a command to execute. Any prefix arg will be passed
  17817. on to the selected command. The default selections are:
  17818. a Call `org-agenda-list' to display the agenda for current day or week.
  17819. t Call `org-todo-list' to display the global todo list.
  17820. T Call `org-todo-list' to display the global todo list, select only
  17821. entries with a specific TODO keyword (the user gets a prompt).
  17822. m Call `org-tags-view' to display headlines with tags matching
  17823. a condition (the user is prompted for the condition).
  17824. M Like `m', but select only TODO entries, no ordinary headlines.
  17825. L Create a timeline for the current buffer.
  17826. e Export views to associated files.
  17827. More commands can be added by configuring the variable
  17828. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  17829. searches can be pre-defined in this way.
  17830. If the current buffer is in Org-mode and visiting a file, you can also
  17831. first press `<' once to indicate that the agenda should be temporarily
  17832. \(until the next use of \\[org-agenda]) restricted to the current file.
  17833. Pressing `<' twice means to restrict to the current subtree or region
  17834. \(if active)."
  17835. (interactive "P")
  17836. (catch 'exit
  17837. (let* ((prefix-descriptions nil)
  17838. (org-agenda-custom-commands-orig org-agenda-custom-commands)
  17839. (org-agenda-custom-commands
  17840. ;; normalize different versions
  17841. (delq nil
  17842. (mapcar
  17843. (lambda (x)
  17844. (cond ((stringp (cdr x))
  17845. (push x prefix-descriptions)
  17846. nil)
  17847. ((stringp (nth 1 x)) x)
  17848. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  17849. (t (cons (car x) (cons "" (cdr x))))))
  17850. org-agenda-custom-commands)))
  17851. (buf (current-buffer))
  17852. (bfn (buffer-file-name (buffer-base-buffer)))
  17853. entry key type match lprops ans)
  17854. ;; Turn off restriction unless there is an overriding one
  17855. (unless org-agenda-overriding-restriction
  17856. (put 'org-agenda-files 'org-restrict nil)
  17857. (setq org-agenda-restrict nil)
  17858. (move-marker org-agenda-restrict-begin nil)
  17859. (move-marker org-agenda-restrict-end nil))
  17860. ;; Delete old local properties
  17861. (put 'org-agenda-redo-command 'org-lprops nil)
  17862. ;; Remember where this call originated
  17863. (setq org-agenda-last-dispatch-buffer (current-buffer))
  17864. (unless keys
  17865. (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
  17866. keys (car ans)
  17867. restriction (cdr ans)))
  17868. ;; Estabish the restriction, if any
  17869. (when (and (not org-agenda-overriding-restriction) restriction)
  17870. (put 'org-agenda-files 'org-restrict (list bfn))
  17871. (cond
  17872. ((eq restriction 'region)
  17873. (setq org-agenda-restrict t)
  17874. (move-marker org-agenda-restrict-begin (region-beginning))
  17875. (move-marker org-agenda-restrict-end (region-end)))
  17876. ((eq restriction 'subtree)
  17877. (save-excursion
  17878. (setq org-agenda-restrict t)
  17879. (org-back-to-heading t)
  17880. (move-marker org-agenda-restrict-begin (point))
  17881. (move-marker org-agenda-restrict-end
  17882. (progn (org-end-of-subtree t)))))))
  17883. (require 'calendar) ; FIXME: can we avoid this for some commands?
  17884. ;; For example the todo list should not need it (but does...)
  17885. (cond
  17886. ((setq entry (assoc keys org-agenda-custom-commands))
  17887. (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
  17888. (progn
  17889. (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
  17890. (put 'org-agenda-redo-command 'org-lprops lprops)
  17891. (cond
  17892. ((eq type 'agenda)
  17893. (org-let lprops '(org-agenda-list current-prefix-arg)))
  17894. ((eq type 'alltodo)
  17895. (org-let lprops '(org-todo-list current-prefix-arg)))
  17896. ((eq type 'stuck)
  17897. (org-let lprops '(org-agenda-list-stuck-projects
  17898. current-prefix-arg)))
  17899. ((eq type 'tags)
  17900. (org-let lprops '(org-tags-view current-prefix-arg match)))
  17901. ((eq type 'tags-todo)
  17902. (org-let lprops '(org-tags-view '(4) match)))
  17903. ((eq type 'todo)
  17904. (org-let lprops '(org-todo-list match)))
  17905. ((eq type 'tags-tree)
  17906. (org-check-for-org-mode)
  17907. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  17908. ((eq type 'todo-tree)
  17909. (org-check-for-org-mode)
  17910. (org-let lprops
  17911. '(org-occur (concat "^" outline-regexp "[ \t]*"
  17912. (regexp-quote match) "\\>"))))
  17913. ((eq type 'occur-tree)
  17914. (org-check-for-org-mode)
  17915. (org-let lprops '(org-occur match)))
  17916. ((functionp type)
  17917. (org-let lprops '(funcall type match)))
  17918. ((fboundp type)
  17919. (org-let lprops '(funcall type match)))
  17920. (t (error "Invalid custom agenda command type %s" type))))
  17921. (org-run-agenda-series (nth 1 entry) (cddr entry))))
  17922. ((equal keys "C")
  17923. (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
  17924. (customize-variable 'org-agenda-custom-commands))
  17925. ((equal keys "a") (call-interactively 'org-agenda-list))
  17926. ((equal keys "t") (call-interactively 'org-todo-list))
  17927. ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
  17928. ((equal keys "m") (call-interactively 'org-tags-view))
  17929. ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
  17930. ((equal keys "e") (call-interactively 'org-store-agenda-views))
  17931. ((equal keys "L")
  17932. (unless (org-mode-p)
  17933. (error "This is not an Org-mode file"))
  17934. (unless restriction
  17935. (put 'org-agenda-files 'org-restrict (list bfn))
  17936. (org-call-with-arg 'org-timeline arg)))
  17937. ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
  17938. ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
  17939. ((equal keys "!") (customize-variable 'org-stuck-projects))
  17940. (t (error "Invalid agenda key"))))))
  17941. (defun org-agenda-normalize-custom-commands (cmds)
  17942. (delq nil
  17943. (mapcar
  17944. (lambda (x)
  17945. (cond ((stringp (cdr x)) nil)
  17946. ((stringp (nth 1 x)) x)
  17947. ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
  17948. (t (cons (car x) (cons "" (cdr x))))))
  17949. cmds)))
  17950. (defun org-agenda-get-restriction-and-command (prefix-descriptions)
  17951. "The user interface for selecting an agenda command."
  17952. (catch 'exit
  17953. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  17954. (restrict-ok (and bfn (org-mode-p)))
  17955. (region-p (org-region-active-p))
  17956. (custom org-agenda-custom-commands)
  17957. (selstring "")
  17958. restriction second-time
  17959. c entry key type match prefixes rmheader header-end custom1 desc)
  17960. (save-window-excursion
  17961. (delete-other-windows)
  17962. (org-switch-to-buffer-other-window " *Agenda Commands*")
  17963. (erase-buffer)
  17964. (insert (eval-when-compile
  17965. (let ((header
  17966. "
  17967. Press key for an agenda command: < Buffer,subtree/region restriction
  17968. -------------------------------- > Remove restriction
  17969. a Agenda for current week or day e Export agenda views
  17970. t List of all TODO entries T Entries with special TODO kwd
  17971. m Match a TAGS query M Like m, but only TODO entries
  17972. L Timeline for current buffer # List stuck projects (!=configure)
  17973. / Multi-occur C Configure custom agenda commands
  17974. ")
  17975. (start 0))
  17976. (while (string-match
  17977. "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
  17978. header start)
  17979. (setq start (match-end 0))
  17980. (add-text-properties (match-beginning 2) (match-end 2)
  17981. '(face bold) header))
  17982. header)))
  17983. (setq header-end (move-marker (make-marker) (point)))
  17984. (while t
  17985. (setq custom1 custom)
  17986. (when (eq rmheader t)
  17987. (goto-line 1)
  17988. (re-search-forward ":" nil t)
  17989. (delete-region (match-end 0) (point-at-eol))
  17990. (forward-char 1)
  17991. (looking-at "-+")
  17992. (delete-region (match-end 0) (point-at-eol))
  17993. (move-marker header-end (match-end 0)))
  17994. (goto-char header-end)
  17995. (delete-region (point) (point-max))
  17996. (while (setq entry (pop custom1))
  17997. (setq key (car entry) desc (nth 1 entry)
  17998. type (nth 2 entry) match (nth 3 entry))
  17999. (if (> (length key) 1)
  18000. (add-to-list 'prefixes (string-to-char key))
  18001. (insert
  18002. (format
  18003. "\n%-4s%-14s: %s"
  18004. (org-add-props (copy-sequence key)
  18005. '(face bold))
  18006. (cond
  18007. ((string-match "\\S-" desc) desc)
  18008. ((eq type 'agenda) "Agenda for current week or day")
  18009. ((eq type 'alltodo) "List of all TODO entries")
  18010. ((eq type 'stuck) "List of stuck projects")
  18011. ((eq type 'todo) "TODO keyword")
  18012. ((eq type 'tags) "Tags query")
  18013. ((eq type 'tags-todo) "Tags (TODO)")
  18014. ((eq type 'tags-tree) "Tags tree")
  18015. ((eq type 'todo-tree) "TODO kwd tree")
  18016. ((eq type 'occur-tree) "Occur tree")
  18017. ((functionp type) (if (symbolp type)
  18018. (symbol-name type)
  18019. "Lambda expression"))
  18020. (t "???"))
  18021. (cond
  18022. ((stringp match)
  18023. (org-add-props match nil 'face 'org-warning))
  18024. (match
  18025. (format "set of %d commands" (length match)))
  18026. (t ""))))))
  18027. (when prefixes
  18028. (mapc (lambda (x)
  18029. (insert
  18030. (format "\n%s %s"
  18031. (org-add-props (char-to-string x)
  18032. nil 'face 'bold)
  18033. (or (cdr (assoc (concat selstring (char-to-string x))
  18034. prefix-descriptions))
  18035. "Prefix key"))))
  18036. prefixes))
  18037. (goto-char (point-min))
  18038. (when (fboundp 'fit-window-to-buffer)
  18039. (if second-time
  18040. (if (not (pos-visible-in-window-p (point-max)))
  18041. (fit-window-to-buffer))
  18042. (setq second-time t)
  18043. (fit-window-to-buffer)))
  18044. (message "Press key for agenda command%s:"
  18045. (if (or restrict-ok org-agenda-overriding-restriction)
  18046. (if org-agenda-overriding-restriction
  18047. " (restriction lock active)"
  18048. (if restriction
  18049. (format " (restricted to %s)" restriction)
  18050. " (unrestricted)"))
  18051. ""))
  18052. (setq c (read-char-exclusive))
  18053. (message "")
  18054. (cond
  18055. ((assoc (char-to-string c) custom)
  18056. (setq selstring (concat selstring (char-to-string c)))
  18057. (throw 'exit (cons selstring restriction)))
  18058. ((memq c prefixes)
  18059. (setq selstring (concat selstring (char-to-string c))
  18060. prefixes nil
  18061. rmheader (or rmheader t)
  18062. custom (delq nil (mapcar
  18063. (lambda (x)
  18064. (if (or (= (length (car x)) 1)
  18065. (/= (string-to-char (car x)) c))
  18066. nil
  18067. (cons (substring (car x) 1) (cdr x))))
  18068. custom))))
  18069. ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
  18070. (message "Restriction is only possible in Org-mode buffers")
  18071. (ding) (sit-for 1))
  18072. ((eq c ?1)
  18073. (org-agenda-remove-restriction-lock 'noupdate)
  18074. (setq restriction 'buffer))
  18075. ((eq c ?0)
  18076. (org-agenda-remove-restriction-lock 'noupdate)
  18077. (setq restriction (if region-p 'region 'subtree)))
  18078. ((eq c ?<)
  18079. (org-agenda-remove-restriction-lock 'noupdate)
  18080. (setq restriction
  18081. (cond
  18082. ((eq restriction 'buffer)
  18083. (if region-p 'region 'subtree))
  18084. ((memq restriction '(subtree region))
  18085. nil)
  18086. (t 'buffer))))
  18087. ((eq c ?>)
  18088. (org-agenda-remove-restriction-lock 'noupdate)
  18089. (setq restriction nil))
  18090. ((and (equal selstring "") (memq c '(?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
  18091. (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
  18092. ((and (> (length selstring) 0) (eq c ?\d))
  18093. (delete-window)
  18094. (org-agenda-get-restriction-and-command prefix-descriptions))
  18095. ((equal c ?q) (error "Abort"))
  18096. (t (error "Invalid key %c" c))))))))
  18097. (defun org-run-agenda-series (name series)
  18098. (org-prepare-agenda name)
  18099. (let* ((org-agenda-multi t)
  18100. (redo (list 'org-run-agenda-series name (list 'quote series)))
  18101. (cmds (car series))
  18102. (gprops (nth 1 series))
  18103. match ;; The byte compiler incorrectly complains about this. Keep it!
  18104. cmd type lprops)
  18105. (while (setq cmd (pop cmds))
  18106. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  18107. (cond
  18108. ((eq type 'agenda)
  18109. (org-let2 gprops lprops
  18110. '(call-interactively 'org-agenda-list)))
  18111. ((eq type 'alltodo)
  18112. (org-let2 gprops lprops
  18113. '(call-interactively 'org-todo-list)))
  18114. ((eq type 'stuck)
  18115. (org-let2 gprops lprops
  18116. '(call-interactively 'org-agenda-list-stuck-projects)))
  18117. ((eq type 'tags)
  18118. (org-let2 gprops lprops
  18119. '(org-tags-view current-prefix-arg match)))
  18120. ((eq type 'tags-todo)
  18121. (org-let2 gprops lprops
  18122. '(org-tags-view '(4) match)))
  18123. ((eq type 'todo)
  18124. (org-let2 gprops lprops
  18125. '(org-todo-list match)))
  18126. ((fboundp type)
  18127. (org-let2 gprops lprops
  18128. '(funcall type match)))
  18129. (t (error "Invalid type in command series"))))
  18130. (widen)
  18131. (setq org-agenda-redo-command redo)
  18132. (goto-char (point-min)))
  18133. (org-finalize-agenda))
  18134. ;;;###autoload
  18135. (defmacro org-batch-agenda (cmd-key &rest parameters)
  18136. "Run an agenda command in batch mode and send the result to STDOUT.
  18137. If CMD-KEY is a string of length 1, it is used as a key in
  18138. `org-agenda-custom-commands' and triggers this command. If it is a
  18139. longer string it is used as a tags/todo match string.
  18140. Paramters are alternating variable names and values that will be bound
  18141. before running the agenda command."
  18142. (let (pars)
  18143. (while parameters
  18144. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18145. (if (> (length cmd-key) 2)
  18146. (eval (list 'let (nreverse pars)
  18147. (list 'org-tags-view nil cmd-key)))
  18148. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18149. (set-buffer org-agenda-buffer-name)
  18150. (princ (org-encode-for-stdout (buffer-string)))))
  18151. (defun org-encode-for-stdout (string)
  18152. (if (fboundp 'encode-coding-string)
  18153. (encode-coding-string string buffer-file-coding-system)
  18154. string))
  18155. (defvar org-agenda-info nil)
  18156. ;;;###autoload
  18157. (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
  18158. "Run an agenda command in batch mode and send the result to STDOUT.
  18159. If CMD-KEY is a string of length 1, it is used as a key in
  18160. `org-agenda-custom-commands' and triggers this command. If it is a
  18161. longer string it is used as a tags/todo match string.
  18162. Paramters are alternating variable names and values that will be bound
  18163. before running the agenda command.
  18164. The output gives a line for each selected agenda item. Each
  18165. item is a list of comma-separated values, like this:
  18166. category,head,type,todo,tags,date,time,extra,priority-l,priority-n
  18167. category The category of the item
  18168. head The headline, without TODO kwd, TAGS and PRIORITY
  18169. type The type of the agenda entry, can be
  18170. todo selected in TODO match
  18171. tagsmatch selected in tags match
  18172. diary imported from diary
  18173. deadline a deadline on given date
  18174. scheduled scheduled on given date
  18175. timestamp entry has timestamp on given date
  18176. closed entry was closed on given date
  18177. upcoming-deadline warning about deadline
  18178. past-scheduled forwarded scheduled item
  18179. block entry has date block including g. date
  18180. todo The todo keyword, if any
  18181. tags All tags including inherited ones, separated by colons
  18182. date The relevant date, like 2007-2-14
  18183. time The time, like 15:00-16:50
  18184. extra Sting with extra planning info
  18185. priority-l The priority letter if any was given
  18186. priority-n The computed numerical priority
  18187. agenda-day The day in the agenda where this is listed"
  18188. (let (pars)
  18189. (while parameters
  18190. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18191. (push (list 'org-agenda-remove-tags t) pars)
  18192. (if (> (length cmd-key) 2)
  18193. (eval (list 'let (nreverse pars)
  18194. (list 'org-tags-view nil cmd-key)))
  18195. (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
  18196. (set-buffer org-agenda-buffer-name)
  18197. (let* ((lines (org-split-string (buffer-string) "\n"))
  18198. line)
  18199. (while (setq line (pop lines))
  18200. (catch 'next
  18201. (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
  18202. (setq org-agenda-info
  18203. (org-fix-agenda-info (text-properties-at 0 line)))
  18204. (princ
  18205. (org-encode-for-stdout
  18206. (mapconcat 'org-agenda-export-csv-mapper
  18207. '(org-category txt type todo tags date time-of-day extra
  18208. priority-letter priority agenda-day)
  18209. ",")))
  18210. (princ "\n"))))))
  18211. (defun org-fix-agenda-info (props)
  18212. "Make sure all properties on an agenda item have a canonical form,
  18213. so the export commands can easily use it."
  18214. (let (tmp re)
  18215. (when (setq tmp (plist-get props 'tags))
  18216. (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
  18217. (when (setq tmp (plist-get props 'date))
  18218. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18219. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18220. '((format "%4d, %9s %2s, %4s" dayname monthname day year))
  18221. (setq tmp (calendar-date-string tmp)))
  18222. (setq props (plist-put props 'date tmp)))
  18223. (when (setq tmp (plist-get props 'day))
  18224. (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
  18225. (let ((calendar-date-display-form '(year "-" month "-" day)))
  18226. (setq tmp (calendar-date-string tmp)))
  18227. (setq props (plist-put props 'day tmp))
  18228. (setq props (plist-put props 'agenda-day tmp)))
  18229. (when (setq tmp (plist-get props 'txt))
  18230. (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
  18231. (plist-put props 'priority-letter (match-string 1 tmp))
  18232. (setq tmp (replace-match "" t t tmp)))
  18233. (when (and (setq re (plist-get props 'org-todo-regexp))
  18234. (setq re (concat "\\`\\.*" re " ?"))
  18235. (string-match re tmp))
  18236. (plist-put props 'todo (match-string 1 tmp))
  18237. (setq tmp (replace-match "" t t tmp)))
  18238. (plist-put props 'txt tmp)))
  18239. props)
  18240. (defun org-agenda-export-csv-mapper (prop)
  18241. (let ((res (plist-get org-agenda-info prop)))
  18242. (setq res
  18243. (cond
  18244. ((not res) "")
  18245. ((stringp res) res)
  18246. (t (prin1-to-string res))))
  18247. (while (string-match "," res)
  18248. (setq res (replace-match ";" t t res)))
  18249. (org-trim res)))
  18250. ;;;###autoload
  18251. (defun org-store-agenda-views (&rest parameters)
  18252. (interactive)
  18253. (eval (list 'org-batch-store-agenda-views)))
  18254. ;; FIXME, why is this a macro?????
  18255. ;;;###autoload
  18256. (defmacro org-batch-store-agenda-views (&rest parameters)
  18257. "Run all custom agenda commands that have a file argument."
  18258. (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
  18259. (pop-up-frames nil)
  18260. (dir default-directory)
  18261. pars cmd thiscmdkey files opts)
  18262. (while parameters
  18263. (push (list (pop parameters) (if parameters (pop parameters))) pars))
  18264. (setq pars (reverse pars))
  18265. (save-window-excursion
  18266. (while cmds
  18267. (setq cmd (pop cmds)
  18268. thiscmdkey (car cmd)
  18269. opts (nth 4 cmd)
  18270. files (nth 5 cmd))
  18271. (if (stringp files) (setq files (list files)))
  18272. (when files
  18273. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18274. (list 'org-agenda nil thiscmdkey)))
  18275. (set-buffer org-agenda-buffer-name)
  18276. (while files
  18277. (eval (list 'let (append org-agenda-exporter-settings opts pars)
  18278. (list 'org-write-agenda
  18279. (expand-file-name (pop files) dir) t))))
  18280. (and (get-buffer org-agenda-buffer-name)
  18281. (kill-buffer org-agenda-buffer-name)))))))
  18282. (defun org-write-agenda (file &optional nosettings)
  18283. "Write the current buffer (an agenda view) as a file.
  18284. Depending on the extension of the file name, plain text (.txt),
  18285. HTML (.html or .htm) or Postscript (.ps) is produced.
  18286. If NOSETTINGS is given, do not scope the settings of
  18287. `org-agenda-exporter-settings' into the export commands. This is used when
  18288. the settings have already been scoped and we do not wish to overrule other,
  18289. higher priority settings."
  18290. (interactive "FWrite agenda to file: ")
  18291. (if (not (file-writable-p file))
  18292. (error "Cannot write agenda to file %s" file))
  18293. (cond
  18294. ((string-match "\\.html?\\'" file) (require 'htmlize))
  18295. ((string-match "\\.ps\\'" file) (require 'ps-print)))
  18296. (org-let (if nosettings nil org-agenda-exporter-settings)
  18297. '(save-excursion
  18298. (save-window-excursion
  18299. (cond
  18300. ((string-match "\\.html?\\'" file)
  18301. (set-buffer (htmlize-buffer (current-buffer)))
  18302. (when (and org-agenda-export-html-style
  18303. (string-match "<style>" org-agenda-export-html-style))
  18304. ;; replace <style> section with org-agenda-export-html-style
  18305. (goto-char (point-min))
  18306. (kill-region (- (search-forward "<style") 6)
  18307. (search-forward "</style>"))
  18308. (insert org-agenda-export-html-style))
  18309. (write-file file)
  18310. (kill-buffer (current-buffer))
  18311. (message "HTML written to %s" file))
  18312. ((string-match "\\.ps\\'" file)
  18313. (ps-print-buffer-with-faces file)
  18314. (message "Postscript written to %s" file))
  18315. (t
  18316. (let ((bs (buffer-string)))
  18317. (find-file file)
  18318. (insert bs)
  18319. (save-buffer 0)
  18320. (kill-buffer (current-buffer))
  18321. (message "Plain text written to %s" file))))))
  18322. (set-buffer org-agenda-buffer-name)))
  18323. (defmacro org-no-read-only (&rest body)
  18324. "Inhibit read-only for BODY."
  18325. `(let ((inhibit-read-only t)) ,@body))
  18326. (defun org-check-for-org-mode ()
  18327. "Make sure current buffer is in org-mode. Error if not."
  18328. (or (org-mode-p)
  18329. (error "Cannot execute org-mode agenda command on buffer in %s."
  18330. major-mode)))
  18331. (defun org-fit-agenda-window ()
  18332. "Fit the window to the buffer size."
  18333. (and (memq org-agenda-window-setup '(reorganize-frame))
  18334. (fboundp 'fit-window-to-buffer)
  18335. (fit-window-to-buffer
  18336. nil
  18337. (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
  18338. (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
  18339. ;;; Agenda file list
  18340. (defun org-agenda-files (&optional unrestricted)
  18341. "Get the list of agenda files.
  18342. Optional UNRESTRICTED means return the full list even if a restriction
  18343. is currently in place."
  18344. (let ((files
  18345. (cond
  18346. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  18347. ((stringp org-agenda-files) (org-read-agenda-file-list))
  18348. ((listp org-agenda-files) org-agenda-files)
  18349. (t (error "Invalid value of `org-agenda-files'")))))
  18350. (setq files (apply 'append
  18351. (mapcar (lambda (f)
  18352. (if (file-directory-p f)
  18353. (directory-files f t
  18354. org-agenda-file-regexp)
  18355. (list f)))
  18356. files)))
  18357. (if org-agenda-skip-unavailable-files
  18358. (delq nil
  18359. (mapcar (function
  18360. (lambda (file)
  18361. (and (file-readable-p file) file)))
  18362. files))
  18363. files))) ; `org-check-agenda-file' will remove them from the list
  18364. (defun org-edit-agenda-file-list ()
  18365. "Edit the list of agenda files.
  18366. Depending on setup, this either uses customize to edit the variable
  18367. `org-agenda-files', or it visits the file that is holding the list. In the
  18368. latter case, the buffer is set up in a way that saving it automatically kills
  18369. the buffer and restores the previous window configuration."
  18370. (interactive)
  18371. (if (stringp org-agenda-files)
  18372. (let ((cw (current-window-configuration)))
  18373. (find-file org-agenda-files)
  18374. (org-set-local 'org-window-configuration cw)
  18375. (org-add-hook 'after-save-hook
  18376. (lambda ()
  18377. (set-window-configuration
  18378. (prog1 org-window-configuration
  18379. (kill-buffer (current-buffer))))
  18380. (org-install-agenda-files-menu)
  18381. (message "New agenda file list installed"))
  18382. nil 'local)
  18383. (message "%s" (substitute-command-keys
  18384. "Edit list and finish with \\[save-buffer]")))
  18385. (customize-variable 'org-agenda-files)))
  18386. (defun org-store-new-agenda-file-list (list)
  18387. "Set new value for the agenda file list and save it correcly."
  18388. (if (stringp org-agenda-files)
  18389. (let ((f org-agenda-files) b)
  18390. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  18391. (with-temp-file f
  18392. (insert (mapconcat 'identity list "\n") "\n")))
  18393. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  18394. (setq org-agenda-files list)
  18395. (customize-save-variable 'org-agenda-files org-agenda-files))))
  18396. (defun org-read-agenda-file-list ()
  18397. "Read the list of agenda files from a file."
  18398. (when (stringp org-agenda-files)
  18399. (with-temp-buffer
  18400. (insert-file-contents org-agenda-files)
  18401. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  18402. ;;;###autoload
  18403. (defun org-cycle-agenda-files ()
  18404. "Cycle through the files in `org-agenda-files'.
  18405. If the current buffer visits an agenda file, find the next one in the list.
  18406. If the current buffer does not, find the first agenda file."
  18407. (interactive)
  18408. (let* ((fs (org-agenda-files t))
  18409. (files (append fs (list (car fs))))
  18410. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  18411. file)
  18412. (unless files (error "No agenda files"))
  18413. (catch 'exit
  18414. (while (setq file (pop files))
  18415. (if (equal (file-truename file) tcf)
  18416. (when (car files)
  18417. (find-file (car files))
  18418. (throw 'exit t))))
  18419. (find-file (car fs)))
  18420. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  18421. (defun org-agenda-file-to-front (&optional to-end)
  18422. "Move/add the current file to the top of the agenda file list.
  18423. If the file is not present in the list, it is added to the front. If it is
  18424. present, it is moved there. With optional argument TO-END, add/move to the
  18425. end of the list."
  18426. (interactive "P")
  18427. (let ((org-agenda-skip-unavailable-files nil)
  18428. (file-alist (mapcar (lambda (x)
  18429. (cons (file-truename x) x))
  18430. (org-agenda-files t)))
  18431. (ctf (file-truename buffer-file-name))
  18432. x had)
  18433. (setq x (assoc ctf file-alist) had x)
  18434. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  18435. (if to-end
  18436. (setq file-alist (append (delq x file-alist) (list x)))
  18437. (setq file-alist (cons x (delq x file-alist))))
  18438. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  18439. (org-install-agenda-files-menu)
  18440. (message "File %s to %s of agenda file list"
  18441. (if had "moved" "added") (if to-end "end" "front"))))
  18442. (defun org-remove-file (&optional file)
  18443. "Remove current file from the list of files in variable `org-agenda-files'.
  18444. These are the files which are being checked for agenda entries.
  18445. Optional argument FILE means, use this file instead of the current."
  18446. (interactive)
  18447. (let* ((org-agenda-skip-unavailable-files nil)
  18448. (file (or file buffer-file-name))
  18449. (true-file (file-truename file))
  18450. (afile (abbreviate-file-name file))
  18451. (files (delq nil (mapcar
  18452. (lambda (x)
  18453. (if (equal true-file
  18454. (file-truename x))
  18455. nil x))
  18456. (org-agenda-files t)))))
  18457. (if (not (= (length files) (length (org-agenda-files t))))
  18458. (progn
  18459. (org-store-new-agenda-file-list files)
  18460. (org-install-agenda-files-menu)
  18461. (message "Removed file: %s" afile))
  18462. (message "File was not in list: %s (not removed)" afile))))
  18463. (defun org-file-menu-entry (file)
  18464. (vector file (list 'find-file file) t))
  18465. (defun org-check-agenda-file (file)
  18466. "Make sure FILE exists. If not, ask user what to do."
  18467. (when (not (file-exists-p file))
  18468. (message "non-existent file %s. [R]emove from list or [A]bort?"
  18469. (abbreviate-file-name file))
  18470. (let ((r (downcase (read-char-exclusive))))
  18471. (cond
  18472. ((equal r ?r)
  18473. (org-remove-file file)
  18474. (throw 'nextfile t))
  18475. (t (error "Abort"))))))
  18476. ;;; Agenda prepare and finalize
  18477. (defvar org-agenda-multi nil) ; dynammically scoped
  18478. (defvar org-agenda-buffer-name "*Org Agenda*")
  18479. (defvar org-pre-agenda-window-conf nil)
  18480. (defvar org-agenda-name nil)
  18481. (defun org-prepare-agenda (&optional name)
  18482. (setq org-todo-keywords-for-agenda nil)
  18483. (setq org-done-keywords-for-agenda nil)
  18484. (if org-agenda-multi
  18485. (progn
  18486. (setq buffer-read-only nil)
  18487. (goto-char (point-max))
  18488. (unless (or (bobp) org-agenda-compact-blocks)
  18489. (insert "\n" (make-string (window-width) ?=) "\n"))
  18490. (narrow-to-region (point) (point-max)))
  18491. (org-agenda-maybe-reset-markers 'force)
  18492. (org-prepare-agenda-buffers (org-agenda-files))
  18493. (setq org-todo-keywords-for-agenda
  18494. (org-uniquify org-todo-keywords-for-agenda))
  18495. (setq org-done-keywords-for-agenda
  18496. (org-uniquify org-done-keywords-for-agenda))
  18497. (let* ((abuf (get-buffer-create org-agenda-buffer-name))
  18498. (awin (get-buffer-window abuf)))
  18499. (cond
  18500. ((equal (current-buffer) abuf) nil)
  18501. (awin (select-window awin))
  18502. ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
  18503. ((equal org-agenda-window-setup 'current-window)
  18504. (switch-to-buffer abuf))
  18505. ((equal org-agenda-window-setup 'other-window)
  18506. (org-switch-to-buffer-other-window abuf))
  18507. ((equal org-agenda-window-setup 'other-frame)
  18508. (switch-to-buffer-other-frame abuf))
  18509. ((equal org-agenda-window-setup 'reorganize-frame)
  18510. (delete-other-windows)
  18511. (org-switch-to-buffer-other-window abuf))))
  18512. (setq buffer-read-only nil)
  18513. (erase-buffer)
  18514. (org-agenda-mode)
  18515. (and name (not org-agenda-name)
  18516. (org-set-local 'org-agenda-name name)))
  18517. (setq buffer-read-only nil))
  18518. (defun org-finalize-agenda ()
  18519. "Finishing touch for the agenda buffer, called just before displaying it."
  18520. (unless org-agenda-multi
  18521. (save-excursion
  18522. (let ((inhibit-read-only t))
  18523. (goto-char (point-min))
  18524. (while (org-activate-bracket-links (point-max))
  18525. (add-text-properties (match-beginning 0) (match-end 0)
  18526. '(face org-link)))
  18527. (org-agenda-align-tags)
  18528. (unless org-agenda-with-colors
  18529. (remove-text-properties (point-min) (point-max) '(face nil))))
  18530. (if (and (boundp 'org-overriding-columns-format)
  18531. org-overriding-columns-format)
  18532. (org-set-local 'org-overriding-columns-format
  18533. org-overriding-columns-format))
  18534. (if (and (boundp 'org-agenda-view-columns-initially)
  18535. org-agenda-view-columns-initially)
  18536. (org-agenda-columns))
  18537. (when org-agenda-fontify-priorities
  18538. (org-fontify-priorities))
  18539. (run-hooks 'org-finalize-agenda-hook)
  18540. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  18541. )))
  18542. (defun org-fontify-priorities ()
  18543. "Make highest priority lines bold, and lowest italic."
  18544. (interactive)
  18545. (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
  18546. (org-delete-overlay o)))
  18547. (org-overlays-in (point-min) (point-max)))
  18548. (save-excursion
  18549. (let ((inhibit-read-only t)
  18550. b e p ov h l)
  18551. (goto-char (point-min))
  18552. (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
  18553. (setq h (or (get-char-property (point) 'org-highest-priority)
  18554. org-highest-priority)
  18555. l (or (get-char-property (point) 'org-lowest-priority)
  18556. org-lowest-priority)
  18557. p (string-to-char (match-string 1))
  18558. b (match-beginning 0) e (point-at-eol)
  18559. ov (org-make-overlay b e))
  18560. (org-overlay-put
  18561. ov 'face
  18562. (cond ((listp org-agenda-fontify-priorities)
  18563. (cdr (assoc p org-agenda-fontify-priorities)))
  18564. ((equal p l) 'italic)
  18565. ((equal p h) 'bold)))
  18566. (org-overlay-put ov 'org-type 'org-priority)))))
  18567. (defun org-prepare-agenda-buffers (files)
  18568. "Create buffers for all agenda files, protect archived trees and comments."
  18569. (interactive)
  18570. (let ((pa '(:org-archived t))
  18571. (pc '(:org-comment t))
  18572. (pall '(:org-archived t :org-comment t))
  18573. (inhibit-read-only t)
  18574. (rea (concat ":" org-archive-tag ":"))
  18575. bmp file re)
  18576. (save-excursion
  18577. (save-restriction
  18578. (while (setq file (pop files))
  18579. (if (bufferp file)
  18580. (set-buffer file)
  18581. (org-check-agenda-file file)
  18582. (set-buffer (org-get-agenda-file-buffer file)))
  18583. (widen)
  18584. (setq bmp (buffer-modified-p))
  18585. (org-refresh-category-properties)
  18586. (setq org-todo-keywords-for-agenda
  18587. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  18588. (setq org-done-keywords-for-agenda
  18589. (append org-done-keywords-for-agenda org-done-keywords))
  18590. (save-excursion
  18591. (remove-text-properties (point-min) (point-max) pall)
  18592. (when org-agenda-skip-archived-trees
  18593. (goto-char (point-min))
  18594. (while (re-search-forward rea nil t)
  18595. (if (org-on-heading-p t)
  18596. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  18597. (goto-char (point-min))
  18598. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  18599. (while (re-search-forward re nil t)
  18600. (add-text-properties
  18601. (match-beginning 0) (org-end-of-subtree t) pc)))
  18602. (set-buffer-modified-p bmp))))))
  18603. (defvar org-agenda-skip-function nil
  18604. "Function to be called at each match during agenda construction.
  18605. If this function returns nil, the current match should not be skipped.
  18606. Otherwise, the function must return a position from where the search
  18607. should be continued.
  18608. This may also be a Lisp form, it will be evaluated.
  18609. Never set this variable using `setq' or so, because then it will apply
  18610. to all future agenda commands. Instead, bind it with `let' to scope
  18611. it dynamically into the agenda-constructing command. A good way to set
  18612. it is through options in org-agenda-custom-commands.")
  18613. (defun org-agenda-skip ()
  18614. "Throw to `:skip' in places that should be skipped.
  18615. Also moves point to the end of the skipped region, so that search can
  18616. continue from there."
  18617. (let ((p (point-at-bol)) to fp)
  18618. (and org-agenda-skip-archived-trees
  18619. (get-text-property p :org-archived)
  18620. (org-end-of-subtree t)
  18621. (throw :skip t))
  18622. (and (get-text-property p :org-comment)
  18623. (org-end-of-subtree t)
  18624. (throw :skip t))
  18625. (if (equal (char-after p) ?#) (throw :skip t))
  18626. (when (and (or (setq fp (functionp org-agenda-skip-function))
  18627. (consp org-agenda-skip-function))
  18628. (setq to (save-excursion
  18629. (save-match-data
  18630. (if fp
  18631. (funcall org-agenda-skip-function)
  18632. (eval org-agenda-skip-function))))))
  18633. (goto-char to)
  18634. (throw :skip t))))
  18635. (defvar org-agenda-markers nil
  18636. "List of all currently active markers created by `org-agenda'.")
  18637. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  18638. "Creation time of the last agenda marker.")
  18639. (defun org-agenda-new-marker (&optional pos)
  18640. "Return a new agenda marker.
  18641. Org-mode keeps a list of these markers and resets them when they are
  18642. no longer in use."
  18643. (let ((m (copy-marker (or pos (point)))))
  18644. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  18645. (push m org-agenda-markers)
  18646. m))
  18647. (defun org-agenda-maybe-reset-markers (&optional force)
  18648. "Reset markers created by `org-agenda'. But only if they are old enough."
  18649. (if (or (and force (not org-agenda-multi))
  18650. (> (- (time-to-seconds (current-time))
  18651. org-agenda-last-marker-time)
  18652. 5))
  18653. (while org-agenda-markers
  18654. (move-marker (pop org-agenda-markers) nil))))
  18655. (defun org-get-agenda-file-buffer (file)
  18656. "Get a buffer visiting FILE. If the buffer needs to be created, add
  18657. it to the list of buffers which might be released later."
  18658. (let ((buf (org-find-base-buffer-visiting file)))
  18659. (if buf
  18660. buf ; just return it
  18661. ;; Make a new buffer and remember it
  18662. (setq buf (find-file-noselect file))
  18663. (if buf (push buf org-agenda-new-buffers))
  18664. buf)))
  18665. (defun org-release-buffers (blist)
  18666. "Release all buffers in list, asking the user for confirmation when needed.
  18667. When a buffer is unmodified, it is just killed. When modified, it is saved
  18668. \(if the user agrees) and then killed."
  18669. (let (buf file)
  18670. (while (setq buf (pop blist))
  18671. (setq file (buffer-file-name buf))
  18672. (when (and (buffer-modified-p buf)
  18673. file
  18674. (y-or-n-p (format "Save file %s? " file)))
  18675. (with-current-buffer buf (save-buffer)))
  18676. (kill-buffer buf))))
  18677. (defun org-get-category (&optional pos)
  18678. "Get the category applying to position POS."
  18679. (get-text-property (or pos (point)) 'org-category))
  18680. ;;; Agenda timeline
  18681. (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
  18682. (defun org-timeline (&optional include-all)
  18683. "Show a time-sorted view of the entries in the current org file.
  18684. Only entries with a time stamp of today or later will be listed. With
  18685. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  18686. under the current date.
  18687. If the buffer contains an active region, only check the region for
  18688. dates."
  18689. (interactive "P")
  18690. (require 'calendar)
  18691. (org-compile-prefix-format 'timeline)
  18692. (org-set-sorting-strategy 'timeline)
  18693. (let* ((dopast t)
  18694. (dotodo include-all)
  18695. (doclosed org-agenda-show-log)
  18696. (entry buffer-file-name)
  18697. (date (calendar-current-date))
  18698. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  18699. (end (if (org-region-active-p) (region-end) (point-max)))
  18700. (day-numbers (org-get-all-dates beg end 'no-ranges
  18701. t doclosed ; always include today
  18702. org-timeline-show-empty-dates))
  18703. (org-deadline-warning-days 0)
  18704. (org-agenda-only-exact-dates t)
  18705. (today (time-to-days (current-time)))
  18706. (past t)
  18707. args
  18708. s e rtn d emptyp)
  18709. (setq org-agenda-redo-command
  18710. (list 'progn
  18711. (list 'org-switch-to-buffer-other-window (current-buffer))
  18712. (list 'org-timeline (list 'quote include-all))))
  18713. (if (not dopast)
  18714. ;; Remove past dates from the list of dates.
  18715. (setq day-numbers (delq nil (mapcar (lambda(x)
  18716. (if (>= x today) x nil))
  18717. day-numbers))))
  18718. (org-prepare-agenda (concat "Timeline "
  18719. (file-name-nondirectory buffer-file-name)))
  18720. (if doclosed (push :closed args))
  18721. (push :timestamp args)
  18722. (push :deadline args)
  18723. (push :scheduled args)
  18724. (push :sexp args)
  18725. (if dotodo (push :todo args))
  18726. (while (setq d (pop day-numbers))
  18727. (if (and (listp d) (eq (car d) :omitted))
  18728. (progn
  18729. (setq s (point))
  18730. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  18731. (put-text-property s (1- (point)) 'face 'org-agenda-structure))
  18732. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  18733. (if (and (>= d today)
  18734. dopast
  18735. past)
  18736. (progn
  18737. (setq past nil)
  18738. (insert (make-string 79 ?-) "\n")))
  18739. (setq date (calendar-gregorian-from-absolute d))
  18740. (setq s (point))
  18741. (setq rtn (and (not emptyp)
  18742. (apply 'org-agenda-get-day-entries entry
  18743. date args)))
  18744. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  18745. (progn
  18746. (insert
  18747. (if (stringp org-agenda-format-date)
  18748. (format-time-string org-agenda-format-date
  18749. (org-time-from-absolute date))
  18750. (funcall org-agenda-format-date date))
  18751. "\n")
  18752. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18753. (put-text-property s (1- (point)) 'org-date-line t)
  18754. (if (equal d today)
  18755. (put-text-property s (1- (point)) 'org-today t))
  18756. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  18757. (put-text-property s (1- (point)) 'day d)))))
  18758. (goto-char (point-min))
  18759. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  18760. (point-min)))
  18761. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  18762. (org-finalize-agenda)
  18763. (setq buffer-read-only t)))
  18764. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
  18765. "Return a list of all relevant day numbers from BEG to END buffer positions.
  18766. If NO-RANGES is non-nil, include only the start and end dates of a range,
  18767. not every single day in the range. If FORCE-TODAY is non-nil, make
  18768. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  18769. inactive time stamps (those in square brackets) are included.
  18770. When EMPTY is non-nil, also include days without any entries."
  18771. (let ((re (concat
  18772. (if pre-re pre-re "")
  18773. (if inactive org-ts-regexp-both org-ts-regexp)))
  18774. dates dates1 date day day1 day2 ts1 ts2)
  18775. (if force-today
  18776. (setq dates (list (time-to-days (current-time)))))
  18777. (save-excursion
  18778. (goto-char beg)
  18779. (while (re-search-forward re end t)
  18780. (setq day (time-to-days (org-time-string-to-time
  18781. (substring (match-string 1) 0 10))))
  18782. (or (memq day dates) (push day dates)))
  18783. (unless no-ranges
  18784. (goto-char beg)
  18785. (while (re-search-forward org-tr-regexp end t)
  18786. (setq ts1 (substring (match-string 1) 0 10)
  18787. ts2 (substring (match-string 2) 0 10)
  18788. day1 (time-to-days (org-time-string-to-time ts1))
  18789. day2 (time-to-days (org-time-string-to-time ts2)))
  18790. (while (< (setq day1 (1+ day1)) day2)
  18791. (or (memq day1 dates) (push day1 dates)))))
  18792. (setq dates (sort dates '<))
  18793. (when empty
  18794. (while (setq day (pop dates))
  18795. (setq day2 (car dates))
  18796. (push day dates1)
  18797. (when (and day2 empty)
  18798. (if (or (eq empty t)
  18799. (and (numberp empty) (<= (- day2 day) empty)))
  18800. (while (< (setq day (1+ day)) day2)
  18801. (push (list day) dates1))
  18802. (push (cons :omitted (- day2 day)) dates1))))
  18803. (setq dates (nreverse dates1)))
  18804. dates)))
  18805. ;;; Agenda Daily/Weekly
  18806. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  18807. (defvar org-agenda-start-day nil) ; dynamically scoped parameter
  18808. (defvar org-agenda-last-arguments nil
  18809. "The arguments of the previous call to org-agenda")
  18810. (defvar org-starting-day nil) ; local variable in the agenda buffer
  18811. (defvar org-agenda-span nil) ; local variable in the agenda buffer
  18812. (defvar org-include-all-loc nil) ; local variable
  18813. (defvar org-agenda-remove-date nil) ; dynamically scoped
  18814. ;;;###autoload
  18815. (defun org-agenda-list (&optional include-all start-day ndays)
  18816. "Produce a daily/weekly view from all files in variable `org-agenda-files'.
  18817. The view will be for the current day or week, but from the overview buffer
  18818. you will be able to go to other days/weeks.
  18819. With one \\[universal-argument] prefix argument INCLUDE-ALL,
  18820. all unfinished TODO items will also be shown, before the agenda.
  18821. This feature is considered obsolete, please use the TODO list or a block
  18822. agenda instead.
  18823. With a numeric prefix argument in an interactive call, the agenda will
  18824. span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
  18825. the number of days. NDAYS defaults to `org-agenda-ndays'.
  18826. START-DAY defaults to TODAY, or to the most recent match for the weekday
  18827. given in `org-agenda-start-on-weekday'."
  18828. (interactive "P")
  18829. (if (and (integerp include-all) (> include-all 0))
  18830. (setq ndays include-all include-all nil))
  18831. (setq ndays (or ndays org-agenda-ndays)
  18832. start-day (or start-day org-agenda-start-day))
  18833. (if org-agenda-overriding-arguments
  18834. (setq include-all (car org-agenda-overriding-arguments)
  18835. start-day (nth 1 org-agenda-overriding-arguments)
  18836. ndays (nth 2 org-agenda-overriding-arguments)))
  18837. (if (stringp start-day)
  18838. ;; Convert to an absolute day number
  18839. (setq start-day (time-to-days (org-read-date nil t start-day))))
  18840. (setq org-agenda-last-arguments (list include-all start-day ndays))
  18841. (org-compile-prefix-format 'agenda)
  18842. (org-set-sorting-strategy 'agenda)
  18843. (require 'calendar)
  18844. (let* ((org-agenda-start-on-weekday
  18845. (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
  18846. org-agenda-start-on-weekday nil))
  18847. (thefiles (org-agenda-files))
  18848. (files thefiles)
  18849. (today (time-to-days
  18850. (time-subtract (current-time)
  18851. (list 0 (* 3600 org-extend-today-until) 0))))
  18852. (sd (or start-day today))
  18853. (start (if (or (null org-agenda-start-on-weekday)
  18854. (< org-agenda-ndays 7))
  18855. sd
  18856. (let* ((nt (calendar-day-of-week
  18857. (calendar-gregorian-from-absolute sd)))
  18858. (n1 org-agenda-start-on-weekday)
  18859. (d (- nt n1)))
  18860. (- sd (+ (if (< d 0) 7 0) d)))))
  18861. (day-numbers (list start))
  18862. (day-cnt 0)
  18863. (inhibit-redisplay (not debug-on-error))
  18864. s e rtn rtnall file date d start-pos end-pos todayp nd)
  18865. (setq org-agenda-redo-command
  18866. (list 'org-agenda-list (list 'quote include-all) start-day ndays))
  18867. ;; Make the list of days
  18868. (setq ndays (or ndays org-agenda-ndays)
  18869. nd ndays)
  18870. (while (> ndays 1)
  18871. (push (1+ (car day-numbers)) day-numbers)
  18872. (setq ndays (1- ndays)))
  18873. (setq day-numbers (nreverse day-numbers))
  18874. (org-prepare-agenda "Day/Week")
  18875. (org-set-local 'org-starting-day (car day-numbers))
  18876. (org-set-local 'org-include-all-loc include-all)
  18877. (org-set-local 'org-agenda-span
  18878. (org-agenda-ndays-to-span nd))
  18879. (when (and (or include-all org-agenda-include-all-todo)
  18880. (member today day-numbers))
  18881. (setq files thefiles
  18882. rtnall nil)
  18883. (while (setq file (pop files))
  18884. (catch 'nextfile
  18885. (org-check-agenda-file file)
  18886. (setq date (calendar-gregorian-from-absolute today)
  18887. rtn (org-agenda-get-day-entries
  18888. file date :todo))
  18889. (setq rtnall (append rtnall rtn))))
  18890. (when rtnall
  18891. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  18892. (add-text-properties (point-min) (1- (point))
  18893. (list 'face 'org-agenda-structure))
  18894. (insert (org-finalize-agenda-entries rtnall) "\n")))
  18895. (unless org-agenda-compact-blocks
  18896. (setq s (point))
  18897. (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
  18898. "-agenda:\n")
  18899. (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
  18900. 'org-date-line t)))
  18901. (while (setq d (pop day-numbers))
  18902. (setq date (calendar-gregorian-from-absolute d)
  18903. s (point))
  18904. (if (or (setq todayp (= d today))
  18905. (and (not start-pos) (= d sd)))
  18906. (setq start-pos (point))
  18907. (if (and start-pos (not end-pos))
  18908. (setq end-pos (point))))
  18909. (setq files thefiles
  18910. rtnall nil)
  18911. (while (setq file (pop files))
  18912. (catch 'nextfile
  18913. (org-check-agenda-file file)
  18914. (if org-agenda-show-log
  18915. (setq rtn (org-agenda-get-day-entries
  18916. file date
  18917. :deadline :scheduled :timestamp :sexp :closed))
  18918. (setq rtn (org-agenda-get-day-entries
  18919. file date
  18920. :deadline :scheduled :sexp :timestamp)))
  18921. (setq rtnall (append rtnall rtn))))
  18922. (if org-agenda-include-diary
  18923. (progn
  18924. (require 'diary-lib)
  18925. (setq rtn (org-get-entries-from-diary date))
  18926. (setq rtnall (append rtnall rtn))))
  18927. (if (or rtnall org-agenda-show-all-dates)
  18928. (progn
  18929. (setq day-cnt (1+ day-cnt))
  18930. (insert
  18931. (if (stringp org-agenda-format-date)
  18932. (format-time-string org-agenda-format-date
  18933. (org-time-from-absolute date))
  18934. (funcall org-agenda-format-date date))
  18935. "\n")
  18936. (put-text-property s (1- (point)) 'face 'org-agenda-structure)
  18937. (put-text-property s (1- (point)) 'org-date-line t)
  18938. (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
  18939. (if todayp (put-text-property s (1- (point)) 'org-today t))
  18940. (if rtnall (insert
  18941. (org-finalize-agenda-entries
  18942. (org-agenda-add-time-grid-maybe
  18943. rtnall nd todayp))
  18944. "\n"))
  18945. (put-text-property s (1- (point)) 'day d)
  18946. (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
  18947. (goto-char (point-min))
  18948. (org-fit-agenda-window)
  18949. (unless (and (pos-visible-in-window-p (point-min))
  18950. (pos-visible-in-window-p (point-max)))
  18951. (goto-char (1- (point-max)))
  18952. (recenter -1)
  18953. (if (not (pos-visible-in-window-p (or start-pos 1)))
  18954. (progn
  18955. (goto-char (or start-pos 1))
  18956. (recenter 1))))
  18957. (goto-char (or start-pos 1))
  18958. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  18959. (org-finalize-agenda)
  18960. (setq buffer-read-only t)
  18961. (message "")))
  18962. (defun org-agenda-ndays-to-span (n)
  18963. (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
  18964. ;;; Agenda TODO list
  18965. (defvar org-select-this-todo-keyword nil)
  18966. (defvar org-last-arg nil)
  18967. ;;;###autoload
  18968. (defun org-todo-list (arg)
  18969. "Show all TODO entries from all agenda file in a single list.
  18970. The prefix arg can be used to select a specific TODO keyword and limit
  18971. the list to these. When using \\[universal-argument], you will be prompted
  18972. for a keyword. A numeric prefix directly selects the Nth keyword in
  18973. `org-todo-keywords-1'."
  18974. (interactive "P")
  18975. (require 'calendar)
  18976. (org-compile-prefix-format 'todo)
  18977. (org-set-sorting-strategy 'todo)
  18978. (org-prepare-agenda "TODO")
  18979. (let* ((today (time-to-days (current-time)))
  18980. (date (calendar-gregorian-from-absolute today))
  18981. (kwds org-todo-keywords-for-agenda)
  18982. (completion-ignore-case t)
  18983. (org-select-this-todo-keyword
  18984. (if (stringp arg) arg
  18985. (and arg (integerp arg) (> arg 0)
  18986. (nth (1- arg) kwds))))
  18987. rtn rtnall files file pos)
  18988. (when (equal arg '(4))
  18989. (setq org-select-this-todo-keyword
  18990. (completing-read "Keyword (or KWD1|K2D2|...): "
  18991. (mapcar 'list kwds) nil nil)))
  18992. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  18993. (org-set-local 'org-last-arg arg)
  18994. (setq org-agenda-redo-command
  18995. '(org-todo-list (or current-prefix-arg org-last-arg)))
  18996. (setq files (org-agenda-files)
  18997. rtnall nil)
  18998. (while (setq file (pop files))
  18999. (catch 'nextfile
  19000. (org-check-agenda-file file)
  19001. (setq rtn (org-agenda-get-day-entries file date :todo))
  19002. (setq rtnall (append rtnall rtn))))
  19003. (if org-agenda-overriding-header
  19004. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19005. nil 'face 'org-agenda-structure) "\n")
  19006. (insert "Global list of TODO items of type: ")
  19007. (add-text-properties (point-min) (1- (point))
  19008. (list 'face 'org-agenda-structure))
  19009. (setq pos (point))
  19010. (insert (or org-select-this-todo-keyword "ALL") "\n")
  19011. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19012. (setq pos (point))
  19013. (unless org-agenda-multi
  19014. (insert "Available with `N r': (0)ALL")
  19015. (let ((n 0) s)
  19016. (mapc (lambda (x)
  19017. (setq s (format "(%d)%s" (setq n (1+ n)) x))
  19018. (if (> (+ (current-column) (string-width s) 1) (frame-width))
  19019. (insert "\n "))
  19020. (insert " " s))
  19021. kwds))
  19022. (insert "\n"))
  19023. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19024. (when rtnall
  19025. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19026. (goto-char (point-min))
  19027. (org-fit-agenda-window)
  19028. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  19029. (org-finalize-agenda)
  19030. (setq buffer-read-only t)))
  19031. ;;; Agenda tags match
  19032. ;;;###autoload
  19033. (defun org-tags-view (&optional todo-only match)
  19034. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  19035. The prefix arg TODO-ONLY limits the search to TODO entries."
  19036. (interactive "P")
  19037. (org-compile-prefix-format 'tags)
  19038. (org-set-sorting-strategy 'tags)
  19039. (let* ((org-tags-match-list-sublevels
  19040. (if todo-only t org-tags-match-list-sublevels))
  19041. (completion-ignore-case t)
  19042. rtn rtnall files file pos matcher
  19043. buffer)
  19044. (setq matcher (org-make-tags-matcher match)
  19045. match (car matcher) matcher (cdr matcher))
  19046. (org-prepare-agenda (concat "TAGS " match))
  19047. (setq org-agenda-redo-command
  19048. (list 'org-tags-view (list 'quote todo-only)
  19049. (list 'if 'current-prefix-arg nil match)))
  19050. (setq files (org-agenda-files)
  19051. rtnall nil)
  19052. (while (setq file (pop files))
  19053. (catch 'nextfile
  19054. (org-check-agenda-file file)
  19055. (setq buffer (if (file-exists-p file)
  19056. (org-get-agenda-file-buffer file)
  19057. (error "No such file %s" file)))
  19058. (if (not buffer)
  19059. ;; If file does not exist, merror message to agenda
  19060. (setq rtn (list
  19061. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19062. rtnall (append rtnall rtn))
  19063. (with-current-buffer buffer
  19064. (unless (org-mode-p)
  19065. (error "Agenda file %s is not in `org-mode'" file))
  19066. (save-excursion
  19067. (save-restriction
  19068. (if org-agenda-restrict
  19069. (narrow-to-region org-agenda-restrict-begin
  19070. org-agenda-restrict-end)
  19071. (widen))
  19072. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  19073. (setq rtnall (append rtnall rtn))))))))
  19074. (if org-agenda-overriding-header
  19075. (insert (org-add-props (copy-sequence org-agenda-overriding-header)
  19076. nil 'face 'org-agenda-structure) "\n")
  19077. (insert "Headlines with TAGS match: ")
  19078. (add-text-properties (point-min) (1- (point))
  19079. (list 'face 'org-agenda-structure))
  19080. (setq pos (point))
  19081. (insert match "\n")
  19082. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  19083. (setq pos (point))
  19084. (unless org-agenda-multi
  19085. (insert "Press `C-u r' to search again with new search string\n"))
  19086. (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
  19087. (when rtnall
  19088. (insert (org-finalize-agenda-entries rtnall) "\n"))
  19089. (goto-char (point-min))
  19090. (org-fit-agenda-window)
  19091. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  19092. (org-finalize-agenda)
  19093. (setq buffer-read-only t)))
  19094. ;;; Agenda Finding stuck projects
  19095. (defvar org-agenda-skip-regexp nil
  19096. "Regular expression used in skipping subtrees for the agenda.
  19097. This is basically a temporary global variable that can be set and then
  19098. used by user-defined selections using `org-agenda-skip-function'.")
  19099. (defvar org-agenda-overriding-header nil
  19100. "When this is set during todo and tags searches, will replace header.")
  19101. (defun org-agenda-skip-subtree-when-regexp-matches ()
  19102. "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
  19103. If yes, it returns the end position of this tree, causing agenda commands
  19104. to skip this subtree. This is a function that can be put into
  19105. `org-agenda-skip-function' for the duration of a command."
  19106. (let ((end (save-excursion (org-end-of-subtree t)))
  19107. skip)
  19108. (save-excursion
  19109. (setq skip (re-search-forward org-agenda-skip-regexp end t)))
  19110. (and skip end)))
  19111. (defun org-agenda-skip-entry-if (&rest conditions)
  19112. "Skip entry if any of CONDITIONS is true.
  19113. See `org-agenda-skip-if' for details."
  19114. (org-agenda-skip-if nil conditions))
  19115. (defun org-agenda-skip-subtree-if (&rest conditions)
  19116. "Skip entry if any of CONDITIONS is true.
  19117. See `org-agenda-skip-if' for details."
  19118. (org-agenda-skip-if t conditions))
  19119. (defun org-agenda-skip-if (subtree conditions)
  19120. "Checks current entity for CONDITIONS.
  19121. If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
  19122. the entry, i.e. the text before the next heading is checked.
  19123. CONDITIONS is a list of symbols, boolean OR is used to combine the results
  19124. from different tests. Valid conditions are:
  19125. scheduled Check if there is a scheduled cookie
  19126. notscheduled Check if there is no scheduled cookie
  19127. deadline Check if there is a deadline
  19128. notdeadline Check if there is no deadline
  19129. regexp Check if regexp matches
  19130. notregexp Check if regexp does not match.
  19131. The regexp is taken from the conditions list, it must come right after
  19132. the `regexp' or `notregexp' element.
  19133. If any of these conditions is met, this function returns the end point of
  19134. the entity, causing the search to continue from there. This is a function
  19135. that can be put into `org-agenda-skip-function' for the duration of a command."
  19136. (let (beg end m)
  19137. (org-back-to-heading t)
  19138. (setq beg (point)
  19139. end (if subtree
  19140. (progn (org-end-of-subtree t) (point))
  19141. (progn (outline-next-heading) (1- (point)))))
  19142. (goto-char beg)
  19143. (and
  19144. (or
  19145. (and (memq 'scheduled conditions)
  19146. (re-search-forward org-scheduled-time-regexp end t))
  19147. (and (memq 'notscheduled conditions)
  19148. (not (re-search-forward org-scheduled-time-regexp end t)))
  19149. (and (memq 'deadline conditions)
  19150. (re-search-forward org-deadline-time-regexp end t))
  19151. (and (memq 'notdeadline conditions)
  19152. (not (re-search-forward org-deadline-time-regexp end t)))
  19153. (and (setq m (memq 'regexp conditions))
  19154. (stringp (nth 1 m))
  19155. (re-search-forward (nth 1 m) end t))
  19156. (and (setq m (memq 'notregexp conditions))
  19157. (stringp (nth 1 m))
  19158. (not (re-search-forward (nth 1 m) end t))))
  19159. end)))
  19160. ;;;###autoload
  19161. (defun org-agenda-list-stuck-projects (&rest ignore)
  19162. "Create agenda view for projects that are stuck.
  19163. Stuck projects are project that have no next actions. For the definitions
  19164. of what a project is and how to check if it stuck, customize the variable
  19165. `org-stuck-projects'.
  19166. MATCH is being ignored."
  19167. (interactive)
  19168. (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
  19169. ;; FIXME: we could have used org-agenda-skip-if here.
  19170. (org-agenda-overriding-header "List of stuck projects: ")
  19171. (matcher (nth 0 org-stuck-projects))
  19172. (todo (nth 1 org-stuck-projects))
  19173. (todo-wds (if (member "*" todo)
  19174. (progn
  19175. (org-prepare-agenda-buffers (org-agenda-files))
  19176. (org-delete-all
  19177. org-done-keywords-for-agenda
  19178. (copy-sequence org-todo-keywords-for-agenda)))
  19179. todo))
  19180. (todo-re (concat "^\\*+[ \t]+\\("
  19181. (mapconcat 'identity todo-wds "\\|")
  19182. "\\)\\>"))
  19183. (tags (nth 2 org-stuck-projects))
  19184. (tags-re (if (member "*" tags)
  19185. (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
  19186. (concat "^\\*+ .*:\\("
  19187. (mapconcat 'identity tags "\\|")
  19188. (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
  19189. (gen-re (nth 3 org-stuck-projects))
  19190. (re-list
  19191. (delq nil
  19192. (list
  19193. (if todo todo-re)
  19194. (if tags tags-re)
  19195. (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
  19196. gen-re)))))
  19197. (setq org-agenda-skip-regexp
  19198. (if re-list
  19199. (mapconcat 'identity re-list "\\|")
  19200. (error "No information how to identify unstuck projects")))
  19201. (org-tags-view nil matcher)
  19202. (with-current-buffer org-agenda-buffer-name
  19203. (setq org-agenda-redo-command
  19204. '(org-agenda-list-stuck-projects
  19205. (or current-prefix-arg org-last-arg))))))
  19206. ;;; Diary integration
  19207. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  19208. (defun org-get-entries-from-diary (date)
  19209. "Get the (Emacs Calendar) diary entries for DATE."
  19210. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  19211. (diary-display-hook '(fancy-diary-display))
  19212. (pop-up-frames nil)
  19213. (list-diary-entries-hook
  19214. (cons 'org-diary-default-entry list-diary-entries-hook))
  19215. (diary-file-name-prefix-function nil) ; turn this feature off
  19216. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  19217. entries
  19218. (org-disable-agenda-to-diary t))
  19219. (save-excursion
  19220. (save-window-excursion
  19221. (funcall (if (fboundp 'diary-list-entries)
  19222. 'diary-list-entries 'list-diary-entries)
  19223. date 1)))
  19224. (if (not (get-buffer fancy-diary-buffer))
  19225. (setq entries nil)
  19226. (with-current-buffer fancy-diary-buffer
  19227. (setq buffer-read-only nil)
  19228. (if (zerop (buffer-size))
  19229. ;; No entries
  19230. (setq entries nil)
  19231. ;; Omit the date and other unnecessary stuff
  19232. (org-agenda-cleanup-fancy-diary)
  19233. ;; Add prefix to each line and extend the text properties
  19234. (if (zerop (buffer-size))
  19235. (setq entries nil)
  19236. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  19237. (set-buffer-modified-p nil)
  19238. (kill-buffer fancy-diary-buffer)))
  19239. (when entries
  19240. (setq entries (org-split-string entries "\n"))
  19241. (setq entries
  19242. (mapcar
  19243. (lambda (x)
  19244. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  19245. ;; Extend the text properties to the beginning of the line
  19246. (org-add-props x (text-properties-at (1- (length x)) x)
  19247. 'type "diary" 'date date))
  19248. entries)))))
  19249. (defun org-agenda-cleanup-fancy-diary ()
  19250. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  19251. This gets rid of the date, the underline under the date, and
  19252. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  19253. date. It also removes lines that contain only whitespace."
  19254. (goto-char (point-min))
  19255. (if (looking-at ".*?:[ \t]*")
  19256. (progn
  19257. (replace-match "")
  19258. (re-search-forward "\n=+$" nil t)
  19259. (replace-match "")
  19260. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  19261. (re-search-forward "\n=+$" nil t)
  19262. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  19263. (goto-char (point-min))
  19264. (while (re-search-forward "^ +\n" nil t)
  19265. (replace-match ""))
  19266. (goto-char (point-min))
  19267. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  19268. (replace-match "")))
  19269. ;; Make sure entries from the diary have the right text properties.
  19270. (eval-after-load "diary-lib"
  19271. '(if (boundp 'diary-modify-entry-list-string-function)
  19272. ;; We can rely on the hook, nothing to do
  19273. nil
  19274. ;; Hook not avaiable, must use advice to make this work
  19275. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  19276. "Make the position visible."
  19277. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  19278. (stringp string)
  19279. buffer-file-name)
  19280. (setq string (org-modify-diary-entry-string string))))))
  19281. (defun org-modify-diary-entry-string (string)
  19282. "Add text properties to string, allowing org-mode to act on it."
  19283. (org-add-props string nil
  19284. 'mouse-face 'highlight
  19285. 'keymap org-agenda-keymap
  19286. 'help-echo (if buffer-file-name
  19287. (format "mouse-2 or RET jump to diary file %s"
  19288. (abbreviate-file-name buffer-file-name))
  19289. "")
  19290. 'org-agenda-diary-link t
  19291. 'org-marker (org-agenda-new-marker (point-at-bol))))
  19292. (defun org-diary-default-entry ()
  19293. "Add a dummy entry to the diary.
  19294. Needed to avoid empty dates which mess up holiday display."
  19295. ;; Catch the error if dealing with the new add-to-diary-alist
  19296. (when org-disable-agenda-to-diary
  19297. (condition-case nil
  19298. (add-to-diary-list original-date "Org-mode dummy" "")
  19299. (error
  19300. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  19301. ;;;###autoload
  19302. (defun org-diary (&rest args)
  19303. "Return diary information from org-files.
  19304. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  19305. It accesses org files and extracts information from those files to be
  19306. listed in the diary. The function accepts arguments specifying what
  19307. items should be listed. The following arguments are allowed:
  19308. :timestamp List the headlines of items containing a date stamp or
  19309. date range matching the selected date. Deadlines will
  19310. also be listed, on the expiration day.
  19311. :sexp List entries resulting from diary-like sexps.
  19312. :deadline List any deadlines past due, or due within
  19313. `org-deadline-warning-days'. The listing occurs only
  19314. in the diary for *today*, not at any other date. If
  19315. an entry is marked DONE, it is no longer listed.
  19316. :scheduled List all items which are scheduled for the given date.
  19317. The diary for *today* also contains items which were
  19318. scheduled earlier and are not yet marked DONE.
  19319. :todo List all TODO items from the org-file. This may be a
  19320. long list - so this is not turned on by default.
  19321. Like deadlines, these entries only show up in the
  19322. diary for *today*, not at any other date.
  19323. The call in the diary file should look like this:
  19324. &%%(org-diary) ~/path/to/some/orgfile.org
  19325. Use a separate line for each org file to check. Or, if you omit the file name,
  19326. all files listed in `org-agenda-files' will be checked automatically:
  19327. &%%(org-diary)
  19328. If you don't give any arguments (as in the example above), the default
  19329. arguments (:deadline :scheduled :timestamp :sexp) are used.
  19330. So the example above may also be written as
  19331. &%%(org-diary :deadline :timestamp :sexp :scheduled)
  19332. The function expects the lisp variables `entry' and `date' to be provided
  19333. by the caller, because this is how the calendar works. Don't use this
  19334. function from a program - use `org-agenda-get-day-entries' instead."
  19335. (org-agenda-maybe-reset-markers)
  19336. (org-compile-prefix-format 'agenda)
  19337. (org-set-sorting-strategy 'agenda)
  19338. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19339. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  19340. (list entry)
  19341. (org-agenda-files t)))
  19342. file rtn results)
  19343. (org-prepare-agenda-buffers files)
  19344. ;; If this is called during org-agenda, don't return any entries to
  19345. ;; the calendar. Org Agenda will list these entries itself.
  19346. (if org-disable-agenda-to-diary (setq files nil))
  19347. (while (setq file (pop files))
  19348. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  19349. (setq results (append results rtn)))
  19350. (if results
  19351. (concat (org-finalize-agenda-entries results) "\n"))))
  19352. ;;; Agenda entry finders
  19353. (defun org-agenda-get-day-entries (file date &rest args)
  19354. "Does the work for `org-diary' and `org-agenda'.
  19355. FILE is the path to a file to be checked for entries. DATE is date like
  19356. the one returned by `calendar-current-date'. ARGS are symbols indicating
  19357. which kind of entries should be extracted. For details about these, see
  19358. the documentation of `org-diary'."
  19359. (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
  19360. (let* ((org-startup-folded nil)
  19361. (org-startup-align-all-tables nil)
  19362. (buffer (if (file-exists-p file)
  19363. (org-get-agenda-file-buffer file)
  19364. (error "No such file %s" file)))
  19365. arg results rtn)
  19366. (if (not buffer)
  19367. ;; If file does not exist, make sure an error message ends up in diary
  19368. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  19369. (with-current-buffer buffer
  19370. (unless (org-mode-p)
  19371. (error "Agenda file %s is not in `org-mode'" file))
  19372. (let ((case-fold-search nil))
  19373. (save-excursion
  19374. (save-restriction
  19375. (if org-agenda-restrict
  19376. (narrow-to-region org-agenda-restrict-begin
  19377. org-agenda-restrict-end)
  19378. (widen))
  19379. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  19380. (while (setq arg (pop args))
  19381. (cond
  19382. ((and (eq arg :todo)
  19383. (equal date (calendar-current-date)))
  19384. (setq rtn (org-agenda-get-todos))
  19385. (setq results (append results rtn)))
  19386. ((eq arg :timestamp)
  19387. (setq rtn (org-agenda-get-blocks))
  19388. (setq results (append results rtn))
  19389. (setq rtn (org-agenda-get-timestamps))
  19390. (setq results (append results rtn)))
  19391. ((eq arg :sexp)
  19392. (setq rtn (org-agenda-get-sexps))
  19393. (setq results (append results rtn)))
  19394. ((eq arg :scheduled)
  19395. (setq rtn (org-agenda-get-scheduled))
  19396. (setq results (append results rtn)))
  19397. ((eq arg :closed)
  19398. (setq rtn (org-agenda-get-closed))
  19399. (setq results (append results rtn)))
  19400. ((eq arg :deadline)
  19401. (setq rtn (org-agenda-get-deadlines))
  19402. (setq results (append results rtn))))))))
  19403. results))))
  19404. (defun org-entry-is-todo-p ()
  19405. (member (org-get-todo-state) org-not-done-keywords))
  19406. (defun org-entry-is-done-p ()
  19407. (member (org-get-todo-state) org-done-keywords))
  19408. (defun org-get-todo-state ()
  19409. (save-excursion
  19410. (org-back-to-heading t)
  19411. (and (looking-at org-todo-line-regexp)
  19412. (match-end 2)
  19413. (match-string 2))))
  19414. (defun org-at-date-range-p (&optional inactive-ok)
  19415. "Is the cursor inside a date range?"
  19416. (interactive)
  19417. (save-excursion
  19418. (catch 'exit
  19419. (let ((pos (point)))
  19420. (skip-chars-backward "^[<\r\n")
  19421. (skip-chars-backward "<[")
  19422. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19423. (>= (match-end 0) pos)
  19424. (throw 'exit t))
  19425. (skip-chars-backward "^<[\r\n")
  19426. (skip-chars-backward "<[")
  19427. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  19428. (>= (match-end 0) pos)
  19429. (throw 'exit t)))
  19430. nil)))
  19431. (defun org-agenda-get-todos ()
  19432. "Return the TODO information for agenda display."
  19433. (let* ((props (list 'face nil
  19434. 'done-face 'org-done
  19435. 'org-not-done-regexp org-not-done-regexp
  19436. 'org-todo-regexp org-todo-regexp
  19437. 'mouse-face 'highlight
  19438. 'keymap org-agenda-keymap
  19439. 'help-echo
  19440. (format "mouse-2 or RET jump to org file %s"
  19441. (abbreviate-file-name buffer-file-name))))
  19442. ;; FIXME: get rid of the \n at some point but watch out
  19443. (regexp (concat "^\\*+[ \t]+\\("
  19444. (if org-select-this-todo-keyword
  19445. (if (equal org-select-this-todo-keyword "*")
  19446. org-todo-regexp
  19447. (concat "\\<\\("
  19448. (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
  19449. "\\)\\>"))
  19450. org-not-done-regexp)
  19451. "[^\n\r]*\\)"))
  19452. marker priority category tags
  19453. ee txt beg end)
  19454. (goto-char (point-min))
  19455. (while (re-search-forward regexp nil t)
  19456. (catch :skip
  19457. (save-match-data
  19458. (beginning-of-line)
  19459. (setq beg (point) end (progn (outline-next-heading) (point)))
  19460. (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
  19461. (re-search-forward org-ts-regexp end t))
  19462. (and org-agenda-todo-ignore-scheduled (goto-char beg)
  19463. (re-search-forward org-scheduled-time-regexp end t))
  19464. (and org-agenda-todo-ignore-deadlines (goto-char beg)
  19465. (re-search-forward org-deadline-time-regexp end t)
  19466. (org-deadline-close (match-string 1))))
  19467. (goto-char (1+ beg))
  19468. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  19469. (throw :skip nil)))
  19470. (goto-char beg)
  19471. (org-agenda-skip)
  19472. (goto-char (match-beginning 1))
  19473. (setq marker (org-agenda-new-marker (match-beginning 0))
  19474. category (org-get-category)
  19475. tags (org-get-tags-at (point))
  19476. txt (org-format-agenda-item "" (match-string 1) category tags)
  19477. priority (1+ (org-get-priority txt)))
  19478. (org-add-props txt props
  19479. 'org-marker marker 'org-hd-marker marker
  19480. 'priority priority 'org-category category
  19481. 'type "todo")
  19482. (push txt ee)
  19483. (if org-agenda-todo-list-sublevels
  19484. (goto-char (match-end 1))
  19485. (org-end-of-subtree 'invisible))))
  19486. (nreverse ee)))
  19487. (defconst org-agenda-no-heading-message
  19488. "No heading for this item in buffer or region.")
  19489. (defun org-agenda-get-timestamps ()
  19490. "Return the date stamp information for agenda display."
  19491. (let* ((props (list 'face nil
  19492. 'org-not-done-regexp org-not-done-regexp
  19493. 'org-todo-regexp org-todo-regexp
  19494. 'mouse-face 'highlight
  19495. 'keymap org-agenda-keymap
  19496. 'help-echo
  19497. (format "mouse-2 or RET jump to org file %s"
  19498. (abbreviate-file-name buffer-file-name))))
  19499. (d1 (calendar-absolute-from-gregorian date))
  19500. (remove-re
  19501. (concat
  19502. (regexp-quote
  19503. (format-time-string
  19504. "<%Y-%m-%d"
  19505. (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19506. ".*?>"))
  19507. (regexp
  19508. (concat
  19509. (regexp-quote
  19510. (substring
  19511. (format-time-string
  19512. (car org-time-stamp-formats)
  19513. (apply 'encode-time ; DATE bound by calendar
  19514. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19515. 0 11))
  19516. "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  19517. "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
  19518. marker hdmarker deadlinep scheduledp donep tmp priority category
  19519. ee txt timestr tags b0 b3 e3 head)
  19520. (goto-char (point-min))
  19521. (while (re-search-forward regexp nil t)
  19522. (setq b0 (match-beginning 0)
  19523. b3 (match-beginning 3) e3 (match-end 3))
  19524. (catch :skip
  19525. (and (org-at-date-range-p) (throw :skip nil))
  19526. (org-agenda-skip)
  19527. (if (and (match-end 1)
  19528. (not (= d1 (org-time-string-to-absolute (match-string 1) d1))))
  19529. (throw :skip nil))
  19530. (if (and e3
  19531. (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
  19532. (throw :skip nil))
  19533. (setq marker (org-agenda-new-marker b0)
  19534. category (org-get-category b0)
  19535. tmp (buffer-substring (max (point-min)
  19536. (- b0 org-ds-keyword-length))
  19537. b0)
  19538. timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
  19539. deadlinep (string-match org-deadline-regexp tmp)
  19540. scheduledp (string-match org-scheduled-regexp tmp)
  19541. donep (org-entry-is-done-p))
  19542. (if (or scheduledp deadlinep) (throw :skip t))
  19543. (if (string-match ">" timestr)
  19544. ;; substring should only run to end of time stamp
  19545. (setq timestr (substring timestr 0 (match-end 0))))
  19546. (save-excursion
  19547. (if (re-search-backward "^\\*+ " nil t)
  19548. (progn
  19549. (goto-char (match-beginning 0))
  19550. (setq hdmarker (org-agenda-new-marker)
  19551. tags (org-get-tags-at))
  19552. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19553. (setq head (match-string 1))
  19554. (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
  19555. (setq txt (org-format-agenda-item
  19556. nil head category tags timestr nil
  19557. remove-re)))
  19558. (setq txt org-agenda-no-heading-message))
  19559. (setq priority (org-get-priority txt))
  19560. (org-add-props txt props
  19561. 'org-marker marker 'org-hd-marker hdmarker)
  19562. (org-add-props txt nil 'priority priority
  19563. 'org-category category 'date date
  19564. 'type "timestamp")
  19565. (push txt ee))
  19566. (outline-next-heading)))
  19567. (nreverse ee)))
  19568. (defun org-agenda-get-sexps ()
  19569. "Return the sexp information for agenda display."
  19570. (require 'diary-lib)
  19571. (let* ((props (list 'face nil
  19572. 'mouse-face 'highlight
  19573. 'keymap org-agenda-keymap
  19574. 'help-echo
  19575. (format "mouse-2 or RET jump to org file %s"
  19576. (abbreviate-file-name buffer-file-name))))
  19577. (regexp "^&?%%(")
  19578. marker category ee txt tags entry result beg b sexp sexp-entry)
  19579. (goto-char (point-min))
  19580. (while (re-search-forward regexp nil t)
  19581. (catch :skip
  19582. (org-agenda-skip)
  19583. (setq beg (match-beginning 0))
  19584. (goto-char (1- (match-end 0)))
  19585. (setq b (point))
  19586. (forward-sexp 1)
  19587. (setq sexp (buffer-substring b (point)))
  19588. (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
  19589. (org-trim (match-string 1))
  19590. ""))
  19591. (setq result (org-diary-sexp-entry sexp sexp-entry date))
  19592. (when result
  19593. (setq marker (org-agenda-new-marker beg)
  19594. category (org-get-category beg))
  19595. (if (string-match "\\S-" result)
  19596. (setq txt result)
  19597. (setq txt "SEXP entry returned empty string"))
  19598. (setq txt (org-format-agenda-item
  19599. "" txt category tags 'time))
  19600. (org-add-props txt props 'org-marker marker)
  19601. (org-add-props txt nil
  19602. 'org-category category 'date date
  19603. 'type "sexp")
  19604. (push txt ee))))
  19605. (nreverse ee)))
  19606. (defun org-agenda-get-closed ()
  19607. "Return the logged TODO entries for agenda display."
  19608. (let* ((props (list 'mouse-face 'highlight
  19609. 'org-not-done-regexp org-not-done-regexp
  19610. 'org-todo-regexp org-todo-regexp
  19611. 'keymap org-agenda-keymap
  19612. 'help-echo
  19613. (format "mouse-2 or RET jump to org file %s"
  19614. (abbreviate-file-name buffer-file-name))))
  19615. (regexp (concat
  19616. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  19617. (regexp-quote
  19618. (substring
  19619. (format-time-string
  19620. (car org-time-stamp-formats)
  19621. (apply 'encode-time ; DATE bound by calendar
  19622. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  19623. 1 11))))
  19624. marker hdmarker priority category tags closedp
  19625. ee txt timestr)
  19626. (goto-char (point-min))
  19627. (while (re-search-forward regexp nil t)
  19628. (catch :skip
  19629. (org-agenda-skip)
  19630. (setq marker (org-agenda-new-marker (match-beginning 0))
  19631. closedp (equal (match-string 1) org-closed-string)
  19632. category (org-get-category (match-beginning 0))
  19633. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  19634. ;; donep (org-entry-is-done-p)
  19635. )
  19636. (if (string-match "\\]" timestr)
  19637. ;; substring should only run to end of time stamp
  19638. (setq timestr (substring timestr 0 (match-end 0))))
  19639. (save-excursion
  19640. (if (re-search-backward "^\\*+ " nil t)
  19641. (progn
  19642. (goto-char (match-beginning 0))
  19643. (setq hdmarker (org-agenda-new-marker)
  19644. tags (org-get-tags-at))
  19645. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19646. (setq txt (org-format-agenda-item
  19647. (if closedp "Closed: " "Clocked: ")
  19648. (match-string 1) category tags timestr)))
  19649. (setq txt org-agenda-no-heading-message))
  19650. (setq priority 100000)
  19651. (org-add-props txt props
  19652. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  19653. 'priority priority 'org-category category
  19654. 'type "closed" 'date date
  19655. 'undone-face 'org-warning 'done-face 'org-done)
  19656. (push txt ee))
  19657. (goto-char (point-at-eol))))
  19658. ; (outline-next-heading)))
  19659. (nreverse ee)))
  19660. (defun org-agenda-get-deadlines ()
  19661. "Return the deadline information for agenda display."
  19662. (let* ((props (list 'mouse-face 'highlight
  19663. 'org-not-done-regexp org-not-done-regexp
  19664. 'org-todo-regexp org-todo-regexp
  19665. 'keymap org-agenda-keymap
  19666. 'help-echo
  19667. (format "mouse-2 or RET jump to org file %s"
  19668. (abbreviate-file-name buffer-file-name))))
  19669. (regexp org-deadline-time-regexp)
  19670. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19671. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19672. d2 diff dfrac wdays pos pos1 category tags
  19673. ee txt head face s upcomingp donep timestr)
  19674. (goto-char (point-min))
  19675. (while (re-search-forward regexp nil t)
  19676. (catch :skip
  19677. (org-agenda-skip)
  19678. (setq s (match-string 1)
  19679. pos (1- (match-beginning 1))
  19680. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19681. diff (- d2 d1)
  19682. wdays (org-get-wdays s)
  19683. dfrac (/ (* 1.0 (- wdays diff)) wdays)
  19684. upcomingp (and todayp (> diff 0)))
  19685. ;; When to show a deadline in the calendar:
  19686. ;; If the expiration is within wdays warning time.
  19687. ;; Past-due deadlines are only shown on the current date
  19688. (if (or (and (<= diff wdays)
  19689. (and todayp (not org-agenda-only-exact-dates)))
  19690. (= diff 0))
  19691. (save-excursion
  19692. (setq category (org-get-category))
  19693. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19694. (progn
  19695. (goto-char (match-end 0))
  19696. (setq pos1 (match-beginning 0))
  19697. (setq tags (org-get-tags-at pos1))
  19698. (setq head (buffer-substring-no-properties
  19699. (point)
  19700. (progn (skip-chars-forward "^\r\n")
  19701. (point))))
  19702. (setq donep (string-match org-looking-at-done-regexp head))
  19703. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19704. (setq timestr
  19705. (concat (substring s (match-beginning 1)) " "))
  19706. (setq timestr 'time))
  19707. (if (and donep
  19708. (or org-agenda-skip-deadline-if-done
  19709. (not (= diff 0))))
  19710. (setq txt nil)
  19711. (setq txt (org-format-agenda-item
  19712. (if (= diff 0)
  19713. (car org-agenda-deadline-leaders)
  19714. (format (nth 1 org-agenda-deadline-leaders)
  19715. diff))
  19716. head category tags timestr))))
  19717. (setq txt org-agenda-no-heading-message))
  19718. (when txt
  19719. (setq face (org-agenda-deadline-face dfrac))
  19720. (org-add-props txt props
  19721. 'org-marker (org-agenda-new-marker pos)
  19722. 'org-hd-marker (org-agenda-new-marker pos1)
  19723. 'priority (+ (if upcomingp (floor (* dfrac 10.)) 100)
  19724. (org-get-priority txt))
  19725. 'org-category category
  19726. 'type (if upcomingp "upcoming-deadline" "deadline")
  19727. 'date (if upcomingp date d2)
  19728. 'face (if donep 'org-done face)
  19729. 'undone-face face 'done-face 'org-done)
  19730. (push txt ee))))))
  19731. (nreverse ee)))
  19732. (defun org-agenda-deadline-face (fraction)
  19733. "Return the face to displaying a deadline item.
  19734. FRACTION is what fraction of the head-warning time has passed."
  19735. (let ((faces org-agenda-deadline-faces) f)
  19736. (catch 'exit
  19737. (while (setq f (pop faces))
  19738. (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
  19739. (defun org-agenda-get-scheduled ()
  19740. "Return the scheduled information for agenda display."
  19741. (let* ((props (list 'org-not-done-regexp org-not-done-regexp
  19742. 'org-todo-regexp org-todo-regexp
  19743. 'done-face 'org-done
  19744. 'mouse-face 'highlight
  19745. 'keymap org-agenda-keymap
  19746. 'help-echo
  19747. (format "mouse-2 or RET jump to org file %s"
  19748. (abbreviate-file-name buffer-file-name))))
  19749. (regexp org-scheduled-time-regexp)
  19750. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  19751. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  19752. d2 diff pos pos1 category tags
  19753. ee txt head pastschedp donep face timestr s)
  19754. (goto-char (point-min))
  19755. (while (re-search-forward regexp nil t)
  19756. (catch :skip
  19757. (org-agenda-skip)
  19758. (setq s (match-string 1)
  19759. pos (1- (match-beginning 1))
  19760. d2 (org-time-string-to-absolute (match-string 1) d1 'past)
  19761. ;;; is this right?
  19762. ;;; do we need to do this for deadleine too????
  19763. ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
  19764. diff (- d2 d1))
  19765. (setq pastschedp (and todayp (< diff 0)))
  19766. ;; When to show a scheduled item in the calendar:
  19767. ;; If it is on or past the date.
  19768. (if (or (and (< diff 0)
  19769. (and todayp (not org-agenda-only-exact-dates)))
  19770. (= diff 0))
  19771. (save-excursion
  19772. (setq category (org-get-category))
  19773. (if (re-search-backward "^\\*+[ \t]+" nil t)
  19774. (progn
  19775. (goto-char (match-end 0))
  19776. (setq pos1 (match-beginning 0))
  19777. (setq tags (org-get-tags-at))
  19778. (setq head (buffer-substring-no-properties
  19779. (point)
  19780. (progn (skip-chars-forward "^\r\n") (point))))
  19781. (setq donep (string-match org-looking-at-done-regexp head))
  19782. (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
  19783. (setq timestr
  19784. (concat (substring s (match-beginning 1)) " "))
  19785. (setq timestr 'time))
  19786. (if (and donep
  19787. (or org-agenda-skip-scheduled-if-done
  19788. (not (= diff 0))))
  19789. (setq txt nil)
  19790. (setq txt (org-format-agenda-item
  19791. (if (= diff 0)
  19792. (car org-agenda-scheduled-leaders)
  19793. (format (nth 1 org-agenda-scheduled-leaders)
  19794. (- 1 diff)))
  19795. head category tags timestr))))
  19796. (setq txt org-agenda-no-heading-message))
  19797. (when txt
  19798. (setq face (if pastschedp
  19799. 'org-scheduled-previously
  19800. 'org-scheduled-today))
  19801. (org-add-props txt props
  19802. 'undone-face face
  19803. 'face (if donep 'org-done face)
  19804. 'org-marker (org-agenda-new-marker pos)
  19805. 'org-hd-marker (org-agenda-new-marker pos1)
  19806. 'type (if pastschedp "past-scheduled" "scheduled")
  19807. 'date (if pastschedp d2 date)
  19808. 'priority (+ 94 (- 5 diff) (org-get-priority txt))
  19809. 'org-category category)
  19810. (push txt ee))))))
  19811. (nreverse ee)))
  19812. (defun org-agenda-get-blocks ()
  19813. "Return the date-range information for agenda display."
  19814. (let* ((props (list 'face nil
  19815. 'org-not-done-regexp org-not-done-regexp
  19816. 'org-todo-regexp org-todo-regexp
  19817. 'mouse-face 'highlight
  19818. 'keymap org-agenda-keymap
  19819. 'help-echo
  19820. (format "mouse-2 or RET jump to org file %s"
  19821. (abbreviate-file-name buffer-file-name))))
  19822. (regexp org-tr-regexp)
  19823. (d0 (calendar-absolute-from-gregorian date))
  19824. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
  19825. donep head)
  19826. (goto-char (point-min))
  19827. (while (re-search-forward regexp nil t)
  19828. (catch :skip
  19829. (org-agenda-skip)
  19830. (setq pos (point))
  19831. (setq timestr (match-string 0)
  19832. s1 (match-string 1)
  19833. s2 (match-string 2)
  19834. d1 (time-to-days (org-time-string-to-time s1))
  19835. d2 (time-to-days (org-time-string-to-time s2)))
  19836. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  19837. ;; Only allow days between the limits, because the normal
  19838. ;; date stamps will catch the limits.
  19839. (save-excursion
  19840. (setq marker (org-agenda-new-marker (point)))
  19841. (setq category (org-get-category))
  19842. (if (re-search-backward "^\\*+ " nil t)
  19843. (progn
  19844. (goto-char (match-beginning 0))
  19845. (setq hdmarker (org-agenda-new-marker (point)))
  19846. (setq tags (org-get-tags-at))
  19847. (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
  19848. (setq head (match-string 1))
  19849. (and org-agenda-skip-timestamp-if-done
  19850. (org-entry-is-done-p)
  19851. (throw :skip t))
  19852. (setq txt (org-format-agenda-item
  19853. (format (if (= d1 d2) "" "(%d/%d): ")
  19854. (1+ (- d0 d1)) (1+ (- d2 d1)))
  19855. head category tags
  19856. (if (= d0 d1) timestr))))
  19857. (setq txt org-agenda-no-heading-message))
  19858. (org-add-props txt props
  19859. 'org-marker marker 'org-hd-marker hdmarker
  19860. 'type "block" 'date date
  19861. 'priority (org-get-priority txt) 'org-category category)
  19862. (push txt ee)))
  19863. (goto-char pos)))
  19864. ;; Sort the entries by expiration date.
  19865. (nreverse ee)))
  19866. ;;; Agenda presentation and sorting
  19867. (defconst org-plain-time-of-day-regexp
  19868. (concat
  19869. "\\(\\<[012]?[0-9]"
  19870. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19871. "\\(--?"
  19872. "\\(\\<[012]?[0-9]"
  19873. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19874. "\\)?")
  19875. "Regular expression to match a plain time or time range.
  19876. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  19877. groups carry important information:
  19878. 0 the full match
  19879. 1 the first time, range or not
  19880. 8 the second time, if it is a range.")
  19881. (defconst org-plain-time-extension-regexp
  19882. (concat
  19883. "\\(\\<[012]?[0-9]"
  19884. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  19885. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  19886. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  19887. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  19888. groups carry important information:
  19889. 0 the full match
  19890. 7 hours of duration
  19891. 9 minutes of duration")
  19892. (defconst org-stamp-time-of-day-regexp
  19893. (concat
  19894. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  19895. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  19896. "\\(--?"
  19897. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  19898. "Regular expression to match a timestamp time or time range.
  19899. After a match, the following groups carry important information:
  19900. 0 the full match
  19901. 1 date plus weekday, for backreferencing to make sure both times on same day
  19902. 2 the first time, range or not
  19903. 4 the second time, if it is a range.")
  19904. (defvar org-prefix-has-time nil
  19905. "A flag, set by `org-compile-prefix-format'.
  19906. The flag is set if the currently compiled format contains a `%t'.")
  19907. (defvar org-prefix-has-tag nil
  19908. "A flag, set by `org-compile-prefix-format'.
  19909. The flag is set if the currently compiled format contains a `%T'.")
  19910. (defun org-format-agenda-item (extra txt &optional category tags dotime
  19911. noprefix remove-re)
  19912. "Format TXT to be inserted into the agenda buffer.
  19913. In particular, it adds the prefix and corresponding text properties. EXTRA
  19914. must be a string and replaces the `%s' specifier in the prefix format.
  19915. CATEGORY (string, symbol or nil) may be used to overrule the default
  19916. category taken from local variable or file name. It will replace the `%c'
  19917. specifier in the format. DOTIME, when non-nil, indicates that a
  19918. time-of-day should be extracted from TXT for sorting of this entry, and for
  19919. the `%t' specifier in the format. When DOTIME is a string, this string is
  19920. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  19921. only the correctly processes TXT should be returned - this is used by
  19922. `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
  19923. Any match of REMOVE-RE will be removed from TXT."
  19924. (save-match-data
  19925. ;; Diary entries sometimes have extra whitespace at the beginning
  19926. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  19927. (let* ((category (or category
  19928. org-category
  19929. (if buffer-file-name
  19930. (file-name-sans-extension
  19931. (file-name-nondirectory buffer-file-name))
  19932. "")))
  19933. (tag (if tags (nth (1- (length tags)) tags) ""))
  19934. time ; time and tag are needed for the eval of the prefix format
  19935. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  19936. (time-of-day (and dotime (org-get-time-of-day ts)))
  19937. stamp plain s0 s1 s2 rtn srp)
  19938. (when (and dotime time-of-day org-prefix-has-time)
  19939. ;; Extract starting and ending time and move them to prefix
  19940. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  19941. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  19942. (setq s0 (match-string 0 ts)
  19943. srp (and stamp (match-end 3))
  19944. s1 (match-string (if plain 1 2) ts)
  19945. s2 (match-string (if plain 8 (if srp 4 6)) ts))
  19946. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  19947. ;; them, we might want to remove them there to avoid duplication.
  19948. ;; The user can turn this off with a variable.
  19949. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  19950. (string-match (concat (regexp-quote s0) " *") txt)
  19951. (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
  19952. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  19953. (= (match-beginning 0) 0)
  19954. t))
  19955. (setq txt (replace-match "" nil nil txt))))
  19956. ;; Normalize the time(s) to 24 hour
  19957. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  19958. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  19959. (when (and s1 (not s2) org-agenda-default-appointment-duration
  19960. (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
  19961. (let ((m (+ (string-to-number (match-string 2 s1))
  19962. (* 60 (string-to-number (match-string 1 s1)))
  19963. org-agenda-default-appointment-duration))
  19964. h)
  19965. (setq h (/ m 60) m (- m (* h 60)))
  19966. (setq s2 (format "%02d:%02d" h m))))
  19967. (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  19968. txt)
  19969. ;; Tags are in the string
  19970. (if (or (eq org-agenda-remove-tags t)
  19971. (and org-agenda-remove-tags
  19972. org-prefix-has-tag))
  19973. (setq txt (replace-match "" t t txt))
  19974. (setq txt (replace-match
  19975. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  19976. (match-string 2 txt))
  19977. t t txt))))
  19978. (when remove-re
  19979. (while (string-match remove-re txt)
  19980. (setq txt (replace-match "" t t txt))))
  19981. ;; Create the final string
  19982. (if noprefix
  19983. (setq rtn txt)
  19984. ;; Prepare the variables needed in the eval of the compiled format
  19985. (setq time (cond (s2 (concat s1 "-" s2))
  19986. (s1 (concat s1 "......"))
  19987. (t ""))
  19988. extra (or extra "")
  19989. category (if (symbolp category) (symbol-name category) category))
  19990. ;; Evaluate the compiled format
  19991. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  19992. ;; And finally add the text properties
  19993. (org-add-props rtn nil
  19994. 'org-category (downcase category) 'tags tags
  19995. 'org-highest-priority org-highest-priority
  19996. 'org-lowest-priority org-lowest-priority
  19997. 'prefix-length (- (length rtn) (length txt))
  19998. 'time-of-day time-of-day
  19999. 'txt txt
  20000. 'time time
  20001. 'extra extra
  20002. 'dotime dotime))))
  20003. (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
  20004. (defvar org-agenda-sorting-strategy-selected nil)
  20005. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  20006. (catch 'exit
  20007. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  20008. ((and todayp (member 'today (car org-agenda-time-grid))))
  20009. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  20010. ((member 'weekly (car org-agenda-time-grid)))
  20011. (t (throw 'exit list)))
  20012. (let* ((have (delq nil (mapcar
  20013. (lambda (x) (get-text-property 1 'time-of-day x))
  20014. list)))
  20015. (string (nth 1 org-agenda-time-grid))
  20016. (gridtimes (nth 2 org-agenda-time-grid))
  20017. (req (car org-agenda-time-grid))
  20018. (remove (member 'remove-match req))
  20019. new time)
  20020. (if (and (member 'require-timed req) (not have))
  20021. ;; don't show empty grid
  20022. (throw 'exit list))
  20023. (while (setq time (pop gridtimes))
  20024. (unless (and remove (member time have))
  20025. (setq time (int-to-string time))
  20026. (push (org-format-agenda-item
  20027. nil string "" nil
  20028. (concat (substring time 0 -2) ":" (substring time -2)))
  20029. new)
  20030. (put-text-property
  20031. 1 (length (car new)) 'face 'org-time-grid (car new))))
  20032. (if (member 'time-up org-agenda-sorting-strategy-selected)
  20033. (append new list)
  20034. (append list new)))))
  20035. (defun org-compile-prefix-format (key)
  20036. "Compile the prefix format into a Lisp form that can be evaluated.
  20037. The resulting form is returned and stored in the variable
  20038. `org-prefix-format-compiled'."
  20039. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  20040. (let ((s (cond
  20041. ((stringp org-agenda-prefix-format)
  20042. org-agenda-prefix-format)
  20043. ((assq key org-agenda-prefix-format)
  20044. (cdr (assq key org-agenda-prefix-format)))
  20045. (t " %-12:c%?-12t% s")))
  20046. (start 0)
  20047. varform vars var e c f opt)
  20048. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  20049. s start)
  20050. (setq var (cdr (assoc (match-string 4 s)
  20051. '(("c" . category) ("t" . time) ("s" . extra)
  20052. ("T" . tag))))
  20053. c (or (match-string 3 s) "")
  20054. opt (match-beginning 1)
  20055. start (1+ (match-beginning 0)))
  20056. (if (equal var 'time) (setq org-prefix-has-time t))
  20057. (if (equal var 'tag) (setq org-prefix-has-tag t))
  20058. (setq f (concat "%" (match-string 2 s) "s"))
  20059. (if opt
  20060. (setq varform
  20061. `(if (equal "" ,var)
  20062. ""
  20063. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  20064. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  20065. (setq s (replace-match "%s" t nil s))
  20066. (push varform vars))
  20067. (setq vars (nreverse vars))
  20068. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  20069. (defun org-set-sorting-strategy (key)
  20070. (if (symbolp (car org-agenda-sorting-strategy))
  20071. ;; the old format
  20072. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  20073. (setq org-agenda-sorting-strategy-selected
  20074. (or (cdr (assq key org-agenda-sorting-strategy))
  20075. (cdr (assq 'agenda org-agenda-sorting-strategy))
  20076. '(time-up category-keep priority-down)))))
  20077. (defun org-get-time-of-day (s &optional string mod24)
  20078. "Check string S for a time of day.
  20079. If found, return it as a military time number between 0 and 2400.
  20080. If not found, return nil.
  20081. The optional STRING argument forces conversion into a 5 character wide string
  20082. HH:MM."
  20083. (save-match-data
  20084. (when
  20085. (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  20086. (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  20087. (let* ((h (string-to-number (match-string 1 s)))
  20088. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  20089. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  20090. (am-p (equal ampm "am"))
  20091. (h1 (cond ((not ampm) h)
  20092. ((= h 12) (if am-p 0 12))
  20093. (t (+ h (if am-p 0 12)))))
  20094. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  20095. (mod h1 24) h1))
  20096. (t0 (+ (* 100 h2) m))
  20097. (t1 (concat (if (>= h1 24) "+" " ")
  20098. (if (< t0 100) "0" "")
  20099. (if (< t0 10) "0" "")
  20100. (int-to-string t0))))
  20101. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  20102. (defun org-finalize-agenda-entries (list &optional nosort)
  20103. "Sort and concatenate the agenda items."
  20104. (setq list (mapcar 'org-agenda-highlight-todo list))
  20105. (if nosort
  20106. list
  20107. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  20108. (defun org-agenda-highlight-todo (x)
  20109. (let (re pl)
  20110. (if (eq x 'line)
  20111. (save-excursion
  20112. (beginning-of-line 1)
  20113. (setq re (get-text-property (point) 'org-todo-regexp))
  20114. (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
  20115. (when (looking-at (concat "[ \t]*\\.*" re " +"))
  20116. (add-text-properties (match-beginning 0) (match-end 0)
  20117. (list 'face (org-get-todo-face 0)))
  20118. (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
  20119. (delete-region (match-beginning 1) (1- (match-end 0)))
  20120. (goto-char (match-beginning 1))
  20121. (insert (format org-agenda-todo-keyword-format s)))))
  20122. (setq re (concat (get-text-property 0 'org-todo-regexp x))
  20123. pl (get-text-property 0 'prefix-length x))
  20124. ; (and re (equal (string-match (concat "\\(\\.*\\)" re) x (or pl 0)) pl)
  20125. ; (add-text-properties
  20126. ; (or (match-end 1) (match-end 0)) (match-end 0)
  20127. ; (list 'face (org-get-todo-face (match-string 2 x)))
  20128. ; x))
  20129. (when (and re
  20130. (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
  20131. x (or pl 0)) pl))
  20132. (add-text-properties
  20133. (or (match-end 1) (match-end 0)) (match-end 0)
  20134. (list 'face (org-get-todo-face (match-string 2 x)))
  20135. x)
  20136. (setq x (concat (substring x 0 (match-end 1))
  20137. (format org-agenda-todo-keyword-format
  20138. (match-string 2 x))
  20139. " "
  20140. (substring x (match-end 3)))))
  20141. x)))
  20142. (defsubst org-cmp-priority (a b)
  20143. "Compare the priorities of string A and B."
  20144. (let ((pa (or (get-text-property 1 'priority a) 0))
  20145. (pb (or (get-text-property 1 'priority b) 0)))
  20146. (cond ((> pa pb) +1)
  20147. ((< pa pb) -1)
  20148. (t nil))))
  20149. (defsubst org-cmp-category (a b)
  20150. "Compare the string values of categories of strings A and B."
  20151. (let ((ca (or (get-text-property 1 'org-category a) ""))
  20152. (cb (or (get-text-property 1 'org-category b) "")))
  20153. (cond ((string-lessp ca cb) -1)
  20154. ((string-lessp cb ca) +1)
  20155. (t nil))))
  20156. (defsubst org-cmp-tag (a b)
  20157. "Compare the string values of categories of strings A and B."
  20158. (let ((ta (car (last (get-text-property 1 'tags a))))
  20159. (tb (car (last (get-text-property 1 'tags b)))))
  20160. (cond ((not ta) +1)
  20161. ((not tb) -1)
  20162. ((string-lessp ta tb) -1)
  20163. ((string-lessp tb ta) +1)
  20164. (t nil))))
  20165. (defsubst org-cmp-time (a b)
  20166. "Compare the time-of-day values of strings A and B."
  20167. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  20168. (ta (or (get-text-property 1 'time-of-day a) def))
  20169. (tb (or (get-text-property 1 'time-of-day b) def)))
  20170. (cond ((< ta tb) -1)
  20171. ((< tb ta) +1)
  20172. (t nil))))
  20173. (defun org-entries-lessp (a b)
  20174. "Predicate for sorting agenda entries."
  20175. ;; The following variables will be used when the form is evaluated.
  20176. ;; So even though the compiler complains, keep them.
  20177. (let* ((time-up (org-cmp-time a b))
  20178. (time-down (if time-up (- time-up) nil))
  20179. (priority-up (org-cmp-priority a b))
  20180. (priority-down (if priority-up (- priority-up) nil))
  20181. (category-up (org-cmp-category a b))
  20182. (category-down (if category-up (- category-up) nil))
  20183. (category-keep (if category-up +1 nil))
  20184. (tag-up (org-cmp-tag a b))
  20185. (tag-down (if tag-up (- tag-up) nil)))
  20186. (cdr (assoc
  20187. (eval (cons 'or org-agenda-sorting-strategy-selected))
  20188. '((-1 . t) (1 . nil) (nil . nil))))))
  20189. ;;; Agenda restriction lock
  20190. (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
  20191. "Overlay to mark the headline to which arenda commands are restricted.")
  20192. (org-overlay-put org-agenda-restriction-lock-overlay
  20193. 'face 'org-agenda-restriction-lock)
  20194. (org-overlay-put org-agenda-restriction-lock-overlay
  20195. 'help-echo "Agendas are currently limited to this subtree.")
  20196. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20197. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  20198. "Overlay marking the agenda restriction line in speedbar.")
  20199. (org-overlay-put org-speedbar-restriction-lock-overlay
  20200. 'face 'org-agenda-restriction-lock)
  20201. (org-overlay-put org-speedbar-restriction-lock-overlay
  20202. 'help-echo "Agendas are currently limited to this item.")
  20203. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20204. (defun org-agenda-set-restriction-lock (&optional type)
  20205. "Set restriction lock for agenda, to current subtree or file.
  20206. Restriction will be the file if TYPE is `file', or if type is the
  20207. universal prefix '(4), or if the cursor is before the first headline
  20208. in the file. Otherwise, restriction will be to the current subtree."
  20209. (interactive "P")
  20210. (and (equal type '(4)) (setq type 'file))
  20211. (setq type (cond
  20212. (type type)
  20213. ((org-at-heading-p) 'subtree)
  20214. ((condition-case nil (org-back-to-heading t) (error nil))
  20215. 'subtree)
  20216. (t 'file)))
  20217. (if (eq type 'subtree)
  20218. (progn
  20219. (setq org-agenda-restrict t)
  20220. (setq org-agenda-overriding-restriction 'subtree)
  20221. (put 'org-agenda-files 'org-restrict
  20222. (list (buffer-file-name (buffer-base-buffer))))
  20223. (org-back-to-heading t)
  20224. (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
  20225. (move-marker org-agenda-restrict-begin (point))
  20226. (move-marker org-agenda-restrict-end
  20227. (save-excursion (org-end-of-subtree t)))
  20228. (message "Locking agenda restriction to subtree"))
  20229. (put 'org-agenda-files 'org-restrict
  20230. (list (buffer-file-name (buffer-base-buffer))))
  20231. (setq org-agenda-restrict nil)
  20232. (setq org-agenda-overriding-restriction 'file)
  20233. (move-marker org-agenda-restrict-begin nil)
  20234. (move-marker org-agenda-restrict-end nil)
  20235. (message "Locking agenda restriction to file"))
  20236. (setq current-prefix-arg nil)
  20237. (org-agenda-maybe-redo))
  20238. (defun org-agenda-remove-restriction-lock (&optional noupdate)
  20239. "Remove the agenda restriction lock."
  20240. (interactive "P")
  20241. (org-detach-overlay org-agenda-restriction-lock-overlay)
  20242. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20243. (setq org-agenda-overriding-restriction nil)
  20244. (setq org-agenda-restrict nil)
  20245. (put 'org-agenda-files 'org-restrict nil)
  20246. (move-marker org-agenda-restrict-begin nil)
  20247. (move-marker org-agenda-restrict-end nil)
  20248. (setq current-prefix-arg nil)
  20249. (message "Agenda restriction lock removed")
  20250. (or noupdate (org-agenda-maybe-redo)))
  20251. (defun org-agenda-maybe-redo ()
  20252. "If there is any window showing the agenda view, update it."
  20253. (let ((w (get-buffer-window org-agenda-buffer-name t))
  20254. (w0 (selected-window)))
  20255. (when w
  20256. (select-window w)
  20257. (org-agenda-redo)
  20258. (select-window w0)
  20259. (if org-agenda-overriding-restriction
  20260. (message "Agenda view shifted to new %s restriction"
  20261. org-agenda-overriding-restriction)
  20262. (message "Agenda restriction lock removed")))))
  20263. ;;; Agenda commands
  20264. (defun org-agenda-check-type (error &rest types)
  20265. "Check if agenda buffer is of allowed type.
  20266. If ERROR is non-nil, throw an error, otherwise just return nil."
  20267. (if (memq org-agenda-type types)
  20268. t
  20269. (if error
  20270. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  20271. nil)))
  20272. (defun org-agenda-quit ()
  20273. "Exit agenda by removing the window or the buffer."
  20274. (interactive)
  20275. (let ((buf (current-buffer)))
  20276. (if (not (one-window-p)) (delete-window))
  20277. (kill-buffer buf)
  20278. (org-agenda-maybe-reset-markers 'force)
  20279. (org-columns-remove-overlays))
  20280. ;; Maybe restore the pre-agenda window configuration.
  20281. (and org-agenda-restore-windows-after-quit
  20282. (not (eq org-agenda-window-setup 'other-frame))
  20283. org-pre-agenda-window-conf
  20284. (set-window-configuration org-pre-agenda-window-conf)))
  20285. (defun org-agenda-exit ()
  20286. "Exit agenda by removing the window or the buffer.
  20287. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  20288. Org-mode buffers visited directly by the user will not be touched."
  20289. (interactive)
  20290. (org-release-buffers org-agenda-new-buffers)
  20291. (setq org-agenda-new-buffers nil)
  20292. (org-agenda-quit))
  20293. (defun org-agenda-execute (arg)
  20294. "Execute another agenda command, keeping same window.\\<global-map>
  20295. So this is just a shortcut for `\\[org-agenda]', available in the agenda."
  20296. (interactive "P")
  20297. (let ((org-agenda-window-setup 'current-window))
  20298. (org-agenda arg)))
  20299. (defun org-save-all-org-buffers ()
  20300. "Save all Org-mode buffers without user confirmation."
  20301. (interactive)
  20302. (message "Saving all Org-mode buffers...")
  20303. (save-some-buffers t 'org-mode-p)
  20304. (message "Saving all Org-mode buffers... done"))
  20305. (defun org-agenda-redo ()
  20306. "Rebuild Agenda.
  20307. When this is the global TODO list, a prefix argument will be interpreted."
  20308. (interactive)
  20309. (let* ((org-agenda-keep-modes t)
  20310. (line (org-current-line))
  20311. (window-line (- line (org-current-line (window-start))))
  20312. (lprops (get 'org-agenda-redo-command 'org-lprops)))
  20313. (message "Rebuilding agenda buffer...")
  20314. (org-let lprops '(eval org-agenda-redo-command))
  20315. (setq org-agenda-undo-list nil
  20316. org-agenda-pending-undo-list nil)
  20317. (message "Rebuilding agenda buffer...done")
  20318. (goto-line line)
  20319. (recenter window-line)))
  20320. (defun org-agenda-goto-date (date)
  20321. "Jump to DATE in agenda."
  20322. (interactive (list (org-read-date)))
  20323. (org-agenda-list nil date))
  20324. (defun org-agenda-goto-today ()
  20325. "Go to today."
  20326. (interactive)
  20327. (org-agenda-check-type t 'timeline 'agenda)
  20328. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  20329. (cond
  20330. (tdpos (goto-char tdpos))
  20331. ((eq org-agenda-type 'agenda)
  20332. (let* ((sd (time-to-days
  20333. (time-subtract (current-time)
  20334. (list 0 (* 3600 org-extend-today-until) 0))))
  20335. (comp (org-agenda-compute-time-span sd org-agenda-span))
  20336. (org-agenda-overriding-arguments org-agenda-last-arguments))
  20337. (setf (nth 1 org-agenda-overriding-arguments) (car comp))
  20338. (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
  20339. (org-agenda-redo)
  20340. (org-agenda-find-same-or-today-or-agenda)))
  20341. (t (error "Cannot find today")))))
  20342. (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
  20343. (goto-char
  20344. (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
  20345. (text-property-any (point-min) (point-max) 'org-today t)
  20346. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  20347. (point-min))))
  20348. (defun org-agenda-later (arg)
  20349. "Go forward in time by thee current span.
  20350. With prefix ARG, go forward that many times the current span."
  20351. (interactive "p")
  20352. (org-agenda-check-type t 'agenda)
  20353. (let* ((span org-agenda-span)
  20354. (sd org-starting-day)
  20355. (greg (calendar-gregorian-from-absolute sd))
  20356. (cnt (get-text-property (point) 'org-day-cnt))
  20357. greg2 nd)
  20358. (cond
  20359. ((eq span 'day)
  20360. (setq sd (+ arg sd) nd 1))
  20361. ((eq span 'week)
  20362. (setq sd (+ (* 7 arg) sd) nd 7))
  20363. ((eq span 'month)
  20364. (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
  20365. sd (calendar-absolute-from-gregorian greg2))
  20366. (setcar greg2 (1+ (car greg2)))
  20367. (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
  20368. ((eq span 'year)
  20369. (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
  20370. sd (calendar-absolute-from-gregorian greg2))
  20371. (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
  20372. (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
  20373. (let ((org-agenda-overriding-arguments
  20374. (list (car org-agenda-last-arguments) sd nd t)))
  20375. (org-agenda-redo)
  20376. (org-agenda-find-same-or-today-or-agenda cnt))))
  20377. (defun org-agenda-earlier (arg)
  20378. "Go backward in time by the current span.
  20379. With prefix ARG, go backward that many times the current span."
  20380. (interactive "p")
  20381. (org-agenda-later (- arg)))
  20382. (defun org-agenda-day-view ()
  20383. "Switch to daily view for agenda."
  20384. (interactive)
  20385. (setq org-agenda-ndays 1)
  20386. (org-agenda-change-time-span 'day))
  20387. (defun org-agenda-week-view ()
  20388. "Switch to daily view for agenda."
  20389. (interactive)
  20390. (setq org-agenda-ndays 7)
  20391. (org-agenda-change-time-span 'week))
  20392. (defun org-agenda-month-view ()
  20393. "Switch to daily view for agenda."
  20394. (interactive)
  20395. (org-agenda-change-time-span 'month))
  20396. (defun org-agenda-year-view ()
  20397. "Switch to daily view for agenda."
  20398. (interactive)
  20399. (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
  20400. (org-agenda-change-time-span 'year)
  20401. (error "Abort")))
  20402. (defun org-agenda-change-time-span (span)
  20403. "Change the agenda view to SPAN.
  20404. SPAN may be `day', `week', `month', `year'."
  20405. (org-agenda-check-type t 'agenda)
  20406. (if (equal org-agenda-span span)
  20407. (error "Viewing span is already \"%s\"" span))
  20408. (let* ((sd (or (get-text-property (point) 'day)
  20409. org-starting-day))
  20410. (computed (org-agenda-compute-time-span sd span))
  20411. (org-agenda-overriding-arguments
  20412. (list (car org-agenda-last-arguments)
  20413. (car computed) (cdr computed) t)))
  20414. (org-agenda-redo)
  20415. (org-agenda-find-same-or-today-or-agenda))
  20416. (org-agenda-set-mode-name)
  20417. (message "Switched to %s view" span))
  20418. (defun org-agenda-compute-time-span (sd span)
  20419. "Compute starting date and number of days for agenda.
  20420. SPAN may be `day', `week', `month', `year'. The return value
  20421. is a cons cell with the starting date and the number of days,
  20422. so that the date SD will be in that range."
  20423. (let* ((greg (calendar-gregorian-from-absolute sd))
  20424. nd)
  20425. (cond
  20426. ((eq span 'day)
  20427. (setq nd 1))
  20428. ((eq span 'week)
  20429. (let* ((nt (calendar-day-of-week
  20430. (calendar-gregorian-from-absolute sd)))
  20431. (d (if org-agenda-start-on-weekday
  20432. (- nt org-agenda-start-on-weekday)
  20433. 0)))
  20434. (setq sd (- sd (+ (if (< d 0) 7 0) d)))
  20435. (setq nd 7)))
  20436. ((eq span 'month)
  20437. (setq sd (calendar-absolute-from-gregorian
  20438. (list (car greg) 1 (nth 2 greg)))
  20439. nd (- (calendar-absolute-from-gregorian
  20440. (list (1+ (car greg)) 1 (nth 2 greg)))
  20441. sd)))
  20442. ((eq span 'year)
  20443. (setq sd (calendar-absolute-from-gregorian
  20444. (list 1 1 (nth 2 greg)))
  20445. nd (- (calendar-absolute-from-gregorian
  20446. (list 1 1 (1+ (nth 2 greg))))
  20447. sd))))
  20448. (cons sd nd)))
  20449. ;; FIXME: does not work if user makes date format that starts with a blank
  20450. (defun org-agenda-next-date-line (&optional arg)
  20451. "Jump to the next line indicating a date in agenda buffer."
  20452. (interactive "p")
  20453. (org-agenda-check-type t 'agenda 'timeline)
  20454. (beginning-of-line 1)
  20455. (if (looking-at "^\\S-") (forward-char 1))
  20456. (if (not (re-search-forward "^\\S-" nil t arg))
  20457. (progn
  20458. (backward-char 1)
  20459. (error "No next date after this line in this buffer")))
  20460. (goto-char (match-beginning 0)))
  20461. (defun org-agenda-previous-date-line (&optional arg)
  20462. "Jump to the previous line indicating a date in agenda buffer."
  20463. (interactive "p")
  20464. (org-agenda-check-type t 'agenda 'timeline)
  20465. (beginning-of-line 1)
  20466. (if (not (re-search-backward "^\\S-" nil t arg))
  20467. (error "No previous date before this line in this buffer")))
  20468. ;; Initialize the highlight
  20469. (defvar org-hl (org-make-overlay 1 1))
  20470. (org-overlay-put org-hl 'face 'highlight)
  20471. (defun org-highlight (begin end &optional buffer)
  20472. "Highlight a region with overlay."
  20473. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  20474. org-hl begin end (or buffer (current-buffer))))
  20475. (defun org-unhighlight ()
  20476. "Detach overlay INDEX."
  20477. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  20478. ;; FIXME this is currently not used.
  20479. (defun org-highlight-until-next-command (beg end &optional buffer)
  20480. (org-highlight beg end buffer)
  20481. (add-hook 'pre-command-hook 'org-unhighlight-once))
  20482. (defun org-unhighlight-once ()
  20483. (remove-hook 'pre-command-hook 'org-unhighlight-once)
  20484. (org-unhighlight))
  20485. (defun org-agenda-follow-mode ()
  20486. "Toggle follow mode in an agenda buffer."
  20487. (interactive)
  20488. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  20489. (org-agenda-set-mode-name)
  20490. (message "Follow mode is %s"
  20491. (if org-agenda-follow-mode "on" "off")))
  20492. (defun org-agenda-log-mode ()
  20493. "Toggle log mode in an agenda buffer."
  20494. (interactive)
  20495. (org-agenda-check-type t 'agenda 'timeline)
  20496. (setq org-agenda-show-log (not org-agenda-show-log))
  20497. (org-agenda-set-mode-name)
  20498. (org-agenda-redo)
  20499. (message "Log mode is %s"
  20500. (if org-agenda-show-log "on" "off")))
  20501. (defun org-agenda-toggle-diary ()
  20502. "Toggle diary inclusion in an agenda buffer."
  20503. (interactive)
  20504. (org-agenda-check-type t 'agenda)
  20505. (setq org-agenda-include-diary (not org-agenda-include-diary))
  20506. (org-agenda-redo)
  20507. (org-agenda-set-mode-name)
  20508. (message "Diary inclusion turned %s"
  20509. (if org-agenda-include-diary "on" "off")))
  20510. (defun org-agenda-toggle-time-grid ()
  20511. "Toggle time grid in an agenda buffer."
  20512. (interactive)
  20513. (org-agenda-check-type t 'agenda)
  20514. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  20515. (org-agenda-redo)
  20516. (org-agenda-set-mode-name)
  20517. (message "Time-grid turned %s"
  20518. (if org-agenda-use-time-grid "on" "off")))
  20519. (defun org-agenda-set-mode-name ()
  20520. "Set the mode name to indicate all the small mode settings."
  20521. (setq mode-name
  20522. (concat "Org-Agenda"
  20523. (if (equal org-agenda-ndays 1) " Day" "")
  20524. (if (equal org-agenda-ndays 7) " Week" "")
  20525. (if org-agenda-follow-mode " Follow" "")
  20526. (if org-agenda-include-diary " Diary" "")
  20527. (if org-agenda-use-time-grid " Grid" "")
  20528. (if org-agenda-show-log " Log" "")))
  20529. (force-mode-line-update))
  20530. (defun org-agenda-post-command-hook ()
  20531. (and (eolp) (not (bolp)) (backward-char 1))
  20532. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  20533. (if (and org-agenda-follow-mode
  20534. (get-text-property (point) 'org-marker))
  20535. (org-agenda-show)))
  20536. (defun org-agenda-show-priority ()
  20537. "Show the priority of the current item.
  20538. This priority is composed of the main priority given with the [#A] cookies,
  20539. and by additional input from the age of a schedules or deadline entry."
  20540. (interactive)
  20541. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  20542. (message "Priority is %d" (if pri pri -1000))))
  20543. (defun org-agenda-show-tags ()
  20544. "Show the tags applicable to the current item."
  20545. (interactive)
  20546. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  20547. (if tags
  20548. (message "Tags are :%s:"
  20549. (org-no-properties (mapconcat 'identity tags ":")))
  20550. (message "No tags associated with this line"))))
  20551. (defun org-agenda-goto (&optional highlight)
  20552. "Go to the Org-mode file which contains the item at point."
  20553. (interactive)
  20554. (let* ((marker (or (get-text-property (point) 'org-marker)
  20555. (org-agenda-error)))
  20556. (buffer (marker-buffer marker))
  20557. (pos (marker-position marker)))
  20558. (switch-to-buffer-other-window buffer)
  20559. (widen)
  20560. (goto-char pos)
  20561. (when (org-mode-p)
  20562. (org-show-context 'agenda)
  20563. (save-excursion
  20564. (and (outline-next-heading)
  20565. (org-flag-heading nil)))) ; show the next heading
  20566. (run-hooks 'org-agenda-after-show-hook)
  20567. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  20568. (defvar org-agenda-after-show-hook nil
  20569. "Normal hook run after an item has been shown from the agenda.
  20570. Point is in the buffer where the item originated.")
  20571. (defun org-agenda-kill ()
  20572. "Kill the entry or subtree belonging to the current agenda entry."
  20573. (interactive)
  20574. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20575. (let* ((marker (or (get-text-property (point) 'org-marker)
  20576. (org-agenda-error)))
  20577. (buffer (marker-buffer marker))
  20578. (pos (marker-position marker))
  20579. (type (get-text-property (point) 'type))
  20580. dbeg dend (n 0) conf)
  20581. (org-with-remote-undo buffer
  20582. (with-current-buffer buffer
  20583. (save-excursion
  20584. (goto-char pos)
  20585. (if (and (org-mode-p) (not (member type '("sexp"))))
  20586. (setq dbeg (progn (org-back-to-heading t) (point))
  20587. dend (org-end-of-subtree t t))
  20588. (setq dbeg (point-at-bol)
  20589. dend (min (point-max) (1+ (point-at-eol)))))
  20590. (goto-char dbeg)
  20591. (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
  20592. (setq conf (or (eq t org-agenda-confirm-kill)
  20593. (and (numberp org-agenda-confirm-kill)
  20594. (> n org-agenda-confirm-kill))))
  20595. (and conf
  20596. (not (y-or-n-p
  20597. (format "Delete entry with %d lines in buffer \"%s\"? "
  20598. n (buffer-name buffer))))
  20599. (error "Abort"))
  20600. (org-remove-subtree-entries-from-agenda buffer dbeg dend)
  20601. (with-current-buffer buffer (delete-region dbeg dend))
  20602. (message "Agenda item and source killed"))))
  20603. (defun org-agenda-archive ()
  20604. "Kill the entry or subtree belonging to the current agenda entry."
  20605. (interactive)
  20606. (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
  20607. (let* ((marker (or (get-text-property (point) 'org-marker)
  20608. (org-agenda-error)))
  20609. (buffer (marker-buffer marker))
  20610. (pos (marker-position marker)))
  20611. (org-with-remote-undo buffer
  20612. (with-current-buffer buffer
  20613. (if (org-mode-p)
  20614. (save-excursion
  20615. (goto-char pos)
  20616. (org-remove-subtree-entries-from-agenda)
  20617. (org-back-to-heading t)
  20618. (org-archive-subtree))
  20619. (error "Archiving works only in Org-mode files"))))))
  20620. (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
  20621. "Remove all lines in the agenda that correspond to a given subtree.
  20622. The subtree is the one in buffer BUF, starting at BEG and ending at END.
  20623. If this information is not given, the function uses the tree at point."
  20624. (let ((buf (or buf (current-buffer))) m p)
  20625. (save-excursion
  20626. (unless (and beg end)
  20627. (org-back-to-heading t)
  20628. (setq beg (point))
  20629. (org-end-of-subtree t)
  20630. (setq end (point)))
  20631. (set-buffer (get-buffer org-agenda-buffer-name))
  20632. (save-excursion
  20633. (goto-char (point-max))
  20634. (beginning-of-line 1)
  20635. (while (not (bobp))
  20636. (when (and (setq m (get-text-property (point) 'org-marker))
  20637. (equal buf (marker-buffer m))
  20638. (setq p (marker-position m))
  20639. (>= p beg)
  20640. (<= p end))
  20641. (let ((inhibit-read-only t))
  20642. (delete-region (point-at-bol) (1+ (point-at-eol)))))
  20643. (beginning-of-line 0))))))
  20644. (defun org-agenda-open-link ()
  20645. "Follow the link in the current line, if any."
  20646. (interactive)
  20647. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
  20648. (save-excursion
  20649. (save-restriction
  20650. (narrow-to-region (point-at-bol) (point-at-eol))
  20651. (org-open-at-point))))
  20652. (defun org-agenda-copy-local-variable (var)
  20653. "Get a variable from a referenced buffer and install it here."
  20654. (let ((m (get-text-property (point) 'org-marker)))
  20655. (when (and m (buffer-live-p (marker-buffer m)))
  20656. (org-set-local var (with-current-buffer (marker-buffer m)
  20657. (symbol-value var))))))
  20658. (defun org-agenda-switch-to (&optional delete-other-windows)
  20659. "Go to the Org-mode file which contains the item at point."
  20660. (interactive)
  20661. (let* ((marker (or (get-text-property (point) 'org-marker)
  20662. (org-agenda-error)))
  20663. (buffer (marker-buffer marker))
  20664. (pos (marker-position marker)))
  20665. (switch-to-buffer buffer)
  20666. (and delete-other-windows (delete-other-windows))
  20667. (widen)
  20668. (goto-char pos)
  20669. (when (org-mode-p)
  20670. (org-show-context 'agenda)
  20671. (save-excursion
  20672. (and (outline-next-heading)
  20673. (org-flag-heading nil)))))) ; show the next heading
  20674. (defun org-agenda-goto-mouse (ev)
  20675. "Go to the Org-mode file which contains the item at the mouse click."
  20676. (interactive "e")
  20677. (mouse-set-point ev)
  20678. (org-agenda-goto))
  20679. (defun org-agenda-show ()
  20680. "Display the Org-mode file which contains the item at point."
  20681. (interactive)
  20682. (let ((win (selected-window)))
  20683. (org-agenda-goto t)
  20684. (select-window win)))
  20685. (defun org-agenda-recenter (arg)
  20686. "Display the Org-mode file which contains the item at point and recenter."
  20687. (interactive "P")
  20688. (let ((win (selected-window)))
  20689. (org-agenda-goto t)
  20690. (recenter arg)
  20691. (select-window win)))
  20692. (defun org-agenda-show-mouse (ev)
  20693. "Display the Org-mode file which contains the item at the mouse click."
  20694. (interactive "e")
  20695. (mouse-set-point ev)
  20696. (org-agenda-show))
  20697. (defun org-agenda-check-no-diary ()
  20698. "Check if the entry is a diary link and abort if yes."
  20699. (if (get-text-property (point) 'org-agenda-diary-link)
  20700. (org-agenda-error)))
  20701. (defun org-agenda-error ()
  20702. (error "Command not allowed in this line"))
  20703. (defun org-agenda-tree-to-indirect-buffer ()
  20704. "Show the subtree corresponding to the current entry in an indirect buffer.
  20705. This calls the command `org-tree-to-indirect-buffer' from the original
  20706. Org-mode buffer.
  20707. With numerical prefix arg ARG, go up to this level and then take that tree.
  20708. With a C-u prefix, make a separate frame for this tree (i.e. don't use the
  20709. dedicated frame)."
  20710. (interactive)
  20711. (org-agenda-check-no-diary)
  20712. (let* ((marker (or (get-text-property (point) 'org-marker)
  20713. (org-agenda-error)))
  20714. (buffer (marker-buffer marker))
  20715. (pos (marker-position marker)))
  20716. (with-current-buffer buffer
  20717. (save-excursion
  20718. (goto-char pos)
  20719. (call-interactively 'org-tree-to-indirect-buffer)))))
  20720. (defvar org-last-heading-marker (make-marker)
  20721. "Marker pointing to the headline that last changed its TODO state
  20722. by a remote command from the agenda.")
  20723. (defun org-agenda-todo-nextset ()
  20724. "Switch TODO entry to next sequence."
  20725. (interactive)
  20726. (org-agenda-todo 'nextset))
  20727. (defun org-agenda-todo-previousset ()
  20728. "Switch TODO entry to previous sequence."
  20729. (interactive)
  20730. (org-agenda-todo 'previousset))
  20731. (defun org-agenda-todo (&optional arg)
  20732. "Cycle TODO state of line at point, also in Org-mode file.
  20733. This changes the line at point, all other lines in the agenda referring to
  20734. the same tree node, and the headline of the tree node in the Org-mode file."
  20735. (interactive "P")
  20736. (org-agenda-check-no-diary)
  20737. (let* ((col (current-column))
  20738. (marker (or (get-text-property (point) 'org-marker)
  20739. (org-agenda-error)))
  20740. (buffer (marker-buffer marker))
  20741. (pos (marker-position marker))
  20742. (hdmarker (get-text-property (point) 'org-hd-marker))
  20743. (inhibit-read-only t)
  20744. newhead)
  20745. (org-with-remote-undo buffer
  20746. (with-current-buffer buffer
  20747. (widen)
  20748. (goto-char pos)
  20749. (org-show-context 'agenda)
  20750. (save-excursion
  20751. (and (outline-next-heading)
  20752. (org-flag-heading nil))) ; show the next heading
  20753. (org-todo arg)
  20754. (and (bolp) (forward-char 1))
  20755. (setq newhead (org-get-heading))
  20756. (save-excursion
  20757. (org-back-to-heading)
  20758. (move-marker org-last-heading-marker (point))))
  20759. (beginning-of-line 1)
  20760. (save-excursion
  20761. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  20762. (move-to-column col))))
  20763. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  20764. "Change all lines in the agenda buffer which match HDMARKER.
  20765. The new content of the line will be NEWHEAD (as modified by
  20766. `org-format-agenda-item'). HDMARKER is checked with
  20767. `equal' against all `org-hd-marker' text properties in the file.
  20768. If FIXFACE is non-nil, the face of each item is modified acording to
  20769. the new TODO state."
  20770. (let* ((inhibit-read-only t)
  20771. props m pl undone-face done-face finish new dotime cat tags)
  20772. (save-excursion
  20773. (goto-char (point-max))
  20774. (beginning-of-line 1)
  20775. (while (not finish)
  20776. (setq finish (bobp))
  20777. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  20778. (equal m hdmarker))
  20779. (setq props (text-properties-at (point))
  20780. dotime (get-text-property (point) 'dotime)
  20781. cat (get-text-property (point) 'org-category)
  20782. tags (get-text-property (point) 'tags)
  20783. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  20784. pl (get-text-property (point) 'prefix-length)
  20785. undone-face (get-text-property (point) 'undone-face)
  20786. done-face (get-text-property (point) 'done-face))
  20787. (move-to-column pl)
  20788. (cond
  20789. ((equal new "")
  20790. (beginning-of-line 1)
  20791. (and (looking-at ".*\n?") (replace-match "")))
  20792. ((looking-at ".*")
  20793. (replace-match new t t)
  20794. (beginning-of-line 1)
  20795. (add-text-properties (point-at-bol) (point-at-eol) props)
  20796. (when fixface
  20797. (add-text-properties
  20798. (point-at-bol) (point-at-eol)
  20799. (list 'face
  20800. (if org-last-todo-state-is-todo
  20801. undone-face done-face))))
  20802. (org-agenda-highlight-todo 'line)
  20803. (beginning-of-line 1))
  20804. (t (error "Line update did not work"))))
  20805. (beginning-of-line 0)))
  20806. (org-finalize-agenda)))
  20807. (defun org-agenda-align-tags (&optional line)
  20808. "Align all tags in agenda items to `org-agenda-tags-column'."
  20809. (let ((inhibit-read-only t) l c)
  20810. (save-excursion
  20811. (goto-char (if line (point-at-bol) (point-min)))
  20812. (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
  20813. (if line (point-at-eol) nil) t)
  20814. (add-text-properties
  20815. (match-beginning 2) (match-end 2)
  20816. (list 'face (list 'org-tag (get-text-property
  20817. (match-beginning 2) 'face))))
  20818. (setq l (- (match-end 2) (match-beginning 2))
  20819. c (if (< org-agenda-tags-column 0)
  20820. (- (abs org-agenda-tags-column) l)
  20821. org-agenda-tags-column))
  20822. (delete-region (match-beginning 1) (match-end 1))
  20823. (goto-char (match-beginning 1))
  20824. (insert (org-add-props
  20825. (make-string (max 1 (- c (current-column))) ?\ )
  20826. (text-properties-at (point))))))))
  20827. (defun org-agenda-priority-up ()
  20828. "Increase the priority of line at point, also in Org-mode file."
  20829. (interactive)
  20830. (org-agenda-priority 'up))
  20831. (defun org-agenda-priority-down ()
  20832. "Decrease the priority of line at point, also in Org-mode file."
  20833. (interactive)
  20834. (org-agenda-priority 'down))
  20835. (defun org-agenda-priority (&optional force-direction)
  20836. "Set the priority of line at point, also in Org-mode file.
  20837. This changes the line at point, all other lines in the agenda referring to
  20838. the same tree node, and the headline of the tree node in the Org-mode file."
  20839. (interactive)
  20840. (org-agenda-check-no-diary)
  20841. (let* ((marker (or (get-text-property (point) 'org-marker)
  20842. (org-agenda-error)))
  20843. (hdmarker (get-text-property (point) 'org-hd-marker))
  20844. (buffer (marker-buffer hdmarker))
  20845. (pos (marker-position hdmarker))
  20846. (inhibit-read-only t)
  20847. newhead)
  20848. (org-with-remote-undo buffer
  20849. (with-current-buffer buffer
  20850. (widen)
  20851. (goto-char pos)
  20852. (org-show-context 'agenda)
  20853. (save-excursion
  20854. (and (outline-next-heading)
  20855. (org-flag-heading nil))) ; show the next heading
  20856. (funcall 'org-priority force-direction)
  20857. (end-of-line 1)
  20858. (setq newhead (org-get-heading)))
  20859. (org-agenda-change-all-lines newhead hdmarker)
  20860. (beginning-of-line 1))))
  20861. (defun org-get-tags-at (&optional pos)
  20862. "Get a list of all headline tags applicable at POS.
  20863. POS defaults to point. If tags are inherited, the list contains
  20864. the targets in the same sequence as the headlines appear, i.e.
  20865. the tags of the current headline come last."
  20866. (interactive)
  20867. (let (tags lastpos)
  20868. (save-excursion
  20869. (save-restriction
  20870. (widen)
  20871. (goto-char (or pos (point)))
  20872. (save-match-data
  20873. (org-back-to-heading t)
  20874. (condition-case nil
  20875. (while (not (equal lastpos (point)))
  20876. (setq lastpos (point))
  20877. (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  20878. (setq tags (append (org-split-string
  20879. (org-match-string-no-properties 1) ":")
  20880. tags)))
  20881. (or org-use-tag-inheritance (error ""))
  20882. (org-up-heading-all 1))
  20883. (error nil))))
  20884. tags)))
  20885. ;; FIXME: should fix the tags property of the agenda line.
  20886. (defun org-agenda-set-tags ()
  20887. "Set tags for the current headline."
  20888. (interactive)
  20889. (org-agenda-check-no-diary)
  20890. (if (and (org-region-active-p) (interactive-p))
  20891. (call-interactively 'org-change-tag-in-region)
  20892. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  20893. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  20894. (org-agenda-error)))
  20895. (buffer (marker-buffer hdmarker))
  20896. (pos (marker-position hdmarker))
  20897. (inhibit-read-only t)
  20898. newhead)
  20899. (org-with-remote-undo buffer
  20900. (with-current-buffer buffer
  20901. (widen)
  20902. (goto-char pos)
  20903. (save-excursion
  20904. (org-show-context 'agenda))
  20905. (save-excursion
  20906. (and (outline-next-heading)
  20907. (org-flag-heading nil))) ; show the next heading
  20908. (goto-char pos)
  20909. (call-interactively 'org-set-tags)
  20910. (end-of-line 1)
  20911. (setq newhead (org-get-heading)))
  20912. (org-agenda-change-all-lines newhead hdmarker)
  20913. (beginning-of-line 1)))))
  20914. (defun org-agenda-toggle-archive-tag ()
  20915. "Toggle the archive tag for the current entry."
  20916. (interactive)
  20917. (org-agenda-check-no-diary)
  20918. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  20919. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  20920. (org-agenda-error)))
  20921. (buffer (marker-buffer hdmarker))
  20922. (pos (marker-position hdmarker))
  20923. (inhibit-read-only t)
  20924. newhead)
  20925. (org-with-remote-undo buffer
  20926. (with-current-buffer buffer
  20927. (widen)
  20928. (goto-char pos)
  20929. (org-show-context 'agenda)
  20930. (save-excursion
  20931. (and (outline-next-heading)
  20932. (org-flag-heading nil))) ; show the next heading
  20933. (call-interactively 'org-toggle-archive-tag)
  20934. (end-of-line 1)
  20935. (setq newhead (org-get-heading)))
  20936. (org-agenda-change-all-lines newhead hdmarker)
  20937. (beginning-of-line 1))))
  20938. (defun org-agenda-date-later (arg &optional what)
  20939. "Change the date of this item to one day later."
  20940. (interactive "p")
  20941. (org-agenda-check-type t 'agenda 'timeline)
  20942. (org-agenda-check-no-diary)
  20943. (let* ((marker (or (get-text-property (point) 'org-marker)
  20944. (org-agenda-error)))
  20945. (buffer (marker-buffer marker))
  20946. (pos (marker-position marker)))
  20947. (org-with-remote-undo buffer
  20948. (with-current-buffer buffer
  20949. (widen)
  20950. (goto-char pos)
  20951. (if (not (org-at-timestamp-p))
  20952. (error "Cannot find time stamp"))
  20953. (org-timestamp-change arg (or what 'day)))
  20954. (org-agenda-show-new-time marker org-last-changed-timestamp))
  20955. (message "Time stamp changed to %s" org-last-changed-timestamp)))
  20956. (defun org-agenda-date-earlier (arg &optional what)
  20957. "Change the date of this item to one day earlier."
  20958. (interactive "p")
  20959. (org-agenda-date-later (- arg) what))
  20960. (defun org-agenda-show-new-time (marker stamp &optional prefix)
  20961. "Show new date stamp via text properties."
  20962. ;; We use text properties to make this undoable
  20963. (let ((inhibit-read-only t))
  20964. (setq stamp (concat " " prefix " => " stamp))
  20965. (save-excursion
  20966. (goto-char (point-max))
  20967. (while (not (bobp))
  20968. (when (equal marker (get-text-property (point) 'org-marker))
  20969. (move-to-column (- (window-width) (length stamp)) t)
  20970. (if (featurep 'xemacs)
  20971. ;; Use `duplicable' property to trigger undo recording
  20972. (let ((ex (make-extent nil nil))
  20973. (gl (make-glyph stamp)))
  20974. (set-glyph-face gl 'secondary-selection)
  20975. (set-extent-properties
  20976. ex (list 'invisible t 'end-glyph gl 'duplicable t))
  20977. (insert-extent ex (1- (point)) (point-at-eol)))
  20978. (add-text-properties
  20979. (1- (point)) (point-at-eol)
  20980. (list 'display (org-add-props stamp nil
  20981. 'face 'secondary-selection))))
  20982. (beginning-of-line 1))
  20983. (beginning-of-line 0)))))
  20984. (defun org-agenda-date-prompt (arg)
  20985. "Change the date of this item. Date is prompted for, with default today.
  20986. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  20987. be used to request time specification in the time stamp."
  20988. (interactive "P")
  20989. (org-agenda-check-type t 'agenda 'timeline)
  20990. (org-agenda-check-no-diary)
  20991. (let* ((marker (or (get-text-property (point) 'org-marker)
  20992. (org-agenda-error)))
  20993. (buffer (marker-buffer marker))
  20994. (pos (marker-position marker)))
  20995. (org-with-remote-undo buffer
  20996. (with-current-buffer buffer
  20997. (widen)
  20998. (goto-char pos)
  20999. (if (not (org-at-timestamp-p))
  21000. (error "Cannot find time stamp"))
  21001. (org-time-stamp arg)
  21002. (message "Time stamp changed to %s" org-last-changed-timestamp)))))
  21003. (defun org-agenda-schedule (arg)
  21004. "Schedule the item at point."
  21005. (interactive "P")
  21006. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21007. (org-agenda-check-no-diary)
  21008. (let* ((marker (or (get-text-property (point) 'org-marker)
  21009. (org-agenda-error)))
  21010. (buffer (marker-buffer marker))
  21011. (pos (marker-position marker))
  21012. (org-insert-labeled-timestamps-at-point nil)
  21013. ts)
  21014. (org-with-remote-undo buffer
  21015. (with-current-buffer buffer
  21016. (widen)
  21017. (goto-char pos)
  21018. (setq ts (org-schedule arg)))
  21019. (org-agenda-show-new-time marker ts "S"))
  21020. (message "Item scheduled for %s" ts)))
  21021. (defun org-agenda-deadline (arg)
  21022. "Schedule the item at point."
  21023. (interactive "P")
  21024. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  21025. (org-agenda-check-no-diary)
  21026. (let* ((marker (or (get-text-property (point) 'org-marker)
  21027. (org-agenda-error)))
  21028. (buffer (marker-buffer marker))
  21029. (pos (marker-position marker))
  21030. (org-insert-labeled-timestamps-at-point nil)
  21031. ts)
  21032. (org-with-remote-undo buffer
  21033. (with-current-buffer buffer
  21034. (widen)
  21035. (goto-char pos)
  21036. (setq ts (org-deadline arg)))
  21037. (org-agenda-show-new-time marker ts "S"))
  21038. (message "Deadline for this item set to %s" ts)))
  21039. (defun org-get-heading (&optional no-tags)
  21040. "Return the heading of the current entry, without the stars."
  21041. (save-excursion
  21042. (org-back-to-heading t)
  21043. (if (looking-at
  21044. (if no-tags
  21045. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  21046. "\\*+[ \t]+\\([^\r\n]*\\)"))
  21047. (match-string 1) "")))
  21048. (defun org-agenda-clock-in (&optional arg)
  21049. "Start the clock on the currently selected item."
  21050. (interactive "P")
  21051. (org-agenda-check-no-diary)
  21052. (let* ((marker (or (get-text-property (point) 'org-marker)
  21053. (org-agenda-error)))
  21054. (pos (marker-position marker)))
  21055. (org-with-remote-undo (marker-buffer marker)
  21056. (with-current-buffer (marker-buffer marker)
  21057. (widen)
  21058. (goto-char pos)
  21059. (org-clock-in)))))
  21060. (defun org-agenda-clock-out (&optional arg)
  21061. "Stop the currently running clock."
  21062. (interactive "P")
  21063. (unless (marker-buffer org-clock-marker)
  21064. (error "No running clock"))
  21065. (org-with-remote-undo (marker-buffer org-clock-marker)
  21066. (org-clock-out)))
  21067. (defun org-agenda-clock-cancel (&optional arg)
  21068. "Cancel the currently running clock."
  21069. (interactive "P")
  21070. (unless (marker-buffer org-clock-marker)
  21071. (error "No running clock"))
  21072. (org-with-remote-undo (marker-buffer org-clock-marker)
  21073. (org-clock-cancel)))
  21074. (defun org-agenda-diary-entry ()
  21075. "Make a diary entry, like the `i' command from the calendar.
  21076. All the standard commands work: block, weekly etc."
  21077. (interactive)
  21078. (org-agenda-check-type t 'agenda 'timeline)
  21079. (require 'diary-lib)
  21080. (let* ((char (progn
  21081. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  21082. (read-char-exclusive)))
  21083. (cmd (cdr (assoc char
  21084. '((?d . insert-diary-entry)
  21085. (?w . insert-weekly-diary-entry)
  21086. (?m . insert-monthly-diary-entry)
  21087. (?y . insert-yearly-diary-entry)
  21088. (?a . insert-anniversary-diary-entry)
  21089. (?b . insert-block-diary-entry)
  21090. (?c . insert-cyclic-diary-entry)))))
  21091. (oldf (symbol-function 'calendar-cursor-to-date))
  21092. ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
  21093. (point (point))
  21094. (mark (or (mark t) (point))))
  21095. (unless cmd
  21096. (error "No command associated with <%c>" char))
  21097. (unless (and (get-text-property point 'day)
  21098. (or (not (equal ?b char))
  21099. (get-text-property mark 'day)))
  21100. (error "Don't know which date to use for diary entry"))
  21101. ;; We implement this by hacking the `calendar-cursor-to-date' function
  21102. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  21103. (let ((calendar-mark-ring
  21104. (list (calendar-gregorian-from-absolute
  21105. (or (get-text-property mark 'day)
  21106. (get-text-property point 'day))))))
  21107. (unwind-protect
  21108. (progn
  21109. (fset 'calendar-cursor-to-date
  21110. (lambda (&optional error)
  21111. (calendar-gregorian-from-absolute
  21112. (get-text-property point 'day))))
  21113. (call-interactively cmd))
  21114. (fset 'calendar-cursor-to-date oldf)))))
  21115. (defun org-agenda-execute-calendar-command (cmd)
  21116. "Execute a calendar command from the agenda, with the date associated to
  21117. the cursor position."
  21118. (org-agenda-check-type t 'agenda 'timeline)
  21119. (require 'diary-lib)
  21120. (unless (get-text-property (point) 'day)
  21121. (error "Don't know which date to use for calendar command"))
  21122. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  21123. (point (point))
  21124. (date (calendar-gregorian-from-absolute
  21125. (get-text-property point 'day)))
  21126. ;; the following 3 vars are needed in the calendar
  21127. (displayed-day (extract-calendar-day date))
  21128. (displayed-month (extract-calendar-month date))
  21129. (displayed-year (extract-calendar-year date)))
  21130. (unwind-protect
  21131. (progn
  21132. (fset 'calendar-cursor-to-date
  21133. (lambda (&optional error)
  21134. (calendar-gregorian-from-absolute
  21135. (get-text-property point 'day))))
  21136. (call-interactively cmd))
  21137. (fset 'calendar-cursor-to-date oldf))))
  21138. (defun org-agenda-phases-of-moon ()
  21139. "Display the phases of the moon for the 3 months around the cursor date."
  21140. (interactive)
  21141. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  21142. (defun org-agenda-holidays ()
  21143. "Display the holidays for the 3 months around the cursor date."
  21144. (interactive)
  21145. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  21146. (defun org-agenda-sunrise-sunset (arg)
  21147. "Display sunrise and sunset for the cursor date.
  21148. Latitude and longitude can be specified with the variables
  21149. `calendar-latitude' and `calendar-longitude'. When called with prefix
  21150. argument, latitude and longitude will be prompted for."
  21151. (interactive "P")
  21152. (let ((calendar-longitude (if arg nil calendar-longitude))
  21153. (calendar-latitude (if arg nil calendar-latitude))
  21154. (calendar-location-name
  21155. (if arg "the given coordinates" calendar-location-name)))
  21156. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  21157. (defun org-agenda-goto-calendar ()
  21158. "Open the Emacs calendar with the date at the cursor."
  21159. (interactive)
  21160. (org-agenda-check-type t 'agenda 'timeline)
  21161. (let* ((day (or (get-text-property (point) 'day)
  21162. (error "Don't know which date to open in calendar")))
  21163. (date (calendar-gregorian-from-absolute day))
  21164. (calendar-move-hook nil)
  21165. (view-calendar-holidays-initially nil)
  21166. (view-diary-entries-initially nil))
  21167. (calendar)
  21168. (calendar-goto-date date)))
  21169. (defun org-calendar-goto-agenda ()
  21170. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  21171. This is a command that has to be installed in `calendar-mode-map'."
  21172. (interactive)
  21173. (org-agenda-list nil (calendar-absolute-from-gregorian
  21174. (calendar-cursor-to-date))
  21175. nil))
  21176. (defun org-agenda-convert-date ()
  21177. (interactive)
  21178. (org-agenda-check-type t 'agenda 'timeline)
  21179. (let ((day (get-text-property (point) 'day))
  21180. date s)
  21181. (unless day
  21182. (error "Don't know which date to convert"))
  21183. (setq date (calendar-gregorian-from-absolute day))
  21184. (setq s (concat
  21185. "Gregorian: " (calendar-date-string date) "\n"
  21186. "ISO: " (calendar-iso-date-string date) "\n"
  21187. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  21188. "Julian: " (calendar-julian-date-string date) "\n"
  21189. "Astron. JD: " (calendar-astro-date-string date)
  21190. " (Julian date number at noon UTC)\n"
  21191. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  21192. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  21193. "French: " (calendar-french-date-string date) "\n"
  21194. "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
  21195. "Mayan: " (calendar-mayan-date-string date) "\n"
  21196. "Coptic: " (calendar-coptic-date-string date) "\n"
  21197. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  21198. "Persian: " (calendar-persian-date-string date) "\n"
  21199. "Chinese: " (calendar-chinese-date-string date) "\n"))
  21200. (with-output-to-temp-buffer "*Dates*"
  21201. (princ s))
  21202. (if (fboundp 'fit-window-to-buffer)
  21203. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  21204. ;;;; Embedded LaTeX
  21205. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  21206. "Keymap for the minor `org-cdlatex-mode'.")
  21207. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  21208. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  21209. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  21210. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  21211. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  21212. (defvar org-cdlatex-texmathp-advice-is-done nil
  21213. "Flag remembering if we have applied the advice to texmathp already.")
  21214. (define-minor-mode org-cdlatex-mode
  21215. "Toggle the minor `org-cdlatex-mode'.
  21216. This mode supports entering LaTeX environment and math in LaTeX fragments
  21217. in Org-mode.
  21218. \\{org-cdlatex-mode-map}"
  21219. nil " OCDL" nil
  21220. (when org-cdlatex-mode (require 'cdlatex))
  21221. (unless org-cdlatex-texmathp-advice-is-done
  21222. (setq org-cdlatex-texmathp-advice-is-done t)
  21223. (defadvice texmathp (around org-math-always-on activate)
  21224. "Always return t in org-mode buffers.
  21225. This is because we want to insert math symbols without dollars even outside
  21226. the LaTeX math segments. If Orgmode thinks that point is actually inside
  21227. en embedded LaTeX fragement, let texmathp do its job.
  21228. \\[org-cdlatex-mode-map]"
  21229. (interactive)
  21230. (let (p)
  21231. (cond
  21232. ((not (org-mode-p)) ad-do-it)
  21233. ((eq this-command 'cdlatex-math-symbol)
  21234. (setq ad-return-value t
  21235. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  21236. (t
  21237. (let ((p (org-inside-LaTeX-fragment-p)))
  21238. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  21239. (setq ad-return-value t
  21240. texmathp-why '("Org-mode embedded math" . 0))
  21241. (if p ad-do-it)))))))))
  21242. (defun turn-on-org-cdlatex ()
  21243. "Unconditionally turn on `org-cdlatex-mode'."
  21244. (org-cdlatex-mode 1))
  21245. (defun org-inside-LaTeX-fragment-p ()
  21246. "Test if point is inside a LaTeX fragment.
  21247. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  21248. sequence appearing also before point.
  21249. Even though the matchers for math are configurable, this function assumes
  21250. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  21251. delimiters are skipped when they have been removed by customization.
  21252. The return value is nil, or a cons cell with the delimiter and
  21253. and the position of this delimiter.
  21254. This function does a reasonably good job, but can locally be fooled by
  21255. for example currency specifications. For example it will assume being in
  21256. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  21257. fragments that are properly closed, but during editing, we have to live
  21258. with the uncertainty caused by missing closing delimiters. This function
  21259. looks only before point, not after."
  21260. (catch 'exit
  21261. (let ((pos (point))
  21262. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  21263. (lim (progn
  21264. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  21265. (point)))
  21266. dd-on str (start 0) m re)
  21267. (goto-char pos)
  21268. (when dodollar
  21269. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  21270. re (nth 1 (assoc "$" org-latex-regexps)))
  21271. (while (string-match re str start)
  21272. (cond
  21273. ((= (match-end 0) (length str))
  21274. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  21275. ((= (match-end 0) (- (length str) 5))
  21276. (throw 'exit nil))
  21277. (t (setq start (match-end 0))))))
  21278. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  21279. (goto-char pos)
  21280. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  21281. (and (match-beginning 2) (throw 'exit nil))
  21282. ;; count $$
  21283. (while (re-search-backward "\\$\\$" lim t)
  21284. (setq dd-on (not dd-on)))
  21285. (goto-char pos)
  21286. (if dd-on (cons "$$" m))))))
  21287. (defun org-try-cdlatex-tab ()
  21288. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  21289. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  21290. - inside a LaTeX fragment, or
  21291. - after the first word in a line, where an abbreviation expansion could
  21292. insert a LaTeX environment."
  21293. (when org-cdlatex-mode
  21294. (cond
  21295. ((save-excursion
  21296. (skip-chars-backward "a-zA-Z0-9*")
  21297. (skip-chars-backward " \t")
  21298. (bolp))
  21299. (cdlatex-tab) t)
  21300. ((org-inside-LaTeX-fragment-p)
  21301. (cdlatex-tab) t)
  21302. (t nil))))
  21303. (defun org-cdlatex-underscore-caret (&optional arg)
  21304. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  21305. Revert to the normal definition outside of these fragments."
  21306. (interactive "P")
  21307. (if (org-inside-LaTeX-fragment-p)
  21308. (call-interactively 'cdlatex-sub-superscript)
  21309. (let (org-cdlatex-mode)
  21310. (call-interactively (key-binding (vector last-input-event))))))
  21311. (defun org-cdlatex-math-modify (&optional arg)
  21312. "Execute `cdlatex-math-modify' in LaTeX fragments.
  21313. Revert to the normal definition outside of these fragments."
  21314. (interactive "P")
  21315. (if (org-inside-LaTeX-fragment-p)
  21316. (call-interactively 'cdlatex-math-modify)
  21317. (let (org-cdlatex-mode)
  21318. (call-interactively (key-binding (vector last-input-event))))))
  21319. (defvar org-latex-fragment-image-overlays nil
  21320. "List of overlays carrying the images of latex fragments.")
  21321. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  21322. (defun org-remove-latex-fragment-image-overlays ()
  21323. "Remove all overlays with LaTeX fragment images in current buffer."
  21324. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  21325. (setq org-latex-fragment-image-overlays nil))
  21326. (defun org-preview-latex-fragment (&optional subtree)
  21327. "Preview the LaTeX fragment at point, or all locally or globally.
  21328. If the cursor is in a LaTeX fragment, create the image and overlay
  21329. it over the source code. If there is no fragment at point, display
  21330. all fragments in the current text, from one headline to the next. With
  21331. prefix SUBTREE, display all fragments in the current subtree. With a
  21332. double prefix `C-u C-u', or when the cursor is before the first headline,
  21333. display all fragments in the buffer.
  21334. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  21335. (interactive "P")
  21336. (org-remove-latex-fragment-image-overlays)
  21337. (save-excursion
  21338. (save-restriction
  21339. (let (beg end at msg)
  21340. (cond
  21341. ((or (equal subtree '(16))
  21342. (not (save-excursion
  21343. (re-search-backward (concat "^" outline-regexp) nil t))))
  21344. (setq beg (point-min) end (point-max)
  21345. msg "Creating images for buffer...%s"))
  21346. ((equal subtree '(4))
  21347. (org-back-to-heading)
  21348. (setq beg (point) end (org-end-of-subtree t)
  21349. msg "Creating images for subtree...%s"))
  21350. (t
  21351. (if (setq at (org-inside-LaTeX-fragment-p))
  21352. (goto-char (max (point-min) (- (cdr at) 2)))
  21353. (org-back-to-heading))
  21354. (setq beg (point) end (progn (outline-next-heading) (point))
  21355. msg (if at "Creating image...%s"
  21356. "Creating images for entry...%s"))))
  21357. (message msg "")
  21358. (narrow-to-region beg end)
  21359. (goto-char beg)
  21360. (org-format-latex
  21361. (concat "ltxpng/" (file-name-sans-extension
  21362. (file-name-nondirectory
  21363. buffer-file-name)))
  21364. default-directory 'overlays msg at 'forbuffer)
  21365. (message msg "done. Use `C-c C-c' to remove images.")))))
  21366. (defvar org-latex-regexps
  21367. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  21368. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  21369. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  21370. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  21371. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  21372. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  21373. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  21374. "Regular expressions for matching embedded LaTeX.")
  21375. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  21376. "Replace LaTeX fragments with links to an image, and produce images."
  21377. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  21378. (let* ((prefixnodir (file-name-nondirectory prefix))
  21379. (absprefix (expand-file-name prefix dir))
  21380. (todir (file-name-directory absprefix))
  21381. (opt org-format-latex-options)
  21382. (matchers (plist-get opt :matchers))
  21383. (re-list org-latex-regexps)
  21384. (cnt 0) txt link beg end re e checkdir
  21385. m n block linkfile movefile ov)
  21386. ;; Check if there are old images files with this prefix, and remove them
  21387. (when (file-directory-p todir)
  21388. (mapc 'delete-file
  21389. (directory-files
  21390. todir 'full
  21391. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  21392. ;; Check the different regular expressions
  21393. (while (setq e (pop re-list))
  21394. (setq m (car e) re (nth 1 e) n (nth 2 e)
  21395. block (if (nth 3 e) "\n\n" ""))
  21396. (when (member m matchers)
  21397. (goto-char (point-min))
  21398. (while (re-search-forward re nil t)
  21399. (when (or (not at) (equal (cdr at) (match-beginning n)))
  21400. (setq txt (match-string n)
  21401. beg (match-beginning n) end (match-end n)
  21402. cnt (1+ cnt)
  21403. linkfile (format "%s_%04d.png" prefix cnt)
  21404. movefile (format "%s_%04d.png" absprefix cnt)
  21405. link (concat block "[[file:" linkfile "]]" block))
  21406. (if msg (message msg cnt))
  21407. (goto-char beg)
  21408. (unless checkdir ; make sure the directory exists
  21409. (setq checkdir t)
  21410. (or (file-directory-p todir) (make-directory todir)))
  21411. (org-create-formula-image
  21412. txt movefile opt forbuffer)
  21413. (if overlays
  21414. (progn
  21415. (setq ov (org-make-overlay beg end))
  21416. (if (featurep 'xemacs)
  21417. (progn
  21418. (org-overlay-put ov 'invisible t)
  21419. (org-overlay-put
  21420. ov 'end-glyph
  21421. (make-glyph (vector 'png :file movefile))))
  21422. (org-overlay-put
  21423. ov 'display
  21424. (list 'image :type 'png :file movefile :ascent 'center)))
  21425. (push ov org-latex-fragment-image-overlays)
  21426. (goto-char end))
  21427. (delete-region beg end)
  21428. (insert link))))))))
  21429. ;; This function borrows from Ganesh Swami's latex2png.el
  21430. (defun org-create-formula-image (string tofile options buffer)
  21431. (let* ((tmpdir (if (featurep 'xemacs)
  21432. (temp-directory)
  21433. temporary-file-directory))
  21434. (texfilebase (make-temp-name
  21435. (expand-file-name "orgtex" tmpdir)))
  21436. (texfile (concat texfilebase ".tex"))
  21437. (dvifile (concat texfilebase ".dvi"))
  21438. (pngfile (concat texfilebase ".png"))
  21439. (fnh (face-attribute 'default :height nil))
  21440. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  21441. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  21442. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  21443. "Black"))
  21444. (bg (or (plist-get options (if buffer :background :html-background))
  21445. "Transparent")))
  21446. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  21447. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  21448. (with-temp-file texfile
  21449. (insert org-format-latex-header
  21450. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  21451. (let ((dir default-directory))
  21452. (condition-case nil
  21453. (progn
  21454. (cd tmpdir)
  21455. (call-process "latex" nil nil nil texfile))
  21456. (error nil))
  21457. (cd dir))
  21458. (if (not (file-exists-p dvifile))
  21459. (progn (message "Failed to create dvi file from %s" texfile) nil)
  21460. (call-process "dvipng" nil nil nil
  21461. "-E" "-fg" fg "-bg" bg
  21462. "-D" dpi
  21463. ;;"-x" scale "-y" scale
  21464. "-T" "tight"
  21465. "-o" pngfile
  21466. dvifile)
  21467. (if (not (file-exists-p pngfile))
  21468. (progn (message "Failed to create png file from %s" texfile) nil)
  21469. ;; Use the requested file name and clean up
  21470. (copy-file pngfile tofile 'replace)
  21471. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  21472. (delete-file (concat texfilebase e)))
  21473. pngfile))))
  21474. (defun org-dvipng-color (attr)
  21475. "Return an rgb color specification for dvipng."
  21476. (apply 'format "rgb %s %s %s"
  21477. (mapcar 'org-normalize-color
  21478. (color-values (face-attribute 'default attr nil)))))
  21479. (defun org-normalize-color (value)
  21480. "Return string to be used as color value for an RGB component."
  21481. (format "%g" (/ value 65535.0)))
  21482. ;;;; Exporting
  21483. ;;; Variables, constants, and parameter plists
  21484. (defconst org-level-max 20)
  21485. (defvar org-export-html-preamble nil
  21486. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  21487. (defvar org-export-html-postamble nil
  21488. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  21489. (defvar org-export-html-auto-preamble t
  21490. "Should default preamble be inserted? Set by publishing functions.")
  21491. (defvar org-export-html-auto-postamble t
  21492. "Should default postamble be inserted? Set by publishing functions.")
  21493. (defvar org-current-export-file nil) ; dynamically scoped parameter
  21494. (defvar org-current-export-dir nil) ; dynamically scoped parameter
  21495. (defconst org-export-plist-vars
  21496. '((:language . org-export-default-language)
  21497. (:customtime . org-display-custom-times)
  21498. (:headline-levels . org-export-headline-levels)
  21499. (:section-numbers . org-export-with-section-numbers)
  21500. (:table-of-contents . org-export-with-toc)
  21501. (:preserve-breaks . org-export-preserve-breaks)
  21502. (:archived-trees . org-export-with-archived-trees)
  21503. (:emphasize . org-export-with-emphasize)
  21504. (:sub-superscript . org-export-with-sub-superscripts)
  21505. (:special-strings . org-export-with-special-strings)
  21506. (:footnotes . org-export-with-footnotes)
  21507. (:drawers . org-export-with-drawers)
  21508. (:tags . org-export-with-tags)
  21509. (:TeX-macros . org-export-with-TeX-macros)
  21510. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  21511. (:skip-before-1st-heading . org-export-skip-text-before-1st-heading)
  21512. (:fixed-width . org-export-with-fixed-width)
  21513. (:timestamps . org-export-with-timestamps)
  21514. (:author-info . org-export-author-info)
  21515. (:time-stamp-file . org-export-time-stamp-file)
  21516. (:tables . org-export-with-tables)
  21517. (:table-auto-headline . org-export-highlight-first-table-line)
  21518. (:style . org-export-html-style)
  21519. (:agenda-style . org-agenda-export-html-style)
  21520. (:convert-org-links . org-export-html-link-org-files-as-html)
  21521. (:inline-images . org-export-html-inline-images)
  21522. (:html-extension . org-export-html-extension)
  21523. (:html-table-tag . org-export-html-table-tag)
  21524. (:expand-quoted-html . org-export-html-expand)
  21525. (:timestamp . org-export-html-with-timestamp)
  21526. (:publishing-directory . org-export-publishing-directory)
  21527. (:preamble . org-export-html-preamble)
  21528. (:postamble . org-export-html-postamble)
  21529. (:auto-preamble . org-export-html-auto-preamble)
  21530. (:auto-postamble . org-export-html-auto-postamble)
  21531. (:author . user-full-name)
  21532. (:email . user-mail-address)))
  21533. (defun org-default-export-plist ()
  21534. "Return the property list with default settings for the export variables."
  21535. (let ((l org-export-plist-vars) rtn e)
  21536. (while (setq e (pop l))
  21537. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  21538. rtn))
  21539. (defun org-infile-export-plist ()
  21540. "Return the property list with file-local settings for export."
  21541. (save-excursion
  21542. (save-restriction
  21543. (widen)
  21544. (goto-char 0)
  21545. (let ((re (org-make-options-regexp
  21546. '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  21547. p key val text options)
  21548. (while (re-search-forward re nil t)
  21549. (setq key (org-match-string-no-properties 1)
  21550. val (org-match-string-no-properties 2))
  21551. (cond
  21552. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  21553. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  21554. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  21555. ((string-equal key "DATE") (setq p (plist-put p :date val)))
  21556. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  21557. ((string-equal key "TEXT")
  21558. (setq text (if text (concat text "\n" val) val)))
  21559. ((string-equal key "OPTIONS") (setq options val))))
  21560. (setq p (plist-put p :text text))
  21561. (when options
  21562. (let ((op '(("H" . :headline-levels)
  21563. ("num" . :section-numbers)
  21564. ("toc" . :table-of-contents)
  21565. ("\\n" . :preserve-breaks)
  21566. ("@" . :expand-quoted-html)
  21567. (":" . :fixed-width)
  21568. ("|" . :tables)
  21569. ("^" . :sub-superscript)
  21570. ("-" . :special-strings)
  21571. ("f" . :footnotes)
  21572. ("d" . :drawers)
  21573. ("tags" . :tags)
  21574. ("*" . :emphasize)
  21575. ("TeX" . :TeX-macros)
  21576. ("LaTeX" . :LaTeX-fragments)
  21577. ("skip" . :skip-before-1st-heading)
  21578. ("author" . :author-info)
  21579. ("timestamp" . :time-stamp-file)))
  21580. o)
  21581. (while (setq o (pop op))
  21582. (if (string-match (concat (regexp-quote (car o))
  21583. ":\\([^ \t\n\r;,.]*\\)")
  21584. options)
  21585. (setq p (plist-put p (cdr o)
  21586. (car (read-from-string
  21587. (match-string 1 options)))))))))
  21588. p))))
  21589. (defun org-export-directory (type plist)
  21590. (let* ((val (plist-get plist :publishing-directory))
  21591. (dir (if (listp val)
  21592. (or (cdr (assoc type val)) ".")
  21593. val)))
  21594. dir))
  21595. (defun org-skip-comments (lines)
  21596. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  21597. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  21598. (re2 "^\\(\\*+\\)[ \t\n\r]")
  21599. (case-fold-search nil)
  21600. rtn line level)
  21601. (while (setq line (pop lines))
  21602. (cond
  21603. ((and (string-match re1 line)
  21604. (setq level (- (match-end 1) (match-beginning 1))))
  21605. ;; Beginning of a COMMENT subtree. Skip it.
  21606. (while (and (setq line (pop lines))
  21607. (or (not (string-match re2 line))
  21608. (> (- (match-end 1) (match-beginning 1)) level))))
  21609. (setq lines (cons line lines)))
  21610. ((string-match "^#" line)
  21611. ;; an ordinary comment line
  21612. )
  21613. ((and org-export-table-remove-special-lines
  21614. (string-match "^[ \t]*|" line)
  21615. (or (string-match "^[ \t]*| *[!_^] *|" line)
  21616. (and (string-match "| *<[0-9]+> *|" line)
  21617. (not (string-match "| *[^ <|]" line)))))
  21618. ;; a special table line that should be removed
  21619. )
  21620. (t (setq rtn (cons line rtn)))))
  21621. (nreverse rtn)))
  21622. (defun org-export (&optional arg)
  21623. (interactive)
  21624. (let ((help "[t] insert the export option template
  21625. \[v] limit export to visible part of outline tree
  21626. \[a] export as ASCII
  21627. \[h] export as HTML
  21628. \[H] export as HTML to temporary buffer
  21629. \[R] export region as HTML
  21630. \[b] export as HTML and browse immediately
  21631. \[x] export as XOXO
  21632. \[l] export as LaTeX
  21633. \[L] export as LaTeX to temporary buffer
  21634. \[i] export current file as iCalendar file
  21635. \[I] export all agenda files as iCalendar files
  21636. \[c] export agenda files into combined iCalendar file
  21637. \[F] publish current file
  21638. \[P] publish current project
  21639. \[X] publish... (project will be prompted for)
  21640. \[A] publish all projects")
  21641. (cmds
  21642. '((?t . org-insert-export-options-template)
  21643. (?v . org-export-visible)
  21644. (?a . org-export-as-ascii)
  21645. (?h . org-export-as-html)
  21646. (?b . org-export-as-html-and-open)
  21647. (?H . org-export-as-html-to-buffer)
  21648. (?R . org-export-region-as-html)
  21649. (?x . org-export-as-xoxo)
  21650. (?l . org-export-as-latex)
  21651. (?L . org-export-as-latex-to-buffer)
  21652. (?i . org-export-icalendar-this-file)
  21653. (?I . org-export-icalendar-all-agenda-files)
  21654. (?c . org-export-icalendar-combine-agenda-files)
  21655. (?F . org-publish-current-file)
  21656. (?P . org-publish-current-project)
  21657. (?X . org-publish)
  21658. (?A . org-publish-all)))
  21659. r1 r2 ass)
  21660. (save-window-excursion
  21661. (delete-other-windows)
  21662. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  21663. (princ help))
  21664. (message "Select command: ")
  21665. (setq r1 (read-char-exclusive)))
  21666. (setq r2 (if (< r1 27) (+ r1 96) r1))
  21667. (if (setq ass (assq r2 cmds))
  21668. (call-interactively (cdr ass))
  21669. (error "No command associated with key %c" r1))))
  21670. (defconst org-html-entities
  21671. '(("nbsp")
  21672. ("iexcl")
  21673. ("cent")
  21674. ("pound")
  21675. ("curren")
  21676. ("yen")
  21677. ("brvbar")
  21678. ("vert" . "&#124;")
  21679. ("sect")
  21680. ("uml")
  21681. ("copy")
  21682. ("ordf")
  21683. ("laquo")
  21684. ("not")
  21685. ("shy")
  21686. ("reg")
  21687. ("macr")
  21688. ("deg")
  21689. ("plusmn")
  21690. ("sup2")
  21691. ("sup3")
  21692. ("acute")
  21693. ("micro")
  21694. ("para")
  21695. ("middot")
  21696. ("odot"."o")
  21697. ("star"."*")
  21698. ("cedil")
  21699. ("sup1")
  21700. ("ordm")
  21701. ("raquo")
  21702. ("frac14")
  21703. ("frac12")
  21704. ("frac34")
  21705. ("iquest")
  21706. ("Agrave")
  21707. ("Aacute")
  21708. ("Acirc")
  21709. ("Atilde")
  21710. ("Auml")
  21711. ("Aring") ("AA"."&Aring;")
  21712. ("AElig")
  21713. ("Ccedil")
  21714. ("Egrave")
  21715. ("Eacute")
  21716. ("Ecirc")
  21717. ("Euml")
  21718. ("Igrave")
  21719. ("Iacute")
  21720. ("Icirc")
  21721. ("Iuml")
  21722. ("ETH")
  21723. ("Ntilde")
  21724. ("Ograve")
  21725. ("Oacute")
  21726. ("Ocirc")
  21727. ("Otilde")
  21728. ("Ouml")
  21729. ("times")
  21730. ("Oslash")
  21731. ("Ugrave")
  21732. ("Uacute")
  21733. ("Ucirc")
  21734. ("Uuml")
  21735. ("Yacute")
  21736. ("THORN")
  21737. ("szlig")
  21738. ("agrave")
  21739. ("aacute")
  21740. ("acirc")
  21741. ("atilde")
  21742. ("auml")
  21743. ("aring")
  21744. ("aelig")
  21745. ("ccedil")
  21746. ("egrave")
  21747. ("eacute")
  21748. ("ecirc")
  21749. ("euml")
  21750. ("igrave")
  21751. ("iacute")
  21752. ("icirc")
  21753. ("iuml")
  21754. ("eth")
  21755. ("ntilde")
  21756. ("ograve")
  21757. ("oacute")
  21758. ("ocirc")
  21759. ("otilde")
  21760. ("ouml")
  21761. ("divide")
  21762. ("oslash")
  21763. ("ugrave")
  21764. ("uacute")
  21765. ("ucirc")
  21766. ("uuml")
  21767. ("yacute")
  21768. ("thorn")
  21769. ("yuml")
  21770. ("fnof")
  21771. ("Alpha")
  21772. ("Beta")
  21773. ("Gamma")
  21774. ("Delta")
  21775. ("Epsilon")
  21776. ("Zeta")
  21777. ("Eta")
  21778. ("Theta")
  21779. ("Iota")
  21780. ("Kappa")
  21781. ("Lambda")
  21782. ("Mu")
  21783. ("Nu")
  21784. ("Xi")
  21785. ("Omicron")
  21786. ("Pi")
  21787. ("Rho")
  21788. ("Sigma")
  21789. ("Tau")
  21790. ("Upsilon")
  21791. ("Phi")
  21792. ("Chi")
  21793. ("Psi")
  21794. ("Omega")
  21795. ("alpha")
  21796. ("beta")
  21797. ("gamma")
  21798. ("delta")
  21799. ("epsilon")
  21800. ("varepsilon"."&epsilon;")
  21801. ("zeta")
  21802. ("eta")
  21803. ("theta")
  21804. ("iota")
  21805. ("kappa")
  21806. ("lambda")
  21807. ("mu")
  21808. ("nu")
  21809. ("xi")
  21810. ("omicron")
  21811. ("pi")
  21812. ("rho")
  21813. ("sigmaf") ("varsigma"."&sigmaf;")
  21814. ("sigma")
  21815. ("tau")
  21816. ("upsilon")
  21817. ("phi")
  21818. ("chi")
  21819. ("psi")
  21820. ("omega")
  21821. ("thetasym") ("vartheta"."&thetasym;")
  21822. ("upsih")
  21823. ("piv")
  21824. ("bull") ("bullet"."&bull;")
  21825. ("hellip") ("dots"."&hellip;")
  21826. ("prime")
  21827. ("Prime")
  21828. ("oline")
  21829. ("frasl")
  21830. ("weierp")
  21831. ("image")
  21832. ("real")
  21833. ("trade")
  21834. ("alefsym")
  21835. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  21836. ("uarr") ("uparrow"."&uarr;")
  21837. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  21838. ("darr")("downarrow"."&darr;")
  21839. ("harr") ("leftrightarrow"."&harr;")
  21840. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  21841. ("lArr") ("Leftarrow"."&lArr;")
  21842. ("uArr") ("Uparrow"."&uArr;")
  21843. ("rArr") ("Rightarrow"."&rArr;")
  21844. ("dArr") ("Downarrow"."&dArr;")
  21845. ("hArr") ("Leftrightarrow"."&hArr;")
  21846. ("forall")
  21847. ("part") ("partial"."&part;")
  21848. ("exist") ("exists"."&exist;")
  21849. ("empty") ("emptyset"."&empty;")
  21850. ("nabla")
  21851. ("isin") ("in"."&isin;")
  21852. ("notin")
  21853. ("ni")
  21854. ("prod")
  21855. ("sum")
  21856. ("minus")
  21857. ("lowast") ("ast"."&lowast;")
  21858. ("radic")
  21859. ("prop") ("proptp"."&prop;")
  21860. ("infin") ("infty"."&infin;")
  21861. ("ang") ("angle"."&ang;")
  21862. ("and") ("wedge"."&and;")
  21863. ("or") ("vee"."&or;")
  21864. ("cap")
  21865. ("cup")
  21866. ("int")
  21867. ("there4")
  21868. ("sim")
  21869. ("cong") ("simeq"."&cong;")
  21870. ("asymp")("approx"."&asymp;")
  21871. ("ne") ("neq"."&ne;")
  21872. ("equiv")
  21873. ("le")
  21874. ("ge")
  21875. ("sub") ("subset"."&sub;")
  21876. ("sup") ("supset"."&sup;")
  21877. ("nsub")
  21878. ("sube")
  21879. ("supe")
  21880. ("oplus")
  21881. ("otimes")
  21882. ("perp")
  21883. ("sdot") ("cdot"."&sdot;")
  21884. ("lceil")
  21885. ("rceil")
  21886. ("lfloor")
  21887. ("rfloor")
  21888. ("lang")
  21889. ("rang")
  21890. ("loz") ("Diamond"."&loz;")
  21891. ("spades") ("spadesuit"."&spades;")
  21892. ("clubs") ("clubsuit"."&clubs;")
  21893. ("hearts") ("diamondsuit"."&hearts;")
  21894. ("diams") ("diamondsuit"."&diams;")
  21895. ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
  21896. ("quot")
  21897. ("amp")
  21898. ("lt")
  21899. ("gt")
  21900. ("OElig")
  21901. ("oelig")
  21902. ("Scaron")
  21903. ("scaron")
  21904. ("Yuml")
  21905. ("circ")
  21906. ("tilde")
  21907. ("ensp")
  21908. ("emsp")
  21909. ("thinsp")
  21910. ("zwnj")
  21911. ("zwj")
  21912. ("lrm")
  21913. ("rlm")
  21914. ("ndash")
  21915. ("mdash")
  21916. ("lsquo")
  21917. ("rsquo")
  21918. ("sbquo")
  21919. ("ldquo")
  21920. ("rdquo")
  21921. ("bdquo")
  21922. ("dagger")
  21923. ("Dagger")
  21924. ("permil")
  21925. ("lsaquo")
  21926. ("rsaquo")
  21927. ("euro")
  21928. ("arccos"."arccos")
  21929. ("arcsin"."arcsin")
  21930. ("arctan"."arctan")
  21931. ("arg"."arg")
  21932. ("cos"."cos")
  21933. ("cosh"."cosh")
  21934. ("cot"."cot")
  21935. ("coth"."coth")
  21936. ("csc"."csc")
  21937. ("deg"."deg")
  21938. ("det"."det")
  21939. ("dim"."dim")
  21940. ("exp"."exp")
  21941. ("gcd"."gcd")
  21942. ("hom"."hom")
  21943. ("inf"."inf")
  21944. ("ker"."ker")
  21945. ("lg"."lg")
  21946. ("lim"."lim")
  21947. ("liminf"."liminf")
  21948. ("limsup"."limsup")
  21949. ("ln"."ln")
  21950. ("log"."log")
  21951. ("max"."max")
  21952. ("min"."min")
  21953. ("Pr"."Pr")
  21954. ("sec"."sec")
  21955. ("sin"."sin")
  21956. ("sinh"."sinh")
  21957. ("sup"."sup")
  21958. ("tan"."tan")
  21959. ("tanh"."tanh")
  21960. )
  21961. "Entities for TeX->HTML translation.
  21962. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  21963. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  21964. In that case, \"\\ent\" will be translated to \"&other;\".
  21965. The list contains HTML entities for Latin-1, Greek and other symbols.
  21966. It is supplemented by a number of commonly used TeX macros with appropriate
  21967. translations. There is currently no way for users to extend this.")
  21968. ;;; General functions for all backends
  21969. (defun org-cleaned-string-for-export (string &rest parameters)
  21970. "Cleanup a buffer STRING so that links can be created safely."
  21971. (interactive)
  21972. (let* ((re-radio (and org-target-link-regexp
  21973. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  21974. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  21975. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  21976. (re-archive (concat ":" org-archive-tag ":"))
  21977. (re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>"))
  21978. (re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
  21979. (htmlp (plist-get parameters :for-html))
  21980. (asciip (plist-get parameters :for-ascii))
  21981. (latexp (plist-get parameters :for-LaTeX))
  21982. (commentsp (plist-get parameters :comments))
  21983. (archived-trees (plist-get parameters :archived-trees))
  21984. (inhibit-read-only t)
  21985. (drawers org-drawers)
  21986. (exp-drawers (plist-get parameters :drawers))
  21987. (outline-regexp "\\*+ ")
  21988. a b xx
  21989. rtn p)
  21990. (with-current-buffer (get-buffer-create " org-mode-tmp")
  21991. (erase-buffer)
  21992. (insert string)
  21993. ;; Remove license-to-kill stuff
  21994. (while (setq p (text-property-any (point-min) (point-max)
  21995. :org-license-to-kill t))
  21996. (delete-region p (next-single-property-change p :org-license-to-kill)))
  21997. (let ((org-inhibit-startup t)) (org-mode))
  21998. (untabify (point-min) (point-max))
  21999. ;; Get the correct stuff before the first headline
  22000. (when (plist-get parameters :skip-before-1st-heading)
  22001. (goto-char (point-min))
  22002. (when (re-search-forward "^\\*+[ \t]" nil t)
  22003. (delete-region (point-min) (match-beginning 0))
  22004. (goto-char (point-min))
  22005. (insert "\n")))
  22006. (when (plist-get parameters :add-text)
  22007. (goto-char (point-min))
  22008. (insert (plist-get parameters :add-text) "\n"))
  22009. ;; Get rid of archived trees
  22010. (when (not (eq archived-trees t))
  22011. (goto-char (point-min))
  22012. (while (re-search-forward re-archive nil t)
  22013. (if (not (org-on-heading-p t))
  22014. (org-end-of-subtree t)
  22015. (beginning-of-line 1)
  22016. (setq a (if archived-trees
  22017. (1+ (point-at-eol)) (point))
  22018. b (org-end-of-subtree t))
  22019. (if (> b a) (delete-region a b)))))
  22020. ;; Get rid of drawers
  22021. (unless (eq t exp-drawers)
  22022. (goto-char (point-min))
  22023. (let ((re (concat "^[ \t]*:\\("
  22024. (mapconcat
  22025. 'identity
  22026. (org-delete-all exp-drawers
  22027. (copy-sequence drawers))
  22028. "\\|")
  22029. "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
  22030. (while (re-search-forward re nil t)
  22031. (replace-match ""))))
  22032. ;; Find targets in comments and move them out of comments,
  22033. ;; but mark them as targets that should be invisible
  22034. (goto-char (point-min))
  22035. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  22036. (replace-match "\\1(INVISIBLE)"))
  22037. ;; Protect backend specific stuff, throw away the others.
  22038. (let ((formatters
  22039. `((,htmlp "HTML" "BEGIN_HTML" "END_HTML")
  22040. (,asciip "ASCII" "BEGIN_ASCII" "END_ASCII")
  22041. (,latexp "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
  22042. fmt)
  22043. (goto-char (point-min))
  22044. (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t)
  22045. (goto-char (match-end 0))
  22046. (while (not (looking-at "#\\+END_EXAMPLE"))
  22047. (insert ": ")
  22048. (beginning-of-line 2)))
  22049. (goto-char (point-min))
  22050. (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t)
  22051. (add-text-properties (match-beginning 0) (match-end 0)
  22052. '(org-protected t)))
  22053. (while formatters
  22054. (setq fmt (pop formatters))
  22055. (when (car fmt)
  22056. (goto-char (point-min))
  22057. (while (re-search-forward (concat "^#\\+" (cadr fmt)
  22058. ":[ \t]*\\(.*\\)") nil t)
  22059. (replace-match "\\1" t)
  22060. (add-text-properties
  22061. (point-at-bol) (min (1+ (point-at-eol)) (point-max))
  22062. '(org-protected t))))
  22063. (goto-char (point-min))
  22064. (while (re-search-forward
  22065. (concat "^#\\+"
  22066. (caddr fmt) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
  22067. (cadddr fmt) "\\>.*\n?") nil t)
  22068. (if (car fmt)
  22069. (add-text-properties (match-beginning 1) (1+ (match-end 1))
  22070. '(org-protected t))
  22071. (delete-region (match-beginning 0) (match-end 0))))))
  22072. ;; Protect quoted subtrees
  22073. (goto-char (point-min))
  22074. (while (re-search-forward re-quote nil t)
  22075. (goto-char (match-beginning 0))
  22076. (end-of-line 1)
  22077. (add-text-properties (point) (org-end-of-subtree t)
  22078. '(org-protected t)))
  22079. ;; Protect verbatim elements
  22080. (goto-char (point-min))
  22081. (while (re-search-forward org-verbatim-re nil t)
  22082. (add-text-properties (match-beginning 4) (match-end 4)
  22083. '(org-protected t))
  22084. (goto-char (1+ (match-end 4))))
  22085. ;; Remove subtrees that are commented
  22086. (goto-char (point-min))
  22087. (while (re-search-forward re-commented nil t)
  22088. (goto-char (match-beginning 0))
  22089. (delete-region (point) (org-end-of-subtree t)))
  22090. ;; Remove special table lines
  22091. (when org-export-table-remove-special-lines
  22092. (goto-char (point-min))
  22093. (while (re-search-forward "^[ \t]*|" nil t)
  22094. (beginning-of-line 1)
  22095. (if (or (looking-at "[ \t]*| *[!_^] *|")
  22096. (and (looking-at ".*?| *<[0-9]+> *|")
  22097. (not (looking-at ".*?| *[^ <|]"))))
  22098. (delete-region (max (point-min) (1- (point-at-bol)))
  22099. (point-at-eol))
  22100. (end-of-line 1))))
  22101. ;; Specific LaTeX stuff
  22102. (when latexp
  22103. (require 'org-export-latex nil)
  22104. (org-export-latex-cleaned-string))
  22105. (when asciip
  22106. (org-export-ascii-clean-string))
  22107. ;; Specific HTML stuff
  22108. (when htmlp
  22109. ;; Convert LaTeX fragments to images
  22110. (when (plist-get parameters :LaTeX-fragments)
  22111. (org-format-latex
  22112. (concat "ltxpng/" (file-name-sans-extension
  22113. (file-name-nondirectory
  22114. org-current-export-file)))
  22115. org-current-export-dir nil "Creating LaTeX image %s"))
  22116. (message "Exporting..."))
  22117. ;; Remove or replace comments
  22118. (goto-char (point-min))
  22119. (while (re-search-forward "^#\\(.*\n?\\)" nil t)
  22120. (if commentsp
  22121. (progn (add-text-properties
  22122. (match-beginning 0) (match-end 0) '(org-protected t))
  22123. (replace-match (format commentsp (match-string 1)) t t))
  22124. (replace-match "")))
  22125. ;; Find matches for radio targets and turn them into internal links
  22126. (goto-char (point-min))
  22127. (when re-radio
  22128. (while (re-search-forward re-radio nil t)
  22129. (org-if-unprotected
  22130. (replace-match "\\1[[\\2]]"))))
  22131. ;; Find all links that contain a newline and put them into a single line
  22132. (goto-char (point-min))
  22133. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  22134. (org-if-unprotected
  22135. (replace-match "\\1 \\3")
  22136. (goto-char (match-beginning 0))))
  22137. ;; Normalize links: Convert angle and plain links into bracket links
  22138. ;; Expand link abbreviations
  22139. (goto-char (point-min))
  22140. (while (re-search-forward re-plain-link nil t)
  22141. (goto-char (1- (match-end 0)))
  22142. (org-if-unprotected
  22143. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22144. ":" (match-string 3) "]]")))
  22145. ;; added 'org-link face to links
  22146. (put-text-property 0 (length s) 'face 'org-link s)
  22147. (replace-match s t t))))
  22148. (goto-char (point-min))
  22149. (while (re-search-forward re-angle-link nil t)
  22150. (goto-char (1- (match-end 0)))
  22151. (org-if-unprotected
  22152. (let* ((s (concat (match-string 1) "[[" (match-string 2)
  22153. ":" (match-string 3) "]]")))
  22154. (put-text-property 0 (length s) 'face 'org-link s)
  22155. (replace-match s t t))))
  22156. (goto-char (point-min))
  22157. (while (re-search-forward org-bracket-link-regexp nil t)
  22158. (org-if-unprotected
  22159. (let* ((s (concat "[[" (setq xx (save-match-data
  22160. (org-link-expand-abbrev (match-string 1))))
  22161. "]"
  22162. (if (match-end 3)
  22163. (match-string 2)
  22164. (concat "[" xx "]"))
  22165. "]")))
  22166. (put-text-property 0 (length s) 'face 'org-link s)
  22167. (replace-match s t t))))
  22168. ;; Find multiline emphasis and put them into single line
  22169. (when (plist-get parameters :emph-multiline)
  22170. (goto-char (point-min))
  22171. (while (re-search-forward org-emph-re nil t)
  22172. (if (not (= (char-after (match-beginning 3))
  22173. (char-after (match-beginning 4))))
  22174. (org-if-unprotected
  22175. (subst-char-in-region (match-beginning 0) (match-end 0)
  22176. ?\n ?\ t)
  22177. (goto-char (1- (match-end 0))))
  22178. (goto-char (1+ (match-beginning 0))))))
  22179. (setq rtn (buffer-string)))
  22180. (kill-buffer " org-mode-tmp")
  22181. rtn))
  22182. (defun org-export-grab-title-from-buffer ()
  22183. "Get a title for the current document, from looking at the buffer."
  22184. (let ((inhibit-read-only t))
  22185. (save-excursion
  22186. (goto-char (point-min))
  22187. (let ((end (save-excursion (outline-next-heading) (point))))
  22188. (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
  22189. ;; Mark the line so that it will not be exported as normal text.
  22190. (org-unmodified
  22191. (add-text-properties (match-beginning 0) (match-end 0)
  22192. (list :org-license-to-kill t)))
  22193. ;; Return the title string
  22194. (org-trim (match-string 0)))))))
  22195. (defun org-export-get-title-from-subtree ()
  22196. "Return subtree title and exclude it from export."
  22197. (let (title (m (mark)))
  22198. (save-excursion
  22199. (goto-char (region-beginning))
  22200. (when (and (org-at-heading-p)
  22201. (>= (org-end-of-subtree t t) (region-end)))
  22202. ;; This is a subtree, we take the title from the first heading
  22203. (goto-char (region-beginning))
  22204. (looking-at org-todo-line-regexp)
  22205. (setq title (match-string 3))
  22206. (org-unmodified
  22207. (add-text-properties (point) (1+ (point-at-eol))
  22208. (list :org-license-to-kill t)))))
  22209. title))
  22210. (defun org-solidify-link-text (s &optional alist)
  22211. "Take link text and make a safe target out of it."
  22212. (save-match-data
  22213. (let* ((rtn
  22214. (mapconcat
  22215. 'identity
  22216. (org-split-string s "[ \t\r\n]+") "--"))
  22217. (a (assoc rtn alist)))
  22218. (or (cdr a) rtn))))
  22219. (defun org-get-min-level (lines)
  22220. "Get the minimum level in LINES."
  22221. (let ((re "^\\(\\*+\\) ") l min)
  22222. (catch 'exit
  22223. (while (setq l (pop lines))
  22224. (if (string-match re l)
  22225. (throw 'exit (org-tr-level (length (match-string 1 l))))))
  22226. 1)))
  22227. ;; Variable holding the vector with section numbers
  22228. (defvar org-section-numbers (make-vector org-level-max 0))
  22229. (defun org-init-section-numbers ()
  22230. "Initialize the vector for the section numbers."
  22231. (let* ((level -1)
  22232. (numbers (nreverse (org-split-string "" "\\.")))
  22233. (depth (1- (length org-section-numbers)))
  22234. (i depth) number-string)
  22235. (while (>= i 0)
  22236. (if (> i level)
  22237. (aset org-section-numbers i 0)
  22238. (setq number-string (or (car numbers) "0"))
  22239. (if (string-match "\\`[A-Z]\\'" number-string)
  22240. (aset org-section-numbers i
  22241. (- (string-to-char number-string) ?A -1))
  22242. (aset org-section-numbers i (string-to-number number-string)))
  22243. (pop numbers))
  22244. (setq i (1- i)))))
  22245. (defun org-section-number (&optional level)
  22246. "Return a string with the current section number.
  22247. When LEVEL is non-nil, increase section numbers on that level."
  22248. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  22249. (when level
  22250. (when (> level -1)
  22251. (aset org-section-numbers
  22252. level (1+ (aref org-section-numbers level))))
  22253. (setq idx (1+ level))
  22254. (while (<= idx depth)
  22255. (if (not (= idx 1))
  22256. (aset org-section-numbers idx 0))
  22257. (setq idx (1+ idx))))
  22258. (setq idx 0)
  22259. (while (<= idx depth)
  22260. (setq n (aref org-section-numbers idx))
  22261. (setq string (concat string (if (not (string= string "")) "." "")
  22262. (int-to-string n)))
  22263. (setq idx (1+ idx)))
  22264. (save-match-data
  22265. (if (string-match "\\`\\([@0]\\.\\)+" string)
  22266. (setq string (replace-match "" t nil string)))
  22267. (if (string-match "\\(\\.0\\)+\\'" string)
  22268. (setq string (replace-match "" t nil string))))
  22269. string))
  22270. ;;; ASCII export
  22271. (defvar org-last-level nil) ; dynamically scoped variable
  22272. (defvar org-min-level nil) ; dynamically scoped variable
  22273. (defvar org-levels-open nil) ; dynamically scoped parameter
  22274. (defvar org-ascii-current-indentation nil) ; For communication
  22275. (defun org-export-as-ascii (arg)
  22276. "Export the outline as a pretty ASCII file.
  22277. If there is an active region, export only the region.
  22278. The prefix ARG specifies how many levels of the outline should become
  22279. underlined headlines. The default is 3."
  22280. (interactive "P")
  22281. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22282. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22283. (org-infile-export-plist)))
  22284. (region-p (org-region-active-p))
  22285. (subtree-p
  22286. (when region-p
  22287. (save-excursion
  22288. (goto-char (region-beginning))
  22289. (and (org-at-heading-p)
  22290. (>= (org-end-of-subtree t t) (region-end))))))
  22291. (custom-times org-display-custom-times)
  22292. (org-ascii-current-indentation '(0 . 0))
  22293. (level 0) line txt
  22294. (umax nil)
  22295. (umax-toc nil)
  22296. (case-fold-search nil)
  22297. (filename (concat (file-name-as-directory
  22298. (org-export-directory :ascii opt-plist))
  22299. (file-name-sans-extension
  22300. (or (and subtree-p
  22301. (org-entry-get (region-beginning)
  22302. "EXPORT_FILE_NAME" t))
  22303. (file-name-nondirectory buffer-file-name)))
  22304. ".txt"))
  22305. (filename (if (equal (file-truename filename)
  22306. (file-truename buffer-file-name))
  22307. (concat filename ".txt")
  22308. filename))
  22309. (buffer (find-file-noselect filename))
  22310. (org-levels-open (make-vector org-level-max nil))
  22311. (odd org-odd-levels-only)
  22312. (date (plist-get opt-plist :date))
  22313. (author (plist-get opt-plist :author))
  22314. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22315. (plist-get opt-plist :title)
  22316. (and (not
  22317. (plist-get opt-plist :skip-before-1st-heading))
  22318. (org-export-grab-title-from-buffer))
  22319. (file-name-sans-extension
  22320. (file-name-nondirectory buffer-file-name))))
  22321. (email (plist-get opt-plist :email))
  22322. (language (plist-get opt-plist :language))
  22323. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22324. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  22325. (todo nil)
  22326. (lang-words nil)
  22327. (region
  22328. (buffer-substring
  22329. (if (org-region-active-p) (region-beginning) (point-min))
  22330. (if (org-region-active-p) (region-end) (point-max))))
  22331. (lines (org-split-string
  22332. (org-cleaned-string-for-export
  22333. region
  22334. :for-ascii t
  22335. :skip-before-1st-heading
  22336. (plist-get opt-plist :skip-before-1st-heading)
  22337. :drawers (plist-get opt-plist :drawers)
  22338. :verbatim-multiline t
  22339. :archived-trees
  22340. (plist-get opt-plist :archived-trees)
  22341. :add-text (plist-get opt-plist :text))
  22342. "\n"))
  22343. thetoc have-headings first-heading-pos
  22344. table-open table-buffer)
  22345. (let ((inhibit-read-only t))
  22346. (org-unmodified
  22347. (remove-text-properties (point-min) (point-max)
  22348. '(:org-license-to-kill t))))
  22349. (setq org-min-level (org-get-min-level lines))
  22350. (setq org-last-level org-min-level)
  22351. (org-init-section-numbers)
  22352. (find-file-noselect filename)
  22353. (setq lang-words (or (assoc language org-export-language-setup)
  22354. (assoc "en" org-export-language-setup)))
  22355. (switch-to-buffer-other-window buffer)
  22356. (erase-buffer)
  22357. (fundamental-mode)
  22358. ;; create local variables for all options, to make sure all called
  22359. ;; functions get the correct information
  22360. (mapc (lambda (x)
  22361. (set (make-local-variable (cdr x))
  22362. (plist-get opt-plist (car x))))
  22363. org-export-plist-vars)
  22364. (org-set-local 'org-odd-levels-only odd)
  22365. (setq umax (if arg (prefix-numeric-value arg)
  22366. org-export-headline-levels))
  22367. (setq umax-toc (if (integerp org-export-with-toc)
  22368. (min org-export-with-toc umax)
  22369. umax))
  22370. ;; File header
  22371. (if title (org-insert-centered title ?=))
  22372. (insert "\n")
  22373. (if (and (or author email)
  22374. org-export-author-info)
  22375. (insert (concat (nth 1 lang-words) ": " (or author "")
  22376. (if email (concat " <" email ">") "")
  22377. "\n")))
  22378. (cond
  22379. ((and date (string-match "%" date))
  22380. (setq date (format-time-string date (current-time))))
  22381. (date)
  22382. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22383. (if (and date org-export-time-stamp-file)
  22384. (insert (concat (nth 2 lang-words) ": " date"\n")))
  22385. (insert "\n\n")
  22386. (if org-export-with-toc
  22387. (progn
  22388. (push (concat (nth 3 lang-words) "\n") thetoc)
  22389. (push (concat (make-string (length (nth 3 lang-words)) ?=) "\n") thetoc)
  22390. (mapc '(lambda (line)
  22391. (if (string-match org-todo-line-regexp
  22392. line)
  22393. ;; This is a headline
  22394. (progn
  22395. (setq have-headings t)
  22396. (setq level (- (match-end 1) (match-beginning 1))
  22397. level (org-tr-level level)
  22398. txt (match-string 3 line)
  22399. todo
  22400. (or (and org-export-mark-todo-in-toc
  22401. (match-beginning 2)
  22402. (not (member (match-string 2 line)
  22403. org-done-keywords)))
  22404. ; TODO, not DONE
  22405. (and org-export-mark-todo-in-toc
  22406. (= level umax-toc)
  22407. (org-search-todo-below
  22408. line lines level))))
  22409. (setq txt (org-html-expand-for-ascii txt))
  22410. (while (string-match org-bracket-link-regexp txt)
  22411. (setq txt
  22412. (replace-match
  22413. (match-string (if (match-end 2) 3 1) txt)
  22414. t t txt)))
  22415. (if (and (memq org-export-with-tags '(not-in-toc nil))
  22416. (string-match
  22417. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  22418. txt))
  22419. (setq txt (replace-match "" t t txt)))
  22420. (if (string-match quote-re0 txt)
  22421. (setq txt (replace-match "" t t txt)))
  22422. (if org-export-with-section-numbers
  22423. (setq txt (concat (org-section-number level)
  22424. " " txt)))
  22425. (if (<= level umax-toc)
  22426. (progn
  22427. (push
  22428. (concat
  22429. (make-string
  22430. (* (max 0 (- level org-min-level)) 4) ?\ )
  22431. (format (if todo "%s (*)\n" "%s\n") txt))
  22432. thetoc)
  22433. (setq org-last-level level))
  22434. ))))
  22435. lines)
  22436. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  22437. (org-init-section-numbers)
  22438. (while (setq line (pop lines))
  22439. ;; Remove the quoted HTML tags.
  22440. (setq line (org-html-expand-for-ascii line))
  22441. ;; Remove targets
  22442. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  22443. (setq line (replace-match "" t t line)))
  22444. ;; Replace internal links
  22445. (while (string-match org-bracket-link-regexp line)
  22446. (setq line (replace-match
  22447. (if (match-end 3) "[\\3]" "[\\1]")
  22448. t nil line)))
  22449. (when custom-times
  22450. (setq line (org-translate-time line)))
  22451. (cond
  22452. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  22453. ;; a Headline
  22454. (setq first-heading-pos (or first-heading-pos (point)))
  22455. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  22456. txt (match-string 2 line))
  22457. (org-ascii-level-start level txt umax lines))
  22458. ((and org-export-with-tables
  22459. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  22460. (if (not table-open)
  22461. ;; New table starts
  22462. (setq table-open t table-buffer nil))
  22463. ;; Accumulate lines
  22464. (setq table-buffer (cons line table-buffer))
  22465. (when (or (not lines)
  22466. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  22467. (car lines))))
  22468. (setq table-open nil
  22469. table-buffer (nreverse table-buffer))
  22470. (insert (mapconcat
  22471. (lambda (x)
  22472. (org-fix-indentation x org-ascii-current-indentation))
  22473. (org-format-table-ascii table-buffer)
  22474. "\n") "\n")))
  22475. (t
  22476. (setq line (org-fix-indentation line org-ascii-current-indentation))
  22477. (if (and org-export-with-fixed-width
  22478. (string-match "^\\([ \t]*\\)\\(:\\)" line))
  22479. (setq line (replace-match "\\1" nil nil line)))
  22480. (insert line "\n"))))
  22481. (normal-mode)
  22482. ;; insert the table of contents
  22483. (when thetoc
  22484. (goto-char (point-min))
  22485. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  22486. (progn
  22487. (goto-char (match-beginning 0))
  22488. (replace-match ""))
  22489. (goto-char first-heading-pos))
  22490. (mapc 'insert thetoc)
  22491. (or (looking-at "[ \t]*\n[ \t]*\n")
  22492. (insert "\n\n")))
  22493. ;; Convert whitespace place holders
  22494. (goto-char (point-min))
  22495. (let (beg end)
  22496. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  22497. (setq end (next-single-property-change beg 'org-whitespace))
  22498. (goto-char beg)
  22499. (delete-region beg end)
  22500. (insert (make-string (- end beg) ?\ ))))
  22501. (save-buffer)
  22502. ;; remove display and invisible chars
  22503. (let (beg end)
  22504. (goto-char (point-min))
  22505. (while (setq beg (next-single-property-change (point) 'display))
  22506. (setq end (next-single-property-change beg 'display))
  22507. (delete-region beg end)
  22508. (goto-char beg)
  22509. (insert "=>"))
  22510. (goto-char (point-min))
  22511. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  22512. (setq end (next-single-property-change beg 'org-cwidth))
  22513. (delete-region beg end)
  22514. (goto-char beg)))
  22515. (goto-char (point-min))))
  22516. (defun org-export-ascii-clean-string ()
  22517. "Do extra work for ASCII export"
  22518. (goto-char (point-min))
  22519. (while (re-search-forward org-verbatim-re nil t)
  22520. (goto-char (match-end 2))
  22521. (backward-delete-char 1) (insert "'")
  22522. (goto-char (match-beginning 2))
  22523. (delete-char 1) (insert "`")
  22524. (goto-char (match-end 2))))
  22525. (defun org-search-todo-below (line lines level)
  22526. "Search the subtree below LINE for any TODO entries."
  22527. (let ((rest (cdr (memq line lines)))
  22528. (re org-todo-line-regexp)
  22529. line lv todo)
  22530. (catch 'exit
  22531. (while (setq line (pop rest))
  22532. (if (string-match re line)
  22533. (progn
  22534. (setq lv (- (match-end 1) (match-beginning 1))
  22535. todo (and (match-beginning 2)
  22536. (not (member (match-string 2 line)
  22537. org-done-keywords))))
  22538. ; TODO, not DONE
  22539. (if (<= lv level) (throw 'exit nil))
  22540. (if todo (throw 'exit t))))))))
  22541. (defun org-html-expand-for-ascii (line)
  22542. "Handle quoted HTML for ASCII export."
  22543. (if org-export-html-expand
  22544. (while (string-match "@<[^<>\n]*>" line)
  22545. ;; We just remove the tags for now.
  22546. (setq line (replace-match "" nil nil line))))
  22547. line)
  22548. (defun org-insert-centered (s &optional underline)
  22549. "Insert the string S centered and underline it with character UNDERLINE."
  22550. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  22551. (insert (make-string ind ?\ ) s "\n")
  22552. (if underline
  22553. (insert (make-string ind ?\ )
  22554. (make-string (string-width s) underline)
  22555. "\n"))))
  22556. (defun org-ascii-level-start (level title umax &optional lines)
  22557. "Insert a new level in ASCII export."
  22558. (let (char (n (- level umax 1)) (ind 0))
  22559. (if (> level umax)
  22560. (progn
  22561. (insert (make-string (* 2 n) ?\ )
  22562. (char-to-string (nth (% n (length org-export-ascii-bullets))
  22563. org-export-ascii-bullets))
  22564. " " title "\n")
  22565. ;; find the indentation of the next non-empty line
  22566. (catch 'stop
  22567. (while lines
  22568. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  22569. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  22570. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  22571. (pop lines)))
  22572. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  22573. (if (or (not (equal (char-before) ?\n))
  22574. (not (equal (char-before (1- (point))) ?\n)))
  22575. (insert "\n"))
  22576. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  22577. (unless org-export-with-tags
  22578. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  22579. (setq title (replace-match "" t t title))))
  22580. (if org-export-with-section-numbers
  22581. (setq title (concat (org-section-number level) " " title)))
  22582. (insert title "\n" (make-string (string-width title) char) "\n")
  22583. (setq org-ascii-current-indentation '(0 . 0)))))
  22584. (defun org-export-visible (type arg)
  22585. "Create a copy of the visible part of the current buffer, and export it.
  22586. The copy is created in a temporary buffer and removed after use.
  22587. TYPE is the final key (as a string) that also select the export command in
  22588. the `C-c C-e' export dispatcher.
  22589. As a special case, if the you type SPC at the prompt, the temporary
  22590. org-mode file will not be removed but presented to you so that you can
  22591. continue to use it. The prefix arg ARG is passed through to the exporting
  22592. command."
  22593. (interactive
  22594. (list (progn
  22595. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
  22596. (read-char-exclusive))
  22597. current-prefix-arg))
  22598. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  22599. (error "Invalid export key"))
  22600. (let* ((binding (cdr (assoc type
  22601. '((?a . org-export-as-ascii)
  22602. (?\C-a . org-export-as-ascii)
  22603. (?b . org-export-as-html-and-open)
  22604. (?\C-b . org-export-as-html-and-open)
  22605. (?h . org-export-as-html)
  22606. (?H . org-export-as-html-to-buffer)
  22607. (?R . org-export-region-as-html)
  22608. (?x . org-export-as-xoxo)))))
  22609. (keepp (equal type ?\ ))
  22610. (file buffer-file-name)
  22611. (buffer (get-buffer-create "*Org Export Visible*"))
  22612. s e)
  22613. ;; Need to hack the drawers here.
  22614. (save-excursion
  22615. (goto-char (point-min))
  22616. (while (re-search-forward org-drawer-regexp nil t)
  22617. (goto-char (match-beginning 1))
  22618. (or (org-invisible-p) (org-flag-drawer nil))))
  22619. (with-current-buffer buffer (erase-buffer))
  22620. (save-excursion
  22621. (setq s (goto-char (point-min)))
  22622. (while (not (= (point) (point-max)))
  22623. (goto-char (org-find-invisible))
  22624. (append-to-buffer buffer s (point))
  22625. (setq s (goto-char (org-find-visible))))
  22626. (org-cycle-hide-drawers 'all)
  22627. (goto-char (point-min))
  22628. (unless keepp
  22629. ;; Copy all comment lines to the end, to make sure #+ settings are
  22630. ;; still available for the second export step. Kind of a hack, but
  22631. ;; does do the trick.
  22632. (if (looking-at "#[^\r\n]*")
  22633. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  22634. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  22635. (append-to-buffer buffer (1+ (match-beginning 0))
  22636. (min (point-max) (1+ (match-end 0))))))
  22637. (set-buffer buffer)
  22638. (let ((buffer-file-name file)
  22639. (org-inhibit-startup t))
  22640. (org-mode)
  22641. (show-all)
  22642. (unless keepp (funcall binding arg))))
  22643. (if (not keepp)
  22644. (kill-buffer buffer)
  22645. (switch-to-buffer-other-window buffer)
  22646. (goto-char (point-min)))))
  22647. (defun org-find-visible ()
  22648. (let ((s (point)))
  22649. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22650. (get-char-property s 'invisible)))
  22651. s))
  22652. (defun org-find-invisible ()
  22653. (let ((s (point)))
  22654. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  22655. (not (get-char-property s 'invisible))))
  22656. s))
  22657. ;;; HTML export
  22658. (defun org-get-current-options ()
  22659. "Return a string with current options as keyword options.
  22660. Does include HTML export options as well as TODO and CATEGORY stuff."
  22661. (format
  22662. "#+TITLE: %s
  22663. #+AUTHOR: %s
  22664. #+EMAIL: %s
  22665. #+LANGUAGE: %s
  22666. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  22667. #+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
  22668. #+CATEGORY: %s
  22669. #+SEQ_TODO: %s
  22670. #+TYP_TODO: %s
  22671. #+PRIORITIES: %c %c %c
  22672. #+DRAWERS: %s
  22673. #+STARTUP: %s %s %s %s %s
  22674. #+TAGS: %s
  22675. #+ARCHIVE: %s
  22676. #+LINK: %s
  22677. "
  22678. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  22679. org-export-headline-levels
  22680. org-export-with-section-numbers
  22681. org-export-with-toc
  22682. org-export-preserve-breaks
  22683. org-export-html-expand
  22684. org-export-with-fixed-width
  22685. org-export-with-tables
  22686. org-export-with-sub-superscripts
  22687. org-export-with-special-strings
  22688. org-export-with-footnotes
  22689. org-export-with-emphasize
  22690. org-export-with-TeX-macros
  22691. org-export-with-LaTeX-fragments
  22692. org-export-skip-text-before-1st-heading
  22693. org-export-with-drawers
  22694. org-export-with-tags
  22695. (file-name-nondirectory buffer-file-name)
  22696. "TODO FEEDBACK VERIFY DONE"
  22697. "Me Jason Marie DONE"
  22698. org-highest-priority org-lowest-priority org-default-priority
  22699. (mapconcat 'identity org-drawers " ")
  22700. (cdr (assoc org-startup-folded
  22701. '((nil . "showall") (t . "overview") (content . "content"))))
  22702. (if org-odd-levels-only "odd" "oddeven")
  22703. (if org-hide-leading-stars "hidestars" "showstars")
  22704. (if org-startup-align-all-tables "align" "noalign")
  22705. (cond ((eq t org-log-done) "logdone")
  22706. ((not org-log-done) "nologging")
  22707. ((listp org-log-done)
  22708. (mapconcat (lambda (x) (concat "lognote" (symbol-name x)))
  22709. org-log-done " ")))
  22710. (or (mapconcat (lambda (x)
  22711. (cond
  22712. ((equal '(:startgroup) x) "{")
  22713. ((equal '(:endgroup) x) "}")
  22714. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  22715. (t (car x))))
  22716. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  22717. org-archive-location
  22718. "org file:~/org/%s.org"
  22719. ))
  22720. (defun org-insert-export-options-template ()
  22721. "Insert into the buffer a template with information for exporting."
  22722. (interactive)
  22723. (if (not (bolp)) (newline))
  22724. (let ((s (org-get-current-options)))
  22725. (and (string-match "#\\+CATEGORY" s)
  22726. (setq s (substring s 0 (match-beginning 0))))
  22727. (insert s)))
  22728. (defun org-toggle-fixed-width-section (arg)
  22729. "Toggle the fixed-width export.
  22730. If there is no active region, the QUOTE keyword at the current headline is
  22731. inserted or removed. When present, it causes the text between this headline
  22732. and the next to be exported as fixed-width text, and unmodified.
  22733. If there is an active region, this command adds or removes a colon as the
  22734. first character of this line. If the first character of a line is a colon,
  22735. this line is also exported in fixed-width font."
  22736. (interactive "P")
  22737. (let* ((cc 0)
  22738. (regionp (org-region-active-p))
  22739. (beg (if regionp (region-beginning) (point)))
  22740. (end (if regionp (region-end)))
  22741. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  22742. (case-fold-search nil)
  22743. (re "[ \t]*\\(:\\)")
  22744. off)
  22745. (if regionp
  22746. (save-excursion
  22747. (goto-char beg)
  22748. (setq cc (current-column))
  22749. (beginning-of-line 1)
  22750. (setq off (looking-at re))
  22751. (while (> nlines 0)
  22752. (setq nlines (1- nlines))
  22753. (beginning-of-line 1)
  22754. (cond
  22755. (arg
  22756. (move-to-column cc t)
  22757. (insert ":\n")
  22758. (forward-line -1))
  22759. ((and off (looking-at re))
  22760. (replace-match "" t t nil 1))
  22761. ((not off) (move-to-column cc t) (insert ":")))
  22762. (forward-line 1)))
  22763. (save-excursion
  22764. (org-back-to-heading)
  22765. (if (looking-at (concat outline-regexp
  22766. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  22767. (replace-match "" t t nil 1)
  22768. (if (looking-at outline-regexp)
  22769. (progn
  22770. (goto-char (match-end 0))
  22771. (insert org-quote-string " "))))))))
  22772. (defun org-export-as-html-and-open (arg)
  22773. "Export the outline as HTML and immediately open it with a browser.
  22774. If there is an active region, export only the region.
  22775. The prefix ARG specifies how many levels of the outline should become
  22776. headlines. The default is 3. Lower levels will become bulleted lists."
  22777. (interactive "P")
  22778. (org-export-as-html arg 'hidden)
  22779. (org-open-file buffer-file-name))
  22780. (defun org-export-as-html-batch ()
  22781. "Call `org-export-as-html', may be used in batch processing as
  22782. emacs --batch
  22783. --load=$HOME/lib/emacs/org.el
  22784. --eval \"(setq org-export-headline-levels 2)\"
  22785. --visit=MyFile --funcall org-export-as-html-batch"
  22786. (org-export-as-html org-export-headline-levels 'hidden))
  22787. (defun org-export-as-html-to-buffer (arg)
  22788. "Call `org-exort-as-html` with output to a temporary buffer.
  22789. No file is created. The prefix ARG is passed through to `org-export-as-html'."
  22790. (interactive "P")
  22791. (org-export-as-html arg nil nil "*Org HTML Export*")
  22792. (switch-to-buffer-other-window "*Org HTML Export*"))
  22793. (defun org-replace-region-by-html (beg end)
  22794. "Assume the current region has org-mode syntax, and convert it to HTML.
  22795. This can be used in any buffer. For example, you could write an
  22796. itemized list in org-mode syntax in an HTML buffer and then use this
  22797. command to convert it."
  22798. (interactive "r")
  22799. (let (reg html buf pop-up-frames)
  22800. (save-window-excursion
  22801. (if (org-mode-p)
  22802. (setq html (org-export-region-as-html
  22803. beg end t 'string))
  22804. (setq reg (buffer-substring beg end)
  22805. buf (get-buffer-create "*Org tmp*"))
  22806. (with-current-buffer buf
  22807. (erase-buffer)
  22808. (insert reg)
  22809. (org-mode)
  22810. (setq html (org-export-region-as-html
  22811. (point-min) (point-max) t 'string)))
  22812. (kill-buffer buf)))
  22813. (delete-region beg end)
  22814. (insert html)))
  22815. (defun org-export-region-as-html (beg end &optional body-only buffer)
  22816. "Convert region from BEG to END in org-mode buffer to HTML.
  22817. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  22818. contents, and only produce the region of converted text, useful for
  22819. cut-and-paste operations.
  22820. If BUFFER is a buffer or a string, use/create that buffer as a target
  22821. of the converted HTML. If BUFFER is the symbol `string', return the
  22822. produced HTML as a string and leave not buffer behind. For example,
  22823. a Lisp program could call this function in the following way:
  22824. (setq html (org-export-region-as-html beg end t 'string))
  22825. When called interactively, the output buffer is selected, and shown
  22826. in a window. A non-interactive call will only retunr the buffer."
  22827. (interactive "r\nP")
  22828. (when (interactive-p)
  22829. (setq buffer "*Org HTML Export*"))
  22830. (let ((transient-mark-mode t) (zmacs-regions t)
  22831. rtn)
  22832. (goto-char end)
  22833. (set-mark (point)) ;; to activate the region
  22834. (goto-char beg)
  22835. (setq rtn (org-export-as-html
  22836. nil nil nil
  22837. buffer body-only))
  22838. (if (fboundp 'deactivate-mark) (deactivate-mark))
  22839. (if (and (interactive-p) (bufferp rtn))
  22840. (switch-to-buffer-other-window rtn)
  22841. rtn)))
  22842. (defvar html-table-tag nil) ; dynamically scoped into this.
  22843. (defun org-export-as-html (arg &optional hidden ext-plist
  22844. to-buffer body-only)
  22845. "Export the outline as a pretty HTML file.
  22846. If there is an active region, export only the region. The prefix
  22847. ARG specifies how many levels of the outline should become
  22848. headlines. The default is 3. Lower levels will become bulleted
  22849. lists. When HIDDEN is non-nil, don't display the HTML buffer.
  22850. EXT-PLIST is a property list with external parameters overriding
  22851. org-mode's default settings, but still inferior to file-local
  22852. settings. When TO-BUFFER is non-nil, create a buffer with that
  22853. name and export to that buffer. If TO-BUFFER is the symbol `string',
  22854. don't leave any buffer behind but just return the resulting HTML as
  22855. a string. When BODY-ONLY is set, don't produce the file header and footer,
  22856. simply return the content of <body>...</body>, without even
  22857. the body tags themselves."
  22858. (interactive "P")
  22859. ;; Make sure we have a file name when we need it.
  22860. (when (and (not (or to-buffer body-only))
  22861. (not buffer-file-name))
  22862. (if (buffer-base-buffer)
  22863. (org-set-local 'buffer-file-name
  22864. (with-current-buffer (buffer-base-buffer)
  22865. buffer-file-name))
  22866. (error "Need a file name to be able to export.")))
  22867. (message "Exporting...")
  22868. (setq-default org-todo-line-regexp org-todo-line-regexp)
  22869. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  22870. (setq-default org-done-keywords org-done-keywords)
  22871. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  22872. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  22873. ext-plist
  22874. (org-infile-export-plist)))
  22875. (style (plist-get opt-plist :style))
  22876. (link-validate (plist-get opt-plist :link-validation-function))
  22877. valid thetoc have-headings first-heading-pos
  22878. (odd org-odd-levels-only)
  22879. (region-p (org-region-active-p))
  22880. (subtree-p
  22881. (when region-p
  22882. (save-excursion
  22883. (goto-char (region-beginning))
  22884. (and (org-at-heading-p)
  22885. (>= (org-end-of-subtree t t) (region-end))))))
  22886. ;; The following two are dynamically scoped into other
  22887. ;; routines below.
  22888. (org-current-export-dir (org-export-directory :html opt-plist))
  22889. (org-current-export-file buffer-file-name)
  22890. (level 0) (line "") (origline "") txt todo
  22891. (umax nil)
  22892. (umax-toc nil)
  22893. (filename (if to-buffer nil
  22894. (expand-file-name
  22895. (concat
  22896. (file-name-sans-extension
  22897. (or (and subtree-p
  22898. (org-entry-get (region-beginning)
  22899. "EXPORT_FILE_NAME" t))
  22900. (file-name-nondirectory buffer-file-name)))
  22901. "." org-export-html-extension)
  22902. (file-name-as-directory
  22903. (org-export-directory :html opt-plist)))))
  22904. (current-dir (if buffer-file-name
  22905. (file-name-directory buffer-file-name)
  22906. default-directory))
  22907. (buffer (if to-buffer
  22908. (cond
  22909. ((eq to-buffer 'string) (get-buffer-create "*Org HTML Export*"))
  22910. (t (get-buffer-create to-buffer)))
  22911. (find-file-noselect filename)))
  22912. (org-levels-open (make-vector org-level-max nil))
  22913. (date (plist-get opt-plist :date))
  22914. (author (plist-get opt-plist :author))
  22915. (title (or (and subtree-p (org-export-get-title-from-subtree))
  22916. (plist-get opt-plist :title)
  22917. (and (not
  22918. (plist-get opt-plist :skip-before-1st-heading))
  22919. (org-export-grab-title-from-buffer))
  22920. (and buffer-file-name
  22921. (file-name-sans-extension
  22922. (file-name-nondirectory buffer-file-name)))
  22923. "UNTITLED"))
  22924. (html-table-tag (plist-get opt-plist :html-table-tag))
  22925. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  22926. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  22927. (inquote nil)
  22928. (infixed nil)
  22929. (in-local-list nil)
  22930. (local-list-num nil)
  22931. (local-list-indent nil)
  22932. (llt org-plain-list-ordered-item-terminator)
  22933. (email (plist-get opt-plist :email))
  22934. (language (plist-get opt-plist :language))
  22935. (lang-words nil)
  22936. (target-alist nil) tg
  22937. (head-count 0) cnt
  22938. (start 0)
  22939. (coding-system (and (boundp 'buffer-file-coding-system)
  22940. buffer-file-coding-system))
  22941. (coding-system-for-write (or org-export-html-coding-system
  22942. coding-system))
  22943. (save-buffer-coding-system (or org-export-html-coding-system
  22944. coding-system))
  22945. (charset (and coding-system-for-write
  22946. (fboundp 'coding-system-get)
  22947. (coding-system-get coding-system-for-write
  22948. 'mime-charset)))
  22949. (region
  22950. (buffer-substring
  22951. (if region-p (region-beginning) (point-min))
  22952. (if region-p (region-end) (point-max))))
  22953. (lines
  22954. (org-split-string
  22955. (org-cleaned-string-for-export
  22956. region
  22957. :emph-multiline t
  22958. :for-html t
  22959. :skip-before-1st-heading
  22960. (plist-get opt-plist :skip-before-1st-heading)
  22961. :drawers (plist-get opt-plist :drawers)
  22962. :archived-trees
  22963. (plist-get opt-plist :archived-trees)
  22964. :add-text
  22965. (plist-get opt-plist :text)
  22966. :LaTeX-fragments
  22967. (plist-get opt-plist :LaTeX-fragments))
  22968. "[\r\n]"))
  22969. table-open type
  22970. table-buffer table-orig-buffer
  22971. ind start-is-num starter didclose
  22972. rpl path desc descp desc1 desc2 link
  22973. )
  22974. (let ((inhibit-read-only t))
  22975. (org-unmodified
  22976. (remove-text-properties (point-min) (point-max)
  22977. '(:org-license-to-kill t))))
  22978. (message "Exporting...")
  22979. (setq org-min-level (org-get-min-level lines))
  22980. (setq org-last-level org-min-level)
  22981. (org-init-section-numbers)
  22982. (cond
  22983. ((and date (string-match "%" date))
  22984. (setq date (format-time-string date (current-time))))
  22985. (date)
  22986. (t (setq date (format-time-string "%Y/%m/%d %X" (current-time)))))
  22987. ;; Get the language-dependent settings
  22988. (setq lang-words (or (assoc language org-export-language-setup)
  22989. (assoc "en" org-export-language-setup)))
  22990. ;; Switch to the output buffer
  22991. (set-buffer buffer)
  22992. (let ((inhibit-read-only t)) (erase-buffer))
  22993. (fundamental-mode)
  22994. (and (fboundp 'set-buffer-file-coding-system)
  22995. (set-buffer-file-coding-system coding-system-for-write))
  22996. (let ((case-fold-search nil)
  22997. (org-odd-levels-only odd))
  22998. ;; create local variables for all options, to make sure all called
  22999. ;; functions get the correct information
  23000. (mapc (lambda (x)
  23001. (set (make-local-variable (cdr x))
  23002. (plist-get opt-plist (car x))))
  23003. org-export-plist-vars)
  23004. (setq umax (if arg (prefix-numeric-value arg)
  23005. org-export-headline-levels))
  23006. (setq umax-toc (if (integerp org-export-with-toc)
  23007. (min org-export-with-toc umax)
  23008. umax))
  23009. (unless body-only
  23010. ;; File header
  23011. (insert (format
  23012. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  23013. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  23014. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  23015. lang=\"%s\" xml:lang=\"%s\">
  23016. <head>
  23017. <title>%s</title>
  23018. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  23019. <meta name=\"generator\" content=\"Org-mode\"/>
  23020. <meta name=\"generated\" content=\"%s\"/>
  23021. <meta name=\"author\" content=\"%s\"/>
  23022. %s
  23023. </head><body>
  23024. "
  23025. language language (org-html-expand title)
  23026. (or charset "iso-8859-1") date author style))
  23027. (insert (or (plist-get opt-plist :preamble) ""))
  23028. (when (plist-get opt-plist :auto-preamble)
  23029. (if title (insert (format org-export-html-title-format
  23030. (org-html-expand title))))))
  23031. (if (and org-export-with-toc (not body-only))
  23032. (progn
  23033. (push (format "<h%d>%s</h%d>\n"
  23034. org-export-html-toplevel-hlevel
  23035. (nth 3 lang-words)
  23036. org-export-html-toplevel-hlevel)
  23037. thetoc)
  23038. (push "<ul>\n<li>" thetoc)
  23039. (setq lines
  23040. (mapcar '(lambda (line)
  23041. (if (string-match org-todo-line-regexp line)
  23042. ;; This is a headline
  23043. (progn
  23044. (setq have-headings t)
  23045. (setq level (- (match-end 1) (match-beginning 1))
  23046. level (org-tr-level level)
  23047. txt (save-match-data
  23048. (org-html-expand
  23049. (org-export-cleanup-toc-line
  23050. (match-string 3 line))))
  23051. todo
  23052. (or (and org-export-mark-todo-in-toc
  23053. (match-beginning 2)
  23054. (not (member (match-string 2 line)
  23055. org-done-keywords)))
  23056. ; TODO, not DONE
  23057. (and org-export-mark-todo-in-toc
  23058. (= level umax-toc)
  23059. (org-search-todo-below
  23060. line lines level))))
  23061. (if (string-match
  23062. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  23063. (setq txt (replace-match "&nbsp;&nbsp;&nbsp;<span class=\"tag\"> \\1</span>" t nil txt)))
  23064. (if (string-match quote-re0 txt)
  23065. (setq txt (replace-match "" t t txt)))
  23066. (if org-export-with-section-numbers
  23067. (setq txt (concat (org-section-number level)
  23068. " " txt)))
  23069. (if (<= level (max umax umax-toc))
  23070. (setq head-count (+ head-count 1)))
  23071. (if (<= level umax-toc)
  23072. (progn
  23073. (if (> level org-last-level)
  23074. (progn
  23075. (setq cnt (- level org-last-level))
  23076. (while (>= (setq cnt (1- cnt)) 0)
  23077. (push "\n<ul>\n<li>" thetoc))
  23078. (push "\n" thetoc)))
  23079. (if (< level org-last-level)
  23080. (progn
  23081. (setq cnt (- org-last-level level))
  23082. (while (>= (setq cnt (1- cnt)) 0)
  23083. (push "</li>\n</ul>" thetoc))
  23084. (push "\n" thetoc)))
  23085. ;; Check for targets
  23086. (while (string-match org-target-regexp line)
  23087. (setq tg (match-string 1 line)
  23088. line (replace-match
  23089. (concat "@<span class=\"target\">" tg "@</span> ")
  23090. t t line))
  23091. (push (cons (org-solidify-link-text tg)
  23092. (format "sec-%d" head-count))
  23093. target-alist))
  23094. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  23095. (setq txt (replace-match "" t t txt)))
  23096. (push
  23097. (format
  23098. (if todo
  23099. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  23100. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  23101. head-count txt) thetoc)
  23102. (setq org-last-level level))
  23103. )))
  23104. line)
  23105. lines))
  23106. (while (> org-last-level (1- org-min-level))
  23107. (setq org-last-level (1- org-last-level))
  23108. (push "</li>\n</ul>\n" thetoc))
  23109. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  23110. (setq head-count 0)
  23111. (org-init-section-numbers)
  23112. (while (setq line (pop lines) origline line)
  23113. (catch 'nextline
  23114. ;; end of quote section?
  23115. (when (and inquote (string-match "^\\*+ " line))
  23116. (insert "</pre>\n")
  23117. (setq inquote nil))
  23118. ;; inside a quote section?
  23119. (when inquote
  23120. (insert (org-html-protect line) "\n")
  23121. (throw 'nextline nil))
  23122. ;; verbatim lines
  23123. (when (and org-export-with-fixed-width
  23124. (string-match "^[ \t]*:\\(.*\\)" line))
  23125. (when (not infixed)
  23126. (setq infixed t)
  23127. (insert "<pre>\n"))
  23128. (insert (org-html-protect (match-string 1 line)) "\n")
  23129. (when (and lines
  23130. (not (string-match "^[ \t]*\\(:.*\\)"
  23131. (car lines))))
  23132. (setq infixed nil)
  23133. (insert "</pre>\n"))
  23134. (throw 'nextline nil))
  23135. ;; Protected HTML
  23136. (when (get-text-property 0 'org-protected line)
  23137. (let (par)
  23138. (when (re-search-backward
  23139. "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t)
  23140. (setq par (match-string 1))
  23141. (replace-match "\\2\n"))
  23142. (insert line "\n")
  23143. (while (and lines
  23144. (or (= (length (car lines)) 0)
  23145. (get-text-property 0 'org-protected (car lines))))
  23146. (insert (pop lines) "\n"))
  23147. (and par (insert "<p>\n")))
  23148. (throw 'nextline nil))
  23149. ;; Horizontal line
  23150. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  23151. (insert "\n<hr/>\n")
  23152. (throw 'nextline nil))
  23153. ;; make targets to anchors
  23154. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  23155. (cond
  23156. ((match-end 2)
  23157. (setq line (replace-match
  23158. (concat "@<a name=\""
  23159. (org-solidify-link-text (match-string 1 line))
  23160. "\">\\nbsp@</a>")
  23161. t t line)))
  23162. ((and org-export-with-toc (equal (string-to-char line) ?*))
  23163. (setq line (replace-match
  23164. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  23165. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  23166. t t line)))
  23167. (t
  23168. (setq line (replace-match
  23169. (concat "@<a name=\""
  23170. (org-solidify-link-text (match-string 1 line))
  23171. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  23172. t t line)))))
  23173. (setq line (org-html-handle-time-stamps line))
  23174. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  23175. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  23176. ;; Also handle sub_superscripts and checkboxes
  23177. (or (string-match org-table-hline-regexp line)
  23178. (setq line (org-html-expand line)))
  23179. ;; Format the links
  23180. (setq start 0)
  23181. (while (string-match org-bracket-link-analytic-regexp line start)
  23182. (setq start (match-beginning 0))
  23183. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  23184. (setq path (match-string 3 line))
  23185. (setq desc1 (if (match-end 5) (match-string 5 line))
  23186. desc2 (if (match-end 2) (concat type ":" path) path)
  23187. descp (and desc1 (not (equal desc1 desc2)))
  23188. desc (or desc1 desc2))
  23189. ;; Make an image out of the description if that is so wanted
  23190. (when (and descp (org-file-image-p desc))
  23191. (save-match-data
  23192. (if (string-match "^file:" desc)
  23193. (setq desc (substring desc (match-end 0)))))
  23194. (setq desc (concat "<img src=\"" desc "\"/>")))
  23195. ;; FIXME: do we need to unescape here somewhere?
  23196. (cond
  23197. ((equal type "internal")
  23198. (setq rpl
  23199. (concat
  23200. "<a href=\"#"
  23201. (org-solidify-link-text
  23202. (save-match-data (org-link-unescape path)) target-alist)
  23203. "\">" desc "</a>")))
  23204. ((member type '("http" "https"))
  23205. ;; standard URL, just check if we need to inline an image
  23206. (if (and (or (eq t org-export-html-inline-images)
  23207. (and org-export-html-inline-images (not descp)))
  23208. (org-file-image-p path))
  23209. (setq rpl (concat "<img src=\"" type ":" path "\"/>"))
  23210. (setq link (concat type ":" path))
  23211. (setq rpl (concat "<a href=\"" link "\">" desc "</a>"))))
  23212. ((member type '("ftp" "mailto" "news"))
  23213. ;; standard URL
  23214. (setq link (concat type ":" path))
  23215. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  23216. ((string= type "file")
  23217. ;; FILE link
  23218. (let* ((filename path)
  23219. (abs-p (file-name-absolute-p filename))
  23220. thefile file-is-image-p search)
  23221. (save-match-data
  23222. (if (string-match "::\\(.*\\)" filename)
  23223. (setq search (match-string 1 filename)
  23224. filename (replace-match "" t nil filename)))
  23225. (setq valid
  23226. (if (functionp link-validate)
  23227. (funcall link-validate filename current-dir)
  23228. t))
  23229. (setq file-is-image-p (org-file-image-p filename))
  23230. (setq thefile (if abs-p (expand-file-name filename) filename))
  23231. (when (and org-export-html-link-org-files-as-html
  23232. (string-match "\\.org$" thefile))
  23233. (setq thefile (concat (substring thefile 0
  23234. (match-beginning 0))
  23235. "." org-export-html-extension))
  23236. (if (and search
  23237. ;; make sure this is can be used as target search
  23238. (not (string-match "^[0-9]*$" search))
  23239. (not (string-match "^\\*" search))
  23240. (not (string-match "^/.*/$" search)))
  23241. (setq thefile (concat thefile "#"
  23242. (org-solidify-link-text
  23243. (org-link-unescape search)))))
  23244. (when (string-match "^file:" desc)
  23245. (setq desc (replace-match "" t t desc))
  23246. (if (string-match "\\.org$" desc)
  23247. (setq desc (replace-match "" t t desc))))))
  23248. (setq rpl (if (and file-is-image-p
  23249. (or (eq t org-export-html-inline-images)
  23250. (and org-export-html-inline-images
  23251. (not descp))))
  23252. (concat "<img src=\"" thefile "\"/>")
  23253. (concat "<a href=\"" thefile "\">" desc "</a>")))
  23254. (if (not valid) (setq rpl desc))))
  23255. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  23256. (setq rpl (concat "<i>&lt;" type ":"
  23257. (save-match-data (org-link-unescape path))
  23258. "&gt;</i>"))))
  23259. (setq line (replace-match rpl t t line)
  23260. start (+ start (length rpl))))
  23261. ;; TODO items
  23262. (if (and (string-match org-todo-line-regexp line)
  23263. (match-beginning 2))
  23264. (setq line
  23265. (concat (substring line 0 (match-beginning 2))
  23266. "<span class=\""
  23267. (if (member (match-string 2 line)
  23268. org-done-keywords)
  23269. "done" "todo")
  23270. "\">" (match-string 2 line)
  23271. "</span>" (substring line (match-end 2)))))
  23272. ;; Does this contain a reference to a footnote?
  23273. (when org-export-with-footnotes
  23274. (setq start 0)
  23275. (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start)
  23276. (if (get-text-property (match-beginning 2) 'org-protected line)
  23277. (setq start (match-end 2))
  23278. (let ((n (match-string 2 line)))
  23279. (setq line
  23280. (replace-match
  23281. (format
  23282. "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
  23283. (match-string 1 line) n n n)
  23284. t t line))))))
  23285. (cond
  23286. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  23287. ;; This is a headline
  23288. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  23289. txt (match-string 2 line))
  23290. (if (string-match quote-re0 txt)
  23291. (setq txt (replace-match "" t t txt)))
  23292. (if (<= level (max umax umax-toc))
  23293. (setq head-count (+ head-count 1)))
  23294. (when in-local-list
  23295. ;; Close any local lists before inserting a new header line
  23296. (while local-list-num
  23297. (org-close-li)
  23298. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23299. (pop local-list-num))
  23300. (setq local-list-indent nil
  23301. in-local-list nil))
  23302. (setq first-heading-pos (or first-heading-pos (point)))
  23303. (org-html-level-start level txt umax
  23304. (and org-export-with-toc (<= level umax))
  23305. head-count)
  23306. ;; QUOTES
  23307. (when (string-match quote-re line)
  23308. (insert "<pre>")
  23309. (setq inquote t)))
  23310. ((and org-export-with-tables
  23311. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  23312. (if (not table-open)
  23313. ;; New table starts
  23314. (setq table-open t table-buffer nil table-orig-buffer nil))
  23315. ;; Accumulate lines
  23316. (setq table-buffer (cons line table-buffer)
  23317. table-orig-buffer (cons origline table-orig-buffer))
  23318. (when (or (not lines)
  23319. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  23320. (car lines))))
  23321. (setq table-open nil
  23322. table-buffer (nreverse table-buffer)
  23323. table-orig-buffer (nreverse table-orig-buffer))
  23324. (org-close-par-maybe)
  23325. (insert (org-format-table-html table-buffer table-orig-buffer))))
  23326. (t
  23327. ;; Normal lines
  23328. (when (string-match
  23329. (cond
  23330. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23331. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23332. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  23333. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  23334. line)
  23335. (setq ind (org-get-string-indentation line)
  23336. start-is-num (match-beginning 4)
  23337. starter (if (match-beginning 2)
  23338. (substring (match-string 2 line) 0 -1))
  23339. line (substring line (match-beginning 5)))
  23340. (unless (string-match "[^ \t]" line)
  23341. ;; empty line. Pretend indentation is large.
  23342. (setq ind (if org-empty-line-terminates-plain-lists
  23343. 0
  23344. (1+ (or (car local-list-indent) 1)))))
  23345. (setq didclose nil)
  23346. (while (and in-local-list
  23347. (or (and (= ind (car local-list-indent))
  23348. (not starter))
  23349. (< ind (car local-list-indent))))
  23350. (setq didclose t)
  23351. (org-close-li)
  23352. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  23353. (pop local-list-num) (pop local-list-indent)
  23354. (setq in-local-list local-list-indent))
  23355. (cond
  23356. ((and starter
  23357. (or (not in-local-list)
  23358. (> ind (car local-list-indent))))
  23359. ;; Start new (level of) list
  23360. (org-close-par-maybe)
  23361. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  23362. (push start-is-num local-list-num)
  23363. (push ind local-list-indent)
  23364. (setq in-local-list t))
  23365. (starter
  23366. ;; continue current list
  23367. (org-close-li)
  23368. (insert "<li>\n"))
  23369. (didclose
  23370. ;; we did close a list, normal text follows: need <p>
  23371. (org-open-par)))
  23372. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  23373. (setq line
  23374. (replace-match
  23375. (if (equal (match-string 1 line) "X")
  23376. "<b>[X]</b>"
  23377. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  23378. t t line))))
  23379. ;; Empty lines start a new paragraph. If hand-formatted lists
  23380. ;; are not fully interpreted, lines starting with "-", "+", "*"
  23381. ;; also start a new paragraph.
  23382. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  23383. ;; Is this the start of a footnote?
  23384. (when org-export-with-footnotes
  23385. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  23386. (org-close-par-maybe)
  23387. (let ((n (match-string 1 line)))
  23388. (setq line (replace-match
  23389. (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n) t t line)))))
  23390. ;; Check if the line break needs to be conserved
  23391. (cond
  23392. ((string-match "\\\\\\\\[ \t]*$" line)
  23393. (setq line (replace-match "<br/>" t t line)))
  23394. (org-export-preserve-breaks
  23395. (setq line (concat line "<br/>"))))
  23396. (insert line "\n")))))
  23397. ;; Properly close all local lists and other lists
  23398. (when inquote (insert "</pre>\n"))
  23399. (when in-local-list
  23400. ;; Close any local lists before inserting a new header line
  23401. (while local-list-num
  23402. (org-close-li)
  23403. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  23404. (pop local-list-num))
  23405. (setq local-list-indent nil
  23406. in-local-list nil))
  23407. (org-html-level-start 1 nil umax
  23408. (and org-export-with-toc (<= level umax))
  23409. head-count)
  23410. (unless body-only
  23411. (when (plist-get opt-plist :auto-postamble)
  23412. (insert "<div id=\"postamble\">")
  23413. (when (and org-export-author-info author)
  23414. (insert "<p class=\"author\"> "
  23415. (nth 1 lang-words) ": " author "\n")
  23416. (when email
  23417. (if (listp (split-string email ",+ *"))
  23418. (mapc (lambda(e)
  23419. (insert "<a href=\"mailto:" e "\">&lt;"
  23420. e "&gt;</a>\n"))
  23421. (split-string email ",+ *"))
  23422. (insert "<a href=\"mailto:" email "\">&lt;"
  23423. email "&gt;</a>\n")))
  23424. (insert "</p>\n"))
  23425. (when (and date org-export-time-stamp-file)
  23426. (insert "<p class=\"date\"> "
  23427. (nth 2 lang-words) ": "
  23428. date "</p>\n"))
  23429. (insert "</div>"))
  23430. (if org-export-html-with-timestamp
  23431. (insert org-export-html-html-helper-timestamp))
  23432. (insert (or (plist-get opt-plist :postamble) ""))
  23433. (insert "</body>\n</html>\n"))
  23434. (normal-mode)
  23435. (if (eq major-mode default-major-mode) (html-mode))
  23436. ;; insert the table of contents
  23437. (goto-char (point-min))
  23438. (when thetoc
  23439. (if (or (re-search-forward
  23440. "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t)
  23441. (re-search-forward
  23442. "\\[TABLE-OF-CONTENTS\\]" nil t))
  23443. (progn
  23444. (goto-char (match-beginning 0))
  23445. (replace-match ""))
  23446. (goto-char first-heading-pos)
  23447. (when (looking-at "\\s-*</p>")
  23448. (goto-char (match-end 0))
  23449. (insert "\n")))
  23450. (insert "<div id=\"table-of-contents\">\n")
  23451. (mapc 'insert thetoc)
  23452. (insert "</div>\n"))
  23453. ;; remove empty paragraphs and lists
  23454. (goto-char (point-min))
  23455. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  23456. (replace-match ""))
  23457. (goto-char (point-min))
  23458. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  23459. (replace-match ""))
  23460. ;; Convert whitespace place holders
  23461. (goto-char (point-min))
  23462. (let (beg end n)
  23463. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  23464. (setq n (get-text-property beg 'org-whitespace)
  23465. end (next-single-property-change beg 'org-whitespace))
  23466. (goto-char beg)
  23467. (delete-region beg end)
  23468. (insert (format "<span style=\"visibility:hidden;\">%s</span>"
  23469. (make-string n ?x)))))
  23470. (or to-buffer (save-buffer))
  23471. (goto-char (point-min))
  23472. (message "Exporting... done")
  23473. (if (eq to-buffer 'string)
  23474. (prog1 (buffer-substring (point-min) (point-max))
  23475. (kill-buffer (current-buffer)))
  23476. (current-buffer)))))
  23477. (defvar org-table-colgroup-info nil)
  23478. (defun org-format-table-ascii (lines)
  23479. "Format a table for ascii export."
  23480. (if (stringp lines)
  23481. (setq lines (org-split-string lines "\n")))
  23482. (if (not (string-match "^[ \t]*|" (car lines)))
  23483. ;; Table made by table.el - test for spanning
  23484. lines
  23485. ;; A normal org table
  23486. ;; Get rid of hlines at beginning and end
  23487. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23488. (setq lines (nreverse lines))
  23489. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23490. (setq lines (nreverse lines))
  23491. (when org-export-table-remove-special-lines
  23492. ;; Check if the table has a marking column. If yes remove the
  23493. ;; column and the special lines
  23494. (setq lines (org-table-clean-before-export lines)))
  23495. ;; Get rid of the vertical lines except for grouping
  23496. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  23497. rtn line vl1 start)
  23498. (while (setq line (pop lines))
  23499. (if (string-match org-table-hline-regexp line)
  23500. (and (string-match "|\\(.*\\)|" line)
  23501. (setq line (replace-match " \\1" t nil line)))
  23502. (setq start 0 vl1 vl)
  23503. (while (string-match "|" line start)
  23504. (setq start (match-end 0))
  23505. (or (pop vl1) (setq line (replace-match " " t t line)))))
  23506. (push line rtn))
  23507. (nreverse rtn))))
  23508. (defun org-colgroup-info-to-vline-list (info)
  23509. (let (vl new last)
  23510. (while info
  23511. (setq last new new (pop info))
  23512. (if (or (memq last '(:end :startend))
  23513. (memq new '(:start :startend)))
  23514. (push t vl)
  23515. (push nil vl)))
  23516. (setq vl (nreverse vl))
  23517. (and vl (setcar vl nil))
  23518. vl))
  23519. (defun org-format-table-html (lines olines)
  23520. "Find out which HTML converter to use and return the HTML code."
  23521. (if (stringp lines)
  23522. (setq lines (org-split-string lines "\n")))
  23523. (if (string-match "^[ \t]*|" (car lines))
  23524. ;; A normal org table
  23525. (org-format-org-table-html lines)
  23526. ;; Table made by table.el - test for spanning
  23527. (let* ((hlines (delq nil (mapcar
  23528. (lambda (x)
  23529. (if (string-match "^[ \t]*\\+-" x) x
  23530. nil))
  23531. lines)))
  23532. (first (car hlines))
  23533. (ll (and (string-match "\\S-+" first)
  23534. (match-string 0 first)))
  23535. (re (concat "^[ \t]*" (regexp-quote ll)))
  23536. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  23537. hlines))))
  23538. (if (and (not spanning)
  23539. (not org-export-prefer-native-exporter-for-tables))
  23540. ;; We can use my own converter with HTML conversions
  23541. (org-format-table-table-html lines)
  23542. ;; Need to use the code generator in table.el, with the original text.
  23543. (org-format-table-table-html-using-table-generate-source olines)))))
  23544. (defun org-format-org-table-html (lines &optional splice)
  23545. "Format a table into HTML."
  23546. ;; Get rid of hlines at beginning and end
  23547. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23548. (setq lines (nreverse lines))
  23549. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  23550. (setq lines (nreverse lines))
  23551. (when org-export-table-remove-special-lines
  23552. ;; Check if the table has a marking column. If yes remove the
  23553. ;; column and the special lines
  23554. (setq lines (org-table-clean-before-export lines)))
  23555. (let ((head (and org-export-highlight-first-table-line
  23556. (delq nil (mapcar
  23557. (lambda (x) (string-match "^[ \t]*|-" x))
  23558. (cdr lines)))))
  23559. (nlines 0) fnum i
  23560. tbopen line fields html gr colgropen)
  23561. (if splice (setq head nil))
  23562. (unless splice (push (if head "<thead>" "<tbody>") html))
  23563. (setq tbopen t)
  23564. (while (setq line (pop lines))
  23565. (catch 'next-line
  23566. (if (string-match "^[ \t]*|-" line)
  23567. (progn
  23568. (unless splice
  23569. (push (if head "</thead>" "</tbody>") html)
  23570. (if lines (push "<tbody>" html) (setq tbopen nil)))
  23571. (setq head nil) ;; head ends here, first time around
  23572. ;; ignore this line
  23573. (throw 'next-line t)))
  23574. ;; Break the line into fields
  23575. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23576. (unless fnum (setq fnum (make-vector (length fields) 0)))
  23577. (setq nlines (1+ nlines) i -1)
  23578. (push (concat "<tr>"
  23579. (mapconcat
  23580. (lambda (x)
  23581. (setq i (1+ i))
  23582. (if (and (< i nlines)
  23583. (string-match org-table-number-regexp x))
  23584. (incf (aref fnum i)))
  23585. (if head
  23586. (concat (car org-export-table-header-tags) x
  23587. (cdr org-export-table-header-tags))
  23588. (concat (car org-export-table-data-tags) x
  23589. (cdr org-export-table-data-tags))))
  23590. fields "")
  23591. "</tr>")
  23592. html)))
  23593. (unless splice (if tbopen (push "</tbody>" html)))
  23594. (unless splice (push "</table>\n" html))
  23595. (setq html (nreverse html))
  23596. (unless splice
  23597. ;; Put in col tags with the alignment (unfortuntely often ignored...)
  23598. (push (mapconcat
  23599. (lambda (x)
  23600. (setq gr (pop org-table-colgroup-info))
  23601. (format "%s<col align=\"%s\"></col>%s"
  23602. (if (memq gr '(:start :startend))
  23603. (prog1
  23604. (if colgropen "</colgroup>\n<colgroup>" "<colgroup>")
  23605. (setq colgropen t))
  23606. "")
  23607. (if (> (/ (float x) nlines) org-table-number-fraction)
  23608. "right" "left")
  23609. (if (memq gr '(:end :startend))
  23610. (progn (setq colgropen nil) "</colgroup>")
  23611. "")))
  23612. fnum "")
  23613. html)
  23614. (if colgropen (setq html (cons (car html) (cons "</colgroup>" (cdr html)))))
  23615. (push html-table-tag html))
  23616. (concat (mapconcat 'identity html "\n") "\n")))
  23617. (defun org-table-clean-before-export (lines)
  23618. "Check if the table has a marking column.
  23619. If yes remove the column and the special lines."
  23620. (setq org-table-colgroup-info nil)
  23621. (if (memq nil
  23622. (mapcar
  23623. (lambda (x) (or (string-match "^[ \t]*|-" x)
  23624. (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x)))
  23625. lines))
  23626. (progn
  23627. (setq org-table-clean-did-remove-column nil)
  23628. (delq nil
  23629. (mapcar
  23630. (lambda (x)
  23631. (cond
  23632. ((string-match "^[ \t]*| */ *|" x)
  23633. (setq org-table-colgroup-info
  23634. (mapcar (lambda (x)
  23635. (cond ((member x '("<" "&lt;")) :start)
  23636. ((member x '(">" "&gt;")) :end)
  23637. ((member x '("<>" "&lt;&gt;")) :startend)
  23638. (t nil)))
  23639. (org-split-string x "[ \t]*|[ \t]*")))
  23640. nil)
  23641. (t x)))
  23642. lines)))
  23643. (setq org-table-clean-did-remove-column t)
  23644. (delq nil
  23645. (mapcar
  23646. (lambda (x)
  23647. (cond
  23648. ((string-match "^[ \t]*| */ *|" x)
  23649. (setq org-table-colgroup-info
  23650. (mapcar (lambda (x)
  23651. (cond ((member x '("<" "&lt;")) :start)
  23652. ((member x '(">" "&gt;")) :end)
  23653. ((member x '("<>" "&lt;&gt;")) :startend)
  23654. (t nil)))
  23655. (cdr (org-split-string x "[ \t]*|[ \t]*"))))
  23656. nil)
  23657. ((string-match "^[ \t]*| *[!_^/] *|" x)
  23658. nil) ; ignore this line
  23659. ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
  23660. (string-match "^\\([ \t]*\\)|[^|]*|" x))
  23661. ;; remove the first column
  23662. (replace-match "\\1|" t nil x))))
  23663. lines))))
  23664. (defun org-format-table-table-html (lines)
  23665. "Format a table generated by table.el into HTML.
  23666. This conversion does *not* use `table-generate-source' from table.el.
  23667. This has the advantage that Org-mode's HTML conversions can be used.
  23668. But it has the disadvantage, that no cell- or row-spanning is allowed."
  23669. (let (line field-buffer
  23670. (head org-export-highlight-first-table-line)
  23671. fields html empty)
  23672. (setq html (concat html-table-tag "\n"))
  23673. (while (setq line (pop lines))
  23674. (setq empty "&nbsp;")
  23675. (catch 'next-line
  23676. (if (string-match "^[ \t]*\\+-" line)
  23677. (progn
  23678. (if field-buffer
  23679. (progn
  23680. (setq
  23681. html
  23682. (concat
  23683. html
  23684. "<tr>"
  23685. (mapconcat
  23686. (lambda (x)
  23687. (if (equal x "") (setq x empty))
  23688. (if head
  23689. (concat (car org-export-table-header-tags) x
  23690. (cdr org-export-table-header-tags))
  23691. (concat (car org-export-table-data-tags) x
  23692. (cdr org-export-table-data-tags))))
  23693. field-buffer "\n")
  23694. "</tr>\n"))
  23695. (setq head nil)
  23696. (setq field-buffer nil)))
  23697. ;; Ignore this line
  23698. (throw 'next-line t)))
  23699. ;; Break the line into fields and store the fields
  23700. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  23701. (if field-buffer
  23702. (setq field-buffer (mapcar
  23703. (lambda (x)
  23704. (concat x "<br/>" (pop fields)))
  23705. field-buffer))
  23706. (setq field-buffer fields))))
  23707. (setq html (concat html "</table>\n"))
  23708. html))
  23709. (defun org-format-table-table-html-using-table-generate-source (lines)
  23710. "Format a table into html, using `table-generate-source' from table.el.
  23711. This has the advantage that cell- or row-spanning is allowed.
  23712. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  23713. (require 'table)
  23714. (with-current-buffer (get-buffer-create " org-tmp1 ")
  23715. (erase-buffer)
  23716. (insert (mapconcat 'identity lines "\n"))
  23717. (goto-char (point-min))
  23718. (if (not (re-search-forward "|[^+]" nil t))
  23719. (error "Error processing table"))
  23720. (table-recognize-table)
  23721. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  23722. (table-generate-source 'html " org-tmp2 ")
  23723. (set-buffer " org-tmp2 ")
  23724. (buffer-substring (point-min) (point-max))))
  23725. (defun org-html-handle-time-stamps (s)
  23726. "Format time stamps in string S, or remove them."
  23727. (catch 'exit
  23728. (let (r b)
  23729. (while (string-match org-maybe-keyword-time-regexp s)
  23730. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  23731. ;; never export CLOCK
  23732. (throw 'exit ""))
  23733. (or b (setq b (substring s 0 (match-beginning 0))))
  23734. (if (not org-export-with-timestamps)
  23735. (setq r (concat r (substring s 0 (match-beginning 0)))
  23736. s (substring s (match-end 0)))
  23737. (setq r (concat
  23738. r (substring s 0 (match-beginning 0))
  23739. (if (match-end 1)
  23740. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  23741. (match-string 1 s)))
  23742. (format " @<span class=\"timestamp\">%s@</span>"
  23743. (substring
  23744. (org-translate-time (match-string 3 s)) 1 -1)))
  23745. s (substring s (match-end 0)))))
  23746. ;; Line break if line started and ended with time stamp stuff
  23747. (if (not r)
  23748. s
  23749. (setq r (concat r s))
  23750. (unless (string-match "\\S-" (concat b s))
  23751. (setq r (concat r "@<br/>")))
  23752. r))))
  23753. (defun org-html-protect (s)
  23754. ;; convert & to &amp;, < to &lt; and > to &gt;
  23755. (let ((start 0))
  23756. (while (string-match "&" s start)
  23757. (setq s (replace-match "&amp;" t t s)
  23758. start (1+ (match-beginning 0))))
  23759. (while (string-match "<" s)
  23760. (setq s (replace-match "&lt;" t t s)))
  23761. (while (string-match ">" s)
  23762. (setq s (replace-match "&gt;" t t s))))
  23763. s)
  23764. (defun org-export-cleanup-toc-line (s)
  23765. "Remove tags and time staps from lines going into the toc."
  23766. (when (memq org-export-with-tags '(not-in-toc nil))
  23767. (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
  23768. (setq s (replace-match "" t t s))))
  23769. (when org-export-remove-timestamps-from-toc
  23770. (while (string-match org-maybe-keyword-time-regexp s)
  23771. (setq s (replace-match "" t t s))))
  23772. (while (string-match org-bracket-link-regexp s)
  23773. (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
  23774. t t s)))
  23775. s)
  23776. (defun org-html-expand (string)
  23777. "Prepare STRING for HTML export. Applies all active conversions.
  23778. If there are links in the string, don't modify these."
  23779. (let* ((re (concat org-bracket-link-regexp "\\|"
  23780. (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
  23781. m s l res)
  23782. (while (setq m (string-match re string))
  23783. (setq s (substring string 0 m)
  23784. l (match-string 0 string)
  23785. string (substring string (match-end 0)))
  23786. (push (org-html-do-expand s) res)
  23787. (push l res))
  23788. (push (org-html-do-expand string) res)
  23789. (apply 'concat (nreverse res))))
  23790. (defun org-html-do-expand (s)
  23791. "Apply all active conversions to translate special ASCII to HTML."
  23792. (setq s (org-html-protect s))
  23793. (if org-export-html-expand
  23794. (let ((start 0))
  23795. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  23796. (setq s (replace-match "<\\1>" t nil s)))))
  23797. (if org-export-with-emphasize
  23798. (setq s (org-export-html-convert-emphasize s)))
  23799. (if org-export-with-special-strings
  23800. (setq s (org-export-html-convert-special-strings s)))
  23801. (if org-export-with-sub-superscripts
  23802. (setq s (org-export-html-convert-sub-super s)))
  23803. (if org-export-with-TeX-macros
  23804. (let ((start 0) wd ass)
  23805. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  23806. (if (get-text-property (match-beginning 0) 'org-protected s)
  23807. (setq start (match-end 0))
  23808. (setq wd (match-string 1 s))
  23809. (if (setq ass (assoc wd org-html-entities))
  23810. (setq s (replace-match (or (cdr ass)
  23811. (concat "&" (car ass) ";"))
  23812. t t s))
  23813. (setq start (+ start (length wd))))))))
  23814. s)
  23815. (defun org-create-multibrace-regexp (left right n)
  23816. "Create a regular expression which will match a balanced sexp.
  23817. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  23818. as single character strings.
  23819. The regexp returned will match the entire expression including the
  23820. delimiters. It will also define a single group which contains the
  23821. match except for the outermost delimiters. The maximum depth of
  23822. stacked delimiters is N. Escaping delimiters is not possible."
  23823. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  23824. (or "\\|")
  23825. (re nothing)
  23826. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  23827. (while (> n 1)
  23828. (setq n (1- n)
  23829. re (concat re or next)
  23830. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  23831. (concat left "\\(" re "\\)" right)))
  23832. (defvar org-match-substring-regexp
  23833. (concat
  23834. "\\([^\\]\\)\\([_^]\\)\\("
  23835. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23836. "\\|"
  23837. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  23838. "\\|"
  23839. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  23840. "The regular expression matching a sub- or superscript.")
  23841. (defvar org-match-substring-with-braces-regexp
  23842. (concat
  23843. "\\([^\\]\\)\\([_^]\\)\\("
  23844. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  23845. "\\)")
  23846. "The regular expression matching a sub- or superscript, forcing braces.")
  23847. (defconst org-export-html-special-string-regexps
  23848. '(("\\\\-" . "&shy;")
  23849. ("---\\([^-]\\)" . "&mdash;\\1")
  23850. ("--\\([^-]\\)" . "&ndash;\\1")
  23851. ("\\.\\.\\." . "&hellip;"))
  23852. "Regular expressions for special string conversion.")
  23853. (defun org-export-html-convert-special-strings (string)
  23854. "Convert special characters in STRING to HTML."
  23855. (let ((all org-export-html-special-string-regexps)
  23856. e a re rpl start)
  23857. (while (setq a (pop all))
  23858. (setq re (car a) rpl (cdr a) start 0)
  23859. (while (string-match re string start)
  23860. (if (get-text-property (match-beginning 0) 'org-protected string)
  23861. (setq start (match-end 0))
  23862. (setq string (replace-match rpl t nil string)))))
  23863. string))
  23864. (defun org-export-html-convert-sub-super (string)
  23865. "Convert sub- and superscripts in STRING to HTML."
  23866. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  23867. (while (string-match org-match-substring-regexp string s)
  23868. (cond
  23869. ((and requireb (match-end 8)) (setq s (match-end 2)))
  23870. ((get-text-property (match-beginning 2) 'org-protected string)
  23871. (setq s (match-end 2)))
  23872. (t
  23873. (setq s (match-end 1)
  23874. key (if (string= (match-string 2 string) "_") "sub" "sup")
  23875. c (or (match-string 8 string)
  23876. (match-string 6 string)
  23877. (match-string 5 string))
  23878. string (replace-match
  23879. (concat (match-string 1 string)
  23880. "<" key ">" c "</" key ">")
  23881. t t string)))))
  23882. (while (string-match "\\\\\\([_^]\\)" string)
  23883. (setq string (replace-match (match-string 1 string) t t string)))
  23884. string))
  23885. (defun org-export-html-convert-emphasize (string)
  23886. "Apply emphasis."
  23887. (let ((s 0) rpl)
  23888. (while (string-match org-emph-re string s)
  23889. (if (not (equal
  23890. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  23891. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  23892. (setq s (match-beginning 0)
  23893. rpl
  23894. (concat
  23895. (match-string 1 string)
  23896. (nth 2 (assoc (match-string 3 string) org-emphasis-alist))
  23897. (match-string 4 string)
  23898. (nth 3 (assoc (match-string 3 string)
  23899. org-emphasis-alist))
  23900. (match-string 5 string))
  23901. string (replace-match rpl t t string)
  23902. s (+ s (- (length rpl) 2)))
  23903. (setq s (1+ s))))
  23904. string))
  23905. (defvar org-par-open nil)
  23906. (defun org-open-par ()
  23907. "Insert <p>, but first close previous paragraph if any."
  23908. (org-close-par-maybe)
  23909. (insert "\n<p>")
  23910. (setq org-par-open t))
  23911. (defun org-close-par-maybe ()
  23912. "Close paragraph if there is one open."
  23913. (when org-par-open
  23914. (insert "</p>")
  23915. (setq org-par-open nil)))
  23916. (defun org-close-li ()
  23917. "Close <li> if necessary."
  23918. (org-close-par-maybe)
  23919. (insert "</li>\n"))
  23920. (defvar body-only) ; dynamically scoped into this.
  23921. (defun org-html-level-start (level title umax with-toc head-count)
  23922. "Insert a new level in HTML export.
  23923. When TITLE is nil, just close all open levels."
  23924. (org-close-par-maybe)
  23925. (let ((l org-level-max))
  23926. (while (>= l level)
  23927. (if (aref org-levels-open (1- l))
  23928. (progn
  23929. (org-html-level-close l umax)
  23930. (aset org-levels-open (1- l) nil)))
  23931. (setq l (1- l)))
  23932. (when title
  23933. ;; If title is nil, this means this function is called to close
  23934. ;; all levels, so the rest is done only if title is given
  23935. (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  23936. (setq title (replace-match
  23937. (if org-export-with-tags
  23938. (save-match-data
  23939. (concat
  23940. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  23941. (mapconcat 'identity (org-split-string
  23942. (match-string 1 title) ":")
  23943. "&nbsp;")
  23944. "</span>"))
  23945. "")
  23946. t t title)))
  23947. (if (> level umax)
  23948. (progn
  23949. (if (aref org-levels-open (1- level))
  23950. (progn
  23951. (org-close-li)
  23952. (insert "<li>" title "<br/>\n"))
  23953. (aset org-levels-open (1- level) t)
  23954. (org-close-par-maybe)
  23955. (insert "<ul>\n<li>" title "<br/>\n")))
  23956. (aset org-levels-open (1- level) t)
  23957. (if (and org-export-with-section-numbers (not body-only))
  23958. (setq title (concat (org-section-number level) " " title)))
  23959. (setq level (+ level org-export-html-toplevel-hlevel -1))
  23960. (if with-toc
  23961. (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
  23962. level level head-count title level))
  23963. (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level)))
  23964. (org-open-par)))))
  23965. (defun org-html-level-close (level max-outline-level)
  23966. "Terminate one level in HTML export."
  23967. (if (<= level max-outline-level)
  23968. (insert "</div>\n")
  23969. (org-close-li)
  23970. (insert "</ul>\n")))
  23971. ;;; iCalendar export
  23972. ;;;###autoload
  23973. (defun org-export-icalendar-this-file ()
  23974. "Export current file as an iCalendar file.
  23975. The iCalendar file will be located in the same directory as the Org-mode
  23976. file, but with extension `.ics'."
  23977. (interactive)
  23978. (org-export-icalendar nil buffer-file-name))
  23979. ;;;###autoload
  23980. (defun org-export-icalendar-all-agenda-files ()
  23981. "Export all files in `org-agenda-files' to iCalendar .ics files.
  23982. Each iCalendar file will be located in the same directory as the Org-mode
  23983. file, but with extension `.ics'."
  23984. (interactive)
  23985. (apply 'org-export-icalendar nil (org-agenda-files t)))
  23986. ;;;###autoload
  23987. (defun org-export-icalendar-combine-agenda-files ()
  23988. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  23989. The file is stored under the name `org-combined-agenda-icalendar-file'."
  23990. (interactive)
  23991. (apply 'org-export-icalendar t (org-agenda-files t)))
  23992. (defun org-export-icalendar (combine &rest files)
  23993. "Create iCalendar files for all elements of FILES.
  23994. If COMBINE is non-nil, combine all calendar entries into a single large
  23995. file and store it under the name `org-combined-agenda-icalendar-file'."
  23996. (save-excursion
  23997. (org-prepare-agenda-buffers files)
  23998. (let* ((dir (org-export-directory
  23999. :ical (list :publishing-directory
  24000. org-export-publishing-directory)))
  24001. file ical-file ical-buffer category started org-agenda-new-buffers)
  24002. (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
  24003. (when combine
  24004. (setq ical-file
  24005. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  24006. org-combined-agenda-icalendar-file
  24007. (expand-file-name org-combined-agenda-icalendar-file dir))
  24008. ical-buffer (org-get-agenda-file-buffer ical-file))
  24009. (set-buffer ical-buffer) (erase-buffer))
  24010. (while (setq file (pop files))
  24011. (catch 'nextfile
  24012. (org-check-agenda-file file)
  24013. (set-buffer (org-get-agenda-file-buffer file))
  24014. (unless combine
  24015. (setq ical-file (concat (file-name-as-directory dir)
  24016. (file-name-sans-extension
  24017. (file-name-nondirectory buffer-file-name))
  24018. ".ics"))
  24019. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  24020. (with-current-buffer ical-buffer (erase-buffer)))
  24021. (setq category (or org-category
  24022. (file-name-sans-extension
  24023. (file-name-nondirectory buffer-file-name))))
  24024. (if (symbolp category) (setq category (symbol-name category)))
  24025. (let ((standard-output ical-buffer))
  24026. (if combine
  24027. (and (not started) (setq started t)
  24028. (org-start-icalendar-file org-icalendar-combined-name))
  24029. (org-start-icalendar-file category))
  24030. (org-print-icalendar-entries combine)
  24031. (when (or (and combine (not files)) (not combine))
  24032. (org-finish-icalendar-file)
  24033. (set-buffer ical-buffer)
  24034. (save-buffer)
  24035. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  24036. (org-release-buffers org-agenda-new-buffers))))
  24037. (defvar org-after-save-iCalendar-file-hook nil
  24038. "Hook run after an iCalendar file has been saved.
  24039. The iCalendar buffer is still current when this hook is run.
  24040. A good way to use this is to tell a desktop calenndar application to re-read
  24041. the iCalendar file.")
  24042. (defun org-print-icalendar-entries (&optional combine)
  24043. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  24044. When COMBINE is non nil, add the category to each line."
  24045. (let ((re1 (concat org-ts-regexp "\\|<%%([^>\n]+>"))
  24046. (re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  24047. (dts (org-ical-ts-to-string
  24048. (format-time-string (cdr org-time-stamp-formats) (current-time))
  24049. "DTSTART"))
  24050. hd ts ts2 state status (inc t) pos b sexp rrule
  24051. scheduledp deadlinep tmp pri category entry location summary desc
  24052. (sexp-buffer (get-buffer-create "*ical-tmp*")))
  24053. (org-refresh-category-properties)
  24054. (save-excursion
  24055. (goto-char (point-min))
  24056. (while (re-search-forward re1 nil t)
  24057. (catch :skip
  24058. (org-agenda-skip)
  24059. (setq pos (match-beginning 0)
  24060. ts (match-string 0)
  24061. inc t
  24062. hd (org-get-heading)
  24063. summary (org-icalendar-cleanup-string
  24064. (org-entry-get nil "SUMMARY"))
  24065. desc (org-icalendar-cleanup-string
  24066. (or (org-entry-get nil "DESCRIPTION")
  24067. (and org-icalendar-include-body (org-get-entry)))
  24068. t org-icalendar-include-body)
  24069. location (org-icalendar-cleanup-string
  24070. (org-entry-get nil "LOCATION"))
  24071. category (org-get-category))
  24072. (if (looking-at re2)
  24073. (progn
  24074. (goto-char (match-end 0))
  24075. (setq ts2 (match-string 1) inc nil))
  24076. (setq tmp (buffer-substring (max (point-min)
  24077. (- pos org-ds-keyword-length))
  24078. pos)
  24079. ts2 (if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts)
  24080. (progn
  24081. (setq inc nil)
  24082. (replace-match "\\1" t nil ts))
  24083. ts)
  24084. deadlinep (string-match org-deadline-regexp tmp)
  24085. scheduledp (string-match org-scheduled-regexp tmp)
  24086. ;; donep (org-entry-is-done-p)
  24087. ))
  24088. (if (or (string-match org-tr-regexp hd)
  24089. (string-match org-ts-regexp hd))
  24090. (setq hd (replace-match "" t t hd)))
  24091. (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts)
  24092. (setq rrule
  24093. (concat "\nRRULE:FREQ="
  24094. (cdr (assoc
  24095. (match-string 2 ts)
  24096. '(("d" . "DAILY")("w" . "WEEKLY")
  24097. ("m" . "MONTHLY")("y" . "YEARLY"))))
  24098. ";INTERVAL=" (match-string 1 ts)))
  24099. (setq rrule ""))
  24100. (setq summary (or summary hd))
  24101. (if (string-match org-bracket-link-regexp summary)
  24102. (setq summary
  24103. (replace-match (if (match-end 3)
  24104. (match-string 3 summary)
  24105. (match-string 1 summary))
  24106. t t summary)))
  24107. (if deadlinep (setq summary (concat "DL: " summary)))
  24108. (if scheduledp (setq summary (concat "S: " summary)))
  24109. (if (string-match "\\`<%%" ts)
  24110. (with-current-buffer sexp-buffer
  24111. (insert (substring ts 1 -1) " " summary "\n"))
  24112. (princ (format "BEGIN:VEVENT
  24113. %s
  24114. %s%s
  24115. SUMMARY:%s%s%s
  24116. CATEGORIES:%s
  24117. END:VEVENT\n"
  24118. (org-ical-ts-to-string ts "DTSTART")
  24119. (org-ical-ts-to-string ts2 "DTEND" inc)
  24120. rrule summary
  24121. (if (and desc (string-match "\\S-" desc))
  24122. (concat "\nDESCRIPTION: " desc) "")
  24123. (if (and location (string-match "\\S-" location))
  24124. (concat "\nLOCATION: " location) "")
  24125. category)))))
  24126. (when (and org-icalendar-include-sexps
  24127. (condition-case nil (require 'icalendar) (error nil))
  24128. (fboundp 'icalendar-export-region))
  24129. ;; Get all the literal sexps
  24130. (goto-char (point-min))
  24131. (while (re-search-forward "^&?%%(" nil t)
  24132. (catch :skip
  24133. (org-agenda-skip)
  24134. (setq b (match-beginning 0))
  24135. (goto-char (1- (match-end 0)))
  24136. (forward-sexp 1)
  24137. (end-of-line 1)
  24138. (setq sexp (buffer-substring b (point)))
  24139. (with-current-buffer sexp-buffer
  24140. (insert sexp "\n"))
  24141. (princ (org-diary-to-ical-string sexp-buffer)))))
  24142. (when org-icalendar-include-todo
  24143. (goto-char (point-min))
  24144. (while (re-search-forward org-todo-line-regexp nil t)
  24145. (catch :skip
  24146. (org-agenda-skip)
  24147. (setq state (match-string 2))
  24148. (setq status (if (member state org-done-keywords)
  24149. "COMPLETED" "NEEDS-ACTION"))
  24150. (when (and state
  24151. (or (not (member state org-done-keywords))
  24152. (eq org-icalendar-include-todo 'all))
  24153. (not (member org-archive-tag (org-get-tags-at)))
  24154. )
  24155. (setq hd (match-string 3)
  24156. summary (org-icalendar-cleanup-string
  24157. (org-entry-get nil "SUMMARY"))
  24158. desc (org-icalendar-cleanup-string
  24159. (or (org-entry-get nil "DESCRIPTION")
  24160. (and org-icalendar-include-body (org-get-entry)))
  24161. t org-icalendar-include-body)
  24162. location (org-icalendar-cleanup-string
  24163. (org-entry-get nil "LOCATION")))
  24164. (if (string-match org-bracket-link-regexp hd)
  24165. (setq hd (replace-match (if (match-end 3) (match-string 3 hd)
  24166. (match-string 1 hd))
  24167. t t hd)))
  24168. (if (string-match org-priority-regexp hd)
  24169. (setq pri (string-to-char (match-string 2 hd))
  24170. hd (concat (substring hd 0 (match-beginning 1))
  24171. (substring hd (match-end 1))))
  24172. (setq pri org-default-priority))
  24173. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  24174. (- org-lowest-priority org-highest-priority))))))
  24175. (princ (format "BEGIN:VTODO
  24176. %s
  24177. SUMMARY:%s%s%s
  24178. CATEGORIES:%s
  24179. SEQUENCE:1
  24180. PRIORITY:%d
  24181. STATUS:%s
  24182. END:VTODO\n"
  24183. dts
  24184. (or summary hd)
  24185. (if (and location (string-match "\\S-" location))
  24186. (concat "\nLOCATION: " location) "")
  24187. (if (and desc (string-match "\\S-" desc))
  24188. (concat "\nDESCRIPTION: " desc) "")
  24189. category pri status)))))))))
  24190. (defun org-icalendar-cleanup-string (s &optional is-body maxlength)
  24191. "Take out stuff and quote what needs to be quoted.
  24192. When IS-BODY is non-nil, assume that this is the body of an item, clean up
  24193. whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
  24194. characters."
  24195. (if (not s)
  24196. nil
  24197. (when is-body
  24198. (let ((re (concat "\\(" org-drawer-regexp "\\)[^\000]*?:END:.*\n?"))
  24199. (re2 (concat "^[ \t]*" org-keyword-time-regexp ".*\n?")))
  24200. (while (string-match re s) (setq s (replace-match "" t t s)))
  24201. (while (string-match re2 s) (setq s (replace-match "" t t s)))))
  24202. (let ((start 0))
  24203. (while (string-match "\\([,;\\]\\)" s start)
  24204. (setq start (+ (match-beginning 0) 2)
  24205. s (replace-match "\\\\\\1" nil nil s))))
  24206. (when is-body
  24207. (while (string-match "[ \t]*\n[ \t]*" s)
  24208. (setq s (replace-match "\\n" t t s))))
  24209. (setq s (org-trim s))
  24210. (if is-body
  24211. (if maxlength
  24212. (if (and (numberp maxlength)
  24213. (> (length s) maxlength))
  24214. (setq s (substring s 0 maxlength)))))
  24215. s))
  24216. (defun org-get-entry ()
  24217. "Clean-up description string."
  24218. (save-excursion
  24219. (org-back-to-heading t)
  24220. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  24221. (defun org-start-icalendar-file (name)
  24222. "Start an iCalendar file by inserting the header."
  24223. (let ((user user-full-name)
  24224. (name (or name "unknown"))
  24225. (timezone (cadr (current-time-zone))))
  24226. (princ
  24227. (format "BEGIN:VCALENDAR
  24228. VERSION:2.0
  24229. X-WR-CALNAME:%s
  24230. PRODID:-//%s//Emacs with Org-mode//EN
  24231. X-WR-TIMEZONE:%s
  24232. CALSCALE:GREGORIAN\n" name user timezone))))
  24233. (defun org-finish-icalendar-file ()
  24234. "Finish an iCalendar file by inserting the END statement."
  24235. (princ "END:VCALENDAR\n"))
  24236. (defun org-ical-ts-to-string (s keyword &optional inc)
  24237. "Take a time string S and convert it to iCalendar format.
  24238. KEYWORD is added in front, to make a complete line like DTSTART....
  24239. When INC is non-nil, increase the hour by two (if time string contains
  24240. a time), or the day by one (if it does not contain a time)."
  24241. (let ((t1 (org-parse-time-string s 'nodefault))
  24242. t2 fmt have-time time)
  24243. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  24244. (setq t2 t1 have-time t)
  24245. (setq t2 (org-parse-time-string s)))
  24246. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  24247. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  24248. (when inc
  24249. (if have-time
  24250. (if org-agenda-default-appointment-duration
  24251. (setq mi (+ org-agenda-default-appointment-duration mi))
  24252. (setq h (+ 2 h)))
  24253. (setq d (1+ d))))
  24254. (setq time (encode-time s mi h d m y)))
  24255. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  24256. (concat keyword (format-time-string fmt time))))
  24257. ;;; XOXO export
  24258. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  24259. (with-current-buffer buffer
  24260. (apply 'insert output)))
  24261. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  24262. (defun org-export-as-xoxo (&optional buffer)
  24263. "Export the org buffer as XOXO.
  24264. The XOXO buffer is named *xoxo-<source buffer name>*"
  24265. (interactive (list (current-buffer)))
  24266. ;; A quickie abstraction
  24267. ;; Output everything as XOXO
  24268. (with-current-buffer (get-buffer buffer)
  24269. (let* ((pos (point))
  24270. (opt-plist (org-combine-plists (org-default-export-plist)
  24271. (org-infile-export-plist)))
  24272. (filename (concat (file-name-as-directory
  24273. (org-export-directory :xoxo opt-plist))
  24274. (file-name-sans-extension
  24275. (file-name-nondirectory buffer-file-name))
  24276. ".html"))
  24277. (out (find-file-noselect filename))
  24278. (last-level 1)
  24279. (hanging-li nil))
  24280. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  24281. ;; Check the output buffer is empty.
  24282. (with-current-buffer out (erase-buffer))
  24283. ;; Kick off the output
  24284. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  24285. (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't)
  24286. (let* ((hd (match-string-no-properties 1))
  24287. (level (length hd))
  24288. (text (concat
  24289. (match-string-no-properties 2)
  24290. (save-excursion
  24291. (goto-char (match-end 0))
  24292. (let ((str ""))
  24293. (catch 'loop
  24294. (while 't
  24295. (forward-line)
  24296. (if (looking-at "^[ \t]\\(.*\\)")
  24297. (setq str (concat str (match-string-no-properties 1)))
  24298. (throw 'loop str)))))))))
  24299. ;; Handle level rendering
  24300. (cond
  24301. ((> level last-level)
  24302. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  24303. ((< level last-level)
  24304. (dotimes (- (- last-level level) 1)
  24305. (if hanging-li
  24306. (org-export-as-xoxo-insert-into out "</li>\n"))
  24307. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24308. (when hanging-li
  24309. (org-export-as-xoxo-insert-into out "</li>\n")
  24310. (setq hanging-li nil)))
  24311. ((equal level last-level)
  24312. (if hanging-li
  24313. (org-export-as-xoxo-insert-into out "</li>\n")))
  24314. )
  24315. (setq last-level level)
  24316. ;; And output the new li
  24317. (setq hanging-li 't)
  24318. (if (equal ?+ (elt text 0))
  24319. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  24320. (org-export-as-xoxo-insert-into out "<li>" text))))
  24321. ;; Finally finish off the ol
  24322. (dotimes (- last-level 1)
  24323. (if hanging-li
  24324. (org-export-as-xoxo-insert-into out "</li>\n"))
  24325. (org-export-as-xoxo-insert-into out "</ol>\n"))
  24326. (goto-char pos)
  24327. ;; Finish the buffer off and clean it up.
  24328. (switch-to-buffer-other-window out)
  24329. (indent-region (point-min) (point-max) nil)
  24330. (save-buffer)
  24331. (goto-char (point-min))
  24332. )))
  24333. ;;;; Key bindings
  24334. ;; Make `C-c C-x' a prefix key
  24335. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  24336. ;; TAB key with modifiers
  24337. (org-defkey org-mode-map "\C-i" 'org-cycle)
  24338. (org-defkey org-mode-map [(tab)] 'org-cycle)
  24339. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  24340. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  24341. (org-defkey org-mode-map "\M-\t" 'org-complete)
  24342. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  24343. ;; The following line is necessary under Suse GNU/Linux
  24344. (unless (featurep 'xemacs)
  24345. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  24346. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  24347. (define-key org-mode-map [backtab] 'org-shifttab)
  24348. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  24349. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  24350. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  24351. ;; Cursor keys with modifiers
  24352. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  24353. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  24354. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  24355. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  24356. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  24357. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  24358. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  24359. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  24360. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  24361. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  24362. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  24363. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  24364. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  24365. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  24366. ;;; Extra keys for tty access.
  24367. ;; We only set them when really needed because otherwise the
  24368. ;; menus don't show the simple keys
  24369. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  24370. (not window-system))
  24371. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  24372. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  24373. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  24374. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  24375. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  24376. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  24377. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  24378. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  24379. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  24380. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  24381. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  24382. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  24383. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  24384. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  24385. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  24386. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  24387. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  24388. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  24389. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  24390. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  24391. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  24392. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  24393. ;; All the other keys
  24394. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  24395. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  24396. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  24397. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  24398. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  24399. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  24400. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  24401. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  24402. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  24403. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  24404. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  24405. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  24406. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  24407. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  24408. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  24409. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  24410. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  24411. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  24412. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  24413. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  24414. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  24415. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  24416. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  24417. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  24418. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  24419. (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  24420. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  24421. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  24422. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  24423. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  24424. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  24425. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  24426. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  24427. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  24428. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  24429. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  24430. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  24431. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  24432. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  24433. (org-defkey org-mode-map "\C-c^" 'org-sort)
  24434. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  24435. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  24436. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  24437. (org-defkey org-mode-map "\C-m" 'org-return)
  24438. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  24439. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  24440. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  24441. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  24442. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  24443. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  24444. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  24445. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  24446. (org-defkey org-mode-map "\C-c*" 'org-table-recalculate)
  24447. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  24448. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  24449. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  24450. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  24451. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  24452. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  24453. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  24454. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  24455. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  24456. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  24457. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  24458. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  24459. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  24460. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  24461. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  24462. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  24463. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  24464. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  24465. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  24466. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  24467. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  24468. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  24469. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  24470. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  24471. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  24472. (when (featurep 'xemacs)
  24473. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  24474. (defsubst org-table-p () (org-at-table-p))
  24475. (defun org-self-insert-command (N)
  24476. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  24477. If the cursor is in a table looking at whitespace, the whitespace is
  24478. overwritten, and the table is not marked as requiring realignment."
  24479. (interactive "p")
  24480. (if (and (org-table-p)
  24481. (progn
  24482. ;; check if we blank the field, and if that triggers align
  24483. (and org-table-auto-blank-field
  24484. (member last-command
  24485. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  24486. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  24487. ;; got extra space, this field does not determine column width
  24488. (let (org-table-may-need-update) (org-table-blank-field))
  24489. ;; no extra space, this field may determine column width
  24490. (org-table-blank-field)))
  24491. t)
  24492. (eq N 1)
  24493. (looking-at "[^|\n]* |"))
  24494. (let (org-table-may-need-update)
  24495. (goto-char (1- (match-end 0)))
  24496. (delete-backward-char 1)
  24497. (goto-char (match-beginning 0))
  24498. (self-insert-command N))
  24499. (setq org-table-may-need-update t)
  24500. (self-insert-command N)
  24501. (org-fix-tags-on-the-fly)))
  24502. (defun org-fix-tags-on-the-fly ()
  24503. (when (and (equal (char-after (point-at-bol)) ?*)
  24504. (org-on-heading-p))
  24505. (org-align-tags-here org-tags-column)))
  24506. (defun org-delete-backward-char (N)
  24507. "Like `delete-backward-char', insert whitespace at field end in tables.
  24508. When deleting backwards, in tables this function will insert whitespace in
  24509. front of the next \"|\" separator, to keep the table aligned. The table will
  24510. still be marked for re-alignment if the field did fill the entire column,
  24511. because, in this case the deletion might narrow the column."
  24512. (interactive "p")
  24513. (if (and (org-table-p)
  24514. (eq N 1)
  24515. (string-match "|" (buffer-substring (point-at-bol) (point)))
  24516. (looking-at ".*?|"))
  24517. (let ((pos (point))
  24518. (noalign (looking-at "[^|\n\r]* |"))
  24519. (c org-table-may-need-update))
  24520. (backward-delete-char N)
  24521. (skip-chars-forward "^|")
  24522. (insert " ")
  24523. (goto-char (1- pos))
  24524. ;; noalign: if there were two spaces at the end, this field
  24525. ;; does not determine the width of the column.
  24526. (if noalign (setq org-table-may-need-update c)))
  24527. (backward-delete-char N)
  24528. (org-fix-tags-on-the-fly)))
  24529. (defun org-delete-char (N)
  24530. "Like `delete-char', but insert whitespace at field end in tables.
  24531. When deleting characters, in tables this function will insert whitespace in
  24532. front of the next \"|\" separator, to keep the table aligned. The table will
  24533. still be marked for re-alignment if the field did fill the entire column,
  24534. because, in this case the deletion might narrow the column."
  24535. (interactive "p")
  24536. (if (and (org-table-p)
  24537. (not (bolp))
  24538. (not (= (char-after) ?|))
  24539. (eq N 1))
  24540. (if (looking-at ".*?|")
  24541. (let ((pos (point))
  24542. (noalign (looking-at "[^|\n\r]* |"))
  24543. (c org-table-may-need-update))
  24544. (replace-match (concat
  24545. (substring (match-string 0) 1 -1)
  24546. " |"))
  24547. (goto-char pos)
  24548. ;; noalign: if there were two spaces at the end, this field
  24549. ;; does not determine the width of the column.
  24550. (if noalign (setq org-table-may-need-update c)))
  24551. (delete-char N))
  24552. (delete-char N)
  24553. (org-fix-tags-on-the-fly)))
  24554. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  24555. (put 'org-self-insert-command 'delete-selection t)
  24556. (put 'orgtbl-self-insert-command 'delete-selection t)
  24557. (put 'org-delete-char 'delete-selection 'supersede)
  24558. (put 'org-delete-backward-char 'delete-selection 'supersede)
  24559. ;; Make `flyspell-mode' delay after some commands
  24560. (put 'org-self-insert-command 'flyspell-delayed t)
  24561. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  24562. (put 'org-delete-char 'flyspell-delayed t)
  24563. (put 'org-delete-backward-char 'flyspell-delayed t)
  24564. ;; Make pabbrev-mode expand after org-mode commands
  24565. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  24566. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  24567. ;; How to do this: Measure non-white length of current string
  24568. ;; If equal to column width, we should realign.
  24569. (defun org-remap (map &rest commands)
  24570. "In MAP, remap the functions given in COMMANDS.
  24571. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  24572. (let (new old)
  24573. (while commands
  24574. (setq old (pop commands) new (pop commands))
  24575. (if (fboundp 'command-remapping)
  24576. (org-defkey map (vector 'remap old) new)
  24577. (substitute-key-definition old new map global-map)))))
  24578. (when (eq org-enable-table-editor 'optimized)
  24579. ;; If the user wants maximum table support, we need to hijack
  24580. ;; some standard editing functions
  24581. (org-remap org-mode-map
  24582. 'self-insert-command 'org-self-insert-command
  24583. 'delete-char 'org-delete-char
  24584. 'delete-backward-char 'org-delete-backward-char)
  24585. (org-defkey org-mode-map "|" 'org-force-self-insert))
  24586. (defun org-shiftcursor-error ()
  24587. "Throw an error because Shift-Cursor command was applied in wrong context."
  24588. (error "This command is active in special context like tables, headlines or timestamps"))
  24589. (defun org-shifttab (&optional arg)
  24590. "Global visibility cycling or move to previous table field.
  24591. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  24592. on context.
  24593. See the individual commands for more information."
  24594. (interactive "P")
  24595. (cond
  24596. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  24597. (arg (message "Content view to level: ")
  24598. (org-content (prefix-numeric-value arg))
  24599. (setq org-cycle-global-status 'overview))
  24600. (t (call-interactively 'org-global-cycle))))
  24601. (defun org-shiftmetaleft ()
  24602. "Promote subtree or delete table column.
  24603. Calls `org-promote-subtree', `org-outdent-item',
  24604. or `org-table-delete-column', depending on context.
  24605. See the individual commands for more information."
  24606. (interactive)
  24607. (cond
  24608. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  24609. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  24610. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24611. (t (org-shiftcursor-error))))
  24612. (defun org-shiftmetaright ()
  24613. "Demote subtree or insert table column.
  24614. Calls `org-demote-subtree', `org-indent-item',
  24615. or `org-table-insert-column', depending on context.
  24616. See the individual commands for more information."
  24617. (interactive)
  24618. (cond
  24619. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  24620. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  24621. ((org-at-item-p) (call-interactively 'org-indent-item))
  24622. (t (org-shiftcursor-error))))
  24623. (defun org-shiftmetaup (&optional arg)
  24624. "Move subtree up or kill table row.
  24625. Calls `org-move-subtree-up' or `org-table-kill-row' or
  24626. `org-move-item-up' depending on context. See the individual commands
  24627. for more information."
  24628. (interactive "P")
  24629. (cond
  24630. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  24631. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24632. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24633. (t (org-shiftcursor-error))))
  24634. (defun org-shiftmetadown (&optional arg)
  24635. "Move subtree down or insert table row.
  24636. Calls `org-move-subtree-down' or `org-table-insert-row' or
  24637. `org-move-item-down', depending on context. See the individual
  24638. commands for more information."
  24639. (interactive "P")
  24640. (cond
  24641. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  24642. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24643. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24644. (t (org-shiftcursor-error))))
  24645. (defun org-metaleft (&optional arg)
  24646. "Promote heading or move table column to left.
  24647. Calls `org-do-promote' or `org-table-move-column', depending on context.
  24648. With no specific context, calls the Emacs default `backward-word'.
  24649. See the individual commands for more information."
  24650. (interactive "P")
  24651. (cond
  24652. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  24653. ((or (org-on-heading-p) (org-region-active-p))
  24654. (call-interactively 'org-do-promote))
  24655. ((org-at-item-p) (call-interactively 'org-outdent-item))
  24656. (t (call-interactively 'backward-word))))
  24657. (defun org-metaright (&optional arg)
  24658. "Demote subtree or move table column to right.
  24659. Calls `org-do-demote' or `org-table-move-column', depending on context.
  24660. With no specific context, calls the Emacs default `forward-word'.
  24661. See the individual commands for more information."
  24662. (interactive "P")
  24663. (cond
  24664. ((org-at-table-p) (call-interactively 'org-table-move-column))
  24665. ((or (org-on-heading-p) (org-region-active-p))
  24666. (call-interactively 'org-do-demote))
  24667. ((org-at-item-p) (call-interactively 'org-indent-item))
  24668. (t (call-interactively 'forward-word))))
  24669. (defun org-metaup (&optional arg)
  24670. "Move subtree up or move table row up.
  24671. Calls `org-move-subtree-up' or `org-table-move-row' or
  24672. `org-move-item-up', depending on context. See the individual commands
  24673. for more information."
  24674. (interactive "P")
  24675. (cond
  24676. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  24677. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  24678. ((org-at-item-p) (call-interactively 'org-move-item-up))
  24679. (t (transpose-lines 1) (beginning-of-line -1))))
  24680. (defun org-metadown (&optional arg)
  24681. "Move subtree down or move table row down.
  24682. Calls `org-move-subtree-down' or `org-table-move-row' or
  24683. `org-move-item-down', depending on context. See the individual
  24684. commands for more information."
  24685. (interactive "P")
  24686. (cond
  24687. ((org-at-table-p) (call-interactively 'org-table-move-row))
  24688. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  24689. ((org-at-item-p) (call-interactively 'org-move-item-down))
  24690. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  24691. (defun org-shiftup (&optional arg)
  24692. "Increase item in timestamp or increase priority of current headline.
  24693. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  24694. depending on context. See the individual commands for more information."
  24695. (interactive "P")
  24696. (cond
  24697. ((org-at-timestamp-p t)
  24698. (call-interactively (if org-edit-timestamp-down-means-later
  24699. 'org-timestamp-down 'org-timestamp-up)))
  24700. ((org-on-heading-p) (call-interactively 'org-priority-up))
  24701. ((org-at-item-p) (call-interactively 'org-previous-item))
  24702. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  24703. (defun org-shiftdown (&optional arg)
  24704. "Decrease item in timestamp or decrease priority of current headline.
  24705. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  24706. depending on context. See the individual commands for more information."
  24707. (interactive "P")
  24708. (cond
  24709. ((org-at-timestamp-p t)
  24710. (call-interactively (if org-edit-timestamp-down-means-later
  24711. 'org-timestamp-up 'org-timestamp-down)))
  24712. ((org-on-heading-p) (call-interactively 'org-priority-down))
  24713. (t (call-interactively 'org-next-item))))
  24714. (defun org-shiftright ()
  24715. "Next TODO keyword or timestamp one day later, depending on context."
  24716. (interactive)
  24717. (cond
  24718. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  24719. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  24720. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  24721. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  24722. (t (org-shiftcursor-error))))
  24723. (defun org-shiftleft ()
  24724. "Previous TODO keyword or timestamp one day earlier, depending on context."
  24725. (interactive)
  24726. (cond
  24727. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  24728. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  24729. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  24730. ((org-at-property-p)
  24731. (call-interactively 'org-property-previous-allowed-value))
  24732. (t (org-shiftcursor-error))))
  24733. (defun org-shiftcontrolright ()
  24734. "Switch to next TODO set."
  24735. (interactive)
  24736. (cond
  24737. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  24738. (t (org-shiftcursor-error))))
  24739. (defun org-shiftcontrolleft ()
  24740. "Switch to previous TODO set."
  24741. (interactive)
  24742. (cond
  24743. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  24744. (t (org-shiftcursor-error))))
  24745. (defun org-ctrl-c-ret ()
  24746. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  24747. (interactive)
  24748. (cond
  24749. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  24750. (t (call-interactively 'org-insert-heading))))
  24751. (defun org-copy-special ()
  24752. "Copy region in table or copy current subtree.
  24753. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  24754. See the individual commands for more information."
  24755. (interactive)
  24756. (call-interactively
  24757. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  24758. (defun org-cut-special ()
  24759. "Cut region in table or cut current subtree.
  24760. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  24761. See the individual commands for more information."
  24762. (interactive)
  24763. (call-interactively
  24764. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  24765. (defun org-paste-special (arg)
  24766. "Paste rectangular region into table, or past subtree relative to level.
  24767. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  24768. See the individual commands for more information."
  24769. (interactive "P")
  24770. (if (org-at-table-p)
  24771. (org-table-paste-rectangle)
  24772. (org-paste-subtree arg)))
  24773. (defun org-ctrl-c-ctrl-c (&optional arg)
  24774. "Set tags in headline, or update according to changed information at point.
  24775. This command does many different things, depending on context:
  24776. - If the cursor is in a headline, prompt for tags and insert them
  24777. into the current line, aligned to `org-tags-column'. When called
  24778. with prefix arg, realign all tags in the current buffer.
  24779. - If the cursor is in one of the special #+KEYWORD lines, this
  24780. triggers scanning the buffer for these lines and updating the
  24781. information.
  24782. - If the cursor is inside a table, realign the table. This command
  24783. works even if the automatic table editor has been turned off.
  24784. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  24785. the entire table.
  24786. - If the cursor is a the beginning of a dynamic block, update it.
  24787. - If the cursor is inside a table created by the table.el package,
  24788. activate that table.
  24789. - If the current buffer is a remember buffer, close note and file it.
  24790. with a prefix argument, file it without further interaction to the default
  24791. location.
  24792. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  24793. links in this buffer.
  24794. - If the cursor is on a numbered item in a plain list, renumber the
  24795. ordered list.
  24796. - If the cursor is on a checkbox, toggle it."
  24797. (interactive "P")
  24798. (let ((org-enable-table-editor t))
  24799. (cond
  24800. ((or org-clock-overlays
  24801. org-occur-highlights
  24802. org-latex-fragment-image-overlays)
  24803. (org-remove-clock-overlays)
  24804. (org-remove-occur-highlights)
  24805. (org-remove-latex-fragment-image-overlays)
  24806. (message "Temporary highlights/overlays removed from current buffer"))
  24807. ((and (local-variable-p 'org-finish-function (current-buffer))
  24808. (fboundp org-finish-function))
  24809. (funcall org-finish-function))
  24810. ((org-at-property-p)
  24811. (call-interactively 'org-property-action))
  24812. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  24813. ((org-on-heading-p) (call-interactively 'org-set-tags))
  24814. ((org-at-table.el-p)
  24815. (require 'table)
  24816. (beginning-of-line 1)
  24817. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  24818. (call-interactively 'table-recognize-table))
  24819. ((org-at-table-p)
  24820. (org-table-maybe-eval-formula)
  24821. (if arg
  24822. (call-interactively 'org-table-recalculate)
  24823. (org-table-maybe-recalculate-line))
  24824. (call-interactively 'org-table-align))
  24825. ((org-at-item-checkbox-p)
  24826. (call-interactively 'org-toggle-checkbox))
  24827. ((org-at-item-p)
  24828. (call-interactively 'org-maybe-renumber-ordered-list))
  24829. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  24830. ;; Dynamic block
  24831. (beginning-of-line 1)
  24832. (org-update-dblock))
  24833. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  24834. (cond
  24835. ((equal (match-string 1) "TBLFM")
  24836. ;; Recalculate the table before this line
  24837. (save-excursion
  24838. (beginning-of-line 1)
  24839. (skip-chars-backward " \r\n\t")
  24840. (if (org-at-table-p)
  24841. (org-call-with-arg 'org-table-recalculate t))))
  24842. (t
  24843. (call-interactively 'org-mode-restart))))
  24844. (t (error "C-c C-c can do nothing useful at this location.")))))
  24845. (defun org-mode-restart ()
  24846. "Restart Org-mode, to scan again for special lines.
  24847. Also updates the keyword regular expressions."
  24848. (interactive)
  24849. (let ((org-inhibit-startup t)) (org-mode))
  24850. (message "Org-mode restarted to refresh keyword and special line setup"))
  24851. (defun org-kill-note-or-show-branches ()
  24852. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  24853. (interactive)
  24854. (if (not org-finish-function)
  24855. (call-interactively 'show-branches)
  24856. (let ((org-note-abort t))
  24857. (funcall org-finish-function))))
  24858. (defun org-return (&optional indent)
  24859. "Goto next table row or insert a newline.
  24860. Calls `org-table-next-row' or `newline', depending on context.
  24861. See the individual commands for more information."
  24862. (interactive)
  24863. (cond
  24864. ((bobp) (if indent (newline-and-indent) (newline)))
  24865. ((org-at-table-p)
  24866. (org-table-justify-field-maybe)
  24867. (call-interactively 'org-table-next-row))
  24868. (t (if indent (newline-and-indent) (newline)))))
  24869. (defun org-return-indent ()
  24870. (interactive)
  24871. "Goto next table row or insert a newline and indent.
  24872. Calls `org-table-next-row' or `newline-and-indent', depending on
  24873. context. See the individual commands for more information."
  24874. (org-return t))
  24875. (defun org-ctrl-c-minus ()
  24876. "Insert separator line in table or modify bullet type in list.
  24877. Calls `org-table-insert-hline' or `org-cycle-list-bullet',
  24878. depending on context."
  24879. (interactive)
  24880. (cond
  24881. ((org-at-table-p)
  24882. (call-interactively 'org-table-insert-hline))
  24883. ((org-on-heading-p)
  24884. ;; Convert to item
  24885. (save-excursion
  24886. (beginning-of-line 1)
  24887. (if (looking-at "\\*+ ")
  24888. (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
  24889. ((org-in-item-p)
  24890. (call-interactively 'org-cycle-list-bullet))
  24891. (t (error "`C-c -' does have no function here."))))
  24892. (defun org-meta-return (&optional arg)
  24893. "Insert a new heading or wrap a region in a table.
  24894. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  24895. See the individual commands for more information."
  24896. (interactive "P")
  24897. (cond
  24898. ((org-at-table-p)
  24899. (call-interactively 'org-table-wrap-region))
  24900. (t (call-interactively 'org-insert-heading))))
  24901. ;;; Menu entries
  24902. ;; Define the Org-mode menus
  24903. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  24904. '("Tbl"
  24905. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  24906. ["Next Field" org-cycle (org-at-table-p)]
  24907. ["Previous Field" org-shifttab (org-at-table-p)]
  24908. ["Next Row" org-return (org-at-table-p)]
  24909. "--"
  24910. ["Blank Field" org-table-blank-field (org-at-table-p)]
  24911. ["Edit Field" org-table-edit-field (org-at-table-p)]
  24912. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  24913. "--"
  24914. ("Column"
  24915. ["Move Column Left" org-metaleft (org-at-table-p)]
  24916. ["Move Column Right" org-metaright (org-at-table-p)]
  24917. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  24918. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  24919. ("Row"
  24920. ["Move Row Up" org-metaup (org-at-table-p)]
  24921. ["Move Row Down" org-metadown (org-at-table-p)]
  24922. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  24923. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  24924. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  24925. "--"
  24926. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  24927. ("Rectangle"
  24928. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  24929. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  24930. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  24931. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  24932. "--"
  24933. ("Calculate"
  24934. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  24935. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  24936. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  24937. "--"
  24938. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  24939. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  24940. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  24941. "--"
  24942. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  24943. "--"
  24944. ["Sum Column/Rectangle" org-table-sum
  24945. (or (org-at-table-p) (org-region-active-p))]
  24946. ["Which Column?" org-table-current-column (org-at-table-p)])
  24947. ["Debug Formulas"
  24948. org-table-toggle-formula-debugger
  24949. :style toggle :selected org-table-formula-debug]
  24950. ["Show Col/Row Numbers"
  24951. org-table-toggle-coordinate-overlays
  24952. :style toggle :selected org-table-overlay-coordinates]
  24953. "--"
  24954. ["Create" org-table-create (and (not (org-at-table-p))
  24955. org-enable-table-editor)]
  24956. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  24957. ["Import from File" org-table-import (not (org-at-table-p))]
  24958. ["Export to File" org-table-export (org-at-table-p)]
  24959. "--"
  24960. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  24961. (easy-menu-define org-org-menu org-mode-map "Org menu"
  24962. '("Org"
  24963. ("Show/Hide"
  24964. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  24965. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  24966. ["Sparse Tree" org-occur t]
  24967. ["Reveal Context" org-reveal t]
  24968. ["Show All" show-all t]
  24969. "--"
  24970. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  24971. "--"
  24972. ["New Heading" org-insert-heading t]
  24973. ("Navigate Headings"
  24974. ["Up" outline-up-heading t]
  24975. ["Next" outline-next-visible-heading t]
  24976. ["Previous" outline-previous-visible-heading t]
  24977. ["Next Same Level" outline-forward-same-level t]
  24978. ["Previous Same Level" outline-backward-same-level t]
  24979. "--"
  24980. ["Jump" org-goto t])
  24981. ("Edit Structure"
  24982. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  24983. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  24984. "--"
  24985. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  24986. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  24987. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  24988. "--"
  24989. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  24990. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  24991. ["Demote Heading" org-metaright (not (org-at-table-p))]
  24992. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  24993. "--"
  24994. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  24995. "--"
  24996. ["Convert to odd levels" org-convert-to-odd-levels t]
  24997. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  24998. ("Editing"
  24999. ["Emphasis..." org-emphasize t])
  25000. ("Archive"
  25001. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  25002. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  25003. ; :active t :keys "C-u C-c C-x C-a"]
  25004. ["Sparse trees open ARCHIVE trees"
  25005. (setq org-sparse-tree-open-archived-trees
  25006. (not org-sparse-tree-open-archived-trees))
  25007. :style toggle :selected org-sparse-tree-open-archived-trees]
  25008. ["Cycling opens ARCHIVE trees"
  25009. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  25010. :style toggle :selected org-cycle-open-archived-trees]
  25011. ["Agenda includes ARCHIVE trees"
  25012. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  25013. :style toggle :selected (not org-agenda-skip-archived-trees)]
  25014. "--"
  25015. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  25016. ; ["Check and Move Children" (org-archive-subtree '(4))
  25017. ; :active t :keys "C-u C-c C-x C-s"]
  25018. )
  25019. "--"
  25020. ("TODO Lists"
  25021. ["TODO/DONE/-" org-todo t]
  25022. ("Select keyword"
  25023. ["Next keyword" org-shiftright (org-on-heading-p)]
  25024. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  25025. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  25026. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  25027. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  25028. ["Show TODO Tree" org-show-todo-tree t]
  25029. ["Global TODO list" org-todo-list t]
  25030. "--"
  25031. ["Set Priority" org-priority t]
  25032. ["Priority Up" org-shiftup t]
  25033. ["Priority Down" org-shiftdown t])
  25034. ("TAGS and Properties"
  25035. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  25036. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  25037. "--"
  25038. ["Set property" 'org-set-property t]
  25039. ["Column view of properties" org-columns t]
  25040. ["Insert Column View DBlock" org-insert-columns-dblock t])
  25041. ("Dates and Scheduling"
  25042. ["Timestamp" org-time-stamp t]
  25043. ["Timestamp (inactive)" org-time-stamp-inactive t]
  25044. ("Change Date"
  25045. ["1 Day Later" org-shiftright t]
  25046. ["1 Day Earlier" org-shiftleft t]
  25047. ["1 ... Later" org-shiftup t]
  25048. ["1 ... Earlier" org-shiftdown t])
  25049. ["Compute Time Range" org-evaluate-time-range t]
  25050. ["Schedule Item" org-schedule t]
  25051. ["Deadline" org-deadline t]
  25052. "--"
  25053. ["Custom time format" org-toggle-time-stamp-overlays
  25054. :style radio :selected org-display-custom-times]
  25055. "--"
  25056. ["Goto Calendar" org-goto-calendar t]
  25057. ["Date from Calendar" org-date-from-calendar t])
  25058. ("Logging work"
  25059. ["Clock in" org-clock-in t]
  25060. ["Clock out" org-clock-out t]
  25061. ["Clock cancel" org-clock-cancel t]
  25062. ["Goto running clock" org-clock-goto t]
  25063. ["Display times" org-clock-display t]
  25064. ["Create clock table" org-clock-report t]
  25065. "--"
  25066. ["Record DONE time"
  25067. (progn (setq org-log-done (not org-log-done))
  25068. (message "Switching to %s will %s record a timestamp"
  25069. (car org-done-keywords)
  25070. (if org-log-done "automatically" "not")))
  25071. :style toggle :selected org-log-done])
  25072. "--"
  25073. ["Agenda Command..." org-agenda t]
  25074. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  25075. ("File List for Agenda")
  25076. ("Special views current file"
  25077. ["TODO Tree" org-show-todo-tree t]
  25078. ["Check Deadlines" org-check-deadlines t]
  25079. ["Timeline" org-timeline t]
  25080. ["Tags Tree" org-tags-sparse-tree t])
  25081. "--"
  25082. ("Hyperlinks"
  25083. ["Store Link (Global)" org-store-link t]
  25084. ["Insert Link" org-insert-link t]
  25085. ["Follow Link" org-open-at-point t]
  25086. "--"
  25087. ["Next link" org-next-link t]
  25088. ["Previous link" org-previous-link t]
  25089. "--"
  25090. ["Descriptive Links"
  25091. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  25092. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  25093. ["Literal Links"
  25094. (progn
  25095. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  25096. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  25097. "--"
  25098. ["Export/Publish..." org-export t]
  25099. ("LaTeX"
  25100. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  25101. :selected org-cdlatex-mode]
  25102. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  25103. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  25104. ["Modify math symbol" org-cdlatex-math-modify
  25105. (org-inside-LaTeX-fragment-p)]
  25106. ["Export LaTeX fragments as images"
  25107. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  25108. :style toggle :selected org-export-with-LaTeX-fragments])
  25109. "--"
  25110. ("Documentation"
  25111. ["Show Version" org-version t]
  25112. ["Info Documentation" org-info t])
  25113. ("Customize"
  25114. ["Browse Org Group" org-customize t]
  25115. "--"
  25116. ["Expand This Menu" org-create-customize-menu
  25117. (fboundp 'customize-menu-create)])
  25118. "--"
  25119. ["Refresh setup" org-mode-restart t]
  25120. ))
  25121. (defun org-info (&optional node)
  25122. "Read documentation for Org-mode in the info system.
  25123. With optional NODE, go directly to that node."
  25124. (interactive)
  25125. (require 'info)
  25126. (Info-goto-node (format "(org)%s" (or node ""))))
  25127. (defun org-install-agenda-files-menu ()
  25128. (let ((bl (buffer-list)))
  25129. (save-excursion
  25130. (while bl
  25131. (set-buffer (pop bl))
  25132. (if (org-mode-p) (setq bl nil)))
  25133. (when (org-mode-p)
  25134. (easy-menu-change
  25135. '("Org") "File List for Agenda"
  25136. (append
  25137. (list
  25138. ["Edit File List" (org-edit-agenda-file-list) t]
  25139. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  25140. ["Remove Current File from List" org-remove-file t]
  25141. ["Cycle through agenda files" org-cycle-agenda-files t]
  25142. ["Occur in all agenda files" org-occur-in-agenda-files t]
  25143. "--")
  25144. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  25145. ;;;; Documentation
  25146. (defun org-customize ()
  25147. "Call the customize function with org as argument."
  25148. (interactive)
  25149. (customize-browse 'org))
  25150. (defun org-create-customize-menu ()
  25151. "Create a full customization menu for Org-mode, insert it into the menu."
  25152. (interactive)
  25153. (if (fboundp 'customize-menu-create)
  25154. (progn
  25155. (easy-menu-change
  25156. '("Org") "Customize"
  25157. `(["Browse Org group" org-customize t]
  25158. "--"
  25159. ,(customize-menu-create 'org)
  25160. ["Set" Custom-set t]
  25161. ["Save" Custom-save t]
  25162. ["Reset to Current" Custom-reset-current t]
  25163. ["Reset to Saved" Custom-reset-saved t]
  25164. ["Reset to Standard Settings" Custom-reset-standard t]))
  25165. (message "\"Org\"-menu now contains full customization menu"))
  25166. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  25167. ;;;; Miscellaneous stuff
  25168. ;;; Generally useful functions
  25169. (defun org-context ()
  25170. "Return a list of contexts of the current cursor position.
  25171. If several contexts apply, all are returned.
  25172. Each context entry is a list with a symbol naming the context, and
  25173. two positions indicating start and end of the context. Possible
  25174. contexts are:
  25175. :headline anywhere in a headline
  25176. :headline-stars on the leading stars in a headline
  25177. :todo-keyword on a TODO keyword (including DONE) in a headline
  25178. :tags on the TAGS in a headline
  25179. :priority on the priority cookie in a headline
  25180. :item on the first line of a plain list item
  25181. :item-bullet on the bullet/number of a plain list item
  25182. :checkbox on the checkbox in a plain list item
  25183. :table in an org-mode table
  25184. :table-special on a special filed in a table
  25185. :table-table in a table.el table
  25186. :link on a hyperlink
  25187. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  25188. :target on a <<target>>
  25189. :radio-target on a <<<radio-target>>>
  25190. :latex-fragment on a LaTeX fragment
  25191. :latex-preview on a LaTeX fragment with overlayed preview image
  25192. This function expects the position to be visible because it uses font-lock
  25193. faces as a help to recognize the following contexts: :table-special, :link,
  25194. and :keyword."
  25195. (let* ((f (get-text-property (point) 'face))
  25196. (faces (if (listp f) f (list f)))
  25197. (p (point)) clist o)
  25198. ;; First the large context
  25199. (cond
  25200. ((org-on-heading-p t)
  25201. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  25202. (when (progn
  25203. (beginning-of-line 1)
  25204. (looking-at org-todo-line-tags-regexp))
  25205. (push (org-point-in-group p 1 :headline-stars) clist)
  25206. (push (org-point-in-group p 2 :todo-keyword) clist)
  25207. (push (org-point-in-group p 4 :tags) clist))
  25208. (goto-char p)
  25209. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  25210. (if (looking-at "\\[#[A-Z0-9]\\]")
  25211. (push (org-point-in-group p 0 :priority) clist)))
  25212. ((org-at-item-p)
  25213. (push (org-point-in-group p 2 :item-bullet) clist)
  25214. (push (list :item (point-at-bol)
  25215. (save-excursion (org-end-of-item) (point)))
  25216. clist)
  25217. (and (org-at-item-checkbox-p)
  25218. (push (org-point-in-group p 0 :checkbox) clist)))
  25219. ((org-at-table-p)
  25220. (push (list :table (org-table-begin) (org-table-end)) clist)
  25221. (if (memq 'org-formula faces)
  25222. (push (list :table-special
  25223. (previous-single-property-change p 'face)
  25224. (next-single-property-change p 'face)) clist)))
  25225. ((org-at-table-p 'any)
  25226. (push (list :table-table) clist)))
  25227. (goto-char p)
  25228. ;; Now the small context
  25229. (cond
  25230. ((org-at-timestamp-p)
  25231. (push (org-point-in-group p 0 :timestamp) clist))
  25232. ((memq 'org-link faces)
  25233. (push (list :link
  25234. (previous-single-property-change p 'face)
  25235. (next-single-property-change p 'face)) clist))
  25236. ((memq 'org-special-keyword faces)
  25237. (push (list :keyword
  25238. (previous-single-property-change p 'face)
  25239. (next-single-property-change p 'face)) clist))
  25240. ((org-on-target-p)
  25241. (push (org-point-in-group p 0 :target) clist)
  25242. (goto-char (1- (match-beginning 0)))
  25243. (if (looking-at org-radio-target-regexp)
  25244. (push (org-point-in-group p 0 :radio-target) clist))
  25245. (goto-char p))
  25246. ((setq o (car (delq nil
  25247. (mapcar
  25248. (lambda (x)
  25249. (if (memq x org-latex-fragment-image-overlays) x))
  25250. (org-overlays-at (point))))))
  25251. (push (list :latex-fragment
  25252. (org-overlay-start o) (org-overlay-end o)) clist)
  25253. (push (list :latex-preview
  25254. (org-overlay-start o) (org-overlay-end o)) clist))
  25255. ((org-inside-LaTeX-fragment-p)
  25256. ;; FIXME: positions wrong.
  25257. (push (list :latex-fragment (point) (point)) clist)))
  25258. (setq clist (nreverse (delq nil clist)))
  25259. clist))
  25260. ;; FIXME: Compare with at-regexp-p Do we need both?
  25261. (defun org-in-regexp (re &optional nlines visually)
  25262. "Check if point is inside a match of regexp.
  25263. Normally only the current line is checked, but you can include NLINES extra
  25264. lines both before and after point into the search.
  25265. If VISUALLY is set, require that the cursor is not after the match but
  25266. really on, so that the block visually is on the match."
  25267. (catch 'exit
  25268. (let ((pos (point))
  25269. (eol (point-at-eol (+ 1 (or nlines 0))))
  25270. (inc (if visually 1 0)))
  25271. (save-excursion
  25272. (beginning-of-line (- 1 (or nlines 0)))
  25273. (while (re-search-forward re eol t)
  25274. (if (and (<= (match-beginning 0) pos)
  25275. (>= (+ inc (match-end 0)) pos))
  25276. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  25277. (defun org-at-regexp-p (regexp)
  25278. "Is point inside a match of REGEXP in the current line?"
  25279. (catch 'exit
  25280. (save-excursion
  25281. (let ((pos (point)) (end (point-at-eol)))
  25282. (beginning-of-line 1)
  25283. (while (re-search-forward regexp end t)
  25284. (if (and (<= (match-beginning 0) pos)
  25285. (>= (match-end 0) pos))
  25286. (throw 'exit t)))
  25287. nil))))
  25288. (defun org-occur-in-agenda-files (regexp &optional nlines)
  25289. "Call `multi-occur' with buffers for all agenda files."
  25290. (interactive "sOrg-files matching: \np")
  25291. (let* ((files (org-agenda-files))
  25292. (tnames (mapcar 'file-truename files))
  25293. (extra org-agenda-multi-occur-extra-files)
  25294. f)
  25295. (while (setq f (pop extra))
  25296. (unless (member (file-truename f) tnames)
  25297. (add-to-list 'files f 'append)
  25298. (add-to-list 'tnames (file-truename f) 'append)))
  25299. (multi-occur
  25300. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  25301. regexp)))
  25302. (if (boundp 'occur-mode-find-occurrence-hook)
  25303. ;; Emacs 23
  25304. (add-hook 'occur-mode-find-occurrence-hook
  25305. (lambda ()
  25306. (when (org-mode-p)
  25307. (org-reveal))))
  25308. ;; Emacs 22
  25309. (defadvice occur-mode-goto-occurrence
  25310. (after org-occur-reveal activate)
  25311. (and (org-mode-p) (org-reveal)))
  25312. (defadvice occur-mode-goto-occurrence-other-window
  25313. (after org-occur-reveal activate)
  25314. (and (org-mode-p) (org-reveal)))
  25315. (defadvice occur-mode-display-occurrence
  25316. (after org-occur-reveal activate)
  25317. (when (org-mode-p)
  25318. (let ((pos (occur-mode-find-occurrence)))
  25319. (with-current-buffer (marker-buffer pos)
  25320. (save-excursion
  25321. (goto-char pos)
  25322. (org-reveal)))))))
  25323. (defun org-uniquify (list)
  25324. "Remove duplicate elements from LIST."
  25325. (let (res)
  25326. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  25327. res))
  25328. (defun org-delete-all (elts list)
  25329. "Remove all elements in ELTS from LIST."
  25330. (while elts
  25331. (setq list (delete (pop elts) list)))
  25332. list)
  25333. (defun org-back-over-empty-lines ()
  25334. "Move backwards over witespace, to the beginning of the first empty line.
  25335. Returns the number o empty lines passed."
  25336. (let ((pos (point)))
  25337. (skip-chars-backward " \t\n\r")
  25338. (beginning-of-line 2)
  25339. (goto-char (min (point) pos))
  25340. (count-lines (point) pos)))
  25341. (defun org-skip-whitespace ()
  25342. (skip-chars-forward " \t\n\r"))
  25343. (defun org-point-in-group (point group &optional context)
  25344. "Check if POINT is in match-group GROUP.
  25345. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  25346. match. If the match group does ot exist or point is not inside it,
  25347. return nil."
  25348. (and (match-beginning group)
  25349. (>= point (match-beginning group))
  25350. (<= point (match-end group))
  25351. (if context
  25352. (list context (match-beginning group) (match-end group))
  25353. t)))
  25354. (defun org-switch-to-buffer-other-window (&rest args)
  25355. "Switch to buffer in a second window on the current frame.
  25356. In particular, do not allow pop-up frames."
  25357. (let (pop-up-frames special-display-buffer-names special-display-regexps
  25358. special-display-function)
  25359. (apply 'switch-to-buffer-other-window args)))
  25360. (defun org-combine-plists (&rest plists)
  25361. "Create a single property list from all plists in PLISTS.
  25362. The process starts by copying the first list, and then setting properties
  25363. from the other lists. Settings in the last list are the most significant
  25364. ones and overrule settings in the other lists."
  25365. (let ((rtn (copy-sequence (pop plists)))
  25366. p v ls)
  25367. (while plists
  25368. (setq ls (pop plists))
  25369. (while ls
  25370. (setq p (pop ls) v (pop ls))
  25371. (setq rtn (plist-put rtn p v))))
  25372. rtn))
  25373. (defun org-move-line-down (arg)
  25374. "Move the current line down. With prefix argument, move it past ARG lines."
  25375. (interactive "p")
  25376. (let ((col (current-column))
  25377. beg end pos)
  25378. (beginning-of-line 1) (setq beg (point))
  25379. (beginning-of-line 2) (setq end (point))
  25380. (beginning-of-line (+ 1 arg))
  25381. (setq pos (move-marker (make-marker) (point)))
  25382. (insert (delete-and-extract-region beg end))
  25383. (goto-char pos)
  25384. (move-to-column col)))
  25385. (defun org-move-line-up (arg)
  25386. "Move the current line up. With prefix argument, move it past ARG lines."
  25387. (interactive "p")
  25388. (let ((col (current-column))
  25389. beg end pos)
  25390. (beginning-of-line 1) (setq beg (point))
  25391. (beginning-of-line 2) (setq end (point))
  25392. (beginning-of-line (- arg))
  25393. (setq pos (move-marker (make-marker) (point)))
  25394. (insert (delete-and-extract-region beg end))
  25395. (goto-char pos)
  25396. (move-to-column col)))
  25397. (defun org-replace-escapes (string table)
  25398. "Replace %-escapes in STRING with values in TABLE.
  25399. TABLE is an association list with keys like \"%a\" and string values.
  25400. The sequences in STRING may contain normal field width and padding information,
  25401. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  25402. so values can contain further %-escapes if they are define later in TABLE."
  25403. (let ((case-fold-search nil)
  25404. e re rpl)
  25405. (while (setq e (pop table))
  25406. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  25407. (while (string-match re string)
  25408. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  25409. (cdr e)))
  25410. (setq string (replace-match rpl t t string))))
  25411. string))
  25412. (defun org-sublist (list start end)
  25413. "Return a section of LIST, from START to END.
  25414. Counting starts at 1."
  25415. (let (rtn (c start))
  25416. (setq list (nthcdr (1- start) list))
  25417. (while (and list (<= c end))
  25418. (push (pop list) rtn)
  25419. (setq c (1+ c)))
  25420. (nreverse rtn)))
  25421. (defun org-find-base-buffer-visiting (file)
  25422. "Like `find-buffer-visiting' but alway return the base buffer and
  25423. not an indirect buffer"
  25424. (let ((buf (find-buffer-visiting file)))
  25425. (if buf
  25426. (or (buffer-base-buffer buf) buf)
  25427. nil)))
  25428. (defun org-image-file-name-regexp ()
  25429. "Return regexp matching the file names of images."
  25430. (if (fboundp 'image-file-name-regexp)
  25431. (image-file-name-regexp)
  25432. (let ((image-file-name-extensions
  25433. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  25434. "xbm" "xpm" "pbm" "pgm" "ppm")))
  25435. (concat "\\."
  25436. (regexp-opt (nconc (mapcar 'upcase
  25437. image-file-name-extensions)
  25438. image-file-name-extensions)
  25439. t)
  25440. "\\'"))))
  25441. (defun org-file-image-p (file)
  25442. "Return non-nil if FILE is an image."
  25443. (save-match-data
  25444. (string-match (org-image-file-name-regexp) file)))
  25445. ;;; Paragraph filling stuff.
  25446. ;; We want this to be just right, so use the full arsenal.
  25447. (defun org-indent-line-function ()
  25448. "Indent line like previous, but further if previous was headline or item."
  25449. (interactive)
  25450. (let* ((pos (point))
  25451. (itemp (org-at-item-p))
  25452. column bpos bcol tpos tcol bullet btype bullet-type)
  25453. ;; Find the previous relevant line
  25454. (beginning-of-line 1)
  25455. (cond
  25456. ((looking-at "#") (setq column 0))
  25457. ((looking-at "\\*+ ") (setq column 0))
  25458. (t
  25459. (beginning-of-line 0)
  25460. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  25461. (beginning-of-line 0))
  25462. (cond
  25463. ((looking-at "\\*+[ \t]+")
  25464. (goto-char (match-end 0))
  25465. (setq column (current-column)))
  25466. ((org-in-item-p)
  25467. (org-beginning-of-item)
  25468. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25469. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  25470. (setq bpos (match-beginning 1) tpos (match-end 0)
  25471. bcol (progn (goto-char bpos) (current-column))
  25472. tcol (progn (goto-char tpos) (current-column))
  25473. bullet (match-string 1)
  25474. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  25475. (if (not itemp)
  25476. (setq column tcol)
  25477. (goto-char pos)
  25478. (beginning-of-line 1)
  25479. (if (looking-at "\\S-")
  25480. (progn
  25481. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  25482. (setq bullet (match-string 1)
  25483. btype (if (string-match "[0-9]" bullet) "n" bullet))
  25484. (setq column (if (equal btype bullet-type) bcol tcol)))
  25485. (setq column (org-get-indentation)))))
  25486. (t (setq column (org-get-indentation))))))
  25487. (goto-char pos)
  25488. (if (<= (current-column) (current-indentation))
  25489. (indent-line-to column)
  25490. (save-excursion (indent-line-to column)))
  25491. (setq column (current-column))
  25492. (beginning-of-line 1)
  25493. (if (looking-at
  25494. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  25495. (replace-match (concat "\\1" (format org-property-format
  25496. (match-string 2) (match-string 3)))
  25497. t nil))
  25498. (move-to-column column)))
  25499. (defun org-set-autofill-regexps ()
  25500. (interactive)
  25501. ;; In the paragraph separator we include headlines, because filling
  25502. ;; text in a line directly attached to a headline would otherwise
  25503. ;; fill the headline as well.
  25504. (org-set-local 'comment-start-skip "^#+[ \t]*")
  25505. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  25506. ;; The paragraph starter includes hand-formatted lists.
  25507. (org-set-local 'paragraph-start
  25508. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  25509. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  25510. ;; But only if the user has not turned off tables or fixed-width regions
  25511. (org-set-local
  25512. 'auto-fill-inhibit-regexp
  25513. (concat "\\*+ \\|#\\+"
  25514. "\\|[ \t]*" org-keyword-time-regexp
  25515. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  25516. (concat
  25517. "\\|[ \t]*["
  25518. (if org-enable-table-editor "|" "")
  25519. (if org-enable-fixed-width-editor ":" "")
  25520. "]"))))
  25521. ;; We use our own fill-paragraph function, to make sure that tables
  25522. ;; and fixed-width regions are not wrapped. That function will pass
  25523. ;; through to `fill-paragraph' when appropriate.
  25524. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  25525. ; Adaptive filling: To get full control, first make sure that
  25526. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  25527. (org-set-local 'adaptive-fill-regexp "\000")
  25528. (org-set-local 'adaptive-fill-function
  25529. 'org-adaptive-fill-function)
  25530. (org-set-local
  25531. 'align-mode-rules-list
  25532. '((org-in-buffer-settings
  25533. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  25534. (modes . '(org-mode))))))
  25535. (defun org-fill-paragraph (&optional justify)
  25536. "Re-align a table, pass through to fill-paragraph if no table."
  25537. (let ((table-p (org-at-table-p))
  25538. (table.el-p (org-at-table.el-p)))
  25539. (cond ((and (equal (char-after (point-at-bol)) ?*)
  25540. (save-excursion (goto-char (point-at-bol))
  25541. (looking-at outline-regexp)))
  25542. t) ; skip headlines
  25543. (table.el-p t) ; skip table.el tables
  25544. (table-p (org-table-align) t) ; align org-mode tables
  25545. (t nil)))) ; call paragraph-fill
  25546. ;; For reference, this is the default value of adaptive-fill-regexp
  25547. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  25548. (defun org-adaptive-fill-function ()
  25549. "Return a fill prefix for org-mode files.
  25550. In particular, this makes sure hanging paragraphs for hand-formatted lists
  25551. work correctly."
  25552. (cond ((looking-at "#[ \t]+")
  25553. (match-string 0))
  25554. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  25555. (save-excursion
  25556. (goto-char (match-end 0))
  25557. (make-string (current-column) ?\ )))
  25558. (t nil)))
  25559. ;;;; Functions extending outline functionality
  25560. (defun org-beginning-of-line (&optional arg)
  25561. "Go to the beginning of the current line. If that is invisible, continue
  25562. to a visible line beginning. This makes the function of C-a more intuitive.
  25563. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25564. first attempt, and only move to after the tags when the cursor is already
  25565. beyond the end of the headline."
  25566. (interactive "P")
  25567. (let ((pos (point)))
  25568. (beginning-of-line 1)
  25569. (if (bobp)
  25570. nil
  25571. (backward-char 1)
  25572. (if (org-invisible-p)
  25573. (while (and (not (bobp)) (org-invisible-p))
  25574. (backward-char 1)
  25575. (beginning-of-line 1))
  25576. (forward-char 1)))
  25577. (when org-special-ctrl-a/e
  25578. (cond
  25579. ((and (looking-at org-todo-line-regexp)
  25580. (= (char-after (match-end 1)) ?\ ))
  25581. (goto-char
  25582. (if (eq org-special-ctrl-a/e t)
  25583. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  25584. ((= pos (point)) (match-beginning 3))
  25585. (t (point)))
  25586. (cond ((> pos (point)) (point))
  25587. ((not (eq last-command this-command)) (point))
  25588. (t (match-beginning 3))))))
  25589. ((org-at-item-p)
  25590. (goto-char
  25591. (if (eq org-special-ctrl-a/e t)
  25592. (cond ((> pos (match-end 4)) (match-end 4))
  25593. ((= pos (point)) (match-end 4))
  25594. (t (point)))
  25595. (cond ((> pos (point)) (point))
  25596. ((not (eq last-command this-command)) (point))
  25597. (t (match-end 4))))))))))
  25598. (defun org-end-of-line (&optional arg)
  25599. "Go to the end of the line.
  25600. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  25601. first attempt, and only move to after the tags when the cursor is already
  25602. beyond the end of the headline."
  25603. (interactive "P")
  25604. (if (or (not org-special-ctrl-a/e)
  25605. (not (org-on-heading-p)))
  25606. (end-of-line arg)
  25607. (let ((pos (point)))
  25608. (beginning-of-line 1)
  25609. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  25610. (if (eq org-special-ctrl-a/e t)
  25611. (if (or (< pos (match-beginning 1))
  25612. (= pos (match-end 0)))
  25613. (goto-char (match-beginning 1))
  25614. (goto-char (match-end 0)))
  25615. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  25616. (goto-char (match-end 0))
  25617. (goto-char (match-beginning 1))))
  25618. (end-of-line arg)))))
  25619. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  25620. (define-key org-mode-map "\C-e" 'org-end-of-line)
  25621. (defun org-kill-line (&optional arg)
  25622. "Kill line, to tags or end of line."
  25623. (interactive "P")
  25624. (cond
  25625. ((or (not org-special-ctrl-k)
  25626. (bolp)
  25627. (not (org-on-heading-p)))
  25628. (call-interactively 'kill-line))
  25629. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  25630. (kill-region (point) (match-beginning 1))
  25631. (org-set-tags nil t))
  25632. (t (kill-region (point) (point-at-eol)))))
  25633. (define-key org-mode-map "\C-k" 'org-kill-line)
  25634. (defun org-invisible-p ()
  25635. "Check if point is at a character currently not visible."
  25636. ;; Early versions of noutline don't have `outline-invisible-p'.
  25637. (if (fboundp 'outline-invisible-p)
  25638. (outline-invisible-p)
  25639. (get-char-property (point) 'invisible)))
  25640. (defun org-invisible-p2 ()
  25641. "Check if point is at a character currently not visible."
  25642. (save-excursion
  25643. (if (and (eolp) (not (bobp))) (backward-char 1))
  25644. ;; Early versions of noutline don't have `outline-invisible-p'.
  25645. (if (fboundp 'outline-invisible-p)
  25646. (outline-invisible-p)
  25647. (get-char-property (point) 'invisible))))
  25648. (defalias 'org-back-to-heading 'outline-back-to-heading)
  25649. (defalias 'org-on-heading-p 'outline-on-heading-p)
  25650. (defalias 'org-at-heading-p 'outline-on-heading-p)
  25651. (defun org-at-heading-or-item-p ()
  25652. (or (org-on-heading-p) (org-at-item-p)))
  25653. (defun org-on-target-p ()
  25654. (or (org-in-regexp org-radio-target-regexp)
  25655. (org-in-regexp org-target-regexp)))
  25656. (defun org-up-heading-all (arg)
  25657. "Move to the heading line of which the present line is a subheading.
  25658. This function considers both visible and invisible heading lines.
  25659. With argument, move up ARG levels."
  25660. (if (fboundp 'outline-up-heading-all)
  25661. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  25662. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  25663. (defun org-up-heading-safe ()
  25664. "Move to the heading line of which the present line is a subheading.
  25665. This version will not throw an error. It will return the level of the
  25666. headline found, or nil if no higher level is found."
  25667. (let ((pos (point)) start-level level
  25668. (re (concat "^" outline-regexp)))
  25669. (catch 'exit
  25670. (outline-back-to-heading t)
  25671. (setq start-level (funcall outline-level))
  25672. (if (equal start-level 1) (throw 'exit nil))
  25673. (while (re-search-backward re nil t)
  25674. (setq level (funcall outline-level))
  25675. (if (< level start-level) (throw 'exit level)))
  25676. nil)))
  25677. (defun org-first-sibling-p ()
  25678. "Is this heading the first child of its parents?"
  25679. (interactive)
  25680. (let ((re (concat "^" outline-regexp))
  25681. level l)
  25682. (unless (org-at-heading-p t)
  25683. (error "Not at a heading"))
  25684. (setq level (funcall outline-level))
  25685. (save-excursion
  25686. (if (not (re-search-backward re nil t))
  25687. t
  25688. (setq l (funcall outline-level))
  25689. (< l level)))))
  25690. (defun org-goto-sibling (&optional previous)
  25691. "Goto the next sibling, even if it is invisible.
  25692. When PREVIOUS is set, go to the previous sibling instead. Returns t
  25693. when a sibling was found. When none is found, return nil and don't
  25694. move point."
  25695. (let ((fun (if previous 're-search-backward 're-search-forward))
  25696. (pos (point))
  25697. (re (concat "^" outline-regexp))
  25698. level l)
  25699. (when (condition-case nil (org-back-to-heading t) (error nil))
  25700. (setq level (funcall outline-level))
  25701. (catch 'exit
  25702. (or previous (forward-char 1))
  25703. (while (funcall fun re nil t)
  25704. (setq l (funcall outline-level))
  25705. (when (< l level) (goto-char pos) (throw 'exit nil))
  25706. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  25707. (goto-char pos)
  25708. nil))))
  25709. (defun org-show-siblings ()
  25710. "Show all siblings of the current headline."
  25711. (save-excursion
  25712. (while (org-goto-sibling) (org-flag-heading nil)))
  25713. (save-excursion
  25714. (while (org-goto-sibling 'previous)
  25715. (org-flag-heading nil))))
  25716. (defun org-show-hidden-entry ()
  25717. "Show an entry where even the heading is hidden."
  25718. (save-excursion
  25719. (org-show-entry)))
  25720. (defun org-flag-heading (flag &optional entry)
  25721. "Flag the current heading. FLAG non-nil means make invisible.
  25722. When ENTRY is non-nil, show the entire entry."
  25723. (save-excursion
  25724. (org-back-to-heading t)
  25725. ;; Check if we should show the entire entry
  25726. (if entry
  25727. (progn
  25728. (org-show-entry)
  25729. (save-excursion
  25730. (and (outline-next-heading)
  25731. (org-flag-heading nil))))
  25732. (outline-flag-region (max (point-min) (1- (point)))
  25733. (save-excursion (outline-end-of-heading) (point))
  25734. flag))))
  25735. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  25736. ;; This is an exact copy of the original function, but it uses
  25737. ;; `org-back-to-heading', to make it work also in invisible
  25738. ;; trees. And is uses an invisible-OK argument.
  25739. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  25740. (org-back-to-heading invisible-OK)
  25741. (let ((first t)
  25742. (level (funcall outline-level)))
  25743. (while (and (not (eobp))
  25744. (or first (> (funcall outline-level) level)))
  25745. (setq first nil)
  25746. (outline-next-heading))
  25747. (unless to-heading
  25748. (if (memq (preceding-char) '(?\n ?\^M))
  25749. (progn
  25750. ;; Go to end of line before heading
  25751. (forward-char -1)
  25752. (if (memq (preceding-char) '(?\n ?\^M))
  25753. ;; leave blank line before heading
  25754. (forward-char -1))))))
  25755. (point))
  25756. (defun org-show-subtree ()
  25757. "Show everything after this heading at deeper levels."
  25758. (outline-flag-region
  25759. (point)
  25760. (save-excursion
  25761. (outline-end-of-subtree) (outline-next-heading) (point))
  25762. nil))
  25763. (defun org-show-entry ()
  25764. "Show the body directly following this heading.
  25765. Show the heading too, if it is currently invisible."
  25766. (interactive)
  25767. (save-excursion
  25768. (condition-case nil
  25769. (progn
  25770. (org-back-to-heading t)
  25771. (outline-flag-region
  25772. (max (point-min) (1- (point)))
  25773. (save-excursion
  25774. (re-search-forward
  25775. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  25776. (or (match-beginning 1) (point-max)))
  25777. nil))
  25778. (error nil))))
  25779. (defun org-make-options-regexp (kwds)
  25780. "Make a regular expression for keyword lines."
  25781. (concat
  25782. "^"
  25783. "#?[ \t]*\\+\\("
  25784. (mapconcat 'regexp-quote kwds "\\|")
  25785. "\\):[ \t]*"
  25786. "\\(.+\\)"))
  25787. ;; Make isearch reveal the necessary context
  25788. (defun org-isearch-end ()
  25789. "Reveal context after isearch exits."
  25790. (when isearch-success ; only if search was successful
  25791. (if (featurep 'xemacs)
  25792. ;; Under XEmacs, the hook is run in the correct place,
  25793. ;; we directly show the context.
  25794. (org-show-context 'isearch)
  25795. ;; In Emacs the hook runs *before* restoring the overlays.
  25796. ;; So we have to use a one-time post-command-hook to do this.
  25797. ;; (Emacs 22 has a special variable, see function `org-mode')
  25798. (unless (and (boundp 'isearch-mode-end-hook-quit)
  25799. isearch-mode-end-hook-quit)
  25800. ;; Only when the isearch was not quitted.
  25801. (org-add-hook 'post-command-hook 'org-isearch-post-command
  25802. 'append 'local)))))
  25803. (defun org-isearch-post-command ()
  25804. "Remove self from hook, and show context."
  25805. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  25806. (org-show-context 'isearch))
  25807. ;;;; Integration with and fixes for other packages
  25808. ;;; Imenu support
  25809. (defvar org-imenu-markers nil
  25810. "All markers currently used by Imenu.")
  25811. (make-variable-buffer-local 'org-imenu-markers)
  25812. (defun org-imenu-new-marker (&optional pos)
  25813. "Return a new marker for use by Imenu, and remember the marker."
  25814. (let ((m (make-marker)))
  25815. (move-marker m (or pos (point)))
  25816. (push m org-imenu-markers)
  25817. m))
  25818. (defun org-imenu-get-tree ()
  25819. "Produce the index for Imenu."
  25820. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  25821. (setq org-imenu-markers nil)
  25822. (let* ((n org-imenu-depth)
  25823. (re (concat "^" outline-regexp))
  25824. (subs (make-vector (1+ n) nil))
  25825. (last-level 0)
  25826. m tree level head)
  25827. (save-excursion
  25828. (save-restriction
  25829. (widen)
  25830. (goto-char (point-max))
  25831. (while (re-search-backward re nil t)
  25832. (setq level (org-reduced-level (funcall outline-level)))
  25833. (when (<= level n)
  25834. (looking-at org-complex-heading-regexp)
  25835. (setq head (org-match-string-no-properties 4)
  25836. m (org-imenu-new-marker))
  25837. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  25838. (if (>= level last-level)
  25839. (push (cons head m) (aref subs level))
  25840. (push (cons head (aref subs (1+ level))) (aref subs level))
  25841. (loop for i from (1+ level) to n do (aset subs i nil)))
  25842. (setq last-level level)))))
  25843. (aref subs 1)))
  25844. (eval-after-load "imenu"
  25845. '(progn
  25846. (add-hook 'imenu-after-jump-hook
  25847. (lambda () (org-show-context 'org-goto)))))
  25848. ;; Speedbar support
  25849. (defun org-speedbar-set-agenda-restriction ()
  25850. "Restrict future agenda commands to the location at point in speedbar.
  25851. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  25852. (interactive)
  25853. (let (p m tp np dir txt w)
  25854. (cond
  25855. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  25856. 'org-imenu t))
  25857. (setq m (get-text-property p 'org-imenu-marker))
  25858. (save-excursion
  25859. (save-restriction
  25860. (set-buffer (marker-buffer m))
  25861. (goto-char m)
  25862. (org-agenda-set-restriction-lock 'subtree))))
  25863. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  25864. 'speedbar-function 'speedbar-find-file))
  25865. (setq tp (previous-single-property-change
  25866. (1+ p) 'speedbar-function)
  25867. np (next-single-property-change
  25868. tp 'speedbar-function)
  25869. dir (speedbar-line-directory)
  25870. txt (buffer-substring-no-properties (or tp (point-min))
  25871. (or np (point-max))))
  25872. (save-excursion
  25873. (save-restriction
  25874. (set-buffer (find-file-noselect
  25875. (let ((default-directory dir))
  25876. (expand-file-name txt))))
  25877. (unless (org-mode-p)
  25878. (error "Cannot restrict to non-Org-mode file"))
  25879. (org-agenda-set-restriction-lock 'file))))
  25880. (t (error "Don't know how to restrict Org-mode's agenda")))
  25881. (org-move-overlay org-speedbar-restriction-lock-overlay
  25882. (point-at-bol) (point-at-eol))
  25883. (setq current-prefix-arg nil)
  25884. (org-agenda-maybe-redo)))
  25885. (eval-after-load "speedbar"
  25886. '(progn
  25887. (speedbar-add-supported-extension ".org")
  25888. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  25889. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  25890. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  25891. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  25892. (add-hook 'speedbar-visiting-tag-hook
  25893. (lambda () (org-show-context 'org-goto)))))
  25894. ;;; Fixes and Hacks
  25895. ;; Make flyspell not check words in links, to not mess up our keymap
  25896. (defun org-mode-flyspell-verify ()
  25897. "Don't let flyspell put overlays at active buttons."
  25898. (not (get-text-property (point) 'keymap)))
  25899. ;; Make `bookmark-jump' show the jump location if it was hidden.
  25900. (eval-after-load "bookmark"
  25901. '(if (boundp 'bookmark-after-jump-hook)
  25902. ;; We can use the hook
  25903. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  25904. ;; Hook not available, use advice
  25905. (defadvice bookmark-jump (after org-make-visible activate)
  25906. "Make the position visible."
  25907. (org-bookmark-jump-unhide))))
  25908. (defun org-bookmark-jump-unhide ()
  25909. "Unhide the current position, to show the bookmark location."
  25910. (and (org-mode-p)
  25911. (or (org-invisible-p)
  25912. (save-excursion (goto-char (max (point-min) (1- (point))))
  25913. (org-invisible-p)))
  25914. (org-show-context 'bookmark-jump)))
  25915. ;; Fix a bug in htmlize where there are text properties (face nil)
  25916. (eval-after-load "htmlize"
  25917. '(progn
  25918. (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
  25919. "Make sure there are no nil faces"
  25920. (setq ad-return-value (delq nil ad-return-value)))))
  25921. ;; Make session.el ignore our circular variable
  25922. (eval-after-load "session"
  25923. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  25924. ;;;; Experimental code
  25925. (defun org-closed-in-range ()
  25926. "Sparse tree of items closed in a certain time range.
  25927. Still experimental, may disappear in the future."
  25928. (interactive)
  25929. ;; Get the time interval from the user.
  25930. (let* ((time1 (time-to-seconds
  25931. (org-read-date nil 'to-time nil "Starting date: ")))
  25932. (time2 (time-to-seconds
  25933. (org-read-date nil 'to-time nil "End date:")))
  25934. ;; callback function
  25935. (callback (lambda ()
  25936. (let ((time
  25937. (time-to-seconds
  25938. (apply 'encode-time
  25939. (org-parse-time-string
  25940. (match-string 1))))))
  25941. ;; check if time in interval
  25942. (and (>= time time1) (<= time time2))))))
  25943. ;; make tree, check each match with the callback
  25944. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  25945. ;(let ((org-refile-targets org-refile-targets)
  25946. ; (org-refile-use-outline-path org-refile-use-outline-path))
  25947. ; (when (equal just-goto '(16))
  25948. ; (setq org-refile-targets '((nil . (:maxlevel . 10))))
  25949. ; (setq org-refile-use-outline-path t))
  25950. ; (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  25951. ;;;; Finish up
  25952. (provide 'org)
  25953. (run-hooks 'org-load-hook)
  25954. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  25955. ;;; org.el ends here